26ea5cde7ec2b1a401b7befcb8d0630c265d2b17
[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/utils/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
16 extern CRITICAL_SECTION mono_delegate_section;
17 extern CRITICAL_SECTION mono_strtod_mutex;
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 * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
65 };
66
67 struct _MonoJitInfoTable
68 {
69         MonoDomain             *domain;
70         int                     num_chunks;
71         MonoJitInfoTableChunk  *chunks [MONO_ZERO_LEN_ARRAY];
72 };
73
74 #define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
75
76 typedef GArray MonoAotModuleInfoTable;
77
78 typedef struct {
79         guint32  flags;
80         gint32   exvar_offset;
81         gpointer try_start;
82         gpointer try_end;
83         gpointer handler_start;
84         union {
85                 MonoClass *catch_class;
86                 gpointer filter;
87         } data;
88 } MonoJitExceptionInfo;
89
90 /*
91  * Will contain information on the generic type arguments in the
92  * future.  For now, all arguments are always reference types.
93  */
94 typedef struct {
95         int dummy;
96 } MonoGenericSharingContext;
97
98 typedef struct
99 {
100         MonoGenericSharingContext *generic_sharing_context;
101         gint32 this_offset;
102         guint8 this_reg;
103         gboolean has_this:1;
104         gboolean this_in_reg:1;
105 } MonoGenericJitInfo;
106
107 struct _MonoJitInfo {
108         /* NOTE: These first two elements (method and
109            next_jit_code_hash) must be in the same order and at the
110            same offset as in RuntimeMethod, because of the jit_code_hash
111            internal hash table in MonoDomain. */
112         MonoMethod *method;
113         struct _MonoJitInfo *next_jit_code_hash;
114         gpointer    code_start;
115         /* This might contain an id for the unwind info instead of a register mask */
116         guint32     used_regs;
117         int         code_size;
118         guint32     num_clauses:15;
119         /* Whenever the code is domain neutral or 'shared' */
120         gboolean    domain_neutral:1;
121         gboolean    cas_inited:1;
122         gboolean    cas_class_assert:1;
123         gboolean    cas_class_deny:1;
124         gboolean    cas_class_permitonly:1;
125         gboolean    cas_method_assert:1;
126         gboolean    cas_method_deny:1;
127         gboolean    cas_method_permitonly:1;
128         gboolean    has_generic_jit_info:1;
129         gboolean    from_aot:1;
130         gboolean    from_llvm:1;
131 #ifdef HAVE_SGEN_GC
132         /* FIXME: Embed this after the structure later */
133         gpointer    gc_info;
134 #endif
135         MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
136         /* There is an optional MonoGenericJitInfo after the clauses */
137 };
138
139 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
140
141 struct _MonoAppContext {
142         MonoObject obj;
143         gint32 domain_id;
144         gint32 context_id;
145         gpointer *static_data;
146 };
147
148 /*
149  * We have two unloading states because the domain
150  * must remain fully functional while AppDomain::DomainUnload is
151  * processed.
152  * After that unloading began and all domain facilities are teared down
153  * such as execution of new threadpool jobs.  
154  */
155 typedef enum {
156         MONO_APPDOMAIN_CREATED,
157         MONO_APPDOMAIN_UNLOADING_START,
158         MONO_APPDOMAIN_UNLOADING,
159         MONO_APPDOMAIN_UNLOADED
160 } MonoAppDomainState;
161
162 typedef struct _MonoThunkFreeList {
163         guint32 size;
164         int length;             /* only valid for the wait list */
165         struct _MonoThunkFreeList *next;
166 } MonoThunkFreeList;
167
168 typedef struct _MonoJitCodeHash MonoJitCodeHash;
169
170 struct _MonoDomain {
171         /*
172          * This lock must never be taken before the loader lock,
173          * i.e. if both are taken by the same thread, the loader lock
174          * must taken first.
175          */
176         CRITICAL_SECTION    lock;
177         MonoMemPool        *mp;
178         MonoCodeManager    *code_mp;
179         /*
180          * keep all the managed objects close to each other for the precise GC
181          * For the Boehm GC we additionally keep close also other GC-tracked pointers.
182          */
183 #define MONO_DOMAIN_FIRST_OBJECT setup
184         MonoAppDomainSetup *setup;
185         MonoAppDomain      *domain;
186         MonoAppContext     *default_context;
187         MonoException      *out_of_memory_ex;
188         MonoException      *null_reference_ex;
189         MonoException      *stack_overflow_ex;
190         /* typeof (void) */
191         MonoObject         *typeof_void;
192         /* 
193          * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
194          * not object references.
195          */
196 #define MONO_DOMAIN_FIRST_GC_TRACKED env
197         MonoGHashTable     *env;
198         MonoGHashTable     *ldstr_table;
199         /* hashtables for Reflection handles */
200         MonoGHashTable     *type_hash;
201         MonoGHashTable     *refobject_hash;
202         /* a GC-tracked array to keep references to the static fields of types */
203         gpointer           *static_data_array;
204         /* maps class -> type initialization exception object */
205         MonoGHashTable    *type_init_exception_hash;
206         /* maps delegate trampoline addr -> delegate object */
207         MonoGHashTable     *delegate_hash_table;
208 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
209         guint32            state;
210         /* Needed by Thread:GetDomainID() */
211         gint32             domain_id;
212         gint32             shadow_serial;
213         unsigned char      inet_family_hint; // used in socket-io.c as a cache
214         GSList             *domain_assemblies;
215         MonoAssembly       *entry_assembly;
216         char               *friendly_name;
217         GHashTable         *class_vtable_hash;
218         /* maps remote class key -> MonoRemoteClass */
219         GHashTable         *proxy_vtable_hash;
220         /* Protected by 'jit_code_hash_lock' */
221         MonoInternalHashTable jit_code_hash;
222         CRITICAL_SECTION    jit_code_hash_lock;
223         int                 num_jit_info_tables;
224         MonoJitInfoTable * 
225           volatile          jit_info_table;
226         GSList             *jit_info_free_queue;
227         /* Used when loading assemblies */
228         gchar **search_path;
229         gchar *private_bin_path;
230         
231         /* Used by remoting proxies */
232         MonoMethod         *create_proxy_for_type_method;
233         MonoMethod         *private_invoke_method;
234         /* Used to store offsets of thread and context static fields */
235         GHashTable         *special_static_fields;
236         /* 
237          * This must be a GHashTable, since these objects can't be finalized
238          * if the hashtable contains a GC visible reference to them.
239          */
240         GHashTable         *finalizable_objects_hash;
241 #ifndef HAVE_SGEN_GC
242         /* Maps MonoObjects to a GSList of WeakTrackResurrection GCHandles pointing to them */
243         GHashTable         *track_resurrection_objects_hash;
244         /* Maps WeakTrackResurrection GCHandles to the MonoObjects they point to */
245         GHashTable         *track_resurrection_handles_hash;
246 #endif
247         /* Protects the three hashes above */
248         CRITICAL_SECTION   finalizable_objects_hash_lock;
249         /* Used when accessing 'domain_assemblies' */
250         CRITICAL_SECTION    assemblies_lock;
251
252         GHashTable         *method_rgctx_hash;
253
254         GHashTable         *generic_virtual_cases;
255         MonoThunkFreeList **thunk_free_lists;
256
257         /* Information maintained by the JIT engine */
258         gpointer runtime_info;
259
260         /*thread pool jobs, used to coordinate shutdown.*/
261         int                                     threadpool_jobs;
262         HANDLE                          cleanup_semaphore;
263         
264         /* Contains the compiled runtime invoke wrapper used by finalizers */
265         gpointer            finalize_runtime_invoke;
266
267         /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
268         gpointer            capture_context_runtime_invoke;
269
270         /* Contains the compiled method used by async resylt creation to capture thread context*/
271         gpointer            capture_context_method;
272 };
273
274 typedef struct  {
275         guint16 major, minor, build, revision;
276 } AssemblyVersionSet;
277
278 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
279 typedef struct  {
280         const char runtime_version [12];
281         const char framework_version [4];
282         const AssemblyVersionSet version_sets [2];
283 } MonoRuntimeInfo;
284
285 #define mono_domain_lock(domain) mono_locks_acquire(&(domain)->lock, DomainLock)
286 #define mono_domain_unlock(domain) mono_locks_release(&(domain)->lock, DomainLock)
287 #define mono_domain_assemblies_lock(domain) mono_locks_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
288 #define mono_domain_assemblies_unlock(domain) mono_locks_release(&(domain)->assemblies_lock, DomainAssembliesLock)
289 #define mono_domain_jit_code_hash_lock(domain) mono_locks_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
290 #define mono_domain_jit_code_hash_unlock(domain) mono_locks_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
291
292 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
293
294 void
295 mono_install_runtime_load  (MonoLoadFunc func) MONO_INTERNAL;
296
297 MonoDomain*
298 mono_runtime_load (const char *filename, const char *runtime_version) MONO_INTERNAL;
299
300 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
301
302 void
303 mono_install_create_domain_hook (MonoCreateDomainFunc func) MONO_INTERNAL;
304
305 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
306
307 void
308 mono_install_free_domain_hook (MonoFreeDomainFunc func) MONO_INTERNAL;
309
310 void 
311 mono_init_com_types (void) MONO_INTERNAL;
312
313 void 
314 mono_cleanup (void) MONO_INTERNAL;
315
316 void
317 mono_close_exe_image (void) MONO_INTERNAL;
318
319 void
320 mono_jit_info_table_add    (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
321
322 void
323 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
324
325 void
326 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end) MONO_INTERNAL;
327
328 MonoGenericJitInfo*
329 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji) MONO_INTERNAL;
330
331 MonoGenericSharingContext*
332 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji) MONO_INTERNAL;
333
334 void
335 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx) MONO_INTERNAL;
336
337 MonoJitInfo*
338 mono_domain_lookup_shared_generic (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
339
340 char *
341 mono_make_shadow_copy (const char *filename) MONO_INTERNAL;
342
343 gboolean
344 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name) MONO_INTERNAL;
345
346 gpointer
347 mono_domain_alloc  (MonoDomain *domain, guint size) MONO_INTERNAL;
348
349 gpointer
350 mono_domain_alloc0 (MonoDomain *domain, guint size) MONO_INTERNAL;
351
352 void*
353 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_INTERNAL;
354
355 void*
356 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment) MONO_INTERNAL;
357
358 void
359 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize) MONO_INTERNAL;
360
361 void
362 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data) MONO_INTERNAL;
363
364 void
365 mono_domain_unset (void) MONO_INTERNAL;
366
367 void
368 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception) MONO_INTERNAL;
369
370 /* 
371  * Installs a new function which is used to return a MonoJitInfo for a method inside
372  * an AOT module.
373  */
374 typedef MonoJitInfo *(*MonoJitInfoFindInAot)         (MonoDomain *domain, MonoImage *image, gpointer addr);
375 void          mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func) MONO_INTERNAL;
376
377 void
378 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash) MONO_INTERNAL;
379
380 MonoAppDomain *
381 ves_icall_System_AppDomain_getCurDomain            (void) MONO_INTERNAL;
382
383 MonoAppDomain *
384 ves_icall_System_AppDomain_getRootDomain           (void) MONO_INTERNAL;
385
386 MonoAppDomain *
387 ves_icall_System_AppDomain_createDomain            (MonoString         *friendly_name,
388                                                     MonoAppDomainSetup *setup) MONO_INTERNAL;
389
390 MonoObject *
391 ves_icall_System_AppDomain_GetData                 (MonoAppDomain *ad, 
392                                                     MonoString    *name) MONO_INTERNAL;
393
394 MonoReflectionAssembly *
395 ves_icall_System_AppDomain_LoadAssemblyRaw         (MonoAppDomain *ad,
396                                                     MonoArray *raw_assembly, 
397                                                     MonoArray *raw_symbol_store,
398                                                     MonoObject *evidence,
399                                                     MonoBoolean refonly) MONO_INTERNAL;
400
401 void
402 ves_icall_System_AppDomain_SetData                 (MonoAppDomain *ad, 
403                                                     MonoString    *name, 
404                                                     MonoObject    *data) MONO_INTERNAL;
405
406 MonoAppDomainSetup *
407 ves_icall_System_AppDomain_getSetup                (MonoAppDomain *ad) MONO_INTERNAL;
408
409 MonoString *
410 ves_icall_System_AppDomain_getFriendlyName         (MonoAppDomain *ad) MONO_INTERNAL;
411
412 MonoArray *
413 ves_icall_System_AppDomain_GetAssemblies           (MonoAppDomain *ad,
414                                                     MonoBoolean refonly) MONO_INTERNAL;
415
416 MonoReflectionAssembly *
417 ves_icall_System_Reflection_Assembly_LoadFrom      (MonoString *fname,
418                                                     MonoBoolean refonly) MONO_INTERNAL;
419
420 MonoReflectionAssembly *
421 ves_icall_System_AppDomain_LoadAssembly            (MonoAppDomain *ad, 
422                                                     MonoString *assRef,
423                                                     MonoObject    *evidence,
424                                                     MonoBoolean refonly) MONO_INTERNAL;
425
426 gboolean
427 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id) MONO_INTERNAL;
428
429 void
430 ves_icall_System_AppDomain_InternalUnload          (gint32 domain_id) MONO_INTERNAL;
431
432 gint32
433 ves_icall_System_AppDomain_ExecuteAssembly         (MonoAppDomain *ad, 
434                                                                                                         MonoReflectionAssembly *refass,
435                                                                                                         MonoArray     *args) MONO_INTERNAL;
436
437 MonoAppDomain * 
438 ves_icall_System_AppDomain_InternalSetDomain       (MonoAppDomain *ad) MONO_INTERNAL;
439
440 MonoAppDomain * 
441 ves_icall_System_AppDomain_InternalSetDomainByID   (gint32 domainid) MONO_INTERNAL;
442
443 void
444 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad) MONO_INTERNAL;
445
446 void
447 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id) MONO_INTERNAL;
448
449 void
450 ves_icall_System_AppDomain_InternalPopDomainRef (void) MONO_INTERNAL;
451
452 MonoAppContext * 
453 ves_icall_System_AppDomain_InternalGetContext      (void) MONO_INTERNAL;
454
455 MonoAppContext * 
456 ves_icall_System_AppDomain_InternalGetDefaultContext      (void) MONO_INTERNAL;
457
458 MonoAppContext * 
459 ves_icall_System_AppDomain_InternalSetContext      (MonoAppContext *mc) MONO_INTERNAL;
460
461 gint32 
462 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad) MONO_INTERNAL;
463
464 MonoString *
465 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid) MONO_INTERNAL;
466
467 MonoAssembly *
468 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status) MONO_INTERNAL;
469
470 const MonoRuntimeInfo*
471 mono_get_runtime_info (void) MONO_INTERNAL;
472
473 void
474 mono_runtime_set_no_exec (gboolean val) MONO_INTERNAL;
475
476 gboolean
477 mono_runtime_get_no_exec (void) MONO_INTERNAL;
478
479 gboolean
480 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname) MONO_INTERNAL;
481
482 MonoImage *mono_assembly_open_from_bundle (const char *filename,
483                                            MonoImageOpenStatus *status,
484                                            gboolean refonly) MONO_INTERNAL;
485
486 void
487 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
488
489 MonoReflectionAssembly *
490 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, gboolean refonly) MONO_INTERNAL;
491
492 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname, 
493                                                 const char       *basedir, 
494                                                 MonoImageOpenStatus *status,
495                                                 gboolean refonly) MONO_INTERNAL;
496
497 void mono_set_private_bin_path_from_config (MonoDomain *domain) MONO_INTERNAL;
498
499 int mono_framework_version (void) MONO_INTERNAL;
500
501 void mono_reflection_cleanup_domain (MonoDomain *domain) MONO_INTERNAL;
502
503 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */