/*
- * aot.c: mono Ahead of Time compiler
+ * aot-runtime.c: mono Ahead of Time compiler
*
* Author:
* Dietmar Maurer (dietmar@ximian.com)
#include <errno.h>
#include <sys/stat.h>
-#include <limits.h> /* for PAGESIZE */
-#ifndef PAGESIZE
-#define PAGESIZE 4096
-#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
#include <mono/metadata/monitor.h>
#include <mono/metadata/threads-types.h>
#include <mono/utils/mono-logger.h>
+#include <mono/utils/mono-mmap.h>
#include "mono/utils/mono-compiler.h"
+#include <mono/utils/mono-counters.h>
#include "mini.h"
#include "version.h"
guint32 *extra_method_table;
guint32 *extra_method_info_offsets;
guint8 *extra_method_info;
- guint8 *trampolines;
- guint32 num_trampolines, trampoline_got_offset_base, trampoline_index;
- guint32 specific_trampoline_size;
+ guint8 *unwind_info;
+
+ guint8 *specific_trampolines;
+ guint32 num_specific_trampolines, specific_trampoline_got_offset_base;
+ guint32 specific_trampoline_index, specific_trampoline_size;
+
+ guint8 *static_rgctx_trampolines;
+ guint32 num_static_rgctx_trampolines, static_rgctx_trampoline_got_offset_base;
+ guint32 static_rgctx_trampoline_index, static_rgctx_trampoline_size;
+
gpointer *globals;
MonoDl *sofile;
} MonoAotModule;
typedef struct MonoAotFileInfo
{
guint32 plt_got_offset_base;
- guint32 trampoline_got_offset_base;
- guint32 num_trampolines;
guint32 got_size;
guint32 plt_size;
+ guint32 num_specific_trampolines;
guint32 specific_trampoline_size;
+ guint32 specific_trampoline_got_offset_base;
+ guint32 num_static_rgctx_trampolines;
+ guint32 static_rgctx_trampoline_size;
+ guint32 static_rgctx_trampoline_got_offset_base;
gpointer *got;
} MonoAotFileInfo;
if (module->out_of_date)
return NULL;
- assembly = mono_assembly_load (&module->image_names [index], module->assembly->basedir, &status);
+ assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
if (!assembly) {
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", module->aot_name, module->image_names [index].name);
module->out_of_date = TRUE;
mono_metadata_free_type (type);
} else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
MonoType *t;
- gboolean is_method;
MonoGenericContainer *container;
- // FIXME: Maybe use types directly to avoid
- // the overhead of creating MonoClass-es
+ int num = decode_value (p, &p);
+ gboolean is_method = decode_value (p, &p);
- // FIXME: Memory management
- t = g_new0 (MonoType, 1);
- t->type = type;
- t->data.generic_param = g_new0 (MonoGenericParam, 1);
- t->data.generic_param->num = decode_value (p, &p);
- t->data.generic_param->name = "T";
-
- is_method = decode_value (p, &p);
if (is_method) {
- MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
-
- if (!method_def) {
- g_free (t->data.generic_param);
- g_free (t);
+ MonoMethod *method_def;
+ g_assert (type == MONO_TYPE_MVAR);
+ method_def = decode_method_ref_2 (module, p, &p);
+ if (!method_def)
return NULL;
- }
container = mono_method_get_generic_container (method_def);
} else {
- MonoClass *class_def = decode_klass_ref (module, p, &p);
-
- if (!class_def) {
- g_free (t->data.generic_param);
- g_free (t);
+ MonoClass *class_def;
+ g_assert (type == MONO_TYPE_VAR);
+ class_def = decode_klass_ref (module, p, &p);
+ if (!class_def)
return NULL;
- }
container = class_def->generic_container;
}
g_assert (container);
- t->data.generic_param->owner = container;
+ // FIXME: Memory management
+ t = g_new0 (MonoType, 1);
+ t->type = type;
+ t->data.generic_param = mono_generic_container_get_param (container, num);
+
+ // FIXME: Maybe use types directly to avoid
+ // the overhead of creating MonoClass-es
klass = mono_class_from_mono_type (t);
+
+ g_free (t);
} else {
g_assert_not_reached ();
}
return mono_class_get_field (klass, token);
}
+/*
+ * can_method_ref_match_method:
+ *
+ * Determine if calling decode_method_ref_2 on P could return the same method as
+ * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
+ * would create MonoMethods which are not needed etc.
+ */
+static gboolean
+can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
+{
+ guint8 *p = buf;
+ guint32 image_index, value;
+
+ /* Keep this in sync with decode_method_ref () */
+ value = decode_value (p, &p);
+ image_index = value >> 24;
+
+ if (image_index == MONO_AOT_METHODREF_WRAPPER) {
+ guint32 wrapper_type;
+
+ if (!method->wrapper_type)
+ return FALSE;
+
+ wrapper_type = decode_value (p, &p);
+
+ if (method->wrapper_type != wrapper_type)
+ return FALSE;
+ } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
+ if (method->wrapper_type)
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
/*
* decode_method_ref:
*
decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
{
guint32 image_index, value;
- MonoImage *image;
+ MonoImage *image = NULL;
guint8 *p = buf;
if (method)
*method = mono_marshal_get_static_rgctx_invoke (m);
break;
}
+ case MONO_WRAPPER_SYNCHRONIZED: {
+ MonoMethod *m = decode_method_ref_2 (module, p, &p);
+
+ if (!m)
+ return NULL;
+ *method = mono_marshal_get_synchronized_wrapper (m);
+ break;
+ }
case MONO_WRAPPER_UNKNOWN: {
MonoMethodDesc *desc;
MonoMethod *orig_method;
static void
make_writable (guint8* addr, guint32 len)
{
-#ifndef PLATFORM_WIN32
guint8 *page_start;
int pages, err;
if (mono_aot_only)
g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
- page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
- pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
- err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
+ page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1));
+ pages = (addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ();
+
+ err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_EXEC);
g_assert (err == 0);
-#else
- {
- DWORD oldp;
- g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
- }
-#endif
}
static void
amodule->aot_name = aot_name;
amodule->assembly = assembly;
amodule->plt_got_offset_base = file_info->plt_got_offset_base;
- amodule->num_trampolines = file_info->num_trampolines;
- amodule->trampoline_got_offset_base = file_info->trampoline_got_offset_base;
+ amodule->num_specific_trampolines = file_info->num_specific_trampolines;
+ amodule->specific_trampoline_got_offset_base = file_info->specific_trampoline_got_offset_base;
+ amodule->specific_trampoline_size = file_info->specific_trampoline_size;
amodule->got_size = file_info->got_size;
amodule->plt_size = file_info->plt_size;
- amodule->specific_trampoline_size = file_info->specific_trampoline_size;
+ amodule->num_static_rgctx_trampolines = file_info->num_static_rgctx_trampolines;
+ amodule->static_rgctx_trampoline_got_offset_base = file_info->static_rgctx_trampoline_got_offset_base;
+ amodule->static_rgctx_trampoline_size = file_info->static_rgctx_trampoline_size;
amodule->got = file_info->got;
amodule->got [0] = assembly->image;
amodule->globals = globals;
find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
- find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
+ find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&amodule->specific_trampolines);
+ find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&amodule->static_rgctx_trampolines);
+ find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
amodule->mem_begin = amodule->code;
len = amodule->mem_end - amodule->mem_begin;
/* Round down in both directions to avoid modifying data which is not ours */
- page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
- pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
- err = mprotect (page_start, pages * PAGESIZE, 0);
+ page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
+ pages = ((addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ()) - 1;
+ err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_NONE);
g_assert (err == 0);
#endif
}
MonoJitInfo *jinfo;
guint code_len, used_int_regs, flags;
gboolean has_generic_jit_info, has_dwarf_unwind_info;
- guint8 *p, *unwind_info_block;
+ guint8 *p;
MonoMethodHeader *header;
- int generic_info_size, unwind_info_len;
+ int generic_info_size;
header = mono_method_get_header (method);
flags = decode_value (p, &p);
has_generic_jit_info = (flags & 1) != 0;
has_dwarf_unwind_info = (flags & 2) != 0;
- unwind_info_block = p;
if (has_dwarf_unwind_info) {
- gssize offset;
+ guint32 offset;
- unwind_info_len = decode_value (p, &p);
- offset = unwind_info_block - (guint8*)aot_module->ex_info;
- g_assert (offset > 0 && offset < (1 << 30));
+ offset = decode_value (p, &p);
+ g_assert (offset < (1 << 30));
used_int_regs = offset;
- p += unwind_info_len;
} else {
used_int_regs = decode_value (p, &p);
}
g_assert (amodule);
g_assert (ji->from_aot);
- p = amodule->ex_info + ji->used_regs;
+ p = amodule->unwind_info + ji->used_regs;
*unwind_info_len = decode_value (p, &p);
return p;
}
jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
}
- mono_domain_lock (domain);
- code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
- mono_domain_unlock (domain);
+ code2 = mono_domain_code_reserve (domain, jinfo->code_size);
memcpy (code2, code, jinfo->code_size);
mono_arch_flush_icache (code2, jinfo->code_size);
code = code2;
{
guint32 table_size, entry_size, hash;
guint32 *table, *entry;
- char *full_name = NULL;
+ char *name = NULL;
+ int num_checks = 0;
+ guint32 index;
if (!amodule)
return 0xffffff;
entry_size = 3;
if (method->wrapper_type) {
- /* FIXME: This is a hack to work around the fact that runtime invoke wrappers get assigned to some random class */
- if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
- char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
- full_name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
- g_free (tmpsig);
- } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
- char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
- full_name = g_strdup_printf ("(wrapper delegate-invoke):%s (%s)", method->name, tmpsig);
- g_free (tmpsig);
- } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_BEGIN_INVOKE) {
- char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
- full_name = g_strdup_printf ("(wrapper delegate-begin-invoke):%s (%s)", method->name, tmpsig);
- g_free (tmpsig);
- } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_END_INVOKE) {
- char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
- full_name = g_strdup_printf ("(wrapper delegate-end-invoke):%s (%s)", method->name, tmpsig);
- g_free (tmpsig);
- } else {
- full_name = mono_method_full_name (method, TRUE);
- }
+ name = mono_aot_wrapper_name (method);
}
- if (method->wrapper_type)
- hash = g_str_hash (method->name) % table_size;
- else
- hash = 0 % table_size;
+ hash = mono_aot_method_hash (method) % table_size;
entry = &table [hash * entry_size];
- if (entry [0] != 0) {
- while (TRUE) {
- guint32 key = entry [0];
- guint32 value = entry [1];
- guint32 next = entry [entry_size - 1];
- MonoMethod *m;
- guint8 *p;
- int is_wrapper;
-
- // FIXME: Avoid fully decoding the method ref
- p = amodule->extra_method_info + key;
- is_wrapper = decode_value (p, &p);
- if (is_wrapper) {
- if (full_name && !strcmp (full_name, (char*)p))
- return value;
- } else {
- mono_aot_lock ();
- if (!amodule->method_ref_to_method)
- amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
- m = g_hash_table_lookup (amodule->method_ref_to_method, p);
- mono_aot_unlock ();
- if (!m) {
- guint8 *orig_p = p;
- m = decode_method_ref_2 (amodule, p, &p);
- if (m) {
- mono_aot_lock ();
- g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
- mono_aot_unlock ();
- }
+ if (entry [0] == 0)
+ return 0xffffff;
+
+ index = 0xffffff;
+ while (TRUE) {
+ guint32 key = entry [0];
+ guint32 value = entry [1];
+ guint32 next = entry [entry_size - 1];
+ MonoMethod *m;
+ guint8 *p;
+ int is_wrapper_name;
+
+ p = amodule->extra_method_info + key;
+ is_wrapper_name = decode_value (p, &p);
+ if (is_wrapper_name) {
+ int wrapper_type = decode_value (p, &p);
+ if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
+ index = value;
+ break;
+ }
+ } else if (can_method_ref_match_method (amodule, p, method)) {
+ num_checks ++;
+ mono_aot_lock ();
+ if (!amodule->method_ref_to_method)
+ amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
+ m = g_hash_table_lookup (amodule->method_ref_to_method, p);
+ mono_aot_unlock ();
+ if (!m) {
+ guint8 *orig_p = p;
+ m = decode_method_ref_2 (amodule, p, &p);
+ if (m) {
+ mono_aot_lock ();
+ g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
+ mono_aot_unlock ();
}
- if (m == method)
- return value;
}
-
- if (next != 0) {
- entry = &table [next * entry_size];
- } else {
+ /*
+ if (m)
+ printf ("%d %s %s\n", num_checks, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
+ */
+ if (m == method) {
+ index = value;
break;
}
+
+ /* Special case: wrappers of shared generic methods */
+ if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
+ method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
+ MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
+ MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
+
+ if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
+ index = value;
+ break;
+ }
+ }
}
+
+ if (next != 0)
+ entry = &table [next * entry_size];
+ else
+ break;
}
- return 0xffffff;
+ g_free (name);
+ return index;
}
static void
int index, tramp_size;
guint8 *code, *tramp;
static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
+ static gboolean inited;
+ static guint32 num_trampolines;
/* Currently, we keep all trampolines in the mscorlib AOT image */
image = mono_defaults.corlib;
mono_aot_lock ();
+ if (!inited) {
+ mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
+ inited = TRUE;
+ }
+
amodule = image->aot_module;
g_assert (amodule);
- if (amodule->trampoline_index == amodule->num_trampolines)
- g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
+ if (amodule->specific_trampoline_index == amodule->num_specific_trampolines)
+ g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_specific_trampolines);
+
+ index = amodule->specific_trampoline_index ++;
- index = amodule->trampoline_index ++;
+ num_trampolines ++;
mono_aot_unlock ();
tramp = generic_trampolines [tramp_type];
g_assert (tramp);
- amodule->got [amodule->trampoline_got_offset_base + (index *2)] = tramp;
- amodule->got [amodule->trampoline_got_offset_base + (index *2) + 1] = arg1;
+ amodule->got [amodule->specific_trampoline_got_offset_base + (index *2)] = tramp;
+ amodule->got [amodule->specific_trampoline_got_offset_base + (index *2) + 1] = arg1;
tramp_size = amodule->specific_trampoline_size;
- code = amodule->trampolines + (index * tramp_size);
+ code = amodule->specific_trampolines + (index * tramp_size);
if (code_len)
*code_len = tramp_size;
return code;
}
+gpointer
+mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
+{
+ MonoAotModule *amodule;
+ int index, tramp_size;
+ guint8 *code;
+ MonoImage *image;
+
+ /* Currently, we keep all trampolines in the mscorlib AOT image */
+ image = mono_defaults.corlib;
+ g_assert (image);
+
+ mono_aot_lock ();
+
+ amodule = image->aot_module;
+ g_assert (amodule);
+
+ if (amodule->static_rgctx_trampoline_index == amodule->num_static_rgctx_trampolines)
+ g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_static_rgctx_trampolines);
+
+ index = amodule->static_rgctx_trampoline_index ++;
+
+ mono_aot_unlock ();
+
+ amodule->got [amodule->static_rgctx_trampoline_got_offset_base + (index *2)] = ctx;
+ amodule->got [amodule->static_rgctx_trampoline_got_offset_base + (index *2) + 1] = addr;
+
+ tramp_size = amodule->static_rgctx_trampoline_size;
+
+ code = amodule->static_rgctx_trampolines + (index * tramp_size);
+
+ return code;
+}
+
gpointer
mono_aot_get_unbox_trampoline (MonoMethod *method)
{
char *symbol;
gpointer code;
- amodule = method->klass->image->aot_module;
- g_assert (amodule);
+ if (method->is_inflated) {
+ guint32 index = find_extra_method (method, &amodule);
+
+ g_assert (index != 0xffffff);
+
+ symbol = g_strdup_printf ("ut_e_%d", index);
+ } else {
+ amodule = method->klass->image->aot_module;
+ g_assert (amodule);
- symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
+ symbol = g_strdup_printf ("ut_%d", method_index);
+ }
code = load_named_code (amodule, symbol);
g_free (symbol);
return code;
return NULL;
}
+gpointer
+mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
gpointer
mono_aot_get_named_code (const char *name)
{