Implement wasm-c-api frame/trap APIs for interpreter mode (#660)
And enable to cache compiled AOT file buffer for wasm-c-api JIT mode Avoid checks that rely on undefined C behavior Fix issues of wasm-c-api sample trap and callback_chain Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
@ -1950,7 +1950,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
|
||||
local_type_index = 0;
|
||||
for (j = 0; j < local_set_count; j++) {
|
||||
read_leb_uint32(p_code, buf_code_end, sub_local_count);
|
||||
if (local_type_index + sub_local_count <= local_type_index
|
||||
if (!sub_local_count
|
||||
|| local_type_index > UINT32_MAX - sub_local_count
|
||||
|| local_type_index + sub_local_count > local_count) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"invalid local count");
|
||||
|
||||
@ -981,8 +981,10 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
|
||||
local_type_index = 0;
|
||||
for (j = 0; j < local_set_count; j++) {
|
||||
read_leb_uint32(p_code, buf_code_end, sub_local_count);
|
||||
bh_assert(!(local_type_index + sub_local_count <= local_type_index
|
||||
|| local_type_index + sub_local_count > local_count));
|
||||
bh_assert(sub_local_count
|
||||
&& local_type_index <= UINT32_MAX - sub_local_count
|
||||
&& local_type_index + sub_local_count
|
||||
<= local_count);
|
||||
|
||||
CHECK_BUF(p_code, buf_code_end, 1);
|
||||
/* 0x7F/0x7E/0x7D/0x7C */
|
||||
|
||||
@ -1580,6 +1580,14 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
|
||||
if (module_inst->exec_env_singleton)
|
||||
wasm_exec_env_destroy(module_inst->exec_env_singleton);
|
||||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
if (module_inst->frames) {
|
||||
bh_vector_destroy(module_inst->frames);
|
||||
wasm_runtime_free(module_inst->frames);
|
||||
module_inst->frames = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
wasm_runtime_free(module_inst);
|
||||
}
|
||||
|
||||
@ -1925,7 +1933,7 @@ wasm_validate_app_addr(WASMModuleInstance *module_inst,
|
||||
memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
|
||||
|
||||
/* integer overflow check */
|
||||
if (app_offset + size < app_offset) {
|
||||
if (app_offset > UINT32_MAX - size) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
@ -1949,7 +1957,7 @@ wasm_validate_native_addr(WASMModuleInstance *module_inst,
|
||||
}
|
||||
|
||||
/* integer overflow check */
|
||||
if (addr + size < addr) {
|
||||
if ((uintptr_t)addr > UINTPTR_MAX - size) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
@ -2421,19 +2429,59 @@ void
|
||||
wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
|
||||
{
|
||||
WASMModuleInstance *module_inst =
|
||||
(WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
|
||||
WASMInterpFrame *cur_frame =
|
||||
wasm_exec_env_get_cur_frame(exec_env);
|
||||
WASMFunctionInstance *func_inst;
|
||||
WASMExportFuncInstance *export_func;
|
||||
const char *func_name = NULL;
|
||||
uint32 n, i;
|
||||
(WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
|
||||
WASMInterpFrame *first_frame,
|
||||
*cur_frame = wasm_exec_env_get_cur_frame(exec_env);
|
||||
uint32 n = 0;
|
||||
|
||||
/* release previous stack frame */
|
||||
if (module_inst->frames) {
|
||||
bh_vector_destroy(module_inst->frames);
|
||||
wasm_runtime_free(module_inst->frames);
|
||||
module_inst->frames = NULL;
|
||||
}
|
||||
|
||||
/* count frames includes a function */
|
||||
first_frame = cur_frame;
|
||||
while (cur_frame) {
|
||||
if (cur_frame->function) {
|
||||
n++;
|
||||
}
|
||||
cur_frame = cur_frame->prev_frame;
|
||||
}
|
||||
|
||||
if (!(module_inst->frames = runtime_malloc(
|
||||
(uint64)sizeof(Vector), module_inst->cur_exception, 128))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!bh_vector_init(module_inst->frames, n, sizeof(struct WASMFrame))) {
|
||||
wasm_runtime_free(module_inst->frames);
|
||||
module_inst->frames = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
cur_frame = first_frame;
|
||||
n = 0;
|
||||
os_printf("\n");
|
||||
for (n = 0; cur_frame && cur_frame->function; n++) {
|
||||
func_name = NULL;
|
||||
func_inst = cur_frame->function;
|
||||
while (cur_frame) {
|
||||
struct WASMFrame frame = { 0 };
|
||||
WASMFunctionInstance *func_inst = cur_frame->function;
|
||||
const char *func_name = NULL;
|
||||
|
||||
if (!func_inst) {
|
||||
cur_frame = cur_frame->prev_frame;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* place holder, will overwrite it in wasm_c_api */
|
||||
frame.instance = module_inst;
|
||||
frame.module_offset = 0;
|
||||
frame.func_index = func_inst - module_inst->functions;
|
||||
frame.func_offset =
|
||||
cur_frame->ip ? cur_frame->ip - func_inst->u.func->code : 0;
|
||||
|
||||
/* look for the function name */
|
||||
if (func_inst->is_import_func) {
|
||||
func_name = func_inst->u.func_import->field_name;
|
||||
}
|
||||
@ -2444,8 +2492,10 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
|
||||
/* if custom name section is not generated,
|
||||
search symbols from export table */
|
||||
if (!func_name) {
|
||||
uint32 i;
|
||||
for (i = 0; i < module_inst->export_func_count; i++) {
|
||||
export_func = module_inst->export_functions + i;
|
||||
WASMExportFuncInstance *export_func =
|
||||
module_inst->export_functions + i;
|
||||
if (export_func->function == func_inst) {
|
||||
func_name = export_func->name;
|
||||
break;
|
||||
@ -2462,7 +2512,11 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
|
||||
os_printf("#%02d %s \n", n, func_name);
|
||||
}
|
||||
|
||||
/* keep print */
|
||||
bh_vector_append(module_inst->frames, &frame);
|
||||
|
||||
cur_frame = cur_frame->prev_frame;
|
||||
n++;
|
||||
}
|
||||
os_printf("\n");
|
||||
}
|
||||
|
||||
@ -150,6 +150,15 @@ typedef struct WASMExportMemInstance {
|
||||
} WASMExportMemInstance;
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
struct WASMFrame {
|
||||
void *instance;
|
||||
uint32 module_offset;
|
||||
uint32 func_index;
|
||||
uint32 func_offset;
|
||||
};
|
||||
#endif
|
||||
|
||||
struct WASMModuleInstance {
|
||||
/* Module instance type, for module instance loaded from
|
||||
WASM bytecode binary, this field is Wasm_Module_Bytecode;
|
||||
@ -209,6 +218,10 @@ struct WASMModuleInstance {
|
||||
/* The exception buffer of wasm interpreter for current thread. */
|
||||
char cur_exception[128];
|
||||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
Vector *frames;
|
||||
#endif
|
||||
|
||||
/* The custom data that can be set/get by
|
||||
* wasm_set_custom_data/wasm_get_custom_data */
|
||||
void *custom_data;
|
||||
|
||||
Reference in New Issue
Block a user