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/utils/mono-mutex.h>
13 #include <mono/metadata/mono-hash.h>
14 #include <mono/utils/mono-compiler.h>
15 #include <mono/utils/mono-internal-hash.h>
16 #include <mono/io-layer/io-layer.h>
17 #include <mono/metadata/mempool-internals.h>
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
25 extern gboolean mono_dont_free_domains;
27 /* This is a copy of System.AppDomainSetup */
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;
54 typedef struct _MonoJitInfoTable MonoJitInfoTable;
55 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
57 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE 64
59 struct _MonoJitInfoTableChunk
62 volatile int num_elements;
63 volatile gint8 *last_code_end;
64 MonoJitInfo * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
67 struct _MonoJitInfoTable
71 MonoJitInfoTableChunk *chunks [MONO_ZERO_LEN_ARRAY];
74 #define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
76 typedef GArray MonoAotModuleInfoTable;
83 gpointer handler_start;
85 * For LLVM compiled code, this is the index of the il clause
86 * associated with this handler.
90 MonoClass *catch_class;
94 } MonoJitExceptionInfo;
97 * Contains information about the type arguments for generic shared methods.
100 gboolean is_gsharedvt;
101 } MonoGenericSharingContext;
103 /* Simplified DWARF location list entry */
105 /* Whenever the value is in a register */
108 * If is_reg is TRUE, the register which contains the value. Otherwise
113 * If is_reg is FALSE, the offset of the stack location relative to 'reg'.
118 * Offsets of the PC interval where the value is in this location.
121 } MonoDwarfLocListEntry;
125 MonoGenericSharingContext *generic_sharing_context;
127 MonoDwarfLocListEntry *locations;
131 gboolean this_in_reg:1;
132 } MonoGenericJitInfo;
135 A try block hole is used to represent a non-contiguous part of
136 of a segment of native code protected by a given .try block.
137 Usually, a try block is defined as a contiguous segment of code.
138 But in some cases it's needed to have some parts of it to not be protected.
139 For example, given "try {} finally {}", the code in the .try block to call
140 the finally part looks like:
146 jump outside try block
152 The instructions between the call and the jump should not be under the try block since they happen
153 after the finally block executes, which means if an async exceptions happens at that point we would
154 execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
155 that those instructions are not protected.
162 } MonoTryBlockHoleJitInfo;
167 MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
168 } MonoTryBlockHoleTableJitInfo;
177 /* Relative to code_start */
184 JIT_INFO_HAS_GENERIC_JIT_INFO = (1 << 0),
185 JIT_INFO_HAS_TRY_BLOCK_HOLES = (1 << 1),
186 JIT_INFO_HAS_ARCH_EH_INFO = (1 << 2),
187 JIT_INFO_HAS_THUNK_INFO = (1 << 3)
190 struct _MonoJitInfo {
191 /* NOTE: These first two elements (method and
192 next_jit_code_hash) must be in the same order and at the
193 same offset as in RuntimeMethod, because of the jit_code_hash
194 internal hash table in MonoDomain. */
201 struct _MonoJitInfo *next_jit_code_hash;
205 guint32 num_clauses:15;
206 /* Whenever the code is domain neutral or 'shared' */
207 gboolean domain_neutral:1;
208 gboolean has_generic_jit_info:1;
209 gboolean has_try_block_holes:1;
210 gboolean has_arch_eh_info:1;
211 gboolean has_thunk_info:1;
213 gboolean from_llvm:1;
214 gboolean dbg_attrs_inited:1;
215 gboolean dbg_hidden:1;
216 /* Whenever this jit info was loaded in async context */
218 gboolean dbg_step_through:1;
219 gboolean dbg_non_user_code:1;
221 * Whenever this jit info refers to a trampoline.
222 * d.tramp_info contains additional data in this case.
224 gboolean is_trampoline:1;
226 /* FIXME: Embed this after the structure later*/
227 gpointer gc_info; /* Currently only used by SGen */
229 MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
230 /* There is an optional MonoGenericJitInfo after the clauses */
231 /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
232 /* There is an optional MonoArchEHJitInfo after MonoTryBlockHoleTableJitInfo */
233 /* There is an optional MonoThunkJitInfo after MonoArchEHJitInfo */
236 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
239 gpointer *static_data; /* Used to free the static data without going through the MonoAppContext object itself. */
243 struct _MonoAppContext {
247 gpointer *static_data;
248 ContextStaticData *data;
251 /* Lock-free allocator */
256 } LockFreeMempoolChunk;
259 LockFreeMempoolChunk *current, *chunks;
263 * We have two unloading states because the domain
264 * must remain fully functional while AppDomain::DomainUnload is
266 * After that unloading began and all domain facilities are teared down
267 * such as execution of new threadpool jobs.
270 MONO_APPDOMAIN_CREATED,
271 MONO_APPDOMAIN_UNLOADING_START,
272 MONO_APPDOMAIN_UNLOADING,
273 MONO_APPDOMAIN_UNLOADED
274 } MonoAppDomainState;
276 typedef struct _MonoThunkFreeList {
278 int length; /* only valid for the wait list */
279 struct _MonoThunkFreeList *next;
282 typedef struct _MonoJitCodeHash MonoJitCodeHash;
286 * This lock must never be taken before the loader lock,
287 * i.e. if both are taken by the same thread, the loader lock
292 MonoCodeManager *code_mp;
294 * keep all the managed objects close to each other for the precise GC
295 * For the Boehm GC we additionally keep close also other GC-tracked pointers.
297 #define MONO_DOMAIN_FIRST_OBJECT setup
298 MonoAppDomainSetup *setup;
299 MonoAppDomain *domain;
300 MonoAppContext *default_context;
301 MonoException *out_of_memory_ex;
302 MonoException *null_reference_ex;
303 MonoException *stack_overflow_ex;
305 MonoObject *typeof_void;
306 /* Ephemeron Tombstone*/
307 MonoObject *ephemeron_tombstone;
308 /* new MonoType [0] */
309 MonoArray *empty_types;
311 * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
312 * not object references.
314 #define MONO_DOMAIN_FIRST_GC_TRACKED env
316 MonoGHashTable *ldstr_table;
317 /* hashtables for Reflection handles */
318 MonoGHashTable *type_hash;
319 MonoGHashTable *refobject_hash;
320 /* a GC-tracked array to keep references to the static fields of types */
321 gpointer *static_data_array;
322 /* maps class -> type initialization exception object */
323 MonoGHashTable *type_init_exception_hash;
324 /* maps delegate trampoline addr -> delegate object */
325 MonoGHashTable *delegate_hash_table;
326 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
328 /* Needed by Thread:GetDomainID() */
330 gint32 shadow_serial;
331 unsigned char inet_family_hint; // used in socket-io.c as a cache
332 GSList *domain_assemblies;
333 MonoAssembly *entry_assembly;
335 GPtrArray *class_vtable_array;
336 /* maps remote class key -> MonoRemoteClass */
337 GHashTable *proxy_vtable_hash;
338 /* Protected by 'jit_code_hash_lock' */
339 MonoInternalHashTable jit_code_hash;
340 mono_mutex_t jit_code_hash_lock;
341 int num_jit_info_tables;
343 volatile jit_info_table;
345 * Contains information about AOT loaded code.
346 * Only used in the root domain.
349 volatile aot_modules;
350 GSList *jit_info_free_queue;
351 /* Used when loading assemblies */
353 gchar *private_bin_path;
354 LockFreeMempool *lock_free_mp;
356 /* Used by remoting proxies */
357 MonoMethod *create_proxy_for_type_method;
358 MonoMethod *private_invoke_method;
359 /* Used to store offsets of thread and context static fields */
360 GHashTable *special_static_fields;
362 * This must be a GHashTable, since these objects can't be finalized
363 * if the hashtable contains a GC visible reference to them.
365 GHashTable *finalizable_objects_hash;
367 /* Protects the three hashes above */
368 mono_mutex_t finalizable_objects_hash_lock;
369 /* Used when accessing 'domain_assemblies' */
370 mono_mutex_t assemblies_lock;
372 GHashTable *method_rgctx_hash;
374 GHashTable *generic_virtual_cases;
375 MonoThunkFreeList **thunk_free_lists;
377 GHashTable *generic_virtual_thunks;
379 /* Information maintained by the JIT engine */
380 gpointer runtime_info;
382 /*thread pool jobs, used to coordinate shutdown.*/
383 volatile int threadpool_jobs;
384 HANDLE cleanup_semaphore;
386 /* Contains the compiled runtime invoke wrapper used by finalizers */
387 gpointer finalize_runtime_invoke;
389 /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
390 gpointer capture_context_runtime_invoke;
392 /* Contains the compiled method used by async resylt creation to capture thread context*/
393 gpointer capture_context_method;
395 /* Assembly bindings, the per-domain part */
396 GSList *assembly_bindings;
397 gboolean assembly_bindings_parsed;
399 /* Used by socket-io.c */
400 /* These are domain specific, since the assembly can be unloaded */
401 MonoImage *socket_assembly;
402 MonoClass *sockaddr_class;
403 MonoClassField *sockaddr_data_field;
405 /* Cache function pointers for architectures */
406 /* that require wrappers */
407 GHashTable *ftnptrs_hash;
409 /* Maps MonoMethod* to weak links to DynamicMethod objects */
410 GHashTable *method_to_dyn_method;
412 guint32 execution_context_field_offset;
416 guint16 major, minor, build, revision;
417 } AssemblyVersionSet;
419 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
421 const char runtime_version [12];
422 const char framework_version [4];
423 const AssemblyVersionSet version_sets [4];
426 #define mono_domain_assemblies_lock(domain) mono_locks_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
427 #define mono_domain_assemblies_unlock(domain) mono_locks_release(&(domain)->assemblies_lock, DomainAssembliesLock)
428 #define mono_domain_jit_code_hash_lock(domain) mono_locks_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
429 #define mono_domain_jit_code_hash_unlock(domain) mono_locks_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
431 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
433 void mono_domain_lock (MonoDomain *domain);
434 void mono_domain_unlock (MonoDomain *domain);
437 mono_install_runtime_load (MonoLoadFunc func);
440 mono_runtime_load (const char *filename, const char *runtime_version);
442 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
445 mono_install_create_domain_hook (MonoCreateDomainFunc func);
447 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
450 mono_install_free_domain_hook (MonoFreeDomainFunc func);
456 mono_close_exe_image (void);
459 mono_jit_info_size (MonoJitInfoFlags flags, int num_clauses, int num_holes);
462 mono_jit_info_init (MonoJitInfo *ji, MonoMethod *method, guint8 *code, int code_size,
463 MonoJitInfoFlags flags, int num_clauses, int num_holes);
466 mono_jit_info_table_new (MonoDomain *domain);
469 mono_jit_info_table_free (MonoJitInfoTable *table);
472 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji);
475 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji);
478 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end);
481 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji);
483 MonoGenericSharingContext*
484 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji);
487 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx);
490 mono_make_shadow_copy (const char *filename);
493 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name);
496 mono_domain_alloc (MonoDomain *domain, guint size);
499 mono_domain_alloc0 (MonoDomain *domain, guint size);
502 mono_domain_alloc0_lock_free (MonoDomain *domain, guint size);
505 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
508 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment);
511 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize);
514 nacl_domain_get_code_dest (MonoDomain *domain, void *data);
517 nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end);
520 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data);
523 mono_domain_unset (void);
526 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception);
528 MonoTryBlockHoleTableJitInfo*
529 mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji);
532 mono_jit_info_get_arch_eh_info (MonoJitInfo *ji);
535 mono_jit_info_get_thunk_info (MonoJitInfo *ji);
538 * Installs a new function which is used to return a MonoJitInfo for a method inside
541 typedef MonoJitInfo *(*MonoJitInfoFindInAot) (MonoDomain *domain, MonoImage *image, gpointer addr);
542 void mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func);
545 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash);
548 ves_icall_System_AppDomain_getCurDomain (void);
551 ves_icall_System_AppDomain_getRootDomain (void);
554 ves_icall_System_AppDomain_createDomain (MonoString *friendly_name,
555 MonoAppDomainSetup *setup);
558 ves_icall_System_AppDomain_GetData (MonoAppDomain *ad,
561 MonoReflectionAssembly *
562 ves_icall_System_AppDomain_LoadAssemblyRaw (MonoAppDomain *ad,
563 MonoArray *raw_assembly,
564 MonoArray *raw_symbol_store,
565 MonoObject *evidence,
566 MonoBoolean refonly);
569 ves_icall_System_AppDomain_SetData (MonoAppDomain *ad,
574 ves_icall_System_AppDomain_getSetup (MonoAppDomain *ad);
577 ves_icall_System_AppDomain_getFriendlyName (MonoAppDomain *ad);
580 ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad,
581 MonoBoolean refonly);
583 MonoReflectionAssembly *
584 ves_icall_System_Reflection_Assembly_LoadFrom (MonoString *fname,
585 MonoBoolean refonly);
587 MonoReflectionAssembly *
588 ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,
590 MonoObject *evidence,
591 MonoBoolean refonly);
594 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id);
597 ves_icall_System_AppDomain_InternalUnload (gint32 domain_id);
600 ves_icall_System_AppDomain_DoUnhandledException (MonoException *exc);
603 ves_icall_System_AppDomain_ExecuteAssembly (MonoAppDomain *ad,
604 MonoReflectionAssembly *refass,
608 ves_icall_System_AppDomain_InternalSetDomain (MonoAppDomain *ad);
611 ves_icall_System_AppDomain_InternalSetDomainByID (gint32 domainid);
614 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad);
617 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id);
620 ves_icall_System_AppDomain_InternalPopDomainRef (void);
623 ves_icall_System_AppDomain_InternalGetContext (void);
626 ves_icall_System_AppDomain_InternalGetDefaultContext (void);
629 ves_icall_System_AppDomain_InternalSetContext (MonoAppContext *mc);
632 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad);
635 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid);
638 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status);
640 const MonoRuntimeInfo*
641 mono_get_runtime_info (void);
644 mono_runtime_set_no_exec (gboolean val);
647 mono_runtime_get_no_exec (void);
650 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname);
652 MonoImage *mono_assembly_open_from_bundle (const char *filename,
653 MonoImageOpenStatus *status,
657 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
659 MonoReflectionAssembly *
660 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, MonoAssembly *requesting, gboolean refonly);
663 mono_domain_assembly_postload_search (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly);
665 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
667 MonoImageOpenStatus *status,
670 void mono_set_private_bin_path_from_config (MonoDomain *domain);
672 int mono_framework_version (void);
674 void mono_reflection_cleanup_domain (MonoDomain *domain);
676 void mono_assembly_cleanup_domain_bindings (guint32 domain_id);
678 MonoJitInfo* mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_aot, gboolean allow_trampolines);
680 void mono_enable_debug_domain_unload (gboolean enable);
682 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */