2007-09-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mono / metadata / domain-internals.h
1 /*
2  * Appdomain-related internal data structures and functions.
3  */
4 #ifndef __MONO_METADATA_DOMAIN_INTERNALS_H__
5 #define __MONO_METADATA_DOMAIN_INTERNALS_H__
6
7 #include <mono/metadata/appdomain.h>
8 #include <mono/utils/mono-codeman.h>
9 #include <mono/utils/mono-hash.h>
10 #include <mono/utils/mono-compiler.h>
11 #include <mono/utils/mono-internal-hash.h>
12 #include <mono/io-layer/io-layer.h>
13
14 extern CRITICAL_SECTION mono_delegate_section;
15
16 /* This is a copy of System.AppDomainSetup */
17 typedef struct {
18         MonoObject object;
19         MonoString *application_base;
20         MonoString *application_name;
21         MonoString *cache_path;
22         MonoString *configuration_file;
23         MonoString *dynamic_base;
24         MonoString *license_file;
25         MonoString *private_bin_path;
26         MonoString *private_bin_path_probe;
27         MonoString *shadow_copy_directories;
28         MonoString *shadow_copy_files;
29         MonoBoolean publisher_policy;
30         MonoBoolean path_changed;
31         int loader_optimization;
32         MonoBoolean disallow_binding_redirects;
33         MonoBoolean disallow_code_downloads;
34         MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
35         MonoObject *domain_initializer;
36         MonoArray *domain_initializer_args;
37         MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
38         MonoBoolean disallow_appbase_probe;
39         MonoArray *configuration_bytes;
40 } MonoAppDomainSetup;
41
42 typedef struct _MonoJitInfoTable MonoJitInfoTable;
43 typedef struct _MonoJitInfoTableChunk MonoJitInfoTableChunk;
44
45 #define MONO_JIT_INFO_TABLE_CHUNK_SIZE          64
46
47 struct _MonoJitInfoTableChunk
48 {
49         int                    refcount;
50         volatile int           num_elements;
51         volatile gint8        *last_code_end;
52         MonoJitInfo * volatile data [MONO_JIT_INFO_TABLE_CHUNK_SIZE];
53 };
54
55 struct _MonoJitInfoTable
56 {
57         int                     num_chunks;
58         MonoJitInfoTableChunk  *chunks [MONO_ZERO_LEN_ARRAY];
59 };
60
61 typedef GArray MonoAotModuleInfoTable;
62
63 typedef struct {
64         guint32  flags;
65         gint32   exvar_offset;
66         gpointer try_start;
67         gpointer try_end;
68         gpointer handler_start;
69         union {
70                 MonoClass *catch_class;
71                 gpointer filter;
72         } data;
73 } MonoJitExceptionInfo;
74
75 struct _MonoJitInfo {
76         /* NOTE: These first two elements (method and
77            next_jit_code_hash) must be in the same order and at the
78            same offset as in RuntimeMethod, because of the jit_code_hash
79            internal hash table in MonoDomain. */
80         MonoMethod *method;
81         struct _MonoJitInfo *next_jit_code_hash;
82         gpointer    code_start;
83         guint32     used_regs;
84         int         code_size;
85         guint32     num_clauses:24;
86         /* Whenever the code is domain neutral or 'shared' */
87         gboolean    domain_neutral:1;
88         gboolean    cas_inited:1;
89         gboolean    cas_class_assert:1;
90         gboolean    cas_class_deny:1;
91         gboolean    cas_class_permitonly:1;
92         gboolean    cas_method_assert:1;
93         gboolean    cas_method_deny:1;
94         gboolean    cas_method_permitonly:1;
95         MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
96 };
97
98 typedef struct {
99         MonoJitInfo *ji;
100         MonoCodeManager *code_mp;
101 } MonoJitDynamicMethodInfo;
102
103 struct _MonoAppContext {
104         MonoObject obj;
105         gint32 domain_id;
106         gint32 context_id;
107         gpointer *static_data;
108 };
109
110 typedef enum {
111         MONO_APPDOMAIN_CREATED,
112         MONO_APPDOMAIN_UNLOADING,
113         MONO_APPDOMAIN_UNLOADED
114 } MonoAppDomainState;
115
116 typedef struct _MonoJitCodeHash MonoJitCodeHash;
117
118 struct _MonoDomain {
119         CRITICAL_SECTION    lock;
120         MonoMemPool        *mp;
121         MonoCodeManager    *code_mp;
122         /*
123          * keep all the managed objects close to each other for the precise GC
124          * For the Boehm GC we additionally keep close also other GC-tracked pointers.
125          */
126 #define MONO_DOMAIN_FIRST_OBJECT setup
127         MonoAppDomainSetup *setup;
128         MonoAppDomain      *domain;
129         MonoAppContext     *default_context;
130         MonoException      *out_of_memory_ex;
131         MonoException      *null_reference_ex;
132         MonoException      *stack_overflow_ex;
133 #define MONO_DOMAIN_FIRST_GC_TRACKED env
134         MonoGHashTable     *env;
135         MonoGHashTable     *ldstr_table;
136         /* hashtables for Reflection handles */
137         MonoGHashTable     *type_hash;
138         MonoGHashTable     *refobject_hash;
139         /* a GC-tracked array to keep references to the static fields of types */
140         gpointer           *static_data_array;
141         /* maps class -> type initialization exception object */
142         MonoGHashTable    *type_init_exception_hash;
143         /* maps delegate trampoline addr -> delegate object */
144         MonoGHashTable     *delegate_hash_table;
145 #define MONO_DOMAIN_LAST_GC_TRACKED delegate_hash_table
146         guint32            state;
147         /* Needed by Thread:GetDomainID() */
148         gint32             domain_id;
149         gint32             shadow_serial;
150         GSList             *domain_assemblies;
151         MonoAssembly       *entry_assembly;
152         char               *friendly_name;
153         GHashTable         *class_vtable_hash;
154         /* maps remote class key -> MonoRemoteClass */
155         GHashTable         *proxy_vtable_hash;
156         MonoInternalHashTable jit_code_hash;
157         /* maps MonoMethod -> MonoJitDynamicMethodInfo */
158         GHashTable         *dynamic_code_hash;
159         MonoJitInfoTable * 
160           volatile          jit_info_table;
161         /* Used when loading assemblies */
162         gchar **search_path;
163         gchar *private_bin_path;
164         
165         /* Used by remoting proxies */
166         MonoMethod         *create_proxy_for_type_method;
167         MonoMethod         *private_invoke_method;
168         /* Used to store offsets of thread and context static fields */
169         GHashTable         *special_static_fields;
170         GHashTable         *jump_target_hash;
171         GHashTable         *class_init_trampoline_hash;
172         GHashTable         *jump_trampoline_hash;
173         GHashTable         *jit_trampoline_hash;
174         GHashTable         *delegate_trampoline_hash;
175         /* 
176          * This must be a GHashTable, since these objects can't be finalized
177          * if the hashtable contains a GC visible reference to them.
178          */
179         GHashTable         *finalizable_objects_hash;
180         /* Used when accessing 'domain_assemblies' */
181         CRITICAL_SECTION    assemblies_lock;
182 };
183
184 typedef struct  {
185         guint16 major, minor, build, revision;
186 } AssemblyVersionSet;
187
188 /* MonoRuntimeInfo: Contains information about versions supported by this runtime */
189 typedef struct  {
190         const char runtime_version [12];
191         const char framework_version [4];
192         const AssemblyVersionSet version_sets [2];
193 } MonoRuntimeInfo;
194
195 #define mono_domain_lock(domain)   EnterCriticalSection(&(domain)->lock)
196 #define mono_domain_unlock(domain) LeaveCriticalSection(&(domain)->lock)
197 #define mono_domain_assemblies_lock(domain)   EnterCriticalSection(&(domain)->assemblies_lock)
198 #define mono_domain_assemblies_unlock(domain) LeaveCriticalSection(&(domain)->assemblies_lock)
199
200 void 
201 mono_init_com_types (void) MONO_INTERNAL;
202
203 void 
204 mono_cleanup (void) MONO_INTERNAL;
205
206 void
207 mono_jit_info_table_add    (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
208
209 void
210 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
211
212 void
213 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end) MONO_INTERNAL;
214
215 /* 
216  * Installs a new function which is used to return a MonoJitInfo for a method inside
217  * an AOT module.
218  */
219 typedef MonoJitInfo *(*MonoJitInfoFindInAot)         (MonoDomain *domain, MonoImage *image, gpointer addr);
220 void          mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func) MONO_INTERNAL;
221
222 void
223 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash) MONO_INTERNAL;
224
225 MonoAppDomain *
226 ves_icall_System_AppDomain_getCurDomain            (void) MONO_INTERNAL;
227
228 MonoAppDomain *
229 ves_icall_System_AppDomain_getRootDomain           (void) MONO_INTERNAL;
230
231 MonoAppDomain *
232 ves_icall_System_AppDomain_createDomain            (MonoString         *friendly_name,
233                                                     MonoAppDomainSetup *setup) MONO_INTERNAL;
234
235 MonoObject *
236 ves_icall_System_AppDomain_GetData                 (MonoAppDomain *ad, 
237                                                     MonoString    *name) MONO_INTERNAL;
238
239 MonoReflectionAssembly *
240 ves_icall_System_AppDomain_LoadAssemblyRaw         (MonoAppDomain *ad,
241                                                     MonoArray *raw_assembly, 
242                                                     MonoArray *raw_symbol_store,
243                                                     MonoObject *evidence,
244                                                     MonoBoolean refonly) MONO_INTERNAL;
245
246 void
247 ves_icall_System_AppDomain_SetData                 (MonoAppDomain *ad, 
248                                                     MonoString    *name, 
249                                                     MonoObject    *data) MONO_INTERNAL;
250
251 MonoAppDomainSetup *
252 ves_icall_System_AppDomain_getSetup                (MonoAppDomain *ad) MONO_INTERNAL;
253
254 MonoString *
255 ves_icall_System_AppDomain_getFriendlyName         (MonoAppDomain *ad) MONO_INTERNAL;
256
257 MonoArray *
258 ves_icall_System_AppDomain_GetAssemblies           (MonoAppDomain *ad,
259                                                     MonoBoolean refonly) MONO_INTERNAL;
260
261 MonoReflectionAssembly *
262 ves_icall_System_Reflection_Assembly_LoadFrom      (MonoString *fname,
263                                                     MonoBoolean refonly) MONO_INTERNAL;
264
265 MonoReflectionAssembly *
266 ves_icall_System_AppDomain_LoadAssembly            (MonoAppDomain *ad, 
267                                                     MonoString *assRef,
268                                                     MonoObject    *evidence,
269                                                     MonoBoolean refonly) MONO_INTERNAL;
270
271 gboolean
272 ves_icall_System_AppDomain_InternalIsFinalizingForUnload (gint32 domain_id) MONO_INTERNAL;
273
274 void
275 ves_icall_System_AppDomain_InternalUnload          (gint32 domain_id) MONO_INTERNAL;
276
277 gint32
278 ves_icall_System_AppDomain_ExecuteAssembly         (MonoAppDomain *ad, 
279                                                     MonoString    *file, 
280                                                     MonoObject    *evidence,
281                                                     MonoArray     *args) MONO_INTERNAL;
282
283 MonoAppDomain * 
284 ves_icall_System_AppDomain_InternalSetDomain       (MonoAppDomain *ad) MONO_INTERNAL;
285
286 MonoAppDomain * 
287 ves_icall_System_AppDomain_InternalSetDomainByID   (gint32 domainid) MONO_INTERNAL;
288
289 void
290 ves_icall_System_AppDomain_InternalPushDomainRef (MonoAppDomain *ad) MONO_INTERNAL;
291
292 void
293 ves_icall_System_AppDomain_InternalPushDomainRefByID (gint32 domain_id) MONO_INTERNAL;
294
295 void
296 ves_icall_System_AppDomain_InternalPopDomainRef (void) MONO_INTERNAL;
297
298 MonoAppContext * 
299 ves_icall_System_AppDomain_InternalGetContext      (void) MONO_INTERNAL;
300
301 MonoAppContext * 
302 ves_icall_System_AppDomain_InternalGetDefaultContext      (void) MONO_INTERNAL;
303
304 MonoAppContext * 
305 ves_icall_System_AppDomain_InternalSetContext      (MonoAppContext *mc) MONO_INTERNAL;
306
307 gint32 
308 ves_icall_System_AppDomain_GetIDFromDomain (MonoAppDomain * ad) MONO_INTERNAL;
309
310 MonoString *
311 ves_icall_System_AppDomain_InternalGetProcessGuid (MonoString* newguid) MONO_INTERNAL;
312
313 MonoAssembly *
314 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status) MONO_INTERNAL;
315
316 const MonoRuntimeInfo*
317 mono_get_runtime_info (void) MONO_INTERNAL;
318
319 gboolean
320 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname) MONO_INTERNAL;
321
322 void
323 mono_assembly_name_free (MonoAssemblyName *aname) MONO_INTERNAL;
324
325 MonoImage *mono_assembly_open_from_bundle (const char *filename,
326                                            MonoImageOpenStatus *status,
327                                            gboolean refonly) MONO_INTERNAL;
328
329 void
330 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap);
331
332 MonoReflectionAssembly *
333 mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, gboolean refonly) MONO_INTERNAL;
334
335 MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname, 
336                                                 const char       *basedir, 
337                                                 MonoImageOpenStatus *status,
338                                                 gboolean refonly) MONO_INTERNAL;
339
340 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */