Skip to content

Commit 2bac6a4

Browse files
authored
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
1 parent d62543c commit 2bac6a4

10 files changed

Lines changed: 41 additions & 33 deletions

File tree

core/app-mgr/app-manager/module_wasm_app.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,16 @@ wasm_app_routine(void *arg)
492492
fail2:
493493
/* Call WASM app onDestroy() method if there is */
494494
func_onDestroy = app_manager_lookup_function(inst, "_on_destroy", "()");
495-
if (func_onDestroy)
496-
wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0,
497-
NULL);
495+
if (func_onDestroy) {
496+
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0,
497+
NULL)) {
498+
const char *exception = wasm_runtime_get_exception(inst);
499+
bh_assert(exception);
500+
app_manager_printf("Got exception running WASM code: %s\n",
501+
exception);
502+
wasm_runtime_clear_exception(inst);
503+
}
504+
}
498505

499506
fail1:
500507

core/iwasm/aot/aot_runtime.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
654654

655655
/* Get default memory instance */
656656
memory_inst = aot_get_default_memory(module_inst);
657+
if (!memory_inst) {
658+
/* Ignore setting memory init data if no memory inst is created */
659+
return true;
660+
}
657661

658662
for (i = 0; i < module->mem_init_data_count; i++) {
659663
data_seg = module->mem_init_data_list[i];
@@ -1794,9 +1798,9 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
17941798
malloc_func =
17951799
aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig);
17961800

1797-
bh_assert(malloc_func);
1798-
if (!execute_malloc_function(module_inst, malloc_func, retain_func,
1799-
size, &offset)) {
1801+
if (!malloc_func
1802+
|| !execute_malloc_function(module_inst, malloc_func, retain_func,
1803+
size, &offset)) {
18001804
return 0;
18011805
}
18021806
addr = offset ? (uint8 *)memory_inst->memory_data.ptr + offset : NULL;
@@ -1889,8 +1893,8 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
18891893
if (!free_func && module->retain_func_index != (uint32)-1)
18901894
free_func = aot_lookup_function(module_inst, "__unpin", "(i)i");
18911895

1892-
bh_assert(free_func);
1893-
execute_free_function(module_inst, free_func, ptr);
1896+
if (free_func)
1897+
execute_free_function(module_inst, free_func, ptr);
18941898
}
18951899
}
18961900
}

core/iwasm/common/wasm_exec_env.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
5454
if (!(exec_env->current_status = wasm_cluster_create_exenv_status()))
5555
goto fail4;
5656
#endif
57-
5857
#endif
5958

6059
exec_env->module_inst = module_inst;

core/iwasm/common/wasm_runtime_common.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -827,26 +827,28 @@ wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
827827
{
828828
WASMModuleCommon *module_common;
829829

830-
#if WASM_ENABLE_INTERP != 0
831830
if (!is_aot) {
831+
#if WASM_ENABLE_INTERP != 0
832832
module_common = (WASMModuleCommon *)wasm_load_from_sections(
833833
section_list, error_buf, error_buf_size);
834834
return register_module_with_null_name(module_common, error_buf,
835835
error_buf_size);
836-
}
837836
#endif
837+
}
838+
else {
838839
#if WASM_ENABLE_AOT != 0
839-
if (is_aot) {
840840
module_common = (WASMModuleCommon *)aot_load_from_sections(
841841
section_list, error_buf, error_buf_size);
842842
return register_module_with_null_name(module_common, error_buf,
843843
error_buf_size);
844-
}
845844
#endif
845+
}
846846

847+
#if WASM_ENABLE_INTERP == 0 || WASM_ENABLE_AOT == 0
847848
set_error_buf(error_buf, error_buf_size,
848849
"WASM module load failed: invalid section list type");
849850
return NULL;
851+
#endif
850852
}
851853

852854
void

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
141141
comp_ctx->comp_data->memories[0].num_bytes_per_page;
142142
uint32 init_page_count =
143143
comp_ctx->comp_data->memories[0].mem_init_page_count;
144-
uint64 mem_data_size = num_bytes_per_page * init_page_count;
144+
uint64 mem_data_size = (uint64)num_bytes_per_page * init_page_count;
145145

146146
if (mem_offset + bytes <= mem_data_size) {
147147
/* inside memory space */

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
222222

223223
/* Adjust __heap_base global value */
224224
global_idx = module->aux_heap_base_global_index;
225+
bh_assert(module_inst->globals
226+
&& global_idx < module_inst->global_count);
225227
global_addr = module_inst->global_data
226228
+ module_inst->globals[global_idx].data_offset;
227229
*(uint32 *)global_addr = aux_heap_base;
@@ -403,19 +405,6 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
403405
}
404406
}
405407

406-
if (mem_index == 0) {
407-
/**
408-
* no import memory and define memory, but still need heap
409-
* for wasm code
410-
*/
411-
if (!(memory = memories[mem_index++] =
412-
memory_instantiate(module_inst, 0, 0, 0, heap_size, 0,
413-
error_buf, error_buf_size))) {
414-
memories_deinstantiate(module_inst, memories, memory_count);
415-
return NULL;
416-
}
417-
}
418-
419408
bh_assert(mem_index == memory_count);
420409
(void)module_inst;
421410
return memories;

samples/gui/wasm-runtime-wgl/src/platform/linux/iwasm_main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ func_server_mode(void *arg)
175175
struct sockaddr_in serv_addr, cli_addr;
176176
int n;
177177
char buff[MAX];
178-
179178
struct sigaction sa;
179+
180180
sa.sa_handler = SIG_IGN;
181+
sa.sa_flags = 0;
182+
sigemptyset(&sa.sa_mask);
181183
sigaction(SIGPIPE, &sa, 0);
182184

183185
/* First call to socket() function */

samples/littlevgl/vgl-wasm-runtime/src/platform/linux/iwasm_main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ func_server_mode(void *arg)
169169
struct sockaddr_in serv_addr, cli_addr;
170170
int n;
171171
char buff[MAX];
172-
173172
struct sigaction sa;
173+
174174
sa.sa_handler = SIG_IGN;
175+
sa.sa_flags = 0;
176+
sigemptyset(&sa.sa_mask);
175177
sigaction(SIGPIPE, &sa, 0);
176178

177179
/* First call to socket() function */

samples/simple/src/iwasm_main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,11 @@ func_server_mode(void *arg)
178178
struct sockaddr_in serv_addr, cli_addr;
179179
int n;
180180
char buff[MAX];
181-
182181
struct sigaction sa;
182+
183183
sa.sa_handler = SIG_IGN;
184+
sa.sa_flags = 0;
185+
sigemptyset(&sa.sa_mask);
184186
sigaction(SIGPIPE, &sa, 0);
185187

186188
/* First call to socket() function */

test-tools/host-tool/src/transport.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ bool
125125
udp_send(const char *address, int port, const char *buf, int len)
126126
{
127127
int sockfd;
128+
ssize_t size_sent;
128129
struct sockaddr_in servaddr;
129130

130131
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)
136137
servaddr.sin_port = htons(port);
137138
servaddr.sin_addr.s_addr = INADDR_ANY;
138139

139-
sendto(sockfd, buf, len, MSG_CONFIRM, (const struct sockaddr *)&servaddr,
140-
sizeof(servaddr));
140+
size_sent = sendto(sockfd, buf, len, MSG_CONFIRM,
141+
(const struct sockaddr *)&servaddr, sizeof(servaddr));
141142

142143
close(sockfd);
143-
return true;
144+
return (size_sent != -1) ? true : false;
144145
}
145146

146147
bool

0 commit comments

Comments
 (0)