[xbuild] Fix test on windows.
[mono.git] / mono / metadata / domain-internals.h
1 /*
2  * Appdomain-related internal data structures and functions.
3  */
4 #ifndef __MONO_METADATA_DOMAIN_INTERNALS_H__
5 #define __MONO_METADATA_DOMAIN_INTERNALS_H__
6
7 #include <mono/metadata/appdomain.h>
8 #include <mono/metadata/mempool.h>
9 #include <mono/metadata/lock-tracer.h>
10 #include <mono/utils/mono-codeman.h>
11 #include <mono/metadata/mono-hash.h>
12 #include <mono/utils/mono-compiler.h>
13 #include <mono/utils/mono-internal-hash.h>
14 #include <mono/io-layer/io-layer.h>
15 #include <mono/metadata/mempool-internals.h>
16
17 extern CRITICAL_SECTION mono_delegate_section;
18 extern CRITICAL_SECTION mono_strtod_mutex;
19
20 /*
21  * If this is set, the memory belonging to appdomains is not freed when a domain is
22  * unloaded, and assemblies loaded by the appdomain are not unloaded either. This
23  * allows us to use typed gc in non-default appdomains too, leading to increased
24  * performance.
25  */ 
26 extern gboolean mono_dont_free_domains;
27
28 /* This is a copy of System.AppDomainSetup */
29 typedef struct {
30         MonoObject object;
31         MonoString *application_base;
32         MonoString *application_name;
33         MonoString *cache_path;
34         MonoString *configuration_file;
35         MonoString *dynamic_base;
36         MonoString *license_file;
37         MonoString *private_bin_path;
38         MonoString *private_bin_path_probe;
39         MonoString *shadow_copy_directories;
40         MonoString *shadow_copy_files;
41         MonoBoolean publisher_policy;
42         MonoBoolean path_changed;
43         int loader_optimization;
44         MonoBoolean disallow_binding_redirects;
45         MonoBoolean disallow_code_downloads;
46         MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
47         MonoObject *domain_initializer;
48         MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
49         MonoArray *domain_initializer_args;
50         MonoBoolean disallow_appbase_probe;
51         MonoArray *configuration_bytes;
52         MonoArray *serialized_non_primitives;
53 } MonoAppDomainSetup;
54
55 typedef struct _MonoJitInfoTable MonoJitInfoTable;
56 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
57
58 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE          64
59
60 struct _MonoJitInfoTableChunk
61 {
62         int                    refcount;
63         volatile int           num_elements;
64         volatile gint8        *last_code_end;
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         union {
86                 MonoClass *catch_class;
87                 gpointer filter;
88                 gpointer handler_end;
89         } data;
90 } MonoJitExceptionInfo;
91
92 /*
93  * Will contain information on the generic type arguments in the
94  * future.  For now, all arguments are always reference types.
95  */
96 typedef struct {
97         int dummy;
98 } MonoGenericSharingContext;
99
100 typedef struct
101 {
102         MonoGenericSharingContext *generic_sharing_context;
103         gint32 this_offset;
104         guint8 this_reg;
105         gboolean has_this:1;
106         gboolean this_in_reg:1;
107 } MonoGenericJitInfo;
108
109 /*
110 A try block hole is used to represent a non-contiguous part of
111 of a segment of native code protected by a given .try block.
112 Usually, a try block is defined as a contiguous segment of code.
113 But in some cases it's needed to have some parts of it to not be protected.
114 For example, given "try {} finally {}", the code in the .try block to call
115 the finally part looks like:
116
117 try {
118     ...
119         call finally_block
120         adjust stack
121         jump outside try block
122         ...
123 } finally {
124         ...
125 }
126
127 The instructions between the call and the jump should not be under the try block since they happen
128 after the finally block executes, which means if an async exceptions happens at that point we would
129 execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
130 that those instructions are not protected.
131 */
132 typedef struct
133 {
134         guint32 offset;
135         guint16 clause;
136         guint16 length;
137 } MonoTryBlockHoleJitInfo;
138
139 typedef struct
140 {
141         guint16 num_holes;
142         MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
143 } MonoTryBlockHoleTableJitInfo;
144
145 struct _MonoJitInfo {
146         /* NOTE: These first two elements (method and
147            next_jit_code_hash) must be in the same order and at the
148            same offset as in RuntimeMethod, because of the jit_code_hash
149            internal hash table in MonoDomain. */
150         MonoMethod *method;
151         struct _MonoJitInfo *next_jit_code_hash;
152         gpointer    code_start;
153         /* This might contain an id for the unwind info instead of a register mask */
154         guint32     used_regs;
155         int         code_size;
156         guint32     num_clauses:15;
157         /* Whenever the code is domain neutral or 'shared' */
158         gboolean    domain_neutral:1;
159         gboolean    cas_inited:1;
160         gboolean    cas_class_assert:1;
161         gboolean    cas_class_deny:1;
162         gboolean    cas_class_permitonly:1;
163         gboolean    cas_method_assert:1;
164         gboolean    cas_method_deny:1;
165         gboolean    cas_method_permitonly:1;
166         gboolean    has_generic_jit_info:1;
167         gboolean    has_try_block_holes:1;
168         gboolean    from_aot:1;
169         gboolean    from_llvm:1;
170
171         /* FIXME: Embed this after the structure later*/
172         gpointer    gc_info; /* Currently only used by SGen */
173         
174         MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
175         /* There is an optional MonoGenericJitInfo after the clauses */
176         /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
177 };
178
179 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
180
181 struct _MonoAppContext {
182         MonoObject obj;
183         gint32 domain_id;
184         gint32 context_id;
185         gpointer *static_data;
186 };
187
188 /*
189  * We have two unloading states because the domain
190  * must remain fully functional while AppDomain::DomainUnload is
191  * processed.
192  * After that unloading began and all domain facilities are teared down
193  * such as execution of new threadpool jobs.  
194  */
195 typedef enum {
196         MONO_APPDOMAIN_CREATED,
197         MONO_APPDOMAIN_UNLOADING_START,
198         MONO_APPDOMAIN_UNLOADING,
199         MONO_APPDOMAIN_UNLOADED
200 } MonoAppDomainState;
201
202 typedef struct _MonoThunkFreeList {
203         guint32 size;
204         int length;             /* only valid for the wait list */
205         struct _MonoThunkFreeList *next;
206 } MonoThunkFreeList;
207
208 typedef struct _MonoJitCodeHash MonoJitCodeHash;
209
210 typedef struct _MonoTlsDataRecord MonoTlsDataRecord;
211 struct _MonoTlsDataRecord {
212         MonoTlsDataRecord *next;
213         guint32 tls_offset;
214         guint32 size;
215 };
216
217 struct _MonoDomain {
218         /*
219          * This lock must never be taken before the loader lock,
220          * i.e. if both are taken by the same thread, the loader lock
221          * must taken first.
222          */
223         CRITICAL_SECTION    lock;
224         MonoMemPool        *mp;
225         MonoCodeManager    *code_mp;
226         /*
227          * keep all the managed objects close to each other for the precise GC
228          * For the Boehm GC we additionally keep close also other GC-tracked pointers.
229          */
230 #define MONO_DOMAIN_FIRST_OBJECT setup
231         MonoAppDomainSetup *setup;
232         MonoAppDomain      *domain;
233         MonoAppContext     *default_context;
234         MonoException      *out_of_memory_ex;
235         MonoException      *null_reference_ex;
236         MonoException      *stack_overflow_ex;
237         /* typeof (void) */
238         MonoObject         *typeof_void;
239         /* Ephemeron Tombstone*/
240         MonoObject         *ephemeron_tombstone;
241         /* new MonoType [0] */
242         MonoArray          *empty_types;
243         /* 
244          * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
245          * not object references.
246          */
247 #define MONO_DOMAIN_FIRST_GC_TRACKED env
248         MonoGHashTable     *env;
249         MonoGHashTable     *ldstr_table;
250         /* hashtables for Reflection handles */
251         MonoGHashTable     *type_hash;
252         MonoGHashTable     *refobject_hash;
253         /* a GC-tracked array to keep references to the static fields of types */
254         gpointer           *static_data_array;
255         /* maps class -> type initialization exception object */
256         MonoGHashTable    *type_init_exception_hash;
257         /* maps delegate trampoline addr -> delegate object */
258         MonoGHashTable     *delegate_hash_table;
259 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
260         guint32            state;
261         /* Needed by Thread:GetDomainID() */
262         gint32             domain_id;
263         gint32             shadow_serial;
264         unsigned char      inet_family_hint; // used in socket-io.c as a cache
265         GSList             *domain_assemblies;
266         MonoAssembly       *entry_assembly;
267         char               *friendly_name;
268         GPtrArray          *class_vtable_array;
269         /* maps remote class key -> MonoRemoteClass */
270         GHashTable         *proxy_vtable_hash;
271         /* Protected by 'jit_code_hash_lock' */
272         MonoInternalHashTable jit_code_hash;
273         CRITICAL_SECTION    jit_code_hash_lock;
274         int                 num_jit_info_tables;
275         MonoJitInfoTable * 
276           volatile          jit_info_table;
277         GSList             *jit_info_free_queue;
278         /* Used when loading assemblies */
279         gchar **search_path;
280         gchar *private_bin_path;
281         
282         /* Used by remoting proxies */
283         MonoMethod         *create_proxy_for_type_method;
284         MonoMethod         *private_invoke_method;
285         /* Used to store offsets of thread and context static fields */
286         GHashTable         *special_static_fields;
287         MonoTlsDataRecord  *tlsrec_list;
288         /* 
289          * This must be a GHashTable, since these objects can't be finalized
290          * if the hashtable contains a GC visible reference to them.
291          */
292         GHashTable         *finalizable_objects_hash;
293
294         /* These two are boehm only */
295         /* Maps MonoObjects to a GSList of WeakTrackResurrection GCHandles pointing to them */
296         GHashTable         *track_resurrection_objects_hash;
297         /* Maps WeakTrackResurrection GCHandles to the MonoObjects they point to */
298         GHashTable         *track_resurrection_handles_hash;
299
300         /* Protects the three hashes above */
301         CRITICAL_SECTION   finalizable_objects_hash_lock;
302         /* Used when accessing 'domain_assemblies' */
303         CRITICAL_SECTION    assemblies_lock;
304
305         GHashTable         *method_rgctx_hash;
306
307         GHashTable         *generic_virtual_cases;
308         MonoThunkFreeList **thunk_free_lists;
309
310         /* Information maintained by the JIT engine */
311         gpointer runtime_info;
312
313         /*thread pool jobs, used to coordinate shutdown.*/
314         volatile int                    threadpool_jobs;
315         HANDLE                          cleanup_semaphore;
316         
317         /* Contains the compiled runtime invoke wrapper used by finalizers */
318         gpointer            finalize_runtime_invoke;
319
320         /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
321         gpointer            capture_context_runtime_invoke;
322
323         /* Contains the compiled method used by async resylt creation to capture thread context*/
324         gpointer            capture_context_method;
325
326         /* Assembly bindings, the per-domain part */
327         GSList *assembly_bindings;
328         gboolean assembly_bindings_parsed;
329
330         /* Used by socket-io.c */
331         /* These are domain specific, since the assembly can be unloaded */
332         MonoImage *socket_assembly;
333         MonoClass *sockaddr_class;
334         MonoClassField *sockaddr_data_field;
335
336         /* Used by threadpool.c */
337         MonoImage *system_image;
338         MonoImage *system_net_dll;
339         MonoClass *corlib_asyncresult_class;
340         MonoClass *socket_class;
341         MonoClass *ad_unloaded_ex_class;
342         MonoClass *process_class;
343 };
344
345 typedef struct  {
346         guint16 major, minor, build, revision;
347 } AssemblyVersionSet;
348
349 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
350 typedef struct  {
351         const char runtime_version [12];
352         const char framework_version [4];
353         const AssemblyVersionSet version_sets [3];
354 } MonoRuntimeInfo;
355
356 #define mono_domain_lock(domain) mono_locks_acquire(&(domain)->lock, DomainLock)
357 #define mono_domain_unlock(domain) mono_locks_release(&(domain)->lock, DomainLock)
358 #define mono_domain_assemblies_lock(domain) mono_locks_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
359 #define mono_domain_assemblies_unlock(domain) mono_locks_release(&(domain)->assemblies_lock, DomainAssembliesLock)
360 #define mono_domain_jit_code_hash_lock(domain) mono_locks_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
361 #define mono_domain_jit_code_hash_unlock(domain) mono_locks_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
362
363 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
364
365 void
366 mono_install_runtime_load  (MonoLoadFunc func) MONO_INTERNAL;
367
368 MonoDomain*
369 mono_runtime_load (const char *filename, const char *runtime_version) MONO_INTERNAL;
370
371 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
372
373 void
374 mono_install_create_domain_hook (MonoCreateDomainFunc func) MONO_INTERNAL;
375
376 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
377
378 void
379 mono_install_free_domain_hook (MonoFreeDomainFunc func) MONO_INTERNAL;
380
381 void 
382 mono_init_com_types (void) MONO_INTERNAL;
383
384 void 
385 mono_cleanup (void) MONO_INTERNAL;
386
387 void
388 mono_close_exe_image (void) MONO_INTERNAL;
389
390 void
391 mono_jit_info_table_add    (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
392
393 void
394 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
395
396 void
397 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end) MONO_INTERNAL;
398
399 MonoGenericJitInfo*
400 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji) MONO_INTERNAL;
401
402 MonoGenericSharingContext*
403 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji) MONO_INTERNAL;
404
405 void
406 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx) MONO_INTERNAL;
407
408 MonoJitInfo*
409 mono_domain_lookup_shared_generic (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
410
411 char *
412 mono_make_shadow_copy (const char *filename) MONO_INTERNAL;
413
414 gboolean
415 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name) MONO_INTERNAL;
416
417 gpointer
418 mono_domain_alloc  (MonoDomain *domain, guint size) MONO_INTERNAL;
419
420 gpointer
421 mono_domain_alloc0 (MonoDomain *domain, guint size) MONO_INTERNAL;
422
423 void*
424 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
425
426 void*
427 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment) MONO_INTERNAL;
428
429 void
430 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize) MONO_INTERNAL;
431
432 void *
433 nacl_domain_get_code_dest (MonoDomain *domain, void *data) MONO_INTERNAL;
434
435 void 
436 nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end) MONO_INTERNAL;
437
438 void
439 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data) MONO_INTERNAL;
440
441 void
442 mono_domain_unset (void) MONO_INTERNAL;
443
444 void
445 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception) MONO_INTERNAL;
446
447 MonoTryBlockHoleTableJitInfo*
448 mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji) MONO_INTERNAL;
449
450 /* 
451  * Installs a new function which is used to return a MonoJitInfo for a method inside
452  * an AOT module.
453  */
454 typedef MonoJitInfo *(*MonoJitInfoFindInAot)         (MonoDomain *domain, MonoImage *image, gpointer addr);
455 void          mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func) MONO_INTERNAL;
456
457 void
458 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash) MONO_INTERNAL;
459
460 MonoAppDomain *
461 ves_icall_System_AppDomain_getCurDomain            (void) MONO_INTERNAL;
462
463 MonoAppDomain *
464 ves_icall_System_AppDomain_getRootDomain           (void) MONO_INTERNAL;
465
466 MonoAppDomain *
467 ves_icall_System_AppDomain_createDomain            (MonoString         *friendly_name,
468                                                     MonoAppDomainSetup *setup) MONO_INTERNAL;
469
470 MonoObject *
471 ves_icall_System_AppDomain_GetData                 (MonoAppDomain *ad, 
472                                                     MonoString    *name) MONO_INTERNAL;
473
474 MonoReflectionAssembly *
475 ves_icall_System_AppDomain_LoadAssemblyRaw         (MonoAppDomain *ad,
476                                                     MonoArray *raw_assembly, 
477                                                     MonoArray *raw_symbol_store,
478                                                     MonoObject *evidence,
479                                                     MonoBoolean refonly) MONO_INTERNAL;
480
481 void
482 ves_icall_System_AppDomain_SetData                 (MonoAppDomain *ad, 
483                                                     MonoString    *name, 
484                                                     MonoObject    *data) MONO_INTERNAL;
485
486 MonoAppDomainSetup *
487 ves_icall_System_AppDomain_getSetup                (MonoAppDomain *ad) MONO_INTERNAL;
488
489 MonoString *
490 ves_icall_System_AppDomain_getFriendlyName         (MonoAppDomain *ad) MONO_INTERNAL;
491
492 MonoArray *
493 ves_icall_System_AppDomain_GetAssemblies           (MonoAppDomain *ad,
494                                                     MonoBoolean refonly) MONO_INTERNAL;
495
496 MonoReflectionAssembly *
497 ves_icall_System_Reflection_Assembly_LoadFrom      (MonoString *fname,
498                                                     MonoBoolean refonly) MONO_INTERNAL;
499
500 MonoReflectionAssembly *
501 ves_icall_System_AppDomain_LoadAssembly            (MonoAppDomain *ad, 
502                                                     MonoString *assRef,
503                                                     MonoObject    *evidence,
504                                                     MonoBoolean refonly) MONO_INTERNAL;
505
506 gboolean
507 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id) MONO_INTERNAL;
508
509 void
510 ves_icall_System_AppDomain_InternalUnload          (gint32 domain_id) MONO_INTERNAL;
511
512 gint32
513 ves_icall_System_AppDomain_ExecuteAssembly         (MonoAppDomain *ad, 
514                                                                                                         MonoReflectionAssembly *refass,
515                                                                                                         MonoArray     *args) MONO_INTERNAL;
516
517 MonoAppDomain * 
518 ves_icall_System_AppDomain_InternalSetDomain       (MonoAppDomain *ad) MONO_INTERNAL;
519
520 MonoAppDomain * 
521 ves_icall_System_AppDomain_InternalSetDomainByID   (gint32 domainid) MONO_INTERNAL;
522
523 void
524 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad) MONO_INTERNAL;
525
526 void
527 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id) MONO_INTERNAL;
528
529 void
530 ves_icall_System_AppDomain_InternalPopDomainRef (void) MONO_INTERNAL;
531
532 MonoAppContext * 
533 ves_icall_System_AppDomain_InternalGetContext      (void) MONO_INTERNAL;
534
535 MonoAppContext * 
536 ves_icall_System_AppDomain_InternalGetDefaultContext      (void) MONO_INTERNAL;
537
538 MonoAppContext * 
539 ves_icall_System_AppDomain_InternalSetContext      (MonoAppContext *mc) MONO_INTERNAL;
540
541 gint32 
542 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad) MONO_INTERNAL;
543
544 MonoString *
545 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid) MONO_INTERNAL;
546
547 MonoAssembly *
548 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status) MONO_INTERNAL;
549
550 const MonoRuntimeInfo*
551 mono_get_runtime_info (void) MONO_INTERNAL;
552
553 void
554 mono_runtime_set_no_exec (gboolean val) MONO_INTERNAL;
555
556 gboolean
557 mono_runtime_get_no_exec (void) MONO_INTERNAL;
558
559 gboolean
560 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname) MONO_INTERNAL;
561
562 MonoImage *mono_assembly_open_from_bundle (const char *filename,
563                                            MonoImageOpenStatus *status,
564                                            gboolean refonly) MONO_INTERNAL;
565
566 void
567 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
568
569 MonoReflectionAssembly *
570 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, gboolean refonly) MONO_INTERNAL;
571
572 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname, 
573                                                 const char       *basedir, 
574                                                 MonoImageOpenStatus *status,
575                                                 gboolean refonly) MONO_INTERNAL;
576
577 void mono_set_private_bin_path_from_config (MonoDomain *domain) MONO_INTERNAL;
578
579 int mono_framework_version (void) MONO_INTERNAL;
580
581 void mono_reflection_cleanup_domain (MonoDomain *domain) MONO_INTERNAL;
582
583 void mono_assembly_cleanup_domain_bindings (guint32 domain_id) MONO_INTERNAL;;
584
585 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */