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>
16 #ifdef MONO_ARCH_HAVE_IMT
19 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, MonoMethod *method) {
20 MonoObject *this_argument = mono_arch_find_this_argument (regs, method);
21 MonoVTable *vt = this_argument->vtable;
22 int displacement = slot - ((gpointer*)vt);
24 if (displacement > 0) {
25 /* slot is in the vtable, not in the IMT */
27 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
31 MonoMethod *imt_method = mono_arch_find_imt_method (regs);
32 int interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
33 int imt_slot = MONO_IMT_SIZE + displacement;
36 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
37 method->klass->name_space, method->klass->name, method->name,
38 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
40 g_assert (imt_slot < MONO_IMT_SIZE);
41 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
42 int vtable_offset = interface_offset + imt_method->slot;
43 gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
45 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);
47 g_assert (vtable_offset >= 0);
51 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
60 * mono_magic_trampoline:
62 * This trampoline handles calls from JITted code.
65 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
68 gpointer *vtable_slot;
70 addr = mono_compile_method (m);
73 /* the method was jumped to */
77 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
80 if (m->klass->valuetype)
81 addr = mono_arch_get_unbox_trampoline (m, addr);
83 g_assert (*vtable_slot);
85 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
86 #ifdef MONO_ARCH_HAVE_IMT
87 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, m);
89 *vtable_slot = mono_get_addr_from_ftnptr (addr);
93 guint8 *plt_entry = mono_aot_get_plt_entry (code);
95 /* Patch calling code */
97 mono_arch_patch_plt_entry (plt_entry, addr);
100 mono_jit_info_table_find (mono_domain_get (), (char*)code);
101 MonoJitInfo *target_ji =
102 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
104 if (mono_method_same_domain (ji, target_ji))
105 mono_arch_patch_callsite (code, addr);
113 * mono_aot_trampoline:
115 * This trampoline handles calls made from AOT code. We try to bypass the
116 * normal JIT compilation logic to avoid loading the metadata for the method.
118 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
120 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
125 MonoMethod *method = NULL;
127 gpointer *vtable_slot;
128 gboolean is_got_entry;
130 image = *(gpointer*)(gpointer)token_info;
131 token_info += sizeof (gpointer);
132 token = *(guint32*)(gpointer)token_info;
134 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
136 method = mono_get_method (image, token, NULL);
139 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
141 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
142 method = mono_marshal_get_synchronized_wrapper (method);
144 addr = mono_compile_method (method);
148 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
151 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
155 method = mono_get_method (image, token, NULL);
156 if (method->klass->valuetype)
157 addr = mono_arch_get_unbox_trampoline (method, addr);
160 /* This is a normal call through a PLT entry */
161 guint8 *plt_entry = mono_aot_get_plt_entry (code);
163 g_assert (plt_entry);
165 mono_arch_patch_plt_entry (plt_entry, addr);
167 is_got_entry = FALSE;
171 * Since AOT code is only used in the root domain,
172 * mono_domain_get () != mono_get_root_domain () means the calling method
173 * is AppDomain:InvokeInDomain, so this is the same check as in
174 * mono_method_same_domain () but without loading the metadata for the method.
176 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
183 * mono_aot_plt_trampoline:
185 * This trampoline handles calls made from AOT code through the PLT table.
188 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
191 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
192 guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
194 guint32 plt_info_offset = -1;
197 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
202 * mono_class_init_trampoline:
204 * This method calls mono_runtime_class_init () to run the static constructor
205 * for the type, then patches the caller code so it is not called again.
208 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
210 guint8 *plt_entry = mono_aot_get_plt_entry (code);
212 mono_runtime_class_init (vtable);
214 if (!mono_running_on_valgrind ()) {
216 mono_arch_nullify_plt_entry (plt_entry);
218 mono_arch_nullify_class_init_trampoline (code, regs);
223 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
226 * mono_delegate_trampoline:
228 * This trampoline handles calls made to Delegate:Invoke ().
231 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
233 MonoDomain *domain = mono_domain_get ();
234 MonoDelegate *delegate;
240 /* Find the Invoke method */
242 while ((invoke = mono_class_get_methods (klass, &iter))) {
243 if (!strcmp (invoke->name, "Invoke"))
248 /* Obtain the delegate object according to the calling convention */
250 delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
253 * If the called address is a trampoline, replace it with the compiled method so
254 * further calls don't have to go through the trampoline.
256 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
258 delegate->method_ptr = mono_compile_method (ji->method);
260 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
265 mono_domain_lock (domain);
266 if (delegate->target != NULL) {
267 if (!domain->delegate_invoke_impl_with_target_hash)
268 domain->delegate_invoke_impl_with_target_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
269 cache = domain->delegate_invoke_impl_with_target_hash;
271 if (!domain->delegate_invoke_impl_no_target_hash)
272 domain->delegate_invoke_impl_no_target_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
273 cache = domain->delegate_invoke_impl_no_target_hash;
275 code = g_hash_table_lookup (cache, mono_method_signature (invoke));
276 mono_domain_unlock (domain);
278 delegate->invoke_impl = code;
282 code = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), delegate->target != NULL);
285 mono_domain_lock (domain);
286 g_hash_table_insert (cache, mono_method_signature (invoke), code);
287 mono_domain_unlock (domain);
289 delegate->invoke_impl = code;
294 /* The general, unoptimized case */
295 delegate->invoke_impl = mono_compile_method (mono_marshal_get_delegate_invoke (invoke));
296 return delegate->invoke_impl;