Fix issues reported by Coverity (#1775)

Fix some issues reported by Coverity and fix windows exception
check with guard page issue
This commit is contained in:
Wenyong Huang 2022-12-01 19:24:13 +08:00 committed by GitHub
parent 6eaf779a2d
commit 1652f22a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 35 deletions

View File

@ -143,9 +143,9 @@ runtime_signal_handler(void *sig_addr)
WASMJmpBuf *jmpbuf_node; WASMJmpBuf *jmpbuf_node;
uint8 *mapped_mem_start_addr = NULL; uint8 *mapped_mem_start_addr = NULL;
uint8 *mapped_mem_end_addr = NULL; uint8 *mapped_mem_end_addr = NULL;
uint32 page_size = os_getpagesize();
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0 #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
uint8 *stack_min_addr; uint8 *stack_min_addr;
uint32 page_size;
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT; uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
#endif #endif
@ -163,7 +163,6 @@ runtime_signal_handler(void *sig_addr)
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0 #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
/* Get stack info of current thread */ /* Get stack info of current thread */
page_size = os_getpagesize();
stack_min_addr = os_thread_get_stack_boundary(); stack_min_addr = os_thread_get_stack_boundary();
#endif #endif
@ -216,29 +215,41 @@ runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
mapped_mem_start_addr = memory_inst->memory_data; mapped_mem_start_addr = memory_inst->memory_data;
mapped_mem_end_addr = mapped_mem_end_addr =
memory_inst->memory_data + 8 * (uint64)BH_GB; memory_inst->memory_data + 8 * (uint64)BH_GB;
if (mapped_mem_start_addr <= (uint8 *)sig_addr }
&& (uint8 *)sig_addr < mapped_mem_end_addr) {
/* The address which causes segmentation fault is inside if (memory_inst && mapped_mem_start_addr <= (uint8 *)sig_addr
the memory instance's guard regions. && (uint8 *)sig_addr < mapped_mem_end_addr) {
Set exception and let the wasm func continue to run, when /* The address which causes segmentation fault is inside
the wasm func returns, the caller will check whether the the memory instance's guard regions.
exception is thrown and return to runtime. */ Set exception and let the wasm func continue to run, when
wasm_set_exception(module_inst, the wasm func returns, the caller will check whether the
"out of bounds memory access"); exception is thrown and return to runtime. */
if (module_inst->module_type == Wasm_Module_Bytecode) { wasm_set_exception(module_inst, "out of bounds memory access");
/* Continue to search next exception handler for if (module_inst->module_type == Wasm_Module_Bytecode) {
interpreter mode as it can be caught by /* Continue to search next exception handler for
`__try { .. } __except { .. }` sentences in interpreter mode as it can be caught by
wasm_runtime.c */ `__try { .. } __except { .. }` sentences in
return EXCEPTION_CONTINUE_SEARCH; wasm_runtime.c */
} return EXCEPTION_CONTINUE_SEARCH;
else { }
/* Skip current instruction and continue to run for else {
AOT mode. TODO: implement unwind support for AOT /* Skip current instruction and continue to run for
code in Windows platform */ AOT mode. TODO: implement unwind support for AOT
exce_info->ContextRecord->Rip++; code in Windows platform */
return EXCEPTION_CONTINUE_EXECUTION; exce_info->ContextRecord->Rip++;
} return EXCEPTION_CONTINUE_EXECUTION;
}
}
else if (exec_env_tls->exce_check_guard_page <= (uint8 *)sig_addr
&& (uint8 *)sig_addr
< exec_env_tls->exce_check_guard_page + page_size) {
bh_assert(wasm_get_exception(module_inst));
if (module_inst->module_type == Wasm_Module_Bytecode) {
return EXCEPTION_CONTINUE_SEARCH;
}
else {
exce_info->ContextRecord->Rip++;
return EXCEPTION_CONTINUE_EXECUTION;
} }
} }
} }

View File

@ -462,6 +462,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
false, NULL, NULL))) { false, NULL, NULL))) {
goto fail; goto fail;
} }
aot_block_destroy(block);
return aot_handle_next_reachable_block(comp_ctx, func_ctx, return aot_handle_next_reachable_block(comp_ctx, func_ctx,
p_frame_ip); p_frame_ip);
} }

View File

@ -5125,10 +5125,11 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
/* Free the emit data */ /* Free the emit data */
wasm_runtime_free(emit_data); wasm_runtime_free(emit_data);
return true; return true;
fail: fail:
/* Free the emit data */
wasm_runtime_free(emit_data);
return false; return false;
} }
#endif #endif

View File

@ -3081,14 +3081,15 @@ wasi_ssp_sock_addr_resolve(
size_t _max_info_size; size_t _max_info_size;
size_t actual_info_size; size_t actual_info_size;
if (!ns_lookup_list_search(ns_lookup_list, host)) {
return __WASI_EACCES;
}
if (!wamr_addr_info) { if (!wamr_addr_info) {
return __WASI_ENOMEM; return __WASI_ENOMEM;
} }
if (!ns_lookup_list_search(ns_lookup_list, host)) {
wasm_runtime_free(wamr_addr_info);
return __WASI_EACCES;
}
int ret = os_socket_addr_resolve( int ret = os_socket_addr_resolve(
host, service, hints->hints_enabled ? &hints_is_tcp : NULL, host, service, hints->hints_enabled ? &hints_is_tcp : NULL,
hints->hints_enabled ? &hints_is_ipv4 : NULL, wamr_addr_info, hints->hints_enabled ? &hints_is_ipv4 : NULL, wamr_addr_info,

View File

@ -26,7 +26,6 @@ main(int argc, char *argv_main[])
wasm_module_inst_t module_inst = NULL; wasm_module_inst_t module_inst = NULL;
wasm_exec_env_t exec_env = NULL; wasm_exec_env_t exec_env = NULL;
uint32 buf_size, stack_size = 8092, heap_size = 8092; uint32 buf_size, stack_size = 8092, heap_size = 8092;
uint32_t wasm_buffer = 0;
RuntimeInitArgs init_args; RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs)); memset(&init_args, 0, sizeof(RuntimeInitArgs));
@ -103,11 +102,8 @@ main(int argc, char *argv_main[])
fail: fail:
if (exec_env) if (exec_env)
wasm_runtime_destroy_exec_env(exec_env); wasm_runtime_destroy_exec_env(exec_env);
if (module_inst) { if (module_inst)
if (wasm_buffer)
wasm_runtime_module_free(module_inst, wasm_buffer);
wasm_runtime_deinstantiate(module_inst); wasm_runtime_deinstantiate(module_inst);
}
if (module) if (module)
wasm_runtime_unload(module); wasm_runtime_unload(module);
if (buffer) if (buffer)