Parcourir la source

Add more checks to enhance security (#446)

add more checks to enhance security
clear "wasi proc exit" exception before return to caller in wasm/aot call functions
fix memory profiling issue
change movdqa to movdqu in simd invokeNative asm codes to fix issue of unaligned address access
move setjmp/longjmp from libc-builtin to libc-emcc
fix zephyr platform compilation issue in latest zephyr version
Wenyong Huang il y a 5 ans
Parent
commit
74be7a0b7c

+ 105 - 30
core/iwasm/aot/aot_runtime.c

@@ -643,6 +643,24 @@ create_exports(AOTModuleInstance *module_inst, AOTModule *module,
                                error_buf, error_buf_size);
 }
 
+static bool
+clear_wasi_proc_exit_exception(AOTModuleInstance *module_inst)
+{
+#if WASM_ENABLE_LIBC_WASI != 0
+    const char *exception = aot_get_exception(module_inst);
+    if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
+        /* The "wasi proc exit" exception is thrown by native lib to
+           let wasm app exit, which is a normal behavior, we clear
+           the exception here. */
+        aot_set_exception(module_inst, NULL);
+        return true;
+    }
+    return false;
+#else
+    return false;
+#endif
+}
+
 static bool
 execute_post_inst_function(AOTModuleInstance *module_inst)
 {
@@ -677,6 +695,7 @@ execute_start_function(AOTModuleInstance *module_inst)
     u.f(exec_env);
 
     wasm_exec_env_destroy(exec_env);
+    (void)clear_wasi_proc_exit_exception(module_inst);
     return !aot_get_exception(module_inst);
 }
 
@@ -974,6 +993,7 @@ touch_pages(uint8 *stack_min_addr, uint32 page_size)
             sum += *(stack_min_addr + page_size - 1);
             break;
         }
+        *touch_addr = 0;
         sum += *touch_addr;
     }
     return sum;
@@ -1011,7 +1031,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
     if (!exec_env->jmpbuf_stack_top) {
         /* Touch each stack page to ensure that it has been mapped: the OS may
            lazily grow the stack mapping as a guard page is hit. */
-        touch_pages(stack_min_addr, page_size);
+        (void)touch_pages(stack_min_addr, page_size);
         /* First time to call aot function, protect one page */
         if (os_mprotect(stack_min_addr, page_size * guard_page_count,
                         MMAP_PROT_NONE) != 0) {
@@ -1106,6 +1126,8 @@ aot_call_function(WASMExecEnv *exec_env,
         if (!ret || aot_get_exception(module_inst)) {
             if (argv1 != argv1_buf)
                 wasm_runtime_free(argv1);
+            if (clear_wasi_proc_exit_exception(module_inst))
+                return true;
             return false;
         }
 
@@ -1134,6 +1156,8 @@ aot_call_function(WASMExecEnv *exec_env,
     else {
         ret = invoke_native_internal(exec_env, function->u.func.func_ptr,
                                      func_type, NULL, NULL, argv, argc, argv);
+        if (clear_wasi_proc_exit_exception(module_inst))
+            return true;
         return ret && !aot_get_exception(module_inst) ? true : false;
     }
 }
@@ -1377,10 +1401,15 @@ aot_validate_app_addr(AOTModuleInstance *module_inst,
 {
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
 
+    if (!memory_inst) {
+        goto fail;
+    }
+
     /* integer overflow check */
     if(app_offset + size < app_offset) {
         goto fail;
     }
+
     if (app_offset + size <= memory_inst->memory_data_size) {
         return true;
     }
@@ -1393,8 +1422,12 @@ bool
 aot_validate_native_addr(AOTModuleInstance *module_inst,
                          void *native_ptr, uint32 size)
 {
-    uint8 *addr = (uint8 *)native_ptr;
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
+    uint8 *addr = (uint8 *)native_ptr;
+
+    if (!memory_inst) {
+        goto fail;
+    }
 
     /* integer overflow check */
     if (addr + size < addr) {
@@ -1413,7 +1446,13 @@ void *
 aot_addr_app_to_native(AOTModuleInstance *module_inst, uint32 app_offset)
 {
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
-    uint8 *addr = (uint8 *)memory_inst->memory_data.ptr + app_offset;
+    uint8 *addr;
+
+    if (!memory_inst) {
+        return NULL;
+    }
+
+    addr = (uint8 *)memory_inst->memory_data.ptr + app_offset;
 
     if ((uint8 *)memory_inst->memory_data.ptr <= addr
         && addr < (uint8 *)memory_inst->memory_data_end.ptr)
@@ -1424,8 +1463,12 @@ aot_addr_app_to_native(AOTModuleInstance *module_inst, uint32 app_offset)
 uint32
 aot_addr_native_to_app(AOTModuleInstance *module_inst, void *native_ptr)
 {
-    uint8 *addr = (uint8 *)native_ptr;
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
+    uint8 *addr = (uint8 *)native_ptr;
+
+    if (!memory_inst) {
+        return 0;
+    }
 
     if ((uint8 *)memory_inst->memory_data.ptr <= addr
         && addr < (uint8 *)memory_inst->memory_data_end.ptr)
@@ -1440,7 +1483,13 @@ aot_get_app_addr_range(AOTModuleInstance *module_inst,
                        uint32 *p_app_end_offset)
 {
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
-    uint32 memory_data_size = memory_inst->memory_data_size;
+    uint32 memory_data_size;
+
+    if (!memory_inst) {
+        return false;
+    }
+
+    memory_data_size = memory_inst->memory_data_size;
 
     if (app_offset < memory_data_size) {
         if (p_app_start_offset)
@@ -1458,8 +1507,12 @@ aot_get_native_addr_range(AOTModuleInstance *module_inst,
                           uint8 **p_native_start_addr,
                           uint8 **p_native_end_addr)
 {
-    uint8 *addr = (uint8 *)native_ptr;
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
+    uint8 *addr = (uint8 *)native_ptr;
+
+    if (!memory_inst) {
+        return false;
+    }
 
     if ((uint8 *)memory_inst->memory_data.ptr <= addr
         && addr < (uint8 *)memory_inst->memory_data_end.ptr) {
@@ -1477,17 +1530,24 @@ bool
 aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
 {
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
-    uint32 num_bytes_per_page = memory_inst->num_bytes_per_page;
-    uint32 cur_page_count = memory_inst->cur_page_count;
-    uint32 max_page_count = memory_inst->max_page_count;
-    uint32 total_page_count = cur_page_count + inc_page_count;
-    uint32 total_size_old = memory_inst->memory_data_size;
-    uint64 total_size = (uint64)num_bytes_per_page * total_page_count;
-    uint32 heap_size = (uint32)((uint8 *)memory_inst->heap_data_end.ptr
-                                - (uint8 *)memory_inst->heap_data.ptr);
-    uint8 *memory_data_old = (uint8 *)memory_inst->memory_data.ptr;
-    uint8 *heap_data_old = (uint8 *)memory_inst->heap_data.ptr;
-    uint8 *memory_data, *heap_data;
+    uint32 num_bytes_per_page, cur_page_count, max_page_count;
+    uint32 total_page_count, total_size_old, heap_size;
+    uint64 total_size;
+    uint8 *memory_data_old, *heap_data_old, *memory_data, *heap_data;
+
+    if (!memory_inst)
+        return false;
+
+    num_bytes_per_page = memory_inst->num_bytes_per_page;
+    cur_page_count = memory_inst->cur_page_count;
+    max_page_count = memory_inst->max_page_count;
+    total_page_count = cur_page_count + inc_page_count;
+    total_size_old = memory_inst->memory_data_size;
+    total_size = (uint64)num_bytes_per_page * total_page_count;
+    heap_size = (uint32)((uint8 *)memory_inst->heap_data_end.ptr
+                         - (uint8 *)memory_inst->heap_data.ptr);
+    memory_data_old = (uint8 *)memory_inst->memory_data.ptr;
+    heap_data_old = (uint8 *)memory_inst->heap_data.ptr;
 
     if (inc_page_count <= 0)
         /* No need to enlarge memory */
@@ -1563,11 +1623,18 @@ bool
 aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
 {
     AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
-    uint32 num_bytes_per_page = memory_inst->num_bytes_per_page;
-    uint32 cur_page_count = memory_inst->cur_page_count;
-    uint32 max_page_count = memory_inst->max_page_count;
-    uint32 total_page_count = cur_page_count + inc_page_count;
-    uint64 total_size = (uint64)num_bytes_per_page * total_page_count;
+    uint32 num_bytes_per_page, cur_page_count, max_page_count;
+    uint32 total_page_count;
+    uint64 total_size;
+
+    if (!memory_inst)
+        return false;
+
+    num_bytes_per_page = memory_inst->num_bytes_per_page;
+    cur_page_count = memory_inst->cur_page_count;
+    max_page_count = memory_inst->max_page_count;
+    total_page_count = cur_page_count + inc_page_count;
+    total_size = (uint64)num_bytes_per_page * total_page_count;
 
     if (inc_page_count <= 0)
         /* No need to enlarge memory */
@@ -1802,6 +1869,8 @@ aot_call_indirect(WASMExecEnv *exec_env,
         if (!ret || aot_get_exception(module_inst)) {
             if (argv1 != argv1_buf)
                 wasm_runtime_free(argv1);
+            if (clear_wasi_proc_exit_exception(module_inst))
+                return true;
             return false;
         }
 
@@ -1829,9 +1898,12 @@ aot_call_indirect(WASMExecEnv *exec_env,
         return true;
     }
     else {
-        return invoke_native_internal(exec_env, func_ptr,
-                                      func_type, signature, attachment,
-                                      argv, argc, argv);
+        ret = invoke_native_internal(exec_env, func_ptr,
+                                     func_type, signature, attachment,
+                                     argv, argc, argv);
+        if (clear_wasi_proc_exit_exception(module_inst))
+            return true;
+        return ret;
     }
 }
 
@@ -2013,15 +2085,15 @@ aot_get_module_mem_consumption(const AOTModule *module,
         mem_conspn->data_segs_size += sizeof(AOTMemInitData);
     }
 
-    mem_conspn->const_strs_size =
-        bh_hash_map_get_struct_size(module->const_str_set);
-
-    const_string_size = 0;
     if (module->const_str_set) {
+        mem_conspn->const_strs_size =
+            bh_hash_map_get_struct_size(module->const_str_set);
+
+        const_string_size = 0;
         bh_hash_map_traverse(module->const_str_set,
                              const_string_node_size_cb);
+        mem_conspn->const_strs_size += const_string_size;
     }
-    mem_conspn->const_strs_size += const_string_size;
 
     /* code size + literal size + object data section size */
     mem_conspn->aot_code_size = module->code_size + module->literal_size
@@ -2065,6 +2137,9 @@ aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
             mem_inst->num_bytes_per_page * mem_inst->cur_page_count;
         mem_conspn->app_heap_size =
             mem_inst->heap_data_end.ptr - mem_inst->heap_data.ptr;
+        /* size of app heap structure */
+        mem_conspn->memories_size +=
+            mem_allocator_get_heap_struct_size();
     }
 
     mem_conspn->tables_size = sizeof(uint32) * module_inst->table_size;

+ 8 - 8
core/iwasm/common/arch/invokeNative_em64_simd.s

@@ -41,14 +41,14 @@ push_args:
     loop push_args
 push_args_end:
     /* fill all fp args */
-    movdqa 0x00(%rsi), %xmm0
-    movdqa 0x10(%rsi), %xmm1
-    movdqa 0x20(%rsi), %xmm2
-    movdqa 0x30(%rsi), %xmm3
-    movdqa 0x40(%rsi), %xmm4
-    movdqa 0x50(%rsi), %xmm5
-    movdqa 0x60(%rsi), %xmm6
-    movdqa 0x70(%rsi), %xmm7
+    movdqu 0x00(%rsi), %xmm0
+    movdqu 0x10(%rsi), %xmm1
+    movdqu 0x20(%rsi), %xmm2
+    movdqu 0x30(%rsi), %xmm3
+    movdqu 0x40(%rsi), %xmm4
+    movdqu 0x50(%rsi), %xmm5
+    movdqu 0x60(%rsi), %xmm6
+    movdqu 0x70(%rsi), %xmm7
 
     /* fill all int args */
     movq 0x80(%rsi), %rdi

+ 14 - 7
core/iwasm/common/wasm_c_api.c

@@ -579,8 +579,10 @@ wasm_functype_new(wasm_valtype_vec_t *params, wasm_valtype_vec_t *results)
 
 failed:
     LOG_DEBUG("%s failed", __FUNCTION__);
-    FREEIF(func_type->params);
-    FREEIF(func_type->results);
+    if (func_type)
+        FREEIF(func_type->params);
+    if (func_type)
+        FREEIF(func_type->results);
     FREEIF(func_type);
     return NULL;
 }
@@ -1151,10 +1153,15 @@ native_func_trampoline(wasm_exec_env_t exec_env, uint64 *argv)
     }
 
     if (trap) {
-        wasm_name_t *message = NULL;
-        wasm_trap_message(trap, message);
-        LOG_WARNING("got a trap %s", message->data);
-        wasm_name_delete(message);
+        wasm_name_t *message = malloc_internal(sizeof(wasm_name_t));
+        if (message) {
+            wasm_trap_message(trap, message);
+            if (message->data) {
+                LOG_WARNING("got a trap %s", message->data);
+                wasm_name_delete(message);
+            }
+            FREEIF(message);
+        }
     }
 
     /* there is no result or there is an exception */
@@ -2188,7 +2195,7 @@ interp_process_export(wasm_store_t *store,
     uint32 export_cnt = 0;
     uint32 i = 0;
 
-    bh_assert(store && inst_interp && externals);
+    bh_assert(store && inst_interp && inst_interp->module && externals);
 
     exports = inst_interp->module->exports;
     export_cnt = inst_interp->module->export_count;

+ 11 - 1
core/iwasm/common/wasm_runtime_common.c

@@ -2094,6 +2094,11 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
         func_type = ((AOTFunctionInstance*)func)->u.func.func_type;
 #endif
 
+    if (!func_type) {
+        LOG_ERROR("invalid module instance type");
+        return false;
+    }
+
     if (!check_main_func_type(func_type)) {
         wasm_runtime_set_exception(module_inst,
                                    "invalid function type of main function");
@@ -2318,7 +2323,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
 {
     WASMFunctionInstanceCommon *func;
     WASMType *type = NULL;
-    uint32 argc1, *argv1 = NULL, cell_num, j, k = 0;
+    uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
     int32 i, p;
     uint64 total_size;
     const char *exception;
@@ -2362,6 +2367,11 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
     }
 #endif
 
+    if (!type) {
+        LOG_ERROR("invalid module instance type");
+        return false;
+    }
+
     if (type->param_count != (uint32)argc) {
         wasm_runtime_set_exception(module_inst,
                                    "invalid input argument count");

+ 3 - 1
core/iwasm/common/wasm_shared_memory.c

@@ -200,8 +200,10 @@ notify_wait_list(bh_list *wait_list, uint32 count)
         notify_count = wait_list->len;
 
     node = bh_list_first_elem(wait_list);
+    if (!node)
+        return 0;
 
-    for (i = 0; i < count; i++) {
+    for (i = 0; i < notify_count; i++) {
         bh_assert(node);
         next = bh_list_elem_next(node);
 

+ 1 - 1
core/iwasm/compilation/aot_compiler.h

@@ -180,11 +180,11 @@ typedef enum FloatArithmetic {
       goto fail;                                            \
     }                                                       \
     aot_value = wasm_runtime_malloc(sizeof(AOTValue));      \
-    memset(aot_value, 0, sizeof(AOTValue));                 \
     if (!aot_value) {                                       \
       aot_set_last_error("allocate memory failed.");        \
       goto fail;                                            \
     }                                                       \
+    memset(aot_value, 0, sizeof(AOTValue));                 \
     aot_value->type = value_type;                           \
     aot_value->value = llvm_value;                          \
     aot_value_stack_push                                    \

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

@@ -878,7 +878,7 @@ static union {
     uint64 *t = (uint64*)v.i64x2;           \
     CHECK_BUF(16);                          \
     if (!is_little_endian())                \
-        exchange_uint128((uint8 *)&t);      \
+        exchange_uint128((uint8 *)t);       \
     PUT_U64_TO_ADDR(buf + offset, t[0]);    \
     offset += (uint32)sizeof(uint64);       \
     PUT_U64_TO_ADDR(buf + offset, t[1]);    \

+ 3 - 1
core/iwasm/compilation/aot_emit_control.c

@@ -155,12 +155,14 @@ handle_next_reachable_block(AOTCompContext *comp_ctx,
 {
     AOTBlock *block = func_ctx->block_stack.block_list_end;
     AOTBlock *block_prev;
-    uint8 *frame_ip;
+    uint8 *frame_ip = NULL;
     uint32 i;
     AOTFuncType *func_type;
 
     aot_checked_addr_list_destroy(func_ctx);
 
+    bh_assert(block);
+
     if (block->label_type == LABEL_TYPE_IF
         && block->llvm_else_block
         && !block->skip_wasm_code_else

+ 40 - 40
core/iwasm/compilation/aot_emit_function.c

@@ -358,6 +358,46 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
     for (i = param_count - 1; i >= 0; i--)
         POP(param_values[i + j], func_type->types[i]);
 
+    /* Set parameters for multiple return values, the first return value
+       is returned by function return value, and the other return values
+       are returned by function parameters with pointer types */
+    if (ext_ret_count > 0) {
+        ext_ret_types = func_type->types + param_count + 1;
+        ext_ret_cell_num = wasm_get_cell_num(ext_ret_types, ext_ret_count);
+        if (ext_ret_cell_num > 64) {
+            aot_set_last_error("prepare extra results's return "
+                               "address arguments failed: "
+                               "maximum 64 parameter cell number supported.");
+            goto fail;
+        }
+
+        for (i = 0; i < ext_ret_count; i++) {
+            if (!(ext_ret_idx = I32_CONST(cell_num))
+                || !(ext_ret_ptr_type =
+                        LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
+                aot_set_last_error("llvm add const or pointer type failed.");
+                goto fail;
+            }
+
+            snprintf(buf, sizeof(buf), "ext_ret%d_ptr", i);
+            if (!(ext_ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
+                                                     func_ctx->argv_buf,
+                                                     &ext_ret_idx, 1, buf))) {
+                aot_set_last_error("llvm build GEP failed.");
+                goto fail;
+            }
+            snprintf(buf, sizeof(buf), "ext_ret%d_ptr_cast", i);
+            if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder,
+                                                 ext_ret_ptr, ext_ret_ptr_type,
+                                                 buf))) {
+                aot_set_last_error("llvm build bit cast failed.");
+                goto fail;
+            }
+            param_values[param_count + 1 + i] = ext_ret_ptr;
+            cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
+        }
+    }
+
     if (func_idx < import_func_count) {
         if (!(import_func_idx = I32_CONST(func_idx))) {
             aot_set_last_error("llvm build inbounds gep failed.");
@@ -407,46 +447,6 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
             && !check_stack_boundary(comp_ctx, func_ctx, callee_cell_num))
             goto fail;
 
-        /* Prepare parameters for extra results */
-        if (ext_ret_count > 0) {
-            ext_ret_types = func_type->types + param_count + 1;
-            ext_ret_cell_num =
-                wasm_get_cell_num(ext_ret_types, ext_ret_count);
-            if (ext_ret_cell_num > 64) {
-                aot_set_last_error("prepare extra results's return "
-                                   "address arguments failed: "
-                                   "maximum 64 parameter cell number supported.");
-                goto fail;
-            }
-
-            for (i = 0; i < ext_ret_count; i++) {
-                if (!(ext_ret_idx = I32_CONST(cell_num))
-                    || !(ext_ret_ptr_type =
-                             LLVMPointerType(TO_LLVM_TYPE(ext_ret_types[i]), 0))) {
-                    aot_set_last_error("llvm add const or pointer type failed.");
-                    goto fail;
-                }
-
-                snprintf(buf, sizeof(buf), "func%d_ext_ret%d_ptr", func_idx, i);
-                if (!(ext_ret_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
-                                                         func_ctx->argv_buf,
-                                                         &ext_ret_idx, 1, buf))) {
-                    aot_set_last_error("llvm build GEP failed.");
-                    goto fail;
-                }
-                snprintf(buf, sizeof(buf), "func%d_ext_ret%d_ptr_cast", func_idx, i);
-                if (!(ext_ret_ptr = LLVMBuildBitCast(comp_ctx->builder,
-                                                        ext_ret_ptr,
-                                                        ext_ret_ptr_type,
-                                                        buf))) {
-                    aot_set_last_error("llvm build bit cast failed.");
-                    goto fail;
-                }
-                param_values[1 + param_count + i] = ext_ret_ptr;
-                cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
-            }
-        }
-
         /* Call the function */
         if (!(value_ret = LLVMBuildCall(comp_ctx->builder, func,
                                         param_values,

+ 1 - 1
core/iwasm/interpreter/wasm_loader.c

@@ -2999,7 +2999,7 @@ create_sections(const uint8 *buf, uint32 size,
             section->section_body = (uint8*)p;
             section->section_body_size = section_size;
 
-            if (!*p_section_list)
+            if (!section_list_end)
                 *p_section_list = section_list_end = section;
             else {
                 section_list_end->next = section;

+ 145 - 88
core/iwasm/interpreter/wasm_runtime.c

@@ -343,26 +343,22 @@ memories_instantiate(const WASMModule *module,
         uint32 actual_heap_size = heap_size;
 
 #if WASM_ENABLE_MULTI_MODULE != 0
-        WASMMemoryInstance *memory_inst_linked = NULL;
         if (import->u.memory.import_module != NULL) {
             WASMModuleInstance *module_inst_linked;
 
-            LOG_DEBUG("(%s, %s) is a memory of a sub-module",
-                      import->u.memory.module_name,
-                      import->u.memory.field_name);
-
-            module_inst_linked =
-                get_sub_module_inst(module_inst,
-                                    import->u.memory.import_module);
-            bh_assert(module_inst_linked);
-
-            memory_inst_linked =
-              wasm_lookup_memory(module_inst_linked,
-                                 import->u.memory.field_name);
-            bh_assert(memory_inst_linked);
+            if (!(module_inst_linked = get_sub_module_inst(
+                    module_inst, import->u.memory.import_module))) {
+                set_error_buf(error_buf, error_buf_size, "unknown memory");
+                memories_deinstantiate(module_inst, memories, memory_count);
+                return NULL;
+            }
 
-            memories[mem_index++] = memory_inst_linked;
-            memory = memory_inst_linked;
+            if (!(memory = memories[mem_index++] = wasm_lookup_memory(
+                    module_inst_linked, import->u.memory.field_name))) {
+                set_error_buf(error_buf, error_buf_size, "unknown memory");
+                memories_deinstantiate(module_inst, memories, memory_count);
+                return NULL;
+            }
         }
         else
 #endif
@@ -374,6 +370,13 @@ memories_instantiate(const WASMModule *module,
                 memories_deinstantiate(module_inst, memories, memory_count);
                 return NULL;
             }
+#if WASM_ENABLE_MULTI_MODULE != 0
+            /* The module of the import memory is a builtin module, and
+               the memory is created by current module, set its owner
+               to current module, so the memory can be destroyed in
+               memories_deinstantiate. */
+            memory->owner = module_inst;
+#endif
         }
     }
 
@@ -453,17 +456,19 @@ tables_instantiate(const WASMModule *module,
         WASMTableInstance *table_inst_linked = NULL;
         WASMModuleInstance *module_inst_linked = NULL;
         if (import->u.table.import_module) {
-            LOG_DEBUG("(%s, %s) is a table of a sub-module",
-                      import->u.table.module_name,
-                      import->u.memory.field_name);
-
-            module_inst_linked =
-              get_sub_module_inst(module_inst, import->u.table.import_module);
-            bh_assert(module_inst_linked);
+            if (!(module_inst_linked =
+              get_sub_module_inst(module_inst, import->u.table.import_module))) {
+                set_error_buf(error_buf, error_buf_size, "unknown table");
+                tables_deinstantiate(tables, table_count);
+                return NULL;
+            }
 
-            table_inst_linked = wasm_lookup_table(module_inst_linked,
-                                                  import->u.table.field_name);
-            bh_assert(table_inst_linked);
+            if (!(table_inst_linked = wasm_lookup_table(module_inst_linked,
+                                                  import->u.table.field_name))) {
+                set_error_buf(error_buf, error_buf_size, "unknown table");
+                tables_deinstantiate(tables, table_count);
+                return NULL;
+            }
 
             total_size = offsetof(WASMTableInstance, base_addr);
         }
@@ -692,16 +697,17 @@ globals_instantiate(const WASMModule *module,
         global->is_mutable = global_import->is_mutable;
 #if WASM_ENABLE_MULTI_MODULE != 0
         if (global_import->import_module) {
-            WASMModuleInstance *sub_module_inst = get_sub_module_inst(
-              module_inst, global_import->import_module);
-            bh_assert(sub_module_inst);
-
-            WASMGlobalInstance *global_inst_linked =
-              wasm_lookup_global(sub_module_inst, global_import->field_name);
-            bh_assert(global_inst_linked);
+            if (!(global->import_module_inst = get_sub_module_inst(
+                    module_inst, global_import->import_module))) {
+                set_error_buf(error_buf, error_buf_size, "unknown global");
+                return NULL;
+            }
 
-            global->import_global_inst = global_inst_linked;
-            global->import_module_inst = sub_module_inst;
+            if (!(global->import_global_inst = wasm_lookup_global(
+                    global->import_module_inst, global_import->field_name))) {
+                set_error_buf(error_buf, error_buf_size, "unknown global");
+                return NULL;
+            }
 
             /**
              * although, actually don't need initial_value for an imported
@@ -709,15 +715,11 @@ globals_instantiate(const WASMModule *module,
              * global-data and
              * (global $g2 i32 (global.get $g1))
              */
-            WASMGlobal *linked_global = global_import->import_global_linked;
-            InitializerExpression *linked_init_expr =
-              &(linked_global->init_expr);
-
-            bool ret = parse_init_expr(
-              linked_init_expr,
-              sub_module_inst->globals,
-              sub_module_inst->global_count, &(global->initial_value));
-            if (!ret) {
+            if (!parse_init_expr(
+                  &(global_import->import_global_linked->init_expr),
+                  global->import_module_inst->globals,
+                  global->import_module_inst->global_count,
+                  &(global->initial_value))) {
                 set_error_buf(error_buf, error_buf_size, "unknown global");
                 return NULL;
             }
@@ -1047,6 +1049,7 @@ sub_module_deinstantiate(WASMModuleInstance *module_inst)
         WASMSubModInstNode *next_node = bh_list_elem_next(node);
         bh_list_remove(list, node);
         wasm_deinstantiate(node->module_inst, false);
+        wasm_runtime_free(node);
         node = next_node;
     }
 }
@@ -1143,8 +1146,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
                                  error_buf, error_buf_size);
     if (!ret) {
         LOG_DEBUG("build a sub module list failed");
-        wasm_deinstantiate(module_inst, false);
-        return NULL;
+        goto fail;
     }
 #endif
 
@@ -1154,8 +1156,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
         && !(globals = globals_instantiate(module, module_inst,
                                            &global_data_size,
                                            error_buf, error_buf_size))) {
-        wasm_deinstantiate(module_inst, false);
-        return NULL;
+        goto fail;
     }
     module_inst->global_count = global_count;
     module_inst->globals = globals;
@@ -1178,8 +1179,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
     if (global_count > 0) {
         if (!(module_inst->global_data = runtime_malloc
                     (global_data_size, error_buf, error_buf_size))) {
-            wasm_deinstantiate(module_inst, false);
-            return NULL;
+            goto fail;
         }
     }
 
@@ -1210,8 +1210,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
                    error_buf, error_buf_size)))
 #endif
     ) {
-        wasm_deinstantiate(module_inst, false);
-        return NULL;
+        goto fail;
     }
 
     if (global_count > 0) {
@@ -1221,8 +1220,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
          */
         if (!globals_instantiate_fix(globals, module,
                                      error_buf, error_buf_size)) {
-            wasm_deinstantiate(module_inst, false);
-            return NULL;
+            goto fail;
         }
 
         /* Initialize the global data */
@@ -1250,8 +1248,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
     }
 
     if (!check_linked_symbol(module_inst, error_buf, error_buf_size)) {
-        wasm_deinstantiate(module_inst, false);
-        return NULL;
+        goto fail;
     }
 
     /* Initialize the memory data with data segment section */
@@ -1285,9 +1282,14 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
 
         if (data_seg->base_offset.init_expr_type
             == INIT_EXPR_TYPE_GET_GLOBAL) {
-            bh_assert(data_seg->base_offset.u.global_index < global_count
-                        && globals[data_seg->base_offset.u.global_index].type
-                            == VALUE_TYPE_I32);
+            if (!globals
+                || data_seg->base_offset.u.global_index >= global_count
+                || globals[data_seg->base_offset.u.global_index].type
+                   != VALUE_TYPE_I32) {
+                set_error_buf(error_buf, error_buf_size,
+                              "data segment does not fit");
+                goto fail;
+            }
             data_seg->base_offset.u.i32 =
                 globals[data_seg->base_offset.u.global_index]
                 .initial_value.i32;
@@ -1300,8 +1302,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
                       memory_size);
             set_error_buf(error_buf, error_buf_size,
                           "data segment does not fit");
-            wasm_deinstantiate(module_inst, false);
-            return NULL;
+            goto fail;
         }
 
         /* check offset + length(could be zero) */
@@ -1311,8 +1312,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
                       base_offset, length, memory_size);
             set_error_buf(error_buf, error_buf_size,
                           "data segment does not fit");
-            wasm_deinstantiate(module_inst, false);
-            return NULL;
+            goto fail;
         }
 
         bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
@@ -1344,9 +1344,14 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
 
         if (table_seg->base_offset.init_expr_type
             == INIT_EXPR_TYPE_GET_GLOBAL) {
-            bh_assert(table_seg->base_offset.u.global_index < global_count
-                      && globals[table_seg->base_offset.u.global_index].type
-                           == VALUE_TYPE_I32);
+            if (!globals
+                || table_seg->base_offset.u.global_index >= global_count
+                || globals[table_seg->base_offset.u.global_index].type
+                   != VALUE_TYPE_I32) {
+                set_error_buf(error_buf, error_buf_size,
+                              "elements segment does not fit");
+                goto fail;
+            }
             table_seg->base_offset.u.i32 =
               globals[table_seg->base_offset.u.global_index].initial_value.i32;
         }
@@ -1357,8 +1362,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
                       table_seg->base_offset.u.i32, table->cur_size);
             set_error_buf(error_buf, error_buf_size,
                           "elements segment does not fit");
-            wasm_deinstantiate(module_inst, false);
-            return NULL;
+            goto fail;
         }
 
         /* check offset + length(could be zero) */
@@ -1368,8 +1372,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
                       table_seg->base_offset.u.i32, length, table->cur_size);
             set_error_buf(error_buf, error_buf_size,
                           "elements segment does not fit");
-            wasm_deinstantiate(module_inst, false);
-            return NULL;
+            goto fail;
         }
 
         /**
@@ -1420,8 +1423,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
                                        module->wasi_args.argv,
                                        module->wasi_args.argc,
                                        error_buf, error_buf_size)) {
-            wasm_deinstantiate(module_inst, false);
-            return NULL;
+            goto fail;
         }
     }
 #endif
@@ -1438,8 +1440,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
         || !execute_start_function(module_inst)) {
         set_error_buf(error_buf, error_buf_size,
                       module_inst->cur_exception);
-        wasm_deinstantiate(module_inst, false);
-        return NULL;
+        goto fail;
     }
 
 #if WASM_ENABLE_BULK_MEMORY != 0
@@ -1453,8 +1454,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
             if (!execute_memory_init_function(module_inst)) {
                 set_error_buf(error_buf, error_buf_size,
                               module_inst->cur_exception);
-                wasm_deinstantiate(module_inst, false);
-                return NULL;
+                goto fail;
             }
         }
 #if WASM_ENABLE_LIBC_WASI != 0
@@ -1468,6 +1468,9 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
 #endif
     (void)global_data_end;
     return module_inst;
+fail:
+    wasm_deinstantiate(module_inst, false);
+    return NULL;
 }
 
 void
@@ -1555,6 +1558,24 @@ wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
 }
 #endif
 
+static bool
+clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
+{
+#if WASM_ENABLE_LIBC_WASI != 0
+    const char *exception = wasm_get_exception(module_inst);
+    if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
+        /* The "wasi proc exit" exception is thrown by native lib to
+           let wasm app exit, which is a normal behavior, we clear
+           the exception here. */
+        wasm_set_exception(module_inst, NULL);
+        return true;
+    }
+    return false;
+#else
+    return false;
+#endif
+}
+
 bool
 wasm_call_function(WASMExecEnv *exec_env,
                    WASMFunctionInstance *function,
@@ -1562,6 +1583,7 @@ wasm_call_function(WASMExecEnv *exec_env,
 {
     WASMModuleInstance *module_inst = (WASMModuleInstance*)exec_env->module_inst;
     wasm_interp_call_wasm(module_inst, exec_env, function, argc, argv);
+    (void)clear_wasi_proc_exit_exception(module_inst);
     return !wasm_get_exception(module_inst) ? true : false;
 }
 
@@ -1696,8 +1718,13 @@ wasm_validate_app_addr(WASMModuleInstance *module_inst,
                        uint32 app_offset, uint32 size)
 {
     WASMMemoryInstance *memory = module_inst->default_memory;
-    uint32 memory_data_size =
-        memory->num_bytes_per_page * memory->cur_page_count;
+    uint32 memory_data_size;
+
+    if (!memory) {
+        goto fail;
+    }
+
+    memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
 
     /* integer overflow check */
     if (app_offset + size < app_offset) {
@@ -1716,8 +1743,12 @@ bool
 wasm_validate_native_addr(WASMModuleInstance *module_inst,
                           void *native_ptr, uint32 size)
 {
-    uint8 *addr = (uint8 *)native_ptr;
     WASMMemoryInstance *memory = module_inst->default_memory;
+    uint8 *addr = (uint8 *)native_ptr;
+
+    if (!memory) {
+        goto fail;
+    }
 
     /* integer overflow check */
     if (addr + size < addr) {
@@ -1738,7 +1769,12 @@ wasm_addr_app_to_native(WASMModuleInstance *module_inst,
                         uint32 app_offset)
 {
     WASMMemoryInstance *memory = module_inst->default_memory;
-    uint8 *addr = memory->memory_data + app_offset;
+    uint8 *addr;
+
+    if (!memory)
+        return NULL;
+
+    addr = memory->memory_data + app_offset;
 
     if (memory->memory_data <= addr
         && addr < memory->memory_data_end)
@@ -1753,6 +1789,9 @@ wasm_addr_native_to_app(WASMModuleInstance *module_inst,
     WASMMemoryInstance *memory = module_inst->default_memory;
     uint8 *addr = (uint8 *)native_ptr;
 
+    if (!memory)
+        return 0;
+
     if (memory->memory_data <= addr
         && addr < memory->memory_data_end)
         return (uint32)(addr - memory->memory_data);
@@ -1766,8 +1805,12 @@ wasm_get_app_addr_range(WASMModuleInstance *module_inst,
                         uint32 *p_app_end_offset)
 {
     WASMMemoryInstance *memory = module_inst->default_memory;
-    uint32 memory_data_size =
-        memory->num_bytes_per_page * memory->cur_page_count;
+    uint32 memory_data_size;
+
+    if (!memory)
+        return false;
+
+    memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
 
     if (app_offset < memory_data_size) {
         if (p_app_start_offset)
@@ -1788,6 +1831,9 @@ wasm_get_native_addr_range(WASMModuleInstance *module_inst,
     WASMMemoryInstance *memory = module_inst->default_memory;
     uint8 *addr = (uint8 *)native_ptr;
 
+    if (!memory)
+        return false;
+
     if (memory->memory_data <= addr
         && addr < memory->memory_data_end) {
         if (p_native_start_addr)
@@ -1803,12 +1849,19 @@ bool
 wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
 {
     WASMMemoryInstance *memory = module->default_memory;
-    uint8 *new_memory_data, *memory_data = memory->memory_data;
-    uint32 heap_size = memory->heap_data_end - memory->heap_data;
-    uint32 total_size_old = memory->memory_data_end - memory_data;
-    uint32 total_page_count = inc_page_count + memory->cur_page_count;
-    uint64 total_size = memory->num_bytes_per_page * (uint64)total_page_count;
-    uint8 *heap_data_old = memory->heap_data;
+    uint8 *new_memory_data, *memory_data, *heap_data_old;
+    uint32 heap_size, total_size_old, total_page_count;
+    uint64 total_size;
+
+    if (!memory)
+        return false;
+
+    memory_data = memory->memory_data;
+    heap_size = memory->heap_data_end - memory->heap_data;
+    total_size_old = memory->memory_data_end - memory_data;
+    total_page_count = inc_page_count + memory->cur_page_count;
+    total_size = memory->num_bytes_per_page * (uint64)total_page_count;
+    heap_data_old = memory->heap_data;
 
     if (inc_page_count <= 0)
         /* No need to enlarge memory */
@@ -1911,6 +1964,7 @@ wasm_call_indirect(WASMExecEnv *exec_env,
 
     wasm_interp_call_wasm(module_inst, exec_env, function_inst, argc, argv);
 
+    (void)clear_wasi_proc_exit_exception(module_inst);
     return !wasm_get_exception(module_inst) ? true : false;
 
 got_exception:
@@ -2075,6 +2129,9 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
         mem_conspn->memories_size += size;
         mem_conspn->app_heap_size += memory->heap_data_end
                                      - memory->heap_data;
+        /* size of app heap structure */
+        mem_conspn->memories_size +=
+            mem_allocator_get_heap_struct_size();
     }
 
     mem_conspn->tables_size = sizeof(WASMTableInstance *)

+ 11 - 0
core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c

@@ -265,6 +265,10 @@ call_key_destructor(wasm_exec_env_t exec_env)
     WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
     ClusterInfoNode *info = get_cluster_info(cluster);
 
+    if (!info) {
+        return;
+    }
+
     value_node = bh_list_first_elem(info->thread_list);
     while (value_node) {
         if (value_node->exec_env == exec_env)
@@ -435,6 +439,11 @@ get_thread_info(wasm_exec_env_t exec_env, uint32 handle)
 {
     WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
     ClusterInfoNode *info = get_cluster_info(cluster);
+
+    if (!info) {
+        return NULL;
+    }
+
     return bh_hash_map_find(info->thread_info_map, (void *)(uintptr_t)handle);
 }
 
@@ -524,6 +533,8 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
     WASIContext *wasi_ctx = get_wasi_ctx(module_inst);
 #endif
 
+    bh_assert(module);
+
     if (!(new_module_inst =
             wasm_runtime_instantiate_internal(module, true, 8192, 0,
                                               NULL, 0)))

+ 0 - 17
core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c

@@ -1007,21 +1007,6 @@ __cxa_throw_wrapper(wasm_exec_env_t exec_env,
     wasm_runtime_set_exception(module_inst, buf);
 }
 
-static int
-setjmp_wrapper(wasm_exec_env_t exec_env,
-               void *jmp_buf)
-{
-    os_printf("in setjmp()\n");
-    return 0;
-}
-
-static void
-longjmp_wrapper(wasm_exec_env_t exec_env,
-               void *jmp_buf, int val)
-{
-    os_printf("in longjmp()\n");
-}
-
 #if WASM_ENABLE_SPEC_TEST != 0
 static void
 print_wrapper(wasm_exec_env_t exec_env)
@@ -1120,8 +1105,6 @@ static NativeSymbol native_symbols_libc_builtin[] = {
     REG_NATIVE_FUNC(__cxa_allocate_exception, "(i)i"),
     REG_NATIVE_FUNC(__cxa_begin_catch, "(*)"),
     REG_NATIVE_FUNC(__cxa_throw, "(**i)"),
-    REG_NATIVE_FUNC(setjmp, "(*)i"),
-    REG_NATIVE_FUNC(longjmp, "(*i)"),
 };
 
 #if WASM_ENABLE_SPEC_TEST != 0

+ 17 - 0
core/iwasm/libraries/libc-emcc/libc_emcc_wrapper.c

@@ -267,6 +267,21 @@ getentropy_wrapper(wasm_exec_env_t exec_env, void *buffer, uint32 length)
     return getentropy(buffer, length);
 }
 
+static int
+setjmp_wrapper(wasm_exec_env_t exec_env,
+               void *jmp_buf)
+{
+    os_printf("setjmp() called\n");
+    return 0;
+}
+
+static void
+longjmp_wrapper(wasm_exec_env_t exec_env,
+               void *jmp_buf, int val)
+{
+    os_printf("longjmp() called\n");
+}
+
 #if !defined(BH_PLATFORM_LINUX_SGX)
 static FILE *file_list[32] = { 0 };
 
@@ -506,6 +521,8 @@ static NativeSymbol native_symbols_libc_emcc[] = {
     REG_NATIVE_FUNC(munmap, "(ii)i"),
     REG_NATIVE_FUNC(__munmap, "(ii)i"),
     REG_NATIVE_FUNC(getentropy, "(*~)i"),
+    REG_NATIVE_FUNC(setjmp, "(*)i"),
+    REG_NATIVE_FUNC(longjmp, "(*i)"),
 #if !defined(BH_PLATFORM_LINUX_SGX)
     REG_NATIVE_FUNC(fopen, "($$)i"),
     REG_NATIVE_FUNC(fread, "(*iii)i"),

+ 5 - 1
core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

@@ -1019,9 +1019,13 @@ wasi_poll_oneoff(wasm_exec_env_t exec_env,
     return 0;
 }
 
-void wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval)
+static void
+wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval)
 {
     wasm_module_inst_t module_inst = get_module_inst(exec_env);
+    /* Here throwing exception is just to let wasm app exit,
+       the upper layer should clear the exception and return
+       as normal */
     wasm_runtime_set_exception(module_inst, "wasi proc exit");
 }
 

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

@@ -1902,9 +1902,9 @@ __wasi_errno_t wasmtime_ssp_path_open(
     }
 
     if (S_ISDIR(sb.st_mode))
-      rights_base |= RIGHTS_DIRECTORY_BASE;
+      rights_base |= (__wasi_rights_t)RIGHTS_DIRECTORY_BASE;
     else if (S_ISREG(sb.st_mode))
-      rights_base |= RIGHTS_REGULAR_FILE_BASE;
+      rights_base |= (__wasi_rights_t)RIGHTS_REGULAR_FILE_BASE;
   }
 
   return fd_table_insert_fd(curfds, nfd, type, rights_base & max_base,

+ 4 - 0
core/iwasm/libraries/thread-mgr/thread_manager.c

@@ -288,6 +288,10 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
     WASMExecEnv *new_exec_env;
     uint32 aux_stack_start, aux_stack_size;
 
+    if (!module) {
+        return NULL;
+    }
+
     if (!(new_module_inst =
         wasm_runtime_instantiate_internal(module, true, 8192,
                                           0, NULL, 0))) {

+ 7 - 0
core/shared/platform/zephyr/zephyr_platform.c

@@ -87,6 +87,7 @@ os_free(void *ptr)
 {
 }
 
+#if 0
 struct out_context {
     int count;
 };
@@ -100,13 +101,19 @@ char_out(int c, void *ctx)
     out_ctx->count++;
     return _stdout_hook_iwasm(c);
 }
+#endif
 
 int
 os_vprintf(const char *fmt, va_list ap)
 {
+#if 0
     struct out_context ctx = { 0 };
     z_vprintk(char_out, &ctx, fmt, ap);
     return ctx.count;
+#else
+    vprintk(fmt, ap);
+    return 0;
+#endif
 }
 
 void *

+ 1 - 7
core/shared/utils/bh_assert.c

@@ -8,8 +8,6 @@
 void bh_assert_internal(int v, const char *file_name, int line_number,
                         const char *expr_string)
 {
-    int i;
-
     if (v)
         return;
 
@@ -22,10 +20,6 @@ void bh_assert_internal(int v, const char *file_name, int line_number,
     os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n",
               expr_string, file_name, line_number);
 
-    i = os_printf(" ");
-
-    /* divived by 0 to make it abort */
-    os_printf("%d\n", i / (i - 1));
-    while (1);
+    abort();
 }
 

+ 5 - 5
core/shared/utils/bh_hashmap.c

@@ -290,11 +290,11 @@ bh_hash_map_destroy(HashMap *map)
 uint32
 bh_hash_map_get_struct_size(HashMap *hashmap)
 {
-    uint32 size = offsetof(HashMap, elements)
-                  + sizeof(HashMapElem *) * hashmap->size;
+    uint32 size = (uint32)(uintptr_t)offsetof(HashMap, elements)
+                  + (uint32)sizeof(HashMapElem *) * hashmap->size;
 
     if (hashmap->lock) {
-        size += sizeof(korp_mutex);
+        size += (uint32)sizeof(korp_mutex);
     }
 
     return size;
@@ -303,7 +303,7 @@ bh_hash_map_get_struct_size(HashMap *hashmap)
 uint32
 bh_hash_map_get_elem_struct_size()
 {
-    return sizeof(HashMapElem);
+    return (uint32)sizeof(HashMapElem);
 }
 
 bool
@@ -335,4 +335,4 @@ bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback)
     }
 
     return true;
-}
+}

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

@@ -443,7 +443,10 @@ static void hal_init(void)
 
     /*Create a display*/
     lv_disp_drv_t disp_drv;
-    lv_disp_drv_init(&disp_drv);            /*Basic initialization*/
+
+    /*Basic initialization*/
+    memset(&disp_drv, 0, sizeof(disp_drv));
+    lv_disp_drv_init(&disp_drv);
     disp_drv.buffer = &disp_buf1;
     disp_drv.flush_cb = monitor_flush;
     //    disp_drv.hor_res = 200;

+ 1 - 1
samples/workload/wasm-av1/README.md

@@ -39,7 +39,7 @@ Firstly please build iwasm with simd support:
 ``` shell
 $ cd <wamr dir>/product-mini/platforms/linux/
 $ mkdir build && cd build
-$ cmake .. -DWAMR_BUILD_SIMD=1
+$ cmake .. -DWAMR_BUILD_SIMD=1 -DWAMR_BUILD_LIBC_EMCC=1
 $ make
 ```
 

+ 7 - 2
samples/workload/wasm-av1/wasm-av1.patch

@@ -30,7 +30,7 @@ index c39fff6..4682d43 100644
  		)
  
 diff --git a/test.c b/test.c
-index df2d44b..8e81cdc 100644
+index df2d44b..cb270de 100644
 --- a/test.c
 +++ b/test.c
 @@ -18,6 +18,9 @@
@@ -58,7 +58,7 @@ index df2d44b..8e81cdc 100644
      fclose(f);
  }
  
-@@ -63,6 +67,7 @@ main(int argc, char *argv[]) {
+@@ -63,9 +67,12 @@ main(int argc, char *argv[]) {
                      static int     i = 0;
                      
                      ++i;
@@ -66,6 +66,11 @@ index df2d44b..8e81cdc 100644
                      if (30 <= i && i < 40) {
                          dump_raw_frame(af, i);
                      }
++                    if (i >= 1000)
++                        break;
+                 }
+                 /*
+                  * Run the decoder every time, so that we keep
 diff --git a/third_party/aom/CMakeLists.txt b/third_party/aom/CMakeLists.txt
 index 9dbe301..20c7be4 100644
 --- a/third_party/aom/CMakeLists.txt

+ 1 - 1
wamr-compiler/main.c

@@ -159,7 +159,7 @@ main(int argc, char *argv[])
       return print_help();
   }
 
-  if (argc == 0)
+  if (argc == 0 || !out_file_name)
     return print_help();
 
   if (sgx_mode) {