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>
10 #ifdef HAVE_VALGRIND_MEMCHECK_H
11 #include <valgrind/memcheck.h>
15 #include "debug-mini.h"
17 static MonoGenericSharingContext*
18 get_generic_context (guint8 *code)
20 MonoJitInfo *jit_info = mono_jit_info_table_find (mono_domain_get (), (char*)code);
24 return mono_jit_info_get_generic_sharing_context (jit_info);
27 #ifdef MONO_ARCH_HAVE_IMT
30 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
32 MonoGenericSharingContext *gsctx = get_generic_context (code);
33 MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
34 MonoVTable *vt = this_argument->vtable;
35 int displacement = slot - ((gpointer*)vt);
37 if (displacement > 0) {
38 /* slot is in the vtable, not in the IMT */
40 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
44 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
46 int imt_slot = MONO_IMT_SIZE + displacement;
48 mono_class_setup_vtable (vt->klass);
49 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
51 if (interface_offset < 0) {
52 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));
53 g_assert_not_reached ();
55 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
58 *impl_method = vt->klass->vtable [interface_offset + imt_method->slot];
60 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
61 method->klass->name_space, method->klass->name, method->name,
62 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
64 g_assert (imt_slot < MONO_IMT_SIZE);
65 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
66 int vtable_offset = interface_offset + imt_method->slot;
67 gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
69 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);
74 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
83 * mono_magic_trampoline:
85 * This trampoline handles calls from JITted code.
88 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
91 gpointer *vtable_slot;
92 gboolean generic_shared = FALSE;
93 MonoMethod *declaring = NULL;
95 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
96 if (m == MONO_FAKE_VTABLE_METHOD) {
98 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
100 if (displacement > 0) {
101 displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
102 g_assert (displacement >= 0);
103 displacement /= sizeof (gpointer);
105 /* Avoid loading metadata or creating a generic vtable if possible */
106 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
107 if (addr && !vt->klass->valuetype) {
108 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
109 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
110 *vtable_slot = mono_get_addr_from_ftnptr (addr);
116 mono_class_setup_vtable (vt->klass);
117 m = vt->klass->vtable [displacement];
118 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
119 m = mono_marshal_get_synchronized_wrapper (m);
120 /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
122 /* We got here from an interface method: redirect to IMT handling */
123 m = MONO_FAKE_IMT_METHOD;
124 /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
128 /* this is the IMT trampoline */
129 #ifdef MONO_ARCH_HAVE_IMT
130 if (m == MONO_FAKE_IMT_METHOD) {
131 MonoMethod *impl_method;
132 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
133 * needs the signature to be able to find the this argument
135 m = mono_arch_find_imt_method ((gpointer*)regs, code);
136 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
137 g_assert (vtable_slot);
138 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
139 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
140 * to be called, so we compile it and go ahead as usual.
142 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
147 if (mono_method_check_context_used (m)) {
148 MonoClass *klass = NULL;
149 MonoMethod *actual_method = NULL;
150 MonoVTable *vt = NULL;
153 generic_shared = TRUE;
158 if (m->flags & METHOD_ATTRIBUTE_STATIC) {
159 #ifdef MONO_ARCH_RGCTX_REG
160 MonoRuntimeGenericContext *rgctx = mono_arch_find_static_call_rgctx ((gpointer*)regs, code);
162 klass = rgctx->vtable->klass;
164 g_assert_not_reached ();
167 #ifdef MONO_ARCH_HAVE_IMT
168 MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
169 get_generic_context (code));
171 vt = this_argument->vtable;
172 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
174 g_assert (this_argument->vtable->klass->inited);
175 //mono_class_init (this_argument->vtable->klass);
178 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
184 g_assert (vtable_slot || klass);
187 int displacement = vtable_slot - ((gpointer*)vt);
189 g_assert_not_reached ();
191 g_assert (displacement > 0);
193 actual_method = vt->klass->vtable [displacement];
198 declaring = mono_method_get_declaring_generic_method (m);
202 for (i = 0; i < klass->method.count; ++i) {
203 actual_method = klass->methods [i];
204 if (actual_method->is_inflated) {
205 if (mono_method_get_declaring_generic_method (actual_method) == declaring)
210 g_assert (mono_method_get_declaring_generic_method (actual_method) == declaring);
213 g_assert (actual_method);
217 addr = mono_compile_method (m);
220 mono_debugger_trampoline_compiled (m, addr);
222 /* the method was jumped to */
226 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
229 if (m->klass->valuetype)
230 addr = mono_arch_get_unbox_trampoline (m, addr);
232 g_assert (*vtable_slot);
234 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
235 #ifdef MONO_ARCH_HAVE_IMT
236 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
238 *vtable_slot = mono_get_addr_from_ftnptr (addr);
241 else if (!generic_shared || mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
242 guint8 *plt_entry = mono_aot_get_plt_entry (code);
244 /* Patch calling code */
246 mono_arch_patch_plt_entry (plt_entry, addr);
249 mono_jit_info_table_find (mono_domain_get (), (char*)code);
250 MonoJitInfo *target_ji =
251 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
253 if (mono_method_same_domain (ji, target_ji))
254 mono_arch_patch_callsite (ji->code_start, code, addr);
262 * mono_aot_trampoline:
264 * This trampoline handles calls made from AOT code. We try to bypass the
265 * normal JIT compilation logic to avoid loading the metadata for the method.
267 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
269 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
274 MonoMethod *method = NULL;
276 gpointer *vtable_slot;
277 gboolean is_got_entry;
279 image = *(gpointer*)(gpointer)token_info;
280 token_info += sizeof (gpointer);
281 token = *(guint32*)(gpointer)token_info;
283 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
285 method = mono_get_method (image, token, NULL);
288 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
290 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
291 method = mono_marshal_get_synchronized_wrapper (method);
293 addr = mono_compile_method (method);
297 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
300 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
304 method = mono_get_method (image, token, NULL);
305 if (method->klass->valuetype)
306 addr = mono_arch_get_unbox_trampoline (method, addr);
309 /* This is a normal call through a PLT entry */
310 guint8 *plt_entry = mono_aot_get_plt_entry (code);
312 g_assert (plt_entry);
314 mono_arch_patch_plt_entry (plt_entry, addr);
316 is_got_entry = FALSE;
320 * Since AOT code is only used in the root domain,
321 * mono_domain_get () != mono_get_root_domain () means the calling method
322 * is AppDomain:InvokeInDomain, so this is the same check as in
323 * mono_method_same_domain () but without loading the metadata for the method.
325 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
326 #ifdef MONO_ARCH_HAVE_IMT
328 method = mono_get_method (image, token, NULL);
329 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
338 * mono_aot_plt_trampoline:
340 * This trampoline handles calls made from AOT code through the PLT table.
343 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
346 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
347 guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
349 guint32 plt_info_offset = -1;
352 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
357 * mono_class_init_trampoline:
359 * This method calls mono_runtime_class_init () to run the static constructor
360 * for the type, then patches the caller code so it is not called again.
363 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
365 guint8 *plt_entry = mono_aot_get_plt_entry (code);
367 mono_runtime_class_init (vtable);
369 if (!mono_running_on_valgrind ()) {
371 mono_arch_nullify_plt_entry (plt_entry);
373 mono_arch_nullify_class_init_trampoline (code, regs);
379 * mono_generic_class_init_trampoline:
381 * This method calls mono_runtime_class_init () to run the static constructor
385 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
387 //g_print ("generic class init for class %s.%s\n", vtable->klass->name_space, vtable->klass->name);
389 mono_runtime_class_init (vtable);
391 //g_print ("done initing generic\n");
395 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, MonoRuntimeGenericContext *rgctx, guint8 *tramp)
397 guint32 encoded_offset = mono_arch_get_rgctx_lazy_fetch_offset ((gpointer*)regs);
400 mono_class_fill_runtime_generic_context (rgctx);
402 if (MONO_RGCTX_OFFSET_IS_INDIRECT (encoded_offset)) {
403 int indirect_slot = MONO_RGCTX_OFFSET_INDIRECT_SLOT (encoded_offset);
404 int offset = MONO_RGCTX_OFFSET_INDIRECT_OFFSET (encoded_offset);
406 g_assert (indirect_slot == 0);
408 result = *(gpointer*)((guint8*)rgctx->extra_other_infos + offset);
410 int offset = MONO_RGCTX_OFFSET_DIRECT_OFFSET (encoded_offset);
412 result = *(gpointer*)((guint8*)rgctx + offset);
420 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
423 * mono_delegate_trampoline:
425 * This trampoline handles calls made to Delegate:Invoke ().
428 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
430 MonoDomain *domain = mono_domain_get ();
431 MonoDelegate *delegate;
433 MonoMethod *invoke, *m;
434 MonoMethod *method = NULL;
435 gboolean multicast, callvirt;
437 invoke = mono_get_delegate_invoke (klass);
440 /* Obtain the delegate object according to the calling convention */
442 delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
444 if (!delegate->method_ptr && delegate->method) {
445 /* The delegate was initialized by mini_delegate_ctor */
446 method = delegate->method;
448 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
449 method = mono_marshal_get_remoting_invoke (method);
450 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
451 method = mono_marshal_get_unbox_wrapper (method);
453 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
457 callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
460 * If the called address is a trampoline, replace it with the compiled method so
461 * further calls don't have to go through the trampoline.
463 if (method && !callvirt) {
464 delegate->method_ptr = mono_compile_method (method);
465 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
468 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
469 if (!multicast && !callvirt) {
470 code = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), delegate->target != NULL);
473 delegate->invoke_impl = code;
478 /* The general, unoptimized case */
479 m = mono_marshal_get_delegate_invoke (invoke, delegate);
480 code = mono_compile_method (m);
481 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
482 mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
490 * mono_get_trampoline_func:
492 * Return the C function which needs to be called by the generic trampoline of type
496 mono_get_trampoline_func (MonoTrampolineType tramp_type)
498 switch (tramp_type) {
499 case MONO_TRAMPOLINE_GENERIC:
500 case MONO_TRAMPOLINE_JUMP:
501 return mono_magic_trampoline;
502 case MONO_TRAMPOLINE_CLASS_INIT:
503 return mono_class_init_trampoline;
504 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
505 return mono_generic_class_init_trampoline;
506 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
507 return mono_rgctx_lazy_fetch_trampoline;
508 #ifdef MONO_ARCH_AOT_SUPPORTED
509 case MONO_TRAMPOLINE_AOT:
510 return mono_aot_trampoline;
511 case MONO_TRAMPOLINE_AOT_PLT:
512 return mono_aot_plt_trampoline;
514 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
515 case MONO_TRAMPOLINE_DELEGATE:
516 return mono_delegate_trampoline;
519 g_assert_not_reached ();