فهرست منبع

aot: Avoid possible relocations around "stack_sizes" for XIP mode (#2322)

Fixes https://github.com/bytecodealliance/wasm-micro-runtime/issues/2316

Lightly tested on riscv64 qemu.
YAMAMOTO Takashi 2 سال پیش
والد
کامیت
03418ef5ac
4فایلهای تغییر یافته به همراه57 افزوده شده و 1 حذف شده
  1. 7 0
      core/iwasm/aot/aot_loader.c
  2. 5 0
      core/iwasm/aot/aot_runtime.c
  3. 5 0
      core/iwasm/aot/aot_runtime.h
  4. 40 1
      core/iwasm/compilation/aot_llvm.c

+ 7 - 0
core/iwasm/aot/aot_loader.c

@@ -1843,6 +1843,13 @@ get_data_section_addr(AOTModule *module, const char *section_name,
     return NULL;
     return NULL;
 }
 }
 
 
+const void *
+aot_get_data_section_addr(AOTModule *module, const char *section_name,
+                          uint32 *p_data_size)
+{
+    return get_data_section_addr(module, section_name, p_data_size);
+}
+
 static void *
 static void *
 resolve_target_sym(const char *symbol, int32 *p_index)
 resolve_target_sym(const char *symbol, int32 *p_index)
 {
 {

+ 5 - 0
core/iwasm/aot/aot_runtime.c

@@ -42,6 +42,8 @@ bh_static_assert(offsetof(AOTModuleInstance, cur_exception)
 bh_static_assert(offsetof(AOTModuleInstance, global_table_data)
 bh_static_assert(offsetof(AOTModuleInstance, global_table_data)
                  == 13 * sizeof(uint64) + 128 + 11 * sizeof(uint64));
                  == 13 * sizeof(uint64) + 128 + 11 * sizeof(uint64));
 
 
+bh_static_assert(offsetof(AOTModuleInstanceExtra, stack_sizes) == 0);
+
 static void
 static void
 set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
 set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
 {
 {
@@ -1210,6 +1212,9 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
 #endif
 #endif
     module_inst->default_wasm_stack_size = stack_size;
     module_inst->default_wasm_stack_size = stack_size;
 
 
+    ((AOTModuleInstanceExtra *)module_inst->e)->stack_sizes =
+        aot_get_data_section_addr(module, AOT_STACK_SIZES_SECTION_NAME, NULL);
+
 #if WASM_ENABLE_PERF_PROFILING != 0
 #if WASM_ENABLE_PERF_PROFILING != 0
     total_size = (uint64)sizeof(AOTFuncPerfProfInfo)
     total_size = (uint64)sizeof(AOTFuncPerfProfInfo)
                  * (module->import_func_count + module->func_count);
                  * (module->import_func_count + module->func_count);

+ 5 - 0
core/iwasm/aot/aot_runtime.h

@@ -88,6 +88,7 @@ typedef struct AOTFunctionInstance {
 } AOTFunctionInstance;
 } AOTFunctionInstance;
 
 
 typedef struct AOTModuleInstanceExtra {
 typedef struct AOTModuleInstanceExtra {
+    DefPointer(const uint32 *, stack_sizes);
     CApiFuncImport *c_api_func_imports;
     CApiFuncImport *c_api_func_imports;
 } AOTModuleInstanceExtra;
 } AOTModuleInstanceExtra;
 
 
@@ -633,6 +634,10 @@ aot_dump_perf_profiling(const AOTModuleInstance *module_inst);
 const uint8 *
 const uint8 *
 aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len);
 aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len);
 
 
+const void *
+aot_get_data_section_addr(AOTModule *module, const char *section_name,
+                          uint32 *p_data_size);
+
 #if WASM_ENABLE_STATIC_PGO != 0
 #if WASM_ENABLE_STATIC_PGO != 0
 void
 void
 llvm_profile_instrument_target(uint64 target_value, void *data,
 llvm_profile_instrument_target(uint64 target_value, void *data,

+ 40 - 1
core/iwasm/compilation/aot_llvm.c

@@ -7,6 +7,7 @@
 #include "aot_llvm_extra2.h"
 #include "aot_llvm_extra2.h"
 #include "aot_compiler.h"
 #include "aot_compiler.h"
 #include "aot_emit_exception.h"
 #include "aot_emit_exception.h"
+#include "aot_emit_table.h"
 #include "../aot/aot_runtime.h"
 #include "../aot/aot_runtime.h"
 #include "../aot/aot_intrinsic.h"
 #include "../aot/aot_intrinsic.h"
 
 
@@ -230,6 +231,17 @@ aot_estimate_stack_usage_for_function_call(const AOTCompContext *comp_ctx,
     return size;
     return size;
 }
 }
 
 
+static uint32
+get_inst_extra_offset(AOTCompContext *comp_ctx)
+{
+    const AOTCompData *comp_data = comp_ctx->comp_data;
+    uint32 table_count = comp_data->import_table_count + comp_data->table_count;
+    uint64 offset = get_tbl_inst_offset(comp_ctx, NULL, table_count);
+    bh_assert(offset <= UINT_MAX);
+    offset = align_uint(offset, 8);
+    return offset;
+}
+
 /*
 /*
  * a "precheck" function performs a few things before calling wrapped_func.
  * a "precheck" function performs a few things before calling wrapped_func.
  *
  *
@@ -327,9 +339,36 @@ aot_add_precheck_function(AOTCompContext *comp_ctx, LLVMModuleRef module,
     /*
     /*
      * load the value for this wrapped function from the stack_sizes array
      * load the value for this wrapped function from the stack_sizes array
      */
      */
+    LLVMValueRef stack_sizes;
+    if (comp_ctx->is_indirect_mode) {
+        uint32 offset_u32;
+        LLVMValueRef offset;
+        LLVMValueRef stack_sizes_p;
+
+        offset_u32 = get_inst_extra_offset(comp_ctx);
+        offset_u32 += offsetof(AOTModuleInstanceExtra, stack_sizes);
+        offset = I32_CONST(offset_u32);
+        if (!offset) {
+            goto fail;
+        }
+        stack_sizes_p =
+            LLVMBuildInBoundsGEP2(b, INT8_TYPE, func_ctx->aot_inst, &offset, 1,
+                                  "aot_inst_stack_sizes_p");
+        if (!stack_sizes_p) {
+            goto fail;
+        }
+        stack_sizes =
+            LLVMBuildLoad2(b, INT32_PTR_TYPE, stack_sizes_p, "stack_sizes");
+        if (!stack_sizes) {
+            goto fail;
+        }
+    }
+    else {
+        stack_sizes = comp_ctx->stack_sizes;
+    }
     LLVMValueRef func_index_const = I32_CONST(func_index);
     LLVMValueRef func_index_const = I32_CONST(func_index);
     LLVMValueRef sizes =
     LLVMValueRef sizes =
-        LLVMBuildBitCast(b, comp_ctx->stack_sizes, INT32_PTR_TYPE, "sizes");
+        LLVMBuildBitCast(b, stack_sizes, INT32_PTR_TYPE, "sizes");
     if (!sizes) {
     if (!sizes) {
         goto fail;
         goto fail;
     }
     }