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_IMT
45 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
47 MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
48 MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
49 MonoVTable *vt = this_argument->vtable;
50 int displacement = slot - ((gpointer*)vt);
52 if (displacement > 0) {
53 /* slot is in the vtable, not in the IMT */
55 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
59 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
61 int imt_slot = MONO_IMT_SIZE + displacement;
63 mono_class_setup_vtable (vt->klass);
64 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
66 if (interface_offset < 0) {
67 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));
68 g_assert_not_reached ();
70 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
73 *impl_method = vt->klass->vtable [interface_offset + imt_method->slot];
75 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
76 method->klass->name_space, method->klass->name, method->name,
77 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
79 g_assert (imt_slot < MONO_IMT_SIZE);
80 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
81 int vtable_offset = interface_offset + mono_method_get_vtable_index (imt_method);
82 gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
84 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);
89 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
98 * mono_magic_trampoline:
100 * This trampoline handles calls from JITted code.
103 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
106 gpointer *vtable_slot;
107 gboolean generic_shared = FALSE;
108 MonoMethod *declaring = NULL;
109 MonoGenericInst *generic_virtual_method_inst = NULL;
112 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
113 if (m == MONO_FAKE_VTABLE_METHOD) {
115 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
117 if (displacement > 0) {
118 displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
119 g_assert (displacement >= 0);
120 displacement /= sizeof (gpointer);
122 /* Avoid loading metadata or creating a generic vtable if possible */
123 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
124 if (addr && !vt->klass->valuetype) {
125 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
126 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
127 *vtable_slot = mono_get_addr_from_ftnptr (addr);
133 mono_class_setup_vtable (vt->klass);
134 m = vt->klass->vtable [displacement];
135 /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
137 /* We got here from an interface method: redirect to IMT handling */
138 m = MONO_FAKE_IMT_METHOD;
139 /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
143 /* this is the IMT trampoline */
144 #ifdef MONO_ARCH_HAVE_IMT
145 if (m == MONO_FAKE_IMT_METHOD) {
146 MonoMethod *impl_method;
147 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
148 * needs the signature to be able to find the this argument
150 m = mono_arch_find_imt_method ((gpointer*)regs, code);
151 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
152 g_assert (vtable_slot);
153 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
154 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
155 * to be called, so we compile it and go ahead as usual.
157 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
163 MonoGenericContext context = { NULL, NULL };
164 MonoMethod *declaring;
167 declaring = mono_method_get_declaring_generic_method (m);
171 if (m->klass->generic_class)
172 context.class_inst = m->klass->generic_class->context.class_inst;
174 g_assert (!m->klass->generic_container);
176 generic_virtual_method_inst = (MonoGenericInst*)mono_arch_find_imt_method ((gpointer*)regs, code);
177 context.method_inst = generic_virtual_method_inst;
179 m = mono_class_inflate_generic_method (declaring, &context);
180 /* FIXME: only do this if the method is sharable */
181 m = mono_marshal_get_static_rgctx_invoke (m);
182 } else if ((context_used = mono_method_check_context_used (m))) {
183 MonoClass *klass = NULL;
184 MonoMethod *actual_method = NULL;
185 MonoVTable *vt = NULL;
186 MonoGenericInst *method_inst = NULL;
189 generic_shared = TRUE;
193 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
194 #ifdef MONO_ARCH_RGCTX_REG
195 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable ((gpointer*)regs, code);
197 klass = mrgctx->class_vtable->klass;
198 method_inst = mrgctx->method_inst;
200 g_assert_not_reached ();
202 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
203 #ifdef MONO_ARCH_RGCTX_REG
204 MonoVTable *vtable = mono_arch_find_static_call_vtable ((gpointer*)regs, code);
206 klass = vtable->klass;
208 g_assert_not_reached ();
211 #ifdef MONO_ARCH_HAVE_IMT
212 MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
213 mono_get_generic_context_from_code (code));
215 vt = this_argument->vtable;
216 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
218 g_assert (this_argument->vtable->klass->inited);
219 //mono_class_init (this_argument->vtable->klass);
222 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
228 g_assert (vtable_slot || klass);
231 int displacement = vtable_slot - ((gpointer*)vt);
233 g_assert_not_reached ();
235 g_assert (displacement > 0);
237 actual_method = vt->klass->vtable [displacement];
241 MonoGenericContext context = { NULL, NULL };
244 declaring = mono_method_get_declaring_generic_method (m);
248 if (klass->generic_class)
249 context.class_inst = klass->generic_class->context.class_inst;
250 else if (klass->generic_container)
251 context.class_inst = klass->generic_container->context.class_inst;
252 context.method_inst = method_inst;
254 actual_method = mono_class_inflate_generic_method (declaring, &context);
256 actual_method = mono_class_get_method_generic (klass, m);
260 g_assert (actual_method->klass == klass);
262 if (actual_method->is_inflated)
263 declaring = mono_method_get_declaring_generic_method (actual_method);
270 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
274 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
278 /* Avoid recursion */
279 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
280 m = mono_marshal_get_synchronized_wrapper (m);
283 addr = mono_compile_method (m);
286 mono_debugger_trampoline_compiled (m, addr);
288 if (generic_virtual_method_inst) {
289 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
290 g_assert (vtable_slot);
292 mono_method_add_generic_virtual_invocation (mono_domain_get (), vtable_slot,
293 generic_virtual_method_inst, addr);
298 /* the method was jumped to */
300 MonoDomain *domain = mono_domain_get ();
302 /* Patch the got entries pointing to this method */
304 * We do this here instead of in mono_codegen () to cover the case when m
305 * was loaded from an aot image.
307 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
310 mono_domain_lock (domain);
311 list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
313 for (tmp = list; tmp; tmp = tmp->next) {
314 gpointer *got_slot = tmp->data;
317 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
320 mono_domain_unlock (domain);
326 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
329 if (m->klass->valuetype)
330 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
332 g_assert (*vtable_slot);
334 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
335 #ifdef MONO_ARCH_HAVE_IMT
336 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
338 *vtable_slot = mono_get_addr_from_ftnptr (addr);
341 else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
342 mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
343 guint8 *plt_entry = mono_aot_get_plt_entry (code);
345 if (generic_shared) {
346 if (m->wrapper_type != MONO_WRAPPER_NONE)
347 m = mono_marshal_method_from_wrapper (m);
348 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
351 /* Patch calling code */
353 mono_arch_patch_plt_entry (plt_entry, addr);
356 mono_jit_info_table_find (mono_domain_get (), (char*)code);
357 MonoJitInfo *target_ji =
358 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
360 if (mono_method_same_domain (ji, target_ji))
361 mono_arch_patch_callsite (ji->code_start, code, addr);
369 * mono_aot_trampoline:
371 * This trampoline handles calls made from AOT code. We try to bypass the
372 * normal JIT compilation logic to avoid loading the metadata for the method.
374 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
376 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
381 MonoMethod *method = NULL;
383 gpointer *vtable_slot;
384 gboolean is_got_entry;
387 image = *(gpointer*)(gpointer)token_info;
388 token_info += sizeof (gpointer);
389 token = *(guint32*)(gpointer)token_info;
391 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
393 method = mono_get_method (image, token, NULL);
396 /* Use the generic code */
397 return mono_magic_trampoline (regs, code, method, tramp);
400 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
401 g_assert (!vtable_slot);
403 /* This is a normal call through a PLT entry */
404 plt_entry = mono_aot_get_plt_entry (code);
405 g_assert (plt_entry);
407 mono_arch_patch_plt_entry (plt_entry, addr);
409 is_got_entry = FALSE;
412 * Since AOT code is only used in the root domain,
413 * mono_domain_get () != mono_get_root_domain () means the calling method
414 * is AppDomain:InvokeInDomain, so this is the same check as in
415 * mono_method_same_domain () but without loading the metadata for the method.
417 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
418 #ifdef MONO_ARCH_HAVE_IMT
420 method = mono_get_method (image, token, NULL);
421 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
430 * mono_aot_plt_trampoline:
432 * This trampoline handles calls made from AOT code through the PLT table.
435 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
438 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
440 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
445 * mono_class_init_trampoline:
447 * This method calls mono_runtime_class_init () to run the static constructor
448 * for the type, then patches the caller code so it is not called again.
451 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
453 guint8 *plt_entry = mono_aot_get_plt_entry (code);
455 mono_runtime_class_init (vtable);
457 if (!mono_running_on_valgrind ()) {
459 mono_arch_nullify_plt_entry (plt_entry);
461 mono_arch_nullify_class_init_trampoline (code, regs);
467 * mono_generic_class_init_trampoline:
469 * This method calls mono_runtime_class_init () to run the static constructor
473 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
475 g_assert (!vtable->initialized);
477 mono_runtime_class_init (vtable);
481 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
483 #ifdef MONO_ARCH_VTABLE_REG
484 static gboolean inited = FALSE;
485 static int num_lookups = 0;
486 guint32 slot = GPOINTER_TO_UINT (data);
487 gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
488 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
489 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
492 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
499 return mono_method_fill_runtime_generic_context (arg, index);
501 return mono_class_fill_runtime_generic_context (arg, index);
503 g_assert_not_reached ();
507 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
510 * mono_delegate_trampoline:
512 * This trampoline handles calls made to Delegate:Invoke ().
513 * This is called once the first time a delegate is invoked, so it must be fast.
516 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
518 MonoDomain *domain = mono_domain_get ();
519 MonoDelegate *delegate;
522 MonoMethod *method = NULL;
523 gboolean multicast, callvirt;
524 MonoMethod *invoke = tramp_data [0];
525 guint8 *impl_this = tramp_data [1];
526 guint8 *impl_nothis = tramp_data [2];
528 /* Obtain the delegate object according to the calling convention */
531 * Avoid calling mono_get_generic_context_from_code () now since it is expensive,
532 * get_this_arg_from_call will call it if needed.
534 delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
536 if (delegate->method) {
537 method = delegate->method;
540 * delegate->method_ptr == NULL means the delegate was initialized by
541 * mini_delegate_ctor, while != NULL means it is initialized by
542 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
543 * (ctor_with_method () does this, but it doesn't store the wrapper back into
546 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
547 method = mono_marshal_get_remoting_invoke (method);
548 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
549 method = mono_marshal_get_unbox_wrapper (method);
551 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
555 callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
557 if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
558 method = mono_marshal_get_synchronized_wrapper (method);
561 * If the called address is a trampoline, replace it with the compiled method so
562 * further calls don't have to go through the trampoline.
564 if (method && !callvirt) {
565 /* Avoid the overhead of looking up an already compiled method if possible */
566 if (delegate->method_code && *delegate->method_code) {
567 delegate->method_ptr = *delegate->method_code;
569 delegate->method_ptr = mono_compile_method (method);
570 if (delegate->method_code)
571 *delegate->method_code = delegate->method_ptr;
572 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
576 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
577 if (!multicast && !callvirt) {
578 code = delegate->target ? impl_this : impl_nothis;
581 delegate->invoke_impl = code;
586 /* The general, unoptimized case */
587 m = mono_marshal_get_delegate_invoke (invoke, delegate);
588 code = mono_compile_method (m);
589 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
590 mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
598 * mono_get_trampoline_func:
600 * Return the C function which needs to be called by the generic trampoline of type
604 mono_get_trampoline_func (MonoTrampolineType tramp_type)
606 switch (tramp_type) {
607 case MONO_TRAMPOLINE_JIT:
608 case MONO_TRAMPOLINE_JUMP:
609 return mono_magic_trampoline;
610 case MONO_TRAMPOLINE_CLASS_INIT:
611 return mono_class_init_trampoline;
612 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
613 return mono_generic_class_init_trampoline;
614 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
615 return mono_rgctx_lazy_fetch_trampoline;
616 #ifdef MONO_ARCH_AOT_SUPPORTED
617 case MONO_TRAMPOLINE_AOT:
618 return mono_aot_trampoline;
619 case MONO_TRAMPOLINE_AOT_PLT:
620 return mono_aot_plt_trampoline;
622 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
623 case MONO_TRAMPOLINE_DELEGATE:
624 return mono_delegate_trampoline;
626 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
627 return mono_altstack_restore_prot;
629 g_assert_not_reached ();
635 mono_trampolines_init (void)
637 InitializeCriticalSection (&trampolines_mutex);
642 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
643 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
644 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
645 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
646 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
647 #ifdef MONO_ARCH_AOT_SUPPORTED
648 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
649 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
651 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
652 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
654 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
658 mono_trampolines_cleanup (void)
660 if (class_init_hash_addr)
661 g_hash_table_destroy (class_init_hash_addr);
662 if (delegate_trampoline_hash_addr)
663 g_hash_table_destroy (delegate_trampoline_hash_addr);
665 DeleteCriticalSection (&trampolines_mutex);
669 mono_get_trampoline_code (MonoTrampolineType tramp_type)
671 g_assert (mono_trampoline_code [tramp_type]);
673 return mono_trampoline_code [tramp_type];
677 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
680 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
682 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
686 mono_create_class_init_trampoline (MonoVTable *vtable)
689 MonoDomain *domain = vtable->domain;
691 g_assert (!vtable->klass->generic_container);
693 /* previously created trampoline code */
694 mono_domain_lock (domain);
696 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
698 mono_domain_unlock (domain);
702 code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
704 ptr = mono_create_ftnptr (domain, code);
706 /* store trampoline address */
707 mono_domain_lock (domain);
708 g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
710 mono_domain_unlock (domain);
712 mono_trampolines_lock ();
713 if (!class_init_hash_addr)
714 class_init_hash_addr = g_hash_table_new (NULL, NULL);
715 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
716 mono_trampolines_unlock ();
722 mono_create_generic_class_init_trampoline (void)
724 #ifdef MONO_ARCH_VTABLE_REG
725 static gpointer code;
727 mono_trampolines_lock ();
730 code = mono_arch_create_generic_class_init_trampoline ();
732 mono_trampolines_unlock ();
736 g_assert_not_reached ();
741 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
745 guint32 code_size = 0;
747 code = mono_jit_find_compiled_method (domain, method);
751 mono_domain_lock (domain);
752 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
753 mono_domain_unlock (domain);
757 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
758 g_assert (code_size);
760 mono_domain_lock (domain);
761 ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
762 mono_domain_unlock (domain);
763 ji->code_start = code;
764 ji->code_size = code_size;
768 * mono_delegate_ctor needs to find the method metadata from the
769 * trampoline address, so we save it here.
772 mono_jit_info_table_add (domain, ji);
774 mono_domain_lock (domain);
775 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
776 mono_domain_unlock (domain);
778 return ji->code_start;
782 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
787 /* Avoid creating trampolines if possible */
788 gpointer code = mono_jit_find_compiled_method (domain, method);
794 mono_domain_lock (domain);
795 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
796 mono_domain_unlock (domain);
800 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
802 mono_domain_lock (domain);
803 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
804 mono_domain_unlock (domain);
806 mono_jit_stats.method_trampolines++;
812 mono_create_jit_trampoline (MonoMethod *method)
814 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
817 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
819 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
823 MonoDomain *domain = mono_domain_get ();
826 mono_domain_lock (domain);
827 buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
828 mono_domain_unlock (domain);
830 *(gpointer*)(gpointer)buf = image;
831 buf += sizeof (gpointer);
832 *(guint32*)(gpointer)buf = token;
834 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
836 mono_jit_stats.method_trampolines++;
843 mono_create_delegate_trampoline (MonoClass *klass)
845 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
846 MonoDomain *domain = mono_domain_get ();
848 guint32 code_size = 0;
849 gpointer *tramp_data;
852 mono_domain_lock (domain);
853 ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
854 mono_domain_unlock (domain);
858 // Precompute the delegate invoke impl and pass it to the delegate trampoline
859 invoke = mono_get_delegate_invoke (klass);
862 mono_domain_lock (domain );
863 tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
864 mono_domain_unlock (domain);
865 tramp_data [0] = invoke;
867 tramp_data [1] = NULL;
868 tramp_data [2] = NULL;
870 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
871 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
874 ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
875 g_assert (code_size);
877 /* store trampoline address */
878 mono_domain_lock (domain);
879 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
881 mono_domain_unlock (domain);
883 mono_trampolines_lock ();
884 if (!delegate_trampoline_hash_addr)
885 delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
886 g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
887 mono_trampolines_unlock ();
896 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
898 static gboolean inited = FALSE;
899 static int num_trampolines = 0;
904 return mono_aot_get_lazy_fetch_trampoline (offset);
906 mono_trampolines_lock ();
907 if (rgctx_lazy_fetch_trampoline_hash)
908 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
911 mono_trampolines_unlock ();
915 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
916 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
918 mono_trampolines_lock ();
919 if (!rgctx_lazy_fetch_trampoline_hash) {
920 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
921 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
923 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
924 g_assert (offset != -1);
925 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
926 mono_trampolines_unlock ();
929 mono_counters_register ("RGCTX num lazy fetch trampolines",
930 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
939 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
943 mono_trampolines_lock ();
944 if (class_init_hash_addr)
945 res = g_hash_table_lookup (class_init_hash_addr, addr);
948 mono_trampolines_unlock ();
953 mono_find_delegate_trampoline_by_addr (gconstpointer addr)
957 mono_trampolines_lock ();
958 if (delegate_trampoline_hash_addr)
959 res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
962 mono_trampolines_unlock ();
967 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
971 mono_trampolines_lock ();
972 if (rgctx_lazy_fetch_trampoline_hash_addr) {
973 /* We store the real offset + 1 so we can detect when the lookup fails */
974 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
982 mono_trampolines_unlock ();