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/utils/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>
17 #define MONO_SECMAN_FLAG_INIT(x) (x & 0x2)
18 #define MONO_SECMAN_FLAG_GET_VALUE(x) (x & 0x1)
19 #define MONO_SECMAN_FLAG_SET_VALUE(x,y) do { x = ((y) ? 0x3 : 0x2); } while (0)
21 #define MONO_PUBLIC_KEY_TOKEN_LENGTH 17
23 struct _MonoAssemblyName {
26 const char *hash_value;
27 const mono_byte* public_key;
28 // string of 16 hex chars + 1 NULL
29 mono_byte public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
33 uint16_t major, minor, build, revision;
36 struct MonoTypeNameParse {
39 MonoAssemblyName assembly;
40 GList *modifiers; /* 0 -> byref, -1 -> pointer, > 0 -> array rank */
41 GPtrArray *type_arguments;
45 struct _MonoAssembly {
47 * The number of appdomains which have this assembly loaded plus the number of
48 * assemblies referencing this assembly through an entry in their image->references
49 * arrays. The later is needed because entries in the image->references array
50 * might point to assemblies which are only loaded in some appdomains, and without
51 * the additional reference, they can be freed at any time.
52 * The ref_count is initially 0.
54 int ref_count; /* use atomic operations only */
56 MonoAssemblyName aname;
58 GSList *friend_assembly_names; /* Computed by mono_assembly_load_friends () */
59 guint8 friend_assembly_names_inited;
62 guint8 corlib_internal;
64 /* security manager flags (one bit is for lazy initialization) */
65 guint32 ecma:2; /* Has the ECMA key */
66 guint32 aptc:2; /* Has the [AllowPartiallyTrustedCallers] attributes */
67 guint32 fulltrust:2; /* Has FullTrust permission */
68 guint32 unmanaged:2; /* Has SecurityPermissionFlag.UnmanagedCode permission */
69 guint32 skipverification:2; /* Has SecurityPermissionFlag.SkipVerification permission */
77 struct _MonoTableInfo {
83 * Tables contain up to 9 columns and the possible sizes of the
84 * fields in the documentation are 1, 2 and 4 bytes. So we
85 * can encode in 2 bits the size.
87 * A 32 bit value can encode the resulting size
89 * The top eight bits encode the number of columns in the table.
90 * we only need 4, but 8 is aligned no shift required.
92 guint32 size_bitfield;
95 #define REFERENCE_MISSING ((gpointer) -1)
97 typedef struct _MonoDllMap MonoDllMap;
101 * The number of assemblies which reference this MonoImage though their 'image'
102 * field plus the number of images which reference this MonoImage through their
103 * 'modules' field, plus the number of threads holding temporary references to
104 * this image between calls of mono_image_open () and mono_image_close ().
107 void *raw_data_handle;
109 guint32 raw_data_len;
110 guint8 raw_buffer_used : 1;
111 guint8 raw_data_allocated : 1;
114 /* Module was loaded using LoadLibrary. */
115 guint8 is_module_handle : 1;
117 /* Module entry point is _CorDllMain. */
118 guint8 has_entry_point : 1;
121 /* Whenever this is a dynamically emitted module */
124 /* Whenever this is a reflection only image */
127 /* Whenever this image contains uncompressed metadata */
128 guint8 uncompressed_metadata : 1;
130 guint8 checked_module_cctor : 1;
131 guint8 has_module_cctor : 1;
133 guint8 idx_string_wide : 1;
134 guint8 idx_guid_wide : 1;
135 guint8 idx_blob_wide : 1;
137 /* Whenever this image is considered as platform code for the CoreCLR security model */
138 guint8 core_clr_platform_code : 1;
141 const char *assembly_name;
142 const char *module_name;
144 gint16 md_version_major, md_version_minor;
147 MonoMemPool *mempool; /*protected by the image lock*/
151 MonoStreamHeader heap_strings;
152 MonoStreamHeader heap_us;
153 MonoStreamHeader heap_blob;
154 MonoStreamHeader heap_guid;
155 MonoStreamHeader heap_tables;
157 const char *tables_base;
160 MonoTableInfo tables [MONO_TABLE_NUM];
163 * references is initialized only by using the mono_assembly_open
164 * function, and not by using the lowlevel mono_image_open.
166 * It is NULL terminated.
168 MonoAssembly **references;
171 guint32 module_count;
172 gboolean *modules_loaded;
179 * The Assembly this image was loaded from.
181 MonoAssembly *assembly;
184 * Indexed by method tokens and typedef tokens.
186 GHashTable *method_cache; /*protected by the image lock*/
187 MonoInternalHashTable class_cache;
189 /* Indexed by memberref + methodspec tokens */
190 GHashTable *methodref_cache; /*protected by the image lock*/
193 * Indexed by fielddef and memberref tokens
195 GHashTable *field_cache;
197 /* indexed by typespec tokens. */
198 GHashTable *typespec_cache;
199 /* indexed by token */
200 GHashTable *memberref_signatures;
201 GHashTable *helper_signatures;
203 /* Indexed by blob heap indexes */
204 GHashTable *method_signatures;
207 * Indexes namespaces to hash tables that map class name to typedef token.
209 GHashTable *name_cache; /*protected by the image lock*/
212 * Indexed by MonoClass
214 GHashTable *array_cache;
215 GHashTable *ptr_cache;
217 GHashTable *szarray_cache;
218 /* This has a separate lock to improve scalability */
219 CRITICAL_SECTION szarray_cache_lock;
222 * indexed by MonoMethodSignature
224 GHashTable *delegate_begin_invoke_cache;
225 GHashTable *delegate_end_invoke_cache;
226 GHashTable *delegate_invoke_cache;
227 GHashTable *runtime_invoke_cache;
230 * indexed by SignatureMethodPair
232 GHashTable *delegate_abstract_invoke_cache;
235 * indexed by MonoMethod pointers
237 GHashTable *runtime_invoke_direct_cache;
238 GHashTable *runtime_invoke_vcall_cache;
239 GHashTable *managed_wrapper_cache;
240 GHashTable *native_wrapper_cache;
241 GHashTable *native_wrapper_aot_cache;
242 GHashTable *remoting_invoke_cache;
243 GHashTable *synchronized_cache;
244 GHashTable *unbox_wrapper_cache;
245 GHashTable *cominterop_invoke_cache;
246 GHashTable *cominterop_wrapper_cache; /* LOCKING: marshal lock */
247 GHashTable *thunk_invoke_cache;
250 * indexed by MonoClass pointers
252 GHashTable *ldfld_wrapper_cache;
253 GHashTable *ldflda_wrapper_cache;
254 GHashTable *stfld_wrapper_cache;
255 GHashTable *isinst_cache;
256 GHashTable *castclass_cache;
257 GHashTable *proxy_isinst_cache;
258 GHashTable *rgctx_template_hash; /* LOCKING: templates lock */
260 /* Contains rarely used fields of runtime structures belonging to this image */
261 MonoPropertyHash *property_hash;
263 void *reflection_info;
266 * user_info is a public field and is not touched by the
271 /* dll map entries */
274 /* interfaces IDs from this image */
275 MonoBitSet *interface_bitset;
277 /* when the image is being closed, this is abused as a list of
278 malloc'ed regions to be freed. */
279 GSList *reflection_info_unregister_classes;
281 /* List of image sets containing this image */
285 * No other runtime locks must be taken while holding this lock.
286 * It's meant to be used only to mutate and query structures part of this image.
288 CRITICAL_SECTION lock;
292 * Generic instances depend on many images, and they need to be deleted if one
293 * of the images they depend on is unloaded. For example,
294 * List<Foo> depends on both List's image and Foo's image.
295 * A MonoImageSet is the owner of all generic instances depending on the same set of
302 GHashTable *gclass_cache, *ginst_cache, *gmethod_cache, *gsignature_cache;
304 CRITICAL_SECTION lock;
307 * Memory for generic instances owned by this image set should be allocated from
308 * this mempool, using the mono_image_set_alloc family of functions.
310 MonoMemPool *mempool;
323 guint32 alloc_size; /* malloced bytes */
325 guint32 offset; /* from start of metadata */
331 guint8 row_size; /* calculated later with column_sizes */
334 guint32 *values; /* rows * columns */
337 struct _MonoDynamicAssembly {
338 MonoAssembly assembly;
340 guint32 strong_name_size;
346 struct _MonoDynamicImage {
350 guint32 metadata_rva;
352 guint32 cli_header_offset;
356 guint32 imp_names_offset;
362 } sections [MONO_SECTION_MAX];
363 GHashTable *typespec;
365 GHashTable *handleref;
366 MonoGHashTable *handleref_managed;
367 MonoGHashTable *tokens;
368 GHashTable *blob_cache;
369 GHashTable *standalonesig_cache;
370 GList *array_methods;
371 GPtrArray *gen_params;
372 MonoGHashTable *token_fixups;
373 GHashTable *method_to_table_idx;
374 GHashTable *field_to_table_idx;
375 GHashTable *method_aux_hash;
376 MonoGHashTable *generic_def_objects;
377 MonoGHashTable *methodspec;
380 gboolean initial_image;
381 guint32 pe_kind, machine;
383 guint32 strong_name_size;
385 guint32 win32_res_size;
388 MonoDynamicStream sheap;
389 MonoDynamicStream code; /* used to store method headers and bytecode */
390 MonoDynamicStream resources; /* managed embedded resources */
391 MonoDynamicStream us;
392 MonoDynamicStream blob;
393 MonoDynamicStream tstream;
394 MonoDynamicStream guid;
395 MonoDynamicTable tables [MONO_TABLE_NUM];
396 MonoClass *wrappers_type; /*wrappers are bound to this type instead of <Module>*/
399 /* Contains information about assembly binding */
400 typedef struct _MonoAssemblyBindingInfo {
403 guchar public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
406 AssemblyVersionSet old_version_bottom;
407 AssemblyVersionSet old_version_top;
408 AssemblyVersionSet new_version;
409 guint has_old_version_bottom : 1;
410 guint has_old_version_top : 1;
411 guint has_new_version : 1;
413 } MonoAssemblyBindingInfo;
415 struct _MonoMethodHeader {
417 const unsigned char *code;
419 unsigned int num_clauses : 15;
420 /* if num_locals != 0, then the following apply: */
421 unsigned int init_locals : 1;
423 MonoExceptionClause *clauses;
424 MonoType *locals [MONO_ZERO_LEN_ARRAY];
429 gboolean has_clauses;
430 } MonoMethodHeaderSummary;
432 #define MONO_SIZEOF_METHOD_HEADER (sizeof (struct _MonoMethodHeader) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
434 struct _MonoMethodSignature {
435 unsigned int hasthis : 1;
436 unsigned int explicit_this : 1;
437 unsigned int call_convention : 6;
438 unsigned int pinvoke : 1;
439 unsigned int ref_count : 23;
442 unsigned int generic_param_count : 30;
443 unsigned int is_inflated : 1;
444 unsigned int has_type_parameters : 1;
446 MonoType *params [MONO_ZERO_LEN_ARRAY];
449 #define MONO_SIZEOF_METHOD_SIGNATURE (sizeof (struct _MonoMethodSignature) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
451 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
452 guint mono_aligned_addr_hash (gconstpointer ptr) MONO_INTERNAL;
455 mono_image_check_for_module_cctor (MonoImage *image) MONO_INTERNAL;
458 mono_image_alloc (MonoImage *image, guint size) MONO_INTERNAL;
461 mono_image_alloc0 (MonoImage *image, guint size) MONO_INTERNAL;
464 mono_image_strdup (MonoImage *image, const char *s) MONO_INTERNAL;
467 g_list_prepend_image (MonoImage *image, GList *list, gpointer data) MONO_INTERNAL;
470 g_slist_append_image (MonoImage *image, GSList *list, gpointer data) MONO_INTERNAL;
473 mono_image_lock (MonoImage *image) MONO_INTERNAL;
476 mono_image_unlock (MonoImage *image) MONO_INTERNAL;
479 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property) MONO_INTERNAL;
482 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value) MONO_INTERNAL;
485 mono_image_property_remove (MonoImage *image, gpointer subject) MONO_INTERNAL;
488 mono_image_close_except_pools (MonoImage *image) MONO_INTERNAL;
491 mono_image_close_finish (MonoImage *image) MONO_INTERNAL;
495 mono_metadata_get_shared_type (MonoType *type) MONO_INTERNAL;
498 mono_metadata_clean_for_image (MonoImage *image) MONO_INTERNAL;
501 mono_metadata_clean_generic_classes_for_image (MonoImage *image) MONO_INTERNAL;
504 mono_metadata_cleanup (void);
506 const char * mono_meta_table_name (int table) MONO_INTERNAL;
507 void mono_metadata_compute_table_bases (MonoImage *meta) MONO_INTERNAL;
510 mono_metadata_interfaces_from_typedef_full (MonoImage *image,
512 MonoClass ***interfaces,
514 gboolean heap_alloc_result,
515 MonoGenericContext *context) MONO_INTERNAL;
518 mono_metadata_parse_array_full (MonoImage *image,
519 MonoGenericContainer *container,
521 const char **rptr) MONO_INTERNAL;
524 mono_metadata_parse_type_full (MonoImage *image,
525 MonoGenericContainer *container,
526 MonoParseTypeMode mode,
531 MonoMethodSignature *
532 mono_metadata_parse_signature_full (MonoImage *image,
533 MonoGenericContainer *generic_container,
534 guint32 token) MONO_INTERNAL;
536 MonoMethodSignature *
537 mono_metadata_parse_method_signature_full (MonoImage *image,
538 MonoGenericContainer *generic_container,
544 mono_metadata_parse_mh_full (MonoImage *image,
545 MonoGenericContainer *container,
549 mono_method_get_header_summary (MonoMethod *method, MonoMethodHeaderSummary *summary) MONO_INTERNAL;
551 int* mono_metadata_get_param_attrs (MonoImage *m, int def, int param_count) MONO_INTERNAL;
552 gboolean mono_metadata_method_has_param_attrs (MonoImage *m, int def) MONO_INTERNAL;
555 mono_metadata_generic_context_hash (const MonoGenericContext *context) MONO_INTERNAL;
558 mono_metadata_generic_context_equal (const MonoGenericContext *g1,
559 const MonoGenericContext *g2) MONO_INTERNAL;
562 mono_metadata_parse_generic_inst (MonoImage *image,
563 MonoGenericContainer *container,
566 const char **rptr) MONO_INTERNAL;
569 mono_metadata_get_generic_inst (int type_argc,
570 MonoType **type_argv) MONO_INTERNAL;
573 mono_metadata_lookup_generic_class (MonoClass *gclass,
574 MonoGenericInst *inst,
575 gboolean is_dynamic) MONO_INTERNAL;
577 MonoGenericInst * mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error) MONO_INTERNAL;
579 void mono_dynamic_stream_reset (MonoDynamicStream* stream) MONO_INTERNAL;
580 void mono_assembly_addref (MonoAssembly *assembly) MONO_INTERNAL;
581 void mono_assembly_load_friends (MonoAssembly* ass) MONO_INTERNAL;
582 gboolean mono_assembly_has_skip_verification (MonoAssembly* ass) MONO_INTERNAL;
584 gboolean mono_assembly_close_except_image_pools (MonoAssembly *assembly) MONO_INTERNAL;
585 void mono_assembly_close_finish (MonoAssembly *assembly) MONO_INTERNAL;
588 gboolean mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2) MONO_INTERNAL;
590 void mono_config_parse_publisher_policy (const char *filename, MonoAssemblyBindingInfo *binding_info) MONO_INTERNAL;
593 mono_assembly_name_parse_full (const char *name,
594 MonoAssemblyName *aname,
595 gboolean save_public_key,
596 gboolean *is_version_defined,
597 gboolean *is_token_defined) MONO_INTERNAL;
599 guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
601 void mono_unload_interface_ids (MonoBitSet *bitset) MONO_INTERNAL;
604 MonoType *mono_metadata_type_dup (MonoImage *image, const MonoType *original) MONO_INTERNAL;
605 MonoMethodSignature *mono_metadata_signature_dup_full (MonoImage *image,MonoMethodSignature *sig) MONO_INTERNAL;
606 MonoMethodSignature *mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig) MONO_INTERNAL;
609 mono_get_shared_generic_inst (MonoGenericContainer *container) MONO_INTERNAL;
612 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open) MONO_INTERNAL;
614 void mono_type_get_desc (GString *res, MonoType *type, mono_bool include_namespace);
617 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only) MONO_INTERNAL;
620 mono_metadata_parse_marshal_spec_full (MonoImage *image, const char *ptr) MONO_INTERNAL;
622 guint mono_metadata_generic_inst_hash (gconstpointer data) MONO_INTERNAL;
623 gboolean mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb) MONO_INTERNAL;
626 mono_metadata_field_info_with_mempool (
631 MonoMarshalSpec **marshal_spec);
634 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field) MONO_INTERNAL;
637 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event) MONO_INTERNAL;
640 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property) MONO_INTERNAL;
643 mono_metadata_signature_size (MonoMethodSignature *sig) MONO_INTERNAL;
645 guint mono_metadata_str_hash (gconstpointer v1) MONO_INTERNAL;
647 gboolean mono_image_load_pe_data (MonoImage *image) MONO_INTERNAL;
649 gboolean mono_image_load_cli_data (MonoImage *image) MONO_INTERNAL;
651 void mono_image_load_names (MonoImage *image) MONO_INTERNAL;
653 MonoImage *mono_image_open_raw (const char *fname, MonoImageOpenStatus *status) MONO_INTERNAL;
655 #endif /* __MONO_METADATA_INTERNALS_H__ */