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);
92 mono_get_vcall_slot_addr (guint8* code, gpointer *regs)
96 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
99 return (gpointer*)((char*)vt + displacement);
102 #ifdef MONO_ARCH_HAVE_IMT
105 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
107 MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
108 MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
109 MonoVTable *vt = this_argument->vtable;
110 int displacement = slot - ((gpointer*)vt);
112 if (displacement > 0) {
113 /* slot is in the vtable, not in the IMT */
115 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
119 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
120 int interface_offset;
121 int imt_slot = MONO_IMT_SIZE + displacement;
123 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
125 if (interface_offset < 0) {
126 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));
127 g_assert_not_reached ();
129 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
134 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
135 MonoGenericContext context = { NULL, NULL };
138 * Generic virtual method, imt_method contains the inflated interface
139 * method, need to get the inflated impl method.
141 /* imt_method->slot might not be set */
142 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
144 if (impl->klass->generic_class)
145 context.class_inst = impl->klass->generic_class->context.class_inst;
146 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
147 impl = mono_class_inflate_generic_method (impl, &context);
149 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + imt_method->slot);
154 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
155 method->klass->name_space, method->klass->name, method->name,
156 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
159 g_assert (imt_slot < MONO_IMT_SIZE);
160 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
161 int vtable_offset = interface_offset + mono_method_get_vtable_index (imt_method);
162 gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
164 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);
169 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
178 * mono_magic_trampoline:
180 * This trampoline handles calls from JITted code.
183 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
186 gpointer *vtable_slot;
187 gboolean generic_shared = FALSE;
188 MonoMethod *declaring = NULL;
189 MonoMethod *generic_virtual = NULL;
191 gboolean proxy = FALSE;
192 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
193 gboolean need_rgctx_tramp = FALSE;
196 if (m == MONO_FAKE_VTABLE_METHOD) {
198 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
203 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
205 printf ("Caller: %s\n", mono_method_full_name (ji->method, TRUE));
206 /* Print some debug info */
207 for (i = 0; i < 32; ++i)
208 printf ("0x%x ", code [-32 + i]);
212 if (displacement > 0) {
213 displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
214 g_assert (displacement >= 0);
215 displacement /= sizeof (gpointer);
217 /* Avoid loading metadata or creating a generic vtable if possible */
218 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
219 if (addr && !vt->klass->valuetype) {
220 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
221 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
222 *vtable_slot = mono_get_addr_from_ftnptr (addr);
228 m = mono_class_get_vtable_entry (vt->klass, displacement);
229 /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
231 /* We got here from an interface method: redirect to IMT handling */
232 m = MONO_FAKE_IMT_METHOD;
233 /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
237 /* this is the IMT trampoline */
238 #ifdef MONO_ARCH_HAVE_IMT
239 if (m == MONO_FAKE_IMT_METHOD) {
240 MonoMethod *impl_method;
241 MonoGenericSharingContext *gsctx;
242 MonoObject *this_arg;
244 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
245 * needs the signature to be able to find the this argument
247 m = mono_arch_find_imt_method ((gpointer*)regs, code);
248 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
249 g_assert (vtable_slot);
251 gsctx = mono_get_generic_context_from_code (code);
252 this_arg = mono_arch_find_this_argument ((gpointer*)regs, m, gsctx);
254 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
255 /* Use the slow path for now */
257 m = mono_object_get_virtual_method (this_arg, m);
259 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
260 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
261 * to be called, so we compile it and go ahead as usual.
263 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
264 if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
265 /* Generic virtual method */
268 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
269 need_rgctx_tramp = TRUE;
271 m = mono_marshal_get_static_rgctx_invoke (m);
281 MonoGenericContext context = { NULL, NULL };
282 MonoMethod *declaring;
285 declaring = mono_method_get_declaring_generic_method (m);
289 if (m->klass->generic_class)
290 context.class_inst = m->klass->generic_class->context.class_inst;
292 g_assert (!m->klass->generic_container);
294 #ifdef MONO_ARCH_HAVE_IMT
295 generic_virtual = mono_arch_find_imt_method ((gpointer*)regs, code);
297 if (generic_virtual) {
298 g_assert (generic_virtual->is_inflated);
299 context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
302 m = mono_class_inflate_generic_method (declaring, &context);
303 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
304 need_rgctx_tramp = TRUE;
306 /* FIXME: only do this if the method is sharable */
307 m = mono_marshal_get_static_rgctx_invoke (m);
309 } else if ((context_used = mono_method_check_context_used (m))) {
310 MonoClass *klass = NULL;
311 MonoMethod *actual_method = NULL;
312 MonoVTable *vt = NULL;
313 MonoGenericInst *method_inst = NULL;
316 generic_shared = TRUE;
320 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
321 #ifdef MONO_ARCH_RGCTX_REG
322 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable ((gpointer*)regs, code);
324 klass = mrgctx->class_vtable->klass;
325 method_inst = mrgctx->method_inst;
327 g_assert_not_reached ();
329 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
330 #ifdef MONO_ARCH_RGCTX_REG
331 MonoVTable *vtable = mono_arch_find_static_call_vtable ((gpointer*)regs, code);
333 klass = vtable->klass;
335 g_assert_not_reached ();
338 #ifdef MONO_ARCH_HAVE_IMT
339 MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
340 mono_get_generic_context_from_code (code));
342 vt = this_argument->vtable;
343 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
345 g_assert (this_argument->vtable->klass->inited);
346 //mono_class_init (this_argument->vtable->klass);
349 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
355 g_assert (vtable_slot || klass);
358 int displacement = vtable_slot - ((gpointer*)vt);
360 g_assert_not_reached ();
362 g_assert (displacement > 0);
364 actual_method = vt->klass->vtable [displacement];
368 MonoGenericContext context = { NULL, NULL };
371 declaring = mono_method_get_declaring_generic_method (m);
375 if (klass->generic_class)
376 context.class_inst = klass->generic_class->context.class_inst;
377 else if (klass->generic_container)
378 context.class_inst = klass->generic_container->context.class_inst;
379 context.method_inst = method_inst;
381 actual_method = mono_class_inflate_generic_method (declaring, &context);
383 actual_method = mono_class_get_method_generic (klass, m);
387 g_assert (actual_method->klass == klass);
389 if (actual_method->is_inflated)
390 declaring = mono_method_get_declaring_generic_method (actual_method);
397 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
401 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
405 /* Avoid recursion */
406 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
407 m = mono_marshal_get_synchronized_wrapper (m);
410 /* Calls made through delegates on platforms without delegate trampolines */
411 if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
412 m = mono_marshal_get_static_rgctx_invoke (m);
414 addr = mono_compile_method (m);
417 mono_debugger_trampoline_compiled (m, addr);
419 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
420 if (need_rgctx_tramp)
421 addr = mono_create_static_rgctx_trampoline (m, addr);
424 if (generic_virtual) {
426 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
428 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
429 g_assert (vtable_slot);
431 if (vt->klass->valuetype)
432 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
434 mono_method_add_generic_virtual_invocation (mono_domain_get (),
436 generic_virtual, addr);
441 /* the method was jumped to */
443 MonoDomain *domain = mono_domain_get ();
445 /* Patch the got entries pointing to this method */
447 * We do this here instead of in mono_codegen () to cover the case when m
448 * was loaded from an aot image.
450 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
453 mono_domain_lock (domain);
454 list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
456 for (tmp = list; tmp; tmp = tmp->next) {
457 gpointer *got_slot = tmp->data;
460 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
463 mono_domain_unlock (domain);
469 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
472 if (m->klass->valuetype)
473 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
474 g_assert (*vtable_slot);
476 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
477 #ifdef MONO_ARCH_HAVE_IMT
478 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
480 *vtable_slot = mono_get_addr_from_ftnptr (addr);
484 guint8 *plt_entry = mono_aot_get_plt_entry (code);
487 mono_arch_patch_plt_entry (plt_entry, addr);
488 } else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
489 mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
490 if (generic_shared) {
491 if (m->wrapper_type != MONO_WRAPPER_NONE)
492 m = mono_marshal_method_from_wrapper (m);
493 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
496 /* Patch calling code */
501 mono_jit_info_table_find (mono_domain_get (), (char*)code);
502 MonoJitInfo *target_ji =
503 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
505 if (mono_method_same_domain (ji, target_ji))
506 mono_arch_patch_callsite (ji->code_start, code, addr);
516 * mono_llvm_vcall_trampoline:
518 * This trampoline handles virtual calls when using LLVM.
521 mono_llvm_vcall_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
526 gpointer *vtable_slot;
527 gboolean proxy = FALSE;
530 * We have the method which is called, we need to obtain the vtable slot without
531 * disassembly which is impossible with LLVM.
532 * So we use the this argument.
534 this = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
539 /* This is a simplified version of mono_magic_trampoline () */
540 /* FIXME: Avoid code duplication */
542 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
546 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
550 /* Avoid recursion */
551 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
552 m = mono_marshal_get_synchronized_wrapper (m);
555 addr = mono_compile_method (m);
558 if (m->klass->valuetype)
559 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
561 vtable_slot = &(vt->vtable [mono_method_get_vtable_slot (m)]);
562 g_assert (*vtable_slot);
564 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
565 #ifdef MONO_ARCH_HAVE_IMT
566 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
568 *vtable_slot = mono_get_addr_from_ftnptr (addr);
571 mono_debugger_trampoline_compiled (m, addr);
578 mono_generic_virtual_remoting_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
580 MonoGenericContext context = { NULL, NULL };
581 MonoMethod *imt_method, *declaring;
584 g_assert (m->is_generic);
587 declaring = mono_method_get_declaring_generic_method (m);
591 if (m->klass->generic_class)
592 context.class_inst = m->klass->generic_class->context.class_inst;
594 g_assert (!m->klass->generic_container);
596 #ifdef MONO_ARCH_HAVE_IMT
597 imt_method = mono_arch_find_imt_method ((gpointer*)regs, code);
598 if (imt_method->is_inflated)
599 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
601 m = mono_class_inflate_generic_method (declaring, &context);
602 m = mono_marshal_get_remoting_invoke_with_check (m);
604 addr = mono_compile_method (m);
607 mono_debugger_trampoline_compiled (m, addr);
613 * mono_aot_trampoline:
615 * This trampoline handles calls made from AOT code. We try to bypass the
616 * normal JIT compilation logic to avoid loading the metadata for the method.
618 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
620 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
625 MonoMethod *method = NULL;
627 gpointer *vtable_slot;
628 gboolean is_got_entry;
631 image = *(gpointer*)(gpointer)token_info;
632 token_info += sizeof (gpointer);
633 token = *(guint32*)(gpointer)token_info;
635 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
637 method = mono_get_method (image, token, NULL);
640 /* Use the generic code */
641 return mono_magic_trampoline (regs, code, method, tramp);
644 vtable_slot = mono_get_vcall_slot_addr (code, (gpointer*)regs);
645 g_assert (!vtable_slot);
647 /* This is a normal call through a PLT entry */
648 plt_entry = mono_aot_get_plt_entry (code);
649 g_assert (plt_entry);
651 mono_arch_patch_plt_entry (plt_entry, addr);
653 is_got_entry = FALSE;
656 * Since AOT code is only used in the root domain,
657 * mono_domain_get () != mono_get_root_domain () means the calling method
658 * is AppDomain:InvokeInDomain, so this is the same check as in
659 * mono_method_same_domain () but without loading the metadata for the method.
661 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
662 #ifdef MONO_ARCH_HAVE_IMT
664 method = mono_get_method (image, token, NULL);
665 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
674 * mono_aot_plt_trampoline:
676 * This trampoline handles calls made from AOT code through the PLT table.
679 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
682 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
684 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
689 * mono_class_init_trampoline:
691 * This method calls mono_runtime_class_init () to run the static constructor
692 * for the type, then patches the caller code so it is not called again.
695 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
697 guint8 *plt_entry = mono_aot_get_plt_entry (code);
699 mono_runtime_class_init (vtable);
702 mono_arch_nullify_plt_entry (plt_entry);
704 mono_arch_nullify_class_init_trampoline (code, regs);
709 * mono_generic_class_init_trampoline:
711 * This method calls mono_runtime_class_init () to run the static constructor
715 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
717 g_assert (!vtable->initialized);
719 mono_runtime_class_init (vtable);
723 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
725 #ifdef MONO_ARCH_VTABLE_REG
726 static gboolean inited = FALSE;
727 static int num_lookups = 0;
728 guint32 slot = GPOINTER_TO_UINT (data);
729 gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
730 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
731 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
734 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
741 return mono_method_fill_runtime_generic_context (arg, index);
743 return mono_class_fill_runtime_generic_context (arg, index);
745 g_assert_not_reached ();
750 mono_monitor_enter_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
752 mono_monitor_enter (obj);
756 mono_monitor_exit_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
758 mono_monitor_exit (obj);
761 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
764 * mono_delegate_trampoline:
766 * This trampoline handles calls made to Delegate:Invoke ().
767 * This is called once the first time a delegate is invoked, so it must be fast.
770 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
772 MonoDomain *domain = mono_domain_get ();
773 MonoDelegate *delegate;
776 MonoMethod *method = NULL;
777 gboolean multicast, callvirt;
778 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
779 gboolean need_rgctx_tramp = FALSE;
781 MonoMethod *invoke = tramp_data [0];
782 guint8 *impl_this = tramp_data [1];
783 guint8 *impl_nothis = tramp_data [2];
785 /* Obtain the delegate object according to the calling convention */
788 * Avoid calling mono_get_generic_context_from_code () now since it is expensive,
789 * get_this_arg_from_call will call it if needed.
791 delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
793 if (delegate->method) {
794 method = delegate->method;
797 * delegate->method_ptr == NULL means the delegate was initialized by
798 * mini_delegate_ctor, while != NULL means it is initialized by
799 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
800 * (ctor_with_method () does this, but it doesn't store the wrapper back into
803 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
804 method = mono_marshal_get_remoting_invoke (method);
805 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
806 method = mono_marshal_get_unbox_wrapper (method);
808 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
812 callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
814 if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
815 method = mono_marshal_get_synchronized_wrapper (method);
817 if (method && mono_method_needs_static_rgctx_invoke (method, FALSE)) {
818 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
819 need_rgctx_tramp = TRUE;
821 method = mono_marshal_get_static_rgctx_invoke (method);
826 * If the called address is a trampoline, replace it with the compiled method so
827 * further calls don't have to go through the trampoline.
829 if (method && !callvirt) {
830 /* Avoid the overhead of looking up an already compiled method if possible */
831 if (delegate->method_code && *delegate->method_code) {
832 delegate->method_ptr = *delegate->method_code;
834 delegate->method_ptr = mono_compile_method (method);
835 if (delegate->method_code)
836 *delegate->method_code = delegate->method_ptr;
837 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
841 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
842 if (need_rgctx_tramp)
843 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
846 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
847 if (!multicast && !callvirt) {
848 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
849 /* Closed static delegate */
852 code = delegate->target ? impl_this : impl_nothis;
855 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
860 /* The general, unoptimized case */
861 m = mono_marshal_get_delegate_invoke (invoke, delegate);
862 code = mono_compile_method (m);
863 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
864 mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
872 * mono_get_trampoline_func:
874 * Return the C function which needs to be called by the generic trampoline of type
878 mono_get_trampoline_func (MonoTrampolineType tramp_type)
880 switch (tramp_type) {
881 case MONO_TRAMPOLINE_JIT:
882 case MONO_TRAMPOLINE_JUMP:
883 return mono_magic_trampoline;
884 case MONO_TRAMPOLINE_CLASS_INIT:
885 return mono_class_init_trampoline;
886 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
887 return mono_generic_class_init_trampoline;
888 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
889 return mono_rgctx_lazy_fetch_trampoline;
890 #ifdef MONO_ARCH_AOT_SUPPORTED
891 case MONO_TRAMPOLINE_AOT:
892 return mono_aot_trampoline;
893 case MONO_TRAMPOLINE_AOT_PLT:
894 return mono_aot_plt_trampoline;
896 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
897 case MONO_TRAMPOLINE_DELEGATE:
898 return mono_delegate_trampoline;
900 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
901 return mono_altstack_restore_prot;
902 case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
903 return mono_generic_virtual_remoting_trampoline;
904 case MONO_TRAMPOLINE_MONITOR_ENTER:
905 return mono_monitor_enter_trampoline;
906 case MONO_TRAMPOLINE_MONITOR_EXIT:
907 return mono_monitor_exit_trampoline;
909 case MONO_TRAMPOLINE_LLVM_VCALL:
910 return mono_llvm_vcall_trampoline;
913 g_assert_not_reached ();
919 mono_trampolines_init (void)
921 InitializeCriticalSection (&trampolines_mutex);
926 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
927 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
928 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
929 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
930 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
931 #ifdef MONO_ARCH_AOT_SUPPORTED
932 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
933 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
935 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
936 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
938 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
939 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
940 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
941 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
943 mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
948 mono_trampolines_cleanup (void)
950 if (class_init_hash_addr)
951 g_hash_table_destroy (class_init_hash_addr);
952 if (delegate_trampoline_hash_addr)
953 g_hash_table_destroy (delegate_trampoline_hash_addr);
955 DeleteCriticalSection (&trampolines_mutex);
959 mono_get_trampoline_code (MonoTrampolineType tramp_type)
961 g_assert (mono_trampoline_code [tramp_type]);
963 return mono_trampoline_code [tramp_type];
967 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
970 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
972 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
976 mono_create_class_init_trampoline (MonoVTable *vtable)
979 MonoDomain *domain = vtable->domain;
981 g_assert (!vtable->klass->generic_container);
983 /* previously created trampoline code */
984 mono_domain_lock (domain);
986 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
988 mono_domain_unlock (domain);
992 code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
994 ptr = mono_create_ftnptr (domain, code);
996 /* store trampoline address */
997 mono_domain_lock (domain);
998 g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
1000 mono_domain_unlock (domain);
1002 mono_trampolines_lock ();
1003 if (!class_init_hash_addr)
1004 class_init_hash_addr = g_hash_table_new (NULL, NULL);
1005 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
1006 mono_trampolines_unlock ();
1012 mono_create_generic_class_init_trampoline (void)
1014 #ifdef MONO_ARCH_VTABLE_REG
1015 static gpointer code;
1017 mono_trampolines_lock ();
1021 code = mono_aot_get_named_code ("generic_class_init_trampoline");
1023 code = mono_arch_create_generic_class_init_trampoline ();
1026 mono_trampolines_unlock ();
1030 g_assert_not_reached ();
1035 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
1039 guint32 code_size = 0;
1041 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1043 * We cannot recover the correct type of a shared generic
1044 * method from its native code address, so we use the
1045 * trampoline instead.
1047 if (code && !ji->has_generic_jit_info)
1050 mono_domain_lock (domain);
1051 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1052 mono_domain_unlock (domain);
1056 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1057 g_assert (code_size);
1059 ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
1060 ji->code_start = code;
1061 ji->code_size = code_size;
1062 ji->method = method;
1065 * mono_delegate_ctor needs to find the method metadata from the
1066 * trampoline address, so we save it here.
1069 mono_jit_info_table_add (domain, ji);
1071 mono_domain_lock (domain);
1072 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1073 mono_domain_unlock (domain);
1075 return ji->code_start;
1079 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1083 if (mono_aot_only) {
1084 /* Avoid creating trampolines if possible */
1085 gpointer code = mono_jit_find_compiled_method (domain, method);
1091 mono_domain_lock (domain);
1092 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1093 mono_domain_unlock (domain);
1097 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1099 mono_domain_lock (domain);
1100 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1101 mono_domain_unlock (domain);
1103 mono_jit_stats.method_trampolines++;
1109 mono_create_jit_trampoline (MonoMethod *method)
1111 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1114 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1116 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1120 MonoDomain *domain = mono_domain_get ();
1121 guint8 *buf, *start;
1123 buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1125 *(gpointer*)(gpointer)buf = image;
1126 buf += sizeof (gpointer);
1127 *(guint32*)(gpointer)buf = token;
1129 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1131 mono_jit_stats.method_trampolines++;
1138 mono_create_delegate_trampoline (MonoClass *klass)
1140 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1141 MonoDomain *domain = mono_domain_get ();
1143 guint32 code_size = 0;
1144 gpointer *tramp_data;
1147 mono_domain_lock (domain);
1148 ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1149 mono_domain_unlock (domain);
1153 // Precompute the delegate invoke impl and pass it to the delegate trampoline
1154 invoke = mono_get_delegate_invoke (klass);
1157 tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1158 tramp_data [0] = invoke;
1159 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1160 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1162 ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1163 g_assert (code_size);
1165 /* store trampoline address */
1166 mono_domain_lock (domain);
1167 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1169 mono_domain_unlock (domain);
1171 mono_trampolines_lock ();
1172 if (!delegate_trampoline_hash_addr)
1173 delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1174 g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
1175 mono_trampolines_unlock ();
1184 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1186 static gboolean inited = FALSE;
1187 static int num_trampolines = 0;
1189 gpointer tramp, ptr;
1192 return mono_aot_get_lazy_fetch_trampoline (offset);
1194 mono_trampolines_lock ();
1195 if (rgctx_lazy_fetch_trampoline_hash)
1196 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1199 mono_trampolines_unlock ();
1203 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
1204 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1206 mono_trampolines_lock ();
1207 if (!rgctx_lazy_fetch_trampoline_hash) {
1208 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1209 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1211 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1212 g_assert (offset != -1);
1213 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1214 mono_trampolines_unlock ();
1217 mono_counters_register ("RGCTX num lazy fetch trampolines",
1218 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1227 mono_create_monitor_enter_trampoline (void)
1229 static gpointer code;
1231 if (mono_aot_only) {
1233 code = mono_aot_get_named_code ("monitor_enter_trampoline");
1237 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1238 mono_trampolines_lock ();
1241 code = mono_arch_create_monitor_enter_trampoline ();
1243 mono_trampolines_unlock ();
1246 g_assert_not_reached ();
1253 mono_create_monitor_exit_trampoline (void)
1255 static gpointer code;
1257 if (mono_aot_only) {
1259 code = mono_aot_get_named_code ("monitor_exit_trampoline");
1263 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1264 mono_trampolines_lock ();
1267 code = mono_arch_create_monitor_exit_trampoline ();
1269 mono_trampolines_unlock ();
1272 g_assert_not_reached ();
1279 * mono_create_llvm_vcall_trampoline:
1281 * LLVM emits code for virtual calls which mono_get_vcall_slot is unable to
1282 * decode, i.e. only the final branch address is available:
1283 * mov <offset>(%rax), %rax
1284 * <random code inserted by instruction scheduling>
1287 * To work around this problem, we don't use the common vtable trampoline when
1288 * llvm is enabled. Instead, we use one trampoline per method.
1291 mono_create_llvm_vcall_trampoline (MonoMethod *method)
1296 domain = mono_domain_get ();
1298 mono_domain_lock (domain);
1299 res = g_hash_table_lookup (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method);
1300 mono_domain_unlock (domain);
1304 res = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_LLVM_VCALL, domain, NULL);
1306 mono_domain_lock (domain);
1307 g_hash_table_insert (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method, res);
1308 mono_domain_unlock (domain);
1315 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1319 mono_trampolines_lock ();
1320 if (class_init_hash_addr)
1321 res = g_hash_table_lookup (class_init_hash_addr, addr);
1324 mono_trampolines_unlock ();
1329 mono_find_delegate_trampoline_by_addr (gconstpointer addr)
1333 mono_trampolines_lock ();
1334 if (delegate_trampoline_hash_addr)
1335 res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
1338 mono_trampolines_unlock ();
1343 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1347 mono_trampolines_lock ();
1348 if (rgctx_lazy_fetch_trampoline_hash_addr) {
1349 /* We store the real offset + 1 so we can detect when the lookup fails */
1350 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1358 mono_trampolines_unlock ();