2 * reflection.c: System.Type icalls and related reflection queries.
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Rodrigo Kumpera
10 * Copyright 2016 Microsoft
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include "mono/utils/mono-membar.h"
16 #include "mono/metadata/reflection-internals.h"
17 #include "mono/metadata/tabledefs.h"
18 #include "mono/metadata/metadata-internals.h"
19 #include <mono/metadata/profiler-private.h>
20 #include "mono/metadata/class-internals.h"
21 #include "mono/metadata/gc-internals.h"
22 #include "mono/metadata/domain-internals.h"
23 #include "mono/metadata/opcodes.h"
24 #include "mono/metadata/assembly.h"
25 #include "mono/metadata/object-internals.h"
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/security-manager.h>
29 #include <mono/metadata/reflection-cache.h>
30 #include <mono/metadata/sre-internals.h>
39 #include "mono-endian.h"
40 #include <mono/metadata/gc-internals.h>
41 #include <mono/metadata/mempool-internals.h>
42 #include <mono/metadata/security-core-clr.h>
43 #include <mono/metadata/debug-helpers.h>
44 #include <mono/metadata/verify-internals.h>
45 #include <mono/metadata/mono-ptr-array.h>
46 #include <mono/utils/mono-string.h>
47 #include <mono/utils/mono-error-internals.h>
48 #include <mono/utils/checked-build.h>
50 static void get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types);
51 static MonoType* mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error);
53 /* Class lazy loading functions */
54 static GENERATE_GET_CLASS_WITH_CACHE (mono_assembly, System.Reflection, MonoAssembly)
55 static GENERATE_GET_CLASS_WITH_CACHE (mono_module, System.Reflection, MonoModule)
56 static GENERATE_GET_CLASS_WITH_CACHE (mono_generic_method, System.Reflection, MonoGenericMethod);
57 static GENERATE_GET_CLASS_WITH_CACHE (mono_generic_cmethod, System.Reflection, MonoGenericCMethod);
58 static GENERATE_GET_CLASS_WITH_CACHE (mono_method, System.Reflection, MonoMethod);
59 static GENERATE_GET_CLASS_WITH_CACHE (mono_cmethod, System.Reflection, MonoCMethod);
60 static GENERATE_GET_CLASS_WITH_CACHE (mono_field, System.Reflection, MonoField);
61 static GENERATE_GET_CLASS_WITH_CACHE (mono_event, System.Reflection, MonoEvent);
62 static GENERATE_GET_CLASS_WITH_CACHE (mono_property, System.Reflection, MonoProperty);
63 static GENERATE_GET_CLASS_WITH_CACHE (mono_parameter_info, System.Reflection, MonoParameterInfo);
64 static GENERATE_GET_CLASS_WITH_CACHE (missing, System.Reflection, Missing);
65 static GENERATE_GET_CLASS_WITH_CACHE (method_body, System.Reflection, MethodBody);
66 static GENERATE_GET_CLASS_WITH_CACHE (local_variable_info, System.Reflection, LocalVariableInfo);
67 static GENERATE_GET_CLASS_WITH_CACHE (exception_handling_clause, System.Reflection, ExceptionHandlingClause);
68 static GENERATE_GET_CLASS_WITH_CACHE (type_builder, System.Reflection.Emit, TypeBuilder);
69 static GENERATE_GET_CLASS_WITH_CACHE (dbnull, System, DBNull);
72 mono_reflection_init (void)
74 mono_reflection_emit_init ();
78 * mono_class_get_ref_info:
80 * Return the type builder/generic param builder corresponding to KLASS, if it exists.
83 mono_class_get_ref_info (MonoClass *klass)
85 MONO_REQ_GC_UNSAFE_MODE;
87 if (klass->ref_info_handle == 0)
90 return mono_gchandle_get_target (klass->ref_info_handle);
94 mono_class_set_ref_info (MonoClass *klass, gpointer obj)
96 MONO_REQ_GC_UNSAFE_MODE;
98 klass->ref_info_handle = mono_gchandle_new ((MonoObject*)obj, FALSE);
99 g_assert (klass->ref_info_handle != 0);
103 mono_class_free_ref_info (MonoClass *klass)
105 MONO_REQ_GC_NEUTRAL_MODE;
107 if (klass->ref_info_handle) {
108 mono_gchandle_free (klass->ref_info_handle);
109 klass->ref_info_handle = 0;
115 mono_custom_attrs_free (MonoCustomAttrInfo *ainfo)
117 MONO_REQ_GC_NEUTRAL_MODE;
119 if (ainfo && !ainfo->cached)
125 reflected_equal (gconstpointer a, gconstpointer b)
127 const ReflectedEntry *ea = (const ReflectedEntry *)a;
128 const ReflectedEntry *eb = (const ReflectedEntry *)b;
130 return (ea->item == eb->item) && (ea->refclass == eb->refclass);
134 reflected_hash (gconstpointer a) {
135 const ReflectedEntry *ea = (const ReflectedEntry *)a;
136 return mono_aligned_addr_hash (ea->item);
141 clear_cached_object (MonoDomain *domain, gpointer o, MonoClass *klass)
143 mono_domain_lock (domain);
144 if (domain->refobject_hash) {
146 gpointer orig_pe, orig_value;
150 if (mono_g_hash_table_lookup_extended (domain->refobject_hash, &pe, &orig_pe, &orig_value)) {
151 mono_g_hash_table_remove (domain->refobject_hash, &pe);
152 FREE_REFENTRY (orig_pe);
155 mono_domain_unlock (domain);
158 #ifdef REFENTRY_REQUIRES_CLEANUP
160 cleanup_refobject_hash (gpointer key, gpointer value, gpointer user_data)
167 mono_reflection_cleanup_domain (MonoDomain *domain)
169 if (domain->refobject_hash) {
170 /*let's avoid scanning the whole hashtable if not needed*/
171 #ifdef REFENTRY_REQUIRES_CLEANUP
172 mono_g_hash_table_foreach (domain->refobject_hash, cleanup_refobject_hash, NULL);
174 mono_g_hash_table_destroy (domain->refobject_hash);
175 domain->refobject_hash = NULL;
181 * mono_assembly_get_object:
182 * @domain: an app domain
183 * @assembly: an assembly
185 * Return an System.Reflection.Assembly object representing the MonoAssembly @assembly.
187 MonoReflectionAssembly*
188 mono_assembly_get_object (MonoDomain *domain, MonoAssembly *assembly)
191 MonoReflectionAssembly *result;
192 result = mono_assembly_get_object_checked (domain, assembly, &error);
193 mono_error_cleanup (&error); /* FIXME new API that doesn't swallow the error */
197 * mono_assembly_get_object_checked:
198 * @domain: an app domain
199 * @assembly: an assembly
201 * Return an System.Reflection.Assembly object representing the MonoAssembly @assembly.
203 MonoReflectionAssembly*
204 mono_assembly_get_object_checked (MonoDomain *domain, MonoAssembly *assembly, MonoError *error)
206 MonoReflectionAssembly *res;
208 mono_error_init (error);
210 CHECK_OBJECT (MonoReflectionAssembly *, assembly, NULL);
211 res = (MonoReflectionAssembly *)mono_object_new_checked (domain, mono_class_get_mono_assembly_class (), error);
214 res->assembly = assembly;
216 CACHE_OBJECT (MonoReflectionAssembly *, assembly, res, NULL);
221 MonoReflectionModule*
222 mono_module_get_object (MonoDomain *domain, MonoImage *image)
225 MonoReflectionModule *result;
226 result = mono_module_get_object_checked (domain, image, &error);
227 mono_error_cleanup (&error);
231 MonoReflectionModule*
232 mono_module_get_object_checked (MonoDomain *domain, MonoImage *image, MonoError *error)
234 MonoReflectionModule *res;
237 mono_error_init (error);
238 CHECK_OBJECT (MonoReflectionModule *, image, NULL);
239 res = (MonoReflectionModule *)mono_object_new_checked (domain, mono_class_get_mono_module_class (), error);
244 MonoReflectionAssembly *assm_obj = mono_assembly_get_object_checked (domain, image->assembly, error);
247 MONO_OBJECT_SETREF (res, assembly, assm_obj);
249 MONO_OBJECT_SETREF (res, fqname, mono_string_new (domain, image->name));
250 basename = g_path_get_basename (image->name);
251 MONO_OBJECT_SETREF (res, name, mono_string_new (domain, basename));
252 MONO_OBJECT_SETREF (res, scopename, mono_string_new (domain, image->module_name));
256 if (image->assembly->image == image) {
257 res->token = mono_metadata_make_token (MONO_TABLE_MODULE, 1);
261 if (image->assembly->image->modules) {
262 for (i = 0; i < image->assembly->image->module_count; i++) {
263 if (image->assembly->image->modules [i] == image)
264 res->token = mono_metadata_make_token (MONO_TABLE_MODULEREF, i + 1);
266 g_assert (res->token);
270 CACHE_OBJECT (MonoReflectionModule *, image, res, NULL);
273 MonoReflectionModule*
274 mono_module_file_get_object (MonoDomain *domain, MonoImage *image, int table_index)
277 MonoReflectionModule *result;
278 result = mono_module_file_get_object_checked (domain, image, table_index, &error);
279 mono_error_cleanup (&error);
283 MonoReflectionModule*
284 mono_module_file_get_object_checked (MonoDomain *domain, MonoImage *image, int table_index, MonoError *error)
286 MonoReflectionModule *res;
287 MonoTableInfo *table;
288 guint32 cols [MONO_FILE_SIZE];
293 mono_error_init (error);
295 res = (MonoReflectionModule *)mono_object_new_checked (domain, mono_class_get_mono_module_class (), error);
299 table = &image->tables [MONO_TABLE_FILE];
300 g_assert (table_index < table->rows);
301 mono_metadata_decode_row (table, table_index, cols, MONO_FILE_SIZE);
304 MonoReflectionAssembly *assm_obj = mono_assembly_get_object_checked (domain, image->assembly, error);
307 MONO_OBJECT_SETREF (res, assembly, assm_obj);
308 name = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
310 /* Check whenever the row has a corresponding row in the moduleref table */
311 table = &image->tables [MONO_TABLE_MODULEREF];
312 for (i = 0; i < table->rows; ++i) {
313 name_idx = mono_metadata_decode_row_col (table, i, MONO_MODULEREF_NAME);
314 val = mono_metadata_string_heap (image, name_idx);
315 if (strcmp (val, name) == 0)
316 res->image = image->modules [i];
319 MONO_OBJECT_SETREF (res, fqname, mono_string_new (domain, name));
320 MONO_OBJECT_SETREF (res, name, mono_string_new (domain, name));
321 MONO_OBJECT_SETREF (res, scopename, mono_string_new (domain, name));
322 res->is_resource = cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA;
323 res->token = mono_metadata_make_token (MONO_TABLE_FILE, table_index + 1);
329 verify_safe_for_managed_space (MonoType *type)
331 switch (type->type) {
333 case MONO_TYPE_ARRAY:
334 return verify_safe_for_managed_space (&type->data.array->eklass->byval_arg);
336 return verify_safe_for_managed_space (type->data.type);
337 case MONO_TYPE_SZARRAY:
338 return verify_safe_for_managed_space (&type->data.klass->byval_arg);
339 case MONO_TYPE_GENERICINST: {
340 MonoGenericInst *inst = type->data.generic_class->inst;
344 for (i = 0; i < inst->type_argc; ++i)
345 if (!verify_safe_for_managed_space (inst->type_argv [i]))
359 mono_type_normalize (MonoType *type)
362 MonoGenericClass *gclass;
363 MonoGenericInst *ginst;
365 MonoGenericContainer *gcontainer;
366 MonoType **argv = NULL;
367 gboolean is_denorm_gtd = TRUE, requires_rebind = FALSE;
369 if (type->type != MONO_TYPE_GENERICINST)
372 gclass = type->data.generic_class;
373 ginst = gclass->context.class_inst;
377 gtd = gclass->container_class;
378 gcontainer = gtd->generic_container;
379 argv = g_newa (MonoType*, ginst->type_argc);
381 for (i = 0; i < ginst->type_argc; ++i) {
382 MonoType *t = ginst->type_argv [i], *norm;
383 if (t->type != MONO_TYPE_VAR || t->data.generic_param->num != i || t->data.generic_param->owner != gcontainer)
384 is_denorm_gtd = FALSE;
385 norm = mono_type_normalize (t);
388 requires_rebind = TRUE;
392 return type->byref == gtd->byval_arg.byref ? >d->byval_arg : >d->this_arg;
394 if (requires_rebind) {
395 MonoClass *klass = mono_class_bind_generic_parameters (gtd, ginst->type_argc, argv, gclass->is_dynamic);
396 return type->byref == klass->byval_arg.byref ? &klass->byval_arg : &klass->this_arg;
402 * mono_type_get_object:
403 * @domain: an app domain
406 * Return an System.MonoType object representing the type @type.
409 mono_type_get_object (MonoDomain *domain, MonoType *type)
412 MonoReflectionType *ret = mono_type_get_object_checked (domain, type, &error);
413 mono_error_cleanup (&error);
419 mono_type_get_object_checked (MonoDomain *domain, MonoType *type, MonoError *error)
422 MonoReflectionType *res;
425 mono_error_init (error);
427 klass = mono_class_from_mono_type (type);
429 /*we must avoid using @type as it might have come
430 * from a mono_metadata_type_dup and the caller
431 * expects that is can be freed.
432 * Using the right type from
434 type = klass->byval_arg.byref == type->byref ? &klass->byval_arg : &klass->this_arg;
436 /* void is very common */
437 if (type->type == MONO_TYPE_VOID && domain->typeof_void)
438 return (MonoReflectionType*)domain->typeof_void;
441 * If the vtable of the given class was already created, we can use
442 * the MonoType from there and avoid all locking and hash table lookups.
444 * We cannot do this for TypeBuilders as mono_reflection_create_runtime_class expects
445 * that the resulting object is different.
447 if (type == &klass->byval_arg && !image_is_dynamic (klass->image)) {
448 MonoVTable *vtable = mono_class_try_get_vtable (domain, klass);
449 if (vtable && vtable->type)
450 return (MonoReflectionType *)vtable->type;
453 mono_loader_lock (); /*FIXME mono_class_init and mono_class_vtable acquire it*/
454 mono_domain_lock (domain);
455 if (!domain->type_hash)
456 domain->type_hash = mono_g_hash_table_new_type ((GHashFunc)mono_metadata_type_hash,
457 (GCompareFunc)mono_metadata_type_equal, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "domain reflection types table");
458 if ((res = (MonoReflectionType *)mono_g_hash_table_lookup (domain->type_hash, type))) {
459 mono_domain_unlock (domain);
460 mono_loader_unlock ();
464 /*Types must be normalized so a generic instance of the GTD get's the same inner type.
465 * For example in: Foo<A,B>; Bar<A> : Foo<A, Bar<A>>
466 * The second Bar will be encoded a generic instance of Bar with <A> as parameter.
467 * On all other places, Bar<A> will be encoded as the GTD itself. This is an implementation
468 * artifact of how generics are encoded and should be transparent to managed code so we
469 * need to weed out this diference when retrieving managed System.Type objects.
471 norm_type = mono_type_normalize (type);
472 if (norm_type != type) {
473 res = mono_type_get_object_checked (domain, norm_type, error);
474 if (!mono_error_ok (error))
476 mono_g_hash_table_insert (domain->type_hash, type, res);
477 mono_domain_unlock (domain);
478 mono_loader_unlock ();
482 /* This MonoGenericClass hack is no longer necessary. Let's leave it here until we finish with the 2-stage type-builder setup.*/
483 if ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->is_dynamic && !type->data.generic_class->container_class->wastypebuilder)
486 if (!verify_safe_for_managed_space (type)) {
487 mono_domain_unlock (domain);
488 mono_loader_unlock ();
489 mono_error_set_generic_error (error, "System", "InvalidOperationException", "This type cannot be propagated to managed space");
493 if (mono_class_get_ref_info (klass) && !klass->wastypebuilder) {
494 gboolean is_type_done = TRUE;
495 /* Generic parameters have reflection_info set but they are not finished together with their enclosing type.
496 * We must ensure that once a type is finished we don't return a GenericTypeParameterBuilder.
497 * We can't simply close the types as this will interfere with other parts of the generics machinery.
499 if (klass->byval_arg.type == MONO_TYPE_MVAR || klass->byval_arg.type == MONO_TYPE_VAR) {
500 MonoGenericParam *gparam = klass->byval_arg.data.generic_param;
502 if (gparam->owner && gparam->owner->is_method) {
503 MonoMethod *method = gparam->owner->owner.method;
504 if (method && mono_class_get_generic_type_definition (method->klass)->wastypebuilder)
505 is_type_done = FALSE;
506 } else if (gparam->owner && !gparam->owner->is_method) {
507 MonoClass *klass = gparam->owner->owner.klass;
508 if (klass && mono_class_get_generic_type_definition (klass)->wastypebuilder)
509 is_type_done = FALSE;
513 /* g_assert_not_reached (); */
514 /* should this be considered an error condition? */
515 if (is_type_done && !type->byref) {
516 mono_domain_unlock (domain);
517 mono_loader_unlock ();
518 return (MonoReflectionType *)mono_class_get_ref_info (klass);
521 /* This is stored in vtables/JITted code so it has to be pinned */
522 res = (MonoReflectionType *)mono_object_new_pinned (domain, mono_defaults.runtimetype_class, error);
523 if (!mono_error_ok (error))
527 mono_g_hash_table_insert (domain->type_hash, type, res);
529 if (type->type == MONO_TYPE_VOID)
530 domain->typeof_void = (MonoObject*)res;
532 mono_domain_unlock (domain);
533 mono_loader_unlock ();
538 * mono_method_get_object:
539 * @domain: an app domain
541 * @refclass: the reflected type (can be NULL)
543 * Return an System.Reflection.MonoMethod object representing the method @method.
545 MonoReflectionMethod*
546 mono_method_get_object (MonoDomain *domain, MonoMethod *method, MonoClass *refclass)
549 MonoReflectionMethod *ret = NULL;
550 ret = mono_method_get_object_checked (domain, method, refclass, &error);
551 mono_error_cleanup (&error);
556 * mono_method_get_object_checked:
557 * @domain: an app domain
559 * @refclass: the reflected type (can be NULL)
560 * @error: set on error.
562 * Return an System.Reflection.MonoMethod object representing the method @method.
563 * Returns NULL and sets @error on error.
565 MonoReflectionMethod*
566 mono_method_get_object_checked (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
569 * We use the same C representation for methods and constructors, but the type
570 * name in C# is different.
572 MonoReflectionType *rt;
574 MonoReflectionMethod *ret;
576 mono_error_init (error);
578 if (method->is_inflated) {
579 MonoReflectionGenericMethod *gret;
582 refclass = method->klass;
583 CHECK_OBJECT (MonoReflectionMethod *, method, refclass);
584 if ((*method->name == '.') && (!strcmp (method->name, ".ctor") || !strcmp (method->name, ".cctor"))) {
585 klass = mono_class_get_mono_generic_cmethod_class ();
587 klass = mono_class_get_mono_generic_method_class ();
589 gret = (MonoReflectionGenericMethod*)mono_object_new_checked (domain, klass, error);
590 if (!mono_error_ok (error))
592 gret->method.method = method;
594 MONO_OBJECT_SETREF (gret, method.name, mono_string_new (domain, method->name));
596 rt = mono_type_get_object_checked (domain, &refclass->byval_arg, error);
597 if (!mono_error_ok (error))
600 MONO_OBJECT_SETREF (gret, method.reftype, rt);
602 CACHE_OBJECT (MonoReflectionMethod *, method, (MonoReflectionMethod*)gret, refclass);
606 refclass = method->klass;
608 CHECK_OBJECT (MonoReflectionMethod *, method, refclass);
609 if (*method->name == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)) {
610 klass = mono_class_get_mono_cmethod_class ();
613 klass = mono_class_get_mono_method_class ();
615 ret = (MonoReflectionMethod*)mono_object_new_checked (domain, klass, error);
616 if (!mono_error_ok (error))
618 ret->method = method;
620 rt = mono_type_get_object_checked (domain, &refclass->byval_arg, error);
621 if (!mono_error_ok (error))
624 MONO_OBJECT_SETREF (ret, reftype, rt);
626 CACHE_OBJECT (MonoReflectionMethod *, method, ret, refclass);
629 g_assert (!mono_error_ok (error));
634 * mono_method_clear_object:
636 * Clear the cached reflection objects for the dynamic method METHOD.
639 mono_method_clear_object (MonoDomain *domain, MonoMethod *method)
642 g_assert (method_is_dynamic (method));
644 klass = method->klass;
646 clear_cached_object (domain, method, klass);
647 klass = klass->parent;
649 /* Added by mono_param_get_objects () */
650 clear_cached_object (domain, &(method->signature), NULL);
651 klass = method->klass;
653 clear_cached_object (domain, &(method->signature), klass);
654 klass = klass->parent;
659 * mono_field_get_object:
660 * @domain: an app domain
664 * Return an System.Reflection.MonoField object representing the field @field
668 mono_field_get_object (MonoDomain *domain, MonoClass *klass, MonoClassField *field)
671 MonoReflectionField *result;
672 result = mono_field_get_object_checked (domain, klass, field, &error);
673 mono_error_cleanup (&error);
678 * mono_field_get_object_checked:
679 * @domain: an app domain
682 * @error: set on error
684 * Return an System.Reflection.MonoField object representing the field @field
685 * in class @klass. On error, returns NULL and sets @error.
688 mono_field_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
690 MonoReflectionType *rt;
691 MonoReflectionField *res;
693 mono_error_init (error);
695 CHECK_OBJECT (MonoReflectionField *, field, klass);
696 res = (MonoReflectionField *)mono_object_new_checked (domain, mono_class_get_mono_field_class (), error);
701 MONO_OBJECT_SETREF (res, name, mono_string_new (domain, mono_field_get_name (field)));
704 rt = mono_type_get_object_checked (domain, field->type, error);
705 if (!mono_error_ok (error))
708 MONO_OBJECT_SETREF (res, type, rt);
710 res->attrs = mono_field_get_flags (field);
711 CACHE_OBJECT (MonoReflectionField *, field, res, klass);
715 * mono_property_get_object:
716 * @domain: an app domain
718 * @property: a property
720 * Return an System.Reflection.MonoProperty object representing the property @property
723 MonoReflectionProperty*
724 mono_property_get_object (MonoDomain *domain, MonoClass *klass, MonoProperty *property)
727 MonoReflectionProperty *result;
728 result = mono_property_get_object_checked (domain, klass, property, &error);
729 mono_error_cleanup (&error);
734 * mono_property_get_object:
735 * @domain: an app domain
737 * @property: a property
738 * @error: set on error
740 * Return an System.Reflection.MonoProperty object representing the property @property
741 * in class @klass. On error returns NULL and sets @error.
743 MonoReflectionProperty*
744 mono_property_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
746 MonoReflectionProperty *res;
748 mono_error_init (error);
750 CHECK_OBJECT (MonoReflectionProperty *, property, klass);
751 res = (MonoReflectionProperty *)mono_object_new_checked (domain, mono_class_get_mono_property_class (), error);
755 res->property = property;
756 CACHE_OBJECT (MonoReflectionProperty *, property, res, klass);
760 * mono_event_get_object:
761 * @domain: an app domain
765 * Return an System.Reflection.MonoEvent object representing the event @event
769 mono_event_get_object (MonoDomain *domain, MonoClass *klass, MonoEvent *event)
772 MonoReflectionEvent *result;
773 result = mono_event_get_object_checked (domain, klass, event, &error);
774 mono_error_cleanup (&error);
779 * mono_event_get_object_checked:
780 * @domain: an app domain
783 * @error: set on error
785 * Return an System.Reflection.MonoEvent object representing the event @event
786 * in class @klass. On failure sets @error and returns NULL
789 mono_event_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoEvent *event, MonoError *error)
791 MonoReflectionEvent *res;
792 MonoReflectionMonoEvent *mono_event;
794 mono_error_init (error);
795 CHECK_OBJECT (MonoReflectionEvent *, event, klass);
796 mono_event = (MonoReflectionMonoEvent *)mono_object_new_checked (domain, mono_class_get_mono_event_class (), error);
799 mono_event->klass = klass;
800 mono_event->event = event;
801 res = (MonoReflectionEvent*)mono_event;
802 CACHE_OBJECT (MonoReflectionEvent *, event, res, klass);
806 * mono_get_reflection_missing_object:
807 * @domain: Domain where the object lives
809 * Returns the System.Reflection.Missing.Value singleton object
810 * (of type System.Reflection.Missing).
812 * Used as the value for ParameterInfo.DefaultValue when Optional
816 mono_get_reflection_missing_object (MonoDomain *domain)
820 static MonoClassField *missing_value_field = NULL;
822 if (!missing_value_field) {
823 MonoClass *missing_klass;
824 missing_klass = mono_class_get_missing_class ();
825 mono_class_init (missing_klass);
826 missing_value_field = mono_class_get_field_from_name (missing_klass, "Value");
827 g_assert (missing_value_field);
829 obj = mono_field_get_value_object_checked (domain, missing_value_field, NULL, &error);
830 mono_error_assert_ok (&error);
835 get_dbnull (MonoDomain *domain, MonoObject **dbnull)
838 *dbnull = mono_get_dbnull_object (domain);
843 get_reflection_missing (MonoDomain *domain, MonoObject **reflection_missing)
845 if (!*reflection_missing)
846 *reflection_missing = mono_get_reflection_missing_object (domain);
847 return *reflection_missing;
851 * mono_param_get_objects:
852 * @domain: an app domain
855 * Return an System.Reflection.ParameterInfo array object representing the parameters
856 * in the method @method.
859 mono_param_get_objects_internal (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
861 static MonoClass *System_Reflection_ParameterInfo;
862 static MonoClass *System_Reflection_ParameterInfo_array;
863 MonoArray *res = NULL;
864 MonoReflectionMethod *member = NULL;
865 MonoReflectionParameter *param = NULL;
866 char **names = NULL, **blobs = NULL;
867 guint32 *types = NULL;
868 MonoType *type = NULL;
869 MonoObject *dbnull = NULL;
870 MonoObject *missing = NULL;
871 MonoMarshalSpec **mspecs = NULL;
872 MonoMethodSignature *sig = NULL;
873 MonoVTable *pinfo_vtable;
874 MonoReflectionType *rt;
877 mono_error_init (error);
879 if (!System_Reflection_ParameterInfo_array) {
882 klass = mono_class_get_mono_parameter_info_class ();
884 mono_memory_barrier ();
885 System_Reflection_ParameterInfo = klass;
888 klass = mono_array_class_get (klass, 1);
889 mono_memory_barrier ();
890 System_Reflection_ParameterInfo_array = klass;
893 sig = mono_method_signature_checked (method, error);
894 if (!mono_error_ok (error))
897 if (!sig->param_count) {
898 res = mono_array_new_specific_checked (mono_class_vtable (domain, System_Reflection_ParameterInfo_array), 0, error);
905 /* Note: the cache is based on the address of the signature into the method
906 * since we already cache MethodInfos with the method as keys.
908 CHECK_OBJECT (MonoArray*, &(method->signature), refclass);
910 member = mono_method_get_object_checked (domain, method, refclass, error);
913 names = g_new (char *, sig->param_count);
914 mono_method_get_param_names (method, (const char **) names);
916 mspecs = g_new (MonoMarshalSpec*, sig->param_count + 1);
917 mono_method_get_marshal_info (method, mspecs);
919 res = mono_array_new_specific_checked (mono_class_vtable (domain, System_Reflection_ParameterInfo_array), sig->param_count, error);
923 pinfo_vtable = mono_class_vtable (domain, System_Reflection_ParameterInfo);
924 for (i = 0; i < sig->param_count; ++i) {
925 param = (MonoReflectionParameter *) mono_object_new_specific_checked (pinfo_vtable, error);
929 rt = mono_type_get_object_checked (domain, sig->params [i], error);
933 MONO_OBJECT_SETREF (param, ClassImpl, rt);
935 MONO_OBJECT_SETREF (param, MemberImpl, (MonoObject*)member);
937 MONO_OBJECT_SETREF (param, NameImpl, mono_string_new (domain, names [i]));
939 param->PositionImpl = i;
940 param->AttrsImpl = sig->params [i]->attrs;
942 if (!(param->AttrsImpl & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
943 if (param->AttrsImpl & PARAM_ATTRIBUTE_OPTIONAL)
944 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_reflection_missing (domain, &missing));
946 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_dbnull (domain, &dbnull));
950 blobs = g_new0 (char *, sig->param_count);
951 types = g_new0 (guint32, sig->param_count);
952 get_default_param_value_blobs (method, blobs, types);
955 /* Build MonoType for the type from the Constant Table */
957 type = g_new0 (MonoType, 1);
958 type->type = (MonoTypeEnum)types [i];
959 type->data.klass = NULL;
960 if (types [i] == MONO_TYPE_CLASS)
961 type->data.klass = mono_defaults.object_class;
962 else if ((sig->params [i]->type == MONO_TYPE_VALUETYPE) && sig->params [i]->data.klass->enumtype) {
963 /* For enums, types [i] contains the base type */
965 type->type = MONO_TYPE_VALUETYPE;
966 type->data.klass = mono_class_from_mono_type (sig->params [i]);
968 type->data.klass = mono_class_from_mono_type (type);
970 MonoObject *default_val_obj = mono_get_object_from_blob (domain, type, blobs [i], error);
973 MONO_OBJECT_SETREF (param, DefaultValueImpl, default_val_obj);
975 /* Type in the Constant table is MONO_TYPE_CLASS for nulls */
976 if (types [i] != MONO_TYPE_CLASS && !param->DefaultValueImpl) {
977 if (param->AttrsImpl & PARAM_ATTRIBUTE_OPTIONAL)
978 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_reflection_missing (domain, &missing));
980 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_dbnull (domain, &dbnull));
985 if (mspecs [i + 1]) {
986 MonoReflectionMarshalAsAttribute* mobj;
987 mobj = mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspecs [i + 1], error);
990 MONO_OBJECT_SETREF (param, MarshalAsImpl, (MonoObject*)mobj);
993 mono_array_setref (res, i, param);
1003 for (i = sig->param_count; i >= 0; i--) {
1005 mono_metadata_free_marshal_spec (mspecs [i]);
1013 CACHE_OBJECT (MonoArray *, &(method->signature), res, refclass);
1017 mono_param_get_objects (MonoDomain *domain, MonoMethod *method)
1020 MonoArray *result = mono_param_get_objects_internal (domain, method, NULL, &error);
1021 mono_error_assert_ok (&error);
1026 * mono_method_body_get_object:
1027 * @domain: an app domain
1030 * Return an System.Reflection.MethodBody object representing the method @method.
1032 MonoReflectionMethodBody*
1033 mono_method_body_get_object (MonoDomain *domain, MonoMethod *method)
1036 MonoReflectionMethodBody *result = mono_method_body_get_object_checked (domain, method, &error);
1037 mono_error_cleanup (&error);
1042 * mono_method_body_get_object_checked:
1043 * @domain: an app domain
1045 * @error: set on error
1047 * Return an System.Reflection.MethodBody object representing the
1048 * method @method. On failure, returns NULL and sets @error.
1050 MonoReflectionMethodBody*
1051 mono_method_body_get_object_checked (MonoDomain *domain, MonoMethod *method, MonoError *error)
1053 MonoReflectionMethodBody *ret;
1054 MonoMethodHeader *header;
1056 MonoReflectionType *rt;
1057 guint32 method_rva, local_var_sig_token;
1059 unsigned char format, flags;
1062 mono_error_init (error);
1064 /* for compatibility with .net */
1065 if (method_is_dynamic (method)) {
1066 mono_error_set_generic_error (error, "System", "InvalidOperationException", "");
1070 CHECK_OBJECT (MonoReflectionMethodBody *, method, NULL);
1072 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1073 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
1074 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1075 (method->klass->image->raw_data && method->klass->image->raw_data [1] != 'Z') ||
1076 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME))
1079 image = method->klass->image;
1080 header = mono_method_get_header_checked (method, error);
1081 return_val_if_nok (error, NULL);
1083 if (!image_is_dynamic (image)) {
1084 /* Obtain local vars signature token */
1085 method_rva = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD], mono_metadata_token_index (method->token) - 1, MONO_METHOD_RVA);
1086 ptr = mono_image_rva_map (image, method_rva);
1087 flags = *(const unsigned char *) ptr;
1088 format = flags & METHOD_HEADER_FORMAT_MASK;
1090 case METHOD_HEADER_TINY_FORMAT:
1091 local_var_sig_token = 0;
1093 case METHOD_HEADER_FAT_FORMAT:
1097 local_var_sig_token = read32 (ptr);
1100 g_assert_not_reached ();
1103 local_var_sig_token = 0; //FIXME
1105 ret = (MonoReflectionMethodBody*)mono_object_new_checked (domain, mono_class_get_method_body_class (), error);
1109 ret->init_locals = header->init_locals;
1110 ret->max_stack = header->max_stack;
1111 ret->local_var_sig_token = local_var_sig_token;
1112 MonoArray *il_arr = mono_array_new_cached (domain, mono_defaults.byte_class, header->code_size, error);
1115 MONO_OBJECT_SETREF (ret, il, il_arr);
1116 memcpy (mono_array_addr (ret->il, guint8, 0), header->code, header->code_size);
1119 MonoArray *locals_arr = mono_array_new_cached (domain, mono_class_get_local_variable_info_class (), header->num_locals, error);
1122 MONO_OBJECT_SETREF (ret, locals, locals_arr);
1123 for (i = 0; i < header->num_locals; ++i) {
1124 MonoReflectionLocalVariableInfo *info = (MonoReflectionLocalVariableInfo*)mono_object_new_checked (domain, mono_class_get_local_variable_info_class (), error);
1128 rt = mono_type_get_object_checked (domain, header->locals [i], error);
1132 MONO_OBJECT_SETREF (info, local_type, rt);
1134 info->is_pinned = header->locals [i]->pinned;
1135 info->local_index = i;
1136 mono_array_setref (ret->locals, i, info);
1140 MonoArray *exn_clauses = mono_array_new_cached (domain, mono_class_get_exception_handling_clause_class (), header->num_clauses, error);
1143 MONO_OBJECT_SETREF (ret, clauses, exn_clauses);
1144 for (i = 0; i < header->num_clauses; ++i) {
1145 MonoReflectionExceptionHandlingClause *info = (MonoReflectionExceptionHandlingClause*)mono_object_new_checked (domain, mono_class_get_exception_handling_clause_class (), error);
1148 MonoExceptionClause *clause = &header->clauses [i];
1150 info->flags = clause->flags;
1151 info->try_offset = clause->try_offset;
1152 info->try_length = clause->try_len;
1153 info->handler_offset = clause->handler_offset;
1154 info->handler_length = clause->handler_len;
1155 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1156 info->filter_offset = clause->data.filter_offset;
1157 else if (clause->data.catch_class) {
1158 rt = mono_type_get_object_checked (mono_domain_get (), &clause->data.catch_class->byval_arg, error);
1162 MONO_OBJECT_SETREF (info, catch_type, rt);
1165 mono_array_setref (ret->clauses, i, info);
1168 mono_metadata_free_mh (header);
1169 CACHE_OBJECT (MonoReflectionMethodBody *, method, ret, NULL);
1173 mono_metadata_free_mh (header);
1178 * mono_get_dbnull_object:
1179 * @domain: Domain where the object lives
1181 * Returns the System.DBNull.Value singleton object
1183 * Used as the value for ParameterInfo.DefaultValue
1186 mono_get_dbnull_object (MonoDomain *domain)
1190 static MonoClassField *dbnull_value_field = NULL;
1192 if (!dbnull_value_field) {
1193 MonoClass *dbnull_klass;
1194 dbnull_klass = mono_class_get_dbnull_class ();
1195 dbnull_value_field = mono_class_get_field_from_name (dbnull_klass, "Value");
1196 g_assert (dbnull_value_field);
1198 obj = mono_field_get_value_object_checked (domain, dbnull_value_field, NULL, &error);
1199 mono_error_assert_ok (&error);
1204 get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types)
1206 guint32 param_index, i, lastp, crow = 0;
1207 guint32 param_cols [MONO_PARAM_SIZE], const_cols [MONO_CONSTANT_SIZE];
1210 MonoClass *klass = method->klass;
1211 MonoImage *image = klass->image;
1212 MonoMethodSignature *methodsig = mono_method_signature (method);
1214 MonoTableInfo *constt;
1215 MonoTableInfo *methodt;
1216 MonoTableInfo *paramt;
1218 if (!methodsig->param_count)
1221 mono_class_init (klass);
1223 if (image_is_dynamic (klass->image)) {
1224 MonoReflectionMethodAux *aux;
1225 if (method->is_inflated)
1226 method = ((MonoMethodInflated*)method)->declaring;
1227 aux = (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1228 if (aux && aux->param_defaults) {
1229 memcpy (blobs, &(aux->param_defaults [1]), methodsig->param_count * sizeof (char*));
1230 memcpy (types, &(aux->param_default_types [1]), methodsig->param_count * sizeof (guint32));
1235 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1236 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1237 constt = &image->tables [MONO_TABLE_CONSTANT];
1239 idx = mono_method_get_index (method) - 1;
1240 g_assert (idx != -1);
1242 param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1243 if (idx + 1 < methodt->rows)
1244 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1246 lastp = paramt->rows + 1;
1248 for (i = param_index; i < lastp; ++i) {
1251 mono_metadata_decode_row (paramt, i - 1, param_cols, MONO_PARAM_SIZE);
1252 paramseq = param_cols [MONO_PARAM_SEQUENCE];
1254 if (!(param_cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT))
1257 crow = mono_metadata_get_constant_index (image, MONO_TOKEN_PARAM_DEF | i, crow + 1);
1262 mono_metadata_decode_row (constt, crow - 1, const_cols, MONO_CONSTANT_SIZE);
1263 blobs [paramseq - 1] = (char *)mono_metadata_blob_heap (image, const_cols [MONO_CONSTANT_VALUE]);
1264 types [paramseq - 1] = const_cols [MONO_CONSTANT_TYPE];
1271 mono_get_object_from_blob (MonoDomain *domain, MonoType *type, const char *blob, MonoError *error)
1276 MonoType *basetype = type;
1278 mono_error_init (error);
1283 klass = mono_class_from_mono_type (type);
1284 if (klass->valuetype) {
1285 object = mono_object_new_checked (domain, klass, error);
1286 return_val_if_nok (error, NULL);
1287 retval = ((gchar *) object + sizeof (MonoObject));
1288 if (klass->enumtype)
1289 basetype = mono_class_enum_basetype (klass);
1294 if (!mono_get_constant_value_from_blob (domain, basetype->type, blob, retval, error))
1301 assembly_name_to_aname (MonoAssemblyName *assembly, char *p) {
1304 gboolean quoted = FALSE;
1306 memset (assembly, 0, sizeof (MonoAssemblyName));
1307 assembly->culture = "";
1308 memset (assembly->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1315 while (*p && (isalnum (*p) || *p == '.' || *p == '-' || *p == '_' || *p == '$' || *p == '@' || g_ascii_isspace (*p)))
1326 /* Remove trailing whitespace */
1328 while (*s && g_ascii_isspace (*s))
1331 while (g_ascii_isspace (*p))
1334 if (*p == 'V' && g_ascii_strncasecmp (p, "Version=", 8) == 0) {
1336 assembly->major = strtoul (p, &s, 10);
1337 if (s == p || *s != '.')
1340 assembly->minor = strtoul (p, &s, 10);
1341 if (s == p || *s != '.')
1344 assembly->build = strtoul (p, &s, 10);
1345 if (s == p || *s != '.')
1348 assembly->revision = strtoul (p, &s, 10);
1352 } else if (*p == 'C' && g_ascii_strncasecmp (p, "Culture=", 8) == 0) {
1354 if (g_ascii_strncasecmp (p, "neutral", 7) == 0) {
1355 assembly->culture = "";
1358 assembly->culture = p;
1359 while (*p && *p != ',') {
1363 } else if (*p == 'P' && g_ascii_strncasecmp (p, "PublicKeyToken=", 15) == 0) {
1365 if (strncmp (p, "null", 4) == 0) {
1370 while (*p && *p != ',') {
1373 len = (p - start + 1);
1374 if (len > MONO_PUBLIC_KEY_TOKEN_LENGTH)
1375 len = MONO_PUBLIC_KEY_TOKEN_LENGTH;
1376 g_strlcpy ((char*)assembly->public_key_token, start, len);
1379 while (*p && *p != ',')
1383 while (g_ascii_isspace (*p) || *p == ',') {
1397 * mono_reflection_parse_type:
1400 * Parse a type name as accepted by the GetType () method and output the info
1401 * extracted in the info structure.
1402 * the name param will be mangled, so, make a copy before passing it to this function.
1403 * The fields in info will be valid until the memory pointed to by name is valid.
1405 * See also mono_type_get_name () below.
1407 * Returns: 0 on parse error.
1410 _mono_reflection_parse_type (char *name, char **endptr, gboolean is_recursed,
1411 MonoTypeNameParse *info)
1413 char *start, *p, *w, *last_point, *startn;
1414 int in_modifiers = 0;
1415 int isbyref = 0, rank = 0, isptr = 0;
1417 start = p = w = name;
1419 //FIXME could we just zero the whole struct? memset (&info, 0, sizeof (MonoTypeNameParse))
1420 memset (&info->assembly, 0, sizeof (MonoAssemblyName));
1421 info->name = info->name_space = NULL;
1422 info->nested = NULL;
1423 info->modifiers = NULL;
1424 info->type_arguments = NULL;
1426 /* last_point separates the namespace from the name */
1429 while (*p == ' ') p++, start++, w++, name++;
1434 *p = 0; /* NULL terminate the name */
1436 info->nested = g_list_append (info->nested, startn);
1437 /* we have parsed the nesting namespace + name */
1441 info->name_space = start;
1443 info->name = last_point + 1;
1445 info->name_space = (char *)"";
1473 info->name_space = start;
1475 info->name = last_point + 1;
1477 info->name_space = (char *)"";
1484 if (isbyref) /* only one level allowed by the spec */
1488 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (0));
1492 if (isbyref) /* pointer to ref not okay */
1494 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-1));
1499 if (isbyref) /* array of ref and generic ref are not okay */
1501 //Decide if it's an array of a generic argument list
1506 if (*p == ',' || *p == '*' || *p == ']') { //array
1514 else if (*p == '*') /* '*' means unknown lower bound */
1515 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-2));
1522 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (rank));
1524 if (rank || isptr) /* generic args after array spec or ptr*/ //XXX test
1527 info->type_arguments = g_ptr_array_new ();
1529 MonoTypeNameParse *subinfo = g_new0 (MonoTypeNameParse, 1);
1530 gboolean fqname = FALSE;
1532 g_ptr_array_add (info->type_arguments, subinfo);
1534 while (*p == ' ') p++;
1540 if (!_mono_reflection_parse_type (p, &p, TRUE, subinfo))
1543 /*MS is lenient on [] delimited parameters that aren't fqn - and F# uses them.*/
1544 if (fqname && (*p != ']')) {
1552 while (*p && (*p != ']'))
1560 if (g_ascii_isspace (*aname)) {
1567 !assembly_name_to_aname (&subinfo->assembly, aname))
1569 } else if (fqname && (*p == ']')) {
1591 if (g_ascii_isspace (*p)) {
1598 return 0; /* missing assembly name */
1599 if (!assembly_name_to_aname (&info->assembly, p))
1605 if (info->assembly.name)
1608 // *w = 0; /* terminate class name */
1610 if (!info->name || !*info->name)
1614 /* add other consistency checks */
1620 * mono_identifier_unescape_type_name_chars:
1621 * @identifier: the display name of a mono type
1624 * The name in internal form, that is without escaping backslashes.
1626 * The string is modified in place!
1629 mono_identifier_unescape_type_name_chars(char* identifier)
1634 for (w = r = identifier; *r != 0; r++)
1652 mono_identifier_unescape_info (MonoTypeNameParse* info);
1655 unescape_each_type_argument(void* data, void* user_data)
1657 MonoTypeNameParse* info = (MonoTypeNameParse*)data;
1658 mono_identifier_unescape_info (info);
1662 unescape_each_nested_name (void* data, void* user_data)
1664 char* nested_name = (char*) data;
1665 mono_identifier_unescape_type_name_chars(nested_name);
1669 * mono_identifier_unescape_info:
1671 * @info: a parsed display form of an (optionally assembly qualified) full type name.
1675 * Destructively updates the info by unescaping the identifiers that
1676 * comprise the type namespace, name, nested types (if any) and
1677 * generic type arguments (if any).
1679 * The resulting info has the names in internal form.
1683 mono_identifier_unescape_info (MonoTypeNameParse *info)
1687 mono_identifier_unescape_type_name_chars(info->name_space);
1688 mono_identifier_unescape_type_name_chars(info->name);
1689 // but don't escape info->assembly
1690 if (info->type_arguments)
1691 g_ptr_array_foreach(info->type_arguments, &unescape_each_type_argument, NULL);
1693 g_list_foreach(info->nested, &unescape_each_nested_name, NULL);
1697 mono_reflection_parse_type (char *name, MonoTypeNameParse *info)
1699 int ok = _mono_reflection_parse_type (name, NULL, FALSE, info);
1701 mono_identifier_unescape_info (info);
1707 _mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, gboolean ignorecase, MonoError *error)
1709 gboolean type_resolve = FALSE;
1711 MonoImage *rootimage = image;
1713 mono_error_init (error);
1715 if (info->assembly.name) {
1716 MonoAssembly *assembly = mono_assembly_loaded (&info->assembly);
1717 if (!assembly && image && image->assembly && mono_assembly_names_equal (&info->assembly, &image->assembly->aname))
1719 * This could happen in the AOT compiler case when the search hook is not
1722 assembly = image->assembly;
1724 /* then we must load the assembly ourselve - see #60439 */
1725 assembly = mono_assembly_load (&info->assembly, image->assembly->basedir, NULL);
1729 image = assembly->image;
1730 } else if (!image) {
1731 image = mono_defaults.corlib;
1734 type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1735 if (type == NULL && !info->assembly.name && image != mono_defaults.corlib) {
1736 /* ignore the error and try again */
1737 mono_error_cleanup (error);
1738 mono_error_init (error);
1739 image = mono_defaults.corlib;
1740 type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1747 * mono_reflection_get_type_internal:
1749 * Returns: may return NULL on success, sets error on failure.
1752 mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
1757 gboolean bounded = FALSE;
1759 mono_error_init (error);
1761 image = mono_defaults.corlib;
1764 rootimage = mono_defaults.corlib;
1767 klass = mono_class_from_name_case_checked (image, info->name_space, info->name, error);
1769 klass = mono_class_from_name_checked (image, info->name_space, info->name, error);
1774 for (mod = info->nested; mod; mod = mod->next) {
1775 gpointer iter = NULL;
1779 mono_class_init (parent);
1781 while ((klass = mono_class_get_nested_types (parent, &iter))) {
1783 char *nested_name, *nested_nspace;
1784 gboolean match = TRUE;
1786 lastp = strrchr ((const char *)mod->data, '.');
1788 /* Nested classes can have namespaces */
1791 nested_name = g_strdup (lastp + 1);
1792 nspace_len = lastp - (char*)mod->data;
1793 nested_nspace = (char *)g_malloc (nspace_len + 1);
1794 memcpy (nested_nspace, mod->data, nspace_len);
1795 nested_nspace [nspace_len] = '\0';
1798 nested_name = (char *)mod->data;
1799 nested_nspace = NULL;
1802 if (nested_nspace) {
1804 if (!(klass->name_space && mono_utf8_strcasecmp (klass->name_space, nested_nspace) == 0))
1807 if (!(klass->name_space && strcmp (klass->name_space, nested_nspace) == 0))
1813 if (mono_utf8_strcasecmp (klass->name, nested_name) != 0)
1816 if (strcmp (klass->name, nested_name) != 0)
1821 g_free (nested_name);
1822 g_free (nested_nspace);
1834 if (info->type_arguments) {
1835 MonoType **type_args = g_new0 (MonoType *, info->type_arguments->len);
1836 MonoReflectionType *the_type;
1840 for (i = 0; i < info->type_arguments->len; i++) {
1841 MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
1843 type_args [i] = _mono_reflection_get_type_from_info (subinfo, rootimage, ignorecase, error);
1844 if (!type_args [i]) {
1850 the_type = mono_type_get_object_checked (mono_domain_get (), &klass->byval_arg, error);
1854 instance = mono_reflection_bind_generic_parameters (
1855 the_type, info->type_arguments->len, type_args, error);
1861 klass = mono_class_from_mono_type (instance);
1864 for (mod = info->modifiers; mod; mod = mod->next) {
1865 modval = GPOINTER_TO_UINT (mod->data);
1866 if (!modval) { /* byref: must be last modifier */
1867 return &klass->this_arg;
1868 } else if (modval == -1) {
1869 klass = mono_ptr_class_get (&klass->byval_arg);
1870 } else if (modval == -2) {
1872 } else { /* array rank */
1873 klass = mono_bounded_array_class_get (klass, modval, bounded);
1877 return &klass->byval_arg;
1881 * mono_reflection_get_type:
1882 * @image: a metadata context
1883 * @info: type description structure
1884 * @ignorecase: flag for case-insensitive string compares
1885 * @type_resolve: whenever type resolve was already tried
1887 * Build a MonoType from the type description in @info.
1892 mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve) {
1894 MonoType *result = mono_reflection_get_type_with_rootimage (image, image, info, ignorecase, type_resolve, &error);
1895 mono_error_cleanup (&error);
1900 * mono_reflection_get_type_checked:
1901 * @rootimage: the image of the currently active managed caller
1902 * @image: a metadata context
1903 * @info: type description structure
1904 * @ignorecase: flag for case-insensitive string compares
1905 * @type_resolve: whenever type resolve was already tried
1906 * @error: set on error.
1908 * Build a MonoType from the type description in @info. On failure returns NULL and sets @error.
1912 mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error) {
1913 mono_error_init (error);
1914 return mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, type_resolve, error);
1919 mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *assembly, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
1921 MonoReflectionAssemblyBuilder *abuilder;
1925 mono_error_init (error);
1926 g_assert (assembly_is_dynamic (assembly));
1927 abuilder = (MonoReflectionAssemblyBuilder*)mono_assembly_get_object_checked (((MonoDynamicAssembly*)assembly)->domain, assembly, error);
1931 /* Enumerate all modules */
1934 if (abuilder->modules) {
1935 for (i = 0; i < mono_array_length (abuilder->modules); ++i) {
1936 MonoReflectionModuleBuilder *mb = mono_array_get (abuilder->modules, MonoReflectionModuleBuilder*, i);
1937 type = mono_reflection_get_type_internal (rootimage, &mb->dynamic_image->image, info, ignorecase, error);
1940 if (!mono_error_ok (error))
1945 if (!type && abuilder->loaded_modules) {
1946 for (i = 0; i < mono_array_length (abuilder->loaded_modules); ++i) {
1947 MonoReflectionModule *mod = mono_array_get (abuilder->loaded_modules, MonoReflectionModule*, i);
1948 type = mono_reflection_get_type_internal (rootimage, mod->image, info, ignorecase, error);
1951 if (!mono_error_ok (error))
1960 mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error)
1963 MonoReflectionAssembly *assembly;
1967 mono_error_init (error);
1969 if (image && image_is_dynamic (image))
1970 type = mono_reflection_get_type_internal_dynamic (rootimage, image->assembly, info, ignorecase, error);
1972 type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
1974 return_val_if_nok (error, NULL);
1978 if (!mono_domain_has_type_resolve (mono_domain_get ()))
1985 *type_resolve = TRUE;
1988 /* Reconstruct the type name */
1989 fullName = g_string_new ("");
1990 if (info->name_space && (info->name_space [0] != '\0'))
1991 g_string_printf (fullName, "%s.%s", info->name_space, info->name);
1993 g_string_printf (fullName, "%s", info->name);
1994 for (mod = info->nested; mod; mod = mod->next)
1995 g_string_append_printf (fullName, "+%s", (char*)mod->data);
1997 assembly = mono_domain_try_type_resolve_checked ( mono_domain_get (), fullName->str, NULL, error);
1998 if (!is_ok (error)) {
1999 g_string_free (fullName, TRUE);
2004 if (assembly_is_dynamic (assembly->assembly))
2005 type = mono_reflection_get_type_internal_dynamic (rootimage, assembly->assembly,
2006 info, ignorecase, error);
2008 type = mono_reflection_get_type_internal (rootimage, assembly->assembly->image,
2009 info, ignorecase, error);
2011 g_string_free (fullName, TRUE);
2012 return_val_if_nok (error, NULL);
2017 mono_reflection_free_type_info (MonoTypeNameParse *info)
2019 g_list_free (info->modifiers);
2020 g_list_free (info->nested);
2022 if (info->type_arguments) {
2025 for (i = 0; i < info->type_arguments->len; i++) {
2026 MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
2028 mono_reflection_free_type_info (subinfo);
2029 /*We free the subinfo since it is allocated by _mono_reflection_parse_type*/
2033 g_ptr_array_free (info->type_arguments, TRUE);
2038 * mono_reflection_type_from_name:
2040 * @image: a metadata context (can be NULL).
2042 * Retrieves a MonoType from its @name. If the name is not fully qualified,
2043 * it defaults to get the type from @image or, if @image is NULL or loading
2044 * from it fails, uses corlib.
2048 mono_reflection_type_from_name (char *name, MonoImage *image)
2051 MonoType *result = mono_reflection_type_from_name_checked (name, image, &error);
2052 mono_error_cleanup (&error);
2057 * mono_reflection_type_from_name_checked:
2059 * @image: a metadata context (can be NULL).
2060 * @error: set on errror.
2062 * Retrieves a MonoType from its @name. If the name is not fully qualified,
2063 * it defaults to get the type from @image or, if @image is NULL or loading
2064 * from it fails, uses corlib. On failure returns NULL and sets @error.
2068 mono_reflection_type_from_name_checked (char *name, MonoImage *image, MonoError *error)
2070 MonoType *type = NULL;
2071 MonoTypeNameParse info;
2074 mono_error_init (error);
2075 /* Make a copy since parse_type modifies its argument */
2076 tmp = g_strdup (name);
2078 /*g_print ("requested type %s\n", str);*/
2079 if (mono_reflection_parse_type (tmp, &info)) {
2080 type = _mono_reflection_get_type_from_info (&info, image, FALSE, error);
2081 if (!is_ok (error)) {
2083 mono_reflection_free_type_info (&info);
2089 mono_reflection_free_type_info (&info);
2094 * mono_reflection_get_token:
2096 * Return the metadata token of OBJ which should be an object
2097 * representing a metadata element.
2100 mono_reflection_get_token (MonoObject *obj)
2103 guint32 result = mono_reflection_get_token_checked (obj, &error);
2104 mono_error_assert_ok (&error);
2109 * mono_reflection_get_token_checked:
2111 * @error: set on error
2113 * Return the metadata token of @obj which should be an object
2114 * representing a metadata element. On failure sets @error.
2117 mono_reflection_get_token_checked (MonoObject *obj, MonoError *error)
2122 mono_error_init (error);
2124 klass = obj->vtable->klass;
2126 if (strcmp (klass->name, "MethodBuilder") == 0) {
2127 MonoReflectionMethodBuilder *mb = (MonoReflectionMethodBuilder *)obj;
2129 token = mb->table_idx | MONO_TOKEN_METHOD_DEF;
2130 } else if (strcmp (klass->name, "ConstructorBuilder") == 0) {
2131 MonoReflectionCtorBuilder *mb = (MonoReflectionCtorBuilder *)obj;
2133 token = mb->table_idx | MONO_TOKEN_METHOD_DEF;
2134 } else if (strcmp (klass->name, "FieldBuilder") == 0) {
2135 MonoReflectionFieldBuilder *fb = (MonoReflectionFieldBuilder *)obj;
2137 token = fb->table_idx | MONO_TOKEN_FIELD_DEF;
2138 } else if (strcmp (klass->name, "TypeBuilder") == 0) {
2139 MonoReflectionTypeBuilder *tb = (MonoReflectionTypeBuilder *)obj;
2140 token = tb->table_idx | MONO_TOKEN_TYPE_DEF;
2141 } else if (strcmp (klass->name, "RuntimeType") == 0) {
2142 MonoType *type = mono_reflection_type_get_handle ((MonoReflectionType*)obj, error);
2143 return_val_if_nok (error, 0);
2144 MonoClass *mc = mono_class_from_mono_type (type);
2145 if (!mono_class_init (mc)) {
2146 mono_error_set_for_class_failure (error, mc);
2150 token = mc->type_token;
2151 } else if (strcmp (klass->name, "MonoCMethod") == 0 ||
2152 strcmp (klass->name, "MonoMethod") == 0 ||
2153 strcmp (klass->name, "MonoGenericMethod") == 0 ||
2154 strcmp (klass->name, "MonoGenericCMethod") == 0) {
2155 MonoReflectionMethod *m = (MonoReflectionMethod *)obj;
2156 if (m->method->is_inflated) {
2157 MonoMethodInflated *inflated = (MonoMethodInflated *) m->method;
2158 return inflated->declaring->token;
2160 token = m->method->token;
2162 } else if (strcmp (klass->name, "MonoField") == 0) {
2163 MonoReflectionField *f = (MonoReflectionField*)obj;
2165 token = mono_class_get_field_token (f->field);
2166 } else if (strcmp (klass->name, "MonoProperty") == 0) {
2167 MonoReflectionProperty *p = (MonoReflectionProperty*)obj;
2169 token = mono_class_get_property_token (p->property);
2170 } else if (strcmp (klass->name, "MonoEvent") == 0) {
2171 MonoReflectionMonoEvent *p = (MonoReflectionMonoEvent*)obj;
2173 token = mono_class_get_event_token (p->event);
2174 } else if (strcmp (klass->name, "ParameterInfo") == 0 || strcmp (klass->name, "MonoParameterInfo") == 0) {
2175 MonoReflectionParameter *p = (MonoReflectionParameter*)obj;
2176 MonoClass *member_class = mono_object_class (p->MemberImpl);
2177 g_assert (mono_class_is_reflection_method_or_constructor (member_class));
2179 token = mono_method_get_param_token (((MonoReflectionMethod*)p->MemberImpl)->method, p->PositionImpl);
2180 } else if (strcmp (klass->name, "Module") == 0 || strcmp (klass->name, "MonoModule") == 0) {
2181 MonoReflectionModule *m = (MonoReflectionModule*)obj;
2184 } else if (strcmp (klass->name, "Assembly") == 0 || strcmp (klass->name, "MonoAssembly") == 0) {
2185 token = mono_metadata_make_token (MONO_TABLE_ASSEMBLY, 1);
2187 mono_error_set_not_implemented (error, "MetadataToken is not supported for type '%s.%s'",
2188 klass->name_space, klass->name);
2197 mono_reflection_is_usertype (MonoReflectionType *ref)
2199 MonoClass *klass = mono_object_class (ref);
2200 return klass->image != mono_defaults.corlib || strcmp ("TypeDelegator", klass->name) == 0;
2204 * mono_reflection_bind_generic_parameters:
2205 * @type: a managed type object (which should be some kind of generic (instance? definition?))
2206 * @type_args: the number of type arguments to bind
2207 * @types: array of type arguments
2208 * @error: set on error
2210 * Given a managed type object for a generic type instance, binds each of its arguments to the specified types.
2211 * Returns the MonoType* for the resulting type instantiation. On failure returns NULL and sets @error.
2214 mono_reflection_bind_generic_parameters (MonoReflectionType *type, int type_argc, MonoType **types, MonoError *error)
2217 MonoReflectionTypeBuilder *tb = NULL;
2218 gboolean is_dynamic = FALSE;
2221 mono_error_init (error);
2223 mono_loader_lock ();
2225 if (mono_is_sre_type_builder (mono_object_class (type))) {
2226 tb = (MonoReflectionTypeBuilder *) type;
2229 } else if (mono_is_sre_generic_instance (mono_object_class (type))) {
2230 MonoReflectionGenericClass *rgi = (MonoReflectionGenericClass *) type;
2231 MonoReflectionType *gtd = rgi->generic_type;
2233 if (mono_is_sre_type_builder (mono_object_class (gtd))) {
2234 tb = (MonoReflectionTypeBuilder *)gtd;
2239 /* FIXME: fix the CreateGenericParameters protocol to avoid the two stage setup of TypeBuilders */
2240 if (tb && tb->generic_container) {
2241 if (!mono_reflection_create_generic_class (tb, error)) {
2242 mono_loader_unlock ();
2247 MonoType *t = mono_reflection_type_get_handle (type, error);
2248 if (!is_ok (error)) {
2249 mono_loader_unlock ();
2253 klass = mono_class_from_mono_type (t);
2254 if (!klass->generic_container) {
2255 mono_loader_unlock ();
2256 mono_error_set_type_load_class (error, klass, "Cannot bind generic parameters of a non-generic type");
2260 if (klass->wastypebuilder) {
2261 tb = (MonoReflectionTypeBuilder *) mono_class_get_ref_info (klass);
2266 mono_loader_unlock ();
2268 geninst = mono_class_bind_generic_parameters (klass, type_argc, types, is_dynamic);
2270 return &geninst->byval_arg;
2274 mono_class_bind_generic_parameters (MonoClass *klass, int type_argc, MonoType **types, gboolean is_dynamic)
2276 MonoGenericClass *gclass;
2277 MonoGenericInst *inst;
2279 g_assert (klass->generic_container);
2281 inst = mono_metadata_get_generic_inst (type_argc, types);
2282 gclass = mono_metadata_lookup_generic_class (klass, inst, is_dynamic);
2284 return mono_generic_class_get_class (gclass);
2287 static MonoReflectionMethod*
2288 reflection_bind_generic_method_parameters (MonoReflectionMethod *rmethod, MonoArray *types, MonoError *error)
2291 MonoMethod *method, *inflated;
2292 MonoMethodInflated *imethod;
2293 MonoGenericContext tmp_context;
2294 MonoGenericInst *ginst;
2295 MonoType **type_argv;
2298 mono_error_init (error);
2300 /*FIXME but this no longer should happen*/
2301 if (!strcmp (rmethod->object.vtable->klass->name, "MethodBuilder")) {
2302 method = mono_reflection_method_builder_to_mono_method ((MonoReflectionMethodBuilder*)rmethod, error);
2303 return_val_if_nok (error, NULL);
2305 method = rmethod->method;
2308 klass = method->klass;
2310 if (method->is_inflated)
2311 method = ((MonoMethodInflated *) method)->declaring;
2313 count = mono_method_signature (method)->generic_param_count;
2314 if (count != mono_array_length (types))
2317 type_argv = g_new0 (MonoType *, count);
2318 for (i = 0; i < count; i++) {
2319 MonoReflectionType *garg = (MonoReflectionType *)mono_array_get (types, gpointer, i);
2320 type_argv [i] = mono_reflection_type_get_handle (garg, error);
2321 if (!is_ok (error)) {
2326 ginst = mono_metadata_get_generic_inst (count, type_argv);
2329 tmp_context.class_inst = klass->generic_class ? klass->generic_class->context.class_inst : NULL;
2330 tmp_context.method_inst = ginst;
2332 inflated = mono_class_inflate_generic_method_checked (method, &tmp_context, error);
2333 mono_error_assert_ok (error);
2334 imethod = (MonoMethodInflated *) inflated;
2336 /*FIXME but I think this is no longer necessary*/
2337 if (image_is_dynamic (method->klass->image)) {
2338 MonoDynamicImage *image = (MonoDynamicImage*)method->klass->image;
2340 * This table maps metadata structures representing inflated methods/fields
2341 * to the reflection objects representing their generic definitions.
2343 mono_image_lock ((MonoImage*)image);
2344 mono_g_hash_table_insert (image->generic_def_objects, imethod, rmethod);
2345 mono_image_unlock ((MonoImage*)image);
2348 if (!mono_verifier_is_method_valid_generic_instantiation (inflated)) {
2349 mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
2353 MonoReflectionMethod *ret = mono_method_get_object_checked (mono_object_domain (rmethod), inflated, NULL, error);
2357 MonoReflectionMethod*
2358 ves_icall_MethodBuilder_MakeGenericMethod (MonoReflectionMethod *rmethod, MonoArray *types)
2361 MonoReflectionMethod *result = reflection_bind_generic_method_parameters (rmethod, types, &error);
2362 mono_error_set_pending_exception (&error);
2366 MonoReflectionMethod*
2367 ves_icall_MonoMethod_MakeGenericMethod_impl (MonoReflectionMethod *rmethod, MonoArray *types)
2370 MonoReflectionMethod *result = reflection_bind_generic_method_parameters (rmethod, types, &error);
2371 mono_error_set_pending_exception (&error);
2376 /* SECURITY_ACTION_* are defined in mono/metadata/tabledefs.h */
2377 const static guint32 declsec_flags_map[] = {
2378 0x00000000, /* empty */
2379 MONO_DECLSEC_FLAG_REQUEST, /* SECURITY_ACTION_REQUEST (x01) */
2380 MONO_DECLSEC_FLAG_DEMAND, /* SECURITY_ACTION_DEMAND (x02) */
2381 MONO_DECLSEC_FLAG_ASSERT, /* SECURITY_ACTION_ASSERT (x03) */
2382 MONO_DECLSEC_FLAG_DENY, /* SECURITY_ACTION_DENY (x04) */
2383 MONO_DECLSEC_FLAG_PERMITONLY, /* SECURITY_ACTION_PERMITONLY (x05) */
2384 MONO_DECLSEC_FLAG_LINKDEMAND, /* SECURITY_ACTION_LINKDEMAND (x06) */
2385 MONO_DECLSEC_FLAG_INHERITANCEDEMAND, /* SECURITY_ACTION_INHERITANCEDEMAND (x07) */
2386 MONO_DECLSEC_FLAG_REQUEST_MINIMUM, /* SECURITY_ACTION_REQUEST_MINIMUM (x08) */
2387 MONO_DECLSEC_FLAG_REQUEST_OPTIONAL, /* SECURITY_ACTION_REQUEST_OPTIONAL (x09) */
2388 MONO_DECLSEC_FLAG_REQUEST_REFUSE, /* SECURITY_ACTION_REQUEST_REFUSE (x0A) */
2389 MONO_DECLSEC_FLAG_PREJIT_GRANT, /* SECURITY_ACTION_PREJIT_GRANT (x0B) */
2390 MONO_DECLSEC_FLAG_PREJIT_DENY, /* SECURITY_ACTION_PREJIT_DENY (x0C) */
2391 MONO_DECLSEC_FLAG_NONCAS_DEMAND, /* SECURITY_ACTION_NONCAS_DEMAND (x0D) */
2392 MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND, /* SECURITY_ACTION_NONCAS_LINKDEMAND (x0E) */
2393 MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND, /* SECURITY_ACTION_NONCAS_INHERITANCEDEMAND (x0F) */
2394 MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE, /* SECURITY_ACTION_LINKDEMAND_CHOICE (x10) */
2395 MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE, /* SECURITY_ACTION_INHERITANCEDEMAND_CHOICE (x11) */
2396 MONO_DECLSEC_FLAG_DEMAND_CHOICE, /* SECURITY_ACTION_DEMAND_CHOICE (x12) */
2400 * Returns flags that includes all available security action associated to the handle.
2401 * @token: metadata token (either for a class or a method)
2402 * @image: image where resides the metadata.
2405 mono_declsec_get_flags (MonoImage *image, guint32 token)
2407 int index = mono_metadata_declsec_from_index (image, token);
2408 MonoTableInfo *t = &image->tables [MONO_TABLE_DECLSECURITY];
2413 /* HasSecurity can be present for other, not specially encoded, attributes,
2414 e.g. SuppressUnmanagedCodeSecurityAttribute */
2418 for (i = index; i < t->rows; i++) {
2419 guint32 cols [MONO_DECL_SECURITY_SIZE];
2421 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2422 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2425 action = cols [MONO_DECL_SECURITY_ACTION];
2426 if ((action >= MONO_DECLSEC_ACTION_MIN) && (action <= MONO_DECLSEC_ACTION_MAX)) {
2427 result |= declsec_flags_map [action];
2429 g_assert_not_reached ();
2436 * Get the security actions (in the form of flags) associated with the specified method.
2438 * @method: The method for which we want the declarative security flags.
2439 * Return the declarative security flags for the method (only).
2441 * Note: To keep MonoMethod size down we do not cache the declarative security flags
2442 * (except for the stack modifiers which are kept in the MonoJitInfo structure)
2445 mono_declsec_flags_from_method (MonoMethod *method)
2447 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2448 /* FIXME: No cache (for the moment) */
2449 guint32 idx = mono_method_get_index (method);
2450 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2451 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2452 return mono_declsec_get_flags (method->klass->image, idx);
2458 * Get the security actions (in the form of flags) associated with the specified class.
2460 * @klass: The class for which we want the declarative security flags.
2461 * Return the declarative security flags for the class.
2463 * Note: We cache the flags inside the MonoClass structure as this will get
2464 * called very often (at least for each method).
2467 mono_declsec_flags_from_class (MonoClass *klass)
2469 if (klass->flags & TYPE_ATTRIBUTE_HAS_SECURITY) {
2470 if (!klass->ext || !klass->ext->declsec_flags) {
2473 idx = mono_metadata_token_index (klass->type_token);
2474 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2475 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2476 mono_loader_lock ();
2477 mono_class_alloc_ext (klass);
2478 mono_loader_unlock ();
2479 /* we cache the flags on classes */
2480 klass->ext->declsec_flags = mono_declsec_get_flags (klass->image, idx);
2482 return klass->ext->declsec_flags;
2488 * Get the security actions (in the form of flags) associated with the specified assembly.
2490 * @assembly: The assembly for which we want the declarative security flags.
2491 * Return the declarative security flags for the assembly.
2494 mono_declsec_flags_from_assembly (MonoAssembly *assembly)
2496 guint32 idx = 1; /* there is only one assembly */
2497 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2498 idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2499 return mono_declsec_get_flags (assembly->image, idx);
2504 * Fill actions for the specific index (which may either be an encoded class token or
2505 * an encoded method token) from the metadata image.
2506 * Returns TRUE if some actions requiring code generation are present, FALSE otherwise.
2509 fill_actions_from_index (MonoImage *image, guint32 token, MonoDeclSecurityActions* actions,
2510 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2512 MonoBoolean result = FALSE;
2514 guint32 cols [MONO_DECL_SECURITY_SIZE];
2515 int index = mono_metadata_declsec_from_index (image, token);
2518 t = &image->tables [MONO_TABLE_DECLSECURITY];
2519 for (i = index; i < t->rows; i++) {
2520 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2522 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2525 /* if present only replace (class) permissions with method permissions */
2526 /* if empty accept either class or method permissions */
2527 if (cols [MONO_DECL_SECURITY_ACTION] == id_std) {
2528 if (!actions->demand.blob) {
2529 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2530 actions->demand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2531 actions->demand.blob = (char*) (blob + 2);
2532 actions->demand.size = mono_metadata_decode_blob_size (blob, &blob);
2535 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_noncas) {
2536 if (!actions->noncasdemand.blob) {
2537 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2538 actions->noncasdemand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2539 actions->noncasdemand.blob = (char*) (blob + 2);
2540 actions->noncasdemand.size = mono_metadata_decode_blob_size (blob, &blob);
2543 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_choice) {
2544 if (!actions->demandchoice.blob) {
2545 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2546 actions->demandchoice.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2547 actions->demandchoice.blob = (char*) (blob + 2);
2548 actions->demandchoice.size = mono_metadata_decode_blob_size (blob, &blob);
2558 mono_declsec_get_class_demands_params (MonoClass *klass, MonoDeclSecurityActions* demands,
2559 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2561 guint32 idx = mono_metadata_token_index (klass->type_token);
2562 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2563 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2564 return fill_actions_from_index (klass->image, idx, demands, id_std, id_noncas, id_choice);
2568 mono_declsec_get_method_demands_params (MonoMethod *method, MonoDeclSecurityActions* demands,
2569 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2571 guint32 idx = mono_method_get_index (method);
2572 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2573 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2574 return fill_actions_from_index (method->klass->image, idx, demands, id_std, id_noncas, id_choice);
2578 * Collect all actions (that requires to generate code in mini) assigned for
2579 * the specified method.
2580 * Note: Don't use the content of actions if the function return FALSE.
2583 mono_declsec_get_demands (MonoMethod *method, MonoDeclSecurityActions* demands)
2585 guint32 mask = MONO_DECLSEC_FLAG_DEMAND | MONO_DECLSEC_FLAG_NONCAS_DEMAND |
2586 MONO_DECLSEC_FLAG_DEMAND_CHOICE;
2587 MonoBoolean result = FALSE;
2590 /* quick exit if no declarative security is present in the metadata */
2591 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2594 /* we want the original as the wrapper is "free" of the security informations */
2595 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2596 method = mono_marshal_method_from_wrapper (method);
2601 /* First we look for method-level attributes */
2602 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2603 mono_class_init (method->klass);
2604 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2606 result = mono_declsec_get_method_demands_params (method, demands,
2607 SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2610 /* Here we use (or create) the class declarative cache to look for demands */
2611 flags = mono_declsec_flags_from_class (method->klass);
2614 mono_class_init (method->klass);
2615 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2617 result |= mono_declsec_get_class_demands_params (method->klass, demands,
2618 SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2621 /* The boolean return value is used as a shortcut in case nothing needs to
2622 be generated (e.g. LinkDemand[Choice] and InheritanceDemand[Choice]) */
2628 * Collect all Link actions: LinkDemand, NonCasLinkDemand and LinkDemandChoice (2.0).
2630 * Note: Don't use the content of actions if the function return FALSE.
2633 mono_declsec_get_linkdemands (MonoMethod *method, MonoDeclSecurityActions* klass, MonoDeclSecurityActions *cmethod)
2635 MonoBoolean result = FALSE;
2638 /* quick exit if no declarative security is present in the metadata */
2639 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2642 /* we want the original as the wrapper is "free" of the security informations */
2643 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2644 method = mono_marshal_method_from_wrapper (method);
2649 /* results are independant - zeroize both */
2650 memset (cmethod, 0, sizeof (MonoDeclSecurityActions));
2651 memset (klass, 0, sizeof (MonoDeclSecurityActions));
2653 /* First we look for method-level attributes */
2654 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2655 mono_class_init (method->klass);
2657 result = mono_declsec_get_method_demands_params (method, cmethod,
2658 SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2661 /* Here we use (or create) the class declarative cache to look for demands */
2662 flags = mono_declsec_flags_from_class (method->klass);
2663 if (flags & (MONO_DECLSEC_FLAG_LINKDEMAND | MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND | MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE)) {
2664 mono_class_init (method->klass);
2666 result |= mono_declsec_get_class_demands_params (method->klass, klass,
2667 SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2674 * Collect all Inherit actions: InheritanceDemand, NonCasInheritanceDemand and InheritanceDemandChoice (2.0).
2676 * @klass The inherited class - this is the class that provides the security check (attributes)
2678 * return TRUE if inheritance demands (any kind) are present, FALSE otherwise.
2680 * Note: Don't use the content of actions if the function return FALSE.
2683 mono_declsec_get_inheritdemands_class (MonoClass *klass, MonoDeclSecurityActions* demands)
2685 MonoBoolean result = FALSE;
2688 /* quick exit if no declarative security is present in the metadata */
2689 if (!klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2692 /* Here we use (or create) the class declarative cache to look for demands */
2693 flags = mono_declsec_flags_from_class (klass);
2694 if (flags & (MONO_DECLSEC_FLAG_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE)) {
2695 mono_class_init (klass);
2696 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2698 result |= mono_declsec_get_class_demands_params (klass, demands,
2699 SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2706 * Collect all Inherit actions: InheritanceDemand, NonCasInheritanceDemand and InheritanceDemandChoice (2.0).
2708 * Note: Don't use the content of actions if the function return FALSE.
2711 mono_declsec_get_inheritdemands_method (MonoMethod *method, MonoDeclSecurityActions* demands)
2713 /* quick exit if no declarative security is present in the metadata */
2714 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2717 /* we want the original as the wrapper is "free" of the security informations */
2718 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2719 method = mono_marshal_method_from_wrapper (method);
2724 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2725 mono_class_init (method->klass);
2726 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2728 return mono_declsec_get_method_demands_params (method, demands,
2729 SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2736 get_declsec_action (MonoImage *image, guint32 token, guint32 action, MonoDeclSecurityEntry *entry)
2738 guint32 cols [MONO_DECL_SECURITY_SIZE];
2742 int index = mono_metadata_declsec_from_index (image, token);
2746 t = &image->tables [MONO_TABLE_DECLSECURITY];
2747 for (i = index; i < t->rows; i++) {
2748 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2750 /* shortcut - index are ordered */
2751 if (token != cols [MONO_DECL_SECURITY_PARENT])
2754 if (cols [MONO_DECL_SECURITY_ACTION] == action) {
2755 const char *metadata = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2756 entry->blob = (char*) (metadata + 2);
2757 entry->size = mono_metadata_decode_blob_size (metadata, &metadata);
2766 mono_declsec_get_method_action (MonoMethod *method, guint32 action, MonoDeclSecurityEntry *entry)
2768 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2769 guint32 idx = mono_method_get_index (method);
2770 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2771 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2772 return get_declsec_action (method->klass->image, idx, action, entry);
2778 mono_declsec_get_class_action (MonoClass *klass, guint32 action, MonoDeclSecurityEntry *entry)
2781 guint32 flags = mono_declsec_flags_from_class (klass);
2782 if (declsec_flags_map [action] & flags) {
2783 guint32 idx = mono_metadata_token_index (klass->type_token);
2784 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2785 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2786 return get_declsec_action (klass->image, idx, action, entry);
2792 mono_declsec_get_assembly_action (MonoAssembly *assembly, guint32 action, MonoDeclSecurityEntry *entry)
2794 guint32 idx = 1; /* there is only one assembly */
2795 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2796 idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2798 return get_declsec_action (assembly->image, idx, action, entry);
2802 mono_reflection_call_is_assignable_to (MonoClass *klass, MonoClass *oklass, MonoError *error)
2804 MonoObject *res, *exc;
2806 static MonoMethod *method = NULL;
2808 mono_error_init (error);
2810 if (method == NULL) {
2811 method = mono_class_get_method_from_name (mono_class_get_type_builder_class (), "IsAssignableTo", 1);
2816 * The result of mono_type_get_object_checked () might be a System.MonoType but we
2817 * need a TypeBuilder so use mono_class_get_ref_info (klass).
2819 g_assert (mono_class_get_ref_info (klass));
2820 g_assert (!strcmp (((MonoObject*)(mono_class_get_ref_info (klass)))->vtable->klass->name, "TypeBuilder"));
2822 params [0] = mono_type_get_object_checked (mono_domain_get (), &oklass->byval_arg, error);
2823 return_val_if_nok (error, FALSE);
2825 MonoError inner_error;
2826 res = mono_runtime_try_invoke (method, (MonoObject*)(mono_class_get_ref_info (klass)), params, &exc, &inner_error);
2828 if (exc || !is_ok (&inner_error)) {
2829 mono_error_cleanup (&inner_error);
2832 return *(MonoBoolean*)mono_object_unbox (res);
2836 * mono_reflection_type_get_type:
2837 * @reftype: the System.Type object
2839 * Returns the MonoType* associated with the C# System.Type object @reftype.
2842 mono_reflection_type_get_type (MonoReflectionType *reftype)
2847 MonoType *result = mono_reflection_type_get_handle (reftype, &error);
2848 mono_error_assert_ok (&error);
2853 * mono_reflection_assembly_get_assembly:
2854 * @refassembly: the System.Reflection.Assembly object
2856 * Returns the MonoAssembly* associated with the C# System.Reflection.Assembly object @refassembly.
2859 mono_reflection_assembly_get_assembly (MonoReflectionAssembly *refassembly)
2861 g_assert (refassembly);
2863 return refassembly->assembly;