5 #include <mono/metadata/appdomain.h>
6 #include <mono/metadata/metadata-internals.h>
7 #include <mono/metadata/marshal.h>
8 #include <mono/metadata/tabledefs.h>
9 #include <mono/utils/mono-counters.h>
11 #ifdef HAVE_VALGRIND_MEMCHECK_H
12 #include <valgrind/memcheck.h>
16 #include "debug-mini.h"
19 * Address of the trampoline code. This is used by the debugger to check
20 * whether a method is a trampoline.
22 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
24 static GHashTable *class_init_hash_addr = NULL;
25 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
26 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
28 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
29 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
30 static CRITICAL_SECTION trampolines_mutex;
33 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
38 * The unbox trampolines call the method directly, so need to add
39 * an rgctx tramp before them.
41 return mono_create_static_rgctx_trampoline (m, mono_aot_get_unbox_trampoline (m));
43 return mono_aot_get_unbox_trampoline (m);
45 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
49 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
51 * mono_create_static_rgctx_trampoline:
53 * Return a static rgctx trampoline for M which branches to ADDR which should
54 * point to the compiled code of M.
56 * Static rgctx trampolines are used when a shared generic method which doesn't
57 * have a this argument is called indirectly, ie. from code which can't pass in
58 * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
59 * methods code. These trampolines are similar to the unbox trampolines, they
60 * perform the same task as the static rgctx wrappers, but they are smaller/faster,
61 * and can be made to work with full AOT.
64 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
70 if (mini_method_get_context (m)->method_inst)
71 ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
73 ctx = mono_class_vtable (mono_domain_get (), m->klass);
76 return mono_aot_get_static_rgctx_trampoline (ctx, addr);
78 domain = mono_domain_get ();
80 mono_domain_lock (domain);
81 res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
83 mono_domain_unlock (domain);
87 res = mono_arch_get_static_rgctx_trampoline (m, ctx, addr);
89 mono_domain_lock (domain);
90 /* Duplicates inserted while we didn't hold the lock are OK */
91 g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, m, res);
92 mono_domain_unlock (domain);
98 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
101 * This shouldn't happen as all arches which support generic sharing support
102 * static rgctx trampolines as well.
104 g_assert_not_reached ();
109 mono_get_vcall_slot_addr (guint8* code, mgreg_t *regs)
113 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
116 return (gpointer*)((char*)vt + displacement);
119 #ifdef MONO_ARCH_HAVE_IMT
122 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp)
124 MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
125 MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
126 MonoVTable *vt = this_argument->vtable;
127 int displacement = slot - ((gpointer*)vt);
129 if (displacement > 0) {
130 /* slot is in the vtable, not in the IMT */
132 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
136 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
137 int interface_offset;
138 int imt_slot = MONO_IMT_SIZE + displacement;
140 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
142 if (interface_offset < 0) {
143 g_print ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, 0), mono_type_get_name_full (&imt_method->klass->byval_arg, 0));
144 g_assert_not_reached ();
146 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
151 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
152 MonoGenericContext context = { NULL, NULL };
155 * Generic virtual method, imt_method contains the inflated interface
156 * method, need to get the inflated impl method.
158 /* imt_method->slot might not be set */
159 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
161 if (impl->klass->generic_class)
162 context.class_inst = impl->klass->generic_class->context.class_inst;
163 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
164 impl = mono_class_inflate_generic_method (impl, &context);
166 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + imt_method->slot);
169 if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
170 *need_rgctx_tramp = TRUE;
174 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
175 method->klass->name_space, method->klass->name, method->name,
176 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
179 g_assert (imt_slot < MONO_IMT_SIZE);
180 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
181 int vtable_offset = interface_offset + mono_method_get_vtable_index (imt_method);
182 gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
184 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, and colliding becomes %p[%d] (interface_offset = %d, method->slot = %d)\n", slot, imt_slot, vtable_slot, vtable_offset, interface_offset, imt_method->slot);
189 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
198 * mono_magic_trampoline:
200 * This trampoline handles calls from JITted code.
203 mono_magic_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tramp)
206 gpointer *vtable_slot;
207 gboolean generic_shared = FALSE;
208 MonoMethod *declaring = NULL;
209 MonoMethod *generic_virtual = NULL;
211 gboolean proxy = FALSE;
212 gboolean need_rgctx_tramp = FALSE;
214 if (m == MONO_FAKE_VTABLE_METHOD) {
216 MonoVTable *vt = mono_arch_get_vcall_slot (code, regs, &displacement);
221 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
223 printf ("Caller: %s\n", mono_method_full_name (ji->method, TRUE));
224 /* Print some debug info */
225 for (i = 0; i < 32; ++i)
226 printf ("0x%x ", code [-32 + i]);
230 if (displacement > 0) {
231 displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
232 g_assert (displacement >= 0);
233 displacement /= sizeof (gpointer);
235 /* Avoid loading metadata or creating a generic vtable if possible */
236 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
238 addr = mono_create_ftnptr (mono_domain_get (), addr);
239 if (addr && !vt->klass->valuetype) {
240 vtable_slot = mono_get_vcall_slot_addr (code, regs);
241 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
242 *vtable_slot = mono_get_addr_from_ftnptr (addr);
248 m = mono_class_get_vtable_entry (vt->klass, displacement);
249 if (mono_method_needs_static_rgctx_invoke (m, FALSE))
250 need_rgctx_tramp = TRUE;
252 /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
254 /* We got here from an interface method: redirect to IMT handling */
255 m = MONO_FAKE_IMT_METHOD;
256 /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
260 /* this is the IMT trampoline */
261 #ifdef MONO_ARCH_HAVE_IMT
262 if (m == MONO_FAKE_IMT_METHOD) {
263 MonoMethod *impl_method;
264 MonoGenericSharingContext *gsctx;
265 MonoObject *this_arg;
267 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
268 * needs the signature to be able to find the this argument
270 m = mono_arch_find_imt_method (regs, code);
271 vtable_slot = mono_get_vcall_slot_addr (code, regs);
272 g_assert (vtable_slot);
274 gsctx = mono_get_generic_context_from_code (code);
275 this_arg = mono_arch_find_this_argument (regs, m, gsctx);
277 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
278 /* Use the slow path for now */
280 m = mono_object_get_virtual_method (this_arg, m);
282 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp);
283 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
284 * to be called, so we compile it and go ahead as usual.
286 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
287 if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
288 /* Generic virtual method */
291 need_rgctx_tramp = TRUE;
300 MonoGenericContext context = { NULL, NULL };
301 MonoMethod *declaring;
304 declaring = mono_method_get_declaring_generic_method (m);
308 if (m->klass->generic_class)
309 context.class_inst = m->klass->generic_class->context.class_inst;
311 g_assert (!m->klass->generic_container);
313 #ifdef MONO_ARCH_HAVE_IMT
314 generic_virtual = mono_arch_find_imt_method (regs, code);
316 if (generic_virtual) {
317 g_assert (generic_virtual->is_inflated);
318 context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
321 m = mono_class_inflate_generic_method (declaring, &context);
322 /* FIXME: only do this if the method is sharable */
323 need_rgctx_tramp = TRUE;
324 } else if ((context_used = mono_method_check_context_used (m))) {
325 MonoClass *klass = NULL;
326 MonoMethod *actual_method = NULL;
327 MonoVTable *vt = NULL;
328 MonoGenericInst *method_inst = NULL;
331 generic_shared = TRUE;
335 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
336 #ifdef MONO_ARCH_RGCTX_REG
337 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable (regs, code);
339 klass = mrgctx->class_vtable->klass;
340 method_inst = mrgctx->method_inst;
342 g_assert_not_reached ();
344 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
345 #ifdef MONO_ARCH_RGCTX_REG
346 MonoVTable *vtable = mono_arch_find_static_call_vtable (regs, code);
348 klass = vtable->klass;
350 g_assert_not_reached ();
353 #ifdef MONO_ARCH_HAVE_IMT
354 MonoObject *this_argument = mono_arch_find_this_argument (regs, m,
355 mono_get_generic_context_from_code (code));
357 vt = this_argument->vtable;
358 vtable_slot = mono_get_vcall_slot_addr (code, regs);
360 g_assert (this_argument->vtable->klass->inited);
361 //mono_class_init (this_argument->vtable->klass);
364 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
370 g_assert (vtable_slot || klass);
373 int displacement = vtable_slot - ((gpointer*)vt);
375 g_assert_not_reached ();
377 g_assert (displacement > 0);
379 actual_method = vt->klass->vtable [displacement];
383 MonoGenericContext context = { NULL, NULL };
386 declaring = mono_method_get_declaring_generic_method (m);
390 if (klass->generic_class)
391 context.class_inst = klass->generic_class->context.class_inst;
392 else if (klass->generic_container)
393 context.class_inst = klass->generic_container->context.class_inst;
394 context.method_inst = method_inst;
396 actual_method = mono_class_inflate_generic_method (declaring, &context);
398 actual_method = mono_class_get_method_generic (klass, m);
402 g_assert (actual_method->klass == klass);
404 if (actual_method->is_inflated)
405 declaring = mono_method_get_declaring_generic_method (actual_method);
412 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
416 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
420 /* Avoid recursion */
421 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
422 m = mono_marshal_get_synchronized_wrapper (m);
425 /* Calls made through delegates on platforms without delegate trampolines */
426 if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
427 need_rgctx_tramp = TRUE;
429 addr = mono_compile_method (m);
432 mono_debugger_trampoline_compiled (m, addr);
434 if (need_rgctx_tramp)
435 addr = mono_create_static_rgctx_trampoline (m, addr);
437 if (generic_virtual) {
439 MonoVTable *vt = mono_arch_get_vcall_slot (code, regs, &displacement);
441 vtable_slot = mono_get_vcall_slot_addr (code, regs);
442 g_assert (vtable_slot);
444 if (vt->klass->valuetype)
445 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
447 mono_method_add_generic_virtual_invocation (mono_domain_get (),
449 generic_virtual, addr);
454 /* the method was jumped to */
456 MonoDomain *domain = mono_domain_get ();
458 /* Patch the got entries pointing to this method */
460 * We do this here instead of in mono_codegen () to cover the case when m
461 * was loaded from an aot image.
463 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
466 mono_domain_lock (domain);
467 list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
469 for (tmp = list; tmp; tmp = tmp->next) {
470 gpointer *got_slot = tmp->data;
473 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
476 mono_domain_unlock (domain);
482 vtable_slot = mono_get_vcall_slot_addr (code, regs);
485 if (m->klass->valuetype)
486 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
487 g_assert (*vtable_slot);
489 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
490 #ifdef MONO_ARCH_HAVE_IMT
491 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, NULL, &need_rgctx_tramp);
493 *vtable_slot = mono_get_addr_from_ftnptr (addr);
497 guint8 *plt_entry = mono_aot_get_plt_entry (code);
500 mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
501 } else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
502 mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
503 if (generic_shared) {
504 if (m->wrapper_type != MONO_WRAPPER_NONE)
505 m = mono_marshal_method_from_wrapper (m);
506 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
509 /* Patch calling code */
514 mono_jit_info_table_find (mono_domain_get (), (char*)code);
515 MonoJitInfo *target_ji =
516 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
518 if (mono_method_same_domain (ji, target_ji))
519 mono_arch_patch_callsite (ji->code_start, code, addr);
529 * mono_llvm_vcall_trampoline:
531 * This trampoline handles virtual calls when using LLVM.
534 mono_llvm_vcall_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
539 gpointer *vtable_slot;
540 gboolean proxy = FALSE;
541 gboolean need_rgctx_tramp = FALSE;
544 * We have the method which is called, we need to obtain the vtable slot without
545 * disassembly which is impossible with LLVM.
546 * So we use the this argument.
548 this = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
553 g_assert (!m->is_generic);
555 /* This is a simplified version of mono_magic_trampoline () */
556 /* FIXME: Avoid code duplication */
558 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
562 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
566 /* Avoid recursion */
567 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
568 m = mono_marshal_get_synchronized_wrapper (m);
571 addr = mono_compile_method (m);
574 if (m->klass->valuetype)
575 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
577 vtable_slot = &(vt->vtable [mono_method_get_vtable_slot (m)]);
578 g_assert (*vtable_slot);
580 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
581 #ifdef MONO_ARCH_HAVE_IMT
582 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, NULL, &need_rgctx_tramp);
584 *vtable_slot = mono_get_addr_from_ftnptr (addr);
587 mono_debugger_trampoline_compiled (m, addr);
594 mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
596 MonoGenericContext context = { NULL, NULL };
597 MonoMethod *imt_method, *declaring;
600 g_assert (m->is_generic);
603 declaring = mono_method_get_declaring_generic_method (m);
607 if (m->klass->generic_class)
608 context.class_inst = m->klass->generic_class->context.class_inst;
610 g_assert (!m->klass->generic_container);
612 #ifdef MONO_ARCH_HAVE_IMT
613 imt_method = mono_arch_find_imt_method (regs, code);
614 if (imt_method->is_inflated)
615 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
617 m = mono_class_inflate_generic_method (declaring, &context);
618 m = mono_marshal_get_remoting_invoke_with_check (m);
620 addr = mono_compile_method (m);
623 mono_debugger_trampoline_compiled (m, addr);
629 * mono_aot_trampoline:
631 * This trampoline handles calls made from AOT code. We try to bypass the
632 * normal JIT compilation logic to avoid loading the metadata for the method.
634 #ifdef MONO_ARCH_AOT_SUPPORTED
636 mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
641 MonoMethod *method = NULL;
643 gpointer *vtable_slot;
644 gboolean is_got_entry;
646 gboolean need_rgctx_tramp = FALSE;
648 image = *(gpointer*)(gpointer)token_info;
649 token_info += sizeof (gpointer);
650 token = *(guint32*)(gpointer)token_info;
652 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
654 method = mono_get_method (image, token, NULL);
657 /* Use the generic code */
658 return mono_magic_trampoline (regs, code, method, tramp);
661 addr = mono_create_ftnptr (mono_domain_get (), addr);
663 vtable_slot = mono_get_vcall_slot_addr (code, regs);
664 g_assert (!vtable_slot);
666 /* This is a normal call through a PLT entry */
667 plt_entry = mono_aot_get_plt_entry (code);
668 g_assert (plt_entry);
670 mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
672 is_got_entry = FALSE;
675 * Since AOT code is only used in the root domain,
676 * mono_domain_get () != mono_get_root_domain () means the calling method
677 * is AppDomain:InvokeInDomain, so this is the same check as in
678 * mono_method_same_domain () but without loading the metadata for the method.
680 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
681 #ifdef MONO_ARCH_HAVE_IMT
683 method = mono_get_method (image, token, NULL);
684 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, method, NULL, &need_rgctx_tramp);
693 * mono_aot_plt_trampoline:
695 * This trampoline handles calls made from AOT code through the PLT table.
698 mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *aot_module,
701 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
703 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
708 * mono_class_init_trampoline:
710 * This method calls mono_runtime_class_init () to run the static constructor
711 * for the type, then patches the caller code so it is not called again.
714 mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
716 guint8 *plt_entry = mono_aot_get_plt_entry (code);
718 mono_runtime_class_init (vtable);
721 mono_arch_nullify_plt_entry (plt_entry, regs);
723 mono_arch_nullify_class_init_trampoline (code, regs);
728 * mono_generic_class_init_trampoline:
730 * This method calls mono_runtime_class_init () to run the static constructor
734 mono_generic_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
736 mono_runtime_class_init (vtable);
740 mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, guint8 *tramp)
742 #ifdef MONO_ARCH_VTABLE_REG
743 static gboolean inited = FALSE;
744 static int num_lookups = 0;
745 guint32 slot = GPOINTER_TO_UINT (data);
746 mgreg_t *r = (mgreg_t*)regs;
747 gpointer arg = (gpointer)(gssize)r [MONO_ARCH_VTABLE_REG];
748 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
749 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
752 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
759 return mono_method_fill_runtime_generic_context (arg, index);
761 return mono_class_fill_runtime_generic_context (arg, index);
763 g_assert_not_reached ();
768 mono_monitor_enter_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
770 mono_monitor_enter (obj);
774 mono_monitor_exit_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
776 mono_monitor_exit (obj);
779 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
782 * mono_delegate_trampoline:
784 * This trampoline handles calls made to Delegate:Invoke ().
785 * This is called once the first time a delegate is invoked, so it must be fast.
788 mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
790 MonoDomain *domain = mono_domain_get ();
791 MonoDelegate *delegate;
794 MonoMethod *method = NULL;
795 gboolean multicast, callvirt;
796 gboolean need_rgctx_tramp = FALSE;
797 MonoMethod *invoke = tramp_data [0];
798 guint8 *impl_this = tramp_data [1];
799 guint8 *impl_nothis = tramp_data [2];
801 /* Obtain the delegate object according to the calling convention */
804 * Avoid calling mono_get_generic_context_from_code () now since it is expensive,
805 * get_this_arg_from_call will call it if needed.
807 delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
809 if (delegate->method) {
810 method = delegate->method;
813 * delegate->method_ptr == NULL means the delegate was initialized by
814 * mini_delegate_ctor, while != NULL means it is initialized by
815 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
816 * (ctor_with_method () does this, but it doesn't store the wrapper back into
819 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
820 method = mono_marshal_get_remoting_invoke (method);
821 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
822 method = mono_marshal_get_unbox_wrapper (method);
824 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
828 callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
830 if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
831 method = mono_marshal_get_synchronized_wrapper (method);
833 if (method && mono_method_needs_static_rgctx_invoke (method, FALSE))
834 need_rgctx_tramp = TRUE;
837 * If the called address is a trampoline, replace it with the compiled method so
838 * further calls don't have to go through the trampoline.
840 if (method && !callvirt) {
841 /* Avoid the overhead of looking up an already compiled method if possible */
842 if (delegate->method_code && *delegate->method_code) {
843 delegate->method_ptr = *delegate->method_code;
845 delegate->method_ptr = mono_compile_method (method);
846 if (delegate->method_code)
847 *delegate->method_code = delegate->method_ptr;
848 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
852 if (need_rgctx_tramp)
853 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
855 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
856 if (!multicast && !callvirt) {
857 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
858 /* Closed static delegate */
861 code = delegate->target ? impl_this : impl_nothis;
864 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
869 /* The general, unoptimized case */
870 m = mono_marshal_get_delegate_invoke (invoke, delegate);
871 code = mono_compile_method (m);
872 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
873 mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
881 * mono_get_trampoline_func:
883 * Return the C function which needs to be called by the generic trampoline of type
887 mono_get_trampoline_func (MonoTrampolineType tramp_type)
889 switch (tramp_type) {
890 case MONO_TRAMPOLINE_JIT:
891 case MONO_TRAMPOLINE_JUMP:
892 return mono_magic_trampoline;
893 case MONO_TRAMPOLINE_CLASS_INIT:
894 return mono_class_init_trampoline;
895 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
896 return mono_generic_class_init_trampoline;
897 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
898 return mono_rgctx_lazy_fetch_trampoline;
899 #ifdef MONO_ARCH_AOT_SUPPORTED
900 case MONO_TRAMPOLINE_AOT:
901 return mono_aot_trampoline;
902 case MONO_TRAMPOLINE_AOT_PLT:
903 return mono_aot_plt_trampoline;
905 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
906 case MONO_TRAMPOLINE_DELEGATE:
907 return mono_delegate_trampoline;
909 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
910 return mono_altstack_restore_prot;
911 case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
912 return mono_generic_virtual_remoting_trampoline;
913 case MONO_TRAMPOLINE_MONITOR_ENTER:
914 return mono_monitor_enter_trampoline;
915 case MONO_TRAMPOLINE_MONITOR_EXIT:
916 return mono_monitor_exit_trampoline;
918 case MONO_TRAMPOLINE_LLVM_VCALL:
919 return mono_llvm_vcall_trampoline;
922 g_assert_not_reached ();
928 mono_trampolines_init (void)
930 InitializeCriticalSection (&trampolines_mutex);
935 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
936 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
937 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
938 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
939 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
940 #ifdef MONO_ARCH_AOT_SUPPORTED
941 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
942 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
944 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
945 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
947 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
948 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
949 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
950 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
952 mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
957 mono_trampolines_cleanup (void)
959 if (class_init_hash_addr)
960 g_hash_table_destroy (class_init_hash_addr);
962 DeleteCriticalSection (&trampolines_mutex);
966 mono_get_trampoline_code (MonoTrampolineType tramp_type)
968 g_assert (mono_trampoline_code [tramp_type]);
970 return mono_trampoline_code [tramp_type];
974 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
977 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
979 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
983 mono_create_class_init_trampoline (MonoVTable *vtable)
986 MonoDomain *domain = vtable->domain;
988 g_assert (!vtable->klass->generic_container);
990 /* previously created trampoline code */
991 mono_domain_lock (domain);
993 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
995 mono_domain_unlock (domain);
999 code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
1001 ptr = mono_create_ftnptr (domain, code);
1003 /* store trampoline address */
1004 mono_domain_lock (domain);
1005 g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
1007 mono_domain_unlock (domain);
1009 mono_trampolines_lock ();
1010 if (!class_init_hash_addr)
1011 class_init_hash_addr = g_hash_table_new (NULL, NULL);
1012 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
1013 mono_trampolines_unlock ();
1019 mono_create_generic_class_init_trampoline (void)
1021 #ifdef MONO_ARCH_VTABLE_REG
1022 static gpointer code;
1024 mono_trampolines_lock ();
1028 code = mono_aot_get_named_code ("generic_class_init_trampoline");
1030 code = mono_arch_create_generic_class_init_trampoline ();
1033 mono_trampolines_unlock ();
1037 g_assert_not_reached ();
1042 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
1046 guint32 code_size = 0;
1048 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1050 * We cannot recover the correct type of a shared generic
1051 * method from its native code address, so we use the
1052 * trampoline instead.
1054 if (code && !ji->has_generic_jit_info)
1057 mono_domain_lock (domain);
1058 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1059 mono_domain_unlock (domain);
1063 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1064 g_assert (code_size);
1066 ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
1067 ji->code_start = code;
1068 ji->code_size = code_size;
1069 ji->method = method;
1072 * mono_delegate_ctor needs to find the method metadata from the
1073 * trampoline address, so we save it here.
1076 mono_jit_info_table_add (domain, ji);
1078 mono_domain_lock (domain);
1079 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1080 mono_domain_unlock (domain);
1082 return ji->code_start;
1086 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1090 if (mono_aot_only) {
1091 /* Avoid creating trampolines if possible */
1092 gpointer code = mono_jit_find_compiled_method (domain, method);
1098 mono_domain_lock (domain);
1099 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1100 mono_domain_unlock (domain);
1104 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1106 mono_domain_lock (domain);
1107 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1108 mono_domain_unlock (domain);
1110 mono_jit_stats.method_trampolines++;
1116 mono_create_jit_trampoline (MonoMethod *method)
1118 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1122 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1126 MonoDomain *domain = mono_domain_get ();
1127 guint8 *buf, *start;
1129 buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1131 *(gpointer*)(gpointer)buf = image;
1132 buf += sizeof (gpointer);
1133 *(guint32*)(gpointer)buf = token;
1135 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1137 mono_jit_stats.method_trampolines++;
1143 mono_create_delegate_trampoline (MonoClass *klass)
1145 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1146 MonoDomain *domain = mono_domain_get ();
1148 guint32 code_size = 0;
1149 gpointer *tramp_data;
1152 mono_domain_lock (domain);
1153 ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1154 mono_domain_unlock (domain);
1158 // Precompute the delegate invoke impl and pass it to the delegate trampoline
1159 invoke = mono_get_delegate_invoke (klass);
1162 tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1163 tramp_data [0] = invoke;
1164 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1165 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1167 ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1168 g_assert (code_size);
1170 /* store trampoline address */
1171 mono_domain_lock (domain);
1172 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1174 mono_domain_unlock (domain);
1183 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1185 static gboolean inited = FALSE;
1186 static int num_trampolines = 0;
1188 gpointer tramp, ptr;
1191 return mono_aot_get_lazy_fetch_trampoline (offset);
1193 mono_trampolines_lock ();
1194 if (rgctx_lazy_fetch_trampoline_hash)
1195 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1198 mono_trampolines_unlock ();
1202 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
1203 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1205 mono_trampolines_lock ();
1206 if (!rgctx_lazy_fetch_trampoline_hash) {
1207 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1208 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1210 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1211 g_assert (offset != -1);
1212 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1213 mono_trampolines_unlock ();
1216 mono_counters_register ("RGCTX num lazy fetch trampolines",
1217 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1226 mono_create_monitor_enter_trampoline (void)
1228 static gpointer code;
1230 if (mono_aot_only) {
1232 code = mono_aot_get_named_code ("monitor_enter_trampoline");
1236 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1237 mono_trampolines_lock ();
1240 code = mono_arch_create_monitor_enter_trampoline ();
1242 mono_trampolines_unlock ();
1245 g_assert_not_reached ();
1252 mono_create_monitor_exit_trampoline (void)
1254 static gpointer code;
1256 if (mono_aot_only) {
1258 code = mono_aot_get_named_code ("monitor_exit_trampoline");
1262 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1263 mono_trampolines_lock ();
1266 code = mono_arch_create_monitor_exit_trampoline ();
1268 mono_trampolines_unlock ();
1271 g_assert_not_reached ();
1278 * mono_create_llvm_vcall_trampoline:
1280 * LLVM emits code for virtual calls which mono_get_vcall_slot is unable to
1281 * decode, i.e. only the final branch address is available:
1282 * mov <offset>(%rax), %rax
1283 * <random code inserted by instruction scheduling>
1286 * To work around this problem, we don't use the common vtable trampoline when
1287 * llvm is enabled. Instead, we use one trampoline per method.
1290 mono_create_llvm_vcall_trampoline (MonoMethod *method)
1295 domain = mono_domain_get ();
1297 mono_domain_lock (domain);
1298 res = g_hash_table_lookup (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method);
1299 mono_domain_unlock (domain);
1303 res = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_LLVM_VCALL, domain, NULL);
1305 mono_domain_lock (domain);
1306 g_hash_table_insert (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method, res);
1307 mono_domain_unlock (domain);
1314 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1318 mono_trampolines_lock ();
1319 if (class_init_hash_addr)
1320 res = g_hash_table_lookup (class_init_hash_addr, addr);
1323 mono_trampolines_unlock ();
1328 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1332 mono_trampolines_lock ();
1333 if (rgctx_lazy_fetch_trampoline_hash_addr) {
1334 /* We store the real offset + 1 so we can detect when the lookup fails */
1335 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1343 mono_trampolines_unlock ();