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>
10 #include <mono/utils/mono-error-internals.h>
13 #include "debug-mini.h"
16 * Address of the trampoline code. This is used by the debugger to check
17 * whether a method is a trampoline.
19 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
21 static GHashTable *class_init_hash_addr = NULL;
22 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
23 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
24 static guint32 trampoline_calls;
26 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
27 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
28 static CRITICAL_SECTION trampolines_mutex;
31 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
36 * The unbox trampolines call the method directly, so need to add
37 * an rgctx tramp before them.
39 return mono_create_static_rgctx_trampoline (m, mono_aot_get_unbox_trampoline (m));
41 return mono_aot_get_unbox_trampoline (m);
43 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
47 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
55 rgctx_tramp_info_equal (gconstpointer ka, gconstpointer kb)
57 const RgctxTrampInfo *i1 = ka;
58 const RgctxTrampInfo *i2 = kb;
60 if (i1->m == i2->m && i1->addr == i2->addr)
67 rgctx_tramp_info_hash (gconstpointer data)
69 const RgctxTrampInfo *info = data;
71 return GPOINTER_TO_UINT (info->m) ^ GPOINTER_TO_UINT (info->addr);
75 * mono_create_static_rgctx_trampoline:
77 * Return a static rgctx trampoline for M which branches to ADDR which should
78 * point to the compiled code of M.
80 * Static rgctx trampolines are used when a shared generic method which doesn't
81 * have a this argument is called indirectly, ie. from code which can't pass in
82 * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
83 * methods code. These trampolines are similar to the unbox trampolines, they
84 * perform the same task as the static rgctx wrappers, but they are smaller/faster,
85 * and can be made to work with full AOT.
86 * On PPC addr should be an ftnptr and the return value is an ftnptr too.
89 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
94 RgctxTrampInfo tmp_info;
97 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
98 g_assert (((gpointer*)addr) [2] == 0);
101 if (mini_method_get_context (m)->method_inst)
102 ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
104 ctx = mono_class_vtable (mono_domain_get (), m->klass);
106 domain = mono_domain_get ();
109 * In the AOT case, addr might point to either the method, or to an unbox trampoline,
110 * so make the hash keyed on the m+addr pair.
112 mono_domain_lock (domain);
113 if (!domain_jit_info (domain)->static_rgctx_trampoline_hash)
114 domain_jit_info (domain)->static_rgctx_trampoline_hash = g_hash_table_new (rgctx_tramp_info_hash, rgctx_tramp_info_equal);
116 tmp_info.addr = addr;
117 res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
119 mono_domain_unlock (domain);
124 res = mono_aot_get_static_rgctx_trampoline (ctx, addr);
126 res = mono_arch_get_static_rgctx_trampoline (m, ctx, addr);
128 mono_domain_lock (domain);
129 /* Duplicates inserted while we didn't hold the lock are OK */
130 info = mono_domain_alloc (domain, sizeof (RgctxTrampInfo));
133 g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, info, res);
134 mono_domain_unlock (domain);
140 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
143 * This shouldn't happen as all arches which support generic sharing support
144 * static rgctx trampolines as well.
146 g_assert_not_reached ();
150 #ifdef MONO_ARCH_HAVE_IMT
153 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp, gboolean *variance_used)
155 MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (method), regs, code);
156 MonoVTable *vt = this_argument->vtable;
157 int displacement = slot - ((gpointer*)vt);
159 if (displacement > 0) {
160 /* slot is in the vtable, not in the IMT */
162 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
166 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
167 int interface_offset;
168 int imt_slot = MONO_IMT_SIZE + displacement;
170 /*This has to be variance aware since imt_method can be from an interface that vt->klass doesn't directly implement*/
171 interface_offset = mono_class_interface_offset_with_variance (vt->klass, imt_method->klass, variance_used);
173 if (interface_offset < 0) {
174 g_error ("%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));
176 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
181 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
182 MonoGenericContext context = { NULL, NULL };
185 * Generic virtual method, imt_method contains the inflated interface
186 * method, need to get the inflated impl method.
188 /* imt_method->slot might not be set */
189 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
191 if (impl->klass->generic_class)
192 context.class_inst = impl->klass->generic_class->context.class_inst;
193 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
194 impl = mono_class_inflate_generic_method (impl, &context);
196 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_vtable_slot (imt_method));
199 if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
200 *need_rgctx_tramp = TRUE;
204 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
205 method->klass->name_space, method->klass->name, method->name,
206 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
209 g_assert (imt_slot < MONO_IMT_SIZE);
210 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
211 int slot = mono_method_get_vtable_index (imt_method);
213 gpointer *vtable_slot;
215 g_assert (slot != -1);
216 vtable_offset = interface_offset + slot;
217 vtable_slot = & (vt->vtable [vtable_offset]);
219 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);
224 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
233 * This is a super-ugly hack to fix bug #616463.
235 * The problem is that we don't always set is_generic for generic
236 * method definitions. See the comment at the end of
237 * mono_class_inflate_generic_method_full_checked() in class.c.
240 is_generic_method_definition (MonoMethod *m)
242 MonoGenericContext *context;
248 context = mono_method_get_context (m);
249 if (!context->method_inst)
251 if (context->method_inst == mono_method_get_generic_container (((MonoMethodInflated*)m)->declaring)->context.method_inst)
257 * common_call_trampoline:
259 * The code to handle normal, virtual, and interface method calls and jumps, both
260 * from JITted and LLVM compiled code.
263 common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tramp, MonoVTable *vt, gpointer *vtable_slot, gboolean need_rgctx_tramp)
265 gpointer addr, compiled_method;
266 gboolean generic_shared = FALSE;
267 MonoMethod *declaring = NULL;
268 MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
270 gboolean virtual, proxy = FALSE, variance_used = FALSE;
271 gpointer *orig_vtable_slot, *imt_vcall_slot = NULL;
272 MonoJitInfo *ji = NULL;
274 virtual = (gpointer)vtable_slot > (gpointer)vt;
276 orig_vtable_slot = vtable_slot;
278 #ifdef MONO_ARCH_HAVE_IMT
280 if (vt && (gpointer)vtable_slot < (gpointer)vt) {
281 MonoMethod *impl_method = NULL;
282 MonoObject *this_arg;
284 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
285 * needs the signature to be able to find the this argument
287 m = mono_arch_find_imt_method (regs, code);
288 vtable_slot = orig_vtable_slot;
289 g_assert (vtable_slot);
291 this_arg = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
293 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
294 /* Use the slow path for now */
296 m = mono_object_get_virtual_method (this_arg, m);
298 imt_vcall_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp, &variance_used);
299 vtable_slot = imt_vcall_slot;
300 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
301 * to be called, so we compile it and go ahead as usual.
303 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
304 if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
305 /* Generic virtual method */
307 need_rgctx_tramp = TRUE;
308 } else if (variance_used && mono_class_has_variant_generic_params (m->klass)) {
317 * The virtual check is needed because is_generic_method_definition (m) could
318 * return TRUE for methods used in IMT calls too.
320 if (virtual && is_generic_method_definition (m)) {
321 MonoGenericContext context = { NULL, NULL };
322 MonoMethod *declaring;
325 declaring = mono_method_get_declaring_generic_method (m);
329 if (m->klass->generic_class)
330 context.class_inst = m->klass->generic_class->context.class_inst;
332 g_assert (!m->klass->generic_container);
334 #ifdef MONO_ARCH_HAVE_IMT
335 generic_virtual = mono_arch_find_imt_method (regs, code);
337 if (generic_virtual) {
338 g_assert (generic_virtual->is_inflated);
339 context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
342 m = mono_class_inflate_generic_method (declaring, &context);
343 /* FIXME: only do this if the method is sharable */
344 need_rgctx_tramp = TRUE;
345 } else if ((context_used = mono_method_check_context_used (m))) {
346 MonoClass *klass = NULL;
347 MonoMethod *actual_method = NULL;
348 MonoVTable *vt = NULL;
349 MonoGenericInst *method_inst = NULL;
352 generic_shared = TRUE;
356 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
357 #ifdef MONO_ARCH_RGCTX_REG
358 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable (regs, code);
360 klass = mrgctx->class_vtable->klass;
361 method_inst = mrgctx->method_inst;
363 g_assert_not_reached ();
365 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
366 #ifdef MONO_ARCH_RGCTX_REG
367 MonoVTable *vtable = mono_arch_find_static_call_vtable (regs, code);
369 klass = vtable->klass;
371 g_assert_not_reached ();
374 #ifdef MONO_ARCH_HAVE_IMT
375 MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
377 vt = this_argument->vtable;
378 vtable_slot = orig_vtable_slot;
380 g_assert (this_argument->vtable->klass->inited);
381 //mono_class_init (this_argument->vtable->klass);
384 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
390 g_assert (vtable_slot || klass);
393 int displacement = vtable_slot - ((gpointer*)vt);
395 g_assert_not_reached ();
397 g_assert (displacement > 0);
399 actual_method = vt->klass->vtable [displacement];
403 MonoGenericContext context = { NULL, NULL };
406 declaring = mono_method_get_declaring_generic_method (m);
410 if (klass->generic_class)
411 context.class_inst = klass->generic_class->context.class_inst;
412 else if (klass->generic_container)
413 context.class_inst = klass->generic_container->context.class_inst;
414 context.method_inst = method_inst;
416 actual_method = mono_class_inflate_generic_method (declaring, &context);
418 actual_method = mono_class_get_method_generic (klass, m);
422 g_assert (actual_method);
423 g_assert (actual_method->klass == klass);
425 if (actual_method->is_inflated)
426 declaring = mono_method_get_declaring_generic_method (actual_method);
433 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
437 ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
441 /* Avoid recursion */
442 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
443 m = mono_marshal_get_synchronized_wrapper (m);
446 /* Calls made through delegates on platforms without delegate trampolines */
447 if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
448 need_rgctx_tramp = TRUE;
450 addr = compiled_method = mono_compile_method (m);
453 mono_debugger_trampoline_compiled (code, m, addr);
455 if (need_rgctx_tramp)
456 addr = mono_create_static_rgctx_trampoline (m, addr);
458 if (generic_virtual || variant_iface) {
459 MonoMethod *target = generic_virtual ? generic_virtual : variant_iface;
461 vtable_slot = orig_vtable_slot;
462 g_assert (vtable_slot);
464 if (vt->klass->valuetype) /*FIXME is this required variant iface?*/
465 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
467 mono_method_add_generic_virtual_invocation (mono_domain_get (),
474 /* the method was jumped to */
476 MonoDomain *domain = mono_domain_get ();
478 /* Patch the got entries pointing to this method */
480 * We do this here instead of in mono_codegen () to cover the case when m
481 * was loaded from an aot image.
483 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
486 mono_domain_lock (domain);
487 list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
489 for (tmp = list; tmp; tmp = tmp->next) {
490 gpointer *got_slot = tmp->data;
493 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
496 mono_domain_unlock (domain);
502 vtable_slot = orig_vtable_slot;
505 if (m->klass->valuetype)
506 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
507 g_assert (*vtable_slot);
509 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
511 /* This is the vcall slot which gets called through the IMT thunk */
512 vtable_slot = imt_vcall_slot;
513 *vtable_slot = mono_get_addr_from_ftnptr (addr);
517 guint8 *plt_entry = mono_aot_get_plt_entry (code);
520 mono_aot_patch_plt_entry (plt_entry, NULL, regs, addr);
522 if (generic_shared) {
523 if (m->wrapper_type != MONO_WRAPPER_NONE)
524 m = mono_marshal_method_from_wrapper (m);
525 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
528 /* Patch calling code */
530 MonoJitInfo *target_ji =
531 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (compiled_method));
534 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
536 if (mono_method_same_domain (ji, target_ji))
537 mono_arch_patch_callsite (ji->code_start, code, addr);
546 * mono_magic_trampoline:
548 * This trampoline handles normal calls from JITted code.
551 mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp)
555 return common_call_trampoline (regs, code, arg, tramp, NULL, NULL, FALSE);
559 * mono_vcall_trampoline:
561 * This trampoline handles virtual calls.
564 mono_vcall_trampoline (mgreg_t *regs, guint8 *code, int slot, guint8 *tramp)
568 gpointer *vtable_slot;
570 gboolean need_rgctx_tramp = FALSE;
576 * We need to obtain the following pieces of information:
577 * - the method which needs to be compiled.
579 * We use one vtable trampoline per vtable slot index, so we need only the vtable,
580 * the other two can be computed from the vtable + the slot index.
582 #ifndef MONO_ARCH_THIS_AS_FIRST_ARG
583 /* All architectures should support this */
584 g_assert_not_reached ();
588 * Obtain the vtable from the 'this' arg.
590 this = mono_arch_get_this_arg_from_call (NULL, NULL, regs, code);
596 /* Normal virtual call */
597 vtable_slot = &(vt->vtable [slot]);
599 /* Avoid loading metadata or creating a generic vtable if possible */
600 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, slot);
601 if (addr && !vt->klass->valuetype) {
602 if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
605 return mono_create_ftnptr (mono_domain_get (), addr);
610 * is_generic_method_definition() above) also
611 * goes away if we do a
612 * mono_class_setup_vtable (vt->klass) here,
613 * because we then inflate the method
614 * correctly, put it in the cache, and the
615 * "wrong" inflation invocation still looks up
616 * the correctly inflated method.
618 * The hack above seems more stable and
621 m = mono_class_get_vtable_entry (vt->klass, slot);
623 need_rgctx_tramp = mono_method_needs_static_rgctx_invoke (m, 0);
626 vtable_slot = &(((gpointer*)vt) [slot]);
629 need_rgctx_tramp = FALSE;
632 return common_call_trampoline (regs, code, m, tramp, vt, vtable_slot, need_rgctx_tramp);
636 mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
638 MonoGenericContext context = { NULL, NULL };
639 MonoMethod *imt_method, *declaring;
644 g_assert (m->is_generic);
647 declaring = mono_method_get_declaring_generic_method (m);
651 if (m->klass->generic_class)
652 context.class_inst = m->klass->generic_class->context.class_inst;
654 g_assert (!m->klass->generic_container);
656 #ifdef MONO_ARCH_HAVE_IMT
657 imt_method = mono_arch_find_imt_method (regs, code);
658 if (imt_method->is_inflated)
659 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
661 m = mono_class_inflate_generic_method (declaring, &context);
662 m = mono_marshal_get_remoting_invoke_with_check (m);
664 addr = mono_compile_method (m);
667 mono_debugger_trampoline_compiled (NULL, m, addr);
673 * mono_aot_trampoline:
675 * This trampoline handles calls made from AOT code. We try to bypass the
676 * normal JIT compilation logic to avoid loading the metadata for the method.
678 #ifdef MONO_ARCH_AOT_SUPPORTED
680 mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
685 MonoMethod *method = NULL;
691 image = *(gpointer*)(gpointer)token_info;
692 token_info += sizeof (gpointer);
693 token = *(guint32*)(gpointer)token_info;
695 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
697 method = mono_get_method (image, token, NULL);
700 /* Use the generic code */
701 return mono_magic_trampoline (regs, code, method, tramp);
704 addr = mono_create_ftnptr (mono_domain_get (), addr);
706 /* This is a normal call through a PLT entry */
707 plt_entry = mono_aot_get_plt_entry (code);
708 g_assert (plt_entry);
710 mono_aot_patch_plt_entry (plt_entry, NULL, regs, addr);
716 * mono_aot_plt_trampoline:
718 * This trampoline handles calls made from AOT code through the PLT table.
721 mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *aot_module,
724 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
729 res = mono_aot_plt_resolve (aot_module, plt_info_offset, code);
731 if (mono_loader_get_last_error ())
732 mono_raise_exception (mono_loader_error_prepare_exception (mono_loader_get_last_error ()));
733 // FIXME: Error handling (how ?)
742 * mono_class_init_trampoline:
744 * This method calls mono_runtime_class_init () to run the static constructor
745 * for the type, then patches the caller code so it is not called again.
748 mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
750 guint8 *plt_entry = mono_aot_get_plt_entry (code);
754 mono_runtime_class_init (vtable);
757 mono_arch_nullify_plt_entry (plt_entry, regs);
759 mono_arch_nullify_class_init_trampoline (code, regs);
764 * mono_generic_class_init_trampoline:
766 * This method calls mono_runtime_class_init () to run the static constructor
770 mono_generic_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
774 mono_runtime_class_init (vtable);
778 mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, guint8 *tramp)
780 #ifdef MONO_ARCH_VTABLE_REG
781 static gboolean inited = FALSE;
782 static int num_lookups = 0;
783 guint32 slot = GPOINTER_TO_UINT (data);
784 mgreg_t *r = (mgreg_t*)regs;
785 gpointer arg = (gpointer)(gssize)r [MONO_ARCH_VTABLE_REG];
786 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
787 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
792 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
799 return mono_method_fill_runtime_generic_context (arg, index);
801 return mono_class_fill_runtime_generic_context (arg, index);
803 g_assert_not_reached ();
808 mono_monitor_enter_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
810 mono_monitor_enter (obj);
814 mono_monitor_exit_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
816 mono_monitor_exit (obj);
819 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
822 * mono_delegate_trampoline:
824 * This trampoline handles calls made to Delegate:Invoke ().
825 * This is called once the first time a delegate is invoked, so it must be fast.
828 mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
830 MonoDomain *domain = mono_domain_get ();
831 MonoDelegate *delegate;
834 MonoMethod *method = NULL;
835 gboolean multicast, callvirt = FALSE;
836 gboolean need_rgctx_tramp = FALSE;
837 MonoMethod *invoke = tramp_data [0];
838 guint8 *impl_this = tramp_data [1];
839 guint8 *impl_nothis = tramp_data [2];
841 MonoMethodSignature *sig;
845 /* Obtain the delegate object according to the calling convention */
848 * Avoid calling mono_get_generic_context_from_code () now since it is expensive,
849 * get_this_arg_from_call will call it if needed.
851 delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
853 if (delegate->method) {
854 method = delegate->method;
857 * delegate->method_ptr == NULL means the delegate was initialized by
858 * mini_delegate_ctor, while != NULL means it is initialized by
859 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
860 * (ctor_with_method () does this, but it doesn't store the wrapper back into
863 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class) {
865 if (((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class != mono_defaults.com_object_class &&
866 !((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class->is_com_object)
868 method = mono_marshal_get_remoting_invoke (method);
871 mono_error_init (&err);
872 sig = mono_method_signature_checked (method, &err);
874 mono_error_raise_exception (&err);
876 if (sig->hasthis && method->klass->valuetype)
877 method = mono_marshal_get_unbox_wrapper (method);
880 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
886 mono_error_init (&err);
887 sig = mono_method_signature_checked (method, &err);
889 mono_error_raise_exception (&err);
891 callvirt = !delegate->target && sig->hasthis;
894 if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
895 method = mono_marshal_get_synchronized_wrapper (method);
897 if (method && mono_method_needs_static_rgctx_invoke (method, FALSE))
898 need_rgctx_tramp = TRUE;
901 * If the called address is a trampoline, replace it with the compiled method so
902 * further calls don't have to go through the trampoline.
904 if (method && !callvirt) {
905 /* Avoid the overhead of looking up an already compiled method if possible */
906 if (delegate->method_code && *delegate->method_code) {
907 delegate->method_ptr = *delegate->method_code;
909 delegate->method_ptr = mono_compile_method (method);
910 if (need_rgctx_tramp)
911 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
912 if (delegate->method_code)
913 *delegate->method_code = delegate->method_ptr;
914 mono_debugger_trampoline_compiled (NULL, method, delegate->method_ptr);
917 if (need_rgctx_tramp)
918 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
921 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
922 if (!multicast && !callvirt) {
923 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
924 /* Closed static delegate */
927 code = delegate->target ? impl_this : impl_nothis;
930 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
935 /* The general, unoptimized case */
936 m = mono_marshal_get_delegate_invoke (invoke, delegate);
937 code = mono_compile_method (m);
938 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
939 mono_debugger_trampoline_compiled (NULL, m, delegate->invoke_impl);
946 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
948 mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
952 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
953 gpointer resume_ip = jit_tls->handler_block_return_address;
955 memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
956 MONO_CONTEXT_SET_IP (&ctx, jit_tls->handler_block_return_address);
958 jit_tls->handler_block_return_address = NULL;
959 jit_tls->handler_block = NULL;
961 if (!resume_ip) /*this should not happen, but we should avoid crashing */
962 exc = mono_get_exception_execution_engine ("Invalid internal state, resuming abort after handler block but no resume ip found");
964 exc = mono_thread_resume_interruption ();
967 static void (*restore_context) (MonoContext *);
969 if (!restore_context)
970 restore_context = mono_get_restore_context ();
972 mono_handle_exception (&ctx, exc, NULL, FALSE);
973 restore_context (&ctx);
980 mono_create_handler_block_trampoline (void)
982 static gpointer code;
989 mono_trampolines_lock ();
992 code = mono_arch_create_handler_block_trampoline ();
994 mono_trampolines_unlock ();
1001 * mono_get_trampoline_func:
1003 * Return the C function which needs to be called by the generic trampoline of type
1007 mono_get_trampoline_func (MonoTrampolineType tramp_type)
1009 switch (tramp_type) {
1010 case MONO_TRAMPOLINE_JIT:
1011 case MONO_TRAMPOLINE_JUMP:
1012 return mono_magic_trampoline;
1013 case MONO_TRAMPOLINE_CLASS_INIT:
1014 return mono_class_init_trampoline;
1015 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
1016 return mono_generic_class_init_trampoline;
1017 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
1018 return mono_rgctx_lazy_fetch_trampoline;
1019 #ifdef MONO_ARCH_AOT_SUPPORTED
1020 case MONO_TRAMPOLINE_AOT:
1021 return mono_aot_trampoline;
1022 case MONO_TRAMPOLINE_AOT_PLT:
1023 return mono_aot_plt_trampoline;
1025 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1026 case MONO_TRAMPOLINE_DELEGATE:
1027 return mono_delegate_trampoline;
1029 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
1030 return mono_altstack_restore_prot;
1031 case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
1032 return mono_generic_virtual_remoting_trampoline;
1033 case MONO_TRAMPOLINE_MONITOR_ENTER:
1034 return mono_monitor_enter_trampoline;
1035 case MONO_TRAMPOLINE_MONITOR_EXIT:
1036 return mono_monitor_exit_trampoline;
1037 case MONO_TRAMPOLINE_VCALL:
1038 return mono_vcall_trampoline;
1039 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1040 case MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD:
1041 return mono_handler_block_guard_trampoline;
1044 g_assert_not_reached ();
1050 create_trampoline_code (MonoTrampolineType tramp_type)
1052 MonoTrampInfo *info;
1055 code = mono_arch_create_generic_trampoline (tramp_type, &info, FALSE);
1057 mono_save_trampoline_xdebug_info (info);
1058 mono_tramp_info_free (info);
1065 mono_trampolines_init (void)
1067 InitializeCriticalSection (&trampolines_mutex);
1072 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = create_trampoline_code (MONO_TRAMPOLINE_JIT);
1073 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
1074 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
1075 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
1076 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
1077 #ifdef MONO_ARCH_AOT_SUPPORTED
1078 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = create_trampoline_code (MONO_TRAMPOLINE_AOT);
1079 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
1081 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1082 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
1084 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
1085 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
1086 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
1087 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
1088 mono_trampoline_code [MONO_TRAMPOLINE_VCALL] = create_trampoline_code (MONO_TRAMPOLINE_VCALL);
1089 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1090 mono_trampoline_code [MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD] = create_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1091 mono_create_handler_block_trampoline ();
1094 mono_counters_register ("Calls to trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &trampoline_calls);
1098 mono_trampolines_cleanup (void)
1100 if (class_init_hash_addr)
1101 g_hash_table_destroy (class_init_hash_addr);
1103 DeleteCriticalSection (&trampolines_mutex);
1107 mono_get_trampoline_code (MonoTrampolineType tramp_type)
1109 g_assert (mono_trampoline_code [tramp_type]);
1111 return mono_trampoline_code [tramp_type];
1115 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1118 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
1120 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
1124 mono_create_class_init_trampoline (MonoVTable *vtable)
1127 MonoDomain *domain = vtable->domain;
1129 g_assert (!vtable->klass->generic_container);
1131 /* previously created trampoline code */
1132 mono_domain_lock (domain);
1134 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
1136 mono_domain_unlock (domain);
1140 code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
1142 ptr = mono_create_ftnptr (domain, code);
1144 /* store trampoline address */
1145 mono_domain_lock (domain);
1146 g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
1148 mono_domain_unlock (domain);
1150 mono_trampolines_lock ();
1151 if (!class_init_hash_addr)
1152 class_init_hash_addr = g_hash_table_new (NULL, NULL);
1153 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
1154 mono_trampolines_unlock ();
1160 mono_create_generic_class_init_trampoline (void)
1162 #ifdef MONO_ARCH_VTABLE_REG
1163 static gpointer code;
1164 MonoTrampInfo *info;
1166 mono_trampolines_lock ();
1170 /* get_named_code () might return an ftnptr, but our caller expects a direct pointer */
1171 code = mono_get_addr_from_ftnptr (mono_aot_get_trampoline ("generic_class_init_trampoline"));
1173 code = mono_arch_create_generic_class_init_trampoline (&info, FALSE);
1176 mono_save_trampoline_xdebug_info (info);
1177 mono_tramp_info_free (info);
1182 mono_trampolines_unlock ();
1186 g_assert_not_reached ();
1191 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
1195 guint32 code_size = 0;
1197 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1199 * We cannot recover the correct type of a shared generic
1200 * method from its native code address, so we use the
1201 * trampoline instead.
1203 if (code && !ji->has_generic_jit_info)
1206 mono_domain_lock (domain);
1207 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1208 mono_domain_unlock (domain);
1212 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1213 g_assert (code_size);
1215 ji = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO);
1216 ji->code_start = code;
1217 ji->code_size = code_size;
1218 ji->method = method;
1221 * mono_delegate_ctor needs to find the method metadata from the
1222 * trampoline address, so we save it here.
1225 mono_jit_info_table_add (domain, ji);
1227 mono_domain_lock (domain);
1228 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1229 mono_domain_unlock (domain);
1231 return ji->code_start;
1235 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1239 if (mono_aot_only) {
1240 /* Avoid creating trampolines if possible */
1241 gpointer code = mono_jit_find_compiled_method (domain, method);
1247 mono_domain_lock (domain);
1248 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1249 mono_domain_unlock (domain);
1253 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1255 mono_domain_lock (domain);
1256 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1257 mono_domain_unlock (domain);
1259 mono_jit_stats.method_trampolines++;
1265 mono_create_jit_trampoline (MonoMethod *method)
1267 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1271 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1275 MonoDomain *domain = mono_domain_get ();
1276 guint8 *buf, *start;
1278 buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1280 *(gpointer*)(gpointer)buf = image;
1281 buf += sizeof (gpointer);
1282 *(guint32*)(gpointer)buf = token;
1284 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1286 mono_jit_stats.method_trampolines++;
1292 mono_create_delegate_trampoline (MonoClass *klass)
1294 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1295 MonoDomain *domain = mono_domain_get ();
1297 guint32 code_size = 0;
1298 gpointer *tramp_data;
1301 mono_domain_lock (domain);
1302 ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1303 mono_domain_unlock (domain);
1307 // Precompute the delegate invoke impl and pass it to the delegate trampoline
1308 invoke = mono_get_delegate_invoke (klass);
1311 tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1312 tramp_data [0] = invoke;
1313 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1314 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1316 ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1317 g_assert (code_size);
1319 /* store trampoline address */
1320 mono_domain_lock (domain);
1321 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1323 mono_domain_unlock (domain);
1332 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1334 static gboolean inited = FALSE;
1335 static int num_trampolines = 0;
1336 MonoTrampInfo *info;
1338 gpointer tramp, ptr;
1341 return mono_aot_get_lazy_fetch_trampoline (offset);
1343 mono_trampolines_lock ();
1344 if (rgctx_lazy_fetch_trampoline_hash)
1345 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1348 mono_trampolines_unlock ();
1352 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, FALSE);
1354 mono_save_trampoline_xdebug_info (info);
1355 mono_tramp_info_free (info);
1357 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1359 mono_trampolines_lock ();
1360 if (!rgctx_lazy_fetch_trampoline_hash) {
1361 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1362 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1364 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1365 g_assert (offset != -1);
1366 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1367 mono_trampolines_unlock ();
1370 mono_counters_register ("RGCTX num lazy fetch trampolines",
1371 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1380 mono_create_monitor_enter_trampoline (void)
1382 static gpointer code;
1384 if (mono_aot_only) {
1386 code = mono_aot_get_trampoline ("monitor_enter_trampoline");
1390 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1391 mono_trampolines_lock ();
1394 code = mono_arch_create_monitor_enter_trampoline (NULL, FALSE);
1396 mono_trampolines_unlock ();
1399 g_assert_not_reached ();
1406 mono_create_monitor_exit_trampoline (void)
1408 static gpointer code;
1410 if (mono_aot_only) {
1412 code = mono_aot_get_trampoline ("monitor_exit_trampoline");
1416 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1417 mono_trampolines_lock ();
1420 code = mono_arch_create_monitor_exit_trampoline (NULL, FALSE);
1422 mono_trampolines_unlock ();
1425 g_assert_not_reached ();
1430 #ifdef MONO_ARCH_LLVM_SUPPORTED
1432 * mono_create_llvm_imt_trampoline:
1434 * LLVM compiled code can't pass in the IMT argument, so we use this trampoline, which
1435 * sets the IMT argument, then branches to the contents of the vtable slot given by
1436 * vt_offset in the vtable which is obtained from the argument list.
1439 mono_create_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
1441 #ifdef MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE
1442 return mono_arch_get_llvm_imt_trampoline (domain, m, vt_offset);
1444 g_assert_not_reached ();
1451 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1455 mono_trampolines_lock ();
1456 if (class_init_hash_addr)
1457 res = g_hash_table_lookup (class_init_hash_addr, addr);
1460 mono_trampolines_unlock ();
1465 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1469 mono_trampolines_lock ();
1470 if (rgctx_lazy_fetch_trampoline_hash_addr) {
1471 /* We store the real offset + 1 so we can detect when the lookup fails */
1472 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1480 mono_trampolines_unlock ();
1484 static const char*tramp_names [MONO_TRAMPOLINE_NUM] = {
1488 "generic_class_init",
1493 "restore_stack_prot",
1494 "generic_virtual_remoting",
1498 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1499 "handler_block_guard"
1504 * mono_get_generic_trampoline_name:
1506 * Returns a pointer to malloc-ed memory.
1509 mono_get_generic_trampoline_name (MonoTrampolineType tramp_type)
1511 return g_strdup_printf ("generic_trampoline_%s", tramp_names [tramp_type]);
1515 * mono_get_rgctx_fetch_trampoline_name:
1517 * Returns a pointer to malloc-ed memory.
1520 mono_get_rgctx_fetch_trampoline_name (int slot)
1525 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
1526 index = MONO_RGCTX_SLOT_INDEX (slot);
1528 return g_strdup_printf ("rgctx_fetch_trampoline_%s_%d", mrgctx ? "mrgctx" : "rgctx", index);