[handle] rename mono_handle_arena_(initialize|deinitialize)
[mono.git] / mono / metadata / jit-info.c
index cc0b76622fb8458887eece9a0b021608ab7d49cc..33dd5471c5fc713016ed172149a0b0642720d9bb 100644 (file)
 #include <string.h>
 #include <sys/stat.h>
 
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 
 #include <mono/utils/atomic.h>
 #include <mono/utils/mono-compiler.h>
-#include <mono/utils/mono-logger-internal.h>
+#include <mono/utils/mono-logger-internals.h>
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-counters.h>
 #include <mono/utils/hazard-pointer.h>
@@ -33,7 +33,7 @@
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/metadata-internals.h>
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/mono-debug-debugger.h>
 #include <mono/metadata/mono-config.h>
@@ -90,7 +90,7 @@ jit_info_table_new_chunk (void)
 MonoJitInfoTable *
 mono_jit_info_table_new (MonoDomain *domain)
 {
-       MonoJitInfoTable *table = g_malloc0 (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*));
+       MonoJitInfoTable *table = (MonoJitInfoTable *)g_malloc0 (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*));
 
        table->domain = domain;
        table->num_chunks = 1;
@@ -197,7 +197,7 @@ jit_info_table_chunk_index (MonoJitInfoTableChunk *chunk, MonoThreadHazardPointe
 
        while (left < right) {
                int pos = (left + right) / 2;
-               MonoJitInfo *ji = get_hazardous_pointer((gpointer volatile*)&chunk->data [pos], hp, JIT_INFO_HAZARD_INDEX);
+               MonoJitInfo *ji = (MonoJitInfo *)get_hazardous_pointer((gpointer volatile*)&chunk->data [pos], hp, JIT_INFO_HAZARD_INDEX);
                gint8 *code_end = (gint8*)ji->code_start + ji->code_size;
 
                if (addr < code_end)
@@ -230,7 +230,7 @@ jit_info_table_find (MonoJitInfoTable *table, MonoThreadHazardPointers *hp, gint
                MonoJitInfoTableChunk *chunk = table->chunks [chunk_pos];
 
                while (pos < chunk->num_elements) {
-                       ji = get_hazardous_pointer ((gpointer volatile*)&chunk->data [pos], hp, JIT_INFO_HAZARD_INDEX);
+                       ji = (MonoJitInfo *)get_hazardous_pointer ((gpointer volatile*)&chunk->data [pos], hp, JIT_INFO_HAZARD_INDEX);
 
                        ++pos;
 
@@ -266,12 +266,13 @@ jit_info_table_find (MonoJitInfoTable *table, MonoThreadHazardPointers *hp, gint
  *
  * If TRY_AOT is FALSE, avoid loading information for missing methods from AOT images, which is currently not async safe.
  * In this case, only those AOT methods will be found whose jit info is already loaded.
+ * If ALLOW_TRAMPOLINES is TRUE, this can return a MonoJitInfo which represents a trampoline (ji->is_trampoline is true).
  * ASYNC SAFETY: When called in an async context (mono_thread_info_is_async_context ()), this is async safe.
  * In this case, the returned MonoJitInfo might not have metadata information, in particular,
  * mono_jit_info_get_method () could fail.
  */
 MonoJitInfo*
-mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_aot)
+mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_aot, gboolean allow_trampolines)
 {
        MonoJitInfoTable *table;
        MonoJitInfo *ji, *module_ji;
@@ -287,23 +288,28 @@ mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_
           table by a hazard pointer and make sure that the pointer is
           still there after we've made it hazardous, we don't have to
           worry about the writer freeing the table. */
-       table = get_hazardous_pointer ((gpointer volatile*)&domain->jit_info_table, hp, JIT_INFO_TABLE_HAZARD_INDEX);
+       table = (MonoJitInfoTable *)get_hazardous_pointer ((gpointer volatile*)&domain->jit_info_table, hp, JIT_INFO_TABLE_HAZARD_INDEX);
 
        ji = jit_info_table_find (table, hp, (gint8*)addr);
        if (hp)
                mono_hazard_pointer_clear (hp, JIT_INFO_TABLE_HAZARD_INDEX);
+       if (ji && ji->is_trampoline && !allow_trampolines)
+               return NULL;
        if (ji)
                return ji;
 
        /* Maybe its an AOT module */
        if (try_aot && mono_get_root_domain () && mono_get_root_domain ()->aot_modules) {
-               table = get_hazardous_pointer ((gpointer volatile*)&mono_get_root_domain ()->aot_modules, hp, JIT_INFO_TABLE_HAZARD_INDEX);
+               table = (MonoJitInfoTable *)get_hazardous_pointer ((gpointer volatile*)&mono_get_root_domain ()->aot_modules, hp, JIT_INFO_TABLE_HAZARD_INDEX);
                module_ji = jit_info_table_find (table, hp, (gint8*)addr);
                if (module_ji)
                        ji = jit_info_find_in_aot_func (domain, module_ji->d.image, addr);
                if (hp)
                        mono_hazard_pointer_clear (hp, JIT_INFO_TABLE_HAZARD_INDEX);
        }
+
+       if (ji && ji->is_trampoline && !allow_trampolines)
+               return NULL;
        
        return ji;
 }
@@ -311,7 +317,7 @@ mono_jit_info_table_find_internal (MonoDomain *domain, char *addr, gboolean try_
 MonoJitInfo*
 mono_jit_info_table_find (MonoDomain *domain, char *addr)
 {
-       return mono_jit_info_table_find_internal (domain, addr, TRUE);
+       return mono_jit_info_table_find_internal (domain, addr, TRUE, FALSE);
 }
 
 static G_GNUC_UNUSED void
@@ -329,10 +335,10 @@ jit_info_table_check (MonoJitInfoTable *table)
                g_assert (chunk->num_elements <= MONO_JIT_INFO_TABLE_CHUNK_SIZE);
 
                for (j = 0; j < chunk->num_elements; ++j) {
-                       MonoJitInfo *this = chunk->data [j];
+                       MonoJitInfo *this_ji = chunk->data [j];
                        MonoJitInfo *next;
 
-                       g_assert ((gint8*)this->code_start + this->code_size <= chunk->last_code_end);
+                       g_assert ((gint8*)this_ji->code_start + this_ji->code_size <= chunk->last_code_end);
 
                        if (j < chunk->num_elements - 1)
                                next = chunk->data [j + 1];
@@ -351,7 +357,7 @@ jit_info_table_check (MonoJitInfoTable *table)
                        } else
                                return;
 
-                       g_assert ((gint8*)this->code_start + this->code_size <= (gint8*)next->code_start + next->code_size);
+                       g_assert ((gint8*)this_ji->code_start + this_ji->code_size <= (gint8*)next->code_start + next->code_size);
                }
        }
 }
@@ -364,7 +370,7 @@ jit_info_table_realloc (MonoJitInfoTable *old)
        int required_size;
        int num_chunks;
        int new_chunk, new_element;
-       MonoJitInfoTable *new;
+       MonoJitInfoTable *result;
 
        /* number of needed places for elements needed */
        required_size = (int)((long)num_elements * JIT_INFO_TABLE_FILL_RATIO_DENOM / JIT_INFO_TABLE_FILL_RATIO_NOM);
@@ -375,12 +381,12 @@ jit_info_table_realloc (MonoJitInfoTable *old)
        }
        g_assert (num_chunks > 0);
 
