This should fix #76928. This fix incorporates ideas from a patch
[mono.git] / mono / metadata / domain.c
1
2 /*
3  * domain.c: MonoDomain functions
4  *
5  * Author:
6  *      Dietmar Maurer (dietmar@ximian.com)
7  *      Patrik Torstensson
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16
17 #include <mono/os/gc_wrapper.h>
18
19 #include <mono/utils/mono-compiler.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/object-internals.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/assembly.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/cil-coff.h>
27 #include <mono/metadata/rawbuffer.h>
28 #include <mono/metadata/metadata-internals.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/metadata/mono-debug-debugger.h>
31 #include <mono/metadata/appdomain.h>
32 #include <metadata/threads.h>
33
34 /* #define DEBUG_DOMAIN_UNLOAD */
35
36 /* we need to use both the Tls* functions and __thread because
37  * some archs may generate faster jit code with one meachanism
38  * or the other (we used to do it because tls slots were GC-tracked,
39  * but we can't depend on this).
40  */
41 static guint32 appdomain_thread_id = -1;
42  
43 #ifdef HAVE_KW_THREAD
44 static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
45 #define GET_APPDOMAIN() tls_appdomain
46 #define SET_APPDOMAIN(x) do { \
47         tls_appdomain = x; \
48         TlsSetValue (appdomain_thread_id, x); \
49 } while (FALSE)
50
51 #else
52
53 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
54 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
55
56 #endif
57
58 #define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
59 #define SET_APPCONTEXT(x) mono_thread_current ()->current_appcontext = (x)
60
61 static guint16 appdomain_list_size = 0;
62 static guint16 appdomain_next = 0;
63 static MonoDomain **appdomains_list = NULL;
64
65 #define mono_appdomains_lock() EnterCriticalSection (&appdomains_mutex)
66 #define mono_appdomains_unlock() LeaveCriticalSection (&appdomains_mutex)
67 static CRITICAL_SECTION appdomains_mutex;
68
69 static MonoDomain *mono_root_domain = NULL;
70
71 /* AppConfigInfo: Information about runtime versions supported by an 
72  * aplication.
73  */
74 typedef struct {
75         GSList *supported_runtimes;
76         char *required_runtime;
77         int configuration_count;
78         int startup_count;
79 } AppConfigInfo;
80
81 /*
82  * AotModuleInfo: Contains information about AOT modules.
83  */
84 typedef struct {
85         MonoImage *image;
86         gpointer start, end;
87 } AotModuleInfo;
88
89 static const MonoRuntimeInfo *current_runtime = NULL;
90
91 static MonoJitInfoFindInAot jit_info_find_in_aot_func = NULL;
92
93 /*
94  * Contains information about AOT loaded code.
95  */
96 static MonoJitInfoTable *aot_modules = NULL;
97
98 /* This is the list of runtime versions supported by this JIT.
99  */
100 static const MonoRuntimeInfo supported_runtimes[] = {
101         {"v1.0.3705", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
102         {"v1.1.4322", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
103         {"v2.0.50215","2.0", { {2,0,0,0},    {8,0,0,0} }        },
104         {"v2.0.50727","2.0", { {2,0,0,0},    {8,0,0,0} }        }
105 };
106
107
108 /* The stable runtime version */
109 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
110
111 static void
112 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes);
113
114 static const MonoRuntimeInfo*
115 get_runtime_by_version (const char *version);
116
117 static MonoImage*
118 mono_jit_info_find_aot_module (guint8* addr);
119
120 guint32
121 mono_domain_get_tls_key (void)
122 {
123         return appdomain_thread_id;
124 }
125
126 gint32
127 mono_domain_get_tls_offset (void)
128 {
129         int offset = -1;
130         MONO_THREAD_VAR_OFFSET (tls_appdomain, offset);
131 /*      __asm ("jmp 1f; .section writetext, \"awx\"; 1: movl $tls_appdomain@ntpoff, %0; jmp 2f; .previous; 2:" 
132                 : "=r" (offset));*/
133         return offset;
134 }
135
136 static MonoJitInfoTable *
137 mono_jit_info_table_new (void)
138 {
139         return g_array_new (FALSE, FALSE, sizeof (gpointer));
140 }
141
142 static void
143 mono_jit_info_table_free (MonoJitInfoTable *table)
144 {
145         g_array_free (table, TRUE);
146 }
147
148 static int
149 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
150 {
151         int left = 0, right = table->len;
152
153         while (left < right) {
154                 int pos = (left + right) / 2;
155                 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
156                 char *start = ji->code_start;
157                 char *end = start + ji->code_size;
158
159                 if (addr < start)
160                         right = pos;
161                 else if (addr >= end) 
162                         left = pos + 1;
163                 else
164                         return pos;
165         }
166
167         return left;
168 }
169
170 MonoJitInfo *
171 mono_jit_info_table_find (MonoDomain *domain, char *addr)
172 {
173         MonoJitInfoTable *table = domain->jit_info_table;
174         MonoJitInfo *ji;
175         guint left = 0, right;
176
177         mono_domain_lock (domain);
178
179         right = table->len;
180         while (left < right) {
181                 guint pos = (left + right) / 2;
182                 ji = g_array_index (table, gpointer, pos);
183
184                 if (addr < (char*)ji->code_start)
185                         right = pos;
186                 else if (addr >= (char*)ji->code_start + ji->code_size) 
187                         left = pos + 1;
188                 else {
189                         mono_domain_unlock (domain);
190                         return ji;
191                 }
192         }
193         mono_domain_unlock (domain);
194
195         /* maybe it is shared code, so we also search in the root domain */
196         ji = NULL;
197         if (domain != mono_root_domain)
198                 ji = mono_jit_info_table_find (mono_root_domain, addr);
199
200         if (ji == NULL) {
201                 /* Maybe its an AOT module */
202                 MonoImage *image = mono_jit_info_find_aot_module (addr);
203                 if (image)
204                         ji = jit_info_find_in_aot_func (domain, image, addr);
205         }
206         
207         return ji;
208 }
209
210 void
211 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
212 {
213         MonoJitInfoTable *table = domain->jit_info_table;
214         gpointer start = ji->code_start;
215         int pos;
216
217         mono_domain_lock (domain);
218         pos = mono_jit_info_table_index (table, start);
219
220         g_array_insert_val (table, pos, ji);
221         mono_domain_unlock (domain);
222 }
223
224 void
225 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
226 {
227         MonoJitInfoTable *table = domain->jit_info_table;
228         gpointer start = ji->code_start;
229         int pos;
230
231         mono_domain_lock (domain);
232         pos = mono_jit_info_table_index (table, start);
233         if (g_array_index (table, gpointer, pos) != ji) {
234                 MonoJitInfo *ji2 = g_array_index (table, gpointer, pos);
235                 g_assert (ji == ji2);
236         }
237         g_assert (g_array_index (table, gpointer, pos) == ji);
238
239         g_array_remove_index (table, pos);
240         mono_domain_unlock (domain);
241 }       
242
243 static int
244 aot_info_table_index (MonoJitInfoTable *table, char *addr)
245 {
246         int left = 0, right = table->len;
247
248         while (left < right) {
249                 int pos = (left + right) / 2;
250                 AotModuleInfo *ainfo = g_array_index (table, gpointer, pos);
251                 char *start = ainfo->start;
252                 char *end = ainfo->end;
253
254                 if (addr < start)
255                         right = pos;
256                 else if (addr >= end) 
257                         left = pos + 1;
258                 else
259                         return pos;
260         }
261
262         return left;
263 }
264
265 void
266 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end)
267 {
268         AotModuleInfo *ainfo = g_new0 (AotModuleInfo, 1);
269         int pos;
270
271         ainfo->image = image;
272         ainfo->start = start;
273         ainfo->end = end;
274
275         mono_appdomains_lock ();
276
277         if (!aot_modules)
278                 aot_modules = mono_jit_info_table_new ();
279
280         pos = aot_info_table_index (aot_modules, start);
281
282         g_array_insert_val (aot_modules, pos, ainfo);
283
284         mono_appdomains_unlock ();
285 }
286
287 static MonoImage*
288 mono_jit_info_find_aot_module (guint8* addr)
289 {
290         guint left = 0, right;
291
292         if (!aot_modules)
293                 return NULL;
294
295         mono_appdomains_lock ();
296
297         right = aot_modules->len;
298         while (left < right) {
299                 guint pos = (left + right) / 2;
300                 AotModuleInfo *ai = g_array_index (aot_modules, gpointer, pos);
301
302                 if (addr < (guint8*)ai->start)
303                         right = pos;
304                 else if (addr >= (guint8*)ai->end)
305                         left = pos + 1;
306                 else {
307                         mono_appdomains_unlock ();
308                         return ai->image;
309                 }
310         }
311
312         mono_appdomains_unlock ();
313
314         return NULL;
315 }
316
317 void
318 mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func)
319 {
320         jit_info_find_in_aot_func = func;
321 }
322
323 gboolean
324 mono_string_equal (MonoString *s1, MonoString *s2)
325 {
326         int l1 = mono_string_length (s1);
327         int l2 = mono_string_length (s2);
328
329         if (s1 == s2)
330                 return TRUE;
331         if (l1 != l2)
332                 return FALSE;
333
334         return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1 * 2) == 0; 
335 }
336
337 guint
338 mono_string_hash (MonoString *s)
339 {
340         const guint16 *p = mono_string_chars (s);
341         int i, len = mono_string_length (s);
342         guint h = 0;
343
344         for (i = 0; i < len; i++) {
345                 h = (h << 5) - h + *p;
346                 p++;
347         }
348
349         return h;       
350 }
351
352 static gboolean
353 mono_ptrarray_equal (gpointer *s1, gpointer *s2)
354 {
355         int len = GPOINTER_TO_INT (s1 [0]);
356         if (len != GPOINTER_TO_INT (s2 [0]))
357                 return FALSE;
358
359         return memcmp (s1 + 1, s2 + 1, len * sizeof(gpointer)) == 0; 
360 }
361
362 static guint
363 mono_ptrarray_hash (gpointer *s)
364 {
365         int i;
366         int len = GPOINTER_TO_INT (s [0]);
367         guint hash = 0;
368         
369         for (i = 1; i < len; i++)
370                 hash += GPOINTER_TO_UINT (s [i]);
371
372         return hash;    
373 }
374
375 /*
376  * Allocate an id for domain and set domain->domain_id.
377  * LOCKING: must be called while holding appdomains_mutex.
378  * We try to assign low numbers to the domain, so it can be used
379  * as an index in data tables to lookup domain-specific info
380  * with minimal memory overhead. We also try not to reuse the
381  * same id too quickly (to help debugging).
382  */
383 static int
384 domain_id_alloc (MonoDomain *domain)
385 {
386         int id = -1, i;
387         if (!appdomains_list) {
388                 appdomain_list_size = 2;
389                 appdomains_list = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
390         }
391         for (i = appdomain_next; i < appdomain_list_size; ++i) {
392                 if (!appdomains_list [i]) {
393                         id = i;
394                         break;
395                 }
396         }
397         if (id == -1) {
398                 for (i = 0; i < appdomain_next; ++i) {
399                         if (!appdomains_list [i]) {
400                                 id = i;
401                                 break;
402                         }
403                 }
404         }
405         if (id == -1) {
406                 MonoDomain **new_list;
407                 int new_size = appdomain_list_size * 2;
408                 if (new_size >= (1 << 16))
409                         g_assert_not_reached ();
410                 id = appdomain_list_size;
411                 new_list = mono_gc_alloc_fixed (new_size * sizeof (void*), NULL);
412                 memcpy (new_list, appdomains_list, appdomain_list_size * sizeof (void*));
413                 mono_gc_free_fixed (appdomains_list);
414                 appdomains_list = new_list;
415                 appdomain_list_size = new_size;
416         }
417         domain->domain_id = id;
418         appdomains_list [id] = domain;
419         appdomain_next++;
420         if (appdomain_next > appdomain_list_size)
421                 appdomain_next = 0;
422         return id;
423 }
424
425 MonoDomain *
426 mono_domain_create (void)
427 {
428         MonoDomain *domain;
429
430         domain = mono_gc_alloc_fixed (sizeof (MonoDomain), NULL);
431         domain->domain = NULL;
432         domain->setup = NULL;
433         domain->friendly_name = NULL;
434         domain->search_path = NULL;
435
436         domain->mp = mono_mempool_new ();
437         domain->code_mp = mono_code_manager_new ();
438         domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
439         domain->domain_assemblies = NULL;
440         domain->class_vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
441         domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
442         domain->static_data_hash = mono_g_hash_table_new (mono_aligned_addr_hash, NULL);
443         domain->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
444         domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
445         domain->jit_info_table = mono_jit_info_table_new ();
446         domain->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
447         domain->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
448         domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
449         domain->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
450         domain->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
451
452         InitializeCriticalSection (&domain->lock);
453         InitializeCriticalSection (&domain->assemblies_lock);
454
455         mono_appdomains_lock ();
456         domain_id_alloc (domain);
457         mono_appdomains_unlock ();
458
459         return domain;
460 }
461
462 /**
463  * mono_init_internal:
464  * 
465  * Creates the initial application domain and initializes the mono_defaults
466  * structure.
467  * This function is guaranteed to not run any IL code.
468  * If exe_filename is not NULL, the method will determine the required runtime
469  * from the exe configuration file or the version PE field.
470  * If runtime_version is not NULL, that runtime version will be used.
471  * Either exe_filename or runtime_version must be provided.
472  *
473  * Returns: the initial domain.
474  */
475 static MonoDomain *
476 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
477 {
478         static MonoDomain *domain = NULL;
479         MonoAssembly *ass = NULL;
480         MonoImageOpenStatus status = MONO_IMAGE_OK;
481         const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
482         int n;
483
484         if (domain)
485                 g_assert_not_reached ();
486
487         MONO_GC_PRE_INIT ();
488
489         appdomain_thread_id = TlsAlloc ();
490
491         InitializeCriticalSection (&appdomains_mutex);
492
493         mono_metadata_init ();
494         mono_raw_buffer_init ();
495         mono_images_init ();
496         mono_assemblies_init ();
497         mono_loader_init ();
498
499         /* FIXME: When should we release this memory? */
500         MONO_GC_REGISTER_ROOT (appdomains_list);
501
502         domain = mono_domain_create ();
503         mono_root_domain = domain;
504
505         SET_APPDOMAIN (domain);
506         
507         /* Get a list of runtimes supported by the exe */
508         if (exe_filename != NULL) {
509                 get_runtimes_from_exe (exe_filename, runtimes);
510         } else if (runtime_version != NULL) {
511                 runtimes [0] = get_runtime_by_version (runtime_version);
512                 runtimes [1] = NULL;
513         }
514
515         if (runtimes [0] == NULL) {
516                 const MonoRuntimeInfo *default_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
517                 runtimes [0] = default_runtime;
518                 runtimes [1] = NULL;
519                 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
520                 g_print ("Using default runtime: %s\n", default_runtime->runtime_version);
521         }
522
523         /* The selected runtime will be the first one for which there is a mscrolib.dll */
524         for (n = 0; runtimes [n] != NULL && ass == NULL; n++) {
525                 current_runtime = runtimes [n];
526                 ass = mono_assembly_load_corlib (current_runtime, &status);
527                 if (status != MONO_IMAGE_OK && status != MONO_IMAGE_ERROR_ERRNO)
528                         break;
529         }
530
531         if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
532                 switch (status){
533                 case MONO_IMAGE_ERROR_ERRNO: {
534                         char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", current_runtime->framework_version, "mscorlib.dll", NULL);
535                         g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
536                         g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
537                         g_free (corlib_file);
538                         break;
539                 }
540                 case MONO_IMAGE_IMAGE_INVALID:
541                         g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
542                                  mono_assembly_getrootdir ());
543                         break;
544                 case MONO_IMAGE_MISSING_ASSEMBLYREF:
545                         g_print ("Missing assembly reference in %s/mscorlib.dll\n",
546                                  mono_assembly_getrootdir ());
547                         break;
548                 case MONO_IMAGE_OK:
549                         /* to suppress compiler warning */
550                         break;
551                 }
552                 
553                 exit (1);
554         }
555         mono_defaults.corlib = mono_assembly_get_image (ass);
556
557         mono_defaults.object_class = mono_class_from_name (
558                 mono_defaults.corlib, "System", "Object");
559         g_assert (mono_defaults.object_class != 0);
560
561         mono_defaults.void_class = mono_class_from_name (
562                 mono_defaults.corlib, "System", "Void");
563         g_assert (mono_defaults.void_class != 0);
564
565         mono_defaults.boolean_class = mono_class_from_name (
566                 mono_defaults.corlib, "System", "Boolean");
567         g_assert (mono_defaults.boolean_class != 0);
568
569         mono_defaults.byte_class = mono_class_from_name (
570                 mono_defaults.corlib, "System", "Byte");
571         g_assert (mono_defaults.byte_class != 0);
572
573         mono_defaults.sbyte_class = mono_class_from_name (
574                 mono_defaults.corlib, "System", "SByte");
575         g_assert (mono_defaults.sbyte_class != 0);
576
577         mono_defaults.int16_class = mono_class_from_name (
578                 mono_defaults.corlib, "System", "Int16");
579         g_assert (mono_defaults.int16_class != 0);
580
581         mono_defaults.uint16_class = mono_class_from_name (
582                 mono_defaults.corlib, "System", "UInt16");
583         g_assert (mono_defaults.uint16_class != 0);
584
585         mono_defaults.int32_class = mono_class_from_name (
586                 mono_defaults.corlib, "System", "Int32");
587         g_assert (mono_defaults.int32_class != 0);
588
589         mono_defaults.uint32_class = mono_class_from_name (
590                 mono_defaults.corlib, "System", "UInt32");
591         g_assert (mono_defaults.uint32_class != 0);
592
593         mono_defaults.uint_class = mono_class_from_name (
594                 mono_defaults.corlib, "System", "UIntPtr");
595         g_assert (mono_defaults.uint_class != 0);
596
597         mono_defaults.int_class = mono_class_from_name (
598                 mono_defaults.corlib, "System", "IntPtr");
599         g_assert (mono_defaults.int_class != 0);
600
601         mono_defaults.int64_class = mono_class_from_name (
602                 mono_defaults.corlib, "System", "Int64");
603         g_assert (mono_defaults.int64_class != 0);
604
605         mono_defaults.uint64_class = mono_class_from_name (
606                 mono_defaults.corlib, "System", "UInt64");
607         g_assert (mono_defaults.uint64_class != 0);
608
609         mono_defaults.single_class = mono_class_from_name (
610                 mono_defaults.corlib, "System", "Single");
611         g_assert (mono_defaults.single_class != 0);
612
613         mono_defaults.double_class = mono_class_from_name (
614                 mono_defaults.corlib, "System", "Double");
615         g_assert (mono_defaults.double_class != 0);
616
617         mono_defaults.char_class = mono_class_from_name (
618                 mono_defaults.corlib, "System", "Char");
619         g_assert (mono_defaults.char_class != 0);
620
621         mono_defaults.string_class = mono_class_from_name (
622                 mono_defaults.corlib, "System", "String");
623         g_assert (mono_defaults.string_class != 0);
624
625         mono_defaults.enum_class = mono_class_from_name (
626                 mono_defaults.corlib, "System", "Enum");
627         g_assert (mono_defaults.enum_class != 0);
628
629         mono_defaults.array_class = mono_class_from_name (
630                 mono_defaults.corlib, "System", "Array");
631         g_assert (mono_defaults.array_class != 0);
632
633         mono_defaults.delegate_class = mono_class_from_name (
634                 mono_defaults.corlib, "System", "Delegate");
635         g_assert (mono_defaults.delegate_class != 0 );
636
637         mono_defaults.multicastdelegate_class = mono_class_from_name (
638                 mono_defaults.corlib, "System", "MulticastDelegate");
639         g_assert (mono_defaults.multicastdelegate_class != 0 );
640
641         mono_defaults.asyncresult_class = mono_class_from_name (
642                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", 
643                 "AsyncResult");
644         g_assert (mono_defaults.asyncresult_class != 0 );
645
646         mono_defaults.waithandle_class = mono_class_from_name (
647                 mono_defaults.corlib, "System.Threading", "WaitHandle");
648         g_assert (mono_defaults.waithandle_class != 0 );
649
650         mono_defaults.typehandle_class = mono_class_from_name (
651                 mono_defaults.corlib, "System", "RuntimeTypeHandle");
652         g_assert (mono_defaults.typehandle_class != 0);
653
654         mono_defaults.methodhandle_class = mono_class_from_name (
655                 mono_defaults.corlib, "System", "RuntimeMethodHandle");
656         g_assert (mono_defaults.methodhandle_class != 0);
657
658         mono_defaults.fieldhandle_class = mono_class_from_name (
659                 mono_defaults.corlib, "System", "RuntimeFieldHandle");
660         g_assert (mono_defaults.fieldhandle_class != 0);
661
662         mono_defaults.monotype_class = mono_class_from_name (
663                 mono_defaults.corlib, "System", "MonoType");
664         g_assert (mono_defaults.monotype_class != 0);
665
666         mono_defaults.exception_class = mono_class_from_name (
667                 mono_defaults.corlib, "System", "Exception");
668         g_assert (mono_defaults.exception_class != 0);
669
670         mono_defaults.threadabortexception_class = mono_class_from_name (
671                 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
672         g_assert (mono_defaults.threadabortexception_class != 0);
673
674         mono_defaults.thread_class = mono_class_from_name (
675                 mono_defaults.corlib, "System.Threading", "Thread");
676         g_assert (mono_defaults.thread_class != 0);
677
678         mono_defaults.appdomain_class = mono_class_from_name (
679                 mono_defaults.corlib, "System", "AppDomain");
680         g_assert (mono_defaults.appdomain_class != 0);
681
682         mono_defaults.transparent_proxy_class = mono_class_from_name (
683                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
684         g_assert (mono_defaults.transparent_proxy_class != 0);
685
686         mono_defaults.real_proxy_class = mono_class_from_name (
687                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
688         g_assert (mono_defaults.real_proxy_class != 0);
689
690         mono_defaults.mono_method_message_class = mono_class_from_name (
691                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
692         g_assert (mono_defaults.mono_method_message_class != 0);
693
694         mono_defaults.field_info_class = mono_class_from_name (
695                 mono_defaults.corlib, "System.Reflection", "FieldInfo");
696         g_assert (mono_defaults.field_info_class != 0);
697
698         mono_defaults.method_info_class = mono_class_from_name (
699                 mono_defaults.corlib, "System.Reflection", "MethodInfo");
700         g_assert (mono_defaults.method_info_class != 0);
701
702         mono_defaults.stringbuilder_class = mono_class_from_name (
703                 mono_defaults.corlib, "System.Text", "StringBuilder");
704         g_assert (mono_defaults.stringbuilder_class != 0);
705
706         mono_defaults.math_class = mono_class_from_name (
707                 mono_defaults.corlib, "System", "Math");
708         g_assert (mono_defaults.math_class != 0);
709
710         mono_defaults.stack_frame_class = mono_class_from_name (
711                 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
712         g_assert (mono_defaults.stack_frame_class != 0);
713
714         mono_defaults.stack_trace_class = mono_class_from_name (
715                 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
716         g_assert (mono_defaults.stack_trace_class != 0);
717
718         mono_defaults.marshal_class = mono_class_from_name (
719                 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
720         g_assert (mono_defaults.marshal_class != 0);
721
722         mono_defaults.iserializeable_class = mono_class_from_name (
723                 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
724         g_assert (mono_defaults.iserializeable_class != 0);
725
726         mono_defaults.serializationinfo_class = mono_class_from_name (
727                 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
728         g_assert (mono_defaults.serializationinfo_class != 0);
729
730         mono_defaults.streamingcontext_class = mono_class_from_name (
731                 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
732         g_assert (mono_defaults.streamingcontext_class != 0);
733
734         mono_defaults.typed_reference_class =  mono_class_from_name (
735                 mono_defaults.corlib, "System", "TypedReference");
736         g_assert (mono_defaults.typed_reference_class != 0);
737
738         mono_defaults.argumenthandle_class =  mono_class_from_name (
739                 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
740         g_assert (mono_defaults.argumenthandle_class != 0);
741
742         mono_defaults.marshalbyrefobject_class =  mono_class_from_name (
743                 mono_defaults.corlib, "System", "MarshalByRefObject");
744         g_assert (mono_defaults.marshalbyrefobject_class != 0);
745
746         mono_defaults.monitor_class =  mono_class_from_name (
747                 mono_defaults.corlib, "System.Threading", "Monitor");
748         g_assert (mono_defaults.monitor_class != 0);
749
750         mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
751                 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
752         g_assert (mono_defaults.iremotingtypeinfo_class != 0);
753
754         mono_defaults.runtimesecurityframe_class = mono_class_from_name (
755                 mono_defaults.corlib, "System.Security", "RuntimeSecurityFrame");
756
757         mono_defaults.executioncontext_class = mono_class_from_name (
758                 mono_defaults.corlib, "System.Threading", "ExecutionContext");
759
760         /*
761          * Note that mono_defaults.generic_*_class is only non-NULL if we're
762          * using the 2.0 corlib.
763          */
764         mono_class_init (mono_defaults.array_class);
765         mono_defaults.generic_array_class = mono_class_from_name (
766                 mono_defaults.corlib, "System", "Array/InternalArray`1");
767         mono_defaults.generic_nullable_class = mono_class_from_name (
768                 mono_defaults.corlib, "System", "Nullable`1");
769
770         domain->friendly_name = g_path_get_basename (filename);
771
772         return domain;
773 }
774
775 /**
776  * mono_init:
777  * 
778  * Creates the initial application domain and initializes the mono_defaults
779  * structure.
780  * This function is guaranteed to not run any IL code.
781  * The runtime is initialized using the default runtime version.
782  *
783  * Returns: the initial domain.
784  */
785 MonoDomain *
786 mono_init (const char *domain_name)
787 {
788         return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
789 }
790
791 /**
792  * mono_init_from_assembly:
793  * 
794  * Creates the initial application domain and initializes the mono_defaults
795  * structure.
796  * This function is guaranteed to not run any IL code.
797  * The runtime is initialized using the runtime version required by the
798  * provided executable. The version is determined by looking at the exe 
799  * configuration file and the version PE field)
800  *
801  * Returns: the initial domain.
802  */
803 MonoDomain *
804 mono_init_from_assembly (const char *domain_name, const char *filename)
805 {
806         return mono_init_internal (domain_name, filename, NULL);
807 }
808
809 /**
810  * mono_init_version:
811  * 
812  * Creates the initial application domain and initializes the mono_defaults
813  * structure.
814  * This function is guaranteed to not run any IL code.
815  * The runtime is initialized using the provided rutime version.
816  *
817  * Returns: the initial domain.
818  */
819 MonoDomain *
820 mono_init_version (const char *domain_name, const char *version)
821 {
822         return mono_init_internal (domain_name, NULL, version);
823 }
824
825 MonoDomain*
826 mono_get_root_domain (void)
827 {
828         return mono_root_domain;
829 }
830
831 /**
832  * mono_domain_get:
833  *
834  * Returns: the current domain.
835  */
836 MonoDomain *
837 mono_domain_get ()
838 {
839         return GET_APPDOMAIN ();
840 }
841
842 /**
843  * mono_domain_set_internal:
844  * @domain: the new domain
845  *
846  * Sets the current domain to @domain.
847  */
848 void
849 mono_domain_set_internal (MonoDomain *domain)
850 {
851         SET_APPDOMAIN (domain);
852         SET_APPCONTEXT (domain->default_context);
853 }
854
855 void
856 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
857 {
858         int i, size;
859         MonoDomain **copy;
860
861         /*
862          * Create a copy of the data to avoid calling the user callback
863          * inside the lock because that could lead to deadlocks.
864          * We can do this because this function is not perf. critical.
865          */
866         mono_appdomains_lock ();
867         size = appdomain_list_size;
868         copy = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
869         memcpy (copy, appdomains_list, appdomain_list_size * sizeof (void*));
870         mono_appdomains_unlock ();
871
872         for (i = 0; i < size; ++i) {
873                 if (copy [i])
874                         func (copy [i], user_data);
875         }
876
877         mono_gc_free_fixed (copy);
878 }
879
880 /**
881  * mono_domain_assembly_open:
882  * @domain: the application domain
883  * @name: file name of the assembly
884  *
885  * fixme: maybe we should integrate this with mono_assembly_open ??
886  */
887 MonoAssembly *
888 mono_domain_assembly_open (MonoDomain *domain, const char *name)
889 {
890         MonoAssembly *ass;
891         GSList *tmp;
892
893         mono_domain_assemblies_lock (domain);
894         for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
895                 ass = tmp->data;
896                 if (strcmp (name, ass->aname.name) == 0) {
897                         mono_domain_assemblies_unlock (domain);
898                         return ass;
899                 }
900         }
901         mono_domain_assemblies_unlock (domain);
902
903         if (!(ass = mono_assembly_open (name, NULL)))
904                 return NULL;
905
906         return ass;
907 }
908
909 static void
910 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
911 {
912         MonoJitDynamicMethodInfo *di = value;
913         mono_code_manager_destroy (di->code_mp);
914         g_free (di);
915 }
916
917 static void
918 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
919 {
920         g_slist_free (value);
921 }
922
923 void
924 mono_domain_free (MonoDomain *domain, gboolean force)
925 {
926         GSList *tmp;
927         if ((domain == mono_root_domain) && !force) {
928                 g_warning ("cant unload root domain");
929                 return;
930         }
931
932         mono_appdomains_lock ();
933         appdomains_list [domain->domain_id] = NULL;
934         mono_appdomains_unlock ();
935
936         /* FIXME: free delegate_hash_table when it's used */
937         if (domain->search_path) {
938                 g_strfreev (domain->search_path);
939                 domain->search_path = NULL;
940         }
941         domain->create_proxy_for_type_method = NULL;
942         domain->private_invoke_method = NULL;
943         domain->default_context = NULL;
944         domain->out_of_memory_ex = NULL;
945         domain->null_reference_ex = NULL;
946         domain->stack_overflow_ex = NULL;
947         domain->entry_assembly = NULL;
948         g_free (domain->friendly_name);
949         domain->friendly_name = NULL;
950         for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
951                 MonoAssembly *ass = tmp->data;
952                 /*g_print ("Unloading domain %p, assembly %s, refcount: %d\n", domain, ass->aname.name, ass->ref_count);*/
953                 mono_assembly_close (ass);
954         }
955         g_slist_free (domain->domain_assemblies);
956         domain->domain_assemblies = NULL;
957
958         mono_g_hash_table_destroy (domain->env);
959         domain->env = NULL;
960         g_hash_table_destroy (domain->class_vtable_hash);
961         domain->class_vtable_hash = NULL;
962         mono_g_hash_table_destroy (domain->proxy_vtable_hash);
963         domain->proxy_vtable_hash = NULL;
964         mono_g_hash_table_destroy (domain->static_data_hash);
965         domain->static_data_hash = NULL;
966         g_hash_table_destroy (domain->jit_code_hash);
967         domain->jit_code_hash = NULL;
968         if (domain->dynamic_code_hash) {
969                 g_hash_table_foreach (domain->dynamic_code_hash, dynamic_method_info_free, NULL);
970                 g_hash_table_destroy (domain->dynamic_code_hash);
971                 domain->dynamic_code_hash = NULL;
972         }
973         mono_g_hash_table_destroy (domain->ldstr_table);
974         domain->ldstr_table = NULL;
975         mono_jit_info_table_free (domain->jit_info_table);
976         domain->jit_info_table = NULL;
977 #ifdef DEBUG_DOMAIN_UNLOAD
978         mono_mempool_invalidate (domain->mp);
979         mono_code_manager_invalidate (domain->code_mp);
980 #else
981         mono_mempool_destroy (domain->mp);
982         domain->mp = NULL;
983         mono_code_manager_destroy (domain->code_mp);
984         domain->code_mp = NULL;
985 #endif  
986         if (domain->jump_target_hash) {
987                 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
988                 g_hash_table_destroy (domain->jump_target_hash);
989                 domain->jump_target_hash = NULL;
990         }
991         if (domain->type_hash) {
992                 mono_g_hash_table_destroy (domain->type_hash);
993                 domain->type_hash = NULL;
994         }
995         if (domain->refobject_hash) {
996                 mono_g_hash_table_destroy (domain->refobject_hash);
997                 domain->refobject_hash = NULL;
998         }
999         g_hash_table_destroy (domain->class_init_trampoline_hash);
1000         domain->class_init_trampoline_hash = NULL;
1001         g_hash_table_destroy (domain->jump_trampoline_hash);
1002         domain->jump_trampoline_hash = NULL;
1003         g_hash_table_destroy (domain->finalizable_objects_hash);
1004         domain->finalizable_objects_hash = NULL;
1005         g_hash_table_destroy (domain->jit_trampoline_hash);
1006         domain->jit_trampoline_hash = NULL;
1007         g_hash_table_destroy (domain->delegate_trampoline_hash);
1008         domain->delegate_trampoline_hash = NULL;
1009         if (domain->special_static_fields) {
1010                 g_hash_table_destroy (domain->special_static_fields);
1011                 domain->special_static_fields = NULL;
1012         }
1013         DeleteCriticalSection (&domain->assemblies_lock);
1014         DeleteCriticalSection (&domain->lock);
1015         domain->setup = NULL;
1016
1017         /* FIXME: anything else required ? */
1018
1019         mono_gc_free_fixed (domain);
1020
1021         if ((domain == mono_root_domain))
1022                 mono_root_domain = NULL;
1023 }
1024
1025 /**
1026  * mono_domain_get_id:
1027  *
1028  * Returns: the a domain for a specific domain id.
1029  */
1030 MonoDomain * 
1031 mono_domain_get_by_id (gint32 domainid) 
1032 {
1033         MonoDomain * domain;
1034
1035         mono_appdomains_lock ();
1036         if (domainid < appdomain_list_size)
1037                 domain = appdomains_list [domainid];
1038         else
1039                 domain = NULL;
1040         mono_appdomains_unlock ();
1041
1042         return domain;
1043 }
1044
1045 gint32
1046 mono_domain_get_id (MonoDomain *domain)
1047 {
1048         return domain->domain_id;
1049 }
1050
1051 void 
1052 mono_context_set (MonoAppContext * new_context)
1053 {
1054         SET_APPCONTEXT (new_context);
1055 }
1056
1057 MonoAppContext * 
1058 mono_context_get (void)
1059 {
1060         return GET_APPCONTEXT ();
1061 }
1062
1063 MonoImage*
1064 mono_get_corlib (void)
1065 {
1066         return mono_defaults.corlib;
1067 }
1068
1069 MonoClass*
1070 mono_get_object_class (void)
1071 {
1072         return mono_defaults.object_class;
1073 }
1074
1075 MonoClass*
1076 mono_get_byte_class (void)
1077 {
1078         return mono_defaults.byte_class;
1079 }
1080
1081 MonoClass*
1082 mono_get_void_class (void)
1083 {
1084         return mono_defaults.void_class;
1085 }
1086
1087 MonoClass*
1088 mono_get_boolean_class (void)
1089 {
1090         return mono_defaults.boolean_class;
1091 }
1092
1093 MonoClass*
1094 mono_get_sbyte_class (void)
1095 {
1096         return mono_defaults.sbyte_class;
1097 }
1098
1099 MonoClass*
1100 mono_get_int16_class (void)
1101 {
1102         return mono_defaults.int16_class;
1103 }
1104
1105 MonoClass*
1106 mono_get_uint16_class (void)
1107 {
1108         return mono_defaults.uint16_class;
1109 }
1110
1111 MonoClass*
1112 mono_get_int32_class (void)
1113 {
1114         return mono_defaults.int32_class;
1115 }
1116
1117 MonoClass*
1118 mono_get_uint32_class (void)
1119 {
1120         return mono_defaults.uint32_class;
1121 }
1122
1123 MonoClass*
1124 mono_get_intptr_class (void)
1125 {
1126         return mono_defaults.int_class;
1127 }
1128
1129 MonoClass*
1130 mono_get_uintptr_class (void)
1131 {
1132         return mono_defaults.uint_class;
1133 }
1134
1135 MonoClass*
1136 mono_get_int64_class (void)
1137 {
1138         return mono_defaults.int64_class;
1139 }
1140
1141 MonoClass*
1142 mono_get_uint64_class (void)
1143 {
1144         return mono_defaults.uint64_class;
1145 }
1146
1147 MonoClass*
1148 mono_get_single_class (void)
1149 {
1150         return mono_defaults.single_class;
1151 }
1152
1153 MonoClass*
1154 mono_get_double_class (void)
1155 {
1156         return mono_defaults.double_class;
1157 }
1158
1159 MonoClass*
1160 mono_get_char_class (void)
1161 {
1162         return mono_defaults.char_class;
1163 }
1164
1165 MonoClass*
1166 mono_get_string_class (void)
1167 {
1168         return mono_defaults.string_class;
1169 }
1170
1171 MonoClass*
1172 mono_get_enum_class (void)
1173 {
1174         return mono_defaults.enum_class;
1175 }
1176
1177 MonoClass*
1178 mono_get_array_class (void)
1179 {
1180         return mono_defaults.array_class;
1181 }
1182
1183 MonoClass*
1184 mono_get_thread_class (void)
1185 {
1186         return mono_defaults.thread_class;
1187 }
1188
1189 MonoClass*
1190 mono_get_exception_class (void)
1191 {
1192         return mono_defaults.exception_class;
1193 }
1194
1195
1196 static char* get_attribute_value (const gchar **attribute_names, 
1197                                         const gchar **attribute_values, 
1198                                         const char *att_name)
1199 {
1200         int n;
1201         for (n=0; attribute_names[n] != NULL; n++) {
1202                 if (strcmp (attribute_names[n], att_name) == 0)
1203                         return g_strdup (attribute_values[n]);
1204         }
1205         return NULL;
1206 }
1207
1208 static void start_element (GMarkupParseContext *context, 
1209                            const gchar         *element_name,
1210                            const gchar        **attribute_names,
1211                            const gchar        **attribute_values,
1212                            gpointer             user_data,
1213                            GError             **error)
1214 {
1215         AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1216         
1217         if (strcmp (element_name, "configuration") == 0) {
1218                 app_config->configuration_count++;
1219                 return;
1220         }
1221         if (strcmp (element_name, "startup") == 0) {
1222                 app_config->startup_count++;
1223                 return;
1224         }
1225         
1226         if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1227                 return;
1228         
1229         if (strcmp (element_name, "requiredRuntime") == 0) {
1230                 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1231         } else if (strcmp (element_name, "supportedRuntime") == 0) {
1232                 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1233                 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1234         }
1235 }
1236
1237 static void end_element   (GMarkupParseContext *context,
1238                            const gchar         *element_name,
1239                            gpointer             user_data,
1240                            GError             **error)
1241 {
1242         AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1243         
1244         if (strcmp (element_name, "configuration") == 0) {
1245                 app_config->configuration_count--;
1246         } else if (strcmp (element_name, "startup") == 0) {
1247                 app_config->startup_count--;
1248         }
1249 }
1250
1251 static const GMarkupParser 
1252 mono_parser = {
1253         start_element,
1254         end_element,
1255         NULL,
1256         NULL,
1257         NULL
1258 };
1259
1260 static AppConfigInfo *
1261 app_config_parse (const char *filename)
1262 {
1263         AppConfigInfo *app_config;
1264         GMarkupParseContext *context;
1265         char *text;
1266         gsize len;
1267         
1268         struct stat buf;
1269         if (stat (filename, &buf) != 0)
1270                 return NULL;
1271         
1272         app_config = g_new0 (AppConfigInfo, 1);
1273
1274         if (!g_file_get_contents (filename, &text, &len, NULL))
1275                 return NULL;
1276
1277         context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1278         if (g_markup_parse_context_parse (context, text, len, NULL)) {
1279                 g_markup_parse_context_end_parse (context, NULL);
1280         }
1281         g_markup_parse_context_free (context);
1282         g_free (text);
1283         return app_config;
1284 }
1285
1286 static void 
1287 app_config_free (AppConfigInfo* app_config)
1288 {
1289         char *rt;
1290         GSList *list = app_config->supported_runtimes;
1291         while (list != NULL) {
1292                 rt = (char*)list->data;
1293                 g_free (rt);
1294                 list = g_slist_next (list);
1295         }
1296         g_slist_free (app_config->supported_runtimes);
1297         g_free (app_config->required_runtime);
1298         g_free (app_config);
1299 }
1300
1301
1302 static const MonoRuntimeInfo*
1303 get_runtime_by_version (const char *version)
1304 {
1305         int n;
1306         int max = G_N_ELEMENTS (supported_runtimes);
1307         
1308         for (n=0; n<max; n++) {
1309                 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1310                         return &supported_runtimes[n];
1311         }
1312         return NULL;
1313 }
1314
1315 static void
1316 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes)
1317 {
1318         AppConfigInfo* app_config;
1319         char *version;
1320         char *config_name;
1321         const MonoRuntimeInfo* runtime = NULL;
1322         MonoImage *image = NULL;
1323         
1324         config_name = g_strconcat (exe_file, ".config", NULL);
1325         app_config = app_config_parse (config_name);
1326         g_free (config_name);
1327         
1328         if (app_config != NULL) {
1329                 /* Check supportedRuntime elements, if none is supported, fail.
1330                  * If there are no such elements, look for a requiredRuntime element.
1331                  */
1332                 if (app_config->supported_runtimes != NULL) {
1333                         int n = 0;
1334                         GSList *list = app_config->supported_runtimes;
1335                         while (list != NULL) {
1336                                 version = (char*) list->data;
1337                                 runtime = get_runtime_by_version (version);
1338                                 if (runtime != NULL)
1339                                         runtimes [n++] = runtime;
1340                                 list = g_slist_next (list);
1341                         }
1342                         runtimes [n] = NULL;
1343                         app_config_free (app_config);
1344                         return;
1345                 }
1346                 
1347                 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1348                 if (app_config->required_runtime != NULL) {
1349                         runtimes [0] = get_runtime_by_version (app_config->required_runtime);
1350                         runtimes [1] = NULL;
1351                         app_config_free (app_config);
1352                         return;
1353                 }
1354                 app_config_free (app_config);
1355         }
1356         
1357         /* Look for a runtime with the exact version */
1358         image = mono_image_open (exe_file, NULL);
1359         if (image == NULL) {
1360                 /* The image is wrong or the file was not found. In this case return
1361                  * a default runtime and leave to the initialization method the work of
1362                  * reporting the error.
1363                  */
1364                 runtimes [0] = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1365                 runtimes [1] = NULL;
1366                 return;
1367         }
1368
1369         runtimes [0] = get_runtime_by_version (image->version);
1370         runtimes [1] = NULL;
1371 }
1372
1373
1374 /**
1375  * mono_get_runtime_info:
1376  *
1377  * Returns: the version of the current runtime instance.
1378  */
1379 const MonoRuntimeInfo*
1380 mono_get_runtime_info (void)
1381 {
1382         return current_runtime;
1383 }
1384
1385 gchar *
1386 mono_debugger_check_runtime_version (const char *filename)
1387 {
1388         const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
1389         const MonoRuntimeInfo *rinfo;
1390
1391         get_runtimes_from_exe (filename, runtimes);
1392         rinfo = runtimes [0];
1393
1394         if (!rinfo)
1395                 return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);
1396
1397         if (rinfo != current_runtime)
1398                 return g_strdup_printf ("The Mono Debugger is currently using the `%s' runtime, but "
1399                                         "the assembly `%s' requires version `%s'", current_runtime->runtime_version,
1400                                         filename, rinfo->runtime_version);
1401
1402         return NULL;
1403 }