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)
13 #define MONO_LLVM_IN_MINI 1
22 #ifdef HAVE_SYS_TIME_H
29 #include <mono/utils/memcheck.h>
31 #include <mono/metadata/assembly.h>
32 #include <mono/metadata/loader.h>
33 #include <mono/metadata/tabledefs.h>
34 #include <mono/metadata/class.h>
35 #include <mono/metadata/object.h>
36 #include <mono/metadata/tokentype.h>
37 #include <mono/metadata/tabledefs.h>
38 #include <mono/metadata/threads.h>
39 #include <mono/metadata/appdomain.h>
40 #include <mono/metadata/debug-helpers.h>
41 #include <mono/io-layer/io-layer.h>
42 #include "mono/metadata/profiler.h"
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/metadata/mono-config.h>
45 #include <mono/metadata/environment.h>
46 #include <mono/metadata/mono-debug.h>
47 #include <mono/metadata/gc-internal.h>
48 #include <mono/metadata/threads-types.h>
49 #include <mono/metadata/verify.h>
50 #include <mono/metadata/verify-internals.h>
51 #include <mono/metadata/mempool-internals.h>
52 #include <mono/metadata/attach.h>
53 #include <mono/metadata/runtime.h>
54 #include <mono/metadata/mono-debug-debugger.h>
55 #include <mono/utils/mono-math.h>
56 #include <mono/utils/mono-compiler.h>
57 #include <mono/utils/mono-counters.h>
58 #include <mono/utils/mono-error-internals.h>
59 #include <mono/utils/mono-logger-internal.h>
60 #include <mono/utils/mono-mmap.h>
61 #include <mono/utils/mono-path.h>
62 #include <mono/utils/mono-tls.h>
63 #include <mono/utils/mono-hwcap.h>
64 #include <mono/utils/dtrace.h>
65 #include <mono/utils/mono-signal-handler.h>
66 #include <mono/utils/mono-threads.h>
69 #include "seq-points.h"
70 #include "mini-llvm.h"
78 #include "jit-icalls.h"
81 #include "debugger-agent.h"
82 #include "seq-points.h"
84 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt, MonoException **ex);
87 static guint32 default_opt = 0;
88 static gboolean default_opt_set = FALSE;
90 MonoNativeTlsKey mono_jit_tls_id;
92 #ifdef MONO_HAVE_FAST_TLS
93 MONO_FAST_TLS_DECLARE(mono_jit_tls);
96 gboolean mono_compile_aot = FALSE;
97 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
98 gboolean mono_aot_only = FALSE;
99 /* Whenever to use IMT */
100 gboolean mono_use_imt = TRUE;
101 const char *mono_build_date;
102 gboolean mono_do_signal_chaining;
103 gboolean mono_do_crash_chaining;
104 int mini_verbose = 0;
107 * This flag controls whenever the runtime uses LLVM for JIT compilation, and whenever
108 * it can load AOT code compiled by LLVM.
110 gboolean mono_use_llvm = FALSE;
112 #define mono_jit_lock() mono_mutex_lock (&jit_mutex)
113 #define mono_jit_unlock() mono_mutex_unlock (&jit_mutex)
114 static mono_mutex_t jit_mutex;
116 static MonoCodeManager *global_codeman;
118 MonoDebugOptions debug_options;
120 #ifdef VALGRIND_JIT_REGISTER_MAP
121 int valgrind_register;
124 static GSList *tramp_infos;
126 static void register_icalls (void);
129 mono_running_on_valgrind (void)
131 if (RUNNING_ON_VALGRIND){
132 #ifdef VALGRIND_JIT_REGISTER_MAP
133 valgrind_register = TRUE;
146 find_tramp (gpointer key, gpointer value, gpointer user_data)
148 FindTrampUserData *ud = (FindTrampUserData*)user_data;
151 ud->method = (MonoMethod*)key;
155 G_GNUC_UNUSED static char*
156 get_method_from_ip (void *ip)
161 MonoDomain *domain = mono_domain_get ();
162 MonoDebugSourceLocation *location;
163 FindTrampUserData user_data;
166 domain = mono_get_root_domain ();
168 ji = mono_jit_info_table_find (domain, ip);
171 user_data.method = NULL;
172 mono_domain_lock (domain);
173 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
174 mono_domain_unlock (domain);
175 if (user_data.method) {
176 char *mname = mono_method_full_name (user_data.method, TRUE);
177 res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
184 method = mono_method_full_name (jinfo_get_method (ji), TRUE);
185 /* FIXME: unused ? */
186 location = mono_debug_lookup_source_location (jinfo_get_method (ji), (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
188 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);
190 mono_debug_free_source_location (location);
198 * @ip: an instruction pointer address
200 * This method is used from a debugger to get the name of the
201 * method at address @ip. This routine is typically invoked from
202 * a debugger like this:
204 * (gdb) print mono_pmip ($pc)
206 * Returns: the name of the method at address @ip.
211 return get_method_from_ip (ip);
215 * mono_print_method_from_ip
216 * @ip: an instruction pointer address
218 * This method is used from a debugger to get the name of the
219 * method at address @ip.
221 * This prints the name of the method at address @ip in the standard
222 * output. Unlike mono_pmip which returns a string, this routine
223 * prints the value on the standard output.
226 /* Prevent the linker from optimizing this away in embedding setups to help debugging */
227 __attribute__((used))
230 mono_print_method_from_ip (void *ip)
234 MonoDebugSourceLocation *source;
235 MonoDomain *domain = mono_domain_get ();
236 MonoDomain *target_domain = mono_domain_get ();
237 FindTrampUserData user_data;
238 MonoGenericSharingContext*gsctx;
239 const char *shared_type;
242 ji = mini_jit_info_table_find (domain, ip, &target_domain);
245 user_data.method = NULL;
246 mono_domain_lock (domain);
247 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
248 mono_domain_unlock (domain);
249 if (user_data.method) {
250 char *mname = mono_method_full_name (user_data.method, TRUE);
251 printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
255 for (l = tramp_infos; l; l = l->next) {
256 MonoTrampInfo *tinfo = l->data;
258 if ((guint8*)ip >= tinfo->code && (guint8*)ip <= tinfo->code + tinfo->code_size) {
259 printf ("IP %p is at offset 0x%x of trampoline '%s'.\n", ip, (int)((guint8*)ip - tinfo->code), tinfo->name);
264 g_print ("No method at %p\n", ip);
268 method = mono_method_full_name (jinfo_get_method (ji), TRUE);
269 source = mono_debug_lookup_source_location (jinfo_get_method (ji), (guint32)((guint8*)ip - (guint8*)ji->code_start), target_domain);
271 gsctx = mono_jit_info_get_generic_sharing_context (ji);
274 if (gsctx->var_is_vt || gsctx->mvar_is_vt)
275 shared_type = "gsharedvt ";
277 shared_type = "gshared ";
280 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);
283 g_print ("%s:%d\n", source->source_file, source->row);
286 mono_debug_free_source_location (source);
291 * mono_method_same_domain:
293 * Determine whenever two compiled methods are in the same domain, thus
294 * the address of the callee can be embedded in the caller.
296 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
300 if (!caller || !callee)
304 * If the call was made from domain-neutral to domain-specific
305 * code, we can't patch the call site.
307 if (caller->domain_neutral && !callee->domain_neutral)
310 cmethod = jinfo_get_method (caller);
311 if ((cmethod->klass == mono_defaults.appdomain_class) &&
312 (strstr (cmethod->name, "InvokeInDomain"))) {
313 /* The InvokeInDomain methods change the current appdomain */
321 * mono_global_codeman_reserve:
323 * Allocate code memory from the global code manager.
325 void *mono_global_codeman_reserve (int size)
330 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
332 if (!global_codeman) {
333 /* This can happen during startup */
334 global_codeman = mono_code_manager_new ();
335 return mono_code_manager_reserve (global_codeman, size);
339 ptr = mono_code_manager_reserve (global_codeman, size);
345 #if defined(__native_client_codegen__) && defined(__native_client__)
349 #ifdef __native_client_gc__
350 __nacl_suspend_thread_if_needed();
354 /* Given the temporary buffer (allocated by mono_global_codeman_reserve) into
355 * which we are generating code, return a pointer to the destination in the
356 * dynamic code segment into which the code will be copied when
357 * mono_global_codeman_commit is called.
358 * LOCKING: Acquires the jit lock.
361 nacl_global_codeman_get_dest (void *data)
365 dest = nacl_code_manager_get_code_dest (global_codeman, data);
371 mono_global_codeman_commit (void *data, int size, int newsize)
374 mono_code_manager_commit (global_codeman, data, size, newsize);
379 * Convenience function which calls mono_global_codeman_commit to validate and
380 * copy the code. The caller sets *buf_base and *buf_size to the start and size
381 * of the buffer (allocated by mono_global_codeman_reserve), and *code_end to
382 * the byte after the last instruction byte. On return, *buf_base will point to
383 * the start of the copied in the code segment, and *code_end will point after
384 * the end of the copied code.
387 nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
389 guint8 *tmp = nacl_global_codeman_get_dest (*buf_base);
390 mono_global_codeman_commit (*buf_base, buf_size, *code_end - *buf_base);
391 *code_end = tmp + (*code_end - *buf_base);
395 /* no-op versions of Native Client functions */
397 nacl_global_codeman_get_dest (void *data)
403 mono_global_codeman_commit (void *data, int size, int newsize)
408 nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
412 #endif /* __native_client__ */
415 * mono_create_unwind_op:
417 * Create an unwind op with the given parameters.
420 mono_create_unwind_op (int when, int tag, int reg, int val)
422 MonoUnwindOp *op = g_new0 (MonoUnwindOp, 1);
433 mono_jump_info_token_new2 (MonoMemPool *mp, MonoImage *image, guint32 token, MonoGenericContext *context)
435 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
438 res->has_context = context != NULL;
440 memcpy (&res->context, context, sizeof (MonoGenericContext));
446 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
448 return mono_jump_info_token_new2 (mp, image, token, NULL);
452 * mono_tramp_info_create:
454 * Create a MonoTrampInfo structure from the arguments. This function assumes ownership
455 * of JI, and UNWIND_OPS.
458 mono_tramp_info_create (const char *name, guint8 *code, guint32 code_size, MonoJumpInfo *ji, GSList *unwind_ops)
460 MonoTrampInfo *info = g_new0 (MonoTrampInfo, 1);
462 info->name = g_strdup ((char*)name);
464 info->code_size = code_size;
466 info->unwind_ops = unwind_ops;
472 mono_tramp_info_free (MonoTrampInfo *info)
479 for (l = info->unwind_ops; l; l = l->next)
481 g_slist_free (info->unwind_ops);
486 * mono_tramp_info_register:
488 * Remember INFO for use by xdebug, mono_print_method_from_ip (), jit maps, etc.
493 mono_tramp_info_register (MonoTrampInfo *info)
500 copy = g_new0 (MonoTrampInfo, 1);
501 copy->code = info->code;
502 copy->code_size = info->code_size;
503 copy->name = g_strdup (info->name);
506 tramp_infos = g_slist_prepend (tramp_infos, copy);
509 mono_save_trampoline_xdebug_info (info);
511 if (mono_jit_map_is_enabled ())
512 mono_emit_jit_tramp (info->code, info->code_size, info->name);
514 mono_tramp_info_free (info);
518 mono_tramp_info_cleanup (void)
522 for (l = tramp_infos; l; l = l->next) {
523 MonoTrampInfo *info = l->data;
525 mono_tramp_info_free (info);
527 g_slist_free (tramp_infos);
530 G_GNUC_UNUSED static void
536 * Runtime debugging tool, use if (debug_count ()) <x> else <y> to do <x> the first COUNT times, then do <y> afterwards.
537 * Set a breakpoint in break_count () to break the last time <x> is done.
539 G_GNUC_UNUSED gboolean
540 mono_debug_count (void)
542 static int count = 0;
543 static gboolean inited;
544 static const char *value;
549 value = g_getenv ("COUNT");
556 if (count == atoi (value))
559 if (count > atoi (value))
566 mono_icall_get_wrapper_full (MonoJitICallInfo* callinfo, gboolean do_compile)
570 gconstpointer trampoline;
571 MonoDomain *domain = mono_get_root_domain ();
572 gboolean check_exc = check_for_pending_exc;
574 if (callinfo->wrapper) {
575 return callinfo->wrapper;
578 if (callinfo->trampoline)
579 return callinfo->trampoline;
582 * We use the lock on the root domain instead of the JIT lock to protect
583 * callinfo->trampoline, since we do a lot of stuff inside the critical section.
585 mono_loader_lock (); /*FIXME mono_compile_method requires the loader lock, by large.*/
586 mono_domain_lock (domain);
588 if (callinfo->trampoline) {
589 mono_domain_unlock (domain);
590 mono_loader_unlock ();
591 return callinfo->trampoline;
594 if (!strcmp (callinfo->name, "mono_thread_interruption_checkpoint"))
595 /* This icall is used to check for exceptions, so don't check in the wrapper */
598 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
599 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_exc);
603 trampoline = mono_compile_method (wrapper);
605 trampoline = mono_create_ftnptr (domain, mono_create_jit_trampoline_in_domain (domain, wrapper));
606 mono_register_jit_icall_wrapper (callinfo, trampoline);
608 callinfo->trampoline = trampoline;
610 mono_domain_unlock (domain);
611 mono_loader_unlock ();
613 return callinfo->trampoline;
617 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
619 return mono_icall_get_wrapper_full (callinfo, FALSE);
623 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
625 if (!domain_jit_info (domain)->dynamic_code_hash)
626 domain_jit_info (domain)->dynamic_code_hash = g_hash_table_new (NULL, NULL);
627 g_hash_table_insert (domain_jit_info (domain)->dynamic_code_hash, method, ji);
630 static MonoJitDynamicMethodInfo*
631 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
633 MonoJitDynamicMethodInfo *res;
635 if (domain_jit_info (domain)->dynamic_code_hash)
636 res = g_hash_table_lookup (domain_jit_info (domain)->dynamic_code_hash, method);
643 register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, const char *symbol, gboolean no_throw)
645 return mini_register_opcode_emulation (opcode, name, sigstr, func, symbol, no_throw);
649 * For JIT icalls implemented in C.
650 * NAME should be the same as the name of the C function whose address is FUNC.
651 * If SAVE is TRUE, no wrapper is generated. This is for perf critical icalls which
652 * can't throw exceptions.
655 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
657 MonoMethodSignature *sig;
660 sig = mono_create_icall_signature (sigstr);
664 mono_register_jit_icall_full (func, name, sig, save, FALSE, save ? name : NULL);
667 /* Register a jit icall which doesn't throw exceptions through mono_raise_exception () */
669 register_icall_noraise (gpointer func, const char *name, const char *sigstr)
671 MonoMethodSignature *sig;
674 sig = mono_create_icall_signature (sigstr);
678 mono_register_jit_icall_full (func, name, sig, TRUE, TRUE, name);
682 register_dyn_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
684 MonoMethodSignature *sig;
687 sig = mono_create_icall_signature (sigstr);
691 mono_register_jit_icall (func, name, sig, save);
694 #ifdef MONO_HAVE_FAST_TLS
695 MONO_FAST_TLS_DECLARE(mono_lmf_addr);
696 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
698 * When this is defined, the current lmf is stored in this tls variable instead of in
701 MONO_FAST_TLS_DECLARE(mono_lmf);
706 mono_get_jit_tls_offset (void)
712 offset = mono_jit_tls_id;
714 /* FIXME: Happens during startup */
717 MONO_THREAD_VAR_OFFSET (mono_jit_tls, offset);
723 mono_get_lmf_tls_offset (void)
725 #if defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
727 MONO_THREAD_VAR_OFFSET(mono_lmf,offset);
735 mono_get_lmf_addr_tls_offset (void)
738 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
745 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
746 return MONO_FAST_TLS_GET (mono_lmf);
748 MonoJitTlsData *jit_tls;
750 if ((jit_tls = mono_native_tls_get_value (mono_jit_tls_id)))
753 * We do not assert here because this function can be called from
754 * mini-gc.c on a thread that has not executed any managed code, yet
755 * (the thread object allocation can trigger a collection).
762 mono_get_lmf_addr (void)
764 #ifdef MONO_HAVE_FAST_TLS
765 return MONO_FAST_TLS_GET (mono_lmf_addr);
767 MonoJitTlsData *jit_tls;
769 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
770 if (G_LIKELY (jit_tls))
771 return &jit_tls->lmf;
774 * When resolving the call to mono_jit_thread_attach full-aot will look
775 * in the plt, which causes a call into the generic trampoline, which in turn
776 * tries to resolve the lmf_addr creating a cyclic dependency. We cannot
777 * call mono_jit_thread_attach from the native-to-managed wrapper, without
778 * mono_get_lmf_addr, and mono_get_lmf_addr requires the thread to be attached.
781 mono_jit_thread_attach (NULL);
783 if ((jit_tls = mono_native_tls_get_value (mono_jit_tls_id)))
784 return &jit_tls->lmf;
786 g_assert_not_reached ();
792 mono_set_lmf (MonoLMF *lmf)
794 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
795 MONO_FAST_TLS_SET (mono_lmf, lmf);
798 (*mono_get_lmf_addr ()) = lmf;
802 mono_get_jit_tls (void)
804 return mono_native_tls_get_value (mono_jit_tls_id);
808 mono_set_jit_tls (MonoJitTlsData *jit_tls)
810 MonoThreadInfo *info;
812 mono_native_tls_set_value (mono_jit_tls_id, jit_tls);
814 #ifdef MONO_HAVE_FAST_TLS
815 MONO_FAST_TLS_SET (mono_jit_tls, jit_tls);
818 /* Save it into MonoThreadInfo so it can be accessed by mono_thread_state_init_from_handle () */
819 info = mono_thread_info_current ();
821 mono_thread_info_tls_set (info, TLS_KEY_JIT_TLS, jit_tls);
825 mono_set_lmf_addr (gpointer lmf_addr)
827 MonoThreadInfo *info;
829 #ifdef MONO_HAVE_FAST_TLS
830 MONO_FAST_TLS_SET (mono_lmf_addr, lmf_addr);
833 /* Save it into MonoThreadInfo so it can be accessed by mono_thread_state_init_from_handle () */
834 info = mono_thread_info_current ();
836 mono_thread_info_tls_set (info, TLS_KEY_LMF_ADDR, lmf_addr);
840 * mono_jit_thread_attach:
842 * Called by native->managed wrappers. Returns the original domain which needs to be
846 mono_jit_thread_attach (MonoDomain *domain)
852 * Happens when called from AOTed code which is only used in the root
855 domain = mono_get_root_domain ();
857 #ifdef MONO_HAVE_FAST_TLS
858 if (!MONO_FAST_TLS_GET (mono_lmf_addr)) {
859 mono_thread_attach (domain);
861 mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background);
864 if (!mono_native_tls_get_value (mono_jit_tls_id)) {
865 mono_thread_attach (domain);
866 mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background);
869 orig = mono_domain_get ();
871 mono_domain_set (domain, TRUE);
873 return orig != domain ? orig : NULL;
876 /* Called by native->managed wrappers */
878 mono_jit_set_domain (MonoDomain *domain)
881 mono_domain_set (domain, TRUE);
886 * @obj: exception object
888 * abort the thread, print exception information and stack trace
891 mono_thread_abort (MonoObject *obj)
893 /* MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id); */
895 /* handle_remove should be eventually called for this thread, too
898 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY) ||
899 (obj->vtable->klass == mono_defaults.threadabortexception_class)) {
902 mono_invoke_unhandled_exception_hook (obj);
907 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
909 MonoJitTlsData *jit_tls;
912 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
916 jit_tls = g_new0 (MonoJitTlsData, 1);
918 jit_tls->abort_func = abort_func;
919 jit_tls->end_of_stack = stack_start;
921 mono_set_jit_tls (jit_tls);
923 lmf = g_new0 (MonoLMF, 1);
924 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf);
926 jit_tls->first_lmf = lmf;
928 #if defined(MONO_HAVE_FAST_TLS) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
929 /* jit_tls->lmf is unused */
930 MONO_FAST_TLS_SET (mono_lmf, lmf);
931 mono_set_lmf_addr (MONO_FAST_TLS_ADDR (mono_lmf));
933 mono_set_lmf_addr (&jit_tls->lmf);
938 #ifdef MONO_ARCH_HAVE_TLS_INIT
939 mono_arch_tls_init ();
942 mono_setup_altstack (jit_tls);
948 free_jit_tls_data (MonoJitTlsData *jit_tls)
950 mono_arch_free_jit_tls_data (jit_tls);
951 mono_free_altstack (jit_tls);
953 g_free (jit_tls->first_lmf);
958 mono_thread_start_cb (intptr_t tid, gpointer stack_start, gpointer func)
960 MonoInternalThread *thread;
961 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
962 thread = mono_thread_internal_current ();
964 thread->jit_data = jit_tls;
966 mono_arch_cpu_init ();
969 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
972 mono_thread_abort_dummy (MonoObject *obj)
974 if (mono_thread_attach_aborted_cb)
975 mono_thread_attach_aborted_cb (obj);
977 mono_thread_abort (obj);
981 mono_thread_attach_cb (intptr_t tid, gpointer stack_start)
983 MonoInternalThread *thread;
984 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
985 thread = mono_thread_internal_current ();
987 thread->jit_data = jit_tls;
988 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
989 mono_runtime_setup_stat_profiler ();
991 mono_arch_cpu_init ();
995 mini_thread_cleanup (MonoInternalThread *thread)
997 MonoJitTlsData *jit_tls = thread->jit_data;
1000 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
1001 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
1002 * not a trivial thing.
1004 * The current offender is mono_thread_manage which cleanup threads from the outside.
1006 if (thread == mono_thread_internal_current ())
1007 mono_set_jit_tls (NULL);
1009 /* If we attach a thread but never call into managed land, we might never get an lmf.*/
1010 if (mono_get_lmf ()) {
1011 mono_set_lmf (NULL);
1012 mono_set_lmf_addr (NULL);
1015 free_jit_tls_data (jit_tls);
1017 thread->jit_data = NULL;
1022 mini_get_tls_offset (MonoTlsKey key)
1027 case TLS_KEY_THREAD:
1028 offset = mono_thread_get_tls_offset ();
1030 case TLS_KEY_JIT_TLS:
1031 offset = mono_get_jit_tls_offset ();
1033 case TLS_KEY_DOMAIN:
1034 offset = mono_domain_get_tls_offset ();
1037 offset = mono_get_lmf_tls_offset ();
1039 case TLS_KEY_LMF_ADDR:
1040 offset = mono_get_lmf_addr_tls_offset ();
1043 offset = mono_tls_key_get_offset (key);
1044 g_assert (offset != -1);
1052 mini_tls_key_supported (MonoTlsKey key)
1054 if (!MONO_ARCH_HAVE_TLS_GET)
1057 return mini_get_tls_offset (key) != -1;
1061 mono_patch_info_list_prepend (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target)
1063 MonoJumpInfo *ji = g_new0 (MonoJumpInfo, 1);
1067 ji->data.target = target;
1074 * mono_patch_info_dup_mp:
1076 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
1079 mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
1081 MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
1082 memcpy (res, patch_info, sizeof (MonoJumpInfo));
1084 switch (patch_info->type) {
1085 case MONO_PATCH_INFO_RVA:
1086 case MONO_PATCH_INFO_LDSTR:
1087 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1088 case MONO_PATCH_INFO_LDTOKEN:
1089 case MONO_PATCH_INFO_DECLSEC:
1090 res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
1091 memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
1093 case MONO_PATCH_INFO_SWITCH:
1094 res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
1095 memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
1096 res->data.table->table = mono_mempool_alloc (mp, sizeof (MonoBasicBlock*) * patch_info->data.table->table_size);
1097 memcpy (res->data.table->table, patch_info->data.table->table, sizeof (MonoBasicBlock*) * patch_info->data.table->table_size);
1099 case MONO_PATCH_INFO_RGCTX_FETCH:
1100 res->data.rgctx_entry = mono_mempool_alloc (mp, sizeof (MonoJumpInfoRgctxEntry));
1101 memcpy (res->data.rgctx_entry, patch_info->data.rgctx_entry, sizeof (MonoJumpInfoRgctxEntry));
1102 res->data.rgctx_entry->data = mono_patch_info_dup_mp (mp, res->data.rgctx_entry->data);
1104 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1105 res->data.del_tramp = mono_mempool_alloc0 (mp, sizeof (MonoDelegateClassMethodPair));
1106 memcpy (res->data.del_tramp, patch_info->data.del_tramp, sizeof (MonoDelegateClassMethodPair));
1108 case MONO_PATCH_INFO_GSHAREDVT_CALL:
1109 res->data.gsharedvt = mono_mempool_alloc (mp, sizeof (MonoJumpInfoGSharedVtCall));
1110 memcpy (res->data.gsharedvt, patch_info->data.gsharedvt, sizeof (MonoJumpInfoGSharedVtCall));
1112 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
1113 MonoGSharedVtMethodInfo *info;
1114 MonoGSharedVtMethodInfo *oinfo;
1117 oinfo = patch_info->data.gsharedvt_method;
1118 info = mono_mempool_alloc (mp, sizeof (MonoGSharedVtMethodInfo));
1119 res->data.gsharedvt_method = info;
1120 memcpy (info, oinfo, sizeof (MonoGSharedVtMethodInfo));
1121 info->entries = mono_mempool_alloc (mp, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
1122 for (i = 0; i < oinfo->num_entries; ++i) {
1123 MonoRuntimeGenericContextInfoTemplate *otemplate = &oinfo->entries [i];
1124 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
1126 memcpy (template, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
1128 //info->locals_types = mono_mempool_alloc0 (mp, info->nlocals * sizeof (MonoType*));
1129 //memcpy (info->locals_types, oinfo->locals_types, info->nlocals * sizeof (MonoType*));
1140 mono_patch_info_hash (gconstpointer data)
1142 const MonoJumpInfo *ji = (MonoJumpInfo*)data;
1145 case MONO_PATCH_INFO_RVA:
1146 case MONO_PATCH_INFO_LDSTR:
1147 case MONO_PATCH_INFO_LDTOKEN:
1148 case MONO_PATCH_INFO_DECLSEC:
1149 return (ji->type << 8) | ji->data.token->token;
1150 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1151 return (ji->type << 8) | ji->data.token->token | (ji->data.token->has_context ? (gsize)ji->data.token->context.class_inst : 0);
1152 case MONO_PATCH_INFO_INTERNAL_METHOD:
1153 return (ji->type << 8) | g_str_hash (ji->data.name);
1154 case MONO_PATCH_INFO_VTABLE:
1155 case MONO_PATCH_INFO_CLASS:
1156 case MONO_PATCH_INFO_IID:
1157 case MONO_PATCH_INFO_ADJUSTED_IID:
1158 case MONO_PATCH_INFO_CLASS_INIT:
1159 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1160 case MONO_PATCH_INFO_METHODCONST:
1161 case MONO_PATCH_INFO_METHOD:
1162 case MONO_PATCH_INFO_METHOD_JUMP:
1163 case MONO_PATCH_INFO_IMAGE:
1164 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1165 case MONO_PATCH_INFO_ICALL_ADDR:
1166 case MONO_PATCH_INFO_FIELD:
1167 case MONO_PATCH_INFO_SFLDA:
1168 case MONO_PATCH_INFO_SEQ_POINT_INFO:
1169 case MONO_PATCH_INFO_METHOD_RGCTX:
1170 case MONO_PATCH_INFO_SIGNATURE:
1171 case MONO_PATCH_INFO_TLS_OFFSET:
1172 case MONO_PATCH_INFO_METHOD_CODE_SLOT:
1173 return (ji->type << 8) | (gssize)ji->data.target;
1174 case MONO_PATCH_INFO_GSHAREDVT_CALL:
1175 return (ji->type << 8) | (gssize)ji->data.gsharedvt->method;
1176 case MONO_PATCH_INFO_RGCTX_FETCH: {
1177 MonoJumpInfoRgctxEntry *e = ji->data.rgctx_entry;
1179 return (ji->type << 8) | (gssize)e->method | (e->in_mrgctx) | e->info_type | mono_patch_info_hash (e->data);
1181 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1182 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
1183 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
1184 case MONO_PATCH_INFO_JIT_TLS_ID:
1185 case MONO_PATCH_INFO_MONITOR_ENTER:
1186 case MONO_PATCH_INFO_MONITOR_ENTER_V4:
1187 case MONO_PATCH_INFO_MONITOR_EXIT:
1188 case MONO_PATCH_INFO_GOT_OFFSET:
1189 return (ji->type << 8);
1190 case MONO_PATCH_INFO_CASTCLASS_CACHE:
1191 return (ji->type << 8) | (ji->data.index);
1192 case MONO_PATCH_INFO_SWITCH:
1193 return (ji->type << 8) | ji->data.table->table_size;
1194 case MONO_PATCH_INFO_GSHAREDVT_METHOD:
1195 return (ji->type << 8) | (gssize)ji->data.gsharedvt_method->method;
1196 case MONO_PATCH_INFO_OBJC_SELECTOR_REF:
1197 /* Hash on the selector name */
1198 return g_str_hash (ji->data.target);
1199 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1200 return (ji->type << 8) | (gsize)ji->data.del_tramp->klass | (gsize)ji->data.del_tramp->method | (gsize)ji->data.del_tramp->virtual;
1201 case MONO_PATCH_INFO_LDSTR_LIT:
1202 return g_str_hash (ji->data.target);
1204 printf ("info type: %d\n", ji->type);
1205 mono_print_ji (ji); printf ("\n");
1206 g_assert_not_reached ();
1212 * mono_patch_info_equal:
1214 * This might fail to recognize equivalent patches, i.e. floats, so its only
1215 * usable in those cases where this is not a problem, i.e. sharing GOT slots
1219 mono_patch_info_equal (gconstpointer ka, gconstpointer kb)
1221 const MonoJumpInfo *ji1 = (MonoJumpInfo*)ka;
1222 const MonoJumpInfo *ji2 = (MonoJumpInfo*)kb;
1224 if (ji1->type != ji2->type)
1227 switch (ji1->type) {
1228 case MONO_PATCH_INFO_RVA:
1229 case MONO_PATCH_INFO_LDSTR:
1230 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1231 case MONO_PATCH_INFO_LDTOKEN:
1232 case MONO_PATCH_INFO_DECLSEC:
1233 if ((ji1->data.token->image != ji2->data.token->image) ||
1234 (ji1->data.token->token != ji2->data.token->token) ||
1235 (ji1->data.token->has_context != ji2->data.token->has_context) ||
1236 (ji1->data.token->context.class_inst != ji2->data.token->context.class_inst) ||
1237 (ji1->data.token->context.method_inst != ji2->data.token->context.method_inst))
1240 case MONO_PATCH_INFO_INTERNAL_METHOD:
1241 return g_str_equal (ji1->data.name, ji2->data.name);
1242 case MONO_PATCH_INFO_RGCTX_FETCH: {
1243 MonoJumpInfoRgctxEntry *e1 = ji1->data.rgctx_entry;
1244 MonoJumpInfoRgctxEntry *e2 = ji2->data.rgctx_entry;
1246 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);
1248 case MONO_PATCH_INFO_GSHAREDVT_CALL: {
1249 MonoJumpInfoGSharedVtCall *c1 = ji1->data.gsharedvt;
1250 MonoJumpInfoGSharedVtCall *c2 = ji2->data.gsharedvt;
1252 return c1->sig == c2->sig && c1->method == c2->method;
1254 case MONO_PATCH_INFO_GSHAREDVT_METHOD:
1255 return ji1->data.gsharedvt_method->method == ji2->data.gsharedvt_method->method;
1256 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1257 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;
1258 case MONO_PATCH_INFO_CASTCLASS_CACHE:
1259 return ji1->data.index == ji2->data.index;
1261 if (ji1->data.target != ji2->data.target)
1270 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
1272 unsigned char *ip = patch_info->ip.i + code;
1273 gconstpointer target = NULL;
1275 switch (patch_info->type) {
1276 case MONO_PATCH_INFO_BB:
1278 * FIXME: This could be hit for methods without a prolog. Should use -1
1279 * but too much code depends on a 0 initial value.
1281 //g_assert (patch_info->data.bb->native_offset);
1282 target = patch_info->data.bb->native_offset + code;
1284 case MONO_PATCH_INFO_ABS:
1285 target = patch_info->data.target;
1287 case MONO_PATCH_INFO_LABEL:
1288 target = patch_info->data.inst->inst_c0 + code;
1290 case MONO_PATCH_INFO_IP:
1291 #if defined(__native_client__) && defined(__native_client_codegen__)
1292 /* Need to transform to the destination address, it's */
1293 /* emitted as an immediate in the code. */
1294 target = nacl_inverse_modify_patch_target(ip);
1299 case MONO_PATCH_INFO_METHOD_REL:
1300 target = code + patch_info->data.offset;
1302 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1303 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
1305 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
1306 g_assert_not_reached ();
1308 target = mono_icall_get_wrapper (mi);
1311 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1312 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
1314 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info->data.name);
1315 g_assert_not_reached ();
1320 case MONO_PATCH_INFO_METHOD_JUMP:
1321 target = mono_create_jump_trampoline (domain, patch_info->data.method, FALSE);
1322 #if defined(__native_client__) && defined(__native_client_codegen__)
1323 # if defined(TARGET_AMD64)
1324 /* This target is an absolute address, not relative to the */
1325 /* current code being emitted on AMD64. */
1326 target = nacl_inverse_modify_patch_target(target);
1330 case MONO_PATCH_INFO_METHOD:
1331 #if defined(__native_client_codegen__) && defined(USE_JUMP_TABLES)
1333 * If we use jumptables, for recursive calls we cannot
1334 * avoid trampoline, as we not yet know where we will
1337 target = mono_create_jit_trampoline_in_domain (domain, patch_info->data.method);
1339 if (patch_info->data.method == method) {
1342 /* get the trampoline to the method from the domain */
1343 target = mono_create_jit_trampoline_in_domain (domain, patch_info->data.method);
1347 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
1350 mono_domain_lock (domain);
1351 if (!domain_jit_info (domain)->method_code_hash)
1352 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
1353 code_slot = g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, patch_info->data.method);
1355 code_slot = mono_domain_alloc0 (domain, sizeof (gpointer));
1356 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, patch_info->data.method, code_slot);
1358 mono_domain_unlock (domain);
1362 case MONO_PATCH_INFO_SWITCH: {
1363 gpointer *jump_table;
1365 #if defined(__native_client__) && defined(__native_client_codegen__)
1366 /* This memory will leak, but we don't care if we're */
1367 /* not deleting JIT'd methods anyway */
1368 jump_table = g_malloc0 (sizeof(gpointer) * patch_info->data.table->table_size);
1370 if (method && method->dynamic) {
1371 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
1373 if (mono_aot_only) {
1374 jump_table = mono_domain_alloc (domain, sizeof (gpointer) * patch_info->data.table->table_size);
1376 jump_table = mono_domain_code_reserve (domain, sizeof (gpointer) * patch_info->data.table->table_size);
1381 for (i = 0; i < patch_info->data.table->table_size; i++) {
1382 #if defined(__native_client__) && defined(__native_client_codegen__)
1383 /* 'code' is relative to the current code blob, we */
1384 /* need to do this transform on it to make the */
1385 /* pointers in this table absolute */
1386 jump_table [i] = nacl_inverse_modify_patch_target (code) + GPOINTER_TO_INT (patch_info->data.table->table [i]);
1388 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
1392 #if defined(__native_client__) && defined(__native_client_codegen__)
1393 /* jump_table is in the data section, we need to transform */
1394 /* it here so when it gets modified in amd64_patch it will */
1395 /* then point back to the absolute data address */
1396 target = nacl_inverse_modify_patch_target (jump_table);
1398 target = jump_table;
1402 case MONO_PATCH_INFO_METHODCONST:
1403 case MONO_PATCH_INFO_CLASS:
1404 case MONO_PATCH_INFO_IMAGE:
1405 case MONO_PATCH_INFO_FIELD:
1406 case MONO_PATCH_INFO_SIGNATURE:
1407 target = patch_info->data.target;
1409 case MONO_PATCH_INFO_IID:
1410 mono_class_init (patch_info->data.klass);
1411 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
1413 case MONO_PATCH_INFO_ADJUSTED_IID:
1414 mono_class_init (patch_info->data.klass);
1415 target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
1417 case MONO_PATCH_INFO_VTABLE:
1418 target = mono_class_vtable (domain, patch_info->data.klass);
1421 case MONO_PATCH_INFO_CLASS_INIT: {
1422 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.klass);
1425 target = mono_create_class_init_trampoline (vtable);
1428 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE: {
1429 MonoDelegateClassMethodPair *del_tramp = patch_info->data.del_tramp;
1431 if (del_tramp->virtual)
1432 target = mono_create_delegate_virtual_trampoline (domain, del_tramp->klass, del_tramp->method);
1434 target = mono_create_delegate_trampoline_info (domain, del_tramp->klass, del_tramp->method);
1437 case MONO_PATCH_INFO_SFLDA: {
1438 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
1440 if (mono_class_field_is_special_static (patch_info->data.field)) {
1441 gpointer addr = NULL;
1443 mono_domain_lock (domain);
1444 if (domain->special_static_fields)
1445 addr = g_hash_table_lookup (domain->special_static_fields, patch_info->data.field);
1446 mono_domain_unlock (domain);
1452 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
1453 /* Done by the generated code */
1457 mono_runtime_class_init (vtable);
1459 target = (char*)mono_vtable_get_static_field_data (vtable) + patch_info->data.field->offset;
1462 case MONO_PATCH_INFO_RVA: {
1463 guint32 field_index = mono_metadata_token_index (patch_info->data.token->token);
1466 mono_metadata_field_info (patch_info->data.token->image, field_index - 1, NULL, &rva, NULL);
1467 target = mono_image_rva_map (patch_info->data.token->image, rva);
1470 case MONO_PATCH_INFO_R4:
1471 case MONO_PATCH_INFO_R8:
1472 target = patch_info->data.target;
1474 case MONO_PATCH_INFO_EXC_NAME:
1475 target = patch_info->data.name;
1477 case MONO_PATCH_INFO_LDSTR:
1479 mono_ldstr (domain, patch_info->data.token->image,
1480 mono_metadata_token_index (patch_info->data.token->token));
1482 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
1484 MonoClass *handle_class;
1487 handle = mono_ldtoken_checked (patch_info->data.token->image,
1488 patch_info->data.token->token, &handle_class, patch_info->data.token->has_context ? &patch_info->data.token->context : NULL, &error);
1489 if (!mono_error_ok (&error))
1490 g_error ("Could not patch ldtoken due to %s", mono_error_get_message (&error));
1491 mono_class_init (handle_class);
1492 mono_class_init (mono_class_from_mono_type (handle));
1495 mono_type_get_object (domain, handle);
1498 case MONO_PATCH_INFO_LDTOKEN: {
1500 MonoClass *handle_class;
1503 handle = mono_ldtoken_checked (patch_info->data.token->image,
1504 patch_info->data.token->token, &handle_class, patch_info->data.token->has_context ? &patch_info->data.token->context : NULL, &error);
1505 if (!mono_error_ok (&error))
1506 g_error ("Could not patch ldtoken due to %s", mono_error_get_message (&error));
1507 mono_class_init (handle_class);
1512 case MONO_PATCH_INFO_DECLSEC:
1513 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
1515 case MONO_PATCH_INFO_ICALL_ADDR:
1516 /* run_cctors == 0 -> AOT */
1517 if (patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1518 const char *exc_class;
1519 const char *exc_arg;
1522 target = mono_lookup_pinvoke_call (patch_info->data.method, &exc_class, &exc_arg);
1525 mono_raise_exception (mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg));
1526 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));
1532 target = mono_lookup_internal_call (patch_info->data.method);
1534 if (!target && run_cctors)
1535 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info->data.method, TRUE));
1538 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1539 target = mono_thread_interruption_request_flag ();
1541 case MONO_PATCH_INFO_METHOD_RGCTX: {
1542 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.method->klass);
1545 target = mono_method_lookup_rgctx (vtable, mini_method_get_context (patch_info->data.method)->method_inst);
1548 case MONO_PATCH_INFO_BB_OVF:
1549 case MONO_PATCH_INFO_EXC_OVF:
1550 case MONO_PATCH_INFO_GOT_OFFSET:
1551 case MONO_PATCH_INFO_NONE:
1553 case MONO_PATCH_INFO_RGCTX_FETCH: {
1554 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
1557 switch (entry->data->type) {
1558 case MONO_PATCH_INFO_CLASS:
1559 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));
1561 case MONO_PATCH_INFO_METHOD:
1562 case MONO_PATCH_INFO_METHODCONST:
1563 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));
1565 case MONO_PATCH_INFO_FIELD:
1566 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));
1568 case MONO_PATCH_INFO_SIGNATURE:
1569 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));
1571 case MONO_PATCH_INFO_GSHAREDVT_CALL: {
1572 MonoJumpInfoGSharedVtCall *call_info = g_malloc0 (sizeof (MonoJumpInfoGSharedVtCall)); //mono_domain_alloc0 (domain, sizeof (MonoJumpInfoGSharedVtCall));
1574 memcpy (call_info, entry->data->data.gsharedvt, sizeof (MonoJumpInfoGSharedVtCall));
1575 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, call_info, entry->info_type, mono_method_get_context (entry->method));
1578 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
1579 MonoGSharedVtMethodInfo *info;
1580 MonoGSharedVtMethodInfo *oinfo = entry->data->data.gsharedvt_method;
1583 /* Make a copy into the domain mempool */
1584 info = g_malloc0 (sizeof (MonoGSharedVtMethodInfo)); //mono_domain_alloc0 (domain, sizeof (MonoGSharedVtMethodInfo));
1585 info->method = oinfo->method;
1586 info->num_entries = oinfo->num_entries;
1587 info->entries = g_malloc0 (sizeof (MonoRuntimeGenericContextInfoTemplate) * info->num_entries);
1588 for (i = 0; i < oinfo->num_entries; ++i) {
1589 MonoRuntimeGenericContextInfoTemplate *otemplate = &oinfo->entries [i];
1590 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
1592 memcpy (template, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
1594 slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, info, entry->info_type, mono_method_get_context (entry->method));
1598 g_assert_not_reached ();
1602 target = mono_create_rgctx_lazy_fetch_trampoline (slot);
1605 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1606 target = mono_create_generic_class_init_trampoline ();
1608 case MONO_PATCH_INFO_MONITOR_ENTER:
1609 target = mono_create_monitor_enter_trampoline ();
1611 case MONO_PATCH_INFO_MONITOR_ENTER_V4:
1612 target = mono_create_monitor_enter_v4_trampoline ();
1614 case MONO_PATCH_INFO_MONITOR_EXIT:
1615 target = mono_create_monitor_exit_trampoline ();
1617 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
1618 case MONO_PATCH_INFO_SEQ_POINT_INFO:
1620 /* AOT, not needed */
1623 target = mono_arch_get_seq_point_info (domain, code);
1626 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE:
1627 #ifdef MONO_ARCH_LLVM_SUPPORTED
1628 g_assert (mono_use_llvm);
1629 target = mono_create_llvm_imt_trampoline (domain, patch_info->data.imt_tramp->method, patch_info->data.imt_tramp->vt_offset);
1631 g_assert_not_reached ();
1634 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
1635 int card_table_shift_bits;
1636 gpointer card_table_mask;
1638 target = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
1641 case MONO_PATCH_INFO_CASTCLASS_CACHE: {
1642 target = mono_domain_alloc0 (domain, sizeof (gpointer));
1645 case MONO_PATCH_INFO_JIT_TLS_ID: {
1646 target = (gpointer) (size_t) mono_jit_tls_id;
1649 case MONO_PATCH_INFO_TLS_OFFSET: {
1652 offset = mini_get_tls_offset (GPOINTER_TO_INT (patch_info->data.target));
1653 #ifdef MONO_ARCH_HAVE_TRANSLATE_TLS_OFFSET
1654 offset = mono_arch_translate_tls_offset (offset);
1656 target = GINT_TO_POINTER (offset);
1659 case MONO_PATCH_INFO_OBJC_SELECTOR_REF: {
1663 case MONO_PATCH_INFO_LDSTR_LIT: {
1664 target = mono_string_new (domain, patch_info->data.target);
1668 g_assert_not_reached ();
1671 return (gpointer)target;
1675 get_gsharedvt_type (MonoType *t)
1677 MonoGenericParam *par = t->data.generic_param;
1678 MonoGenericParam *copy;
1680 MonoImage *image = NULL;
1683 * Create an anonymous gparam with a different serial so normal gshared and gsharedvt methods have
1684 * a different instantiation.
1686 g_assert (mono_generic_param_info (par));
1688 image = par->owner->image;
1690 mono_image_lock (image);
1691 if (!image->gsharedvt_types)
1692 image->gsharedvt_types = g_hash_table_new (NULL, NULL);
1693 res = g_hash_table_lookup (image->gsharedvt_types, par);
1694 mono_image_unlock (image);
1697 copy = mono_image_alloc0 (image, sizeof (MonoGenericParamFull));
1698 memcpy (copy, par, sizeof (MonoGenericParamFull));
1700 copy = g_memdup (par, sizeof (MonoGenericParam));
1704 copy->image = mono_defaults.corlib;
1706 res = mono_metadata_type_dup (NULL, t);
1707 res->data.generic_param = copy;
1710 mono_image_lock (image);
1711 /* Duplicates are ok */
1712 g_hash_table_insert (image->gsharedvt_types, par, res);
1713 mono_image_unlock (image);
1720 is_gsharedvt_type (MonoType *t)
1722 return (t->type == MONO_TYPE_VAR || t->type == MONO_TYPE_MVAR) && t->data.generic_param->serial == 1;
1725 /* Return whenever METHOD is a gsharedvt method */
1727 is_gsharedvt_method (MonoMethod *method)
1729 MonoGenericContext *context;
1730 MonoGenericInst *inst;
1733 if (!method->is_inflated)
1735 context = mono_method_get_context (method);
1736 inst = context->class_inst;
1738 for (i = 0; i < inst->type_argc; ++i)
1739 if (is_gsharedvt_type (inst->type_argv [i]))
1742 inst = context->method_inst;
1744 for (i = 0; i < inst->type_argc; ++i)
1745 if (is_gsharedvt_type (inst->type_argv [i]))
1752 is_open_method (MonoMethod *method)
1754 MonoGenericContext *context;
1756 if (!method->is_inflated)
1758 context = mono_method_get_context (method);
1759 if (context->class_inst && context->class_inst->is_open)
1761 if (context->method_inst && context->method_inst->is_open)
1766 static MonoGenericInst*
1767 get_shared_inst (MonoGenericInst *inst, MonoGenericInst *shared_inst, MonoGenericContainer *container, gboolean all_vt, gboolean gsharedvt)
1769 MonoGenericInst *res;
1770 MonoType **type_argv;
1773 type_argv = g_new0 (MonoType*, inst->type_argc);
1774 for (i = 0; i < inst->type_argc; ++i) {
1775 if (!all_vt && (MONO_TYPE_IS_REFERENCE (inst->type_argv [i]) || inst->type_argv [i]->type == MONO_TYPE_VAR || inst->type_argv [i]->type == MONO_TYPE_MVAR)) {
1776 type_argv [i] = shared_inst->type_argv [i];
1777 } else if (all_vt) {
1778 type_argv [i] = get_gsharedvt_type (shared_inst->type_argv [i]);
1779 } else if (gsharedvt) {
1780 type_argv [i] = get_gsharedvt_type (shared_inst->type_argv [i]);
1782 type_argv [i] = inst->type_argv [i];
1786 res = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
1792 * mini_get_shared_method_full:
1794 * Return the method which is actually compiled/registered when doing generic sharing.
1795 * If ALL_VT is true, return the shared method belonging to an all-vtype instantiation.
1796 * If IS_GSHAREDVT is true, treat METHOD as a gsharedvt method even if it fails some constraints.
1797 * METHOD can be a non-inflated generic method.
1800 mini_get_shared_method_full (MonoMethod *method, gboolean all_vt, gboolean is_gsharedvt)
1803 MonoGenericContext shared_context;
1804 MonoMethod *declaring_method, *res;
1805 gboolean partial = FALSE;
1806 gboolean gsharedvt = FALSE;
1807 MonoGenericContainer *class_container, *method_container = NULL;
1809 if (method->is_generic || (method->klass->generic_container && !method->is_inflated)) {
1810 declaring_method = method;
1812 declaring_method = mono_method_get_declaring_generic_method (method);
1815 if (declaring_method->is_generic)
1816 shared_context = mono_method_get_generic_container (declaring_method)->context;
1818 shared_context = declaring_method->klass->generic_container->context;
1820 /* Handle gsharedvt/partial sharing */
1821 if ((method != declaring_method && method->is_inflated && !mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) ||
1822 is_gsharedvt || mini_is_gsharedvt_sharable_method (method)) {
1823 MonoGenericContext *context = mono_method_get_context (method);
1824 MonoGenericInst *inst;
1826 partial = mono_method_is_generic_sharable_full (method, FALSE, TRUE, FALSE);
1828 gsharedvt = is_gsharedvt || (!partial && mini_is_gsharedvt_sharable_method (method));
1830 class_container = declaring_method->klass->generic_container;
1831 method_container = mono_method_get_generic_container (declaring_method);
1834 * Create the shared context by replacing the ref type arguments with
1835 * type parameters, and keeping the rest.
1838 inst = context->class_inst;
1840 inst = shared_context.class_inst;
1842 shared_context.class_inst = get_shared_inst (inst, shared_context.class_inst, class_container, all_vt, gsharedvt);
1845 inst = context->method_inst;
1847 inst = shared_context.method_inst;
1849 shared_context.method_inst = get_shared_inst (inst, shared_context.method_inst, method_container, all_vt, gsharedvt);
1854 res = mono_class_inflate_generic_method_checked (declaring_method, &shared_context, &error);
1855 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
1858 /* The result should be an inflated method whose parent is not inflated */
1859 g_assert (!res->klass->is_inflated);
1865 mini_get_shared_method (MonoMethod *method)
1867 return mini_get_shared_method_full (method, FALSE, FALSE);
1871 mini_init_gsctx (MonoDomain *domain, MonoMemPool *mp, MonoGenericContext *context, MonoGenericSharingContext *gsctx)
1873 MonoGenericInst *inst;
1876 memset (gsctx, 0, sizeof (MonoGenericSharingContext));
1878 if (context && context->class_inst) {
1879 inst = context->class_inst;
1881 gsctx->var_is_vt = mono_domain_alloc0 (domain, sizeof (gboolean) * inst->type_argc);
1883 gsctx->var_is_vt = mono_mempool_alloc0 (mp, sizeof (gboolean) * inst->type_argc);
1885 gsctx->var_is_vt = g_new0 (gboolean, inst->type_argc);
1887 for (i = 0; i < inst->type_argc; ++i) {
1888 MonoType *type = inst->type_argv [i];
1890 if ((type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR) && type->data.generic_param->serial == 1)
1891 gsctx->var_is_vt [i] = TRUE;
1894 if (context && context->method_inst) {
1895 inst = context->method_inst;
1897 gsctx->mvar_is_vt = mono_domain_alloc0 (domain, sizeof (gboolean) * inst->type_argc);
1899 gsctx->mvar_is_vt = mono_mempool_alloc0 (mp, sizeof (gboolean) * inst->type_argc);
1901 gsctx->mvar_is_vt = g_new0 (gboolean, inst->type_argc);
1903 for (i = 0; i < inst->type_argc; ++i) {
1904 MonoType *type = inst->type_argv [i];
1906 if ((type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR) && type->data.generic_param->serial == 1)
1907 gsctx->mvar_is_vt [i] = TRUE;
1913 * LOCKING: Acquires the jit code hash lock.
1916 mini_lookup_method (MonoDomain *domain, MonoMethod *method, MonoMethod *shared)
1919 static gboolean inited = FALSE;
1920 static int lookups = 0;
1921 static int failed_lookups = 0;
1923 mono_domain_jit_code_hash_lock (domain);
1924 ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, method);
1925 if (!ji && shared) {
1926 /* Try generic sharing */
1927 ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, shared);
1928 if (ji && !ji->has_generic_jit_info)
1931 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT|MONO_COUNTER_GENERICS, &lookups);
1932 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT|MONO_COUNTER_GENERICS, &failed_lookups);
1940 mono_domain_jit_code_hash_unlock (domain);
1946 lookup_method (MonoDomain *domain, MonoMethod *method)
1951 ji = mini_lookup_method (domain, method, NULL);
1954 if (!mono_method_is_generic_sharable (method, FALSE))
1956 shared = mini_get_shared_method (method);
1957 ji = mini_lookup_method (domain, method, shared);
1964 static FILE* perf_map_file;
1967 mono_enable_jit_map (void)
1969 if (!perf_map_file) {
1971 g_snprintf (name, sizeof (name), "/tmp/perf-%d.map", getpid ());
1973 perf_map_file = fopen (name, "w");
1978 mono_emit_jit_tramp (void *start, int size, const char *desc)
1981 fprintf (perf_map_file, "%llx %x %s\n", (long long unsigned int)(gsize)start, size, desc);
1985 mono_emit_jit_map (MonoJitInfo *jinfo)
1987 if (perf_map_file) {
1988 char *name = mono_method_full_name (jinfo_get_method (jinfo), TRUE);
1989 mono_emit_jit_tramp (jinfo->code_start, jinfo->code_size, name);
1995 mono_jit_map_is_enabled (void)
1997 return perf_map_file != NULL;
2003 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt, MonoException **ex)
2005 MonoDomain *target_domain, *domain = mono_domain_get ();
2009 MonoJitICallInfo *callinfo = NULL;
2010 WrapperInfo *winfo = NULL;
2013 * ICALL wrappers are handled specially, since there is only one copy of them
2014 * shared by all appdomains.
2016 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2017 winfo = mono_marshal_get_wrapper_info (method);
2018 if (winfo && winfo->subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
2019 callinfo = mono_find_jit_icall_by_addr (winfo->d.icall.func);
2020 g_assert (callinfo);
2022 /* Must be domain neutral since there is only one copy */
2023 opt |= MONO_OPT_SHARED;
2026 if (method->dynamic)
2027 opt &= ~MONO_OPT_SHARED;
2029 /* These methods can become invalid when a domain is unloaded */
2030 if (method->klass->image != mono_get_corlib () || method->is_inflated)
2031 opt &= ~MONO_OPT_SHARED;
2033 if (opt & MONO_OPT_SHARED)
2034 target_domain = mono_get_root_domain ();
2036 target_domain = domain;
2038 if (method->wrapper_type == MONO_WRAPPER_UNKNOWN) {
2039 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2042 if (info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
2043 MonoGenericContext *ctx = NULL;
2044 if (method->is_inflated)
2045 ctx = mono_method_get_context (method);
2046 method = info->d.synchronized_inner.method;
2049 method = mono_class_inflate_generic_method_checked (method, ctx, &error);
2050 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
2055 info = lookup_method (target_domain, method);
2057 /* We can't use a domain specific method in another domain */
2058 if (! ((domain != target_domain) && !info->domain_neutral)) {
2060 MonoException *tmpEx;
2062 mono_jit_stats.methods_lookups++;
2063 vtable = mono_class_vtable (domain, method->klass);
2065 tmpEx = mono_runtime_class_init_full (vtable, ex == NULL);
2070 return mono_create_ftnptr (target_domain, info->code_start);
2074 code = mono_jit_compile_method_inner (method, target_domain, opt, ex);
2078 if (method->wrapper_type == MONO_WRAPPER_WRITE_BARRIER || method->wrapper_type == MONO_WRAPPER_ALLOC) {
2082 * SGEN requires the JIT info for these methods to be registered, see is_ip_in_managed_allocator ().
2084 ji = mini_jit_info_table_find (mono_domain_get (), code, &d);
2088 p = mono_create_ftnptr (target_domain, code);
2091 /*mono_register_jit_icall_wrapper takes the loader lock, so we take it on the outside. */
2092 mono_loader_lock ();
2094 if (!callinfo->wrapper) {
2095 callinfo->wrapper = p;
2096 mono_register_jit_icall_wrapper (callinfo, p);
2099 mono_loader_unlock ();
2106 mono_jit_compile_method (MonoMethod *method)
2108 MonoException *ex = NULL;
2111 code = mono_jit_compile_method_with_opt (method, mono_get_optimizations_for_method (method, default_opt), &ex);
2114 mono_raise_exception (ex);
2120 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
2122 invalidated_delegate_trampoline (char *desc)
2124 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
2125 "See http://www.mono-project.com/Diagnostic:Delegate for an explanation and ways to fix this.",
2131 * mono_jit_free_method:
2133 * Free all memory allocated by the JIT for METHOD.
2136 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
2138 MonoJitDynamicMethodInfo *ji;
2139 gboolean destroy = TRUE;
2140 GHashTableIter iter;
2141 MonoJumpList *jlist;
2143 g_assert (method->dynamic);
2145 mono_domain_lock (domain);
2146 ji = mono_dynamic_code_hash_lookup (domain, method);
2147 mono_domain_unlock (domain);
2152 mono_debug_remove_method (method, domain);
2154 mono_domain_lock (domain);
2155 g_hash_table_remove (domain_jit_info (domain)->dynamic_code_hash, method);
2156 mono_domain_jit_code_hash_lock (domain);
2157 mono_internal_hash_table_remove (&domain->jit_code_hash, method);
2158 mono_domain_jit_code_hash_unlock (domain);
2159 g_hash_table_remove (domain_jit_info (domain)->jump_trampoline_hash, method);
2160 mono_conc_hashtable_remove (domain_jit_info (domain)->runtime_invoke_hash, method);
2162 /* Remove jump targets in this method */
2163 g_hash_table_iter_init (&iter, domain_jit_info (domain)->jump_target_hash);
2164 while (g_hash_table_iter_next (&iter, NULL, (void**)&jlist)) {
2165 GSList *tmp, *remove;
2168 for (tmp = jlist->list; tmp; tmp = tmp->next) {
2169 guint8 *ip = tmp->data;
2171 if (ip >= (guint8*)ji->ji->code_start && ip < (guint8*)ji->ji->code_start + ji->ji->code_size)
2172 remove = g_slist_prepend (remove, tmp);
2174 for (tmp = remove; tmp; tmp = tmp->next) {
2175 jlist->list = g_slist_delete_link (jlist->list, tmp->data);
2177 g_slist_free (remove);
2180 mono_domain_unlock (domain);
2182 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
2183 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
2185 * Instead of freeing the code, change it to call an error routine
2186 * so people can fix their code.
2188 char *type = mono_type_full_name (&method->klass->byval_arg);
2189 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
2192 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
2198 * This needs to be done before freeing code_mp, since the code address is the
2199 * key in the table, so if we free the code_mp first, another thread can grab the
2200 * same code address and replace our entry in the table.
2202 mono_jit_info_table_remove (domain, ji->ji);
2205 mono_code_manager_destroy (ji->code_mp);
2210 mono_jit_find_compiled_method_with_jit_info (MonoDomain *domain, MonoMethod *method, MonoJitInfo **ji)
2212 MonoDomain *target_domain;
2215 if (default_opt & MONO_OPT_SHARED)
2216 target_domain = mono_get_root_domain ();
2218 target_domain = domain;
2220 info = lookup_method (target_domain, method);
2222 /* We can't use a domain specific method in another domain */
2223 if (! ((domain != target_domain) && !info->domain_neutral)) {
2224 mono_jit_stats.methods_lookups++;
2227 return info->code_start;
2236 gboolean mono_do_single_method_regression = FALSE;
2237 guint32 mono_single_method_regression_opt = 0;
2238 MonoMethod *mono_current_single_method;
2239 GSList *mono_single_method_list;
2240 GHashTable *mono_single_method_hash;
2243 mono_get_optimizations_for_method (MonoMethod *method, guint32 default_opt)
2247 if (!mono_do_single_method_regression)
2249 if (!mono_current_single_method) {
2250 if (!mono_single_method_hash)
2251 mono_single_method_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
2252 if (!g_hash_table_lookup (mono_single_method_hash, method)) {
2253 g_hash_table_insert (mono_single_method_hash, method, method);
2254 mono_single_method_list = g_slist_prepend (mono_single_method_list, method);
2258 if (method == mono_current_single_method)
2259 return mono_single_method_regression_opt;
2264 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
2266 return mono_jit_find_compiled_method_with_jit_info (domain, method, NULL);
2271 gpointer compiled_method;
2272 gpointer runtime_invoke;
2274 MonoDynCallInfo *dyn_call_info;
2275 MonoClass *ret_box_class;
2276 } RuntimeInvokeInfo;
2279 * mono_jit_runtime_invoke:
2280 * @method: the method to invoke
2281 * @obj: this pointer
2282 * @params: array of parameter values.
2283 * @exc: used to catch exceptions objects
2286 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
2288 MonoMethod *invoke, *callee;
2289 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
2290 MonoDomain *domain = mono_domain_get ();
2291 MonoJitDomainInfo *domain_info;
2292 RuntimeInvokeInfo *info, *info2;
2294 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
2295 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
2299 domain_info = domain_jit_info (domain);
2301 info = mono_conc_hashtable_lookup (domain_info->runtime_invoke_hash, method);
2304 if (mono_security_core_clr_enabled ()) {
2306 * This might be redundant since mono_class_vtable () already does this,
2307 * but keep it just in case for moonlight.
2309 mono_class_setup_vtable (method->klass);
2310 if (method->klass->exception_type != MONO_EXCEPTION_NONE) {
2312 *exc = (MonoObject*)mono_class_get_exception_for_failure (method->klass);
2314 mono_raise_exception (mono_class_get_exception_for_failure (method->klass));
2319 info = g_new0 (RuntimeInvokeInfo, 1);
2321 invoke = mono_marshal_get_runtime_invoke (method, FALSE);
2322 info->vtable = mono_class_vtable_full (domain, method->klass, TRUE);
2323 g_assert (info->vtable);
2326 if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
2327 (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
2329 * Array Get/Set/Address methods. The JIT implements them using inline code
2330 * inside the runtime invoke wrappers, so no need to compile them.
2332 if (mono_aot_only) {
2334 * Call a wrapper, since the runtime invoke wrapper was not generated.
2336 MonoMethod *wrapper;
2338 wrapper = mono_marshal_get_array_accessor_wrapper (method);
2339 invoke = mono_marshal_get_runtime_invoke (wrapper, FALSE);
2347 MonoException *jit_ex = NULL;
2349 info->compiled_method = mono_jit_compile_method_with_opt (callee, mono_get_optimizations_for_method (callee, default_opt), &jit_ex);
2350 if (!info->compiled_method) {
2354 *exc = (MonoObject*)jit_ex;
2357 mono_raise_exception (jit_ex);
2361 info->compiled_method = mini_add_method_trampoline (NULL, callee, info->compiled_method, mono_method_needs_static_rgctx_invoke (callee, FALSE), FALSE);
2363 info->compiled_method = NULL;
2367 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
2368 * in full-aot mode, so we use a slower, but more generic wrapper if
2369 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
2371 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2372 if (mono_aot_only || debug_options.dyn_runtime_invoke) {
2374 MonoMethodSignature *sig = mono_method_signature (method);
2375 gboolean supported = TRUE;
2378 if (method->string_ctor)
2379 sig = mono_marshal_get_string_ctor_signature (method);
2381 for (i = 0; i < sig->param_count; ++i) {
2382 MonoType *t = sig->params [i];
2384 if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t)))
2388 if (mono_class_is_contextbound (method->klass) || !info->compiled_method)
2392 info->dyn_call_info = mono_arch_dyn_call_prepare (sig);
2394 ret_type = sig->ret;
2395 if (info->dyn_call_info) {
2396 switch (ret_type->type) {
2397 case MONO_TYPE_VOID:
2409 case MONO_TYPE_BOOLEAN:
2410 case MONO_TYPE_CHAR:
2413 info->ret_box_class = mono_class_from_mono_type (ret_type);
2416 info->ret_box_class = mono_defaults.int_class;
2418 case MONO_TYPE_STRING:
2419 case MONO_TYPE_CLASS:
2420 case MONO_TYPE_ARRAY:
2421 case MONO_TYPE_SZARRAY:
2422 case MONO_TYPE_OBJECT:
2424 case MONO_TYPE_GENERICINST:
2425 if (!MONO_TYPE_IS_REFERENCE (ret_type))
2426 info->ret_box_class = mono_class_from_mono_type (ret_type);
2428 case MONO_TYPE_VALUETYPE:
2429 info->ret_box_class = mono_class_from_mono_type (ret_type);
2432 g_assert_not_reached ();
2439 if (!info->dyn_call_info)
2440 info->runtime_invoke = mono_jit_compile_method (invoke);
2442 info2 = mono_conc_hashtable_insert (domain_info->runtime_invoke_hash, method, info);
2449 runtime_invoke = info->runtime_invoke;
2452 * We need this here because mono_marshal_get_runtime_invoke can place
2453 * the helper method in System.Object and not the target class.
2456 *exc = (MonoObject*)mono_runtime_class_init_full (info->vtable, FALSE);
2460 mono_runtime_class_init (info->vtable);
2463 /* The wrappers expect this to be initialized to NULL */
2467 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2468 if (info->dyn_call_info) {
2469 MonoMethodSignature *sig = mono_method_signature (method);
2471 static RuntimeInvokeDynamicFunction dyn_runtime_invoke;
2474 guint8 retval [256];
2476 if (!dyn_runtime_invoke) {
2477 invoke = mono_marshal_get_runtime_invoke_dynamic ();
2478 dyn_runtime_invoke = mono_jit_compile_method (invoke);
2481 /* Convert the arguments to the format expected by start_dyn_call () */
2482 args = g_alloca ((sig->param_count + sig->hasthis) * sizeof (gpointer));
2485 args [pindex ++] = &obj;
2486 for (i = 0; i < sig->param_count; ++i) {
2487 MonoType *t = sig->params [i];
2490 args [pindex ++] = ¶ms [i];
2491 } else if (MONO_TYPE_IS_REFERENCE (t) || t->type == MONO_TYPE_PTR) {
2492 args [pindex ++] = ¶ms [i];
2494 args [pindex ++] = params [i];
2498 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
2500 mono_arch_start_dyn_call (info->dyn_call_info, (gpointer**)args, retval, buf, sizeof (buf));
2502 dyn_runtime_invoke (buf, exc, info->compiled_method);
2504 mono_arch_finish_dyn_call (info->dyn_call_info, buf);
2506 if (info->ret_box_class)
2507 return mono_value_box (domain, info->ret_box_class, retval);
2509 return *(MonoObject**)retval;
2513 return runtime_invoke (obj, params, exc, info->compiled_method);
2516 MONO_SIG_HANDLER_FUNC (, mono_sigfpe_signal_handler)
2518 MonoException *exc = NULL;
2520 MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
2521 MONO_SIG_HANDLER_GET_CONTEXT;
2523 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
2525 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
2526 if (mono_arch_is_int_overflow (ctx, info))
2528 * The spec says this throws ArithmeticException, but MS throws the derived
2529 * OverflowException.
2531 exc = mono_get_exception_overflow ();
2533 exc = mono_get_exception_divide_by_zero ();
2535 exc = mono_get_exception_divide_by_zero ();
2539 if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2542 mono_handle_native_sigsegv (SIGSEGV, ctx, info);
2543 if (mono_do_crash_chaining) {
2544 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
2549 mono_arch_handle_exception (ctx, exc);
2552 MONO_SIG_HANDLER_FUNC (, mono_sigill_signal_handler)
2555 MONO_SIG_HANDLER_GET_CONTEXT;
2557 exc = mono_get_exception_execution_engine ("SIGILL");
2559 mono_arch_handle_exception (ctx, exc);
2562 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
2563 #define HAVE_SIG_INFO
2566 MONO_SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler)
2569 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2570 gpointer fault_addr = NULL;
2571 #ifdef HAVE_SIG_INFO
2572 MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
2576 MONO_SIG_HANDLER_GET_CONTEXT;
2578 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
2579 if (mono_arch_is_single_step_event (info, ctx)) {
2580 mono_debugger_agent_single_step_event (ctx);
2582 } else if (mono_arch_is_breakpoint_event (info, ctx)) {
2583 mono_debugger_agent_breakpoint_hit (ctx);
2588 #if defined(HAVE_SIG_INFO)
2589 #if !defined(HOST_WIN32)
2590 fault_addr = info->si_addr;
2591 if (mono_aot_is_pagefault (info->si_addr)) {
2592 mono_aot_handle_pagefault (info->si_addr);
2597 /* The thread might no be registered with the runtime */
2598 if (!mono_domain_get () || !jit_tls) {
2599 if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2601 mono_handle_native_sigsegv (SIGSEGV, ctx, info);
2602 if (mono_do_crash_chaining) {
2603 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
2609 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
2611 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2612 if (mono_handle_soft_stack_ovf (jit_tls, ji, ctx, info, (guint8*)info->si_addr))
2615 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2616 /* info->si_addr seems to be NULL on some kernels when handling stack overflows */
2617 fault_addr = info->si_addr;
2618 if (fault_addr == NULL) {
2621 mono_sigctx_to_monoctx (ctx, &mctx);
2623 fault_addr = MONO_CONTEXT_GET_SP (&mctx);
2627 if (jit_tls->stack_size &&
2628 ABS ((guint8*)fault_addr - ((guint8*)jit_tls->end_of_stack - jit_tls->stack_size)) < 8192 * sizeof (gpointer)) {
2630 * The hard-guard page has been hit: there is not much we can do anymore
2631 * Print a hopefully clear message and abort.
2633 mono_handle_hard_stack_ovf (jit_tls, ji, ctx, (guint8*)info->si_addr);
2634 g_assert_not_reached ();
2636 /* The original handler might not like that it is executed on an altstack... */
2637 if (!ji && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2640 mono_arch_handle_altstack_exception (ctx, info, info->si_addr, FALSE);
2645 if (!mono_do_crash_chaining && mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
2648 mono_handle_native_sigsegv (SIGSEGV, ctx, info);
2650 if (mono_do_crash_chaining) {
2651 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
2656 mono_arch_handle_exception (ctx, NULL);
2660 MONO_SIG_HANDLER_FUNC (, mono_sigint_signal_handler)
2663 MONO_SIG_HANDLER_GET_CONTEXT;
2665 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
2667 mono_arch_handle_exception (ctx, exc);
2670 #ifndef DISABLE_REMOTING
2671 /* mono_jit_create_remoting_trampoline:
2672 * @method: pointer to the method info
2674 * Creates a trampoline which calls the remoting functions. This
2675 * is used in the vtable of transparent proxies.
2677 * Returns: a pointer to the newly created code
2680 mono_jit_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target)
2683 guint8 *addr = NULL;
2685 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && mono_method_signature (method)->generic_param_count) {
2686 return mono_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING,
2690 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
2691 (mono_method_signature (method)->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))) {
2692 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
2693 addr = mono_compile_method (nm);
2696 addr = mono_compile_method (method);
2698 return mono_get_addr_from_ftnptr (addr);
2702 static gpointer *vtable_trampolines;
2703 static int vtable_trampolines_size;
2706 mini_get_vtable_trampoline (int slot_index)
2708 int index = slot_index + MONO_IMT_SIZE;
2710 g_assert (slot_index >= - MONO_IMT_SIZE);
2711 if (!vtable_trampolines || slot_index + MONO_IMT_SIZE >= vtable_trampolines_size) {
2713 if (!vtable_trampolines || index >= vtable_trampolines_size) {
2717 new_size = vtable_trampolines_size ? vtable_trampolines_size * 2 : 128;
2718 while (new_size <= index)
2720 new_table = g_new0 (gpointer, new_size);
2722 if (vtable_trampolines)
2723 memcpy (new_table, vtable_trampolines, vtable_trampolines_size * sizeof (gpointer));
2724 g_free (vtable_trampolines);
2725 mono_memory_barrier ();
2726 vtable_trampolines = new_table;
2727 vtable_trampolines_size = new_size;
2732 if (!vtable_trampolines [index])
2733 vtable_trampolines [index] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot_index), MONO_TRAMPOLINE_VCALL, mono_get_root_domain (), NULL);
2734 return vtable_trampolines [index];
2738 mono_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method)
2740 gboolean is_virtual_generic, is_interface, load_imt_reg;
2743 static guint8 **cache = NULL;
2744 static int cache_size = 0;
2749 /* FIXME Support more cases */
2753 if (MONO_TYPE_ISSTRUCT (sig->ret))
2756 is_virtual_generic = method->is_inflated && mono_method_get_declaring_generic_method (method)->is_generic;
2757 is_interface = method->klass->flags & TYPE_ATTRIBUTE_INTERFACE ? TRUE : FALSE;
2758 load_imt_reg = is_virtual_generic || is_interface;
2760 if (is_interface && !is_virtual_generic)
2761 offset = ((gint32)mono_method_get_imt_slot (method) - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2763 offset = G_STRUCT_OFFSET (MonoVTable, vtable) + ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2765 idx = (offset / SIZEOF_VOID_P + MONO_IMT_SIZE) * 2 + (load_imt_reg ? 1 : 0);
2766 g_assert (idx >= 0);
2768 /* Resize the cache to idx + 1 */
2769 if (cache_size < idx + 1) {
2771 if (cache_size < idx + 1) {
2773 int new_cache_size = idx + 1;
2775 new_cache = g_new0 (guint8*, new_cache_size);
2777 memcpy (new_cache, cache, cache_size * sizeof (guint8*));
2780 mono_memory_barrier ();
2782 cache_size = new_cache_size;
2790 return cache [idx] = mono_arch_get_delegate_virtual_invoke_impl (sig, method, offset, load_imt_reg);
2794 mini_get_imt_trampoline (int slot_index)
2796 return mini_get_vtable_trampoline (slot_index - MONO_IMT_SIZE);
2800 mini_parse_debug_options (void)
2802 const char *options = g_getenv ("MONO_DEBUG");
2803 gchar **args, **ptr;
2808 args = g_strsplit (options, ",", -1);
2810 for (ptr = args; ptr && *ptr; ptr++) {
2811 const char *arg = *ptr;
2813 if (!strcmp (arg, "handle-sigint"))
2814 debug_options.handle_sigint = TRUE;
2815 else if (!strcmp (arg, "keep-delegates"))
2816 debug_options.keep_delegates = TRUE;
2817 else if (!strcmp (arg, "reverse-pinvoke-exceptions"))
2818 debug_options.reverse_pinvoke_exceptions = TRUE;
2819 else if (!strcmp (arg, "collect-pagefault-stats"))
2820 debug_options.collect_pagefault_stats = TRUE;
2821 else if (!strcmp (arg, "break-on-unverified"))
2822 debug_options.break_on_unverified = TRUE;
2823 else if (!strcmp (arg, "no-gdb-backtrace"))
2824 debug_options.no_gdb_backtrace = TRUE;
2825 else if (!strcmp (arg, "suspend-on-sigsegv"))
2826 debug_options.suspend_on_sigsegv = TRUE;
2827 else if (!strcmp (arg, "suspend-on-exception"))
2828 debug_options.suspend_on_exception = TRUE;
2829 else if (!strcmp (arg, "suspend-on-unhandled"))
2830 debug_options.suspend_on_unhandled = TRUE;
2831 else if (!strcmp (arg, "dont-free-domains"))
2832 mono_dont_free_domains = TRUE;
2833 else if (!strcmp (arg, "dyn-runtime-invoke"))
2834 debug_options.dyn_runtime_invoke = TRUE;
2835 else if (!strcmp (arg, "gdb"))
2836 debug_options.gdb = TRUE;
2837 else if (!strcmp (arg, "explicit-null-checks"))
2838 debug_options.explicit_null_checks = TRUE;
2839 else if (!strcmp (arg, "gen-seq-points"))
2840 debug_options.gen_seq_points_debug_data = TRUE;
2841 else if (!strcmp (arg, "gen-compact-seq-points"))
2842 debug_options.gen_seq_points_compact_data = TRUE;
2843 else if (!strcmp (arg, "init-stacks"))
2844 debug_options.init_stacks = TRUE;
2845 else if (!strcmp (arg, "casts"))
2846 debug_options.better_cast_details = TRUE;
2847 else if (!strcmp (arg, "soft-breakpoints"))
2848 debug_options.soft_breakpoints = TRUE;
2849 else if (!strcmp (arg, "check-pinvoke-callconv"))
2850 debug_options.check_pinvoke_callconv = TRUE;
2851 else if (!strcmp (arg, "debug-domain-unload"))
2852 mono_enable_debug_domain_unload (TRUE);
2854 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
2855 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'\n");
2864 mini_get_debug_options (void)
2866 return &debug_options;
2870 mini_create_ftnptr (MonoDomain *domain, gpointer addr)
2872 #if !defined(__ia64__) && (!defined(__ppc64__) && !defined(__powerpc64__) || _CALL_ELF == 2)
2875 gpointer* desc = NULL;
2877 if ((desc = g_hash_table_lookup (domain->ftnptrs_hash, addr)))
2880 desc = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
2884 # elif defined(__ppc64__) || defined(__powerpc64__)
2886 desc = mono_domain_alloc0 (domain, 3 * sizeof (gpointer));
2892 g_hash_table_insert (domain->ftnptrs_hash, addr, desc);
2898 mini_get_addr_from_ftnptr (gpointer descr)
2900 #if defined(__ia64__) || ((defined(__ppc64__) || defined(__powerpc64__)) && _CALL_ELF != 2)
2901 return *(gpointer*)descr;
2908 register_jit_stats (void)
2910 mono_counters_register ("Compiled methods", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_compiled);
2911 mono_counters_register ("Methods from AOT", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_aot);
2912 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_without_llvm);
2913 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_with_llvm);
2914 mono_counters_register ("Total time spent JITting (sec)", MONO_COUNTER_JIT | MONO_COUNTER_DOUBLE, &mono_jit_stats.jit_time);
2915 mono_counters_register ("Basic blocks", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.basic_blocks);
2916 mono_counters_register ("Max basic blocks", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.max_basic_blocks);
2917 mono_counters_register ("Allocated vars", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.allocate_var);
2918 mono_counters_register ("Code reallocs", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.code_reallocs);
2919 mono_counters_register ("Allocated code size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.allocated_code_size);
2920 mono_counters_register ("Allocated seq points size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.allocated_seq_points_size);
2921 mono_counters_register ("Inlineable methods", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.inlineable_methods);
2922 mono_counters_register ("Inlined methods", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.inlined_methods);
2923 mono_counters_register ("Regvars", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.regvars);
2924 mono_counters_register ("Locals stack size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.locals_stack_size);
2925 mono_counters_register ("Method cache lookups", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.methods_lookups);
2926 mono_counters_register ("Compiled CIL code size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.cil_code_size);
2927 mono_counters_register ("Native code size", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.native_code_size);
2928 mono_counters_register ("Aliases found", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.alias_found);
2929 mono_counters_register ("Aliases eliminated", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.alias_removed);
2930 mono_counters_register ("Aliased loads eliminated", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.loads_eliminated);
2931 mono_counters_register ("Aliased stores eliminated", MONO_COUNTER_JIT | MONO_COUNTER_INT, &mono_jit_stats.stores_eliminated);
2934 static void runtime_invoke_info_free (gpointer value);
2937 class_method_pair_equal (gconstpointer ka, gconstpointer kb)
2939 const MonoClassMethodPair *apair = ka;
2940 const MonoClassMethodPair *bpair = kb;
2942 return apair->klass == bpair->klass && apair->method == bpair->method ? 1 : 0;
2946 class_method_pair_hash (gconstpointer data)
2948 const MonoClassMethodPair *pair = data;
2950 return (gsize)pair->klass ^ (gsize)pair->method;
2954 mini_create_jit_domain_info (MonoDomain *domain)
2956 MonoJitDomainInfo *info = g_new0 (MonoJitDomainInfo, 1);
2958 info->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2959 info->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2960 info->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2961 info->delegate_trampoline_hash = g_hash_table_new (class_method_pair_hash, class_method_pair_equal);
2962 info->llvm_vcall_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2963 info->runtime_invoke_hash = mono_conc_hashtable_new_full (&domain->lock, mono_aligned_addr_hash, NULL, NULL, runtime_invoke_info_free);
2964 info->seq_points = g_hash_table_new_full (mono_aligned_addr_hash, NULL, NULL, seq_point_info_free);
2965 info->arch_seq_points = g_hash_table_new (mono_aligned_addr_hash, NULL);
2966 info->jump_target_hash = g_hash_table_new (NULL, NULL);
2968 domain->runtime_info = info;
2972 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
2974 MonoJumpList *jlist = value;
2975 g_slist_free (jlist->list);
2979 delete_got_slot_list (gpointer key, gpointer value, gpointer user_data)
2981 GSList *list = value;
2982 g_slist_free (list);
2986 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
2988 MonoJitDynamicMethodInfo *di = value;
2989 mono_code_manager_destroy (di->code_mp);
2994 runtime_invoke_info_free (gpointer value)
2996 RuntimeInvokeInfo *info = (RuntimeInvokeInfo*)value;
2998 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2999 if (info->dyn_call_info)
3000 mono_arch_dyn_call_free (info->dyn_call_info);
3006 mini_free_jit_domain_info (MonoDomain *domain)
3008 MonoJitDomainInfo *info = domain_jit_info (domain);
3010 g_hash_table_foreach (info->jump_target_hash, delete_jump_list, NULL);
3011 g_hash_table_destroy (info->jump_target_hash);
3012 if (info->jump_target_got_slot_hash) {
3013 g_hash_table_foreach (info->jump_target_got_slot_hash, delete_got_slot_list, NULL);
3014 g_hash_table_destroy (info->jump_target_got_slot_hash);
3016 if (info->dynamic_code_hash) {
3017 g_hash_table_foreach (info->dynamic_code_hash, dynamic_method_info_free, NULL);
3018 g_hash_table_destroy (info->dynamic_code_hash);
3020 if (info->method_code_hash)
3021 g_hash_table_destroy (info->method_code_hash);
3022 g_hash_table_destroy (info->class_init_trampoline_hash);
3023 g_hash_table_destroy (info->jump_trampoline_hash);
3024 g_hash_table_destroy (info->jit_trampoline_hash);
3025 g_hash_table_destroy (info->delegate_trampoline_hash);
3026 if (info->static_rgctx_trampoline_hash)
3027 g_hash_table_destroy (info->static_rgctx_trampoline_hash);
3028 g_hash_table_destroy (info->llvm_vcall_trampoline_hash);
3029 mono_conc_hashtable_destroy (info->runtime_invoke_hash);
3030 g_hash_table_destroy (info->seq_points);
3031 g_hash_table_destroy (info->arch_seq_points);
3032 if (info->agent_info)
3033 mono_debugger_agent_free_domain_info (domain);
3034 if (info->gsharedvt_arg_tramp_hash)
3035 g_hash_table_destroy (info->gsharedvt_arg_tramp_hash);
3037 mono_llvm_free_domain_info (domain);
3040 g_free (domain->runtime_info);
3041 domain->runtime_info = NULL;
3046 llvm_init_inner (void)
3048 if (!mono_llvm_load (NULL))
3059 * Load and initialize LLVM support.
3060 * Return TRUE on success.
3063 mini_llvm_init (void)
3066 static gboolean llvm_inited;
3067 static gboolean init_result;
3069 mono_loader_lock_if_inited ();
3071 init_result = llvm_init_inner ();
3074 mono_loader_unlock_if_inited ();
3082 mini_init (const char *filename, const char *runtime_version)
3085 MonoRuntimeCallbacks callbacks;
3086 MonoThreadInfoRuntimeCallbacks ticallbacks;
3088 MONO_VES_INIT_BEGIN ();
3090 #if defined(__linux__) && !defined(__native_client__)
3091 if (access ("/proc/self/maps", F_OK) != 0) {
3092 g_print ("Mono requires /proc to be mounted.\n");
3097 mono_mutex_init_recursive (&jit_mutex);
3099 mono_cross_helpers_run ();
3103 /* Happens when using the embedding interface */
3104 if (!default_opt_set)
3105 default_opt = mono_parse_default_optimizations (NULL);
3107 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
3109 mono_set_generic_sharing_vt_supported (TRUE);
3112 #ifdef MONO_HAVE_FAST_TLS
3113 MONO_FAST_TLS_INIT (mono_jit_tls);
3114 MONO_FAST_TLS_INIT (mono_lmf_addr);
3115 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
3116 MONO_FAST_TLS_INIT (mono_lmf);
3120 mono_runtime_set_has_tls_get (MONO_ARCH_HAVE_TLS_GET);
3122 if (!global_codeman)
3123 global_codeman = mono_code_manager_new ();
3125 memset (&callbacks, 0, sizeof (callbacks));
3126 callbacks.create_ftnptr = mini_create_ftnptr;
3127 callbacks.get_addr_from_ftnptr = mini_get_addr_from_ftnptr;
3128 callbacks.get_runtime_build_info = mono_get_runtime_build_info;
3129 callbacks.set_cast_details = mono_set_cast_details;
3130 callbacks.debug_log = mono_debugger_agent_debug_log;
3131 callbacks.debug_log_is_enabled = mono_debugger_agent_debug_log_is_enabled;
3132 callbacks.tls_key_supported = mini_tls_key_supported;
3135 callbacks.get_vtable_trampoline = mini_get_vtable_trampoline;
3136 callbacks.get_imt_trampoline = mini_get_imt_trampoline;
3139 mono_install_callbacks (&callbacks);
3141 memset (&ticallbacks, 0, sizeof (ticallbacks));
3142 ticallbacks.setup_async_callback = mono_setup_async_callback;
3143 ticallbacks.thread_state_init_from_sigctx = mono_thread_state_init_from_sigctx;
3144 ticallbacks.thread_state_init_from_handle = mono_thread_state_init_from_handle;
3146 mono_counters_init ();
3148 mono_threads_runtime_init (&ticallbacks);
3150 if (g_getenv ("MONO_DEBUG") != NULL)
3151 mini_parse_debug_options ();
3153 mono_code_manager_init ();
3157 mono_arch_cpu_init ();
3161 mono_unwind_init ();
3163 if (g_getenv ("MONO_XDEBUG")) {
3164 const char *xdebug_opts = g_getenv ("MONO_XDEBUG");
3165 mono_xdebug_init (xdebug_opts);
3166 /* So methods for multiple domains don't have the same address */
3167 mono_dont_free_domains = TRUE;
3168 mono_using_xdebug = TRUE;
3169 } else if (mini_get_debug_options ()->gdb) {
3170 mono_xdebug_init ((char*)"gdb");
3171 mono_dont_free_domains = TRUE;
3172 mono_using_xdebug = TRUE;
3176 if (mono_use_llvm) {
3177 if (!mono_llvm_load (NULL)) {
3178 mono_use_llvm = FALSE;
3179 fprintf (stderr, "Mono Warning: llvm support could not be loaded.\n");
3186 mono_trampolines_init ();
3188 mono_native_tls_alloc (&mono_jit_tls_id, NULL);
3190 if (default_opt & MONO_OPT_AOT)
3193 mono_debugger_agent_init ();
3195 #ifdef MONO_ARCH_GSHARED_SUPPORTED
3196 mono_set_generic_sharing_supported (TRUE);
3199 #ifndef MONO_CROSS_COMPILE
3200 mono_runtime_install_handlers ();
3202 mono_threads_install_cleanup (mini_thread_cleanup);
3204 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
3205 check_for_pending_exc = FALSE;
3206 mono_threads_install_notify_pending_exc ((MonoThreadNotifyPendingExcFunc)mono_arch_notify_pending_exc);
3209 #define JIT_TRAMPOLINES_WORK
3210 #ifdef JIT_TRAMPOLINES_WORK
3211 mono_install_compile_method (mono_jit_compile_method);
3212 mono_install_free_method (mono_jit_free_method);
3213 mono_install_trampoline (mono_create_jit_trampoline);
3214 mono_install_jump_trampoline (mono_create_jump_trampoline);
3215 #ifndef DISABLE_REMOTING
3216 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
3218 mono_install_delegate_trampoline (mono_create_delegate_trampoline);
3219 mono_install_create_domain_hook (mini_create_jit_domain_info);
3220 mono_install_free_domain_hook (mini_free_jit_domain_info);
3222 #define JIT_INVOKE_WORKS
3223 #ifdef JIT_INVOKE_WORKS
3224 mono_install_runtime_invoke (mono_jit_runtime_invoke);
3226 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
3227 mono_install_get_class_from_name (mono_aot_get_class_from_name);
3228 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
3230 if (debug_options.collect_pagefault_stats)
3231 mono_aot_set_make_unreadable (TRUE);
3233 if (runtime_version)
3234 domain = mono_init_version (filename, runtime_version);
3236 domain = mono_init_from_assembly (filename, filename);
3238 if (mono_aot_only) {
3239 /* This helps catch code allocation requests */
3240 mono_code_manager_set_read_only (domain->code_mp);
3241 mono_marshal_use_aot_wrappers (TRUE);
3246 mono_install_imt_thunk_builder (mono_aot_get_imt_thunk);
3248 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
3251 /*Init arch tls information only after the metadata side is inited to make sure we see dynamic appdomain tls keys*/
3252 mono_arch_finish_init ();
3256 /* This must come after mono_init () in the aot-only case */
3257 mono_exceptions_init ();
3259 /* This should come after mono_init () too */
3263 mono_create_helper_signatures ();
3266 register_jit_stats ();
3268 #define JIT_CALLS_WORK
3269 #ifdef JIT_CALLS_WORK
3270 /* Needs to be called here since register_jit_icall depends on it */
3271 mono_marshal_init ();
3273 mono_arch_register_lowlevel_calls ();
3277 mono_generic_sharing_init ();
3280 #ifdef MONO_ARCH_SIMD_INTRINSICS
3281 mono_simd_intrinsics_init ();
3284 #if MONO_SUPPORT_TASKLETS
3285 mono_tasklets_init ();
3288 if (mono_compile_aot)
3290 * Avoid running managed code when AOT compiling, since the platform
3291 * might only support aot-only execution.
3293 mono_runtime_set_no_exec (TRUE);
3295 #define JIT_RUNTIME_WORKS
3296 #ifdef JIT_RUNTIME_WORKS
3297 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
3298 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
3299 mono_thread_attach (domain);
3302 mono_profiler_runtime_initialized ();
3304 MONO_VES_INIT_END ();
3310 register_icalls (void)
3312 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
3313 ves_icall_get_frame_info);
3314 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
3315 ves_icall_get_trace);
3316 mono_add_internal_call ("System.Exception::get_trace",
3317 ves_icall_System_Exception_get_trace);
3318 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
3319 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
3320 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
3321 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
3322 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
3323 mono_runtime_install_handlers);
3325 #if defined(PLATFORM_ANDROID) || defined(TARGET_ANDROID)
3326 mono_add_internal_call ("System.Diagnostics.Debugger::Mono_UnhandledException_internal",
3327 mono_debugger_agent_unhandled_exception);
3331 * It's important that we pass `TRUE` as the last argument here, as
3332 * it causes the JIT to omit a wrapper for these icalls. If the JIT
3333 * *did* emit a wrapper, we'd be looking at infinite recursion since
3334 * the wrapper would call the icall which would call the wrapper and
3337 register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", "void ptr", TRUE);
3338 register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", "void ptr", TRUE);
3340 register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
3341 register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
3342 register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
3343 register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "ptr ptr", TRUE);
3344 register_icall (mono_jit_set_domain, "mono_jit_set_domain", "void ptr", TRUE);
3345 register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
3347 register_dyn_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
3348 register_dyn_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
3349 register_dyn_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception", "void ptr", TRUE);
3350 register_icall (mono_thread_get_undeniable_exception, "mono_thread_get_undeniable_exception", "object", FALSE);
3351 register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "object", FALSE);
3352 register_icall (mono_thread_force_interruption_checkpoint_noraise, "mono_thread_force_interruption_checkpoint_noraise", "object", FALSE);
3353 register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
3354 #ifndef DISABLE_REMOTING
3355 register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
3356 register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
3359 #if defined(__native_client__) || defined(__native_client_codegen__)
3360 register_icall (mono_nacl_gc, "mono_nacl_gc", "void", TRUE);
3362 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
3363 register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, "mono_llmult", TRUE);
3364 register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, "mono_lldiv", FALSE);
3365 register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, "mono_lldiv_un", FALSE);
3366 register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, "mono_llrem", FALSE);
3367 register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, "mono_llrem_un", FALSE);
3369 #if !defined(MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS) || defined(MONO_ARCH_EMULATE_LONG_MUL_OVF_OPTS)
3370 register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, "mono_llmult_ovf_un", FALSE);
3371 register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, "mono_llmult_ovf", FALSE);
3374 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
3375 register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, "mono_lshl", TRUE);
3376 register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, "mono_lshr", TRUE);
3377 register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, "mono_lshr_un", TRUE);
3380 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
3381 register_opcode_emulation (OP_IDIV, "__emul_op_idiv", "int32 int32 int32", mono_idiv, "mono_idiv", FALSE);
3382 register_opcode_emulation (OP_IDIV_UN, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un, "mono_idiv_un", FALSE);
3383 register_opcode_emulation (OP_IREM, "__emul_op_irem", "int32 int32 int32", mono_irem, "mono_irem", FALSE);
3384 register_opcode_emulation (OP_IREM_UN, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un, "mono_irem_un", FALSE);
3387 #ifdef MONO_ARCH_EMULATE_MUL_DIV
3388 register_opcode_emulation (OP_IMUL, "__emul_op_imul", "int32 int32 int32", mono_imul, "mono_imul", TRUE);
3391 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
3392 register_opcode_emulation (OP_IMUL_OVF, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf, "mono_imul_ovf", FALSE);
3393 register_opcode_emulation (OP_IMUL_OVF_UN, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, "mono_imul_ovf_un", FALSE);
3396 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT_FALLBACK)
3397 if (ARCH_EMULATE_MUL_DIV || mono_arch_is_soft_float ()) {
3398 register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, "mono_fdiv", FALSE);
3402 register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, "mono_fconv_u8", FALSE);
3403 register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, "mono_fconv_u4", FALSE);
3404 register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, "mono_fconv_ovf_i8", FALSE);
3405 register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, "mono_fconv_ovf_u8", FALSE);
3406 register_opcode_emulation (OP_RCONV_TO_OVF_I8, "__emul_rconv_to_ovf_i8", "long float", mono_rconv_ovf_i8, "mono_rconv_ovf_i8", FALSE);
3407 register_opcode_emulation (OP_RCONV_TO_OVF_U8, "__emul_rconv_to_ovf_u8", "ulong float", mono_rconv_ovf_u8, "mono_rconv_ovf_u8", FALSE);
3409 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
3410 register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, "mono_fconv_i8", FALSE);
3411 register_opcode_emulation (OP_RCONV_TO_I8, "__emul_rconv_to_i8", "long float", mono_rconv_i8, "mono_rconv_i8", FALSE);
3413 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
3414 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);
3416 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
3417 register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, "mono_lconv_to_r8", FALSE);
3419 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
3420 register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, "mono_lconv_to_r4", FALSE);
3422 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
3423 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);
3425 #ifdef MONO_ARCH_EMULATE_FREM
3426 #if defined(__default_codegen__)
3427 register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, "fmod", FALSE);
3428 #elif defined(__native_client_codegen__)
3429 register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", mono_fmod, "mono_fmod", FALSE);
3433 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
3434 if (mono_arch_is_soft_float ()) {
3435 register_opcode_emulation (OP_FSUB, "__emul_fsub", "double double double", mono_fsub, "mono_fsub", FALSE);
3436 register_opcode_emulation (OP_FADD, "__emul_fadd", "double double double", mono_fadd, "mono_fadd", FALSE);
3437 register_opcode_emulation (OP_FMUL, "__emul_fmul", "double double double", mono_fmul, "mono_fmul", FALSE);
3438 register_opcode_emulation (OP_FNEG, "__emul_fneg", "double double", mono_fneg, "mono_fneg", FALSE);
3439 register_opcode_emulation (OP_ICONV_TO_R8, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8, "mono_conv_to_r8", FALSE);
3440 register_opcode_emulation (OP_ICONV_TO_R4, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4, "mono_conv_to_r4", FALSE);
3441 register_opcode_emulation (OP_FCONV_TO_R4, "__emul_fconv_to_r4", "double double", mono_fconv_r4, "mono_fconv_r4", FALSE);
3442 register_opcode_emulation (OP_FCONV_TO_I1, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1, "mono_fconv_i1", FALSE);
3443 register_opcode_emulation (OP_FCONV_TO_I2, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2, "mono_fconv_i2", FALSE);
3444 register_opcode_emulation (OP_FCONV_TO_I4, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4, "mono_fconv_i4", FALSE);
3445 register_opcode_emulation (OP_FCONV_TO_U1, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1, "mono_fconv_u1", FALSE);
3446 register_opcode_emulation (OP_FCONV_TO_U2, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2, "mono_fconv_u2", FALSE);
3448 #if SIZEOF_VOID_P == 4
3449 register_opcode_emulation (OP_FCONV_TO_I, "__emul_fconv_to_i", "int32 double", mono_fconv_i4, "mono_fconv_i4", FALSE);
3452 register_opcode_emulation (OP_FBEQ, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq, "mono_fcmp_eq", FALSE);
3453 register_opcode_emulation (OP_FBLT, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt, "mono_fcmp_lt", FALSE);
3454 register_opcode_emulation (OP_FBGT, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt, "mono_fcmp_gt", FALSE);
3455 register_opcode_emulation (OP_FBLE, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le, "mono_fcmp_le", FALSE);
3456 register_opcode_emulation (OP_FBGE, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge, "mono_fcmp_ge", FALSE);
3457 register_opcode_emulation (OP_FBNE_UN, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un, "mono_fcmp_ne_un", FALSE);
3458 register_opcode_emulation (OP_FBLT_UN, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un, "mono_fcmp_lt_un", FALSE);
3459 register_opcode_emulation (OP_FBGT_UN, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un, "mono_fcmp_gt_un", FALSE);
3460 register_opcode_emulation (OP_FBLE_UN, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un, "mono_fcmp_le_un", FALSE);
3461 register_opcode_emulation (OP_FBGE_UN, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un, "mono_fcmp_ge_un", FALSE);
3463 register_opcode_emulation (OP_FCEQ, "__emul_fcmp_ceq", "uint32 double double", mono_fceq, "mono_fceq", FALSE);
3464 register_opcode_emulation (OP_FCGT, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt, "mono_fcgt", FALSE);
3465 register_opcode_emulation (OP_FCGT_UN, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un, "mono_fcgt_un", FALSE);
3466 register_opcode_emulation (OP_FCLT, "__emul_fcmp_clt", "uint32 double double", mono_fclt, "mono_fclt", FALSE);
3467 register_opcode_emulation (OP_FCLT_UN, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un, "mono_fclt_un", FALSE);
3469 register_icall (mono_fload_r4, "mono_fload_r4", "double ptr", FALSE);
3470 register_icall (mono_fstore_r4, "mono_fstore_r4", "void double ptr", FALSE);
3471 register_icall (mono_fload_r4_arg, "mono_fload_r4_arg", "uint32 double", FALSE);
3472 register_icall (mono_isfinite, "mono_isfinite", "uint32 double", FALSE);
3476 #ifdef COMPRESSED_INTERFACE_BITMAP
3477 register_icall (mono_class_interface_match, "mono_class_interface_match", "uint32 ptr int32", TRUE);
3480 #if SIZEOF_REGISTER == 4
3481 register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, "mono_fconv_u4", TRUE);
3483 register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "ulong double", mono_fconv_u8, "mono_fconv_u8", TRUE);
3486 /* other jit icalls */
3487 register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
3488 register_icall (mono_class_static_field_address , "mono_class_static_field_address",
3489 "ptr ptr ptr", FALSE);
3490 register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
3491 register_icall (mono_ldtoken_wrapper_generic_shared, "mono_ldtoken_wrapper_generic_shared",
3492 "ptr ptr ptr ptr", FALSE);
3493 register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
3494 register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
3495 register_icall (mono_helper_stelem_ref_check, "mono_helper_stelem_ref_check", "void object object", FALSE);
3496 register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
3497 register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
3498 register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
3499 register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
3500 register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
3501 register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
3502 register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
3503 register_icall (mono_ldvirtfn_gshared, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE);
3504 register_icall (mono_helper_compile_generic_method, "mono_helper_compile_generic_method", "ptr object ptr ptr", FALSE);
3505 register_icall (mono_helper_ldstr, "mono_helper_ldstr", "object ptr int", FALSE);
3506 register_icall (mono_helper_ldstr_mscorlib, "mono_helper_ldstr_mscorlib", "object int", FALSE);
3507 register_icall (mono_helper_newobj_mscorlib, "mono_helper_newobj_mscorlib", "object int", FALSE);
3508 register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
3509 register_icall (mono_object_castclass_unbox, "mono_object_castclass_unbox", "object object ptr", FALSE);
3510 register_icall (mono_break, "mono_break", NULL, TRUE);
3511 register_icall (mono_create_corlib_exception_0, "mono_create_corlib_exception_0", "object int", TRUE);
3512 register_icall (mono_create_corlib_exception_1, "mono_create_corlib_exception_1", "object int object", TRUE);
3513 register_icall (mono_create_corlib_exception_2, "mono_create_corlib_exception_2", "object int object object", TRUE);
3514 register_icall (mono_array_new_1, "mono_array_new_1", "object ptr int", FALSE);
3515 register_icall (mono_array_new_2, "mono_array_new_2", "object ptr int int", FALSE);
3516 register_icall (mono_array_new_3, "mono_array_new_3", "object ptr int int int", FALSE);
3517 register_icall (mono_array_new_4, "mono_array_new_4", "object ptr int int int int", FALSE);
3518 register_icall (mono_get_native_calli_wrapper, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE);
3519 register_icall (mono_resume_unwind, "mono_resume_unwind", "void", TRUE);
3520 register_icall (mono_gsharedvt_constrained_call, "mono_gsharedvt_constrained_call", "object ptr ptr ptr ptr ptr", FALSE);
3521 register_icall (mono_gsharedvt_value_copy, "mono_gsharedvt_value_copy", "void ptr ptr ptr", TRUE);
3523 register_icall (mono_gc_wbarrier_value_copy_bitmap, "mono_gc_wbarrier_value_copy_bitmap", "void ptr ptr int int", FALSE);
3525 register_icall (mono_object_castclass_with_cache, "mono_object_castclass_with_cache", "object object ptr ptr", FALSE);
3526 register_icall (mono_object_isinst_with_cache, "mono_object_isinst_with_cache", "object object ptr ptr", FALSE);
3528 register_icall (mono_debugger_agent_user_break, "mono_debugger_agent_user_break", "void", FALSE);
3531 register_icall (pthread_getspecific, "pthread_getspecific", "ptr ptr", TRUE);
3535 MonoJitStats mono_jit_stats = {0};
3538 print_jit_stats (void)
3540 if (mono_jit_stats.enabled) {
3541 g_print ("Mono Jit statistics\n");
3542 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
3543 mono_jit_stats.max_ratio_method);
3544 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats.biggest_method_size,
3545 mono_jit_stats.biggest_method);
3547 g_print ("Delegates created: %ld\n", mono_stats.delegate_creations);
3548 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
3549 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
3550 g_print ("Generic vtables: %ld\n", mono_stats.generic_vtable_count);
3551 g_print ("Methods: %ld\n", mono_stats.method_count);
3552 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
3553 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
3554 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults.corlib->mempool));
3556 g_print ("\nInitialized classes: %ld\n", mono_stats.generic_class_count);
3557 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
3558 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats.generic_virtual_invocations);
3560 g_print ("Sharable generic methods: %ld\n", mono_stats.generics_sharable_methods);
3561 g_print ("Unsharable generic methods: %ld\n", mono_stats.generics_unsharable_methods);
3562 g_print ("Shared generic methods: %ld\n", mono_stats.generics_shared_methods);
3563 g_print ("Shared vtype generic methods: %ld\n", mono_stats.gsharedvt_methods);
3565 g_print ("IMT tables size: %ld\n", mono_stats.imt_tables_size);
3566 g_print ("IMT number of tables: %ld\n", mono_stats.imt_number_of_tables);
3567 g_print ("IMT number of methods: %ld\n", mono_stats.imt_number_of_methods);
3568 g_print ("IMT used slots: %ld\n", mono_stats.imt_used_slots);
3569 g_print ("IMT colliding slots: %ld\n", mono_stats.imt_slots_with_collisions);
3570 g_print ("IMT max collisions: %ld\n", mono_stats.imt_max_collisions_in_slot);
3571 g_print ("IMT methods at max col: %ld\n", mono_stats.imt_method_count_when_max_collisions);
3572 g_print ("IMT thunks size: %ld\n", mono_stats.imt_thunks_size);
3574 g_print ("JIT info table inserts: %ld\n", mono_stats.jit_info_table_insert_count);
3575 g_print ("JIT info table removes: %ld\n", mono_stats.jit_info_table_remove_count);
3576 g_print ("JIT info table lookups: %ld\n", mono_stats.jit_info_table_lookup_count);
3578 if (mono_security_cas_enabled ()) {
3579 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
3580 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
3581 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
3582 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
3583 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
3584 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
3587 g_free (mono_jit_stats.max_ratio_method);
3588 mono_jit_stats.max_ratio_method = NULL;
3589 g_free (mono_jit_stats.biggest_method);
3590 mono_jit_stats.biggest_method = NULL;
3595 mini_cleanup (MonoDomain *domain)
3597 mono_runtime_shutdown_stat_profiler ();
3600 cominterop_release_all_rcws ();
3603 #ifndef MONO_CROSS_COMPILE
3605 * mono_domain_finalize () needs to be called early since it needs the
3606 * execution engine still fully working (it may invoke managed finalizers).
3608 mono_domain_finalize (domain, 2000);
3611 /* This accesses metadata so needs to be called before runtime shutdown */
3614 mono_profiler_shutdown ();
3616 #ifndef MONO_CROSS_COMPILE
3617 mono_runtime_cleanup (domain);
3620 free_jit_tls_data (mono_native_tls_get_value (mono_jit_tls_id));
3622 mono_icall_cleanup ();
3624 mono_runtime_cleanup_handlers ();
3626 #ifndef MONO_CROSS_COMPILE
3627 mono_domain_free (domain, TRUE);
3632 mono_llvm_cleanup ();
3635 mono_aot_cleanup ();
3637 mono_trampolines_cleanup ();
3639 mono_unwind_cleanup ();
3641 mono_code_manager_destroy (global_codeman);
3642 g_free (vtable_trampolines);
3644 mini_jit_cleanup ();
3646 mono_tramp_info_cleanup ();
3648 mono_arch_cleanup ();
3650 mono_generic_sharing_cleanup ();
3654 mono_trace_cleanup ();
3656 mono_counters_dump (MONO_COUNTER_SECTION_MASK | MONO_COUNTER_MONOTONIC, stdout);
3658 if (mono_inject_async_exc_method)
3659 mono_method_desc_free (mono_inject_async_exc_method);
3661 mono_native_tls_free (mono_jit_tls_id);
3663 mono_mutex_destroy (&jit_mutex);
3665 mono_mutex_destroy (&mono_delegate_section);
3667 mono_code_manager_cleanup ();
3669 #ifdef USE_JUMP_TABLES
3670 mono_jumptable_cleanup ();
3675 mono_set_defaults (int verbose_level, guint32 opts)
3677 mini_verbose = verbose_level;
3678 mono_set_optimizations (opts);
3682 mono_disable_optimizations (guint32 opts)
3684 default_opt &= ~opts;
3688 mono_set_optimizations (guint32 opts)
3691 default_opt_set = TRUE;
3692 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
3693 mono_set_generic_sharing_vt_supported (mono_aot_only || ((default_opt & MONO_OPT_GSHAREDVT) != 0));
3698 mono_set_verbose_level (guint32 level)
3700 mini_verbose = level;
3704 * mono_get_runtime_build_info:
3706 * Return the runtime version + build date in string format.
3707 * The returned string is owned by the caller. The returned string
3708 * format is "VERSION (FULL_VERSION BUILD_DATE)" and build date is optional.
3711 mono_get_runtime_build_info (void)
3713 if (mono_build_date)
3714 return g_strdup_printf ("%s (%s %s)", VERSION, FULL_VERSION, mono_build_date);
3716 return g_strdup_printf ("%s (%s)", VERSION, FULL_VERSION);
3720 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
3722 GHashTable *assemblies = (GHashTable*)user_data;
3723 MonoImage *image = mono_assembly_get_image (ass);
3724 MonoMethod *method, *invoke;
3727 if (g_hash_table_lookup (assemblies, ass))
3730 g_hash_table_insert (assemblies, ass, ass);
3732 if (mini_verbose > 0)
3733 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
3735 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
3736 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
3737 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3739 if (method->is_generic || method->klass->generic_container)
3743 if (mini_verbose > 1) {
3744 char * desc = mono_method_full_name (method, TRUE);
3745 g_print ("Compiling %d %s\n", count, desc);
3748 mono_compile_method (method);
3749 if (strcmp (method->name, "Finalize") == 0) {
3750 invoke = mono_marshal_get_runtime_invoke (method, FALSE);
3751 mono_compile_method (invoke);
3753 #ifndef DISABLE_REMOTING
3754 if (mono_class_is_marshalbyref (method->klass) && mono_method_signature (method)->hasthis) {
3755 invoke = mono_marshal_get_remoting_invoke_with_check (method);
3756 mono_compile_method (invoke);
3761 /* Load and precompile referenced assemblies as well */
3762 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
3763 mono_assembly_load_reference (image, i);
3764 if (image->references [i])
3765 mono_precompile_assembly (image->references [i], assemblies);
3769 void mono_precompile_assemblies ()
3771 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
3773 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
3775 g_hash_table_destroy (assemblies);
3778 #ifdef USE_JUMP_TABLES
3779 #define DEFAULT_JUMPTABLE_CHUNK_ELEMENTS 128
3781 typedef struct MonoJumpTableChunk {
3784 struct MonoJumpTableChunk *previous;
3785 /* gpointer entries[total]; */
3786 } MonoJumpTableChunk;
3788 static MonoJumpTableChunk* g_jumptable;
3789 #define mono_jumptable_lock() mono_mutex_lock (&jumptable_mutex)
3790 #define mono_jumptable_unlock() mono_mutex_unlock (&jumptable_mutex)
3791 static mono_mutex_t jumptable_mutex;
3793 static MonoJumpTableChunk*
3794 mono_create_jumptable_chunk (guint32 max_entries)
3796 guint32 size = sizeof (MonoJumpTableChunk) + max_entries * sizeof(gpointer);
3797 MonoJumpTableChunk *chunk = (MonoJumpTableChunk*) g_new0 (guchar, size);
3798 chunk->total = max_entries;
3803 mono_jumptable_init (void)
3805 if (g_jumptable == NULL) {
3806 mono_mutex_init_recursive (&jumptable_mutex);
3807 g_jumptable = mono_create_jumptable_chunk (DEFAULT_JUMPTABLE_CHUNK_ELEMENTS);
3812 mono_jumptable_add_entry (void)
3814 return mono_jumptable_add_entries (1);
3818 mono_jumptable_add_entries (guint32 entries)
3823 mono_jumptable_init ();
3824 mono_jumptable_lock ();
3825 index = g_jumptable->active;
3826 if (index + entries >= g_jumptable->total) {
3828 * Grow jumptable, by adding one more chunk.
3829 * We cannot realloc jumptable, as there could be pointers
3830 * to existing jump table entries in the code, so instead
3831 * we just add one more chunk.
3833 guint32 max_entries = entries;
3834 MonoJumpTableChunk *new_chunk;
3836 if (max_entries < DEFAULT_JUMPTABLE_CHUNK_ELEMENTS)
3837 max_entries = DEFAULT_JUMPTABLE_CHUNK_ELEMENTS;
3838 new_chunk = mono_create_jumptable_chunk (max_entries);
3839 /* Link old jumptable, so that we could free it up later. */
3840 new_chunk->previous = g_jumptable;
3841 g_jumptable = new_chunk;
3844 g_jumptable->active = index + entries;
3845 result = (gpointer*)((guchar*)g_jumptable + sizeof(MonoJumpTableChunk)) + index;
3846 mono_jumptable_unlock();
3852 mono_jumptable_cleanup (void)
3855 MonoJumpTableChunk *current = g_jumptable, *prev;
3856 while (current != NULL) {
3857 prev = current->previous;
3862 mono_mutex_destroy (&jumptable_mutex);
3867 mono_jumptable_get_entry (guint8 *code_ptr)
3869 return mono_arch_jumptable_entry_from_code (code_ptr);