2010-02-15 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / aot-runtime.c
index 6efb5bd22c79bc576592a14168d371e023102fd3..7f02b7593c66057a462593390cbf3ab581b6a34f 100644 (file)
@@ -162,6 +162,7 @@ static gint32 mono_last_aot_method = -1;
 
 static gboolean make_unreadable = FALSE;
 static guint32 name_table_accesses = 0;
+static guint32 n_pagefaults = 0;
 
 /* Used to speed-up find_aot_module () */
 static gsize aot_code_low_addr = (gssize)-1;
@@ -174,37 +175,48 @@ init_plt (MonoAotModule *info);
 /*                 AOT RUNTIME                       */
 /*****************************************************/
 
+/*
+ * load_image:
+ *
+ *   Load one of the images referenced by AMODULE. Returns NULL if the image is not
+ * found, and sets the loader error if SET_ERROR is TRUE.
+ */
 static MonoImage *
-load_image (MonoAotModule *module, int index)
+load_image (MonoAotModule *amodule, int index, gboolean set_error)
 {
        MonoAssembly *assembly;
        MonoImageOpenStatus status;
 
-       g_assert (index < module->image_table_len);
+       g_assert (index < amodule->image_table_len);
 
-       if (module->image_table [index])
-               return module->image_table [index];
-       if (module->out_of_date)
+       if (amodule->image_table [index])
+               return amodule->image_table [index];
+       if (amodule->out_of_date)
                return NULL;
 
-       assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
+       assembly = mono_assembly_load (&amodule->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_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name);
+               amodule->out_of_date = TRUE;
+
+               if (set_error) {
+                       char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
+                       mono_loader_set_error_assembly_load (full_name, FALSE);
+                       g_free (full_name);
+               }
                return NULL;
        }
 
-       if (strcmp (assembly->image->guid, module->image_guids [index])) {
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", module->aot_name, module->image_names [index].name);
-               module->out_of_date = TRUE;
+       if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", amodule->aot_name, amodule->image_names [index].name);
+               amodule->out_of_date = TRUE;
                return NULL;
        }
 
-       module->image_table [index] = assembly->image;
+       amodule->image_table [index] = assembly->image;
        return assembly->image;
 }
 
-
 static inline gint32
 decode_value (guint8 *ptr, guint8 **rptr)
 {
@@ -348,7 +360,7 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                return NULL;
        }
        if (mono_metadata_token_table (token) == 0) {
-               image = load_image (module, decode_value (p, &p));
+               image = load_image (module, decode_value (p, &p), TRUE);
                if (!image)
                        return NULL;
                klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
@@ -414,14 +426,14 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                                g_assert_not_reached ();
                        }
                } else {
-                       image = load_image (module, decode_value (p, &p));
+                       image = load_image (module, decode_value (p, &p), TRUE);
                        if (!image)
                                return NULL;
                        klass = mono_class_get (image, token);
                }
        } else if (token == MONO_TOKEN_TYPE_DEF) {
                /* Array */
-               image = load_image (module, decode_value (p, &p));
+               image = load_image (module, decode_value (p, &p), TRUE);
                if (!image)
                        return NULL;
                rank = decode_value (p, &p);
@@ -431,7 +443,6 @@ decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
                g_assert_not_reached ();
        }
        g_assert (klass);
-       mono_class_init (klass);
 
        *endbuf = p;
        return klass;
@@ -576,6 +587,9 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        *method = mono_gc_get_managed_allocator_by_type (atype);
                        break;
                }
+               case MONO_WRAPPER_WRITE_BARRIER:
+                       *method = mono_gc_get_write_barrier ();
+                       break;
                case MONO_WRAPPER_STELEMREF:
                        *method = mono_marshal_get_stelemref ();
                        break;
@@ -613,6 +627,19 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                        *method = mono_marshal_get_runtime_invoke (m, FALSE);
                        break;
                }
+               case MONO_WRAPPER_MANAGED_TO_MANAGED: {
+                       int subtype = decode_value (p, &p);
+
+                       if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
+                               int rank = decode_value (p, &p);
+                               int elem_size = decode_value (p, &p);
+
+                               *method = mono_marshal_get_array_address (rank, elem_size);
+                       } else {
+                               g_assert_not_reached ();
+                       }
+                       break;
+               }
                default:
                        g_assert_not_reached ();
                }
@@ -623,7 +650,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                image_index = decode_value (p, &p);
                *token = decode_value (p, &p);
 
-               image = load_image (module, image_index);
+               image = load_image (module, image_index, TRUE);
                if (!image)
                        return NULL;
        } else if (image_index == MONO_AOT_METHODREF_GINST) {
@@ -641,7 +668,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                image_index = decode_value (p, &p);
                *token = decode_value (p, &p);
 
-               image = load_image (module, image_index);
+               image = load_image (module, image_index, TRUE);
                if (!image)
                        return NULL;
 
@@ -696,7 +723,7 @@ decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, g
                g_assert (image_index < MONO_AOT_METHODREF_MIN);
                *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
 
-               image = load_image (module, image_index);
+               image = load_image (module, image_index, TRUE);
                if (!image)
                        return NULL;
        }
@@ -885,7 +912,7 @@ find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *valu
                table_size = table [0];
                table ++;
 
-               hash = mono_aot_str_hash (name) % table_size;
+               hash = mono_metadata_str_hash (name) % table_size;
 
                entry = &table [hash * 2];
 
@@ -1149,17 +1176,19 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        if (make_unreadable) {
 #ifndef TARGET_WIN32
                guint8 *addr;
-               guint8 *page_start;
-               int pages, err, len;
+               guint8 *page_start, *page_end;
+               int err, len;
 
                addr = amodule->mem_begin;
                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)) & ~ (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);
+               page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
+               if (page_end > page_start) {
+                       err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
+                       g_assert (err == 0);
+               }
 #endif
        }
 
@@ -1196,9 +1225,10 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
         * referenced assemblies, we depend on the exact versions of the referenced assemblies.
         * MS calls this 'hard binding'. This means we have to load all referenced assemblies
         * non-lazily, since we can't handle out-of-date errors later.
+        * The cached class info also depends on the exact assemblies.
         */
        for (i = 0; i < amodule->image_table_len; ++i)
-               load_image (amodule, i);
+               load_image (amodule, i, FALSE);
 
        if (amodule->out_of_date) {
                mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
@@ -1424,7 +1454,7 @@ mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const ch
                        full_name = g_strdup_printf ("%s.%s", name_space, name);
                }
        }
-       hash = mono_aot_str_hash (full_name) % table_size;
+       hash = mono_metadata_str_hash (full_name) % table_size;
        if (full_name != full_name_buf)
                g_free (full_name);
 
@@ -1567,7 +1597,7 @@ decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
 
        eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
 
-       unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len);
+       unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, NULL);
 
        jinfo->code_size = code_len;
        jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
@@ -1938,6 +1968,7 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
 #else
                decode_eh_frame (amodule, domain, method, code, jinfo);
 #endif
+               jinfo->from_llvm = 1;
        } else {
                jinfo->code_size = code_len;
                jinfo->used_regs = used_int_regs;
@@ -1972,6 +2003,7 @@ decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
                int len = decode_value (p, &p);
 
                seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
+               seq_points->len = len;
                last_il_offset = last_native_offset = 0;
                for (i = 0; i < len; ++i) {
                        SeqPoint *sp = &seq_points->seq_points [i];
@@ -2250,7 +2282,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
                        goto cleanup;
                break;
        case MONO_PATCH_INFO_IMAGE:
-               ji->data.image = load_image (aot_module, decode_value (p, &p));
+               ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
                if (!ji->data.image)
                        goto cleanup;
                break;
@@ -2290,7 +2322,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
                break;
        }
        case MONO_PATCH_INFO_LDSTR:
-               image = load_image (aot_module, decode_value (p, &p));
+               image = load_image (aot_module, decode_value (p, &p), TRUE);
                if (!image)
                        goto cleanup;
                ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
@@ -2300,7 +2332,7 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        case MONO_PATCH_INFO_LDTOKEN:
        case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
                /* Shared */
-               image = load_image (aot_module, decode_value (p, &p));
+               image = load_image (aot_module, decode_value (p, &p), TRUE);
                if (!image)
                        goto cleanup;
                ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
@@ -2495,10 +2527,15 @@ load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoM
                if (mono_jit_stats.methods_aot >= mono_last_aot_method)
                                return NULL;
                else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
-                       if (method)
-                               printf ("LAST AOT METHOD: %s%s%s.%s.\n", method->klass->name_space, method->klass->name_space [0] ? "." : "", method->klass->name, method->name);
-                       else
+                       if (!method)
+                               method = mono_get_method (image, token, NULL);
+                       if (method) {
+                               char *name = mono_method_full_name (method, TRUE);
+                               printf ("LAST AOT METHOD: %s.\n", name);
+                               g_free (name);
+                       } else {
                                printf ("LAST AOT METHOD: %p %d\n", code, method_index);
+                       }
                }
        }
 
