From e15db8d732ab6f776b536939e8adb68412644724 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Mon, 22 Aug 2022 14:52:33 +0800 Subject: [PATCH 01/41] Destroy fast-jit compiler after destroy loaded modules (#1402) Destroy Fast-JIT compiler after destroying the modules loaded by multi-module feature, since the Fast JIT's code cache allocator may be used by these modules. If the Fast JIT's code cache allocator was destroyed, then runtime will fail to destroy these modules. And fix the issue of destroying import module's memory instance. --- core/iwasm/common/wasm_runtime_common.c | 12 ++++++++---- core/iwasm/interpreter/wasm_runtime.c | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 4ee42601c..7562877c4 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -341,10 +341,6 @@ wasm_runtime_init() void wasm_runtime_destroy() { -#if WASM_ENABLE_FAST_JIT != 0 - jit_compiler_destroy(); -#endif - #if WASM_ENABLE_REF_TYPES != 0 wasm_externref_map_destroy(); #endif @@ -368,6 +364,14 @@ wasm_runtime_destroy() os_mutex_destroy(®istered_module_list_lock); #endif +#if WASM_ENABLE_FAST_JIT != 0 + /* Destroy Fast-JIT compiler after destroying the modules + * loaded by multi-module feature, since the Fast JIT's + * code cache allocator may be used by these modules. + */ + jit_compiler_destroy(); +#endif + #if WASM_ENABLE_SHARED_MEMORY wasm_shared_memory_destroy(); #endif diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 20b6a0459..e76f3b00a 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -109,8 +109,11 @@ memories_deinstantiate(WASMModuleInstance *module_inst, for (i = 0; i < count; i++) { if (memories[i]) { #if WASM_ENABLE_MULTI_MODULE != 0 - if (i < module_inst->module->import_memory_count) + WASMModule *module = module_inst->module; + if (i < module->import_memory_count + && module->import_memories[i].u.memory.import_module) { continue; + } #endif #if WASM_ENABLE_SHARED_MEMORY != 0 if (memories[i]->is_shared) { From ccd627d2c6d28db5e8235524f3a21545640e2168 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 23 Aug 2022 16:05:13 +0800 Subject: [PATCH 02/41] Fix linear memory page count issues (#1380) Fix issue reported in #1289 and #1371. Enable to set the max page count to 65536. --- core/iwasm/aot/aot_runtime.c | 261 ++++++++++++---------- core/iwasm/interpreter/wasm.h | 1 + core/iwasm/interpreter/wasm_loader.c | 41 ++-- core/iwasm/interpreter/wasm_mini_loader.c | 37 ++- core/iwasm/interpreter/wasm_runtime.c | 176 ++++++++++----- core/iwasm/interpreter/wasm_runtime.h | 21 +- 6 files changed, 327 insertions(+), 210 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 4515e4a9e..ff05456fd 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -412,18 +412,27 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, if (init_page_count == max_page_count && init_page_count == 1) { /* If only one page and at most one page, we just append the app heap to the end of linear memory, enlarge the - num_bytes_per_page, and don't change the page count*/ + num_bytes_per_page, and don't change the page count */ heap_offset = num_bytes_per_page; num_bytes_per_page += heap_size; if (num_bytes_per_page < heap_size) { set_error_buf(error_buf, error_buf_size, - "memory size must be at most 65536 pages (4GiB)"); + "failed to insert app heap into linear memory, " + "try using `--heap_size=0` option"); return NULL; } } else if (heap_size > 0) { - if (module->aux_heap_base_global_index != (uint32)-1 - && module->aux_heap_base < num_bytes_per_page * init_page_count) { + if (init_page_count == max_page_count && init_page_count == 0) { + /* If the memory data size is always 0, we resize it to + one page for app heap */ + num_bytes_per_page = heap_size; + heap_offset = 0; + inc_page_count = 1; + } + else if (module->aux_heap_base_global_index != (uint32)-1 + && module->aux_heap_base + < num_bytes_per_page * init_page_count) { /* Insert app heap before __heap_base */ aux_heap_base = module->aux_heap_base; bytes_of_last_page = aux_heap_base % num_bytes_per_page; @@ -464,13 +473,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, } init_page_count += inc_page_count; max_page_count += inc_page_count; - if (init_page_count > 65536) { + if (init_page_count > DEFAULT_MAX_PAGES) { set_error_buf(error_buf, error_buf_size, - "memory size must be at most 65536 pages (4GiB)"); + "failed to insert app heap into linear memory, " + "try using `--heap_size=0` option"); return NULL; } - if (max_page_count > 65536) - max_page_count = 65536; + else if (init_page_count == DEFAULT_MAX_PAGES) { + num_bytes_per_page = UINT32_MAX; + init_page_count = max_page_count = 1; + } + if (max_page_count > DEFAULT_MAX_PAGES) + max_page_count = DEFAULT_MAX_PAGES; } LOG_VERBOSE("Memory instantiate:"); @@ -487,6 +501,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, total_size = (uint64)num_bytes_per_page * max_page_count; } #endif + bh_assert(total_size <= UINT32_MAX); #ifndef OS_ENABLE_HW_BOUND_CHECK /* Allocate memory */ @@ -502,9 +517,8 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, * both i and memarg.offset are u32 in range 0 to 4G * so the range of ea is 0 to 8G */ - if (total_size >= UINT32_MAX - || !(p = mapped_mem = - os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { + if (!(p = mapped_mem = + os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { set_error_buf(error_buf, error_buf_size, "mmap memory failed"); return NULL; } @@ -529,15 +543,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, * again here */ #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ + if (total_size > UINT32_MAX) + total_size = UINT32_MAX; + memory_inst->module_type = Wasm_Module_AoT; memory_inst->num_bytes_per_page = num_bytes_per_page; memory_inst->cur_page_count = init_page_count; memory_inst->max_page_count = max_page_count; + memory_inst->memory_data_size = (uint32)total_size; /* Init memory info */ memory_inst->memory_data.ptr = p; memory_inst->memory_data_end.ptr = p + (uint32)total_size; - memory_inst->memory_data_size = (uint32)total_size; /* Initialize heap info */ memory_inst->heap_data.ptr = p + heap_offset; @@ -561,21 +578,19 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, } if (total_size > 0) { - if (sizeof(uintptr_t) == sizeof(uint64)) { - memory_inst->mem_bound_check_1byte.u64 = total_size - 1; - memory_inst->mem_bound_check_2bytes.u64 = total_size - 2; - memory_inst->mem_bound_check_4bytes.u64 = total_size - 4; - memory_inst->mem_bound_check_8bytes.u64 = total_size - 8; - memory_inst->mem_bound_check_16bytes.u64 = total_size - 16; - } - else { - memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1; - memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2; - memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4; - memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8; - memory_inst->mem_bound_check_16bytes.u32[0] = - (uint32)total_size - 16; - } +#if UINTPTR_MAX == UINT64_MAX + memory_inst->mem_bound_check_1byte.u64 = total_size - 1; + memory_inst->mem_bound_check_2bytes.u64 = total_size - 2; + memory_inst->mem_bound_check_4bytes.u64 = total_size - 4; + memory_inst->mem_bound_check_8bytes.u64 = total_size - 8; + memory_inst->mem_bound_check_16bytes.u64 = total_size - 16; +#else + memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1; + memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2; + memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4; + memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8; + memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16; +#endif } #if WASM_ENABLE_SHARED_MEMORY != 0 @@ -2032,26 +2047,29 @@ aot_get_native_addr_range(AOTModuleInstance *module_inst, uint8 *native_ptr, 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, 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; + AOTMemoryInstance *memory = aot_get_default_memory(module_inst); + uint8 *memory_data_old, *memory_data_new, *heap_data_old; + uint32 num_bytes_per_page, heap_size, total_size_old; + uint32 cur_page_count, max_page_count, total_page_count; + uint64 total_size_new; bool ret = true; - if (!memory_inst) + if (!memory) 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; + heap_data_old = (uint8 *)memory->heap_data.ptr; + heap_size = (uint32)((uint8 *)memory->heap_data_end.ptr + - (uint8 *)memory->heap_data.ptr); + + memory_data_old = (uint8 *)memory->memory_data.ptr; + total_size_old = + (uint32)((uint8 *)memory->memory_data_end.ptr - memory_data_old); + + num_bytes_per_page = memory->num_bytes_per_page; + cur_page_count = memory->cur_page_count; + max_page_count = memory->max_page_count; + total_page_count = inc_page_count + cur_page_count; + total_size_new = num_bytes_per_page * (uint64)total_page_count; if (inc_page_count <= 0) /* No need to enlarge memory */ @@ -2062,94 +2080,103 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) return false; } - if (total_size >= UINT32_MAX) { - return false; + bh_assert(total_size_new <= 4 * (uint64)BH_GB); + if (total_size_new > UINT32_MAX) { + /* Resize to 1 page with size 4G-1 */ + num_bytes_per_page = UINT32_MAX; + total_page_count = max_page_count = 1; + total_size_new = UINT32_MAX; } #if WASM_ENABLE_SHARED_MEMORY != 0 - if (memory_inst->is_shared) { - /* For shared memory, we have reserved the maximum spaces during - instantiate, only change the cur_page_count here */ - memory_inst->cur_page_count = total_page_count; + if (memory->is_shared) { + memory->num_bytes_per_page = UINT32_MAX; + memory->cur_page_count = total_page_count; + memory->max_page_count = max_page_count; + memory->memory_data_size = (uint32)total_size_new; return true; } #endif if (heap_size > 0) { - if (mem_allocator_is_heap_corrupted(memory_inst->heap_handle.ptr)) { + if (mem_allocator_is_heap_corrupted(memory->heap_handle.ptr)) { wasm_runtime_show_app_heap_corrupted_prompt(); return false; } } - if (!(memory_data = - wasm_runtime_realloc(memory_data_old, (uint32)total_size))) { - if (!(memory_data = wasm_runtime_malloc((uint32)total_size))) { + if (!(memory_data_new = + wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) { + if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) { return false; } if (memory_data_old) { - bh_memcpy_s(memory_data, (uint32)total_size, memory_data_old, - total_size_old); + bh_memcpy_s(memory_data_new, (uint32)total_size_new, + memory_data_old, total_size_old); wasm_runtime_free(memory_data_old); } } - memset(memory_data + total_size_old, 0, - (uint32)total_size - total_size_old); - - memory_inst->cur_page_count = total_page_count; - memory_inst->memory_data_size = (uint32)total_size; - memory_inst->memory_data.ptr = memory_data; - memory_inst->memory_data_end.ptr = memory_data + total_size; + memset(memory_data_new + total_size_old, 0, + (uint32)total_size_new - total_size_old); if (heap_size > 0) { - if (mem_allocator_migrate(memory_inst->heap_handle.ptr, + if (mem_allocator_migrate(memory->heap_handle.ptr, (char *)heap_data_old - + (memory_data - memory_data_old), - heap_size)) { + + (memory_data_new - memory_data_old), + heap_size) + != 0) { /* Don't return here as memory->memory_data is obsolete and must be updated to be correctly used later. */ ret = false; } } - heap_data = memory_data + (heap_data_old - memory_data_old); - memory_inst->heap_data.ptr = heap_data; - memory_inst->heap_data_end.ptr = heap_data + heap_size; + memory->heap_data.ptr = memory_data_new + (heap_data_old - memory_data_old); + memory->heap_data_end.ptr = (uint8 *)memory->heap_data.ptr + heap_size; + + memory->num_bytes_per_page = num_bytes_per_page; + memory->cur_page_count = total_page_count; + memory->max_page_count = max_page_count; + + memory->memory_data.ptr = memory_data_new; + memory->memory_data_end.ptr = memory_data_new + (uint32)total_size_new; + memory->memory_data_size = (uint32)total_size_new; + +#if UINTPTR_MAX == UINT64_MAX + memory->mem_bound_check_1byte.u64 = total_size_new - 1; + memory->mem_bound_check_2bytes.u64 = total_size_new - 2; + memory->mem_bound_check_4bytes.u64 = total_size_new - 4; + memory->mem_bound_check_8bytes.u64 = total_size_new - 8; + memory->mem_bound_check_16bytes.u64 = total_size_new - 16; +#else + memory->mem_bound_check_1byte.u32[0] = (uint32)total_size_new - 1; + memory->mem_bound_check_2bytes.u32[0] = (uint32)total_size_new - 2; + memory->mem_bound_check_4bytes.u32[0] = (uint32)total_size_new - 4; + memory->mem_bound_check_8bytes.u32[0] = (uint32)total_size_new - 8; + memory->mem_bound_check_16bytes.u32[0] = (uint32)total_size_new - 16; +#endif - if (sizeof(uintptr_t) == sizeof(uint64)) { - memory_inst->mem_bound_check_1byte.u64 = total_size - 1; - memory_inst->mem_bound_check_2bytes.u64 = total_size - 2; - memory_inst->mem_bound_check_4bytes.u64 = total_size - 4; - memory_inst->mem_bound_check_8bytes.u64 = total_size - 8; - memory_inst->mem_bound_check_16bytes.u64 = total_size - 16; - } - else { - memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1; - memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2; - memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4; - memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8; - memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16; - } return ret; } #else /* else of OS_ENABLE_HW_BOUND_CHECK */ 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, cur_page_count, max_page_count; - uint32 total_page_count; - uint64 total_size; + AOTMemoryInstance *memory = aot_get_default_memory(module_inst); + uint32 num_bytes_per_page, total_size_old; + uint32 cur_page_count, max_page_count, total_page_count; + uint64 total_size_new; - if (!memory_inst) + if (!memory) 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; + num_bytes_per_page = memory->num_bytes_per_page; + cur_page_count = memory->cur_page_count; + max_page_count = memory->max_page_count; + total_size_old = num_bytes_per_page * cur_page_count; + total_page_count = inc_page_count + cur_page_count; + total_size_new = num_bytes_per_page * (uint64)total_page_count; if (inc_page_count <= 0) /* No need to enlarge memory */ @@ -2160,21 +2187,29 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) return false; } + bh_assert(total_size_new <= 4 * (uint64)BH_GB); + if (total_size_new > UINT32_MAX) { + /* Resize to 1 page with size 4G-1 */ + num_bytes_per_page = UINT32_MAX; + total_page_count = max_page_count = 1; + total_size_new = UINT32_MAX; + } + #ifdef BH_PLATFORM_WINDOWS - if (!os_mem_commit(memory_inst->memory_data_end.ptr, - num_bytes_per_page * inc_page_count, + if (!os_mem_commit(memory->memory_data_end.ptr, + (uint32)total_size_new - total_size_old, MMAP_PROT_READ | MMAP_PROT_WRITE)) { return false; } #endif - if (os_mprotect(memory_inst->memory_data_end.ptr, - num_bytes_per_page * inc_page_count, + if (os_mprotect(memory->memory_data_end.ptr, + (uint32)total_size_new - total_size_old, MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) { #ifdef BH_PLATFORM_WINDOWS - os_mem_decommit(memory_inst->memory_data_end.ptr, - num_bytes_per_page * inc_page_count); + os_mem_decommit(memory->memory_data_end.ptr, + (uint32)total_size_new - total_size_old); #endif return false; } @@ -2182,25 +2217,19 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) /* The increased pages are filled with zero by the OS when os_mmap, no need to memset it again here */ - memory_inst->cur_page_count = total_page_count; - memory_inst->memory_data_size = (uint32)total_size; - memory_inst->memory_data_end.ptr = - (uint8 *)memory_inst->memory_data.ptr + (uint32)total_size; + memory->num_bytes_per_page = num_bytes_per_page; + memory->cur_page_count = total_page_count; + memory->max_page_count = max_page_count; + memory->memory_data_size = (uint32)total_size_new; + memory->memory_data_end.ptr = + (uint8 *)memory->memory_data.ptr + (uint32)total_size_new; + + memory->mem_bound_check_1byte.u64 = total_size_new - 1; + memory->mem_bound_check_2bytes.u64 = total_size_new - 2; + memory->mem_bound_check_4bytes.u64 = total_size_new - 4; + memory->mem_bound_check_8bytes.u64 = total_size_new - 8; + memory->mem_bound_check_16bytes.u64 = total_size_new - 16; - if (sizeof(uintptr_t) == sizeof(uint64)) { - memory_inst->mem_bound_check_1byte.u64 = total_size - 1; - memory_inst->mem_bound_check_2bytes.u64 = total_size - 2; - memory_inst->mem_bound_check_4bytes.u64 = total_size - 4; - memory_inst->mem_bound_check_8bytes.u64 = total_size - 8; - memory_inst->mem_bound_check_16bytes.u64 = total_size - 16; - } - else { - memory_inst->mem_bound_check_1byte.u32[0] = (uint32)total_size - 1; - memory_inst->mem_bound_check_2bytes.u32[0] = (uint32)total_size - 2; - memory_inst->mem_bound_check_4bytes.u32[0] = (uint32)total_size - 4; - memory_inst->mem_bound_check_8bytes.u32[0] = (uint32)total_size - 8; - memory_inst->mem_bound_check_16bytes.u32[0] = (uint32)total_size - 16; - } return true; } #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index 1da303d85..07a553d3a 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -29,6 +29,7 @@ extern "C" { #define VALUE_TYPE_ANY 0x42 #define DEFAULT_NUM_BYTES_PER_PAGE 65536 +#define DEFAULT_MAX_PAGES 65536 #define NULL_REF (0xFFFFFFFF) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index cf106c8af..6fa0a21d1 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -1266,7 +1266,7 @@ fail: static bool check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size) { - if (init_size > 65536) { + if (init_size > DEFAULT_MAX_PAGES) { set_error_buf(error_buf, error_buf_size, "memory size must be at most 65536 pages (4GiB)"); return false; @@ -1284,7 +1284,7 @@ check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf, return false; } - if (max_size > 65536) { + if (max_size > DEFAULT_MAX_PAGES) { set_error_buf(error_buf, error_buf_size, "memory size must be at most 65536 pages (4GiB)"); return false; @@ -1299,12 +1299,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; - uint32 pool_size = wasm_runtime_memory_pool_size(); #if WASM_ENABLE_APP_FRAMEWORK != 0 + uint32 pool_size = wasm_runtime_memory_pool_size(); uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT / DEFAULT_NUM_BYTES_PER_PAGE; #else - uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE; + uint32 max_page_count = DEFAULT_MAX_PAGES; #endif /* WASM_ENABLE_APP_FRAMEWORK */ uint32 declare_max_page_count_flag = 0; uint32 declare_init_page_count = 0; @@ -1529,12 +1529,12 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end, *p_org; - uint32 pool_size = wasm_runtime_memory_pool_size(); #if WASM_ENABLE_APP_FRAMEWORK != 0 + uint32 pool_size = wasm_runtime_memory_pool_size(); uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT / DEFAULT_NUM_BYTES_PER_PAGE; #else - uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE; + uint32 max_page_count = DEFAULT_MAX_PAGES; #endif p_org = p; @@ -3317,17 +3317,30 @@ load_from_sections(WASMModule *module, WASMSection *sections, #if WASM_ENABLE_MULTI_MODULE == 0 if (module->import_memory_count) { memory_import = &module->import_memories[0].u.memory; - /* Memory init page count cannot be larger than 65536, we don't - check integer overflow again. */ - memory_import->num_bytes_per_page *= memory_import->init_page_count; - memory_import->init_page_count = memory_import->max_page_count = 1; + if (memory_import->init_page_count < DEFAULT_MAX_PAGES) + memory_import->num_bytes_per_page *= + memory_import->init_page_count; + else + memory_import->num_bytes_per_page = UINT32_MAX; + + if (memory_import->init_page_count > 0) + memory_import->init_page_count = memory_import->max_page_count = + 1; + else + memory_import->init_page_count = memory_import->max_page_count = + 0; } if (module->memory_count) { - /* Memory init page count cannot be larger than 65536, we don't - check integer overflow again. */ memory = &module->memories[0]; - memory->num_bytes_per_page *= memory->init_page_count; - memory->init_page_count = memory->max_page_count = 1; + if (memory->init_page_count < DEFAULT_MAX_PAGES) + memory->num_bytes_per_page *= memory->init_page_count; + else + memory->num_bytes_per_page = UINT32_MAX; + + if (memory->init_page_count > 0) + memory->init_page_count = memory->max_page_count = 1; + else + memory->init_page_count = memory->max_page_count = 0; } #endif } diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 3ac80b7bd..f3c649ff5 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -534,12 +534,12 @@ load_memory_import(const uint8 **p_buf, const uint8 *buf_end, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end; - uint32 pool_size = wasm_runtime_memory_pool_size(); #if WASM_ENABLE_APP_FRAMEWORK != 0 + uint32 pool_size = wasm_runtime_memory_pool_size(); uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT / DEFAULT_NUM_BYTES_PER_PAGE; #else - uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE; + uint32 max_page_count = DEFAULT_MAX_PAGES; #endif /* WASM_ENABLE_APP_FRAMEWORK */ uint32 declare_max_page_count_flag = 0; uint32 declare_init_page_count = 0; @@ -650,12 +650,12 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory, char *error_buf, uint32 error_buf_size) { const uint8 *p = *p_buf, *p_end = buf_end, *p_org; - uint32 pool_size = wasm_runtime_memory_pool_size(); #if WASM_ENABLE_APP_FRAMEWORK != 0 + uint32 pool_size = wasm_runtime_memory_pool_size(); uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT / DEFAULT_NUM_BYTES_PER_PAGE; #else - uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE; + uint32 max_page_count = DEFAULT_MAX_PAGES; #endif p_org = p; @@ -2153,18 +2153,31 @@ load_from_sections(WASMModule *module, WASMSection *sections, if (module->import_memory_count) { memory_import = &module->import_memories[0].u.memory; - /* Memory init page count cannot be larger than 65536, we don't - check integer overflow again. */ - memory_import->num_bytes_per_page *= memory_import->init_page_count; - memory_import->init_page_count = memory_import->max_page_count = 1; + if (memory_import->init_page_count < DEFAULT_MAX_PAGES) + memory_import->num_bytes_per_page *= + memory_import->init_page_count; + else + memory_import->num_bytes_per_page = UINT32_MAX; + + if (memory_import->init_page_count > 0) + memory_import->init_page_count = memory_import->max_page_count = + 1; + else + memory_import->init_page_count = memory_import->max_page_count = + 0; } if (module->memory_count) { - /* Memory init page count cannot be larger than 65536, we don't - check integer overflow again. */ memory = &module->memories[0]; - memory->num_bytes_per_page *= memory->init_page_count; - memory->init_page_count = memory->max_page_count = 1; + if (memory->init_page_count < DEFAULT_MAX_PAGES) + memory->num_bytes_per_page *= memory->init_page_count; + else + memory->num_bytes_per_page = UINT32_MAX; + + if (memory->init_page_count > 0) + memory->init_page_count = memory->max_page_count = 1; + else + memory->init_page_count = memory->max_page_count = 0; } } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index e76f3b00a..b0f9a3ea3 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -206,18 +206,27 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page, if (init_page_count == max_page_count && init_page_count == 1) { /* If only one page and at most one page, we just append the app heap to the end of linear memory, enlarge the - num_bytes_per_page, and don't change the page count*/ + num_bytes_per_page, and don't change the page count */ heap_offset = num_bytes_per_page; num_bytes_per_page += heap_size; if (num_bytes_per_page < heap_size) { set_error_buf(error_buf, error_buf_size, - "memory size must be at most 65536 pages (4GiB)"); + "failed to insert app heap into linear memory, " + "try using `--heap_size=0` option"); return NULL; } } else if (heap_size > 0) { - if (module->aux_heap_base_global_index != (uint32)-1 - && module->aux_heap_base < num_bytes_per_page * init_page_count) { + if (init_page_count == max_page_count && init_page_count == 0) { + /* If the memory data size is always 0, we resize it to + one page for app heap */ + num_bytes_per_page = heap_size; + heap_offset = 0; + inc_page_count = 1; + } + else if (module->aux_heap_base_global_index != (uint32)-1 + && module->aux_heap_base + < num_bytes_per_page * init_page_count) { /* Insert app heap before __heap_base */ aux_heap_base = module->aux_heap_base; bytes_of_last_page = aux_heap_base % num_bytes_per_page; @@ -259,13 +268,18 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page, } init_page_count += inc_page_count; max_page_count += inc_page_count; - if (init_page_count > 65536) { + if (init_page_count > DEFAULT_MAX_PAGES) { set_error_buf(error_buf, error_buf_size, - "memory size must be at most 65536 pages (4GiB)"); + "failed to insert app heap into linear memory, " + "try using `--heap_size=0` option"); return NULL; } - if (max_page_count > 65536) - max_page_count = 65536; + else if (init_page_count == DEFAULT_MAX_PAGES) { + num_bytes_per_page = UINT32_MAX; + init_page_count = max_page_count = 1; + } + if (max_page_count > DEFAULT_MAX_PAGES) + max_page_count = DEFAULT_MAX_PAGES; } LOG_VERBOSE("Memory instantiate:"); @@ -280,6 +294,7 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page, memory_data_size = (uint64)num_bytes_per_page * max_page_count; } #endif + bh_assert(memory_data_size <= 4 * (uint64)BH_GB); /* Allocate memory space, addr data and global data */ if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance), error_buf, @@ -301,9 +316,8 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page, * both i and memarg.offset are u32 in range 0 to 4G * so the range of ea is 0 to 8G */ - if (memory_data_size >= UINT32_MAX - || !(memory->memory_data = mapped_mem = - os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { + if (!(memory->memory_data = mapped_mem = + os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { set_error_buf(error_buf, error_buf_size, "mmap memory failed"); goto fail1; } @@ -327,10 +341,14 @@ memory_instantiate(WASMModuleInstance *module_inst, uint32 num_bytes_per_page, * again here */ #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ + if (memory_data_size > UINT32_MAX) + memory_data_size = (uint32)memory_data_size; + memory->module_type = Wasm_Module_Bytecode; memory->num_bytes_per_page = num_bytes_per_page; memory->cur_page_count = init_page_count; memory->max_page_count = max_page_count; + memory->memory_data_size = (uint32)memory_data_size; memory->heap_data = memory->memory_data + heap_offset; memory->heap_data_end = memory->heap_data + heap_size; @@ -2419,39 +2437,49 @@ bool wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) { WASMMemoryInstance *memory = module->default_memory; - uint8 *new_memory_data, *memory_data, *heap_data_old; - uint32 heap_size, total_size_old, total_page_count; - uint64 total_size; + uint8 *memory_data_old, *memory_data_new, *heap_data_old; + uint32 num_bytes_per_page, heap_size, total_size_old; + uint32 cur_page_count, max_page_count, total_page_count; + uint64 total_size_new; bool ret = true; if (!memory) return false; - memory_data = memory->memory_data; - heap_size = (uint32)(memory->heap_data_end - memory->heap_data); - total_size_old = (uint32)(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; + heap_size = (uint32)(memory->heap_data_end - memory->heap_data); + + memory_data_old = memory->memory_data; + total_size_old = memory->memory_data_size; + + num_bytes_per_page = memory->num_bytes_per_page; + cur_page_count = memory->cur_page_count; + max_page_count = memory->max_page_count; + total_page_count = inc_page_count + cur_page_count; + total_size_new = num_bytes_per_page * (uint64)total_page_count; if (inc_page_count <= 0) /* No need to enlarge memory */ return true; - if (total_page_count < memory->cur_page_count /* integer overflow */ - || total_page_count > memory->max_page_count) { + if (total_page_count < cur_page_count /* integer overflow */ + || total_page_count > max_page_count) { return false; } - if (total_size >= UINT32_MAX) { - return false; + bh_assert(total_size_new <= 4 * (uint64)BH_GB); + if (total_size_new > UINT32_MAX) { + /* Resize to 1 page with size 4G-1 */ + num_bytes_per_page = UINT32_MAX; + total_page_count = max_page_count = 1; + total_size_new = UINT32_MAX; } #if WASM_ENABLE_SHARED_MEMORY != 0 if (memory->is_shared) { - /* For shared memory, we have reserved the maximum spaces during - instantiate, only change the cur_page_count here */ + memory->num_bytes_per_page = UINT32_MAX; memory->cur_page_count = total_page_count; + memory->max_page_count = max_page_count; return true; } #endif @@ -2463,25 +2491,25 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) } } - if (!(new_memory_data = - wasm_runtime_realloc(memory_data, (uint32)total_size))) { - if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) { + if (!(memory_data_new = + wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) { + if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) { return false; } - if (memory_data) { - bh_memcpy_s(new_memory_data, (uint32)total_size, memory_data, - total_size_old); - wasm_runtime_free(memory_data); + if (memory_data_old) { + bh_memcpy_s(memory_data_new, (uint32)total_size_new, + memory_data_old, total_size_old); + wasm_runtime_free(memory_data_old); } } - memset(new_memory_data + total_size_old, 0, - (uint32)total_size - total_size_old); + memset(memory_data_new + total_size_old, 0, + (uint32)total_size_new - total_size_old); if (heap_size > 0) { if (mem_allocator_migrate(memory->heap_handle, (char *)heap_data_old - + (new_memory_data - memory_data), + + (memory_data_new - memory_data_old), heap_size) != 0) { /* Don't return here as memory->memory_data is obsolete and @@ -2490,26 +2518,30 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) } } - memory->memory_data = new_memory_data; - memory->cur_page_count = total_page_count; - memory->heap_data = new_memory_data + (heap_data_old - memory_data); + memory->heap_data = memory_data_new + (heap_data_old - memory_data_old); memory->heap_data_end = memory->heap_data + heap_size; - memory->memory_data_end = - memory->memory_data + memory->num_bytes_per_page * total_page_count; + + memory->num_bytes_per_page = num_bytes_per_page; + memory->cur_page_count = total_page_count; + memory->max_page_count = max_page_count; + memory->memory_data_size = (uint32)total_size_new; + + memory->memory_data = memory_data_new; + memory->memory_data_end = memory_data_new + (uint32)total_size_new; #if WASM_ENABLE_FAST_JIT != 0 #if UINTPTR_MAX == UINT64_MAX - memory->mem_bound_check_1byte = total_size - 1; - memory->mem_bound_check_2bytes = total_size - 2; - memory->mem_bound_check_4bytes = total_size - 4; - memory->mem_bound_check_8bytes = total_size - 8; - memory->mem_bound_check_16bytes = total_size - 16; + memory->mem_bound_check_1byte = total_size_new - 1; + memory->mem_bound_check_2bytes = total_size_new - 2; + memory->mem_bound_check_4bytes = total_size_new - 4; + memory->mem_bound_check_8bytes = total_size_new - 8; + memory->mem_bound_check_16bytes = total_size_new - 16; #else - memory->mem_bound_check_1byte = (uint32)total_size - 1; - memory->mem_bound_check_2bytes = (uint32)total_size - 2; - memory->mem_bound_check_4bytes = (uint32)total_size - 4; - memory->mem_bound_check_8bytes = (uint32)total_size - 8; - memory->mem_bound_check_16bytes = (uint32)total_size - 16; + memory->mem_bound_check_1byte = (uint32)total_size_new - 1; + memory->mem_bound_check_2bytes = (uint32)total_size_new - 2; + memory->mem_bound_check_4bytes = (uint32)total_size_new - 4; + memory->mem_bound_check_8bytes = (uint32)total_size_new - 8; + memory->mem_bound_check_16bytes = (uint32)total_size_new - 16; #endif #endif @@ -2520,39 +2552,52 @@ bool wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) { WASMMemoryInstance *memory = module->default_memory; - uint32 num_bytes_per_page, total_page_count; + uint32 num_bytes_per_page, total_size_old; + uint32 cur_page_count, max_page_count, total_page_count; + uint64 total_size_new; if (!memory) return false; - total_page_count = inc_page_count + memory->cur_page_count; + num_bytes_per_page = memory->num_bytes_per_page; + cur_page_count = memory->cur_page_count; + max_page_count = memory->max_page_count; + total_size_old = num_bytes_per_page * cur_page_count; + total_page_count = inc_page_count + cur_page_count; + total_size_new = num_bytes_per_page * (uint64)total_page_count; if (inc_page_count <= 0) /* No need to enlarge memory */ return true; - if (total_page_count < memory->cur_page_count /* integer overflow */ - || total_page_count > memory->max_page_count) { + if (total_page_count < cur_page_count /* integer overflow */ + || total_page_count > max_page_count) { return false; } - num_bytes_per_page = memory->num_bytes_per_page; + bh_assert(total_size_new <= 4 * (uint64)BH_GB); + if (total_size_new > UINT32_MAX) { + /* Resize to 1 page with size 4G-1 */ + num_bytes_per_page = UINT32_MAX; + total_page_count = max_page_count = 1; + total_size_new = UINT32_MAX; + } #ifdef BH_PLATFORM_WINDOWS if (!os_mem_commit(memory->memory_data_end, - num_bytes_per_page * inc_page_count, + (uint32)total_size_new - total_size_old, MMAP_PROT_READ | MMAP_PROT_WRITE)) { return false; } #endif if (os_mprotect(memory->memory_data_end, - num_bytes_per_page * inc_page_count, + (uint32)total_size_new - total_size_old, MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) { #ifdef BH_PLATFORM_WINDOWS os_mem_decommit(memory->memory_data_end, - num_bytes_per_page * inc_page_count); + (uint32)total_size_new - total_size_old); #endif return false; } @@ -2560,9 +2605,20 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) /* The increased pages are filled with zero by the OS when os_mmap, no need to memset it again here */ + memory->num_bytes_per_page = num_bytes_per_page; memory->cur_page_count = total_page_count; - memory->memory_data_end = - memory->memory_data + num_bytes_per_page * total_page_count; + memory->max_page_count = max_page_count; + memory->memory_data_size = (uint32)total_size_new; + memory->memory_data_end = memory->memory_data + (uint32)total_size_new; + +#if WASM_ENABLE_FAST_JIT != 0 + memory->mem_bound_check_1byte = total_size_new - 1; + memory->mem_bound_check_2bytes = total_size_new - 2; + memory->mem_bound_check_4bytes = total_size_new - 4; + memory->mem_bound_check_8bytes = total_size_new - 8; + memory->mem_bound_check_16bytes = total_size_new - 16; +#endif + return true; } #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index bbdf9e866..581b80ee3 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -26,12 +26,25 @@ struct WASMMemoryInstance { uint32 module_type; /* Shared memory flag */ bool is_shared; + /* Number bytes per page */ uint32 num_bytes_per_page; /* Current page count */ uint32 cur_page_count; /* Maximum page count */ uint32 max_page_count; + /* Memory data size */ + uint32 memory_data_size; + + /** + * Memory data begin address, Note: + * the app-heap might be inserted in to the linear memory, + * when memory is re-allocated, the heap data and memory data + * must be copied to new memory also + */ + uint8 *memory_data; + /* Memory data end address */ + uint8 *memory_data_end; /* Heap data base address */ uint8 *heap_data; @@ -45,14 +58,6 @@ struct WASMMemoryInstance { korp_mutex mem_lock; #endif - /* Memory data end address */ - uint8 *memory_data_end; - - /* Memory data begin address, the layout is: memory data + heap data - Note: when memory is re-allocated, the heap data and memory data - must be copied to new memory also. */ - uint8 *memory_data; - #if WASM_ENABLE_FAST_JIT != 0 #if UINTPTR_MAX == UINT64_MAX uint64 mem_bound_check_1byte; From 28d9fb60d20567768a96769d15d0b34025718394 Mon Sep 17 00:00:00 2001 From: Xu Jun <693788454@qq.com> Date: Tue, 23 Aug 2022 17:38:55 +0800 Subject: [PATCH 03/41] Add missing symbols for arc aot relocation (#1408) --- core/iwasm/aot/arch/aot_reloc_arc.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/iwasm/aot/arch/aot_reloc_arc.c b/core/iwasm/aot/arch/aot_reloc_arc.c index 031d4b99b..332962998 100644 --- a/core/iwasm/aot/arch/aot_reloc_arc.c +++ b/core/iwasm/aot/arch/aot_reloc_arc.c @@ -53,6 +53,17 @@ void __subsf3(); void __truncdfsf2(); void __floatunsisf(); void __fixunsdfsi(); +void __floatdisf(); +void __floatdidf(); +void __fixdfdi(); +void __ltsf2(); +void __gesf2(); +void __eqdf2(); +void __nedf2(); +void __ltsf2(); +void __nesf2(); +void __unordsf2(); +void __fixunssfsi(); #else void __ac_push_13_to_13(); void __ac_push_13_to_14(); @@ -162,6 +173,17 @@ static SymbolMap target_sym_map[] = { REG_SYM(__truncdfsf2), REG_SYM(__floatunsisf), REG_SYM(__fixunsdfsi), + REG_SYM(__floatdisf), + REG_SYM(__floatdidf), + REG_SYM(__fixdfdi), + REG_SYM(__ltsf2), + REG_SYM(__gesf2), + REG_SYM(__eqdf2), + REG_SYM(__nedf2), + REG_SYM(__ltsf2), + REG_SYM(__nesf2), + REG_SYM(__unordsf2), + REG_SYM(__fixunssfsi), #else REG_SYM(__ac_push_13_to_13), REG_SYM(__ac_push_13_to_14), From f5283399ec0d3ff15d484238cf0a376005317754 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 24 Aug 2022 13:15:37 +0900 Subject: [PATCH 04/41] debug: Retire wasm_debug_(get|set)_engine_active mechanism (#1404) They are no longer necessary because we no longer create debug instances automatically for exec env at this layer. --- core/iwasm/interpreter/wasm_runtime.c | 26 ------------------- .../libraries/debug-engine/debug_engine.c | 21 +-------------- .../libraries/debug-engine/debug_engine.h | 6 ----- 3 files changed, 1 insertion(+), 52 deletions(-) diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index b0f9a3ea3..7f3019825 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2181,27 +2181,11 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size, addr = mem_allocator_malloc(memory->heap_handle, size); } else if (module_inst->malloc_function && module_inst->free_function) { -#if WASM_ENABLE_DEBUG_INTERP != 0 - /* TODO: obviously, we can not create debug instance for - * module malloc here, so, just disable the engine here, - * it is strange, but we now are lack of ways to indicate - * which calls should not be debugged. And we have other - * execute_xxx_function may need to be taken care of - */ - bool active = wasm_debug_get_engine_active(); - wasm_debug_set_engine_active(false); -#endif if (!execute_malloc_function(module_inst, module_inst->malloc_function, module_inst->retain_function, size, &offset)) { -#if WASM_ENABLE_DEBUG_INTERP != 0 - wasm_debug_set_engine_active(active); -#endif return 0; } -#if WASM_ENABLE_DEBUG_INTERP != 0 - wasm_debug_set_engine_active(active); -#endif /* If we use app's malloc function, the default memory may be changed while memory growing */ memory = module_inst->default_memory; @@ -2280,17 +2264,7 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr) else if (module_inst->malloc_function && module_inst->free_function && memory->memory_data <= addr && addr < memory->memory_data_end) { -#if WASM_ENABLE_DEBUG_INTERP != 0 - /*TODO: obviously, we can not create debug instance for module - malloc here, so, just disable the engine here, it is strange. the - wasm's call should be marshed to its own thread */ - bool active = wasm_debug_get_engine_active(); - wasm_debug_set_engine_active(false); -#endif execute_free_function(module_inst, module_inst->free_function, ptr); -#if WASM_ENABLE_DEBUG_INTERP != 0 - wasm_debug_set_engine_active(active); -#endif } } } diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index 1852d05db..d0d85e43b 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -21,7 +21,6 @@ typedef struct WASMDebugEngine { int32 process_base_port; bh_list debug_instance_list; korp_mutex instance_list_lock; - bool active; } WASMDebugEngine; void @@ -313,7 +312,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port) else snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr), "%s", "127.0.0.1"); - g_debug_engine->active = true; } else { wasm_debug_handler_deinit(); @@ -322,23 +320,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port) return g_debug_engine != NULL ? true : false; } -void -wasm_debug_set_engine_active(bool active) -{ - if (g_debug_engine) { - g_debug_engine->active = active; - } -} - -bool -wasm_debug_get_engine_active(void) -{ - if (g_debug_engine) { - return g_debug_engine->active; - } - return false; -} - /* A debug Instance is a debug "process" in gdb remote protocol and bound to a runtime cluster */ WASMDebugInstance * @@ -348,7 +329,7 @@ wasm_debug_instance_create(WASMCluster *cluster) WASMExecEnv *exec_env = NULL; wasm_module_inst_t module_inst = NULL; - if (!g_debug_engine || !g_debug_engine->active) { + if (!g_debug_engine) { return NULL; } diff --git a/core/iwasm/libraries/debug-engine/debug_engine.h b/core/iwasm/libraries/debug-engine/debug_engine.h index 361f02e41..7c159a040 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.h +++ b/core/iwasm/libraries/debug-engine/debug_engine.h @@ -122,12 +122,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port); void wasm_debug_engine_destroy(); -void -wasm_debug_set_engine_active(bool active); - -bool -wasm_debug_get_engine_active(void); - WASMExecEnv * wasm_debug_instance_get_current_env(WASMDebugInstance *instance); From 08eeeb748c47bf9594bd6346657cbc4c6f129113 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 24 Aug 2022 14:05:51 +0800 Subject: [PATCH 05/41] Add more fast jit fixed virtual regs (#1409) Add fast jit fixed virtual register import_func_ptrs_reg and func_type_indexes_reg for the preparation of further optimizations. --- core/iwasm/fast-jit/jit_frontend.c | 34 ++++++++++++++++++ core/iwasm/fast-jit/jit_frontend.h | 6 ++++ core/iwasm/fast-jit/jit_ir.h | 8 +++++ core/iwasm/interpreter/wasm_runtime.c | 50 +++++++++++++++++++++++++++ core/iwasm/interpreter/wasm_runtime.h | 1 + 5 files changed, 99 insertions(+) diff --git a/core/iwasm/fast-jit/jit_frontend.c b/core/iwasm/fast-jit/jit_frontend.c index 117df9381..124a5f553 100644 --- a/core/iwasm/fast-jit/jit_frontend.c +++ b/core/iwasm/fast-jit/jit_frontend.c @@ -70,6 +70,21 @@ get_module_reg(JitFrame *frame) return frame->module_reg; } +JitReg +get_import_func_ptrs_reg(JitFrame *frame) +{ + JitCompContext *cc = frame->cc; + JitReg module_inst_reg = get_module_inst_reg(frame); + + if (!frame->import_func_ptrs_reg) { + frame->import_func_ptrs_reg = cc->import_func_ptrs_reg; + GEN_INSN( + LDPTR, frame->import_func_ptrs_reg, module_inst_reg, + NEW_CONST(I32, offsetof(WASMModuleInstance, import_func_ptrs))); + } + return frame->import_func_ptrs_reg; +} + JitReg get_fast_jit_func_ptrs_reg(JitFrame *frame) { @@ -85,6 +100,21 @@ get_fast_jit_func_ptrs_reg(JitFrame *frame) return frame->fast_jit_func_ptrs_reg; } +JitReg +get_func_type_indexes_reg(JitFrame *frame) +{ + JitCompContext *cc = frame->cc; + JitReg module_inst_reg = get_module_inst_reg(frame); + + if (!frame->func_type_indexes_reg) { + frame->func_type_indexes_reg = cc->func_type_indexes_reg; + GEN_INSN( + LDPTR, frame->func_type_indexes_reg, module_inst_reg, + NEW_CONST(I32, offsetof(WASMModuleInstance, func_type_indexes))); + } + return frame->func_type_indexes_reg; +} + JitReg get_global_data_reg(JitFrame *frame) { @@ -376,7 +406,9 @@ clear_fixed_virtual_regs(JitFrame *frame) frame->module_inst_reg = 0; frame->module_reg = 0; + frame->import_func_ptrs_reg = 0; frame->fast_jit_func_ptrs_reg = 0; + frame->func_type_indexes_reg = 0; frame->global_data_reg = 0; frame->aux_stack_bound_reg = 0; frame->aux_stack_bottom_reg = 0; @@ -572,7 +604,9 @@ create_fixed_virtual_regs(JitCompContext *cc) cc->module_inst_reg = jit_cc_new_reg_ptr(cc); cc->module_reg = jit_cc_new_reg_ptr(cc); + cc->import_func_ptrs_reg = jit_cc_new_reg_ptr(cc); cc->fast_jit_func_ptrs_reg = jit_cc_new_reg_ptr(cc); + cc->func_type_indexes_reg = jit_cc_new_reg_ptr(cc); cc->global_data_reg = jit_cc_new_reg_ptr(cc); cc->aux_stack_bound_reg = jit_cc_new_reg_I32(cc); cc->aux_stack_bottom_reg = jit_cc_new_reg_I32(cc); diff --git a/core/iwasm/fast-jit/jit_frontend.h b/core/iwasm/fast-jit/jit_frontend.h index adb7ef4e4..a31fbc5ae 100644 --- a/core/iwasm/fast-jit/jit_frontend.h +++ b/core/iwasm/fast-jit/jit_frontend.h @@ -187,9 +187,15 @@ get_module_inst_reg(JitFrame *frame); JitReg get_module_reg(JitFrame *frame); +JitReg +get_import_func_ptrs_reg(JitFrame *frame); + JitReg get_fast_jit_func_ptrs_reg(JitFrame *frame); +JitReg +get_func_type_indexes_reg(JitFrame *frame); + JitReg get_global_data_reg(JitFrame *frame); diff --git a/core/iwasm/fast-jit/jit_ir.h b/core/iwasm/fast-jit/jit_ir.h index 7dc453fed..b00694fc0 100644 --- a/core/iwasm/fast-jit/jit_ir.h +++ b/core/iwasm/fast-jit/jit_ir.h @@ -909,8 +909,12 @@ typedef struct JitFrame { JitReg module_inst_reg; /* WASM module */ JitReg module_reg; + /* module_inst->import_func_ptrs */ + JitReg import_func_ptrs_reg; /* module_inst->fast_jit_func_ptrs */ JitReg fast_jit_func_ptrs_reg; + /* module_inst->func_type_indexes */ + JitReg func_type_indexes_reg; /* Base address of global data */ JitReg global_data_reg; /* Boundary of auxiliary stack */ @@ -1027,8 +1031,12 @@ typedef struct JitCompContext { JitReg module_inst_reg; /* WASM module */ JitReg module_reg; + /* module_inst->import_func_ptrs */ + JitReg import_func_ptrs_reg; /* module_inst->fast_jit_func_ptrs */ JitReg fast_jit_func_ptrs_reg; + /* module_inst->func_type_indexes */ + JitReg func_type_indexes_reg; /* Base address of global data */ JitReg global_data_reg; /* Boundary of auxiliary stack */ diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 7f3019825..065ef1139 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1262,6 +1262,47 @@ check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf, return true; } +#if WASM_ENABLE_FAST_JIT != 0 +static uint32 +get_smallest_type_idx(WASMModule *module, WASMType *func_type) +{ + uint32 i; + + for (i = 0; i < module->type_count; i++) { + if (func_type == module->types[i]) + return i; + } + + bh_assert(0); + return -1; +} + +static bool +init_func_type_indexes(WASMModuleInstance *module_inst, char *error_buf, + uint32 error_buf_size) +{ + uint32 i; + uint64 total_size = (uint64)sizeof(uint32) * module_inst->function_count; + + /* Allocate memory */ + if (!(module_inst->func_type_indexes = + runtime_malloc(total_size, error_buf, error_buf_size))) { + return false; + } + + for (i = 0; i < module_inst->function_count; i++) { + WASMFunctionInstance *func_inst = module_inst->functions + i; + WASMType *func_type = func_inst->is_import_func + ? func_inst->u.func_import->func_type + : func_inst->u.func->func_type; + module_inst->func_type_indexes[i] = + get_smallest_type_idx(module_inst->module, func_type); + } + + return true; +} +#endif + /** * Instantiate module */ @@ -1384,6 +1425,10 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size, && !(module_inst->export_globals = export_globals_instantiate( module, module_inst, module_inst->export_glob_count, error_buf, error_buf_size))) +#endif +#if WASM_ENABLE_FAST_JIT != 0 + || (module_inst->function_count > 0 + && !init_func_type_indexes(module_inst, error_buf, error_buf_size)) #endif ) { goto fail; @@ -1712,6 +1757,11 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) if (!module_inst) return; +#if WASM_ENABLE_FAST_JIT != 0 + if (module_inst->func_type_indexes) + wasm_runtime_free(module_inst->func_type_indexes); +#endif + #if WASM_ENABLE_MULTI_MODULE != 0 sub_module_deinstantiate(module_inst); #endif diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 581b80ee3..4cdbf5bad 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -191,6 +191,7 @@ struct WASMModuleInstance { #if WASM_ENABLE_FAST_JIT != 0 /* point to JITed functions */ void **fast_jit_func_ptrs; + uint32 *func_type_indexes; #endif WASMMemoryInstance **memories; From d41543f8299e6484fbba4f5874d3713e7476ca29 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 24 Aug 2022 16:57:03 +0900 Subject: [PATCH 06/41] cmake: Use CMAKE_BINARY_DIR instead of ${CMAKE_CURRENT_SOURCE_DIR}/build (#1410) --- product-mini/platforms/android/CMakeLists.txt | 2 +- samples/multi-module/CMakeLists.txt | 10 +++++----- samples/socket-api/CMakeLists.txt | 6 +++--- samples/workload/XNNPACK/CMakeLists.txt | 2 +- samples/workload/bwa/CMakeLists.txt | 2 +- samples/workload/meshoptimizer/CMakeLists.txt | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/product-mini/platforms/android/CMakeLists.txt b/product-mini/platforms/android/CMakeLists.txt index 22db06b03..a65b18089 100644 --- a/product-mini/platforms/android/CMakeLists.txt +++ b/product-mini/platforms/android/CMakeLists.txt @@ -104,7 +104,7 @@ else() target_link_libraries (iwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -landroid -llog) endif() -set (distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build/distribution) +set (distribution_DIR ${CMAKE_BINARY_DIR}/distribution) set_target_properties (iwasm PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${distribution_DIR}/wasm/lib") add_custom_command (TARGET iwasm POST_BUILD diff --git a/samples/multi-module/CMakeLists.txt b/samples/multi-module/CMakeLists.txt index 26c99d828..b57df2cb4 100644 --- a/samples/multi-module/CMakeLists.txt +++ b/samples/multi-module/CMakeLists.txt @@ -135,11 +135,11 @@ ExternalProject_Add(WASM_MODULE -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps BUILD_COMMAND ${CMAKE_COMMAND} --build . INSTALL_COMMAND ${CMAKE_COMMAND} -E copy - ./mA.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/ - ./mB.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/ - ./mC.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/ - ./mD.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/ - ./mE.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/ + ./mA.wasm ${CMAKE_BINARY_DIR} + ./mB.wasm ${CMAKE_BINARY_DIR} + ./mC.wasm ${CMAKE_BINARY_DIR} + ./mD.wasm ${CMAKE_BINARY_DIR} + ./mE.wasm ${CMAKE_BINARY_DIR} ) ################ NATIVE diff --git a/samples/socket-api/CMakeLists.txt b/samples/socket-api/CMakeLists.txt index 5ce2543b9..2508ac7cf 100644 --- a/samples/socket-api/CMakeLists.txt +++ b/samples/socket-api/CMakeLists.txt @@ -87,9 +87,9 @@ ExternalProject_Add(wasm-app ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src BUILD_COMMAND ${CMAKE_COMMAND} --build . INSTALL_COMMAND ${CMAKE_COMMAND} -E copy - tcp_client.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build - tcp_server.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build - send_recv.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build + tcp_client.wasm ${CMAKE_BINARY_DIR} + tcp_server.wasm ${CMAKE_BINARY_DIR} + send_recv.wasm ${CMAKE_BINARY_DIR} ) add_executable(tcp_server ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/tcp_server.c) diff --git a/samples/workload/XNNPACK/CMakeLists.txt b/samples/workload/XNNPACK/CMakeLists.txt index 1be033268..3f67367a7 100644 --- a/samples/workload/XNNPACK/CMakeLists.txt +++ b/samples/workload/XNNPACK/CMakeLists.txt @@ -90,5 +90,5 @@ ExternalProject_Add(xnnpack //:f32_sqrt_ulp_eval.wasm INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/xnnpack/bazel-out/wasm-opt/bin/ - ${CMAKE_CURRENT_SOURCE_DIR}/build/wasm-opt + ${CMAKE_BINARY_DIR}/wasm-opt ) diff --git a/samples/workload/bwa/CMakeLists.txt b/samples/workload/bwa/CMakeLists.txt index 9ea6704b3..7f7e31825 100644 --- a/samples/workload/bwa/CMakeLists.txt +++ b/samples/workload/bwa/CMakeLists.txt @@ -42,5 +42,5 @@ ExternalProject_Add(bwa -DCMAKE_SYSROOT=${WASI_SDK_HOME}/share/wasi-sysroot ${CMAKE_CURRENT_SOURCE_DIR}/bwa BUILD_COMMAND make bwa_wasm_opt - INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./bwa.opt.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/bwa.wasm + INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./bwa.opt.wasm ${CMAKE_BINARY_DIR}/bwa.wasm ) diff --git a/samples/workload/meshoptimizer/CMakeLists.txt b/samples/workload/meshoptimizer/CMakeLists.txt index 97f98fa3a..172de8d67 100644 --- a/samples/workload/meshoptimizer/CMakeLists.txt +++ b/samples/workload/meshoptimizer/CMakeLists.txt @@ -26,5 +26,5 @@ ExternalProject_Add(codecbench -DCMAKE_SYSROOT=${WASI_SDK_HOME}/share/wasi-sysroot ${CMAKE_CURRENT_SOURCE_DIR}/meshoptimizer BUILD_COMMAND make codecbench - INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./codecbench.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/codecbench.wasm + INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./codecbench.wasm ${CMAKE_BINARY_DIR}/codecbench.wasm ) From 5586f7b82d98b30d6df70913879cd780e0bcc442 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 24 Aug 2022 17:39:32 +0900 Subject: [PATCH 07/41] source debug: Remove unused platform_port (#1403) --- core/iwasm/common/wasm_runtime_common.c | 1 - core/iwasm/include/wasm_export.h | 2 +- core/iwasm/libraries/debug-engine/debug_engine.c | 5 +---- core/iwasm/libraries/debug-engine/debug_engine.h | 2 +- product-mini/platforms/posix/main.c | 2 -- product-mini/platforms/windows/main.c | 2 -- 6 files changed, 3 insertions(+), 11 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 7562877c4..1230b465a 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -408,7 +408,6 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args) #if WASM_ENABLE_DEBUG_INTERP != 0 if (strlen(init_args->ip_addr)) if (!wasm_debug_engine_init(init_args->ip_addr, - init_args->platform_port, init_args->instance_port)) { wasm_runtime_destroy(); return false; diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 9c1b3afc3..da388c850 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -137,7 +137,7 @@ typedef struct RuntimeInitArgs { /* Debug settings, only used when WASM_ENABLE_DEBUG_INTERP != 0 */ char ip_addr[128]; - int platform_port; + int unused; /* was platform_port */ int instance_port; /* Fast JIT code cache size */ diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index d0d85e43b..36adb7538 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -17,7 +17,6 @@ typedef struct WASMDebugEngine { struct WASMDebugEngine *next; WASMDebugControlThread *control_thread; char ip_addr[128]; - int32 platform_port; int32 process_base_port; bh_list debug_instance_list; korp_mutex instance_list_lock; @@ -290,7 +289,7 @@ wasm_debug_engine_destroy() } bool -wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port) +wasm_debug_engine_init(char *ip_addr, int32 process_port) { if (wasm_debug_handler_init() != 0) { return false; @@ -302,8 +301,6 @@ wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port) if (g_debug_engine) { process_port -= 1; - g_debug_engine->platform_port = - platform_port > 0 ? platform_port : 1234; g_debug_engine->process_base_port = (process_port > 0) ? process_port : 0; if (ip_addr) diff --git a/core/iwasm/libraries/debug-engine/debug_engine.h b/core/iwasm/libraries/debug-engine/debug_engine.h index 7c159a040..939d75f91 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.h +++ b/core/iwasm/libraries/debug-engine/debug_engine.h @@ -117,7 +117,7 @@ WASMDebugInstance * wasm_exec_env_get_instance(WASMExecEnv *exec_env); bool -wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port); +wasm_debug_engine_init(char *ip_addr, int32 process_port); void wasm_debug_engine_destroy(); diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 9d544e262..7f3252761 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -329,7 +329,6 @@ main(int argc, char *argv[]) #endif #if WASM_ENABLE_DEBUG_INTERP != 0 char *ip_addr = NULL; - /* int platform_port = 0; */ int instance_port = 0; #endif @@ -497,7 +496,6 @@ main(int argc, char *argv[]) #endif #if WASM_ENABLE_DEBUG_INTERP != 0 - init_args.platform_port = 0; init_args.instance_port = instance_port; if (ip_addr) strcpy(init_args.ip_addr, ip_addr); diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index 6d053c7a3..265cf2c8a 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -247,7 +247,6 @@ main(int argc, char *argv[]) #endif #if WASM_ENABLE_DEBUG_INTERP != 0 char *ip_addr = NULL; - /* int platform_port = 0; */ int instance_port = 0; #endif @@ -371,7 +370,6 @@ main(int argc, char *argv[]) #endif #if WASM_ENABLE_DEBUG_INTERP != 0 - init_args.platform_port = 0; init_args.instance_port = instance_port; if (ip_addr) strcpy(init_args.ip_addr, ip_addr); From 1dcc59f234663b63b8a01d79621a76a3940c5839 Mon Sep 17 00:00:00 2001 From: Qiang <56512053+FromLiQg@users.noreply.github.com> Date: Wed, 24 Aug 2022 17:56:38 +0800 Subject: [PATCH 08/41] Fix fast jit destroy entry/exit basic block issue in jit_cc_destroy (#1401) The entry and exit basic blocks might be destroyed before they are created. Found by instrument test. Add checks to fix the issue. --- core/iwasm/fast-jit/jit_ir.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/iwasm/fast-jit/jit_ir.c b/core/iwasm/fast-jit/jit_ir.c index 2f78bb165..313152733 100644 --- a/core/iwasm/fast-jit/jit_ir.c +++ b/core/iwasm/fast-jit/jit_ir.c @@ -474,8 +474,10 @@ jit_cc_destroy(JitCompContext *cc) } /* Release entry and exit blocks. */ - jit_basic_block_delete(jit_cc_entry_basic_block(cc)); - jit_basic_block_delete(jit_cc_exit_basic_block(cc)); + if (0 != cc->entry_label) + jit_basic_block_delete(jit_cc_entry_basic_block(cc)); + if (0 != cc->exit_label) + jit_basic_block_delete(jit_cc_exit_basic_block(cc)); /* clang-format off */ /* Release blocks and instructions. */ From ebcbe0b571b48e230f3b06ceae787db8607ca02c Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 24 Aug 2022 21:50:26 +0900 Subject: [PATCH 09/41] wasm_application.c: Do not start debug instance automatically (#1407) Leave the control from wasm_application_execute_main to host embedders, eg. product-mini. --- core/iwasm/common/wasm_application.c | 11 ----------- product-mini/platforms/posix/main.c | 20 ++++++++++++++++++++ product-mini/platforms/windows/main.c | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index a4fe5accb..2d7eacb49 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -107,9 +107,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[]) the actual main function. Directly calling main function may cause exception thrown. */ if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) { -#if WASM_ENABLE_DEBUG_INTERP != 0 - wasm_runtime_start_debug_instance(exec_env); -#endif return wasm_runtime_call_wasm(exec_env, func, 0, NULL); } #endif /* end of WASM_ENABLE_LIBC_WASI */ @@ -191,10 +188,6 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[]) (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets); } -#if WASM_ENABLE_DEBUG_INTERP != 0 - wasm_runtime_start_debug_instance(exec_env); -#endif - ret = wasm_runtime_call_wasm(exec_env, func, argc1, argv1); if (ret && func_type->result_count > 0 && argc > 0 && argv) /* copy the return value */ @@ -618,10 +611,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name, goto fail; } -#if WASM_ENABLE_DEBUG_INTERP != 0 - wasm_runtime_start_debug_instance(exec_env); -#endif - if (!wasm_runtime_call_wasm(exec_env, target_func, argc1, argv1)) { goto fail; } diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 7f3252761..641e12a3d 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -568,6 +568,23 @@ main(int argc, char *argv[]) goto fail3; } +#if WASM_ENABLE_DEBUG_INTERP != 0 + if (ip_addr != NULL) { + wasm_exec_env_t exec_env = + wasm_runtime_get_exec_env_singleton(wasm_module_inst); + uint32_t debug_port; + if (exec_env == NULL) { + printf("%s\n", wasm_runtime_get_exception(wasm_module_inst)); + goto fail4; + } + debug_port = wasm_runtime_start_debug_instance(exec_env); + if (debug_port == 0) { + printf("Failed to start debug instance\n"); + goto fail4; + } + } +#endif + if (is_repl_mode) app_instance_repl(wasm_module_inst); else if (func_name) @@ -577,6 +594,9 @@ main(int argc, char *argv[]) ret = 0; +#if WASM_ENABLE_DEBUG_INTERP != 0 +fail4: +#endif /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index 265cf2c8a..b9afae494 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -435,6 +435,23 @@ main(int argc, char *argv[]) goto fail3; } +#if WASM_ENABLE_DEBUG_INTERP != 0 + if (ip_addr != NULL) { + wasm_exec_env_t exec_env = + wasm_runtime_get_exec_env_singleton(wasm_module_inst); + uint32_t debug_port; + if (exec_env == NULL) { + printf("%s\n", wasm_runtime_get_exception(wasm_module_inst)); + goto fail4; + } + debug_port = wasm_runtime_start_debug_instance(exec_env); + if (debug_port == 0) { + printf("Failed to start debug instance\n"); + goto fail4; + } + } +#endif + if (is_repl_mode) app_instance_repl(wasm_module_inst); else if (func_name) @@ -444,6 +461,9 @@ main(int argc, char *argv[]) ret = 0; +#if WASM_ENABLE_DEBUG_INTERP != 0 +fail4: +#endif /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); From 903a27aff208200311118d41952ad526a68815aa Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 24 Aug 2022 22:00:16 +0800 Subject: [PATCH 10/41] Fix fast jit destroy block issue (#1412) Only destroy the block if it hasn't been pushed into the block stack, or it will be destroyed again when destroying the block stack. --- core/iwasm/fast-jit/fe/jit_emit_control.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/iwasm/fast-jit/fe/jit_emit_control.c b/core/iwasm/fast-jit/fe/jit_emit_control.c index e9200e781..eafce6749 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_control.c +++ b/core/iwasm/fast-jit/fe/jit_emit_control.c @@ -807,7 +807,11 @@ jit_compile_op_block(JitCompContext *cc, uint8 **p_frame_ip, return true; fail: - jit_block_destroy(block); + /* Only destroy the block if it hasn't been pushed into + the block stack, or if will be destroyed again when + destroying the block stack */ + if (jit_block_stack_top(&cc->block_stack) != block) + jit_block_destroy(block); return false; } From 7c75f6cf637022bdb934a5d61f2bf739c2fe5606 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 25 Aug 2022 20:29:26 +0900 Subject: [PATCH 11/41] wasm_debug_engine_create: Remove unused comment (#1416) --- core/iwasm/libraries/debug-engine/debug_engine.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index 36adb7538..f1301295f 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -263,16 +263,6 @@ wasm_debug_engine_create() /* reset current instance id */ current_instance_id = 1; - /* TODO: support Wasm platform in LLDB */ - /* - engine->control_thread = - wasm_debug_control_thread_create((WASMDebugObject *)engine); - engine->control_thread->debug_engine = (WASMDebugObject *)engine; - engine->control_thread->debug_instance = NULL; - sprintf(engine->control_thread->ip_addr, "127.0.0.1"); - engine->control_thread->port = 1234; - */ - bh_list_init(&engine->debug_instance_list); return engine; } From ee98b583d58f70c64eb8b4134f2be0785b38ec20 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 25 Aug 2022 21:03:44 +0900 Subject: [PATCH 12/41] debug-engine: Fix process_port handling (#1415) process_port=1 means "use port 1" --- core/iwasm/libraries/debug-engine/debug_engine.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index f1301295f..489a60a95 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -82,7 +82,7 @@ control_thread_routine(void *arg) control_thread->port = (g_debug_engine->process_base_port == 0) ? 0 - : g_debug_engine->process_base_port + debug_inst->id; + : g_debug_engine->process_base_port + debug_inst->id - 1; LOG_WARNING("control thread of debug object %p start\n", debug_inst); @@ -290,7 +290,6 @@ wasm_debug_engine_init(char *ip_addr, int32 process_port) } if (g_debug_engine) { - process_port -= 1; g_debug_engine->process_base_port = (process_port > 0) ? process_port : 0; if (ip_addr) From 3875c6649a3df14b87de2c9e86447176cf4c4c46 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 26 Aug 2022 17:33:47 +0900 Subject: [PATCH 13/41] wasm_runtime_start_debug_instance: Allow to override port (#1421) Allow the embedder to manage port number for this purpose by itself. --- core/iwasm/common/wasm_runtime_common.c | 10 ++++++++-- core/iwasm/common/wasm_runtime_common.h | 5 +++++ core/iwasm/include/wasm_export.h | 9 +++++++++ .../libraries/debug-engine/debug_engine.c | 18 +++++++++++------- .../libraries/debug-engine/debug_engine.h | 2 +- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 1230b465a..f26029595 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -507,7 +507,7 @@ wasm_runtime_is_xip_file(const uint8 *buf, uint32 size) #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0) uint32 -wasm_runtime_start_debug_instance(WASMExecEnv *exec_env) +wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port) { WASMModuleInstanceCommon *module_inst = wasm_runtime_get_module_inst(exec_env); @@ -525,12 +525,18 @@ wasm_runtime_start_debug_instance(WASMExecEnv *exec_env) return cluster->debug_inst->control_thread->port; } - if (wasm_debug_instance_create(cluster)) { + if (wasm_debug_instance_create(cluster, port)) { return cluster->debug_inst->control_thread->port; } return 0; } + +uint32 +wasm_runtime_start_debug_instance(WASMExecEnv *exec_env) +{ + return wasm_runtime_start_debug_instance_with_port(exec_env, -1); +} #endif #if WASM_ENABLE_MULTI_MODULE != 0 diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 50304f29c..326dc003c 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -563,6 +563,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env, uint32 num_args, ...); #if WASM_ENABLE_DEBUG_INTERP != 0 +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN uint32 +wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, + int32_t port); + /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN uint32 wasm_runtime_start_debug_instance(WASMExecEnv *exec_env); diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index da388c850..4beb4d55c 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -516,10 +516,19 @@ wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst); * they are sharing the same cluster with the main exec_env. * * @param exec_env the execution environment to start debug instance + * @param port the port for the debug server to listen on. + * 0 means automatic assignment. + * -1 means to use the global setting in RuntimeInitArgs. * * @return debug port if success, 0 otherwise. */ WASM_RUNTIME_API_EXTERN uint32_t +wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port); + +/** + * Same as wasm_runtime_start_debug_instance_with_port(env, -1). + */ +WASM_RUNTIME_API_EXTERN uint32_t wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env); /** diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index 489a60a95..8ea85e7f3 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -79,10 +79,12 @@ control_thread_routine(void *arg) control_thread->debug_instance = debug_inst; bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr), g_debug_engine->ip_addr); - control_thread->port = - (g_debug_engine->process_base_port == 0) - ? 0 - : g_debug_engine->process_base_port + debug_inst->id - 1; + if (control_thread->port == -1) { + control_thread->port = + (g_debug_engine->process_base_port == 0) + ? 0 + : g_debug_engine->process_base_port + debug_inst->id - 1; + } LOG_WARNING("control thread of debug object %p start\n", debug_inst); @@ -91,6 +93,7 @@ control_thread_routine(void *arg) if (!control_thread->server) { LOG_ERROR("Failed to create debug server\n"); + control_thread->port = 0; os_cond_signal(&debug_inst->wait_cond); os_mutex_unlock(&debug_inst->wait_lock); return NULL; @@ -176,7 +179,7 @@ control_thread_routine(void *arg) } static WASMDebugControlThread * -wasm_debug_control_thread_create(WASMDebugInstance *debug_instance) +wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port) { WASMDebugControlThread *control_thread; @@ -186,6 +189,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance) return NULL; } memset(control_thread, 0, sizeof(WASMDebugControlThread)); + control_thread->port = port; if (os_mutex_init(&control_thread->wait_lock) != 0) goto fail; @@ -309,7 +313,7 @@ wasm_debug_engine_init(char *ip_addr, int32 process_port) /* A debug Instance is a debug "process" in gdb remote protocol and bound to a runtime cluster */ WASMDebugInstance * -wasm_debug_instance_create(WASMCluster *cluster) +wasm_debug_instance_create(WASMCluster *cluster, int32 port) { WASMDebugInstance *instance; WASMExecEnv *exec_env = NULL; @@ -359,7 +363,7 @@ wasm_debug_instance_create(WASMCluster *cluster) } instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset; - if (!wasm_debug_control_thread_create(instance)) { + if (!wasm_debug_control_thread_create(instance, port)) { LOG_ERROR("WASM Debug Engine error: failed to create control thread"); goto fail3; } diff --git a/core/iwasm/libraries/debug-engine/debug_engine.h b/core/iwasm/libraries/debug-engine/debug_engine.h index 939d75f91..70901551e 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.h +++ b/core/iwasm/libraries/debug-engine/debug_engine.h @@ -108,7 +108,7 @@ void on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env); WASMDebugInstance * -wasm_debug_instance_create(WASMCluster *cluster); +wasm_debug_instance_create(WASMCluster *cluster, int32 port); void wasm_debug_instance_destroy(WASMCluster *cluster); From 04c1eb30cf032e127c0a62247d8132908dc6a40b Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 26 Aug 2022 20:49:57 +0900 Subject: [PATCH 14/41] debug-engine: allocate "tmpbuf" dynamically (#1423) Some configurations (eg. esp32/nuttx) have limited space for BSS, 0x20000 byte buffer is huge on embedded systems, change to allocate the buffer dynamically. --- core/iwasm/libraries/debug-engine/handler.c | 73 ++++++++++++--------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/core/iwasm/libraries/debug-engine/handler.c b/core/iwasm/libraries/debug-engine/handler.c index 4623f21d4..9b6009bf6 100644 --- a/core/iwasm/libraries/debug-engine/handler.c +++ b/core/iwasm/libraries/debug-engine/handler.c @@ -11,18 +11,31 @@ #include "wasm_runtime.h" #define MAX_PACKET_SIZE (0x20000) -static char tmpbuf[MAX_PACKET_SIZE]; +static char *tmpbuf; static korp_mutex tmpbuf_lock; int wasm_debug_handler_init() { - return os_mutex_init(&tmpbuf_lock); + int ret; + tmpbuf = wasm_runtime_malloc(MAX_PACKET_SIZE); + if (tmpbuf == NULL) { + LOG_ERROR("debug-engine: Packet buffer allocation failure"); + return BHT_ERROR; + } + ret = os_mutex_init(&tmpbuf_lock); + if (ret != BHT_OK) { + wasm_runtime_free(tmpbuf); + tmpbuf = NULL; + } + return ret; } void wasm_debug_handler_deinit() { + wasm_runtime_free(tmpbuf); + tmpbuf = NULL; os_mutex_destroy(&tmpbuf_lock); } @@ -78,12 +91,12 @@ process_xfer(WASMGDBServer *server, const char *name, char *args) char objname[128]; wasm_debug_instance_get_current_object_name( (WASMDebugInstance *)server->thread->debug_instance, objname, 128); - snprintf(tmpbuf, sizeof(tmpbuf), + snprintf(tmpbuf, MAX_PACKET_SIZE, "l
", objname, addr); #else - snprintf(tmpbuf, sizeof(tmpbuf), + snprintf(tmpbuf, MAX_PACKET_SIZE, "l
", "nobody.wasm", addr); @@ -103,7 +116,7 @@ process_wasm_local(WASMGDBServer *server, char *args) bool ret; os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "E01"); + snprintf(tmpbuf, MAX_PACKET_SIZE, "E01"); if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &local_index) == 2) { ret = wasm_debug_instance_get_local( (WASMDebugInstance *)server->thread->debug_instance, frame_index, @@ -126,7 +139,7 @@ process_wasm_global(WASMGDBServer *server, char *args) bool ret; os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "E01"); + snprintf(tmpbuf, MAX_PACKET_SIZE, "E01"); if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &global_index) == 2) { ret = wasm_debug_instance_get_global( @@ -161,14 +174,14 @@ handle_general_query(WASMGDBServer *server, char *payload) (WASMDebugInstance *)server->thread->debug_instance); os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "QCp%" PRIx64 ".%" PRIx64 "", pid, + snprintf(tmpbuf, MAX_PACKET_SIZE, "QCp%" PRIx64 ".%" PRIx64 "", pid, tid); write_packet(server, tmpbuf); os_mutex_unlock(&tmpbuf_lock); } if (!strcmp(name, "Supported")) { os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), + snprintf(tmpbuf, MAX_PACKET_SIZE, "qXfer:libraries:read+;PacketSize=%" PRIx32 ";", MAX_PACKET_SIZE); write_packet(server, tmpbuf); @@ -196,7 +209,7 @@ handle_general_query(WASMGDBServer *server, char *payload) strlen("wasm32-wamr-wasi-wasm")); os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), + snprintf(tmpbuf, MAX_PACKET_SIZE, "vendor:wamr;ostype:wasi;arch:wasm32;" "triple:%s;endian:little;ptrsize:4;", triple); @@ -227,7 +240,7 @@ handle_general_query(WASMGDBServer *server, char *payload) strlen("wasm32-wamr-wasi-wasm")); os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), + snprintf(tmpbuf, MAX_PACKET_SIZE, "pid:%" PRIx64 ";parent-pid:%" PRIx64 ";vendor:wamr;ostype:wasi;arch:wasm32;" "triple:%s;endian:little;ptrsize:4;", @@ -238,7 +251,7 @@ handle_general_query(WASMGDBServer *server, char *payload) if (!strcmp(name, "RegisterInfo0")) { os_mutex_lock(&tmpbuf_lock); snprintf( - tmpbuf, sizeof(tmpbuf), + tmpbuf, MAX_PACKET_SIZE, "name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;" "set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;"); write_packet(server, tmpbuf); @@ -260,7 +273,7 @@ handle_general_query(WASMGDBServer *server, char *payload) mem2hex(mem_info->name, name_buf, strlen(mem_info->name)); os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), + snprintf(tmpbuf, MAX_PACKET_SIZE, "start:%" PRIx64 ";size:%" PRIx64 ";permissions:%s;name:%s;", (uint64)mem_info->start, mem_info->size, @@ -339,7 +352,7 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid) if (status == 0) { os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "W%02x", status); + snprintf(tmpbuf, MAX_PACKET_SIZE, "W%02x", status); write_packet(server, tmpbuf); os_mutex_unlock(&tmpbuf_lock); return; @@ -355,16 +368,16 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid) os_mutex_lock(&tmpbuf_lock); // TODO: how name a wasm thread? - len += snprintf(tmpbuf, sizeof(tmpbuf), "T%02xthread:%" PRIx64 ";name:%s;", + len += snprintf(tmpbuf, MAX_PACKET_SIZE, "T%02xthread:%" PRIx64 ";name:%s;", gdb_status, (uint64)(uintptr_t)tid, "nobody"); if (tids_count > 0) { - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "threads:"); + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "threads:"); while (i < tids_count) { if (i == tids_count - 1) - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%" PRIx64 ";", (uint64)(uintptr_t)tids[i]); else - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%" PRIx64 ",", (uint64)(uintptr_t)tids[i]); i++; } @@ -383,29 +396,29 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid) * correctly processed by LLDB */ uint32 exception_len = strlen(exception); len += - snprintf(tmpbuf + len, sizeof(tmpbuf) - len, + snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:", pc, pc_string, "exception"); /* The description should be encoded as HEX */ for (i = 0; i < exception_len; i++) { - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%02x", + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%02x", exception[i]); } - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, ";"); + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, ";"); } else { if (status == WAMR_SIG_TRAP) { - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc, pc_string, "breakpoint"); } else if (status == WAMR_SIG_SINGSTEP) { - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc, pc_string, "trace"); } else if (status > 0) { - len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, + len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc, pc_string, "signal"); } @@ -551,7 +564,7 @@ handle_get_read_memory(WASMGDBServer *server, char *payload) bool ret; os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "%s", ""); + snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", ""); if (sscanf(payload, "%" SCNx64 ",%" SCNx64, &maddr, &mlen) == 2) { char *buff; @@ -585,7 +598,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload) bool ret; os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "%s", ""); + snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", ""); if (sscanf(payload, "%" SCNx64 ",%" SCNx64 ":%n", &maddr, &mlen, &offset) == 2) { payload += offset; @@ -599,7 +612,7 @@ handle_get_write_memory(WASMGDBServer *server, char *payload) (WASMDebugInstance *)server->thread->debug_instance, maddr, buff, &mlen); if (ret) { - snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK"); + snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK"); } wasm_runtime_free(buff); } @@ -681,7 +694,7 @@ handle_malloc(WASMGDBServer *server, char *payload) } os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03"); + snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03"); size = strtoll(payload, NULL, 16); if (size > 0) { @@ -701,7 +714,7 @@ handle_malloc(WASMGDBServer *server, char *payload) (WASMDebugInstance *)server->thread->debug_instance, size, map_prot); if (addr) { - snprintf(tmpbuf, sizeof(tmpbuf), "%" PRIx64, addr); + snprintf(tmpbuf, MAX_PACKET_SIZE, "%" PRIx64, addr); } } write_packet(server, tmpbuf); @@ -715,13 +728,13 @@ handle_free(WASMGDBServer *server, char *payload) bool ret; os_mutex_lock(&tmpbuf_lock); - snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03"); + snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03"); addr = strtoll(payload, NULL, 16); ret = wasm_debug_instance_ummap( (WASMDebugInstance *)server->thread->debug_instance, addr); if (ret) { - snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK"); + snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK"); } write_packet(server, tmpbuf); From 1985a251cd20409a8c6b5acfb6be7c9b970d7ccf Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 26 Aug 2022 22:13:30 +0900 Subject: [PATCH 15/41] debug-engine: Use default stack size for control thread (#1424) --- core/iwasm/libraries/debug-engine/debug_engine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index 8ea85e7f3..67bef6040 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -200,7 +200,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port) if (0 != os_thread_create(&control_thread->tid, control_thread_routine, - debug_instance, APP_THREAD_STACK_SIZE_MAX)) { + debug_instance, APP_THREAD_STACK_SIZE_DEFAULT)) { os_mutex_unlock(&debug_instance->wait_lock); goto fail1; } From 29dee732e9c553b25ab13d64867defdaa88786d6 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Mon, 29 Aug 2022 18:01:19 +0800 Subject: [PATCH 16/41] Fix typo in simd_conversions.c (#1428) Change "==" to "=" when setting trunc_type[] array elements. --- core/iwasm/compilation/simd/simd_conversions.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/iwasm/compilation/simd/simd_conversions.c b/core/iwasm/compilation/simd/simd_conversions.c index 554b088d0..e9d30bfcb 100644 --- a/core/iwasm/compilation/simd/simd_conversions.c +++ b/core/iwasm/compilation/simd/simd_conversions.c @@ -135,9 +135,9 @@ simd_integer_narrow_common(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, LLVM_CONST(i32_fourteen), LLVM_CONST(i32_fifteen), }; - if (!(trunc_type[0] == LLVMVectorType(INT8_TYPE, 8)) - || !(trunc_type[1] == LLVMVectorType(INT16_TYPE, 4)) - || !(trunc_type[2] == LLVMVectorType(I32_TYPE, 2))) { + if (!(trunc_type[0] = LLVMVectorType(INT8_TYPE, 8)) + || !(trunc_type[1] = LLVMVectorType(INT16_TYPE, 4)) + || !(trunc_type[2] = LLVMVectorType(I32_TYPE, 2))) { HANDLE_FAILURE("LLVMVectorType"); return false; } From fba12e99696ca2301f6642ac66d344f3ce36c203 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 30 Aug 2022 16:43:47 +0900 Subject: [PATCH 17/41] debug-engine: constify several global variables (#1427) --- core/iwasm/libraries/debug-engine/debug_engine.c | 2 +- core/iwasm/libraries/debug-engine/gdbserver.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index 67bef6040..e62ba5a2f 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -11,7 +11,7 @@ #include "wasm_opcode.h" #include "wasm_runtime.h" -static uint8 break_instr[] = { DEBUG_OP_BREAK }; +static const uint8 break_instr[] = { DEBUG_OP_BREAK }; typedef struct WASMDebugEngine { struct WASMDebugEngine *next; diff --git a/core/iwasm/libraries/debug-engine/gdbserver.c b/core/iwasm/libraries/debug-engine/gdbserver.c index eeb0c06bd..eab4ece08 100644 --- a/core/iwasm/libraries/debug-engine/gdbserver.c +++ b/core/iwasm/libraries/debug-engine/gdbserver.c @@ -18,7 +18,7 @@ struct packet_handler_elem { #define DEL_HANDLER(r, h) [r] = { .request = r, .handler = h } -static struct packet_handler_elem packet_handler_table[255] = { +static const struct packet_handler_elem packet_handler_table[255] = { DEL_HANDLER('Q', handle_general_set), DEL_HANDLER('q', handle_general_query), DEL_HANDLER('v', handle_v_packet), From 6b073af3b00f70c589f13a6f995c43a97b412375 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 30 Aug 2022 18:00:32 +0900 Subject: [PATCH 18/41] debug-engine: Reduce the default MAX_PACKET_SIZE (#1425) --- core/iwasm/libraries/debug-engine/handler.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/core/iwasm/libraries/debug-engine/handler.c b/core/iwasm/libraries/debug-engine/handler.c index 9b6009bf6..f45638343 100644 --- a/core/iwasm/libraries/debug-engine/handler.c +++ b/core/iwasm/libraries/debug-engine/handler.c @@ -10,7 +10,26 @@ #include "utils.h" #include "wasm_runtime.h" -#define MAX_PACKET_SIZE (0x20000) +/* + * Note: A moderate MAX_PACKET_SIZE is ok because + * LLDB queries our buffer size (via qSupported PacketSize) + * and limits packet sizes accordingly. + */ + +#if defined(DEBUG_MAX_PACKET_SIZE) +#define MAX_PACKET_SIZE DEBUG_MAX_PACKET_SIZE +#else +#define MAX_PACKET_SIZE (4096) +#endif + +/* + * Note: It's assumed that MAX_PACKET_SIZE is reasonably large. + * See GetWorkingDir, WasmCallStack, etc. + */ +#if MAX_PACKET_SIZE < PATH_MAX || MAX_PACKET_SIZE < (2048 + 1) +#error MAX_PACKET_SIZE is too small +#endif + static char *tmpbuf; static korp_mutex tmpbuf_lock; From 7e02be836fb1310cdd3fc786ae4a58e01e4bf457 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 30 Aug 2022 19:38:28 +0900 Subject: [PATCH 19/41] nuttx: Add CONFIG_INTERPRETERS_WAMR_DEBUG_INTERP (#1426) the corresponding nuttx PR: https://github.com/apache/incubator-nuttx-apps/pull/1299 --- product-mini/platforms/nuttx/wamr.mk | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 28ca1f601..d694cbdce 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -147,6 +147,19 @@ CSRCS += wasm_loader.c endif endif +ifeq ($(CONFIG_INTERPRETERS_WAMR_DEBUG_INTERP),y) +# Note: INTERPRETERS_WAMR_CLASSIC/INTERPRETERS_WAMR_THREAD_MGR +# dependencies are already handled in NuttX apps Kconfig +CFLAGS += -DWASM_ENABLE_DEBUG_INTERP=1 +CFLAGS += -I$(IWASM_ROOT)/libraries/debug-engine +CSRCS += debug_engine.c +CSRCS += gdbserver.c +CSRCS += handler.c +CSRCS += packets.c +CSRCS += utils.c +VPATH += $(IWASM_ROOT)/libraries/debug-engine +endif + ifeq ($(CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE),) CFLAGS += -DWASM_STACK_GUARD_SIZE=0 else From e0c2acd178265f6e3db3eb71764c0230a8faf605 Mon Sep 17 00:00:00 2001 From: Xu Jun <693788454@qq.com> Date: Tue, 30 Aug 2022 22:05:54 +0800 Subject: [PATCH 20/41] add language bindings in README (#1434) * add language bindings in README * minor update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b8bb08977..5a1444b51 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ iwasm VM core - [WAMR-IDE (Experimental)](./test-tools/wamr-ide) to develop WebAssembly applications with build, run and debug support, ref to [document](./test-tools/wamr-ide) - [XIP (Execution In Place) support](./doc/xip.md), ref to [document](./doc/xip.md) - [Berkeley/Posix Socket support](./doc/socket_api.md), ref to [document](./doc/socket_api.md) and [sample](./samples/socket-api) +- Language bindings: [Go](./language-bindings/go/README.md), [Python](./language-bindings/python/README.md) ### WASM post-MVP features - [wasm-c-api](https://github.com/WebAssembly/wasm-c-api), ref to [document](doc/wasm_c_api.md) and [sample](samples/wasm-c-api) From 77c516ac80621e4c9d0f0c3e8b197187a84327ba Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Wed, 31 Aug 2022 16:39:25 +0800 Subject: [PATCH 21/41] Add a new API to get free memory in memory pool (#1430) --- core/iwasm/common/wasm_memory.c | 9 +++++++++ core/iwasm/include/wasm_export.h | 13 +++++++++++++ core/shared/mem-alloc/mem_alloc.c | 7 +++++++ core/shared/mem-alloc/mem_alloc.h | 3 +++ 4 files changed, 32 insertions(+) diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index 4f58bbc3c..fac6f4af5 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -167,3 +167,12 @@ wasm_runtime_free(void *ptr) { wasm_runtime_free_internal(ptr); } + +bool +wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info) +{ + if (memory_mode == MEMORY_MODE_POOL) { + return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info); + } + return false; +} diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 4beb4d55c..f5970ea85 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -121,6 +121,13 @@ typedef union MemAllocOption { } MemAllocOption; #endif +/* Memory pool info */ +typedef struct mem_alloc_info_t { + uint32_t total_size; + uint32_t total_free_size; + uint32_t highmark_size; +} mem_alloc_info_t; + /* WASM runtime initialize arguments */ typedef struct RuntimeInitArgs { mem_alloc_type_t mem_alloc_type; @@ -229,6 +236,12 @@ wasm_runtime_realloc(void *ptr, unsigned int size); WASM_RUNTIME_API_EXTERN void wasm_runtime_free(void *ptr); +/* + * Get memory info, only pool mode is supported now. + */ +WASM_RUNTIME_API_EXTERN bool +wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info); + /** * Get the package type of a buffer. * diff --git a/core/shared/mem-alloc/mem_alloc.c b/core/shared/mem-alloc/mem_alloc.c index 166529651..3a3748c41 100644 --- a/core/shared/mem-alloc/mem_alloc.c +++ b/core/shared/mem-alloc/mem_alloc.c @@ -69,6 +69,13 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator) return gc_is_heap_corrupted((gc_handle_t)allocator); } +bool +mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info) +{ + gc_heap_stats((gc_handle_t)allocator, mem_alloc_info, 3); + return true; +} + #else /* else of DEFAULT_MEM_ALLOCATOR */ #include "tlsf/tlsf.h" diff --git a/core/shared/mem-alloc/mem_alloc.h b/core/shared/mem-alloc/mem_alloc.h index d5dc66f11..224eb736b 100644 --- a/core/shared/mem-alloc/mem_alloc.h +++ b/core/shared/mem-alloc/mem_alloc.h @@ -45,6 +45,9 @@ mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new, bool mem_allocator_is_heap_corrupted(mem_allocator_t allocator); +bool +mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info); + #ifdef __cplusplus } #endif From 47f17ef8f5e6fcf92dddc12d23b13883728c836e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 31 Aug 2022 18:27:24 +0900 Subject: [PATCH 22/41] wamr-ide/VSCode-Extension: "set -e" in shell scripts (#1436) To avoid mysterious behaviors caused by ignored errors. --- .../VSCode-Extension/resource/scripts/boot_debugger_server.sh | 2 ++ test-tools/wamr-ide/VSCode-Extension/resource/scripts/build.sh | 2 ++ .../wamr-ide/VSCode-Extension/resource/scripts/destroy.sh | 2 ++ test-tools/wamr-ide/VSCode-Extension/resource/scripts/run.sh | 2 ++ 4 files changed, 8 insertions(+) diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/boot_debugger_server.sh b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/boot_debugger_server.sh index 9bd245480..97e290fce 100755 --- a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/boot_debugger_server.sh +++ b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/boot_debugger_server.sh @@ -3,6 +3,8 @@ # Copyright (C) 2019 Intel Corporation. All rights reserved. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +set -e + docker run --rm -it --name=wasm-debug-server-ctr \ -v "$(pwd)":/mnt \ -p 1234:1234 \ diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/build.sh b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/build.sh index 193d53b6b..04892b4ee 100755 --- a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/build.sh +++ b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/build.sh @@ -3,6 +3,8 @@ # Copyright (C) 2019 Intel Corporation. All rights reserved. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +set -e + docker run --rm --name=wasm-toolchain-ctr \ -it -v "$(pwd)":/mnt \ --env=PROJ_PATH="$(pwd)" \ diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/destroy.sh b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/destroy.sh index aa90a86db..41faf3eaf 100755 --- a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/destroy.sh +++ b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/destroy.sh @@ -3,6 +3,8 @@ # Copyright (C) 2019 Intel Corporation. All rights reserved. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +set -e + docker -v>/dev/null if [ $? -ne 0 ]; then echo "\nDocker is not installed, please install docker firstly.\n" diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/run.sh b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/run.sh index e9aacbc41..31961847f 100755 --- a/test-tools/wamr-ide/VSCode-Extension/resource/scripts/run.sh +++ b/test-tools/wamr-ide/VSCode-Extension/resource/scripts/run.sh @@ -3,6 +3,8 @@ # Copyright (C) 2019 Intel Corporation. All rights reserved. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +set -e + docker run --rm -it --name=wasm-debug-server-ctr \ -v "$(pwd)":/mnt \ wasm-debug-server:1.0 \ From 22c235b5ec5c473579984a51e2bd1207f4570201 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 31 Aug 2022 17:38:38 +0800 Subject: [PATCH 23/41] Fix multi-module and some other issues (#1435) Fix multi-module issue: don't call the sub module's function with "$sub_module_name$func_name" Fix the aot_call_function free argv1 issue Modify some API comments in wasm_export.h Fix the wamrc help info --- core/iwasm/aot/aot_runtime.c | 13 ++-- core/iwasm/common/wasm_application.c | 111 +-------------------------- core/iwasm/include/wasm_export.h | 22 +++--- samples/multi-module/src/main.c | 16 ---- wamr-compiler/main.c | 5 +- 5 files changed, 23 insertions(+), 144 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index ff05456fd..e915bbd38 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1483,7 +1483,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) if (!aot_alloc_frame(exec_env, function->func_index)) { - wasm_runtime_free(argv1); + if (argv1 != argv1_buf) + wasm_runtime_free(argv1); return false; } #endif @@ -1492,9 +1493,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, func_type, NULL, NULL, argv1, argc, argv); if (!ret || aot_get_exception(module_inst)) { - if (argv1 != argv1_buf) - wasm_runtime_free(argv1); - if (clear_wasi_proc_exit_exception(module_inst)) ret = true; else @@ -1512,8 +1510,11 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) aot_free_frame(exec_env); #endif - if (!ret) + if (!ret) { + if (argv1 != argv1_buf) + wasm_runtime_free(argv1); return ret; + } /* Get extra result values */ switch (func_type->types[func_type->param_count]) { @@ -1542,9 +1543,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count; bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets, sizeof(uint32) * cell_num); + if (argv1 != argv1_buf) wasm_runtime_free(argv1); - return true; } else { diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index 2d7eacb49..8a19de8ee 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -233,113 +233,10 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, return (ret && !wasm_runtime_get_exception(module_inst)) ? true : false; } -#if WASM_ENABLE_MULTI_MODULE != 0 -static WASMModuleInstance * -get_sub_module_inst(const WASMModuleInstance *parent_module_inst, - const char *sub_module_name) -{ - WASMSubModInstNode *node = - bh_list_first_elem(parent_module_inst->sub_module_inst_list); - - while (node && strcmp(node->module_name, sub_module_name)) { - node = bh_list_elem_next(node); - } - return node ? node->module_inst : NULL; -} - -static bool -parse_function_name(char *orig_function_name, char **p_module_name, - char **p_function_name) -{ - if (orig_function_name[0] != '$') { - *p_module_name = NULL; - *p_function_name = orig_function_name; - return true; - } - - /** - * $module_name$function_name\0 - * ===> - * module_name\0function_name\0 - * ===> - * module_name - * function_name - */ - char *p1 = orig_function_name; - char *p2 = strchr(p1 + 1, '$'); - if (!p2) { - LOG_DEBUG("can not parse the incoming function name"); - return false; - } - - *p_module_name = p1 + 1; - *p2 = '\0'; - *p_function_name = p2 + 1; - return strlen(*p_module_name) && strlen(*p_function_name); -} -#endif - /** * Implementation of wasm_application_execute_func() */ -static bool -resolve_function(WASMModuleInstanceCommon *module_inst, const char *name, - WASMFunctionInstanceCommon **out_func, - WASMModuleInstanceCommon **out_module_inst) -{ - WASMFunctionInstanceCommon *target_func = NULL; - WASMModuleInstanceCommon *target_inst = NULL; - -#if WASM_ENABLE_MULTI_MODULE != 0 - char *function_name = NULL; - char *orig_name = NULL; - char *sub_module_name = NULL; - uint32 length = (uint32)(strlen(name) + 1); - - orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0); - if (!orig_name) { - goto LEAVE; - } - - strncpy(orig_name, name, length); - - if (!parse_function_name(orig_name, &sub_module_name, &function_name)) { - goto LEAVE; - } - - LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name); - - if (sub_module_name) { - target_inst = (WASMModuleInstanceCommon *)get_sub_module_inst( - (WASMModuleInstance *)module_inst, sub_module_name); - if (!target_inst) { - LOG_DEBUG("can not find a sub module named %s", sub_module_name); - goto LEAVE; - } - } - else { - target_inst = module_inst; - } -#else - const char *function_name = name; - target_inst = module_inst; -#endif - - target_func = - wasm_runtime_lookup_function(target_inst, function_name, NULL); - -#if WASM_ENABLE_MULTI_MODULE != 0 -LEAVE: - if (orig_name) - wasm_runtime_free(orig_name); -#endif - - *out_func = target_func; - *out_module_inst = target_inst; - return target_func; -} - union ieee754_float { float f; @@ -386,7 +283,6 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name, int32 argc, char *argv[]) { WASMFunctionInstanceCommon *target_func; - WASMModuleInstanceCommon *target_inst; WASMType *type = NULL; WASMExecEnv *exec_env = NULL; uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0; @@ -401,13 +297,14 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name, bh_assert(argc >= 0); LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc); - if (!resolve_function(module_inst, name, &target_func, &target_inst)) { + if (!(target_func = + wasm_runtime_lookup_function(module_inst, name, NULL))) { snprintf(buf, sizeof(buf), "lookup function %s failed", name); wasm_runtime_set_exception(module_inst, buf); goto fail; } - module_type = target_inst->module_type; + module_type = module_inst->module_type; type = wasm_runtime_get_function_type(target_func, module_type); if (!type) { @@ -439,7 +336,7 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name, #endif total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2); - if ((!(argv1 = runtime_malloc((uint32)total_size, target_inst, NULL, 0)))) { + if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, NULL, 0)))) { goto fail; } diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index f5970ea85..3b8bdb031 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -123,9 +123,9 @@ typedef union MemAllocOption { /* Memory pool info */ typedef struct mem_alloc_info_t { - uint32_t total_size; - uint32_t total_free_size; - uint32_t highmark_size; + uint32_t total_size; + uint32_t total_free_size; + uint32_t highmark_size; } mem_alloc_info_t; /* WASM runtime initialize arguments */ @@ -265,20 +265,18 @@ WASM_RUNTIME_API_EXTERN bool wasm_runtime_is_xip_file(const uint8_t *buf, uint32_t size); /** - * It is a callback for WAMR providing by embedding to load a module file - * into a buffer + * Callback to load a module file into a buffer in multi-module feature */ typedef bool (*module_reader)(const char *module_name, uint8_t **p_buffer, uint32_t *p_size); /** - * It is a callback for WAMR providing by embedding to release the buffer which - * is used by loading a module file + * Callback to release the buffer loaded by module_reader callback */ typedef void (*module_destroyer)(uint8_t *buffer, uint32_t size); /** - * To setup callbacks for reading and releasing a buffer about a module file + * Setup callbacks for reading and releasing a buffer about a module file * * @param reader a callback to read a module file into a buffer * @param destroyer a callback to release above buffer @@ -288,7 +286,7 @@ wasm_runtime_set_module_reader(const module_reader reader, const module_destroyer destroyer); /** * Give the "module" a name "module_name". - * can not assign a new name to a module if it already has a name + * Can not assign a new name to a module if it already has a name * * @param module_name indicate a name * @param module the target module @@ -302,8 +300,8 @@ wasm_runtime_register_module(const char *module_name, wasm_module_t module, char *error_buf, uint32_t error_buf_size); /** - * To check if there is already a loaded module named module_name in the - * runtime. you will not want to load repeately + * Check if there is already a loaded module named module_name in the + * runtime. Repeately loading a module with the same name is not allowed. * * @param module_name indicate a name * @@ -1060,7 +1058,7 @@ wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid, wasm_thread_callback_t callback, void *arg); /** - * Waits a spawned thread to terminate + * Wait a spawned thread to terminate * * @param tid thread id * @param retval if not NULL, output the return value of the thread diff --git a/samples/multi-module/src/main.c b/samples/multi-module/src/main.c index 557ffec3e..06b8fc716 100644 --- a/samples/multi-module/src/main.c +++ b/samples/multi-module/src/main.c @@ -122,22 +122,6 @@ main() "call \"C5\", it will be failed since it is a export function, ===> "); wasm_application_execute_func(module_inst, "C5", 0, args); - /* call functions of mB */ - printf("call \"mB.B1\", it will return 0x15:i32, ===> "); - wasm_application_execute_func(module_inst, "$mB$B1", 0, args); - printf("call \"mB.B2\", it will call A1() of mA and return 0xb:i32, ===> "); - wasm_application_execute_func(module_inst, "$mB$B2", 0, args); - printf("call \"mB.B3\", it will be failed since it is a export function, " - "===> "); - wasm_application_execute_func(module_inst, "$mB$B3", 0, args); - - /* call functions of mA */ - printf("call \"mA.A1\", it will return 0xb:i32, ===>"); - wasm_application_execute_func(module_inst, "$mA$A1", 0, args); - printf("call \"mA.A2\", it will be failed since it is a export function, " - "===> "); - wasm_application_execute_func(module_inst, "$mA$A2", 0, args); - printf("----------------------------------------\n\n"); ret = true; printf("- wasm_runtime_deinstantiate\n"); diff --git a/wamr-compiler/main.c b/wamr-compiler/main.c index 820420ebc..b77533ed3 100644 --- a/wamr-compiler/main.c +++ b/wamr-compiler/main.c @@ -48,9 +48,8 @@ print_help() printf(" thread-mgr will be enabled automatically\n"); printf(" --enable-tail-call Enable the post-MVP tail call feature\n"); printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n"); - printf(" currently 128-bit SIMD is only supported for x86-64 target,\n"); - printf(" and by default it is enabled in x86-64 target and disabled\n"); - printf(" in other targets\n"); + printf(" currently 128-bit SIMD is supported for x86-64 and aarch64 targets,\n"); + printf(" and by default it is enabled in them and disabled in other targets\n"); printf(" --disable-ref-types Disable the MVP reference types feature\n"); printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n"); printf(" --enable-dump-call-stack Enable stack trace feature\n"); From 9f09e2661f251d94690af0af317d3c85c840393f Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 1 Sep 2022 13:33:59 +0900 Subject: [PATCH 24/41] wamr-ide/VSCode-Extension: Make lldb binary path per platform (#1437) hopefully this allows an extension package to run on multiple platforms. cf. https://code.visualstudio.com/api/extension-guides/debugger-extension#anatomy-of-the-package.json-of-a-debugger-extension --- test-tools/wamr-ide/VSCode-Extension/package.json | 9 +++++++-- .../wamr-ide/VSCode-Extension/resource/debug/README.md | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/test-tools/wamr-ide/VSCode-Extension/package.json b/test-tools/wamr-ide/VSCode-Extension/package.json index 98aee2a96..455045237 100644 --- a/test-tools/wamr-ide/VSCode-Extension/package.json +++ b/test-tools/wamr-ide/VSCode-Extension/package.json @@ -125,9 +125,14 @@ "swift" ] }, - "program": "./resource/debug/bin/lldb-vscode", "windows": { - "program": "./resource/debug/bin/lldb-vscode.exe" + "program": "./resource/debug/windows/bin/lldb-vscode.exe" + }, + "osx": { + "program": "./resource/debug/osx/bin/lldb-vscode" + }, + "linux": { + "program": "./resource/debug/linux/bin/lldb-vscode" }, "configurationAttributes": { "attach": { diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/debug/README.md b/test-tools/wamr-ide/VSCode-Extension/resource/debug/README.md index 27efb169d..36fbc5d6e 100644 --- a/test-tools/wamr-ide/VSCode-Extension/resource/debug/README.md +++ b/test-tools/wamr-ide/VSCode-Extension/resource/debug/README.md @@ -1,11 +1,11 @@ ### If you want to enable `source debugging` for this extension, please build `lldb` firstly following this [instruction](../../../../../doc/source_debugging.md#debugging-with-interpreter). -### After building(`linux` for example), create `bin` folder and `lib` folder respectively in current directory, add following necessary target files into the folders. +### After building(`linux` for example), create `bin` folder and `lib` folder respectively in `linux` directory, add following necessary target files into the folders. ```shell - /llvm/build-lldb/bin/lldb # move this file to resource/debug/bin/ - /llvm/build-lldb/bin/lldb-vscode # move this file to resource/debug/bin/ - /llvm/build-lldb/lib/liblldb.so.13 # move this file to resource/debug/lib/ + /llvm/build-lldb/bin/lldb # move this file to resource/debug/linux/bin/ + /llvm/build-lldb/bin/lldb-vscode # move this file to resource/debug/linux/bin/ + /llvm/build-lldb/lib/liblldb.so.13 # move this file to resource/debug/linux/lib/ ``` Note: For macOS, the library is named like `liblldb.13.0.1.dylib`. From c64186b70414ad49dd1277878f283b1e14999a4d Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Thu, 1 Sep 2022 14:11:00 +0800 Subject: [PATCH 25/41] Fix potential warning of printf in main.c of posix and windows (#1440) --- product-mini/platforms/posix/main.c | 3 ++- product-mini/platforms/windows/main.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 641e12a3d..944329534 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -464,7 +464,8 @@ main(int argc, char *argv[]) else if (!strncmp(argv[0], "--version", 9)) { uint32 major, minor, patch; wasm_runtime_get_version(&major, &minor, &patch); - printf("iwasm %u.%u.%u\n", major, minor, patch); + printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor, + patch); return 0; } else diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index b9afae494..73319083a 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -342,7 +342,8 @@ main(int argc, char *argv[]) else if (!strncmp(argv[0], "--version", 9)) { uint32 major, minor, patch; wasm_runtime_get_version(&major, &minor, &patch); - printf("iwasm %u.%u.%u\n", major, minor, patch); + printf("iwasm %" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n", major, minor, + patch); return 0; } else From 18774805cc8d4eb40cb0bd0b5ca67c968f859952 Mon Sep 17 00:00:00 2001 From: Qiang <56512053+FromLiQg@users.noreply.github.com> Date: Thu, 1 Sep 2022 15:31:00 +0800 Subject: [PATCH 26/41] Add more checks for Fast JIT (#1433) Add more checks for Fast JIT to fix the issues reported by instrument test: - add check for the jit_value before pushing it into the stack - add check at the end of form_and_translate_func - add checks after each jit pass --- core/iwasm/fast-jit/jit_compiler.c | 2 +- core/iwasm/fast-jit/jit_frontend.c | 3 +++ core/iwasm/fast-jit/jit_frontend.h | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/iwasm/fast-jit/jit_compiler.c b/core/iwasm/fast-jit/jit_compiler.c index 6282e3046..c10a40994 100644 --- a/core/iwasm/fast-jit/jit_compiler.c +++ b/core/iwasm/fast-jit/jit_compiler.c @@ -64,7 +64,7 @@ apply_compiler_passes(JitCompContext *cc) cc->cur_pass_no = p - jit_globals.passes; bh_assert(*p < COMPILER_PASS_NUM); - if (!compiler_passes[*p].run(cc)) { + if (!compiler_passes[*p].run(cc) || jit_get_last_error(cc)) { LOG_VERBOSE("JIT: compilation failed at pass[%td] = %s\n", p - jit_globals.passes, compiler_passes[*p].name); return false; diff --git a/core/iwasm/fast-jit/jit_frontend.c b/core/iwasm/fast-jit/jit_frontend.c index 124a5f553..44c06a7dc 100644 --- a/core/iwasm/fast-jit/jit_frontend.c +++ b/core/iwasm/fast-jit/jit_frontend.c @@ -731,6 +731,9 @@ form_and_translate_func(JitCompContext *cc) *(jit_annl_end_bcip(cc, cc->exit_label)) = cc->cur_wasm_module->load_addr; + if (jit_get_last_error(cc)) { + return false; + } return true; } diff --git a/core/iwasm/fast-jit/jit_frontend.h b/core/iwasm/fast-jit/jit_frontend.h index a31fbc5ae..eeecf48fb 100644 --- a/core/iwasm/fast-jit/jit_frontend.h +++ b/core/iwasm/fast-jit/jit_frontend.h @@ -513,6 +513,8 @@ set_local_f64(JitFrame *frame, int n, JitReg val) #define PUSH(jit_value, value_type) \ do { \ + if (!jit_value) \ + goto fail; \ if (!jit_cc_push_value(cc, value_type, jit_value)) \ goto fail; \ } while (0) From d095876ae6174c38b1911802d8799b0f9ffe6784 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 1 Sep 2022 16:15:00 +0800 Subject: [PATCH 27/41] Enable memory leak check (#1429) Report the memory leak info when building iwasm with `cmake .. -DWAMR_BUILD_GC_VERIFY=1` --- core/config.h | 20 ++++++++++++++++++++ core/iwasm/common/wasm_memory.c | 13 +++++++++++-- core/shared/mem-alloc/ems/ems_kfc.c | 11 ++++++----- core/shared/mem-alloc/mem_alloc.c | 4 ++-- core/shared/mem-alloc/mem_alloc.cmake | 4 ++++ core/shared/mem-alloc/mem_alloc.h | 2 +- product-mini/platforms/nuttx/wamr.mk | 2 +- product-mini/platforms/posix/main.c | 6 +----- 8 files changed, 46 insertions(+), 16 deletions(-) diff --git a/core/config.h b/core/config.h index 6b4392b79..a00d241c6 100644 --- a/core/config.h +++ b/core/config.h @@ -272,6 +272,26 @@ #define BH_ENABLE_GC_VERIFY 0 #endif +/* Enable global heap pool if heap verification is enabled */ +#if BH_ENABLE_GC_VERIFY != 0 +#define WASM_ENABLE_GLOBAL_HEAP_POOL 1 +#endif + +/* Global heap pool */ +#ifndef WASM_ENABLE_GLOBAL_HEAP_POOL +#define WASM_ENABLE_GLOBAL_HEAP_POOL 0 +#endif + +/* Global heap pool size in bytes */ +#ifndef WASM_GLOBAL_HEAP_SIZE +#if WASM_ENABLE_SPEC_TEST != 0 +/* Spec test requires more heap pool size */ +#define WASM_GLOBAL_HEAP_SIZE (300 * 1024 * 1024) +#else +#define WASM_GLOBAL_HEAP_SIZE (10 * 1024 * 1024) +#endif +#endif + /* Max app number of all modules */ #define MAX_APP_INSTALLATIONS 3 diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index fac6f4af5..49a9b0867 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -75,8 +75,17 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, void wasm_runtime_memory_destroy() { - if (memory_mode == MEMORY_MODE_POOL) - mem_allocator_destroy(pool_allocator); + if (memory_mode == MEMORY_MODE_POOL) { +#if BH_ENABLE_GC_VERIFY == 0 + (void)mem_allocator_destroy(pool_allocator); +#else + int ret = mem_allocator_destroy(pool_allocator); + if (ret != 0) { + /* Memory leak detected */ + exit(-1); + } +#endif + } memory_mode = MEMORY_MODE_UNKNOWN; } diff --git a/core/shared/mem-alloc/ems/ems_kfc.c b/core/shared/mem-alloc/ems/ems_kfc.c index d2a1eb701..cb405fd16 100644 --- a/core/shared/mem-alloc/ems/ems_kfc.c +++ b/core/shared/mem-alloc/ems/ems_kfc.c @@ -125,23 +125,24 @@ int gc_destroy_with_pool(gc_handle_t handle) { gc_heap_t *heap = (gc_heap_t *)handle; + int ret = GC_SUCCESS; + #if BH_ENABLE_GC_VERIFY != 0 hmu_t *cur = (hmu_t *)heap->base_addr; hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size); + if (!heap->is_heap_corrupted && (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) { os_printf("Memory leak detected:\n"); gci_dump(heap); -#if WASM_ENABLE_SPEC_TEST != 0 - while (1) { - } -#endif + ret = GC_ERROR; } #endif + os_mutex_destroy(&heap->lock); memset(heap->base_addr, 0, heap->current_size); memset(heap, 0, sizeof(gc_heap_t)); - return GC_SUCCESS; + return ret; } uint32 diff --git a/core/shared/mem-alloc/mem_alloc.c b/core/shared/mem-alloc/mem_alloc.c index 3a3748c41..f952c1858 100644 --- a/core/shared/mem-alloc/mem_alloc.c +++ b/core/shared/mem-alloc/mem_alloc.c @@ -25,10 +25,10 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf, pool_buf, pool_buf_size); } -void +int mem_allocator_destroy(mem_allocator_t allocator) { - gc_destroy_with_pool((gc_handle_t)allocator); + return gc_destroy_with_pool((gc_handle_t)allocator); } uint32 diff --git a/core/shared/mem-alloc/mem_alloc.cmake b/core/shared/mem-alloc/mem_alloc.cmake index be57f3794..c0b4157f4 100644 --- a/core/shared/mem-alloc/mem_alloc.cmake +++ b/core/shared/mem-alloc/mem_alloc.cmake @@ -6,6 +6,10 @@ set (MEM_ALLOC_DIR ${CMAKE_CURRENT_LIST_DIR}) include_directories(${MEM_ALLOC_DIR}) +if (WAMR_BUILD_GC_VERIFY EQUAL 1) + add_definitions (-DBH_ENABLE_GC_VERIFY=1) +endif () + file (GLOB_RECURSE source_all ${MEM_ALLOC_DIR}/ems/*.c ${MEM_ALLOC_DIR}/tlsf/*.c diff --git a/core/shared/mem-alloc/mem_alloc.h b/core/shared/mem-alloc/mem_alloc.h index 224eb736b..1f35b2792 100644 --- a/core/shared/mem-alloc/mem_alloc.h +++ b/core/shared/mem-alloc/mem_alloc.h @@ -23,7 +23,7 @@ mem_allocator_create_with_struct_and_pool(void *struct_buf, void *pool_buf, uint32_t pool_buf_size); -void +int mem_allocator_destroy(mem_allocator_t allocator); uint32 diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index d694cbdce..36953a67d 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -261,7 +261,7 @@ endif ifeq ($(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL),y) CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=1 -CFLAGS += -DWASM_GLOBAL_HEAP_SIZE=$(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE) +CFLAGS += -DWASM_GLOBAL_HEAP_SIZE="$(CONFIG_INTERPRETERS_WAMR_GLOBAL_HEAP_POOL_SIZE) * 1024" else CFLAGS += -DWASM_ENABLE_GLOBAL_HEAP_POOL=0 endif diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 944329534..2b3064172 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -285,11 +285,7 @@ moudle_destroyer(uint8 *buffer, uint32 size) #endif /* WASM_ENABLE_MULTI_MODULE */ #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0 -#ifdef __NuttX__ -static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE * BH_KB] = { 0 }; -#else -static char global_heap_buf[10 * 1024 * 1024] = { 0 }; -#endif +static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 }; #endif int From c56fd8bd5bba0b05e314ee9ff60d75371c77dc2c Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 2 Sep 2022 11:07:59 +0800 Subject: [PATCH 28/41] Introduce basic CI for nuttx (#1444) --- .github/workflows/compilation_on_nuttx.yml | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 .github/workflows/compilation_on_nuttx.yml diff --git a/.github/workflows/compilation_on_nuttx.yml b/.github/workflows/compilation_on_nuttx.yml new file mode 100644 index 000000000..e248ca42e --- /dev/null +++ b/.github/workflows/compilation_on_nuttx.yml @@ -0,0 +1,86 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +name: compilation on nuttx + +on: + # will be triggered on PR events + pull_request: + paths-ignore: + - "assembly-script/**" + - "ci/**" + - "doc/**" + - "test-tools/**" + # will be triggered on push events + push: + paths-ignore: + - "assembly-script/**" + - "ci/**" + - "doc/**" + - "test-tools/**" + # allow to be triggered manually + workflow_dispatch: + +# Cancel any in-flight jobs for the same PR/branch so there's only one active +# at a time +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + # Cancel any in-flight jobs for the same PR/branch so there's only one active + # at a time + cancel_previous: + runs-on: ubuntu-22.04 + steps: + - name: Cancel Workflow Action + uses: styfle/cancel-workflow-action@0.9.1 + with: + access_token: ${{ github.token }} + + build_iwasm_on_nuttx: + runs-on: ubuntu-22.04 + strategy: + matrix: + nuttx_board_config : [ + "stm32f4discovery:nsh", + "nucleo-h743zi:nsh", + "sabre-6quad:nsh", + ] + wamr_config_option: [ + "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\n", + "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\n", + "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\n", + "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\n", + ] + + steps: + - name: Install Toolchains + run: sudo apt install -y gcc-arm-none-eabi kconfig-frontends-nox genromfs + + - name: Checkout NuttX + uses: actions/checkout@v3 + with: + repository: apache/incubator-nuttx + path: nuttx + + - name: Checkout NuttX Apps + uses: actions/checkout@v3 + with: + repository: apache/incubator-nuttx-apps + path: apps + + - name: Checkout WAMR + uses: actions/checkout@v3 + with: + repository: ${{ github.repository }} + path: apps/interpreters/wamr/wamr + + - name: Enable WAMR for NuttX + run: find nuttx/boards -name defconfig | xargs sed -i '$a\CONFIG_EOL_IS_CR=y\n${{ matrix.wamr_config_option }}' + + - name: Build + run: | + cd nuttx + tools/configure.sh ${{ matrix.nuttx_board_config }} + make -j$(nproc) From 531a011700b8d86e436b2b398476ce42153922b5 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Sat, 3 Sep 2022 08:18:45 +0800 Subject: [PATCH 29/41] Move WASM_ENABLE_SPEC_TEST to correct place (#1447) Fix compilation warning: ``` wamr/product-mini/platforms/nuttx/../posix/main.c:622:5: warning: "WASM_ENABLE_SPEC_TEST" is not defined, evaluates to 0 [-Wundef] 622 | #if WASM_ENABLE_SPEC_TEST != 0 ``` Signed-off-by: Huang Qi --- core/config.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/config.h b/core/config.h index a00d241c6..e30449752 100644 --- a/core/config.h +++ b/core/config.h @@ -282,6 +282,10 @@ #define WASM_ENABLE_GLOBAL_HEAP_POOL 0 #endif +#ifndef WASM_ENABLE_SPEC_TEST +#define WASM_ENABLE_SPEC_TEST 0 +#endif + /* Global heap pool size in bytes */ #ifndef WASM_GLOBAL_HEAP_SIZE #if WASM_ENABLE_SPEC_TEST != 0 @@ -383,10 +387,6 @@ #endif #define BLOCK_ADDR_CONFLICT_SIZE 2 -#ifndef WASM_ENABLE_SPEC_TEST -#define WASM_ENABLE_SPEC_TEST 0 -#endif - /* Default max thread num per cluster. Can be overwrite by wasm_runtime_set_max_thread_num */ #define CLUSTER_MAX_THREAD_NUM 4 From ef8c6bd98aab0a3c6443e43f4256ba48c150392c Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 5 Sep 2022 12:56:24 +0900 Subject: [PATCH 30/41] doc/build_wasm_app.md: Update a note about LLVM 13 incompatibility (#1441) Remove the workaround as it wasn't so good. (It can cause the lack of ctor calls) Mention that it's only about a command. --- doc/build_wasm_app.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/doc/build_wasm_app.md b/doc/build_wasm_app.md index 29cc91101..5039e4bad 100644 --- a/doc/build_wasm_app.md +++ b/doc/build_wasm_app.md @@ -160,10 +160,9 @@ Firstly if libc-builtin (-nostdlib) mode meets the requirements, e.g. there are Most of the above methods are also available for libc-wasi mode, besides them, we can export malloc and free functions with `-Wl,--export=malloc -Wl,--export=free` option, so WAMR runtime will disable its app heap and call the malloc/free function exported to allocate/free the memory from/to the heap space managed by libc. -Note: wasm-ld from LLVM 13 and later automatically inserts dtor calls -for exported funtions. It breaks the malloc/free export mentioned above. -A workaround: Add `-Wl,--export=__wasm_call_ctors`, which happens to -prevent the automatic insertions for the current implementation. +Note: wasm-ld from LLVM 13 and later automatically inserts ctor/dtor calls +for all exported functions for a command. (vs reactor) +It breaks the malloc/free exports mentioned above. ## 3. Build wasm app with pthread support From 8fcc556e50432052db21d8330da2b5da84104925 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Mon, 5 Sep 2022 12:12:32 +0800 Subject: [PATCH 31/41] ci: Cover more configurations & targets on NuttX (#1448) Enable building iwasm on Nuttx for target: * x86 * cortex-m0 * cortex-m4 * cortex-m7 * rv32imac * rv64imac * rv64gc --- .github/workflows/compilation_on_nuttx.yml | 39 ++++++++++++++++++---- core/iwasm/aot/arch/aot_reloc_thumb.c | 32 +++++++++--------- product-mini/platforms/nuttx/wamr.mk | 4 ++- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/.github/workflows/compilation_on_nuttx.yml b/.github/workflows/compilation_on_nuttx.yml index e248ca42e..02392a907 100644 --- a/.github/workflows/compilation_on_nuttx.yml +++ b/.github/workflows/compilation_on_nuttx.yml @@ -43,20 +43,44 @@ jobs: strategy: matrix: nuttx_board_config : [ - "stm32f4discovery:nsh", - "nucleo-h743zi:nsh", - "sabre-6quad:nsh", + # x64 + "boards/sim/sim/sim/configs/nsh", + # cortex-m0 + "boards/arm/rp2040/raspberrypi-pico/configs/nsh", + # cortex-m4 + "boards/arm/stm32/stm32f4discovery/configs/nsh", + # cortex-m7 + "boards/arm/stm32h7/nucleo-h743zi/configs/nsh", + # cortex-a9 + "boards/arm/imx6/sabre-6quad/configs/nsh", + # riscv32imac + "boards/risc-v/qemu-rv/rv-virt/configs/nsh", + # riscv64imac + "boards/risc-v/qemu-rv/rv-virt/configs/nsh64", + # riscv64gc + "boards/risc-v/k210/maix-bit/configs/nsh", ] wamr_config_option: [ "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\n", "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\n", + "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_CLASSIC=y\\n", "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\nCONFIG_INTERPRETERS_WAMR_LIBC_BUILTIN=y\\n", "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_AOT=y\\n", + "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_FAST=y\\n", + "CONFIG_INTERPRETERS_WAMR=y\\nCONFIG_INTERPRETERS_WAMR_CLASSIC=y\\n", ] steps: - - name: Install Toolchains - run: sudo apt install -y gcc-arm-none-eabi kconfig-frontends-nox genromfs + - name: Install Utilities + run: sudo apt install -y kconfig-frontends-nox genromfs + + - name: Install ARM Compilers + if: ${{ contains(matrix.nuttx_board_config, 'arm') }} + run: sudo apt install -y gcc-arm-none-eabi + + - name: Install RISC-V Compilers + if: ${{ contains(matrix.nuttx_board_config, 'risc-v') }} + run: sudo apt install -y gcc-riscv64-unknown-elf - name: Checkout NuttX uses: actions/checkout@v3 @@ -77,7 +101,10 @@ jobs: path: apps/interpreters/wamr/wamr - name: Enable WAMR for NuttX - run: find nuttx/boards -name defconfig | xargs sed -i '$a\CONFIG_EOL_IS_CR=y\n${{ matrix.wamr_config_option }}' + run: | + find nuttx/boards -name defconfig | xargs sed -i '$a\CONFIG_EOL_IS_CR=y\n${{ matrix.wamr_config_option }}' + find nuttx/boards/sim -name defconfig | xargs sed -i '$a\CONFIG_LIBM=y\n' + find nuttx/boards/risc-v -name defconfig | xargs sed -i '$a\CONFIG_LIBM=y\n' - name: Build run: | diff --git a/core/iwasm/aot/arch/aot_reloc_thumb.c b/core/iwasm/aot/arch/aot_reloc_thumb.c index bfcf42330..627f334b2 100644 --- a/core/iwasm/aot/arch/aot_reloc_thumb.c +++ b/core/iwasm/aot/arch/aot_reloc_thumb.c @@ -97,8 +97,24 @@ static SymbolMap target_sym_map[] = { /* clang-format off */ REG_COMMON_SYMBOLS /* compiler-rt symbols that come from compiler(e.g. gcc) */ +#if __ARM_ARCH != 6 REG_SYM(__adddf3), REG_SYM(__addsf3), + REG_SYM(__divdf3), + REG_SYM(__extendsfdf2), + REG_SYM(__fixdfsi), + REG_SYM(__floatsidf), + REG_SYM(__floatsisf), + REG_SYM(__floatunsidf), + REG_SYM(__floatunsisf), + REG_SYM(__muldf3), + REG_SYM(__mulsf3), + REG_SYM(__subdf3), + REG_SYM(__subsf3), + REG_SYM(__truncdfsf2), + REG_SYM(__unorddf2), + REG_SYM(__unordsf2), +#endif /* clang-format on */ REG_SYM(__aeabi_d2iz), REG_SYM(__aeabi_d2lz), @@ -133,26 +149,19 @@ static SymbolMap target_sym_map[] = { REG_SYM(__aeabi_uldivmod), REG_SYM(__ashldi3), REG_SYM(__clzsi2), - REG_SYM(__divdf3), REG_SYM(__divdi3), REG_SYM(__divsi3), REG_SYM(__eqdf2), REG_SYM(__eqsf2), - REG_SYM(__extendsfdf2), REG_SYM(__fixdfdi), - REG_SYM(__fixdfsi), REG_SYM(__fixsfdi), REG_SYM(__fixunsdfdi), REG_SYM(__fixunsdfsi), REG_SYM(__fixunssfdi), REG_SYM(__floatdidf), REG_SYM(__floatdisf), - REG_SYM(__floatsidf), - REG_SYM(__floatsisf), REG_SYM(__floatundidf), REG_SYM(__floatundisf), - REG_SYM(__floatunsidf), - REG_SYM(__floatunsisf), REG_SYM(__gedf2), REG_SYM(__gesf2), REG_SYM(__gtdf2), @@ -164,21 +173,14 @@ static SymbolMap target_sym_map[] = { REG_SYM(__ltsf2), REG_SYM(__moddi3), REG_SYM(__modsi3), - REG_SYM(__muldf3), REG_SYM(__muldi3), - REG_SYM(__mulsf3), REG_SYM(__nedf2), REG_SYM(__nesf2), - REG_SYM(__subdf3), - REG_SYM(__subsf3), - REG_SYM(__truncdfsf2), REG_SYM(__udivdi3), REG_SYM(__udivmoddi4), REG_SYM(__udivsi3), REG_SYM(__umoddi3), REG_SYM(__umodsi3), - REG_SYM(__unorddf2), - REG_SYM(__unordsf2), }; static void @@ -375,7 +377,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, if (error_buf != NULL) snprintf(error_buf, error_buf_size, "Load relocation section failed: " - "invalid relocation type %d.", + "invalid relocation type %" PRId32 ".", reloc_type); return false; } diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 36953a67d..bebd5f2ad 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -5,7 +5,9 @@ CORE_ROOT := wamr/core IWASM_ROOT := wamr/core/iwasm SHARED_ROOT := wamr/core/shared -ifeq ($(CONFIG_ARCH_ARMV7A),y) +ifeq ($(CONFIG_ARCH_ARMV6M),y) +WAMR_BUILD_TARGET := THUMBV6M +else ifeq ($(CONFIG_ARCH_ARMV7A),y) WAMR_BUILD_TARGET := THUMBV7A else ifeq ($(CONFIG_ARCH_ARMV7M),y) WAMR_BUILD_TARGET := THUMBV7EM From 0ca271873df7fcdb13694209f7210cc9d7bceef0 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 5 Sep 2022 20:45:20 +0900 Subject: [PATCH 32/41] debug-engine: Handle wasm_debug_instance_get_current_object_name failure (#1453) Use an empty string instead of stack garbage. --- core/iwasm/libraries/debug-engine/handler.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/iwasm/libraries/debug-engine/handler.c b/core/iwasm/libraries/debug-engine/handler.c index f45638343..da07f80da 100644 --- a/core/iwasm/libraries/debug-engine/handler.c +++ b/core/iwasm/libraries/debug-engine/handler.c @@ -108,8 +108,11 @@ process_xfer(WASMGDBServer *server, const char *name, char *args) os_mutex_lock(&tmpbuf_lock); #if WASM_ENABLE_LIBC_WASI != 0 char objname[128]; - wasm_debug_instance_get_current_object_name( - (WASMDebugInstance *)server->thread->debug_instance, objname, 128); + if (!wasm_debug_instance_get_current_object_name( + (WASMDebugInstance *)server->thread->debug_instance, objname, + 128)) { + objname[0] = 0; /* use an empty string */ + } snprintf(tmpbuf, MAX_PACKET_SIZE, "l
", From a9cb9206d6e1dc57cdb4694d1be109f3ec4c4cf8 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Mon, 5 Sep 2022 21:56:23 +0800 Subject: [PATCH 33/41] Fix build issue of the meshoptimizer workload (#1452) meshoptimizer repo had changed, change the patch accordingly. --- .../workload/meshoptimizer/codecbench.patch | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/samples/workload/meshoptimizer/codecbench.patch b/samples/workload/meshoptimizer/codecbench.patch index 4c9063aa1..667b8ab28 100644 --- a/samples/workload/meshoptimizer/codecbench.patch +++ b/samples/workload/meshoptimizer/codecbench.patch @@ -1,10 +1,10 @@ diff --git a/CMakeLists.txt b/CMakeLists.txt -index f4378ce..9bc104b 100644 +index b13d946..4254003 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt -@@ -129,3 +129,43 @@ install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/meshoptimizerConfig.cmake +@@ -149,3 +149,43 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/meshoptimizerConfigVersion.cmake + COMPONENT meshoptimizer DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/meshoptimizer) + +################################################## @@ -47,10 +47,10 @@ index f4378ce..9bc104b 100644 + +add_dependencies(codecbench.opt codecbench) diff --git a/src/vertexcodec.cpp b/src/vertexcodec.cpp -index 5f3ec20..b79bfad 100644 +index 821c467..b7d30b1 100644 --- a/src/vertexcodec.cpp +++ b/src/vertexcodec.cpp -@@ -81,13 +81,13 @@ +@@ -83,13 +83,13 @@ #endif #ifdef SIMD_WASM @@ -71,7 +71,7 @@ index 5f3ec20..b79bfad 100644 #endif namespace meshopt -@@ -700,7 +700,7 @@ static v128_t decodeShuffleMask(unsigned char mask0, unsigned char mask1) +@@ -691,7 +691,7 @@ static v128_t decodeShuffleMask(unsigned char mask0, unsigned char mask1) v128_t sm1 = wasm_v128_load(&kDecodeBytesGroupShuffle[mask1]); v128_t sm1off = wasm_v128_load(&kDecodeBytesGroupCount[mask0]); @@ -80,7 +80,7 @@ index 5f3ec20..b79bfad 100644 v128_t sm1r = wasm_i8x16_add(sm1, sm1off); -@@ -751,7 +751,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi +@@ -741,7 +741,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi v128_t shuf = decodeShuffleMask(mask0, mask1); @@ -89,7 +89,7 @@ index 5f3ec20..b79bfad 100644 wasm_v128_store(buffer, result); -@@ -773,7 +773,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi +@@ -763,7 +763,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi v128_t shuf = decodeShuffleMask(mask0, mask1); @@ -99,10 +99,10 @@ index 5f3ec20..b79bfad 100644 wasm_v128_store(buffer, result); diff --git a/src/vertexfilter.cpp b/src/vertexfilter.cpp -index 023452c..2374cf7 100644 +index 14a73b1..8f4b3c1 100644 --- a/src/vertexfilter.cpp +++ b/src/vertexfilter.cpp -@@ -56,10 +56,10 @@ +@@ -57,10 +57,10 @@ #endif #ifdef SIMD_WASM From 729c4aeeaafaab724085c882cbbc6f637c5e0655 Mon Sep 17 00:00:00 2001 From: Zeuson <63415490+zeuson0@users.noreply.github.com> Date: Tue, 6 Sep 2022 14:29:58 +0800 Subject: [PATCH 34/41] Enable remote attestation by librats in SGX mode (#1445) Add library librats, update SGX build scripts, add sample and update document. --- README.md | 1 + build-scripts/config_common.cmake | 3 + build-scripts/runtime_lib.cmake | 5 ++ core/config.h | 4 + core/iwasm/common/wasm_native.c | 11 +++ core/iwasm/libraries/lib-rats/lib_rats.cmake | 33 ++++++++ .../libraries/lib-rats/lib_rats_wrapper.c | 60 ++++++++++++++ .../libraries/lib-rats/lib_rats_wrapper.h | 18 +++++ .../platforms/linux-sgx/CMakeLists.txt | 19 ++++- .../enclave-sample/Enclave/Enclave.edl | 5 ++ .../linux-sgx/enclave-sample/Makefile | 66 +++++++++++---- samples/sgx-ra/CMakeLists.txt | 81 +++++++++++++++++++ samples/sgx-ra/README.md | 42 ++++++++++ samples/sgx-ra/wasm-app/CMakeLists.txt | 38 +++++++++ samples/sgx-ra/wasm-app/main.c | 36 +++++++++ 15 files changed, 404 insertions(+), 18 deletions(-) create mode 100644 core/iwasm/libraries/lib-rats/lib_rats.cmake create mode 100644 core/iwasm/libraries/lib-rats/lib_rats_wrapper.c create mode 100644 core/iwasm/libraries/lib-rats/lib_rats_wrapper.h create mode 100644 samples/sgx-ra/CMakeLists.txt create mode 100644 samples/sgx-ra/README.md create mode 100644 samples/sgx-ra/wasm-app/CMakeLists.txt create mode 100644 samples/sgx-ra/wasm-app/main.c diff --git a/README.md b/README.md index 5a1444b51..7f419ec30 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ The WAMR [samples](./samples) integrate the iwasm VM core, application manager a - **[wasm-c-api](./samples/wasm-c-api/README.md)**: Demonstrating how to run some samples from [wasm-c-api proposal](https://github.com/WebAssembly/wasm-c-api) and showing the supported API's. - **[socket-api](./samples/socket-api/README.md)**: Demonstrating how to run wasm tcp server and tcp client applications, and how they communicate with each other. - **[workload](./samples/workload/README.md)**: Demonstrating how to build and run some complex workloads, e.g. tensorflow-lite, XNNPACK, wasm-av1, meshoptimizer and bwa. +- **[sgx-ra](./samples/sgx-ra/README.md)**: Demonstrating how to execute Remote Attestation on SGX with [librats](https://github.com/inclavare-containers/librats), which enables mutual attestation with other runtimes or other entities that support librats to ensure that each is running within the TEE. Project Technical Steering Committee diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 84b7e2bf7..8d99b93a4 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -215,6 +215,9 @@ endif () if (WAMR_BUILD_LIBC_EMCC EQUAL 1) message (" Libc emcc enabled") endif () +if (WAMR_BUILD_LIB_RATS EQUAL 1) + message (" Lib rats enabled") +endif() if (WAMR_BUILD_MINI_LOADER EQUAL 1) add_definitions (-DWASM_ENABLE_MINI_LOADER=1) message (" WASM mini loader enabled") diff --git a/build-scripts/runtime_lib.cmake b/build-scripts/runtime_lib.cmake index 75fb69132..967aa344a 100644 --- a/build-scripts/runtime_lib.cmake +++ b/build-scripts/runtime_lib.cmake @@ -118,6 +118,10 @@ if (WAMR_BUILD_LIBC_EMCC EQUAL 1) include (${IWASM_DIR}/libraries/libc-emcc/libc_emcc.cmake) endif () +if (WAMR_BUILD_LIB_RATS EQUAL 1) + include (${IWASM_DIR}/libraries/lib-rats/lib_rats.cmake) +endif () + ####################### Common sources ####################### if (NOT MSVC) set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -ffunction-sections -fdata-sections \ @@ -159,6 +163,7 @@ set (source_all ${LIB_PTHREAD_SOURCE} ${THREAD_MGR_SOURCE} ${LIBC_EMCC_SOURCE} + ${LIB_RATS_SOURCE} ${DEBUG_ENGINE_SOURCE} ) diff --git a/core/config.h b/core/config.h index e30449752..728bdc7e4 100644 --- a/core/config.h +++ b/core/config.h @@ -135,6 +135,10 @@ #define WASM_ENABLE_LIBC_EMCC 0 #endif +#ifndef WASM_ENABLE_LIB_RATS +#define WASM_ENABLE_LIB_RATS 0 +#endif + #ifndef WASM_ENABLE_LIB_PTHREAD #define WASM_ENABLE_LIB_PTHREAD 0 #endif diff --git a/core/iwasm/common/wasm_native.c b/core/iwasm/common/wasm_native.c index bbef1def4..ff1084ed9 100644 --- a/core/iwasm/common/wasm_native.c +++ b/core/iwasm/common/wasm_native.c @@ -53,6 +53,9 @@ get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis); uint32 get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis); +uint32 +get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis); + static bool compare_type_with_signautre(uint8 type, const char signature) { @@ -414,6 +417,14 @@ wasm_native_init() return false; #endif /* WASM_ENABLE_LIBC_EMCC */ +#if WASM_ENABLE_LIB_RATS != 0 + n_native_symbols = get_lib_rats_export_apis(&native_symbols); + if (n_native_symbols > 0 + && !wasm_native_register_natives("env", native_symbols, + n_native_symbols)) + return false; +#endif /* WASM_ENABLE_LIB_RATS */ + return true; } diff --git a/core/iwasm/libraries/lib-rats/lib_rats.cmake b/core/iwasm/libraries/lib-rats/lib_rats.cmake new file mode 100644 index 000000000..482fd9565 --- /dev/null +++ b/core/iwasm/libraries/lib-rats/lib_rats.cmake @@ -0,0 +1,33 @@ +# Copyright (c) 2022 Intel Corporation +# Copyright (c) 2020-2021 Alibaba Cloud +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +set (LIB_RATS_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_definitions (-DWASM_ENABLE_LIB_RATS=1) + +include_directories(${LIB_RATS_DIR}) + +include(FetchContent) + +set(RATS_BUILD_MODE "sgx" + CACHE INTERNAL "Select build mode for librats(host|occlum|sgx|wasm)") +set(RATS_INSTALL_PATH "${CMAKE_BINARY_DIR}/librats" CACHE INTERNAL "") + +FetchContent_Declare( + librats + GIT_REPOSITORY https://github.com/inclavare-containers/librats + GIT_TAG master +) +FetchContent_GetProperties(librats) +if (NOT librats_POPULATED) + message("-- Fetching librats ..") + FetchContent_Populate(librats) + include_directories("${librats_SOURCE_DIR}/include") + add_subdirectory(${librats_SOURCE_DIR} ${librats_BINARY_DIR} EXCLUDE_FROM_ALL) + +endif() + +file (GLOB source_all ${LIB_RATS_DIR}/*.c) + +set (LIB_RATS_SOURCE ${source_all}) \ No newline at end of file diff --git a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c new file mode 100644 index 000000000..575d17ba9 --- /dev/null +++ b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2022 Intel Corporation + * Copyright (c) 2020-2021 Alibaba Cloud + * + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include + +#include "wasm_export.h" +#include "bh_common.h" + +static uint32 +librats_collect_wrapper(wasm_exec_env_t exec_env, const uint8_t *hash) +{ + char *json = NULL; + char *str_ret; + uint32 len; + uint32 str_ret_offset = 0; + wasm_module_inst_t module_inst = get_module_inst(exec_env); + int code = librats_collect_evidence_to_json(hash, &json); + if (code != 0) { + return str_ret_offset; + } + if (json) { + len = (uint32)strlen(json) + 1; + + str_ret_offset = module_malloc(len, (void **)&str_ret); + if (str_ret_offset) { + bh_memcpy_s(str_ret, len, json, len); + } + } + return str_ret_offset; +} + +static int +librats_verify_wrapper(wasm_exec_env_t exec_env, const char *evidence_json, + const uint8_t *hash) +{ + return librats_verify_evidence_from_json(evidence_json, hash); +} + +/* clang-format off */ +#define REG_NATIVE_FUNC(func_name, signature) \ + { #func_name, func_name##_wrapper, signature, NULL } +/* clang-format off */ + +static NativeSymbol native_symbols_lib_rats[] = { + REG_NATIVE_FUNC(librats_collect, "($)i"), + REG_NATIVE_FUNC(librats_verify, "($$)i") +}; + +uint32_t +get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis) +{ + *p_lib_rats_apis = native_symbols_lib_rats; + return sizeof(native_symbols_lib_rats) / sizeof(NativeSymbol); +} \ No newline at end of file diff --git a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.h b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.h new file mode 100644 index 000000000..348b3b00c --- /dev/null +++ b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022 Intel Corporation + * Copyright (c) 2020-2021 Alibaba Cloud + * + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _RATS_WAMR_API_H +#define _RATS_WAMR_API_H + +#include + +char * +librats_collect(const uint8_t *hash); +int +librats_verify(const char *json_string, const uint8_t *hash); + +#endif \ No newline at end of file diff --git a/product-mini/platforms/linux-sgx/CMakeLists.txt b/product-mini/platforms/linux-sgx/CMakeLists.txt index 43f5bb9d4..dfc98975c 100644 --- a/product-mini/platforms/linux-sgx/CMakeLists.txt +++ b/product-mini/platforms/linux-sgx/CMakeLists.txt @@ -59,6 +59,11 @@ if (NOT DEFINED WAMR_BUILD_LIBC_WASI) set (WAMR_BUILD_LIBC_WASI 1) endif () +if (NOT DEFINED WAMR_BUILD_LIB_RATS) + # Disable lib rats support by default + set (WAMR_BUILD_LIB_RATS 0) +endif() + if (NOT DEFINED WAMR_BUILD_FAST_INTERP) # Enable fast interpreter set (WAMR_BUILD_FAST_INTERP 1) @@ -84,7 +89,7 @@ if (COLLECT_CODE_COVERAGE EQUAL 1) endif () set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") -set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -ffunction-sections -fdata-sections \ +set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -ffunction-sections -fdata-sections \ -Wall -Wno-unused-parameter -Wno-pedantic \ -nostdinc -fvisibility=hidden -fpie" ) @@ -100,3 +105,15 @@ add_custom_command ( COMMAND ${CMAKE_AR} rc libvmlib_untrusted.a untrusted/*.o) add_custom_target (vmlib_untrusted ALL DEPENDS libvmlib_untrusted.a) + +if (WAMR_BUILD_LIB_RATS EQUAL 1) + execute_process( + COMMAND bash -c "sed -i -E 's/^#define LIB_RATS 0 /#define LIB_RATS 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl" + OUTPUT_VARIABLE cmdOutput + ) +else() + execute_process( + COMMAND bash -c "sed -i -E 's/^#define LIB_RATS 1/#define LIB_RATS 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Enclave/Enclave.edl" + OUTPUT_VARIABLE cmdOutput + ) +endif() diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl index 3978465e0..d3281f849 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.edl @@ -3,10 +3,15 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +#define LIB_RATS 0 + enclave { from "sgx_tstdc.edl" import *; from "sgx_pthread.edl" import *; from "sgx_wamr.edl" import *; +#if LIB_RATS != 0 + from "rats.edl" import *; +#endif trusted { /* define ECALLs here. */ diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Makefile b/product-mini/platforms/linux-sgx/enclave-sample/Makefile index 45389628c..e1fea1e3c 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Makefile +++ b/product-mini/platforms/linux-sgx/enclave-sample/Makefile @@ -9,6 +9,13 @@ SGX_ARCH ?= x64 SGX_DEBUG ?= 0 SPEC_TEST ?= 0 +VMLIB_BUILD_DIR ?= $(CURDIR)/../build +LIB_RATS_SRC ?= $(VMLIB_BUILD_DIR)/_deps/librats-build +LIB_RATS := $(shell if [ -d $(LIB_RATS_SRC) ]; then echo 1; else echo 0; fi) + +LIB_RATS_INSTALL_DIR := $(VMLIB_BUILD_DIR)/librats/lib/librats +LIB_RATS_INCLUDE_DIR := $(VMLIB_BUILD_DIR)/librats/include + ifeq ($(shell getconf LONG_BIT), 32) SGX_ARCH := x86 else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32) @@ -49,6 +56,9 @@ endif App_Cpp_Files := App/App.cpp App_Include_Paths := -IApp -I$(SGX_SDK)/include +ifeq ($(LIB_RATS), 1) + App_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR) +endif App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths) @@ -79,6 +89,10 @@ else App_Link_Flags += -lsgx_uae_service endif +ifeq ($(LIB_RATS), 1) + App_Link_Flags += -L$(LIB_RATS_INSTALL_DIR) -lrats_u -lsgx_dcap_ql -lsgx_dcap_quoteverify -lsgx_ukey_exchange +endif + App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o) App_Name := iwasm @@ -105,21 +119,40 @@ Enclave_Include_Paths := -IEnclave -I$(WAMR_ROOT)/core/iwasm/include \ -I$(SGX_SDK)/include/tlibc \ -I$(SGX_SDK)/include/stlport +ifeq ($(LIB_RATS), 1) + Enclave_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR) +endif + Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths) ifeq ($(SPEC_TEST), 1) Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=1 else Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=0 endif -Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++03 -nostdinc++ -Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \ - -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \ - libvmlib.a \ - -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -lsgx_pthread -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \ + +ifeq ($(LIB_RATS), 1) + Rats_Lib_Link_Dirs := -L$(LIB_RATS_INSTALL_DIR) -L$(LIB_RATS_INSTALL_DIR)/attesters -L$(LIB_RATS_INSTALL_DIR)/verifiers + Rats_Lib_Link_libs := -lattester_nullattester -lattester_sgx_ecdsa -lattester_sgx_la \ + -lverifier_nullverifier -lverifier_sgx_ecdsa -lverifier_sgx_la -lverifier_sgx_ecdsa_qve \ + -lrats_lib +endif + +Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++11 -nostdinc++ +Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) ${Rats_Lib_Link_Dirs} \ + -Wl,--whole-archive -l$(Trts_Library_Name) ${Rats_Lib_Link_libs} -Wl,--no-whole-archive \ + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -lsgx_pthread -lsgx_tkey_exchange -l$(Crypto_Library_Name) -l$(Service_Library_Name) -lsgx_dcap_tvl -Wl,--end-group \ -Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \ -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \ -Wl,--defsym,__ImageBase=0 +Enclave_Edl_Search_Path = --search-path ../Enclave \ + --search-path $(SGX_SDK)/include \ + --search-path $(WAMR_ROOT)/core/shared/platform/linux-sgx +ifeq ($(LIB_RATS), 1) + Enclave_Edl_Search_Path += --search-path $(LIB_RATS_INCLUDE_DIR)/librats/edl +endif + + Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o) Enclave_Name := enclave.so @@ -156,12 +189,14 @@ ifneq ($(Build_Mode), HW_RELEASE) endif ######## App Objects ######## +librats: +ifeq ($(LIB_RATS), 1) + @cd $(LIB_RATS_SRC) && make install + @echo "librats build success" +endif -App/Enclave_u.c: $(SGX_EDGER8R) Enclave/Enclave.edl - @cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl \ - --search-path ../Enclave \ - --search-path $(SGX_SDK)/include \ - --search-path $(WAMR_ROOT)/core/shared/platform/linux-sgx +App/Enclave_u.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats + @cd App && $(SGX_EDGER8R) --untrusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path) @echo "GEN => $@" App/Enclave_u.o: App/Enclave_u.c @@ -172,7 +207,7 @@ App/%.o: App/%.cpp @$(CXX) $(App_Cpp_Flags) -c $< -o $@ @echo "CXX <= $<" -libvmlib_untrusted.a: ../build/libvmlib_untrusted.a +libvmlib_untrusted.a: $(VMLIB_BUILD_DIR)/libvmlib_untrusted.a @cp $< $@ @echo "CP $@ <= $<" @@ -183,11 +218,8 @@ $(App_Name): App/Enclave_u.o $(App_Cpp_Objects) libvmlib_untrusted.a ######## Enclave Objects ######## -Enclave/Enclave_t.c: $(SGX_EDGER8R) Enclave/Enclave.edl - @cd Enclave && $(SGX_EDGER8R) --trusted ../Enclave/Enclave.edl \ - --search-path ../Enclave \ - --search-path $(SGX_SDK)/include \ - --search-path $(WAMR_ROOT)/core/shared/platform/linux-sgx +Enclave/Enclave_t.c: $(SGX_EDGER8R) Enclave/Enclave.edl librats + @cd Enclave && $(SGX_EDGER8R) --trusted ../Enclave/Enclave.edl $(Enclave_Edl_Search_Path) @echo "GEN => $@" Enclave/Enclave_t.o: Enclave/Enclave_t.c @@ -198,7 +230,7 @@ Enclave/%.o: Enclave/%.cpp @$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@ @echo "CXX <= $<" -libvmlib.a: ../build/libvmlib.a +libvmlib.a: $(VMLIB_BUILD_DIR)/libvmlib.a @cp $< $@ @echo "CP $@ <= $<" diff --git a/samples/sgx-ra/CMakeLists.txt b/samples/sgx-ra/CMakeLists.txt new file mode 100644 index 000000000..331e540c4 --- /dev/null +++ b/samples/sgx-ra/CMakeLists.txt @@ -0,0 +1,81 @@ +# Copyright (c) 2022 Intel Corporation +# Copyright (c) 2020-2021 Alibaba Cloud +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required(VERSION 3.1.4) +project(sgx-ra) + +################ runtime settings ############## +set (WAMR_BUILD_PLATFORM "linux-sgx") + +# Reset default linker flags +set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") +set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") + +# Set WAMR_BUILD_TARGET +if (NOT DEFINED WAMR_BUILD_TARGET) + if (CMAKE_SIZEOF_VOID_P EQUAL 8) + # Build as X86_64 by default in 64-bit platform + set (WAMR_BUILD_TARGET "X86_64") + elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) + # Build as X86_32 by default in 32-bit platform + set (WAMR_BUILD_TARGET "X86_32") + else () + message(SEND_ERROR "Unsupported build target platform!") + endif () +endif () + +if (NOT CMAKE_BUILD_TYPE) + set (CMAKE_BUILD_TYPE Release) +endif () + +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_JIT 0) +set (WAMR_BUILD_LIBC_BUILTIN 1) +set (WAMR_BUILD_LIBC_WASI 1) +set (WAMR_BUILD_LIB_PTHREAD 1) +set (WAMR_BUILD_FAST_INTERP 1) +set (WAMR_BUILD_LIB_RATS 1) + +# compiling and linking flags +if (COLLECT_CODE_COVERAGE EQUAL 1) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") +endif () + +set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") +set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -ffunction-sections -fdata-sections \ + -Wall -Wno-unused-parameter -Wno-pedantic \ + -nostdinc -fvisibility=hidden -fpie" ) + +# build out vmlib +set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) +set (SGX_PLATFORM_DIR ${WAMR_ROOT_DIR}/product-mini/platforms/linux-sgx) +include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) + +add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) + +add_custom_command ( + OUTPUT libvmlib_untrusted.a + COMMAND mkdir -p untrusted && cd untrusted && + ${CMAKE_C_COMPILER} -c ${PLATFORM_SHARED_SOURCE_UNTRUSTED} + COMMAND ${CMAKE_AR} rc libvmlib_untrusted.a untrusted/*.o) + +add_custom_target (vmlib_untrusted ALL DEPENDS libvmlib_untrusted.a) + +execute_process ( + COMMAND bash -c "sed -i -E 's/^#define LIB_RATS 0/#define LIB_RATS 1/g' ${SGX_PLATFORM_DIR}/enclave-sample/Enclave/Enclave.edl" + OUTPUT_VARIABLE cmdOutput +) + +################ wamr runtime ################### +add_custom_target ( + iwasm ALL + DEPENDS vmlib_untrusted vmlib_untrusted + COMMAND make -C ${SGX_PLATFORM_DIR}/enclave-sample SGX_MODE=HW SGX_DEBUG=1 VMLIB_BUILD_DIR=${CMAKE_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E copy ${SGX_PLATFORM_DIR}/enclave-sample/enclave.signed.so ${CMAKE_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E copy ${SGX_PLATFORM_DIR}/enclave-sample/iwasm ${CMAKE_BINARY_DIR} + COMMAND make -C ${SGX_PLATFORM_DIR}/enclave-sample clean) + +################ wasm application ############### +add_subdirectory(wasm-app) diff --git a/samples/sgx-ra/README.md b/samples/sgx-ra/README.md new file mode 100644 index 000000000..92b5138ee --- /dev/null +++ b/samples/sgx-ra/README.md @@ -0,0 +1,42 @@ +"sgx-ra" sample introduction +============== + +This sample demonstrates how to execute Remote Attestation on SGX with [librats](https://github.com/inclavare-containers/librats) and run it with iwasm. It can only build on [SGX supported processors](https://www.intel.com/content/www/us/en/support/articles/000028173/processors.html), please check it. + +## Preparation + +Before staring, we need to download and intall [SGX SDK](https://download.01.org/intel-sgx/latest/linux-latest/distro) and [SGX DCAP Library](https://download.01.org/intel-sgx/latest/dcap-latest) referring to this [guide](https://download.01.org/intel-sgx/sgx-dcap/1.8/linux/docs/Intel_SGX_DCAP_Linux_SW_Installation_Guide.pdf). + +The following command is the example of the SGX environment installation on ubuntu18.04. +``` shell +$ cd $HOME +$ # Set your platform, you can get the platforms list on +$ # https://download.01.org/intel-sgx/latest/linux-latest/distro +$ SGX_PALTFORM=ubuntu18.04-server +$ SGX_SDK_VERSION=2.17.100.3 +$ SGX_DRIVER_VERSION=1.41 +$ # install SGX Driver +$ wget https://download.01.org/intel-sgx/latest/linux-latest/distro/$SGX_PALTFORM/sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin +$ chmod +x sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin +$ sudo ./sgx_linux_x64_driver_$SGX_DRIVER_VERSION.bin +$ # install SGX SDK +$ wget https://download.01.org/intel-sgx/latest/linux-latest/distro/$SGX_PALTFORM/sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin +$ chmod +x sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin +$ sudo ./sgx_linux_x64_sdk_$SGX_SDK_VERSION.bin +$ # install SGX DCAP Library +$ echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu bionic main' | sudo tee /etc/apt/sources.list.d/intel-sgx.list > /dev/null +$ wget -O - https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add - +$ sudo apt update +$ sudo apt install libsgx-uae-service libsgx-dcap-default-qpl-dev libsgx-dcap-ql-dev libsgx-dcap-quote-verify-dev +``` + +## Build +``` shell +$ mkdir build && cd build +$ cmake .. +$ make +$ # run the sample +$ ./iwasm wasm-app/test.wasm +``` + +The sample will print the evidence in json and "Evidence is trusted." by default. \ No newline at end of file diff --git a/samples/sgx-ra/wasm-app/CMakeLists.txt b/samples/sgx-ra/wasm-app/CMakeLists.txt new file mode 100644 index 000000000..afba7dfb6 --- /dev/null +++ b/samples/sgx-ra/wasm-app/CMakeLists.txt @@ -0,0 +1,38 @@ +# Copyright (c) 2022 Intel Corporation +# Copyright (c) 2020-2021 Alibaba Cloud +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required(VERSION 3.0) +project(wasm-app) + +set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..) +set (LIB_RATS_DIR ${WAMR_ROOT_DIR}/core/iwasm/libraries/lib-rats) + +set (CMAKE_C_LINK_FLAGS "") +set (CMAKE_CXX_LINK_FLAGS "") +if (APPLE) + set (HAVE_FLAG_SEARCH_PATHS_FIRST 0) +endif () + +set (CMAKE_SYSTEM_PROCESSOR wasm32) +set (CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot) + +if (NOT DEFINED WASI_SDK_DIR) + set (WASI_SDK_DIR "/opt/wasi-sdk") +endif () + +set (CMAKE_C_FLAGS "-nostdlib") +set (CMAKE_C_COMPILER_TARGET "wasm32") +set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang") + +set (CMAKE_EXE_LINKER_FLAGS + "-Wl,--max-memory=131072 -z stack-size=8192 \ + -Wl,--no-entry,--strip-all \ + -Wl,--export=__main_argc_argv \ + -Wl,--export=__heap_base,--export=__data_end \ + -Wl,--allow-undefined" +) + +add_executable(test.wasm main.c) +set_target_properties(test.wasm PROPERTIES INCLUDE_DIRECTORIES ${LIB_RATS_DIR}) +target_link_libraries(test.wasm) diff --git a/samples/sgx-ra/wasm-app/main.c b/samples/sgx-ra/wasm-app/main.c new file mode 100644 index 000000000..d6b079c4e --- /dev/null +++ b/samples/sgx-ra/wasm-app/main.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Intel Corporation + * Copyright (c) 2020-2021 Alibaba Cloud + * + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include "lib_rats_wrapper.h" + +int +main(int argc, char **argv) +{ + char *evidence_json = NULL; + const char *hash = "12345678123456781234567812345678"; + evidence_json = librats_collect((const uint8_t *)hash); + if (evidence_json == NULL) { + printf("Librats collect evidence failed.\n"); + return -1; + } + printf("evidence json:\n%s\n", evidence_json); + + if (librats_verify(evidence_json, (const uint8_t *)hash) != 0) { + printf("Evidence is not trusted.\n"); + } + else { + printf("Evidence is trusted.\n"); + } + + if (evidence_json) { + free(evidence_json); + } + + return 0; +} From 5722fcc40a633979e8cbbde6fff528628b97d891 Mon Sep 17 00:00:00 2001 From: Xu Jun <693788454@qq.com> Date: Wed, 7 Sep 2022 12:56:50 +0800 Subject: [PATCH 35/41] Fix issue in multi-thread sample (#1458) Set `WAMR_BUILD_LIB_PTHREAD_SEMAPHORE` for the sample, and update document. --- core/shared/utils/bh_list.h | 2 +- doc/build_wamr.md | 4 ++++ samples/multi-thread/CMakeLists.txt | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/shared/utils/bh_list.h b/core/shared/utils/bh_list.h index eaf15f2f3..f10215324 100644 --- a/core/shared/utils/bh_list.h +++ b/core/shared/utils/bh_list.h @@ -59,7 +59,7 @@ bh_list_init(bh_list *list); * BH_LIST_ERROR if input is invalid or no memory * available. */ -extern bh_list_status +bh_list_status bh_list_insert(bh_list *list, void *elem); /** diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 555ca2ac0..298664055 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -77,6 +77,10 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM - **WAMR_BUILD_LIB_PTHREAD**=1/0, default to disable if not set > Note: The dependent feature of lib pthread such as the `shared memory` and `thread manager` will be enabled automatically. +#### **Enable lib-pthread-semaphore** +- **WAMR_BUILD_LIB_PTHREAD_SEMAPHORE**=1/0, default to disable if not set +> Note: This feature depends on `lib-pthread`, it will be enabled automatically if this feature is enabled. + #### **Disable boundary check with hardware trap in AOT or JIT mode** - **WAMR_DISABLE_HW_BOUND_CHECK**=1/0, default to enable if not set and supported by platform > Note: by default only platform linux/darwin/android/vxworks 64-bit will enable boundary check with hardware trap in AOT or JIT mode, and the wamrc tool will generate AOT code without boundary check instructions in all 64-bit targets except SGX to improve performance. diff --git a/samples/multi-thread/CMakeLists.txt b/samples/multi-thread/CMakeLists.txt index 63c14eebd..39485e1c1 100644 --- a/samples/multi-thread/CMakeLists.txt +++ b/samples/multi-thread/CMakeLists.txt @@ -45,6 +45,7 @@ set(WAMR_BUILD_JIT 0) set(WAMR_BUILD_LIBC_BUILTIN 1) set(WAMR_BUILD_FAST_INTERP 1) set(WAMR_BUILD_LIB_PTHREAD 1) +set(WAMR_BUILD_LIB_PTHREAD_SEMAPHORE 1) # compiling and linking flags set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE") From 8a2c1a2471543505cc2311e5ccd38335160ed080 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 7 Sep 2022 13:50:11 +0800 Subject: [PATCH 36/41] Fix build error on alios platform (#1459) Add the definition of `korp_sem` in platform_internal.h. --- core/shared/platform/alios/platform_internal.h | 1 + 1 file changed, 1 insertion(+) diff --git a/core/shared/platform/alios/platform_internal.h b/core/shared/platform/alios/platform_internal.h index 07b8f75e2..ac9fb5cab 100644 --- a/core/shared/platform/alios/platform_internal.h +++ b/core/shared/platform/alios/platform_internal.h @@ -30,6 +30,7 @@ typedef aos_task_t korp_thread; typedef korp_thread *korp_tid; typedef aos_task_t *aos_tid_t; typedef aos_mutex_t korp_mutex; +typedef aos_sem_t korp_sem; struct os_thread_wait_node; typedef struct os_thread_wait_node *os_thread_wait_list; From 0a08b9266244b848f682afe936b6d41fd11bc9a6 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 7 Sep 2022 15:30:39 +0900 Subject: [PATCH 37/41] VSCode-Extension: Add empty platform directories (#1460) --- .../wamr-ide/VSCode-Extension/resource/debug/linux/.placeholder | 0 .../wamr-ide/VSCode-Extension/resource/debug/osx/.placeholder | 0 .../wamr-ide/VSCode-Extension/resource/debug/windows/.placeholder | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 test-tools/wamr-ide/VSCode-Extension/resource/debug/linux/.placeholder create mode 100644 test-tools/wamr-ide/VSCode-Extension/resource/debug/osx/.placeholder create mode 100644 test-tools/wamr-ide/VSCode-Extension/resource/debug/windows/.placeholder diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/debug/linux/.placeholder b/test-tools/wamr-ide/VSCode-Extension/resource/debug/linux/.placeholder new file mode 100644 index 000000000..e69de29bb diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/debug/osx/.placeholder b/test-tools/wamr-ide/VSCode-Extension/resource/debug/osx/.placeholder new file mode 100644 index 000000000..e69de29bb diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/debug/windows/.placeholder b/test-tools/wamr-ide/VSCode-Extension/resource/debug/windows/.placeholder new file mode 100644 index 000000000..e69de29bb From da79e3e9b283272a63ad53b24310d88c640fbd8a Mon Sep 17 00:00:00 2001 From: Qiang <56512053+FromLiQg@users.noreply.github.com> Date: Wed, 7 Sep 2022 16:52:35 +0800 Subject: [PATCH 38/41] Add more checks for Fast JIT gen insn and jit_lock_reg_in_insn (#1449) Add more checks for Fast JIT generating insn and jit_lock_reg_in_insn in compile_int_div_no_check and other places to avoid accessing NULL insn. --- core/iwasm/fast-jit/fe/jit_emit_numberic.c | 77 ++++++++++++++++++---- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/core/iwasm/fast-jit/fe/jit_emit_numberic.c b/core/iwasm/fast-jit/fe/jit_emit_numberic.c index a382facf0..3b2f21af8 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_numberic.c +++ b/core/iwasm/fast-jit/fe/jit_emit_numberic.c @@ -539,8 +539,13 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op, insn = GEN_INSN(DIV_U, rax_hreg, rax_hreg, right); } - jit_lock_reg_in_insn(cc, insn, eax_hreg); - jit_lock_reg_in_insn(cc, insn, edx_hreg); + if (!insn) { + goto fail; + } + if (!jit_lock_reg_in_insn(cc, insn, eax_hreg) + || !jit_lock_reg_in_insn(cc, insn, edx_hreg)) { + goto fail; + } if (is_i32) { res = jit_cc_new_reg_I32(cc); @@ -551,9 +556,12 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op, insn1 = jit_insn_new_MOV(res, rax_hreg); } - if (insn && insn1) { - jit_insn_insert_after(insn, insn1); + if (!insn1) { + jit_set_last_error(cc, "generate insn failed"); + goto fail; } + + jit_insn_insert_after(insn, insn1); break; } case INT_REM_S: @@ -576,8 +584,13 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op, insn = GEN_INSN(REM_U, rdx_hreg, rax_hreg, right); } - jit_lock_reg_in_insn(cc, insn, eax_hreg); - jit_lock_reg_in_insn(cc, insn, edx_hreg); + if (!insn) { + goto fail; + } + if (!jit_lock_reg_in_insn(cc, insn, eax_hreg) + || !jit_lock_reg_in_insn(cc, insn, edx_hreg)) { + goto fail; + } if (is_i32) { res = jit_cc_new_reg_I32(cc); @@ -588,9 +601,12 @@ compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op, insn1 = jit_insn_new_MOV(res, rdx_hreg); } - if (insn && insn1) { - jit_insn_insert_after(insn, insn1); + if (!insn1) { + jit_set_last_error(cc, "generate insn failed"); + goto fail; } + + jit_insn_insert_after(insn, insn1); break; } #else @@ -1133,13 +1149,20 @@ compile_int_shl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32) #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right); insn = GEN_INSN(SHL, res, left, is_i32 ? ecx_hreg : rcx_hreg); - jit_lock_reg_in_insn(cc, insn, ecx_hreg); + if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) { + goto fail; + } #else GEN_INSN(SHL, res, left, right); + if (jit_get_last_error(cc)) { + goto fail; + } #endif shortcut: return res; +fail: + return (JitReg)0; } static JitReg @@ -1164,13 +1187,20 @@ compile_int_shrs(JitCompContext *cc, JitReg left, JitReg right, bool is_i32) #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right); insn = GEN_INSN(SHRS, res, left, is_i32 ? ecx_hreg : rcx_hreg); - jit_lock_reg_in_insn(cc, insn, ecx_hreg); + if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) { + goto fail; + } #else GEN_INSN(SHRS, res, left, right); + if (jit_get_last_error(cc)) { + goto fail; + } #endif shortcut: return res; +fail: + return (JitReg)0; } static JitReg @@ -1195,13 +1225,20 @@ compile_int_shru(JitCompContext *cc, JitReg left, JitReg right, bool is_i32) #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right); insn = GEN_INSN(SHRU, res, left, is_i32 ? ecx_hreg : rcx_hreg); - jit_lock_reg_in_insn(cc, insn, ecx_hreg); + if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) { + goto fail; + } #else GEN_INSN(SHRU, res, left, right); + if (jit_get_last_error(cc)) { + goto fail; + } #endif shortcut: return res; +fail: + return (JitReg)0; } DEF_UNI_INT_CONST_OPS(rotl) @@ -1257,13 +1294,20 @@ compile_int_rotl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32) #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right); insn = GEN_INSN(ROTL, res, left, is_i32 ? ecx_hreg : rcx_hreg); - jit_lock_reg_in_insn(cc, insn, ecx_hreg); + if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) { + goto fail; + } #else GEN_INSN(ROTL, res, left, right); + if (jit_get_last_error(cc)) { + goto fail; + } #endif shortcut: return res; +fail: + return (JitReg)0; } DEF_UNI_INT_CONST_OPS(rotr) @@ -1319,13 +1363,20 @@ compile_int_rotr(JitCompContext *cc, JitReg left, JitReg right, bool is_i32) #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right); insn = GEN_INSN(ROTR, res, left, is_i32 ? ecx_hreg : rcx_hreg); - jit_lock_reg_in_insn(cc, insn, ecx_hreg); + if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) { + goto fail; + } #else GEN_INSN(ROTR, res, left, right); + if (jit_get_last_error(cc)) { + goto fail; + } #endif shortcut: return res; +fail: + return (JitReg)0; } static bool From 6820af621265413b677e450cf126bb66c9f77077 Mon Sep 17 00:00:00 2001 From: Qiang <56512053+FromLiQg@users.noreply.github.com> Date: Thu, 8 Sep 2022 14:22:34 +0800 Subject: [PATCH 39/41] Fix a potential memory leak issue in wasm_native_init (#1465) Should call `wasm_native_destroy` to destroy the registered native libs when registering native lib failed in `wasm_native_init`. --- core/iwasm/common/wasm_native.c | 23 +++++++++++-------- .../libraries/lib-rats/lib_rats_wrapper.c | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/core/iwasm/common/wasm_native.c b/core/iwasm/common/wasm_native.c index ff1084ed9..c44d28310 100644 --- a/core/iwasm/common/wasm_native.c +++ b/core/iwasm/common/wasm_native.c @@ -362,24 +362,24 @@ wasm_native_init() #if WASM_ENABLE_LIBC_BUILTIN != 0 n_native_symbols = get_libc_builtin_export_apis(&native_symbols); if (!wasm_native_register_natives("env", native_symbols, n_native_symbols)) - return false; + goto fail; #endif /* WASM_ENABLE_LIBC_BUILTIN */ #if WASM_ENABLE_SPEC_TEST n_native_symbols = get_spectest_export_apis(&native_symbols); if (!wasm_native_register_natives("spectest", native_symbols, n_native_symbols)) - return false; + goto fail; #endif /* WASM_ENABLE_SPEC_TEST */ #if WASM_ENABLE_LIBC_WASI != 0 n_native_symbols = get_libc_wasi_export_apis(&native_symbols); if (!wasm_native_register_natives("wasi_unstable", native_symbols, n_native_symbols)) - return false; + goto fail; if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols, n_native_symbols)) - return false; + goto fail; #endif #if WASM_ENABLE_BASE_LIB != 0 @@ -387,7 +387,7 @@ wasm_native_init() if (n_native_symbols > 0 && !wasm_native_register_natives("env", native_symbols, n_native_symbols)) - return false; + goto fail; #endif #if WASM_ENABLE_APP_FRAMEWORK != 0 @@ -395,18 +395,18 @@ wasm_native_init() if (n_native_symbols > 0 && !wasm_native_register_natives("env", native_symbols, n_native_symbols)) - return false; + goto fail; #endif #if WASM_ENABLE_LIB_PTHREAD != 0 if (!lib_pthread_init()) - return false; + goto fail; n_native_symbols = get_lib_pthread_export_apis(&native_symbols); if (n_native_symbols > 0 && !wasm_native_register_natives("env", native_symbols, n_native_symbols)) - return false; + goto fail; #endif #if WASM_ENABLE_LIBC_EMCC != 0 @@ -414,7 +414,7 @@ wasm_native_init() if (n_native_symbols > 0 && !wasm_native_register_natives("env", native_symbols, n_native_symbols)) - return false; + goto fail; #endif /* WASM_ENABLE_LIBC_EMCC */ #if WASM_ENABLE_LIB_RATS != 0 @@ -422,10 +422,13 @@ wasm_native_init() if (n_native_symbols > 0 && !wasm_native_register_natives("env", native_symbols, n_native_symbols)) - return false; + goto fail; #endif /* WASM_ENABLE_LIB_RATS */ return true; +fail: + wasm_native_destroy(); + return false; } void diff --git a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c index 575d17ba9..e89337fd4 100644 --- a/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c +++ b/core/iwasm/libraries/lib-rats/lib_rats_wrapper.c @@ -45,7 +45,7 @@ librats_verify_wrapper(wasm_exec_env_t exec_env, const char *evidence_json, /* clang-format off */ #define REG_NATIVE_FUNC(func_name, signature) \ { #func_name, func_name##_wrapper, signature, NULL } -/* clang-format off */ +/* clang-format on */ static NativeSymbol native_symbols_lib_rats[] = { REG_NATIVE_FUNC(librats_collect, "($)i"), From 8a7dd4dc3ed417008ec1685e8d934b4ccb7ad0da Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 8 Sep 2022 15:38:16 +0800 Subject: [PATCH 40/41] Remove handling unsupported opcodes in loader (#1464) Remove handling opcode DROP_64/SELECT_64 in loader stage prepare_bytecode, as they are the modified opcodes of DROP/SELECT for optimization purpose, but not the opcodes defined by spec. --- core/iwasm/interpreter/wasm_loader.c | 2 -- core/iwasm/interpreter/wasm_mini_loader.c | 2 -- 2 files changed, 4 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 6fa0a21d1..0aeda3eca 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -7313,7 +7313,6 @@ re_scan: } case WASM_OP_DROP: - case WASM_OP_DROP_64: { BranchBlock *cur_block = loader_ctx->frame_csp - 1; int32 available_stack_cell = @@ -7381,7 +7380,6 @@ re_scan: } case WASM_OP_SELECT: - case WASM_OP_SELECT_64: { uint8 ref_type; BranchBlock *cur_block = loader_ctx->frame_csp - 1; diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index f3c649ff5..ac240689c 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -5519,7 +5519,6 @@ re_scan: } case WASM_OP_DROP: - case WASM_OP_DROP_64: { BranchBlock *cur_block = loader_ctx->frame_csp - 1; int32 available_stack_cell = @@ -5572,7 +5571,6 @@ re_scan: } case WASM_OP_SELECT: - case WASM_OP_SELECT_64: { uint8 ref_type; BranchBlock *cur_block = loader_ctx->frame_csp - 1; From 75936680662ddc31af6763daa55a622422f9e2f5 Mon Sep 17 00:00:00 2001 From: Wang Ning Date: Thu, 8 Sep 2022 17:34:24 +0800 Subject: [PATCH 41/41] WARM-IDE: Add check for invalid project name when creating new project (#1461) --- .../resource/webview/css/style.css | 2 +- .../src/utilities/directoryUtilities.ts | 24 ++++++ .../src/view/NewProjectPanel.ts | 76 ++++++++++--------- 3 files changed, 66 insertions(+), 36 deletions(-) diff --git a/test-tools/wamr-ide/VSCode-Extension/resource/webview/css/style.css b/test-tools/wamr-ide/VSCode-Extension/resource/webview/css/style.css index 8975ecf7e..bff10e5b4 100644 --- a/test-tools/wamr-ide/VSCode-Extension/resource/webview/css/style.css +++ b/test-tools/wamr-ide/VSCode-Extension/resource/webview/css/style.css @@ -59,7 +59,7 @@ } #ipt_projName, -#tselect_dropdown, +#select_dropdown, .config_form_body vscode-text-field, .config_form_body vscode-text-area { width: 100%; diff --git a/test-tools/wamr-ide/VSCode-Extension/src/utilities/directoryUtilities.ts b/test-tools/wamr-ide/VSCode-Extension/src/utilities/directoryUtilities.ts index 35b8a4cf8..cff54c7f0 100644 --- a/test-tools/wamr-ide/VSCode-Extension/src/utilities/directoryUtilities.ts +++ b/test-tools/wamr-ide/VSCode-Extension/src/utilities/directoryUtilities.ts @@ -6,6 +6,7 @@ import fileSystem = require('fs'); import vscode = require('vscode'); import path = require('path'); +import os = require('os'); /** * @@ -94,3 +95,26 @@ export function CheckIfDirectoryExist(path: string): boolean { return false; } } + +export function checkFolderName(folderName: string) { + let invalidCharacterArr: string[] = []; + var valid = true; + + if (folderName.length > 255) { + valid = false; + } + + if (os.platform() === 'win32') { + invalidCharacterArr = ['\\', '/', ':', '?', '*', '"', '|', '<', '>']; + } else if (os.platform() === 'linux' || os.platform() === 'darwin') { + invalidCharacterArr = ['/']; + } + + invalidCharacterArr.forEach(function (c) { + if (folderName.indexOf(c) !== -1) { + valid = false; + } + }); + + return valid; +} diff --git a/test-tools/wamr-ide/VSCode-Extension/src/view/NewProjectPanel.ts b/test-tools/wamr-ide/VSCode-Extension/src/view/NewProjectPanel.ts index 17732ec00..a7536564f 100644 --- a/test-tools/wamr-ide/VSCode-Extension/src/view/NewProjectPanel.ts +++ b/test-tools/wamr-ide/VSCode-Extension/src/view/NewProjectPanel.ts @@ -6,7 +6,12 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; -import { CreateDirectory, CopyFiles } from '../utilities/directoryUtilities'; +import * as os from 'os'; +import { + CreateDirectory, + CopyFiles, + checkFolderName, +} from '../utilities/directoryUtilities'; import { getUri } from '../utilities/getUri'; export class NewProjectPanel { @@ -15,13 +20,11 @@ export class NewProjectPanel { private readonly _panel: vscode.WebviewPanel; private _disposables: vscode.Disposable[] = []; - static readonly USER_INTPUT_ERR: number = -2; - static readonly DIR_EXSITED_ERR: number = -1; static readonly EXCUTION_SUCCESS: number = 0; + static readonly DIR_EXSITED_ERR: number = -1; + static readonly USER_INTPUT_ERR: number = -2; + static readonly DIR_PATH_INVALID_ERR: number = -3; - /** - * @param context extension context from extension.ts active func - */ constructor(extensionUri: vscode.Uri, panel: vscode.WebviewPanel) { this._panel = panel; this._panel.webview.html = this._getHtmlForWebview( @@ -33,9 +36,6 @@ export class NewProjectPanel { this._panel.onDidDispose(this.dispose, null, this._disposables); } - /** - * @param context - */ public static render(context: vscode.ExtensionContext) { NewProjectPanel.USER_SET_WORKSPACE = vscode.workspace .getConfiguration() @@ -55,7 +55,6 @@ export class NewProjectPanel { } ); - /* create new project panel obj */ NewProjectPanel.currentPanel = new NewProjectPanel( context.extensionUri, panel @@ -63,10 +62,6 @@ export class NewProjectPanel { } } - /** - * @param projName project name input by user - * @param template - */ private _creatNewProject( projName: string, template: string, @@ -76,22 +71,23 @@ export class NewProjectPanel { return NewProjectPanel.USER_INTPUT_ERR; } + if (!checkFolderName(projName)) { + return NewProjectPanel.DIR_PATH_INVALID_ERR; + } + let ROOT_PATH = path.join(NewProjectPanel.USER_SET_WORKSPACE, projName); let EXT_PATH = extensionUri.fsPath; - /* if the direcotry has exsited, then ignore the creation and return */ if (fs.existsSync(ROOT_PATH)) { if (fs.lstatSync(ROOT_PATH).isDirectory()) { return NewProjectPanel.DIR_EXSITED_ERR; } } - /* create necessary floders under the project directory */ CreateDirectory(path.join(ROOT_PATH, '.wamr')); CreateDirectory(path.join(ROOT_PATH, 'include')); CreateDirectory(path.join(ROOT_PATH, 'src')); - /* copy scripts files to project_root_path/.wamr */ CopyFiles( path.join(EXT_PATH, 'resource/scripts/CMakeLists.txt'), path.join(ROOT_PATH, '.wamr/CMakeLists.txt') @@ -110,7 +106,6 @@ export class NewProjectPanel { extensionUri: vscode.Uri, templatePath: string ) { - /* get toolkit uri */ const toolkitUri = getUri(webview, extensionUri, [ 'node_modules', '@vscode', @@ -147,29 +142,26 @@ export class NewProjectPanel { webview: vscode.Webview, extensionUri: vscode.Uri ) { - // Handle messages from the webview webview.onDidReceiveMessage( message => { switch (message.command) { case 'create_new_project': + let createNewProjectStatus = this._creatNewProject( + message.projectName, + message.template, + extensionUri + ); if ( - this._creatNewProject( - message.projectName, - message.template, - extensionUri - ) === NewProjectPanel.EXCUTION_SUCCESS + createNewProjectStatus === + NewProjectPanel.EXCUTION_SUCCESS ) { - /* post message to page to inform the project creation has finished */ webview.postMessage({ command: 'proj_creation_finish', prjName: message.projectName, }); } else if ( - this._creatNewProject( - message.projectName, - message.template, - extensionUri - ) === NewProjectPanel.DIR_EXSITED_ERR + createNewProjectStatus === + NewProjectPanel.DIR_EXSITED_ERR ) { vscode.window.showErrorMessage( 'Project : ' + @@ -178,16 +170,30 @@ export class NewProjectPanel { ); return; } else if ( - this._creatNewProject( - message.projectName, - message.template, - extensionUri - ) === NewProjectPanel.USER_INTPUT_ERR + createNewProjectStatus === + NewProjectPanel.USER_INTPUT_ERR ) { vscode.window.showErrorMessage( 'Please fill chart before your submit!' ); return; + } else if ( + createNewProjectStatus === + NewProjectPanel.DIR_PATH_INVALID_ERR + ) { + if (os.platform() === 'win32') { + vscode.window.showErrorMessage( + "A file name can't contain any of the following characters: ' / \\ : * ? < > | ' and the length should be less than 255" + ); + } else if ( + os.platform() === 'linux' || + os.platform() === 'darwin' + ) { + vscode.window.showErrorMessage( + "A file name can't contain following characters: '/' and the length should be less than 255" + ); + } + return; } return;