#include <mono/metadata/image.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/tokentype.h>
-#include <mono/metadata/cil-coff.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/loader.h>
* with the appropriate arguments, then return NULL to report the failure. The error
* should be propagated until it reaches code which can throw managed exceptions. At that
* point, an exception should be thrown based on the information returned by
- * mono_loader_get_error (). Then the error should be cleared by calling
+ * mono_loader_get_last_error (). Then the error should be cleared by calling
* mono_loader_clear_error ().
*/
return;
error = g_new0 (MonoLoaderError, 1);
- error->kind = MONO_LOADER_ERROR_ASSEMBLY;
+ error->exception_type = MONO_EXCEPTION_FILE_NOT_FOUND;
error->assembly_name = g_strdup (assembly_name);
error->ref_only = ref_only;
return;
error = g_new0 (MonoLoaderError, 1);
- error->kind = MONO_LOADER_ERROR_TYPE;
+ error->exception_type = MONO_EXCEPTION_TYPE_LOAD;
error->class_name = g_strdup (class_name);
error->assembly_name = g_strdup (assembly_name);
return;
error = g_new0 (MonoLoaderError, 1);
- error->kind = MONO_LOADER_ERROR_METHOD;
+ error->exception_type = MONO_EXCEPTION_MISSING_METHOD;
error->class_name = g_strdup (class_name);
error->member_name = member_name;
return;
error = g_new0 (MonoLoaderError, 1);
- error->kind = MONO_LOADER_ERROR_FIELD;
+ error->exception_type = MONO_EXCEPTION_MISSING_FIELD;
error->klass = klass;
error->member_name = member_name;
set_loader_error (error);
}
+/*
+ * mono_loader_set_error_bad_image:
+ *
+ * Set the loader error for this thread.
+ */
+void
+mono_loader_set_error_bad_image (char *msg)
+{
+ MonoLoaderError *error;
+
+ if (mono_loader_get_last_error ())
+ return;
+
+ error = g_new0 (MonoLoaderError, 1);
+ error->exception_type = MONO_EXCEPTION_BAD_IMAGE;
+ error->msg = msg;
+
+ set_loader_error (error);
+}
+
+
/*
* mono_loader_get_last_error:
*
if (ex) {
g_free (ex->class_name);
g_free (ex->assembly_name);
+ g_free (ex->msg);
g_free (ex);
TlsSetValue (loader_error_thread_id, NULL);
{
MonoException *ex = NULL;
- switch (error->kind) {
- case MONO_LOADER_ERROR_TYPE: {
+ switch (error->exception_type) {
+ case MONO_EXCEPTION_TYPE_LOAD: {
char *cname = g_strdup (error->class_name);
char *aname = g_strdup (error->assembly_name);
MonoString *class_name;
class_name = mono_string_new (mono_domain_get (), cname);
- ex = mono_get_exception_type_load (class_name, aname);
+ ex = mono_get_exception_type_load (class_name, aname);
g_free (cname);
g_free (aname);
- break;
+ break;
}
- case MONO_LOADER_ERROR_METHOD: {
+ case MONO_EXCEPTION_MISSING_METHOD: {
char *cname = g_strdup (error->class_name);
char *aname = g_strdup (error->member_name);
break;
}
- case MONO_LOADER_ERROR_FIELD: {
+ case MONO_EXCEPTION_MISSING_FIELD: {
char *cnspace = g_strdup (*error->klass->name_space ? error->klass->name_space : "");
char *cname = g_strdup (error->klass->name);
char *cmembername = g_strdup (error->member_name);
class_name = g_strdup_printf ("%s%s%s", cnspace, cnspace ? "." : "", cname);
ex = mono_get_exception_missing_field (class_name, cmembername);
- g_free (class_name);
+ g_free (class_name);
g_free (cname);
g_free (cmembername);
g_free (cnspace);
- break;
+ break;
}
- case MONO_LOADER_ERROR_ASSEMBLY: {
+ case MONO_EXCEPTION_FILE_NOT_FOUND: {
char *msg;
char *filename;
g_free (filename);
break;
}
-
+
+ case MONO_EXCEPTION_BAD_IMAGE: {
+ char *msg = g_strdup (error->msg);
+ mono_loader_clear_error ();
+ ex = mono_get_exception_bad_image_format (msg);
+ g_free (msg);
+ break;
+ }
+
default:
g_assert_not_reached ();
}
const char *ptr;
guint32 idx = mono_metadata_token_index (token);
- if (image->dynamic) {
- MonoClassField *result = mono_lookup_dynamic_token (image, token);
- *retklass = result->parent;
- return result;
- }
-
mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
case MONO_MEMBERREF_PARENT_TYPEDEF:
klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
if (!klass) {
- char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
- g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
+ char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
+ g_warning ("Missing field %s in class %s (typedef index %d)", fname, name, nindex);
+ mono_loader_set_error_type_load (name, image->assembly_name);
g_free (name);
return NULL;
}
klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
if (!klass) {
char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
- g_warning ("Missing field %s in class %s (typeref index %d)", fname, name, nindex);
+ g_warning ("missing field %s in class %s (typeref index %d)", fname, name, nindex);
+ mono_loader_set_error_type_load (name, image->assembly_name);
g_free (name);
return NULL;
}
mono_class_init (klass);
g_print ("type in sig: %s\n", klass->name);*/
klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
+ //FIXME can't klass be null?
mono_class_init (klass);
if (retklass)
*retklass = klass;
MonoClassField *field;
if (image->dynamic) {
- MonoClassField *result = mono_lookup_dynamic_token (image, token);
+ MonoClassField *result;
+ MonoClass *handle_class;
+
+ *retklass = NULL;
+ result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
+ // This checks the memberref type as well
+ if (result && handle_class != mono_defaults.fieldhandle_class) {
+ mono_loader_set_error_bad_image (g_strdup ("Bad field token."));
+ return NULL;
+ }
*retklass = result->parent;
return result;
}
}
mono_loader_lock ();
- if (field && !field->parent->generic_class)
+ if (field && !field->parent->generic_class && !field->parent->generic_container)
g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
mono_loader_unlock ();
return field;
MonoMethod *m = in_class->methods [i];
if (!((fqname && !strcmp (m->name, fqname)) ||
- (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
+ (qname && !strcmp (m->name, qname)) ||
+ (name && !strcmp (m->name, name))))
continue;
if (sig->call_convention == MONO_CALL_VARARG) {
g_assert (from_class->interface_count == in_class->interface_count);
for (i = 0; i < in_class->interface_count; i++) {
- MonoClass *ic = in_class->interfaces [i];
+ MonoClass *in_ic = in_class->interfaces [i];
MonoClass *from_ic = from_class->interfaces [i];
+ char *ic_qname, *ic_fqname, *ic_class_name;
+
+ ic_class_name = mono_type_get_name_full (&in_ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
+ ic_qname = g_strconcat (ic_class_name, ".", name, NULL);
+ if (in_ic->name_space && in_ic->name_space [0])
+ ic_fqname = g_strconcat (in_ic->name_space, ".", ic_class_name, ".", name, NULL);
+ else
+ ic_fqname = NULL;
- result = find_method_in_class (ic, name, qname, fqname, sig, from_ic);
+ result = find_method_in_class (in_ic, ic ? name : NULL, ic_qname, ic_fqname, sig, from_ic);
+ g_free (ic_class_name);
+ g_free (ic_fqname);
+ g_free (ic_qname);
if (result)
goto out;
}
if (!context)
return sig;
- res = mono_metadata_signature_alloc (image, sig->param_count);
+ res = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
+ res->param_count = sig->param_count;
+ res->sentinelpos = -1;
res->ret = mono_class_inflate_generic_type (sig->ret, context);
is_open = mono_class_is_open_constructed_type (res->ret);
for (i = 0; i < sig->param_count; ++i) {
continue;
t = mono_class_inflate_generic_type (&clause->data.catch_class->byval_arg, context);
clause->data.catch_class = mono_class_from_mono_type (t);
+ mono_metadata_free_type (t);
}
}
return res;
prev_sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token));
if (prev_sig) {
/* Somebody got in before us */
- /* FIXME: Free sig */
sig = prev_sig;
}
else
mono_loader_unlock ();
}
- sig = inflate_generic_signature (image, sig, context);
+ if (context) {
+ MonoMethodSignature *cached;
+
+ /* This signature is not owned by a MonoMethod, so need to cache */
+ sig = inflate_generic_signature (image, sig, context);
+ cached = mono_metadata_get_inflated_signature (sig, context);
+ if (cached != sig)
+ mono_metadata_free_inflated_signature (sig);
+ sig = cached;
+ }
return sig;
}
if (!klass) {
char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_REF | nindex);
g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
- mono_loader_set_error_method_load (name, mname);
+ mono_loader_set_error_type_load (name, image->assembly_name);
g_free (name);
return NULL;
}
if (!klass) {
char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_SPEC | nindex);
g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
- mono_loader_set_error_method_load (name, mname);
+ mono_loader_set_error_type_load (name, image->assembly_name);
g_free (name);
return NULL;
}
if (!klass) {
char *name = mono_class_name_from_token (image, MONO_TOKEN_TYPE_DEF | nindex);
g_warning ("Missing method %s in assembly %s, type %s", mname, image->name, name);
- mono_loader_set_error_method_load (name, mname);
+ mono_loader_set_error_type_load (name, image->assembly_name);
g_free (name);
return NULL;
}
static MonoMethod *
method_from_methodspec (MonoImage *image, MonoGenericContext *context, guint32 idx)
{
- MonoMethod *method, *inflated;
+ MonoMethod *method;
+ MonoClass *klass;
MonoTableInfo *tables = image->tables;
- MonoGenericContext *new_context = NULL;
- MonoGenericMethod *gmethod;
- MonoGenericContainer *container = NULL;
+ MonoGenericContext new_context;
+ MonoGenericInst *inst;
const char *ptr;
guint32 cols [MONO_METHODSPEC_SIZE];
guint32 token, nindex, param_count;
param_count = mono_metadata_decode_value (ptr, &ptr);
g_assert (param_count);
- /*
- * Be careful with the two contexts here:
- *
- * ----------------------------------------
- * class Foo<S> {
- * static void Hello<T> (S s, T t) { }
- *
- * static void Test<U> (U u) {
- * Foo<U>.Hello<string> (u, "World");
- * }
- * }
- * ----------------------------------------
- *
- * Let's assume we're currently JITing Foo<int>.Test<long>
- * (ie. `S' is instantiated as `int' and `U' is instantiated as `long').
- *
- * The call to Hello() is encoded with a MethodSpec with a TypeSpec as parent
- * (MONO_MEMBERREF_PARENT_TYPESPEC).
- *
- * The TypeSpec is encoded as `Foo<!!0>', so we need to parse it in the current
- * context (S=int, U=long) to get the correct `Foo<long>'.
- *
- * After that, we parse the memberref signature in the new context
- * (S=int, T=uninstantiated) and get the open generic method `Foo<long>.Hello<T>'.
- *
- */
+ inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
+ if (context && inst->is_open)
+ inst = mono_metadata_inflate_generic_inst (inst, context);
+
if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
method = mono_get_method_full (image, MONO_TOKEN_METHOD_DEF | nindex, NULL, context);
else
method = method_from_memberref (image, nindex, context, NULL);
- method = mono_get_inflated_method (method);
-
- container = method->generic_container;
- g_assert (container);
-
- gmethod = g_new0 (MonoGenericMethod, 1);
- if (method->klass->generic_class)
- gmethod->class_inst = method->klass->generic_class->inst;
- gmethod->container = container;
-
- new_context = g_new0 (MonoGenericContext, 1);
- new_context->gmethod = gmethod;
- /* FIXME: Is this correct? */
- if (container->parent)
- new_context->class_inst = container->parent->context.class_inst;
-
- /*
- * When parsing the methodspec signature, we're in the old context again:
- *
- * ----------------------------------------
- * class Foo {
- * static void Hello<T> (T t) { }
- *
- * static void Test<U> (U u) {
- * Foo.Hello<U> (u);
- * }
- * }
- * ----------------------------------------
- *
- * Let's assume we're currently JITing "Foo.Test<float>".
- *
- * In this case, we already parsed the memberref as "Foo.Hello<T>" and the methodspec
- * signature is "<!!0>". This means that we must instantiate the method type parameter
- * `T' from the new method with the method type parameter `U' from the current context;
- * ie. instantiate the method as `Foo.Hello<float>.
- */
-
- gmethod->inst = mono_metadata_parse_generic_inst (image, NULL, param_count, ptr, &ptr);
-
- if (context && gmethod->inst->is_open)
- gmethod->inst = mono_metadata_inflate_generic_inst (gmethod->inst, context);
-
- if (!container->method_hash)
- container->method_hash = g_hash_table_new (
- (GHashFunc)mono_metadata_generic_method_hash, (GEqualFunc)mono_metadata_generic_method_equal);
+ klass = method->klass;
- inflated = g_hash_table_lookup (container->method_hash, gmethod);
- if (inflated) {
- g_free (gmethod);
- g_free (new_context);
- return inflated;
+ if (klass->generic_class) {
+ g_assert (method->is_inflated);
+ method = ((MonoMethodInflated *) method)->declaring;
}
- context = new_context;
-
- mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
- sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
-
- inflated = mono_class_inflate_generic_method_full (method, method->klass, new_context);
- g_hash_table_insert (container->method_hash, gmethod, inflated);
+ new_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
+ new_context.method_inst = inst;
- return inflated;
+ return mono_class_inflate_generic_method_full (method, klass, &new_context);
}
struct _MonoDllMap {
return piinfo->addr;
}
+/*
+ * LOCKING: assumes the loader lock to be taken.
+ */
static MonoMethod *
mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
MonoGenericContext *context, gboolean *used_context)
int size, i;
guint32 cols [MONO_TYPEDEF_SIZE];
- if (image->dynamic)
- return mono_lookup_dynamic_token (image, token);
+ if (image->dynamic) {
+ MonoClass *handle_class;
+
+ result = mono_lookup_dynamic_token_class (image, token, TRUE, &handle_class, context);
+ // This checks the memberref type as well
+ if (result && handle_class != mono_defaults.methodhandle_class) {
+ mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
+ return NULL;
+ }
+ return result;
+ }
if (table != MONO_TABLE_METHOD) {
if (table == MONO_TABLE_METHODSPEC) {
if (used_context) *used_context = FALSE;
+ if (idx > image->tables [MONO_TABLE_METHOD].rows) {
+ mono_loader_set_error_bad_image (g_strdup ("Bad method token."));
+ return NULL;
+ }
+
mono_metadata_decode_row (&image->tables [MONO_TABLE_METHOD], idx - 1, cols, 6);
if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
}
mono_class_init (constrained_class);
- method = mono_get_inflated_method (*cil_method);
+ method = *cil_method;
sig = mono_method_signature (method);
if (method->is_inflated && sig->generic_param_count) {
void
mono_free_method (MonoMethod *method)
{
+ if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
+ mono_profiler_method_free (method);
+
+ /* FIXME: This hack will go away when the profiler will support freeing methods */
if (mono_profiler_get_events () != MONO_PROFILE_NONE)
return;
if (method->dynamic) {
MonoMethodWrapper *mw = (MonoMethodWrapper*)method;
-
+ int i;
+
g_free ((char*)method->name);
- if (mw->method.header)
+ if (mw->method.header) {
g_free ((char*)mw->method.header->code);
+ for (i = 0; i < mw->method.header->num_locals; ++i)
+ g_free (mw->method.header->locals [i]);
+ g_free (mw->method.header->clauses);
+ g_free (mw->method.header);
+ }
g_free (mw->method_data);
- }
-
- if (method->dynamic && !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && ((MonoMethodNormal *)method)->header) {
- /* FIXME: Ditto */
- /* mono_metadata_free_mh (((MonoMethodNormal *)method)->header); */
- g_free (((MonoMethodNormal*)method)->header);
- }
-
- if (method->dynamic)
+ g_free (method->signature);
g_free (method);
+ }
}
void
mono_method_get_param_names (MonoMethod *method, const char **names)
{
int i, lastp;
- MonoClass *klass = method->klass;
+ MonoClass *klass;
MonoTableInfo *methodt;
MonoTableInfo *paramt;
guint32 idx;
+ if (method->is_inflated)
+ method = ((MonoMethodInflated *) method)->declaring;
+
if (!mono_method_signature (method)->param_count)
return;
for (i = 0; i < mono_method_signature (method)->param_count; ++i)
names [i] = "";
- if (klass->generic_class || klass->rank) /* copy the names later */
+ klass = method->klass;
+ if (klass->rank)
return;
mono_class_init (klass);
MonoTableInfo *methodt;
guint32 idx;
- if (klass->generic_class)
- g_assert_not_reached ();
-
mono_class_init (klass);
if (klass->image->dynamic) {
if (idx > 0) {
guint param_index = mono_metadata_decode_row_col (methodt, idx - 1, MONO_METHOD_PARAMLIST);
- return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
+ if (index == -1)
+ /* Return value */
+ return mono_metadata_make_token (MONO_TABLE_PARAM, 0);
+ else
+ return mono_metadata_make_token (MONO_TABLE_PARAM, param_index + index);
}
return 0;