Skip to content

Commit

Permalink
Refactor: improve function pointer retrieval and error handling in AO…
Browse files Browse the repository at this point in the history
…T runtime
  • Loading branch information
lum1n0us committed Dec 5, 2024
1 parent d783ef3 commit 68f35cb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 36 deletions.
51 changes: 22 additions & 29 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@ functions_instantiate(AOTModuleInstance *module_inst, AOTModule *module,

function->import_func_inst = aot_lookup_function(
function->import_module_inst, import->func_name);
if (!function->import_func_inst) {
set_error_buf_v(error_buf, error_buf_size,
"unknown import function \"%s\"",
import->func_name);
return NULL;
}
}

/* from c_api (loading) */
Expand Down Expand Up @@ -1863,6 +1869,7 @@ cmp_func_inst(const void *a, const void *b)
return strcmp(func_inst1->func_name, func_inst2->func_name);
}

/* TODO: Can I reuse the AOTFunctionInstance ?*/
static bool
create_export_funcs(AOTModuleInstance *module_inst, AOTModule *module,
char *error_buf, uint32 error_buf_size)
Expand All @@ -1883,23 +1890,16 @@ create_export_funcs(AOTModuleInstance *module_inst, AOTModule *module,

for (i = 0; i < module->export_count; i++) {
if (exports[i].kind == EXPORT_KIND_FUNC) {
export_func->func_name = exports[i].name;
export_func->func_index = exports[i].index;
if (export_func->func_index < module->import_func_count) {
export_func->is_import_func = true;
export_func->u.func_import =
&module->import_funcs[export_func->func_index];
}
else {
export_func->is_import_func = false;
func_index =
export_func->func_index - module->import_func_count;
ftype_index = module->func_type_indexes[func_index];
export_func->u.func.func_type =
(AOTFuncType *)module->types[ftype_index];
export_func->u.func.func_ptr =
module->func_ptrs[func_index];
AOTFunctionInstance *function =
aot_locate_function_instance(module_inst, exports[i].index);
if (!function) {
set_error_buf_v(error_buf, error_buf_size,
"unknown function %s", exports[i].name);
return false;
}

*export_func = *function;
export_func->func_name = exports[i].name;
export_func++;
}
}
Expand Down Expand Up @@ -2849,9 +2849,12 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
bool ret;
/* init_func_ptrs() has already copied func_ptr_linked value*/
void *func_ptr = function->u.func.func_ptr;
// bh_assert(false);
void *attachment = NULL;

void *func_ptr =
aot_get_function_pointer(module_inst, function->func_index, function);

#if WASM_ENABLE_MULTI_MODULE != 0
/*
* TODO: this searching for sub_module_inst
Expand Down Expand Up @@ -3509,7 +3512,7 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
uint32 func_type_idx = func_type_indexes[func_idx];
AOTFuncType *func_type = (AOTFuncType *)aot_module->types[func_type_idx];
void **func_ptrs = module_inst->func_ptrs;
void *func_ptr = func_ptrs[func_idx];
void *func_ptr = NULL;
AOTImportFunc *import_func;
const char *signature;
void *attachment;
Expand All @@ -3522,14 +3525,7 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
AOTFunctionInstance *func_inst =
aot_locate_function_instance(module_inst, func_idx);

if (func_inst->call_conv_wasm_c_api) {
c_api_func_import = module_inst->c_api_func_imports
? module_inst->c_api_func_imports + func_idx
: NULL;
func_ptr =
c_api_func_import ? c_api_func_import->func_ptr_linked : NULL;
}

func_ptr = aot_get_function_pointer(module_inst, func_idx, func_inst);
if (!func_ptr) {
snprintf(buf, sizeof(buf),
"failed to call unlinked import function (%s, %s)",
Expand Down Expand Up @@ -3563,9 +3559,6 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
"create singleton exec_env failed");
goto fail;
}

func_ptr = func_inst->import_module_inst->func_ptrs[func_idx];
bh_assert(func_ptr);
}

#if WASM_ENABLE_AOT_STACK_FRAME != 0
Expand Down
38 changes: 35 additions & 3 deletions core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,46 @@ aot_locate_table_elems(const AOTModule *module, AOTTableInstance *table,
}

static inline AOTFunctionInstance *
aot_locate_function_instance(const AOTModuleInstance *module_inst,
uint32 func_idx)
aot_locate_function_instance(const AOTModuleInstance *inst, uint32 func_idx)
{
AOTModuleInstanceExtra *e = (AOTModuleInstanceExtra *)module_inst->e;
AOTModuleInstanceExtra *e = (AOTModuleInstanceExtra *)inst->e;
AOTFunctionInstance *func = e->functions + func_idx;
return func;
}

static inline void *
aot_get_function_pointer(const AOTModuleInstance *module_inst, uint32 func_idx,
const AOTFunctionInstance *func_inst)
{
void **func_ptrs = module_inst->func_ptrs;
void *func_ptr = NULL;
CApiFuncImport *c_api_func_import = NULL;

if (func_inst->call_conv_wasm_c_api) {
c_api_func_import = module_inst->c_api_func_imports
? module_inst->c_api_func_imports + func_idx
: NULL;
func_ptr =
c_api_func_import ? c_api_func_import->func_ptr_linked : NULL;
}
else if (func_inst->call_conv_raw) {
func_ptr = func_ptrs[func_idx];
}
else {
if (func_inst->import_module_inst) {
uint32 funx_idx_of_import_func =
func_inst->import_func_inst->func_index;
func_ptr = func_inst->import_module_inst
->func_ptrs[funx_idx_of_import_func];
}
else {
func_ptr = func_ptrs[func_idx];
}
}

return func_ptr;
}

/**
* Load a AOT module from aot file buffer
* @param buf the byte buffer which contains the AOT file data
Expand Down
18 changes: 14 additions & 4 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -4851,7 +4851,6 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
func_type_indexes = module_inst->func_type_indexes;
func_type_idx = func_type_indexes[func_idx];
func_type = (WASMFuncType *)module->types[func_type_idx];
func_ptr = module_inst->func_ptrs[func_idx];

bh_assert(func_idx < module->import_function_count);

Expand All @@ -4867,6 +4866,20 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
func_ptr =
c_api_func_import ? c_api_func_import->func_ptr_linked : NULL;
}
else if (func_inst->call_conv_raw) {
func_ptr = module_inst->func_ptrs[func_idx];
}
else {
if (func_inst->import_module_inst) {
uint32 funx_idx_of_import_func = wasm_calc_function_index(
func_inst->import_module_inst, func_inst->import_func_inst);
func_ptr = func_inst->import_module_inst
->func_ptrs[funx_idx_of_import_func];
}
else {
func_ptr = module_inst->func_ptrs[func_idx];
}
}

if (!func_ptr) {
snprintf(buf, sizeof(buf),
Expand Down Expand Up @@ -4901,9 +4914,6 @@ llvm_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
"create singleton exec_env failed");
goto fail;
}

func_ptr = func_inst->import_module_inst->func_ptrs[func_idx];
bh_assert(func_ptr);
}

/* from wasm_native */
Expand Down
8 changes: 8 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ wasm_locate_function_instance(const WASMModuleInstance *module_inst,
return func;
}

static inline uint32
wasm_calc_function_index(const WASMModuleInstance *module_inst,
const WASMFunctionInstance *func)
{
return (uint32)(func
- ((WASMModuleInstanceExtra *)module_inst->e)->functions);
}

static inline uint32
wasm_get_tbl_data_slots(const WASMTableType *table_type,
const WASMTableImport *import_type)
Expand Down

0 comments on commit 68f35cb

Please sign in to comment.