소스 검색

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
Wenyong Huang 3 년 전
부모
커밋
2bac6a42a7

+ 10 - 3
core/app-mgr/app-manager/module_wasm_app.c

@@ -492,9 +492,16 @@ wasm_app_routine(void *arg)
 fail2:
     /* Call WASM app onDestroy() method if there is */
     func_onDestroy = app_manager_lookup_function(inst, "_on_destroy", "()");
-    if (func_onDestroy)
-        wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0,
-                               NULL);
+    if (func_onDestroy) {
+        if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0,
+                                    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:
 

+ 9 - 5
core/iwasm/aot/aot_runtime.c

@@ -654,6 +654,10 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
 
     /* Get default memory instance */
     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++) {
         data_seg = module->mem_init_data_list[i];
@@ -1794,9 +1798,9 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
         malloc_func =
             aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig);
 
-        bh_assert(malloc_func);
-        if (!execute_malloc_function(module_inst, malloc_func, retain_func,
-                                     size, &offset)) {
+        if (!malloc_func
+            || !execute_malloc_function(module_inst, malloc_func, retain_func,
+                                        size, &offset)) {
             return 0;
         }
         addr = offset ? (uint8 *)memory_inst->memory_data.ptr + offset : NULL;
@@ -1889,8 +1893,8 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
             if (!free_func && module->retain_func_index != (uint32)-1)
                 free_func = aot_lookup_function(module_inst, "__unpin", "(i)i");
 
-            bh_assert(free_func);
-            execute_free_function(module_inst, free_func, ptr);
+            if (free_func)
+                execute_free_function(module_inst, free_func, ptr);
         }
     }
 }

+ 0 - 1
core/iwasm/common/wasm_exec_env.c

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

+ 6 - 4
core/iwasm/common/wasm_runtime_common.c

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

+ 1 - 1
core/iwasm/compilation/aot_emit_memory.c

@@ -141,7 +141,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
             comp_ctx->comp_data->memories[0].num_bytes_per_page;
         uint32 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) {
             /* inside memory space */

+ 2 - 13
core/iwasm/interpreter/wasm_runtime.c

@@ -222,6 +222,8 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page,
 
             /* Adjust __heap_base global value */
             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
                           + module_inst->globals[global_idx].data_offset;
             *(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);
     (void)module_inst;
     return memories;

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

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

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

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

+ 3 - 1
samples/simple/src/iwasm_main.c

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

+ 4 - 3
test-tools/host-tool/src/transport.c

@@ -125,6 +125,7 @@ bool
 udp_send(const char *address, int port, const char *buf, int len)
 {
     int sockfd;
+    ssize_t size_sent;
     struct sockaddr_in servaddr;
 
     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_addr.s_addr = INADDR_ANY;
 
-    sendto(sockfd, buf, len, MSG_CONFIRM, (const struct sockaddr *)&servaddr,
-           sizeof(servaddr));
+    size_sent = sendto(sockfd, buf, len, MSG_CONFIRM,
+                       (const struct sockaddr *)&servaddr, sizeof(servaddr));
 
     close(sockfd);
-    return true;
+    return (size_sent != -1) ? true : false;
 }
 
 bool