@@ -2829,8 +2866,11 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
 
                        code = mono_aot_get_method (domain, m);
                        if (code) {
-                               if (mono_method_needs_static_rgctx_invoke (m, FALSE))
-                                       code = mono_create_static_rgctx_trampoline (m, code);
+                               if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
+                                       code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
+                                       /* The call above returns an ftnptr */
+                                       code = mono_get_addr_from_ftnptr (code);
+                               }
 
                                return code;
                        }
@@ -2988,6 +3028,7 @@ find_aot_module (guint8 *code)
  *
  *   This function is called by the entries in the PLT to resolve the actual method that
  * needs to be called. It returns a trampoline to the method and patches the PLT entry.
+ * Returns NULL if the something cannot be loaded.
  */
 gpointer
 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
@@ -3007,8 +3048,11 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
 
        mp = mono_mempool_new_size (512);
        res = decode_patch (module, mp, &ji, p, &p);
-       // FIXME: Error handling (how ?)
-       g_assert (res);
+
+       if (!res) {
+               mono_mempool_destroy (mp);
+               return NULL;
+       }
 
        /* 
         * Avoid calling resolve_patch_target in the full-aot case if possible, since
@@ -3033,8 +3077,11 @@ mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code
        if (ji.type == MONO_PATCH_INFO_ABS || ji.type == MONO_PATCH_INFO_INTERNAL_METHOD || ji.type == MONO_PATCH_INFO_CLASS_INIT || ji.type == MONO_PATCH_INFO_ICALL_ADDR || ji.type == MONO_PATCH_INFO_JIT_ICALL_ADDR || ji.type == MONO_PATCH_INFO_RGCTX_FETCH) {
                /* These should already have a function descriptor */
 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
-               g_assert (((gpointer*)target) [2] == 0);
+               /* Our function descriptors have a 0 environment, gcc created ones don't */
+               if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
+                       g_assert (((gpointer*)target) [2] == 0);
 #endif
+               /* Empty */
        } else if (!no_ftnptr) {
 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
                g_assert (((gpointer*)target) [2] != 0);
@@ -3496,7 +3543,8 @@ mono_aot_get_lazy_fetch_trampoline (guint32 slot)
        symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
        code = load_function (mono_defaults.corlib->aot_module, symbol);
        g_free (symbol);
-       return code;
+       /* The caller expects an ftnptr */
+       return mono_create_ftnptr (mono_domain_get (), code);
 }
 
 gpointer
@@ -3529,6 +3577,98 @@ mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem
 
        return code;
 }
+/*
+ * mono_aot_set_make_unreadable:
+ *
+ *   Set whenever to make all mmaped memory unreadable. In conjuction with a
+ * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
+ */
+void
+mono_aot_set_make_unreadable (gboolean unreadable)
+{
+       static int inited;
+
+       make_unreadable = unreadable;
+
+       if (make_unreadable && !inited) {
+               mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
+       }               
+}
+
+typedef struct {
+       MonoAotModule *module;
+       guint8 *ptr;
+} FindMapUserData;
+
+static void
+find_map (gpointer key, gpointer value, gpointer user_data)
+{
+       MonoAotModule *module = (MonoAotModule*)value;
+       FindMapUserData *data = (FindMapUserData*)user_data;
+
+       if (!data->module)
+               if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
+                       data->module = module;
+}
+
+static MonoAotModule*
+find_module_for_addr (void *ptr)
+{
+       FindMapUserData data;
+
+       if (!make_unreadable)
+               return NULL;
+
+       data.module = NULL;
+       data.ptr = (guint8*)ptr;
+
+       mono_aot_lock ();
+       g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
+       mono_aot_unlock ();
+
+       return data.module;
+}
+
+/*
+ * mono_aot_is_pagefault:
+ *
+ *   Should be called from a SIGSEGV signal handler to find out whenever @ptr is
+ * within memory allocated by this module.
+ */
+gboolean
+mono_aot_is_pagefault (void *ptr)
+{
+       if (!make_unreadable)
+               return FALSE;
+
+       /* 
+        * Not signal safe, but SIGSEGV's are synchronous, and
+        * this is only turned on by a MONO_DEBUG option.
+        */
+       return find_module_for_addr (ptr) != NULL;
+}
+
+/*
+ * mono_aot_handle_pagefault:
+ *
+ *   Handle a pagefault caused by an unreadable page by making it readable again.
+ */
+void
+mono_aot_handle_pagefault (void *ptr)
+{
+#ifndef PLATFORM_WIN32
+       guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
+       int res;
+
+       mono_aot_lock ();
+       res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
+       g_assert (res == 0);
+
+       n_pagefaults ++;
+       mono_aot_unlock ();
+#endif
+}
 
 #else
 /* AOT disabled */