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;
21 extern mono_mutex_t mono_strtod_mutex;
24 * If this is set, the memory belonging to appdomains is not freed when a domain is
25 * unloaded, and assemblies loaded by the appdomain are not unloaded either. This
26 * allows us to use typed gc in non-default appdomains too, leading to increased
29 extern gboolean mono_dont_free_domains;
31 /* This is a copy of System.AppDomainSetup */
34 MonoString *application_base;
35 MonoString *application_name;
36 MonoString *cache_path;
37 MonoString *configuration_file;
38 MonoString *dynamic_base;
39 MonoString *license_file;
40 MonoString *private_bin_path;
41 MonoString *private_bin_path_probe;
42 MonoString *shadow_copy_directories;
43 MonoString *shadow_copy_files;
44 MonoBoolean publisher_policy;
45 MonoBoolean path_changed;
46 int loader_optimization;
47 MonoBoolean disallow_binding_redirects;
48 MonoBoolean disallow_code_downloads;
49 MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
50 MonoObject *domain_initializer;
51 MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
52 MonoArray *domain_initializer_args;
53 MonoBoolean disallow_appbase_probe;
54 MonoArray *configuration_bytes;
55 MonoArray *serialized_non_primitives;
58 typedef struct _MonoJitInfoTable MonoJitInfoTable;
59 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
61 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE 64
63 struct _MonoJitInfoTableChunk
66 volatile int num_elements;
67 volatile gint8 *last_code_end;
68 MonoJitInfo * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
71 struct _MonoJitInfoTable
75 MonoJitInfoTableChunk *chunks [MONO_ZERO_LEN_ARRAY];
78 #define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
80 typedef GArray MonoAotModuleInfoTable;
87 gpointer handler_start;
89 * For LLVM compiled code, this is the index of the il clause
90 * associated with this handler.
94 MonoClass *catch_class;
98 } MonoJitExceptionInfo;
101 * Contains information about the type arguments for generic shared methods.
105 * If not NULL, determines whenever the class type arguments of the gshared method are references or vtypes.
106 * The array length is equal to class_inst->type_argv.
109 /* Same for method type parameters */
110 gboolean *mvar_is_vt;
111 } MonoGenericSharingContext;
113 /* Simplified DWARF location list entry */
115 /* Whenever the value is in a register */
118 * If is_reg is TRUE, the register which contains the value. Otherwise
123 * If is_reg is FALSE, the offset of the stack location relative to 'reg'.
128 * Offsets of the PC interval where the value is in this location.
131 } MonoDwarfLocListEntry;
135 MonoGenericSharingContext *generic_sharing_context;
137 MonoDwarfLocListEntry *locations;
141 gboolean this_in_reg:1;
142 } MonoGenericJitInfo;
145 A try block hole is used to represent a non-contiguous part of
146 of a segment of native code protected by a given .try block.
147 Usually, a try block is defined as a contiguous segment of code.
148 But in some cases it's needed to have some parts of it to not be protected.
149 For example, given "try {} finally {}", the code in the .try block to call
150 the finally part looks like:
156 jump outside try block
162 The instructions between the call and the jump should not be under the try block since they happen
163 after the finally block executes, which means if an async exceptions happens at that point we would
164 execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
165 that those instructions are not protected.
172 } MonoTryBlockHoleJitInfo;
177 MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
178 } MonoTryBlockHoleTableJitInfo;
187 /* Relative to code_start */
193 gboolean cas_inited:1;
194 gboolean cas_class_assert:1;
195 gboolean cas_class_deny:1;
196 gboolean cas_class_permitonly:1;
197 gboolean cas_method_assert:1;
198 gboolean cas_method_deny:1;
199 gboolean cas_method_permitonly:1;
204 JIT_INFO_HAS_CAS_INFO = (1 << 0),
205 JIT_INFO_HAS_GENERIC_JIT_INFO = (1 << 1),
206 JIT_INFO_HAS_TRY_BLOCK_HOLES = (1 << 2),
207 JIT_INFO_HAS_ARCH_EH_INFO = (1 << 3),
208 JIT_INFO_HAS_THUNK_INFO = (1 << 4)
211 struct _MonoJitInfo {
212 /* NOTE: These first two elements (method and
213 next_jit_code_hash) must be in the same order and at the
214 same offset as in RuntimeMethod, because of the jit_code_hash
215 internal hash table in MonoDomain. */
222 struct _MonoJitInfo *next_jit_code_hash;
226 guint32 num_clauses:15;
227 /* Whenever the code is domain neutral or 'shared' */
228 gboolean domain_neutral:1;
229 gboolean has_cas_info:1;
230 gboolean has_generic_jit_info:1;
231 gboolean has_try_block_holes:1;
232 gboolean has_arch_eh_info:1;
233 gboolean has_thunk_info:1;
235 gboolean from_llvm:1;
236 gboolean dbg_attrs_inited:1;
237 gboolean dbg_hidden:1;
238 /* Whenever this jit info was loaded in async context */
240 gboolean dbg_step_through:1;
241 gboolean dbg_non_user_code:1;
243 * Whenever this jit info refers to a trampoline.
244 * d.tramp_info contains additional data in this case.
246 gboolean is_trampoline:1;
248 /* FIXME: Embed this after the structure later*/
249 gpointer gc_info; /* Currently only used by SGen */
251 MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
252 /* There is an optional MonoGenericJitInfo after the clauses */
253 /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
254 /* There is an optional MonoArchEHJitInfo after MonoTryBlockHoleTableJitInfo */
255 /* There is an optional MonoThunkJitInfo after MonoArchEHJitInfo */
258 #define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
260 struct _MonoAppContext {
264 gpointer *static_data;
267 /* Lock-free allocator */
272 } LockFreeMempoolChunk;
275 LockFreeMempoolChunk *current, *chunks;
279 * We have two unloading states because the domain
280 * must remain fully functional while AppDomain::DomainUnload is
282 * After that unloading began and all domain facilities are teared down
283 * such as execution of new threadpool jobs.
286 MONO_APPDOMAIN_CREATED,
287 MONO_APPDOMAIN_UNLOADING_START,
288 MONO_APPDOMAIN_UNLOADING,
289 MONO_APPDOMAIN_UNLOADED
290 } MonoAppDomainState;
292 typedef struct _MonoThunkFreeList {
294 int length; /* only valid for the wait list */
295 struct _MonoThunkFreeList *next;
298 typedef struct _MonoJitCodeHash MonoJitCodeHash;
300 typedef struct _MonoTlsDataRecord MonoTlsDataRecord;
301 struct _MonoTlsDataRecord {
302 MonoTlsDataRecord *next;
309 * This lock must never be taken before the loader lock,
310 * i.e. if both are taken by the same thread, the loader lock
315 MonoCodeManager *code_mp;
317 * keep all the managed objects close to each other for the precise GC
318 * For the Boehm GC we additionally keep close also other GC-tracked pointers.
320 #define MONO_DOMAIN_FIRST_OBJECT setup
321 MonoAppDomainSetup *setup;
322 MonoAppDomain *domain;
323 MonoAppContext *default_context;
324 MonoException *out_of_memory_ex;
325 MonoException *null_reference_ex;
326 MonoException *stack_overflow_ex;
328 MonoObject *typeof_void;
329 /* Ephemeron Tombstone*/
330 MonoObject *ephemeron_tombstone;
331 /* new MonoType [0] */
332 MonoArray *empty_types;
334 * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
335 * not object references.
337 #define MONO_DOMAIN_FIRST_GC_TRACKED env
339 MonoGHashTable *ldstr_table;
340 /* hashtables for Reflection handles */
341 MonoGHashTable *type_hash;
342 MonoGHashTable *refobject_hash;
343 /* a GC-tracked array to keep references to the static fields of types */
344 gpointer *static_data_array;
345 /* maps class -> type initialization exception object */
346 MonoGHashTable *type_init_exception_hash;
347 /* maps delegate trampoline addr -> delegate object */
348 MonoGHashTable *delegate_hash_table;
349 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
351 /* Needed by Thread:GetDomainID() */
353 gint32 shadow_serial;
354 unsigned char inet_family_hint; // used in socket-io.c as a cache
355 GSList *domain_assemblies;
356 MonoAssembly *entry_assembly;
358 GPtrArray *class_vtable_array;
359 /* maps remote class key -> MonoRemoteClass */
360 GHashTable *proxy_vtable_hash;
361 /* Protected by 'jit_code_hash_lock' */
362 MonoInternalHashTable jit_code_hash;
363 mono_mutex_t jit_code_hash_lock;
364 int num_jit_info_tables;
366 volatile jit_info_table;
368 * Contains information about AOT loaded code.
369 * Only used in the root domain.
372 volatile aot_modules;
373 GSList *jit_info_free_queue;
374 /* Used when loading assemblies */
376 gchar *private_bin_path;
377 LockFreeMempool *lock_free_mp;
379 /* Used by remoting proxies */
380 MonoMethod *create_proxy_for_type_method;
381 MonoMethod *private_invoke_method;
382 /* Used to store offsets of thread and context static fields */
383 GHashTable *special_static_fields;
384 MonoTlsDataRecord *tlsrec_list;
386 * This must be a GHashTable, since these objects can't be finalized
387 * if the hashtable contains a GC visible reference to them.
389 GHashTable *finalizable_objects_hash;
391 /* Protects the three hashes above */
392 mono_mutex_t finalizable_objects_hash_lock;
393 /* Used when accessing 'domain_assemblies' */
394 mono_mutex_t assemblies_lock;
396 GHashTable *method_rgctx_hash;
398 GHashTable *generic_virtual_cases;
399 MonoThunkFreeList **thunk_free_lists;
401 GHashTable *generic_virtual_thunks;
403 /* Information maintained by the JIT engine */
404 gpointer runtime_info;
406 /*thread pool jobs, used to coordinate shutdown.*/
407 volatile int threadpool_jobs;
408 HANDLE cleanup_semaphore;
410 /* Contains the compiled runtime invoke wrapper used by finalizers */
411 gpointer finalize_runtime_invoke;
413 /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
414 gpointer capture_context_runtime_invoke;
416 /* Contains the compiled method used by async resylt creation to capture thread context*/
417 gpointer capture_context_method;
419 /* Assembly bindings, the per-domain part */
420 GSList *assembly_bindings;
421 gboolean assembly_bindings_parsed;
423 /* Used by socket-io.c */
424 /* These are domain specific, since the assembly can be unloaded */
425 MonoImage *socket_assembly;
426 MonoClass *sockaddr_class;
427 MonoClassField *sockaddr_data_field;
429 /* Used by threadpool.c */
430 MonoImage *system_image;
431 MonoClass *corlib_asyncresult_class;
432 MonoClass *socket_class;
433 MonoClass *ad_unloaded_ex_class;
434 MonoClass *process_class;
436 /* Cache function pointers for architectures */
437 /* that require wrappers */
438 GHashTable *ftnptrs_hash;
440 guint32 execution_context_field_offset;
444 guint16 major, minor, build, revision;
445 } AssemblyVersionSet;
447 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
449 const char runtime_version [12];
450 const char framework_version [4];
451 const AssemblyVersionSet version_sets [4];
454 #define mono_domain_assemblies_lock(domain) mono_locks_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
455 #define mono_domain_assemblies_unlock(domain) mono_locks_release(&(domain)->assemblies_lock, DomainAssembliesLock)
456 #define mono_domain_jit_code_hash_lock(domain) mono_locks_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
457 #define mono_domain_jit_code_hash_unlock(domain) mono_locks_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
459 typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
461 void mono_domain_lock (MonoDomain *domain);
462 void mono_domain_unlock (MonoDomain *domain);
465 mono_install_runtime_load (MonoLoadFunc func);
468 mono_runtime_load (const char *filename, const char *runtime_version);
470 typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
473 mono_install_create_domain_hook (MonoCreateDomainFunc func);
475 typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
478 mono_install_free_domain_hook (MonoFreeDomainFunc func);
484 mono_close_exe_image (void);
487 mono_jit_info_size (MonoJitInfoFlags flags, int num_clauses, int num_holes);
490 mono_jit_info_init (MonoJitInfo *ji, MonoMethod *method, guint8 *code, int code_size,
491 MonoJitInfoFlags flags, int num_clauses, int num_holes);
494 mono_jit_info_table_new (MonoDomain *domain);
497 mono_jit_info_table_free (MonoJitInfoTable *table);
500 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji);
503 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji);
506 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end);
509 mono_jit_info_get_generic_jit_info (MonoJitInfo *ji);
511 MonoGenericSharingContext*
512 mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji);
515 mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx);
518 mono_make_shadow_copy (const char *filename);
521 mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name);
524 mono_domain_alloc (MonoDomain *domain, guint size);
527 mono_domain_alloc0 (MonoDomain *domain, guint size);
530 mono_domain_alloc0_lock_free (MonoDomain *domain, guint size);
533 mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
536 mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment);
539 mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize);
542 nacl_domain_get_code_dest (MonoDomain *domain, void *data);
545 nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end);
548 mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data);
551 mono_domain_unset (void);
554 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception);
556 MonoTryBlockHoleTableJitInfo*
557 mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji);
560 mono_jit_info_get_arch_eh_info (MonoJitInfo *ji);
563 mono_jit_info_get_thunk_info (MonoJitInfo *ji);
566 mono_jit_info_get_cas_info (MonoJitInfo *ji);
569 * Installs a new function which is used to return a MonoJitInfo for a method inside
572 typedef MonoJitInfo *(*MonoJitInfoFindInAot) (MonoDomain *domain, MonoImage *image, gpointer addr);
573 void mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func);
576 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash);
579 ves_icall_System_AppDomain_getCurDomain (void);
582 ves_icall_System_AppDomain_getRootDomain (void);
585 ves_icall_System_AppDomain_createDomain (MonoString *friendly_name,
586 MonoAppDomainSetup *setup);
589 ves_icall_System_AppDomain_GetData (MonoAppDomain *ad,
592 MonoReflectionAssembly *
593 ves_icall_System_AppDomain_LoadAssemblyRaw (MonoAppDomain *ad,
594 MonoArray *raw_assembly,
595 MonoArray *raw_symbol_store,
596 MonoObject *evidence,
597 MonoBoolean refonly);
600 ves_icall_System_AppDomain_SetData (MonoAppDomain *ad,
605 ves_icall_System_AppDomain_getSetup (MonoAppDomain *ad);
608 ves_icall_System_AppDomain_getFriendlyName (MonoAppDomain *ad);
611 ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad,
612 MonoBoolean refonly);
614 MonoReflectionAssembly *
615 ves_icall_System_Reflection_Assembly_LoadFrom (MonoString *fname,
616 MonoBoolean refonly);
618 MonoReflectionAssembly *
619 ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,
621 MonoObject *evidence,
622 MonoBoolean refonly);
625 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id);
628 ves_icall_System_AppDomain_InternalUnload (gint32 domain_id);
631 ves_icall_System_AppDomain_ExecuteAssembly (MonoAppDomain *ad,
632 MonoReflectionAssembly *refass,
636 ves_icall_System_AppDomain_InternalSetDomain (MonoAppDomain *ad);
639 ves_icall_System_AppDomain_InternalSetDomainByID (gint32 domainid);
642 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad);
645 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id);
648 ves_icall_System_AppDomain_InternalPopDomainRef (void);
651 ves_icall_System_AppDomain_InternalGetContext (void);
654 ves_icall_System_AppDomain_InternalGetDefaultContext (void);
657 ves_icall_System_AppDomain_InternalSetContext (MonoAppContext *mc);
660 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad);
663 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid);
666 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status);
668 const MonoRuntimeInfo*
669 mono_get_runtime_info (void);
672 mono_runtime_set_no_exec (gboolean val);
675 mono_runtime_get_no_exec (void);
678 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname);
680 MonoImage *mono_assembly_open_from_bundle (const char *filename,
681 MonoImageOpenStatus *status,
685 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
687 MonoReflectionAssembly *
688 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, MonoAssembly *requesting, gboolean refonly);
691 mono_domain_assembly_postload_search (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly);
693 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
695 MonoImageOpenStatus *status,
698 void mono_set_private_bin_path_from_config (MonoDomain *domain);
700 int mono_framework_version (void);
702 void mono_reflection_cleanup_domain (MonoDomain *domain);
704 void mono_assembly_cleanup_domain_bindings (guint32 domain_id);
706 MonoJitInfo* mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_aot, gboolean allow_trampolines);
708 void mono_enable_debug_domain_unload (gboolean enable);
710 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */