2 #ifndef __MONO_METADATA_INTERNALS_H__
3 #define __MONO_METADATA_INTERNALS_H__
5 #include "mono/metadata/image.h"
6 #include "mono/metadata/blob.h"
7 #include "mono/metadata/mempool.h"
8 #include "mono/metadata/domain-internals.h"
9 #include "mono/metadata/mono-hash.h"
10 #include "mono/utils/mono-compiler.h"
11 #include "mono/utils/mono-dl.h"
12 #include "mono/utils/monobitset.h"
13 #include "mono/utils/mono-property-hash.h"
14 #include "mono/utils/mono-value-hash.h"
15 #include <mono/utils/mono-error.h>
16 #include "mono/utils/mono-conc-hashtable.h"
20 MonoClass *klass; /* for VALUETYPE and CLASS */
21 MonoType *type; /* for PTR */
22 MonoArrayType *array; /* for ARRAY */
23 MonoMethodSignature *method;
24 MonoGenericParam *generic_param; /* for VAR and MVAR */
25 MonoGenericClass *generic_class; /* for GENERICINST */
27 unsigned int attrs : 16; /* param attributes or field flags */
28 MonoTypeEnum type : 8;
29 unsigned int num_mods : 6; /* max 64 modifiers follow at the end */
30 unsigned int byref : 1;
31 unsigned int pinned : 1; /* valid when included in a local var signature */
32 MonoCustomMod modifiers [MONO_ZERO_LEN_ARRAY]; /* this may grow */
35 #define MONO_SIZEOF_TYPE (offsetof (struct _MonoType, modifiers))
37 #define MONO_SECMAN_FLAG_INIT(x) (x & 0x2)
38 #define MONO_SECMAN_FLAG_GET_VALUE(x) (x & 0x1)
39 #define MONO_SECMAN_FLAG_SET_VALUE(x,y) do { x = ((y) ? 0x3 : 0x2); } while (0)
41 #define MONO_PUBLIC_KEY_TOKEN_LENGTH 17
43 #define MONO_PROCESSOR_ARCHITECTURE_NONE 0
44 #define MONO_PROCESSOR_ARCHITECTURE_MSIL 1
45 #define MONO_PROCESSOR_ARCHITECTURE_X86 2
46 #define MONO_PROCESSOR_ARCHITECTURE_IA64 3
47 #define MONO_PROCESSOR_ARCHITECTURE_AMD64 4
48 #define MONO_PROCESSOR_ARCHITECTURE_ARM 5
50 struct _MonoAssemblyName {
53 const char *hash_value;
54 const mono_byte* public_key;
55 // string of 16 hex chars + 1 NULL
56 mono_byte public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
60 uint16_t major, minor, build, revision, arch;
63 struct MonoTypeNameParse {
66 MonoAssemblyName assembly;
67 GList *modifiers; /* 0 -> byref, -1 -> pointer, > 0 -> array rank */
68 GPtrArray *type_arguments;
72 struct _MonoAssembly {
74 * The number of appdomains which have this assembly loaded plus the number of
75 * assemblies referencing this assembly through an entry in their image->references
76 * arrays. The later is needed because entries in the image->references array
77 * might point to assemblies which are only loaded in some appdomains, and without
78 * the additional reference, they can be freed at any time.
79 * The ref_count is initially 0.
81 int ref_count; /* use atomic operations only */
83 MonoAssemblyName aname;
85 GSList *friend_assembly_names; /* Computed by mono_assembly_load_friends () */
86 guint8 friend_assembly_names_inited;
89 guint8 corlib_internal;
91 guint8 wrap_non_exception_throws;
92 guint8 wrap_non_exception_throws_inited;
93 guint8 jit_optimizer_disabled;
94 guint8 jit_optimizer_disabled_inited;
95 /* security manager flags (one bit is for lazy initialization) */
96 guint32 ecma:2; /* Has the ECMA key */
97 guint32 aptc:2; /* Has the [AllowPartiallyTrustedCallers] attributes */
98 guint32 fulltrust:2; /* Has FullTrust permission */
99 guint32 unmanaged:2; /* Has SecurityPermissionFlag.UnmanagedCode permission */
100 guint32 skipverification:2; /* Has SecurityPermissionFlag.SkipVerification permission */
108 struct _MonoTableInfo {
114 * Tables contain up to 9 columns and the possible sizes of the
115 * fields in the documentation are 1, 2 and 4 bytes. So we
116 * can encode in 2 bits the size.
118 * A 32 bit value can encode the resulting size
120 * The top eight bits encode the number of columns in the table.
121 * we only need 4, but 8 is aligned no shift required.
123 guint32 size_bitfield;
126 #define REFERENCE_MISSING ((gpointer) -1)
128 typedef struct _MonoDllMap MonoDllMap;
132 * The number of assemblies which reference this MonoImage though their 'image'
133 * field plus the number of images which reference this MonoImage through their
134 * 'modules' field, plus the number of threads holding temporary references to
135 * this image between calls of mono_image_open () and mono_image_close ().
138 void *raw_data_handle;
140 guint32 raw_data_len;
141 guint8 raw_buffer_used : 1;
142 guint8 raw_data_allocated : 1;
143 guint8 fileio_used : 1;
146 /* Module was loaded using LoadLibrary. */
147 guint8 is_module_handle : 1;
149 /* Module entry point is _CorDllMain. */
150 guint8 has_entry_point : 1;
153 /* Whenever this is a dynamically emitted module */
156 /* Whenever this is a reflection only image */
159 /* Whenever this image contains uncompressed metadata */
160 guint8 uncompressed_metadata : 1;
162 /* Whenever this image contains metadata only without PE data */
163 guint8 metadata_only : 1;
165 guint8 checked_module_cctor : 1;
166 guint8 has_module_cctor : 1;
168 guint8 idx_string_wide : 1;
169 guint8 idx_guid_wide : 1;
170 guint8 idx_blob_wide : 1;
172 /* Whenever this image is considered as platform code for the CoreCLR security model */
173 guint8 core_clr_platform_code : 1;
176 const char *assembly_name;
177 const char *module_name;
179 gint16 md_version_major, md_version_minor;
182 MonoMemPool *mempool; /*protected by the image lock*/
186 MonoStreamHeader heap_strings;
187 MonoStreamHeader heap_us;
188 MonoStreamHeader heap_blob;
189 MonoStreamHeader heap_guid;
190 MonoStreamHeader heap_tables;
192 const char *tables_base;
195 MonoTableInfo tables [MONO_TABLE_NUM];
198 * references is initialized only by using the mono_assembly_open
199 * function, and not by using the lowlevel mono_image_open.
201 * It is NULL terminated.
203 MonoAssembly **references;
207 guint32 module_count;
208 gboolean *modules_loaded;
210 MonoImage **files; /*protected by the image lock*/
215 * The Assembly this image was loaded from.
217 MonoAssembly *assembly;
220 * Indexed by method tokens and typedef tokens.
222 GHashTable *method_cache; /*protected by the image lock*/
223 MonoInternalHashTable class_cache;
225 /* Indexed by memberref + methodspec tokens */
226 GHashTable *methodref_cache; /*protected by the image lock*/
229 * Indexed by fielddef and memberref tokens
231 MonoConcurrentHashTable *field_cache; /*protected by the image lock*/
233 /* indexed by typespec tokens. */
234 GHashTable *typespec_cache; /* protected by the image lock */
235 /* indexed by token */
236 GHashTable *memberref_signatures;
237 GHashTable *helper_signatures;
239 /* Indexed by blob heap indexes */
240 GHashTable *method_signatures;
243 * Indexes namespaces to hash tables that map class name to typedef token.
245 GHashTable *name_cache; /*protected by the image lock*/
248 * Indexed by MonoClass
250 GHashTable *array_cache;
251 GHashTable *ptr_cache;
253 GHashTable *szarray_cache;
254 /* This has a separate lock to improve scalability */
255 mono_mutex_t szarray_cache_lock;
258 * indexed by MonoMethodSignature
260 GHashTable *delegate_begin_invoke_cache;
261 GHashTable *delegate_end_invoke_cache;
262 GHashTable *delegate_invoke_cache;
263 GHashTable *runtime_invoke_cache;
264 GHashTable *runtime_invoke_vtype_cache;
267 * indexed by SignaturePointerPair
269 GHashTable *delegate_abstract_invoke_cache;
270 GHashTable *delegate_bound_static_invoke_cache;
271 GHashTable *native_func_wrapper_cache;
274 * indexed by MonoMethod pointers
276 GHashTable *runtime_invoke_direct_cache;
277 GHashTable *runtime_invoke_vcall_cache;
278 GHashTable *managed_wrapper_cache;
279 GHashTable *native_wrapper_cache;
280 GHashTable *native_wrapper_aot_cache;
281 GHashTable *native_wrapper_check_cache;
282 GHashTable *native_wrapper_aot_check_cache;
283 GHashTable *native_func_wrapper_aot_cache;
284 GHashTable *remoting_invoke_cache;
285 GHashTable *synchronized_cache;
286 GHashTable *unbox_wrapper_cache;
287 GHashTable *cominterop_invoke_cache;
288 GHashTable *cominterop_wrapper_cache; /* LOCKING: marshal lock */
289 GHashTable *thunk_invoke_cache;
290 GHashTable *wrapper_param_names;
291 GHashTable *synchronized_generic_cache;
292 GHashTable *array_accessor_cache;
295 * indexed by MonoClass pointers
297 GHashTable *ldfld_wrapper_cache;
298 GHashTable *ldflda_wrapper_cache;
299 GHashTable *stfld_wrapper_cache;
300 GHashTable *isinst_cache;
301 GHashTable *castclass_cache;
302 GHashTable *proxy_isinst_cache;
303 GHashTable *rgctx_template_hash; /* LOCKING: templates lock */
304 GHashTable *delegate_invoke_generic_cache;
305 GHashTable *delegate_begin_invoke_generic_cache;
306 GHashTable *delegate_end_invoke_generic_cache;
308 /* Contains rarely used fields of runtime structures belonging to this image */
309 MonoPropertyHash *property_hash;
311 void *reflection_info;
314 * user_info is a public field and is not touched by the
319 /* dll map entries */
322 /* interfaces IDs from this image */
323 /* protected by the classes lock */
324 MonoBitSet *interface_bitset;
326 /* when the image is being closed, this is abused as a list of
327 malloc'ed regions to be freed. */
328 GSList *reflection_info_unregister_classes;
330 /* List of image sets containing this image */
331 /* Protected by image_sets_lock */
334 /* Caches for MonoClass-es representing anon generic params */
335 MonoClass **var_cache_fast;
336 MonoClass **mvar_cache_fast;
337 GHashTable *var_cache_slow;
338 GHashTable *mvar_cache_slow;
339 GHashTable *var_cache_constrained;
340 GHashTable *mvar_cache_constrained;
342 /* Maps malloc-ed char* pinvoke scope -> MonoDl* */
343 GHashTable *pinvoke_scopes;
345 /* Maps malloc-ed char* pinvoke scope -> malloced-ed char* filename */
346 GHashTable *pinvoke_scope_filenames;
348 /* Indexed by MonoGenericParam pointers */
349 GHashTable **gshared_types;
350 /* The length of the above array */
351 int gshared_types_len;
354 * No other runtime locks must be taken while holding this lock.
355 * It's meant to be used only to mutate and query structures part of this image.
361 * Generic instances depend on many images, and they need to be deleted if one
362 * of the images they depend on is unloaded. For example,
363 * List<Foo> depends on both List's image and Foo's image.
364 * A MonoImageSet is the owner of all generic instances depending on the same set of
371 GHashTable *gclass_cache, *ginst_cache, *gmethod_cache, *gsignature_cache;
376 * Memory for generic instances owned by this image set should be allocated from
377 * this mempool, using the mono_image_set_alloc family of functions.
379 MonoMemPool *mempool;
392 guint32 alloc_size; /* malloced bytes */
394 guint32 offset; /* from start of metadata */
400 guint8 row_size; /* calculated later with column_sizes */
403 guint32 *values; /* rows * columns */
406 struct _MonoDynamicAssembly {
407 MonoAssembly assembly;
409 guint32 strong_name_size;
415 struct _MonoDynamicImage {
419 guint32 metadata_rva;
421 guint32 cli_header_offset;
425 guint32 imp_names_offset;
431 } sections [MONO_SECTION_MAX];
432 GHashTable *typespec;
434 GHashTable *handleref;
435 MonoGHashTable *handleref_managed;
436 MonoGHashTable *tokens;
437 GHashTable *blob_cache;
438 GHashTable *standalonesig_cache;
439 GList *array_methods;
440 GPtrArray *gen_params;
441 MonoGHashTable *token_fixups;
442 GHashTable *method_to_table_idx;
443 GHashTable *field_to_table_idx;
444 GHashTable *method_aux_hash;
445 GHashTable *vararg_aux_hash;
446 MonoGHashTable *generic_def_objects;
447 MonoGHashTable *methodspec;
449 * Maps final token values to the object they describe.
451 MonoGHashTable *remapped_tokens;
454 gboolean initial_image;
455 guint32 pe_kind, machine;
457 guint32 strong_name_size;
459 guint32 win32_res_size;
462 MonoDynamicStream sheap;
463 MonoDynamicStream code; /* used to store method headers and bytecode */
464 MonoDynamicStream resources; /* managed embedded resources */
465 MonoDynamicStream us;
466 MonoDynamicStream blob;
467 MonoDynamicStream tstream;
468 MonoDynamicStream guid;
469 MonoDynamicTable tables [MONO_TABLE_NUM];
470 MonoClass *wrappers_type; /*wrappers are bound to this type instead of <Module>*/
473 /* Contains information about assembly binding */
474 typedef struct _MonoAssemblyBindingInfo {
477 guchar public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
480 AssemblyVersionSet old_version_bottom;
481 AssemblyVersionSet old_version_top;
482 AssemblyVersionSet new_version;
483 guint has_old_version_bottom : 1;
484 guint has_old_version_top : 1;
485 guint has_new_version : 1;
487 gint32 domain_id; /*Needed to unload per-domain binding*/
488 } MonoAssemblyBindingInfo;
490 struct _MonoMethodHeader {
491 const unsigned char *code;
492 #ifdef MONO_SMALL_CONFIG
497 guint16 max_stack : 15;
498 unsigned int is_transient: 1; /* mono_metadata_free_mh () will actually free this header */
499 unsigned int num_clauses : 15;
500 /* if num_locals != 0, then the following apply: */
501 unsigned int init_locals : 1;
503 MonoExceptionClause *clauses;
504 MonoType *locals [MONO_ZERO_LEN_ARRAY];
509 gboolean has_clauses;
510 } MonoMethodHeaderSummary;
512 #define MONO_SIZEOF_METHOD_HEADER (sizeof (struct _MonoMethodHeader) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
514 struct _MonoMethodSignature {
516 #ifdef MONO_SMALL_CONFIG
519 unsigned int generic_param_count : 5;
523 unsigned int generic_param_count : 16;
525 unsigned int call_convention : 6;
526 unsigned int hasthis : 1;
527 unsigned int explicit_this : 1;
528 unsigned int pinvoke : 1;
529 unsigned int is_inflated : 1;
530 unsigned int has_type_parameters : 1;
531 MonoType *params [MONO_ZERO_LEN_ARRAY];
535 * AOT cache configuration loaded from config files.
536 * Doesn't really belong here.
540 * Enable aot caching for applications whose main assemblies are in
546 } MonoAotCacheConfig;
548 #define MONO_SIZEOF_METHOD_SIGNATURE (sizeof (struct _MonoMethodSignature) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
550 static inline gboolean
551 image_is_dynamic (MonoImage *image)
553 #ifdef DISABLE_REFLECTION_EMIT
556 return image->dynamic;
560 static inline gboolean
561 assembly_is_dynamic (MonoAssembly *assembly)
563 #ifdef DISABLE_REFLECTION_EMIT
566 return assembly->dynamic;
570 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
571 guint mono_aligned_addr_hash (gconstpointer ptr);
574 mono_image_check_for_module_cctor (MonoImage *image);
577 mono_image_alloc (MonoImage *image, guint size);
580 mono_image_alloc0 (MonoImage *image, guint size);
582 #define mono_image_new0(image,type,size) ((type *) mono_image_alloc0 (image, sizeof (type)* (size)))
585 mono_image_strdup (MonoImage *image, const char *s);
588 g_list_prepend_image (MonoImage *image, GList *list, gpointer data);
591 g_slist_append_image (MonoImage *image, GSList *list, gpointer data);
594 mono_image_lock (MonoImage *image);
597 mono_image_unlock (MonoImage *image);
600 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property);
603 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value);
606 mono_image_property_remove (MonoImage *image, gpointer subject);
609 mono_image_close_except_pools (MonoImage *image);
612 mono_image_close_finish (MonoImage *image);
614 typedef void (*MonoImageUnloadFunc) (MonoImage *image, gpointer user_data);
617 mono_install_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data);
620 mono_remove_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data);
623 mono_image_append_class_to_reflection_info_set (MonoClass *klass);
626 mono_image_set_alloc (MonoImageSet *set, guint size);
629 mono_image_set_alloc0 (MonoImageSet *set, guint size);
632 mono_image_set_strdup (MonoImageSet *set, const char *s);
634 #define mono_image_set_new0(image,type,size) ((type *) mono_image_set_alloc0 (image, sizeof (type)* (size)))
637 mono_metadata_get_shared_type (MonoType *type);
640 mono_metadata_clean_for_image (MonoImage *image);
643 mono_metadata_clean_generic_classes_for_image (MonoImage *image);
646 mono_metadata_cleanup (void);
648 const char * mono_meta_table_name (int table);
649 void mono_metadata_compute_table_bases (MonoImage *meta);
652 mono_metadata_interfaces_from_typedef_full (MonoImage *image,
654 MonoClass ***interfaces,
656 gboolean heap_alloc_result,
657 MonoGenericContext *context,
661 mono_metadata_parse_array_full (MonoImage *image,
662 MonoGenericContainer *container,
667 mono_metadata_parse_type_full (MonoImage *image,
668 MonoGenericContainer *container,
669 MonoParseTypeMode mode,
674 MONO_API MonoMethodSignature *
675 mono_metadata_parse_method_signature_full (MonoImage *image,
676 MonoGenericContainer *generic_container,
682 MONO_API MonoMethodHeader *
683 mono_metadata_parse_mh_full (MonoImage *image,
684 MonoGenericContainer *container,
688 mono_method_get_header_summary (MonoMethod *method, MonoMethodHeaderSummary *summary);
690 int* mono_metadata_get_param_attrs (MonoImage *m, int def, int param_count);
691 gboolean mono_metadata_method_has_param_attrs (MonoImage *m, int def);
694 mono_metadata_generic_context_hash (const MonoGenericContext *context);
697 mono_metadata_generic_context_equal (const MonoGenericContext *g1,
698 const MonoGenericContext *g2);
701 mono_metadata_parse_generic_inst (MonoImage *image,
702 MonoGenericContainer *container,
708 mono_metadata_get_generic_inst (int type_argc,
709 MonoType **type_argv);
712 mono_metadata_lookup_generic_class (MonoClass *gclass,
713 MonoGenericInst *inst,
714 gboolean is_dynamic);
716 MonoGenericInst * mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error);
719 mono_metadata_generic_param_hash (MonoGenericParam *p);
722 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2);
724 void mono_dynamic_stream_reset (MonoDynamicStream* stream);
725 void mono_assembly_addref (MonoAssembly *assembly);
726 void mono_assembly_load_friends (MonoAssembly* ass);
727 gboolean mono_assembly_has_skip_verification (MonoAssembly* ass);
729 void mono_assembly_release_gc_roots (MonoAssembly *assembly);
730 gboolean mono_assembly_close_except_image_pools (MonoAssembly *assembly);
731 void mono_assembly_close_finish (MonoAssembly *assembly);
734 gboolean mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2);
736 void mono_config_parse_publisher_policy (const char *filename, MonoAssemblyBindingInfo *binding_info);
737 void mono_config_parse_assembly_bindings (const char *filename, int major, int minor, void *user_data,
738 void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data));
741 mono_assembly_name_parse_full (const char *name,
742 MonoAssemblyName *aname,
743 gboolean save_public_key,
744 gboolean *is_version_defined,
745 gboolean *is_token_defined);
747 MONO_API guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
749 void mono_unload_interface_ids (MonoBitSet *bitset);
752 MonoType *mono_metadata_type_dup (MonoImage *image, const MonoType *original);
753 MonoMethodSignature *mono_metadata_signature_dup_full (MonoImage *image,MonoMethodSignature *sig);
754 MonoMethodSignature *mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig);
757 mono_get_shared_generic_inst (MonoGenericContainer *container);
760 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open);
762 MONO_API void mono_type_get_desc (GString *res, MonoType *type, mono_bool include_namespace);
765 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only);
768 mono_metadata_parse_marshal_spec_full (MonoImage *image, MonoImage *parent_image, const char *ptr);
770 guint mono_metadata_generic_inst_hash (gconstpointer data);
771 gboolean mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb);
774 mono_metadata_field_info_with_mempool (
779 MonoMarshalSpec **marshal_spec);
782 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field);
785 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event);
788 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property);
791 mono_metadata_signature_size (MonoMethodSignature *sig);
793 guint mono_metadata_str_hash (gconstpointer v1);
795 gboolean mono_image_load_pe_data (MonoImage *image);
797 gboolean mono_image_load_cli_data (MonoImage *image);
799 void mono_image_load_names (MonoImage *image);
801 MonoImage *mono_image_open_raw (const char *fname, MonoImageOpenStatus *status);
803 MonoImage *mono_image_open_metadata_only (const char *fname, MonoImageOpenStatus *status);
805 MonoException *mono_get_exception_field_access_msg (const char *msg);
807 MonoException *mono_get_exception_method_access_msg (const char *msg);
809 MonoMethod* method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context, MonoError *error);
811 MonoMethod *mono_get_method_constrained_with_method (MonoImage *image, MonoMethod *method, MonoClass *constrained_class, MonoGenericContext *context, MonoError *error);
812 MonoMethod *mono_get_method_constrained_checked (MonoImage *image, guint32 token, MonoClass *constrained_class, MonoGenericContext *context, MonoMethod **cil_method, MonoError *error);
814 void mono_type_set_alignment (MonoTypeEnum type, int align);
816 MonoAotCacheConfig *mono_get_aot_cache_config (void);
818 mono_type_create_from_typespec_checked (MonoImage *image, guint32 type_spec, MonoError *error);
821 mono_method_get_signature_checked (MonoMethod *method, MonoImage *image, guint32 token, MonoGenericContext *context, MonoError *error);
824 mono_get_method_checked (MonoImage *image, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error);
827 mono_metadata_localscope_from_methoddef (MonoImage *meta, guint32 index);
829 #endif /* __MONO_METADATA_INTERNALS_H__ */