3 * Runtime code for the JIT
6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
9 * Copyright 2002-2003 Ximian, Inc.
10 * Copyright 2003-2010 Novell, Inc.
11 * Copyright 2011-2015 Xamarin, Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
23 #ifdef HAVE_SYS_TIME_H
30 #include <mono/utils/memcheck.h>
32 #include <mono/metadata/assembly.h>
33 #include <mono/metadata/loader.h>
34 #include <mono/metadata/tabledefs.h>
35 #include <mono/metadata/class.h>
36 #include <mono/metadata/object.h>
37 #include <mono/metadata/tokentype.h>
38 #include <mono/metadata/tabledefs.h>
39 #include <mono/metadata/threads.h>
40 #include <mono/metadata/appdomain.h>
41 #include <mono/metadata/debug-helpers.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-internals.h>
48 #include <mono/metadata/threads-types.h>
49 #include <mono/metadata/mempool-internals.h>
50 #include <mono/metadata/attach.h>
51 #include <mono/metadata/runtime.h>
52 #include <mono/metadata/reflection-internals.h>
53 #include <mono/metadata/monitor.h>
54 #include <mono/utils/mono-math.h>
55 #include <mono/utils/mono-compiler.h>
56 #include <mono/utils/mono-counters.h>
57 #include <mono/utils/mono-error-internals.h>
58 #include <mono/utils/mono-logger-internals.h>
59 #include <mono/utils/mono-mmap.h>
60 #include <mono/utils/mono-path.h>
61 #include <mono/utils/mono-tls.h>
62 #include <mono/utils/mono-hwcap.h>
63 #include <mono/utils/dtrace.h>
64 #include <mono/utils/mono-signal-handler.h>
65 #include <mono/utils/mono-threads.h>
66 #include <mono/utils/mono-threads-coop.h>
67 #include <mono/utils/checked-build.h>
68 #include <mono/metadata/w32handle.h>
69 #include <mono/metadata/threadpool.h>
72 #include "seq-points.h"
79 #include "jit-icalls.h"
82 #include "mini-llvm.h"
83 #include "debugger-agent.h"
86 #ifdef MONO_ARCH_LLVM_SUPPORTED
88 #include "mini-llvm-cpp.h"
93 #ifdef ENABLE_INTERPRETER
94 #include "interp/interp.h"
97 static guint32 default_opt
= 0;
98 static gboolean default_opt_set
= FALSE
;
100 gboolean mono_compile_aot
= FALSE
;
101 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
102 gboolean mono_aot_only
= FALSE
;
103 /* Same as mono_aot_only, but only LLVM compiled code is used, no trampolines */
104 gboolean mono_llvm_only
= FALSE
;
105 MonoAotMode mono_aot_mode
= MONO_AOT_MODE_NONE
;
107 const char *mono_build_date
;
108 gboolean mono_do_signal_chaining
;
109 gboolean mono_do_crash_chaining
;
110 int mini_verbose
= 0;
113 * This flag controls whenever the runtime uses LLVM for JIT compilation, and whenever
114 * it can load AOT code compiled by LLVM.
116 gboolean mono_use_llvm
= FALSE
;
118 gboolean mono_use_interpreter
= FALSE
;
120 #define mono_jit_lock() mono_os_mutex_lock (&jit_mutex)
121 #define mono_jit_unlock() mono_os_mutex_unlock (&jit_mutex)
122 static mono_mutex_t jit_mutex
;
124 static MonoCodeManager
*global_codeman
;
126 MonoDebugOptions debug_options
;
128 #ifdef VALGRIND_JIT_REGISTER_MAP
129 int valgrind_register
;
131 GList
* mono_aot_paths
;
133 static gboolean mini_enable_profiler
= FALSE
;
134 static char* mini_profiler_options
= NULL
;
136 static GSList
*tramp_infos
;
138 static void register_icalls (void);
140 static gboolean
mini_profiler_enabled (void) { return mini_enable_profiler
; }
141 static const char* mini_profiler_get_options (void) { return mini_profiler_options
; }
144 mono_running_on_valgrind (void)
147 if (RUNNING_ON_VALGRIND
){
148 #ifdef VALGRIND_JIT_REGISTER_MAP
149 valgrind_register
= TRUE
;
163 find_tramp (gpointer key
, gpointer value
, gpointer user_data
)
165 FindTrampUserData
*ud
= (FindTrampUserData
*)user_data
;
168 ud
->method
= (MonoMethod
*)key
;
172 G_GNUC_UNUSED
static char*
173 get_method_from_ip (void *ip
)
179 MonoDomain
*domain
= mono_domain_get ();
180 MonoDebugSourceLocation
*location
;
181 FindTrampUserData user_data
;
184 domain
= mono_get_root_domain ();
186 ji
= mono_jit_info_table_find_internal (domain
, (char *)ip
, TRUE
, TRUE
);
189 user_data
.method
= NULL
;
190 mono_domain_lock (domain
);
191 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
192 mono_domain_unlock (domain
);
193 if (user_data
.method
) {
194 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
195 res
= g_strdup_printf ("<%p - JIT trampoline for %s>", ip
, mname
);
201 } else if (ji
->is_trampoline
) {
202 res
= g_strdup_printf ("<%p - %s trampoline>", ip
, ((MonoTrampInfo
*)ji
->d
.tramp_info
)->name
);
206 method
= jinfo_get_method (ji
);
207 method_name
= mono_method_full_name (method
, TRUE
);
208 /* FIXME: unused ? */
209 location
= mono_debug_lookup_source_location (method
, (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), domain
);
211 res
= g_strdup_printf (" %s {%p} + 0x%x (%p %p) [%p - %s]", method_name
, method
, (int)((char*)ip
- (char*)ji
->code_start
), ji
->code_start
, (char*)ji
->code_start
+ ji
->code_size
, domain
, domain
->friendly_name
);
213 mono_debug_free_source_location (location
);
214 g_free (method_name
);
221 * \param ip an instruction pointer address
223 * This method is used from a debugger to get the name of the
224 * method at address \p ip. This routine is typically invoked from
225 * a debugger like this:
227 * (gdb) print mono_pmip ($pc)
229 * \returns the name of the method at address \p ip.
234 return get_method_from_ip (ip
);
238 * mono_print_method_from_ip:
239 * \param ip an instruction pointer address
241 * This method is used from a debugger to get the name of the
242 * method at address \p ip.
244 * This prints the name of the method at address \p ip in the standard
245 * output. Unlike \c mono_pmip which returns a string, this routine
246 * prints the value on the standard output.
249 mono_print_method_from_ip (void *ip
)
253 MonoDebugSourceLocation
*source
;
254 MonoDomain
*domain
= mono_domain_get ();
255 MonoDomain
*target_domain
= mono_domain_get ();
256 FindTrampUserData user_data
;
257 MonoGenericSharingContext
*gsctx
;
258 const char *shared_type
;
261 domain
= mono_get_root_domain ();
262 ji
= mini_jit_info_table_find_ext (domain
, (char *)ip
, TRUE
, &target_domain
);
263 if (ji
&& ji
->is_trampoline
) {
264 MonoTrampInfo
*tinfo
= (MonoTrampInfo
*)ji
->d
.tramp_info
;
266 printf ("IP %p is at offset 0x%x of trampoline '%s'.\n", ip
, (int)((guint8
*)ip
- tinfo
->code
), tinfo
->name
);
272 user_data
.method
= NULL
;
273 mono_domain_lock (domain
);
274 g_hash_table_foreach (domain_jit_info (domain
)->jit_trampoline_hash
, find_tramp
, &user_data
);
275 mono_domain_unlock (domain
);
277 if (user_data
.method
) {
278 char *mname
= mono_method_full_name (user_data
.method
, TRUE
);
279 printf ("IP %p is a JIT trampoline for %s\n", ip
, mname
);
284 g_print ("No method at %p\n", ip
);
288 method
= mono_method_full_name (jinfo_get_method (ji
), TRUE
);
289 source
= mono_debug_lookup_source_location (jinfo_get_method (ji
), (guint32
)((guint8
*)ip
- (guint8
*)ji
->code_start
), target_domain
);
291 gsctx
= mono_jit_info_get_generic_sharing_context (ji
);
294 if (gsctx
->is_gsharedvt
)
295 shared_type
= "gsharedvt ";
297 shared_type
= "gshared ";
300 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
);
303 g_print ("%s:%d\n", source
->source_file
, source
->row
);
306 mono_debug_free_source_location (source
);
311 * mono_method_same_domain:
313 * Determine whenever two compiled methods are in the same domain, thus
314 * the address of the callee can be embedded in the caller.
316 gboolean
mono_method_same_domain (MonoJitInfo
*caller
, MonoJitInfo
*callee
)
320 if (!caller
|| caller
->is_trampoline
|| !callee
|| callee
->is_trampoline
)
324 * If the call was made from domain-neutral to domain-specific
325 * code, we can't patch the call site.
327 if (caller
->domain_neutral
&& !callee
->domain_neutral
)
330 cmethod
= jinfo_get_method (caller
);
331 if ((cmethod
->klass
== mono_defaults
.appdomain_class
) &&
332 (strstr (cmethod
->name
, "InvokeInDomain"))) {
333 /* The InvokeInDomain methods change the current appdomain */
341 * mono_global_codeman_reserve:
343 * Allocate code memory from the global code manager.
345 void *mono_global_codeman_reserve (int size
)
350 g_error ("Attempting to allocate from the global code manager while running in aot-only mode.\n");
352 if (!global_codeman
) {
353 /* This can happen during startup */
354 global_codeman
= mono_code_manager_new ();
355 return mono_code_manager_reserve (global_codeman
, size
);
359 ptr
= mono_code_manager_reserve (global_codeman
, size
);
365 /* The callback shouldn't take any locks */
367 mono_global_codeman_foreach (MonoCodeManagerFunc func
, void *user_data
)
370 mono_code_manager_foreach (global_codeman
, func
, user_data
);
374 #if defined(__native_client_codegen__) && defined(__native_client__)
378 #ifdef __native_client_gc__
379 __nacl_suspend_thread_if_needed();
382 #endif /* __native_client__ */
385 * mono_create_unwind_op:
387 * Create an unwind op with the given parameters.
390 mono_create_unwind_op (int when
, int tag
, int reg
, int val
)
392 MonoUnwindOp
*op
= g_new0 (MonoUnwindOp
, 1);
403 mono_jump_info_token_new2 (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
405 MonoJumpInfoToken
*res
= (MonoJumpInfoToken
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoToken
));
408 res
->has_context
= context
!= NULL
;
410 memcpy (&res
->context
, context
, sizeof (MonoGenericContext
));
416 mono_jump_info_token_new (MonoMemPool
*mp
, MonoImage
*image
, guint32 token
)
418 return mono_jump_info_token_new2 (mp
, image
, token
, NULL
);
422 * mono_tramp_info_create:
424 * Create a MonoTrampInfo structure from the arguments. This function assumes ownership
425 * of JI, and UNWIND_OPS.
428 mono_tramp_info_create (const char *name
, guint8
*code
, guint32 code_size
, MonoJumpInfo
*ji
, GSList
*unwind_ops
)
430 MonoTrampInfo
*info
= g_new0 (MonoTrampInfo
, 1);
432 info
->name
= g_strdup ((char*)name
);
434 info
->code_size
= code_size
;
436 info
->unwind_ops
= unwind_ops
;
442 mono_tramp_info_free (MonoTrampInfo
*info
)
447 mono_free_unwind_info (info
->unwind_ops
);
452 register_trampoline_jit_info (MonoDomain
*domain
, MonoTrampInfo
*info
)
456 ji
= (MonoJitInfo
*)mono_domain_alloc0 (domain
, mono_jit_info_size ((MonoJitInfoFlags
)0, 0, 0));
457 mono_jit_info_init (ji
, NULL
, info
->code
, info
->code_size
, (MonoJitInfoFlags
)0, 0, 0);
458 ji
->d
.tramp_info
= info
;
459 ji
->is_trampoline
= TRUE
;
461 ji
->unwind_info
= mono_cache_unwind_info (info
->uw_info
, info
->uw_info_len
);
463 mono_jit_info_table_add (domain
, ji
);
467 * mono_tramp_info_register:
469 * Remember INFO for use by xdebug, mono_print_method_from_ip (), jit maps, etc.
474 mono_tramp_info_register_internal (MonoTrampInfo
*info
, MonoDomain
*domain
, gboolean aot
)
482 domain
= mono_get_root_domain ();
484 copy
= g_new0 (MonoTrampInfo
, 1);
485 copy
->code
= info
->code
;
486 copy
->code_size
= info
->code_size
;
487 copy
->name
= g_strdup (info
->name
);
489 if (info
->unwind_ops
) {
490 copy
->uw_info
= mono_unwind_ops_encode (info
->unwind_ops
, ©
->uw_info_len
);
492 /* Trampolines from aot have the unwind ops already encoded */
493 copy
->uw_info
= info
->uw_info
;
494 copy
->uw_info_len
= info
->uw_info_len
;
498 tramp_infos
= g_slist_prepend (tramp_infos
, copy
);
501 mono_save_trampoline_xdebug_info (info
);
502 mono_lldb_save_trampoline_info (info
);
504 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
506 mono_arch_unwindinfo_install_tramp_unwind_info (info
->unwind_ops
, info
->code
, info
->code_size
);
509 /* Only register trampolines that have unwind infos */
510 if (mono_get_root_domain () && copy
->uw_info
)
511 register_trampoline_jit_info (domain
, copy
);
513 if (mono_jit_map_is_enabled ())
514 mono_emit_jit_tramp (info
->code
, info
->code_size
, info
->name
);
516 mono_tramp_info_free (info
);
520 mono_tramp_info_register (MonoTrampInfo
*info
, MonoDomain
*domain
)
522 mono_tramp_info_register_internal (info
, domain
, FALSE
);
526 mono_aot_tramp_info_register (MonoTrampInfo
*info
, MonoDomain
*domain
)
528 mono_tramp_info_register_internal (info
, domain
, TRUE
);
532 mono_tramp_info_cleanup (void)
536 for (l
= tramp_infos
; l
; l
= l
->next
) {
537 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
539 mono_tramp_info_free (info
);
541 g_slist_free (tramp_infos
);
544 /* Register trampolines created before the root domain was created in the jit info tables */
546 register_trampolines (MonoDomain
*domain
)
550 for (l
= tramp_infos
; l
; l
= l
->next
) {
551 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
553 register_trampoline_jit_info (domain
, info
);
557 G_GNUC_UNUSED
static void
563 * Runtime debugging tool, use if (debug_count ()) <x> else <y> to do <x> the first COUNT times, then do <y> afterwards.
564 * Set a breakpoint in break_count () to break the last time <x> is done.
566 G_GNUC_UNUSED gboolean
567 mono_debug_count (void)
569 static int count
= 0;
570 static gboolean inited
;
571 static const char *value
;
576 value
= g_getenv ("COUNT");
583 if (count
== atoi (value
))
586 if (count
> atoi (value
))
593 mono_icall_get_wrapper_full (MonoJitICallInfo
* callinfo
, gboolean do_compile
)
598 gconstpointer trampoline
;
599 MonoDomain
*domain
= mono_get_root_domain ();
600 gboolean check_exc
= TRUE
;
602 if (callinfo
->wrapper
)
603 return callinfo
->wrapper
;
605 if (callinfo
->trampoline
)
606 return callinfo
->trampoline
;
608 if (!strcmp (callinfo
->name
, "mono_thread_interruption_checkpoint"))
609 /* This icall is used to check for exceptions, so don't check in the wrapper */
612 name
= g_strdup_printf ("__icall_wrapper_%s", callinfo
->name
);
613 wrapper
= mono_marshal_get_icall_wrapper (callinfo
->sig
, name
, callinfo
->func
, check_exc
);
617 trampoline
= mono_compile_method_checked (wrapper
, &error
);
618 mono_error_assert_ok (&error
);
621 trampoline
= mono_create_jit_trampoline (domain
, wrapper
, &error
);
622 mono_error_assert_ok (&error
);
623 trampoline
= mono_create_ftnptr (domain
, (gpointer
)trampoline
);
627 if (!callinfo
->trampoline
) {
628 mono_register_jit_icall_wrapper (callinfo
, trampoline
);
629 callinfo
->trampoline
= trampoline
;
631 mono_loader_unlock ();
633 return callinfo
->trampoline
;
637 mono_icall_get_wrapper (MonoJitICallInfo
* callinfo
)
639 return mono_icall_get_wrapper_full (callinfo
, FALSE
);
642 static MonoJitDynamicMethodInfo
*
643 mono_dynamic_code_hash_lookup (MonoDomain
*domain
, MonoMethod
*method
)
645 MonoJitDynamicMethodInfo
*res
;
647 if (domain_jit_info (domain
)->dynamic_code_hash
)
648 res
= (MonoJitDynamicMethodInfo
*)g_hash_table_lookup (domain_jit_info (domain
)->dynamic_code_hash
, method
);
655 register_opcode_emulation (int opcode
, const char *name
, const char *sigstr
, gpointer func
, const char *symbol
, gboolean no_throw
)
658 mini_register_opcode_emulation (opcode
, name
, sigstr
, func
, symbol
, no_throw
);
660 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
662 g_assert (!sig
->hasthis
);
663 g_assert (sig
->param_count
< 3);
665 /* Opcode emulation functions are assumed to don't call mono_raise_exception () */
666 mono_register_jit_icall_full (func
, name
, sig
, no_throw
, TRUE
, symbol
);
671 * For JIT icalls implemented in C.
672 * NAME should be the same as the name of the C function whose address is FUNC.
673 * If @avoid_wrapper is TRUE, no wrapper is generated. This is for perf critical icalls which
674 * can't throw exceptions.
677 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean avoid_wrapper
)
679 MonoMethodSignature
*sig
;
682 sig
= mono_create_icall_signature (sigstr
);
686 mono_register_jit_icall_full (func
, name
, sig
, avoid_wrapper
, FALSE
, avoid_wrapper
? name
: NULL
);
690 register_icall_no_wrapper (gpointer func
, const char *name
, const char *sigstr
)
692 MonoMethodSignature
*sig
;
695 sig
= mono_create_icall_signature (sigstr
);
699 mono_register_jit_icall_full (func
, name
, sig
, TRUE
, FALSE
, name
);
703 register_icall_with_wrapper (gpointer func
, const char *name
, const char *sigstr
)
705 MonoMethodSignature
*sig
;
708 sig
= mono_create_icall_signature (sigstr
);
712 mono_register_jit_icall_full (func
, name
, sig
, FALSE
, FALSE
, NULL
);
716 register_dyn_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
718 MonoMethodSignature
*sig
;
721 sig
= mono_create_icall_signature (sigstr
);
725 mono_register_jit_icall (func
, name
, sig
, save
);
731 MonoJitTlsData
*jit_tls
;
733 if ((jit_tls
= mono_tls_get_jit_tls ()))
736 * We do not assert here because this function can be called from
737 * mini-gc.c on a thread that has not executed any managed code, yet
738 * (the thread object allocation can trigger a collection).
744 mono_get_lmf_addr (void)
746 return (MonoLMF
**)mono_tls_get_lmf_addr ();
750 mono_set_lmf (MonoLMF
*lmf
)
752 (*mono_get_lmf_addr ()) = lmf
;
756 mono_get_jit_tls (void)
758 return (MonoJitTlsData
*)mono_tls_get_jit_tls ();
762 mono_set_jit_tls (MonoJitTlsData
*jit_tls
)
764 MonoThreadInfo
*info
;
766 mono_tls_set_jit_tls (jit_tls
);
768 /* Save it into MonoThreadInfo so it can be accessed by mono_thread_state_init_from_handle () */
769 info
= mono_thread_info_current ();
771 mono_thread_info_tls_set (info
, TLS_KEY_JIT_TLS
, jit_tls
);
775 mono_set_lmf_addr (gpointer lmf_addr
)
777 MonoThreadInfo
*info
;
779 mono_tls_set_lmf_addr (lmf_addr
);
781 /* Save it into MonoThreadInfo so it can be accessed by mono_thread_state_init_from_handle () */
782 info
= mono_thread_info_current ();
784 mono_thread_info_tls_set (info
, TLS_KEY_LMF_ADDR
, lmf_addr
);
788 * mono_jit_thread_attach:
790 * Called by Xamarin.Mac and other products. Attach thread to runtime if
791 * needed and switch to @domain.
793 * @return the original domain which needs to be restored, or NULL.
796 mono_jit_thread_attach (MonoDomain
*domain
)
801 g_assert (!mono_threads_is_coop_enabled ());
804 /* Happens when called from AOTed code which is only used in the root domain. */
805 domain
= mono_get_root_domain ();
810 attached
= mono_tls_get_jit_tls () != NULL
;
813 mono_thread_attach (domain
);
816 mono_thread_set_state (mono_thread_internal_current (), ThreadState_Background
);
819 orig
= mono_domain_get ();
821 mono_domain_set (domain
, TRUE
);
823 return orig
!= domain
? orig
: NULL
;
827 * mono_jit_set_domain:
829 * Set domain to @domain if @domain is not null
832 mono_jit_set_domain (MonoDomain
*domain
)
834 g_assert (!mono_threads_is_coop_enabled ());
837 mono_domain_set (domain
, TRUE
);
842 * \param obj exception object
843 * Abort the thread, print exception information and stack trace
846 mono_thread_abort (MonoObject
*obj
)
848 /* MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id); */
850 /* handle_remove should be eventually called for this thread, too
853 if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_LEGACY
) ||
854 (obj
->vtable
->klass
== mono_defaults
.threadabortexception_class
)) {
857 mono_invoke_unhandled_exception_hook (obj
);
862 setup_jit_tls_data (gpointer stack_start
, gpointer abort_func
)
864 MonoJitTlsData
*jit_tls
;
867 jit_tls
= (MonoJitTlsData
*)mono_tls_get_jit_tls ();
871 jit_tls
= g_new0 (MonoJitTlsData
, 1);
873 jit_tls
->abort_func
= (void (*)(MonoObject
*))abort_func
;
874 jit_tls
->end_of_stack
= stack_start
;
876 mono_set_jit_tls (jit_tls
);
878 lmf
= g_new0 (MonoLMF
, 1);
879 MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf
);
881 jit_tls
->first_lmf
= lmf
;
883 mono_set_lmf_addr (&jit_tls
->lmf
);
887 #ifdef MONO_ARCH_HAVE_TLS_INIT
888 mono_arch_tls_init ();
891 mono_setup_altstack (jit_tls
);
897 free_jit_tls_data (MonoJitTlsData
*jit_tls
)
899 mono_arch_free_jit_tls_data (jit_tls
);
900 mono_free_altstack (jit_tls
);
902 g_free (jit_tls
->first_lmf
);
907 mono_thread_start_cb (intptr_t tid
, gpointer stack_start
, gpointer func
)
909 MonoThreadInfo
*thread
;
910 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort
);
911 thread
= mono_thread_info_current_unchecked ();
913 thread
->jit_data
= jit_tls
;
915 mono_arch_cpu_init ();
918 void (*mono_thread_attach_aborted_cb
) (MonoObject
*obj
) = NULL
;
921 mono_thread_abort_dummy (MonoObject
*obj
)
923 if (mono_thread_attach_aborted_cb
)
924 mono_thread_attach_aborted_cb (obj
);
926 mono_thread_abort (obj
);
930 mono_thread_attach_cb (intptr_t tid
, gpointer stack_start
)
932 MonoThreadInfo
*thread
;
933 void *jit_tls
= setup_jit_tls_data (stack_start
, mono_thread_abort_dummy
);
934 thread
= mono_thread_info_current_unchecked ();
936 thread
->jit_data
= jit_tls
;
938 mono_arch_cpu_init ();
942 mini_thread_cleanup (MonoNativeThreadId tid
)
944 MonoJitTlsData
*jit_tls
= NULL
;
945 MonoThreadInfo
*info
;
947 info
= mono_thread_info_current_unchecked ();
949 /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
950 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
951 * not a trivial thing.
953 * The current offender is mono_thread_manage which cleanup threads from the outside.
955 if (info
&& mono_thread_info_get_tid (info
) == tid
) {
956 jit_tls
= (MonoJitTlsData
*)info
->jit_data
;
957 info
->jit_data
= NULL
;
959 mono_set_jit_tls (NULL
);
961 /* If we attach a thread but never call into managed land, we might never get an lmf.*/
962 if (mono_get_lmf ()) {
964 mono_set_lmf_addr (NULL
);
967 info
= mono_thread_info_lookup (tid
);
969 jit_tls
= (MonoJitTlsData
*)info
->jit_data
;
970 info
->jit_data
= NULL
;
972 mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
976 free_jit_tls_data (jit_tls
);
980 mono_patch_info_list_prepend (MonoJumpInfo
*list
, int ip
, MonoJumpInfoType type
, gconstpointer target
)
982 MonoJumpInfo
*ji
= g_new0 (MonoJumpInfo
, 1);
986 ji
->data
.target
= target
;
992 #if !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT)
994 static const char* const patch_info_str
[] = {
995 #define PATCH_INFO(a,b) "" #a,
996 #include "patch-info.h"
1001 mono_ji_type_to_string (MonoJumpInfoType type
)
1003 return patch_info_str
[type
];
1007 mono_print_ji (const MonoJumpInfo
*ji
)
1010 case MONO_PATCH_INFO_RGCTX_FETCH
: {
1011 MonoJumpInfoRgctxEntry
*entry
= ji
->data
.rgctx_entry
;
1013 printf ("[RGCTX_FETCH ");
1014 mono_print_ji (entry
->data
);
1015 printf (" - %s]", mono_rgctx_info_type_to_str (entry
->info_type
));
1018 case MONO_PATCH_INFO_METHODCONST
: {
1019 char *s
= mono_method_full_name (ji
->data
.method
, TRUE
);
1020 printf ("[METHODCONST - %s]", s
);
1024 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
1025 printf ("[INTERNAL_METHOD - %s]", ji
->data
.name
);
1029 printf ("[%s]", patch_info_str
[ji
->type
]);
1037 mono_ji_type_to_string (MonoJumpInfoType type
)
1043 mono_print_ji (const MonoJumpInfo
*ji
)
1050 * mono_patch_info_dup_mp:
1052 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
1055 mono_patch_info_dup_mp (MonoMemPool
*mp
, MonoJumpInfo
*patch_info
)
1057 MonoJumpInfo
*res
= (MonoJumpInfo
*)mono_mempool_alloc (mp
, sizeof (MonoJumpInfo
));
1058 memcpy (res
, patch_info
, sizeof (MonoJumpInfo
));
1060 switch (patch_info
->type
) {
1061 case MONO_PATCH_INFO_RVA
:
1062 case MONO_PATCH_INFO_LDSTR
:
1063 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
1064 case MONO_PATCH_INFO_LDTOKEN
:
1065 case MONO_PATCH_INFO_DECLSEC
:
1066 res
->data
.token
= (MonoJumpInfoToken
*)mono_mempool_alloc (mp
, sizeof (MonoJumpInfoToken
));
1067 memcpy (res
->data
.token
, patch_info
->data
.token
, sizeof (MonoJumpInfoToken
));
1069 case MONO_PATCH_INFO_SWITCH
:
1070 res
->data
.table
= (MonoJumpInfoBBTable
*)mono_mempool_alloc (mp
, sizeof (MonoJumpInfoBBTable
));
1071 memcpy (res
->data
.table
, patch_info
->data
.table
, sizeof (MonoJumpInfoBBTable
));
1072 res
->data
.table
->table
= (MonoBasicBlock
**)mono_mempool_alloc (mp
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
1073 memcpy (res
->data
.table
->table
, patch_info
->data
.table
->table
, sizeof (MonoBasicBlock
*) * patch_info
->data
.table
->table_size
);
1075 case MONO_PATCH_INFO_RGCTX_FETCH
:
1076 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
:
1077 res
->data
.rgctx_entry
= (MonoJumpInfoRgctxEntry
*)mono_mempool_alloc (mp
, sizeof (MonoJumpInfoRgctxEntry
));
1078 memcpy (res
->data
.rgctx_entry
, patch_info
->data
.rgctx_entry
, sizeof (MonoJumpInfoRgctxEntry
));
1079 res
->data
.rgctx_entry
->data
= mono_patch_info_dup_mp (mp
, res
->data
.rgctx_entry
->data
);
1081 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
1082 res
->data
.del_tramp
= (MonoDelegateClassMethodPair
*)mono_mempool_alloc0 (mp
, sizeof (MonoDelegateClassMethodPair
));
1083 memcpy (res
->data
.del_tramp
, patch_info
->data
.del_tramp
, sizeof (MonoDelegateClassMethodPair
));
1085 case MONO_PATCH_INFO_GSHAREDVT_CALL
:
1086 res
->data
.gsharedvt
= (MonoJumpInfoGSharedVtCall
*)mono_mempool_alloc (mp
, sizeof (MonoJumpInfoGSharedVtCall
));
1087 memcpy (res
->data
.gsharedvt
, patch_info
->data
.gsharedvt
, sizeof (MonoJumpInfoGSharedVtCall
));
1089 case MONO_PATCH_INFO_GSHAREDVT_METHOD
: {
1090 MonoGSharedVtMethodInfo
*info
;
1091 MonoGSharedVtMethodInfo
*oinfo
;
1094 oinfo
= patch_info
->data
.gsharedvt_method
;
1095 info
= (MonoGSharedVtMethodInfo
*)mono_mempool_alloc (mp
, sizeof (MonoGSharedVtMethodInfo
));
1096 res
->data
.gsharedvt_method
= info
;
1097 memcpy (info
, oinfo
, sizeof (MonoGSharedVtMethodInfo
));
1098 info
->entries
= (MonoRuntimeGenericContextInfoTemplate
*)mono_mempool_alloc (mp
, sizeof (MonoRuntimeGenericContextInfoTemplate
) * info
->count_entries
);
1099 for (i
= 0; i
< oinfo
->num_entries
; ++i
) {
1100 MonoRuntimeGenericContextInfoTemplate
*otemplate
= &oinfo
->entries
[i
];
1101 MonoRuntimeGenericContextInfoTemplate
*template_
= &info
->entries
[i
];
1103 memcpy (template_
, otemplate
, sizeof (MonoRuntimeGenericContextInfoTemplate
));
1105 //info->locals_types = mono_mempool_alloc0 (mp, info->nlocals * sizeof (MonoType*));
1106 //memcpy (info->locals_types, oinfo->locals_types, info->nlocals * sizeof (MonoType*));
1109 case MONO_PATCH_INFO_VIRT_METHOD
: {
1110 MonoJumpInfoVirtMethod
*info
;
1111 MonoJumpInfoVirtMethod
*oinfo
;
1113 oinfo
= patch_info
->data
.virt_method
;
1114 info
= (MonoJumpInfoVirtMethod
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoVirtMethod
));
1115 res
->data
.virt_method
= info
;
1116 memcpy (info
, oinfo
, sizeof (MonoJumpInfoVirtMethod
));
1127 mono_patch_info_hash (gconstpointer data
)
1129 const MonoJumpInfo
*ji
= (MonoJumpInfo
*)data
;
1132 case MONO_PATCH_INFO_RVA
:
1133 case MONO_PATCH_INFO_LDSTR
:
1134 case MONO_PATCH_INFO_LDTOKEN
:
1135 case MONO_PATCH_INFO_DECLSEC
:
1136 return (ji
->type
<< 8) | ji
->data
.token
->token
;
1137 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
1138 return (ji
->type
<< 8) | ji
->data
.token
->token
| (ji
->data
.token
->has_context
? (gsize
)ji
->data
.token
->context
.class_inst
: 0);
1139 case MONO_PATCH_INFO_INTERNAL_METHOD
:
1140 return (ji
->type
<< 8) | g_str_hash (ji
->data
.name
);
1141 case MONO_PATCH_INFO_VTABLE
:
1142 case MONO_PATCH_INFO_CLASS
:
1143 case MONO_PATCH_INFO_IID
:
1144 case MONO_PATCH_INFO_ADJUSTED_IID
:
1145 case MONO_PATCH_INFO_METHODCONST
:
1146 case MONO_PATCH_INFO_METHOD
:
1147 case MONO_PATCH_INFO_METHOD_JUMP
:
1148 case MONO_PATCH_INFO_IMAGE
:
1149 case MONO_PATCH_INFO_ICALL_ADDR
:
1150 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
1151 case MONO_PATCH_INFO_FIELD
:
1152 case MONO_PATCH_INFO_SFLDA
:
1153 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
1154 case MONO_PATCH_INFO_METHOD_RGCTX
:
1155 case MONO_PATCH_INFO_SIGNATURE
:
1156 case MONO_PATCH_INFO_METHOD_CODE_SLOT
:
1157 case MONO_PATCH_INFO_AOT_JIT_INFO
:
1158 case MONO_PATCH_INFO_GET_TLS_TRAMP
:
1159 case MONO_PATCH_INFO_SET_TLS_TRAMP
:
1160 return (ji
->type
<< 8) | (gssize
)ji
->data
.target
;
1161 case MONO_PATCH_INFO_GSHAREDVT_CALL
:
1162 return (ji
->type
<< 8) | (gssize
)ji
->data
.gsharedvt
->method
;
1163 case MONO_PATCH_INFO_RGCTX_FETCH
:
1164 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
1165 MonoJumpInfoRgctxEntry
*e
= ji
->data
.rgctx_entry
;
1167 return (ji
->type
<< 8) | (gssize
)e
->method
| (e
->in_mrgctx
) | e
->info_type
| mono_patch_info_hash (e
->data
);
1169 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
1170 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
:
1171 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
1172 case MONO_PATCH_INFO_GC_NURSERY_START
:
1173 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
1174 case MONO_PATCH_INFO_GOT_OFFSET
:
1175 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
:
1176 case MONO_PATCH_INFO_AOT_MODULE
:
1177 case MONO_PATCH_INFO_JIT_THREAD_ATTACH
:
1178 return (ji
->type
<< 8);
1179 case MONO_PATCH_INFO_CASTCLASS_CACHE
:
1180 return (ji
->type
<< 8) | (ji
->data
.index
);
1181 case MONO_PATCH_INFO_SWITCH
:
1182 return (ji
->type
<< 8) | ji
->data
.table
->table_size
;
1183 case MONO_PATCH_INFO_GSHAREDVT_METHOD
:
1184 return (ji
->type
<< 8) | (gssize
)ji
->data
.gsharedvt_method
->method
;
1185 case MONO_PATCH_INFO_OBJC_SELECTOR_REF
:
1186 /* Hash on the selector name */
1187 return g_str_hash (ji
->data
.target
);
1188 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
1189 return (ji
->type
<< 8) | (gsize
)ji
->data
.del_tramp
->klass
| (gsize
)ji
->data
.del_tramp
->method
| (gsize
)ji
->data
.del_tramp
->is_virtual
;
1190 case MONO_PATCH_INFO_LDSTR_LIT
:
1191 return g_str_hash (ji
->data
.target
);
1192 case MONO_PATCH_INFO_VIRT_METHOD
: {
1193 MonoJumpInfoVirtMethod
*info
= ji
->data
.virt_method
;
1195 return (ji
->type
<< 8) | (gssize
)info
->klass
| (gssize
)info
->method
;
1197 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
1198 return (ji
->type
<< 8) | g_str_hash (ji
->data
.target
);
1199 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
1200 return (ji
->type
<< 8) | mono_signature_hash (ji
->data
.sig
);
1202 printf ("info type: %d\n", ji
->type
);
1203 mono_print_ji (ji
); printf ("\n");
1204 g_assert_not_reached ();
1210 * mono_patch_info_equal:
1212 * This might fail to recognize equivalent patches, i.e. floats, so its only
1213 * usable in those cases where this is not a problem, i.e. sharing GOT slots
1217 mono_patch_info_equal (gconstpointer ka
, gconstpointer kb
)
1219 const MonoJumpInfo
*ji1
= (MonoJumpInfo
*)ka
;
1220 const MonoJumpInfo
*ji2
= (MonoJumpInfo
*)kb
;
1222 if (ji1
->type
!= ji2
->type
)
1225 switch (ji1
->type
) {
1226 case MONO_PATCH_INFO_RVA
:
1227 case MONO_PATCH_INFO_LDSTR
:
1228 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
1229 case MONO_PATCH_INFO_LDTOKEN
:
1230 case MONO_PATCH_INFO_DECLSEC
:
1231 if ((ji1
->data
.token
->image
!= ji2
->data
.token
->image
) ||
1232 (ji1
->data
.token
->token
!= ji2
->data
.token
->token
) ||
1233 (ji1
->data
.token
->has_context
!= ji2
->data
.token
->has_context
) ||
1234 (ji1
->data
.token
->context
.class_inst
!= ji2
->data
.token
->context
.class_inst
) ||
1235 (ji1
->data
.token
->context
.method_inst
!= ji2
->data
.token
->context
.method_inst
))
1238 case MONO_PATCH_INFO_INTERNAL_METHOD
:
1239 return g_str_equal (ji1
->data
.name
, ji2
->data
.name
);
1240 case MONO_PATCH_INFO_RGCTX_FETCH
:
1241 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
1242 MonoJumpInfoRgctxEntry
*e1
= ji1
->data
.rgctx_entry
;
1243 MonoJumpInfoRgctxEntry
*e2
= ji2
->data
.rgctx_entry
;
1245 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
);
1247 case MONO_PATCH_INFO_GSHAREDVT_CALL
: {
1248 MonoJumpInfoGSharedVtCall
*c1
= ji1
->data
.gsharedvt
;
1249 MonoJumpInfoGSharedVtCall
*c2
= ji2
->data
.gsharedvt
;
1251 return c1
->sig
== c2
->sig
&& c1
->method
== c2
->method
;
1253 case MONO_PATCH_INFO_GSHAREDVT_METHOD
:
1254 return ji1
->data
.gsharedvt_method
->method
== ji2
->data
.gsharedvt_method
->method
;
1255 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
1256 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
->is_virtual
== ji2
->data
.del_tramp
->is_virtual
;
1257 case MONO_PATCH_INFO_CASTCLASS_CACHE
:
1258 return ji1
->data
.index
== ji2
->data
.index
;
1259 case MONO_PATCH_INFO_VIRT_METHOD
:
1260 return ji1
->data
.virt_method
->klass
== ji2
->data
.virt_method
->klass
&& ji1
->data
.virt_method
->method
== ji2
->data
.virt_method
->method
;
1261 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
1262 if (ji1
->data
.target
== ji2
->data
.target
)
1264 return strcmp (ji1
->data
.target
, ji2
->data
.target
) == 0 ? 1 : 0;
1265 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
1266 return mono_metadata_signature_equal (ji1
->data
.sig
, ji2
->data
.sig
) ? 1 : 0;
1268 if (ji1
->data
.target
!= ji2
->data
.target
)
1277 mono_resolve_patch_target (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*patch_info
, gboolean run_cctors
, MonoError
*error
)
1279 unsigned char *ip
= patch_info
->ip
.i
+ code
;
1280 gconstpointer target
= NULL
;
1284 switch (patch_info
->type
) {
1285 case MONO_PATCH_INFO_BB
:
1287 * FIXME: This could be hit for methods without a prolog. Should use -1
1288 * but too much code depends on a 0 initial value.
1290 //g_assert (patch_info->data.bb->native_offset);
1291 target
= patch_info
->data
.bb
->native_offset
+ code
;
1293 case MONO_PATCH_INFO_ABS
:
1294 target
= patch_info
->data
.target
;
1296 case MONO_PATCH_INFO_LABEL
:
1297 target
= patch_info
->data
.inst
->inst_c0
+ code
;
1299 case MONO_PATCH_INFO_IP
:
1302 case MONO_PATCH_INFO_METHOD_REL
:
1303 target
= code
+ patch_info
->data
.offset
;
1305 case MONO_PATCH_INFO_INTERNAL_METHOD
: {
1306 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
1308 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info
->data
.name
);
1309 g_assert_not_reached ();
1311 target
= mono_icall_get_wrapper (mi
);
1314 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
1315 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
1317 g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info
->data
.name
);
1318 g_assert_not_reached ();
1323 case MONO_PATCH_INFO_METHOD_JUMP
:
1324 target
= mono_create_jump_trampoline (domain
, patch_info
->data
.method
, FALSE
, error
);
1325 if (!mono_error_ok (error
))
1328 case MONO_PATCH_INFO_METHOD
:
1329 if (patch_info
->data
.method
== method
) {
1332 /* get the trampoline to the method from the domain */
1333 target
= mono_create_jit_trampoline (domain
, patch_info
->data
.method
, error
);
1334 if (!mono_error_ok (error
))
1338 case MONO_PATCH_INFO_METHOD_CODE_SLOT
: {
1341 mono_domain_lock (domain
);
1342 if (!domain_jit_info (domain
)->method_code_hash
)
1343 domain_jit_info (domain
)->method_code_hash
= g_hash_table_new (NULL
, NULL
);
1344 code_slot
= g_hash_table_lookup (domain_jit_info (domain
)->method_code_hash
, patch_info
->data
.method
);
1346 code_slot
= mono_domain_alloc0 (domain
, sizeof (gpointer
));
1347 g_hash_table_insert (domain_jit_info (domain
)->method_code_hash
, patch_info
->data
.method
, code_slot
);
1349 mono_domain_unlock (domain
);
1353 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
:
1354 #if defined(__native_client_codegen__)
1355 target
= (gpointer
)&__nacl_thread_suspension_needed
;
1357 g_assert (mono_threads_is_coop_enabled ());
1358 target
= (gpointer
)&mono_polling_required
;
1361 case MONO_PATCH_INFO_SWITCH
: {
1362 gpointer
*jump_table
;
1364 #if defined(__native_client__) && defined(__native_client_codegen__)
1365 /* This memory will leak, but we don't care if we're */
1366 /* not deleting JIT'd methods anyway */
1367 jump_table
= g_malloc0 (sizeof(gpointer
) * patch_info
->data
.table
->table_size
);
1369 if (method
&& method
->dynamic
) {
1370 jump_table
= (void **)mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain
, method
)->code_mp
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
1372 if (mono_aot_only
) {
1373 jump_table
= (void **)mono_domain_alloc (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
1375 jump_table
= (void **)mono_domain_code_reserve (domain
, sizeof (gpointer
) * patch_info
->data
.table
->table_size
);
1380 for (i
= 0; i
< patch_info
->data
.table
->table_size
; i
++) {
1381 jump_table
[i
] = code
+ GPOINTER_TO_INT (patch_info
->data
.table
->table
[i
]);
1384 target
= jump_table
;
1387 case MONO_PATCH_INFO_METHODCONST
:
1388 case MONO_PATCH_INFO_CLASS
:
1389 case MONO_PATCH_INFO_IMAGE
:
1390 case MONO_PATCH_INFO_FIELD
:
1391 case MONO_PATCH_INFO_SIGNATURE
:
1392 case MONO_PATCH_INFO_AOT_MODULE
:
1393 target
= patch_info
->data
.target
;
1395 case MONO_PATCH_INFO_IID
:
1396 mono_class_init (patch_info
->data
.klass
);
1397 target
= GUINT_TO_POINTER (patch_info
->data
.klass
->interface_id
);
1399 case MONO_PATCH_INFO_ADJUSTED_IID
:
1400 mono_class_init (patch_info
->data
.klass
);
1401 target
= GUINT_TO_POINTER ((guint32
)(-((patch_info
->data
.klass
->interface_id
+ 1) * SIZEOF_VOID_P
)));
1403 case MONO_PATCH_INFO_VTABLE
:
1404 target
= mono_class_vtable (domain
, patch_info
->data
.klass
);
1407 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
: {
1408 MonoDelegateClassMethodPair
*del_tramp
= patch_info
->data
.del_tramp
;
1410 if (del_tramp
->is_virtual
)
1411 target
= mono_create_delegate_virtual_trampoline (domain
, del_tramp
->klass
, del_tramp
->method
);
1413 target
= mono_create_delegate_trampoline_info (domain
, del_tramp
->klass
, del_tramp
->method
);
1416 case MONO_PATCH_INFO_SFLDA
: {
1417 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.field
->parent
);
1419 if (mono_class_field_is_special_static (patch_info
->data
.field
)) {
1420 gpointer addr
= NULL
;
1422 mono_domain_lock (domain
);
1423 if (domain
->special_static_fields
)
1424 addr
= g_hash_table_lookup (domain
->special_static_fields
, patch_info
->data
.field
);
1425 mono_domain_unlock (domain
);
1431 if (!vtable
->initialized
&& !mono_class_is_before_field_init (vtable
->klass
) && (method
&& mono_class_needs_cctor_run (vtable
->klass
, method
)))
1432 /* Done by the generated code */
1436 if (!mono_runtime_class_init_full (vtable
, error
)) {
1441 target
= (char*)mono_vtable_get_static_field_data (vtable
) + patch_info
->data
.field
->offset
;
1444 case MONO_PATCH_INFO_RVA
: {
1445 guint32 field_index
= mono_metadata_token_index (patch_info
->data
.token
->token
);
1448 mono_metadata_field_info (patch_info
->data
.token
->image
, field_index
- 1, NULL
, &rva
, NULL
);
1449 target
= mono_image_rva_map (patch_info
->data
.token
->image
, rva
);
1452 case MONO_PATCH_INFO_R4
:
1453 case MONO_PATCH_INFO_R8
:
1454 target
= patch_info
->data
.target
;
1456 case MONO_PATCH_INFO_EXC_NAME
:
1457 target
= patch_info
->data
.name
;
1459 case MONO_PATCH_INFO_LDSTR
:
1461 mono_ldstr_checked (domain
, patch_info
->data
.token
->image
,
1462 mono_metadata_token_index (patch_info
->data
.token
->token
), error
);
1464 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
: {
1466 MonoClass
*handle_class
;
1468 handle
= mono_ldtoken_checked (patch_info
->data
.token
->image
,
1469 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
, error
);
1470 if (!mono_error_ok (error
))
1472 mono_class_init (handle_class
);
1473 mono_class_init (mono_class_from_mono_type ((MonoType
*)handle
));
1475 target
= mono_type_get_object_checked (domain
, (MonoType
*)handle
, error
);
1476 if (!mono_error_ok (error
))
1480 case MONO_PATCH_INFO_LDTOKEN
: {
1482 MonoClass
*handle_class
;
1484 handle
= mono_ldtoken_checked (patch_info
->data
.token
->image
,
1485 patch_info
->data
.token
->token
, &handle_class
, patch_info
->data
.token
->has_context
? &patch_info
->data
.token
->context
: NULL
, error
);
1486 if (!mono_error_ok (error
))
1487 g_error ("Could not patch ldtoken due to %s", mono_error_get_message (error
));
1488 mono_class_init (handle_class
);
1493 case MONO_PATCH_INFO_DECLSEC
:
1494 target
= (mono_metadata_blob_heap (patch_info
->data
.token
->image
, patch_info
->data
.token
->token
) + 2);
1496 case MONO_PATCH_INFO_ICALL_ADDR
:
1497 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
1498 /* run_cctors == 0 -> AOT */
1499 if (patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) {
1500 const char *exc_class
;
1501 const char *exc_arg
;
1504 target
= mono_lookup_pinvoke_call (patch_info
->data
.method
, &exc_class
, &exc_arg
);
1506 if (mono_aot_only
) {
1507 mono_error_set_exception_instance (error
, mono_exception_from_name_msg (mono_defaults
.corlib
, "System", exc_class
, exc_arg
));
1510 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
));
1516 target
= mono_lookup_internal_call (patch_info
->data
.method
);
1518 if (!target
&& run_cctors
)
1519 g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info
->data
.method
, TRUE
));
1522 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
1523 target
= mono_thread_interruption_request_flag ();
1525 case MONO_PATCH_INFO_METHOD_RGCTX
: {
1526 MonoVTable
*vtable
= mono_class_vtable (domain
, patch_info
->data
.method
->klass
);
1529 target
= mono_method_lookup_rgctx (vtable
, mini_method_get_context (patch_info
->data
.method
)->method_inst
);
1532 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
1533 int slot
= mini_get_rgctx_entry_slot (patch_info
->data
.rgctx_entry
);
1535 target
= GINT_TO_POINTER (MONO_RGCTX_SLOT_INDEX (slot
));
1538 case MONO_PATCH_INFO_BB_OVF
:
1539 case MONO_PATCH_INFO_EXC_OVF
:
1540 case MONO_PATCH_INFO_GOT_OFFSET
:
1541 case MONO_PATCH_INFO_NONE
:
1543 case MONO_PATCH_INFO_RGCTX_FETCH
: {
1544 int slot
= mini_get_rgctx_entry_slot (patch_info
->data
.rgctx_entry
);
1546 target
= mono_create_rgctx_lazy_fetch_trampoline (slot
);
1549 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
1550 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
1552 /* AOT, not needed */
1555 target
= mono_arch_get_seq_point_info (domain
, code
);
1558 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
: {
1559 int card_table_shift_bits
;
1560 gpointer card_table_mask
;
1562 target
= mono_gc_get_card_table (&card_table_shift_bits
, &card_table_mask
);
1565 case MONO_PATCH_INFO_GC_NURSERY_START
: {
1569 target
= mono_gc_get_nursery (&shift_bits
, &size
);
1572 case MONO_PATCH_INFO_GC_NURSERY_BITS
: {
1576 mono_gc_get_nursery (&shift_bits
, &size
);
1578 target
= (gpointer
)(mgreg_t
)shift_bits
;
1581 case MONO_PATCH_INFO_CASTCLASS_CACHE
: {
1582 target
= mono_domain_alloc0 (domain
, sizeof (gpointer
));
1585 case MONO_PATCH_INFO_OBJC_SELECTOR_REF
: {
1589 case MONO_PATCH_INFO_LDSTR_LIT
: {
1593 len
= strlen ((const char *)patch_info
->data
.target
);
1594 s
= (char *)mono_domain_alloc0 (domain
, len
+ 1);
1595 memcpy (s
, patch_info
->data
.target
, len
);
1600 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
1601 target
= mini_get_gsharedvt_wrapper (TRUE
, NULL
, patch_info
->data
.sig
, NULL
, -1, FALSE
);
1603 case MONO_PATCH_INFO_GET_TLS_TRAMP
:
1604 target
= mono_tls_get_tls_getter (patch_info
->data
.index
, FALSE
);
1606 case MONO_PATCH_INFO_SET_TLS_TRAMP
:
1607 target
= mono_tls_get_tls_setter (patch_info
->data
.index
, FALSE
);
1609 case MONO_PATCH_INFO_JIT_THREAD_ATTACH
: {
1610 MonoJitICallInfo
*mi
= mono_find_jit_icall_by_name ("mono_jit_thread_attach");
1616 g_assert_not_reached ();
1619 return (gpointer
)target
;
1623 mini_init_gsctx (MonoDomain
*domain
, MonoMemPool
*mp
, MonoGenericContext
*context
, MonoGenericSharingContext
*gsctx
)
1625 MonoGenericInst
*inst
;
1628 memset (gsctx
, 0, sizeof (MonoGenericSharingContext
));
1630 if (context
&& context
->class_inst
) {
1631 inst
= context
->class_inst
;
1632 for (i
= 0; i
< inst
->type_argc
; ++i
) {
1633 MonoType
*type
= inst
->type_argv
[i
];
1635 if (mini_is_gsharedvt_gparam (type
))
1636 gsctx
->is_gsharedvt
= TRUE
;
1639 if (context
&& context
->method_inst
) {
1640 inst
= context
->method_inst
;
1642 for (i
= 0; i
< inst
->type_argc
; ++i
) {
1643 MonoType
*type
= inst
->type_argv
[i
];
1645 if (mini_is_gsharedvt_gparam (type
))
1646 gsctx
->is_gsharedvt
= TRUE
;
1652 * LOCKING: Acquires the jit code hash lock.
1655 mini_lookup_method (MonoDomain
*domain
, MonoMethod
*method
, MonoMethod
*shared
)
1658 static gboolean inited
= FALSE
;
1659 static int lookups
= 0;
1660 static int failed_lookups
= 0;
1662 mono_domain_jit_code_hash_lock (domain
);
1663 ji
= (MonoJitInfo
*)mono_internal_hash_table_lookup (&domain
->jit_code_hash
, method
);
1664 if (!ji
&& shared
) {
1665 /* Try generic sharing */
1666 ji
= (MonoJitInfo
*)mono_internal_hash_table_lookup (&domain
->jit_code_hash
, shared
);
1667 if (ji
&& !ji
->has_generic_jit_info
)
1670 mono_counters_register ("Shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &lookups
);
1671 mono_counters_register ("Failed shared generic lookups", MONO_COUNTER_INT
|MONO_COUNTER_GENERICS
, &failed_lookups
);
1679 mono_domain_jit_code_hash_unlock (domain
);
1685 lookup_method (MonoDomain
*domain
, MonoMethod
*method
)
1690 ji
= mini_lookup_method (domain
, method
, NULL
);
1693 if (!mono_method_is_generic_sharable (method
, FALSE
))
1695 shared
= mini_get_shared_method (method
);
1696 ji
= mini_lookup_method (domain
, method
, shared
);
1703 mono_get_jit_info_from_method (MonoDomain
*domain
, MonoMethod
*method
)
1705 return lookup_method (domain
, method
);
1709 static FILE* perf_map_file
;
1712 mono_enable_jit_map (void)
1714 if (!perf_map_file
) {
1716 g_snprintf (name
, sizeof (name
), "/tmp/perf-%d.map", getpid ());
1718 perf_map_file
= fopen (name
, "w");
1723 mono_emit_jit_tramp (void *start
, int size
, const char *desc
)
1726 fprintf (perf_map_file
, "%llx %x %s\n", (long long unsigned int)(gsize
)start
, size
, desc
);
1730 mono_emit_jit_map (MonoJitInfo
*jinfo
)
1732 if (perf_map_file
) {
1733 char *name
= mono_method_full_name (jinfo_get_method (jinfo
), TRUE
);
1734 mono_emit_jit_tramp (jinfo
->code_start
, jinfo
->code_size
, name
);
1740 mono_jit_map_is_enabled (void)
1742 return perf_map_file
!= NULL
;
1748 no_gsharedvt_in_wrapper (void)
1750 g_assert_not_reached ();
1754 mono_jit_compile_method_with_opt (MonoMethod
*method
, guint32 opt
, gboolean jit_only
, MonoError
*error
)
1756 MonoDomain
*target_domain
, *domain
= mono_domain_get ();
1758 gpointer code
= NULL
, p
;
1760 MonoJitICallInfo
*callinfo
= NULL
;
1761 WrapperInfo
*winfo
= NULL
;
1765 #ifdef ENABLE_INTERPRETER
1766 if (mono_use_interpreter
&& !jit_only
) {
1767 code
= mono_interp_create_method_pointer (method
, error
);
1774 /* Should be handled by the caller */
1775 g_assert (!(method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
));
1778 * ICALL wrappers are handled specially, since there is only one copy of them
1779 * shared by all appdomains.
1781 if (method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
)
1782 winfo
= mono_marshal_get_wrapper_info (method
);
1783 if (winfo
&& winfo
->subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
1784 callinfo
= mono_find_jit_icall_by_addr (winfo
->d
.icall
.func
);
1785 g_assert (callinfo
);
1787 /* Must be domain neutral since there is only one copy */
1788 opt
|= MONO_OPT_SHARED
;
1790 /* MONO_OPT_SHARED is no longer supported, we only use it for icall wrappers */
1791 opt
&= ~MONO_OPT_SHARED
;
1794 if (opt
& MONO_OPT_SHARED
)
1795 target_domain
= mono_get_root_domain ();
1797 target_domain
= domain
;
1799 if (method
->wrapper_type
== MONO_WRAPPER_UNKNOWN
) {
1800 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
1803 if (info
->subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
) {
1804 MonoGenericContext
*ctx
= NULL
;
1805 if (method
->is_inflated
)
1806 ctx
= mono_method_get_context (method
);
1807 method
= info
->d
.synchronized_inner
.method
;
1809 method
= mono_class_inflate_generic_method_checked (method
, ctx
, error
);
1810 g_assert (mono_error_ok (error
)); /* FIXME don't swallow the error */
1815 info
= lookup_method (target_domain
, method
);
1817 /* We can't use a domain specific method in another domain */
1818 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
1821 mono_jit_stats
.methods_lookups
++;
1822 vtable
= mono_class_vtable_full (domain
, method
->klass
, error
);
1826 if (!mono_runtime_class_init_full (vtable
, error
))
1828 return mono_create_ftnptr (target_domain
, info
->code_start
);
1832 #ifdef MONO_USE_AOT_COMPILER
1833 if (opt
& MONO_OPT_AOT
) {
1834 MonoDomain
*domain
= mono_domain_get ();
1836 mono_class_init (method
->klass
);
1838 if ((code
= mono_aot_get_method_checked (domain
, method
, error
))) {
1841 if (mono_runtime_is_critical_method (method
) || mono_gc_is_critical_method (method
)) {
1843 * The suspend code needs to be able to lookup these methods by ip in async context,
1844 * so preload their jit info.
1846 MonoJitInfo
*ji
= mono_jit_info_table_find (domain
, code
);
1851 * In llvm-only mode, method might be a shared method, so we can't initialize its class.
1852 * This is not a problem, since it will be initialized when the method is first
1853 * called by init_method ().
1855 if (!mono_llvm_only
) {
1856 vtable
= mono_class_vtable (domain
, method
->klass
);
1858 if (!mono_runtime_class_init_full (vtable
, error
))
1867 if (!code
&& mono_llvm_only
) {
1868 if (method
->wrapper_type
== MONO_WRAPPER_UNKNOWN
) {
1869 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
1871 if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
) {
1873 * These wrappers are only created for signatures which are in the program, but
1874 * sometimes we load methods too eagerly and have to create them even if they
1875 * will never be called.
1877 return no_gsharedvt_in_wrapper
;
1883 code
= mono_jit_compile_method_inner (method
, target_domain
, opt
, error
);
1884 if (!mono_error_ok (error
))
1887 if (!code
&& mono_llvm_only
) {
1888 printf ("AOT method not found in llvmonly mode: %s\n", mono_method_full_name (method
, 1));
1889 g_assert_not_reached ();
1895 if (method
->wrapper_type
== MONO_WRAPPER_WRITE_BARRIER
|| method
->wrapper_type
== MONO_WRAPPER_ALLOC
) {
1899 * SGEN requires the JIT info for these methods to be registered, see is_ip_in_managed_allocator ().
1901 ji
= mini_jit_info_table_find (mono_domain_get (), (char *)code
, &d
);
1905 p
= mono_create_ftnptr (target_domain
, code
);
1908 /*mono_register_jit_icall_wrapper takes the loader lock, so we take it on the outside. */
1909 mono_loader_lock ();
1911 if (!callinfo
->wrapper
) {
1912 callinfo
->wrapper
= p
;
1913 mono_register_jit_icall_wrapper (callinfo
, p
);
1916 mono_loader_unlock ();
1923 mono_jit_compile_method (MonoMethod
*method
, MonoError
*error
)
1927 code
= mono_jit_compile_method_with_opt (method
, mono_get_optimizations_for_method (method
, default_opt
), FALSE
, error
);
1932 * mono_jit_compile_method_jit_only:
1934 * Compile METHOD using the JIT/AOT, even in interpreted mode.
1937 mono_jit_compile_method_jit_only (MonoMethod
*method
, MonoError
*error
)
1941 code
= mono_jit_compile_method_with_opt (method
, mono_get_optimizations_for_method (method
, default_opt
), TRUE
, error
);
1945 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
1947 invalidated_delegate_trampoline (char *desc
)
1949 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
1950 "See http://www.mono-project.com/Diagnostic:Delegate for an explanation and ways to fix this.",
1956 * mono_jit_free_method:
1958 * Free all memory allocated by the JIT for METHOD.
1961 mono_jit_free_method (MonoDomain
*domain
, MonoMethod
*method
)
1963 MonoJitDynamicMethodInfo
*ji
;
1964 gboolean destroy
= TRUE
;
1965 GHashTableIter iter
;
1966 MonoJumpList
*jlist
;
1968 g_assert (method
->dynamic
);
1970 mono_domain_lock (domain
);
1971 ji
= mono_dynamic_code_hash_lookup (domain
, method
);
1972 mono_domain_unlock (domain
);
1977 mono_debug_remove_method (method
, domain
);
1978 mono_lldb_remove_method (domain
, method
, ji
);
1980 mono_domain_lock (domain
);
1981 g_hash_table_remove (domain_jit_info (domain
)->dynamic_code_hash
, method
);
1982 mono_domain_jit_code_hash_lock (domain
);
1983 mono_internal_hash_table_remove (&domain
->jit_code_hash
, method
);
1984 mono_domain_jit_code_hash_unlock (domain
);
1985 g_hash_table_remove (domain_jit_info (domain
)->jump_trampoline_hash
, method
);
1987 /* requires the domain lock - took above */
1988 mono_conc_hashtable_remove (domain_jit_info (domain
)->runtime_invoke_hash
, method
);
1990 /* Remove jump targets in this method */
1991 g_hash_table_iter_init (&iter
, domain_jit_info (domain
)->jump_target_hash
);
1992 while (g_hash_table_iter_next (&iter
, NULL
, (void**)&jlist
)) {
1993 GSList
*tmp
, *remove
;
1996 for (tmp
= jlist
->list
; tmp
; tmp
= tmp
->next
) {
1997 guint8
*ip
= (guint8
*)tmp
->data
;
1999 if (ip
>= (guint8
*)ji
->ji
->code_start
&& ip
< (guint8
*)ji
->ji
->code_start
+ ji
->ji
->code_size
)
2000 remove
= g_slist_prepend (remove
, tmp
);
2002 for (tmp
= remove
; tmp
; tmp
= tmp
->next
) {
2003 jlist
->list
= g_slist_delete_link ((GSList
*)jlist
->list
, (GSList
*)tmp
->data
);
2005 g_slist_free (remove
);
2007 mono_domain_unlock (domain
);
2009 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
2010 if (debug_options
.keep_delegates
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
2012 * Instead of freeing the code, change it to call an error routine
2013 * so people can fix their code.
2015 char *type
= mono_type_full_name (&method
->klass
->byval_arg
);
2016 char *type_and_method
= g_strdup_printf ("%s.%s", type
, method
->name
);
2019 mono_arch_invalidate_method (ji
->ji
, invalidated_delegate_trampoline
, type_and_method
);
2025 * This needs to be done before freeing code_mp, since the code address is the
2026 * key in the table, so if we free the code_mp first, another thread can grab the
2027 * same code address and replace our entry in the table.
2029 mono_jit_info_table_remove (domain
, ji
->ji
);
2032 mono_code_manager_destroy (ji
->code_mp
);
2037 mono_jit_find_compiled_method_with_jit_info (MonoDomain
*domain
, MonoMethod
*method
, MonoJitInfo
**ji
)
2039 MonoDomain
*target_domain
;
2042 if (default_opt
& MONO_OPT_SHARED
)
2043 target_domain
= mono_get_root_domain ();
2045 target_domain
= domain
;
2047 info
= lookup_method (target_domain
, method
);
2049 /* We can't use a domain specific method in another domain */
2050 if (! ((domain
!= target_domain
) && !info
->domain_neutral
)) {
2051 mono_jit_stats
.methods_lookups
++;
2054 return info
->code_start
;
2063 static guint32 bisect_opt
= 0;
2064 static GHashTable
*bisect_methods_hash
= NULL
;
2067 mono_set_bisect_methods (guint32 opt
, const char *method_list_filename
)
2070 char method_name
[2048];
2073 bisect_methods_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
2074 g_assert (bisect_methods_hash
);
2076 file
= fopen (method_list_filename
, "r");
2079 while (fgets (method_name
, sizeof (method_name
), file
)) {
2080 size_t len
= strlen (method_name
);
2082 g_assert (method_name
[len
- 1] == '\n');
2083 method_name
[len
- 1] = 0;
2084 g_hash_table_insert (bisect_methods_hash
, g_strdup (method_name
), GINT_TO_POINTER (1));
2086 g_assert (feof (file
));
2089 gboolean mono_do_single_method_regression
= FALSE
;
2090 guint32 mono_single_method_regression_opt
= 0;
2091 MonoMethod
*mono_current_single_method
;
2092 GSList
*mono_single_method_list
;
2093 GHashTable
*mono_single_method_hash
;
2096 mono_get_optimizations_for_method (MonoMethod
*method
, guint32 default_opt
)
2100 if (bisect_methods_hash
) {
2101 char *name
= mono_method_full_name (method
, TRUE
);
2102 void *res
= g_hash_table_lookup (bisect_methods_hash
, name
);
2105 return default_opt
| bisect_opt
;
2107 if (!mono_do_single_method_regression
)
2109 if (!mono_current_single_method
) {
2110 if (!mono_single_method_hash
)
2111 mono_single_method_hash
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
2112 if (!g_hash_table_lookup (mono_single_method_hash
, method
)) {
2113 g_hash_table_insert (mono_single_method_hash
, method
, method
);
2114 mono_single_method_list
= g_slist_prepend (mono_single_method_list
, method
);
2118 if (method
== mono_current_single_method
)
2119 return mono_single_method_regression_opt
;
2124 mono_jit_find_compiled_method (MonoDomain
*domain
, MonoMethod
*method
)
2126 return mono_jit_find_compiled_method_with_jit_info (domain
, method
, NULL
);
2131 gpointer compiled_method
;
2132 gpointer runtime_invoke
;
2134 MonoDynCallInfo
*dyn_call_info
;
2135 MonoClass
*ret_box_class
;
2136 MonoMethodSignature
*sig
;
2137 gboolean gsharedvt_invoke
;
2138 gpointer
*wrapper_arg
;
2139 } RuntimeInvokeInfo
;
2141 static RuntimeInvokeInfo
*
2142 create_runtime_invoke_info (MonoDomain
*domain
, MonoMethod
*method
, gpointer compiled_method
, gboolean callee_gsharedvt
, MonoError
*error
)
2145 RuntimeInvokeInfo
*info
;
2147 info
= g_new0 (RuntimeInvokeInfo
, 1);
2148 info
->compiled_method
= compiled_method
;
2149 if (mono_llvm_only
&& method
->string_ctor
)
2150 info
->sig
= mono_marshal_get_string_ctor_signature (method
);
2152 info
->sig
= mono_method_signature (method
);
2154 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
2155 info
->vtable
= mono_class_vtable_full (domain
, method
->klass
, error
);
2156 if (!mono_error_ok (error
))
2158 g_assert (info
->vtable
);
2160 MonoMethodSignature
*sig
= info
->sig
;
2164 * We want to avoid AOTing 1000s of runtime-invoke wrappers when running
2165 * in full-aot mode, so we use a slower, but more generic wrapper if
2166 * possible, built on top of the OP_DYN_CALL opcode provided by the JIT.
2168 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2169 if (!mono_llvm_only
&& (mono_aot_only
|| debug_options
.dyn_runtime_invoke
)) {
2170 gboolean supported
= TRUE
;
2173 if (method
->string_ctor
)
2174 sig
= mono_marshal_get_string_ctor_signature (method
);
2176 for (i
= 0; i
< sig
->param_count
; ++i
) {
2177 MonoType
*t
= sig
->params
[i
];
2179 if (t
->byref
&& t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
)))
2183 if (mono_class_is_contextbound (method
->klass
) || !info
->compiled_method
)
2187 info
->dyn_call_info
= mono_arch_dyn_call_prepare (sig
);
2191 ret_type
= sig
->ret
;
2192 switch (ret_type
->type
) {
2193 case MONO_TYPE_VOID
:
2205 case MONO_TYPE_BOOLEAN
:
2206 case MONO_TYPE_CHAR
:
2209 info
->ret_box_class
= mono_class_from_mono_type (ret_type
);
2212 info
->ret_box_class
= mono_defaults
.int_class
;
2214 case MONO_TYPE_STRING
:
2215 case MONO_TYPE_CLASS
:
2216 case MONO_TYPE_ARRAY
:
2217 case MONO_TYPE_SZARRAY
:
2218 case MONO_TYPE_OBJECT
:
2220 case MONO_TYPE_GENERICINST
:
2221 if (!MONO_TYPE_IS_REFERENCE (ret_type
))
2222 info
->ret_box_class
= mono_class_from_mono_type (ret_type
);
2224 case MONO_TYPE_VALUETYPE
:
2225 info
->ret_box_class
= mono_class_from_mono_type (ret_type
);
2228 g_assert_not_reached ();
2232 if (!info
->dyn_call_info
) {
2233 if (mono_llvm_only
) {
2234 #ifndef MONO_ARCH_GSHAREDVT_SUPPORTED
2235 g_assert_not_reached ();
2237 info
->gsharedvt_invoke
= TRUE
;
2238 if (!callee_gsharedvt
) {
2239 /* Invoke a gsharedvt out wrapper instead */
2240 MonoMethod
*wrapper
= mini_get_gsharedvt_out_sig_wrapper (sig
);
2241 MonoMethodSignature
*wrapper_sig
= mini_get_gsharedvt_out_sig_wrapper_signature (sig
->hasthis
, sig
->ret
->type
!= MONO_TYPE_VOID
, sig
->param_count
);
2243 info
->wrapper_arg
= g_malloc0 (2 * sizeof (gpointer
));
2244 info
->wrapper_arg
[0] = mini_add_method_wrappers_llvmonly (method
, info
->compiled_method
, FALSE
, FALSE
, &(info
->wrapper_arg
[1]));
2246 /* Pass has_rgctx == TRUE since the wrapper has an extra arg */
2247 invoke
= mono_marshal_get_runtime_invoke_for_sig (wrapper_sig
);
2248 g_free (wrapper_sig
);
2250 info
->compiled_method
= mono_jit_compile_method (wrapper
, error
);
2251 if (!mono_error_ok (error
)) {
2256 /* Gsharedvt methods can be invoked the same way */
2257 /* The out wrapper has the same signature as the compiled gsharedvt method */
2258 MonoMethodSignature
*wrapper_sig
= mini_get_gsharedvt_out_sig_wrapper_signature (sig
->hasthis
, sig
->ret
->type
!= MONO_TYPE_VOID
, sig
->param_count
);
2260 info
->wrapper_arg
= mono_method_needs_static_rgctx_invoke (method
, TRUE
) ? mini_method_get_rgctx (method
) : NULL
;
2262 invoke
= mono_marshal_get_runtime_invoke_for_sig (wrapper_sig
);
2263 g_free (wrapper_sig
);
2266 info
->runtime_invoke
= mono_jit_compile_method (invoke
, error
);
2267 if (!mono_error_ok (error
)) {
2277 mono_llvmonly_runtime_invoke (MonoMethod
*method
, RuntimeInvokeInfo
*info
, void *obj
, void **params
, MonoObject
**exc
, MonoError
*error
)
2279 MonoMethodSignature
*sig
= info
->sig
;
2280 MonoDomain
*domain
= mono_domain_get ();
2281 MonoObject
*(*runtime_invoke
) (MonoObject
*this_obj
, void **params
, MonoObject
**exc
, void* compiled_method
);
2283 gpointer retval_ptr
;
2284 guint8 retval
[256];
2285 gpointer
*param_refs
;
2290 g_assert (info
->gsharedvt_invoke
);
2293 * Instead of invoking the method directly, we invoke a gsharedvt out wrapper.
2294 * The advantage of this is the gsharedvt out wrappers have a reduced set of
2295 * signatures, so we only have to generate runtime invoke wrappers for these
2297 * This code also handles invocation of gsharedvt methods directly, no
2298 * out wrappers are used in that case.
2300 args
= (void **)g_alloca ((sig
->param_count
+ sig
->hasthis
+ 2) * sizeof (gpointer
));
2301 param_refs
= (gpointer
*)g_alloca ((sig
->param_count
+ sig
->hasthis
+ 2) * sizeof (gpointer
));
2304 * The runtime invoke wrappers expects pointers to primitive types, so have to
2308 args
[pindex
++] = &obj
;
2309 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
2310 retval_ptr
= (gpointer
)&retval
;
2311 args
[pindex
++] = &retval_ptr
;
2313 for (i
= 0; i
< sig
->param_count
; ++i
) {
2314 MonoType
*t
= sig
->params
[i
];
2316 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
))) {
2317 MonoClass
*klass
= mono_class_from_mono_type (t
);
2318 guint8
*nullable_buf
;
2321 size
= mono_class_value_size (klass
, NULL
);
2322 nullable_buf
= g_alloca (size
);
2323 g_assert (nullable_buf
);
2325 /* The argument pointed to by params [i] is either a boxed vtype or null */
2326 mono_nullable_init (nullable_buf
, (MonoObject
*)params
[i
], klass
);
2327 params
[i
] = nullable_buf
;
2330 if (!t
->byref
&& (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
)) {
2331 param_refs
[i
] = params
[i
];
2332 params
[i
] = &(param_refs
[i
]);
2334 args
[pindex
++] = ¶ms
[i
];
2336 /* The gsharedvt out wrapper has an extra argument which contains the method to call */
2337 args
[pindex
++] = &info
->wrapper_arg
;
2339 runtime_invoke
= (MonoObject
*(*)(MonoObject
*, void **, MonoObject
**, void *))info
->runtime_invoke
;
2341 runtime_invoke (NULL
, args
, exc
, info
->compiled_method
);
2345 if (sig
->ret
->type
!= MONO_TYPE_VOID
&& info
->ret_box_class
)
2346 return mono_value_box_checked (domain
, info
->ret_box_class
, retval
, error
);
2348 return *(MonoObject
**)retval
;
2352 * mono_jit_runtime_invoke:
2353 * \param method: the method to invoke
2354 * \param obj: this pointer
2355 * \param params: array of parameter values.
2356 * \param exc: Set to the exception raised in the managed method.
2357 * \param error: error or caught exception object
2358 * If \p exc is NULL, \p error is thrown instead.
2359 * If coop is enabled, \p exc argument is ignored -
2360 * all exceptions are caught and propagated through \p error
2363 mono_jit_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
, MonoError
*error
)
2365 MonoMethod
*invoke
, *callee
;
2366 MonoObject
*(*runtime_invoke
) (MonoObject
*this_obj
, void **params
, MonoObject
**exc
, void* compiled_method
);
2367 MonoDomain
*domain
= mono_domain_get ();
2368 MonoJitDomainInfo
*domain_info
;
2369 RuntimeInvokeInfo
*info
, *info2
;
2370 MonoJitInfo
*ji
= NULL
;
2371 gboolean callee_gsharedvt
= FALSE
;
2373 #ifdef ENABLE_INTERPRETER
2374 if (mono_use_interpreter
)
2375 return mono_interp_runtime_invoke (method
, obj
, params
, exc
, error
);
2380 if (obj
== NULL
&& !(method
->flags
& METHOD_ATTRIBUTE_STATIC
) && !method
->string_ctor
&& (method
->wrapper_type
== 0)) {
2381 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
2385 domain_info
= domain_jit_info (domain
);
2387 info
= (RuntimeInvokeInfo
*)mono_conc_hashtable_lookup (domain_info
->runtime_invoke_hash
, method
);
2390 if (mono_security_core_clr_enabled ()) {
2392 * This might be redundant since mono_class_vtable () already does this,
2393 * but keep it just in case for moonlight.
2395 mono_class_setup_vtable (method
->klass
);
2396 if (mono_class_has_failure (method
->klass
)) {
2397 mono_error_set_for_class_failure (error
, method
->klass
);
2399 *exc
= (MonoObject
*)mono_class_get_exception_for_failure (method
->klass
);
2404 gpointer compiled_method
;
2407 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
2408 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
2410 * Array Get/Set/Address methods. The JIT implements them using inline code
2411 * inside the runtime invoke wrappers, so no need to compile them.
2413 if (mono_aot_only
) {
2415 * Call a wrapper, since the runtime invoke wrapper was not generated.
2417 MonoMethod
*wrapper
;
2419 wrapper
= mono_marshal_get_array_accessor_wrapper (method
);
2420 invoke
= mono_marshal_get_runtime_invoke (wrapper
, FALSE
);
2428 compiled_method
= mono_jit_compile_method (callee
, error
);
2429 if (!compiled_method
) {
2430 g_assert (!mono_error_ok (error
));
2434 if (mono_llvm_only
) {
2435 ji
= mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method
), NULL
);
2436 callee_gsharedvt
= mini_jit_info_is_gsharedvt (ji
);
2437 if (callee_gsharedvt
)
2438 callee_gsharedvt
= mini_is_gsharedvt_variable_signature (mono_method_signature (jinfo_get_method (ji
)));
2441 if (!callee_gsharedvt
)
2442 compiled_method
= mini_add_method_trampoline (callee
, compiled_method
, mono_method_needs_static_rgctx_invoke (callee
, TRUE
), FALSE
);
2444 compiled_method
= NULL
;
2447 info
= create_runtime_invoke_info (domain
, method
, compiled_method
, callee_gsharedvt
, error
);
2448 if (!mono_error_ok (error
))
2451 mono_domain_lock (domain
);
2452 info2
= (RuntimeInvokeInfo
*)mono_conc_hashtable_insert (domain_info
->runtime_invoke_hash
, method
, info
);
2453 mono_domain_unlock (domain
);
2461 * We need this here because mono_marshal_get_runtime_invoke can place
2462 * the helper method in System.Object and not the target class.
2464 if (!mono_runtime_class_init_full (info
->vtable
, error
)) {
2466 *exc
= (MonoObject
*) mono_error_convert_to_exception (error
);
2470 /* If coop is enabled, and the caller didn't ask for the exception to be caught separately,
2471 we always catch the exception and propagate it through the MonoError */
2472 gboolean catchExcInMonoError
=
2473 (exc
== NULL
) && mono_threads_is_coop_enabled ();
2474 MonoObject
*invoke_exc
= NULL
;
2475 if (catchExcInMonoError
)
2478 /* The wrappers expect this to be initialized to NULL */
2482 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2483 if (info
->dyn_call_info
) {
2484 MonoMethodSignature
*sig
= mono_method_signature (method
);
2486 static RuntimeInvokeDynamicFunction dyn_runtime_invoke
;
2489 guint8 retval
[256];
2491 if (!dyn_runtime_invoke
) {
2492 invoke
= mono_marshal_get_runtime_invoke_dynamic ();
2493 dyn_runtime_invoke
= (RuntimeInvokeDynamicFunction
)mono_jit_compile_method (invoke
, error
);
2494 if (!mono_error_ok (error
))
2498 /* Convert the arguments to the format expected by start_dyn_call () */
2499 args
= (void **)g_alloca ((sig
->param_count
+ sig
->hasthis
) * sizeof (gpointer
));
2502 args
[pindex
++] = &obj
;
2503 for (i
= 0; i
< sig
->param_count
; ++i
) {
2504 MonoType
*t
= sig
->params
[i
];
2507 args
[pindex
++] = ¶ms
[i
];
2508 } else if (MONO_TYPE_IS_REFERENCE (t
) || t
->type
== MONO_TYPE_PTR
) {
2509 args
[pindex
++] = ¶ms
[i
];
2511 args
[pindex
++] = params
[i
];
2515 //printf ("M: %s\n", mono_method_full_name (method, TRUE));
2517 mono_arch_start_dyn_call (info
->dyn_call_info
, (gpointer
**)args
, retval
, buf
, sizeof (buf
));
2519 dyn_runtime_invoke (buf
, exc
, info
->compiled_method
);
2520 mono_arch_finish_dyn_call (info
->dyn_call_info
, buf
);
2522 if (catchExcInMonoError
&& *exc
!= NULL
) {
2523 mono_error_set_exception_instance (error
, (MonoException
*) *exc
);
2527 if (info
->ret_box_class
)
2528 return mono_value_box_checked (domain
, info
->ret_box_class
, retval
, error
);
2530 return *(MonoObject
**)retval
;
2536 if (mono_llvm_only
) {
2537 result
= mono_llvmonly_runtime_invoke (method
, info
, obj
, params
, exc
, error
);
2541 runtime_invoke
= (MonoObject
*(*)(MonoObject
*, void **, MonoObject
**, void *))info
->runtime_invoke
;
2543 result
= runtime_invoke ((MonoObject
*)obj
, params
, exc
, info
->compiled_method
);
2545 if (catchExcInMonoError
&& *exc
!= NULL
)
2546 mono_error_set_exception_instance (error
, (MonoException
*) *exc
);
2555 typedef gpointer (*IMTTrampFunc
) (gpointer
*arg
, MonoMethod
*imt_method
);
2558 * mini_llvmonly_initial_imt_tramp:
2560 * This function is called the first time a call is made through an IMT trampoline.
2561 * It should have the same signature as the mono_llvmonly_imt_tramp_... functions.
2564 mini_llvmonly_initial_imt_tramp (gpointer
*arg
, MonoMethod
*imt_method
)
2566 IMTTrampInfo
*info
= (IMTTrampInfo
*)arg
;
2571 mono_vtable_build_imt_slot (info
->vtable
, info
->slot
);
2573 imt
= (gpointer
*)info
->vtable
;
2574 imt
-= MONO_IMT_SIZE
;
2576 /* Return what the real IMT trampoline returns */
2577 ftndesc
= imt
[info
->slot
];
2580 if (func
== (IMTTrampFunc
)mini_llvmonly_initial_imt_tramp
)
2581 /* Happens when the imt slot contains only a generic virtual method */
2583 return func ((gpointer
*)ftndesc
[1], imt_method
);
2586 /* This is called indirectly through an imt slot. */
2588 mono_llvmonly_imt_tramp (gpointer
*arg
, MonoMethod
*imt_method
)
2592 /* arg points to an array created in mono_llvmonly_get_imt_trampoline () */
2593 while (arg
[i
] && arg
[i
] != imt_method
)
2600 /* Optimized versions of mono_llvmonly_imt_trampoline () for different table sizes */
2602 mono_llvmonly_imt_tramp_1 (gpointer
*arg
, MonoMethod
*imt_method
)
2604 //g_assert (arg [0] == imt_method);
2609 mono_llvmonly_imt_tramp_2 (gpointer
*arg
, MonoMethod
*imt_method
)
2611 //g_assert (arg [0] == imt_method || arg [2] == imt_method);
2612 if (arg
[0] == imt_method
)
2619 mono_llvmonly_imt_tramp_3 (gpointer
*arg
, MonoMethod
*imt_method
)
2621 //g_assert (arg [0] == imt_method || arg [2] == imt_method || arg [4] == imt_method);
2622 if (arg
[0] == imt_method
)
2624 else if (arg
[2] == imt_method
)
2631 * A version of the imt trampoline used for generic virtual/variant iface methods.
2632 * Unlikely a normal imt trampoline, its possible that IMT_METHOD is not found
2633 * in the search table. The original JIT code had a 'fallback' trampoline it could
2634 * call, but we can't do that, so we just return NULL, and the compiled code
2638 mono_llvmonly_fallback_imt_tramp (gpointer
*arg
, MonoMethod
*imt_method
)
2642 while (arg
[i
] && arg
[i
] != imt_method
)
2651 mono_llvmonly_get_imt_trampoline (MonoVTable
*vtable
, MonoDomain
*domain
, MonoIMTCheckItem
**imt_entries
, int count
, gpointer fail_tramp
)
2655 int i
, index
, real_count
;
2656 gboolean virtual_generic
= FALSE
;
2659 * Create an array which is passed to the imt trampoline functions.
2660 * The array contains MonoMethod-function descriptor pairs, terminated by a NULL entry.
2664 for (i
= 0; i
< count
; ++i
) {
2665 MonoIMTCheckItem
*item
= imt_entries
[i
];
2667 if (item
->is_equals
)
2669 if (item
->has_target_code
)
2670 virtual_generic
= TRUE
;
2674 * Initialize all vtable entries reachable from this imt slot, so the compiled
2675 * code doesn't have to check it.
2677 for (i
= 0; i
< count
; ++i
) {
2678 MonoIMTCheckItem
*item
= imt_entries
[i
];
2681 if (!item
->is_equals
|| item
->has_target_code
)
2683 vt_slot
= item
->value
.vtable_slot
;
2684 mono_init_vtable_slot (vtable
, vt_slot
);
2687 /* Save the entries into an array */
2688 buf
= (void **)mono_domain_alloc (domain
, (real_count
+ 1) * 2 * sizeof (gpointer
));
2690 for (i
= 0; i
< count
; ++i
) {
2691 MonoIMTCheckItem
*item
= imt_entries
[i
];
2693 if (!item
->is_equals
)
2696 g_assert (item
->key
);
2697 buf
[(index
* 2)] = item
->key
;
2698 if (item
->has_target_code
)
2699 buf
[(index
* 2) + 1] = item
->value
.target_code
;
2701 buf
[(index
* 2) + 1] = vtable
->vtable
[item
->value
.vtable_slot
];
2704 buf
[(index
* 2)] = NULL
;
2705 buf
[(index
* 2) + 1] = fail_tramp
;
2708 * Return a function descriptor for a C function with 'buf' as its argument.
2709 * It will by called by JITted code.
2711 res
= (void **)mono_domain_alloc (domain
, 2 * sizeof (gpointer
));
2712 switch (real_count
) {
2714 res
[0] = mono_llvmonly_imt_tramp_1
;
2717 res
[0] = mono_llvmonly_imt_tramp_2
;
2720 res
[0] = mono_llvmonly_imt_tramp_3
;
2723 res
[0] = mono_llvmonly_imt_tramp
;
2726 if (virtual_generic
|| fail_tramp
)
2727 res
[0] = mono_llvmonly_fallback_imt_tramp
;
2733 MONO_SIG_HANDLER_FUNC (, mono_sigfpe_signal_handler
)
2735 MonoException
*exc
= NULL
;
2737 MONO_SIG_HANDLER_INFO_TYPE
*info
= MONO_SIG_HANDLER_GET_INFO ();
2738 MONO_SIG_HANDLER_GET_CONTEXT
;
2740 ji
= mono_jit_info_table_find_internal (mono_domain_get (), (char *)mono_arch_ip_from_context (ctx
), TRUE
, TRUE
);
2742 MONO_ENTER_GC_UNSAFE_UNBALANCED
;
2744 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
2745 if (mono_arch_is_int_overflow (ctx
, info
))
2747 * The spec says this throws ArithmeticException, but MS throws the derived
2748 * OverflowException.
2750 exc
= mono_get_exception_overflow ();
2752 exc
= mono_get_exception_divide_by_zero ();
2754 exc
= mono_get_exception_divide_by_zero ();
2758 if (!mono_do_crash_chaining
&& mono_chain_signal (MONO_SIG_HANDLER_PARAMS
))
2761 mono_handle_native_crash ("SIGFPE", ctx
, info
);
2762 if (mono_do_crash_chaining
) {
2763 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
2768 mono_arch_handle_exception (ctx
, exc
);
2771 MONO_EXIT_GC_UNSAFE_UNBALANCED
;
2774 MONO_SIG_HANDLER_FUNC (, mono_sigill_signal_handler
)
2776 MONO_SIG_HANDLER_INFO_TYPE
*info
= MONO_SIG_HANDLER_GET_INFO ();
2777 MONO_SIG_HANDLER_GET_CONTEXT
;
2779 if (mono_runtime_get_no_exec ())
2783 mono_handle_native_crash ("SIGILL", ctx
, info
);
2784 if (mono_do_crash_chaining
) {
2785 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
2789 g_assert_not_reached ();
2792 #if defined(MONO_ARCH_USE_SIGACTION) || defined(HOST_WIN32)
2793 #define HAVE_SIG_INFO
2796 MONO_SIG_HANDLER_FUNC (, mono_sigsegv_signal_handler
)
2799 MonoJitTlsData
*jit_tls
= (MonoJitTlsData
*)mono_tls_get_jit_tls ();
2800 gpointer fault_addr
= NULL
;
2801 #ifdef HAVE_SIG_INFO
2802 MONO_SIG_HANDLER_INFO_TYPE
*info
= MONO_SIG_HANDLER_GET_INFO ();
2806 MONO_SIG_HANDLER_GET_CONTEXT
;
2808 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && defined(HAVE_SIG_INFO)
2809 if (mono_arch_is_single_step_event (info
, ctx
)) {
2810 mono_debugger_agent_single_step_event (ctx
);
2812 } else if (mono_arch_is_breakpoint_event (info
, ctx
)) {
2813 mono_debugger_agent_breakpoint_hit (ctx
);
2818 #if defined(HAVE_SIG_INFO)
2819 #if !defined(HOST_WIN32)
2820 fault_addr
= info
->si_addr
;
2821 if (mono_aot_is_pagefault (info
->si_addr
)) {
2822 mono_aot_handle_pagefault (info
->si_addr
);
2827 /* The thread might no be registered with the runtime */
2828 if (!mono_domain_get () || !jit_tls
) {
2829 if (!mono_do_crash_chaining
&& mono_chain_signal (MONO_SIG_HANDLER_PARAMS
))
2831 mono_handle_native_crash ("SIGSEGV", ctx
, info
);
2832 if (mono_do_crash_chaining
) {
2833 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
2839 ji
= mono_jit_info_table_find_internal (mono_domain_get (), (char *)mono_arch_ip_from_context (ctx
), TRUE
, TRUE
);
2841 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2842 if (mono_handle_soft_stack_ovf (jit_tls
, ji
, ctx
, info
, (guint8
*)info
->si_addr
))
2845 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2846 /* info->si_addr seems to be NULL on some kernels when handling stack overflows */
2847 fault_addr
= info
->si_addr
;
2848 if (fault_addr
== NULL
) {
2851 mono_sigctx_to_monoctx (ctx
, &mctx
);
2853 fault_addr
= MONO_CONTEXT_GET_SP (&mctx
);
2857 if (jit_tls
->stack_size
&&
2858 ABS ((guint8
*)fault_addr
- ((guint8
*)jit_tls
->end_of_stack
- jit_tls
->stack_size
)) < 8192 * sizeof (gpointer
)) {
2860 * The hard-guard page has been hit: there is not much we can do anymore
2861 * Print a hopefully clear message and abort.
2863 mono_handle_hard_stack_ovf (jit_tls
, ji
, ctx
, (guint8
*)info
->si_addr
);
2864 g_assert_not_reached ();
2866 /* The original handler might not like that it is executed on an altstack... */
2867 if (!ji
&& mono_chain_signal (MONO_SIG_HANDLER_PARAMS
))
2870 mono_arch_handle_altstack_exception (ctx
, info
, info
->si_addr
, FALSE
);
2875 if (!mono_do_crash_chaining
&& mono_chain_signal (MONO_SIG_HANDLER_PARAMS
))
2878 mono_handle_native_crash ("SIGSEGV", ctx
, info
);
2880 if (mono_do_crash_chaining
) {
2881 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
2886 mono_arch_handle_exception (ctx
, NULL
);
2890 MONO_SIG_HANDLER_FUNC (, mono_sigint_signal_handler
)
2893 MONO_SIG_HANDLER_GET_CONTEXT
;
2895 MONO_ENTER_GC_UNSAFE_UNBALANCED
;
2897 exc
= mono_get_exception_execution_engine ("Interrupted (SIGINT).");
2899 mono_arch_handle_exception (ctx
, exc
);
2901 MONO_EXIT_GC_UNSAFE_UNBALANCED
;
2904 #ifndef DISABLE_REMOTING
2905 /* mono_jit_create_remoting_trampoline:
2906 * @method: pointer to the method info
2908 * Creates a trampoline which calls the remoting functions. This
2909 * is used in the vtable of transparent proxies.
2911 * Returns: a pointer to the newly created code
2914 mono_jit_create_remoting_trampoline (MonoDomain
*domain
, MonoMethod
*method
, MonoRemotingTarget target
, MonoError
*error
)
2917 guint8
*addr
= NULL
;
2921 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && mono_method_signature (method
)->generic_param_count
) {
2922 return mono_create_specific_trampoline (method
, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
,
2926 if ((method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
2927 (mono_method_signature (method
)->hasthis
&& (mono_class_is_marshalbyref (method
->klass
) || method
->klass
== mono_defaults
.object_class
)))
2928 nm
= mono_marshal_get_remoting_invoke_for_target (method
, target
);
2931 addr
= (guint8
*)mono_compile_method_checked (nm
, error
);
2932 return_val_if_nok (error
, NULL
);
2933 return mono_get_addr_from_ftnptr (addr
);
2937 static G_GNUC_UNUSED
void
2938 no_imt_trampoline (void)
2940 g_assert_not_reached ();
2943 static G_GNUC_UNUSED
void
2944 no_vcall_trampoline (void)
2946 g_assert_not_reached ();
2949 static gpointer
*vtable_trampolines
;
2950 static int vtable_trampolines_size
;
2953 mini_get_vtable_trampoline (MonoVTable
*vt
, int slot_index
)
2955 int index
= slot_index
+ MONO_IMT_SIZE
;
2957 if (mono_llvm_only
) {
2958 if (slot_index
< 0) {
2959 /* Initialize the IMT trampoline to a 'trampoline' so the generated code doesn't have to initialize it */
2960 // FIXME: Memory management
2961 gpointer
*ftndesc
= g_malloc (2 * sizeof (gpointer
));
2962 IMTTrampInfo
*info
= g_new0 (IMTTrampInfo
, 1);
2965 ftndesc
[0] = mini_llvmonly_initial_imt_tramp
;
2967 mono_memory_barrier ();
2974 g_assert (slot_index
>= - MONO_IMT_SIZE
);
2975 if (!vtable_trampolines
|| slot_index
+ MONO_IMT_SIZE
>= vtable_trampolines_size
) {
2977 if (!vtable_trampolines
|| index
>= vtable_trampolines_size
) {
2981 new_size
= vtable_trampolines_size
? vtable_trampolines_size
* 2 : 128;
2982 while (new_size
<= index
)
2984 new_table
= g_new0 (gpointer
, new_size
);
2986 if (vtable_trampolines
)
2987 memcpy (new_table
, vtable_trampolines
, vtable_trampolines_size
* sizeof (gpointer
));
2988 g_free (vtable_trampolines
);
2989 mono_memory_barrier ();
2990 vtable_trampolines
= (void **)new_table
;
2991 vtable_trampolines_size
= new_size
;
2996 if (!vtable_trampolines
[index
])
2997 vtable_trampolines
[index
] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot_index
), MONO_TRAMPOLINE_VCALL
, mono_get_root_domain (), NULL
);
2998 return vtable_trampolines
[index
];
3002 mini_get_imt_trampoline (MonoVTable
*vt
, int slot_index
)
3004 return mini_get_vtable_trampoline (vt
, slot_index
- MONO_IMT_SIZE
);
3008 mini_imt_entry_inited (MonoVTable
*vt
, int imt_slot_index
)
3013 gpointer
*imt
= (gpointer
*)vt
;
3014 imt
-= MONO_IMT_SIZE
;
3016 return (imt
[imt_slot_index
] != mini_get_imt_trampoline (vt
, imt_slot_index
));
3020 is_callee_gsharedvt_variable (gpointer addr
)
3023 gboolean callee_gsharedvt
;
3025 ji
= mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (addr
), NULL
);
3027 callee_gsharedvt
= mini_jit_info_is_gsharedvt (ji
);
3028 if (callee_gsharedvt
)
3029 callee_gsharedvt
= mini_is_gsharedvt_variable_signature (mono_method_signature (jinfo_get_method (ji
)));
3030 return callee_gsharedvt
;
3034 mini_get_delegate_arg (MonoMethod
*method
, gpointer method_ptr
)
3036 gpointer arg
= NULL
;
3038 if (mono_method_needs_static_rgctx_invoke (method
, FALSE
))
3039 arg
= mini_method_get_rgctx (method
);
3042 * Avoid adding gsharedvt in wrappers since they might not exist if
3043 * this delegate is called through a gsharedvt delegate invoke wrapper.
3044 * Instead, encode that the method is gsharedvt in del->extra_arg,
3045 * the CEE_MONO_CALLI_EXTRA_ARG implementation in the JIT depends on this.
3047 if (method
->is_inflated
&& is_callee_gsharedvt_variable (method_ptr
)) {
3048 g_assert ((((mgreg_t
)arg
) & 1) == 0);
3049 arg
= (gpointer
)(((mgreg_t
)arg
) | 1);
3055 mini_init_delegate (MonoDelegate
*del
)
3058 del
->extra_arg
= mini_get_delegate_arg (del
->method
, del
->method_ptr
);
3059 #ifdef ENABLE_INTERPRETER
3060 if (mono_use_interpreter
)
3061 mono_interp_init_delegate (del
);
3066 mono_get_delegate_virtual_invoke_impl_name (gboolean load_imt_reg
, int offset
)
3070 abs_offset
= offset
;
3072 abs_offset
= - abs_offset
;
3073 return g_strdup_printf ("delegate_virtual_invoke%s_%s%d", load_imt_reg
? "_imt" : "", offset
< 0 ? "m_" : "", abs_offset
/ SIZEOF_VOID_P
);
3077 mono_get_delegate_virtual_invoke_impl (MonoMethodSignature
*sig
, MonoMethod
*method
)
3079 gboolean is_virtual_generic
, is_interface
, load_imt_reg
;
3082 static guint8
**cache
= NULL
;
3083 static int cache_size
= 0;
3088 if (MONO_TYPE_ISSTRUCT (sig
->ret
))
3091 is_virtual_generic
= method
->is_inflated
&& mono_method_get_declaring_generic_method (method
)->is_generic
;
3092 is_interface
= mono_class_is_interface (method
->klass
);
3093 load_imt_reg
= is_virtual_generic
|| is_interface
;
3096 offset
= ((gint32
)mono_method_get_imt_slot (method
) - MONO_IMT_SIZE
) * SIZEOF_VOID_P
;
3098 offset
= G_STRUCT_OFFSET (MonoVTable
, vtable
) + ((mono_method_get_vtable_index (method
)) * (SIZEOF_VOID_P
));
3100 idx
= (offset
/ SIZEOF_VOID_P
+ MONO_IMT_SIZE
) * 2 + (load_imt_reg
? 1 : 0);
3101 g_assert (idx
>= 0);
3103 /* Resize the cache to idx + 1 */
3104 if (cache_size
< idx
+ 1) {
3106 if (cache_size
< idx
+ 1) {
3108 int new_cache_size
= idx
+ 1;
3110 new_cache
= g_new0 (guint8
*, new_cache_size
);
3112 memcpy (new_cache
, cache
, cache_size
* sizeof (guint8
*));
3115 mono_memory_barrier ();
3117 cache_size
= new_cache_size
;
3125 /* FIXME Support more cases */
3126 if (mono_aot_only
) {
3127 cache
[idx
] = (guint8
*)mono_aot_get_trampoline (mono_get_delegate_virtual_invoke_impl_name (load_imt_reg
, offset
));
3128 g_assert (cache
[idx
]);
3130 cache
[idx
] = (guint8
*)mono_arch_get_delegate_virtual_invoke_impl (sig
, method
, offset
, load_imt_reg
);
3136 * mini_parse_debug_option:
3137 * @option: The option to parse.
3139 * Parses debug options for the mono runtime. The options are the same as for
3140 * the MONO_DEBUG environment variable.
3144 mini_parse_debug_option (const char *option
)
3146 if (!strcmp (option
, "handle-sigint"))
3147 debug_options
.handle_sigint
= TRUE
;
3148 else if (!strcmp (option
, "keep-delegates"))
3149 debug_options
.keep_delegates
= TRUE
;
3150 else if (!strcmp (option
, "reverse-pinvoke-exceptions"))
3151 debug_options
.reverse_pinvoke_exceptions
= TRUE
;
3152 else if (!strcmp (option
, "collect-pagefault-stats"))
3153 debug_options
.collect_pagefault_stats
= TRUE
;
3154 else if (!strcmp (option
, "break-on-unverified"))
3155 debug_options
.break_on_unverified
= TRUE
;
3156 else if (!strcmp (option
, "no-gdb-backtrace"))
3157 debug_options
.no_gdb_backtrace
= TRUE
;
3158 else if (!strcmp (option
, "suspend-on-native-crash") || !strcmp (option
, "suspend-on-sigsegv"))
3159 debug_options
.suspend_on_native_crash
= TRUE
;
3160 else if (!strcmp (option
, "suspend-on-exception"))
3161 debug_options
.suspend_on_exception
= TRUE
;
3162 else if (!strcmp (option
, "suspend-on-unhandled"))
3163 debug_options
.suspend_on_unhandled
= TRUE
;
3164 else if (!strcmp (option
, "dont-free-domains"))
3165 mono_dont_free_domains
= TRUE
;
3166 else if (!strcmp (option
, "dyn-runtime-invoke"))
3167 debug_options
.dyn_runtime_invoke
= TRUE
;
3168 else if (!strcmp (option
, "gdb"))
3169 debug_options
.gdb
= TRUE
;
3170 else if (!strcmp (option
, "lldb"))
3171 debug_options
.lldb
= TRUE
;
3172 else if (!strcmp (option
, "explicit-null-checks"))
3173 debug_options
.explicit_null_checks
= TRUE
;
3174 else if (!strcmp (option
, "gen-seq-points"))
3175 debug_options
.gen_sdb_seq_points
= TRUE
;
3176 else if (!strcmp (option
, "gen-compact-seq-points"))
3177 fprintf (stderr
, "Mono Warning: option gen-compact-seq-points is deprecated.\n");
3178 else if (!strcmp (option
, "no-compact-seq-points"))
3179 debug_options
.no_seq_points_compact_data
= TRUE
;
3180 else if (!strcmp (option
, "single-imm-size"))
3181 debug_options
.single_imm_size
= TRUE
;
3182 else if (!strcmp (option
, "init-stacks"))
3183 debug_options
.init_stacks
= TRUE
;
3184 else if (!strcmp (option
, "casts"))
3185 debug_options
.better_cast_details
= TRUE
;
3186 else if (!strcmp (option
, "soft-breakpoints"))
3187 debug_options
.soft_breakpoints
= TRUE
;
3188 else if (!strcmp (option
, "check-pinvoke-callconv"))
3189 debug_options
.check_pinvoke_callconv
= TRUE
;
3190 else if (!strcmp (option
, "use-fallback-tls"))
3191 debug_options
.use_fallback_tls
= TRUE
;
3192 else if (!strcmp (option
, "debug-domain-unload"))
3193 mono_enable_debug_domain_unload (TRUE
);
3194 else if (!strcmp (option
, "partial-sharing"))
3195 mono_set_partial_sharing_supported (TRUE
);
3196 else if (!strcmp (option
, "align-small-structs"))
3197 mono_align_small_structs
= TRUE
;
3198 else if (!strcmp (option
, "native-debugger-break"))
3199 debug_options
.native_debugger_break
= TRUE
;
3200 else if (!strcmp (option
, "disable_omit_fp"))
3201 debug_options
.disable_omit_fp
= TRUE
;
3209 mini_parse_debug_options (void)
3211 const char *options
= g_getenv ("MONO_DEBUG");
3212 gchar
**args
, **ptr
;
3217 args
= g_strsplit (options
, ",", -1);
3219 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
3220 const char *arg
= *ptr
;
3222 if (!mini_parse_debug_option (arg
)) {
3223 fprintf (stderr
, "Invalid option for the MONO_DEBUG env variable: %s\n", arg
);
3224 fprintf (stderr
, "Available options: 'handle-sigint', 'keep-delegates', 'reverse-pinvoke-exceptions', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'suspend-on-native-crash', 'suspend-on-sigsegv', 'suspend-on-exception', 'suspend-on-unhandled', 'dont-free-domains', 'dyn-runtime-invoke', 'gdb', 'explicit-null-checks', 'gen-seq-points', 'no-compact-seq-points', 'single-imm-size', 'init-stacks', 'casts', 'soft-breakpoints', 'check-pinvoke-callconv', 'use-fallback-tls', 'debug-domain-unload', 'partial-sharing', 'align-small-structs', 'native-debugger-break'\n");
3233 mini_get_debug_options (void)
3235 return &debug_options
;
3239 mini_create_ftnptr (MonoDomain
*domain
, gpointer addr
)
3241 #if !defined(__ia64__) && (!defined(__ppc64__) && !defined(__powerpc64__) || _CALL_ELF == 2)
3244 gpointer
* desc
= NULL
;
3246 if ((desc
= g_hash_table_lookup (domain
->ftnptrs_hash
, addr
)))
3249 desc
= mono_domain_code_reserve (domain
, 2 * sizeof (gpointer
));
3253 # elif defined(__ppc64__) || defined(__powerpc64__)
3255 desc
= mono_domain_alloc0 (domain
, 3 * sizeof (gpointer
));
3261 g_hash_table_insert (domain
->ftnptrs_hash
, addr
, desc
);
3267 mini_get_addr_from_ftnptr (gpointer descr
)
3269 #if defined(__ia64__) || ((defined(__ppc64__) || defined(__powerpc64__)) && _CALL_ELF != 2)
3270 return *(gpointer
*)descr
;
3277 register_jit_stats (void)
3279 mono_counters_register ("Compiled methods", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_compiled
);
3280 mono_counters_register ("Methods from AOT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_aot
);
3281 mono_counters_register ("Methods JITted using mono JIT", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_without_llvm
);
3282 mono_counters_register ("Methods JITted using LLVM", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_with_llvm
);
3283 mono_counters_register ("JIT/method_to_ir (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_method_to_ir
);
3284 mono_counters_register ("JIT/liveness_handle_exception_clauses (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_liveness_handle_exception_clauses
);
3285 mono_counters_register ("JIT/handle_out_of_line_bblock (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_handle_out_of_line_bblock
);
3286 mono_counters_register ("JIT/decompose_long_opts (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_decompose_long_opts
);
3287 mono_counters_register ("JIT/decompose_typechecks (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_decompose_typechecks
);
3288 mono_counters_register ("JIT/local_cprop (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_cprop
);
3289 mono_counters_register ("JIT/local_emulate_ops (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_emulate_ops
);
3290 mono_counters_register ("JIT/optimize_branches (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_optimize_branches
);
3291 mono_counters_register ("JIT/handle_global_vregs (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_handle_global_vregs
);
3292 mono_counters_register ("JIT/local_deadce (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_deadce
);
3293 mono_counters_register ("JIT/local_alias_analysis (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_alias_analysis
);
3294 mono_counters_register ("JIT/if_conversion (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_if_conversion
);
3295 mono_counters_register ("JIT/bb_ordering (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_bb_ordering
);
3296 mono_counters_register ("JIT/compile_dominator_info (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_compile_dominator_info
);
3297 mono_counters_register ("JIT/compute_natural_loops (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_compute_natural_loops
);
3298 mono_counters_register ("JIT/insert_safepoints (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_insert_safepoints
);
3299 mono_counters_register ("JIT/ssa_compute (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_ssa_compute
);
3300 mono_counters_register ("JIT/ssa_cprop (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_ssa_cprop
);
3301 mono_counters_register ("JIT/ssa_deadce(sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_ssa_deadce
);
3302 mono_counters_register ("JIT/perform_abc_removal (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_perform_abc_removal
);
3303 mono_counters_register ("JIT/ssa_remove (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_ssa_remove
);
3304 mono_counters_register ("JIT/local_cprop2 (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_cprop2
);
3305 mono_counters_register ("JIT/handle_global_vregs2 (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_handle_global_vregs2
);
3306 mono_counters_register ("JIT/local_deadce2 (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_deadce2
);
3307 mono_counters_register ("JIT/optimize_branches2 (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_optimize_branches2
);
3308 mono_counters_register ("JIT/decompose_vtype_opts (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_decompose_vtype_opts
);
3309 mono_counters_register ("JIT/decompose_array_access_opts (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_decompose_array_access_opts
);
3310 mono_counters_register ("JIT/liveness_handle_exception_clauses2 (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_liveness_handle_exception_clauses2
);
3311 mono_counters_register ("JIT/analyze_liveness (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_analyze_liveness
);
3312 mono_counters_register ("JIT/linear_scan (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_linear_scan
);
3313 mono_counters_register ("JIT/arch_allocate_vars (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_arch_allocate_vars
);
3314 mono_counters_register ("JIT/spill_global_vars (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_spill_global_vars
);
3315 mono_counters_register ("JIT/local_cprop3 (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_cprop3
);
3316 mono_counters_register ("JIT/local_deadce3 (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_local_deadce3
);
3317 mono_counters_register ("JIT/codegen (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_codegen
);
3318 mono_counters_register ("JIT/create_jit_info (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_create_jit_info
);
3319 mono_counters_register ("JIT/gc_create_gc_map (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_gc_create_gc_map
);
3320 mono_counters_register ("JIT/save_seq_point_info (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_save_seq_point_info
);
3321 mono_counters_register ("Total time spent JITting (sec)", MONO_COUNTER_JIT
| MONO_COUNTER_DOUBLE
, &mono_jit_stats
.jit_time
);
3322 mono_counters_register ("Basic blocks", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.basic_blocks
);
3323 mono_counters_register ("Max basic blocks", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.max_basic_blocks
);
3324 mono_counters_register ("Allocated vars", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.allocate_var
);
3325 mono_counters_register ("Code reallocs", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.code_reallocs
);
3326 mono_counters_register ("Allocated code size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.allocated_code_size
);
3327 mono_counters_register ("Allocated seq points size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.allocated_seq_points_size
);
3328 mono_counters_register ("Inlineable methods", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.inlineable_methods
);
3329 mono_counters_register ("Inlined methods", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.inlined_methods
);
3330 mono_counters_register ("Regvars", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.regvars
);
3331 mono_counters_register ("Locals stack size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.locals_stack_size
);
3332 mono_counters_register ("Method cache lookups", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.methods_lookups
);
3333 mono_counters_register ("Compiled CIL code size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.cil_code_size
);
3334 mono_counters_register ("Native code size", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.native_code_size
);
3335 mono_counters_register ("Aliases found", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.alias_found
);
3336 mono_counters_register ("Aliases eliminated", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.alias_removed
);
3337 mono_counters_register ("Aliased loads eliminated", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.loads_eliminated
);
3338 mono_counters_register ("Aliased stores eliminated", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.stores_eliminated
);
3339 mono_counters_register ("Optimized immediate divisions", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &mono_jit_stats
.optimized_divisions
);
3342 static void runtime_invoke_info_free (gpointer value
);
3345 class_method_pair_equal (gconstpointer ka
, gconstpointer kb
)
3347 const MonoClassMethodPair
*apair
= (const MonoClassMethodPair
*)ka
;
3348 const MonoClassMethodPair
*bpair
= (const MonoClassMethodPair
*)kb
;
3350 return apair
->klass
== bpair
->klass
&& apair
->method
== bpair
->method
? 1 : 0;
3354 class_method_pair_hash (gconstpointer data
)
3356 const MonoClassMethodPair
*pair
= (const MonoClassMethodPair
*)data
;
3358 return (gsize
)pair
->klass
^ (gsize
)pair
->method
;
3362 mini_create_jit_domain_info (MonoDomain
*domain
)
3364 MonoJitDomainInfo
*info
= g_new0 (MonoJitDomainInfo
, 1);
3366 info
->jump_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3367 info
->jit_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3368 info
->delegate_trampoline_hash
= g_hash_table_new (class_method_pair_hash
, class_method_pair_equal
);
3369 info
->llvm_vcall_trampoline_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3370 info
->runtime_invoke_hash
= mono_conc_hashtable_new_full (mono_aligned_addr_hash
, NULL
, NULL
, runtime_invoke_info_free
);
3371 info
->seq_points
= g_hash_table_new_full (mono_aligned_addr_hash
, NULL
, NULL
, mono_seq_point_info_free
);
3372 info
->arch_seq_points
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3373 info
->jump_target_hash
= g_hash_table_new (NULL
, NULL
);
3374 mono_jit_code_hash_init (&info
->interp_code_hash
);
3376 domain
->runtime_info
= info
;
3380 delete_jump_list (gpointer key
, gpointer value
, gpointer user_data
)
3382 MonoJumpList
*jlist
= (MonoJumpList
*)value
;
3383 g_slist_free (jlist
->list
);
3387 delete_got_slot_list (gpointer key
, gpointer value
, gpointer user_data
)
3389 GSList
*list
= (GSList
*)value
;
3390 g_slist_free (list
);
3394 dynamic_method_info_free (gpointer key
, gpointer value
, gpointer user_data
)
3396 MonoJitDynamicMethodInfo
*di
= (MonoJitDynamicMethodInfo
*)value
;
3397 mono_code_manager_destroy (di
->code_mp
);
3402 runtime_invoke_info_free (gpointer value
)
3404 RuntimeInvokeInfo
*info
= (RuntimeInvokeInfo
*)value
;
3406 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
3407 if (info
->dyn_call_info
)
3408 mono_arch_dyn_call_free (info
->dyn_call_info
);
3414 free_jit_callee_list (gpointer key
, gpointer value
, gpointer user_data
)
3416 g_slist_free (value
);
3420 mini_free_jit_domain_info (MonoDomain
*domain
)
3422 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
3424 g_hash_table_foreach (info
->jump_target_hash
, delete_jump_list
, NULL
);
3425 g_hash_table_destroy (info
->jump_target_hash
);
3426 if (info
->jump_target_got_slot_hash
) {
3427 g_hash_table_foreach (info
->jump_target_got_slot_hash
, delete_got_slot_list
, NULL
);
3428 g_hash_table_destroy (info
->jump_target_got_slot_hash
);
3430 if (info
->dynamic_code_hash
) {
3431 g_hash_table_foreach (info
->dynamic_code_hash
, dynamic_method_info_free
, NULL
);
3432 g_hash_table_destroy (info
->dynamic_code_hash
);
3434 if (info
->method_code_hash
)
3435 g_hash_table_destroy (info
->method_code_hash
);
3436 g_hash_table_destroy (info
->jump_trampoline_hash
);
3437 g_hash_table_destroy (info
->jit_trampoline_hash
);
3438 g_hash_table_destroy (info
->delegate_trampoline_hash
);
3439 if (info
->static_rgctx_trampoline_hash
)
3440 g_hash_table_destroy (info
->static_rgctx_trampoline_hash
);
3441 g_hash_table_destroy (info
->llvm_vcall_trampoline_hash
);
3442 mono_conc_hashtable_destroy (info
->runtime_invoke_hash
);
3443 g_hash_table_destroy (info
->seq_points
);
3444 g_hash_table_destroy (info
->arch_seq_points
);
3445 if (info
->agent_info
)
3446 mono_debugger_agent_free_domain_info (domain
);
3447 if (info
->gsharedvt_arg_tramp_hash
)
3448 g_hash_table_destroy (info
->gsharedvt_arg_tramp_hash
);
3449 if (info
->llvm_jit_callees
) {
3450 g_hash_table_foreach (info
->llvm_jit_callees
, free_jit_callee_list
, NULL
);
3451 g_hash_table_destroy (info
->llvm_jit_callees
);
3454 mono_llvm_free_domain_info (domain
);
3457 g_free (domain
->runtime_info
);
3458 domain
->runtime_info
= NULL
;
3461 #ifdef MONO_ARCH_HAVE_CODE_CHUNK_TRACKING
3464 code_manager_chunk_new (void *chunk
, int size
)
3466 mono_arch_code_chunk_new (chunk
, size
);
3470 code_manager_chunk_destroy (void *chunk
)
3472 mono_arch_code_chunk_destroy (chunk
);
3479 llvm_init_inner (void)
3481 if (!mono_llvm_load (NULL
))
3492 * Load and initialize LLVM support.
3493 * Return TRUE on success.
3496 mini_llvm_init (void)
3499 static gboolean llvm_inited
;
3500 static gboolean init_result
;
3502 mono_loader_lock_if_inited ();
3504 init_result
= llvm_init_inner ();
3507 mono_loader_unlock_if_inited ();
3515 mini_profiler_enable_with_options (const char* profile_options
)
3517 mini_enable_profiler
= TRUE
;
3518 mini_profiler_options
= g_strdup (profile_options
);
3522 mini_init (const char *filename
, const char *runtime_version
)
3526 MonoRuntimeCallbacks callbacks
;
3527 MonoThreadInfoRuntimeCallbacks ticallbacks
;
3528 MonoCodeManagerCallbacks code_manager_callbacks
;
3530 MONO_VES_INIT_BEGIN ();
3532 CHECKED_MONO_INIT ();
3534 #if defined(__linux__) && !defined(__native_client__)
3535 if (access ("/proc/self/maps", F_OK
) != 0) {
3536 g_print ("Mono requires /proc to be mounted.\n");
3541 mono_os_mutex_init_recursive (&jit_mutex
);
3543 mono_cross_helpers_run ();
3545 mono_counters_init ();
3549 /* Happens when using the embedding interface */
3550 if (!default_opt_set
)
3551 default_opt
= mono_parse_default_optimizations (NULL
);
3553 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
3555 mono_set_generic_sharing_vt_supported (TRUE
);
3558 mono_set_generic_sharing_vt_supported (TRUE
);
3561 mono_tls_init_runtime_keys ();
3563 if (!global_codeman
)
3564 global_codeman
= mono_code_manager_new ();
3566 memset (&callbacks
, 0, sizeof (callbacks
));
3567 callbacks
.create_ftnptr
= mini_create_ftnptr
;
3568 callbacks
.get_addr_from_ftnptr
= mini_get_addr_from_ftnptr
;
3569 callbacks
.get_runtime_build_info
= mono_get_runtime_build_info
;
3570 callbacks
.set_cast_details
= mono_set_cast_details
;
3571 callbacks
.debug_log
= mono_debugger_agent_debug_log
;
3572 callbacks
.debug_log_is_enabled
= mono_debugger_agent_debug_log_is_enabled
;
3573 callbacks
.get_vtable_trampoline
= mini_get_vtable_trampoline
;
3574 callbacks
.get_imt_trampoline
= mini_get_imt_trampoline
;
3575 callbacks
.imt_entry_inited
= mini_imt_entry_inited
;
3576 callbacks
.init_delegate
= mini_init_delegate
;
3577 #define JIT_INVOKE_WORKS
3578 #ifdef JIT_INVOKE_WORKS
3579 callbacks
.runtime_invoke
= mono_jit_runtime_invoke
;
3581 #define JIT_TRAMPOLINES_WORK
3582 #ifdef JIT_TRAMPOLINES_WORK
3583 callbacks
.compile_method
= mono_jit_compile_method
;
3584 callbacks
.create_jump_trampoline
= mono_create_jump_trampoline
;
3585 callbacks
.create_jit_trampoline
= mono_create_jit_trampoline
;
3586 callbacks
.create_delegate_trampoline
= mono_create_delegate_trampoline
;
3587 callbacks
.free_method
= mono_jit_free_method
;
3588 #ifndef DISABLE_REMOTING
3589 callbacks
.create_remoting_trampoline
= mono_jit_create_remoting_trampoline
;
3593 mono_install_callbacks (&callbacks
);
3595 memset (&ticallbacks
, 0, sizeof (ticallbacks
));
3596 ticallbacks
.setup_async_callback
= mono_setup_async_callback
;
3597 ticallbacks
.thread_state_init_from_sigctx
= mono_thread_state_init_from_sigctx
;
3598 ticallbacks
.thread_state_init_from_handle
= mono_thread_state_init_from_handle
;
3599 ticallbacks
.thread_state_init
= mono_thread_state_init
;
3602 mono_w32handle_init ();
3605 mono_threads_runtime_init (&ticallbacks
);
3607 if (g_getenv ("MONO_DEBUG") != NULL
)
3608 mini_parse_debug_options ();
3610 mono_code_manager_init ();
3612 memset (&code_manager_callbacks
, 0, sizeof (code_manager_callbacks
));
3613 #ifdef MONO_ARCH_HAVE_CODE_CHUNK_TRACKING
3614 code_manager_callbacks
.chunk_new
= code_manager_chunk_new
;
3615 code_manager_callbacks
.chunk_destroy
= code_manager_chunk_destroy
;
3617 mono_code_manager_install_callbacks (&code_manager_callbacks
);
3621 mono_arch_cpu_init ();
3625 mono_unwind_init ();
3627 if (mini_get_debug_options ()->lldb
|| g_getenv ("MONO_LLDB")) {
3628 mono_lldb_init ("");
3629 mono_dont_free_domains
= TRUE
;
3632 #ifdef XDEBUG_ENABLED
3633 if (g_getenv ("MONO_XDEBUG")) {
3634 const char *xdebug_opts
= g_getenv ("MONO_XDEBUG");
3635 mono_xdebug_init (xdebug_opts
);
3636 /* So methods for multiple domains don't have the same address */
3637 mono_dont_free_domains
= TRUE
;
3638 mono_using_xdebug
= TRUE
;
3639 } else if (mini_get_debug_options ()->gdb
) {
3640 mono_xdebug_init ((char*)"gdb");
3641 mono_dont_free_domains
= TRUE
;
3642 mono_using_xdebug
= TRUE
;
3647 if (mono_use_llvm
) {
3648 if (!mono_llvm_load (NULL
)) {
3649 mono_use_llvm
= FALSE
;
3650 fprintf (stderr
, "Mono Warning: llvm support could not be loaded.\n");
3657 mono_trampolines_init ();
3659 if (default_opt
& MONO_OPT_AOT
)
3662 mono_debugger_agent_init ();
3664 #ifdef MONO_ARCH_GSHARED_SUPPORTED
3665 mono_set_generic_sharing_supported (TRUE
);
3668 mono_threads_signals_init ();
3670 #ifndef MONO_CROSS_COMPILE
3671 mono_runtime_install_handlers ();
3673 mono_threads_install_cleanup (mini_thread_cleanup
);
3675 #ifdef JIT_TRAMPOLINES_WORK
3676 mono_install_create_domain_hook (mini_create_jit_domain_info
);
3677 mono_install_free_domain_hook (mini_free_jit_domain_info
);
3679 mono_install_get_cached_class_info (mono_aot_get_cached_class_info
);
3680 mono_install_get_class_from_name (mono_aot_get_class_from_name
);
3681 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info
);
3683 if (mini_profiler_enabled ()) {
3684 mono_profiler_load (mini_profiler_get_options ());
3685 mono_profiler_thread_name (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ()), "Main");
3688 if (debug_options
.collect_pagefault_stats
)
3689 mono_aot_set_make_unreadable (TRUE
);
3691 if (runtime_version
)
3692 domain
= mono_init_version (filename
, runtime_version
);
3694 domain
= mono_init_from_assembly (filename
, filename
);
3696 if (mono_aot_only
) {
3697 /* This helps catch code allocation requests */
3698 mono_code_manager_set_read_only (domain
->code_mp
);
3699 mono_marshal_use_aot_wrappers (TRUE
);
3702 if (mono_llvm_only
) {
3703 mono_install_imt_trampoline_builder (mono_llvmonly_get_imt_trampoline
);
3704 mono_set_always_build_imt_trampolines (TRUE
);
3705 } else if (mono_aot_only
) {
3706 mono_install_imt_trampoline_builder (mono_aot_get_imt_trampoline
);
3708 mono_install_imt_trampoline_builder (mono_arch_build_imt_trampoline
);
3711 /*Init arch tls information only after the metadata side is inited to make sure we see dynamic appdomain tls keys*/
3712 mono_arch_finish_init ();
3716 /* This must come after mono_init () in the aot-only case */
3717 mono_exceptions_init ();
3719 /* This should come after mono_init () too */
3723 mono_create_helper_signatures ();
3726 register_jit_stats ();
3728 #define JIT_CALLS_WORK
3729 #ifdef JIT_CALLS_WORK
3730 /* Needs to be called here since register_jit_icall depends on it */
3731 mono_marshal_init ();
3733 mono_arch_register_lowlevel_calls ();
3737 mono_generic_sharing_init ();
3740 #ifdef MONO_ARCH_SIMD_INTRINSICS
3741 mono_simd_intrinsics_init ();
3744 mono_tasklets_init ();
3746 register_trampolines (domain
);
3748 if (mono_compile_aot
)
3750 * Avoid running managed code when AOT compiling, since the platform
3751 * might only support aot-only execution.
3753 mono_runtime_set_no_exec (TRUE
);
3755 mono_mem_account_register_counters ();
3757 #define JIT_RUNTIME_WORKS
3758 #ifdef JIT_RUNTIME_WORKS
3759 mono_install_runtime_cleanup ((MonoDomainFunc
)mini_cleanup
);
3760 mono_runtime_init_checked (domain
, mono_thread_start_cb
, mono_thread_attach_cb
, &error
);
3761 mono_error_assert_ok (&error
);
3762 mono_thread_attach (domain
);
3765 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL
)
3766 mono_runtime_setup_stat_profiler ();
3768 mono_profiler_runtime_initialized ();
3770 MONO_VES_INIT_END ();
3776 register_icalls (void)
3778 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
3779 ves_icall_get_frame_info
);
3780 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
3781 ves_icall_get_trace
);
3782 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
3783 mono_runtime_install_handlers
);
3784 mono_add_internal_call ("Mono.Runtime::mono_runtime_cleanup_handlers",
3785 mono_runtime_cleanup_handlers
);
3787 #if defined(PLATFORM_ANDROID) || defined(TARGET_ANDROID)
3788 mono_add_internal_call ("System.Diagnostics.Debugger::Mono_UnhandledException_internal",
3789 mono_debugger_agent_unhandled_exception
);
3793 * It's important that we pass `TRUE` as the last argument here, as
3794 * it causes the JIT to omit a wrapper for these icalls. If the JIT
3795 * *did* emit a wrapper, we'd be looking at infinite recursion since
3796 * the wrapper would call the icall which would call the wrapper and
3799 register_icall (mono_profiler_method_enter
, "mono_profiler_method_enter", "void ptr", TRUE
);
3800 register_icall (mono_profiler_method_leave
, "mono_profiler_method_leave", "void ptr", TRUE
);
3802 register_icall (mono_trace_enter_method
, "mono_trace_enter_method", NULL
, TRUE
);
3803 register_icall (mono_trace_leave_method
, "mono_trace_leave_method", NULL
, TRUE
);
3804 register_icall (mono_get_lmf_addr
, "mono_get_lmf_addr", "ptr", TRUE
);
3805 register_icall (mono_jit_thread_attach
, "mono_jit_thread_attach", "ptr ptr", TRUE
);
3806 register_icall (mono_jit_set_domain
, "mono_jit_set_domain", "void ptr", TRUE
);
3807 register_icall (mono_domain_get
, "mono_domain_get", "ptr", TRUE
);
3809 register_icall (mono_llvm_throw_exception
, "mono_llvm_throw_exception", "void object", TRUE
);
3810 register_icall (mono_llvm_rethrow_exception
, "mono_llvm_rethrow_exception", "void object", TRUE
);
3811 register_icall (mono_llvm_resume_exception
, "mono_llvm_resume_exception", "void", TRUE
);
3812 register_icall (mono_llvm_match_exception
, "mono_llvm_match_exception", "int ptr int int", TRUE
);
3813 register_icall (mono_llvm_clear_exception
, "mono_llvm_clear_exception", NULL
, TRUE
);
3814 register_icall (mono_llvm_load_exception
, "mono_llvm_load_exception", "object", TRUE
);
3815 register_icall (mono_llvm_throw_corlib_exception
, "mono_llvm_throw_corlib_exception", "void int", TRUE
);
3816 #if defined(ENABLE_LLVM) && !defined(MONO_LLVM_LOADED)
3817 register_icall (mono_llvm_set_unhandled_exception_handler
, "mono_llvm_set_unhandled_exception_handler", NULL
, TRUE
);
3819 // FIXME: This is broken
3820 register_icall (mono_debug_personality
, "mono_debug_personality", "int int int ptr ptr ptr", TRUE
);
3823 register_dyn_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE
);
3824 register_dyn_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE
);
3825 register_dyn_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception", "void ptr", TRUE
);
3826 register_icall (mono_thread_get_undeniable_exception
, "mono_thread_get_undeniable_exception", "object", FALSE
);
3827 register_icall (mono_thread_interruption_checkpoint
, "mono_thread_interruption_checkpoint", "object", FALSE
);
3828 register_icall (mono_thread_force_interruption_checkpoint_noraise
, "mono_thread_force_interruption_checkpoint_noraise", "object", FALSE
);
3830 #if defined(__native_client__) || defined(__native_client_codegen__)
3831 register_icall (mono_nacl_gc
, "mono_nacl_gc", "void", FALSE
);
3834 if (mono_threads_is_coop_enabled ())
3835 register_icall (mono_threads_state_poll
, "mono_threads_state_poll", "void", FALSE
);
3837 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
3838 register_opcode_emulation (OP_LMUL
, "__emul_lmul", "long long long", mono_llmult
, "mono_llmult", FALSE
);
3839 register_opcode_emulation (OP_LDIV
, "__emul_ldiv", "long long long", mono_lldiv
, "mono_lldiv", FALSE
);
3840 register_opcode_emulation (OP_LDIV_UN
, "__emul_ldiv_un", "long long long", mono_lldiv_un
, "mono_lldiv_un", FALSE
);
3841 register_opcode_emulation (OP_LREM
, "__emul_lrem", "long long long", mono_llrem
, "mono_llrem", FALSE
);
3842 register_opcode_emulation (OP_LREM_UN
, "__emul_lrem_un", "long long long", mono_llrem_un
, "mono_llrem_un", FALSE
);
3844 #if !defined(MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS) || defined(MONO_ARCH_EMULATE_LONG_MUL_OVF_OPTS)
3845 register_opcode_emulation (OP_LMUL_OVF_UN
, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un
, "mono_llmult_ovf_un", FALSE
);
3846 register_opcode_emulation (OP_LMUL_OVF
, "__emul_lmul_ovf", "long long long", mono_llmult_ovf
, "mono_llmult_ovf", FALSE
);
3849 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
3850 register_opcode_emulation (OP_LSHL
, "__emul_lshl", "long long int32", mono_lshl
, "mono_lshl", TRUE
);
3851 register_opcode_emulation (OP_LSHR
, "__emul_lshr", "long long int32", mono_lshr
, "mono_lshr", TRUE
);
3852 register_opcode_emulation (OP_LSHR_UN
, "__emul_lshr_un", "long long int32", mono_lshr_un
, "mono_lshr_un", TRUE
);
3855 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
3856 register_opcode_emulation (OP_IDIV
, "__emul_op_idiv", "int32 int32 int32", mono_idiv
, "mono_idiv", FALSE
);
3857 register_opcode_emulation (OP_IDIV_UN
, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un
, "mono_idiv_un", FALSE
);
3858 register_opcode_emulation (OP_IREM
, "__emul_op_irem", "int32 int32 int32", mono_irem
, "mono_irem", FALSE
);
3859 register_opcode_emulation (OP_IREM_UN
, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un
, "mono_irem_un", FALSE
);
3862 #ifdef MONO_ARCH_EMULATE_MUL_DIV
3863 register_opcode_emulation (OP_IMUL
, "__emul_op_imul", "int32 int32 int32", mono_imul
, "mono_imul", TRUE
);
3866 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_MUL_OVF)
3867 register_opcode_emulation (OP_IMUL_OVF
, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf
, "mono_imul_ovf", FALSE
);
3868 register_opcode_emulation (OP_IMUL_OVF_UN
, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un
, "mono_imul_ovf_un", FALSE
);
3871 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT_FALLBACK)
3872 register_opcode_emulation (OP_FDIV
, "__emul_fdiv", "double double double", mono_fdiv
, "mono_fdiv", FALSE
);
3875 register_opcode_emulation (OP_FCONV_TO_U8
, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8
, "mono_fconv_u8", FALSE
);
3876 register_opcode_emulation (OP_RCONV_TO_U8
, "__emul_rconv_to_u8", "ulong float", mono_rconv_u8
, "mono_rconv_u8", FALSE
);
3877 register_opcode_emulation (OP_FCONV_TO_U4
, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4
, "mono_fconv_u4", FALSE
);
3878 register_opcode_emulation (OP_FCONV_TO_OVF_I8
, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8
, "mono_fconv_ovf_i8", FALSE
);
3879 register_opcode_emulation (OP_FCONV_TO_OVF_U8
, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8
, "mono_fconv_ovf_u8", FALSE
);
3880 register_opcode_emulation (OP_RCONV_TO_OVF_I8
, "__emul_rconv_to_ovf_i8", "long float", mono_rconv_ovf_i8
, "mono_rconv_ovf_i8", FALSE
);
3881 register_opcode_emulation (OP_RCONV_TO_OVF_U8
, "__emul_rconv_to_ovf_u8", "ulong float", mono_rconv_ovf_u8
, "mono_rconv_ovf_u8", FALSE
);
3884 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
3885 register_opcode_emulation (OP_FCONV_TO_I8
, "__emul_fconv_to_i8", "long double", mono_fconv_i8
, "mono_fconv_i8", FALSE
);
3886 register_opcode_emulation (OP_RCONV_TO_I8
, "__emul_rconv_to_i8", "long float", mono_rconv_i8
, "mono_rconv_i8", FALSE
);
3889 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
3890 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
);
3892 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
3893 register_opcode_emulation (OP_LCONV_TO_R8
, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8
, "mono_lconv_to_r8", FALSE
);
3895 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
3896 register_opcode_emulation (OP_LCONV_TO_R4
, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4
, "mono_lconv_to_r4", FALSE
);
3898 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
3899 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
);
3901 #ifdef MONO_ARCH_EMULATE_FREM
3902 #if !defined(__native_client__)
3903 register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", fmod
, "fmod", FALSE
);
3904 register_opcode_emulation (OP_RREM
, "__emul_rrem", "float float float", fmodf
, "fmodf", FALSE
);
3906 register_opcode_emulation (OP_FREM
, "__emul_frem", "double double double", mono_fmod
, "mono_fmod", FALSE
);
3910 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
3911 if (mono_arch_is_soft_float ()) {
3912 register_opcode_emulation (OP_FSUB
, "__emul_fsub", "double double double", mono_fsub
, "mono_fsub", FALSE
);
3913 register_opcode_emulation (OP_FADD
, "__emul_fadd", "double double double", mono_fadd
, "mono_fadd", FALSE
);
3914 register_opcode_emulation (OP_FMUL
, "__emul_fmul", "double double double", mono_fmul
, "mono_fmul", FALSE
);
3915 register_opcode_emulation (OP_FNEG
, "__emul_fneg", "double double", mono_fneg
, "mono_fneg", FALSE
);
3916 register_opcode_emulation (OP_ICONV_TO_R8
, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8
, "mono_conv_to_r8", FALSE
);
3917 register_opcode_emulation (OP_ICONV_TO_R4
, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4
, "mono_conv_to_r4", FALSE
);
3918 register_opcode_emulation (OP_FCONV_TO_R4
, "__emul_fconv_to_r4", "double double", mono_fconv_r4
, "mono_fconv_r4", FALSE
);
3919 register_opcode_emulation (OP_FCONV_TO_I1
, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1
, "mono_fconv_i1", FALSE
);
3920 register_opcode_emulation (OP_FCONV_TO_I2
, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2
, "mono_fconv_i2", FALSE
);
3921 register_opcode_emulation (OP_FCONV_TO_I4
, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4
, "mono_fconv_i4", FALSE
);
3922 register_opcode_emulation (OP_FCONV_TO_U1
, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1
, "mono_fconv_u1", FALSE
);
3923 register_opcode_emulation (OP_FCONV_TO_U2
, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2
, "mono_fconv_u2", FALSE
);
3925 #if SIZEOF_VOID_P == 4
3926 register_opcode_emulation (OP_FCONV_TO_I
, "__emul_fconv_to_i", "int32 double", mono_fconv_i4
, "mono_fconv_i4", FALSE
);
3929 register_opcode_emulation (OP_FBEQ
, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq
, "mono_fcmp_eq", FALSE
);
3930 register_opcode_emulation (OP_FBLT
, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt
, "mono_fcmp_lt", FALSE
);
3931 register_opcode_emulation (OP_FBGT
, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt
, "mono_fcmp_gt", FALSE
);
3932 register_opcode_emulation (OP_FBLE
, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le
, "mono_fcmp_le", FALSE
);
3933 register_opcode_emulation (OP_FBGE
, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge
, "mono_fcmp_ge", FALSE
);
3934 register_opcode_emulation (OP_FBNE_UN
, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un
, "mono_fcmp_ne_un", FALSE
);
3935 register_opcode_emulation (OP_FBLT_UN
, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un
, "mono_fcmp_lt_un", FALSE
);
3936 register_opcode_emulation (OP_FBGT_UN
, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un
, "mono_fcmp_gt_un", FALSE
);
3937 register_opcode_emulation (OP_FBLE_UN
, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un
, "mono_fcmp_le_un", FALSE
);
3938 register_opcode_emulation (OP_FBGE_UN
, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un
, "mono_fcmp_ge_un", FALSE
);
3940 register_opcode_emulation (OP_FCEQ
, "__emul_fcmp_ceq", "uint32 double double", mono_fceq
, "mono_fceq", FALSE
);
3941 register_opcode_emulation (OP_FCGT
, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt
, "mono_fcgt", FALSE
);
3942 register_opcode_emulation (OP_FCGT_UN
, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un
, "mono_fcgt_un", FALSE
);
3943 register_opcode_emulation (OP_FCLT
, "__emul_fcmp_clt", "uint32 double double", mono_fclt
, "mono_fclt", FALSE
);
3944 register_opcode_emulation (OP_FCLT_UN
, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un
, "mono_fclt_un", FALSE
);
3946 register_icall (mono_fload_r4
, "mono_fload_r4", "double ptr", FALSE
);
3947 register_icall (mono_fstore_r4
, "mono_fstore_r4", "void double ptr", FALSE
);
3948 register_icall (mono_fload_r4_arg
, "mono_fload_r4_arg", "uint32 double", FALSE
);
3949 register_icall (mono_isfinite
, "mono_isfinite", "uint32 double", FALSE
);
3952 register_icall (mono_ckfinite
, "mono_ckfinite", "double double", FALSE
);
3954 #ifdef COMPRESSED_INTERFACE_BITMAP
3955 register_icall (mono_class_interface_match
, "mono_class_interface_match", "uint32 ptr int32", TRUE
);
3958 #if SIZEOF_REGISTER == 4
3959 register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4
, "mono_fconv_u4", TRUE
);
3961 register_opcode_emulation (OP_FCONV_TO_U
, "__emul_fconv_to_u", "ulong double", mono_fconv_u8
, "mono_fconv_u8", TRUE
);
3964 /* other jit icalls */
3965 register_icall (ves_icall_mono_delegate_ctor
, "ves_icall_mono_delegate_ctor", "void object object ptr", FALSE
);
3966 register_icall (mono_class_static_field_address
, "mono_class_static_field_address",
3967 "ptr ptr ptr", FALSE
);
3968 register_icall (mono_ldtoken_wrapper
, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE
);
3969 register_icall (mono_ldtoken_wrapper_generic_shared
, "mono_ldtoken_wrapper_generic_shared",
3970 "ptr ptr ptr ptr", FALSE
);
3971 register_icall (mono_get_special_static_data
, "mono_get_special_static_data", "ptr int", FALSE
);
3972 register_icall (ves_icall_mono_ldstr
, "ves_icall_mono_ldstr", "object ptr ptr int32", FALSE
);
3973 register_icall (mono_helper_stelem_ref_check
, "mono_helper_stelem_ref_check", "void object object", FALSE
);
3974 register_icall (ves_icall_object_new
, "ves_icall_object_new", "object ptr ptr", FALSE
);
3975 register_icall (ves_icall_object_new_specific
, "ves_icall_object_new_specific", "object ptr", FALSE
);
3976 register_icall (ves_icall_array_new
, "ves_icall_array_new", "object ptr ptr int32", FALSE
);
3977 register_icall (ves_icall_array_new_specific
, "ves_icall_array_new_specific", "object ptr int32", FALSE
);
3978 register_icall (ves_icall_runtime_class_init
, "ves_icall_runtime_class_init", "void ptr", FALSE
);
3979 register_icall (mono_ldftn
, "mono_ldftn", "ptr ptr", FALSE
);
3980 register_icall (mono_ldvirtfn
, "mono_ldvirtfn", "ptr object ptr", FALSE
);
3981 register_icall (mono_ldvirtfn_gshared
, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE
);
3982 register_icall (mono_helper_compile_generic_method
, "mono_helper_compile_generic_method", "ptr object ptr ptr", FALSE
);
3983 register_icall (mono_helper_ldstr
, "mono_helper_ldstr", "object ptr int", FALSE
);
3984 register_icall (mono_helper_ldstr_mscorlib
, "mono_helper_ldstr_mscorlib", "object int", FALSE
);
3985 register_icall (mono_helper_newobj_mscorlib
, "mono_helper_newobj_mscorlib", "object int", FALSE
);
3986 register_icall (mono_value_copy
, "mono_value_copy", "void ptr ptr ptr", FALSE
);
3987 register_icall (mono_object_castclass_unbox
, "mono_object_castclass_unbox", "object object ptr", FALSE
);
3988 register_icall (mono_break
, "mono_break", NULL
, TRUE
);
3989 register_icall (mono_create_corlib_exception_0
, "mono_create_corlib_exception_0", "object int", TRUE
);
3990 register_icall (mono_create_corlib_exception_1
, "mono_create_corlib_exception_1", "object int object", TRUE
);
3991 register_icall (mono_create_corlib_exception_2
, "mono_create_corlib_exception_2", "object int object object", TRUE
);
3992 register_icall (mono_array_new_1
, "mono_array_new_1", "object ptr int", FALSE
);
3993 register_icall (mono_array_new_2
, "mono_array_new_2", "object ptr int int", FALSE
);
3994 register_icall (mono_array_new_3
, "mono_array_new_3", "object ptr int int int", FALSE
);
3995 register_icall (mono_array_new_4
, "mono_array_new_4", "object ptr int int int int", FALSE
);
3996 register_icall (mono_get_native_calli_wrapper
, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE
);
3997 register_icall (mono_resume_unwind
, "mono_resume_unwind", "void", TRUE
);
3998 register_icall (mono_gsharedvt_constrained_call
, "mono_gsharedvt_constrained_call", "object ptr ptr ptr ptr ptr", FALSE
);
3999 register_icall (mono_gsharedvt_value_copy
, "mono_gsharedvt_value_copy", "void ptr ptr ptr", TRUE
);
4001 register_icall (mono_gc_wbarrier_value_copy_bitmap
, "mono_gc_wbarrier_value_copy_bitmap", "void ptr ptr int int", FALSE
);
4003 register_icall (mono_object_castclass_with_cache
, "mono_object_castclass_with_cache", "object object ptr ptr", FALSE
);
4004 register_icall (mono_object_isinst_with_cache
, "mono_object_isinst_with_cache", "object object ptr ptr", FALSE
);
4005 register_icall (mono_generic_class_init
, "mono_generic_class_init", "void ptr", FALSE
);
4006 register_icall (mono_fill_class_rgctx
, "mono_fill_class_rgctx", "ptr ptr int", FALSE
);
4007 register_icall (mono_fill_method_rgctx
, "mono_fill_method_rgctx", "ptr ptr int", FALSE
);
4009 register_icall (mono_debugger_agent_user_break
, "mono_debugger_agent_user_break", "void", FALSE
);
4011 register_icall (mono_aot_init_llvm_method
, "mono_aot_init_llvm_method", "void ptr int", TRUE
);
4012 register_icall (mono_aot_init_gshared_method_this
, "mono_aot_init_gshared_method_this", "void ptr int object", TRUE
);
4013 register_icall (mono_aot_init_gshared_method_mrgctx
, "mono_aot_init_gshared_method_mrgctx", "void ptr int ptr", TRUE
);
4014 register_icall (mono_aot_init_gshared_method_vtable
, "mono_aot_init_gshared_method_vtable", "void ptr int ptr", TRUE
);
4016 register_icall_no_wrapper (mono_resolve_iface_call_gsharedvt
, "mono_resolve_iface_call_gsharedvt", "ptr object int ptr ptr");
4017 register_icall_no_wrapper (mono_resolve_vcall_gsharedvt
, "mono_resolve_vcall_gsharedvt", "ptr object int ptr ptr");
4018 register_icall_no_wrapper (mono_resolve_generic_virtual_call
, "mono_resolve_generic_virtual_call", "ptr ptr int ptr");
4019 register_icall_no_wrapper (mono_resolve_generic_virtual_iface_call
, "mono_resolve_generic_virtual_iface_call", "ptr ptr int ptr");
4020 /* This needs a wrapper so it can have a preserveall cconv */
4021 register_icall (mono_init_vtable_slot
, "mono_init_vtable_slot", "ptr ptr int", FALSE
);
4022 register_icall (mono_llvmonly_init_delegate
, "mono_llvmonly_init_delegate", "void object", TRUE
);
4023 register_icall (mono_llvmonly_init_delegate_virtual
, "mono_llvmonly_init_delegate_virtual", "void object object ptr", TRUE
);
4024 register_icall (mono_get_assembly_object
, "mono_get_assembly_object", "object ptr", TRUE
);
4025 register_icall (mono_get_method_object
, "mono_get_method_object", "object ptr", TRUE
);
4026 register_icall (mono_throw_method_access
, "mono_throw_method_access", "void ptr ptr", FALSE
);
4027 register_icall_no_wrapper (mono_dummy_jit_icall
, "mono_dummy_jit_icall", "void");
4029 register_icall_with_wrapper (mono_monitor_enter_internal
, "mono_monitor_enter_internal", "void obj");
4030 register_icall_with_wrapper (mono_monitor_enter_v4_internal
, "mono_monitor_enter_v4_internal", "void obj ptr");
4031 register_icall_no_wrapper (mono_monitor_enter_fast
, "mono_monitor_enter_fast", "int obj");
4032 register_icall_no_wrapper (mono_monitor_enter_v4_fast
, "mono_monitor_enter_v4_fast", "int obj ptr");
4035 register_icall (pthread_getspecific
, "pthread_getspecific", "ptr ptr", TRUE
);
4037 /* Register tls icalls */
4038 register_icall_no_wrapper (mono_tls_get_thread
, "mono_tls_get_thread", "ptr");
4039 register_icall_no_wrapper (mono_tls_get_jit_tls
, "mono_tls_get_jit_tls", "ptr");
4040 register_icall_no_wrapper (mono_tls_get_domain
, "mono_tls_get_domain", "ptr");
4041 register_icall_no_wrapper (mono_tls_get_sgen_thread_info
, "mono_tls_get_sgen_thread_info", "ptr");
4042 register_icall_no_wrapper (mono_tls_get_lmf_addr
, "mono_tls_get_lmf_addr", "ptr");
4043 register_icall_no_wrapper (mono_tls_set_thread
, "mono_tls_set_thread", "void ptr");
4044 register_icall_no_wrapper (mono_tls_set_jit_tls
, "mono_tls_set_jit_tls", "void ptr");
4045 register_icall_no_wrapper (mono_tls_set_domain
, "mono_tls_set_domain", "void ptr");
4046 register_icall_no_wrapper (mono_tls_set_sgen_thread_info
, "mono_tls_set_sgen_thread_info", "void ptr");
4047 register_icall_no_wrapper (mono_tls_set_lmf_addr
, "mono_tls_set_lmf_addr", "void ptr");
4050 MonoJitStats mono_jit_stats
= {0};
4053 print_jit_stats (void)
4055 if (mono_jit_stats
.enabled
) {
4056 g_print ("Mono Jit statistics\n");
4057 g_print ("Max code size ratio: %.2f (%s)\n", mono_jit_stats
.max_code_size_ratio
/100.0,
4058 mono_jit_stats
.max_ratio_method
);
4059 g_print ("Biggest method: %ld (%s)\n", mono_jit_stats
.biggest_method_size
,
4060 mono_jit_stats
.biggest_method
);
4062 g_print ("Delegates created: %ld\n", mono_stats
.delegate_creations
);
4063 g_print ("Initialized classes: %ld\n", mono_stats
.initialized_class_count
);
4064 g_print ("Used classes: %ld\n", mono_stats
.used_class_count
);
4065 g_print ("Generic vtables: %ld\n", mono_stats
.generic_vtable_count
);
4066 g_print ("Methods: %ld\n", mono_stats
.method_count
);
4067 g_print ("Static data size: %ld\n", mono_stats
.class_static_data_size
);
4068 g_print ("VTable data size: %ld\n", mono_stats
.class_vtable_size
);
4069 g_print ("Mscorlib mempool size: %d\n", mono_mempool_get_allocated (mono_defaults
.corlib
->mempool
));
4071 g_print ("\nInitialized classes: %ld\n", mono_stats
.generic_class_count
);
4072 g_print ("Inflated types: %ld\n", mono_stats
.inflated_type_count
);
4073 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats
.generic_virtual_invocations
);
4075 g_print ("Sharable generic methods: %ld\n", mono_stats
.generics_sharable_methods
);
4076 g_print ("Unsharable generic methods: %ld\n", mono_stats
.generics_unsharable_methods
);
4077 g_print ("Shared generic methods: %ld\n", mono_stats
.generics_shared_methods
);
4078 g_print ("Shared vtype generic methods: %ld\n", mono_stats
.gsharedvt_methods
);
4080 g_print ("IMT tables size: %ld\n", mono_stats
.imt_tables_size
);
4081 g_print ("IMT number of tables: %ld\n", mono_stats
.imt_number_of_tables
);
4082 g_print ("IMT number of methods: %ld\n", mono_stats
.imt_number_of_methods
);
4083 g_print ("IMT used slots: %ld\n", mono_stats
.imt_used_slots
);
4084 g_print ("IMT colliding slots: %ld\n", mono_stats
.imt_slots_with_collisions
);
4085 g_print ("IMT max collisions: %ld\n", mono_stats
.imt_max_collisions_in_slot
);
4086 g_print ("IMT methods at max col: %ld\n", mono_stats
.imt_method_count_when_max_collisions
);
4087 g_print ("IMT trampolines size: %ld\n", mono_stats
.imt_trampolines_size
);
4089 g_print ("JIT info table inserts: %ld\n", mono_stats
.jit_info_table_insert_count
);
4090 g_print ("JIT info table removes: %ld\n", mono_stats
.jit_info_table_remove_count
);
4091 g_print ("JIT info table lookups: %ld\n", mono_stats
.jit_info_table_lookup_count
);
4093 g_free (mono_jit_stats
.max_ratio_method
);
4094 mono_jit_stats
.max_ratio_method
= NULL
;
4095 g_free (mono_jit_stats
.biggest_method
);
4096 mono_jit_stats
.biggest_method
= NULL
;
4101 mini_cleanup (MonoDomain
*domain
)
4103 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL
)
4104 mono_runtime_shutdown_stat_profiler ();
4107 cominterop_release_all_rcws ();
4110 #ifndef MONO_CROSS_COMPILE
4112 * mono_domain_finalize () needs to be called early since it needs the
4113 * execution engine still fully working (it may invoke managed finalizers).
4115 mono_domain_finalize (domain
, 2000);
4118 /* This accesses metadata so needs to be called before runtime shutdown */
4121 #ifndef MONO_CROSS_COMPILE
4122 mono_runtime_cleanup (domain
);
4125 mono_threadpool_cleanup ();
4127 mono_profiler_shutdown ();
4129 free_jit_tls_data ((MonoJitTlsData
*)mono_tls_get_jit_tls ());
4131 mono_icall_cleanup ();
4133 mono_runtime_cleanup_handlers ();
4135 #ifndef MONO_CROSS_COMPILE
4136 mono_domain_free (domain
, TRUE
);
4141 mono_llvm_cleanup ();
4144 mono_aot_cleanup ();
4146 mono_trampolines_cleanup ();
4148 mono_unwind_cleanup ();
4150 mono_code_manager_destroy (global_codeman
);
4151 g_free (vtable_trampolines
);
4153 mini_jit_cleanup ();
4155 mono_tramp_info_cleanup ();
4157 mono_arch_cleanup ();
4159 mono_generic_sharing_cleanup ();
4163 mono_trace_cleanup ();
4165 mono_counters_dump (MONO_COUNTER_SECTION_MASK
| MONO_COUNTER_MONOTONIC
, stdout
);
4167 if (mono_inject_async_exc_method
)
4168 mono_method_desc_free (mono_inject_async_exc_method
);
4170 mono_tls_free_keys ();
4172 mono_os_mutex_destroy (&jit_mutex
);
4174 mono_code_manager_cleanup ();
4177 mono_w32handle_cleanup ();
4182 mono_set_defaults (int verbose_level
, guint32 opts
)
4184 mini_verbose
= verbose_level
;
4185 mono_set_optimizations (opts
);
4189 mono_disable_optimizations (guint32 opts
)
4191 default_opt
&= ~opts
;
4195 mono_set_optimizations (guint32 opts
)
4198 default_opt_set
= TRUE
;
4199 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
4200 mono_set_generic_sharing_vt_supported (mono_aot_only
|| ((default_opt
& MONO_OPT_GSHAREDVT
) != 0));
4203 mono_set_generic_sharing_vt_supported (TRUE
);
4208 mono_set_verbose_level (guint32 level
)
4210 mini_verbose
= level
;
4214 * mono_get_runtime_build_info:
4215 * The returned string is owned by the caller. The returned string
4216 * format is <code>VERSION (FULL_VERSION BUILD_DATE)</code> and build date is optional.
4217 * \returns the runtime version + build date in string format.
4220 mono_get_runtime_build_info (void)
4222 if (mono_build_date
)
4223 return g_strdup_printf ("%s (%s %s)", VERSION
, FULL_VERSION
, mono_build_date
);
4225 return g_strdup_printf ("%s (%s)", VERSION
, FULL_VERSION
);
4229 mono_precompile_assembly (MonoAssembly
*ass
, void *user_data
)
4231 GHashTable
*assemblies
= (GHashTable
*)user_data
;
4232 MonoImage
*image
= mono_assembly_get_image (ass
);
4233 MonoMethod
*method
, *invoke
;
4236 if (g_hash_table_lookup (assemblies
, ass
))
4239 g_hash_table_insert (assemblies
, ass
, ass
);
4241 if (mini_verbose
> 0)
4242 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image
));
4244 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_METHOD
); ++i
) {
4247 method
= mono_get_method_checked (image
, MONO_TOKEN_METHOD_DEF
| (i
+ 1), NULL
, NULL
, &error
);
4249 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
4252 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
4254 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
))
4258 if (mini_verbose
> 1) {
4259 char * desc
= mono_method_full_name (method
, TRUE
);
4260 g_print ("Compiling %d %s\n", count
, desc
);
4263 mono_compile_method_checked (method
, &error
);
4264 if (!is_ok (&error
)) {
4265 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
4268 if (strcmp (method
->name
, "Finalize") == 0) {
4269 invoke
= mono_marshal_get_runtime_invoke (method
, FALSE
);
4270 mono_compile_method_checked (invoke
, &error
);
4271 mono_error_assert_ok (&error
);
4273 #ifndef DISABLE_REMOTING
4274 if (mono_class_is_marshalbyref (method
->klass
) && mono_method_signature (method
)->hasthis
) {
4275 invoke
= mono_marshal_get_remoting_invoke_with_check (method
);
4276 mono_compile_method_checked (invoke
, &error
);
4277 mono_error_assert_ok (&error
);
4282 /* Load and precompile referenced assemblies as well */
4283 for (i
= 0; i
< mono_image_get_table_rows (image
, MONO_TABLE_ASSEMBLYREF
); ++i
) {
4284 mono_assembly_load_reference (image
, i
);
4285 if (image
->references
[i
])
4286 mono_precompile_assembly (image
->references
[i
], assemblies
);
4290 void mono_precompile_assemblies ()
4292 GHashTable
*assemblies
= g_hash_table_new (NULL
, NULL
);
4294 mono_assembly_foreach ((GFunc
)mono_precompile_assembly
, assemblies
);
4296 g_hash_table_destroy (assemblies
);
4301 * Have to export this for AOT.
4304 mono_personality (void)
4307 g_assert_not_reached ();
4310 // Custom handlers currently only implemented by Windows.
4313 mono_runtime_install_custom_handlers (const char *handlers
)
4319 mono_runtime_install_custom_handlers_usage (void)
4322 "Custom Handlers:\n"
4323 " --handlers=HANDLERS Enable handler support, HANDLERS is a comma\n"
4324 " separated list of available handlers to install.\n"
4326 "No handlers supported on current platform.\n");
4328 #endif /* HOST_WIN32 */