Skip to content

Commit 1652f22

Browse files
authored
Fix issues reported by Coverity (#1775)
Fix some issues reported by Coverity and fix windows exception check with guard page issue
1 parent 6eaf779 commit 1652f22

5 files changed

Lines changed: 45 additions & 35 deletions

File tree

core/iwasm/common/wasm_runtime_common.c

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ runtime_signal_handler(void *sig_addr)
143143
WASMJmpBuf *jmpbuf_node;
144144
uint8 *mapped_mem_start_addr = NULL;
145145
uint8 *mapped_mem_end_addr = NULL;
146+
uint32 page_size = os_getpagesize();
146147
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
147148
uint8 *stack_min_addr;
148-
uint32 page_size;
149149
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
150150
#endif
151151

@@ -163,7 +163,6 @@ runtime_signal_handler(void *sig_addr)
163163

164164
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
165165
/* Get stack info of current thread */
166-
page_size = os_getpagesize();
167166
stack_min_addr = os_thread_get_stack_boundary();
168167
#endif
169168

@@ -216,29 +215,41 @@ runtime_exception_handler(EXCEPTION_POINTERS *exce_info)
216215
mapped_mem_start_addr = memory_inst->memory_data;
217216
mapped_mem_end_addr =
218217
memory_inst->memory_data + 8 * (uint64)BH_GB;
219-
if (mapped_mem_start_addr <= (uint8 *)sig_addr
220-
&& (uint8 *)sig_addr < mapped_mem_end_addr) {
221-
/* The address which causes segmentation fault is inside
222-
the memory instance's guard regions.
223-
Set exception and let the wasm func continue to run, when
224-
the wasm func returns, the caller will check whether the
225-
exception is thrown and return to runtime. */
226-
wasm_set_exception(module_inst,
227-
"out of bounds memory access");
228-
if (module_inst->module_type == Wasm_Module_Bytecode) {
229-
/* Continue to search next exception handler for
230-
interpreter mode as it can be caught by
231-
`__try { .. } __except { .. }` sentences in
232-
wasm_runtime.c */
233-
return EXCEPTION_CONTINUE_SEARCH;
234-
}
235-
else {
236-
/* Skip current instruction and continue to run for
237-
AOT mode. TODO: implement unwind support for AOT
238-
code in Windows platform */
239-
exce_info->ContextRecord->Rip++;
240-
return EXCEPTION_CONTINUE_EXECUTION;
241-
}
218+
}
219+
220+
if (memory_inst && mapped_mem_start_addr <= (uint8 *)sig_addr
221+
&& (uint8 *)sig_addr < mapped_mem_end_addr) {
222+
/* The address which causes segmentation fault is inside
223+
the memory instance's guard regions.
224+
Set exception and let the wasm func continue to run, when
225+
the wasm func returns, the caller will check whether the
226+
exception is thrown and return to runtime. */
227+
wasm_set_exception(module_inst, "out of bounds memory access");
228+
if (module_inst->module_type == Wasm_Module_Bytecode) {
229+
/* Continue to search next exception handler for
230+
interpreter mode as it can be caught by
231+
`__try { .. } __except { .. }` sentences in
232+
wasm_runtime.c */
233+
return EXCEPTION_CONTINUE_SEARCH;
234+
}
235+
else {
236+
/* Skip current instruction and continue to run for
237+
AOT mode. TODO: implement unwind support for AOT
238+
code in Windows platform */
239+
exce_info->ContextRecord->Rip++;
240+
return EXCEPTION_CONTINUE_EXECUTION;
241+
}
242+
}
243+
else if (exec_env_tls->exce_check_guard_page <= (uint8 *)sig_addr
244+
&& (uint8 *)sig_addr
245+
< exec_env_tls->exce_check_guard_page + page_size) {
246+
bh_assert(wasm_get_exception(module_inst));
247+
if (module_inst->module_type == Wasm_Module_Bytecode) {
248+
return EXCEPTION_CONTINUE_SEARCH;
249+
}
250+
else {
251+
exce_info->ContextRecord->Rip++;
252+
return EXCEPTION_CONTINUE_EXECUTION;
242253
}
243254
}
244255
}

core/iwasm/compilation/aot_emit_control.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
462462
false, NULL, NULL))) {
463463
goto fail;
464464
}
465+
aot_block_destroy(block);
465466
return aot_handle_next_reachable_block(comp_ctx, func_ctx,
466467
p_frame_ip);
467468
}

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5125,10 +5125,11 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
51255125

51265126
/* Free the emit data */
51275127
wasm_runtime_free(emit_data);
5128-
51295128
return true;
51305129

51315130
fail:
5131+
/* Free the emit data */
5132+
wasm_runtime_free(emit_data);
51325133
return false;
51335134
}
51345135
#endif

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,14 +3081,15 @@ wasi_ssp_sock_addr_resolve(
30813081
size_t _max_info_size;
30823082
size_t actual_info_size;
30833083

3084-
if (!ns_lookup_list_search(ns_lookup_list, host)) {
3085-
return __WASI_EACCES;
3086-
}
3087-
30883084
if (!wamr_addr_info) {
30893085
return __WASI_ENOMEM;
30903086
}
30913087

3088+
if (!ns_lookup_list_search(ns_lookup_list, host)) {
3089+
wasm_runtime_free(wamr_addr_info);
3090+
return __WASI_EACCES;
3091+
}
3092+
30923093
int ret = os_socket_addr_resolve(
30933094
host, service, hints->hints_enabled ? &hints_is_tcp : NULL,
30943095
hints->hints_enabled ? &hints_is_ipv4 : NULL, wamr_addr_info,

samples/file/src/main.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ main(int argc, char *argv_main[])
2626
wasm_module_inst_t module_inst = NULL;
2727
wasm_exec_env_t exec_env = NULL;
2828
uint32 buf_size, stack_size = 8092, heap_size = 8092;
29-
uint32_t wasm_buffer = 0;
3029

3130
RuntimeInitArgs init_args;
3231
memset(&init_args, 0, sizeof(RuntimeInitArgs));
@@ -103,11 +102,8 @@ main(int argc, char *argv_main[])
103102
fail:
104103
if (exec_env)
105104
wasm_runtime_destroy_exec_env(exec_env);
106-
if (module_inst) {
107-
if (wasm_buffer)
108-
wasm_runtime_module_free(module_inst, wasm_buffer);
105+
if (module_inst)
109106
wasm_runtime_deinstantiate(module_inst);
110-
}
111107
if (module)
112108
wasm_runtime_unload(module);
113109
if (buffer)

0 commit comments

Comments
 (0)