2 * custom-attrs.c: Custom attributes.
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/metadata/assembly.h"
16 #include "mono/metadata/gc-internals.h"
17 #include "mono/metadata/mono-endian.h"
18 #include "mono/metadata/object-internals.h"
19 #include "mono/metadata/reflection-cache.h"
20 #include "mono/metadata/custom-attrs-internals.h"
21 #include "mono/metadata/sre-internals.h"
22 #include "mono/metadata/reflection-internals.h"
23 #include "mono/metadata/tabledefs.h"
24 #include "mono/metadata/tokentype.h"
25 #include "mono/metadata/verify-internals.h"
26 #include "mono/utils/checked-build.h"
29 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
30 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
32 #if SIZEOF_VOID_P == 4
33 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
35 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
38 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
39 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
41 static gboolean type_is_reference (MonoType *type);
43 static GENERATE_GET_CLASS_WITH_CACHE (custom_attribute_typed_argument, System.Reflection, CustomAttributeTypedArgument);
44 static GENERATE_GET_CLASS_WITH_CACHE (custom_attribute_named_argument, System.Reflection, CustomAttributeNamedArgument);
47 * LOCKING: Acquires the loader lock.
49 static MonoCustomAttrInfo*
50 lookup_custom_attr (MonoImage *image, gpointer member)
52 MONO_REQ_GC_NEUTRAL_MODE;
54 MonoCustomAttrInfo* res;
56 res = (MonoCustomAttrInfo *)mono_image_property_lookup (image, member, MONO_PROP_DYNAMIC_CATTR);
61 res = (MonoCustomAttrInfo *)g_memdup (res, MONO_SIZEOF_CUSTOM_ATTR_INFO + sizeof (MonoCustomAttrEntry) * res->num_attrs);
67 custom_attr_visible (MonoImage *image, MonoReflectionCustomAttr *cattr)
69 MONO_REQ_GC_UNSAFE_MODE;
71 /* FIXME: Need to do more checks */
72 if (cattr->ctor->method && (cattr->ctor->method->klass->image != image)) {
73 int visibility = mono_class_get_flags (cattr->ctor->method->klass) & TYPE_ATTRIBUTE_VISIBILITY_MASK;
75 if ((visibility != TYPE_ATTRIBUTE_PUBLIC) && (visibility != TYPE_ATTRIBUTE_NESTED_PUBLIC))
83 type_is_reference (MonoType *type)
86 case MONO_TYPE_BOOLEAN:
100 case MONO_TYPE_VALUETYPE:
108 free_param_data (MonoMethodSignature *sig, void **params) {
110 for (i = 0; i < sig->param_count; ++i) {
111 if (!type_is_reference (sig->params [i]))
117 * Find the field index in the metadata FieldDef table.
120 find_field_index (MonoClass *klass, MonoClassField *field) {
123 int fcount = mono_class_get_field_count (klass);
124 for (i = 0; i < fcount; ++i) {
125 if (field == &klass->fields [i])
126 return mono_class_get_first_field_idx (klass) + 1 + i;
132 * Find the property index in the metadata Property table.
135 find_property_index (MonoClass *klass, MonoProperty *property) {
138 for (i = 0; i < klass->ext->property.count; ++i) {
139 if (property == &klass->ext->properties [i])
140 return klass->ext->property.first + 1 + i;
146 * Find the event index in the metadata Event table.
149 find_event_index (MonoClass *klass, MonoEvent *event) {
152 for (i = 0; i < klass->ext->event.count; ++i) {
153 if (event == &klass->ext->events [i])
154 return klass->ext->event.first + 1 + i;
160 * Load the type with name @n on behalf of image @image. On failure sets @error and returns NULL.
161 * The @is_enum flag only affects the error message that's displayed on failure.
164 cattr_type_from_name (char *n, MonoImage *image, gboolean is_enum, MonoError *error)
166 MonoError inner_error;
167 MonoType *t = mono_reflection_type_from_name_checked (n, image, &inner_error);
169 mono_error_set_type_load_name (error, g_strdup(n), NULL,
170 "Could not load %s %s while decoding custom attribute: %s",
171 is_enum ? "enum type": "type",
173 mono_error_get_message (&inner_error));
174 mono_error_cleanup (&inner_error);
181 load_cattr_enum_type (MonoImage *image, const char *p, const char **end, MonoError *error)
185 int slen = mono_metadata_decode_value (p, &p);
187 mono_error_init (error);
189 n = (char *)g_memdup (p, slen + 1);
191 t = cattr_type_from_name (n, image, TRUE, error);
193 return_val_if_nok (error, NULL);
196 return mono_class_from_mono_type (t);
200 load_cattr_value (MonoImage *image, MonoType *t, const char *p, const char **end, MonoError *error)
202 int slen, type = t->type;
203 MonoClass *tklass = t->data.klass;
205 mono_error_init (error);
211 case MONO_TYPE_BOOLEAN: {
212 MonoBoolean *bval = (MonoBoolean *)g_malloc (sizeof (MonoBoolean));
220 guint16 *val = (guint16 *)g_malloc (sizeof (guint16));
225 #if SIZEOF_VOID_P == 4
232 guint32 *val = (guint32 *)g_malloc (sizeof (guint32));
237 #if SIZEOF_VOID_P == 8
238 case MONO_TYPE_U: /* error out instead? this should probably not happen */
243 guint64 *val = (guint64 *)g_malloc (sizeof (guint64));
249 double *val = (double *)g_malloc (sizeof (double));
254 case MONO_TYPE_VALUETYPE:
255 if (t->data.klass->enumtype) {
256 type = mono_class_enum_basetype (t->data.klass)->type;
259 MonoClass *k = t->data.klass;
261 if (mono_is_corlib_image (k->image) && strcmp (k->name_space, "System") == 0 && strcmp (k->name, "DateTime") == 0){
262 guint64 *val = (guint64 *)g_malloc (sizeof (guint64));
268 g_error ("generic valutype %s not handled in custom attr value decoding", t->data.klass->name);
271 case MONO_TYPE_STRING:
272 if (*p == (char)0xFF) {
276 slen = mono_metadata_decode_value (p, &p);
278 return mono_string_new_len_checked (mono_domain_get (), p, slen, error);
279 case MONO_TYPE_CLASS: {
280 MonoReflectionType *rt;
283 if (*p == (char)0xFF) {
288 slen = mono_metadata_decode_value (p, &p);
289 n = (char *)g_memdup (p, slen + 1);
291 t = cattr_type_from_name (n, image, FALSE, error);
293 return_val_if_nok (error, NULL);
296 rt = mono_type_get_object_checked (mono_domain_get (), t, error);
297 if (!mono_error_ok (error))
302 case MONO_TYPE_OBJECT: {
305 MonoClass *subc = NULL;
310 } else if (subt == 0x0E) {
311 type = MONO_TYPE_STRING;
313 } else if (subt == 0x1D) {
314 MonoType simple_type = {{0}};
318 type = MONO_TYPE_SZARRAY;
320 tklass = mono_defaults.systemtype_class;
321 } else if (etype == 0x55) {
322 tklass = load_cattr_enum_type (image, p, &p, error);
323 if (!mono_error_ok (error))
327 /* See Partition II, Appendix B3 */
328 etype = MONO_TYPE_OBJECT;
329 simple_type.type = (MonoTypeEnum)etype;
330 tklass = mono_class_from_mono_type (&simple_type);
333 } else if (subt == 0x55) {
336 slen = mono_metadata_decode_value (p, &p);
337 n = (char *)g_memdup (p, slen + 1);
339 t = cattr_type_from_name (n, image, FALSE, error);
341 return_val_if_nok (error, NULL);
343 subc = mono_class_from_mono_type (t);
344 } else if (subt >= MONO_TYPE_BOOLEAN && subt <= MONO_TYPE_R8) {
345 MonoType simple_type = {{0}};
346 simple_type.type = (MonoTypeEnum)subt;
347 subc = mono_class_from_mono_type (&simple_type);
349 g_error ("Unknown type 0x%02x for object type encoding in custom attr", subt);
351 val = load_cattr_value (image, &subc->byval_arg, p, end, error);
353 if (mono_error_ok (error)) {
354 obj = mono_object_new_checked (mono_domain_get (), subc, error);
355 g_assert (!subc->has_references);
356 if (mono_error_ok (error))
357 mono_gc_memmove_atomic ((char*)obj + sizeof (MonoObject), val, mono_class_value_size (subc, NULL));
363 case MONO_TYPE_SZARRAY: {
365 guint32 i, alen, basetype;
368 if (alen == 0xffffffff) {
372 arr = mono_array_new_checked (mono_domain_get(), tklass, alen, error);
373 return_val_if_nok (error, NULL);
374 basetype = tklass->byval_arg.type;
375 if (basetype == MONO_TYPE_VALUETYPE && tklass->enumtype)
376 basetype = mono_class_enum_basetype (tklass)->type;
381 case MONO_TYPE_BOOLEAN:
382 for (i = 0; i < alen; i++) {
383 MonoBoolean val = *p++;
384 mono_array_set (arr, MonoBoolean, i, val);
390 for (i = 0; i < alen; i++) {
391 guint16 val = read16 (p);
392 mono_array_set (arr, guint16, i, val);
399 for (i = 0; i < alen; i++) {
400 guint32 val = read32 (p);
401 mono_array_set (arr, guint32, i, val);
406 for (i = 0; i < alen; i++) {
409 mono_array_set (arr, double, i, val);
415 for (i = 0; i < alen; i++) {
416 guint64 val = read64 (p);
417 mono_array_set (arr, guint64, i, val);
421 case MONO_TYPE_CLASS:
422 case MONO_TYPE_OBJECT:
423 case MONO_TYPE_STRING:
424 case MONO_TYPE_SZARRAY:
425 for (i = 0; i < alen; i++) {
426 MonoObject *item = (MonoObject *)load_cattr_value (image, &tklass->byval_arg, p, &p, error);
427 if (!mono_error_ok (error))
429 mono_array_setref (arr, i, item);
433 g_error ("Type 0x%02x not handled in custom attr array decoding", basetype);
439 g_error ("Type 0x%02x not handled in custom attr value decoding", type);
445 load_cattr_value_boxed (MonoDomain *domain, MonoImage *image, MonoType *t, const char* p, const char** end, MonoError *error)
447 mono_error_init (error);
449 gboolean is_ref = type_is_reference (t);
451 void *val = load_cattr_value (image, t, p, end, error);
452 if (!is_ok (error)) {
459 return (MonoObject*)val;
461 MonoObject *boxed = mono_value_box_checked (domain, mono_class_from_mono_type (t), val, error);
467 create_cattr_typed_arg (MonoType *t, MonoObject *val, MonoError *error)
469 static MonoMethod *ctor;
471 void *params [2], *unboxed;
473 mono_error_init (error);
476 ctor = mono_class_get_method_from_name (mono_class_get_custom_attribute_typed_argument_class (), ".ctor", 2);
478 params [0] = mono_type_get_object_checked (mono_domain_get (), t, error);
479 return_val_if_nok (error, NULL);
482 retval = mono_object_new_checked (mono_domain_get (), mono_class_get_custom_attribute_typed_argument_class (), error);
483 return_val_if_nok (error, NULL);
484 unboxed = mono_object_unbox (retval);
486 mono_runtime_invoke_checked (ctor, unboxed, params, error);
487 return_val_if_nok (error, NULL);
493 create_cattr_named_arg (void *minfo, MonoObject *typedarg, MonoError *error)
495 static MonoMethod *ctor;
497 void *unboxed, *params [2];
499 mono_error_init (error);
502 ctor = mono_class_get_method_from_name (mono_class_get_custom_attribute_named_argument_class (), ".ctor", 2);
505 params [1] = typedarg;
506 retval = mono_object_new_checked (mono_domain_get (), mono_class_get_custom_attribute_named_argument_class (), error);
507 return_val_if_nok (error, NULL);
509 unboxed = mono_object_unbox (retval);
511 mono_runtime_invoke_checked (ctor, unboxed, params, error);
512 return_val_if_nok (error, NULL);
519 mono_custom_attrs_from_builders (MonoImage *alloc_img, MonoImage *image, MonoArray *cattrs)
521 MONO_REQ_GC_UNSAFE_MODE;
523 int i, index, count, not_visible;
524 MonoCustomAttrInfo *ainfo;
525 MonoReflectionCustomAttr *cattr;
529 /* FIXME: check in assembly the Run flag is set */
531 count = mono_array_length (cattrs);
533 /* Skip nonpublic attributes since MS.NET seems to do the same */
534 /* FIXME: This needs to be done more globally */
536 for (i = 0; i < count; ++i) {
537 cattr = (MonoReflectionCustomAttr*)mono_array_get (cattrs, gpointer, i);
538 if (!custom_attr_visible (image, cattr))
542 int num_attrs = count - not_visible;
543 ainfo = (MonoCustomAttrInfo *)mono_image_g_malloc0 (alloc_img, MONO_SIZEOF_CUSTOM_ATTR_INFO + sizeof (MonoCustomAttrEntry) * num_attrs);
545 ainfo->image = image;
546 ainfo->num_attrs = num_attrs;
547 ainfo->cached = alloc_img != NULL;
549 for (i = 0; i < count; ++i) {
550 cattr = (MonoReflectionCustomAttr*)mono_array_get (cattrs, gpointer, i);
551 if (custom_attr_visible (image, cattr)) {
552 unsigned char *saved = (unsigned char *)mono_image_alloc (image, mono_array_length (cattr->data));
553 memcpy (saved, mono_array_addr (cattr->data, char, 0), mono_array_length (cattr->data));
554 ainfo->attrs [index].ctor = cattr->ctor->method;
555 g_assert (cattr->ctor->method);
556 ainfo->attrs [index].data = saved;
557 ainfo->attrs [index].data_size = mono_array_length (cattr->data);
561 g_assert (index == num_attrs && count == num_attrs + not_visible);
568 create_custom_attr (MonoImage *image, MonoMethod *method, const guchar *data, guint32 len, MonoError *error)
570 const char *p = (const char*)data;
572 guint32 i, j, num_named;
574 void *params_buf [32];
575 void **params = NULL;
576 MonoMethodSignature *sig;
578 mono_error_init (error);
580 mono_class_init (method->klass);
582 if (!mono_verifier_verify_cattr_content (image, method, data, len, NULL)) {
583 mono_error_set_generic_error (error, "System.Reflection", "CustomAttributeFormatException", "Binary format of the specified custom attribute was invalid.");
588 attr = mono_object_new_checked (mono_domain_get (), method->klass, error);
589 if (!mono_error_ok (error)) return NULL;
591 mono_runtime_invoke_checked (method, attr, NULL, error);
592 if (!mono_error_ok (error))
598 if (len < 2 || read16 (p) != 0x0001) /* Prolog */
601 /*g_print ("got attr %s\n", method->klass->name);*/
603 sig = mono_method_signature (method);
604 if (sig->param_count < 32) {
606 memset (params, 0, sizeof (void*) * sig->param_count);
608 /* Allocate using GC so it gets GC tracking */
609 params = (void **)mono_gc_alloc_fixed (sig->param_count * sizeof (void*), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_REFLECTION, "custom attribute parameters");
614 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
615 params [i] = load_cattr_value (image, mono_method_signature (method)->params [i], p, &p, error);
616 if (!mono_error_ok (error))
621 attr = mono_object_new_checked (mono_domain_get (), method->klass, error);
622 if (!mono_error_ok (error)) goto fail;
624 MonoObject *exc = NULL;
625 mono_runtime_try_invoke (method, attr, params, &exc, error);
626 if (!mono_error_ok (error))
629 mono_error_set_exception_instance (error, (MonoException*)exc);
633 num_named = read16 (named);
635 for (j = 0; j < num_named; j++) {
637 char *name, named_type, data_type;
638 named_type = *named++;
639 data_type = *named++; /* type of data */
640 if (data_type == MONO_TYPE_SZARRAY)
641 data_type = *named++;
642 if (data_type == MONO_TYPE_ENUM) {
645 type_len = mono_metadata_decode_blob_size (named, &named);
646 type_name = (char *)g_malloc (type_len + 1);
647 memcpy (type_name, named, type_len);
648 type_name [type_len] = 0;
650 /* FIXME: lookup the type and check type consistency */
653 name_len = mono_metadata_decode_blob_size (named, &named);
654 name = (char *)g_malloc (name_len + 1);
655 memcpy (name, named, name_len);
658 if (named_type == 0x53) {
659 MonoClassField *field;
662 /* how this fail is a blackbox */
663 field = mono_class_get_field_from_name (mono_object_class (attr), name);
665 mono_error_set_generic_error (error, "System.Reflection", "CustomAttributeFormatException", "Could not find a field with name %s", name);
670 val = load_cattr_value (image, field->type, named, &named, error);
671 if (!mono_error_ok (error)) {
673 if (!type_is_reference (field->type))
678 mono_field_set_value (attr, field, val);
679 if (!type_is_reference (field->type))
681 } else if (named_type == 0x54) {
686 prop = mono_class_get_property_from_name (mono_object_class (attr), name);
689 mono_error_set_generic_error (error, "System.Reflection", "CustomAttributeFormatException", "Could not find a property with name %s", name);
695 mono_error_set_generic_error (error, "System.Reflection", "CustomAttributeFormatException", "Could not find the setter for %s", name);
700 /* can we have more that 1 arg in a custom attr named property? */
701 prop_type = prop->get? mono_method_signature (prop->get)->ret :
702 mono_method_signature (prop->set)->params [mono_method_signature (prop->set)->param_count - 1];
704 pparams [0] = load_cattr_value (image, prop_type, named, &named, error);
705 if (!mono_error_ok (error)) {
707 if (!type_is_reference (prop_type))
708 g_free (pparams [0]);
713 mono_property_set_value_checked (prop, attr, pparams, error);
714 if (!type_is_reference (prop_type))
715 g_free (pparams [0]);
716 if (!is_ok (error)) {
724 free_param_data (method->signature, params);
725 if (params != params_buf)
726 mono_gc_free_fixed (params);
731 free_param_data (method->signature, params);
732 if (params != params_buf)
733 mono_gc_free_fixed (params);
738 * mono_reflection_create_custom_attr_data_args:
740 * Create an array of typed and named arguments from the cattr blob given by DATA.
741 * TYPED_ARGS and NAMED_ARGS will contain the objects representing the arguments,
742 * NAMED_ARG_INFO will contain information about the named arguments.
745 mono_reflection_create_custom_attr_data_args (MonoImage *image, MonoMethod *method, const guchar *data, guint32 len, MonoArray **typed_args, MonoArray **named_args, CattrNamedArg **named_arg_info, MonoError *error)
747 MonoArray *typedargs, *namedargs;
748 MonoClass *attrklass;
750 const char *p = (const char*)data;
752 guint32 i, j, num_named;
753 CattrNamedArg *arginfo = NULL;
757 *named_arg_info = NULL;
759 mono_error_init (error);
761 if (!mono_verifier_verify_cattr_content (image, method, data, len, NULL)) {
762 mono_error_set_generic_error (error, "System.Reflection", "CustomAttributeFormatException", "Binary format of the specified custom attribute was invalid.");
766 mono_class_init (method->klass);
768 domain = mono_domain_get ();
770 if (len < 2 || read16 (p) != 0x0001) /* Prolog */
773 typedargs = mono_array_new_checked (domain, mono_get_object_class (), mono_method_signature (method)->param_count, error);
774 return_if_nok (error);
778 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
781 obj = load_cattr_value_boxed (domain, image, mono_method_signature (method)->params [i], p, &p, error);
782 return_if_nok (error);
783 mono_array_setref (typedargs, i, obj);
787 num_named = read16 (named);
788 namedargs = mono_array_new_checked (domain, mono_get_object_class (), num_named, error);
789 return_if_nok (error);
791 attrklass = method->klass;
793 arginfo = g_new0 (CattrNamedArg, num_named);
794 *named_arg_info = arginfo;
796 for (j = 0; j < num_named; j++) {
798 char *name, named_type, data_type;
799 named_type = *named++;
800 data_type = *named++; /* type of data */
801 if (data_type == MONO_TYPE_SZARRAY)
802 data_type = *named++;
803 if (data_type == MONO_TYPE_ENUM) {
806 type_len = mono_metadata_decode_blob_size (named, &named);
807 if (ADDP_IS_GREATER_OR_OVF ((const guchar*)named, type_len, data + len))
810 type_name = (char *)g_malloc (type_len + 1);
811 memcpy (type_name, named, type_len);
812 type_name [type_len] = 0;
814 /* FIXME: lookup the type and check type consistency */
817 name_len = mono_metadata_decode_blob_size (named, &named);
818 if (ADDP_IS_GREATER_OR_OVF ((const guchar*)named, name_len, data + len))
820 name = (char *)g_malloc (name_len + 1);
821 memcpy (name, named, name_len);
824 if (named_type == 0x53) {
826 MonoClassField *field = mono_class_get_field_from_name (attrklass, name);
833 arginfo [j].type = field->type;
834 arginfo [j].field = field;
836 obj = load_cattr_value_boxed (domain, image, field->type, named, &named, error);
837 if (!is_ok (error)) {
841 mono_array_setref (namedargs, j, obj);
843 } else if (named_type == 0x54) {
846 MonoProperty *prop = mono_class_get_property_from_name (attrklass, name);
848 if (!prop || !prop->set) {
853 prop_type = prop->get? mono_method_signature (prop->get)->ret :
854 mono_method_signature (prop->set)->params [mono_method_signature (prop->set)->param_count - 1];
856 arginfo [j].type = prop_type;
857 arginfo [j].prop = prop;
859 obj = load_cattr_value_boxed (domain, image, prop_type, named, &named, error);
860 if (!is_ok (error)) {
864 mono_array_setref (namedargs, j, obj);
869 *typed_args = typedargs;
870 *named_args = namedargs;
873 mono_error_set_generic_error (error, "System.Reflection", "CustomAttributeFormatException", "Binary format of the specified custom attribute was invalid.");
875 *named_arg_info = NULL;
879 reflection_resolve_custom_attribute_data (MonoReflectionMethod *ref_method, MonoReflectionAssembly *assembly, gpointer data, guint32 len, MonoArray **ctor_args, MonoArray **named_args, MonoError *error)
882 MonoArray *typedargs, *namedargs;
885 CattrNamedArg *arginfo = NULL;
888 mono_error_init (error);
896 image = assembly->assembly->image;
897 method = ref_method->method;
898 domain = mono_object_domain (ref_method);
900 if (!mono_class_init (method->klass)) {
901 mono_error_set_for_class_failure (error, method->klass);
905 mono_reflection_create_custom_attr_data_args (image, method, (const guchar *)data, len, &typedargs, &namedargs, &arginfo, error);
909 if (!typedargs || !namedargs)
912 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
913 MonoObject *obj = mono_array_get (typedargs, MonoObject*, i);
914 MonoObject *typedarg;
916 typedarg = create_cattr_typed_arg (mono_method_signature (method)->params [i], obj, error);
919 mono_array_setref (typedargs, i, typedarg);
922 for (i = 0; i < mono_array_length (namedargs); ++i) {
923 MonoObject *obj = mono_array_get (namedargs, MonoObject*, i);
924 MonoObject *typedarg, *namedarg, *minfo;
926 if (arginfo [i].prop) {
927 minfo = (MonoObject*)mono_property_get_object_checked (domain, NULL, arginfo [i].prop, error);
931 minfo = (MonoObject*)mono_field_get_object_checked (domain, NULL, arginfo [i].field, error);
936 typedarg = create_cattr_typed_arg (arginfo [i].type, obj, error);
939 namedarg = create_cattr_named_arg (minfo, typedarg, error);
943 mono_array_setref (namedargs, i, namedarg);
946 *ctor_args = typedargs;
947 *named_args = namedargs;
951 return mono_error_ok (error);
955 ves_icall_System_Reflection_CustomAttributeData_ResolveArgumentsInternal (MonoReflectionMethod *ref_method, MonoReflectionAssembly *assembly, gpointer data, guint32 len, MonoArray **ctor_args, MonoArray **named_args)
958 (void) reflection_resolve_custom_attribute_data (ref_method, assembly, data, len, ctor_args, named_args, &error);
959 mono_error_set_pending_exception (&error);
963 create_custom_attr_data (MonoImage *image, MonoCustomAttrEntry *cattr, MonoError *error)
965 static MonoMethod *ctor;
971 mono_error_init (error);
973 g_assert (image->assembly);
976 ctor = mono_class_get_method_from_name (mono_defaults.customattribute_data_class, ".ctor", 4);
978 domain = mono_domain_get ();
979 attr = mono_object_new_checked (domain, mono_defaults.customattribute_data_class, error);
980 return_val_if_nok (error, NULL);
981 params [0] = mono_method_get_object_checked (domain, cattr->ctor, NULL, error);
982 return_val_if_nok (error, NULL);
983 params [1] = mono_assembly_get_object_checked (domain, image->assembly, error);
984 return_val_if_nok (error, NULL);
985 params [2] = (gpointer)&cattr->data;
986 params [3] = &cattr->data_size;
988 mono_runtime_invoke_checked (ctor, attr, params, error);
989 return_val_if_nok (error, NULL);
994 mono_custom_attrs_construct_by_type (MonoCustomAttrInfo *cinfo, MonoClass *attr_klass, MonoError *error)
1000 mono_error_init (error);
1002 for (i = 0; i < cinfo->num_attrs; ++i) {
1003 MonoCustomAttrEntry *centry = &cinfo->attrs[i];
1004 if (!centry->ctor) {
1005 /* The cattr type is not finished yet */
1006 /* We should include the type name but cinfo doesn't contain it */
1007 mono_error_set_type_load_name (error, NULL, NULL, "Custom attribute constructor is null because the custom attribute type is not finished yet.");
1014 for (i = 0; i < cinfo->num_attrs; ++i) {
1015 MonoMethod *ctor = cinfo->attrs[i].ctor;
1017 if (mono_class_is_assignable_from (attr_klass, ctor->klass))
1021 n = cinfo->num_attrs;
1024 result = mono_array_new_cached (mono_domain_get (), mono_defaults.attribute_class, n, error);
1025 return_val_if_nok (error, NULL);
1027 for (i = 0; i < cinfo->num_attrs; ++i) {
1028 MonoCustomAttrEntry *centry = &cinfo->attrs [i];
1029 if (!attr_klass || mono_class_is_assignable_from (attr_klass, centry->ctor->klass)) {
1030 attr = create_custom_attr (cinfo->image, centry->ctor, centry->data, centry->data_size, error);
1031 if (!mono_error_ok (error))
1033 mono_array_setref (result, n, attr);
1041 mono_custom_attrs_construct (MonoCustomAttrInfo *cinfo)
1044 MonoArray *result = mono_custom_attrs_construct_by_type (cinfo, NULL, &error);
1045 mono_error_assert_ok (&error); /*FIXME proper error handling*/
1051 mono_custom_attrs_data_construct (MonoCustomAttrInfo *cinfo, MonoError *error)
1057 mono_error_init (error);
1058 result = mono_array_new_checked (mono_domain_get (), mono_defaults.customattribute_data_class, cinfo->num_attrs, error);
1059 return_val_if_nok (error, NULL);
1060 for (i = 0; i < cinfo->num_attrs; ++i) {
1061 attr = create_custom_attr_data (cinfo->image, &cinfo->attrs [i], error);
1062 return_val_if_nok (error, NULL);
1063 mono_array_setref (result, i, attr);
1069 * mono_custom_attrs_from_index:
1071 * Returns: NULL if no attributes are found or if a loading error occurs.
1074 mono_custom_attrs_from_index (MonoImage *image, guint32 idx)
1077 MonoCustomAttrInfo *result = mono_custom_attrs_from_index_checked (image, idx, FALSE, &error);
1078 mono_error_cleanup (&error);
1082 * mono_custom_attrs_from_index_checked:
1084 * Returns: NULL if no attributes are found. On error returns NULL and sets @error.
1087 mono_custom_attrs_from_index_checked (MonoImage *image, guint32 idx, gboolean ignore_missing, MonoError *error)
1089 guint32 mtoken, i, len;
1090 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
1092 MonoCustomAttrInfo *ainfo;
1093 GList *tmp, *list = NULL;
1095 MonoCustomAttrEntry* attr;
1097 mono_error_init (error);
1099 ca = &image->tables [MONO_TABLE_CUSTOMATTRIBUTE];
1101 i = mono_metadata_custom_attrs_from_index (image, idx);
1105 while (i < ca->rows) {
1106 if (mono_metadata_decode_row_col (ca, i, MONO_CUSTOM_ATTR_PARENT) != idx)
1108 list = g_list_prepend (list, GUINT_TO_POINTER (i));
1111 len = g_list_length (list);
1114 ainfo = (MonoCustomAttrInfo *)g_malloc0 (MONO_SIZEOF_CUSTOM_ATTR_INFO + sizeof (MonoCustomAttrEntry) * len);
1115 ainfo->num_attrs = len;
1116 ainfo->image = image;
1117 for (i = len, tmp = list; i != 0; --i, tmp = tmp->next) {
1118 mono_metadata_decode_row (ca, GPOINTER_TO_UINT (tmp->data), cols, MONO_CUSTOM_ATTR_SIZE);
1119 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
1120 switch (cols [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
1121 case MONO_CUSTOM_ATTR_TYPE_METHODDEF:
1122 mtoken |= MONO_TOKEN_METHOD_DEF;
1124 case MONO_CUSTOM_ATTR_TYPE_MEMBERREF:
1125 mtoken |= MONO_TOKEN_MEMBER_REF;
1128 g_error ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
1131 attr = &ainfo->attrs [i - 1];
1132 attr->ctor = mono_get_method_checked (image, mtoken, NULL, NULL, error);
1134 g_warning ("Can't find custom attr constructor image: %s mtoken: 0x%08x due to: %s", image->name, mtoken, mono_error_get_message (error));
1135 if (ignore_missing) {
1136 mono_error_cleanup (error);
1137 mono_error_init (error);
1145 if (!mono_verifier_verify_cattr_blob (image, cols [MONO_CUSTOM_ATTR_VALUE], NULL)) {
1146 /*FIXME raising an exception here doesn't make any sense*/
1147 g_warning ("Invalid custom attribute blob on image %s for index %x", image->name, idx);
1152 data = mono_metadata_blob_heap (image, cols [MONO_CUSTOM_ATTR_VALUE]);
1153 attr->data_size = mono_metadata_decode_value (data, &data);
1154 attr->data = (guchar*)data;
1162 mono_custom_attrs_from_method (MonoMethod *method)
1165 MonoCustomAttrInfo* result = mono_custom_attrs_from_method_checked (method, &error);
1166 mono_error_cleanup (&error); /* FIXME want a better API that doesn't swallow the error */
1171 mono_custom_attrs_from_method_checked (MonoMethod *method, MonoError *error)
1175 mono_error_init (error);
1178 * An instantiated method has the same cattrs as the generic method definition.
1180 * LAMESPEC: The .NET SRE throws an exception for instantiations of generic method builders
1181 * Note that this stanza is not necessary for non-SRE types, but it's a micro-optimization
1183 if (method->is_inflated)
1184 method = ((MonoMethodInflated *) method)->declaring;
1186 if (method_is_dynamic (method) || image_is_dynamic (method->klass->image))
1187 return lookup_custom_attr (method->klass->image, method);
1190 /* Synthetic methods */
1193 idx = mono_method_get_index (method);
1194 idx <<= MONO_CUSTOM_ATTR_BITS;
1195 idx |= MONO_CUSTOM_ATTR_METHODDEF;
1196 return mono_custom_attrs_from_index_checked (method->klass->image, idx, FALSE, error);
1200 mono_custom_attrs_from_class (MonoClass *klass)
1203 MonoCustomAttrInfo *result = mono_custom_attrs_from_class_checked (klass, &error);
1204 mono_error_cleanup (&error);
1209 mono_custom_attrs_from_class_checked (MonoClass *klass, MonoError *error)
1213 mono_error_init (error);
1215 if (mono_class_is_ginst (klass))
1216 klass = mono_class_get_generic_class (klass)->container_class;
1218 if (image_is_dynamic (klass->image))
1219 return lookup_custom_attr (klass->image, klass);
1221 if (klass->byval_arg.type == MONO_TYPE_VAR || klass->byval_arg.type == MONO_TYPE_MVAR) {
1222 idx = mono_metadata_token_index (klass->sizes.generic_param_token);
1223 idx <<= MONO_CUSTOM_ATTR_BITS;
1224 idx |= MONO_CUSTOM_ATTR_GENERICPAR;
1226 idx = mono_metadata_token_index (klass->type_token);
1227 idx <<= MONO_CUSTOM_ATTR_BITS;
1228 idx |= MONO_CUSTOM_ATTR_TYPEDEF;
1230 return mono_custom_attrs_from_index_checked (klass->image, idx, FALSE, error);
1234 mono_custom_attrs_from_assembly (MonoAssembly *assembly)
1237 MonoCustomAttrInfo *result = mono_custom_attrs_from_assembly_checked (assembly, FALSE, &error);
1238 mono_error_cleanup (&error);
1243 mono_custom_attrs_from_assembly_checked (MonoAssembly *assembly, gboolean ignore_missing, MonoError *error)
1247 mono_error_init (error);
1249 if (image_is_dynamic (assembly->image))
1250 return lookup_custom_attr (assembly->image, assembly);
1251 idx = 1; /* there is only one assembly */
1252 idx <<= MONO_CUSTOM_ATTR_BITS;
1253 idx |= MONO_CUSTOM_ATTR_ASSEMBLY;
1254 return mono_custom_attrs_from_index_checked (assembly->image, idx, ignore_missing, error);
1257 static MonoCustomAttrInfo*
1258 mono_custom_attrs_from_module (MonoImage *image, MonoError *error)
1262 if (image_is_dynamic (image))
1263 return lookup_custom_attr (image, image);
1264 idx = 1; /* there is only one module */
1265 idx <<= MONO_CUSTOM_ATTR_BITS;
1266 idx |= MONO_CUSTOM_ATTR_MODULE;
1267 return mono_custom_attrs_from_index_checked (image, idx, FALSE, error);
1271 mono_custom_attrs_from_property (MonoClass *klass, MonoProperty *property)
1274 MonoCustomAttrInfo * result = mono_custom_attrs_from_property_checked (klass, property, &error);
1275 mono_error_cleanup (&error);
1280 mono_custom_attrs_from_property_checked (MonoClass *klass, MonoProperty *property, MonoError *error)
1284 if (image_is_dynamic (klass->image)) {
1285 property = mono_metadata_get_corresponding_property_from_generic_type_definition (property);
1286 return lookup_custom_attr (klass->image, property);
1288 idx = find_property_index (klass, property);
1289 idx <<= MONO_CUSTOM_ATTR_BITS;
1290 idx |= MONO_CUSTOM_ATTR_PROPERTY;
1291 return mono_custom_attrs_from_index_checked (klass->image, idx, FALSE, error);
1295 mono_custom_attrs_from_event (MonoClass *klass, MonoEvent *event)
1298 MonoCustomAttrInfo * result = mono_custom_attrs_from_event_checked (klass, event, &error);
1299 mono_error_cleanup (&error);
1304 mono_custom_attrs_from_event_checked (MonoClass *klass, MonoEvent *event, MonoError *error)
1308 if (image_is_dynamic (klass->image)) {
1309 event = mono_metadata_get_corresponding_event_from_generic_type_definition (event);
1310 return lookup_custom_attr (klass->image, event);
1312 idx = find_event_index (klass, event);
1313 idx <<= MONO_CUSTOM_ATTR_BITS;
1314 idx |= MONO_CUSTOM_ATTR_EVENT;
1315 return mono_custom_attrs_from_index_checked (klass->image, idx, FALSE, error);
1319 mono_custom_attrs_from_field (MonoClass *klass, MonoClassField *field)
1322 MonoCustomAttrInfo * result = mono_custom_attrs_from_field_checked (klass, field, &error);
1323 mono_error_cleanup (&error);
1328 mono_custom_attrs_from_field_checked (MonoClass *klass, MonoClassField *field, MonoError *error)
1331 mono_error_init (error);
1333 if (image_is_dynamic (klass->image)) {
1334 field = mono_metadata_get_corresponding_field_from_generic_type_definition (field);
1335 return lookup_custom_attr (klass->image, field);
1337 idx = find_field_index (klass, field);
1338 idx <<= MONO_CUSTOM_ATTR_BITS;
1339 idx |= MONO_CUSTOM_ATTR_FIELDDEF;
1340 return mono_custom_attrs_from_index_checked (klass->image, idx, FALSE, error);
1344 * mono_custom_attrs_from_param:
1345 * @method: handle to the method that we want to retrieve custom parameter information from
1346 * @param: parameter number, where zero represent the return value, and one is the first parameter in the method
1348 * The result must be released with mono_custom_attrs_free().
1350 * Returns: the custom attribute object for the specified parameter, or NULL if there are none.
1353 mono_custom_attrs_from_param (MonoMethod *method, guint32 param)
1356 MonoCustomAttrInfo *result = mono_custom_attrs_from_param_checked (method, param, &error);
1357 mono_error_cleanup (&error);
1362 * mono_custom_attrs_from_param_checked:
1363 * @method: handle to the method that we want to retrieve custom parameter information from
1364 * @param: parameter number, where zero represent the return value, and one is the first parameter in the method
1365 * @error: set on error
1367 * The result must be released with mono_custom_attrs_free().
1369 * Returns: the custom attribute object for the specified parameter, or NULL if there are none. On failure returns NULL and sets @error.
1372 mono_custom_attrs_from_param_checked (MonoMethod *method, guint32 param, MonoError *error)
1375 guint32 i, idx, method_index;
1376 guint32 param_list, param_last, param_pos, found;
1378 MonoReflectionMethodAux *aux;
1380 mono_error_init (error);
1383 * An instantiated method has the same cattrs as the generic method definition.
1385 * LAMESPEC: The .NET SRE throws an exception for instantiations of generic method builders
1386 * Note that this stanza is not necessary for non-SRE types, but it's a micro-optimization
1388 if (method->is_inflated)
1389 method = ((MonoMethodInflated *) method)->declaring;
1391 if (image_is_dynamic (method->klass->image)) {
1392 MonoCustomAttrInfo *res, *ainfo;
1395 aux = (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1396 if (!aux || !aux->param_cattr)
1399 /* Need to copy since it will be freed later */
1400 ainfo = aux->param_cattr [param];
1403 size = MONO_SIZEOF_CUSTOM_ATTR_INFO + sizeof (MonoCustomAttrEntry) * ainfo->num_attrs;
1404 res = (MonoCustomAttrInfo *)g_malloc0 (size);
1405 memcpy (res, ainfo, size);
1409 image = method->klass->image;
1410 method_index = mono_method_get_index (method);
1413 ca = &image->tables [MONO_TABLE_METHOD];
1415 param_list = mono_metadata_decode_row_col (ca, method_index - 1, MONO_METHOD_PARAMLIST);
1416 if (method_index == ca->rows) {
1417 ca = &image->tables [MONO_TABLE_PARAM];
1418 param_last = ca->rows + 1;
1420 param_last = mono_metadata_decode_row_col (ca, method_index, MONO_METHOD_PARAMLIST);
1421 ca = &image->tables [MONO_TABLE_PARAM];
1424 for (i = param_list; i < param_last; ++i) {
1425 param_pos = mono_metadata_decode_row_col (ca, i - 1, MONO_PARAM_SEQUENCE);
1426 if (param_pos == param) {
1434 idx <<= MONO_CUSTOM_ATTR_BITS;
1435 idx |= MONO_CUSTOM_ATTR_PARAMDEF;
1436 return mono_custom_attrs_from_index_checked (image, idx, FALSE, error);
1440 mono_custom_attrs_has_attr (MonoCustomAttrInfo *ainfo, MonoClass *attr_klass)
1443 for (i = 0; i < ainfo->num_attrs; ++i) {
1444 MonoCustomAttrEntry *centry = &ainfo->attrs[i];
1445 if (centry->ctor == NULL)
1447 MonoClass *klass = centry->ctor->klass;
1448 if (klass == attr_klass || mono_class_has_parent (klass, attr_klass) || (MONO_CLASS_IS_INTERFACE (attr_klass) && mono_class_is_assignable_from (attr_klass, klass)))
1455 mono_custom_attrs_get_attr (MonoCustomAttrInfo *ainfo, MonoClass *attr_klass)
1458 MonoObject *res = mono_custom_attrs_get_attr_checked (ainfo, attr_klass, &error);
1459 mono_error_assert_ok (&error); /*FIXME proper error handling*/
1464 mono_custom_attrs_get_attr_checked (MonoCustomAttrInfo *ainfo, MonoClass *attr_klass, MonoError *error)
1467 MonoCustomAttrEntry *centry = NULL;
1469 g_assert (attr_klass != NULL);
1471 mono_error_init (error);
1473 for (i = 0; i < ainfo->num_attrs; ++i) {
1474 centry = &ainfo->attrs[i];
1475 if (centry->ctor == NULL)
1477 MonoClass *klass = centry->ctor->klass;
1478 if (attr_klass == klass || mono_class_is_assignable_from (attr_klass, klass))
1484 return create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, error);
1488 * mono_reflection_get_custom_attrs_info:
1489 * @obj: a reflection object handle
1491 * Return the custom attribute info for attributes defined for the
1492 * reflection handle @obj. The objects.
1494 * FIXME this function leaks like a sieve for SRE objects.
1497 mono_reflection_get_custom_attrs_info (MonoObject *obj)
1500 MonoCustomAttrInfo *result = mono_reflection_get_custom_attrs_info_checked (obj, &error);
1501 mono_error_assert_ok (&error);
1506 * mono_reflection_get_custom_attrs_info_checked:
1507 * @obj: a reflection object handle
1508 * @error: set on error
1510 * Return the custom attribute info for attributes defined for the
1511 * reflection handle @obj. The objects.
1513 * On failure returns NULL and sets @error.
1515 * FIXME this function leaks like a sieve for SRE objects.
1518 mono_reflection_get_custom_attrs_info_checked (MonoObject *obj, MonoError *error)
1521 MonoCustomAttrInfo *cinfo = NULL;
1523 mono_error_init (error);
1525 klass = obj->vtable->klass;
1526 if (klass == mono_defaults.runtimetype_class) {
1527 MonoType *type = mono_reflection_type_get_handle ((MonoReflectionType *)obj, error);
1528 return_val_if_nok (error, NULL);
1529 klass = mono_class_from_mono_type (type);
1530 /*We cannot mono_class_init the class from which we'll load the custom attributes since this must work with broken types.*/
1531 cinfo = mono_custom_attrs_from_class_checked (klass, error);
1532 return_val_if_nok (error, NULL);
1533 } else if (strcmp ("Assembly", klass->name) == 0 || strcmp ("MonoAssembly", klass->name) == 0) {
1534 MonoReflectionAssembly *rassembly = (MonoReflectionAssembly*)obj;
1535 cinfo = mono_custom_attrs_from_assembly_checked (rassembly->assembly, FALSE, error);
1536 return_val_if_nok (error, NULL);
1537 } else if (strcmp ("Module", klass->name) == 0 || strcmp ("MonoModule", klass->name) == 0) {
1538 MonoReflectionModule *module = (MonoReflectionModule*)obj;
1539 cinfo = mono_custom_attrs_from_module (module->image, error);
1540 return_val_if_nok (error, NULL);
1541 } else if (strcmp ("MonoProperty", klass->name) == 0) {
1542 MonoReflectionProperty *rprop = (MonoReflectionProperty*)obj;
1543 cinfo = mono_custom_attrs_from_property_checked (rprop->property->parent, rprop->property, error);
1544 return_val_if_nok (error, NULL);
1545 } else if (strcmp ("MonoEvent", klass->name) == 0) {
1546 MonoReflectionMonoEvent *revent = (MonoReflectionMonoEvent*)obj;
1547 cinfo = mono_custom_attrs_from_event_checked (revent->event->parent, revent->event, error);
1548 return_val_if_nok (error, NULL);
1549 } else if (strcmp ("MonoField", klass->name) == 0) {
1550 MonoReflectionField *rfield = (MonoReflectionField*)obj;
1551 cinfo = mono_custom_attrs_from_field_checked (rfield->field->parent, rfield->field, error);
1552 return_val_if_nok (error, NULL);
1553 } else if ((strcmp ("MonoMethod", klass->name) == 0) || (strcmp ("MonoCMethod", klass->name) == 0)) {
1554 MonoReflectionMethod *rmethod = (MonoReflectionMethod*)obj;
1555 cinfo = mono_custom_attrs_from_method_checked (rmethod->method, error);
1556 return_val_if_nok (error, NULL);
1557 } else if (strcmp ("ParameterInfo", klass->name) == 0 || strcmp ("MonoParameterInfo", klass->name) == 0) {
1558 MonoReflectionParameter *param = (MonoReflectionParameter*)obj;
1559 MonoClass *member_class = mono_object_class (param->MemberImpl);
1560 if (mono_class_is_reflection_method_or_constructor (member_class)) {
1561 MonoReflectionMethod *rmethod = (MonoReflectionMethod*)param->MemberImpl;
1562 cinfo = mono_custom_attrs_from_param_checked (rmethod->method, param->PositionImpl + 1, error);
1563 return_val_if_nok (error, NULL);
1564 } else if (mono_is_sr_mono_property (member_class)) {
1565 MonoReflectionProperty *prop = (MonoReflectionProperty *)param->MemberImpl;
1567 if (!(method = prop->property->get))
1568 method = prop->property->set;
1571 cinfo = mono_custom_attrs_from_param_checked (method, param->PositionImpl + 1, error);
1572 return_val_if_nok (error, NULL);
1574 #ifndef DISABLE_REFLECTION_EMIT
1575 else if (mono_is_sre_method_on_tb_inst (member_class)) {/*XXX This is a workaround for Compiler Context*/
1576 // FIXME: Is this still needed ?
1577 g_assert_not_reached ();
1578 } else if (mono_is_sre_ctor_on_tb_inst (member_class)) { /*XX This is a workaround for Compiler Context*/
1579 // FIXME: Is this still needed ?
1580 g_assert_not_reached ();
1584 char *type_name = mono_type_get_full_name (member_class);
1585 mono_error_set_not_supported (error,
1586 "Custom attributes on a ParamInfo with member %s are not supported",
1591 } else if (strcmp ("AssemblyBuilder", klass->name) == 0) {
1592 MonoReflectionAssemblyBuilder *assemblyb = (MonoReflectionAssemblyBuilder*)obj;
1593 cinfo = mono_custom_attrs_from_builders (NULL, assemblyb->assembly.assembly->image, assemblyb->cattrs);
1594 } else if (strcmp ("TypeBuilder", klass->name) == 0) {
1595 MonoReflectionTypeBuilder *tb = (MonoReflectionTypeBuilder*)obj;
1596 cinfo = mono_custom_attrs_from_builders (NULL, &tb->module->dynamic_image->image, tb->cattrs);
1597 } else if (strcmp ("ModuleBuilder", klass->name) == 0) {
1598 MonoReflectionModuleBuilder *mb = (MonoReflectionModuleBuilder*)obj;
1599 cinfo = mono_custom_attrs_from_builders (NULL, &mb->dynamic_image->image, mb->cattrs);
1600 } else if (strcmp ("ConstructorBuilder", klass->name) == 0) {
1601 MonoReflectionCtorBuilder *cb = (MonoReflectionCtorBuilder*)obj;
1602 cinfo = mono_custom_attrs_from_builders (NULL, cb->mhandle->klass->image, cb->cattrs);
1603 } else if (strcmp ("MethodBuilder", klass->name) == 0) {
1604 MonoReflectionMethodBuilder *mb = (MonoReflectionMethodBuilder*)obj;
1605 cinfo = mono_custom_attrs_from_builders (NULL, mb->mhandle->klass->image, mb->cattrs);
1606 } else if (strcmp ("FieldBuilder", klass->name) == 0) {
1607 MonoReflectionFieldBuilder *fb = (MonoReflectionFieldBuilder*)obj;
1608 cinfo = mono_custom_attrs_from_builders (NULL, &((MonoReflectionTypeBuilder*)fb->typeb)->module->dynamic_image->image, fb->cattrs);
1609 } else if (strcmp ("MonoGenericClass", klass->name) == 0) {
1610 MonoReflectionGenericClass *gclass = (MonoReflectionGenericClass*)obj;
1611 cinfo = mono_reflection_get_custom_attrs_info_checked ((MonoObject*)gclass->generic_type, error);
1612 return_val_if_nok (error, NULL);
1613 } else { /* handle other types here... */
1614 g_error ("get custom attrs not yet supported for %s", klass->name);
1621 * mono_reflection_get_custom_attrs_by_type:
1622 * @obj: a reflection object handle
1624 * Return an array with all the custom attributes defined of the
1625 * reflection handle @obj. If @attr_klass is non-NULL, only custom attributes
1626 * of that type are returned. The objects are fully build. Return NULL if a loading error
1630 mono_reflection_get_custom_attrs_by_type (MonoObject *obj, MonoClass *attr_klass, MonoError *error)
1633 MonoCustomAttrInfo *cinfo;
1635 mono_error_init (error);
1637 cinfo = mono_reflection_get_custom_attrs_info_checked (obj, error);
1638 return_val_if_nok (error, NULL);
1640 result = mono_custom_attrs_construct_by_type (cinfo, attr_klass, error);
1642 mono_custom_attrs_free (cinfo);
1646 result = mono_array_new_cached (mono_domain_get (), mono_defaults.attribute_class, 0, error);
1653 * mono_reflection_get_custom_attrs:
1654 * @obj: a reflection object handle
1656 * Return an array with all the custom attributes defined of the
1657 * reflection handle @obj. The objects are fully build. Return NULL if a loading error
1661 mono_reflection_get_custom_attrs (MonoObject *obj)
1665 return mono_reflection_get_custom_attrs_by_type (obj, NULL, &error);
1669 * mono_reflection_get_custom_attrs_data:
1670 * @obj: a reflection obj handle
1672 * Returns an array of System.Reflection.CustomAttributeData,
1673 * which include information about attributes reflected on
1674 * types loaded using the Reflection Only methods
1677 mono_reflection_get_custom_attrs_data (MonoObject *obj)
1681 result = mono_reflection_get_custom_attrs_data_checked (obj, &error);
1682 mono_error_cleanup (&error);
1687 * mono_reflection_get_custom_attrs_data_checked:
1688 * @obj: a reflection obj handle
1689 * @error: set on error
1691 * Returns an array of System.Reflection.CustomAttributeData,
1692 * which include information about attributes reflected on
1693 * types loaded using the Reflection Only methods
1696 mono_reflection_get_custom_attrs_data_checked (MonoObject *obj, MonoError *error)
1699 MonoCustomAttrInfo *cinfo;
1701 mono_error_init (error);
1703 cinfo = mono_reflection_get_custom_attrs_info_checked (obj, error);
1704 return_val_if_nok (error, NULL);
1706 result = mono_custom_attrs_data_construct (cinfo, error);
1708 mono_custom_attrs_free (cinfo);
1709 return_val_if_nok (error, NULL);
1711 result = mono_array_new_checked (mono_domain_get (), mono_defaults.customattribute_data_class, 0, error);
1717 custom_attr_class_name_from_methoddef (MonoImage *image, guint32 method_token, const gchar **nspace, const gchar **class_name)
1719 /* mono_get_method_from_token () */
1720 g_assert (mono_metadata_token_table (method_token) == MONO_TABLE_METHOD);
1721 guint32 type_token = mono_metadata_typedef_from_method (image, method_token);
1723 /* Bad method token (could not find corresponding typedef) */
1726 type_token |= MONO_TOKEN_TYPE_DEF;
1728 /* mono_class_create_from_typedef () */
1729 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1730 guint32 cols [MONO_TYPEDEF_SIZE];
1731 guint tidx = mono_metadata_token_index (type_token);
1733 if (mono_metadata_token_table (type_token) != MONO_TABLE_TYPEDEF || tidx > tt->rows) {
1734 /* "Invalid typedef token %x", type_token */
1738 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1741 *class_name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1743 *nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1750 * custom_attr_class_name_from_method_token:
1751 * @image: The MonoImage
1752 * @method_token: a token for a custom attr constructor in @image
1753 * @assembly_token: out argment set to the assembly ref token of the custom attr
1754 * @nspace: out argument set to namespace (a string in the string heap of @image) of the custom attr
1755 * @class_name: out argument set to the class name of the custom attr.
1757 * Given an @image and a @method_token (which is assumed to be a
1758 * constructor), fills in the out arguments with the assembly ref (if
1759 * a methodref) and the namespace and class name of the custom
1762 * Returns: TRUE on success, FALSE otherwise.
1764 * LOCKING: does not take locks
1767 custom_attr_class_name_from_method_token (MonoImage *image, guint32 method_token, guint32 *assembly_token, const gchar **nspace, const gchar **class_name)
1769 /* This only works with method tokens constructed from a
1770 * custom attr token, which can only be methoddef or
1772 g_assert (mono_metadata_token_table (method_token) == MONO_TABLE_METHOD
1773 || mono_metadata_token_table (method_token) == MONO_TABLE_MEMBERREF);
1775 if (mono_metadata_token_table (method_token) == MONO_TABLE_MEMBERREF) {
1776 /* method_from_memberref () */
1778 guint32 nindex, class_index;
1780 int idx = mono_metadata_token_index (method_token);
1782 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
1783 nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
1784 class_index = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
1785 if (class_index == MONO_MEMBERREF_PARENT_TYPEREF) {
1786 guint32 type_token = MONO_TOKEN_TYPE_REF | nindex;
1787 /* mono_class_from_typeref_checked () */
1789 guint32 cols [MONO_TYPEREF_SIZE];
1790 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
1792 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
1795 *class_name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
1797 *nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
1799 *assembly_token = cols [MONO_TYPEREF_SCOPE];
1802 } else if (class_index == MONO_MEMBERREF_PARENT_METHODDEF) {
1803 guint32 methoddef_token = MONO_TOKEN_METHOD_DEF | nindex;
1805 *assembly_token = 0;
1806 return custom_attr_class_name_from_methoddef (image, methoddef_token, nspace, class_name);
1808 /* Attributes can't be generic, so it won't be
1809 * a typespec, and they're always
1810 * constructors, so it won't be a moduleref */
1811 g_assert_not_reached ();
1814 /* must be MONO_TABLE_METHOD */
1816 *assembly_token = 0;
1817 return custom_attr_class_name_from_methoddef (image, method_token, nspace, class_name);
1822 * mono_assembly_metadata_foreach_custom_attr:
1823 * @assembly: the assembly to iterate over
1824 * @func: the function to call for each custom attribute
1825 * @user_data: passed to @func
1827 * Calls @func for each custom attribute type on the given assembly until @func returns TRUE.
1828 * Everything is done using low-level metadata APIs, so it is safe to use during assembly loading.
1832 mono_assembly_metadata_foreach_custom_attr (MonoAssembly *assembly, MonoAssemblyMetadataCustomAttrIterFunc func, gpointer user_data)
1836 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
1841 * This might be called during assembly loading, so do everything using the low-level
1845 image = assembly->image;
1846 g_assert (!image_is_dynamic (image));
1847 idx = 1; /* there is only one assembly */
1848 idx <<= MONO_CUSTOM_ATTR_BITS;
1849 idx |= MONO_CUSTOM_ATTR_ASSEMBLY;
1851 /* Inlined from mono_custom_attrs_from_index_checked () */
1852 ca = &image->tables [MONO_TABLE_CUSTOMATTRIBUTE];
1853 i = mono_metadata_custom_attrs_from_index (image, idx);
1857 gboolean stop_iterating = FALSE;
1858 while (!stop_iterating && i < ca->rows) {
1859 if (mono_metadata_decode_row_col (ca, i, MONO_CUSTOM_ATTR_PARENT) != idx)
1861 mono_metadata_decode_row (ca, i, cols, MONO_CUSTOM_ATTR_SIZE);
1863 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
1864 switch (cols [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
1865 case MONO_CUSTOM_ATTR_TYPE_METHODDEF:
1866 mtoken |= MONO_TOKEN_METHOD_DEF;
1868 case MONO_CUSTOM_ATTR_TYPE_MEMBERREF:
1869 mtoken |= MONO_TOKEN_MEMBER_REF;
1872 g_warning ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
1876 const char *nspace = NULL;
1877 const char *name = NULL;
1878 guint32 assembly_token = 0;
1880 if (!custom_attr_class_name_from_method_token (image, mtoken, &assembly_token, &nspace, &name))
1883 stop_iterating = func (image, assembly_token, nspace, name, mtoken, user_data);