2010-01-15 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / metadata-internals.h
1
2 #ifndef __MONO_METADATA_INTERNALS_H__
3 #define __MONO_METADATA_INTERNALS_H__
4
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
16 #define MONO_SECMAN_FLAG_INIT(x)                (x & 0x2)
17 #define MONO_SECMAN_FLAG_GET_VALUE(x)           (x & 0x1)
18 #define MONO_SECMAN_FLAG_SET_VALUE(x,y)         do { x = ((y) ? 0x3 : 0x2); } while (0)
19
20 struct _MonoAssembly {
21         /* 
22          * The number of appdomains which have this assembly loaded plus the number of 
23          * assemblies referencing this assembly through an entry in their image->references
24          * arrays. The later is needed because entries in the image->references array
25          * might point to assemblies which are only loaded in some appdomains, and without
26          * the additional reference, they can be freed at any time.
27          * The ref_count is initially 0.
28          */
29         int ref_count; /* use atomic operations only */
30         char *basedir;
31         MonoAssemblyName aname;
32         MonoImage *image;
33         GSList *friend_assembly_names; /* Computed by mono_assembly_load_friends () */
34         guint8 friend_assembly_names_inited;
35         guint8 in_gac;
36         guint8 dynamic;
37         guint8 corlib_internal;
38         gboolean ref_only;
39         /* security manager flags (one bit is for lazy initialization) */
40         guint32 ecma:2;         /* Has the ECMA key */
41         guint32 aptc:2;         /* Has the [AllowPartiallyTrustedCallers] attributes */
42         guint32 fulltrust:2;    /* Has FullTrust permission */
43         guint32 unmanaged:2;    /* Has SecurityPermissionFlag.UnmanagedCode permission */
44         guint32 skipverification:2;     /* Has SecurityPermissionFlag.SkipVerification permission */
45 };
46
47 typedef struct {
48         const char* data;
49         guint32  size;
50 } MonoStreamHeader;
51
52 struct _MonoTableInfo {
53         const char *base;
54         guint       rows     : 24;
55         guint       row_size : 8;
56
57         /*
58          * Tables contain up to 9 columns and the possible sizes of the
59          * fields in the documentation are 1, 2 and 4 bytes.  So we
60          * can encode in 2 bits the size.
61          *
62          * A 32 bit value can encode the resulting size
63          *
64          * The top eight bits encode the number of columns in the table.
65          * we only need 4, but 8 is aligned no shift required. 
66          */
67         guint32   size_bitfield;
68 };
69
70 #define REFERENCE_MISSING ((gpointer) -1)
71
72 typedef struct _MonoDllMap MonoDllMap;
73
74 struct _MonoImage {
75         /*
76          * The number of assemblies which reference this MonoImage though their 'image'
77          * field plus the number of images which reference this MonoImage through their 
78          * 'modules' field, plus the number of threads holding temporary references to
79          * this image between calls of mono_image_open () and mono_image_close ().
80          */
81         int   ref_count;
82         void *raw_data_handle;
83         char *raw_data;
84         guint32 raw_data_len;
85         guint8 raw_buffer_used    : 1;
86         guint8 raw_data_allocated : 1;
87
88 #ifdef HOST_WIN32
89         /* Module was loaded using LoadLibrary. */
90         guint8 is_module_handle : 1;
91
92         /* Module entry point is _CorDllMain. */
93         guint8 has_entry_point : 1;
94 #endif
95
96         /* Whenever this is a dynamically emitted module */
97         guint8 dynamic : 1;
98
99         /* Whenever this is a reflection only image */
100         guint8 ref_only : 1;
101
102         /* Whenever this image contains uncompressed metadata */
103         guint8 uncompressed_metadata : 1;
104
105         guint8 checked_module_cctor : 1;
106         guint8 has_module_cctor : 1;
107
108         guint8 idx_string_wide : 1;
109         guint8 idx_guid_wide : 1;
110         guint8 idx_blob_wide : 1;
111
112         /* Whenever this image is considered as platform code for the CoreCLR security model */
113         guint8 core_clr_platform_code : 1;
114                             
115         char *name;
116         const char *assembly_name;
117         const char *module_name;
118         char *version;
119         gint16 md_version_major, md_version_minor;
120         char *guid;
121         void *image_info;
122         MonoMemPool         *mempool; /*protected by the image lock*/
123
124         char                *raw_metadata;
125                             
126         MonoStreamHeader     heap_strings;
127         MonoStreamHeader     heap_us;
128         MonoStreamHeader     heap_blob;
129         MonoStreamHeader     heap_guid;
130         MonoStreamHeader     heap_tables;
131                             
132         const char          *tables_base;
133
134         /**/
135         MonoTableInfo        tables [MONO_TABLE_NUM];
136
137         /*
138          * references is initialized only by using the mono_assembly_open
139          * function, and not by using the lowlevel mono_image_open.
140          *
141          * It is NULL terminated.
142          */
143         MonoAssembly **references;
144
145         MonoImage **modules;
146         guint32 module_count;
147         gboolean *modules_loaded;
148
149         MonoImage **files;
150
151         gpointer aot_module;
152
153         /*
154          * The Assembly this image was loaded from.
155          */
156         MonoAssembly *assembly;
157
158         /*
159          * Indexed by method tokens and typedef tokens.
160          */
161         GHashTable *method_cache; /*protected by the image lock*/
162         MonoInternalHashTable class_cache;
163
164         /* Indexed by memberref + methodspec tokens */
165         GHashTable *methodref_cache; /*protected by the image lock*/
166
167         /*
168          * Indexed by fielddef and memberref tokens
169          */
170         GHashTable *field_cache;
171
172         /* indexed by typespec tokens. */
173         GHashTable *typespec_cache;
174         /* indexed by token */
175         GHashTable *memberref_signatures;
176         GHashTable *helper_signatures;
177
178         /* Indexed by blob heap indexes */
179         GHashTable *method_signatures;
180
181         /*
182          * Indexes namespaces to hash tables that map class name to typedef token.
183          */
184         GHashTable *name_cache;  /*protected by the image lock*/
185
186         /*
187          * Indexed by MonoClass
188          */
189         GHashTable *array_cache;
190         GHashTable *ptr_cache;
191
192         GHashTable *szarray_cache;
193         /* This has a separate lock to improve scalability */
194         CRITICAL_SECTION szarray_cache_lock;
195
196         /*
197          * indexed by MonoMethodSignature 
198          */
199         GHashTable *delegate_begin_invoke_cache;
200         GHashTable *delegate_end_invoke_cache;
201         GHashTable *delegate_invoke_cache;
202         GHashTable *runtime_invoke_cache;
203
204         /*
205          * indexed by SignatureMethodPair
206          */
207         GHashTable *delegate_abstract_invoke_cache;
208
209         /*
210          * indexed by MonoMethod pointers 
211          */
212         GHashTable *runtime_invoke_direct_cache;
213         GHashTable *runtime_invoke_vcall_cache;
214         GHashTable *managed_wrapper_cache;
215         GHashTable *native_wrapper_cache;
216         GHashTable *native_wrapper_aot_cache;
217         GHashTable *remoting_invoke_cache;
218         GHashTable *synchronized_cache;
219         GHashTable *unbox_wrapper_cache;
220         GHashTable *cominterop_invoke_cache;
221         GHashTable *cominterop_wrapper_cache; /* LOCKING: marshal lock */
222         GHashTable *thunk_invoke_cache;
223
224         /*
225          * indexed by MonoClass pointers
226          */
227         GHashTable *ldfld_wrapper_cache;
228         GHashTable *ldflda_wrapper_cache;
229         GHashTable *stfld_wrapper_cache;
230         GHashTable *isinst_cache;
231         GHashTable *castclass_cache;
232         GHashTable *proxy_isinst_cache;
233         GHashTable *rgctx_template_hash; /* LOCKING: templates lock */
234
235         /* Contains rarely used fields of runtime structures belonging to this image */
236         MonoPropertyHash *property_hash;
237
238         void *reflection_info;
239
240         /*
241          * user_info is a public field and is not touched by the
242          * metadata engine
243          */
244         void *user_info;
245
246         /* dll map entries */
247         MonoDllMap *dll_map;
248
249         /* interfaces IDs from this image */
250         MonoBitSet *interface_bitset;
251
252         /* when the image is being closed, this is abused as a list of
253            malloc'ed regions to be freed. */
254         GSList *reflection_info_unregister_classes;
255
256         /* List of image sets containing this image */
257         GSList *image_sets;
258
259         /*
260          * No other runtime locks must be taken while holding this lock.
261          * It's meant to be used only to mutate and query structures part of this image.
262          */
263         CRITICAL_SECTION    lock;
264 };
265
266 /*
267  * Generic instances depend on many images, and they need to be deleted if one
268  * of the images they depend on is unloaded. For example,
269  * List<Foo> depends on both List's image and Foo's image.
270  * A MonoImageSet is the owner of all generic instances depending on the same set of
271  * images.
272  */
273 typedef struct {
274         int nimages;
275         MonoImage **images;
276
277         GHashTable *gclass_cache, *ginst_cache, *gmethod_cache, *gsignature_cache;
278
279         CRITICAL_SECTION    lock;
280
281         /*
282          * Memory for generic instances owned by this image set should be allocated from
283          * this mempool, using the mono_image_set_alloc family of functions.
284          */
285         MonoMemPool         *mempool;
286 } MonoImageSet;
287
288 enum {
289         MONO_SECTION_TEXT,
290         MONO_SECTION_RSRC,
291         MONO_SECTION_RELOC,
292         MONO_SECTION_MAX
293 };
294
295 typedef struct {
296         GHashTable *hash;
297         char *data;
298         guint32 alloc_size; /* malloced bytes */
299         guint32 index;
300         guint32 offset; /* from start of metadata */
301 } MonoDynamicStream;
302
303 typedef struct {
304         guint32 alloc_rows;
305         guint32 rows;
306         guint8  row_size; /*  calculated later with column_sizes */
307         guint8  columns;
308         guint32 next_idx;
309         guint32 *values; /* rows * columns */
310 } MonoDynamicTable;
311
312 struct _MonoDynamicAssembly {
313         MonoAssembly assembly;
314         char *strong_name;
315         guint32 strong_name_size;
316         guint8 run;
317         guint8 save;
318         MonoDomain *domain;
319 };
320
321 struct _MonoDynamicImage {
322         MonoImage image;
323         guint32 meta_size;
324         guint32 text_rva;
325         guint32 metadata_rva;
326         guint32 image_base;
327         guint32 cli_header_offset;
328         guint32 iat_offset;
329         guint32 idt_offset;
330         guint32 ilt_offset;
331         guint32 imp_names_offset;
332         struct {
333                 guint32 rva;
334                 guint32 size;
335                 guint32 offset;
336                 guint32 attrs;
337         } sections [MONO_SECTION_MAX];
338         GHashTable *typespec;
339         GHashTable *typeref;
340         GHashTable *handleref;
341         MonoGHashTable *handleref_managed;
342         MonoGHashTable *tokens;
343         GHashTable *blob_cache;
344         GHashTable *standalonesig_cache;
345         GList *array_methods;
346         GPtrArray *gen_params;
347         MonoGHashTable *token_fixups;
348         GHashTable *method_to_table_idx;
349         GHashTable *field_to_table_idx;
350         GHashTable *method_aux_hash;
351         MonoGHashTable *generic_def_objects;
352         MonoGHashTable *methodspec;
353         gboolean run;
354         gboolean save;
355         gboolean initial_image;
356         guint32 pe_kind, machine;
357         char *strong_name;
358         guint32 strong_name_size;
359         char *win32_res;
360         guint32 win32_res_size;
361         guint8 *public_key;
362         int public_key_len;
363         MonoDynamicStream sheap;
364         MonoDynamicStream code; /* used to store method headers and bytecode */
365         MonoDynamicStream resources; /* managed embedded resources */
366         MonoDynamicStream us;
367         MonoDynamicStream blob;
368         MonoDynamicStream tstream;
369         MonoDynamicStream guid;
370         MonoDynamicTable tables [MONO_TABLE_NUM];
371         MonoClass *wrappers_type; /*wrappers are bound to this type instead of <Module>*/
372 };
373
374 /* Contains information about assembly binding */
375 typedef struct _MonoAssemblyBindingInfo {
376         char *name;
377         char *culture;
378         guchar public_key_token [MONO_PUBLIC_KEY_TOKEN_LENGTH];
379         int major;
380         int minor;
381         AssemblyVersionSet old_version_bottom;
382         AssemblyVersionSet old_version_top;
383         AssemblyVersionSet new_version;
384         guint has_old_version_bottom : 1;
385         guint has_old_version_top : 1;
386         guint has_new_version : 1;
387         guint is_valid : 1;
388 } MonoAssemblyBindingInfo;
389
390 struct _MonoMethodHeader {
391         guint32      code_size;
392         const unsigned char  *code;
393         guint16      max_stack;
394         unsigned int num_clauses : 15;
395         /* if num_locals != 0, then the following apply: */
396         unsigned int init_locals : 1;
397         guint16      num_locals;
398         MonoExceptionClause *clauses;
399         MonoType    *locals [MONO_ZERO_LEN_ARRAY];
400 };
401
402 #define MONO_SIZEOF_METHOD_HEADER (sizeof (struct _MonoMethodHeader) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
403
404 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
405 guint mono_aligned_addr_hash (gconstpointer ptr) MONO_INTERNAL;
406
407 void
408 mono_image_check_for_module_cctor (MonoImage *image) MONO_INTERNAL;
409
410 gpointer
411 mono_image_alloc  (MonoImage *image, guint size) MONO_INTERNAL;
412
413 gpointer
414 mono_image_alloc0 (MonoImage *image, guint size) MONO_INTERNAL;
415
416 char*
417 mono_image_strdup (MonoImage *image, const char *s) MONO_INTERNAL;
418
419 GList*
420 g_list_prepend_image (MonoImage *image, GList *list, gpointer data) MONO_INTERNAL;
421
422 GSList*
423 g_slist_append_image (MonoImage *image, GSList *list, gpointer data) MONO_INTERNAL;
424
425 void
426 mono_image_lock (MonoImage *image) MONO_INTERNAL;
427
428 void
429 mono_image_unlock (MonoImage *image) MONO_INTERNAL;
430
431 gpointer
432 mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property) MONO_INTERNAL;
433
434 void
435 mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value) MONO_INTERNAL;
436
437 void
438 mono_image_property_remove (MonoImage *image, gpointer subject) MONO_INTERNAL;
439
440 gboolean
441 mono_image_close_except_pools (MonoImage *image) MONO_INTERNAL;
442
443 void
444 mono_image_close_finish (MonoImage *image) MONO_INTERNAL;
445
446
447 MonoType*
448 mono_metadata_get_shared_type (MonoType *type) MONO_INTERNAL;
449
450 GSList*
451 mono_metadata_clean_for_image (MonoImage *image) MONO_INTERNAL;
452
453 void
454 mono_metadata_clean_generic_classes_for_image (MonoImage *image) MONO_INTERNAL;
455
456 void
457 mono_metadata_cleanup (void);
458
459 const char *   mono_meta_table_name              (int table) MONO_INTERNAL;
460 void           mono_metadata_compute_table_bases (MonoImage *meta) MONO_INTERNAL;
461
462 gboolean
463 mono_metadata_interfaces_from_typedef_full  (MonoImage             *image,
464                                                                                          guint32                table_index,
465                                                                                          MonoClass           ***interfaces,
466                                                                                          guint                 *count,
467                                                                                          gboolean               heap_alloc_result,
468                                                                                          MonoGenericContext    *context) MONO_INTERNAL;
469
470 MonoArrayType *
471 mono_metadata_parse_array_full              (MonoImage             *image,
472                                              MonoGenericContainer  *container,
473                                              const char            *ptr,
474                                              const char           **rptr) MONO_INTERNAL;
475
476 MonoType *
477 mono_metadata_parse_type_full               (MonoImage             *image,
478                                              MonoGenericContainer  *container,
479                                              MonoParseTypeMode      mode,
480                                              short                  opt_attrs,
481                                              const char            *ptr,
482                                              const char           **rptr);
483
484 MonoMethodSignature *
485 mono_metadata_parse_signature_full          (MonoImage             *image,
486                                              MonoGenericContainer  *generic_container,
487                                              guint32                token) MONO_INTERNAL;
488
489 MonoMethodSignature *
490 mono_metadata_parse_method_signature_full   (MonoImage             *image,
491                                              MonoGenericContainer  *generic_container,
492                                              int                     def,
493                                              const char             *ptr,
494                                              const char            **rptr);
495
496 MonoMethodHeader *
497 mono_metadata_parse_mh_full                 (MonoImage             *image,
498                                              MonoGenericContainer  *container,
499                                              const char            *ptr);
500
501 int* mono_metadata_get_param_attrs          (MonoImage *m, int def, int param_count) MONO_INTERNAL;
502 gboolean mono_metadata_method_has_param_attrs (MonoImage *m, int def) MONO_INTERNAL;
503
504 guint
505 mono_metadata_generic_context_hash          (const MonoGenericContext *context) MONO_INTERNAL;
506
507 gboolean
508 mono_metadata_generic_context_equal         (const MonoGenericContext *g1,
509                                              const MonoGenericContext *g2) MONO_INTERNAL;
510
511 MonoGenericInst *
512 mono_metadata_parse_generic_inst            (MonoImage             *image,
513                                              MonoGenericContainer  *container,
514                                              int                    count,
515                                              const char            *ptr,
516                                              const char           **rptr) MONO_INTERNAL;
517
518 MonoGenericInst *
519 mono_metadata_get_generic_inst              (int                    type_argc,
520                                              MonoType             **type_argv) MONO_INTERNAL;
521
522 MonoGenericClass *
523 mono_metadata_lookup_generic_class          (MonoClass             *gclass,
524                                              MonoGenericInst       *inst,
525                                              gboolean               is_dynamic) MONO_INTERNAL;
526
527 MonoGenericInst * mono_metadata_inflate_generic_inst  (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error) MONO_INTERNAL;
528
529 void mono_dynamic_stream_reset  (MonoDynamicStream* stream) MONO_INTERNAL;
530 void mono_assembly_addref       (MonoAssembly *assembly) MONO_INTERNAL;
531 void mono_assembly_load_friends (MonoAssembly* ass) MONO_INTERNAL;
532 gboolean mono_assembly_has_skip_verification (MonoAssembly* ass) MONO_INTERNAL;
533
534 gboolean mono_assembly_close_except_image_pools (MonoAssembly *assembly) MONO_INTERNAL;
535 void mono_assembly_close_finish (MonoAssembly *assembly) MONO_INTERNAL;
536
537
538 gboolean mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2) MONO_INTERNAL;
539
540 void mono_config_parse_publisher_policy (const char *filename, MonoAssemblyBindingInfo *binding_info) MONO_INTERNAL;
541
542 gboolean
543 mono_assembly_name_parse_full                (const char           *name,
544                                               MonoAssemblyName     *aname,
545                                               gboolean save_public_key,
546                                               gboolean *is_version_defined,
547                                                   gboolean *is_token_defined) MONO_INTERNAL;
548
549 guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
550
551 void mono_unload_interface_ids (MonoBitSet *bitset) MONO_INTERNAL;
552
553
554 MonoType *mono_metadata_type_dup (MonoImage *image, const MonoType *original) MONO_INTERNAL;
555 MonoMethodSignature  *mono_metadata_signature_dup_full (MonoImage *image,MonoMethodSignature *sig) MONO_INTERNAL;
556 MonoMethodSignature  *mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig) MONO_INTERNAL;
557
558 MonoGenericInst *
559 mono_get_shared_generic_inst (MonoGenericContainer *container) MONO_INTERNAL;
560
561 int
562 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open) MONO_INTERNAL;
563
564 gboolean
565 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only) MONO_INTERNAL;
566
567 MonoMarshalSpec *
568 mono_metadata_parse_marshal_spec_full (MonoImage *image, const char *ptr) MONO_INTERNAL;
569
570 guint          mono_metadata_generic_inst_hash (gconstpointer data) MONO_INTERNAL;
571 gboolean       mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb) MONO_INTERNAL;
572
573 void
574 mono_metadata_field_info_with_mempool (
575                                           MonoImage *meta, 
576                                       guint32       table_index,
577                                       guint32      *offset,
578                                       guint32      *rva,
579                                       MonoMarshalSpec **marshal_spec);
580
581 MonoClassField*
582 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field) MONO_INTERNAL;
583
584 MonoEvent*
585 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event) MONO_INTERNAL;
586
587 MonoProperty*
588 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property) MONO_INTERNAL;
589
590 guint32
591 mono_metadata_signature_size (MonoMethodSignature *sig) MONO_INTERNAL;
592
593 guint mono_metadata_str_hash (gconstpointer v1) MONO_INTERNAL;
594
595 gboolean mono_image_load_pe_data (MonoImage *image) MONO_INTERNAL;
596
597 gboolean mono_image_load_cli_data (MonoImage *image) MONO_INTERNAL;
598
599 void mono_image_load_names (MonoImage *image) MONO_INTERNAL;
600
601 MonoImage *mono_image_open_raw (const char *fname, MonoImageOpenStatus *status) MONO_INTERNAL;
602
603 #endif /* __MONO_METADATA_INTERNALS_H__ */
604