Fix some issues reported by Coverity (#1150)

module_wasm_app.c: add return value check for wasm_runtime_call_wasm
aot_runtime.c: add return value check for aot_get_default_memory
aot_runtime.c: add return value check before calling wasm app malloc/free func
wasm_runtime_common.c: fix dead code warning in wasm_runtime_load_from_sections
aot_emit_memory.c: fix potential integer overflow issue
wasm_runtime.c: remove dead code in memory_instantiate, add assertion for globals
samples simple/gui/littlevgl: fix fields of struct sigaction initialization issue
host-tool: add return value check for sendto
This commit is contained in:
Wenyong Huang 2022-05-07 16:51:43 +08:00 committed by GitHub
parent d62543c99c
commit 2bac6a42a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 41 additions and 33 deletions

View File

@ -492,9 +492,16 @@ wasm_app_routine(void *arg)
fail2: fail2:
/* Call WASM app onDestroy() method if there is */ /* Call WASM app onDestroy() method if there is */
func_onDestroy = app_manager_lookup_function(inst, "_on_destroy", "()"); func_onDestroy = app_manager_lookup_function(inst, "_on_destroy", "()");
if (func_onDestroy) if (func_onDestroy) {
wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0, if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0,
NULL); NULL)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
app_manager_printf("Got exception running WASM code: %s\n",
exception);
wasm_runtime_clear_exception(inst);
}
}
fail1: fail1:

View File

@ -654,6 +654,10 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
/* Get default memory instance */ /* Get default memory instance */
memory_inst = aot_get_default_memory(module_inst); memory_inst = aot_get_default_memory(module_inst);
if (!memory_inst) {
/* Ignore setting memory init data if no memory inst is created */
return true;
}
for (i = 0; i < module->mem_init_data_count; i++) { for (i = 0; i < module->mem_init_data_count; i++) {
data_seg = module->mem_init_data_list[i]; data_seg = module->mem_init_data_list[i];
@ -1794,8 +1798,8 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
malloc_func = malloc_func =
aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig); aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig);
bh_assert(malloc_func); if (!malloc_func
if (!execute_malloc_function(module_inst, malloc_func, retain_func, || !execute_malloc_function(module_inst, malloc_func, retain_func,
size, &offset)) { size, &offset)) {
return 0; return 0;
} }
@ -1889,7 +1893,7 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
if (!free_func && module->retain_func_index != (uint32)-1) if (!free_func && module->retain_func_index != (uint32)-1)
free_func = aot_lookup_function(module_inst, "__unpin", "(i)i"); free_func = aot_lookup_function(module_inst, "__unpin", "(i)i");
bh_assert(free_func); if (free_func)
execute_free_function(module_inst, free_func, ptr); execute_free_function(module_inst, free_func, ptr);
} }
} }

View File

@ -54,7 +54,6 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
if (!(exec_env->current_status = wasm_cluster_create_exenv_status())) if (!(exec_env->current_status = wasm_cluster_create_exenv_status()))
goto fail4; goto fail4;
#endif #endif
#endif #endif
exec_env->module_inst = module_inst; exec_env->module_inst = module_inst;

View File

@ -827,26 +827,28 @@ wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
{ {
WASMModuleCommon *module_common; WASMModuleCommon *module_common;
#if WASM_ENABLE_INTERP != 0
if (!is_aot) { if (!is_aot) {
#if WASM_ENABLE_INTERP != 0
module_common = (WASMModuleCommon *)wasm_load_from_sections( module_common = (WASMModuleCommon *)wasm_load_from_sections(
section_list, error_buf, error_buf_size); section_list, error_buf, error_buf_size);
return register_module_with_null_name(module_common, error_buf, return register_module_with_null_name(module_common, error_buf,
error_buf_size); error_buf_size);
}
#endif #endif
}
else {
#if WASM_ENABLE_AOT != 0 #if WASM_ENABLE_AOT != 0
if (is_aot) {
module_common = (WASMModuleCommon *)aot_load_from_sections( module_common = (WASMModuleCommon *)aot_load_from_sections(
section_list, error_buf, error_buf_size); section_list, error_buf, error_buf_size);
return register_module_with_null_name(module_common, error_buf, return register_module_with_null_name(module_common, error_buf,
error_buf_size); error_buf_size);
}
#endif #endif
}
#if WASM_ENABLE_INTERP == 0 || WASM_ENABLE_AOT == 0
set_error_buf(error_buf, error_buf_size, set_error_buf(error_buf, error_buf_size,
"WASM module load failed: invalid section list type"); "WASM module load failed: invalid section list type");
return NULL; return NULL;
#endif
} }
void void

