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>
49 #include <mono/utils/mono-counters.h>
51 static void get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types);
52 static MonoType* mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error);
54 /* Class lazy loading functions */
55 static GENERATE_GET_CLASS_WITH_CACHE (mono_assembly, System.Reflection, MonoAssembly)
56 static GENERATE_GET_CLASS_WITH_CACHE (mono_module, System.Reflection, MonoModule)
57 static GENERATE_GET_CLASS_WITH_CACHE (mono_method, System.Reflection, MonoMethod);
58 static GENERATE_GET_CLASS_WITH_CACHE (mono_cmethod, System.Reflection, MonoCMethod);
59 static GENERATE_GET_CLASS_WITH_CACHE (mono_field, System.Reflection, MonoField);
60 static GENERATE_GET_CLASS_WITH_CACHE (mono_event, System.Reflection, MonoEvent);
61 static GENERATE_GET_CLASS_WITH_CACHE (mono_property, System.Reflection, MonoProperty);
62 static GENERATE_GET_CLASS_WITH_CACHE (mono_parameter_info, System.Reflection, MonoParameterInfo);
63 static GENERATE_GET_CLASS_WITH_CACHE (missing, System.Reflection, Missing);
64 static GENERATE_GET_CLASS_WITH_CACHE (method_body, System.Reflection, MethodBody);
65 static GENERATE_GET_CLASS_WITH_CACHE (local_variable_info, System.Reflection, LocalVariableInfo);
66 static GENERATE_GET_CLASS_WITH_CACHE (exception_handling_clause, System.Reflection, ExceptionHandlingClause);
67 static GENERATE_GET_CLASS_WITH_CACHE (type_builder, System.Reflection.Emit, TypeBuilder);
68 static GENERATE_GET_CLASS_WITH_CACHE (dbnull, System, DBNull);
71 static int class_ref_info_handle_count;
74 mono_reflection_init (void)
76 mono_reflection_emit_init ();
78 mono_counters_register ("MonoClass::ref_info_handle count",
79 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &class_ref_info_handle_count);
84 * mono_class_get_ref_info:
86 * Return the type builder/generic param builder corresponding to KLASS, if it exists.
89 mono_class_get_ref_info (MonoClass *klass)
91 MONO_REQ_GC_UNSAFE_MODE;
93 if (klass->ref_info_handle == 0)
96 return mono_gchandle_get_target (klass->ref_info_handle);
100 mono_class_set_ref_info (MonoClass *klass, gpointer obj)
102 MONO_REQ_GC_UNSAFE_MODE;
104 klass->ref_info_handle = mono_gchandle_new ((MonoObject*)obj, FALSE);
105 ++class_ref_info_handle_count;
106 g_assert (klass->ref_info_handle != 0);
110 mono_class_free_ref_info (MonoClass *klass)
112 MONO_REQ_GC_NEUTRAL_MODE;
114 if (klass->ref_info_handle) {
115 mono_gchandle_free (klass->ref_info_handle);
116 klass->ref_info_handle = 0;
122 mono_custom_attrs_free (MonoCustomAttrInfo *ainfo)
124 MONO_REQ_GC_NEUTRAL_MODE;
126 if (ainfo && !ainfo->cached)
132 reflected_equal (gconstpointer a, gconstpointer b)
134 const ReflectedEntry *ea = (const ReflectedEntry *)a;
135 const ReflectedEntry *eb = (const ReflectedEntry *)b;
137 return (ea->item == eb->item) && (ea->refclass == eb->refclass);
141 reflected_hash (gconstpointer a) {
142 const ReflectedEntry *ea = (const ReflectedEntry *)a;
143 return mono_aligned_addr_hash (ea->item);
148 clear_cached_object (MonoDomain *domain, gpointer o, MonoClass *klass)
150 mono_domain_lock (domain);
151 if (domain->refobject_hash) {
153 gpointer orig_pe, orig_value;
157 if (mono_g_hash_table_lookup_extended (domain->refobject_hash, &pe, &orig_pe, &orig_value)) {
158 mono_g_hash_table_remove (domain->refobject_hash, &pe);
159 FREE_REFENTRY (orig_pe);
162 mono_domain_unlock (domain);
165 #ifdef REFENTRY_REQUIRES_CLEANUP
167 cleanup_refobject_hash (gpointer key, gpointer value, gpointer user_data)
174 mono_reflection_cleanup_domain (MonoDomain *domain)
176 if (domain->refobject_hash) {
177 /*let's avoid scanning the whole hashtable if not needed*/
178 #ifdef REFENTRY_REQUIRES_CLEANUP
179 mono_g_hash_table_foreach (domain->refobject_hash, cleanup_refobject_hash, NULL);
181 mono_g_hash_table_destroy (domain->refobject_hash);
182 domain->refobject_hash = NULL;
188 * mono_assembly_get_object:
189 * @domain: an app domain
190 * @assembly: an assembly
192 * Return an System.Reflection.Assembly object representing the MonoAssembly @assembly.
194 MonoReflectionAssembly*
195 mono_assembly_get_object (MonoDomain *domain, MonoAssembly *assembly)
198 MonoReflectionAssembly *result;
199 result = mono_assembly_get_object_checked (domain, assembly, &error);
200 mono_error_cleanup (&error); /* FIXME new API that doesn't swallow the error */
204 * mono_assembly_get_object_checked:
205 * @domain: an app domain
206 * @assembly: an assembly
208 * Return an System.Reflection.Assembly object representing the MonoAssembly @assembly.
210 MonoReflectionAssembly*
211 mono_assembly_get_object_checked (MonoDomain *domain, MonoAssembly *assembly, MonoError *error)
213 MonoReflectionAssembly *res;
215 mono_error_init (error);
217 CHECK_OBJECT (MonoReflectionAssembly *, assembly, NULL);
218 res = (MonoReflectionAssembly *)mono_object_new_checked (domain, mono_class_get_mono_assembly_class (), error);
221 res->assembly = assembly;
223 CACHE_OBJECT (MonoReflectionAssembly *, assembly, res, NULL);
228 MonoReflectionModule*
229 mono_module_get_object (MonoDomain *domain, MonoImage *image)
232 MonoReflectionModule *result;
233 result = mono_module_get_object_checked (domain, image, &error);
234 mono_error_cleanup (&error);
238 MonoReflectionModule*
239 mono_module_get_object_checked (MonoDomain *domain, MonoImage *image, MonoError *error)
241 MonoReflectionModule *res;
244 mono_error_init (error);
245 CHECK_OBJECT (MonoReflectionModule *, image, NULL);
246 res = (MonoReflectionModule *)mono_object_new_checked (domain, mono_class_get_mono_module_class (), error);
251 MonoReflectionAssembly *assm_obj = mono_assembly_get_object_checked (domain, image->assembly, error);
254 MONO_OBJECT_SETREF (res, assembly, assm_obj);
256 MONO_OBJECT_SETREF (res, fqname, mono_string_new (domain, image->name));
257 basename = g_path_get_basename (image->name);
258 MONO_OBJECT_SETREF (res, name, mono_string_new (domain, basename));
259 MONO_OBJECT_SETREF (res, scopename, mono_string_new (domain, image->module_name));
263 if (image->assembly->image == image) {
264 res->token = mono_metadata_make_token (MONO_TABLE_MODULE, 1);
268 if (image->assembly->image->modules) {
269 for (i = 0; i < image->assembly->image->module_count; i++) {
270 if (image->assembly->image->modules [i] == image)
271 res->token = mono_metadata_make_token (MONO_TABLE_MODULEREF, i + 1);
273 g_assert (res->token);
277 CACHE_OBJECT (MonoReflectionModule *, image, res, NULL);
280 MonoReflectionModule*
281 mono_module_file_get_object (MonoDomain *domain, MonoImage *image, int table_index)
284 MonoReflectionModule *result;
285 result = mono_module_file_get_object_checked (domain, image, table_index, &error);
286 mono_error_cleanup (&error);
290 MonoReflectionModule*
291 mono_module_file_get_object_checked (MonoDomain *domain, MonoImage *image, int table_index, MonoError *error)
293 MonoReflectionModule *res;
294 MonoTableInfo *table;
295 guint32 cols [MONO_FILE_SIZE];
300 mono_error_init (error);
302 res = (MonoReflectionModule *)mono_object_new_checked (domain, mono_class_get_mono_module_class (), error);
306 table = &image->tables [MONO_TABLE_FILE];
307 g_assert (table_index < table->rows);
308 mono_metadata_decode_row (table, table_index, cols, MONO_FILE_SIZE);
311 MonoReflectionAssembly *assm_obj = mono_assembly_get_object_checked (domain, image->assembly, error);
314 MONO_OBJECT_SETREF (res, assembly, assm_obj);
315 name = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
317 /* Check whenever the row has a corresponding row in the moduleref table */
318 table = &image->tables [MONO_TABLE_MODULEREF];
319 for (i = 0; i < table->rows; ++i) {
320 name_idx = mono_metadata_decode_row_col (table, i, MONO_MODULEREF_NAME);
321 val = mono_metadata_string_heap (image, name_idx);
322 if (strcmp (val, name) == 0)
323 res->image = image->modules [i];
326 MONO_OBJECT_SETREF (res, fqname, mono_string_new (domain, name));
327 MONO_OBJECT_SETREF (res, name, mono_string_new (domain, name));
328 MONO_OBJECT_SETREF (res, scopename, mono_string_new (domain, name));
329 res->is_resource = cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA;
330 res->token = mono_metadata_make_token (MONO_TABLE_FILE, table_index + 1);
336 mono_type_normalize (MonoType *type)
339 MonoGenericClass *gclass;
340 MonoGenericInst *ginst;
342 MonoGenericContainer *gcontainer;
343 MonoType **argv = NULL;
344 gboolean is_denorm_gtd = TRUE, requires_rebind = FALSE;
346 if (type->type != MONO_TYPE_GENERICINST)
349 gclass = type->data.generic_class;
350 ginst = gclass->context.class_inst;
354 gtd = gclass->container_class;
355 gcontainer = mono_class_get_generic_container (gtd);
356 argv = g_newa (MonoType*, ginst->type_argc);
358 for (i = 0; i < ginst->type_argc; ++i) {
359 MonoType *t = ginst->type_argv [i], *norm;
360 if (t->type != MONO_TYPE_VAR || t->data.generic_param->num != i || t->data.generic_param->owner != gcontainer)
361 is_denorm_gtd = FALSE;
362 norm = mono_type_normalize (t);
365 requires_rebind = TRUE;
369 return type->byref == gtd->byval_arg.byref ? >d->byval_arg : >d->this_arg;
371 if (requires_rebind) {
372 MonoClass *klass = mono_class_bind_generic_parameters (gtd, ginst->type_argc, argv, gclass->is_dynamic);
373 return type->byref == klass->byval_arg.byref ? &klass->byval_arg : &klass->this_arg;
379 * mono_type_get_object:
380 * @domain: an app domain
383 * Return an System.MonoType object representing the type @type.
386 mono_type_get_object (MonoDomain *domain, MonoType *type)
389 MonoReflectionType *ret = mono_type_get_object_checked (domain, type, &error);
390 mono_error_cleanup (&error);
396 mono_type_get_object_checked (MonoDomain *domain, MonoType *type, MonoError *error)
399 MonoReflectionType *res;
402 mono_error_init (error);
404 klass = mono_class_from_mono_type (type);
406 /*we must avoid using @type as it might have come
407 * from a mono_metadata_type_dup and the caller
408 * expects that is can be freed.
409 * Using the right type from
411 type = klass->byval_arg.byref == type->byref ? &klass->byval_arg : &klass->this_arg;
413 /* void is very common */
414 if (type->type == MONO_TYPE_VOID && domain->typeof_void)
415 return (MonoReflectionType*)domain->typeof_void;
418 * If the vtable of the given class was already created, we can use
419 * the MonoType from there and avoid all locking and hash table lookups.
421 * We cannot do this for TypeBuilders as mono_reflection_create_runtime_class expects
422 * that the resulting object is different.
424 if (type == &klass->byval_arg && !image_is_dynamic (klass->image)) {
425 MonoVTable *vtable = mono_class_try_get_vtable (domain, klass);
426 if (vtable && vtable->type)
427 return (MonoReflectionType *)vtable->type;
430 mono_loader_lock (); /*FIXME mono_class_init and mono_class_vtable acquire it*/
431 mono_domain_lock (domain);
432 if (!domain->type_hash)
433 domain->type_hash = mono_g_hash_table_new_type ((GHashFunc)mono_metadata_type_hash,
434 (GCompareFunc)mono_metadata_type_equal, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "domain reflection types table");
435 if ((res = (MonoReflectionType *)mono_g_hash_table_lookup (domain->type_hash, type))) {
436 mono_domain_unlock (domain);
437 mono_loader_unlock ();
441 /*Types must be normalized so a generic instance of the GTD get's the same inner type.
442 * For example in: Foo<A,B>; Bar<A> : Foo<A, Bar<A>>
443 * The second Bar will be encoded a generic instance of Bar with <A> as parameter.
444 * On all other places, Bar<A> will be encoded as the GTD itself. This is an implementation
445 * artifact of how generics are encoded and should be transparent to managed code so we
446 * need to weed out this diference when retrieving managed System.Type objects.
448 norm_type = mono_type_normalize (type);
449 if (norm_type != type) {
450 res = mono_type_get_object_checked (domain, norm_type, error);
451 if (!mono_error_ok (error))
453 mono_g_hash_table_insert (domain->type_hash, type, res);
454 mono_domain_unlock (domain);
455 mono_loader_unlock ();
459 /* This MonoGenericClass hack is no longer necessary. Let's leave it here until we finish with the 2-stage type-builder setup.*/
460 if ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->is_dynamic && !type->data.generic_class->container_class->wastypebuilder)
463 if (mono_class_get_ref_info (klass) && !klass->wastypebuilder && !type->byref) {
464 mono_domain_unlock (domain);
465 mono_loader_unlock ();
466 return (MonoReflectionType *)mono_class_get_ref_info (klass);
468 /* This is stored in vtables/JITted code so it has to be pinned */
469 res = (MonoReflectionType *)mono_object_new_pinned (domain, mono_defaults.runtimetype_class, error);
470 if (!mono_error_ok (error))
474 mono_g_hash_table_insert (domain->type_hash, type, res);
476 if (type->type == MONO_TYPE_VOID)
477 domain->typeof_void = (MonoObject*)res;
479 mono_domain_unlock (domain);
480 mono_loader_unlock ();
485 * mono_method_get_object:
486 * @domain: an app domain
488 * @refclass: the reflected type (can be NULL)
490 * Return an System.Reflection.MonoMethod object representing the method @method.
492 MonoReflectionMethod*
493 mono_method_get_object (MonoDomain *domain, MonoMethod *method, MonoClass *refclass)
496 MonoReflectionMethod *ret = NULL;
497 ret = mono_method_get_object_checked (domain, method, refclass, &error);
498 mono_error_cleanup (&error);
503 * mono_method_get_object_checked:
504 * @domain: an app domain
506 * @refclass: the reflected type (can be NULL)
507 * @error: set on error.
509 * Return an System.Reflection.MonoMethod object representing the method @method.
510 * Returns NULL and sets @error on error.
512 MonoReflectionMethod*
513 mono_method_get_object_checked (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
516 * We use the same C representation for methods and constructors, but the type
517 * name in C# is different.
519 MonoReflectionType *rt;
521 MonoReflectionMethod *ret;
523 mono_error_init (error);
526 refclass = method->klass;
528 CHECK_OBJECT (MonoReflectionMethod *, method, refclass);
529 if (*method->name == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)) {
530 klass = mono_class_get_mono_cmethod_class ();
533 klass = mono_class_get_mono_method_class ();
535 ret = (MonoReflectionMethod*)mono_object_new_checked (domain, klass, error);
536 if (!mono_error_ok (error))
538 ret->method = method;
540 rt = mono_type_get_object_checked (domain, &refclass->byval_arg, error);
541 if (!mono_error_ok (error))
544 MONO_OBJECT_SETREF (ret, reftype, rt);
546 CACHE_OBJECT (MonoReflectionMethod *, method, ret, refclass);
549 g_assert (!mono_error_ok (error));
554 * mono_method_clear_object:
556 * Clear the cached reflection objects for the dynamic method METHOD.
559 mono_method_clear_object (MonoDomain *domain, MonoMethod *method)
562 g_assert (method_is_dynamic (method));
564 klass = method->klass;
566 clear_cached_object (domain, method, klass);
567 klass = klass->parent;
569 /* Added by mono_param_get_objects () */
570 clear_cached_object (domain, &(method->signature), NULL);
571 klass = method->klass;
573 clear_cached_object (domain, &(method->signature), klass);
574 klass = klass->parent;
579 * mono_field_get_object:
580 * @domain: an app domain
584 * Return an System.Reflection.MonoField object representing the field @field
588 mono_field_get_object (MonoDomain *domain, MonoClass *klass, MonoClassField *field)
591 MonoReflectionField *result;
592 result = mono_field_get_object_checked (domain, klass, field, &error);
593 mono_error_cleanup (&error);
598 * mono_field_get_object_checked:
599 * @domain: an app domain
602 * @error: set on error
604 * Return an System.Reflection.MonoField object representing the field @field
605 * in class @klass. On error, returns NULL and sets @error.
608 mono_field_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
610 MonoReflectionType *rt;
611 MonoReflectionField *res;
613 mono_error_init (error);
615 CHECK_OBJECT (MonoReflectionField *, field, klass);
616 res = (MonoReflectionField *)mono_object_new_checked (domain, mono_class_get_mono_field_class (), error);
621 MONO_OBJECT_SETREF (res, name, mono_string_new (domain, mono_field_get_name (field)));
624 rt = mono_type_get_object_checked (domain, field->type, error);
625 if (!mono_error_ok (error))
628 MONO_OBJECT_SETREF (res, type, rt);
630 res->attrs = mono_field_get_flags (field);
631 CACHE_OBJECT (MonoReflectionField *, field, res, klass);
635 * mono_property_get_object:
636 * @domain: an app domain
638 * @property: a property
640 * Return an System.Reflection.MonoProperty object representing the property @property
643 MonoReflectionProperty*
644 mono_property_get_object (MonoDomain *domain, MonoClass *klass, MonoProperty *property)
647 MonoReflectionProperty *result;
648 result = mono_property_get_object_checked (domain, klass, property, &error);
649 mono_error_cleanup (&error);
654 * mono_property_get_object:
655 * @domain: an app domain
657 * @property: a property
658 * @error: set on error
660 * Return an System.Reflection.MonoProperty object representing the property @property
661 * in class @klass. On error returns NULL and sets @error.
663 MonoReflectionProperty*
664 mono_property_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
666 MonoReflectionProperty *res;
668 mono_error_init (error);
670 CHECK_OBJECT (MonoReflectionProperty *, property, klass);
671 res = (MonoReflectionProperty *)mono_object_new_checked (domain, mono_class_get_mono_property_class (), error);
675 res->property = property;
676 CACHE_OBJECT (MonoReflectionProperty *, property, res, klass);
680 * mono_event_get_object:
681 * @domain: an app domain
685 * Return an System.Reflection.MonoEvent object representing the event @event
689 mono_event_get_object (MonoDomain *domain, MonoClass *klass, MonoEvent *event)
692 MonoReflectionEvent *result;
693 result = mono_event_get_object_checked (domain, klass, event, &error);
694 mono_error_cleanup (&error);
699 * mono_event_get_object_checked:
700 * @domain: an app domain
703 * @error: set on error
705 * Return an System.Reflection.MonoEvent object representing the event @event
706 * in class @klass. On failure sets @error and returns NULL
709 mono_event_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoEvent *event, MonoError *error)
711 MonoReflectionEvent *res;
712 MonoReflectionMonoEvent *mono_event;
714 mono_error_init (error);
715 CHECK_OBJECT (MonoReflectionEvent *, event, klass);
716 mono_event = (MonoReflectionMonoEvent *)mono_object_new_checked (domain, mono_class_get_mono_event_class (), error);
719 mono_event->klass = klass;
720 mono_event->event = event;
721 res = (MonoReflectionEvent*)mono_event;
722 CACHE_OBJECT (MonoReflectionEvent *, event, res, klass);
726 * mono_get_reflection_missing_object:
727 * @domain: Domain where the object lives
729 * Returns the System.Reflection.Missing.Value singleton object
730 * (of type System.Reflection.Missing).
732 * Used as the value for ParameterInfo.DefaultValue when Optional
736 mono_get_reflection_missing_object (MonoDomain *domain)
740 static MonoClassField *missing_value_field = NULL;
742 if (!missing_value_field) {
743 MonoClass *missing_klass;
744 missing_klass = mono_class_get_missing_class ();
745 mono_class_init (missing_klass);
746 missing_value_field = mono_class_get_field_from_name (missing_klass, "Value");
747 g_assert (missing_value_field);
749 obj = mono_field_get_value_object_checked (domain, missing_value_field, NULL, &error);
750 mono_error_assert_ok (&error);
755 get_dbnull (MonoDomain *domain, MonoObject **dbnull)
758 *dbnull = mono_get_dbnull_object (domain);
763 get_reflection_missing (MonoDomain *domain, MonoObject **reflection_missing)
765 if (!*reflection_missing)
766 *reflection_missing = mono_get_reflection_missing_object (domain);
767 return *reflection_missing;
771 * mono_param_get_objects:
772 * @domain: an app domain
775 * Return an System.Reflection.ParameterInfo array object representing the parameters
776 * in the method @method.
779 mono_param_get_objects_internal (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
781 static MonoClass *System_Reflection_ParameterInfo;
782 static MonoClass *System_Reflection_ParameterInfo_array;
783 MonoArray *res = NULL;
784 MonoReflectionMethod *member = NULL;
785 MonoReflectionParameter *param = NULL;
786 char **names = NULL, **blobs = NULL;
787 guint32 *types = NULL;
788 MonoType *type = NULL;
789 MonoObject *dbnull = NULL;
790 MonoObject *missing = NULL;
791 MonoMarshalSpec **mspecs = NULL;
792 MonoMethodSignature *sig = NULL;
793 MonoVTable *pinfo_vtable;
794 MonoReflectionType *rt;
797 mono_error_init (error);
799 if (!System_Reflection_ParameterInfo_array) {
802 klass = mono_class_get_mono_parameter_info_class ();
804 mono_memory_barrier ();
805 System_Reflection_ParameterInfo = klass;
808 klass = mono_array_class_get (klass, 1);
809 mono_memory_barrier ();
810 System_Reflection_ParameterInfo_array = klass;
813 sig = mono_method_signature_checked (method, error);
814 if (!mono_error_ok (error))
817 if (!sig->param_count) {
818 res = mono_array_new_specific_checked (mono_class_vtable (domain, System_Reflection_ParameterInfo_array), 0, error);
825 /* Note: the cache is based on the address of the signature into the method
826 * since we already cache MethodInfos with the method as keys.
828 CHECK_OBJECT (MonoArray*, &(method->signature), refclass);
830 member = mono_method_get_object_checked (domain, method, refclass, error);
833 names = g_new (char *, sig->param_count);
834 mono_method_get_param_names (method, (const char **) names);
836 mspecs = g_new (MonoMarshalSpec*, sig->param_count + 1);
837 mono_method_get_marshal_info (method, mspecs);
839 res = mono_array_new_specific_checked (mono_class_vtable (domain, System_Reflection_ParameterInfo_array), sig->param_count, error);
843 pinfo_vtable = mono_class_vtable (domain, System_Reflection_ParameterInfo);
844 for (i = 0; i < sig->param_count; ++i) {
845 param = (MonoReflectionParameter *) mono_object_new_specific_checked (pinfo_vtable, error);
849 rt = mono_type_get_object_checked (domain, sig->params [i], error);
853 MONO_OBJECT_SETREF (param, ClassImpl, rt);
855 MONO_OBJECT_SETREF (param, MemberImpl, (MonoObject*)member);
857 MONO_OBJECT_SETREF (param, NameImpl, mono_string_new (domain, names [i]));
859 param->PositionImpl = i;
860 param->AttrsImpl = sig->params [i]->attrs;
862 if (!(param->AttrsImpl & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
863 if (param->AttrsImpl & PARAM_ATTRIBUTE_OPTIONAL)
864 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_reflection_missing (domain, &missing));
866 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_dbnull (domain, &dbnull));
870 blobs = g_new0 (char *, sig->param_count);
871 types = g_new0 (guint32, sig->param_count);
872 get_default_param_value_blobs (method, blobs, types);
875 /* Build MonoType for the type from the Constant Table */
877 type = g_new0 (MonoType, 1);
878 type->type = (MonoTypeEnum)types [i];
879 type->data.klass = NULL;
880 if (types [i] == MONO_TYPE_CLASS)
881 type->data.klass = mono_defaults.object_class;
882 else if ((sig->params [i]->type == MONO_TYPE_VALUETYPE) && sig->params [i]->data.klass->enumtype) {
883 /* For enums, types [i] contains the base type */
885 type->type = MONO_TYPE_VALUETYPE;
886 type->data.klass = mono_class_from_mono_type (sig->params [i]);
888 type->data.klass = mono_class_from_mono_type (type);
890 MonoObject *default_val_obj = mono_get_object_from_blob (domain, type, blobs [i], error);
893 MONO_OBJECT_SETREF (param, DefaultValueImpl, default_val_obj);
895 /* Type in the Constant table is MONO_TYPE_CLASS for nulls */
896 if (types [i] != MONO_TYPE_CLASS && !param->DefaultValueImpl) {
897 if (param->AttrsImpl & PARAM_ATTRIBUTE_OPTIONAL)
898 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_reflection_missing (domain, &missing));
900 MONO_OBJECT_SETREF (param, DefaultValueImpl, get_dbnull (domain, &dbnull));
905 if (mspecs [i + 1]) {
906 MonoReflectionMarshalAsAttribute* mobj;
907 mobj = mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspecs [i + 1], error);
910 MONO_OBJECT_SETREF (param, MarshalAsImpl, (MonoObject*)mobj);
913 mono_array_setref (res, i, param);
923 for (i = sig->param_count; i >= 0; i--) {
925 mono_metadata_free_marshal_spec (mspecs [i]);
933 CACHE_OBJECT (MonoArray *, &(method->signature), res, refclass);
937 mono_param_get_objects (MonoDomain *domain, MonoMethod *method)
940 MonoArray *result = mono_param_get_objects_internal (domain, method, NULL, &error);
941 mono_error_assert_ok (&error);
946 * mono_method_body_get_object:
947 * @domain: an app domain
950 * Return an System.Reflection.MethodBody object representing the method @method.
952 MonoReflectionMethodBody*
953 mono_method_body_get_object (MonoDomain *domain, MonoMethod *method)
956 MonoReflectionMethodBody *result = mono_method_body_get_object_checked (domain, method, &error);
957 mono_error_cleanup (&error);
962 * mono_method_body_get_object_checked:
963 * @domain: an app domain
965 * @error: set on error
967 * Return an System.Reflection.MethodBody object representing the
968 * method @method. On failure, returns NULL and sets @error.
970 MonoReflectionMethodBody*
971 mono_method_body_get_object_checked (MonoDomain *domain, MonoMethod *method, MonoError *error)
973 MonoReflectionMethodBody *ret;
974 MonoMethodHeader *header;
976 MonoReflectionType *rt;
977 guint32 method_rva, local_var_sig_token;
979 unsigned char format, flags;
982 mono_error_init (error);
984 /* for compatibility with .net */
985 if (method_is_dynamic (method)) {
986 mono_error_set_generic_error (error, "System", "InvalidOperationException", "");
990 CHECK_OBJECT (MonoReflectionMethodBody *, method, NULL);
992 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
993 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
994 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
995 (method->klass->image->raw_data && method->klass->image->raw_data [1] != 'Z') ||
996 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME))
999 image = method->klass->image;
1000 header = mono_method_get_header_checked (method, error);
1001 return_val_if_nok (error, NULL);
1003 if (!image_is_dynamic (image)) {
1004 /* Obtain local vars signature token */
1005 method_rva = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD], mono_metadata_token_index (method->token) - 1, MONO_METHOD_RVA);
1006 ptr = mono_image_rva_map (image, method_rva);
1007 flags = *(const unsigned char *) ptr;
1008 format = flags & METHOD_HEADER_FORMAT_MASK;
1010 case METHOD_HEADER_TINY_FORMAT:
1011 local_var_sig_token = 0;
1013 case METHOD_HEADER_FAT_FORMAT:
1017 local_var_sig_token = read32 (ptr);
1020 g_assert_not_reached ();
1023 local_var_sig_token = 0; //FIXME
1025 ret = (MonoReflectionMethodBody*)mono_object_new_checked (domain, mono_class_get_method_body_class (), error);
1029 ret->init_locals = header->init_locals;
1030 ret->max_stack = header->max_stack;
1031 ret->local_var_sig_token = local_var_sig_token;
1032 MonoArray *il_arr = mono_array_new_cached (domain, mono_defaults.byte_class, header->code_size, error);
1035 MONO_OBJECT_SETREF (ret, il, il_arr);
1036 memcpy (mono_array_addr (ret->il, guint8, 0), header->code, header->code_size);
1039 MonoArray *locals_arr = mono_array_new_cached (domain, mono_class_get_local_variable_info_class (), header->num_locals, error);
1042 MONO_OBJECT_SETREF (ret, locals, locals_arr);
1043 for (i = 0; i < header->num_locals; ++i) {
1044 MonoReflectionLocalVariableInfo *info = (MonoReflectionLocalVariableInfo*)mono_object_new_checked (domain, mono_class_get_local_variable_info_class (), error);
1048 rt = mono_type_get_object_checked (domain, header->locals [i], error);
1052 MONO_OBJECT_SETREF (info, local_type, rt);
1054 info->is_pinned = header->locals [i]->pinned;
1055 info->local_index = i;
1056 mono_array_setref (ret->locals, i, info);
1060 MonoArray *exn_clauses = mono_array_new_cached (domain, mono_class_get_exception_handling_clause_class (), header->num_clauses, error);
1063 MONO_OBJECT_SETREF (ret, clauses, exn_clauses);
1064 for (i = 0; i < header->num_clauses; ++i) {
1065 MonoReflectionExceptionHandlingClause *info = (MonoReflectionExceptionHandlingClause*)mono_object_new_checked (domain, mono_class_get_exception_handling_clause_class (), error);
1068 MonoExceptionClause *clause = &header->clauses [i];
1070 info->flags = clause->flags;
1071 info->try_offset = clause->try_offset;
1072 info->try_length = clause->try_len;
1073 info->handler_offset = clause->handler_offset;
1074 info->handler_length = clause->handler_len;
1075 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1076 info->filter_offset = clause->data.filter_offset;
1077 else if (clause->data.catch_class) {
1078 rt = mono_type_get_object_checked (mono_domain_get (), &clause->data.catch_class->byval_arg, error);
1082 MONO_OBJECT_SETREF (info, catch_type, rt);
1085 mono_array_setref (ret->clauses, i, info);
1088 mono_metadata_free_mh (header);
1089 CACHE_OBJECT (MonoReflectionMethodBody *, method, ret, NULL);
1093 mono_metadata_free_mh (header);
1098 * mono_get_dbnull_object:
1099 * @domain: Domain where the object lives
1101 * Returns the System.DBNull.Value singleton object
1103 * Used as the value for ParameterInfo.DefaultValue
1106 mono_get_dbnull_object (MonoDomain *domain)
1110 static MonoClassField *dbnull_value_field = NULL;
1112 if (!dbnull_value_field) {
1113 MonoClass *dbnull_klass;
1114 dbnull_klass = mono_class_get_dbnull_class ();
1115 dbnull_value_field = mono_class_get_field_from_name (dbnull_klass, "Value");
1116 g_assert (dbnull_value_field);
1118 obj = mono_field_get_value_object_checked (domain, dbnull_value_field, NULL, &error);
1119 mono_error_assert_ok (&error);
1124 get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types)
1126 guint32 param_index, i, lastp, crow = 0;
1127 guint32 param_cols [MONO_PARAM_SIZE], const_cols [MONO_CONSTANT_SIZE];
1130 MonoClass *klass = method->klass;
1131 MonoImage *image = klass->image;
1132 MonoMethodSignature *methodsig = mono_method_signature (method);
1134 MonoTableInfo *constt;
1135 MonoTableInfo *methodt;
1136 MonoTableInfo *paramt;
1138 if (!methodsig->param_count)
1141 mono_class_init (klass);
1143 if (image_is_dynamic (klass->image)) {
1144 MonoReflectionMethodAux *aux;
1145 if (method->is_inflated)
1146 method = ((MonoMethodInflated*)method)->declaring;
1147 aux = (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1148 if (aux && aux->param_defaults) {
1149 memcpy (blobs, &(aux->param_defaults [1]), methodsig->param_count * sizeof (char*));
1150 memcpy (types, &(aux->param_default_types [1]), methodsig->param_count * sizeof (guint32));
1155 methodt = &klass->image->tables [MONO_TABLE_METHOD];
1156 paramt = &klass->image->tables [MONO_TABLE_PARAM];
1157 constt = &image->tables [MONO_TABLE_CONSTANT];
1159 idx = mono_method_get_index (method) - 1;
1160 g_assert (idx != -1);
1162 param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1163 if (idx + 1 < methodt->rows)
1164 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1166 lastp = paramt->rows + 1;
1168 for (i = param_index; i < lastp; ++i) {
1171 mono_metadata_decode_row (paramt, i - 1, param_cols, MONO_PARAM_SIZE);
1172 paramseq = param_cols [MONO_PARAM_SEQUENCE];
1174 if (!(param_cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT))
1177 crow = mono_metadata_get_constant_index (image, MONO_TOKEN_PARAM_DEF | i, crow + 1);
1182 mono_metadata_decode_row (constt, crow - 1, const_cols, MONO_CONSTANT_SIZE);
1183 blobs [paramseq - 1] = (char *)mono_metadata_blob_heap (image, const_cols [MONO_CONSTANT_VALUE]);
1184 types [paramseq - 1] = const_cols [MONO_CONSTANT_TYPE];
1191 mono_get_object_from_blob (MonoDomain *domain, MonoType *type, const char *blob, MonoError *error)
1196 MonoType *basetype = type;
1198 mono_error_init (error);
1203 klass = mono_class_from_mono_type (type);
1204 if (klass->valuetype) {
1205 object = mono_object_new_checked (domain, klass, error);
1206 return_val_if_nok (error, NULL);
1207 retval = ((gchar *) object + sizeof (MonoObject));
1208 if (klass->enumtype)
1209 basetype = mono_class_enum_basetype (klass);
1214 if (!mono_get_constant_value_from_blob (domain, basetype->type, blob, retval, error))
1221 assembly_name_to_aname (MonoAssemblyName *assembly, char *p) {
1224 gboolean quoted = FALSE;
1226 memset (assembly, 0, sizeof (MonoAssemblyName));
1227 assembly->culture = "";
1228 memset (assembly->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1235 while (*p && (isalnum (*p) || *p == '.' || *p == '-' || *p == '_' || *p == '$' || *p == '@' || g_ascii_isspace (*p)))
1246 /* Remove trailing whitespace */
1248 while (*s && g_ascii_isspace (*s))
1251 while (g_ascii_isspace (*p))
1254 if (*p == 'V' && g_ascii_strncasecmp (p, "Version=", 8) == 0) {
1256 assembly->major = strtoul (p, &s, 10);
1257 if (s == p || *s != '.')
1260 assembly->minor = strtoul (p, &s, 10);
1261 if (s == p || *s != '.')
1264 assembly->build = strtoul (p, &s, 10);
1265 if (s == p || *s != '.')
1268 assembly->revision = strtoul (p, &s, 10);
1272 } else if (*p == 'C' && g_ascii_strncasecmp (p, "Culture=", 8) == 0) {
1274 if (g_ascii_strncasecmp (p, "neutral", 7) == 0) {
1275 assembly->culture = "";
1278 assembly->culture = p;
1279 while (*p && *p != ',') {
1283 } else if (*p == 'P' && g_ascii_strncasecmp (p, "PublicKeyToken=", 15) == 0) {
1285 if (strncmp (p, "null", 4) == 0) {
1290 while (*p && *p != ',') {
1293 len = (p - start + 1);
1294 if (len > MONO_PUBLIC_KEY_TOKEN_LENGTH)
1295 len = MONO_PUBLIC_KEY_TOKEN_LENGTH;
1296 g_strlcpy ((char*)assembly->public_key_token, start, len);
1299 while (*p && *p != ',')
1303 while (g_ascii_isspace (*p) || *p == ',') {
1317 * mono_reflection_parse_type:
1320 * Parse a type name as accepted by the GetType () method and output the info
1321 * extracted in the info structure.
1322 * the name param will be mangled, so, make a copy before passing it to this function.
1323 * The fields in info will be valid until the memory pointed to by name is valid.
1325 * See also mono_type_get_name () below.
1327 * Returns: 0 on parse error.
1330 _mono_reflection_parse_type (char *name, char **endptr, gboolean is_recursed,
1331 MonoTypeNameParse *info)
1333 char *start, *p, *w, *last_point, *startn;
1334 int in_modifiers = 0;
1335 int isbyref = 0, rank = 0, isptr = 0;
1337 start = p = w = name;
1339 //FIXME could we just zero the whole struct? memset (&info, 0, sizeof (MonoTypeNameParse))
1340 memset (&info->assembly, 0, sizeof (MonoAssemblyName));
1341 info->name = info->name_space = NULL;
1342 info->nested = NULL;
1343 info->modifiers = NULL;
1344 info->type_arguments = NULL;
1346 /* last_point separates the namespace from the name */
1349 while (*p == ' ') p++, start++, w++, name++;
1354 *p = 0; /* NULL terminate the name */
1356 info->nested = g_list_append (info->nested, startn);
1357 /* we have parsed the nesting namespace + name */
1361 info->name_space = start;
1363 info->name = last_point + 1;
1365 info->name_space = (char *)"";
1393 info->name_space = start;
1395 info->name = last_point + 1;
1397 info->name_space = (char *)"";
1404 if (isbyref) /* only one level allowed by the spec */
1408 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (0));
1412 if (isbyref) /* pointer to ref not okay */
1414 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-1));
1419 if (isbyref) /* array of ref and generic ref are not okay */
1421 //Decide if it's an array of a generic argument list
1426 if (*p == ',' || *p == '*' || *p == ']') { //array
1434 else if (*p == '*') /* '*' means unknown lower bound */
1435 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-2));
1442 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (rank));
1444 if (rank || isptr) /* generic args after array spec or ptr*/ //XXX test
1447 info->type_arguments = g_ptr_array_new ();
1449 MonoTypeNameParse *subinfo = g_new0 (MonoTypeNameParse, 1);
1450 gboolean fqname = FALSE;
1452 g_ptr_array_add (info->type_arguments, subinfo);
1454 while (*p == ' ') p++;
1460 if (!_mono_reflection_parse_type (p, &p, TRUE, subinfo))
1463 /*MS is lenient on [] delimited parameters that aren't fqn - and F# uses them.*/
1464 if (fqname && (*p != ']')) {
1472 while (*p && (*p != ']'))
1480 if (g_ascii_isspace (*aname)) {
1487 !assembly_name_to_aname (&subinfo->assembly, aname))
1489 } else if (fqname && (*p == ']')) {
1511 if (g_ascii_isspace (*p)) {
1518 return 0; /* missing assembly name */
1519 if (!assembly_name_to_aname (&info->assembly, p))
1525 if (info->assembly.name)
1528 // *w = 0; /* terminate class name */
1530 if (!info->name || !*info->name)
1534 /* add other consistency checks */
1540 * mono_identifier_unescape_type_name_chars:
1541 * @identifier: the display name of a mono type
1544 * The name in internal form, that is without escaping backslashes.
1546 * The string is modified in place!
1549 mono_identifier_unescape_type_name_chars(char* identifier)
1554 for (w = r = identifier; *r != 0; r++)
1572 mono_identifier_unescape_info (MonoTypeNameParse* info);
1575 unescape_each_type_argument(void* data, void* user_data)
1577 MonoTypeNameParse* info = (MonoTypeNameParse*)data;
1578 mono_identifier_unescape_info (info);
1582 unescape_each_nested_name (void* data, void* user_data)
1584 char* nested_name = (char*) data;
1585 mono_identifier_unescape_type_name_chars(nested_name);
1589 * mono_identifier_unescape_info:
1591 * @info: a parsed display form of an (optionally assembly qualified) full type name.
1595 * Destructively updates the info by unescaping the identifiers that
1596 * comprise the type namespace, name, nested types (if any) and
1597 * generic type arguments (if any).
1599 * The resulting info has the names in internal form.
1603 mono_identifier_unescape_info (MonoTypeNameParse *info)
1607 mono_identifier_unescape_type_name_chars(info->name_space);
1608 mono_identifier_unescape_type_name_chars(info->name);
1609 // but don't escape info->assembly
1610 if (info->type_arguments)
1611 g_ptr_array_foreach(info->type_arguments, &unescape_each_type_argument, NULL);
1613 g_list_foreach(info->nested, &unescape_each_nested_name, NULL);
1617 mono_reflection_parse_type (char *name, MonoTypeNameParse *info)
1619 int ok = _mono_reflection_parse_type (name, NULL, FALSE, info);
1621 mono_identifier_unescape_info (info);
1627 _mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, gboolean ignorecase, MonoError *error)
1629 gboolean type_resolve = FALSE;
1631 MonoImage *rootimage = image;
1633 mono_error_init (error);
1635 if (info->assembly.name) {
1636 MonoAssembly *assembly = mono_assembly_loaded (&info->assembly);
1637 if (!assembly && image && image->assembly && mono_assembly_names_equal (&info->assembly, &image->assembly->aname))
1639 * This could happen in the AOT compiler case when the search hook is not
1642 assembly = image->assembly;
1644 /* then we must load the assembly ourselve - see #60439 */
1645 assembly = mono_assembly_load (&info->assembly, image->assembly->basedir, NULL);
1649 image = assembly->image;
1650 } else if (!image) {
1651 image = mono_defaults.corlib;
1654 type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1655 if (type == NULL && !info->assembly.name && image != mono_defaults.corlib) {
1656 /* ignore the error and try again */
1657 mono_error_cleanup (error);
1658 mono_error_init (error);
1659 image = mono_defaults.corlib;
1660 type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1667 * mono_reflection_get_type_internal:
1669 * Returns: may return NULL on success, sets error on failure.
1672 mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
1677 gboolean bounded = FALSE;
1679 mono_error_init (error);
1681 image = mono_defaults.corlib;
1684 rootimage = mono_defaults.corlib;
1687 klass = mono_class_from_name_case_checked (image, info->name_space, info->name, error);
1689 klass = mono_class_from_name_checked (image, info->name_space, info->name, error);
1694 for (mod = info->nested; mod; mod = mod->next) {
1695 gpointer iter = NULL;
1699 mono_class_init (parent);
1701 while ((klass = mono_class_get_nested_types (parent, &iter))) {
1703 char *nested_name, *nested_nspace;
1704 gboolean match = TRUE;
1706 lastp = strrchr ((const char *)mod->data, '.');
1708 /* Nested classes can have namespaces */
1711 nested_name = g_strdup (lastp + 1);
1712 nspace_len = lastp - (char*)mod->data;
1713 nested_nspace = (char *)g_malloc (nspace_len + 1);
1714 memcpy (nested_nspace, mod->data, nspace_len);
1715 nested_nspace [nspace_len] = '\0';
1718 nested_name = (char *)mod->data;
1719 nested_nspace = NULL;
1722 if (nested_nspace) {
1724 if (!(klass->name_space && mono_utf8_strcasecmp (klass->name_space, nested_nspace) == 0))
1727 if (!(klass->name_space && strcmp (klass->name_space, nested_nspace) == 0))
1733 if (mono_utf8_strcasecmp (klass->name, nested_name) != 0)
1736 if (strcmp (klass->name, nested_name) != 0)
1741 g_free (nested_name);
1742 g_free (nested_nspace);
1754 if (info->type_arguments) {
1755 MonoType **type_args = g_new0 (MonoType *, info->type_arguments->len);
1756 MonoReflectionType *the_type;
1760 for (i = 0; i < info->type_arguments->len; i++) {
1761 MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
1763 type_args [i] = _mono_reflection_get_type_from_info (subinfo, rootimage, ignorecase, error);
1764 if (!type_args [i]) {
1770 the_type = mono_type_get_object_checked (mono_domain_get (), &klass->byval_arg, error);
1774 instance = mono_reflection_bind_generic_parameters (
1775 the_type, info->type_arguments->len, type_args, error);
1781 klass = mono_class_from_mono_type (instance);
1784 for (mod = info->modifiers; mod; mod = mod->next) {
1785 modval = GPOINTER_TO_UINT (mod->data);
1786 if (!modval) { /* byref: must be last modifier */
1787 return &klass->this_arg;
1788 } else if (modval == -1) {
1789 klass = mono_ptr_class_get (&klass->byval_arg);
1790 } else if (modval == -2) {
1792 } else { /* array rank */
1793 klass = mono_bounded_array_class_get (klass, modval, bounded);
1797 return &klass->byval_arg;
1801 * mono_reflection_get_type:
1802 * @image: a metadata context
1803 * @info: type description structure
1804 * @ignorecase: flag for case-insensitive string compares
1805 * @type_resolve: whenever type resolve was already tried
1807 * Build a MonoType from the type description in @info.
1812 mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve) {
1814 MonoType *result = mono_reflection_get_type_with_rootimage (image, image, info, ignorecase, type_resolve, &error);
1815 mono_error_cleanup (&error);
1820 * mono_reflection_get_type_checked:
1821 * @rootimage: the image of the currently active managed caller
1822 * @image: a metadata context
1823 * @info: type description structure
1824 * @ignorecase: flag for case-insensitive string compares
1825 * @type_resolve: whenever type resolve was already tried
1826 * @error: set on error.
1828 * Build a MonoType from the type description in @info. On failure returns NULL and sets @error.
1832 mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error) {
1833 mono_error_init (error);
1834 return mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, type_resolve, error);
1839 mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *assembly, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
1841 MonoReflectionAssemblyBuilder *abuilder;
1845 mono_error_init (error);
1846 g_assert (assembly_is_dynamic (assembly));
1847 abuilder = (MonoReflectionAssemblyBuilder*)mono_assembly_get_object_checked (((MonoDynamicAssembly*)assembly)->domain, assembly, error);
1851 /* Enumerate all modules */
1854 if (abuilder->modules) {
1855 for (i = 0; i < mono_array_length (abuilder->modules); ++i) {
1856 MonoReflectionModuleBuilder *mb = mono_array_get (abuilder->modules, MonoReflectionModuleBuilder*, i);
1857 type = mono_reflection_get_type_internal (rootimage, &mb->dynamic_image->image, info, ignorecase, error);
1860 if (!mono_error_ok (error))
1865 if (!type && abuilder->loaded_modules) {
1866 for (i = 0; i < mono_array_length (abuilder->loaded_modules); ++i) {
1867 MonoReflectionModule *mod = mono_array_get (abuilder->loaded_modules, MonoReflectionModule*, i);
1868 type = mono_reflection_get_type_internal (rootimage, mod->image, info, ignorecase, error);
1871 if (!mono_error_ok (error))
1880 mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error)
1883 MonoReflectionAssembly *assembly;
1887 mono_error_init (error);
1889 if (image && image_is_dynamic (image))
1890 type = mono_reflection_get_type_internal_dynamic (rootimage, image->assembly, info, ignorecase, error);
1892 type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
1894 return_val_if_nok (error, NULL);
1898 if (!mono_domain_has_type_resolve (mono_domain_get ()))
1905 *type_resolve = TRUE;
1908 /* Reconstruct the type name */
1909 fullName = g_string_new ("");
1910 if (info->name_space && (info->name_space [0] != '\0'))
1911 g_string_printf (fullName, "%s.%s", info->name_space, info->name);
1913 g_string_printf (fullName, "%s", info->name);
1914 for (mod = info->nested; mod; mod = mod->next)
1915 g_string_append_printf (fullName, "+%s", (char*)mod->data);
1917 assembly = mono_domain_try_type_resolve_checked ( mono_domain_get (), fullName->str, NULL, error);
1918 if (!is_ok (error)) {
1919 g_string_free (fullName, TRUE);
1924 if (assembly_is_dynamic (assembly->assembly))
1925 type = mono_reflection_get_type_internal_dynamic (rootimage, assembly->assembly,
1926 info, ignorecase, error);
1928 type = mono_reflection_get_type_internal (rootimage, assembly->assembly->image,
1929 info, ignorecase, error);
1931 g_string_free (fullName, TRUE);
1932 return_val_if_nok (error, NULL);
1937 mono_reflection_free_type_info (MonoTypeNameParse *info)
1939 g_list_free (info->modifiers);
1940 g_list_free (info->nested);
1942 if (info->type_arguments) {
1945 for (i = 0; i < info->type_arguments->len; i++) {
1946 MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
1948 mono_reflection_free_type_info (subinfo);
1949 /*We free the subinfo since it is allocated by _mono_reflection_parse_type*/
1953 g_ptr_array_free (info->type_arguments, TRUE);
1958 * mono_reflection_type_from_name:
1960 * @image: a metadata context (can be NULL).
1962 * Retrieves a MonoType from its @name. If the name is not fully qualified,
1963 * it defaults to get the type from @image or, if @image is NULL or loading
1964 * from it fails, uses corlib.
1968 mono_reflection_type_from_name (char *name, MonoImage *image)
1971 MonoType *result = mono_reflection_type_from_name_checked (name, image, &error);
1972 mono_error_cleanup (&error);
1977 * mono_reflection_type_from_name_checked:
1979 * @image: a metadata context (can be NULL).
1980 * @error: set on errror.
1982 * Retrieves a MonoType from its @name. If the name is not fully qualified,
1983 * it defaults to get the type from @image or, if @image is NULL or loading
1984 * from it fails, uses corlib. On failure returns NULL and sets @error.
1988 mono_reflection_type_from_name_checked (char *name, MonoImage *image, MonoError *error)
1990 MonoType *type = NULL;
1991 MonoTypeNameParse info;
1994 mono_error_init (error);
1995 /* Make a copy since parse_type modifies its argument */
1996 tmp = g_strdup (name);
1998 /*g_print ("requested type %s\n", str);*/
1999 if (mono_reflection_parse_type (tmp, &info)) {
2000 type = _mono_reflection_get_type_from_info (&info, image, FALSE, error);
2001 if (!is_ok (error)) {
2003 mono_reflection_free_type_info (&info);
2009 mono_reflection_free_type_info (&info);
2014 * mono_reflection_get_token:
2016 * Return the metadata token of OBJ which should be an object
2017 * representing a metadata element.
2020 mono_reflection_get_token (MonoObject *obj)
2023 guint32 result = mono_reflection_get_token_checked (obj, &error);
2024 mono_error_assert_ok (&error);
2029 * mono_reflection_get_token_checked:
2031 * @error: set on error
2033 * Return the metadata token of @obj which should be an object
2034 * representing a metadata element. On failure sets @error.
2037 mono_reflection_get_token_checked (MonoObject *obj, MonoError *error)
2042 mono_error_init (error);
2044 klass = obj->vtable->klass;
2046 if (strcmp (klass->name, "MethodBuilder") == 0) {
2047 MonoReflectionMethodBuilder *mb = (MonoReflectionMethodBuilder *)obj;
2049 token = mb->table_idx | MONO_TOKEN_METHOD_DEF;
2050 } else if (strcmp (klass->name, "ConstructorBuilder") == 0) {
2051 MonoReflectionCtorBuilder *mb = (MonoReflectionCtorBuilder *)obj;
2053 token = mb->table_idx | MONO_TOKEN_METHOD_DEF;
2054 } else if (strcmp (klass->name, "FieldBuilder") == 0) {
2055 MonoReflectionFieldBuilder *fb = (MonoReflectionFieldBuilder *)obj;
2057 token = fb->table_idx | MONO_TOKEN_FIELD_DEF;
2058 } else if (strcmp (klass->name, "TypeBuilder") == 0) {
2059 MonoReflectionTypeBuilder *tb = (MonoReflectionTypeBuilder *)obj;
2060 token = tb->table_idx | MONO_TOKEN_TYPE_DEF;
2061 } else if (strcmp (klass->name, "RuntimeType") == 0) {
2062 MonoType *type = mono_reflection_type_get_handle ((MonoReflectionType*)obj, error);
2063 return_val_if_nok (error, 0);
2064 MonoClass *mc = mono_class_from_mono_type (type);
2065 if (!mono_class_init (mc)) {
2066 mono_error_set_for_class_failure (error, mc);
2070 token = mc->type_token;
2071 } else if (strcmp (klass->name, "MonoCMethod") == 0 ||
2072 strcmp (klass->name, "MonoMethod") == 0) {
2073 MonoReflectionMethod *m = (MonoReflectionMethod *)obj;
2074 if (m->method->is_inflated) {
2075 MonoMethodInflated *inflated = (MonoMethodInflated *) m->method;
2076 return inflated->declaring->token;
2078 token = m->method->token;
2080 } else if (strcmp (klass->name, "MonoField") == 0) {
2081 MonoReflectionField *f = (MonoReflectionField*)obj;
2083 token = mono_class_get_field_token (f->field);
2084 } else if (strcmp (klass->name, "MonoProperty") == 0) {
2085 MonoReflectionProperty *p = (MonoReflectionProperty*)obj;
2087 token = mono_class_get_property_token (p->property);
2088 } else if (strcmp (klass->name, "MonoEvent") == 0) {
2089 MonoReflectionMonoEvent *p = (MonoReflectionMonoEvent*)obj;
2091 token = mono_class_get_event_token (p->event);
2092 } else if (strcmp (klass->name, "ParameterInfo") == 0 || strcmp (klass->name, "MonoParameterInfo") == 0) {
2093 MonoReflectionParameter *p = (MonoReflectionParameter*)obj;
2094 MonoClass *member_class = mono_object_class (p->MemberImpl);
2095 g_assert (mono_class_is_reflection_method_or_constructor (member_class));
2097 token = mono_method_get_param_token (((MonoReflectionMethod*)p->MemberImpl)->method, p->PositionImpl);
2098 } else if (strcmp (klass->name, "Module") == 0 || strcmp (klass->name, "MonoModule") == 0) {
2099 MonoReflectionModule *m = (MonoReflectionModule*)obj;
2102 } else if (strcmp (klass->name, "Assembly") == 0 || strcmp (klass->name, "MonoAssembly") == 0) {
2103 token = mono_metadata_make_token (MONO_TABLE_ASSEMBLY, 1);
2105 mono_error_set_not_implemented (error, "MetadataToken is not supported for type '%s.%s'",
2106 klass->name_space, klass->name);
2115 mono_reflection_is_usertype (MonoReflectionType *ref)
2117 MonoClass *klass = mono_object_class (ref);
2118 return klass->image != mono_defaults.corlib || strcmp ("TypeDelegator", klass->name) == 0;
2122 * mono_reflection_bind_generic_parameters:
2123 * @type: a managed type object (which should be some kind of generic (instance? definition?))
2124 * @type_args: the number of type arguments to bind
2125 * @types: array of type arguments
2126 * @error: set on error
2128 * Given a managed type object for a generic type instance, binds each of its arguments to the specified types.
2129 * Returns the MonoType* for the resulting type instantiation. On failure returns NULL and sets @error.
2132 mono_reflection_bind_generic_parameters (MonoReflectionType *type, int type_argc, MonoType **types, MonoError *error)
2135 gboolean is_dynamic = FALSE;
2138 mono_error_init (error);
2140 mono_loader_lock ();
2142 if (mono_is_sre_type_builder (mono_object_class (type))) {
2144 } else if (mono_is_sre_generic_instance (mono_object_class (type))) {
2145 MonoReflectionGenericClass *rgi = (MonoReflectionGenericClass *) type;
2146 MonoReflectionType *gtd = rgi->generic_type;
2148 if (mono_is_sre_type_builder (mono_object_class (gtd)))
2152 MonoType *t = mono_reflection_type_get_handle (type, error);
2153 if (!is_ok (error)) {
2154 mono_loader_unlock ();
2158 klass = mono_class_from_mono_type (t);
2159 if (!mono_class_is_gtd (klass)) {
2160 mono_loader_unlock ();
2161 mono_error_set_type_load_class (error, klass, "Cannot bind generic parameters of a non-generic type");
2165 guint gtd_type_argc = mono_class_get_generic_container (klass)->type_argc;
2166 if (gtd_type_argc != type_argc) {
2167 mono_loader_unlock ();
2168 mono_error_set_argument (error, "types", "The generic type definition needs %d type arguments, but was instantiated with %d ", gtd_type_argc, type_argc);
2173 if (klass->wastypebuilder)
2176 mono_loader_unlock ();
2178 geninst = mono_class_bind_generic_parameters (klass, type_argc, types, is_dynamic);
2180 return &geninst->byval_arg;
2184 mono_class_bind_generic_parameters (MonoClass *klass, int type_argc, MonoType **types, gboolean is_dynamic)
2186 MonoGenericClass *gclass;
2187 MonoGenericInst *inst;
2189 g_assert (mono_class_is_gtd (klass));
2191 inst = mono_metadata_get_generic_inst (type_argc, types);
2192 gclass = mono_metadata_lookup_generic_class (klass, inst, is_dynamic);
2194 return mono_generic_class_get_class (gclass);
2197 static MonoReflectionMethod*
2198 reflection_bind_generic_method_parameters (MonoReflectionMethod *rmethod, MonoArray *types, MonoError *error)
2201 MonoMethod *method, *inflated;
2202 MonoMethodInflated *imethod;
2203 MonoGenericContext tmp_context;
2204 MonoGenericInst *ginst;
2205 MonoType **type_argv;
2208 mono_error_init (error);
2210 g_assert (strcmp (rmethod->object.vtable->klass->name, "MethodBuilder"));
2212 method = rmethod->method;
2214 klass = method->klass;
2216 if (method->is_inflated)
2217 method = ((MonoMethodInflated *) method)->declaring;
2219 count = mono_method_signature (method)->generic_param_count;
2220 if (count != mono_array_length (types))
2223 type_argv = g_new0 (MonoType *, count);
2224 for (i = 0; i < count; i++) {
2225 MonoReflectionType *garg = (MonoReflectionType *)mono_array_get (types, gpointer, i);
2226 type_argv [i] = mono_reflection_type_get_handle (garg, error);
2227 if (!is_ok (error)) {
2232 ginst = mono_metadata_get_generic_inst (count, type_argv);
2235 tmp_context.class_inst = mono_class_is_ginst (klass) ? mono_class_get_generic_class (klass)->context.class_inst : NULL;
2236 tmp_context.method_inst = ginst;
2238 inflated = mono_class_inflate_generic_method_checked (method, &tmp_context, error);
2239 mono_error_assert_ok (error);
2240 imethod = (MonoMethodInflated *) inflated;
2242 /*FIXME but I think this is no longer necessary*/
2243 if (image_is_dynamic (method->klass->image)) {
2244 MonoDynamicImage *image = (MonoDynamicImage*)method->klass->image;
2246 * This table maps metadata structures representing inflated methods/fields
2247 * to the reflection objects representing their generic definitions.
2249 mono_image_lock ((MonoImage*)image);
2250 mono_g_hash_table_insert (image->generic_def_objects, imethod, rmethod);
2251 mono_image_unlock ((MonoImage*)image);
2254 if (!mono_verifier_is_method_valid_generic_instantiation (inflated)) {
2255 mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
2259 return mono_method_get_object_checked (mono_object_domain (rmethod), inflated, NULL, error);
2262 MonoReflectionMethod*
2263 ves_icall_MonoMethod_MakeGenericMethod_impl (MonoReflectionMethod *rmethod, MonoArray *types)
2266 MonoReflectionMethod *result = reflection_bind_generic_method_parameters (rmethod, types, &error);
2267 mono_error_set_pending_exception (&error);
2272 /* SECURITY_ACTION_* are defined in mono/metadata/tabledefs.h */
2273 const static guint32 declsec_flags_map[] = {
2274 0x00000000, /* empty */
2275 MONO_DECLSEC_FLAG_REQUEST, /* SECURITY_ACTION_REQUEST (x01) */
2276 MONO_DECLSEC_FLAG_DEMAND, /* SECURITY_ACTION_DEMAND (x02) */
2277 MONO_DECLSEC_FLAG_ASSERT, /* SECURITY_ACTION_ASSERT (x03) */
2278 MONO_DECLSEC_FLAG_DENY, /* SECURITY_ACTION_DENY (x04) */
2279 MONO_DECLSEC_FLAG_PERMITONLY, /* SECURITY_ACTION_PERMITONLY (x05) */
2280 MONO_DECLSEC_FLAG_LINKDEMAND, /* SECURITY_ACTION_LINKDEMAND (x06) */
2281 MONO_DECLSEC_FLAG_INHERITANCEDEMAND, /* SECURITY_ACTION_INHERITANCEDEMAND (x07) */
2282 MONO_DECLSEC_FLAG_REQUEST_MINIMUM, /* SECURITY_ACTION_REQUEST_MINIMUM (x08) */
2283 MONO_DECLSEC_FLAG_REQUEST_OPTIONAL, /* SECURITY_ACTION_REQUEST_OPTIONAL (x09) */
2284 MONO_DECLSEC_FLAG_REQUEST_REFUSE, /* SECURITY_ACTION_REQUEST_REFUSE (x0A) */
2285 MONO_DECLSEC_FLAG_PREJIT_GRANT, /* SECURITY_ACTION_PREJIT_GRANT (x0B) */
2286 MONO_DECLSEC_FLAG_PREJIT_DENY, /* SECURITY_ACTION_PREJIT_DENY (x0C) */
2287 MONO_DECLSEC_FLAG_NONCAS_DEMAND, /* SECURITY_ACTION_NONCAS_DEMAND (x0D) */
2288 MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND, /* SECURITY_ACTION_NONCAS_LINKDEMAND (x0E) */
2289 MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND, /* SECURITY_ACTION_NONCAS_INHERITANCEDEMAND (x0F) */
2290 MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE, /* SECURITY_ACTION_LINKDEMAND_CHOICE (x10) */
2291 MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE, /* SECURITY_ACTION_INHERITANCEDEMAND_CHOICE (x11) */
2292 MONO_DECLSEC_FLAG_DEMAND_CHOICE, /* SECURITY_ACTION_DEMAND_CHOICE (x12) */
2296 * Returns flags that includes all available security action associated to the handle.
2297 * @token: metadata token (either for a class or a method)
2298 * @image: image where resides the metadata.
2301 mono_declsec_get_flags (MonoImage *image, guint32 token)
2303 int index = mono_metadata_declsec_from_index (image, token);
2304 MonoTableInfo *t = &image->tables [MONO_TABLE_DECLSECURITY];
2309 /* HasSecurity can be present for other, not specially encoded, attributes,
2310 e.g. SuppressUnmanagedCodeSecurityAttribute */
2314 for (i = index; i < t->rows; i++) {
2315 guint32 cols [MONO_DECL_SECURITY_SIZE];
2317 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2318 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2321 action = cols [MONO_DECL_SECURITY_ACTION];
2322 if ((action >= MONO_DECLSEC_ACTION_MIN) && (action <= MONO_DECLSEC_ACTION_MAX)) {
2323 result |= declsec_flags_map [action];
2325 g_assert_not_reached ();
2332 * Get the security actions (in the form of flags) associated with the specified method.
2334 * @method: The method for which we want the declarative security flags.
2335 * Return the declarative security flags for the method (only).
2337 * Note: To keep MonoMethod size down we do not cache the declarative security flags
2338 * (except for the stack modifiers which are kept in the MonoJitInfo structure)
2341 mono_declsec_flags_from_method (MonoMethod *method)
2343 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2344 /* FIXME: No cache (for the moment) */
2345 guint32 idx = mono_method_get_index (method);
2346 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2347 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2348 return mono_declsec_get_flags (method->klass->image, idx);
2354 * Get the security actions (in the form of flags) associated with the specified class.
2356 * @klass: The class for which we want the declarative security flags.
2357 * Return the declarative security flags for the class.
2359 * Note: We cache the flags inside the MonoClass structure as this will get
2360 * called very often (at least for each method).
2363 mono_declsec_flags_from_class (MonoClass *klass)
2365 if (mono_class_get_flags (klass) & TYPE_ATTRIBUTE_HAS_SECURITY) {
2366 if (!klass->ext || !klass->ext->declsec_flags) {
2369 idx = mono_metadata_token_index (klass->type_token);
2370 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2371 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2372 mono_loader_lock ();
2373 mono_class_alloc_ext (klass);
2374 mono_loader_unlock ();
2375 /* we cache the flags on classes */
2376 klass->ext->declsec_flags = mono_declsec_get_flags (klass->image, idx);
2378 return klass->ext->declsec_flags;
2384 * Get the security actions (in the form of flags) associated with the specified assembly.
2386 * @assembly: The assembly for which we want the declarative security flags.
2387 * Return the declarative security flags for the assembly.
2390 mono_declsec_flags_from_assembly (MonoAssembly *assembly)
2392 guint32 idx = 1; /* there is only one assembly */
2393 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2394 idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2395 return mono_declsec_get_flags (assembly->image, idx);
2400 * Fill actions for the specific index (which may either be an encoded class token or
2401 * an encoded method token) from the metadata image.
2402 * Returns TRUE if some actions requiring code generation are present, FALSE otherwise.
2405 fill_actions_from_index (MonoImage *image, guint32 token, MonoDeclSecurityActions* actions,
2406 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2408 MonoBoolean result = FALSE;
2410 guint32 cols [MONO_DECL_SECURITY_SIZE];
2411 int index = mono_metadata_declsec_from_index (image, token);
2414 t = &image->tables [MONO_TABLE_DECLSECURITY];
2415 for (i = index; i < t->rows; i++) {
2416 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2418 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2421 /* if present only replace (class) permissions with method permissions */
2422 /* if empty accept either class or method permissions */
2423 if (cols [MONO_DECL_SECURITY_ACTION] == id_std) {
2424 if (!actions->demand.blob) {
2425 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2426 actions->demand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2427 actions->demand.blob = (char*) (blob + 2);
2428 actions->demand.size = mono_metadata_decode_blob_size (blob, &blob);
2431 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_noncas) {
2432 if (!actions->noncasdemand.blob) {
2433 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2434 actions->noncasdemand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2435 actions->noncasdemand.blob = (char*) (blob + 2);
2436 actions->noncasdemand.size = mono_metadata_decode_blob_size (blob, &blob);
2439 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_choice) {
2440 if (!actions->demandchoice.blob) {
2441 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2442 actions->demandchoice.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2443 actions->demandchoice.blob = (char*) (blob + 2);
2444 actions->demandchoice.size = mono_metadata_decode_blob_size (blob, &blob);
2454 mono_declsec_get_class_demands_params (MonoClass *klass, MonoDeclSecurityActions* demands,
2455 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2457 guint32 idx = mono_metadata_token_index (klass->type_token);
2458 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2459 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2460 return fill_actions_from_index (klass->image, idx, demands, id_std, id_noncas, id_choice);
2464 mono_declsec_get_method_demands_params (MonoMethod *method, MonoDeclSecurityActions* demands,
2465 guint32 id_std, guint32 id_noncas, guint32 id_choice)
2467 guint32 idx = mono_method_get_index (method);
2468 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2469 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2470 return fill_actions_from_index (method->klass->image, idx, demands, id_std, id_noncas, id_choice);
2474 * Collect all actions (that requires to generate code in mini) assigned for
2475 * the specified method.
2476 * Note: Don't use the content of actions if the function return FALSE.
2479 mono_declsec_get_demands (MonoMethod *method, MonoDeclSecurityActions* demands)
2481 guint32 mask = MONO_DECLSEC_FLAG_DEMAND | MONO_DECLSEC_FLAG_NONCAS_DEMAND |
2482 MONO_DECLSEC_FLAG_DEMAND_CHOICE;
2483 MonoBoolean result = FALSE;
2486 /* quick exit if no declarative security is present in the metadata */
2487 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2490 /* we want the original as the wrapper is "free" of the security informations */
2491 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2492 method = mono_marshal_method_from_wrapper (method);
2497 /* First we look for method-level attributes */
2498 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2499 mono_class_init (method->klass);
2500 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2502 result = mono_declsec_get_method_demands_params (method, demands,
2503 SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2506 /* Here we use (or create) the class declarative cache to look for demands */
2507 flags = mono_declsec_flags_from_class (method->klass);
2510 mono_class_init (method->klass);
2511 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2513 result |= mono_declsec_get_class_demands_params (method->klass, demands,
2514 SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2517 /* The boolean return value is used as a shortcut in case nothing needs to
2518 be generated (e.g. LinkDemand[Choice] and InheritanceDemand[Choice]) */
2524 * Collect all Link actions: LinkDemand, NonCasLinkDemand and LinkDemandChoice (2.0).
2526 * Note: Don't use the content of actions if the function return FALSE.
2529 mono_declsec_get_linkdemands (MonoMethod *method, MonoDeclSecurityActions* klass, MonoDeclSecurityActions *cmethod)
2531 MonoBoolean result = FALSE;
2534 /* quick exit if no declarative security is present in the metadata */
2535 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2538 /* we want the original as the wrapper is "free" of the security informations */
2539 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2540 method = mono_marshal_method_from_wrapper (method);
2545 /* results are independant - zeroize both */
2546 memset (cmethod, 0, sizeof (MonoDeclSecurityActions));
2547 memset (klass, 0, sizeof (MonoDeclSecurityActions));
2549 /* First we look for method-level attributes */
2550 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2551 mono_class_init (method->klass);
2553 result = mono_declsec_get_method_demands_params (method, cmethod,
2554 SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2557 /* Here we use (or create) the class declarative cache to look for demands */
2558 flags = mono_declsec_flags_from_class (method->klass);
2559 if (flags & (MONO_DECLSEC_FLAG_LINKDEMAND | MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND | MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE)) {
2560 mono_class_init (method->klass);
2562 result |= mono_declsec_get_class_demands_params (method->klass, klass,
2563 SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2570 * Collect all Inherit actions: InheritanceDemand, NonCasInheritanceDemand and InheritanceDemandChoice (2.0).
2572 * @klass The inherited class - this is the class that provides the security check (attributes)
2574 * return TRUE if inheritance demands (any kind) are present, FALSE otherwise.
2576 * Note: Don't use the content of actions if the function return FALSE.
2579 mono_declsec_get_inheritdemands_class (MonoClass *klass, MonoDeclSecurityActions* demands)
2581 MonoBoolean result = FALSE;
2584 /* quick exit if no declarative security is present in the metadata */
2585 if (!klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2588 /* Here we use (or create) the class declarative cache to look for demands */
2589 flags = mono_declsec_flags_from_class (klass);
2590 if (flags & (MONO_DECLSEC_FLAG_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE)) {
2591 mono_class_init (klass);
2592 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2594 result |= mono_declsec_get_class_demands_params (klass, demands,
2595 SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2602 * Collect all Inherit actions: InheritanceDemand, NonCasInheritanceDemand and InheritanceDemandChoice (2.0).
2604 * Note: Don't use the content of actions if the function return FALSE.
2607 mono_declsec_get_inheritdemands_method (MonoMethod *method, MonoDeclSecurityActions* demands)
2609 /* quick exit if no declarative security is present in the metadata */
2610 if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2613 /* we want the original as the wrapper is "free" of the security informations */
2614 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2615 method = mono_marshal_method_from_wrapper (method);
2620 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2621 mono_class_init (method->klass);
2622 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2624 return mono_declsec_get_method_demands_params (method, demands,
2625 SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2632 get_declsec_action (MonoImage *image, guint32 token, guint32 action, MonoDeclSecurityEntry *entry)
2634 guint32 cols [MONO_DECL_SECURITY_SIZE];
2638 int index = mono_metadata_declsec_from_index (image, token);
2642 t = &image->tables [MONO_TABLE_DECLSECURITY];
2643 for (i = index; i < t->rows; i++) {
2644 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2646 /* shortcut - index are ordered */
2647 if (token != cols [MONO_DECL_SECURITY_PARENT])
2650 if (cols [MONO_DECL_SECURITY_ACTION] == action) {
2651 const char *metadata = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2652 entry->blob = (char*) (metadata + 2);
2653 entry->size = mono_metadata_decode_blob_size (metadata, &metadata);
2662 mono_declsec_get_method_action (MonoMethod *method, guint32 action, MonoDeclSecurityEntry *entry)
2664 if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2665 guint32 idx = mono_method_get_index (method);
2666 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2667 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2668 return get_declsec_action (method->klass->image, idx, action, entry);
2674 mono_declsec_get_class_action (MonoClass *klass, guint32 action, MonoDeclSecurityEntry *entry)
2677 guint32 flags = mono_declsec_flags_from_class (klass);
2678 if (declsec_flags_map [action] & flags) {
2679 guint32 idx = mono_metadata_token_index (klass->type_token);
2680 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2681 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2682 return get_declsec_action (klass->image, idx, action, entry);
2688 mono_declsec_get_assembly_action (MonoAssembly *assembly, guint32 action, MonoDeclSecurityEntry *entry)
2690 guint32 idx = 1; /* there is only one assembly */
2691 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2692 idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2694 return get_declsec_action (assembly->image, idx, action, entry);
2698 mono_reflection_call_is_assignable_to (MonoClass *klass, MonoClass *oklass, MonoError *error)
2700 MonoObject *res, *exc;
2702 static MonoMethod *method = NULL;
2704 mono_error_init (error);
2706 if (method == NULL) {
2707 method = mono_class_get_method_from_name (mono_class_get_type_builder_class (), "IsAssignableTo", 1);
2712 * The result of mono_type_get_object_checked () might be a System.MonoType but we
2713 * need a TypeBuilder so use mono_class_get_ref_info (klass).
2715 g_assert (mono_class_get_ref_info (klass));
2716 g_assert (!strcmp (((MonoObject*)(mono_class_get_ref_info (klass)))->vtable->klass->name, "TypeBuilder"));
2718 params [0] = mono_type_get_object_checked (mono_domain_get (), &oklass->byval_arg, error);
2719 return_val_if_nok (error, FALSE);
2721 MonoError inner_error;
2722 res = mono_runtime_try_invoke (method, (MonoObject*)(mono_class_get_ref_info (klass)), params, &exc, &inner_error);
2724 if (exc || !is_ok (&inner_error)) {
2725 mono_error_cleanup (&inner_error);
2728 return *(MonoBoolean*)mono_object_unbox (res);
2732 * mono_reflection_type_get_type:
2733 * @reftype: the System.Type object
2735 * Returns the MonoType* associated with the C# System.Type object @reftype.
2738 mono_reflection_type_get_type (MonoReflectionType *reftype)
2743 MonoType *result = mono_reflection_type_get_handle (reftype, &error);
2744 mono_error_assert_ok (&error);
2749 * mono_reflection_assembly_get_assembly:
2750 * @refassembly: the System.Reflection.Assembly object
2752 * Returns the MonoAssembly* associated with the C# System.Reflection.Assembly object @refassembly.
2755 mono_reflection_assembly_get_assembly (MonoReflectionAssembly *refassembly)
2757 g_assert (refassembly);
2759 return refassembly->assembly;
2763 * mono_class_from_mono_type_handle:
2764 * @reftype: the System.Type handle
2766 * Returns the MonoClass* corresponding to the given type.
2769 mono_class_from_mono_type_handle (MonoReflectionTypeHandle reftype)
2771 return mono_class_from_mono_type (MONO_HANDLE_RAW (reftype)->type);