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 extern mono_mutex_t mono_delegate_section;
23 * If this is set, the memory belonging to appdomains is not freed when a domain is
24 * unloaded, and assemblies loaded by the appdomain are not unloaded either. This
25 * allows us to use typed gc in non-default appdomains too, leading to increased
28 extern gboolean mono_dont_free_domains;
30 /* This is a copy of System.AppDomainSetup */
33 MonoString *application_base;
34 MonoString *application_name;
35 MonoString *cache_path;
36 MonoString *configuration_file;
37 MonoString *dynamic_base;
38 MonoString *license_file;
39 MonoString *private_bin_path;
40 MonoString *private_bin_path_probe;
41 MonoString *shadow_copy_directories;
42 MonoString *shadow_copy_files;
43 MonoBoolean publisher_policy;
44 MonoBoolean path_changed;
45 int loader_optimization;
46 MonoBoolean disallow_binding_redirects;
47 MonoBoolean disallow_code_downloads;
48 MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
49 MonoObject *domain_initializer;
50 MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
51 MonoArray *domain_initializer_args;
52 MonoBoolean disallow_appbase_probe;
53 MonoArray *configuration_bytes;
54 MonoArray *serialized_non_primitives;
57 typedef struct _MonoJitInfoTable MonoJitInfoTable;
58 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
60 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE 64
62 struct _MonoJitInfoTableChunk
65 volatile int num_elements;
66 volatile gint8 *last_code_end;
67 MonoJitInfo * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
70 struct _MonoJitInfoTable
74 MonoJitInfoTableChunk *chunks [MONO_ZERO_LEN_ARRAY];
77 #define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
79 typedef GArray MonoAotModuleInfoTable;
86 gpointer handler_start;
88 * For LLVM compiled code, this is the index of the il clause
89 * associated with this handler.
93 MonoClass *catch_class;
97 } MonoJitExceptionInfo;
100 * Contains information about the type arguments for generic shared methods.
104 * If not NULL, determines whenever the class type arguments of the gshared method are references or vtypes.
105 * The array length is equal to class_inst->type_argv.
108 /* Same for method type parameters */
109 gboolean *mvar_is_vt;
110 } MonoGenericSharingContext;
112 /* Simplified DWARF location list entry */
114 /* Whenever the value is in a register */
117 * If is_reg is TRUE, the register which contains the value. Otherwise
122 * If is_reg is FALSE, the offset of the stack location relative to 'reg'.
127 * Offsets of the PC interval where the value is in this location.
130 } MonoDwarfLocListEntry;
134 MonoGenericSharingContext *generic_sharing_context;
136 MonoDwarfLocListEntry *locations;
140 gboolean this_in_reg:1;
141 } MonoGenericJitInfo;
144 A try block hole is used to represent a non-contiguous part of
145 of a segment of native code protected by a given .try block.
146 Usually, a try block is defined as a contiguous segment of code.
147 But in some cases it's needed to have some parts of it to not be protected.
148 For example, given "try {} finally {}", the code in the .try block to call
149 the finally part looks like:
155 jump outside try block
161 The instructions between the call and the jump should not be under the try block since they happen
162 after the finally block executes, which means if an async exceptions happens at that point we would
163 execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
164 that those instructions are not protected.
171 } MonoTryBlockHoleJitInfo;
176 MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
177 } MonoTryBlockHoleTableJitInfo;
186 /* Relative to code_start */
193 JIT_INFO_HAS_GENERIC_JIT_INFO = (1 << 0),
194 JIT_INFO_HAS_TRY_BLOCK_HOLES = (1 << 1),
195 JIT_INFO_HAS_ARCH_EH_INFO = (1 << 2),
196 JIT_INFO_HAS_THUNK_INFO = (1 << 3)
199 struct _MonoJitInfo {
200 /* NOTE: These first two elements (method and
201 next_jit_code_hash) must be in the same order and at the
202 same offset as in RuntimeMethod, because of the jit_code_hash
203 internal hash table in MonoDomain. */
210 struct _MonoJitInfo *next_jit_code_hash;
214 guint32 num_clauses:15;
215 /* Whenever the code is domain neutral or 'shared' */
216 gboolean domain_neutral:1;
217 gboolean has_generic_jit_info:1;
218 gboolean has_try_block_holes:1;
219 gboolean has_arch_eh_info:1;
220 gboolean has_thunk_info:1;
222 gboolean from_llvm:1;
223 gboolean dbg_attrs_inited:1;
224 gboolean dbg_hidden:1;
225 /* Whenever this jit info was loaded in async context */
227 gboolean dbg_step_through:1;
228 gboolean dbg_non_user_code:1;
230 * Whenever this jit info refers to a trampoline.
231 * d.tramp_info contains additional data in this case.
233 gboolean is_trampoline:1;
235 /* FIXME: Embed this after the structure later*/
236 gpointer gc_info; /* Currently only used by SGen */
238 MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
239 /* There is an optional MonoGenericJitInfo after the clauses */
240 /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
241 /* There is an optional MonoArchEHJitInfo after MonoTryBlockHoleTableJitInfo */
242 /* There is an optional MonoThunkJitInfo after MonoArchEHJitInfo */
245 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
247 struct _MonoAppContext {
251 gpointer *static_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;
287 typedef struct _MonoTlsDataRecord MonoTlsDataRecord;
288 struct _MonoTlsDataRecord {
289 MonoTlsDataRecord *next;
296 * This lock must never be taken before the loader lock,
297 * i.e. if both are taken by the same thread, the loader lock
302 MonoCodeManager *code_mp;
304 * keep all the managed objects close to each other for the precise GC
305 * For the Boehm GC we additionally keep close also other GC-tracked pointers.
307 #define MONO_DOMAIN_FIRST_OBJECT setup
308 MonoAppDomainSetup *setup;
309 MonoAppDomain *domain;
310 MonoAppContext *default_context;
311 MonoException *out_of_memory_ex;
312 MonoException *null_reference_ex;
313 MonoException *stack_overflow_ex;
315 MonoObject *typeof_void;
316 /* Ephemeron Tombstone*/
317 MonoObject *ephemeron_tombstone;
318 /* new MonoType [0] */
319 MonoArray *empty_types;
321 * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
322 * not object references.
324 #define MONO_DOMAIN_FIRST_GC_TRACKED env
326 MonoGHashTable *ldstr_table;
327 /* hashtables for Reflection handles */
328 MonoGHashTable *type_hash;
329 MonoGHashTable *refobject_hash;
330 /* a GC-tracked array to keep references to the static fields of types */
331 gpointer *static_data_array;
332 /* maps class -> type initialization exception object */
333 MonoGHashTable *type_init_exception_hash;
334 /* maps delegate trampoline addr -> delegate object */
335 MonoGHashTable *delegate_hash_table;
336 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
338 /* Needed by Thread:GetDomainID() */
340 gint32 shadow_serial;
341 unsigned char inet_family_hint; // used in socket-io.c as a cache
342 GSList *domain_assemblies;
343 MonoAssembly *entry_assembly;
345 GPtrArray *class_vtable_array;
346 /* maps remote class key -> MonoRemoteClass */
347 GHashTable *proxy_vtable_hash;
348 /* Protected by 'jit_code_hash_lock' */
349 MonoInternalHashTable jit_code_hash;
350 mono_mutex_t jit_code_hash_lock;
351 int num_jit_info_tables;
353 volatile jit_info_table;
355 * Contains information about AOT loaded code.
356 * Only used in the root domain.
359 volatile aot_modules;
360 GSList *jit_info_free_queue;
361 /* Used when loading assemblies */
363 gchar *private_bin_path;
364 LockFreeMempool *lock_free_mp;
366 /* Used by remoting proxies */
367 MonoMethod *create_proxy_for_type_method;
368 MonoMethod *private_invoke_method;
369 /* Used to store offsets of thread and context static fields */
370 GHashTable *special_static_fields;
371 MonoTlsDataRecord *tlsrec_list;
373 * This must be a GHashTable, since these objects can't be finalized
374 * if the hashtable contains a GC visible reference to them.
376 GHashTable *finalizable_objects_hash;
378 /* Protects the three hashes above */
379 mono_mutex_t finalizable_objects_hash_lock;
380 /* Used when accessing 'domain_assemblies' */
381 mono_mutex_t assemblies_lock;
383 GHashTable *method_rgctx_hash;
385 GHashTable *generic_virtual_cases;
386 MonoThunkFreeList **thunk_free_lists;
388 GHashTable *generic_virtual_thunks;
390 /* Information maintained by the JIT engine */
391 gpointer runtime_info;
393 /*thread pool jobs, used to coordinate shutdown.*/
394 volatile int threadpool_jobs;
395 HANDLE cleanup_semaphore;
397 /* Contains the compiled runtime invoke wrapper used by finalizers */
398 gpointer finalize_runtime_invoke;
400 /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
401 gpointer capture_context_runtime_invoke;
403 /* Contains the compiled method used by async resylt creation to capture thread context*/
404 gpointer capture_context_method;
406 /* Assembly bindings, the per-domain part */
407 GSList *assembly_bindings;
408 gboolean assembly_bindings_parsed;
410 /* Used by socket-io.c */
411 /* These are domain specific, since the assembly can be unloaded */
412 MonoImage *socket_assembly;
413 MonoClass *sockaddr_class;
414 MonoClassField *sockaddr_data_field;
416 /* Used by threadpool.c */
417 MonoImage *system_image;
418 MonoClass *corlib_asyncresult_class;
419 MonoClass *socket_class;
420 MonoClass *ad_unloaded_ex_class;
421 MonoClass *process_class;
423 /* Cache function pointers for architectures */
424 /* that require wrappers */
425 GHashTable *ftnptrs_hash;
427 guint32 execution_context_field_offset;
431 guint16 major, minor, build, revision;
432 } AssemblyVersionSet;
434 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
436 const char runtime_version [12];
437 const char framework_version [4];
438 const AssemblyVersionSet version_sets [4];
441 #define mono_domain_assemblies_lock(domain) mono_locks_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
442 #define mono_domain_assemblies_unlock(domain) mono_locks_release(&(domain)->assemblies_lock, DomainAssembliesLock)
443 #define mono_domain_jit_code_hash_lock(domain) mono_locks_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
444 #define mono_domain_jit_code_hash_unlock(domain) mono_locks_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
446 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
448 void mono_domain_lock (MonoDomain *domain);
449 void mono_domain_unlock (MonoDomain *domain);
452 mono_install_runtime_load (MonoLoadFunc func);
455 mono_runtime_load (const char *filename, const char *runtime_version);
457 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
460 mono_install_create_domain_hook (MonoCreateDomainFunc func);
462 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
465 mono_install_free_domain_hook (MonoFreeDomainFunc func);
471 mono_close_exe_image (void);
474 mono_jit_info_size (MonoJitInfoFlags flags, int num_clauses, int num_holes);
477 mono_jit_info_init (MonoJitInfo *ji, MonoMethod *method, guint8 *code, int code_size,
478 MonoJitInfoFlags flags, int num_clauses, int num_holes);
481 mono_jit_info_table_new (MonoDomain *domain);
484 mono_jit_info_table_free (MonoJitInfoTable *table);
487 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji);
490 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji);
493 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end);
496 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji);
498 MonoGenericSharingContext*
499 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji);
502 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx);
505 mono_make_shadow_copy (const char *filename);
508 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name);
511 mono_domain_alloc (MonoDomain *domain, guint size);
514 mono_domain_alloc0 (MonoDomain *domain, guint size);
517 mono_domain_alloc0_lock_free (MonoDomain *domain, guint size);
520 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
523 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment);
526 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize);
529 nacl_domain_get_code_dest (MonoDomain *domain, void *data);
532 nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end);
535 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data);
538 mono_domain_unset (void);
541 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception);
543 MonoTryBlockHoleTableJitInfo*
544 mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji);
547 mono_jit_info_get_arch_eh_info (MonoJitInfo *ji);
550 mono_jit_info_get_thunk_info (MonoJitInfo *ji);
553 * Installs a new function which is used to return a MonoJitInfo for a method inside
556 typedef MonoJitInfo *(*MonoJitInfoFindInAot) (MonoDomain *domain, MonoImage *image, gpointer addr);
557 void mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func);
560 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash);
563 ves_icall_System_AppDomain_getCurDomain (void);
566 ves_icall_System_AppDomain_getRootDomain (void);
569 ves_icall_System_AppDomain_createDomain (MonoString *friendly_name,
570 MonoAppDomainSetup *setup);
573 ves_icall_System_AppDomain_GetData (MonoAppDomain *ad,
576 MonoReflectionAssembly *
577 ves_icall_System_AppDomain_LoadAssemblyRaw (MonoAppDomain *ad,
578 MonoArray *raw_assembly,
579 MonoArray *raw_symbol_store,
580 MonoObject *evidence,
581 MonoBoolean refonly);
584 ves_icall_System_AppDomain_SetData (MonoAppDomain *ad,
589 ves_icall_System_AppDomain_getSetup (MonoAppDomain *ad);
592 ves_icall_System_AppDomain_getFriendlyName (MonoAppDomain *ad);
595 ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad,
596 MonoBoolean refonly);
598 MonoReflectionAssembly *
599 ves_icall_System_Reflection_Assembly_LoadFrom (MonoString *fname,
600 MonoBoolean refonly);
602 MonoReflectionAssembly *
603 ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,
605 MonoObject *evidence,
606 MonoBoolean refonly);
609 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id);
612 ves_icall_System_AppDomain_InternalUnload (gint32 domain_id);
615 ves_icall_System_AppDomain_ExecuteAssembly (MonoAppDomain *ad,
616 MonoReflectionAssembly *refass,
620 ves_icall_System_AppDomain_InternalSetDomain (MonoAppDomain *ad);
623 ves_icall_System_AppDomain_InternalSetDomainByID (gint32 domainid);
626 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad);
629 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id);
632 ves_icall_System_AppDomain_InternalPopDomainRef (void);
635 ves_icall_System_AppDomain_InternalGetContext (void);
638 ves_icall_System_AppDomain_InternalGetDefaultContext (void);
641 ves_icall_System_AppDomain_InternalSetContext (MonoAppContext *mc);
644 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad);
647 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid);
650 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status);
652 const MonoRuntimeInfo*
653 mono_get_runtime_info (void);
656 mono_runtime_set_no_exec (gboolean val);
659 mono_runtime_get_no_exec (void);
662 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname);
664 MonoImage *mono_assembly_open_from_bundle (const char *filename,
665 MonoImageOpenStatus *status,
669 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
671 MonoReflectionAssembly *
672 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, MonoAssembly *requesting, gboolean refonly);
675 mono_domain_assembly_postload_search (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly);
677 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
679 MonoImageOpenStatus *status,
682 void mono_set_private_bin_path_from_config (MonoDomain *domain);
684 int mono_framework_version (void);
686 void mono_reflection_cleanup_domain (MonoDomain *domain);
688 void mono_assembly_cleanup_domain_bindings (guint32 domain_id);
690 MonoJitInfo* mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_aot, gboolean allow_trampolines);
692 void mono_enable_debug_domain_unload (gboolean enable);
694 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */