2 * mini-runtime.c: Runtime code for the JIT
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * Copyright 2002-2003 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc.
10 * Copyright 2011-2015 Xamarin, Inc (http://www.xamarin.com)
21 #ifdef HAVE_SYS_TIME_H
28 #include <mono/utils/memcheck.h>
30 #include <mono/metadata/assembly.h>
31 #include <mono/metadata/loader.h>
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/class.h>
34 #include <mono/metadata/object.h>
35 #include <mono/metadata/tokentype.h>
36 #include <mono/metadata/tabledefs.h>
37 #include <mono/metadata/threads.h>
38 #include <mono/metadata/appdomain.h>
39 #include <mono/metadata/debug-helpers.h>
40 #include "mono/metadata/profiler.h"
41 #include <mono/metadata/profiler-private.h>
42 #include <mono/metadata/mono-config.h>
43 #include <mono/metadata/environment.h>
44 #include <mono/metadata/mono-debug.h>
45 #include <mono/metadata/gc-internal.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/metadata/mempool-internals.h>
48 #include <mono/metadata/attach.h>
49 #include <mono/metadata/runtime.h>
50 #include <mono/utils/mono-math.h>
51 #include <mono/utils/mono-compiler.h>
52 #include <mono/utils/mono-counters.h>
53 #include <mono/utils/mono-error-internals.h>
54 #include <mono/utils/mono-logger-internal.h>
55 #include <mono/utils/mono-mmap.h>
56 #include <mono/utils/mono-path.h>
57 #include <mono/utils/mono-tls.h>
58 #include <mono/utils/mono-hwcap.h>
59 #include <mono/utils/dtrace.h>
60 #include <mono/utils/mono-signal-handler.h>
61 #include <mono/utils/mono-threads.h>
62 #include <mono/io-layer/io-layer.h>
65 #include "seq-points.h"
72 #include "jit-icalls.h"
75 #include "debugger-agent.h"
77 static guint32 default_opt = 0;
78 static gboolean default_opt_set = FALSE;
80 MonoNativeTlsKey mono_jit_tls_id;
82 #ifdef MONO_HAVE_FAST_TLS
83 MONO_FAST_TLS_DECLARE(mono_jit_tls);
86 gboolean mono_compile_aot = FALSE;
87 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
88 gboolean mono_aot_only = FALSE;
90 const char *mono_build_date;
91 gboolean mono_do_signal_chaining;
92 gboolean mono_do_crash_chaining;
96 * This flag controls whenever the runtime uses LLVM for JIT compilation, and whenever
97 * it can load AOT code compiled by LLVM.
99 gboolean mono_use_llvm = FALSE;
101 #define mono_jit_lock() mono_mutex_lock (&jit_mutex)
102 #define mono_jit_unlock() mono_mutex_unlock (&jit_mutex)
103 static mono_mutex_t jit_mutex;
105 static MonoCodeManager *global_codeman;
107 MonoDebugOptions debug_options;
109 #ifdef VALGRIND_JIT_REGISTER_MAP
110 int valgrind_register;
113 static GSList *tramp_infos;
115 static void register_icalls (void);
118 mono_running_on_valgrind (void)
120 if (RUNNING_ON_VALGRIND){
121 #ifdef VALGRIND_JIT_REGISTER_MAP
122 valgrind_register = TRUE;
135 find_tramp (gpointer key, gpointer value, gpointer user_data)
137 FindTrampUserData *ud = (FindTrampUserData*)user_data;
140 ud->method = (MonoMethod*)key;
144 G_GNUC_UNUSED static char*
145 get_method_from_ip (void *ip)
150 MonoDomain *domain = mono_domain_get ();
151 MonoDebugSourceLocation *location;
152 FindTrampUserData user_data;
155 domain = mono_get_root_domain ();
157 ji = mono_jit_info_table_find (domain, ip);
160 user_data.method = NULL;
161 mono_domain_lock (domain);
162 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
163 mono_domain_unlock (domain);
164 if (user_data.method) {
165 char *mname = mono_method_full_name (user_data.method, TRUE);
166 res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
173 method = mono_method_full_name (jinfo_get_method (ji), TRUE);
174 /* FIXME: unused ? */
175 location = mono_debug_lookup_source_location (jinfo_get_method (ji), (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
177 res = g_strdup_printf (" %s + 0x%x (%p %p) [%p - %s]", method, (int)((char*)ip - (char*)ji->code_start), ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
179 mono_debug_free_source_location (location);
187 * @ip: an instruction pointer address
189 * This method is used from a debugger to get the name of the
190 * method at address @ip. This routine is typically invoked from
191 * a debugger like this:
193 * (gdb) print mono_pmip ($pc)
195 * Returns: the name of the method at address @ip.
200 return get_method_from_ip (ip);
204 * mono_print_method_from_ip
205 * @ip: an instruction pointer address
207 * This method is used from a debugger to get the name of the
208 * method at address @ip.
210 * This prints the name of the method at address @ip in the standard
211 * output. Unlike mono_pmip which returns a string, this routine
212 * prints the value on the standard output.
215 /* Prevent the linker from optimizing this away in embedding setups to help debugging */
216 __attribute__((used))
219 mono_print_method_from_ip (void *ip)
223 MonoDebugSourceLocation *source;
224 MonoDomain *domain = mono_domain_get ();
225 MonoDomain *target_domain = mono_domain_get ();
226 FindTrampUserData user_data;
227 MonoGenericSharingContext*gsctx;
228 const char *shared_type;
230 ji = mini_jit_info_table_find_ext (domain, ip, TRUE, &target_domain);
231 if (ji && ji->is_trampoline) {
232 MonoTrampInfo *tinfo = ji->d.tramp_info;
234 printf ("IP %p is at offset 0x%x of trampoline '%s'.\n", ip, (int)((guint8*)ip - tinfo->code), tinfo->name);
240 user_data.method = NULL;
241 mono_domain_lock (domain);
242 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
243 mono_domain_unlock (domain);
245 if (user_data.method) {
246 char *mname = mono_method_full_name (user_data.method, TRUE);
247 printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
252 g_print ("No method at %p\n", ip);
256 method = mono_method_full_name (jinfo_get_method (ji), TRUE);
257 source = mono_debug_lookup_source_location (jinfo_get_method (ji), (guint32)((guint8*)ip - (guint8*)ji->code_start), target_domain);
259 gsctx = mono_jit_info_get_generic_sharing_context (ji);
262 if (gsctx->var_is_vt || gsctx->mvar_is_vt)
263 shared_type = "gsharedvt ";
265 shared_type = "gshared ";
268 g_print ("IP %p at offset 0x%x of %smethod %s (%p %p)[domain %p - %s]\n", ip, (int)((char*)ip - (char*)ji->code_start), shared_type, method, ji->code_start, (char*)ji->code_start + ji->code_size, target_domain, target_domain->friendly_name);
271 g_print ("%s:%d\n", source->source_file, source->row);
274 mono_debug_free_source_location (source);
279 * mono_method_same_domain:
281 * Determine whenever two compiled methods are in the same domain, thus
282 * the address of the callee can be embedded in the caller.
284 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
288 if (!caller || !callee)
292 * If the call was made from domain-neutral to domain-specific
293 * code, we can't patch the call site.
295 if (caller->domain_neutral && !callee->domain_neutral)
298 cmethod = jinfo_get_method (caller);
299 if ((cmethod->klass == mono_defaults.appdomain_class) &&
300 (strstr (cmethod->name, "InvokeInDomain"))) {
301 /* The InvokeInDomain methods change the current appdomain */
309 * mono_global_codeman_reserve:
311 * Allocate code memory from the global code manager.
313 void *mono_global_codeman_reserve (int size)
318 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
320 if (!global_codeman) {
321 /* This can happen during startup */
322 global_codeman = mono_code_manager_new ();
323 return mono_code_manager_reserve (global_codeman, size);
327 ptr = mono_code_manager_reserve (global_codeman, size);
333 #if defined(__native_client_codegen__) && defined(__native_client__)
337 #ifdef __native_client_gc__
338 __nacl_suspend_thread_if_needed();
342 /* Given the temporary buffer (allocated by mono_global_codeman_reserve) into
343 * which we are generating code, return a pointer to the destination in the
344 * dynamic code segment into which the code will be copied when
345 * mono_global_codeman_commit is called.
346 * LOCKING: Acquires the jit lock.
349 nacl_global_codeman_get_dest (void *data)
353 dest = nacl_code_manager_get_code_dest (global_codeman, data);
359 mono_global_codeman_commit (void *data, int size, int newsize)
362 mono_code_manager_commit (global_codeman, data, size, newsize);
367 * Convenience function which calls mono_global_codeman_commit to validate and
368 * copy the code. The caller sets *buf_base and *buf_size to the start and size
369 * of the buffer (allocated by mono_global_codeman_reserve), and *code_end to
370 * the byte after the last instruction byte. On return, *buf_base will point to
371 * the start of the copied in the code segment, and *code_end will point after
372 * the end of the copied code.
375 nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
377 guint8 *tmp = nacl_global_codeman_get_dest (*buf_base);
378 mono_global_codeman_commit (*buf_base, buf_size, *code_end - *buf_base);
379 *code_end = tmp + (*code_end - *buf_base);
383 /* no-op versions of Native Client functions */
385 nacl_global_codeman_get_dest (void *data)
391 mono_global_codeman_commit (void *data, int size, int newsize)
396 nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
400 #endif /* __native_client__ */
403 * mono_create_unwind_op:
405 * Create an unwind op with the given parameters.
408 mono_create_unwind_op (int when, int tag, int reg, int val)
410 MonoUnwindOp *op = g_new0 (MonoUnwindOp, 1);
421 mono_jump_info_token_new2 (MonoMemPool *mp, MonoImage *image, guint32 token, MonoGenericContext *context)
423 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
426 res->has_context = context != NULL;
428 memcpy (&res->context, context, sizeof (MonoGenericContext));
434 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
436 return mono_jump_info_token_new2 (mp, image, token, NULL);
440 * mono_tramp_info_create:
442 * Create a MonoTrampInfo structure from the arguments. This function assumes ownership
443 * of JI, and UNWIND_OPS.
446 mono_tramp_info_create (const char *name, guint8 *code, guint32 code_size, MonoJumpInfo *ji, GSList *unwind_ops)
448 MonoTrampInfo *info = g_new0 (MonoTrampInfo, 1);
450 info->name = g_strdup ((char*)name);
452 info->code_size = code_size;
454 info->unwind_ops = unwind_ops;
460 mono_tramp_info_free (MonoTrampInfo *info)
467 for (l = info->unwind_ops; l; l = l->next)
469 g_slist_free (info->unwind_ops);
474 register_trampoline_jit_info (MonoDomain *domain, MonoTrampInfo *info)
478 ji = mono_domain_alloc0 (domain, mono_jit_info_size (0, 0, 0));
479 mono_jit_info_init (ji, NULL, info->code, info->code_size, 0, 0, 0);
480 ji->d.tramp_info = info;
481 ji->is_trampoline = TRUE;
482 // FIXME: Unwind info
484 mono_jit_info_table_add (domain, ji);
488 * mono_tramp_info_register:
490 * Remember INFO for use by xdebug, mono_print_method_from_ip (), jit maps, etc.
495 mono_tramp_info_register (MonoTrampInfo *info)
502 copy = g_new0 (MonoTrampInfo, 1);
503 copy->code = info->code;
504 copy->code_size = info->code_size;
505 copy->name = g_strdup (info->name);
508 tramp_infos = g_slist_prepend (tramp_infos, copy);
511 mono_save_trampoline_xdebug_info (info);
513 if (mono_get_root_domain ())
514 register_trampoline_jit_info (mono_get_root_domain (), copy);
516 if (mono_jit_map_is_enabled ())
517 mono_emit_jit_tramp (info->code, info->code_size, info->name);
519 mono_tramp_info_free (info);
523 mono_tramp_info_cleanup (void)
527 for (l = tramp_infos; l; l = l->next) {
528 MonoTrampInfo *info = l->data;
530 mono_tramp_info_free (info);
532 g_slist_free (tramp_infos);
535 /* Register trampolines created before the root domain was created in the jit info tables */
537 register_trampolines (MonoDomain *domain)
541 for (l = tramp_infos; l; l = l->next) {
542 MonoTrampInfo *info = l->data;
544 register_trampoline_jit_info (domain, info);
548 G_GNUC_UNUSED static void
554 * Runtime debugging tool, use if (debug_count ()) <x> else <y> to do <x> the first COUNT times, then do <y> afterwards.
555 * Set a breakpoint in break_count () to break the last time <x> is done.
557 G_GNUC_UNUSED gboolean
558 mono_debug_count (void)
560 static int count = 0;
561 static gboolean inited;
562 static const char *value;
567 value = g_getenv ("COUNT");
574 if (count == atoi (value))
577 if (count > atoi (value))
584 mono_icall_get_wrapper_full (MonoJitICallInfo* callinfo, gboolean do_compile)
588 gconstpointer trampoline;
589 MonoDomain *domain = mono_get_root_domain ();
590 gboolean check_exc = check_for_pending_exc;
592 if (callinfo->wrapper) {
593 return callinfo->wrapper;
596 if (callinfo->trampoline)
597 return callinfo->trampoline;
600 * We use the lock on the root domain instead of the JIT lock to protect
601 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
603 mono_loader_lock (); /*FIXME mono_compile_method requires the loader lock, by large.*/
604 mono_domain_lock (domain);
606 if (callinfo->trampoline) {
607 mono_domain_unlock (domain);
608 mono_loader_unlock ();
609 return callinfo->trampoline;
612 if (!strcmp (callinfo->name, "mono_thread_interruption_checkpoint"))
613 /* This icall is used to check for exceptions, so don't check in the wrapper */
616 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
617 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_exc);
621 trampoline = mono_compile_method (wrapper);
623 trampoline = mono_create_ftnptr (domain, mono_create_jit_trampoline_in_domain (domain, wrapper));
624 mono_register_jit_icall_wrapper (callinfo, trampoline);
626 callinfo->trampoline = trampoline;
628 mono_domain_unlock (domain);
629 mono_loader_unlock ();
631 return callinfo->trampoline;
635 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
637 return mono_icall_get_wrapper_full (callinfo, FALSE);
640 static MonoJitDynamicMethodInfo*
641 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
643 MonoJitDynamicMethodInfo *res;
645 if (domain_jit_info (domain)->dynamic_code_hash)
646 res = g_hash_table_lookup (domain_jit_info (domain)->dynamic_code_hash, method);
653 register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, const char *symbol, gboolean no_throw)
655 return mini_register_opcode_emulation (opcode, name, sigstr, func, symbol, no_throw);
659 * For JIT icalls implemented in C.
660 * NAME should be the same as the name of the C function whose address is FUNC.
661 * If SAVE is TRUE, no wrapper is generated. This is for perf critical icalls which
662 * can't throw exceptions.
665 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
667 MonoMethodSignature *sig;
670 sig = mono_create_icall_signature (sigstr);
674 mono_register_jit_icall_full (func, name, sig, save, FALSE, save ? name : NULL);
678 register_dyn_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
680 MonoMethodSignature *sig;
683 sig = mono_create_icall_signature (sigstr);
687 mono_register_jit_icall (func, name, sig, save);
690 #ifdef MONO_HAVE_FAST_TLS
691 MONO_FAST_TLS_DECLARE(mono_lmf_addr);
692 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
694 * When this is defined, the current lmf is stored in this tls variable instead of in
697 MONO_FAST_TLS_DECLARE(mono_lmf);
702 mono_get_jit_tls_offset (void)
708 offset = mono_jit_tls_id;
710 /* FIXME: Happens during startup */
713 MONO_THREAD_VAR_OFFSET (mono_jit_tls, offset);
719 mono_get_lmf_tls_offset (void)
721 #if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
723 MONO_THREAD_VAR_OFFSET(mono_lmf,offset);
731 mono_get_lmf_addr_tls_offset (void)
734 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
741 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
742 return MONO_FAST_TLS_GET (mono_lmf);
744 MonoJitTlsData *jit_tls;
746 if ((jit_tls = mono_native_tls_get_value (mono_jit_tls_id)))
749 * We do not assert here because this function can be called from
750 * mini-gc.c on a thread that has not executed any managed code, yet
751 * (the thread object allocation can trigger a collection).
758 mono_get_lmf_addr (void)
760 #ifdef MONO_HAVE_FAST_TLS
761 return MONO_FAST_TLS_GET (mono_lmf_addr);
763 MonoJitTlsData *jit_tls;
765 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
766 if (G_LIKELY (jit_tls))
767 return &jit_tls->lmf;
770 * When resolving the call to mono_jit_thread_attach full-aot will look
771 * in the plt, which causes a call into the generic trampoline, which in turn
772 * tries to resolve the lmf_addr creating a cyclic dependency. We cannot
773 * call mono_jit_thread_attach from the native-to-managed wrapper, without
774 * mono_get_lmf_addr, and mono_get_lmf_addr requires the thread to be attached.
777 mono_jit_thread_attach (NULL);
779 if ((jit_tls = mono_native_tls_get_value (mono_jit_tls_id)))
780 return &jit_tls->lmf;
782 g_assert_not_reached ();
788 mono_set_lmf (MonoLMF *lmf)
790 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
791 MONO_FAST_TLS_SET (mono_lmf, lmf);
794 (*mono_get_lmf_addr ()) = lmf;
798 mono_get_jit_tls (void)
800 return mono_native_tls_get_value (mono_jit_tls_id);
804 mono_set_jit_tls (MonoJitTlsData *jit_tls)
806 MonoThreadInfo *info;
808 mono_native_tls_set_value (mono_jit_tls_id, jit_tls);
810 #ifdef MONO_HAVE_FAST_TLS
811 MONO_FAST_TLS_SET (mono_jit_tls, jit_tls);
814 /* Save it into MonoThreadInfo so it can be accessed by mono_thread_state_init_from_handle () */
815 info = mono_thread_info_current ();
817 mono_thread_info_tls_set (info, TLS_KEY_JIT_TLS, jit_tls);
821 mono_set_lmf_addr (gpointer lmf_addr)
823 MonoThreadInfo *info;
825 #ifdef MONO_HAVE_FAST_TLS
826 MONO_FAST_TLS_SET (mono_lmf_addr, lmf_addr);
829 /* Save it into MonoThreadInfo so it can be accessed by mono_thread_state_init_from_handle () */
830 info = mono_thread_info_current ();
832 mono_thread_info_tls_set (info, TLS_KEY_LMF_ADDR, lmf_addr);
836 * mono_jit_thread_attach:
838 * Called by native->managed wrappers. Returns the original domain which needs to be
842 mono_jit_thread_attach (MonoDomain *domain)
848 * Happens when called from AOTed code which is only used in the root
851 domain = mono_get_root_domain ();
853 #ifdef MONO_HAVE_FAST_TLS
854 if (!MONO_FAST_TLS_GET (mono_lmf_addr)) {
855 mono_thread_attach (domain);
857 mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background);
860 if (!mono_native_tls_get_value (mono_jit_tls_id)) {
861 mono_thread_attach (domain);
862 mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background);
865 orig = mono_domain_get ();
867 mono_domain_set (domain, TRUE);
869 return orig != domain ? orig : NULL;
872 /* Called by native->managed wrappers */
874 mono_jit_set_domain (MonoDomain *domain)
877 mono_domain_set (domain, TRUE);
882 * @obj: exception object
884 * abort the thread, print exception information and stack trace
887 mono_thread_abort (MonoObject *obj)
889 /* MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id); */
891 /* handle_remove should be eventually called for this thread, too
894 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY) ||
895 (obj->vtable->klass == mono_defaults.threadabortexception_class)) {
898 mono_invoke_unhandled_exception_hook (obj);
903 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
905 MonoJitTlsData *jit_tls;
908 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
912 jit_tls = g_new0 (MonoJitTlsData, 1);
914 jit_tls->abort_func = abort_func;
915 jit_tls->end_of_stack = stack_start;
917 mono_set_jit_tls (jit_tls);
919 lmf = g_new0 (MonoLMF, 1);
920 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf);
922 jit_tls->first_lmf = lmf;
924 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
925 /* jit_tls->lmf is unused */
926 MONO_FAST_TLS_SET (mono_lmf, lmf);
927 mono_set_lmf_addr (MONO_FAST_TLS_ADDR (mono_lmf));
929 mono_set_lmf_addr (&jit_tls->lmf);
934 #ifdef MONO_ARCH_HAVE_TLS_INIT
935 mono_arch_tls_init ();
938 mono_setup_altstack (jit_tls);
944 free_jit_tls_data (MonoJitTlsData *jit_tls)
946 mono_arch_free_jit_tls_data (jit_tls);
947 mono_free_altstack (jit_tls);
949 g_free (jit_tls->first_lmf);
954 mono_thread_start_cb (intptr_t tid, gpointer stack_start, gpointer func)
956 MonoThreadInfo *thread;
957 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
958 thread = mono_thread_info_current_unchecked ();
960 thread->jit_data = jit_tls;
962 mono_arch_cpu_init ();
965 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
968 mono_thread_abort_dummy (MonoObject *obj)
970 if (mono_thread_attach_aborted_cb)
971 mono_thread_attach_aborted_cb (obj);
973 mono_thread_abort (obj);
977 mono_thread_attach_cb (intptr_t tid, gpointer stack_start)
979 MonoThreadInfo *thread;
980 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
981 thread = mono_thread_info_current_unchecked ();
983 thread->jit_data = jit_tls;
984 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
985 mono_runtime_setup_stat_profiler ();
987 mono_arch_cpu_init ();
991 mini_thread_cleanup (MonoNativeThreadId tid)
993 MonoJitTlsData *jit_tls = NULL;
994 MonoThreadInfo *info;
996 info = mono_thread_info_current_unchecked ();
998 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
999 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
1000 * not a trivial thing.
1002 * The current offender is mono_thread_manage which cleanup threads from the outside.
1004 if (info && mono_thread_info_get_tid (info) == tid) {
1005 jit_tls = info->jit_data;
1006 info->jit_data = NULL;
1008 mono_set_jit_tls (NULL);
1010 /* If we attach a thread but never call into managed land, we might never get an lmf.*/
1011 if (mono_get_lmf ()) {
1012 mono_set_lmf (NULL);
1013 mono_set_lmf_addr (NULL);
1016 info = mono_thread_info_lookup (tid);
1018 jit_tls = info->jit_data;
1019 info->jit_data = NULL;
1021 mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
1025 free_jit_tls_data (jit_tls);
1029 mini_get_tls_offset (MonoTlsKey key)
1034 case TLS_KEY_THREAD:
1035 offset = mono_thread_get_tls_offset ();
1037 case TLS_KEY_JIT_TLS:
1038 offset = mono_get_jit_tls_offset ();
1040 case TLS_KEY_DOMAIN:
1041 offset = mono_domain_get_tls_offset ();
1044 offset = mono_get_lmf_tls_offset ();
1046 case TLS_KEY_LMF_ADDR:
1047 offset = mono_get_lmf_addr_tls_offset ();
1050 offset = mono_tls_key_get_offset (key);
1051 g_assert (offset != -1);
1059 mini_tls_key_supported (MonoTlsKey key)
1061 if (!MONO_ARCH_HAVE_TLS_GET)
1064 return mini_get_tls_offset (key) != -1;
1068 mono_patch_info_list_prepend (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target)
1070 MonoJumpInfo *ji = g_new0 (MonoJumpInfo, 1);
1074 ji->data.target = target;
1081 * mono_patch_info_dup_mp:
1083 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
1086 mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
1088 MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
1089 memcpy (res, patch_info, sizeof (MonoJumpInfo));
1091 switch (patch_info->type) {
1092 case MONO_PATCH_INFO_RVA:
1093 case MONO_PATCH_INFO_LDSTR:
1094 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1095 case MONO_PATCH_INFO_LDTOKEN:
1096 case MONO_PATCH_INFO_DECLSEC:
1097 res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
1098 memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
1100 case MONO_PATCH_INFO_SWITCH:
1101 res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
1102 memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
1103 res->data.table->table = mono_mempool_alloc (mp, sizeof (MonoBasicBlock*) * patch_info->data.table->table_size);
1104 memcpy (res->data.table->table, patch_info->data.table->table, sizeof (MonoBasicBlock*) * patch_info->data.table->table_size);
1106 case MONO_PATCH_INFO_RGCTX_FETCH:
1107 res->data.rgctx_entry = mono_mempool_alloc (mp, sizeof (MonoJumpInfoRgctxEntry));
1108 memcpy (res->data.rgctx_entry, patch_info->data.rgctx_entry, sizeof (MonoJumpInfoRgctxEntry));
1109 res->data.rgctx_entry->data = mono_patch_info_dup_mp (mp, res->data.rgctx_entry->data);
1111 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1112 res->data.del_tramp = mono_mempool_alloc0 (mp, sizeof (MonoDelegateClassMethodPair));
1113 memcpy (res->data.del_tramp, patch_info->data.del_tramp, sizeof (MonoDelegateClassMethodPair));
1115 case MONO_PATCH_INFO_GSHAREDVT_CALL:
1116 res->data.gsharedvt = mono_mempool_alloc (mp, sizeof (MonoJumpInfoGSharedVtCall));
1117 memcpy (res->data.gsharedvt, patch_info->data.gsharedvt, sizeof (MonoJumpInfoGSharedVtCall));
1119 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
1120 MonoGSharedVtMethodInfo *info;
1121 MonoGSharedVtMethodInfo *oinfo;
1124 oinfo = patch_info->data.gsharedvt_method;
1125 info = mono_mempool_alloc (mp, sizeof (MonoGSharedVtMethodInfo));
1126 res->data.gsharedvt_method = info;
1127 memcpy (info, oinfo, sizeof (MonoGSharedVtMethodInfo));
1128 info->entries = mono_mempool_alloc (mp, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
1129 for (i = 0; i < oinfo->num_entries; ++i) {
1130 MonoRuntimeGenericContextInfoTemplate *otemplate = &oinfo->entries [i];
1131 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
1133 memcpy (template, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
1135 //info->locals_types = mono_mempool_alloc0 (mp, info->nlocals * sizeof (MonoType*));
1136 //memcpy (info->locals_types, oinfo->locals_types, info->nlocals * sizeof (MonoType*));
1139 case MONO_PATCH_INFO_VIRT_METHOD: {
1140 MonoJumpInfoVirtMethod *info;
1141 MonoJumpInfoVirtMethod *oinfo;
1143 oinfo = patch_info->data.virt_method;
1144 info = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoVirtMethod));
1145 res->data.virt_method = info;
1146 memcpy (info, oinfo, sizeof (MonoJumpInfoVirtMethod));
1157 mono_patch_info_hash (gconstpointer data)
1159 const MonoJumpInfo *ji = (MonoJumpInfo*)data;
1162 case MONO_PATCH_INFO_RVA:
1163 case MONO_PATCH_INFO_LDSTR:
1164 case MONO_PATCH_INFO_LDTOKEN:
1165 case MONO_PATCH_INFO_DECLSEC:
1166 return (ji->type << 8) | ji->data.token->token;
1167 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1168 return (ji->type << 8) | ji->data.token->token | (ji->data.token->has_context ? (gsize)ji->data.token->context.class_inst : 0);
1169 case MONO_PATCH_INFO_INTERNAL_METHOD:
1170 return (ji->type << 8) | g_str_hash (ji->data.name);
1171 case MONO_PATCH_INFO_VTABLE:
1172 case MONO_PATCH_INFO_CLASS:
1173 case MONO_PATCH_INFO_IID:
1174 case MONO_PATCH_INFO_ADJUSTED_IID:
1175 case MONO_PATCH_INFO_CLASS_INIT:
1176 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1177 case MONO_PATCH_INFO_METHODCONST:
1178 case MONO_PATCH_INFO_METHOD:
1179 case MONO_PATCH_INFO_METHOD_JUMP:
1180 case MONO_PATCH_INFO_IMAGE:
1181 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1182 case MONO_PATCH_INFO_ICALL_ADDR:
1183 case MONO_PATCH_INFO_FIELD:
1184 case MONO_PATCH_INFO_SFLDA:
1185 case MONO_PATCH_INFO_SEQ_POINT_INFO:
1186 case MONO_PATCH_INFO_METHOD_RGCTX:
1187 case MONO_PATCH_INFO_SIGNATURE:
1188 case MONO_PATCH_INFO_TLS_OFFSET:
1189 case MONO_PATCH_INFO_METHOD_CODE_SLOT:
1190 return (ji->type << 8) | (gssize)ji->data.target;
1191 case MONO_PATCH_INFO_GSHAREDVT_CALL:
1192 return (ji->type << 8) | (gssize)ji->data.gsharedvt->method;
1193 case MONO_PATCH_INFO_RGCTX_FETCH: {
1194 MonoJumpInfoRgctxEntry *e = ji->data.rgctx_entry;
1196 return (ji->type << 8) | (gssize)e->method | (e->in_mrgctx) | e->info_type | mono_patch_info_hash (e->data);
1198 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1199 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
1200 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
1201 case MONO_PATCH_INFO_GC_NURSERY_START:
1202 case MONO_PATCH_INFO_JIT_TLS_ID:
1203 case MONO_PATCH_INFO_MONITOR_ENTER:
1204 case MONO_PATCH_INFO_MONITOR_ENTER_V4:
1205 case MONO_PATCH_INFO_MONITOR_EXIT:
1206 case MONO_PATCH_INFO_GOT_OFFSET:
1207 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG:
1208 return (ji->type << 8);
1209 case MONO_PATCH_INFO_CASTCLASS_CACHE:
1210 return (ji->type << 8) | (ji->data.index);
1211 case MONO_PATCH_INFO_SWITCH:
1212 return (ji->type << 8) | ji->data.table->table_size;
1213 case MONO_PATCH_INFO_GSHAREDVT_METHOD:
1214 return (ji->type << 8) | (gssize)ji->data.gsharedvt_method->method;
1215 case MONO_PATCH_INFO_OBJC_SELECTOR_REF:
1216 /* Hash on the selector name */
1217 return g_str_hash (ji->data.target);
1218 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1219 return (ji->type << 8) | (gsize)ji->data.del_tramp->klass | (gsize)ji->data.del_tramp->method | (gsize)ji->data.del_tramp->virtual;
1220 case MONO_PATCH_INFO_LDSTR_LIT:
1221 return g_str_hash (ji->data.target);
1222 case MONO_PATCH_INFO_VIRT_METHOD: {
1223 MonoJumpInfoVirtMethod *info = ji->data.virt_method;
1225 return (ji->type << 8) | (gssize)info->klass | (gssize)info->method;
1228 printf ("info type: %d\n", ji->type);
1229 mono_print_ji (ji); printf ("\n");
1230 g_assert_not_reached ();
1236 * mono_patch_info_equal:
1238 * This might fail to recognize equivalent patches, i.e. floats, so its only
1239 * usable in those cases where this is not a problem, i.e. sharing GOT slots
1243 mono_patch_info_equal (gconstpointer ka, gconstpointer kb)
1245 const MonoJumpInfo *ji1 = (MonoJumpInfo*)ka;
1246 const MonoJumpInfo *ji2 = (MonoJumpInfo*)kb;
1248 if (ji1->type != ji2->type)
1251 switch (ji1->type) {
1252 case MONO_PATCH_INFO_RVA:
1253 case MONO_PATCH_INFO_LDSTR:
1254 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1255 case MONO_PATCH_INFO_LDTOKEN:
1256 case MONO_PATCH_INFO_DECLSEC:
1257 if ((ji1->data.token->image != ji2->data.token->image) ||
1258 (ji1->data.token->token != ji2->data.token->token) ||
1259 (ji1->data.token->has_context != ji2->data.token->has_context) ||
1260 (ji1->data.token->context.class_inst != ji2->data.token->context.class_inst) ||
1261 (ji1->data.token->context.method_inst != ji2->data.token->context.method_inst))
1264 case MONO_PATCH_INFO_INTERNAL_METHOD:
1265 return g_str_equal (ji1->data.name, ji2->data.name);
1266 case MONO_PATCH_INFO_RGCTX_FETCH: {
1267 MonoJumpInfoRgctxEntry *e1 = ji1->data.rgctx_entry;
1268 MonoJumpInfoRgctxEntry *e2 = ji2->data.rgctx_entry;
1270 return e1->method == e2->method && e1->in_mrgctx == e2->in_mrgctx && e1->info_type == e2->info_type && mono_patch_info_equal (e1->data, e2->data);
1272 case MONO_PATCH_INFO_GSHAREDVT_CALL: {
1273 MonoJumpInfoGSharedVtCall *c1 = ji1->data.gsharedvt;
1274 MonoJumpInfoGSharedVtCall *c2 = ji2->data.gsharedvt;
1276 return c1->sig == c2->sig && c1->method == c2->method;
1278 case MONO_PATCH_INFO_GSHAREDVT_METHOD:
1279 return ji1->data.gsharedvt_method->method == ji2->data.gsharedvt_method->method;
1280 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1281 return ji1->data.del_tramp->klass == ji2->data.del_tramp->klass && ji1->data.del_tramp->method == ji2->data.del_tramp->method && ji1->data.del_tramp->virtual == ji2->data.del_tramp->virtual;
1282 case MONO_PATCH_INFO_CASTCLASS_CACHE:
1283 return ji1->data.index == ji2->data.index;
1284 case MONO_PATCH_INFO_VIRT_METHOD:
1285 return ji1->data.virt_method->klass == ji2->data.virt_method->klass && ji1->data.virt_method->method == ji2->data.virt_method->method;
1287 if (ji1->data.target != ji2->data.target)
1296 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
1298 unsigned char *ip = patch_info->ip.i + code;
1299 gconstpointer target = NULL;
1301 switch (patch_info->type) {
1302 case MONO_PATCH_INFO_BB:
1304 * FIXME: This could be hit for methods without a prolog. Should use -1
1305 * but too much code depends on a 0 initial value.
1307 //g_assert (patch_info->data.bb->native_offset);
1308 target = patch_info->data.bb->native_offset + code;
1310 case MONO_PATCH_INFO_ABS:
1311 target = patch_info->data.target;
1313 case MONO_PATCH_INFO_LABEL:
1314 target = patch_info->data.inst->inst_c0 + code;
1316 case MONO_PATCH_INFO_IP:
1317 #if defined(__native_client__) && defined(__native_client_codegen__)
1318 /* Need to transform to the destination address, it's */
1319 /* emitted as an immediate in the code. */
1320 target = nacl_inverse_modify_patch_target(ip);
1325 case MONO_PATCH_INFO_METHOD_REL:
1326 target = code + patch_info->data.offset;
1328 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1329 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
1331 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
1332 g_assert_not_reached ();
1334 target = mono_icall_get_wrapper (mi);
1337 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1338 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
1340 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info->data.name);
1341 g_assert_not_reached ();
1346 case MONO_PATCH_INFO_METHOD_JUMP:
1347 target = mono_create_jump_trampoline (domain, patch_info->data.method, FALSE);
1348 #if defined(__native_client__) && defined(__native_client_codegen__)
1349 # if defined(TARGET_AMD64)
1350 /* This target is an absolute address, not relative to the */
1351 /* current code being emitted on AMD64. */
1352 target = nacl_inverse_modify_patch_target(target);
1356 case MONO_PATCH_INFO_METHOD:
1357 #if defined(__native_client_codegen__) && defined(USE_JUMP_TABLES)
1359 * If we use jumptables, for recursive calls we cannot
1360 * avoid trampoline, as we not yet know where we will
1363 target = mono_create_jit_trampoline_in_domain (domain, patch_info->data.method);
1365 if (patch_info->data.method == method) {
1368 /* get the trampoline to the method from the domain */
1369 target = mono_create_jit_trampoline_in_domain (domain, patch_info->data.method);
1373 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
1376 mono_domain_lock (domain);
1377 if (!domain_jit_info (domain)->method_code_hash)
1378 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
1379 code_slot = g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, patch_info->data.method);
1381 code_slot = mono_domain_alloc0 (domain, sizeof (gpointer));
1382 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, patch_info->data.method, code_slot);
1384 mono_domain_unlock (domain);
1388 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG:
1389 #if defined(__native_client_codegen__)
1390 target = (gpointer)&__nacl_thread_suspension_needed;
1391 #elif defined (USE_COOP_GC)
1392 target = (gpointer)&mono_polling_required;
1394 g_error ("Unsuported patch target");
1397 case MONO_PATCH_INFO_SWITCH: {
1398 gpointer *jump_table;
1400 #if defined(__native_client__) && defined(__native_client_codegen__)
1401 /* This memory will leak, but we don't care if we're */
1402 /* not deleting JIT'd methods anyway */
1403 jump_table = g_malloc0 (sizeof(gpointer) * patch_info->data.table->table_size);
1405 if (method && method->dynamic) {
1406 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
1408 if (mono_aot_only) {
1409 jump_table = mono_domain_alloc (domain, sizeof (gpointer) * patch_info->data.table->table_size);
1411 jump_table = mono_domain_code_reserve (domain, sizeof (gpointer) * patch_info->data.table->table_size);
1416 for (i = 0; i < patch_info->data.table->table_size; i++) {
1417 #if defined(__native_client__) && defined(__native_client_codegen__)
1418 /* 'code' is relative to the current code blob, we */
1419 /* need to do this transform on it to make the */
1420 /* pointers in this table absolute */
1421 jump_table [i] = nacl_inverse_modify_patch_target (code) + GPOINTER_TO_INT (patch_info->data.table->table [i]);
1423 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
1427 #if defined(__native_client__) && defined(__native_client_codegen__)
1428 /* jump_table is in the data section, we need to transform */
1429 /* it here so when it gets modified in amd64_patch it will */
1430 /* then point back to the absolute data address */
1431 target = nacl_inverse_modify_patch_target (jump_table);
1433 target = jump_table;
1437 case MONO_PATCH_INFO_METHODCONST:
1438 case MONO_PATCH_INFO_CLASS:
1439 case MONO_PATCH_INFO_IMAGE:
1440 case MONO_PATCH_INFO_FIELD:
1441 case MONO_PATCH_INFO_SIGNATURE:
1442 target = patch_info->data.target;
1444 case MONO_PATCH_INFO_IID:
1445 mono_class_init (patch_info->data.klass);
1446 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
1448 case MONO_PATCH_INFO_ADJUSTED_IID:
1449 mono_class_init (patch_info->data.klass);
1450 target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
1452 case MONO_PATCH_INFO_VTABLE:
1453 target = mono_class_vtable (domain, patch_info->data.klass);
1456 case MONO_PATCH_INFO_CLASS_INIT: {
1457 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.klass);
1460 target = mono_create_class_init_trampoline (vtable);
1463 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE: {
1464 MonoDelegateClassMethodPair *del_tramp = patch_info->data.del_tramp;
1466 if (del_tramp->virtual)
1467 target = mono_create_delegate_virtual_trampoline (domain, del_tramp->klass, del_tramp->method);
1469 target = mono_create_delegate_trampoline_info (domain, del_tramp->klass, del_tramp->method);
1472 case MONO_PATCH_INFO_SFLDA: {
1473 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
1475 if (mono_class_field_is_special_static (patch_info->data.field)) {
1476 gpointer addr = NULL;
1478 mono_domain_lock (domain);
1479 if (domain->special_static_fields)
1480 addr = g_hash_table_lookup (domain->special_static_fields, patch_info->data.field);
1481 mono_domain_unlock (domain);
1487 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
1488 /* Done by the generated code */
1492 mono_runtime_class_init (vtable);
1494 target = (char*)mono_vtable_get_static_field_data (vtable) + patch_info->data.field->offset;
1497 case MONO_PATCH_INFO_RVA: {
1498 guint32 field_index = mono_metadata_token_index (patch_info->data.token->token);
1501 mono_metadata_field_info (patch_info->data.token->image, field_index - 1, NULL, &rva, NULL);
1502 target = mono_image_rva_map (patch_info->data.token->image, rva);
1505 case MONO_PATCH_INFO_R4:
1506 case MONO_PATCH_INFO_R8:
1507 target = patch_info->data.target;
1509 case MONO_PATCH_INFO_EXC_NAME:
1510 target = patch_info->data.name;
1512 case MONO_PATCH_INFO_LDSTR:
1514 mono_ldstr (domain, patch_info->data.token->image,
1515 mono_metadata_token_index (patch_info->data.token->token));
1517 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
1519 MonoClass *handle_class;
1522 handle = mono_ldtoken_checked (patch_info->data.token->image,
1523 patch_info->data.token->token, &handle_class, patch_info->data.token->has_context ? &patch_info->data.token->context : NULL, &error);
1524 if (!mono_error_ok (&error))
1525 g_error ("Could not patch ldtoken due to %s", mono_error_get_message (&error));
1526 mono_class_init (handle_class);
1527 mono_class_init (mono_class_from_mono_type (handle));
1530 mono_type_get_object (domain, handle);
1533 case MONO_PATCH_INFO_LDTOKEN: {
1535 MonoClass *handle_class;
1538 handle = mono_ldtoken_checked (patch_info->data.token->image,
1539 patch_info->data.token->token, &handle_class, patch_info->data.token->has_context ? &patch_info->data.token->context : NULL, &error);
1540 if (!mono_error_ok (&error))
1541 g_error ("Could not patch ldtoken due to %s", mono_error_get_message (&error));
1542 mono_class_init (handle_class);
1547 case MONO_PATCH_INFO_DECLSEC:
1548 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
1550 case MONO_PATCH_INFO_ICALL_ADDR:
1551 /* run_cctors == 0 -> AOT */
1552 if (patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1553 const char *exc_class;
1554 const char *exc_arg;
1557 target = mono_lookup_pinvoke_call (patch_info->data.method, &exc_class, &exc_arg);
1560 mono_raise_exception (mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg));
1561 g_error ("Unable to resolve pinvoke method '%s' Re-run with MONO_LOG_LEVEL=debug for more information.\n", mono_method_full_name (patch_info->data.method, TRUE));
1567 target = mono_lookup_internal_call (patch_info->data.method);
1569 if (!target && run_cctors)
1570 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info->data.method, TRUE));
1573 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1574 target = mono_thread_interruption_request_flag ();
1576 case MONO_PATCH_INFO_METHOD_RGCTX: {
1577 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.method->klass);
1580 target = mono_method_lookup_rgctx (vtable, mini_method_get_context (patch_info->data.method)->method_inst);
1583 case MONO_PATCH_INFO_BB_OVF:
1584 case MONO_PATCH_INFO_EXC_OVF:
1585 case MONO_PATCH_INFO_GOT_OFFSET:
1586 case MONO_PATCH_INFO_NONE:
1588 case MONO_PATCH_INFO_RGCTX_FETCH: {
1589 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
1592 switch (entry->data->type) {
1593 case MONO_PATCH_INFO_CLASS:
1594 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, &entry->data->data.klass->byval_arg, entry->info_type, mono_method_get_context (entry->method));
1596 case MONO_PATCH_INFO_METHOD:
1597 case MONO_PATCH_INFO_METHODCONST:
1598 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, entry->data->data.method, entry->info_type, mono_method_get_context (entry->method));
1600 case MONO_PATCH_INFO_FIELD:
1601 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, entry->data->data.field, entry->info_type, mono_method_get_context (entry->method));
1603 case MONO_PATCH_INFO_SIGNATURE:
1604 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, entry->data->data.sig, entry->info_type, mono_method_get_context (entry->method));
1606 case MONO_PATCH_INFO_GSHAREDVT_CALL: {
1607 MonoJumpInfoGSharedVtCall *call_info = g_malloc0 (sizeof (MonoJumpInfoGSharedVtCall)); //mono_domain_alloc0 (domain, sizeof (MonoJumpInfoGSharedVtCall));
1609 memcpy (call_info, entry->data->data.gsharedvt, sizeof (MonoJumpInfoGSharedVtCall));
1610 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, call_info, entry->info_type, mono_method_get_context (entry->method));
1613 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
1614 MonoGSharedVtMethodInfo *info;
1615 MonoGSharedVtMethodInfo *oinfo = entry->data->data.gsharedvt_method;
1618 /* Make a copy into the domain mempool */
1619 info = g_malloc0 (sizeof (MonoGSharedVtMethodInfo)); //mono_domain_alloc0 (domain, sizeof (MonoGSharedVtMethodInfo));
1620 info->method = oinfo->method;
1621 info->num_entries = oinfo->num_entries;
1622 info->entries = g_malloc0 (sizeof (MonoRuntimeGenericContextInfoTemplate) * info->num_entries);
1623 for (i = 0; i < oinfo->num_entries; ++i) {
1624 MonoRuntimeGenericContextInfoTemplate *otemplate = &oinfo->entries [i];
1625 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
1627 memcpy (template, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
1629 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, info, entry->info_type, mono_method_get_context (entry->method));
1632 case MONO_PATCH_INFO_VIRT_METHOD: {
1633 MonoJumpInfoVirtMethod *info;
1634 MonoJumpInfoVirtMethod *oinfo = entry->data->data.virt_method;
1636 info = g_malloc0 (sizeof (MonoJumpInfoVirtMethod));
1637 memcpy (info, oinfo, sizeof (MonoJumpInfoVirtMethod));
1638 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, info, entry->info_type, mono_method_get_context (entry->method));
1642 g_assert_not_reached ();
1646 target = mono_create_rgctx_lazy_fetch_trampoline (slot);
1649 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1650 target = mono_create_generic_class_init_trampoline ();
1652 case MONO_PATCH_INFO_MONITOR_ENTER:
1653 target = mono_create_monitor_enter_trampoline ();
1655 case MONO_PATCH_INFO_MONITOR_ENTER_V4:
1656 target = mono_create_monitor_enter_v4_trampoline ();
1658 case MONO_PATCH_INFO_MONITOR_EXIT:
1659 target = mono_create_monitor_exit_trampoline ();
1661 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
1662 case MONO_PATCH_INFO_SEQ_POINT_INFO:
1664 /* AOT, not needed */
1667 target = mono_arch_get_seq_point_info (domain, code);
1670 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE:
1671 #ifdef MONO_ARCH_LLVM_SUPPORTED
1672 g_assert (mono_use_llvm);
1673 target = mono_create_llvm_imt_trampoline (domain, patch_info->data.imt_tramp->method, patch_info->data.imt_tramp->vt_offset);
1675 g_assert_not_reached ();
1678 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
1679 int card_table_shift_bits;
1680 gpointer card_table_mask;
1682 target = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
1685 case MONO_PATCH_INFO_GC_NURSERY_START: {
1689 target = mono_gc_get_nursery (&shift_bits, &size);
1692 case MONO_PATCH_INFO_CASTCLASS_CACHE: {
1693 target = mono_domain_alloc0 (domain, sizeof (gpointer));
1696 case MONO_PATCH_INFO_JIT_TLS_ID: {
1697 target = (gpointer) (size_t) mono_jit_tls_id;
1700 case MONO_PATCH_INFO_TLS_OFFSET: {
1703 offset = mini_get_tls_offset (GPOINTER_TO_INT (patch_info->data.target));
1704 #ifdef MONO_ARCH_HAVE_TRANSLATE_TLS_OFFSET
1705 offset = mono_arch_translate_tls_offset (offset);
1707 target = GINT_TO_POINTER (offset);
1710 case MONO_PATCH_INFO_OBJC_SELECTOR_REF: {
1714 case MONO_PATCH_INFO_LDSTR_LIT: {
1718 len = strlen (patch_info->data.target);
1719 s = mono_domain_alloc0 (domain, len + 1);
1720 memcpy (s, patch_info->data.target, len);
1726 g_assert_not_reached ();
1729 return (gpointer)target;
1733 mini_init_gsctx (MonoDomain *domain, MonoMemPool *mp, MonoGenericContext *context, MonoGenericSharingContext *gsctx)
1735 MonoGenericInst *inst;
1738 memset (gsctx, 0, sizeof (MonoGenericSharingContext));
1740 if (context && context->class_inst) {
1741 inst = context->class_inst;
1743 gsctx->var_is_vt = mono_domain_alloc0 (domain, sizeof (gboolean) * inst->type_argc);
1745 gsctx->var_is_vt = mono_mempool_alloc0 (mp, sizeof (gboolean) * inst->type_argc);
1747 gsctx->var_is_vt = g_new0 (gboolean, inst->type_argc);
1749 for (i = 0; i < inst->type_argc; ++i) {
1750 MonoType *type = inst->type_argv [i];
1752 if (mini_is_gsharedvt_gparam (type))
1753 gsctx->var_is_vt [i] = TRUE;
1756 if (context && context->method_inst) {
1757 inst = context->method_inst;
1759 gsctx->mvar_is_vt = mono_domain_alloc0 (domain, sizeof (gboolean) * inst->type_argc);
1761 gsctx->mvar_is_vt = mono_mempool_alloc0 (mp, sizeof (gboolean) * inst->type_argc);
1763 gsctx->mvar_is_vt = g_new0 (gboolean, inst->type_argc);
1765 for (i = 0; i < inst->type_argc; ++i) {
1766 MonoType *type = inst->type_argv [i];
1768 if (mini_is_gsharedvt_gparam (type))
1769 gsctx->mvar_is_vt [i] = TRUE;
1775 * LOCKING: Acquires the jit code hash lock.
1778 mini_lookup_method (MonoDomain *domain, MonoMethod *method, MonoMethod *shared)
1781 static gboolean inited = FALSE;
1782 static int lookups = 0;
1783 static int failed_lookups = 0;
1785 mono_domain_jit_code_hash_lock (domain);
1786 ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, method);
1787 if (!ji && shared) {
1788 /* Try generic sharing */
1789 ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, shared);
1790 if (ji && !ji->has_generic_jit_info)
1793 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT|MONO_COUNTER_GENERICS, &lookups);
1794 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT|MONO_COUNTER_GENERICS, &failed_lookups);
1802 mono_domain_jit_code_hash_unlock (domain);
1808 lookup_method (MonoDomain *domain, MonoMethod *method)
1813 ji = mini_lookup_method (domain, method, NULL);
1816 if (!mono_method_is_generic_sharable (method, FALSE))
1818 shared = mini_get_shared_method (method);
1819 ji = mini_lookup_method (domain, method, shared);
1826 mono_get_jit_info_from_method (MonoDomain *domain, MonoMethod *method)
1828 return lookup_method (domain, method);
1832 static FILE* perf_map_file;
1835 mono_enable_jit_map (void)
1837 if (!perf_map_file) {
1839 g_snprintf (name, sizeof (name), "/tmp/perf-%d.map", getpid ());
1841 perf_map_file = fopen (name, "w");
1846 mono_emit_jit_tramp (void *start, int size, const char *desc)
1849 fprintf (perf_map_file, "%llx %x %s\n", (long long unsigned int)(gsize)start, size, desc);
1853 mono_emit_jit_map (MonoJitInfo *jinfo)
1855 if (perf_map_file) {
1856 char *name = mono_method_full_name (jinfo_get_method (jinfo), TRUE);
1857 mono_emit_jit_tramp (jinfo->code_start, jinfo->code_size, name);
1863 mono_jit_map_is_enabled (void)
1865 return perf_map_file != NULL;
1871 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt, MonoException **ex)
1873 MonoDomain *target_domain, *domain = mono_domain_get ();
1875 gpointer code = NULL, p;
1877 MonoJitICallInfo *callinfo = NULL;
1878 WrapperInfo *winfo = NULL;
1881 * ICALL wrappers are handled specially, since there is only one copy of them
1882 * shared by all appdomains.
1884 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
1885 winfo = mono_marshal_get_wrapper_info (method);
1886 if (winfo && winfo->subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
1887 callinfo = mono_find_jit_icall_by_addr (winfo->d.icall.func);
1888 g_assert (callinfo);
1890 /* Must be domain neutral since there is only one copy */
1891 opt |= MONO_OPT_SHARED;
1894 if (method->dynamic)
1895 opt &= ~MONO_OPT_SHARED;
1897 /* These methods can become invalid when a domain is unloaded */
1898 if (method->klass->image != mono_get_corlib () || method->is_inflated)
1899 opt &= ~MONO_OPT_SHARED;
1901 if (opt & MONO_OPT_SHARED)
1902 target_domain = mono_get_root_domain ();
1904 target_domain = domain;
1906 if (method->wrapper_type == MONO_WRAPPER_UNKNOWN) {
1907 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
1910 if (info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
1911 MonoGenericContext *ctx = NULL;
1912 if (method->is_inflated)
1913 ctx = mono_method_get_context (method);
1914 method = info->d.synchronized_inner.method;
1917 method = mono_class_inflate_generic_method_checked (method, ctx, &error);
1918 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
1923 info = lookup_method (target_domain, method);
1925 /* We can't use a domain specific method in another domain */
1926 if (! ((domain != target_domain) && !info->domain_neutral)) {
1928 MonoException *tmpEx;
1930 mono_jit_stats.methods_lookups++;
1931 vtable = mono_class_vtable (domain, method->klass);
1933 tmpEx = mono_runtime_class_init_full (vtable, ex == NULL);
1938 return mono_create_ftnptr (target_domain, info->code_start);
1942 #ifdef MONO_USE_AOT_COMPILER
1943 if (opt & MONO_OPT_AOT) {
1944 MonoDomain *domain = mono_domain_get ();
1946 mono_class_init (method->klass);
1948 if ((code = mono_aot_get_method (domain, method))) {
1951 vtable = mono_class_vtable (domain, method->klass);
1953 mono_runtime_class_init (vtable);
1959 code = mono_jit_compile_method_inner (method, target_domain, opt, ex);
1963 if (method->wrapper_type == MONO_WRAPPER_WRITE_BARRIER || method->wrapper_type == MONO_WRAPPER_ALLOC) {
1967 * SGEN requires the JIT info for these methods to be registered, see is_ip_in_managed_allocator ().
1969 ji = mini_jit_info_table_find (mono_domain_get (), code, &d);
1973 p = mono_create_ftnptr (target_domain, code);
1976 /*mono_register_jit_icall_wrapper takes the loader lock, so we take it on the outside. */
1977 mono_loader_lock ();
1979 if (!callinfo->wrapper) {
1980 callinfo->wrapper = p;
1981 mono_register_jit_icall_wrapper (callinfo, p);
1984 mono_loader_unlock ();
1991 mono_jit_compile_method (MonoMethod *method)
1993 MonoException *ex = NULL;
1996 code = mono_jit_compile_method_with_opt (method, mono_get_optimizations_for_method (method, default_opt), &ex);
1999 mono_raise_exception (ex);
2005 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
2007 invalidated_delegate_trampoline (char *desc)
2009 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
2010 "See http://www.mono-project.com/Diagnostic:Delegate for an explanation and ways to fix this.",
2016 * mono_jit_free_method:
2018 * Free all memory allocated by the JIT for METHOD.
2021 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
2023 MonoJitDynamicMethodInfo *ji;
2024 gboolean destroy = TRUE;
2025 GHashTableIter iter;
2026 MonoJumpList *jlist;
2028 g_assert (method->dynamic);
2030 mono_domain_lock (domain);
2031 ji = mono_dynamic_code_hash_lookup (domain, method);
2032 mono_domain_unlock (domain);
2037 mono_debug_remove_method (method, domain);
2039 mono_domain_lock (domain);
2040 g_hash_table_remove (domain_jit_info (domain)->dynamic_code_hash, method);
2041 mono_domain_jit_code_hash_lock (domain);
2042 mono_internal_hash_table_remove (&domain->jit_code_hash, method);
2043 mono_domain_jit_code_hash_unlock (domain);
2044 g_hash_table_remove (domain_jit_info (domain)->jump_trampoline_hash, method);
2045 mono_conc_hashtable_remove (domain_jit_info (domain)->runtime_invoke_hash, method);
2047 /* Remove jump targets in this method */
2048 g_hash_table_iter_init (&iter, domain_jit_info (domain)->jump_target_hash);
2049 while (g_hash_table_iter_next (&iter, NULL, (void**)&jlist)) {
2050 GSList *tmp, *remove;
2053 for (tmp = jlist->list; tmp; tmp = tmp->next) {
2054 guint8 *ip = tmp->data;
2056 if (ip >= (guint8*)ji->ji->code_start && ip < (guint8*)ji->ji->code_start + ji->ji->code_size)
2057 remove = g_slist_prepend (remove, tmp);
2059 for (tmp = remove; tmp; tmp = tmp->next) {
2060 jlist->list = g_slist_delete_link (jlist->list, tmp->data);
2062 g_slist_free (remove);
2065 mono_domain_unlock (domain);
2067 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
2068 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
2070 * Instead of freeing the code, change it to call an error routine
2071 * so people can fix their code.
2073 char *type = mono_type_full_name (&method->klass->byval_arg);
2074 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
2077 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
2083 * This needs to be done before freeing code_mp, since the code address is the
2084 * key in the table, so if we free the code_mp first, another thread can grab the
2085 * same code address and replace our entry in the table.
2087 mono_jit_info_table_remove (domain, ji->ji);
2090 mono_code_manager_destroy (ji->code_mp);
2095 mono_jit_find_compiled_method_with_jit_info (MonoDomain *domain, MonoMethod *method, MonoJitInfo **ji)
2097 MonoDomain *target_domain;
2100 if (default_opt & MONO_OPT_SHARED)
2101 target_domain = mono_get_root_domain ();
2103 target_domain = domain;
2105 info = lookup_method (target_domain, method);
2107 /* We can't use a domain specific method in another domain */
2108 if (! ((domain != target_domain) && !info->domain_neutral)) {
2109 mono_jit_stats.methods_lookups++;
2112 return info->code_start;
2121 gboolean mono_do_single_method_regression = FALSE;
2122 guint32 mono_single_method_regression_opt = 0;
2123 MonoMethod *mono_current_single_method;
2124 GSList *mono_single_method_list;
2125 GHashTable *mono_single_method_hash;
2128 mono_get_optimizations_for_method (MonoMethod *method, guint32 default_opt)
2132 if (!mono_do_single_method_regression)
2134 if (!mono_current_single_method) {
2135 if (!mono_single_method_hash)
2136 mono_single_method_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
2137 if (!g_hash_table_lookup (mono_single_method_hash, method)) {
2138 g_hash_table_insert (mono_single_method_hash, method, method);
2139 mono_single_method_list = g_slist_prepend (mono_single_method_list, method);
2143 if (method == mono_current_single_method)
2144 return mono_single_method_regression_opt;
2149 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
2151 return mono_jit_find_compiled_method_with_jit_info (domain, method, NULL);
2156 gpointer compiled_method;
2157 gpointer runtime_invoke;
2159 MonoDynCallInfo *dyn_call_info;
2160 MonoClass *ret_box_class;
2161 } RuntimeInvokeInfo;
2164 * mono_jit_runtime_invoke:
2165 * @method: the method to invoke
2166 * @obj: this pointer
2167 * @params: array of parameter values.
2168 * @exc: used to catch exceptions objects
2171 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
2173 MonoMethod *invoke, *callee;
2174 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
2175 MonoDomain *domain = mono_domain_get ();
2176 MonoJitDomainInfo *domain_info;
2177 RuntimeInvokeInfo *info, *info2;
2179 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
2180 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
2184 domain_info = domain_jit_info (domain);
2186 info = mono_conc_hashtable_lookup (domain_info->runtime_invoke_hash, method);
2189 if (mono_security_core_clr_enabled ()) {
2191 * This might be redundant since mono_class_vtable () already does this,
2192 * but keep it just in case for moonlight.
2194 mono_class_setup_vtable (method->klass);
2195 if (method->klass->exception_type != MONO_EXCEPTION_NONE) {
2197 *exc = (MonoObject*)mono_class_get_exception_for_failure (method->klass);
2199 mono_raise_exception (mono_class_get_exception_for_failure (method->klass));
2204 info = g_new0 (RuntimeInvokeInfo, 1);
2206 invoke = mono_marshal_get_runtime_invoke (method, FALSE);
2207 info->vtable = mono_class_vtable_full (domain, method->klass, TRUE);
2208 g_assert (info->vtable);
2211 if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
2212 (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
2214 * Array Get/Set/Address methods. The JIT implements them using inline code
2215 * inside the runtime invoke wrappers, so no need to compile them.
2217 if (mono_aot_only) {
2219 * Call a wrapper, since the runtime invoke wrapper was not generated.
2221 MonoMethod *wrapper;
2223 wrapper = mono_marshal_get_array_accessor_wrapper (method);
2224 invoke = mono_marshal_get_runtime_invoke (wrapper, FALSE);
2232 MonoException *jit_ex = NULL;
2234 info->compiled_method = mono_jit_compile_method_with_opt (callee, mono_get_optimizations_for_method (callee, default_opt), &jit_ex);
2235 if (!info->compiled_method) {
2239 *exc = (MonoObject*)jit_ex;
2242 mono_raise_exception (jit_ex);
2246 info->compiled_method = mini_add_method_trampoline (NULL, callee, info->compiled_method, mono_method_needs_static_rgctx_invoke (callee, FALSE), FALSE);
2248 info->compiled_method = NULL;
2252 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
2253 * in full-aot mode, so we use a slower, but more generic wrapper if
2254 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
2256 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2257 if (mono_aot_only || debug_options.dyn_runtime_invoke) {
2259 MonoMethodSignature *sig = mono_method_signature (method);
2260 gboolean supported = TRUE;
2263 if (method->string_ctor)
2264 sig = mono_marshal_get_string_ctor_signature (method);
2266 for (i = 0; i < sig->param_count; ++i) {
2267 MonoType *t = sig->params [i];
2269 if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t)))
2273 if (mono_class_is_contextbound (method->klass) || !info->compiled_method)
2277 info->dyn_call_info = mono_arch_dyn_call_prepare (sig);
2279 ret_type = sig->ret;
2280 if (info->dyn_call_info) {
2281 switch (ret_type->type) {
2282 case MONO_TYPE_VOID:
2294 case MONO_TYPE_BOOLEAN:
2295 case MONO_TYPE_CHAR:
2298 info->ret_box_class = mono_class_from_mono_type (ret_type);
2301 info->ret_box_class = mono_defaults.int_class;
2303 case MONO_TYPE_STRING:
2304 case MONO_TYPE_CLASS:
2305 case MONO_TYPE_ARRAY:
2306 case MONO_TYPE_SZARRAY:
2307 case MONO_TYPE_OBJECT:
2309 case MONO_TYPE_GENERICINST:
2310 if (!MONO_TYPE_IS_REFERENCE (ret_type))
2311 info->ret_box_class = mono_class_from_mono_type (ret_type);
2313 case MONO_TYPE_VALUETYPE:
2314 info->ret_box_class = mono_class_from_mono_type (ret_type);
2317 g_assert_not_reached ();
2324 if (!info->dyn_call_info)
2325 info->runtime_invoke = mono_jit_compile_method (invoke);
2327 info2 = mono_conc_hashtable_insert (domain_info->runtime_invoke_hash, method, info);
2334 runtime_invoke = info->runtime_invoke;
2337 * We need this here because mono_marshal_get_runtime_invoke can place
2338 * the helper method in System.Object and not the target class.
2341 *exc = (MonoObject*)mono_runtime_class_init_full (info->vtable, FALSE);
2345 mono_runtime_class_init (info->vtable);
2348 /* The wrappers expect this to be initialized to NULL */
2352 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2353 if (info->dyn_call_info) {
2354 MonoMethodSignature *sig = mono_method_signature (method);
2356 static RuntimeInvokeDynamicFunction dyn_runtime_invoke;
2359 guint8 retval [256];
2361 if (!dyn_runtime_invoke) {
2362 invoke = mono_marshal_get_runtime_invoke_dynamic ();
2363 dyn_runtime_invoke = mono_jit_compile_method (invoke);
2366 /* Convert the arguments to the format expected by start_dyn_call () */
2367 args = g_alloca ((sig->param_count + sig->hasthis) * sizeof (gpointer));
2370 args [pindex ++] = &obj;
2371 for (i = 0; i < sig->param_count; ++i) {
2372 MonoType *t = sig->params [i];
2375 args [pindex ++] = ¶ms [i];
2376 } else if (MONO_TYPE_IS_REFERENCE (t) || t->type == MONO_TYPE_PTR) {
2377 args [pindex ++] = ¶ms [i];
2379 args [pindex ++] = params [i];
2383 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
2385 mono_arch_start_dyn_call (info->dyn_call_info, (gpointer**)args, retval, buf, sizeof (buf));
2387 dyn_runtime_invoke (buf, exc, info->compiled_method);
2389 mono_arch_finish_dyn_call (info->dyn_call_info, buf);
2391 if (info->ret_box_class)
2392 return mono_value_box (domain, info->ret_box_class, retval);
2394 return *(MonoObject**)retval;
2398 return runtime_invoke (obj, params, exc, info->compiled_method);
2401 MONO_SIG_HANDLER_FUNC (, mono_sigfpe_signal_handler)
2403 MonoException *exc = NULL;
2405 MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
2406 MONO_SIG_HANDLER_GET_CONTEXT;
2408 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
2410 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
2411 if (mono_arch_is_int_overflow (ctx, info))
2413 * The spec says this throws ArithmeticException, but MS throws the derived
2414 * OverflowException.
2416 exc = mono_get_exception_overflow ();
2418 exc = mono_get_exception_divide_by_zero ();
2420 exc = mono_get_exception_divide_by_zero ();
2424 if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2427 mono_handle_native_sigsegv (SIGSEGV, ctx, info);
2428 if (mono_do_crash_chaining) {
2429 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
2434 mono_arch_handle_exception (ctx, exc);
2437 MONO_SIG_HANDLER_FUNC (, mono_sigill_signal_handler)
2440 MONO_SIG_HANDLER_GET_CONTEXT;
2442 exc = mono_get_exception_execution_engine ("SIGILL");
2444 mono_arch_handle_exception (ctx, exc);
2447 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
2448 #define HAVE_SIG_INFO
2451 MONO_SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
2454 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2455 gpointer fault_addr = NULL;
2456 #ifdef HAVE_SIG_INFO
2457 MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
2461 MONO_SIG_HANDLER_GET_CONTEXT;
2463 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
2464 if (mono_arch_is_single_step_event (info, ctx)) {
2465 mono_debugger_agent_single_step_event (ctx);
2467 } else if (mono_arch_is_breakpoint_event (info, ctx)) {
2468 mono_debugger_agent_breakpoint_hit (ctx);
2473 #if defined(HAVE_SIG_INFO)
2474 #if !defined(HOST_WIN32)
2475 fault_addr = info->si_addr;
2476 if (mono_aot_is_pagefault (info->si_addr)) {
2477 mono_aot_handle_pagefault (info->si_addr);
2482 /* The thread might no be registered with the runtime */
2483 if (!mono_domain_get () || !jit_tls) {
2484 if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2486 mono_handle_native_sigsegv (SIGSEGV, ctx, info);
2487 if (mono_do_crash_chaining) {
2488 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
2494 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
2496 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2497 if (mono_handle_soft_stack_ovf (jit_tls, ji, ctx, info, (guint8*)info->si_addr))
2500 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2501 /* info->si_addr seems to be NULL on some kernels when handling stack overflows */
2502 fault_addr = info->si_addr;
2503 if (fault_addr == NULL) {
2506 mono_sigctx_to_monoctx (ctx, &mctx);
2508 fault_addr = MONO_CONTEXT_GET_SP (&mctx);
2512 if (jit_tls->stack_size &&
2513 ABS ((guint8*)fault_addr - ((guint8*)jit_tls->end_of_stack - jit_tls->stack_size)) < 8192 * sizeof (gpointer)) {
2515 * The hard-guard page has been hit: there is not much we can do anymore
2516 * Print a hopefully clear message and abort.
2518 mono_handle_hard_stack_ovf (jit_tls, ji, ctx, (guint8*)info->si_addr);
2519 g_assert_not_reached ();
2521 /* The original handler might not like that it is executed on an altstack... */
2522 if (!ji && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2525 mono_arch_handle_altstack_exception (ctx, info, info->si_addr, FALSE);
2530 if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2533 mono_handle_native_sigsegv (SIGSEGV, ctx, info);
2535 if (mono_do_crash_chaining) {
2536 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
2541 mono_arch_handle_exception (ctx, NULL);
2545 MONO_SIG_HANDLER_FUNC (, mono_sigint_signal_handler)
2548 MONO_SIG_HANDLER_GET_CONTEXT;
2550 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
2552 mono_arch_handle_exception (ctx, exc);
2555 #ifndef DISABLE_REMOTING
2556 /* mono_jit_create_remoting_trampoline:
2557 * @method: pointer to the method info
2559 * Creates a trampoline which calls the remoting functions. This
2560 * is used in the vtable of transparent proxies.
2562 * Returns: a pointer to the newly created code
2565 mono_jit_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target)
2568 guint8 *addr = NULL;
2570 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && mono_method_signature (method)->generic_param_count) {
2571 return mono_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING,
2575 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
2576 (mono_method_signature (method)->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))) {
2577 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
2578 addr = mono_compile_method (nm);
2581 addr = mono_compile_method (method);
2583 return mono_get_addr_from_ftnptr (addr);
2587 static gpointer *vtable_trampolines;
2588 static int vtable_trampolines_size;
2591 mini_get_vtable_trampoline (int slot_index)
2593 int index = slot_index + MONO_IMT_SIZE;
2595 g_assert (slot_index >= - MONO_IMT_SIZE);
2596 if (!vtable_trampolines || slot_index + MONO_IMT_SIZE >= vtable_trampolines_size) {
2598 if (!vtable_trampolines || index >= vtable_trampolines_size) {
2602 new_size = vtable_trampolines_size ? vtable_trampolines_size * 2 : 128;
2603 while (new_size <= index)
2605 new_table = g_new0 (gpointer, new_size);
2607 if (vtable_trampolines)
2608 memcpy (new_table, vtable_trampolines, vtable_trampolines_size * sizeof (gpointer));
2609 g_free (vtable_trampolines);
2610 mono_memory_barrier ();
2611 vtable_trampolines = new_table;
2612 vtable_trampolines_size = new_size;
2617 if (!vtable_trampolines [index])
2618 vtable_trampolines [index] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot_index), MONO_TRAMPOLINE_VCALL, mono_get_root_domain (), NULL);
2619 return vtable_trampolines [index];
2623 mono_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method)
2625 gboolean is_virtual_generic, is_interface, load_imt_reg;
2628 static guint8 **cache = NULL;
2629 static int cache_size = 0;
2634 /* FIXME Support more cases */
2638 if (MONO_TYPE_ISSTRUCT (sig->ret))
2641 is_virtual_generic = method->is_inflated && mono_method_get_declaring_generic_method (method)->is_generic;
2642 is_interface = method->klass->flags & TYPE_ATTRIBUTE_INTERFACE ? TRUE : FALSE;
2643 load_imt_reg = is_virtual_generic || is_interface;
2645 if (is_interface && !is_virtual_generic)
2646 offset = ((gint32)mono_method_get_imt_slot (method) - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2648 offset = G_STRUCT_OFFSET (MonoVTable, vtable) + ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2650 idx = (offset / SIZEOF_VOID_P + MONO_IMT_SIZE) * 2 + (load_imt_reg ? 1 : 0);
2651 g_assert (idx >= 0);
2653 /* Resize the cache to idx + 1 */
2654 if (cache_size < idx + 1) {
2656 if (cache_size < idx + 1) {
2658 int new_cache_size = idx + 1;
2660 new_cache = g_new0 (guint8*, new_cache_size);
2662 memcpy (new_cache, cache, cache_size * sizeof (guint8*));
2665 mono_memory_barrier ();
2667 cache_size = new_cache_size;
2675 return cache [idx] = mono_arch_get_delegate_virtual_invoke_impl (sig, method, offset, load_imt_reg);
2679 mini_get_imt_trampoline (int slot_index)
2681 return mini_get_vtable_trampoline (slot_index - MONO_IMT_SIZE);
2685 mini_parse_debug_options (void)
2687 const char *options = g_getenv ("MONO_DEBUG");
2688 gchar **args, **ptr;
2693 args = g_strsplit (options, ",", -1);
2695 for (ptr = args; ptr && *ptr; ptr++) {
2696 const char *arg = *ptr;
2698 if (!strcmp (arg, "handle-sigint"))
2699 debug_options.handle_sigint = TRUE;
2700 else if (!strcmp (arg, "keep-delegates"))
2701 debug_options.keep_delegates = TRUE;
2702 else if (!strcmp (arg, "reverse-pinvoke-exceptions"))
2703 debug_options.reverse_pinvoke_exceptions = TRUE;
2704 else if (!strcmp (arg, "collect-pagefault-stats"))
2705 debug_options.collect_pagefault_stats = TRUE;
2706 else if (!strcmp (arg, "break-on-unverified"))
2707 debug_options.break_on_unverified = TRUE;
2708 else if (!strcmp (arg, "no-gdb-backtrace"))
2709 debug_options.no_gdb_backtrace = TRUE;
2710 else if (!strcmp (arg, "suspend-on-sigsegv"))
2711 debug_options.suspend_on_sigsegv = TRUE;
2712 else if (!strcmp (arg, "suspend-on-exception"))
2713 debug_options.suspend_on_exception = TRUE;
2714 else if (!strcmp (arg, "suspend-on-unhandled"))
2715 debug_options.suspend_on_unhandled = TRUE;
2716 else if (!strcmp (arg, "dont-free-domains"))
2717 mono_dont_free_domains = TRUE;
2718 else if (!strcmp (arg, "dyn-runtime-invoke"))
2719 debug_options.dyn_runtime_invoke = TRUE;
2720 else if (!strcmp (arg, "gdb"))
2721 debug_options.gdb = TRUE;
2722 else if (!strcmp (arg, "explicit-null-checks"))
2723 debug_options.explicit_null_checks = TRUE;
2724 else if (!strcmp (arg, "gen-seq-points"))
2725 debug_options.gen_sdb_seq_points = TRUE;
2726 else if (!strcmp (arg, "gen-compact-seq-points"))
2727 debug_options.gen_seq_points_compact_data = TRUE;
2728 else if (!strcmp (arg, "init-stacks"))
2729 debug_options.init_stacks = TRUE;
2730 else if (!strcmp (arg, "casts"))
2731 debug_options.better_cast_details = TRUE;
2732 else if (!strcmp (arg, "soft-breakpoints"))
2733 debug_options.soft_breakpoints = TRUE;
2734 else if (!strcmp (arg, "check-pinvoke-callconv"))
2735 debug_options.check_pinvoke_callconv = TRUE;
2736 else if (!strcmp (arg, "debug-domain-unload"))
2737 mono_enable_debug_domain_unload (TRUE);
2738 else if (!strcmp (arg, "partial-sharing"))
2739 mono_set_partial_sharing_supported (TRUE);
2741 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
2742 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'reverse-pinvoke-exceptions', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains', 'suspend-on-sigsegv', 'suspend-on-exception', 'suspend-on-unhandled', 'dyn-runtime-invoke', 'gdb', 'explicit-null-checks', 'init-stacks', 'check-pinvoke-callconv', 'debug-domain-unload', 'partial-sharing'\n");
2751 mini_get_debug_options (void)
2753 return &debug_options;
2757 mini_create_ftnptr (MonoDomain *domain, gpointer addr)
2759 #if !defined(__ia64__) && (!defined(__ppc64__) && !defined(__powerpc64__) || _CALL_ELF == 2)
2762 gpointer* desc = NULL;
2764 if ((desc = g_hash_table_lookup (domain->ftnptrs_hash, addr)))
2767 desc = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
2771 # elif defined(__ppc64__) || defined(__powerpc64__)
2773 desc = mono_domain_alloc0 (domain, 3 * sizeof (gpointer));
2779 g_hash_table_insert (domain->ftnptrs_hash, addr, desc);
2785 mini_get_addr_from_ftnptr (gpointer descr)
2787 #if defined(__ia64__) || ((defined(__ppc64__) || defined(__powerpc64__)) && _CALL_ELF != 2)
2788 return *(gpointer*)descr;
2795 register_jit_stats (void)
2797 mono_counters_register ("Compiled methods", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_compiled);
2798 mono_counters_register ("Methods from AOT", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_aot);
2799 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_without_llvm);
2800 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_with_llvm);
2801 mono_counters_register ("Total time spent JITting (sec)", MONO_COUNTER_JIT | MONO_COUNTER_DOUBLE, &mono_jit_stats.jit_time);
2802 mono_counters_register ("Basic blocks", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.basic_blocks);
2803 mono_counters_register ("Max basic blocks", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.max_basic_blocks);
2804 mono_counters_register ("Allocated vars", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.allocate_var);
2805 mono_counters_register ("Code reallocs", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.code_reallocs);
2806 mono_counters_register ("Allocated code size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.allocated_code_size);
2807 mono_counters_register ("Allocated seq points size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.allocated_seq_points_size);
2808 mono_counters_register ("Inlineable methods", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.inlineable_methods);
2809 mono_counters_register ("Inlined methods", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.inlined_methods);
2810 mono_counters_register ("Regvars", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.regvars);
2811 mono_counters_register ("Locals stack size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.locals_stack_size);
2812 mono_counters_register ("Method cache lookups", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_lookups);
2813 mono_counters_register ("Compiled CIL code size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.cil_code_size);
2814 mono_counters_register ("Native code size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.native_code_size);
2815 mono_counters_register ("Aliases found", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.alias_found);
2816 mono_counters_register ("Aliases eliminated", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.alias_removed);
2817 mono_counters_register ("Aliased loads eliminated", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.loads_eliminated);
2818 mono_counters_register ("Aliased stores eliminated", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.stores_eliminated);
2821 static void runtime_invoke_info_free (gpointer value);
2824 class_method_pair_equal (gconstpointer ka, gconstpointer kb)
2826 const MonoClassMethodPair *apair = ka;
2827 const MonoClassMethodPair *bpair = kb;
2829 return apair->klass == bpair->klass && apair->method == bpair->method ? 1 : 0;
2833 class_method_pair_hash (gconstpointer data)
2835 const MonoClassMethodPair *pair = data;
2837 return (gsize)pair->klass ^ (gsize)pair->method;
2841 mini_create_jit_domain_info (MonoDomain *domain)
2843 MonoJitDomainInfo *info = g_new0 (MonoJitDomainInfo, 1);
2845 info->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2846 info->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2847 info->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2848 info->delegate_trampoline_hash = g_hash_table_new (class_method_pair_hash, class_method_pair_equal);
2849 info->llvm_vcall_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2850 info->runtime_invoke_hash = mono_conc_hashtable_new_full (&domain->lock, mono_aligned_addr_hash, NULL, NULL, runtime_invoke_info_free);
2851 info->seq_points = g_hash_table_new_full (mono_aligned_addr_hash, NULL, NULL, mono_seq_point_info_free);
2852 info->arch_seq_points = g_hash_table_new (mono_aligned_addr_hash, NULL);
2853 info->jump_target_hash = g_hash_table_new (NULL, NULL);
2855 domain->runtime_info = info;
2859 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
2861 MonoJumpList *jlist = value;
2862 g_slist_free (jlist->list);
2866 delete_got_slot_list (gpointer key, gpointer value, gpointer user_data)
2868 GSList *list = value;
2869 g_slist_free (list);
2873 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
2875 MonoJitDynamicMethodInfo *di = value;
2876 mono_code_manager_destroy (di->code_mp);
2881 runtime_invoke_info_free (gpointer value)
2883 RuntimeInvokeInfo *info = (RuntimeInvokeInfo*)value;
2885 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2886 if (info->dyn_call_info)
2887 mono_arch_dyn_call_free (info->dyn_call_info);
2893 mini_free_jit_domain_info (MonoDomain *domain)
2895 MonoJitDomainInfo *info = domain_jit_info (domain);
2897 g_hash_table_foreach (info->jump_target_hash, delete_jump_list, NULL);
2898 g_hash_table_destroy (info->jump_target_hash);
2899 if (info->jump_target_got_slot_hash) {
2900 g_hash_table_foreach (info->jump_target_got_slot_hash, delete_got_slot_list, NULL);
2901 g_hash_table_destroy (info->jump_target_got_slot_hash);
2903 if (info->dynamic_code_hash) {
2904 g_hash_table_foreach (info->dynamic_code_hash, dynamic_method_info_free, NULL);
2905 g_hash_table_destroy (info->dynamic_code_hash);
2907 if (info->method_code_hash)
2908 g_hash_table_destroy (info->method_code_hash);
2909 g_hash_table_destroy (info->class_init_trampoline_hash);
2910 g_hash_table_destroy (info->jump_trampoline_hash);
2911 g_hash_table_destroy (info->jit_trampoline_hash);
2912 g_hash_table_destroy (info->delegate_trampoline_hash);
2913 if (info->static_rgctx_trampoline_hash)
2914 g_hash_table_destroy (info->static_rgctx_trampoline_hash);
2915 g_hash_table_destroy (info->llvm_vcall_trampoline_hash);
2916 mono_conc_hashtable_destroy (info->runtime_invoke_hash);
2917 g_hash_table_destroy (info->seq_points);
2918 g_hash_table_destroy (info->arch_seq_points);
2919 if (info->agent_info)
2920 mono_debugger_agent_free_domain_info (domain);
2921 if (info->gsharedvt_arg_tramp_hash)
2922 g_hash_table_destroy (info->gsharedvt_arg_tramp_hash);
2924 mono_llvm_free_domain_info (domain);
2927 g_free (domain->runtime_info);
2928 domain->runtime_info = NULL;
2933 llvm_init_inner (void)
2935 if (!mono_llvm_load (NULL))
2946 * Load and initialize LLVM support.
2947 * Return TRUE on success.
2950 mini_llvm_init (void)
2953 static gboolean llvm_inited;
2954 static gboolean init_result;
2956 mono_loader_lock_if_inited ();
2958 init_result = llvm_init_inner ();
2961 mono_loader_unlock_if_inited ();
2969 mini_init (const char *filename, const char *runtime_version)
2972 MonoRuntimeCallbacks callbacks;
2973 MonoThreadInfoRuntimeCallbacks ticallbacks;
2975 MONO_VES_INIT_BEGIN ();
2977 #if defined(__linux__) && !defined(__native_client__)
2978 if (access ("/proc/self/maps", F_OK) != 0) {
2979 g_print ("Mono requires /proc to be mounted.\n");
2984 mono_mutex_init_recursive (&jit_mutex);
2986 mono_cross_helpers_run ();
2990 /* Happens when using the embedding interface */
2991 if (!default_opt_set)
2992 default_opt = mono_parse_default_optimizations (NULL);
2994 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
2996 mono_set_generic_sharing_vt_supported (TRUE);
2999 #ifdef MONO_HAVE_FAST_TLS
3000 MONO_FAST_TLS_INIT (mono_jit_tls);
3001 MONO_FAST_TLS_INIT (mono_lmf_addr);
3002 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
3003 MONO_FAST_TLS_INIT (mono_lmf);
3007 mono_runtime_set_has_tls_get (MONO_ARCH_HAVE_TLS_GET);
3009 if (!global_codeman)
3010 global_codeman = mono_code_manager_new ();
3012 memset (&callbacks, 0, sizeof (callbacks));
3013 callbacks.create_ftnptr = mini_create_ftnptr;
3014 callbacks.get_addr_from_ftnptr = mini_get_addr_from_ftnptr;
3015 callbacks.get_runtime_build_info = mono_get_runtime_build_info;
3016 callbacks.set_cast_details = mono_set_cast_details;
3017 callbacks.debug_log = mono_debugger_agent_debug_log;
3018 callbacks.debug_log_is_enabled = mono_debugger_agent_debug_log_is_enabled;
3019 callbacks.tls_key_supported = mini_tls_key_supported;
3021 callbacks.get_vtable_trampoline = mini_get_vtable_trampoline;
3022 callbacks.get_imt_trampoline = mini_get_imt_trampoline;
3024 mono_install_callbacks (&callbacks);
3026 memset (&ticallbacks, 0, sizeof (ticallbacks));
3027 ticallbacks.setup_async_callback = mono_setup_async_callback;
3028 ticallbacks.thread_state_init_from_sigctx = mono_thread_state_init_from_sigctx;
3029 ticallbacks.thread_state_init_from_handle = mono_thread_state_init_from_handle;
3031 mono_counters_init ();
3033 mono_threads_runtime_init (&ticallbacks);
3035 if (g_getenv ("MONO_DEBUG") != NULL)
3036 mini_parse_debug_options ();
3038 mono_code_manager_init ();
3042 mono_arch_cpu_init ();
3046 mono_unwind_init ();
3048 #ifdef XDEBUG_ENABLED
3049 if (g_getenv ("MONO_XDEBUG")) {
3050 const char *xdebug_opts = g_getenv ("MONO_XDEBUG");
3051 mono_xdebug_init (xdebug_opts);
3052 /* So methods for multiple domains don't have the same address */
3053 mono_dont_free_domains = TRUE;
3054 mono_using_xdebug = TRUE;
3055 } else if (mini_get_debug_options ()->gdb) {
3056 mono_xdebug_init ((char*)"gdb");
3057 mono_dont_free_domains = TRUE;
3058 mono_using_xdebug = TRUE;
3063 if (mono_use_llvm) {
3064 if (!mono_llvm_load (NULL)) {
3065 mono_use_llvm = FALSE;
3066 fprintf (stderr, "Mono Warning: llvm support could not be loaded.\n");
3073 mono_trampolines_init ();
3075 mono_native_tls_alloc (&mono_jit_tls_id, NULL);
3077 if (default_opt & MONO_OPT_AOT)
3080 mono_debugger_agent_init ();
3082 #ifdef MONO_ARCH_GSHARED_SUPPORTED
3083 mono_set_generic_sharing_supported (TRUE);
3086 #ifndef MONO_CROSS_COMPILE
3087 mono_runtime_install_handlers ();
3089 mono_threads_install_cleanup (mini_thread_cleanup);
3091 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
3092 check_for_pending_exc = FALSE;
3093 mono_threads_install_notify_pending_exc ((MonoThreadNotifyPendingExcFunc)mono_arch_notify_pending_exc);
3096 #define JIT_TRAMPOLINES_WORK
3097 #ifdef JIT_TRAMPOLINES_WORK
3098 mono_install_compile_method (mono_jit_compile_method);
3099 mono_install_free_method (mono_jit_free_method);
3100 mono_install_trampoline (mono_create_jit_trampoline);
3101 mono_install_jump_trampoline (mono_create_jump_trampoline);
3102 #ifndef DISABLE_REMOTING
3103 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
3105 mono_install_delegate_trampoline (mono_create_delegate_trampoline);
3106 mono_install_create_domain_hook (mini_create_jit_domain_info);
3107 mono_install_free_domain_hook (mini_free_jit_domain_info);
3109 #define JIT_INVOKE_WORKS
3110 #ifdef JIT_INVOKE_WORKS
3111 mono_install_runtime_invoke (mono_jit_runtime_invoke);
3113 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
3114 mono_install_get_class_from_name (mono_aot_get_class_from_name);
3115 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
3117 if (debug_options.collect_pagefault_stats)
3118 mono_aot_set_make_unreadable (TRUE);
3120 if (runtime_version)
3121 domain = mono_init_version (filename, runtime_version);
3123 domain = mono_init_from_assembly (filename, filename);
3125 if (mono_aot_only) {
3126 /* This helps catch code allocation requests */
3127 mono_code_manager_set_read_only (domain->code_mp);
3128 mono_marshal_use_aot_wrappers (TRUE);
3132 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk);
3134 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
3136 /*Init arch tls information only after the metadata side is inited to make sure we see dynamic appdomain tls keys*/
3137 mono_arch_finish_init ();
3141 /* This must come after mono_init () in the aot-only case */
3142 mono_exceptions_init ();
3144 /* This should come after mono_init () too */
3148 mono_create_helper_signatures ();
3151 register_jit_stats ();
3153 #define JIT_CALLS_WORK
3154 #ifdef JIT_CALLS_WORK
3155 /* Needs to be called here since register_jit_icall depends on it */
3156 mono_marshal_init ();
3158 mono_arch_register_lowlevel_calls ();
3162 mono_generic_sharing_init ();
3165 #ifdef MONO_ARCH_SIMD_INTRINSICS
3166 mono_simd_intrinsics_init ();
3169 #if MONO_SUPPORT_TASKLETS
3170 mono_tasklets_init ();
3173 register_trampolines (domain);
3175 if (mono_compile_aot)
3177 * Avoid running managed code when AOT compiling, since the platform
3178 * might only support aot-only execution.
3180 mono_runtime_set_no_exec (TRUE);
3182 #define JIT_RUNTIME_WORKS
3183 #ifdef JIT_RUNTIME_WORKS
3184 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
3185 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
3186 mono_thread_attach (domain);
3189 mono_profiler_runtime_initialized ();
3191 MONO_VES_INIT_END ();
3197 register_icalls (void)
3199 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
3200 ves_icall_get_frame_info);
3201 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
3202 ves_icall_get_trace);
3203 mono_add_internal_call ("System.Exception::get_trace",
3204 ves_icall_System_Exception_get_trace);
3205 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
3206 mono_runtime_install_handlers);
3208 #if defined(PLATFORM_ANDROID) || defined(TARGET_ANDROID)
3209 mono_add_internal_call ("System.Diagnostics.Debugger::Mono_UnhandledException_internal",
3210 mono_debugger_agent_unhandled_exception);
3214 * It's important that we pass `TRUE` as the last argument here, as
3215 * it causes the JIT to omit a wrapper for these icalls. If the JIT
3216 * *did* emit a wrapper, we'd be looking at infinite recursion since
3217 * the wrapper would call the icall which would call the wrapper and
3220 register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", "void ptr", TRUE);
3221 register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", "void ptr", TRUE);
3223 register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
3224 register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
3225 register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
3226 register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "ptr ptr", TRUE);
3227 register_icall (mono_jit_set_domain, "mono_jit_set_domain", "void ptr", TRUE);
3228 register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
3230 register_dyn_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
3231 register_dyn_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
3232 register_dyn_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception", "void ptr", TRUE);
3233 register_icall (mono_thread_get_undeniable_exception, "mono_thread_get_undeniable_exception", "object", FALSE);
3234 register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "object", FALSE);
3235 register_icall (mono_thread_force_interruption_checkpoint_noraise, "mono_thread_force_interruption_checkpoint_noraise", "object", FALSE);
3236 register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
3237 #ifndef DISABLE_REMOTING
3238 register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
3239 register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
3242 #if defined(__native_client__) || defined(__native_client_codegen__)
3243 register_icall (mono_nacl_gc, "mono_nacl_gc", "void", TRUE);
3245 #if defined(USE_COOP_GC)
3246 register_icall (mono_threads_state_poll, "mono_threads_state_poll", "void", TRUE);
3249 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
3250 register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, "mono_llmult", TRUE);
3251 register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, "mono_lldiv", FALSE);
3252 register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, "mono_lldiv_un", FALSE);
3253 register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, "mono_llrem", FALSE);
3254 register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, "mono_llrem_un", FALSE);
3256 #if !defined(MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS) || defined(MONO_ARCH_EMULATE_LONG_MUL_OVF_OPTS)
3257 register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, "mono_llmult_ovf_un", FALSE);
3258 register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, "mono_llmult_ovf", FALSE);
3261 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
3262 register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, "mono_lshl", TRUE);
3263 register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, "mono_lshr", TRUE);
3264 register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, "mono_lshr_un", TRUE);
3267 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
3268 register_opcode_emulation (OP_IDIV, "__emul_op_idiv", "int32 int32 int32", mono_idiv, "mono_idiv", FALSE);
3269 register_opcode_emulation (OP_IDIV_UN, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un, "mono_idiv_un", FALSE);
3270 register_opcode_emulation (OP_IREM, "__emul_op_irem", "int32 int32 int32", mono_irem, "mono_irem", FALSE);
3271 register_opcode_emulation (OP_IREM_UN, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un, "mono_irem_un", FALSE);
3274 #ifdef MONO_ARCH_EMULATE_MUL_DIV
3275 register_opcode_emulation (OP_IMUL, "__emul_op_imul", "int32 int32 int32", mono_imul, "mono_imul", TRUE);
3278 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
3279 register_opcode_emulation (OP_IMUL_OVF, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf, "mono_imul_ovf", FALSE);
3280 register_opcode_emulation (OP_IMUL_OVF_UN, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, "mono_imul_ovf_un", FALSE);
3283 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT_FALLBACK)
3284 if (ARCH_EMULATE_MUL_DIV || mono_arch_is_soft_float ()) {
3285 register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, "mono_fdiv", FALSE);
3289 register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, "mono_fconv_u8", FALSE);
3290 register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, "mono_fconv_u4", FALSE);
3291 register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, "mono_fconv_ovf_i8", FALSE);
3292 register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, "mono_fconv_ovf_u8", FALSE);
3293 register_opcode_emulation (OP_RCONV_TO_OVF_I8, "__emul_rconv_to_ovf_i8", "long float", mono_rconv_ovf_i8, "mono_rconv_ovf_i8", FALSE);
3294 register_opcode_emulation (OP_RCONV_TO_OVF_U8, "__emul_rconv_to_ovf_u8", "ulong float", mono_rconv_ovf_u8, "mono_rconv_ovf_u8", FALSE);
3296 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
3297 register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, "mono_fconv_i8", FALSE);
3298 register_opcode_emulation (OP_RCONV_TO_I8, "__emul_rconv_to_i8", "long float", mono_rconv_i8, "mono_rconv_i8", FALSE);
3300 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
3301 register_opcode_emulation (OP_ICONV_TO_R_UN, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un, "mono_conv_to_r8_un", FALSE);
3303 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
3304 register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, "mono_lconv_to_r8", FALSE);
3306 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
3307 register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, "mono_lconv_to_r4", FALSE);
3309 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
3310 register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, "mono_lconv_to_r8_un", FALSE);
3312 #ifdef MONO_ARCH_EMULATE_FREM
3313 #if defined(__default_codegen__)
3314 register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, "fmod", FALSE);
3315 #elif defined(__native_client_codegen__)
3316 register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", mono_fmod, "mono_fmod", FALSE);
3320 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
3321 if (mono_arch_is_soft_float ()) {
3322 register_opcode_emulation (OP_FSUB, "__emul_fsub", "double double double", mono_fsub, "mono_fsub", FALSE);
3323 register_opcode_emulation (OP_FADD, "__emul_fadd", "double double double", mono_fadd, "mono_fadd", FALSE);
3324 register_opcode_emulation (OP_FMUL, "__emul_fmul", "double double double", mono_fmul, "mono_fmul", FALSE);
3325 register_opcode_emulation (OP_FNEG, "__emul_fneg", "double double", mono_fneg, "mono_fneg", FALSE);
3326 register_opcode_emulation (OP_ICONV_TO_R8, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8, "mono_conv_to_r8", FALSE);
3327 register_opcode_emulation (OP_ICONV_TO_R4, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4, "mono_conv_to_r4", FALSE);
3328 register_opcode_emulation (OP_FCONV_TO_R4, "__emul_fconv_to_r4", "double double", mono_fconv_r4, "mono_fconv_r4", FALSE);
3329 register_opcode_emulation (OP_FCONV_TO_I1, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1, "mono_fconv_i1", FALSE);
3330 register_opcode_emulation (OP_FCONV_TO_I2, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2, "mono_fconv_i2", FALSE);
3331 register_opcode_emulation (OP_FCONV_TO_I4, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4, "mono_fconv_i4", FALSE);
3332 register_opcode_emulation (OP_FCONV_TO_U1, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1, "mono_fconv_u1", FALSE);
3333 register_opcode_emulation (OP_FCONV_TO_U2, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2, "mono_fconv_u2", FALSE);
3335 #if SIZEOF_VOID_P == 4
3336 register_opcode_emulation (OP_FCONV_TO_I, "__emul_fconv_to_i", "int32 double", mono_fconv_i4, "mono_fconv_i4", FALSE);
3339 register_opcode_emulation (OP_FBEQ, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq, "mono_fcmp_eq", FALSE);
3340 register_opcode_emulation (OP_FBLT, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt, "mono_fcmp_lt", FALSE);
3341 register_opcode_emulation (OP_FBGT, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt, "mono_fcmp_gt", FALSE);
3342 register_opcode_emulation (OP_FBLE, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le, "mono_fcmp_le", FALSE);
3343 register_opcode_emulation (OP_FBGE, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge, "mono_fcmp_ge", FALSE);
3344 register_opcode_emulation (OP_FBNE_UN, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un, "mono_fcmp_ne_un", FALSE);
3345 register_opcode_emulation (OP_FBLT_UN, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un, "mono_fcmp_lt_un", FALSE);
3346 register_opcode_emulation (OP_FBGT_UN, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un, "mono_fcmp_gt_un", FALSE);
3347 register_opcode_emulation (OP_FBLE_UN, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un, "mono_fcmp_le_un", FALSE);
3348 register_opcode_emulation (OP_FBGE_UN, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un, "mono_fcmp_ge_un", FALSE);
3350 register_opcode_emulation (OP_FCEQ, "__emul_fcmp_ceq", "uint32 double double", mono_fceq, "mono_fceq", FALSE);
3351 register_opcode_emulation (OP_FCGT, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt, "mono_fcgt", FALSE);
3352 register_opcode_emulation (OP_FCGT_UN, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un, "mono_fcgt_un", FALSE);
3353 register_opcode_emulation (OP_FCLT, "__emul_fcmp_clt", "uint32 double double", mono_fclt, "mono_fclt", FALSE);
3354 register_opcode_emulation (OP_FCLT_UN, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un, "mono_fclt_un", FALSE);
3356 register_icall (mono_fload_r4, "mono_fload_r4", "double ptr", FALSE);
3357 register_icall (mono_fstore_r4, "mono_fstore_r4", "void double ptr", FALSE);
3358 register_icall (mono_fload_r4_arg, "mono_fload_r4_arg", "uint32 double", FALSE);
3359 register_icall (mono_isfinite, "mono_isfinite", "uint32 double", FALSE);
3363 #ifdef COMPRESSED_INTERFACE_BITMAP
3364 register_icall (mono_class_interface_match, "mono_class_interface_match", "uint32 ptr int32", TRUE);
3367 #if SIZEOF_REGISTER == 4
3368 register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, "mono_fconv_u4", TRUE);
3370 register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "ulong double", mono_fconv_u8, "mono_fconv_u8", TRUE);
3373 /* other jit icalls */
3374 register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
3375 register_icall (mono_class_static_field_address , "mono_class_static_field_address",
3376 "ptr ptr ptr", FALSE);
3377 register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
3378 register_icall (mono_ldtoken_wrapper_generic_shared, "mono_ldtoken_wrapper_generic_shared",
3379 "ptr ptr ptr ptr", FALSE);
3380 register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
3381 register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
3382 register_icall (mono_helper_stelem_ref_check, "mono_helper_stelem_ref_check", "void object object", FALSE);
3383 register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
3384 register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
3385 register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
3386 register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
3387 register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
3388 register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
3389 register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
3390 register_icall (mono_ldvirtfn_gshared, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE);
3391 register_icall (mono_helper_compile_generic_method, "mono_helper_compile_generic_method", "ptr object ptr ptr", FALSE);
3392 register_icall (mono_helper_ldstr, "mono_helper_ldstr", "object ptr int", FALSE);
3393 register_icall (mono_helper_ldstr_mscorlib, "mono_helper_ldstr_mscorlib", "object int", FALSE);
3394 register_icall (mono_helper_newobj_mscorlib, "mono_helper_newobj_mscorlib", "object int", FALSE);
3395 register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
3396 register_icall (mono_object_castclass_unbox, "mono_object_castclass_unbox", "object object ptr", FALSE);
3397 register_icall (mono_break, "mono_break", NULL, TRUE);
3398 register_icall (mono_create_corlib_exception_0, "mono_create_corlib_exception_0", "object int", TRUE);
3399 register_icall (mono_create_corlib_exception_1, "mono_create_corlib_exception_1", "object int object", TRUE);
3400 register_icall (mono_create_corlib_exception_2, "mono_create_corlib_exception_2", "object int object object", TRUE);
3401 register_icall (mono_array_new_1, "mono_array_new_1", "object ptr int", FALSE);
3402 register_icall (mono_array_new_2, "mono_array_new_2", "object ptr int int", FALSE);
3403 register_icall (mono_array_new_3, "mono_array_new_3", "object ptr int int int", FALSE);
3404 register_icall (mono_array_new_4, "mono_array_new_4", "object ptr int int int int", FALSE);
3405 register_icall (mono_get_native_calli_wrapper, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE);
3406 register_icall (mono_resume_unwind, "mono_resume_unwind", "void", TRUE);
3407 register_icall (mono_gsharedvt_constrained_call, "mono_gsharedvt_constrained_call", "object ptr ptr ptr ptr ptr", FALSE);
3408 register_icall (mono_gsharedvt_value_copy, "mono_gsharedvt_value_copy", "void ptr ptr ptr", TRUE);
3410 register_icall (mono_gc_wbarrier_value_copy_bitmap, "mono_gc_wbarrier_value_copy_bitmap", "void ptr ptr int int", FALSE);
3412 register_icall (mono_object_castclass_with_cache, "mono_object_castclass_with_cache", "object object ptr ptr", FALSE);
3413 register_icall (mono_object_isinst_with_cache, "mono_object_isinst_with_cache", "object object ptr ptr", FALSE);
3415 register_icall (mono_debugger_agent_user_break, "mono_debugger_agent_user_break", "void", FALSE);
3418 register_icall (pthread_getspecific, "pthread_getspecific", "ptr ptr", TRUE);
3422 MonoJitStats mono_jit_stats = {0};
3425 print_jit_stats (void)
3427 if (mono_jit_stats.enabled) {
3428 g_print ("Mono Jit statistics\n");
3429 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
3430 mono_jit_stats.max_ratio_method);
3431 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats.biggest_method_size,
3432 mono_jit_stats.biggest_method);
3434 g_print ("Delegates created: %ld\n", mono_stats.delegate_creations);
3435 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
3436 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
3437 g_print ("Generic vtables: %ld\n", mono_stats.generic_vtable_count);
3438 g_print ("Methods: %ld\n", mono_stats.method_count);
3439 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
3440 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
3441 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults.corlib->mempool));
3443 g_print ("\nInitialized classes: %ld\n", mono_stats.generic_class_count);
3444 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
3445 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats.generic_virtual_invocations);
3447 g_print ("Sharable generic methods: %ld\n", mono_stats.generics_sharable_methods);
3448 g_print ("Unsharable generic methods: %ld\n", mono_stats.generics_unsharable_methods);
3449 g_print ("Shared generic methods: %ld\n", mono_stats.generics_shared_methods);
3450 g_print ("Shared vtype generic methods: %ld\n", mono_stats.gsharedvt_methods);
3452 g_print ("IMT tables size: %ld\n", mono_stats.imt_tables_size);
3453 g_print ("IMT number of tables: %ld\n", mono_stats.imt_number_of_tables);
3454 g_print ("IMT number of methods: %ld\n", mono_stats.imt_number_of_methods);
3455 g_print ("IMT used slots: %ld\n", mono_stats.imt_used_slots);
3456 g_print ("IMT colliding slots: %ld\n", mono_stats.imt_slots_with_collisions);
3457 g_print ("IMT max collisions: %ld\n", mono_stats.imt_max_collisions_in_slot);
3458 g_print ("IMT methods at max col: %ld\n", mono_stats.imt_method_count_when_max_collisions);
3459 g_print ("IMT thunks size: %ld\n", mono_stats.imt_thunks_size);
3461 g_print ("JIT info table inserts: %ld\n", mono_stats.jit_info_table_insert_count);
3462 g_print ("JIT info table removes: %ld\n", mono_stats.jit_info_table_remove_count);
3463 g_print ("JIT info table lookups: %ld\n", mono_stats.jit_info_table_lookup_count);
3465 g_free (mono_jit_stats.max_ratio_method);
3466 mono_jit_stats.max_ratio_method = NULL;
3467 g_free (mono_jit_stats.biggest_method);
3468 mono_jit_stats.biggest_method = NULL;
3473 mini_cleanup (MonoDomain *domain)
3475 mono_runtime_shutdown_stat_profiler ();
3478 cominterop_release_all_rcws ();
3481 #ifndef MONO_CROSS_COMPILE
3483 * mono_domain_finalize () needs to be called early since it needs the
3484 * execution engine still fully working (it may invoke managed finalizers).
3486 mono_domain_finalize (domain, 2000);
3489 /* This accesses metadata so needs to be called before runtime shutdown */
3492 mono_profiler_shutdown ();
3494 #ifndef MONO_CROSS_COMPILE
3495 mono_runtime_cleanup (domain);
3498 free_jit_tls_data (mono_native_tls_get_value (mono_jit_tls_id));
3500 mono_icall_cleanup ();
3502 mono_runtime_cleanup_handlers ();
3504 #ifndef MONO_CROSS_COMPILE
3505 mono_domain_free (domain, TRUE);
3510 mono_llvm_cleanup ();
3513 mono_aot_cleanup ();
3515 mono_trampolines_cleanup ();
3517 mono_unwind_cleanup ();
3519 mono_code_manager_destroy (global_codeman);
3520 g_free (vtable_trampolines);
3522 mini_jit_cleanup ();
3524 mono_tramp_info_cleanup ();
3526 mono_arch_cleanup ();
3528 mono_generic_sharing_cleanup ();
3532 mono_trace_cleanup ();
3534 mono_counters_dump (MONO_COUNTER_SECTION_MASK | MONO_COUNTER_MONOTONIC, stdout);
3536 if (mono_inject_async_exc_method)
3537 mono_method_desc_free (mono_inject_async_exc_method);
3539 mono_native_tls_free (mono_jit_tls_id);
3541 mono_mutex_destroy (&jit_mutex);
3543 mono_mutex_destroy (&mono_delegate_section);
3545 mono_code_manager_cleanup ();
3547 #ifdef USE_JUMP_TABLES
3548 mono_jumptable_cleanup ();
3553 mono_set_defaults (int verbose_level, guint32 opts)
3555 mini_verbose = verbose_level;
3556 mono_set_optimizations (opts);
3560 mono_disable_optimizations (guint32 opts)
3562 default_opt &= ~opts;
3566 mono_set_optimizations (guint32 opts)
3569 default_opt_set = TRUE;
3570 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
3571 mono_set_generic_sharing_vt_supported (mono_aot_only || ((default_opt & MONO_OPT_GSHAREDVT) != 0));
3576 mono_set_verbose_level (guint32 level)
3578 mini_verbose = level;
3582 * mono_get_runtime_build_info:
3584 * Return the runtime version + build date in string format.
3585 * The returned string is owned by the caller. The returned string
3586 * format is "VERSION (FULL_VERSION BUILD_DATE)" and build date is optional.
3589 mono_get_runtime_build_info (void)
3591 if (mono_build_date)
3592 return g_strdup_printf ("%s (%s %s)", VERSION, FULL_VERSION, mono_build_date);
3594 return g_strdup_printf ("%s (%s)", VERSION, FULL_VERSION);
3598 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
3600 GHashTable *assemblies = (GHashTable*)user_data;
3601 MonoImage *image = mono_assembly_get_image (ass);
3602 MonoMethod *method, *invoke;
3605 if (g_hash_table_lookup (assemblies, ass))
3608 g_hash_table_insert (assemblies, ass, ass);
3610 if (mini_verbose > 0)
3611 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
3613 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
3614 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
3615 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3617 if (method->is_generic || method->klass->generic_container)
3621 if (mini_verbose > 1) {
3622 char * desc = mono_method_full_name (method, TRUE);
3623 g_print ("Compiling %d %s\n", count, desc);
3626 mono_compile_method (method);
3627 if (strcmp (method->name, "Finalize") == 0) {
3628 invoke = mono_marshal_get_runtime_invoke (method, FALSE);
3629 mono_compile_method (invoke);
3631 #ifndef DISABLE_REMOTING
3632 if (mono_class_is_marshalbyref (method->klass) && mono_method_signature (method)->hasthis) {
3633 invoke = mono_marshal_get_remoting_invoke_with_check (method);
3634 mono_compile_method (invoke);
3639 /* Load and precompile referenced assemblies as well */
3640 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
3641 mono_assembly_load_reference (image, i);
3642 if (image->references [i])
3643 mono_precompile_assembly (image->references [i], assemblies);
3647 void mono_precompile_assemblies ()
3649 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
3651 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
3653 g_hash_table_destroy (assemblies);
3656 #ifdef USE_JUMP_TABLES
3657 #define DEFAULT_JUMPTABLE_CHUNK_ELEMENTS 128
3659 typedef struct MonoJumpTableChunk {
3662 struct MonoJumpTableChunk *previous;
3663 /* gpointer entries[total]; */
3664 } MonoJumpTableChunk;
3666 static MonoJumpTableChunk* g_jumptable;
3667 #define mono_jumptable_lock() mono_mutex_lock (&jumptable_mutex)
3668 #define mono_jumptable_unlock() mono_mutex_unlock (&jumptable_mutex)
3669 static mono_mutex_t jumptable_mutex;
3671 static MonoJumpTableChunk*
3672 mono_create_jumptable_chunk (guint32 max_entries)
3674 guint32 size = sizeof (MonoJumpTableChunk) + max_entries * sizeof(gpointer);
3675 MonoJumpTableChunk *chunk = (MonoJumpTableChunk*) g_new0 (guchar, size);
3676 chunk->total = max_entries;
3681 mono_jumptable_init (void)
3683 if (g_jumptable == NULL) {
3684 mono_mutex_init_recursive (&jumptable_mutex);
3685 g_jumptable = mono_create_jumptable_chunk (DEFAULT_JUMPTABLE_CHUNK_ELEMENTS);
3690 mono_jumptable_add_entry (void)
3692 return mono_jumptable_add_entries (1);
3696 mono_jumptable_add_entries (guint32 entries)
3701 mono_jumptable_init ();
3702 mono_jumptable_lock ();
3703 index = g_jumptable->active;
3704 if (index + entries >= g_jumptable->total) {
3706 * Grow jumptable, by adding one more chunk.
3707 * We cannot realloc jumptable, as there could be pointers
3708 * to existing jump table entries in the code, so instead
3709 * we just add one more chunk.
3711 guint32 max_entries = entries;
3712 MonoJumpTableChunk *new_chunk;
3714 if (max_entries < DEFAULT_JUMPTABLE_CHUNK_ELEMENTS)
3715 max_entries = DEFAULT_JUMPTABLE_CHUNK_ELEMENTS;
3716 new_chunk = mono_create_jumptable_chunk (max_entries);
3717 /* Link old jumptable, so that we could free it up later. */
3718 new_chunk->previous = g_jumptable;
3719 g_jumptable = new_chunk;
3722 g_jumptable->active = index + entries;
3723 result = (gpointer*)((guchar*)g_jumptable + sizeof(MonoJumpTableChunk)) + index;
3724 mono_jumptable_unlock();
3730 mono_jumptable_cleanup (void)
3733 MonoJumpTableChunk *current = g_jumptable, *prev;
3734 while (current != NULL) {
3735 prev = current->previous;
3740 mono_mutex_destroy (&jumptable_mutex);
3745 mono_jumptable_get_entry (guint8 *code_ptr)
3747 return mono_arch_jumptable_entry_from_code (code_ptr);