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 *delegate_trampoline_hash_addr = NULL;
26 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
27 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
29 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
30 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
31 static CRITICAL_SECTION trampolines_mutex;
34 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
37 return mono_aot_get_unbox_trampoline (m);
39 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
42 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
44 * mono_create_static_rgctx_trampoline:
46 * Return a static rgctx trampoline for M which branches to ADDR which should
47 * point to the compiled code of M.
49 * Static rgctx trampolines are used when a shared generic method which doesn't
50 * have a this argument is called indirectly, ie. from code which can't pass in
51 * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
52 * methods code. These trampolines are similar to the unbox trampolines, they
53 * perform the same task as the static rgctx wrappers, but they are smaller/faster,
54 * and can be made to work with full AOT.
57 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
63 if (mini_method_get_context (m)->method_inst)
64 ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
66 ctx = mono_class_vtable (mono_domain_get (), m->klass);
69 return mono_aot_get_static_rgctx_trampoline (ctx, addr);
71 domain = mono_domain_get ();
73 mono_domain_lock (domain);
74 res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
76 mono_domain_unlock (domain);
80 res = mono_arch_get_static_rgctx_trampoline (m, ctx, addr);
82 mono_domain_lock (domain);
83 /* Duplicates inserted while we didn't hold the lock are OK */
84 g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, m, res);
85 mono_domain_unlock (domain);
91 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
94 * This shouldn't happen as all arches which support generic sharing support
95 * static rgctx trampolines as well.
97 g_assert_not_reached ();
102 mono_get_vcall_slot_addr (guint8* code, gpointer *regs)
106 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
109 return (gpointer*)((char*)vt + displacement);
112 #ifdef MONO_ARCH_HAVE_IMT
115 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp)
117 MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
118 MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
119 MonoVTable *vt = this_argument->vtable;
120 int displacement = slot - ((gpointer*)vt);
122 if (displacement > 0) {
123 /* slot is in the vtable, not in the IMT */
125 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
129 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
130 int interface_offset;
131 int imt_slot = MONO_IMT_SIZE + displacement;
133 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
135 if (interface_offset < 0) {
136 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));
137 g_assert_not_reached ();
139 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
144 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
145 MonoGenericContext context = { NULL, NULL };
148 * Generic virtual method, imt_method contains the inflated interface
149 * method, need to get the inflated impl method.
151 /* imt_method->slot might not be set */
152 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
154 if (impl->klass->generic_class)
155 context.class_inst = impl->klass->generic_class->context.class_inst;
156 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
157 impl = mono_class_inflate_generic_method (impl, &context);
159 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + imt_method->slot);
162 if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
163 *need_rgctx_tramp = TRUE;
167 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
168 method->klass->name_space, method->klass->name, method->name,
169 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
172 g_assert (imt_slot < MONO_IMT_SIZE);
173 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
174 int vtable_offset = interface_offset + mono_method_get_vtable_index (imt_method);
175 gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
177 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);
182 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
191 * mono_magic_trampoline:
193 * This trampoline handles calls from JITted code.
196 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
199 gpointer *vtable_slot;
200 gboolean generic_shared = FALSE;
201 MonoMethod *declaring = NULL;
202 MonoMethod *generic_virtual = NULL;
204 gboolean proxy = FALSE;
205 gboolean need_rgctx_tramp = FALSE;
207 if (m == MONO_FAKE_VTABLE_METHOD) {
209 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
214 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
216 printf ("Caller: %s\n", mono_method_full_name (ji->method, TRUE));
217 /* Print some debug info */
218 for (i = 0; i < 32; ++i)
219 printf ("0x%x ", code [-32 + i]);
223 if (displacement > 0) {
224 displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
225 g_assert (displacement >= 0);
226 displacement /= sizeof (gpointer);
228 /* Avoid loading metadata or creating a generic vtable if possible */
229 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
230 if (addr && !vt->klass->valuetype) {
231 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
232 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
233 *vtable_slot = mono_get_addr_from_ftnptr (addr);
239 m = mono_class_get_vtable_entry (vt->klass, displacement);
240 if (mono_method_needs_static_rgctx_invoke (m, FALSE))
241 need_rgctx_tramp = TRUE;
243 /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
245 /* We got here from an interface method: redirect to IMT handling */
246 m = MONO_FAKE_IMT_METHOD;
247 /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
251 /* this is the IMT trampoline */
252 #ifdef MONO_ARCH_HAVE_IMT
253 if (m == MONO_FAKE_IMT_METHOD) {
254 MonoMethod *impl_method;
255 MonoGenericSharingContext *gsctx;
256 MonoObject *this_arg;
258 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
259 * needs the signature to be able to find the this argument
261 m = mono_arch_find_imt_method ((gpointer*)regs, code);
262 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
263 g_assert (vtable_slot);
265 gsctx = mono_get_generic_context_from_code (code);
266 this_arg = mono_arch_find_this_argument ((gpointer*)regs, m, gsctx);
268 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
269 /* Use the slow path for now */
271 m = mono_object_get_virtual_method (this_arg, m);
273 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method, &need_rgctx_tramp);
274 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
275 * to be called, so we compile it and go ahead as usual.
277 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
278 if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
279 /* Generic virtual method */
282 need_rgctx_tramp = TRUE;
291 MonoGenericContext context = { NULL, NULL };
292 MonoMethod *declaring;
295 declaring = mono_method_get_declaring_generic_method (m);
299 if (m->klass->generic_class)
300 context.class_inst = m->klass->generic_class->context.class_inst;
302 g_assert (!m->klass->generic_container);
304 #ifdef MONO_ARCH_HAVE_IMT
305 generic_virtual = mono_arch_find_imt_method ((gpointer*)regs, code);
307 if (generic_virtual) {
308 g_assert (generic_virtual->is_inflated);
309 context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
312 m = mono_class_inflate_generic_method (declaring, &context);
313 /* FIXME: only do this if the method is sharable */
314 need_rgctx_tramp = TRUE;
315 } else if ((context_used = mono_method_check_context_used (m))) {
316 MonoClass *klass = NULL;
317 MonoMethod *actual_method = NULL;
318 MonoVTable *vt = NULL;
319 MonoGenericInst *method_inst = NULL;
322 generic_shared = TRUE;
326 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
327 #ifdef MONO_ARCH_RGCTX_REG
328 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable ((gpointer*)regs, code);
330 klass = mrgctx->class_vtable->klass;
331 method_inst = mrgctx->method_inst;
333 g_assert_not_reached ();
335 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
336 #ifdef MONO_ARCH_RGCTX_REG
337 MonoVTable *vtable = mono_arch_find_static_call_vtable ((gpointer*)regs, code);
339 klass = vtable->klass;
341 g_assert_not_reached ();
344 #ifdef MONO_ARCH_HAVE_IMT
345 MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
346 mono_get_generic_context_from_code (code));
348 vt = this_argument->vtable;
349 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
351 g_assert (this_argument->vtable->klass->inited);
352 //mono_class_init (this_argument->vtable->klass);
355 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
361 g_assert (vtable_slot || klass);
364 int displacement = vtable_slot - ((gpointer*)vt);
366 g_assert_not_reached ();
368 g_assert (displacement > 0);
370 actual_method = vt->klass->vtable [displacement];
374 MonoGenericContext context = { NULL, NULL };
377 declaring = mono_method_get_declaring_generic_method (m);
381 if (klass->generic_class)
382 context.class_inst = klass->generic_class->context.class_inst;
383 else if (klass->generic_container)
384 context.class_inst = klass->generic_container->context.class_inst;
385 context.method_inst = method_inst;
387 actual_method = mono_class_inflate_generic_method (declaring, &context);
389 actual_method = mono_class_get_method_generic (klass, m);
393 g_assert (actual_method->klass == klass);
395 if (actual_method->is_inflated)
396 declaring = mono_method_get_declaring_generic_method (actual_method);
403 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
407 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
411 /* Avoid recursion */
412 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
413 m = mono_marshal_get_synchronized_wrapper (m);
416 /* Calls made through delegates on platforms without delegate trampolines */
417 if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
418 need_rgctx_tramp = TRUE;
420 addr = mono_compile_method (m);
423 mono_debugger_trampoline_compiled (m, addr);
425 if (need_rgctx_tramp)
426 addr = mono_create_static_rgctx_trampoline (m, addr);
428 if (generic_virtual) {
430 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
432 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
433 g_assert (vtable_slot);
435 if (vt->klass->valuetype)
436 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
438 mono_method_add_generic_virtual_invocation (mono_domain_get (),
440 generic_virtual, addr);
445 /* the method was jumped to */
447 MonoDomain *domain = mono_domain_get ();
449 /* Patch the got entries pointing to this method */
451 * We do this here instead of in mono_codegen () to cover the case when m
452 * was loaded from an aot image.
454 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
457 mono_domain_lock (domain);
458 list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
460 for (tmp = list; tmp; tmp = tmp->next) {
461 gpointer *got_slot = tmp->data;
464 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
467 mono_domain_unlock (domain);
473 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
476 if (m->klass->valuetype)
477 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
478 g_assert (*vtable_slot);
480 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
481 #ifdef MONO_ARCH_HAVE_IMT
482 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL, &need_rgctx_tramp);
484 *vtable_slot = mono_get_addr_from_ftnptr (addr);
488 guint8 *plt_entry = mono_aot_get_plt_entry (code);
491 mono_arch_patch_plt_entry (plt_entry, addr);
492 } else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
493 mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
494 if (generic_shared) {
495 if (m->wrapper_type != MONO_WRAPPER_NONE)
496 m = mono_marshal_method_from_wrapper (m);
497 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
500 /* Patch calling code */
505 mono_jit_info_table_find (mono_domain_get (), (char*)code);
506 MonoJitInfo *target_ji =
507 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
509 if (mono_method_same_domain (ji, target_ji))
510 mono_arch_patch_callsite (ji->code_start, code, addr);
520 * mono_llvm_vcall_trampoline:
522 * This trampoline handles virtual calls when using LLVM.
525 mono_llvm_vcall_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
530 gpointer *vtable_slot;
531 gboolean proxy = FALSE;
532 gboolean need_rgctx_tramp = FALSE;
535 * We have the method which is called, we need to obtain the vtable slot without
536 * disassembly which is impossible with LLVM.
537 * So we use the this argument.
539 this = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
544 g_assert (!m->is_generic);
546 /* This is a simplified version of mono_magic_trampoline () */
547 /* FIXME: Avoid code duplication */
549 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
553 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
557 /* Avoid recursion */
558 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
559 m = mono_marshal_get_synchronized_wrapper (m);
562 addr = mono_compile_method (m);
565 if (m->klass->valuetype)
566 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
568 vtable_slot = &(vt->vtable [mono_method_get_vtable_slot (m)]);
569 g_assert (*vtable_slot);
571 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
572 #ifdef MONO_ARCH_HAVE_IMT
573 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL, &need_rgctx_tramp);
575 *vtable_slot = mono_get_addr_from_ftnptr (addr);
578 mono_debugger_trampoline_compiled (m, addr);
585 mono_generic_virtual_remoting_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
587 MonoGenericContext context = { NULL, NULL };
588 MonoMethod *imt_method, *declaring;
591 g_assert (m->is_generic);
594 declaring = mono_method_get_declaring_generic_method (m);
598 if (m->klass->generic_class)
599 context.class_inst = m->klass->generic_class->context.class_inst;
601 g_assert (!m->klass->generic_container);
603 #ifdef MONO_ARCH_HAVE_IMT
604 imt_method = mono_arch_find_imt_method ((gpointer*)regs, code);
605 if (imt_method->is_inflated)
606 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
608 m = mono_class_inflate_generic_method (declaring, &context);
609 m = mono_marshal_get_remoting_invoke_with_check (m);
611 addr = mono_compile_method (m);
614 mono_debugger_trampoline_compiled (m, addr);
620 * mono_aot_trampoline:
622 * This trampoline handles calls made from AOT code. We try to bypass the
623 * normal JIT compilation logic to avoid loading the metadata for the method.
625 #ifdef MONO_ARCH_AOT_SUPPORTED
627 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
632 MonoMethod *method = NULL;
634 gpointer *vtable_slot;
635 gboolean is_got_entry;
637 gboolean need_rgctx_tramp = FALSE;
639 image = *(gpointer*)(gpointer)token_info;
640 token_info += sizeof (gpointer);
641 token = *(guint32*)(gpointer)token_info;
643 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
645 method = mono_get_method (image, token, NULL);
648 /* Use the generic code */
649 return mono_magic_trampoline (regs, code, method, tramp);
652 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
653 g_assert (!vtable_slot);
655 /* This is a normal call through a PLT entry */
656 plt_entry = mono_aot_get_plt_entry (code);
657 g_assert (plt_entry);
659 mono_arch_patch_plt_entry (plt_entry, addr);
661 is_got_entry = FALSE;
664 * Since AOT code is only used in the root domain,
665 * mono_domain_get () != mono_get_root_domain () means the calling method
666 * is AppDomain:InvokeInDomain, so this is the same check as in
667 * mono_method_same_domain () but without loading the metadata for the method.
669 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
670 #ifdef MONO_ARCH_HAVE_IMT
672 method = mono_get_method (image, token, NULL);
673 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL, &need_rgctx_tramp);
682 * mono_aot_plt_trampoline:
684 * This trampoline handles calls made from AOT code through the PLT table.
687 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
690 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
692 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
697 * mono_class_init_trampoline:
699 * This method calls mono_runtime_class_init () to run the static constructor
700 * for the type, then patches the caller code so it is not called again.
703 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
705 guint8 *plt_entry = mono_aot_get_plt_entry (code);
707 mono_runtime_class_init (vtable);
710 mono_arch_nullify_plt_entry (plt_entry);
712 mono_arch_nullify_class_init_trampoline (code, regs);
717 * mono_generic_class_init_trampoline:
719 * This method calls mono_runtime_class_init () to run the static constructor
723 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
725 mono_runtime_class_init (vtable);
729 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
731 #ifdef MONO_ARCH_VTABLE_REG
732 static gboolean inited = FALSE;
733 static int num_lookups = 0;
734 guint32 slot = GPOINTER_TO_UINT (data);
735 gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
736 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
737 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
740 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
747 return mono_method_fill_runtime_generic_context (arg, index);
749 return mono_class_fill_runtime_generic_context (arg, index);
751 g_assert_not_reached ();
756 mono_monitor_enter_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
758 mono_monitor_enter (obj);
762 mono_monitor_exit_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
764 mono_monitor_exit (obj);
767 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
770 * mono_delegate_trampoline:
772 * This trampoline handles calls made to Delegate:Invoke ().
773 * This is called once the first time a delegate is invoked, so it must be fast.
776 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
778 MonoDomain *domain = mono_domain_get ();
779 MonoDelegate *delegate;
782 MonoMethod *method = NULL;
783 gboolean multicast, callvirt;
784 gboolean need_rgctx_tramp = FALSE;
785 MonoMethod *invoke = tramp_data [0];
786 guint8 *impl_this = tramp_data [1];
787 guint8 *impl_nothis = tramp_data [2];
789 /* Obtain the delegate object according to the calling convention */
792 * Avoid calling mono_get_generic_context_from_code () now since it is expensive,
793 * get_this_arg_from_call will call it if needed.
795 delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
797 if (delegate->method) {
798 method = delegate->method;
801 * delegate->method_ptr == NULL means the delegate was initialized by
802 * mini_delegate_ctor, while != NULL means it is initialized by
803 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
804 * (ctor_with_method () does this, but it doesn't store the wrapper back into
807 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
808 method = mono_marshal_get_remoting_invoke (method);
809 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
810 method = mono_marshal_get_unbox_wrapper (method);
812 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
816 callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
818 if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
819 method = mono_marshal_get_synchronized_wrapper (method);
821 if (method && mono_method_needs_static_rgctx_invoke (method, FALSE))
822 need_rgctx_tramp = TRUE;
825 * If the called address is a trampoline, replace it with the compiled method so
826 * further calls don't have to go through the trampoline.
828 if (method && !callvirt) {
829 /* Avoid the overhead of looking up an already compiled method if possible */
830 if (delegate->method_code && *delegate->method_code) {
831 delegate->method_ptr = *delegate->method_code;
833 delegate->method_ptr = mono_compile_method (method);
834 if (delegate->method_code)
835 *delegate->method_code = delegate->method_ptr;
836 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
840 if (need_rgctx_tramp)
841 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
843 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
844 if (!multicast && !callvirt) {
845 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
846 /* Closed static delegate */
849 code = delegate->target ? impl_this : impl_nothis;
852 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
857 /* The general, unoptimized case */
858 m = mono_marshal_get_delegate_invoke (invoke, delegate);
859 code = mono_compile_method (m);
860 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
861 mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
869 * mono_get_trampoline_func:
871 * Return the C function which needs to be called by the generic trampoline of type
875 mono_get_trampoline_func (MonoTrampolineType tramp_type)
877 switch (tramp_type) {
878 case MONO_TRAMPOLINE_JIT:
879 case MONO_TRAMPOLINE_JUMP:
880 return mono_magic_trampoline;
881 case MONO_TRAMPOLINE_CLASS_INIT:
882 return mono_class_init_trampoline;
883 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
884 return mono_generic_class_init_trampoline;
885 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
886 return mono_rgctx_lazy_fetch_trampoline;
887 #ifdef MONO_ARCH_AOT_SUPPORTED
888 case MONO_TRAMPOLINE_AOT:
889 return mono_aot_trampoline;
890 case MONO_TRAMPOLINE_AOT_PLT:
891 return mono_aot_plt_trampoline;
893 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
894 case MONO_TRAMPOLINE_DELEGATE:
895 return mono_delegate_trampoline;
897 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
898 return mono_altstack_restore_prot;
899 case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
900 return mono_generic_virtual_remoting_trampoline;
901 case MONO_TRAMPOLINE_MONITOR_ENTER:
902 return mono_monitor_enter_trampoline;
903 case MONO_TRAMPOLINE_MONITOR_EXIT:
904 return mono_monitor_exit_trampoline;
906 case MONO_TRAMPOLINE_LLVM_VCALL:
907 return mono_llvm_vcall_trampoline;
910 g_assert_not_reached ();
916 mono_trampolines_init (void)
918 InitializeCriticalSection (&trampolines_mutex);
923 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
924 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
925 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
926 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
927 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
928 #ifdef MONO_ARCH_AOT_SUPPORTED
929 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
930 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
932 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
933 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
935 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
936 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
937 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
938 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
940 mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
945 mono_trampolines_cleanup (void)
947 if (class_init_hash_addr)
948 g_hash_table_destroy (class_init_hash_addr);
949 if (delegate_trampoline_hash_addr)
950 g_hash_table_destroy (delegate_trampoline_hash_addr);
952 DeleteCriticalSection (&trampolines_mutex);
956 mono_get_trampoline_code (MonoTrampolineType tramp_type)
958 g_assert (mono_trampoline_code [tramp_type]);
960 return mono_trampoline_code [tramp_type];
964 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
967 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
969 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
973 mono_create_class_init_trampoline (MonoVTable *vtable)
976 MonoDomain *domain = vtable->domain;
978 g_assert (!vtable->klass->generic_container);
980 /* previously created trampoline code */
981 mono_domain_lock (domain);
983 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
985 mono_domain_unlock (domain);
989 code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
991 ptr = mono_create_ftnptr (domain, code);
993 /* store trampoline address */
994 mono_domain_lock (domain);
995 g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
997 mono_domain_unlock (domain);
999 mono_trampolines_lock ();
1000 if (!class_init_hash_addr)
1001 class_init_hash_addr = g_hash_table_new (NULL, NULL);
1002 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
1003 mono_trampolines_unlock ();
1009 mono_create_generic_class_init_trampoline (void)
1011 #ifdef MONO_ARCH_VTABLE_REG
1012 static gpointer code;
1014 mono_trampolines_lock ();
1018 code = mono_aot_get_named_code ("generic_class_init_trampoline");
1020 code = mono_arch_create_generic_class_init_trampoline ();
1023 mono_trampolines_unlock ();
1027 g_assert_not_reached ();
1032 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
1036 guint32 code_size = 0;
1038 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1040 * We cannot recover the correct type of a shared generic
1041 * method from its native code address, so we use the
1042 * trampoline instead.
1044 if (code && !ji->has_generic_jit_info)
1047 mono_domain_lock (domain);
1048 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1049 mono_domain_unlock (domain);
1053 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1054 g_assert (code_size);
1056 ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
1057 ji->code_start = code;
1058 ji->code_size = code_size;
1059 ji->method = method;
1062 * mono_delegate_ctor needs to find the method metadata from the
1063 * trampoline address, so we save it here.
1066 mono_jit_info_table_add (domain, ji);
1068 mono_domain_lock (domain);
1069 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1070 mono_domain_unlock (domain);
1072 return ji->code_start;
1076 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1080 if (mono_aot_only) {
1081 /* Avoid creating trampolines if possible */
1082 gpointer code = mono_jit_find_compiled_method (domain, method);
1088 mono_domain_lock (domain);
1089 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1090 mono_domain_unlock (domain);
1094 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1096 mono_domain_lock (domain);
1097 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1098 mono_domain_unlock (domain);
1100 mono_jit_stats.method_trampolines++;
1106 mono_create_jit_trampoline (MonoMethod *method)
1108 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1112 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1116 MonoDomain *domain = mono_domain_get ();
1117 guint8 *buf, *start;
1119 buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1121 *(gpointer*)(gpointer)buf = image;
1122 buf += sizeof (gpointer);
1123 *(guint32*)(gpointer)buf = token;
1125 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1127 mono_jit_stats.method_trampolines++;
1133 mono_create_delegate_trampoline (MonoClass *klass)
1135 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1136 MonoDomain *domain = mono_domain_get ();
1138 guint32 code_size = 0;
1139 gpointer *tramp_data;
1142 mono_domain_lock (domain);
1143 ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1144 mono_domain_unlock (domain);
1148 // Precompute the delegate invoke impl and pass it to the delegate trampoline
1149 invoke = mono_get_delegate_invoke (klass);
1152 tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1153 tramp_data [0] = invoke;
1154 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1155 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1157 ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1158 g_assert (code_size);
1160 /* store trampoline address */
1161 mono_domain_lock (domain);
1162 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1164 mono_domain_unlock (domain);
1166 mono_trampolines_lock ();
1167 if (!delegate_trampoline_hash_addr)
1168 delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1169 g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
1170 mono_trampolines_unlock ();
1179 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1181 static gboolean inited = FALSE;
1182 static int num_trampolines = 0;
1184 gpointer tramp, ptr;
1187 return mono_aot_get_lazy_fetch_trampoline (offset);
1189 mono_trampolines_lock ();
1190 if (rgctx_lazy_fetch_trampoline_hash)
1191 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1194 mono_trampolines_unlock ();
1198 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
1199 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1201 mono_trampolines_lock ();
1202 if (!rgctx_lazy_fetch_trampoline_hash) {
1203 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1204 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1206 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1207 g_assert (offset != -1);
1208 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1209 mono_trampolines_unlock ();
1212 mono_counters_register ("RGCTX num lazy fetch trampolines",
1213 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1222 mono_create_monitor_enter_trampoline (void)
1224 static gpointer code;
1226 if (mono_aot_only) {
1228 code = mono_aot_get_named_code ("monitor_enter_trampoline");
1232 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1233 mono_trampolines_lock ();
1236 code = mono_arch_create_monitor_enter_trampoline ();
1238 mono_trampolines_unlock ();
1241 g_assert_not_reached ();
1248 mono_create_monitor_exit_trampoline (void)
1250 static gpointer code;
1252 if (mono_aot_only) {
1254 code = mono_aot_get_named_code ("monitor_exit_trampoline");
1258 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1259 mono_trampolines_lock ();
1262 code = mono_arch_create_monitor_exit_trampoline ();
1264 mono_trampolines_unlock ();
1267 g_assert_not_reached ();
1274 * mono_create_llvm_vcall_trampoline:
1276 * LLVM emits code for virtual calls which mono_get_vcall_slot is unable to
1277 * decode, i.e. only the final branch address is available:
1278 * mov <offset>(%rax), %rax
1279 * <random code inserted by instruction scheduling>
1282 * To work around this problem, we don't use the common vtable trampoline when
1283 * llvm is enabled. Instead, we use one trampoline per method.
1286 mono_create_llvm_vcall_trampoline (MonoMethod *method)
1291 domain = mono_domain_get ();
1293 mono_domain_lock (domain);
1294 res = g_hash_table_lookup (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method);
1295 mono_domain_unlock (domain);
1299 res = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_LLVM_VCALL, domain, NULL);
1301 mono_domain_lock (domain);
1302 g_hash_table_insert (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method, res);
1303 mono_domain_unlock (domain);
1310 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1314 mono_trampolines_lock ();
1315 if (class_init_hash_addr)
1316 res = g_hash_table_lookup (class_init_hash_addr, addr);
1319 mono_trampolines_unlock ();
1324 mono_find_delegate_trampoline_by_addr (gconstpointer addr)
1328 mono_trampolines_lock ();
1329 if (delegate_trampoline_hash_addr)
1330 res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
1333 mono_trampolines_unlock ();
1338 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1342 mono_trampolines_lock ();
1343 if (rgctx_lazy_fetch_trampoline_hash_addr) {
1344 /* We store the real offset + 1 so we can detect when the lookup fails */
1345 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1353 mono_trampolines_unlock ();