View File

@ -141,7 +141,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
comp_ctx->comp_data->memories[0].num_bytes_per_page; comp_ctx->comp_data->memories[0].num_bytes_per_page;
uint32 init_page_count = uint32 init_page_count =
comp_ctx->comp_data->memories[0].mem_init_page_count; comp_ctx->comp_data->memories[0].mem_init_page_count;
uint64 mem_data_size = num_bytes_per_page * init_page_count; uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
if (mem_offset + bytes <= mem_data_size) { if (mem_offset + bytes <= mem_data_size) {
/* inside memory space */ /* inside memory space */

View File

@ -222,6 +222,8 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
/* Adjust __heap_base global value */ /* Adjust __heap_base global value */
global_idx = module->aux_heap_base_global_index; global_idx = module->aux_heap_base_global_index;
bh_assert(module_inst->globals
&& global_idx < module_inst->global_count);
global_addr = module_inst->global_data global_addr = module_inst->global_data
+ module_inst->globals[global_idx].data_offset; + module_inst->globals[global_idx].data_offset;
*(uint32 *)global_addr = aux_heap_base; *(uint32 *)global_addr = aux_heap_base;
@ -403,19 +405,6 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
} }
} }
if (mem_index == 0) {
/**
* no import memory and define memory, but still need heap
* for wasm code
*/
if (!(memory = memories[mem_index++] =
memory_instantiate(module_inst, 0, 0, 0, heap_size, 0,
error_buf, error_buf_size))) {
memories_deinstantiate(module_inst, memories, memory_count);
return NULL;
}
}
bh_assert(mem_index == memory_count); bh_assert(mem_index == memory_count);
(void)module_inst; (void)module_inst;
return memories; return memories;

View File

@ -175,9 +175,11 @@ func_server_mode(void *arg)
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
int n; int n;
char buff[MAX]; char buff[MAX];
struct sigaction sa; struct sigaction sa;
sa.sa_handler = SIG_IGN; sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGPIPE, &sa, 0); sigaction(SIGPIPE, &sa, 0);
/* First call to socket() function */ /* First call to socket() function */

View File

@ -169,9 +169,11 @@ func_server_mode(void *arg)
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
int n; int n;
char buff[MAX]; char buff[MAX];
struct sigaction sa; struct sigaction sa;
sa.sa_handler = SIG_IGN; sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGPIPE, &sa, 0); sigaction(SIGPIPE, &sa, 0);
/* First call to socket() function */ /* First call to socket() function */

View File

@ -178,9 +178,11 @@ func_server_mode(void *arg)
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
int n; int n;
char buff[MAX]; char buff[MAX];
struct sigaction sa; struct sigaction sa;
sa.sa_handler = SIG_IGN; sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGPIPE, &sa, 0); sigaction(SIGPIPE, &sa, 0);
/* First call to socket() function */ /* First call to socket() function */

View File

@ -125,6 +125,7 @@ bool
udp_send(const char *address, int port, const char *buf, int len) udp_send(const char *address, int port, const char *buf, int len)
{ {
int sockfd; int sockfd;
ssize_t size_sent;
struct sockaddr_in servaddr; struct sockaddr_in servaddr;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
@ -136,11 +137,11 @@ udp_send(const char *address, int port, const char *buf, int len)
servaddr.sin_port = htons(port); servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = INADDR_ANY; servaddr.sin_addr.s_addr = INADDR_ANY;
sendto(sockfd, buf, len, MSG_CONFIRM, (const struct sockaddr *)&servaddr, size_sent = sendto(sockfd, buf, len, MSG_CONFIRM,
sizeof(servaddr)); (const struct sockaddr *)&servaddr, sizeof(servaddr));
close(sockfd); close(sockfd);
return true; return (size_sent != -1) ? true : false;
} }
bool bool