2008-03-14 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-trampolines.c
1
2 #include <config.h>
3 #include <glib.h>
4
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
10 #ifdef HAVE_VALGRIND_MEMCHECK_H
11 #include <valgrind/memcheck.h>
12 #endif
13
14 #include "mini.h"
15 #include "debug-mini.h"
16
17 static MonoGenericSharingContext*
18 get_generic_context (guint8 *code)
19 {
20         MonoJitInfo *jit_info = mono_jit_info_table_find (mono_domain_get (), (char*)code);
21
22         g_assert (jit_info);
23
24         return mono_jit_info_get_generic_sharing_context (jit_info);
25 }
26
27 #ifdef MONO_ARCH_HAVE_IMT
28
29 static gpointer*
30 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
31 {
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);
36
37         if (displacement > 0) {
38                 /* slot is in the vtable, not in the IMT */
39 #if DEBUG_IMT
40                 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
41 #endif
42                 return slot;
43         } else {
44                 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
45                 int interface_offset;
46                 int imt_slot = MONO_IMT_SIZE + displacement;
47
48                 mono_class_setup_vtable (vt->klass);
49                 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
50
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 ();
54                 }
55                 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
56
57                 if (impl_method)
58                         *impl_method = vt->klass->vtable [interface_offset + imt_method->slot];
59 #if DEBUG_IMT
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);
63 #endif
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]);
68 #if DEBUG_IMT
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);
70 #endif
71                         return vtable_slot;
72                 } else {
73 #if DEBUG_IMT
74                         printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
75 #endif
76                         return slot;
77                 }
78         }
79 }
80 #endif
81
82 /**
83  * mono_magic_trampoline:
84  *
85  *   This trampoline handles calls from JITted code.
86  */
87 gpointer
88 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
89 {
90         gpointer addr;
91         gpointer *vtable_slot;
92         gboolean generic_shared = FALSE;
93         MonoMethod *declaring = NULL;
94
95 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
96         if (m == MONO_FAKE_VTABLE_METHOD) {
97                 int displacement;
98                 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
99                 g_assert (vt);
100                 if (displacement > 0) {
101                         displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
102                         g_assert (displacement >= 0);
103                         displacement /= sizeof (gpointer);
104
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);
111                                 }
112
113                                 return addr;
114                         }
115
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);*/
121                 } else {
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);*/
125                 }
126         }
127 #endif
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
134                  */
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.
141                  */
142                 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
143                 m = impl_method;
144         }
145 #endif
146
147         if (mono_method_check_context_used (m)) {
148                 MonoClass *klass = NULL;
149                 MonoMethod *actual_method = NULL;
150                 MonoVTable *vt = NULL;
151
152                 vtable_slot = NULL;
153                 generic_shared = TRUE;
154
155                 g_assert (code);
156
157
158                 if (m->flags & METHOD_ATTRIBUTE_STATIC) {
159                         g_assert_not_reached ();
160                 } else {
161 #ifdef MONO_ARCH_HAVE_IMT
162                         MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
163                                 get_generic_context (code));
164
165                         vt = this_argument->vtable;
166                         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
167
168                         g_assert (this_argument->vtable->klass->inited);
169                         //mono_class_init (this_argument->vtable->klass);
170
171                         if (!vtable_slot)
172                                 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
173 #else
174                         NOT_IMPLEMENTED;
175 #endif
176                 }
177
178                 g_assert (vtable_slot || klass);
179
180                 if (vtable_slot) {
181                         int displacement = vtable_slot - ((gpointer*)vt);
182
183                         g_assert_not_reached ();
184
185                         g_assert (displacement > 0);
186
187                         actual_method = vt->klass->vtable [displacement];
188                 } else {
189                         int i;
190
191                         if (m->is_inflated)
192                                 declaring = mono_method_get_declaring_generic_method (m);
193                         else
194                                 declaring = m;
195
196                         for (i = 0; i < klass->method.count; ++i) {
197                                 actual_method = klass->methods [i];
198                                 if (actual_method->is_inflated) {
199                                         if (mono_method_get_declaring_generic_method (actual_method) == declaring)
200                                                 break;
201                                 }
202                         }
203
204                         g_assert (mono_method_get_declaring_generic_method (actual_method) == declaring);
205                 }
206
207                 g_assert (actual_method);
208                 m = actual_method;
209         }
210
211         addr = mono_compile_method (m);
212         g_assert (addr);
213
214         mono_debugger_trampoline_compiled (m, addr);
215
216         /* the method was jumped to */
217         if (!code)
218                 return addr;
219
220         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
221
222         if (vtable_slot) {
223                 if (m->klass->valuetype)
224                         addr = mono_arch_get_unbox_trampoline (m, addr);
225
226                 g_assert (*vtable_slot);
227
228                 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
229 #ifdef MONO_ARCH_HAVE_IMT
230                         vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
231 #endif
232                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
233                 }
234         }
235         else if (!generic_shared || mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
236                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
237
238                 /* Patch calling code */
239                 if (plt_entry) {
240                         mono_arch_patch_plt_entry (plt_entry, addr);
241                 } else {
242                         MonoJitInfo *ji = 
243                                 mono_jit_info_table_find (mono_domain_get (), (char*)code);
244                         MonoJitInfo *target_ji = 
245                                 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
246
247                         if (mono_method_same_domain (ji, target_ji))
248                                 mono_arch_patch_callsite (code, addr);
249                 }
250         }
251
252         return addr;
253 }
254
255 /*
256  * mono_aot_trampoline:
257  *
258  *   This trampoline handles calls made from AOT code. We try to bypass the 
259  * normal JIT compilation logic to avoid loading the metadata for the method.
260  */
261 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
262 gpointer
263 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
264                                          guint8* tramp)
265 {
266         MonoImage *image;
267         guint32 token;
268         MonoMethod *method = NULL;
269         gpointer addr;
270         gpointer *vtable_slot;
271         gboolean is_got_entry;
272
273         image = *(gpointer*)(gpointer)token_info;
274         token_info += sizeof (gpointer);
275         token = *(guint32*)(gpointer)token_info;
276
277         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
278         if (!addr) {
279                 method = mono_get_method (image, token, NULL);
280                 g_assert (method);
281
282                 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
283
284                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
285                         method = mono_marshal_get_synchronized_wrapper (method);
286
287                 addr = mono_compile_method (method);
288                 g_assert (addr);
289         }
290
291         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
292
293         if (vtable_slot) {
294                 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
295
296                 if (!is_got_entry) {
297                         if (!method)
298                                 method = mono_get_method (image, token, NULL);
299                         if (method->klass->valuetype)
300                                 addr = mono_arch_get_unbox_trampoline (method, addr);
301                 }
302         } else {
303                 /* This is a normal call through a PLT entry */
304                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
305
306                 g_assert (plt_entry);
307
308                 mono_arch_patch_plt_entry (plt_entry, addr);
309
310                 is_got_entry = FALSE;
311         }
312
313         /*
314          * Since AOT code is only used in the root domain, 
315          * mono_domain_get () != mono_get_root_domain () means the calling method
316          * is AppDomain:InvokeInDomain, so this is the same check as in 
317          * mono_method_same_domain () but without loading the metadata for the method.
318          */
319         if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
320 #ifdef MONO_ARCH_HAVE_IMT
321                 if (!method)
322                         method = mono_get_method (image, token, NULL);
323                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
324 #endif
325                 *vtable_slot = addr;
326         }
327
328         return addr;
329 }
330
331 /*
332  * mono_aot_plt_trampoline:
333  *
334  *   This trampoline handles calls made from AOT code through the PLT table.
335  */
336 gpointer
337 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
338                                                  guint8* tramp)
339 {
340 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
341         guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
342 #else
343         guint32 plt_info_offset = -1;
344 #endif
345
346         return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
347 }
348 #endif
349
350 /**
351  * mono_class_init_trampoline:
352  *
353  * This method calls mono_runtime_class_init () to run the static constructor
354  * for the type, then patches the caller code so it is not called again.
355  */
356 void
357 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
358 {
359         guint8 *plt_entry = mono_aot_get_plt_entry (code);
360
361         mono_runtime_class_init (vtable);
362
363         if (!mono_running_on_valgrind ()) {
364                 if (plt_entry) {
365                         mono_arch_nullify_plt_entry (plt_entry);
366                 } else {
367                         mono_arch_nullify_class_init_trampoline (code, regs);
368                 }
369         }
370 }
371
372 /**
373  * mono_generic_class_init_trampoline:
374  *
375  * This method calls mono_runtime_class_init () to run the static constructor
376  * for the type.
377  */
378 void
379 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
380 {
381         //g_print ("generic class init for class %s.%s\n", vtable->klass->name_space, vtable->klass->name);
382
383         mono_runtime_class_init (vtable);
384
385         //g_print ("done initing generic\n");
386 }
387
388 static gpointer
389 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, MonoRuntimeGenericContext *rgctx, guint8 *tramp)
390 {
391         guint32 encoded_offset = mono_arch_get_rgctx_lazy_fetch_offset ((gpointer*)regs);
392         gpointer result;
393
394         mono_class_fill_runtime_generic_context (rgctx);
395
396         if (MONO_RGCTX_OFFSET_IS_INDIRECT (encoded_offset)) {
397                 int indirect_slot = MONO_RGCTX_OFFSET_INDIRECT_SLOT (encoded_offset);
398                 int offset = MONO_RGCTX_OFFSET_INDIRECT_OFFSET (encoded_offset);
399
400                 g_assert (indirect_slot == 0);
401                 
402                 result = *(gpointer*)((guint8*)rgctx->extra_other_infos + offset);
403         } else {
404                 int offset = MONO_RGCTX_OFFSET_DIRECT_OFFSET (encoded_offset);
405
406                 result = *(gpointer*)((guint8*)rgctx + offset);
407         }
408
409         g_assert (result);
410
411         return result;
412 }
413
414 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
415
416 /**
417  * mono_delegate_trampoline:
418  *
419  *   This trampoline handles calls made to Delegate:Invoke ().
420  */
421 gpointer
422 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
423 {
424         MonoDomain *domain = mono_domain_get ();
425         MonoDelegate *delegate;
426         MonoJitInfo *ji;
427         MonoMethod *invoke, *m;
428         MonoMethod *method = NULL;
429         gboolean multicast, callvirt;
430
431         invoke = mono_get_delegate_invoke (klass);
432         g_assert (invoke);
433
434         /* Obtain the delegate object according to the calling convention */
435
436         delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
437
438         if (!delegate->method_ptr && delegate->method) {
439                 /* The delegate was initialized by mini_delegate_ctor */
440                 method = delegate->method;
441
442                 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
443                         method = mono_marshal_get_remoting_invoke (method);
444                 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
445                         method = mono_marshal_get_unbox_wrapper (method);
446         } else {
447                 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
448                 if (ji)
449                         method = ji->method;
450         }
451         callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
452
453         /* 
454          * If the called address is a trampoline, replace it with the compiled method so
455          * further calls don't have to go through the trampoline.
456          */
457         if (method && !callvirt) {
458                 delegate->method_ptr = mono_compile_method (method);
459                 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
460         }
461
462         multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
463         if (!multicast && !callvirt) {
464                 code = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), delegate->target != NULL);
465
466                 if (code) {
467                         delegate->invoke_impl = code;
468                         return code;
469                 }
470         }
471
472         /* The general, unoptimized case */
473         m = mono_marshal_get_delegate_invoke (invoke, delegate);
474         code = mono_compile_method (m);
475         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
476         mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
477
478         return code;
479 }
480
481 #endif
482
483 /*
484  * mono_get_trampoline_func:
485  *
486  *   Return the C function which needs to be called by the generic trampoline of type
487  * TRAMP_TYPE.
488  */
489 gconstpointer
490 mono_get_trampoline_func (MonoTrampolineType tramp_type)
491 {
492         switch (tramp_type) {
493         case MONO_TRAMPOLINE_GENERIC:
494         case MONO_TRAMPOLINE_JUMP:
495                 return mono_magic_trampoline;
496         case MONO_TRAMPOLINE_CLASS_INIT:
497                 return mono_class_init_trampoline;
498         case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
499                 return mono_generic_class_init_trampoline;
500         case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
501                 return mono_rgctx_lazy_fetch_trampoline;
502 #ifdef MONO_ARCH_AOT_SUPPORTED
503         case MONO_TRAMPOLINE_AOT:
504                 return mono_aot_trampoline;
505         case MONO_TRAMPOLINE_AOT_PLT:
506                 return mono_aot_plt_trampoline;
507 #endif
508 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
509         case MONO_TRAMPOLINE_DELEGATE:
510                 return mono_delegate_trampoline;
511 #endif
512         default:
513                 g_assert_not_reached ();
514                 return NULL;
515         }
516 }
517
518