[metadata] Don't use static_data_array
[mono.git] / mono / metadata / domain-internals.h
1 /**
2  * \file
3  * Appdomain-related internal data structures and functions.
4  * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
5  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6  */
7 #ifndef __MONO_METADATA_DOMAIN_INTERNALS_H__
8 #define __MONO_METADATA_DOMAIN_INTERNALS_H__
9
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/mempool.h>
12 #include <mono/metadata/lock-tracer.h>
13 #include <mono/utils/mono-codeman.h>
14 #include <mono/metadata/mono-hash.h>
15 #include <mono/utils/mono-compiler.h>
16 #include <mono/utils/mono-internal-hash.h>
17 #include <mono/metadata/mempool-internals.h>
18
19 /*
20  * If this is set, the memory belonging to appdomains is not freed when a domain is
21  * unloaded, and assemblies loaded by the appdomain are not unloaded either. This
22  * allows us to use typed gc in non-default appdomains too, leading to increased
23  * performance.
24  */ 
25 extern gboolean mono_dont_free_domains;
26
27 /* This is a copy of System.AppDomainSetup */
28 typedef struct {
29         MonoObject object;
30         MonoString *application_base;
31         MonoString *application_name;
32         MonoString *cache_path;
33         MonoString *configuration_file;
34         MonoString *dynamic_base;
35         MonoString *license_file;
36         MonoString *private_bin_path;
37         MonoString *private_bin_path_probe;
38         MonoString *shadow_copy_directories;
39         MonoString *shadow_copy_files;
40         MonoBoolean publisher_policy;
41         MonoBoolean path_changed;
42         int loader_optimization;
43         MonoBoolean disallow_binding_redirects;
44         MonoBoolean disallow_code_downloads;
45         MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
46         MonoObject *domain_initializer;
47         MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
48         MonoArray *domain_initializer_args;
49         MonoBoolean disallow_appbase_probe;
50         MonoArray *configuration_bytes;
51         MonoArray *serialized_non_primitives;
52 } MonoAppDomainSetup;
53
54 typedef struct _MonoJitInfoTable MonoJitInfoTable;
55 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
56
57 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE          64
58
59 struct _MonoJitInfoTableChunk
60 {
61         int                    refcount;
62         volatile int           num_elements;
63         volatile gint8        *last_code_end;
64         MonoJitInfo *next_tombstone;
65         MonoJitInfo * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
66 };
67
68 struct _MonoJitInfoTable
69 {
70         MonoDomain             *domain;
71         int                     num_chunks;
72         MonoJitInfoTableChunk  *chunks [MONO_ZERO_LEN_ARRAY];
73 };
74
75 #define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
76
77 typedef GArray MonoAotModuleInfoTable;
78
79 typedef struct {
80         guint32  flags;
81         gint32   exvar_offset;
82         gpointer try_start;
83         gpointer try_end;
84         gpointer handler_start;
85         /*
86          * For LLVM compiled code, this is the index of the il clause
87          * associated with this handler.
88          */
89         int clause_index;
90         uint32_t try_offset;
91         uint32_t try_len;
92         uint32_t handler_offset;
93         uint32_t handler_len;
94         union {
95                 MonoClass *catch_class;
96                 gpointer filter;
97                 gpointer handler_end;
98         } data;
99 } MonoJitExceptionInfo;
100
101 /*
102  * Contains information about the type arguments for generic shared methods.
103  */
104 typedef struct {
105         gboolean is_gsharedvt;
106 } MonoGenericSharingContext;
107
108 /* Simplified DWARF location list entry */
109 typedef struct {
110         /* Whenever the value is in a register */
111         gboolean is_reg;
112         /*
113          * If is_reg is TRUE, the register which contains the value. Otherwise
114          * the base register.
115          */
116         int reg;
117         /*
118          * If is_reg is FALSE, the offset of the stack location relative to 'reg'.
119          * Otherwise, 0.
120          */
121         int offset;
122         /*
123          * Offsets of the PC interval where the value is in this location.
124          */
125         int from, to;
126 } MonoDwarfLocListEntry;
127
128 typedef struct
129 {
130         MonoGenericSharingContext *generic_sharing_context;
131         int nlocs;
132         MonoDwarfLocListEntry *locations;
133         gint32 this_offset;
134         guint8 this_reg;
135         gboolean has_this:1;
136         gboolean this_in_reg:1;
137 } MonoGenericJitInfo;
138
139 /*
140 A try block hole is used to represent a non-contiguous part of
141 of a segment of native code protected by a given .try block.
142 Usually, a try block is defined as a contiguous segment of code.
143 But in some cases it's needed to have some parts of it to not be protected.
144 For example, given "try {} finally {}", the code in the .try block to call
145 the finally part looks like:
146
147 try {
148     ...
149         call finally_block
150         adjust stack
151         jump outside try block
152         ...
153 } finally {
154         ...
155 }
156
157 The instructions between the call and the jump should not be under the try block since they happen
158 after the finally block executes, which means if an async exceptions happens at that point we would
159 execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
160 that those instructions are not protected.
161 */
162 typedef struct
163 {
164         guint32 offset;
165         guint16 clause;
166         guint16 length;
167 } MonoTryBlockHoleJitInfo;
168
169 typedef struct
170 {
171         guint16 num_holes;
172         MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
173 } MonoTryBlockHoleTableJitInfo;
174
175 typedef struct
176 {
177         guint32 stack_size;
178         guint32 epilog_size;
179 } MonoArchEHJitInfo;
180
181 typedef struct {
182         /* Relative to code_start */
183         int thunks_offset;
184         int thunks_size;
185 } MonoThunkJitInfo;
186
187 typedef enum {
188         JIT_INFO_NONE = 0,
189         JIT_INFO_HAS_GENERIC_JIT_INFO = (1 << 0),
190         JIT_INFO_HAS_TRY_BLOCK_HOLES = (1 << 1),
191         JIT_INFO_HAS_ARCH_EH_INFO = (1 << 2),
192         JIT_INFO_HAS_THUNK_INFO = (1 << 3)
193 } MonoJitInfoFlags;
194
195 struct _MonoJitInfo {
196         /* NOTE: These first two elements (method and
197            next_jit_code_hash) must be in the same order and at the
198            same offset as in RuntimeMethod, because of the jit_code_hash
199            internal hash table in MonoDomain. */
200         union {
201                 MonoMethod *method;
202                 MonoImage *image;
203                 gpointer aot_info;
204                 gpointer tramp_info;
205         } d;
206         union {
207                 struct _MonoJitInfo *next_jit_code_hash;
208                 struct _MonoJitInfo *next_tombstone;
209         } n;
210         gpointer    code_start;
211         guint32     unwind_info;
212         int         code_size;
213         guint32     num_clauses:15;
214         /* Whenever the code is domain neutral or 'shared' */
215         gboolean    domain_neutral:1;
216         gboolean    has_generic_jit_info:1;
217         gboolean    has_try_block_holes:1;
218         gboolean    has_arch_eh_info:1;
219         gboolean    has_thunk_info:1;
220         gboolean    from_aot:1;
221         gboolean    from_llvm:1;
222         gboolean    dbg_attrs_inited:1;
223         gboolean    dbg_hidden:1;
224         /* Whenever this jit info was loaded in async context */
225         gboolean    async:1;
226         gboolean    dbg_step_through:1;
227         gboolean    dbg_non_user_code:1;
228         /*
229          * Whenever this jit info refers to a trampoline.
230          * d.tramp_info contains additional data in this case.
231          */
232         gboolean    is_trampoline:1;
233
234         /* FIXME: Embed this after the structure later*/
235         gpointer    gc_info; /* Currently only used by SGen */
236         
237         MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
238         /* There is an optional MonoGenericJitInfo after the clauses */
239         /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
240         /* There is an optional MonoArchEHJitInfo after MonoTryBlockHoleTableJitInfo */
241         /* There is an optional MonoThunkJitInfo after MonoArchEHJitInfo */
242 };
243
244 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
245
246 typedef struct {
247         gpointer *static_data; /* Used to free the static data without going through the MonoAppContext object itself. */
248         uint32_t gc_handle;
249 } ContextStaticData;
250
251 struct _MonoAppContext {
252         MonoObject obj;
253         gint32 domain_id;
254         gint32 context_id;
255         gpointer *static_data;
256         ContextStaticData *data;
257 };
258
259 /* Lock-free allocator */
260 typedef struct {
261         guint8 *mem;
262         gpointer prev;
263         int size, pos;
264 } LockFreeMempoolChunk;
265
266 typedef struct {
267         LockFreeMempoolChunk *current, *chunks;
268 } LockFreeMempool;
269
270 /*
271  * We have two unloading states because the domain
272  * must remain fully functional while AppDomain::DomainUnload is
273  * processed.
274  * After that unloading began and all domain facilities are teared down
275  * such as execution of new threadpool jobs.  
276  */
277 typedef enum {
278         MONO_APPDOMAIN_CREATED,
279         MONO_APPDOMAIN_UNLOADING_START,
280         MONO_APPDOMAIN_UNLOADING,
281         MONO_APPDOMAIN_UNLOADED
282 } MonoAppDomainState;
283
284 typedef struct _MonoThunkFreeList {
285         guint32 size;
286         int length;             /* only valid for the wait list */
287         struct _MonoThunkFreeList *next;
288 } MonoThunkFreeList;
289
290 typedef struct _MonoJitCodeHash MonoJitCodeHash;
291
292 struct _MonoDomain {
293         /*
294          * This lock must never be taken before the loader lock,
295          * i.e. if both are taken by the same thread, the loader lock
296          * must taken first.
297          */
298         MonoCoopMutex    lock;
299         MonoMemPool        *mp;
300         MonoCodeManager    *code_mp;
301         /*
302          * keep all the managed objects close to each other for the precise GC
303          * For the Boehm GC we additionally keep close also other GC-tracked pointers.
304          */
305 #define MONO_DOMAIN_FIRST_OBJECT setup
306         MonoAppDomainSetup *setup;
307         MonoAppDomain      *domain;
308         MonoAppContext     *default_context;
309         MonoException      *out_of_memory_ex;
310         MonoException      *null_reference_ex;
311         MonoException      *stack_overflow_ex;
312         /* typeof (void) */
313         MonoObject         *typeof_void;
314         /* Ephemeron Tombstone*/
315         MonoObject         *ephemeron_tombstone;
316         /* new MonoType [0] */
317         MonoArray          *empty_types;
318         MonoString         *empty_string;
319         /* 
320          * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
321          * not object references.
322          */
323 #define MONO_DOMAIN_FIRST_GC_TRACKED env
324         MonoGHashTable     *env;
325         MonoGHashTable     *ldstr_table;
326         /* hashtables for Reflection handles */
327         MonoGHashTable     *type_hash;
328         MonoGHashTable     *refobject_hash;
329         /* maps class -> type initialization exception object */
330         MonoGHashTable    *type_init_exception_hash;
331         /* maps delegate trampoline addr -> delegate object */
332         MonoGHashTable     *delegate_hash_table;
333 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
334         guint32            state;
335         /* Needed by Thread:GetDomainID() */
336         gint32             domain_id;
337         gint32             shadow_serial;
338         GSList             *domain_assemblies;
339         MonoAssembly       *entry_assembly;
340         char               *friendly_name;
341         GPtrArray          *class_vtable_array;
342         /* maps remote class key -> MonoRemoteClass */
343         GHashTable         *proxy_vtable_hash;
344         /* Protected by 'jit_code_hash_lock' */
345         MonoInternalHashTable jit_code_hash;
346         mono_mutex_t    jit_code_hash_lock;
347         int                 num_jit_info_tables;
348         MonoJitInfoTable * 
349           volatile          jit_info_table;
350         /*
351          * Contains information about AOT loaded code.
352          * Only used in the root domain.
353          */
354         MonoJitInfoTable *
355           volatile          aot_modules;
356         GSList             *jit_info_free_queue;
357         /* Used when loading assemblies */
358         gchar **search_path;
359         gchar *private_bin_path;
360         LockFreeMempool *lock_free_mp;
361         
362         /* Used by remoting proxies */
363         MonoMethod         *create_proxy_for_type_method;
364         MonoMethod         *private_invoke_method;
365         /* Used to store offsets of thread and context static fields */
366         GHashTable         *special_static_fields;
367         /* 
368          * This must be a GHashTable, since these objects can't be finalized
369          * if the hashtable contains a GC visible reference to them.
370          */
371         GHashTable         *finalizable_objects_hash;
372
373         /* Protects the three hashes above */
374         mono_mutex_t   finalizable_objects_hash_lock;
375         /* Used when accessing 'domain_assemblies' */
376         mono_mutex_t    assemblies_lock;
377
378         GHashTable         *method_rgctx_hash;
379
380         GHashTable         *generic_virtual_cases;
381
382         /* Information maintained by the JIT engine */
383         gpointer runtime_info;
384         
385         /* Contains the compiled runtime invoke wrapper used by finalizers */
386         gpointer            finalize_runtime_invoke;
387
388         /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
389         gpointer            capture_context_runtime_invoke;
390
391         /* Contains the compiled method used by async resylt creation to capture thread context*/
392         gpointer            capture_context_method;
393
394         /* Assembly bindings, the per-domain part */
395         GSList *assembly_bindings;
396         gboolean assembly_bindings_parsed;
397
398         /* Used by socket-io.c */
399         /* These are domain specific, since the assembly can be unloaded */
400         MonoImage *socket_assembly;
401         MonoClass *sockaddr_class;
402         MonoClassField *sockaddr_data_field;
403         MonoClassField *sockaddr_data_length_field;
404
405         /* Cache function pointers for architectures  */
406         /* that require wrappers */
407         GHashTable *ftnptrs_hash;
408
409         /* Maps MonoMethod* to weak links to DynamicMethod objects */
410         GHashTable *method_to_dyn_method;
411
412         /* <ThrowUnobservedTaskExceptions /> support */
413         gboolean throw_unobserved_task_exceptions;
414
415         guint32 execution_context_field_offset;
416 };
417
418 typedef struct  {
419         guint16 major, minor, build, revision;
420 } AssemblyVersionSet;
421
422 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
423 typedef struct  {
424         const char runtime_version [12];
425         const char framework_version [4];
426         const AssemblyVersionSet version_sets [5];
427 } MonoRuntimeInfo;
428
429 #define mono_domain_assemblies_lock(domain) mono_locks_os_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
430 #define mono_domain_assemblies_unlock(domain) mono_locks_os_release(&(domain)->assemblies_lock, DomainAssembliesLock)
431 #define mono_domain_jit_code_hash_lock(domain) mono_locks_os_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
432 #define mono_domain_jit_code_hash_unlock(domain) mono_locks_os_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
433
434 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
435
436 void mono_domain_lock (MonoDomain *domain) MONO_LLVM_INTERNAL;
437 void mono_domain_unlock (MonoDomain *domain) MONO_LLVM_INTERNAL;
438
439 void
440 mono_install_runtime_load  (MonoLoadFunc func);
441
442 MonoDomain*
443 mono_runtime_load (const char *filename, const char *runtime_version);
444
445 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
446
447 void
448 mono_install_create_domain_hook (MonoCreateDomainFunc func);
449
450 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
451
452 void
453 mono_install_free_domain_hook (MonoFreeDomainFunc func);
454
455 void 
456 mono_cleanup (void);
457
458 void
459 mono_close_exe_image (void);
460
461 int
462 mono_jit_info_size (MonoJitInfoFlags flags, int num_clauses, int num_holes);
463
464 void
465 mono_jit_info_init (MonoJitInfo *ji, MonoMethod *method, guint8 *code, int code_size,
466                                         MonoJitInfoFlags flags, int num_clauses, int num_holes);
467
468 MonoJitInfoTable *
469 mono_jit_info_table_new (MonoDomain *domain);
470
471 void
472 mono_jit_info_table_free (MonoJitInfoTable *table);
473
474 void
475 mono_jit_info_table_add    (MonoDomain *domain, MonoJitInfo *ji);
476
477 void
478 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji);
479
480 void
481 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end);
482
483 MonoGenericJitInfo*
484 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji);
485
486 MonoGenericSharingContext*
487 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji);
488
489 void
490 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx);
491
492 char *
493 mono_make_shadow_copy (const char *filename, MonoError *error);
494
495 gboolean
496 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name);
497
498 gpointer
499 mono_domain_alloc  (MonoDomain *domain, guint size);
500
501 gpointer
502 mono_domain_alloc0 (MonoDomain *domain, guint size);
503
504 gpointer
505 mono_domain_alloc0_lock_free (MonoDomain *domain, guint size);
506
507 void*
508 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
509
510 void*
511 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment);
512
513 void
514 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize);
515
516 void
517 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data);
518
519 void
520 mono_domain_unset (void);
521
522 void
523 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception);
524
525 MonoTryBlockHoleTableJitInfo*
526 mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji);
527
528 MonoArchEHJitInfo*
529 mono_jit_info_get_arch_eh_info (MonoJitInfo *ji);
530
531 MonoThunkJitInfo*
532 mono_jit_info_get_thunk_info (MonoJitInfo *ji);
533
534 /* 
535  * Installs a new function which is used to return a MonoJitInfo for a method inside
536  * an AOT module.
537  */
538 typedef MonoJitInfo *(*MonoJitInfoFindInAot)         (MonoDomain *domain, MonoImage *image, gpointer addr);
539 void          mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func);
540
541 void
542 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash);
543
544 MonoAssembly *
545 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status);
546
547 const MonoRuntimeInfo*
548 mono_get_runtime_info (void);
549
550 void
551 mono_runtime_set_no_exec (gboolean val);
552
553 gboolean
554 mono_runtime_get_no_exec (void);
555
556 gboolean
557 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname);
558
559 MonoImage *mono_assembly_open_from_bundle (const char *filename,
560                                            MonoImageOpenStatus *status,
561                                            gboolean refonly);
562
563 MonoAssembly *
564 mono_try_assembly_resolve (MonoDomain *domain, const char *fname, MonoAssembly *requesting, gboolean refonly, MonoError *error);
565
566 MonoAssembly *
567 mono_domain_assembly_postload_search (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly);
568
569 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname, 
570                                                 const char       *basedir, 
571                                                 MonoImageOpenStatus *status,
572                                                 gboolean refonly);
573
574 void mono_domain_set_options_from_config (MonoDomain *domain);
575
576 int mono_framework_version (void);
577
578 void mono_reflection_cleanup_domain (MonoDomain *domain);
579
580 void mono_assembly_cleanup_domain_bindings (guint32 domain_id);
581
582 MonoJitInfo* mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_aot, gboolean allow_trampolines);
583
584 void mono_enable_debug_domain_unload (gboolean enable);
585
586 MonoReflectionAssembly *
587 mono_domain_try_type_resolve_checked (MonoDomain *domain, char *name, MonoObject *tb, MonoError *error);
588
589 void
590 mono_runtime_init_checked (MonoDomain *domain, MonoThreadStartCB start_cb, MonoThreadAttachCB attach_cb, MonoError *error);
591
592 void
593 mono_context_init_checked (MonoDomain *domain, MonoError *error);
594
595 gboolean
596 mono_assembly_has_reference_assembly_attribute (MonoAssembly *assembly, MonoError *error);
597
598 GPtrArray*
599 mono_domain_get_assemblies (MonoDomain *domain, gboolean refonly);
600
601 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */