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>
19 * If this is set, the memory belonging to appdomains is not freed when a domain is
20 * unloaded, and assemblies loaded by the appdomain are not unloaded either. This
21 * allows us to use typed gc in non-default appdomains too, leading to increased
24 extern gboolean mono_dont_free_domains;
26 /* This is a copy of System.AppDomainSetup */
29 MonoString *application_base;
30 MonoString *application_name;
31 MonoString *cache_path;
32 MonoString *configuration_file;
33 MonoString *dynamic_base;
34 MonoString *license_file;
35 MonoString *private_bin_path;
36 MonoString *private_bin_path_probe;
37 MonoString *shadow_copy_directories;
38 MonoString *shadow_copy_files;
39 MonoBoolean publisher_policy;
40 MonoBoolean path_changed;
41 int loader_optimization;
42 MonoBoolean disallow_binding_redirects;
43 MonoBoolean disallow_code_downloads;
44 MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
45 MonoObject *domain_initializer;
46 MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
47 MonoArray *domain_initializer_args;
48 MonoBoolean disallow_appbase_probe;
49 MonoArray *configuration_bytes;
50 MonoArray *serialized_non_primitives;
53 typedef struct _MonoJitInfoTable MonoJitInfoTable;
54 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
56 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE 64
58 struct _MonoJitInfoTableChunk
61 volatile int num_elements;
62 volatile gint8 *last_code_end;
63 MonoJitInfo * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
66 struct _MonoJitInfoTable
70 MonoJitInfoTableChunk *chunks [MONO_ZERO_LEN_ARRAY];
73 #define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
75 typedef GArray MonoAotModuleInfoTable;
82 gpointer handler_start;
84 * For LLVM compiled code, this is the index of the il clause
85 * associated with this handler.
90 uint32_t handler_offset;
93 MonoClass *catch_class;
97 } MonoJitExceptionInfo;
100 * Contains information about the type arguments for generic shared methods.
103 gboolean is_gsharedvt;
104 } MonoGenericSharingContext;
106 /* Simplified DWARF location list entry */
108 /* Whenever the value is in a register */
111 * If is_reg is TRUE, the register which contains the value. Otherwise
116 * If is_reg is FALSE, the offset of the stack location relative to 'reg'.
121 * Offsets of the PC interval where the value is in this location.
124 } MonoDwarfLocListEntry;
128 MonoGenericSharingContext *generic_sharing_context;
130 MonoDwarfLocListEntry *locations;
134 gboolean this_in_reg:1;
135 } MonoGenericJitInfo;
138 A try block hole is used to represent a non-contiguous part of
139 of a segment of native code protected by a given .try block.
140 Usually, a try block is defined as a contiguous segment of code.
141 But in some cases it's needed to have some parts of it to not be protected.
142 For example, given "try {} finally {}", the code in the .try block to call
143 the finally part looks like:
149 jump outside try block
155 The instructions between the call and the jump should not be under the try block since they happen
156 after the finally block executes, which means if an async exceptions happens at that point we would
157 execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
158 that those instructions are not protected.
165 } MonoTryBlockHoleJitInfo;
170 MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
171 } MonoTryBlockHoleTableJitInfo;
180 /* Relative to code_start */
187 JIT_INFO_HAS_GENERIC_JIT_INFO = (1 << 0),
188 JIT_INFO_HAS_TRY_BLOCK_HOLES = (1 << 1),
189 JIT_INFO_HAS_ARCH_EH_INFO = (1 << 2),
190 JIT_INFO_HAS_THUNK_INFO = (1 << 3)
193 struct _MonoJitInfo {
194 /* NOTE: These first two elements (method and
195 next_jit_code_hash) must be in the same order and at the
196 same offset as in RuntimeMethod, because of the jit_code_hash
197 internal hash table in MonoDomain. */
204 struct _MonoJitInfo *next_jit_code_hash;
208 guint32 num_clauses:15;
209 /* Whenever the code is domain neutral or 'shared' */
210 gboolean domain_neutral:1;
211 gboolean has_generic_jit_info:1;
212 gboolean has_try_block_holes:1;
213 gboolean has_arch_eh_info:1;
214 gboolean has_thunk_info:1;
216 gboolean from_llvm:1;
217 gboolean dbg_attrs_inited:1;
218 gboolean dbg_hidden:1;
219 /* Whenever this jit info was loaded in async context */
221 gboolean dbg_step_through:1;
222 gboolean dbg_non_user_code:1;
224 * Whenever this jit info refers to a trampoline.
225 * d.tramp_info contains additional data in this case.
227 gboolean is_trampoline:1;
229 /* FIXME: Embed this after the structure later*/
230 gpointer gc_info; /* Currently only used by SGen */
232 MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
233 /* There is an optional MonoGenericJitInfo after the clauses */
234 /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
235 /* There is an optional MonoArchEHJitInfo after MonoTryBlockHoleTableJitInfo */
236 /* There is an optional MonoThunkJitInfo after MonoArchEHJitInfo */
239 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
242 gpointer *static_data; /* Used to free the static data without going through the MonoAppContext object itself. */
246 struct _MonoAppContext {
250 gpointer *static_data;
251 ContextStaticData *data;
254 /* Lock-free allocator */
259 } LockFreeMempoolChunk;
262 LockFreeMempoolChunk *current, *chunks;
266 * We have two unloading states because the domain
267 * must remain fully functional while AppDomain::DomainUnload is
269 * After that unloading began and all domain facilities are teared down
270 * such as execution of new threadpool jobs.
273 MONO_APPDOMAIN_CREATED,
274 MONO_APPDOMAIN_UNLOADING_START,
275 MONO_APPDOMAIN_UNLOADING,
276 MONO_APPDOMAIN_UNLOADED
277 } MonoAppDomainState;
279 typedef struct _MonoThunkFreeList {
281 int length; /* only valid for the wait list */
282 struct _MonoThunkFreeList *next;
285 typedef struct _MonoJitCodeHash MonoJitCodeHash;
289 * This lock must never be taken before the loader lock,
290 * i.e. if both are taken by the same thread, the loader lock
295 MonoCodeManager *code_mp;
297 * keep all the managed objects close to each other for the precise GC
298 * For the Boehm GC we additionally keep close also other GC-tracked pointers.
300 #define MONO_DOMAIN_FIRST_OBJECT setup
301 MonoAppDomainSetup *setup;
302 MonoAppDomain *domain;
303 MonoAppContext *default_context;
304 MonoException *out_of_memory_ex;
305 MonoException *null_reference_ex;
306 MonoException *stack_overflow_ex;
308 MonoObject *typeof_void;
309 /* Ephemeron Tombstone*/
310 MonoObject *ephemeron_tombstone;
311 /* new MonoType [0] */
312 MonoArray *empty_types;
314 * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
315 * not object references.
317 #define MONO_DOMAIN_FIRST_GC_TRACKED env
319 MonoGHashTable *ldstr_table;
320 /* hashtables for Reflection handles */
321 MonoGHashTable *type_hash;
322 MonoGHashTable *refobject_hash;
323 /* a GC-tracked array to keep references to the static fields of types */
324 gpointer *static_data_array;
325 /* maps class -> type initialization exception object */
326 MonoGHashTable *type_init_exception_hash;
327 /* maps delegate trampoline addr -> delegate object */
328 MonoGHashTable *delegate_hash_table;
329 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
331 /* Needed by Thread:GetDomainID() */
333 gint32 shadow_serial;
334 unsigned char inet_family_hint; // used in socket-io.c as a cache
335 GSList *domain_assemblies;
336 MonoAssembly *entry_assembly;
338 GPtrArray *class_vtable_array;
339 /* maps remote class key -> MonoRemoteClass */
340 GHashTable *proxy_vtable_hash;
341 /* Protected by 'jit_code_hash_lock' */
342 MonoInternalHashTable jit_code_hash;
343 mono_mutex_t jit_code_hash_lock;
344 int num_jit_info_tables;
346 volatile jit_info_table;
348 * Contains information about AOT loaded code.
349 * Only used in the root domain.
352 volatile aot_modules;
353 GSList *jit_info_free_queue;
354 /* Used when loading assemblies */
356 gchar *private_bin_path;
357 LockFreeMempool *lock_free_mp;
359 /* Used by remoting proxies */
360 MonoMethod *create_proxy_for_type_method;
361 MonoMethod *private_invoke_method;
362 /* Used to store offsets of thread and context static fields */
363 GHashTable *special_static_fields;
365 * This must be a GHashTable, since these objects can't be finalized
366 * if the hashtable contains a GC visible reference to them.
368 GHashTable *finalizable_objects_hash;
370 /* Protects the three hashes above */
371 mono_mutex_t finalizable_objects_hash_lock;
372 /* Used when accessing 'domain_assemblies' */
373 mono_mutex_t assemblies_lock;
375 GHashTable *method_rgctx_hash;
377 GHashTable *generic_virtual_cases;
378 MonoThunkFreeList **thunk_free_lists;
380 GHashTable *generic_virtual_thunks;
382 /* Information maintained by the JIT engine */
383 gpointer runtime_info;
385 /*thread pool jobs, used to coordinate shutdown.*/
386 volatile int threadpool_jobs;
387 HANDLE cleanup_semaphore;
389 /* Contains the compiled runtime invoke wrapper used by finalizers */
390 gpointer finalize_runtime_invoke;
392 /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
393 gpointer capture_context_runtime_invoke;
395 /* Contains the compiled method used by async resylt creation to capture thread context*/
396 gpointer capture_context_method;
398 /* Assembly bindings, the per-domain part */
399 GSList *assembly_bindings;
400 gboolean assembly_bindings_parsed;
402 /* Used by socket-io.c */
403 /* These are domain specific, since the assembly can be unloaded */
404 MonoImage *socket_assembly;
405 MonoClass *sockaddr_class;
406 MonoClassField *sockaddr_data_field;
408 /* Cache function pointers for architectures */
409 /* that require wrappers */
410 GHashTable *ftnptrs_hash;
412 /* Maps MonoMethod* to weak links to DynamicMethod objects */
413 GHashTable *method_to_dyn_method;
415 guint32 execution_context_field_offset;
419 guint16 major, minor, build, revision;
420 } AssemblyVersionSet;
422 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
424 const char runtime_version [12];
425 const char framework_version [4];
426 const AssemblyVersionSet version_sets [4];
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)
434 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
436 void mono_domain_lock (MonoDomain *domain);
437 void mono_domain_unlock (MonoDomain *domain);
440 mono_install_runtime_load (MonoLoadFunc func);
443 mono_runtime_load (const char *filename, const char *runtime_version);
445 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
448 mono_install_create_domain_hook (MonoCreateDomainFunc func);
450 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
453 mono_install_free_domain_hook (MonoFreeDomainFunc func);
459 mono_close_exe_image (void);
462 mono_jit_info_size (MonoJitInfoFlags flags, int num_clauses, int num_holes);
465 mono_jit_info_init (MonoJitInfo *ji, MonoMethod *method, guint8 *code, int code_size,
466 MonoJitInfoFlags flags, int num_clauses, int num_holes);
469 mono_jit_info_table_new (MonoDomain *domain);
472 mono_jit_info_table_free (MonoJitInfoTable *table);
475 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji);
478 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji);
481 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end);
484 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji);
486 MonoGenericSharingContext*
487 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji);
490 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx);
493 mono_make_shadow_copy (const char *filename);
496 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name);
499 mono_domain_alloc (MonoDomain *domain, guint size);
502 mono_domain_alloc0 (MonoDomain *domain, guint size);
505 mono_domain_alloc0_lock_free (MonoDomain *domain, guint size);
508 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
511 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment);
514 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize);
517 nacl_domain_get_code_dest (MonoDomain *domain, void *data);
520 nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end);
523 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data);
526 mono_domain_unset (void);
529 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception);
531 MonoTryBlockHoleTableJitInfo*
532 mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji);
535 mono_jit_info_get_arch_eh_info (MonoJitInfo *ji);
538 mono_jit_info_get_thunk_info (MonoJitInfo *ji);
541 * Installs a new function which is used to return a MonoJitInfo for a method inside
544 typedef MonoJitInfo *(*MonoJitInfoFindInAot) (MonoDomain *domain, MonoImage *image, gpointer addr);
545 void mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func);
548 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash);
551 ves_icall_System_AppDomain_getCurDomain (void);
554 ves_icall_System_AppDomain_getRootDomain (void);
557 ves_icall_System_AppDomain_createDomain (MonoString *friendly_name,
558 MonoAppDomainSetup *setup);
561 ves_icall_System_AppDomain_GetData (MonoAppDomain *ad,
564 MonoReflectionAssembly *
565 ves_icall_System_AppDomain_LoadAssemblyRaw (MonoAppDomain *ad,
566 MonoArray *raw_assembly,
567 MonoArray *raw_symbol_store,
568 MonoObject *evidence,
569 MonoBoolean refonly);
572 ves_icall_System_AppDomain_SetData (MonoAppDomain *ad,
577 ves_icall_System_AppDomain_getSetup (MonoAppDomain *ad);
580 ves_icall_System_AppDomain_getFriendlyName (MonoAppDomain *ad);
583 ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad,
584 MonoBoolean refonly);
586 MonoReflectionAssembly *
587 ves_icall_System_Reflection_Assembly_LoadFrom (MonoString *fname,
588 MonoBoolean refonly);
590 MonoReflectionAssembly *
591 ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,
593 MonoObject *evidence,
594 MonoBoolean refonly);
597 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id);
600 ves_icall_System_AppDomain_InternalUnload (gint32 domain_id);
603 ves_icall_System_AppDomain_DoUnhandledException (MonoException *exc);
606 ves_icall_System_AppDomain_ExecuteAssembly (MonoAppDomain *ad,
607 MonoReflectionAssembly *refass,
611 ves_icall_System_AppDomain_InternalSetDomain (MonoAppDomain *ad);
614 ves_icall_System_AppDomain_InternalSetDomainByID (gint32 domainid);
617 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad);
620 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id);
623 ves_icall_System_AppDomain_InternalPopDomainRef (void);
626 ves_icall_System_AppDomain_InternalGetContext (void);
629 ves_icall_System_AppDomain_InternalGetDefaultContext (void);
632 ves_icall_System_AppDomain_InternalSetContext (MonoAppContext *mc);
635 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad);
638 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid);
641 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status);
643 const MonoRuntimeInfo*
644 mono_get_runtime_info (void);
647 mono_runtime_set_no_exec (gboolean val);
650 mono_runtime_get_no_exec (void);
653 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname);
655 MonoImage *mono_assembly_open_from_bundle (const char *filename,
656 MonoImageOpenStatus *status,
660 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
662 MonoReflectionAssembly *
663 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, MonoAssembly *requesting, gboolean refonly);
666 mono_domain_assembly_postload_search (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly);
668 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
670 MonoImageOpenStatus *status,
673 void mono_set_private_bin_path_from_config (MonoDomain *domain);
675 int mono_framework_version (void);
677 void mono_reflection_cleanup_domain (MonoDomain *domain);
679 void mono_assembly_cleanup_domain_bindings (guint32 domain_id);
681 MonoJitInfo* mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_aot, gboolean allow_trampolines);
683 void mono_enable_debug_domain_unload (gboolean enable);
685 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */