f9b7848ddced04b5c7c1f14af7f2bb4dad4d2fe0
[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 #include <mono/utils/mono-counters.h>
10
11 #ifdef HAVE_VALGRIND_MEMCHECK_H
12 #include <valgrind/memcheck.h>
13 #endif
14
15 #include "mini.h"
16 #include "debug-mini.h"
17
18 /*
19  * Address of the trampoline code.  This is used by the debugger to check
20  * whether a method is a trampoline.
21  */
22 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
23
24 static GHashTable *class_init_hash_addr = NULL;
25 static GHashTable *delegate_trampoline_hash_addr = NULL;
26
27 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
28 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
29 static CRITICAL_SECTION trampolines_mutex;
30
31 static gpointer
32 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
33 {
34         if (mono_aot_only)
35                 return mono_aot_get_unbox_trampoline (m);
36         else
37                 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
38 }
39
40 #ifdef MONO_ARCH_HAVE_IMT
41
42 static gpointer*
43 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
44 {
45         MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
46         MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
47         MonoVTable *vt = this_argument->vtable;
48         int displacement = slot - ((gpointer*)vt);
49
50         if (displacement > 0) {
51                 /* slot is in the vtable, not in the IMT */
52 #if DEBUG_IMT
53                 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
54 #endif
55                 return slot;
56         } else {
57                 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
58                 int interface_offset;
59                 int imt_slot = MONO_IMT_SIZE + displacement;
60
61                 mono_class_setup_vtable (vt->klass);
62                 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
63
64                 if (interface_offset < 0) {
65                         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));
66                         g_assert_not_reached ();
67                 }
68                 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
69
70                 if (impl_method)
71                         *impl_method = vt->klass->vtable [interface_offset + imt_method->slot];
72 #if DEBUG_IMT
73                 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
74                                 method->klass->name_space, method->klass->name, method->name, 
75                                 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
76 #endif
77                 g_assert (imt_slot < MONO_IMT_SIZE);
78                 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
79                         int vtable_offset = interface_offset + imt_method->slot;
80                         gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
81 #if DEBUG_IMT
82                         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);
83 #endif
84                         return vtable_slot;
85                 } else {
86 #if DEBUG_IMT
87                         printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
88 #endif
89                         return slot;
90                 }
91         }
92 }
93 #endif
94
95 /**
96  * mono_magic_trampoline:
97  *
98  *   This trampoline handles calls from JITted code.
99  */
100 gpointer
101 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
102 {
103         gpointer addr;
104         gpointer *vtable_slot;
105         gboolean generic_shared = FALSE;
106         MonoMethod *declaring = NULL;
107         int context_used;
108
109 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
110         if (m == MONO_FAKE_VTABLE_METHOD) {
111                 int displacement;
112                 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
113                 g_assert (vt);
114                 if (displacement > 0) {
115                         displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
116                         g_assert (displacement >= 0);
117                         displacement /= sizeof (gpointer);
118
119                         /* Avoid loading metadata or creating a generic vtable if possible */
120                         addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
121                         if (addr && !vt->klass->valuetype) {
122                                 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
123                                 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
124                                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
125                                 }
126
127                                 return addr;
128                         }
129
130                         mono_class_setup_vtable (vt->klass);
131                         m = vt->klass->vtable [displacement];
132                         if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
133                                 m = mono_marshal_get_synchronized_wrapper (m);
134                         /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
135                 } else {
136                         /* We got here from an interface method: redirect to IMT handling */
137                         m = MONO_FAKE_IMT_METHOD;
138                         /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
139                 }
140         }
141 #endif
142         /* this is the IMT trampoline */
143 #ifdef MONO_ARCH_HAVE_IMT
144         if (m == MONO_FAKE_IMT_METHOD) {
145                 MonoMethod *impl_method;
146                 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
147                  * needs the signature to be able to find the this argument
148                  */
149                 m = mono_arch_find_imt_method ((gpointer*)regs, code);
150                 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
151                 g_assert (vtable_slot);
152                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
153                 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
154                  * to be called, so we compile it and go ahead as usual.
155                  */
156                 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
157                 m = impl_method;
158         }
159 #endif
160
161         if ((context_used = mono_method_check_context_used (m))) {
162                 MonoClass *klass = NULL;
163                 MonoMethod *actual_method = NULL;
164                 MonoVTable *vt = NULL;
165                 MonoGenericInst *method_inst = NULL;
166
167                 vtable_slot = NULL;
168                 generic_shared = TRUE;
169
170                 g_assert (code);
171
172                 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
173 #ifdef MONO_ARCH_RGCTX_REG
174                         MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable ((gpointer*)regs, code);
175
176                         klass = mrgctx->class_vtable->klass;
177                         method_inst = mrgctx->method_inst;
178 #else
179                         g_assert_not_reached ();
180 #endif
181                 } else if (m->flags & METHOD_ATTRIBUTE_STATIC) {
182 #ifdef MONO_ARCH_RGCTX_REG
183                         MonoVTable *vtable = mono_arch_find_static_call_vtable ((gpointer*)regs, code);
184
185                         klass = vtable->klass;
186 #else
187                         g_assert_not_reached ();
188 #endif
189                 } else {
190 #ifdef MONO_ARCH_HAVE_IMT
191                         MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
192                                 mono_get_generic_context_from_code (code));
193
194                         vt = this_argument->vtable;
195                         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
196
197                         g_assert (this_argument->vtable->klass->inited);
198                         //mono_class_init (this_argument->vtable->klass);
199
200                         if (!vtable_slot)
201                                 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
202 #else
203                         NOT_IMPLEMENTED;
204 #endif
205                 }
206
207                 g_assert (vtable_slot || klass);
208
209                 if (vtable_slot) {
210                         int displacement = vtable_slot - ((gpointer*)vt);
211
212                         g_assert_not_reached ();
213
214                         g_assert (displacement > 0);
215
216                         actual_method = vt->klass->vtable [displacement];
217                 }
218
219                 if (method_inst) {
220                         MonoGenericContext context = { NULL, NULL };
221
222                         if (m->is_inflated)
223                                 declaring = mono_method_get_declaring_generic_method (m);
224                         else
225                                 declaring = m;
226
227                         if (klass->generic_class)
228                                 context.class_inst = klass->generic_class->context.class_inst;
229                         else if (klass->generic_container)
230                                 context.class_inst = klass->generic_container->context.class_inst;
231                         context.method_inst = method_inst;
232
233                         actual_method = mono_class_inflate_generic_method (declaring, &context);
234                 } else {
235                         actual_method = mono_class_get_method_generic (klass, m);
236                 }
237
238                 g_assert (klass);
239                 g_assert (actual_method->klass == klass);
240
241                 if (actual_method->is_inflated)
242                         declaring = mono_method_get_declaring_generic_method (actual_method);
243                 else
244                         declaring = NULL;
245
246                 m = actual_method;
247         }
248
249         addr = mono_compile_method (m);
250         g_assert (addr);
251
252         mono_debugger_trampoline_compiled (m, addr);
253
254         /* the method was jumped to */
255         if (!code)
256                 return addr;
257
258         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
259
260         if (vtable_slot) {
261                 if (m->klass->valuetype)
262                         addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
263
264                 g_assert (*vtable_slot);
265
266                 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
267 #ifdef MONO_ARCH_HAVE_IMT
268                         vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
269 #endif
270                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
271                 }
272         }
273         else if (!generic_shared || mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
274                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
275
276                 if (generic_shared)
277                         g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
278
279                 /* Patch calling code */
280                 if (plt_entry) {
281                         mono_arch_patch_plt_entry (plt_entry, addr);
282                 } else {
283                         MonoJitInfo *ji = 
284                                 mono_jit_info_table_find (mono_domain_get (), (char*)code);
285                         MonoJitInfo *target_ji = 
286                                 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
287
288                         if (mono_method_same_domain (ji, target_ji))
289                                 mono_arch_patch_callsite (ji->code_start, code, addr);
290                 }
291         }
292
293         return addr;
294 }
295
296 /*
297  * mono_aot_trampoline:
298  *
299  *   This trampoline handles calls made from AOT code. We try to bypass the 
300  * normal JIT compilation logic to avoid loading the metadata for the method.
301  */
302 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
303 gpointer
304 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
305                                          guint8* tramp)
306 {
307         MonoImage *image;
308         guint32 token;
309         MonoMethod *method = NULL;
310         gpointer addr;
311         gpointer *vtable_slot;
312         gboolean is_got_entry;
313
314         image = *(gpointer*)(gpointer)token_info;
315         token_info += sizeof (gpointer);
316         token = *(guint32*)(gpointer)token_info;
317
318         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
319         if (!addr) {
320                 method = mono_get_method (image, token, NULL);
321                 g_assert (method);
322
323                 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
324
325                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
326                         method = mono_marshal_get_synchronized_wrapper (method);
327
328                 addr = mono_compile_method (method);
329                 g_assert (addr);
330         }
331
332         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
333
334         if (vtable_slot) {
335                 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
336
337                 if (!is_got_entry) {
338                         if (!method)
339                                 method = mono_get_method (image, token, NULL);
340                         if (method->klass->valuetype)
341                                 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), method, addr);
342                 }
343         } else {
344                 /* This is a normal call through a PLT entry */
345                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
346
347                 g_assert (plt_entry);
348
349                 mono_arch_patch_plt_entry (plt_entry, addr);
350
351                 is_got_entry = FALSE;
352         }
353
354         /*
355          * Since AOT code is only used in the root domain, 
356          * mono_domain_get () != mono_get_root_domain () means the calling method
357          * is AppDomain:InvokeInDomain, so this is the same check as in 
358          * mono_method_same_domain () but without loading the metadata for the method.
359          */
360         if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
361 #ifdef MONO_ARCH_HAVE_IMT
362                 if (!method)
363                         method = mono_get_method (image, token, NULL);
364                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
365 #endif
366                 *vtable_slot = addr;
367         }
368
369         return addr;
370 }
371
372 /*
373  * mono_aot_plt_trampoline:
374  *
375  *   This trampoline handles calls made from AOT code through the PLT table.
376  */
377 gpointer
378 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
379                                                  guint8* tramp)
380 {
381 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
382         guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
383 #else
384         guint32 plt_info_offset = -1;
385 #endif
386
387         return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
388 }
389 #endif
390
391 /**
392  * mono_class_init_trampoline:
393  *
394  * This method calls mono_runtime_class_init () to run the static constructor
395  * for the type, then patches the caller code so it is not called again.
396  */
397 void
398 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
399 {
400         guint8 *plt_entry = mono_aot_get_plt_entry (code);
401
402         mono_runtime_class_init (vtable);
403
404         if (!mono_running_on_valgrind ()) {
405                 if (plt_entry) {
406                         mono_arch_nullify_plt_entry (plt_entry);
407                 } else {
408                         mono_arch_nullify_class_init_trampoline (code, regs);
409                 }
410         }
411 }
412
413 /**
414  * mono_generic_class_init_trampoline:
415  *
416  * This method calls mono_runtime_class_init () to run the static constructor
417  * for the type.
418  */
419 void
420 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
421 {
422         g_assert (!vtable->initialized);
423
424         mono_runtime_class_init (vtable);
425 }
426
427 static gpointer
428 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
429 {
430         static gboolean inited = FALSE;
431         static int num_lookups = 0;
432
433         guint32 slot = mono_arch_get_rgctx_lazy_fetch_offset ((gpointer*)regs);
434         guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
435         gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
436
437         if (!inited) {
438                 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
439                 inited = TRUE;
440         }
441
442         num_lookups++;
443
444         if (mrgctx)
445                 return mono_method_fill_runtime_generic_context (data, index);
446         else
447                 return mono_class_fill_runtime_generic_context (data, index);
448 }
449
450 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
451
452 /**
453  * mono_delegate_trampoline:
454  *
455  *   This trampoline handles calls made to Delegate:Invoke ().
456  * This is called once the first time a delegate is invoked, so it must be fast.
457  */
458 gpointer
459 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
460 {
461         MonoDomain *domain = mono_domain_get ();
462         MonoDelegate *delegate;
463         MonoJitInfo *ji;
464         MonoMethod *m;
465         MonoMethod *method = NULL;
466         gboolean multicast, callvirt;
467         MonoMethod *invoke = tramp_data [0];
468         guint8 *impl_this = tramp_data [1];
469         guint8 *impl_nothis = tramp_data [2];
470
471         /* Obtain the delegate object according to the calling convention */
472
473         /* 
474          * Avoid calling mono_get_generic_context_from_code () now since it is expensive, 
475          * get_this_arg_from_call will call it if needed.
476          */
477         delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
478
479         if (!delegate->method_ptr && delegate->method) {
480                 /* The delegate was initialized by mini_delegate_ctor */
481                 method = delegate->method;
482
483                 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
484                         method = mono_marshal_get_remoting_invoke (method);
485                 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
486                         method = mono_marshal_get_unbox_wrapper (method);
487         } else if (delegate->method) {
488                 method = delegate->method;
489         } else {
490                 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
491                 if (ji)
492                         method = ji->method;
493         }
494         callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
495
496         /* 
497          * If the called address is a trampoline, replace it with the compiled method so
498          * further calls don't have to go through the trampoline.
499          */
500         if (method && !callvirt) {
501                 delegate->method_ptr = mono_compile_method (method);
502                 mono_debugger_trampoline_compiled (method, delegate->method_ptr);
503         }
504
505         multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
506         if (!multicast && !callvirt) {
507                 code = delegate->target ? impl_this : impl_nothis;
508
509                 if (code) {
510                         delegate->invoke_impl = code;
511                         return code;
512                 }
513         }
514
515         /* The general, unoptimized case */
516         m = mono_marshal_get_delegate_invoke (invoke, delegate);
517         code = mono_compile_method (m);
518         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
519         mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
520
521         return code;
522 }
523
524 #endif
525
526 /*
527  * mono_get_trampoline_func:
528  *
529  *   Return the C function which needs to be called by the generic trampoline of type
530  * TRAMP_TYPE.
531  */
532 gconstpointer
533 mono_get_trampoline_func (MonoTrampolineType tramp_type)
534 {
535         switch (tramp_type) {
536         case MONO_TRAMPOLINE_JIT:
537         case MONO_TRAMPOLINE_JUMP:
538                 return mono_magic_trampoline;
539         case MONO_TRAMPOLINE_CLASS_INIT:
540                 return mono_class_init_trampoline;
541         case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
542                 return mono_generic_class_init_trampoline;
543         case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
544                 return mono_rgctx_lazy_fetch_trampoline;
545 #ifdef MONO_ARCH_AOT_SUPPORTED
546         case MONO_TRAMPOLINE_AOT:
547                 return mono_aot_trampoline;
548         case MONO_TRAMPOLINE_AOT_PLT:
549                 return mono_aot_plt_trampoline;
550 #endif
551 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
552         case MONO_TRAMPOLINE_DELEGATE:
553                 return mono_delegate_trampoline;
554 #endif
555         default:
556                 g_assert_not_reached ();
557                 return NULL;
558         }
559 }
560
561 void
562 mono_trampolines_init (void)
563 {
564         InitializeCriticalSection (&trampolines_mutex);
565
566         if (mono_aot_only)
567                 return;
568
569         mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
570         mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
571         mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
572         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
573         mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
574 #ifdef MONO_ARCH_AOT_SUPPORTED
575         mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
576         mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
577 #endif
578 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
579         mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
580 #endif
581 }
582
583 void
584 mono_trampolines_cleanup (void)
585 {
586         if (class_init_hash_addr)
587                 g_hash_table_destroy (class_init_hash_addr);
588         if (delegate_trampoline_hash_addr)
589                 g_hash_table_destroy (delegate_trampoline_hash_addr);
590
591         DeleteCriticalSection (&trampolines_mutex);
592 }
593
594 guint8 *
595 mono_get_trampoline_code (MonoTrampolineType tramp_type)
596 {
597         g_assert (mono_trampoline_code [tramp_type]);
598
599         return mono_trampoline_code [tramp_type];
600 }
601
602 gpointer
603 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
604 {
605         if (mono_aot_only)
606                 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
607         else
608                 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
609 }
610
611 gpointer
612 mono_create_class_init_trampoline (MonoVTable *vtable)
613 {
614         gpointer code, ptr;
615
616         g_assert (!vtable->klass->generic_container);
617
618         /* previously created trampoline code */
619         mono_domain_lock (vtable->domain);
620         ptr = 
621                 g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
622                                                                   vtable);
623         mono_domain_unlock (vtable->domain);
624         if (ptr)
625                 return ptr;
626
627         code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain, NULL);
628
629         ptr = mono_create_ftnptr (vtable->domain, code);
630
631         /* store trampoline address */
632         mono_domain_lock (vtable->domain);
633         g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
634                                                           vtable, ptr);
635         mono_domain_unlock (vtable->domain);
636
637         mono_trampolines_lock ();
638         if (!class_init_hash_addr)
639                 class_init_hash_addr = g_hash_table_new (NULL, NULL);
640         g_hash_table_insert (class_init_hash_addr, ptr, vtable);
641         mono_trampolines_unlock ();
642
643         return ptr;
644 }
645
646 gpointer
647 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, 
648                                                          gboolean add_sync_wrapper)
649 {
650         MonoJitInfo *ji;
651         gpointer code;
652         guint32 code_size = 0;
653
654         if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
655                 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
656
657         code = mono_jit_find_compiled_method (domain, method);
658         if (code)
659                 return code;
660
661         mono_domain_lock (domain);
662         code = g_hash_table_lookup (domain->jump_trampoline_hash, method);
663         mono_domain_unlock (domain);
664         if (code)
665                 return code;
666
667         code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
668         g_assert (code_size);
669
670         mono_domain_lock (domain);
671         ji = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
672         mono_domain_unlock (domain);
673         ji->code_start = code;
674         ji->code_size = code_size;
675         ji->method = method;
676
677         /*
678          * mono_delegate_ctor needs to find the method metadata from the 
679          * trampoline address, so we save it here.
680          */
681
682         mono_jit_info_table_add (domain, ji);
683
684         mono_domain_lock (domain);
685         g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
686         mono_domain_unlock (domain);
687
688         return ji->code_start;
689 }
690
691 gpointer
692 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
693 {
694         gpointer tramp;
695
696         if (mono_aot_only) {
697                 /* Avoid creating trampolines if possible */
698                 gpointer code = mono_jit_find_compiled_method (domain, method);
699                 
700                 if (code)
701                         return code;
702         }
703
704         mono_domain_lock (domain);
705         tramp = g_hash_table_lookup (domain->jit_trampoline_hash, method);
706         mono_domain_unlock (domain);
707         if (tramp)
708                 return tramp;
709
710         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && add_sync_wrapper)
711                 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
712
713         tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
714         
715         mono_domain_lock (domain);
716         g_hash_table_insert (domain->jit_trampoline_hash, method, tramp);
717         mono_domain_unlock (domain);
718
719         mono_jit_stats.method_trampolines++;
720
721         return tramp;
722 }       
723
724 gpointer
725 mono_create_jit_trampoline (MonoMethod *method)
726 {
727         return mono_create_jit_trampoline_in_domain (mono_domain_get (), method, TRUE);
728 }
729
730 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
731 gpointer
732 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
733 {
734         gpointer tramp;
735
736         MonoDomain *domain = mono_domain_get ();
737         guint8 *buf, *start;
738
739         mono_domain_lock (domain);
740         buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
741         mono_domain_unlock (domain);
742
743         *(gpointer*)(gpointer)buf = image;
744         buf += sizeof (gpointer);
745         *(guint32*)(gpointer)buf = token;
746
747         tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
748
749         mono_jit_stats.method_trampolines++;
750
751         return tramp;
752 }       
753 #endif
754
755 gpointer
756 mono_create_delegate_trampoline (MonoClass *klass)
757 {
758 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
759         MonoDomain *domain = mono_domain_get ();
760         gpointer ptr;
761         guint32 code_size = 0;
762         gpointer *tramp_data;
763         MonoMethod *invoke;
764
765         mono_domain_lock (domain);
766         ptr = g_hash_table_lookup (domain->delegate_trampoline_hash, klass);
767         mono_domain_unlock (domain);
768         if (ptr)
769                 return ptr;
770
771         // Precompute the delegate invoke impl and pass it to the delegate trampoline
772         invoke = mono_get_delegate_invoke (klass);
773         g_assert (invoke);
774
775         mono_domain_lock (domain );
776         tramp_data = mono_mempool_alloc (domain->mp, sizeof (gpointer) * 3);
777         mono_domain_unlock (domain);
778         tramp_data [0] = invoke;
779         if (mono_aot_only) {
780                 tramp_data [1] = NULL;
781                 tramp_data [2] = NULL;
782         } else {
783                 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
784                 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
785         }
786
787         ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
788         g_assert (code_size);
789
790         /* store trampoline address */
791         mono_domain_lock (domain);
792         g_hash_table_insert (domain->delegate_trampoline_hash,
793                                                           klass, ptr);
794         mono_domain_unlock (domain);
795
796         mono_trampolines_lock ();
797         if (!delegate_trampoline_hash_addr)
798                 delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
799         g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
800         mono_trampolines_unlock ();
801
802         return ptr;
803 #else
804         return NULL;
805 #endif
806 }
807
808 MonoVTable*
809 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
810 {
811         MonoVTable *res;
812
813         mono_trampolines_lock ();
814         if (class_init_hash_addr)
815                 res = g_hash_table_lookup (class_init_hash_addr, addr);
816         else
817                 res = NULL;
818         mono_trampolines_unlock ();
819         return res;
820 }
821
822 MonoClass*
823 mono_find_delegate_trampoline_by_addr (gconstpointer addr)
824 {
825         MonoClass *res;
826
827         mono_trampolines_lock ();
828         if (delegate_trampoline_hash_addr)
829                 res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
830         else
831                 res = NULL;
832         mono_trampolines_unlock ();
833         return res;
834 }