Tue Mar 18 11:50:14 CET 2008 Paolo Molaro <lupus@ximian.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 #ifdef MONO_ARCH_RGCTX_REG
160                         MonoRuntimeGenericContext *rgctx = mono_arch_find_static_call_rgctx ((gpointer*)regs, code);
161
162                         klass = rgctx->vtable->klass;
163 #else
164                         g_assert_not_reached ();
165 #endif
166                 } else {
167 #ifdef MONO_ARCH_HAVE_IMT
168                         MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
169                                 get_generic_context (code));
170
171                         vt = this_argument->vtable;
172                         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
173
174                         g_assert (this_argument->vtable->klass->inited);
175                         //mono_class_init (this_argument->vtable->klass);
176
177                         if (!vtable_slot)
178                                 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
179 #else
180                         NOT_IMPLEMENTED;
181 #endif
182                 }
183
184                 g_assert (vtable_slot || klass);
185
186                 if (vtable_slot) {
187                         int displacement = vtable_slot - ((gpointer*)vt);
188
189                         g_assert_not_reached ();
190
191                         g_assert (displacement > 0);
192
193                         actual_method = vt->klass->vtable [displacement];
194                 } else {
195                         int i;
196
197                         if (m->is_inflated)
198                                 declaring = mono_method_get_declaring_generic_method (m);
199                         else
200                                 declaring = m;
201
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)
206                                                 break;
207                                 }
208                         }
209
210                         g_assert (mono_method_get_declaring_generic_method (actual_method) == declaring);
211                 }
212
213                 g_assert (actual_method);
214                 m = actual_method;
215         }
216
217         addr = mono_compile_method (m);
218         g_assert (addr);
219
220         mono_debugger_trampoline_compiled (m, addr);
221
222         /* the method was jumped to */
223         if (!code)
224                 return addr;
225
226         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
227
228         if (vtable_slot) {
229                 if (m->klass->valuetype)
230                         addr = mono_arch_get_unbox_trampoline (m, addr);
231
232                 g_assert (*vtable_slot);
233
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);
237 #endif
238                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
239                 }
240         }
241         else if (!generic_shared || mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
242                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
243
244                 /* Patch calling code */
245                 if (plt_entry) {
246                         mono_arch_patch_plt_entry (plt_entry, addr);
247                 } else {
248                         MonoJitInfo *ji = 
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));
252
253                         if (mono_method_same_domain (ji, target_ji))
254                                 mono_arch_patch_callsite (ji->code_start, code, addr);
255                 }
256         }
257
258         return addr;
259 }
260
261 /*
262  * mono_aot_trampoline:
263  *
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.
266  */
267 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
268 gpointer
269 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
270                                          guint8* tramp)
271 {
272         MonoImage *image;
273         guint32 token;
274         MonoMethod *method = NULL;
275         gpointer addr;
276         gpointer *vtable_slot;
277         gboolean is_got_entry;
278
279         image = *(gpointer*)(gpointer)token_info;
280         token_info += sizeof (gpointer);
281         token = *(guint32*)(gpointer)token_info;
282
283         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
284         if (!addr) {
285                 method = mono_get_method (image, token, NULL);
286                 g_assert (method);
287
288                 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
289
290                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
291                         method = mono_marshal_get_synchronized_wrapper (method);
292
293                 addr = mono_compile_method (method);
294                 g_assert (addr);
295         }
296
297         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
298
299         if (vtable_slot) {
300                 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
301
302                 if (!is_got_entry) {
303                         if (!method)
304                                 method = mono_get_method (image, token, NULL);
305                         if (method->klass->valuetype)
306                                 addr = mono_arch_get_unbox_trampoline (method, addr);
307                 }
308         } else {
309                 /* This is a normal call through a PLT entry */
310                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
311
312                 g_assert (plt_entry);
313
314                 mono_arch_patch_plt_entry (plt_entry, addr);
315
316                 is_got_entry = FALSE;
317         }
318
319         /*
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.
324          */
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
327                 if (!method)
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);
330 #endif
331                 *vtable_slot = addr;
332         }
333
334         return addr;
335 }
336
337 /*
338  * mono_aot_plt_trampoline:
339  *
340  *   This trampoline handles calls made from AOT code through the PLT table.
341  */
342 gpointer
343 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
344                                                  guint8* tramp)
345 {
346 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
347         guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
348 #else
349         guint32 plt_info_offset = -1;
350 #endif
351
352         return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
353 }
354 #endif
355
356 /**
357  * mono_class_init_trampoline:
358  *
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.
361  */
362 void
363 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
364 {
365         guint8 *plt_entry = mono_aot_get_plt_entry (code);
366
367         mono_runtime_class_init (vtable);
368
369         if (!mono_running_on_valgrind ()) {
370                 if (plt_entry) {
371                         mono_arch_nullify_plt_entry (plt_entry);
372                 } else {
373                         mono_arch_nullify_class_init_trampoline (code, regs);
374                 }
375         }
376 }
377
378 /**
379  * mono_generic_class_init_trampoline:
380  *
381  * This method calls mono_runtime_class_init () to run the static constructor
382  * for the type.
383  */
384 void
385 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
386 {
387         //g_print ("generic class init for class %s.%s\n", vtable->klass->name_space, vtable->klass->name);
388
389         mono_runtime_class_init (vtable);
390
391         //g_print ("done initing generic\n");
392 }
393
394 static gpointer
395 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, MonoRuntimeGenericContext *rgctx, guint8 *tramp)
396 {
397         guint32 encoded_offset = mono_arch_get_rgctx_lazy_fetch_offset ((gpointer*)regs);
398         gpointer result;
399
400         mono_class_fill_runtime_generic_context (rgctx);
401
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);
405
406                 g_assert (indirect_slot == 0);
407                 
408                 result = *(gpointer*)((guint8*)rgctx->extra_other_infos + offset);
409         } else {
410                 int offset = MONO_RGCTX_OFFSET_DIRECT_OFFSET (encoded_offset);
411
412                 result = *(gpointer*)((guint8*)rgctx + offset);
413         }
414
415         g_assert (result);
416
417         return result;
418 }
419
420 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
421
422 /**
423  * mono_delegate_trampoline:
424  *
425  *   This trampoline handles calls made to Delegate:Invoke ().
426  */
427 gpointer
428 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
429 {
430         MonoDomain *domain = mono_domain_get ();
431         MonoDelegate *delegate;
432         MonoJitInfo *ji;
433         MonoMethod *invoke, *m;
434         MonoMethod *method = NULL;
435         gboolean multicast, callvirt;
436
437         invoke = mono_get_delegate_invoke (klass);
438         g_assert (invoke);
439
440         /* Obtain the delegate object according to the calling convention */
441
442         delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
443
444         if (!delegate->method_ptr && delegate->method) {
445                 /* The delegate was initialized by mini_delegate_ctor */
446                 method = delegate->method;
447
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);
452         } else {
453                 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
454                 if (ji)
455                         method = ji->method;
456         }
457         callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
458
459         /* 
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.
462          */
463         if (method && !callvirt) {
464                 delegate->method_ptr = mono_compile_method (method);
465                 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
466         }
467
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);
471
472                 if (code) {
473                         delegate->invoke_impl = code;
474                         return code;
475                 }
476         }
477
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);
483
484         return code;
485 }
486
487 #endif
488
489 /*
490  * mono_get_trampoline_func:
491  *
492  *   Return the C function which needs to be called by the generic trampoline of type
493  * TRAMP_TYPE.
494  */
495 gconstpointer
496 mono_get_trampoline_func (MonoTrampolineType tramp_type)
497 {
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;
513 #endif
514 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
515         case MONO_TRAMPOLINE_DELEGATE:
516                 return mono_delegate_trampoline;
517 #endif
518         default:
519                 g_assert_not_reached ();
520                 return NULL;
521         }
522 }
523
524