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;
386 image = *(gpointer*)(gpointer)token_info;
387 token_info += sizeof (gpointer);
388 token = *(guint32*)(gpointer)token_info;
390 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
392 method = mono_get_method (image, token, NULL);
395 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
397 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
398 method = mono_marshal_get_synchronized_wrapper (method);
400 addr = mono_compile_method (method);
404 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
407 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
411 method = mono_get_method (image, token, NULL);
412 if (method->klass->valuetype)
413 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), method, addr);
416 /* This is a normal call through a PLT entry */
417 guint8 *plt_entry = mono_aot_get_plt_entry (code);
419 g_assert (plt_entry);
421 mono_arch_patch_plt_entry (plt_entry, addr);
423 is_got_entry = FALSE;
427 * Since AOT code is only used in the root domain,
428 * mono_domain_get () != mono_get_root_domain () means the calling method
429 * is AppDomain:InvokeInDomain, so this is the same check as in
430 * mono_method_same_domain () but without loading the metadata for the method.
432 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
433 #ifdef MONO_ARCH_HAVE_IMT
435 method = mono_get_method (image, token, NULL);
436 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
445 * mono_aot_plt_trampoline:
447 * This trampoline handles calls made from AOT code through the PLT table.
450 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
453 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
455 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
460 * mono_class_init_trampoline:
462 * This method calls mono_runtime_class_init () to run the static constructor
463 * for the type, then patches the caller code so it is not called again.
466 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
468 guint8 *plt_entry = mono_aot_get_plt_entry (code);
470 mono_runtime_class_init (vtable);
472 if (!mono_running_on_valgrind ()) {
474 mono_arch_nullify_plt_entry (plt_entry);
476 mono_arch_nullify_class_init_trampoline (code, regs);
482 * mono_generic_class_init_trampoline:
484 * This method calls mono_runtime_class_init () to run the static constructor
488 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
490 g_assert (!vtable->initialized);
492 mono_runtime_class_init (vtable);
496 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
498 #ifdef MONO_ARCH_VTABLE_REG
499 static gboolean inited = FALSE;
500 static int num_lookups = 0;
501 guint32 slot = GPOINTER_TO_UINT (data);
502 gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
503 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
504 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
507 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
514 return mono_method_fill_runtime_generic_context (arg, index);
516 return mono_class_fill_runtime_generic_context (arg, index);
518 g_assert_not_reached ();
522 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
525 * mono_delegate_trampoline:
527 * This trampoline handles calls made to Delegate:Invoke ().
528 * This is called once the first time a delegate is invoked, so it must be fast.
531 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
533 MonoDomain *domain = mono_domain_get ();
534 MonoDelegate *delegate;
537 MonoMethod *method = NULL;
538 gboolean multicast, callvirt;
539 MonoMethod *invoke = tramp_data [0];
540 guint8 *impl_this = tramp_data [1];
541 guint8 *impl_nothis = tramp_data [2];
543 /* Obtain the delegate object according to the calling convention */
546 * Avoid calling mono_get_generic_context_from_code () now since it is expensive,
547 * get_this_arg_from_call will call it if needed.
549 delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
551 if (delegate->method) {
552 method = delegate->method;
555 * delegate->method_ptr == NULL means the delegate was initialized by
556 * mini_delegate_ctor, while != NULL means it is initialized by
557 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
558 * (ctor_with_method () does this, but it doesn't store the wrapper back into
561 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
562 method = mono_marshal_get_remoting_invoke (method);
563 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
564 method = mono_marshal_get_unbox_wrapper (method);
566 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
570 callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
572 if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
573 method = mono_marshal_get_synchronized_wrapper (method);
576 * If the called address is a trampoline, replace it with the compiled method so
577 * further calls don't have to go through the trampoline.
579 if (method && !callvirt) {
580 /* Avoid the overhead of looking up an already compiled method if possible */
581 if (delegate->method_code && *delegate->method_code) {
582 delegate->method_ptr = *delegate->method_code;
584 delegate->method_ptr = mono_compile_method (method);
585 if (delegate->method_code)
586 *delegate->method_code = delegate->method_ptr;
587 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
591 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
592 if (!multicast && !callvirt) {
593 code = delegate->target ? impl_this : impl_nothis;
596 delegate->invoke_impl = code;
601 /* The general, unoptimized case */
602 m = mono_marshal_get_delegate_invoke (invoke, delegate);
603 code = mono_compile_method (m);
604 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
605 mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
613 * mono_get_trampoline_func:
615 * Return the C function which needs to be called by the generic trampoline of type
619 mono_get_trampoline_func (MonoTrampolineType tramp_type)
621 switch (tramp_type) {
622 case MONO_TRAMPOLINE_JIT:
623 case MONO_TRAMPOLINE_JUMP:
624 return mono_magic_trampoline;
625 case MONO_TRAMPOLINE_CLASS_INIT:
626 return mono_class_init_trampoline;
627 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
628 return mono_generic_class_init_trampoline;
629 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
630 return mono_rgctx_lazy_fetch_trampoline;
631 #ifdef MONO_ARCH_AOT_SUPPORTED
632 case MONO_TRAMPOLINE_AOT:
633 return mono_aot_trampoline;
634 case MONO_TRAMPOLINE_AOT_PLT:
635 return mono_aot_plt_trampoline;
637 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
638 case MONO_TRAMPOLINE_DELEGATE:
639 return mono_delegate_trampoline;
641 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
642 return mono_altstack_restore_prot;
644 g_assert_not_reached ();
650 mono_trampolines_init (void)
652 InitializeCriticalSection (&trampolines_mutex);
657 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
658 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
659 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
660 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
661 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
662 #ifdef MONO_ARCH_AOT_SUPPORTED
663 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
664 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
666 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
667 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
669 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
673 mono_trampolines_cleanup (void)
675 if (class_init_hash_addr)
676 g_hash_table_destroy (class_init_hash_addr);
677 if (delegate_trampoline_hash_addr)
678 g_hash_table_destroy (delegate_trampoline_hash_addr);
680 DeleteCriticalSection (&trampolines_mutex);
684 mono_get_trampoline_code (MonoTrampolineType tramp_type)
686 g_assert (mono_trampoline_code [tramp_type]);
688 return mono_trampoline_code [tramp_type];
692 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
695 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
697 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
701 mono_create_class_init_trampoline (MonoVTable *vtable)
704 MonoDomain *domain = vtable->domain;
706 g_assert (!vtable->klass->generic_container);
708 /* previously created trampoline code */
709 mono_domain_lock (domain);
711 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
713 mono_domain_unlock (domain);
717 code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
719 ptr = mono_create_ftnptr (domain, code);
721 /* store trampoline address */
722 mono_domain_lock (domain);
723 g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
725 mono_domain_unlock (domain);
727 mono_trampolines_lock ();
728 if (!class_init_hash_addr)
729 class_init_hash_addr = g_hash_table_new (NULL, NULL);
730 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
731 mono_trampolines_unlock ();
737 mono_create_generic_class_init_trampoline (void)
739 #ifdef MONO_ARCH_VTABLE_REG
740 static gpointer code;
742 mono_trampolines_lock ();
745 code = mono_arch_create_generic_class_init_trampoline ();
747 mono_trampolines_unlock ();
751 g_assert_not_reached ();
756 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
760 guint32 code_size = 0;
762 code = mono_jit_find_compiled_method (domain, method);
766 mono_domain_lock (domain);
767 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
768 mono_domain_unlock (domain);
772 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
773 g_assert (code_size);
775 mono_domain_lock (domain);
776 ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
777 mono_domain_unlock (domain);
778 ji->code_start = code;
779 ji->code_size = code_size;
783 * mono_delegate_ctor needs to find the method metadata from the
784 * trampoline address, so we save it here.
787 mono_jit_info_table_add (domain, ji);
789 mono_domain_lock (domain);
790 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
791 mono_domain_unlock (domain);
793 return ji->code_start;
797 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
802 /* Avoid creating trampolines if possible */
803 gpointer code = mono_jit_find_compiled_method (domain, method);
809 mono_domain_lock (domain);
810 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
811 mono_domain_unlock (domain);
815 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
817 mono_domain_lock (domain);
818 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
819 mono_domain_unlock (domain);
821 mono_jit_stats.method_trampolines++;
827 mono_create_jit_trampoline (MonoMethod *method)
829 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
832 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
834 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
838 MonoDomain *domain = mono_domain_get ();
841 mono_domain_lock (domain);
842 buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
843 mono_domain_unlock (domain);
845 *(gpointer*)(gpointer)buf = image;
846 buf += sizeof (gpointer);
847 *(guint32*)(gpointer)buf = token;
849 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
851 mono_jit_stats.method_trampolines++;
858 mono_create_delegate_trampoline (MonoClass *klass)
860 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
861 MonoDomain *domain = mono_domain_get ();
863 guint32 code_size = 0;
864 gpointer *tramp_data;
867 mono_domain_lock (domain);
868 ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
869 mono_domain_unlock (domain);
873 // Precompute the delegate invoke impl and pass it to the delegate trampoline
874 invoke = mono_get_delegate_invoke (klass);
877 mono_domain_lock (domain );
878 tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
879 mono_domain_unlock (domain);
880 tramp_data [0] = invoke;
882 tramp_data [1] = NULL;
883 tramp_data [2] = NULL;
885 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
886 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
889 ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
890 g_assert (code_size);
892 /* store trampoline address */
893 mono_domain_lock (domain);
894 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
896 mono_domain_unlock (domain);
898 mono_trampolines_lock ();
899 if (!delegate_trampoline_hash_addr)
900 delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
901 g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
902 mono_trampolines_unlock ();
911 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
913 static gboolean inited = FALSE;
914 static int num_trampolines = 0;
919 return mono_aot_get_lazy_fetch_trampoline (offset);
921 mono_trampolines_lock ();
922 if (rgctx_lazy_fetch_trampoline_hash)
923 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
926 mono_trampolines_unlock ();
930 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
931 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
933 mono_trampolines_lock ();
934 if (!rgctx_lazy_fetch_trampoline_hash) {
935 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
936 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
938 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
939 g_assert (offset != -1);
940 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
941 mono_trampolines_unlock ();
944 mono_counters_register ("RGCTX num lazy fetch trampolines",
945 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
954 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
958 mono_trampolines_lock ();
959 if (class_init_hash_addr)
960 res = g_hash_table_lookup (class_init_hash_addr, addr);
963 mono_trampolines_unlock ();
968 mono_find_delegate_trampoline_by_addr (gconstpointer addr)
972 mono_trampolines_lock ();
973 if (delegate_trampoline_hash_addr)
974 res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
977 mono_trampolines_unlock ();
982 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
986 mono_trampolines_lock ();
987 if (rgctx_lazy_fetch_trampoline_hash_addr) {
988 /* We store the real offset + 1 so we can detect when the lookup fails */
989 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
997 mono_trampolines_unlock ();