Skip to content

Commit

Permalink
aot: avoid possible relocations around "stack_sizes" for indirect mode
Browse files Browse the repository at this point in the history
Fixes #2316

Lightly tested on riscv64 qemu.
  • Loading branch information
yamt committed Jun 27, 2023
1 parent 0a0739e commit ebb6a68
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/iwasm/aot/aot_loader.c
Expand Up @@ -1843,6 +1843,13 @@ get_data_section_addr(AOTModule *module, const char *section_name,
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 *
resolve_target_sym(const char *symbol, int32 *p_index)
{
Expand Down
7 changes: 6 additions & 1 deletion core/iwasm/aot/aot_runtime.c
Expand Up @@ -39,8 +39,10 @@ bh_static_assert(offsetof(AOTModuleInstance, func_type_indexes)
== 6 * sizeof(uint64));
bh_static_assert(offsetof(AOTModuleInstance, cur_exception)
== 13 * sizeof(uint64));
bh_static_assert(offsetof(AOTModuleInstance, global_table_data)
bh_static_assert(offsetof(AOTModuleInstance, aot_stack_sizes)
== 13 * sizeof(uint64) + 128 + 11 * sizeof(uint64));
bh_static_assert(offsetof(AOTModuleInstance, global_table_data)
== 13 * sizeof(uint64) + 128 + 12 * sizeof(uint64));

static void
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
Expand Down Expand Up @@ -1221,6 +1223,9 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
#endif
module_inst->default_wasm_stack_size = stack_size;

module_inst->aot_stack_sizes =
aot_get_data_section_addr(module, AOT_STACK_SIZES_SECTION_NAME, NULL);

#if WASM_ENABLE_PERF_PROFILING != 0
total_size = (uint64)sizeof(AOTFuncPerfProfInfo)
* (module->import_func_count + module->func_count);
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/aot/aot_runtime.h
Expand Up @@ -636,6 +636,10 @@ aot_dump_perf_profiling(const AOTModuleInstance *module_inst);
const uint8 *
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
void
llvm_profile_instrument_target(uint64 target_value, void *data,
Expand Down
24 changes: 23 additions & 1 deletion core/iwasm/compilation/aot_llvm.c
Expand Up @@ -327,9 +327,31 @@ aot_add_precheck_function(AOTCompContext *comp_ctx, LLVMModuleRef module,
/*
* load the value for this wrapped function from the stack_sizes array
*/
LLVMValueRef stack_sizes;
if (comp_ctx->is_indirect_mode) {
LLVMValueRef offset =
I32_CONST(offsetof(AOTModuleInstance, aot_stack_sizes));
if (!offset) {
goto fail;
}
LLVMValueRef 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 sizes =
LLVMBuildBitCast(b, comp_ctx->stack_sizes, INT32_PTR_TYPE, "sizes");
LLVMBuildBitCast(b, stack_sizes, INT32_PTR_TYPE, "sizes");
if (!sizes) {
goto fail;
}
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.h
Expand Up @@ -322,6 +322,9 @@ struct WASMModuleInstance {
uint32 default_wasm_stack_size;
uint32 reserved[3];

/* AOT */
DefPointer(const uint32 *, aot_stack_sizes);

/*
* +------------------------------+ <-- memories
* | WASMMemoryInstance[mem_count], mem_count is always 1 for LLVM JIT/AOT
Expand Down

0 comments on commit ebb6a68

Please sign in to comment.