-       new = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*) * num_chunks);
-       new->domain = old->domain;
-       new->num_chunks = num_chunks;
+       result = (MonoJitInfoTable *)g_malloc (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*) * num_chunks);
+       result->domain = old->domain;
+       result->num_chunks = num_chunks;
 
        for (i = 0; i < num_chunks; ++i)
-               new->chunks [i] = jit_info_table_new_chunk ();
+               result->chunks [i] = jit_info_table_new_chunk ();
 
        new_chunk = 0;
        new_element = 0;
@@ -392,9 +398,9 @@ jit_info_table_realloc (MonoJitInfoTable *old)
                for (j = 0; j < chunk_num_elements; ++j) {
                        if (!IS_JIT_INFO_TOMBSTONE (chunk->data [j])) {
                                g_assert (new_chunk < num_chunks);
-                               new->chunks [new_chunk]->data [new_element] = chunk->data [j];
+                               result->chunks [new_chunk]->data [new_element] = chunk->data [j];
                                if (++new_element >= JIT_INFO_TABLE_FILLED_NUM_ELEMENTS) {
-                                       new->chunks [new_chunk]->num_elements = new_element;
+                                       result->chunks [new_chunk]->num_elements = new_element;
                                        ++new_chunk;
                                        new_element = 0;
                                }
@@ -404,18 +410,18 @@ jit_info_table_realloc (MonoJitInfoTable *old)
 
        if (new_chunk < num_chunks) {
                g_assert (new_chunk == num_chunks - 1);
-               new->chunks [new_chunk]->num_elements = new_element;
-               g_assert (new->chunks [new_chunk]->num_elements > 0);
+               result->chunks [new_chunk]->num_elements = new_element;
+               g_assert (result->chunks [new_chunk]->num_elements > 0);
        }
 
        for (i = 0; i < num_chunks; ++i) {
-               MonoJitInfoTableChunk *chunk = new->chunks [i];
+               MonoJitInfoTableChunk *chunk = result->chunks [i];
                MonoJitInfo *ji = chunk->data [chunk->num_elements - 1];
 
-               new->chunks [i]->last_code_end = (gint8*)ji->code_start + ji->code_size;
+               result->chunks [i]->last_code_end = (gint8*)ji->code_start + ji->code_size;
        }
 
-       return new;
+       return result;
 }
 
 static void
@@ -444,7 +450,7 @@ jit_info_table_split_chunk (MonoJitInfoTableChunk *chunk, MonoJitInfoTableChunk
 static MonoJitInfoTable*
 jit_info_table_copy_and_split_chunk (MonoJitInfoTable *table, MonoJitInfoTableChunk *chunk)
 {
-       MonoJitInfoTable *new_table = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
+       MonoJitInfoTable *new_table = (MonoJitInfoTable *)g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
                + sizeof (MonoJitInfoTableChunk*) * (table->num_chunks + 1));
        int i, j;
 
@@ -471,28 +477,28 @@ jit_info_table_copy_and_split_chunk (MonoJitInfoTable *table, MonoJitInfoTableCh
 static MonoJitInfoTableChunk*
 jit_info_table_purify_chunk (MonoJitInfoTableChunk *old)
 {
-       MonoJitInfoTableChunk *new = jit_info_table_new_chunk ();
+       MonoJitInfoTableChunk *result = jit_info_table_new_chunk ();
        int i, j;
 
        j = 0;
        for (i = 0; i < old->num_elements; ++i) {
                if (!IS_JIT_INFO_TOMBSTONE (old->data [i]))
-                       new->data [j++] = old->data [i];
+                       result->data [j++] = old->data [i];
        }
 
-       new->num_elements = j;
-       if (new->num_elements > 0)
-               new->last_code_end = (gint8*)new->data [j - 1]->code_start + new->data [j - 1]->code_size;
+       result->num_elements = j;
+       if (result->num_elements > 0)
+               result->last_code_end = (gint8*)result->data [j - 1]->code_start + result->data [j - 1]->code_size;
        else
-               new->last_code_end = old->last_code_end;
+               result->last_code_end = old->last_code_end;
 
-       return new;
+       return result;
 }
 
 static MonoJitInfoTable*
 jit_info_table_copy_and_purify_chunk (MonoJitInfoTable *table, MonoJitInfoTableChunk *chunk)
 {
-       MonoJitInfoTable *new_table = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
+       MonoJitInfoTable *new_table = (MonoJitInfoTable *)g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
                + sizeof (MonoJitInfoTableChunk*) * table->num_chunks);
        int i, j;
 
@@ -684,10 +690,10 @@ jit_info_table_remove (MonoJitInfoTable *table, MonoJitInfo *ji)
        gpointer start = ji->code_start;
        int chunk_pos, pos;
 
-       chunk_pos = jit_info_table_index (table, start);
+       chunk_pos = jit_info_table_index (table, (gint8 *)start);
        g_assert (chunk_pos < table->num_chunks);
 
-       pos = jit_info_table_chunk_index (table->chunks [chunk_pos], NULL, start);
+       pos = jit_info_table_chunk_index (table->chunks [chunk_pos], NULL, (gint8 *)start);
 
        do {
                chunk = table->chunks [chunk_pos];
@@ -772,14 +778,14 @@ mono_jit_info_size (MonoJitInfoFlags flags, int num_clauses, int num_holes)
        int size = MONO_SIZEOF_JIT_INFO;
 
        size += num_clauses * sizeof (MonoJitExceptionInfo);
-       if (flags & JIT_INFO_HAS_CAS_INFO)
-               size += sizeof (MonoMethodCasInfo);
        if (flags & JIT_INFO_HAS_GENERIC_JIT_INFO)
                size += sizeof (MonoGenericJitInfo);
        if (flags & JIT_INFO_HAS_TRY_BLOCK_HOLES)
                size += sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
        if (flags & JIT_INFO_HAS_ARCH_EH_INFO)
                size += sizeof (MonoArchEHJitInfo);
+       if (flags & JIT_INFO_HAS_THUNK_INFO)
+               size += sizeof (MonoThunkJitInfo);
        return size;
 }
 
@@ -791,14 +797,14 @@ mono_jit_info_init (MonoJitInfo *ji, MonoMethod *method, guint8 *code, int code_
        ji->code_start = code;
        ji->code_size = code_size;
        ji->num_clauses = num_clauses;
-       if (flags & JIT_INFO_HAS_CAS_INFO)
-               ji->has_cas_info = 1;
        if (flags & JIT_INFO_HAS_GENERIC_JIT_INFO)
                ji->has_generic_jit_info = 1;
        if (flags & JIT_INFO_HAS_TRY_BLOCK_HOLES)
                ji->has_try_block_holes = 1;
        if (flags & JIT_INFO_HAS_ARCH_EH_INFO)
                ji->has_arch_eh_info = 1;
+       if (flags & JIT_INFO_HAS_THUNK_INFO)
+               ji->has_thunk_info = 1;
 }
 
 gpointer
@@ -817,6 +823,7 @@ MonoMethod*
 mono_jit_info_get_method (MonoJitInfo* ji)
 {
        g_assert (!ji->async);
+       g_assert (!ji->is_trampoline);
        return ji->d.method;
 }
 
@@ -928,10 +935,10 @@ mono_jit_info_get_arch_eh_info (MonoJitInfo *ji)
        }
 }
 
-MonoMethodCasInfo*
-mono_jit_info_get_cas_info (MonoJitInfo *ji)
+MonoThunkJitInfo*
+mono_jit_info_get_thunk_info (MonoJitInfo *ji)
 {
-       if (ji->has_cas_info) {
+       if (ji->has_thunk_info) {
                char *ptr = (char*)&ji->clauses [ji->num_clauses];
                if (ji->has_generic_jit_info)
                        ptr += sizeof (MonoGenericJitInfo);
@@ -939,7 +946,7 @@ mono_jit_info_get_cas_info (MonoJitInfo *ji)
                        ptr += try_block_hole_table_size (ji);
                if (ji->has_arch_eh_info)
                        ptr += sizeof (MonoArchEHJitInfo);
-               return (MonoMethodCasInfo*)ptr;
+               return (MonoThunkJitInfo*)ptr;
        } else {
                return NULL;
        }