2 * Appdomain-related internal data structures and functions.
3 * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
5 #ifndef __MONO_METADATA_DOMAIN_INTERNALS_H__
6 #define __MONO_METADATA_DOMAIN_INTERNALS_H__
8 #include <mono/metadata/appdomain.h>
9 #include <mono/metadata/mempool.h>
10 #include <mono/metadata/lock-tracer.h>
11 #include <mono/utils/mono-codeman.h>
12 #include <mono/metadata/mono-hash.h>
13 #include <mono/utils/mono-compiler.h>
14 #include <mono/utils/mono-internal-hash.h>
15 #include <mono/io-layer/io-layer.h>
16 #include <mono/metadata/mempool-internals.h>
18 extern CRITICAL_SECTION mono_delegate_section;
19 extern CRITICAL_SECTION mono_strtod_mutex;
22 * If this is set, the memory belonging to appdomains is not freed when a domain is
23 * unloaded, and assemblies loaded by the appdomain are not unloaded either. This
24 * allows us to use typed gc in non-default appdomains too, leading to increased
27 extern gboolean mono_dont_free_domains;
29 /* This is a copy of System.AppDomainSetup */
32 MonoString *application_base;
33 MonoString *application_name;
34 MonoString *cache_path;
35 MonoString *configuration_file;
36 MonoString *dynamic_base;
37 MonoString *license_file;
38 MonoString *private_bin_path;
39 MonoString *private_bin_path_probe;
40 MonoString *shadow_copy_directories;
41 MonoString *shadow_copy_files;
42 MonoBoolean publisher_policy;
43 MonoBoolean path_changed;
44 int loader_optimization;
45 MonoBoolean disallow_binding_redirects;
46 MonoBoolean disallow_code_downloads;
47 MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
48 MonoObject *domain_initializer;
49 MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
50 MonoArray *domain_initializer_args;
51 MonoBoolean disallow_appbase_probe;
52 MonoArray *configuration_bytes;
53 MonoArray *serialized_non_primitives;
56 typedef struct _MonoJitInfoTable MonoJitInfoTable;
57 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
59 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE 64
61 struct _MonoJitInfoTableChunk
64 volatile int num_elements;
65 volatile gint8 *last_code_end;
66 MonoJitInfo * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
69 struct _MonoJitInfoTable
73 MonoJitInfoTableChunk *chunks [MONO_ZERO_LEN_ARRAY];
76 #define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
78 typedef GArray MonoAotModuleInfoTable;
85 gpointer handler_start;
87 MonoClass *catch_class;
91 } MonoJitExceptionInfo;
94 * Will contain information on the generic type arguments in the
95 * future. For now, all arguments are always reference types.
99 } MonoGenericSharingContext;
101 /* Simplified DWARF location list entry */
103 /* Whenever the value is in a register */
106 * If is_reg is TRUE, the register which contains the value. Otherwise
111 * If is_reg is FALSE, the offset of the stack location relative to 'reg'.
116 * Offsets of the PC interval where the value is in this location.
119 } MonoDwarfLocListEntry;
123 MonoGenericSharingContext *generic_sharing_context;
125 MonoDwarfLocListEntry *locations;
129 gboolean this_in_reg:1;
130 } MonoGenericJitInfo;
133 A try block hole is used to represent a non-contiguous part of
134 of a segment of native code protected by a given .try block.
135 Usually, a try block is defined as a contiguous segment of code.
136 But in some cases it's needed to have some parts of it to not be protected.
137 For example, given "try {} finally {}", the code in the .try block to call
138 the finally part looks like:
144 jump outside try block
150 The instructions between the call and the jump should not be under the try block since they happen
151 after the finally block executes, which means if an async exceptions happens at that point we would
152 execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
153 that those instructions are not protected.
160 } MonoTryBlockHoleJitInfo;
165 MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
166 } MonoTryBlockHoleTableJitInfo;
173 struct _MonoJitInfo {
174 /* NOTE: These first two elements (method and
175 next_jit_code_hash) must be in the same order and at the
176 same offset as in RuntimeMethod, because of the jit_code_hash
177 internal hash table in MonoDomain. */
179 struct _MonoJitInfo *next_jit_code_hash;
181 /* This might contain an id for the unwind info instead of a register mask */
184 guint32 num_clauses:15;
185 /* Whenever the code is domain neutral or 'shared' */
186 gboolean domain_neutral:1;
187 gboolean cas_inited:1;
188 gboolean cas_class_assert:1;
189 gboolean cas_class_deny:1;
190 gboolean cas_class_permitonly:1;
191 gboolean cas_method_assert:1;
192 gboolean cas_method_deny:1;
193 gboolean cas_method_permitonly:1;
194 gboolean has_generic_jit_info:1;
195 gboolean has_try_block_holes:1;
196 gboolean has_arch_eh_info:1;
198 gboolean from_llvm:1;
200 /* FIXME: Embed this after the structure later*/
201 gpointer gc_info; /* Currently only used by SGen */
203 MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
204 /* There is an optional MonoGenericJitInfo after the clauses */
205 /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
206 /* There is an optional MonoArchEHJitInfo after MonoTryBlockHoleTableJitInfo */
209 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
211 struct _MonoAppContext {
215 gpointer *static_data;
219 * We have two unloading states because the domain
220 * must remain fully functional while AppDomain::DomainUnload is
222 * After that unloading began and all domain facilities are teared down
223 * such as execution of new threadpool jobs.
226 MONO_APPDOMAIN_CREATED,
227 MONO_APPDOMAIN_UNLOADING_START,
228 MONO_APPDOMAIN_UNLOADING,
229 MONO_APPDOMAIN_UNLOADED
230 } MonoAppDomainState;
232 typedef struct _MonoThunkFreeList {
234 int length; /* only valid for the wait list */
235 struct _MonoThunkFreeList *next;
238 typedef struct _MonoJitCodeHash MonoJitCodeHash;
240 typedef struct _MonoTlsDataRecord MonoTlsDataRecord;
241 struct _MonoTlsDataRecord {
242 MonoTlsDataRecord *next;
249 * This lock must never be taken before the loader lock,
250 * i.e. if both are taken by the same thread, the loader lock
253 CRITICAL_SECTION lock;
255 MonoCodeManager *code_mp;
257 * keep all the managed objects close to each other for the precise GC
258 * For the Boehm GC we additionally keep close also other GC-tracked pointers.
260 #define MONO_DOMAIN_FIRST_OBJECT setup
261 MonoAppDomainSetup *setup;
262 MonoAppDomain *domain;
263 MonoAppContext *default_context;
264 MonoException *out_of_memory_ex;
265 MonoException *null_reference_ex;
266 MonoException *stack_overflow_ex;
268 MonoObject *typeof_void;
269 /* Ephemeron Tombstone*/
270 MonoObject *ephemeron_tombstone;
271 /* new MonoType [0] */
272 MonoArray *empty_types;
274 * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
275 * not object references.
277 #define MONO_DOMAIN_FIRST_GC_TRACKED env
279 MonoGHashTable *ldstr_table;
280 /* hashtables for Reflection handles */
281 MonoGHashTable *type_hash;
282 MonoGHashTable *refobject_hash;
283 /* a GC-tracked array to keep references to the static fields of types */
284 gpointer *static_data_array;
285 /* maps class -> type initialization exception object */
286 MonoGHashTable *type_init_exception_hash;
287 /* maps delegate trampoline addr -> delegate object */
288 MonoGHashTable *delegate_hash_table;
289 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
291 /* Needed by Thread:GetDomainID() */
293 gint32 shadow_serial;
294 unsigned char inet_family_hint; // used in socket-io.c as a cache
295 GSList *domain_assemblies;
296 MonoAssembly *entry_assembly;
298 GPtrArray *class_vtable_array;
299 /* maps remote class key -> MonoRemoteClass */
300 GHashTable *proxy_vtable_hash;
301 /* Protected by 'jit_code_hash_lock' */
302 MonoInternalHashTable jit_code_hash;
303 CRITICAL_SECTION jit_code_hash_lock;
304 int num_jit_info_tables;
306 volatile jit_info_table;
307 GSList *jit_info_free_queue;
308 /* Used when loading assemblies */
310 gchar *private_bin_path;
312 /* Used by remoting proxies */
313 MonoMethod *create_proxy_for_type_method;
314 MonoMethod *private_invoke_method;
315 /* Used to store offsets of thread and context static fields */
316 GHashTable *special_static_fields;
317 MonoTlsDataRecord *tlsrec_list;
319 * This must be a GHashTable, since these objects can't be finalized
320 * if the hashtable contains a GC visible reference to them.
322 GHashTable *finalizable_objects_hash;
324 /* These two are boehm only */
325 /* Maps MonoObjects to a GSList of WeakTrackResurrection GCHandles pointing to them */
326 GHashTable *track_resurrection_objects_hash;
327 /* Maps WeakTrackResurrection GCHandles to the MonoObjects they point to */
328 GHashTable *track_resurrection_handles_hash;
330 /* Protects the three hashes above */
331 CRITICAL_SECTION finalizable_objects_hash_lock;
332 /* Used when accessing 'domain_assemblies' */
333 CRITICAL_SECTION assemblies_lock;
335 GHashTable *method_rgctx_hash;
337 GHashTable *generic_virtual_cases;
338 MonoThunkFreeList **thunk_free_lists;
340 GHashTable *generic_virtual_thunks;
342 /* Information maintained by the JIT engine */
343 gpointer runtime_info;
345 /*thread pool jobs, used to coordinate shutdown.*/
346 volatile int threadpool_jobs;
347 HANDLE cleanup_semaphore;
349 /* Contains the compiled runtime invoke wrapper used by finalizers */
350 gpointer finalize_runtime_invoke;
352 /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
353 gpointer capture_context_runtime_invoke;
355 /* Contains the compiled method used by async resylt creation to capture thread context*/
356 gpointer capture_context_method;
358 /* Assembly bindings, the per-domain part */
359 GSList *assembly_bindings;
360 gboolean assembly_bindings_parsed;
362 /* Used by socket-io.c */
363 /* These are domain specific, since the assembly can be unloaded */
364 MonoImage *socket_assembly;
365 MonoClass *sockaddr_class;
366 MonoClassField *sockaddr_data_field;
368 /* Used by threadpool.c */
369 MonoImage *system_image;
370 MonoImage *system_net_dll;
371 MonoClass *corlib_asyncresult_class;
372 MonoClass *socket_class;
373 MonoClass *ad_unloaded_ex_class;
374 MonoClass *process_class;
376 /* Cache function pointers for architectures */
377 /* that require wrappers */
378 GHashTable *ftnptrs_hash;
380 guint32 execution_context_field_offset;
384 guint16 major, minor, build, revision;
385 } AssemblyVersionSet;
387 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
389 const char runtime_version [12];
390 const char framework_version [4];
391 const AssemblyVersionSet version_sets [3];
394 #define mono_domain_lock(domain) mono_locks_acquire(&(domain)->lock, DomainLock)
395 #define mono_domain_unlock(domain) mono_locks_release(&(domain)->lock, DomainLock)
396 #define mono_domain_assemblies_lock(domain) mono_locks_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
397 #define mono_domain_assemblies_unlock(domain) mono_locks_release(&(domain)->assemblies_lock, DomainAssembliesLock)
398 #define mono_domain_jit_code_hash_lock(domain) mono_locks_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
399 #define mono_domain_jit_code_hash_unlock(domain) mono_locks_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
401 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
404 mono_install_runtime_load (MonoLoadFunc func) MONO_INTERNAL;
407 mono_runtime_load (const char *filename, const char *runtime_version) MONO_INTERNAL;
409 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
412 mono_install_create_domain_hook (MonoCreateDomainFunc func) MONO_INTERNAL;
414 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
417 mono_install_free_domain_hook (MonoFreeDomainFunc func) MONO_INTERNAL;
420 mono_init_com_types (void) MONO_INTERNAL;
423 mono_cleanup (void) MONO_INTERNAL;
426 mono_close_exe_image (void) MONO_INTERNAL;
429 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
432 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
435 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end) MONO_INTERNAL;
438 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji) MONO_INTERNAL;
440 MonoGenericSharingContext*
441 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji) MONO_INTERNAL;
444 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx) MONO_INTERNAL;
447 mono_domain_lookup_shared_generic (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
450 mono_make_shadow_copy (const char *filename) MONO_INTERNAL;
453 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name) MONO_INTERNAL;
456 mono_domain_alloc (MonoDomain *domain, guint size) MONO_INTERNAL;
459 mono_domain_alloc0 (MonoDomain *domain, guint size) MONO_INTERNAL;
462 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
465 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment) MONO_INTERNAL;
468 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize) MONO_INTERNAL;
471 nacl_domain_get_code_dest (MonoDomain *domain, void *data) MONO_INTERNAL;
474 nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end) MONO_INTERNAL;
477 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data) MONO_INTERNAL;
480 mono_domain_unset (void) MONO_INTERNAL;
483 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception) MONO_INTERNAL;
485 MonoTryBlockHoleTableJitInfo*
486 mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji) MONO_INTERNAL;
489 mono_jit_info_get_arch_eh_info (MonoJitInfo *ji) MONO_INTERNAL;
492 * Installs a new function which is used to return a MonoJitInfo for a method inside
495 typedef MonoJitInfo *(*MonoJitInfoFindInAot) (MonoDomain *domain, MonoImage *image, gpointer addr);
496 void mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func) MONO_INTERNAL;
499 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash) MONO_INTERNAL;
502 ves_icall_System_AppDomain_getCurDomain (void) MONO_INTERNAL;
505 ves_icall_System_AppDomain_getRootDomain (void) MONO_INTERNAL;
508 ves_icall_System_AppDomain_createDomain (MonoString *friendly_name,
509 MonoAppDomainSetup *setup) MONO_INTERNAL;
512 ves_icall_System_AppDomain_GetData (MonoAppDomain *ad,
513 MonoString *name) MONO_INTERNAL;
515 MonoReflectionAssembly *
516 ves_icall_System_AppDomain_LoadAssemblyRaw (MonoAppDomain *ad,
517 MonoArray *raw_assembly,
518 MonoArray *raw_symbol_store,
519 MonoObject *evidence,
520 MonoBoolean refonly) MONO_INTERNAL;
523 ves_icall_System_AppDomain_SetData (MonoAppDomain *ad,
525 MonoObject *data) MONO_INTERNAL;
528 ves_icall_System_AppDomain_getSetup (MonoAppDomain *ad) MONO_INTERNAL;
531 ves_icall_System_AppDomain_getFriendlyName (MonoAppDomain *ad) MONO_INTERNAL;
534 ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad,
535 MonoBoolean refonly) MONO_INTERNAL;
537 MonoReflectionAssembly *
538 ves_icall_System_Reflection_Assembly_LoadFrom (MonoString *fname,
539 MonoBoolean refonly) MONO_INTERNAL;
541 MonoReflectionAssembly *
542 ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,
544 MonoObject *evidence,
545 MonoBoolean refonly) MONO_INTERNAL;
548 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id) MONO_INTERNAL;
551 ves_icall_System_AppDomain_InternalUnload (gint32 domain_id) MONO_INTERNAL;
554 ves_icall_System_AppDomain_ExecuteAssembly (MonoAppDomain *ad,
555 MonoReflectionAssembly *refass,
556 MonoArray *args) MONO_INTERNAL;
559 ves_icall_System_AppDomain_InternalSetDomain (MonoAppDomain *ad) MONO_INTERNAL;
562 ves_icall_System_AppDomain_InternalSetDomainByID (gint32 domainid) MONO_INTERNAL;
565 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad) MONO_INTERNAL;
568 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id) MONO_INTERNAL;
571 ves_icall_System_AppDomain_InternalPopDomainRef (void) MONO_INTERNAL;
574 ves_icall_System_AppDomain_InternalGetContext (void) MONO_INTERNAL;
577 ves_icall_System_AppDomain_InternalGetDefaultContext (void) MONO_INTERNAL;
580 ves_icall_System_AppDomain_InternalSetContext (MonoAppContext *mc) MONO_INTERNAL;
583 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad) MONO_INTERNAL;
586 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid) MONO_INTERNAL;
589 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status) MONO_INTERNAL;
591 const MonoRuntimeInfo*
592 mono_get_runtime_info (void) MONO_INTERNAL;
595 mono_runtime_set_no_exec (gboolean val) MONO_INTERNAL;
598 mono_runtime_get_no_exec (void) MONO_INTERNAL;
601 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname) MONO_INTERNAL;
603 MonoImage *mono_assembly_open_from_bundle (const char *filename,
604 MonoImageOpenStatus *status,
605 gboolean refonly) MONO_INTERNAL;
608 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
610 MonoReflectionAssembly *
611 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, gboolean refonly) MONO_INTERNAL;
613 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
615 MonoImageOpenStatus *status,
616 gboolean refonly) MONO_INTERNAL;
618 void mono_set_private_bin_path_from_config (MonoDomain *domain) MONO_INTERNAL;
620 int mono_framework_version (void) MONO_INTERNAL;
622 void mono_reflection_cleanup_domain (MonoDomain *domain) MONO_INTERNAL;
624 void mono_assembly_cleanup_domain_bindings (guint32 domain_id) MONO_INTERNAL;;
626 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */