From 7a287fd1a90836bb3de86eba9889cd689bd493ab Mon Sep 17 00:00:00 2001 From: wenyongh Date: Mon, 8 Jun 2020 11:19:09 +0800 Subject: [PATCH] Implement wasm mini loader and refine footprint of loader and runtime (#276) --- build-scripts/config_common.cmake | 10 +- core/config.h | 5 + core/iwasm/aot/aot_loader.c | 184 +- core/iwasm/aot/aot_runtime.c | 50 +- core/iwasm/common/wasm_runtime_common.c | 88 +- core/iwasm/interpreter/iwasm_interp.cmake | 8 +- core/iwasm/interpreter/wasm_loader.c | 262 +- core/iwasm/interpreter/wasm_mini_loader.c | 4947 +++++++++++++++++ core/iwasm/interpreter/wasm_runtime.c | 141 +- doc/build_wamr.md | 5 + .../platforms/linux-sgx/CMakeLists.txt | 11 +- product-mini/platforms/linux/CMakeLists.txt | 5 + 12 files changed, 5285 insertions(+), 431 deletions(-) create mode 100644 core/iwasm/interpreter/wasm_mini_loader.c diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 6187910d..10642756 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -133,10 +133,16 @@ endif () if (WAMR_BUILD_SPEC_TEST EQUAL 1) add_definitions (-DWASM_ENABLE_SPEC_TEST=1) message (" spec test compatible mode is on") -endif() +endif () if (WAMR_BUILD_BULK_MEMORY EQUAL 1) add_definitions (-DWASM_ENABLE_BULK_MEMORY=1) message (" Bulk memory feature enabled") else () add_definitions (-DWASM_ENABLE_BULK_MEMORY=0) -endif() +endif () +if (WAMR_BUILD_MINI_LOADER EQUAL 1) + add_definitions (-DWASM_ENABLE_MINI_LOADER=1) + message (" WASM mini loader enabled") +else () + add_definitions (-DWASM_ENABLE_MINI_LOADER=0) +endif () diff --git a/core/config.h b/core/config.h index e54a4be0..2bbcb7bf 100644 --- a/core/config.h +++ b/core/config.h @@ -130,6 +130,11 @@ enum { #define WASM_ENABLE_MULTI_MODULE 0 #endif +/* Enable wasm mini loader or not */ +#ifndef WASM_ENABLE_MINI_LOADER +#define WASM_ENABLE_MINI_LOADER 0 +#endif + /* Heap and stack profiling */ #define BH_ENABLE_MEMORY_PROFILING 0 diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index ab043afe..8803213e 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -59,13 +59,24 @@ static union { #define is_little_endian() (__ue.b == 1) -#define CHECK_BUF(buf, buf_end, length) do { \ - if (buf + length > buf_end) { \ - set_error_buf(error_buf, error_buf_size, \ - "Read data failed: unexpected end."); \ - goto fail; \ - } \ - } while (0) +static bool +check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length, + char *error_buf, uint32 error_buf_size) +{ + if (buf + length > buf_end) { + set_error_buf(error_buf, error_buf_size, + "AOT module load failed: unexpect end."); + return false; + } + return true; +} + +#define CHECK_BUF(buf, buf_end, length) do { \ + if (!check_buf(buf, buf_end, length, \ + error_buf, error_buf_size)) { \ + goto fail; \ + } \ +} while (0) static uint8* align_ptr(const uint8 *p, uint32 b) @@ -150,17 +161,32 @@ GET_U64_FROM_ADDR(uint32 *addr) /* Legal values for e_version */ #define E_VERSION_CURRENT 1 /* Current version */ +static void * +loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) +{ + void *mem; + + if (size >= UINT32_MAX + || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, + "AOT module load failed: " + "allocate memory failed."); + return NULL; + } + + memset(mem, 0, (uint32)size); + return mem; +} + static char* const_str_set_insert(const uint8 *str, int32 len, AOTModule *module, char* error_buf, uint32 error_buf_size) { HashMap *set = module->const_str_set; - char *c_str = wasm_runtime_malloc((uint32)len + 1), *value; + char *c_str, *value; - if (!c_str) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(c_str = loader_malloc((uint32)len + 1, + error_buf, error_buf_size))) { return NULL; } @@ -348,17 +374,11 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count; - if (size >= UINT32_MAX - || !(module->mem_init_data_list = - data_list = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->mem_init_data_list = data_list = + loader_malloc(size, error_buf, error_buf_size))) { return false; } - memset(data_list, 0, size); - /* Create each memory data segment */ for (i = 0; i < module->mem_init_data_count; i++) { uint32 init_expr_type, byte_count; @@ -372,11 +392,8 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end, read_uint64(buf, buf_end, init_expr_value); read_uint32(buf, buf_end, byte_count); size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count; - if (size >= UINT32_MAX - || !(data_list[i] = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(data_list[i] = loader_malloc + (size, error_buf, error_buf_size))) { return false; } @@ -447,17 +464,11 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count; - if (size >= UINT32_MAX - || !(module->table_init_data_list = - data_list = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->table_init_data_list = data_list = + loader_malloc(size, error_buf, error_buf_size))) { return false; } - memset(data_list, 0, size); - /* Create each table data segment */ for (i = 0; i < module->table_init_data_count; i++) { uint32 init_expr_type, func_index_count; @@ -469,11 +480,8 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, size1 = sizeof(uint32) * (uint64)func_index_count; size = offsetof(AOTTableInitData, func_indexes) + size1; - if (size >= UINT32_MAX - || !(data_list[i] = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(data_list[i] = loader_malloc + (size, error_buf, error_buf_size))) { return false; } @@ -535,16 +543,11 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTFuncType *) * (uint64)module->func_type_count; - if (size >= UINT32_MAX - || !(module->func_types = func_types = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->func_types = func_types = loader_malloc + (size, error_buf, error_buf_size))) { return false; } - memset(func_types, 0, size); - /* Create each function type */ for (i = 0; i < module->func_type_count; i++) { uint32 param_count, result_count; @@ -555,11 +558,8 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, size1 = (uint64)param_count + (uint64)result_count; size = offsetof(AOTFuncType, types) + size1; - if (size >= UINT32_MAX - || !(func_types[i] = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(func_types[i] = loader_malloc + (size, error_buf, error_buf_size))) { return false; } @@ -613,17 +613,11 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count; - if (size >= UINT32_MAX - || !(module->import_globals = - import_globals = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->import_globals = import_globals = + loader_malloc(size, error_buf, error_buf_size))) { return false; } - memset(import_globals, 0, size); - /* Create each import global */ for (i = 0; i < module->import_global_count; i++) { buf = (uint8*)align_ptr(buf, 2); @@ -685,16 +679,11 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTGlobal) * (uint64)module->global_count; - if (size >= UINT32_MAX - || !(module->globals = globals = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->globals = globals = loader_malloc + (size, error_buf, error_buf_size))) { return false; } - memset(globals, 0, size); - if (module->import_global_count > 0) { last_import_global = &module->import_globals[module->import_global_count - 1]; @@ -767,17 +756,11 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTImportFunc) * (uint64)module->import_func_count; - if (size >= UINT32_MAX - || !(module->import_funcs = - import_funcs = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->import_funcs = import_funcs = + loader_malloc(size, error_buf, error_buf_size))) { return false; } - memset(import_funcs, 0, size); - /* Create each import func */ for (i = 0; i < module->import_func_count; i++) { read_uint16(buf, buf_end, import_funcs[i].func_type_index); @@ -860,17 +843,11 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count; - if (size >= UINT32_MAX - || !(module->data_sections = - data_sections = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->data_sections = data_sections = + loader_malloc(size, error_buf, error_buf_size))) { return false; } - memset(data_sections, 0, size); - /* Create each data section */ for (i = 0; i < module->data_section_count; i++) { int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE; @@ -1021,10 +998,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, uint64 size, text_offset; size = sizeof(void*) * (uint64)module->func_count; - if (size >= UINT32_MAX - || !(module->func_ptrs = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: allocate memory failed."); + if (!(module->func_ptrs = loader_malloc + (size, error_buf, error_buf_size))) { return false; } @@ -1065,10 +1040,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, } size = sizeof(uint32) * (uint64)module->func_count; - if (size >= UINT32_MAX - || !(module->func_type_indexes = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: allocate memory failed."); + if (!(module->func_type_indexes = loader_malloc + (size, error_buf, error_buf_size))) { return false; } @@ -1112,17 +1085,11 @@ load_export_funcs(const uint8 **p_buf, const uint8 *buf_end, /* Allocate memory */ size = sizeof(AOTExportFunc) * (uint64)module->export_func_count; - if (size >= UINT32_MAX - || !(module->export_funcs = - export_funcs = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(module->export_funcs = export_funcs = + loader_malloc(size, error_buf, error_buf_size))) { return false; } - memset(export_funcs, 0, size); - /* Create each export func */ for (i = 0; i < module->export_func_count; i++) { read_uint32(buf, buf_end, export_funcs[i].func_index); @@ -1234,10 +1201,8 @@ do_text_relocation(AOTModule *module, if (symbol_len + 1 <= sizeof(symbol_buf)) symbol = symbol_buf; else { - if (!(symbol = wasm_runtime_malloc(symbol_len + 1))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(symbol = loader_malloc(symbol_len + 1, + error_buf, error_buf_size))) { return false; } } @@ -1432,15 +1397,10 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, /* Allocate memory for relocation groups */ size = sizeof(AOTRelocationGroup) * (uint64)group_count; - if (size >= UINT32_MAX || !(groups = wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(groups = loader_malloc(size, error_buf, error_buf_size))) { goto fail; } - memset(groups, 0, size); - /* Load each relocation group */ for (i = 0, group = groups; i < group_count; i++, group++) { AOTRelocation *relocation; @@ -1473,18 +1433,12 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, /* Allocate memory for relocations */ size = sizeof(AOTRelocation) * (uint64)group->relocation_count; - if (size >= UINT32_MAX - || !(group->relocations = relocation = - wasm_runtime_malloc((uint32)size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + if (!(group->relocations = relocation = + loader_malloc(size, error_buf, error_buf_size))) { ret = false; goto fail; } - memset(group->relocations, 0, size); - /* Load each relocation */ for (j = 0; j < group->relocation_count; j++, relocation++) { uint32 symbol_index; diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index d42989d2..65975a10 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -14,6 +14,23 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) snprintf(error_buf, error_buf_size, "%s", string); } +static void * +runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size) +{ + void *mem; + + if (size >= UINT32_MAX + || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, + "AOT module instantiate failed: " + "allocate memory failed."); + return NULL; + } + + memset(mem, 0, (uint32)size); + return mem; +} + static bool global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, char *error_buf, uint32 error_buf_size) @@ -142,15 +159,10 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, uint8 *p; /* Allocate memory */ - if (total_size >= UINT32_MAX - || !(p = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed: allocate memory failed."); + if (!(p = runtime_malloc(total_size, error_buf, error_buf_size))) { return false; } - memset(p, 0, (uint32)total_size); - /* Initialize heap info */ module_inst->heap_data.ptr = p; p += heap_size; @@ -270,15 +282,11 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module, ((uint64)module->import_func_count + module->func_count) * sizeof(void*); /* Allocate memory */ - if (total_size >= UINT32_MAX - || !(module_inst->func_ptrs.ptr = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed: allocate memory failed."); + if (!(module_inst->func_ptrs.ptr = runtime_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module_inst->func_ptrs.ptr, 0, (uint32)total_size); - /* Set import function pointers */ func_ptrs = (void**)module_inst->func_ptrs.ptr; for (i = 0; i < module->import_func_count; i++, func_ptrs++) @@ -299,16 +307,11 @@ init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module, ((uint64)module->import_func_count + module->func_count) * sizeof(uint32); /* Allocate memory */ - if (total_size >= UINT32_MAX - || !(module_inst->func_type_indexes.ptr = - wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed: allocate memory failed."); + if (!(module_inst->func_type_indexes.ptr = + runtime_malloc(total_size, error_buf, error_buf_size))) { return false; } - memset(module_inst->func_type_indexes.ptr, 0, (uint32)total_size); - /* Set import function type indexes */ func_type_index = (uint32*)module_inst->func_type_indexes.ptr; for (i = 0; i < module->import_func_count; i++, func_type_index++) @@ -381,14 +384,11 @@ aot_instantiate(AOTModule *module, heap_size = APP_HEAP_SIZE_MAX; /* Allocate module instance, global data, table data and heap data */ - if (total_size >= UINT32_MAX - || !(module_inst = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed: allocate memory failed."); + if (!(module_inst = runtime_malloc(total_size, + error_buf, error_buf_size))) { return NULL; } - memset(module_inst, 0, total_size); module_inst->module_type = Wasm_Module_AoT; module_inst->aot_module.ptr = module; @@ -801,7 +801,7 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) /* Destroy heap's lock firstly, if its memory is re-allocated, we cannot access its lock again. */ mem_allocator_destroy_lock(module_inst->heap_handle.ptr); - if (!(heap_data = wasm_runtime_realloc(heap_handle_old, (uint32)total_size))) { + if (!(heap_data = wasm_runtime_realloc(heap_data_old, (uint32)total_size))) { if (!(heap_data = wasm_runtime_malloc((uint32)total_size))) { /* Restore heap's lock if memory re-alloc failed */ mem_allocator_reinit_lock(module_inst->heap_handle.ptr); diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index c3b6da95..02cd5863 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -60,6 +60,29 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) snprintf(error_buf, error_buf_size, "%s", string); } +static void * +runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst, + char *error_buf, uint32 error_buf_size) +{ + void *mem; + + if (size >= UINT32_MAX + || !(mem = wasm_runtime_malloc((uint32)size))) { + if (module_inst != NULL) { + wasm_runtime_set_exception(module_inst, + "allocate memory failed."); + } + else if (error_buf != NULL) { + set_error_buf(error_buf, error_buf_size, + "allocate memory failed."); + } + return NULL; + } + + memset(mem, 0, (uint32)size); + return mem; +} + static bool wasm_runtime_env_init() { @@ -91,12 +114,12 @@ wasm_runtime_env_init() static bool wasm_runtime_exec_env_check(WASMExecEnv *exec_env) { - return !(!exec_env - || !exec_env->module_inst - || exec_env->wasm_stack_size == 0 - || exec_env->wasm_stack.s.top_boundary != + return exec_env + && exec_env->module_inst + && exec_env->wasm_stack_size > 0 + && exec_env->wasm_stack.s.top_boundary == exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size - || exec_env->wasm_stack.s.top > exec_env->wasm_stack.s.top_boundary); + && exec_env->wasm_stack.s.top <= exec_env->wasm_stack.s.top_boundary; } bool @@ -240,13 +263,10 @@ wasm_runtime_register_module_internal(const char *module_name, } /* module hasn't been registered */ - node = wasm_runtime_malloc(sizeof(WASMRegisteredModule)); + node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0); if (!node) { LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d", sizeof(WASMRegisteredModule)); - set_error_buf_v(error_buf, error_buf_size, - "malloc WASMRegisteredModule failed. SZ=%d", - sizeof(WASMRegisteredModule)); return false; } @@ -377,16 +397,15 @@ wasm_runtime_destroy_registered_module_list() } bool -wasm_runtime_add_loading_module(const char *module_name, char *error_buf, - uint32 error_buf_size) +wasm_runtime_add_loading_module(const char *module_name, + char *error_buf, uint32 error_buf_size) { LOG_DEBUG("add %s into a loading list", module_name); - LoadingModule *loadingModule = wasm_runtime_malloc(sizeof(LoadingModule)); + LoadingModule *loadingModule = + runtime_malloc(sizeof(LoadingModule), NULL, + error_buf, error_buf_size); if (!loadingModule) { - set_error_buf_v(error_buf, error_buf_size, - "malloc LoadingModule failed. SZ=%d", - sizeof(LoadingModule)); return false; } @@ -1145,13 +1164,11 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, uint64 total_size; uint32 i; - if (!(wasi_ctx = wasm_runtime_malloc(sizeof(WASIContext)))) { - set_error_buf(error_buf, error_buf_size, - "Init wasi environment failed: allocate memory failed."); + if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL, + error_buf, error_buf_size))) { return false; } - memset(wasi_ctx, 0, sizeof(WASIContext)); wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx); #if WASM_ENABLE_INTERP != 0 @@ -1645,12 +1662,11 @@ resolve_function(const WASMModuleInstanceCommon *module_inst, char *function_name = NULL; uint32 length = strlen(name) + 1; - orig_name = wasm_runtime_malloc(sizeof(char) * length); + orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0); if (!orig_name) { return NULL; } - memset(orig_name, 0, sizeof(char) * length); strncpy(orig_name, name, length); if (!parse_function_name(orig_name, &sub_module_name, &function_name)) { @@ -1808,9 +1824,8 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, } total_size = sizeof(uint32) * (uint64)(argc1 > 2 ? argc1 : 2); - if (total_size >= UINT32_MAX - || (!(argv1 = wasm_runtime_malloc((uint32)total_size)))) { - wasm_runtime_set_exception(module_inst, "allocate memory failed."); + if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst, + NULL, 0)))) { goto fail; } @@ -2008,13 +2023,10 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, argc1 = func_type->param_count; if (argc1 > sizeof(argv_buf) / sizeof(uint64)) { size = sizeof(uint64) * (uint64)argc1; - if (size >= UINT32_MAX - || !(argv1 = wasm_runtime_malloc((uint32)size))) { - wasm_runtime_set_exception(exec_env->module_inst, - "allocate memory failed."); + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, + NULL, 0))) { return false; } - memset(argv1, 0, (uint32)size); } argv_dst = argv1; @@ -2208,10 +2220,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks; if (argc1 > sizeof(argv_buf) / sizeof(uint32)) { size = sizeof(uint32) * (uint32)argc1; - if (size >= UINT32_MAX - || !(argv1 = wasm_runtime_malloc((uint32)size))) { - wasm_runtime_set_exception(exec_env->module_inst, - "allocate memory failed."); + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, + NULL, 0))) { return false; } } @@ -2386,10 +2396,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, if (argc1 > sizeof(argv_buf) / sizeof(uint32)) { size = sizeof(uint32) * (uint64)argc1; - if (size >= UINT_MAX - || !(argv1 = wasm_runtime_malloc((uint32)size))) { - wasm_runtime_set_exception(exec_env->module_inst, - "allocate memory failed."); + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, + NULL, 0))) { return false; } } @@ -2543,10 +2551,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, argc1 = 1 + MAX_REG_FLOATS + func_type->param_count + 2; if (argc1 > sizeof(argv_buf) / sizeof(uint64)) { size = sizeof(uint64) * (uint64)argc1; - if (size >= UINT32_MAX - || !(argv1 = wasm_runtime_malloc((uint32)size))) { - wasm_runtime_set_exception(exec_env->module_inst, - "allocate memory failed."); + if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst, + NULL, 0))) { return false; } } diff --git a/core/iwasm/interpreter/iwasm_interp.cmake b/core/iwasm/interpreter/iwasm_interp.cmake index bb4f2321..e6e52e42 100644 --- a/core/iwasm/interpreter/iwasm_interp.cmake +++ b/core/iwasm/interpreter/iwasm_interp.cmake @@ -13,8 +13,14 @@ else () set (INTERPRETER "wasm_interp_classic.c") endif () +if (WAMR_BUILD_MINI_LOADER EQUAL 1) + set (LOADER "wasm_mini_loader.c") +else () + set (LOADER "wasm_loader.c") +endif () + file (GLOB_RECURSE source_all - ${IWASM_INTERP_DIR}/wasm_loader.c + ${IWASM_INTERP_DIR}/${LOADER} ${IWASM_INTERP_DIR}/wasm_runtime.c ${IWASM_INTERP_DIR}/${INTERPRETER} ) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index b8f4f693..042a503e 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -24,21 +24,43 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) snprintf(error_buf, error_buf_size, "%s", string); } -#define CHECK_BUF(buf, buf_end, length) do { \ - if (buf + length > buf_end) { \ - set_error_buf(error_buf, error_buf_size, \ - "WASM module load failed: " \ - "unexpected end of section or function"); \ - return false; \ - } \ +static bool +check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length, + char *error_buf, uint32 error_buf_size) +{ + if (buf + length > buf_end) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: " + "unexpected end of section or function"); + return false; + } + return true; +} + +static bool +check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length, + char *error_buf, uint32 error_buf_size) +{ + if (buf + length > buf_end) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: unexpected end"); + return false; + } + return true; +} + +#define CHECK_BUF(buf, buf_end, length) do { \ + if (!check_buf(buf, buf_end, length, \ + error_buf, error_buf_size)) { \ + return false; \ + } \ } while (0) -#define CHECK_BUF1(buf, buf_end, length) do { \ - if (buf + length > buf_end) { \ - set_error_buf(error_buf, error_buf_size, \ - "WASM module load failed: unexpected end");\ - return false; \ - } \ +#define CHECK_BUF1(buf, buf_end, length) do { \ + if (!check_buf1(buf, buf_end, length, \ + error_buf, error_buf_size)) { \ + return false; \ + } \ } while (0) static bool @@ -193,6 +215,23 @@ fail_integer_too_large: res = (int32)res64; \ } while (0) +static void * +loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) +{ + void *mem; + + if (size >= UINT32_MAX + || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: " + "allocate memory failed."); + return NULL; + } + + memset(mem, 0, (uint32)size); + return mem; +} + static bool check_utf8_str(const uint8* str, uint32 len) { @@ -256,10 +295,8 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, return node->str; } - if (!(node = wasm_runtime_malloc(sizeof(StringNode) + len + 1))) { - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " - "allocate memory failed."); + if (!(node = loader_malloc(sizeof(StringNode) + len + 1, + error_buf, error_buf_size))) { return NULL; } @@ -361,15 +398,11 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (type_count) { module->type_count = type_count; total_size = sizeof(WASMType*) * (uint64)type_count; - if (total_size >= UINT32_MAX - || !(module->types = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load type section failed: allocate memory failed."); + if (!(module->types = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->types, 0, (uint32)total_size); - for (i = 0; i < type_count; i++) { CHECK_BUF(p, p_end, 1); flag = read_uint8(p); @@ -396,11 +429,8 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, total_size = offsetof(WASMType, types) + sizeof(uint8) * (uint64)(param_count + result_count); - if (total_size >= UINT32_MAX - || !(type = module->types[i] = - wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load type section failed: allocate memory failed."); + if (!(type = module->types[i] = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -808,12 +838,6 @@ load_table_import(WASMModule *sub_module, const char *sub_module_name, } *p_buf = p; - if ((declare_max_size_flag & 1) && declare_init_size > declare_max_size) { - set_error_buf(error_buf, error_buf_size, - "size minimum must not be greater than maximum"); - return false; - } - #if WASM_ENABLE_MULTI_MODULE != 0 if (!wasm_runtime_is_built_in_module(sub_module_name)) { linked_table = wasm_loader_resolve_table( @@ -1093,12 +1117,6 @@ load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table, else table->max_size = 0x10000; - if ((table->flags & 1) && table->init_size > table->max_size) { - set_error_buf(error_buf, error_buf_size, - "size minimum must not be greater than maximum"); - return false; - } - *p_buf = p; return true; } @@ -1327,15 +1345,11 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (import_count) { module->import_count = import_count; total_size = sizeof(WASMImport) * (uint64)import_count; - if (total_size >= UINT32_MAX - || !(module->imports = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load import section failed: allocate memory failed."); + if (!(module->imports = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->imports, 0, (uint32)total_size); - p_old = p; /* Scan firstly to get import count of each type */ @@ -1576,10 +1590,8 @@ init_function_local_offsets(WASMFunction *func, uint32 i, local_offset = 0; uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count); - if (total_size >= UINT32_MAX - || !(func->local_offsets = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load function section failed: allocate memory failed."); + if (!(func->local_offsets = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } @@ -1627,15 +1639,11 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, if (func_count) { module->function_count = func_count; total_size = sizeof(WASMFunction*) * (uint64)func_count; - if (total_size >= UINT32_MAX - || !(module->functions = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load function section failed: allocate memory failed."); + if (!(module->functions = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } - memset(module->functions, 0, (uint32)total_size); - for (i = 0; i < func_count; i++) { /* Resolve function type */ read_leb_uint32(p, p_end, type_index); @@ -1680,17 +1688,12 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, code_size = (uint32)(p_code_end - p_code); total_size = sizeof(WASMFunction) + (uint64)local_count; - if (total_size >= UINT32_MAX - || !(func = module->functions[i] = - wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load function section failed: " - "allocate memory failed."); + if (!(func = module->functions[i] = + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } /* Set function type, local count, code size and code body */ - memset(func, 0, (uint32)total_size); func->func_type = module->types[type_index]; func->local_count = local_count; if (local_count > 0) @@ -1775,15 +1778,11 @@ load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (table_count) { module->table_count = table_count; total_size = sizeof(WASMTable) * (uint64)table_count; - if (total_size >= UINT32_MAX - || !(module->tables = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load table section failed: allocate memory failed."); + if (!(module->tables = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->tables, 0, (uint32)total_size); - /* load each table */ table = module->tables; for (i = 0; i < table_count; i++, table++) @@ -1820,15 +1819,11 @@ load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (memory_count) { module->memory_count = memory_count; total_size = sizeof(WASMMemory) * (uint64)memory_count; - if (total_size >= UINT32_MAX - || !(module->memories = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load memory section failed: allocate memory failed."); + if (!(module->memories = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->memories, 0, (uint32)total_size); - /* load each memory */ memory = module->memories; for (i = 0; i < memory_count; i++, memory++) @@ -1861,16 +1856,11 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (global_count) { module->global_count = global_count; total_size = sizeof(WASMGlobal) * (uint64)global_count; - if (total_size >= UINT32_MAX - || !(module->globals = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load global section failed: " - "allocate memory failed."); + if (!(module->globals = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->globals, 0, (uint32)total_size); - global = module->globals; for(i = 0; i < global_count; i++, global++) { @@ -1932,16 +1922,11 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (export_count) { module->export_count = export_count; total_size = sizeof(WASMExport) * (uint64)export_count; - if (total_size >= UINT32_MAX - || !(module->exports = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load export section failed: " - "allocate memory failed."); + if (!(module->exports = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->exports, 0, (uint32)total_size); - export = module->exports; for (i = 0; i < export_count; i++, export++) { read_leb_uint32(p, p_end, str_len); @@ -2038,16 +2023,11 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m if (table_segment_count) { module->table_seg_count = table_segment_count; total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count; - if (total_size >= UINT32_MAX - || !(module->table_segments = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load table segment section failed: " - "allocate memory failed."); + if (!(module->table_segments = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->table_segments, 0, (uint32)total_size); - table_segment = module->table_segments; for (i = 0; i < table_segment_count; i++, table_segment++) { if (p >= p_end) { @@ -2074,12 +2054,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m read_leb_uint32(p, p_end, function_count); table_segment->function_count = function_count; total_size = sizeof(uint32) * (uint64)function_count; - if (total_size >= UINT32_MAX - || !(table_segment->func_indexes = (uint32 *) - wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load table segment section failed: " - "allocate memory failed."); + if (!(table_segment->func_indexes = (uint32 *) + loader_malloc(total_size, error_buf, error_buf_size))) { return false; } for (j = 0; j < function_count; j++) { @@ -2134,16 +2110,11 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, if (data_seg_count) { module->data_seg_count = data_seg_count; total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count; - if (total_size >= UINT32_MAX - || !(module->data_segments = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Load data segment section failed: " - "allocate memory failed."); + if (!(module->data_segments = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } - memset(module->data_segments, 0, (uint32)total_size); - for (i = 0; i < data_seg_count; i++) { read_leb_uint32(p, p_end, mem_index); #if WASM_ENABLE_BULK_MEMORY != 0 @@ -2157,7 +2128,6 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end, /* no memory index, treat index as 0 */ mem_index = 0; goto check_mem_index; - break; case 0x02: /* read following memory index */ read_leb_uint32(p, p_end, mem_index); @@ -2193,11 +2163,8 @@ check_mem_index: read_leb_uint32(p, p_end, data_seg_len); - if (!(dataseg = module->data_segments[i] = - wasm_runtime_malloc((uint32)sizeof(WASMDataSeg)))) { - set_error_buf(error_buf, error_buf_size, - "Load data segment section failed: " - "allocate memory failed."); + if (!(dataseg = module->data_segments[i] = loader_malloc + (sizeof(WASMDataSeg), error_buf, error_buf_size))) { return false; } @@ -2477,18 +2444,19 @@ load_from_sections(WASMModule *module, WASMSection *sections, #endif total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE * BLOCK_ADDR_CONFLICT_SIZE; - if (total_size >= UINT32_MAX - || !(block_addr_cache = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: allocate memory failed"); + if (!(block_addr_cache = loader_malloc + (total_size, error_buf, error_buf_size))) { return false; } for (i = 0; i < module->function_count; i++) { WASMFunction *func = module->functions[i]; memset(block_addr_cache, 0, (uint32)total_size); - if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache, error_buf, error_buf_size)) + if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache, + error_buf, error_buf_size)) { + wasm_runtime_free(block_addr_cache); return false; + } } wasm_runtime_free(block_addr_cache); @@ -2616,17 +2584,13 @@ static void wasm_loader_free(void *ptr) static WASMModule* create_module(char *error_buf, uint32 error_buf_size) { - WASMModule *module = wasm_runtime_malloc(sizeof(WASMModule)); + WASMModule *module = loader_malloc(sizeof(WASMModule), + error_buf, error_buf_size); if (!module) { - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " - "allocate memory failed."); return NULL; } - memset(module, 0, sizeof(WASMModule)); - module->module_type = Wasm_Module_Bytecode; /* Set start_function to -1, means no start function */ @@ -2732,14 +2696,11 @@ create_sections(const uint8 *buf, uint32 size, read_leb_uint32(p, p_end, section_size); CHECK_BUF1(p, p_end, section_size); - if (!(section = wasm_runtime_malloc(sizeof(WASMSection)))) { - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " - "allocate memory failed."); + if (!(section = loader_malloc(sizeof(WASMSection), + error_buf, error_buf_size))) { return false; } - memset(section, 0, sizeof(WASMSection)); section->section_type = section_type; section->section_body = (uint8*)p; section->section_body_size = section_size; @@ -3416,16 +3377,12 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, { uint8 *mem_new; bh_assert(size_new > size_old); - if ((mem_new = wasm_runtime_malloc(size_new))) { + if ((mem_new = loader_malloc + (size_new, error_buf, error_buf_size))) { bh_memcpy_s(mem_new, size_new, mem_old, size_old); memset(mem_new + size_old, 0, size_new - size_old); wasm_runtime_free(mem_old); } - else { - set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " - "allocate memory failed."); - } return mem_new; } @@ -3485,7 +3442,8 @@ check_offset_pop(WASMLoaderContext *ctx, uint32 cells) return true; } -static void free_label_patch_list(BranchBlock *frame_csp) +static void +free_label_patch_list(BranchBlock *frame_csp) { BranchBlockPatch *label_patch = frame_csp->patch_list; BranchBlockPatch *next; @@ -3497,7 +3455,8 @@ static void free_label_patch_list(BranchBlock *frame_csp) frame_csp->patch_list = NULL; } -static void free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num) +static void +free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num) { BranchBlock *tmp_csp = frame_csp; @@ -3525,7 +3484,6 @@ fail: return false; } - static bool check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, char *error_buf, uint32 error_buf_size) @@ -3580,7 +3538,8 @@ check_stack_pop(WASMLoaderContext *ctx, uint8 type, return true; } -static void wasm_loader_ctx_destroy(WASMLoaderContext *ctx) +static void +wasm_loader_ctx_destroy(WASMLoaderContext *ctx) { if (ctx) { if (ctx->frame_ref_bottom) @@ -4055,11 +4014,9 @@ add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type, uint8 *p_code_compiled, char *error_buf, uint32 error_buf_size) { - BranchBlockPatch *patch = wasm_runtime_malloc(sizeof(BranchBlockPatch)); + BranchBlockPatch *patch = loader_malloc + (sizeof(BranchBlockPatch), error_buf, error_buf_size); if (!patch) { - set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " - "allocate memory failed"); return false; } patch->patch_type = patch_type; @@ -5505,7 +5462,7 @@ re_scan: case WASM_OP_SET_GLOBAL: { - bool is_multable = false; + bool is_mutable = false; read_leb_uint32(p, p_end, global_idx); if (global_idx >= global_count) { set_error_buf(error_buf, error_buf_size, @@ -5514,12 +5471,12 @@ re_scan: goto fail; } - is_multable = + is_mutable = global_idx < module->import_global_count ? module->import_globals[global_idx].u.global.is_mutable : module->globals[global_idx - module->import_global_count] .is_mutable; - if (!is_multable) { + if (!is_mutable) { set_error_buf(error_buf, error_buf_size, "global is immutable"); @@ -5772,8 +5729,6 @@ re_scan: POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); break; - break; - case WASM_OP_I32_CLZ: case WASM_OP_I32_CTZ: case WASM_OP_I32_POPCNT: @@ -6092,13 +6047,10 @@ fail_data_cnt_sec_require: func->const_cell_num = loader_ctx->const_cell_num; if (!(func->consts = func_const = - wasm_runtime_malloc(func->const_cell_num * 4))) { - set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " - "allocate memory failed"); + loader_malloc(func->const_cell_num * 4, + error_buf, error_buf_size))) { goto fail; } - memset(func->consts, 0, func->const_cell_num * 4); func_const_end = func->consts + func->const_cell_num * 4; // reverse the const buf for (int i = loader_ctx->num_const - 1; i >= 0; i--) { diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c new file mode 100644 index 00000000..3ca23f81 --- /dev/null +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -0,0 +1,4947 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_loader.h" +#include "bh_common.h" +#include "bh_log.h" +#include "wasm.h" +#include "wasm_opcode.h" +#include "wasm_runtime.h" +#include "../common/wasm_native.h" + +/* Read a value of given type from the address pointed to by the given + pointer and increase the pointer to the position just after the + value being read. */ +#define TEMPLATE_READ_VALUE(Type, p) \ + (p += sizeof(Type), *(Type *)(p - sizeof(Type))) + +static void +set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) +{ + if (error_buf != NULL) + snprintf(error_buf, error_buf_size, "%s", string); +} + +#define CHECK_BUF(buf, buf_end, length) do { \ + bh_assert(buf + length <= buf_end); \ + } while (0) + +#define CHECK_BUF1(buf, buf_end, length) do { \ + bh_assert(buf + length <= buf_end); \ + } while (0) + +static void +skip_leb(const uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, + char* error_buf, uint32 error_buf_size) +{ + const uint8 *buf = *p_buf; + uint32 offset = 0, bcnt = 0; + uint64 byte; + + while (true) { + bh_assert(bcnt + 1 <= (maxbits + 6) / 7); + CHECK_BUF(buf, buf_end, offset + 1); + byte = buf[offset]; + offset += 1; + bcnt += 1; + if ((byte & 0x80) == 0) { + break; + } + } + + *p_buf += offset; +} + +#define skip_leb_int64(p, p_end) do { \ + skip_leb(&p, p_end, 64, \ + error_buf, error_buf_size); \ + } while (0) + +#define skip_leb_uint32(p, p_end) do { \ + skip_leb(&p, p_end, 32, \ + error_buf, error_buf_size); \ + } while (0) + +#define skip_leb_int32(p, p_end) do { \ + skip_leb(&p, p_end, 32, \ + error_buf, error_buf_size); \ + } while (0) + +static void +read_leb(uint8 **p_buf, const uint8 *buf_end, + uint32 maxbits, bool sign, uint64 *p_result, + char* error_buf, uint32 error_buf_size) +{ + const uint8 *buf = *p_buf; + uint64 result = 0; + uint32 shift = 0; + uint32 offset = 0, bcnt = 0; + uint64 byte; + + while (true) { + bh_assert(bcnt + 1 <= (maxbits + 6) / 7); + CHECK_BUF(buf, buf_end, offset + 1); + byte = buf[offset]; + offset += 1; + result |= ((byte & 0x7f) << shift); + shift += 7; + bcnt += 1; + if ((byte & 0x80) == 0) { + break; + } + } + + if (!sign && maxbits == 32 && shift >= maxbits) { + /* The top bits set represent values > 32 bits */ + bh_assert(!(((uint8)byte) & 0xf0)); + } + else if (sign && maxbits == 32) { + if (shift < maxbits) { + /* Sign extend */ + result = (((int32)result) << (maxbits - shift)) + >> (maxbits - shift); + } + else { + /* The top bits should be a sign-extension of the sign bit */ + bool sign_bit_set = ((uint8)byte) & 0x8; + int top_bits = ((uint8)byte) & 0xf0; + bh_assert(!((sign_bit_set && top_bits != 0x70) + || (!sign_bit_set && top_bits != 0))); + (void)top_bits; + (void)sign_bit_set; + } + } + else if (sign && maxbits == 64) { + if (shift < maxbits) { + /* Sign extend */ + result = (((int64)result) << (maxbits - shift)) + >> (maxbits - shift); + } + else { + /* The top bits should be a sign-extension of the sign bit */ + bool sign_bit_set = ((uint8)byte) & 0x1; + int top_bits = ((uint8)byte) & 0xfe; + + bh_assert(!((sign_bit_set && top_bits != 0x7e) + || (!sign_bit_set && top_bits != 0))); + (void)top_bits; + (void)sign_bit_set; + } + } + + *p_buf += offset; + *p_result = result; +} + +#define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p) +#define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p) +#define read_bool(p) TEMPLATE_READ_VALUE(bool, p) + +#define read_leb_int64(p, p_end, res) do { \ + uint64 res64; \ + read_leb((uint8**)&p, p_end, 64, true, &res64, \ + error_buf, error_buf_size); \ + res = (int64)res64; \ +} while (0) + +#define read_leb_uint32(p, p_end, res) do { \ + uint64 res64; \ + read_leb((uint8**)&p, p_end, 32, false, &res64, \ + error_buf, error_buf_size); \ + res = (uint32)res64; \ +} while (0) + +#define read_leb_int32(p, p_end, res) do { \ + uint64 res64; \ + read_leb((uint8**)&p, p_end, 32, true, &res64, \ + error_buf, error_buf_size); \ + res = (int32)res64; \ +} while (0) + +static void * +loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) +{ + void *mem; + + if (size >= UINT32_MAX + || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: " + "allocate memory failed."); + return NULL; + } + + memset(mem, 0, (uint32)size); + return mem; +} + +static char * +const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + StringNode *node, *node_next; + + /* Search const str list */ + node = module->const_str_list; + while (node) { + node_next = node->next; + if (strlen(node->str) == len + && !memcmp(node->str, str, len)) + break; + node = node_next; + } + + if (node) { + LOG_DEBUG("reuse %s", node->str); + return node->str; + } + + if (!(node = loader_malloc(sizeof(StringNode) + len + 1, + error_buf, error_buf_size))) { + return NULL; + } + + node->str = ((char*)node) + sizeof(StringNode); + bh_memcpy_s(node->str, len + 1, str, len); + node->str[len] = '\0'; + + if (!module->const_str_list) { + /* set as head */ + module->const_str_list = node; + node->next = NULL; + } + else { + /* insert it */ + node->next = module->const_str_list; + module->const_str_list = node; + } + + return node->str; +} + +static bool +load_init_expr(const uint8 **p_buf, const uint8 *buf_end, + InitializerExpression *init_expr, uint8 type, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = *p_buf, *p_end = buf_end; + uint8 flag, end_byte, *p_float; + uint32 i; + + CHECK_BUF(p, p_end, 1); + init_expr->init_expr_type = read_uint8(p); + flag = init_expr->init_expr_type; + + switch (flag) { + /* i32.const */ + case INIT_EXPR_TYPE_I32_CONST: + bh_assert(type == VALUE_TYPE_I32); + read_leb_int32(p, p_end, init_expr->u.i32); + break; + /* i64.const */ + case INIT_EXPR_TYPE_I64_CONST: + bh_assert(type == VALUE_TYPE_I64); + read_leb_int64(p, p_end, init_expr->u.i64); + break; + /* f32.const */ + case INIT_EXPR_TYPE_F32_CONST: + bh_assert(type == VALUE_TYPE_F32); + CHECK_BUF(p, p_end, 4); + p_float = (uint8*)&init_expr->u.f32; + for (i = 0; i < sizeof(float32); i++) + *p_float++ = *p++; + break; + /* f64.const */ + case INIT_EXPR_TYPE_F64_CONST: + bh_assert(type == VALUE_TYPE_F64); + CHECK_BUF(p, p_end, 8); + p_float = (uint8*)&init_expr->u.f64; + for (i = 0; i < sizeof(float64); i++) + *p_float++ = *p++; + break; + /* get_global */ + case INIT_EXPR_TYPE_GET_GLOBAL: + read_leb_uint32(p, p_end, init_expr->u.global_index); + break; + default: + bh_assert(0); + break; + } + CHECK_BUF(p, p_end, 1); + end_byte = read_uint8(p); + bh_assert(end_byte == 0x0b); + *p_buf = p; + + (void)end_byte; + return true; +} + +static bool +load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end, *p_org; + uint32 type_count, param_count, result_count, i, j; + uint64 total_size; + uint8 flag; + WASMType *type; + + read_leb_uint32(p, p_end, type_count); + + if (type_count) { + module->type_count = type_count; + total_size = sizeof(WASMType*) * (uint64)type_count; + if (!(module->types = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + for (i = 0; i < type_count; i++) { + CHECK_BUF(p, p_end, 1); + flag = read_uint8(p); + bh_assert(flag == 0x60); + + read_leb_uint32(p, p_end, param_count); + + /* Resolve param count and result count firstly */ + p_org = p; + CHECK_BUF(p, p_end, param_count); + p += param_count; + read_leb_uint32(p, p_end, result_count); + bh_assert(result_count <= 1); + CHECK_BUF(p, p_end, result_count); + p = p_org; + + total_size = offsetof(WASMType, types) + + sizeof(uint8) * (uint64)(param_count + result_count); + if (!(type = module->types[i] = + loader_malloc(total_size, error_buf, error_buf_size))) { + return false; + } + + /* Resolve param types and result types */ + type->param_count = param_count; + type->result_count = result_count; + for (j = 0; j < param_count; j++) { + CHECK_BUF(p, p_end, 1); + type->types[j] = read_uint8(p); + } + read_leb_uint32(p, p_end, result_count); + for (j = 0; j < result_count; j++) { + CHECK_BUF(p, p_end, 1); + type->types[param_count + j] = read_uint8(p); + } + } + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load type section success.\n"); + (void)flag; + return true; +} + +static bool +load_function_import(const WASMModule *parent_module, WASMModule *sub_module, + char *sub_module_name, char *function_name, + const uint8 **p_buf, const uint8 *buf_end, + WASMFunctionImport *function, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = *p_buf, *p_end = buf_end; + uint32 declare_type_index = 0; + WASMType *declare_func_type = NULL; + WASMFunction *linked_func = NULL; + const char *linked_signature = NULL; + void *linked_attachment = NULL; + bool linked_call_conv_raw = false; + bool is_built_in_module = false; + + CHECK_BUF(p, p_end, 1); + read_leb_uint32(p, p_end, declare_type_index); + *p_buf = p; + + bh_assert(declare_type_index < parent_module->type_count); + + declare_func_type = parent_module->types[declare_type_index]; + + is_built_in_module = wasm_runtime_is_built_in_module(sub_module_name); + if (is_built_in_module) { + LOG_DEBUG("%s is a function of a built-in module %s", + function_name, + sub_module_name); + /* check built-in modules */ + linked_func = wasm_native_resolve_symbol(sub_module_name, + function_name, + declare_func_type, + &linked_signature, + &linked_attachment, + &linked_call_conv_raw); + } + + if (!linked_func) { +#if WASM_ENABLE_SPEC_TEST != 0 + set_error_buf(error_buf, + error_buf_size, + "unknown import or incompatible import type"); + return false; +#else +#if WASM_ENABLE_WAMR_COMPILER == 0 + LOG_WARNING( + "warning: fail to link import function (%s, %s)", + sub_module_name, function_name); +#endif +#endif + } + + function->module_name = sub_module_name; + function->field_name = function_name; + function->func_type = declare_func_type; + /* func_ptr_linked is for built-in functions */ + function->func_ptr_linked = is_built_in_module ? linked_func : NULL; + function->signature = linked_signature; + function->attachment = linked_attachment; + function->call_conv_raw = linked_call_conv_raw; + return true; +} + +static bool +load_table_import(WASMModule *sub_module, const char *sub_module_name, + const char *table_name, const uint8 **p_buf, + const uint8 *buf_end, WASMTableImport *table, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = *p_buf, *p_end = buf_end; + uint32 declare_elem_type = 0; + uint32 declare_max_size_flag = 0; + uint32 declare_init_size = 0; + uint32 declare_max_size = 0; + + CHECK_BUF(p, p_end, 1); + /* 0x70 */ + declare_elem_type = read_uint8(p); + bh_assert(TABLE_ELEM_TYPE_ANY_FUNC == declare_elem_type); + + read_leb_uint32(p, p_end, declare_max_size_flag); + read_leb_uint32(p, p_end, declare_init_size); + if (declare_max_size_flag & 1) { + read_leb_uint32(p, p_end, declare_max_size); + bh_assert(table->init_size <= table->max_size); + } else { + declare_max_size = 0x10000; + } + *p_buf = p; + + bh_assert(!((declare_max_size_flag & 1) + && declare_init_size > declare_max_size)); + + /* now we believe all declaration are ok */ + table->elem_type = declare_elem_type; + table->init_size = declare_init_size; + table->flags = declare_max_size_flag; + table->max_size = declare_max_size; + return true; +} + +unsigned +wasm_runtime_memory_pool_size(); + +static bool +load_memory_import(WASMModule *sub_module, const char *sub_module_name, + const char *memory_name, const uint8 **p_buf, + const uint8 *buf_end, WASMMemoryImport *memory, + 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 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; +#endif /* WASM_ENABLE_APP_FRAMEWORK */ + uint32 declare_max_page_count_flag = 0; + uint32 declare_init_page_count = 0; + uint32 declare_max_page_count = 0; + + read_leb_uint32(p, p_end, declare_max_page_count_flag); + read_leb_uint32(p, p_end, declare_init_page_count); + bh_assert(declare_init_page_count <= 65536); + + if (declare_max_page_count_flag & 1) { + read_leb_uint32(p, p_end, declare_max_page_count); + bh_assert(declare_init_page_count <= declare_max_page_count); + bh_assert(declare_max_page_count <= 65536); + if (declare_max_page_count > max_page_count) { + declare_max_page_count = max_page_count; + } + } + else { + /* Limit the maximum memory size to max_page_count */ + declare_max_page_count = max_page_count; + } + + /* now we believe all declaration are ok */ + memory->flags = declare_max_page_count_flag; + memory->init_page_count = declare_init_page_count; + memory->max_page_count = declare_max_page_count; + memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE; + + *p_buf = p; + return true; +} + +static bool +load_global_import(const WASMModule *parent_module, + WASMModule *sub_module, + char *sub_module_name, char *global_name, + const uint8 **p_buf, const uint8 *buf_end, + WASMGlobalImport *global, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = *p_buf, *p_end = buf_end; + uint8 declare_type = 0; + uint8 declare_mutable = 0; + bool is_mutable = false; + bool ret = false; + + CHECK_BUF(p, p_end, 2); + declare_type = read_uint8(p); + declare_mutable = read_uint8(p); + *p_buf = p; + + bh_assert(declare_mutable < 2); + + is_mutable = declare_mutable & 1 ? true : false; + +#if WASM_ENABLE_LIBC_BUILTIN != 0 + ret = wasm_runtime_is_built_in_module(sub_module_name); + if (ret) { + /* check built-in modules */ + ret = wasm_native_lookup_libc_builtin_global(sub_module_name, + global_name, global); + if (ret) { + LOG_DEBUG("(%s, %s) is a global of a built-in module", + sub_module_name, global_name); + } + } +#endif /* WASM_ENABLE_LIBC_BUILTIN */ + + if (!ret) { + set_error_buf_v(error_buf, error_buf_size, + "unknown import or incompatible import type"); + return false; + } + + global->module_name = sub_module_name; + global->field_name = global_name; + global->type = declare_type; + global->is_mutable = is_mutable; + (void)p_end; + return true; +} + +static bool +load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = *p_buf, *p_end = buf_end; + + CHECK_BUF(p, p_end, 1); + /* 0x70 */ + table->elem_type = read_uint8(p); + bh_assert(TABLE_ELEM_TYPE_ANY_FUNC == table->elem_type); + + read_leb_uint32(p, p_end, table->flags); + read_leb_uint32(p, p_end, table->init_size); + if (table->flags & 1) { + read_leb_uint32(p, p_end, table->max_size); + bh_assert(table->init_size <= table->max_size); + } + else + table->max_size = 0x10000; + + bh_assert(!((table->flags & 1) + && table->init_size > table->max_size)); + + *p_buf = p; + return true; +} + +static bool +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; + uint32 pool_size = wasm_runtime_memory_pool_size(); +#if WASM_ENABLE_APP_FRAMEWORK != 0 + 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; +#endif + + read_leb_uint32(p, p_end, memory->flags); + read_leb_uint32(p, p_end, memory->init_page_count); + bh_assert(memory->init_page_count <= 65536); + + if (memory->flags & 1) { + read_leb_uint32(p, p_end, memory->max_page_count); + bh_assert(memory->init_page_count <= memory->max_page_count); + bh_assert(memory->max_page_count <= 65536); + if (memory->max_page_count > max_page_count) + memory->max_page_count = max_page_count; + } + else + /* Limit the maximum memory size to max_page_count */ + memory->max_page_count = max_page_count; + + memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE; + + *p_buf = p; + return true; +} + +static bool +load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end, *p_old; + uint32 import_count, name_len, type_index, i, u32, flags; + uint64 total_size; + WASMImport *import; + WASMImport *import_functions = NULL, *import_tables = NULL; + WASMImport *import_memories = NULL, *import_globals = NULL; + char *sub_module_name, *field_name; + uint8 u8, kind; + + read_leb_uint32(p, p_end, import_count); + + if (import_count) { + module->import_count = import_count; + total_size = sizeof(WASMImport) * (uint64)import_count; + if (!(module->imports = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + p_old = p; + + /* Scan firstly to get import count of each type */ + for (i = 0; i < import_count; i++) { + /* module name */ + read_leb_uint32(p, p_end, name_len); + CHECK_BUF(p, p_end, name_len); + p += name_len; + + /* field name */ + read_leb_uint32(p, p_end, name_len); + CHECK_BUF(p, p_end, name_len); + p += name_len; + + CHECK_BUF(p, p_end, 1); + /* 0x00/0x01/0x02/0x03 */ + kind = read_uint8(p); + + switch (kind) { + case IMPORT_KIND_FUNC: /* import function */ + read_leb_uint32(p, p_end, type_index); + module->import_function_count++; + break; + + case IMPORT_KIND_TABLE: /* import table */ + CHECK_BUF(p, p_end, 1); + /* 0x70 */ + u8 = read_uint8(p); + read_leb_uint32(p, p_end, flags); + read_leb_uint32(p, p_end, u32); + if (flags & 1) + read_leb_uint32(p, p_end, u32); + module->import_table_count++; + bh_assert(module->import_table_count <= 1); + break; + + case IMPORT_KIND_MEMORY: /* import memory */ + read_leb_uint32(p, p_end, flags); + read_leb_uint32(p, p_end, u32); + if (flags & 1) + read_leb_uint32(p, p_end, u32); + module->import_memory_count++; + bh_assert(module->import_memory_count <= 1); + break; + + case IMPORT_KIND_GLOBAL: /* import global */ + CHECK_BUF(p, p_end, 2); + p += 2; + module->import_global_count++; + break; + + default: + bh_assert(0); + break; + } + } + + if (module->import_function_count) + import_functions = module->import_functions = module->imports; + if (module->import_table_count) + import_tables = module->import_tables = + module->imports + module->import_function_count; + if (module->import_memory_count) + import_memories = module->import_memories = + module->imports + module->import_function_count + module->import_table_count; + if (module->import_global_count) + import_globals = module->import_globals = + module->imports + module->import_function_count + module->import_table_count + + module->import_memory_count; + + p = p_old; + + // TODO: move it out of the loop + /* insert "env", "wasi_unstable" and "wasi_snapshot_preview1" to const str list */ + if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size) + || !const_str_list_insert((uint8*)"wasi_unstable", 13, module, + error_buf, error_buf_size) + || !const_str_list_insert((uint8*)"wasi_snapshot_preview1", 22, module, + error_buf, error_buf_size)) { + return false; + } + + /* Scan again to read the data */ + for (i = 0; i < import_count; i++) { + WASMModule *sub_module = NULL; + + /* load module name */ + read_leb_uint32(p, p_end, name_len); + CHECK_BUF(p, p_end, name_len); + if (!(sub_module_name = const_str_list_insert( + p, name_len, module, error_buf, error_buf_size))) { + return false; + } + p += name_len; + + /* load field name */ + read_leb_uint32(p, p_end, name_len); + CHECK_BUF(p, p_end, name_len); + if (!(field_name = const_str_list_insert( + p, name_len, module, error_buf, error_buf_size))) { + return false; + } + p += name_len; + + LOG_DEBUG("import #%d: (%s, %s)", i, sub_module_name, field_name); + + CHECK_BUF(p, p_end, 1); + /* 0x00/0x01/0x02/0x03 */ + kind = read_uint8(p); + switch (kind) { + case IMPORT_KIND_FUNC: /* import function */ + bh_assert(import_functions); + import = import_functions++; + if (!load_function_import(module, sub_module, + sub_module_name, field_name, &p, + p_end, &import->u.function, + error_buf, error_buf_size)) { + return false; + } + break; + + case IMPORT_KIND_TABLE: /* import table */ + bh_assert(import_tables); + import = import_tables++; + if (!load_table_import(sub_module, + sub_module_name, + field_name, + &p, + p_end, + &import->u.table, + error_buf, + error_buf_size)) { + LOG_DEBUG("can not import such a table (%s,%s)", + sub_module_name, field_name); + return false; + } + break; + + case IMPORT_KIND_MEMORY: /* import memory */ + bh_assert(import_memories); + import = import_memories++; + if (!load_memory_import(sub_module, + sub_module_name, + field_name, + &p, + p_end, + &import->u.memory, + error_buf, + error_buf_size)) { + return false; + } + break; + + case IMPORT_KIND_GLOBAL: /* import global */ + bh_assert(import_globals); + import = import_globals++; + if (!load_global_import(module, + sub_module, + sub_module_name, field_name, &p, + p_end, &import->u.global, + error_buf, error_buf_size)) { + return false; + } + break; + + default: + bh_assert(0); + import = NULL; + break; + } + import->kind = kind; + import->u.names.module_name = sub_module_name; + import->u.names.field_name = field_name; + (void)sub_module; + } + +#if WASM_ENABLE_LIBC_WASI != 0 + import = module->import_functions; + for (i = 0; i < module->import_function_count; i++, import++) { + if (!strcmp(import->u.names.module_name, "wasi_unstable") + || !strcmp(import->u.names.module_name, "wasi_snapshot_preview1")) { + module->is_wasi_module = true; + break; + } + } +#endif + } + + bh_assert(p == p_end); + + LOG_VERBOSE("Load import section success.\n"); + (void)u8; + (void)u32; + (void)type_index; + return true; +} + +static bool +init_function_local_offsets(WASMFunction *func, + char *error_buf, uint32 error_buf_size) +{ + WASMType *param_type = func->func_type; + uint32 param_count = param_type->param_count; + uint8 *param_types = param_type->types; + uint32 local_count = func->local_count; + uint8 *local_types = func->local_types; + uint32 i, local_offset = 0; + uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count); + + if (!(func->local_offsets = + loader_malloc(total_size, error_buf, error_buf_size))) { + return false; + } + + for (i = 0; i < param_count; i++) { + func->local_offsets[i] = (uint16)local_offset; + local_offset += wasm_value_type_cell_num(param_types[i]); + } + + for (i = 0; i < local_count; i++) { + func->local_offsets[param_count + i] = (uint16)local_offset; + local_offset += wasm_value_type_cell_num(local_types[i]); + } + + bh_assert(local_offset == func->param_cell_num + func->local_cell_num); + return true; +} + +static bool +load_function_section(const uint8 *buf, const uint8 *buf_end, + const uint8 *buf_code, const uint8 *buf_code_end, + WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + const uint8 *p_code = buf_code, *p_code_end, *p_code_save; + uint32 func_count; + uint64 total_size; + uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index; + uint32 local_count, local_set_count, sub_local_count; + uint8 type; + WASMFunction *func; + + read_leb_uint32(p, p_end, func_count); + + if (buf_code) + read_leb_uint32(p_code, buf_code_end, code_count); + + bh_assert(func_count == code_count); + + if (func_count) { + module->function_count = func_count; + total_size = sizeof(WASMFunction*) * (uint64)func_count; + if (!(module->functions = + loader_malloc(total_size, error_buf, error_buf_size))) { + return false; + } + + for (i = 0; i < func_count; i++) { + /* Resolve function type */ + read_leb_uint32(p, p_end, type_index); + bh_assert(type_index < module->type_count); + + read_leb_uint32(p_code, buf_code_end, code_size); + bh_assert(code_size > 0 + && p_code + code_size <= buf_code_end); + + /* Resolve local set count */ + p_code_end = p_code + code_size; + local_count = 0; + read_leb_uint32(p_code, buf_code_end, local_set_count); + p_code_save = p_code; + + /* Calculate total local count */ + for (j = 0; j < local_set_count; j++) { + read_leb_uint32(p_code, buf_code_end, sub_local_count); + bh_assert(sub_local_count <= UINT32_MAX - local_count); + + CHECK_BUF(p_code, buf_code_end, 1); + /* 0x7F/0x7E/0x7D/0x7C */ + type = read_uint8(p_code); + local_count += sub_local_count; + } + + /* Alloc memory, layout: function structure + local types */ + code_size = (uint32)(p_code_end - p_code); + + total_size = sizeof(WASMFunction) + (uint64)local_count; + if (!(func = module->functions[i] = + loader_malloc(total_size, error_buf, error_buf_size))) { + return false; + } + + /* Set function type, local count, code size and code body */ + func->func_type = module->types[type_index]; + func->local_count = local_count; + if (local_count > 0) + func->local_types = (uint8*)func + sizeof(WASMFunction); + func->code_size = code_size; + /* + * we shall make a copy of code body [p_code, p_code + code_size] + * when we are worrying about inappropriate releasing behaviour. + * all code bodies are actually in a buffer which user allocates in + * his embedding environment and we don't have power on them. + * it will be like: + * code_body_cp = malloc(code_size); + * memcpy(code_body_cp, p_code, code_size); + * func->code = code_body_cp; + */ + func->code = (uint8*)p_code; + + /* Load each local type */ + p_code = p_code_save; + local_type_index = 0; + for (j = 0; j < local_set_count; j++) { + read_leb_uint32(p_code, buf_code_end, sub_local_count); + bh_assert(!(local_type_index + sub_local_count <= local_type_index + || local_type_index + sub_local_count > local_count)); + + CHECK_BUF(p_code, buf_code_end, 1); + /* 0x7F/0x7E/0x7D/0x7C */ + type = read_uint8(p_code); + bh_assert(type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32); + for (k = 0; k < sub_local_count; k++) { + func->local_types[local_type_index++] = type; + } + } + + func->param_cell_num = wasm_type_param_cell_num(func->func_type); + func->ret_cell_num = wasm_type_return_cell_num(func->func_type); + func->local_cell_num = + wasm_get_cell_num(func->local_types, func->local_count); + + if (!init_function_local_offsets(func, error_buf, error_buf_size)) + return false; + + p_code = p_code_end; + } + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load function section success.\n"); + (void)code_count; + return true; +} + +static bool +load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 table_count, i; + uint64 total_size; + WASMTable *table; + + read_leb_uint32(p, p_end, table_count); + bh_assert(module->import_table_count + table_count <= 1); + + if (table_count) { + module->table_count = table_count; + total_size = sizeof(WASMTable) * (uint64)table_count; + if (!(module->tables = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + /* load each table */ + table = module->tables; + for (i = 0; i < table_count; i++, table++) + if (!load_table(&p, p_end, table, error_buf, error_buf_size)) + return false; + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load table section success.\n"); + return true; +} + +static bool +load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 memory_count, i; + uint64 total_size; + WASMMemory *memory; + + read_leb_uint32(p, p_end, memory_count); + bh_assert(module->import_memory_count + memory_count <= 1); + + if (memory_count) { + module->memory_count = memory_count; + total_size = sizeof(WASMMemory) * (uint64)memory_count; + if (!(module->memories = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + /* load each memory */ + memory = module->memories; + for (i = 0; i < memory_count; i++, memory++) + if (!load_memory(&p, p_end, memory, error_buf, error_buf_size)) + return false; + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load memory section success.\n"); + return true; +} + +static bool +load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 global_count, i; + uint64 total_size; + WASMGlobal *global; + uint8 mutable; + + read_leb_uint32(p, p_end, global_count); + + if (global_count) { + module->global_count = global_count; + total_size = sizeof(WASMGlobal) * (uint64)global_count; + if (!(module->globals = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + global = module->globals; + + for(i = 0; i < global_count; i++, global++) { + CHECK_BUF(p, p_end, 2); + global->type = read_uint8(p); + mutable = read_uint8(p); + bh_assert(mutable < 2); + global->is_mutable = mutable ? true : false; + + /* initialize expression */ + if (!load_init_expr(&p, p_end, &(global->init_expr), + global->type, error_buf, error_buf_size)) + return false; + + if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) { + /** + * Currently, constant expressions occurring as initializers + * of globals are further constrained in that contained + * global.get instructions are + * only allowed to refer to imported globals. + */ + uint32 target_global_index = global->init_expr.u.global_index; + bh_assert(target_global_index < module->import_global_count); + (void)target_global_index; + } + } + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load global section success.\n"); + return true; +} + +static bool +load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 export_count, i, j, index; + uint64 total_size; + uint32 str_len; + WASMExport *export; + const char *name; + + read_leb_uint32(p, p_end, export_count); + + if (export_count) { + module->export_count = export_count; + total_size = sizeof(WASMExport) * (uint64)export_count; + if (!(module->exports = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + export = module->exports; + for (i = 0; i < export_count; i++, export++) { + read_leb_uint32(p, p_end, str_len); + CHECK_BUF(p, p_end, str_len); + + for (j = 0; j < i; j++) { + name = module->exports[j].name; + bh_assert(!(strlen(name) == str_len + && memcmp(name, p, str_len) == 0)); + } + + if (!(export->name = const_str_list_insert(p, str_len, module, + error_buf, error_buf_size))) { + return false; + } + + p += str_len; + CHECK_BUF(p, p_end, 1); + export->kind = read_uint8(p); + read_leb_uint32(p, p_end, index); + export->index = index; + + switch(export->kind) { + /*function index*/ + case EXPORT_KIND_FUNC: + bh_assert(index < module->function_count + + module->import_function_count); + break; + /*table index*/ + case EXPORT_KIND_TABLE: + bh_assert(index < module->table_count + + module->import_table_count); + break; + /*memory index*/ + case EXPORT_KIND_MEMORY: + bh_assert(index < module->memory_count + + module->import_memory_count); + break; + /*global index*/ + case EXPORT_KIND_GLOBAL: + bh_assert(index < module->global_count + + module->import_global_count); + break; + default: + bh_assert(0); + break; + } + } + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load export section success.\n"); + (void)name; + return true; +} + +static bool +load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 table_segment_count, i, j, table_index, function_count, function_index; + uint64 total_size; + WASMTableSeg *table_segment; + + read_leb_uint32(p, p_end, table_segment_count); + + if (table_segment_count) { + module->table_seg_count = table_segment_count; + total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count; + if (!(module->table_segments = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + table_segment = module->table_segments; + for (i = 0; i < table_segment_count; i++, table_segment++) { + bh_assert(p < p_end); + read_leb_uint32(p, p_end, table_index); + bh_assert(table_index < module->import_table_count + + module->table_count); + + table_segment->table_index = table_index; + + /* initialize expression */ + if (!load_init_expr(&p, p_end, &(table_segment->base_offset), + VALUE_TYPE_I32, error_buf, error_buf_size)) + return false; + + read_leb_uint32(p, p_end, function_count); + table_segment->function_count = function_count; + total_size = sizeof(uint32) * (uint64)function_count; + if (!(table_segment->func_indexes = (uint32 *) + loader_malloc(total_size, error_buf, error_buf_size))) { + return false; + } + + for (j = 0; j < function_count; j++) { + read_leb_uint32(p, p_end, function_index); + bh_assert(function_index < module->function_count + + module->function_count); + table_segment->func_indexes[j] = function_index; + } + } + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load table segment section success.\n"); + return true; +} + +static bool +load_data_segment_section(const uint8 *buf, const uint8 *buf_end, + WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 data_seg_count, i, mem_index, data_seg_len; + uint64 total_size; + WASMDataSeg *dataseg; + InitializerExpression init_expr; +#if WASM_ENABLE_BULK_MEMORY != 0 + bool is_passive = false; + uint32 mem_flag; +#endif + + read_leb_uint32(p, p_end, data_seg_count); + +#if WASM_ENABLE_BULK_MEMORY != 0 + bh_assert(module->data_seg_count1 == 0 + || data_seg_count == module->data_seg_count1); +#endif + + if (data_seg_count) { + module->data_seg_count = data_seg_count; + total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count; + if (!(module->data_segments = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + for (i = 0; i < data_seg_count; i++) { + read_leb_uint32(p, p_end, mem_index); +#if WASM_ENABLE_BULK_MEMORY != 0 + is_passive = false; + mem_flag = mem_index & 0x03; + switch (mem_flag) { + case 0x01: + is_passive = true; + break; + case 0x00: + /* no memory index, treat index as 0 */ + mem_index = 0; + goto check_mem_index; + case 0x02: + /* read following memory index */ + read_leb_uint32(p, p_end, mem_index); +check_mem_index: + bh_assert(mem_index < module->import_memory_count + + module->memory_count); + break; + case 0x03: + default: + bh_assert(0); + break; + } +#else + bh_assert(mem_index < module->import_memory_count + + module->memory_count); +#endif /* WASM_ENABLE_BULK_MEMORY */ + +#if WASM_ENABLE_BULK_MEMORY != 0 + if (!is_passive) +#endif + if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32, + error_buf, error_buf_size)) + return false; + + read_leb_uint32(p, p_end, data_seg_len); + + if (!(dataseg = module->data_segments[i] = loader_malloc + (sizeof(WASMDataSeg), error_buf, error_buf_size))) { + return false; + } + +#if WASM_ENABLE_BULK_MEMORY != 0 + dataseg->is_passive = is_passive; + if (!is_passive) +#endif + { + bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression), + &init_expr, sizeof(InitializerExpression)); + + dataseg->memory_index = mem_index; + } + + dataseg->data_length = data_seg_len; + CHECK_BUF(p, p_end, data_seg_len); + dataseg->data = (uint8*)p; + p += data_seg_len; + } + } + + bh_assert(p == p_end); + LOG_VERBOSE("Load data segment section success.\n"); + return true; +} + +#if WASM_ENABLE_BULK_MEMORY != 0 +static bool +load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 data_seg_count1 = 0; + + read_leb_uint32(p, p_end, data_seg_count1); + module->data_seg_count1 = data_seg_count1; + + bh_assert(p == p_end); + LOG_VERBOSE("Load datacount section success.\n"); + return true; +} +#endif + +static bool +load_code_section(const uint8 *buf, const uint8 *buf_end, + const uint8 *buf_func, + const uint8 *buf_func_end, + WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + const uint8 *p_func = buf_func; + uint32 func_count = 0, code_count; + + /* code has been loaded in function section, so pass it here, just check + * whether function and code section have inconsistent lengths */ + read_leb_uint32(p, p_end, code_count); + + if (buf_func) + read_leb_uint32(p_func, buf_func_end, func_count); + + bh_assert(func_count == code_count); + LOG_VERBOSE("Load code segment section success.\n"); + (void)code_count; + (void)func_count; + return true; +} + +static bool +load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + WASMType *type; + uint32 start_function; + + read_leb_uint32(p, p_end, start_function); + + bh_assert(start_function < module->function_count + + module->import_function_count); + + if (start_function < module->import_function_count) + type = module->import_functions[start_function].u.function.func_type; + else + type = module->functions[start_function - module->import_function_count] + ->func_type; + + bh_assert(type->param_count == 0 && type->result_count == 0); + + module->start_function = start_function; + + bh_assert(p == p_end); + LOG_VERBOSE("Load start section success.\n"); + (void)type; + return true; +} + +static bool +load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *p = buf, *p_end = buf_end; + uint32 name_len; + + bh_assert(p < p_end); + + read_leb_uint32(p, p_end, name_len); + + bh_assert(name_len > 0 + && p + name_len <= p_end); + LOG_VERBOSE("Load custom section success.\n"); + (void)name_len; + return true; +} + + +static bool +wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, + BlockAddr *block_addr_cache, + char *error_buf, uint32 error_buf_size); + +#if WASM_ENABLE_FAST_INTERP != 0 +void ** +wasm_interp_get_handle_table(); + +static void **handle_table; +#endif + +static bool +load_from_sections(WASMModule *module, WASMSection *sections, + char *error_buf, uint32 error_buf_size) +{ + WASMExport *export; + WASMSection *section = sections; + const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL, + *buf_func = NULL, *buf_func_end = NULL; + WASMGlobal *llvm_data_end_global = NULL, *llvm_heap_base_global = NULL; + WASMGlobal *llvm_stack_top_global = NULL, *global; + uint32 llvm_data_end = UINT32_MAX, llvm_heap_base = UINT32_MAX; + uint32 llvm_stack_top = UINT32_MAX, global_index, i; + uint32 data_end_global_index = UINT32_MAX; + uint32 heap_base_global_index = UINT32_MAX; + uint32 stack_top_global_index = UINT32_MAX; + BlockAddr *block_addr_cache; + uint64 total_size; + + /* Find code and function sections if have */ + while (section) { + if (section->section_type == SECTION_TYPE_CODE) { + buf_code = section->section_body; + buf_code_end = buf_code + section->section_body_size; + } + else if (section->section_type == SECTION_TYPE_FUNC) { + buf_func = section->section_body; + buf_func_end = buf_func + section->section_body_size; + } + section = section->next; + } + + section = sections; + while (section) { + buf = section->section_body; + buf_end = buf + section->section_body_size; + LOG_DEBUG("to section %d", section->section_type); + switch (section->section_type) { + case SECTION_TYPE_USER: + /* unsupported user section, ignore it. */ + if (!load_user_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_TYPE: + if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_IMPORT: + if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_FUNC: + if (!load_function_section(buf, buf_end, buf_code, buf_code_end, + module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_TABLE: + if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_MEMORY: + if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_GLOBAL: + if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_EXPORT: + if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_START: + if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_ELEM: + if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_CODE: + if (!load_code_section(buf, buf_end, buf_func, buf_func_end, + module, error_buf, error_buf_size)) + return false; + break; + case SECTION_TYPE_DATA: + if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; +#if WASM_ENABLE_BULK_MEMORY != 0 + case SECTION_TYPE_DATACOUNT: + if (!load_datacount_section(buf, buf_end, module, error_buf, error_buf_size)) + return false; + break; +#endif + default: + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: invalid section id"); + return false; + } + + section = section->next; + } + +#if WASM_ENABLE_FAST_INTERP != 0 + handle_table = wasm_interp_get_handle_table(); +#endif + + total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE * BLOCK_ADDR_CONFLICT_SIZE; + if (!(block_addr_cache = loader_malloc + (total_size, error_buf, error_buf_size))) { + return false; + } + + for (i = 0; i < module->function_count; i++) { + WASMFunction *func = module->functions[i]; + memset(block_addr_cache, 0, (uint32)total_size); + if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache, + error_buf, error_buf_size)) { + wasm_runtime_free(block_addr_cache); + return false; + } + } + wasm_runtime_free(block_addr_cache); + + /* Resolve llvm auxiliary data/stack/heap info and reset memory info */ + if (!module->possible_memory_grow) { + export = module->exports; + for (i = 0; i < module->export_count; i++, export++) { + if (export->kind == EXPORT_KIND_GLOBAL) { + if (!strcmp(export->name, "__heap_base")) { + global_index = export->index - module->import_global_count; + global = module->globals + global_index; + if (global->type == VALUE_TYPE_I32 + && !global->is_mutable + && global->init_expr.init_expr_type == + INIT_EXPR_TYPE_I32_CONST) { + heap_base_global_index = global_index; + llvm_heap_base_global = global; + llvm_heap_base = global->init_expr.u.i32; + LOG_VERBOSE("found llvm __heap_base global, value: %d\n", + llvm_heap_base); + } + } + else if (!strcmp(export->name, "__data_end")) { + global_index = export->index - module->import_global_count; + global = module->globals + global_index; + if (global->type == VALUE_TYPE_I32 + && !global->is_mutable + && global->init_expr.init_expr_type == + INIT_EXPR_TYPE_I32_CONST) { + data_end_global_index = global_index; + llvm_data_end_global = global; + llvm_data_end = global->init_expr.u.i32; + LOG_VERBOSE("found llvm __data_end global, value: %d\n", + llvm_data_end); + + llvm_data_end = align_uint(llvm_data_end, 16); + } + } + + if (llvm_data_end_global && llvm_heap_base_global) { + if ((data_end_global_index == heap_base_global_index + 1 + && (int32)data_end_global_index > 1) + || (heap_base_global_index == data_end_global_index + 1 + && (int32)heap_base_global_index > 1)) { + global_index = + data_end_global_index < heap_base_global_index + ? data_end_global_index - 1 : heap_base_global_index - 1; + global = module->globals + global_index; + if (global->type == VALUE_TYPE_I32 + && global->is_mutable + && global->init_expr.init_expr_type == + INIT_EXPR_TYPE_I32_CONST) { + llvm_stack_top_global = global; + llvm_stack_top = global->init_expr.u.i32; + stack_top_global_index = global_index; + LOG_VERBOSE("found llvm stack top global, " + "value: %d, global index: %d\n", + llvm_stack_top, global_index); + } + } + break; + } + } + } + + if (llvm_data_end_global + && llvm_heap_base_global + && llvm_stack_top_global + && llvm_stack_top <= llvm_heap_base) { + WASMMemoryImport *memory_import; + WASMMemory *memory; + uint64 init_memory_size; + uint32 shrunk_memory_size = llvm_heap_base > llvm_data_end + ? llvm_heap_base : llvm_data_end; + if (module->import_memory_count) { + memory_import = &module->import_memories[0].u.memory; + init_memory_size = (uint64)memory_import->num_bytes_per_page * + memory_import->init_page_count; + if (llvm_heap_base <= init_memory_size + && llvm_data_end <= init_memory_size) { + /* Reset memory info to decrease memory usage */ + memory_import->num_bytes_per_page = shrunk_memory_size; + memory_import->init_page_count = 1; + LOG_VERBOSE("reset import memory size to %d\n", + shrunk_memory_size); + } + } + if (module->memory_count) { + memory = &module->memories[0]; + init_memory_size = (uint64)memory->num_bytes_per_page * + memory->init_page_count; + if (llvm_heap_base <= init_memory_size + && llvm_data_end <= init_memory_size) { + /* Reset memory info to decrease memory usage */ + memory->num_bytes_per_page = shrunk_memory_size; + memory->init_page_count = 1; + LOG_VERBOSE("reset memory size to %d\n", shrunk_memory_size); + } + } + + module->llvm_aux_data_end = llvm_data_end; + module->llvm_aux_stack_bottom = llvm_stack_top; + module->llvm_aux_stack_size = llvm_stack_top > llvm_data_end + ? llvm_stack_top - llvm_data_end + : llvm_stack_top; + module->llvm_aux_stack_global_index = stack_top_global_index; + LOG_VERBOSE("aux stack bottom: %d, size: %d\n", + module->llvm_aux_stack_bottom, + module->llvm_aux_stack_size); + } + } + + return true; +} + +#if BH_ENABLE_MEMORY_PROFILING != 0 +static void wasm_loader_free(void *ptr) +{ + wasm_runtime_free(ptr); +} +#else +#define wasm_loader_free wasm_free +#endif + +static WASMModule* +create_module(char *error_buf, uint32 error_buf_size) +{ + WASMModule *module = loader_malloc(sizeof(WASMModule), + error_buf, error_buf_size); + + if (!module) { + return NULL; + } + + module->module_type = Wasm_Module_Bytecode; + + /* Set start_function to -1, means no start function */ + module->start_function = (uint32)-1; + +#if WASM_ENABLE_MULTI_MODULE != 0 + module->import_module_list = &module->import_module_list_head; +#endif + return module; +} + +WASMModule * +wasm_loader_load_from_sections(WASMSection *section_list, + char *error_buf, uint32 error_buf_size) +{ + WASMModule *module = create_module(error_buf, error_buf_size); + if (!module) + return NULL; + + if (!load_from_sections(module, section_list, error_buf, error_buf_size)) { + wasm_loader_unload(module); + return NULL; + } + + LOG_VERBOSE("Load module from sections success.\n"); + return module; +} + +static void +destroy_sections(WASMSection *section_list) +{ + WASMSection *section = section_list, *next; + while (section) { + next = section->next; + wasm_runtime_free(section); + section = next; + } +} + +static uint8 section_ids[] = { + SECTION_TYPE_USER, + SECTION_TYPE_TYPE, + SECTION_TYPE_IMPORT, + SECTION_TYPE_FUNC, + SECTION_TYPE_TABLE, + SECTION_TYPE_MEMORY, + SECTION_TYPE_GLOBAL, + SECTION_TYPE_EXPORT, + SECTION_TYPE_START, + SECTION_TYPE_ELEM, +#if WASM_ENABLE_BULK_MEMORY != 0 + SECTION_TYPE_DATACOUNT, +#endif + SECTION_TYPE_CODE, + SECTION_TYPE_DATA +}; + +static uint8 +get_section_index(uint8 section_type) +{ + uint8 max_id = sizeof(section_ids) / sizeof(uint8); + + for (uint8 i = 0; i < max_id; i++) { + if (section_type == section_ids[i]) + return i; + } + + return (uint8)-1; +} + +static bool +create_sections(const uint8 *buf, uint32 size, + WASMSection **p_section_list, + char *error_buf, uint32 error_buf_size) +{ + WASMSection *section_list_end = NULL, *section; + const uint8 *p = buf, *p_end = buf + size/*, *section_body*/; + uint8 section_type, section_index, last_section_index = (uint8)-1; + uint32 section_size; + + bh_assert(!*p_section_list); + + p += 8; + while (p < p_end) { + CHECK_BUF(p, p_end, 1); + section_type = read_uint8(p); + section_index = get_section_index(section_type); + if (section_index != (uint8)-1) { + if (section_type != SECTION_TYPE_USER) { + /* Custom sections may be inserted at any place, + while other sections must occur at most once + and in prescribed order. */ + bh_assert(last_section_index == (uint8)-1 + || last_section_index < section_index); + last_section_index = section_index; + } + CHECK_BUF1(p, p_end, 1); + read_leb_uint32(p, p_end, section_size); + CHECK_BUF1(p, p_end, section_size); + + if (!(section = loader_malloc(sizeof(WASMSection), + error_buf, error_buf_size))) { + return false; + } + + section->section_type = section_type; + section->section_body = (uint8*)p; + section->section_body_size = section_size; + + if (!*p_section_list) + *p_section_list = section_list_end = section; + else { + section_list_end->next = section; + section_list_end = section; + } + + p += section_size; + } + else { + bh_assert(0); + } + } + + (void)last_section_index; + return true; +} + +static void +exchange32(uint8* p_data) +{ + uint8 value = *p_data; + *p_data = *(p_data + 3); + *(p_data + 3) = value; + + value = *(p_data + 1); + *(p_data + 1) = *(p_data + 2); + *(p_data + 2) = value; +} + +static union { + int a; + char b; +} __ue = { .a = 1 }; + +#define is_little_endian() (__ue.b == 1) + +static bool +load(const uint8 *buf, uint32 size, WASMModule *module, + char *error_buf, uint32 error_buf_size) +{ + const uint8 *buf_end = buf + size; + const uint8 *p = buf, *p_end = buf_end; + uint32 magic_number, version; + WASMSection *section_list = NULL; + + CHECK_BUF1(p, p_end, sizeof(uint32)); + magic_number = read_uint32(p); + if (!is_little_endian()) + exchange32((uint8*)&magic_number); + + bh_assert(magic_number == WASM_MAGIC_NUMBER); + + CHECK_BUF1(p, p_end, sizeof(uint32)); + version = read_uint32(p); + if (!is_little_endian()) + exchange32((uint8*)&version); + + if (version != WASM_CURRENT_VERSION) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: unknown binary version"); + return false; + } + + if (!create_sections(buf, size, §ion_list, error_buf, error_buf_size) + || !load_from_sections(module, section_list, error_buf, error_buf_size)) { + destroy_sections(section_list); + return false; + } + + destroy_sections(section_list); + (void)p_end; + return true; +} + +WASMModule* +wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size) +{ + WASMModule *module = create_module(error_buf, error_buf_size); + if (!module) { + return NULL; + } + + if (!load(buf, size, module, error_buf, error_buf_size)) { + LOG_VERBOSE("Load module failed, %s", error_buf); + goto fail; + } + + LOG_VERBOSE("Load module success"); + return module; + +fail: + wasm_loader_unload(module); + return NULL; +} + +void +wasm_loader_unload(WASMModule *module) +{ + uint32 i; + + if (!module) + return; + + if (module->types) { + for (i = 0; i < module->type_count; i++) { + if (module->types[i]) + wasm_runtime_free(module->types[i]); + } + wasm_runtime_free(module->types); + } + + if (module->imports) + wasm_runtime_free(module->imports); + + if (module->functions) { + for (i = 0; i < module->function_count; i++) { + if (module->functions[i]) { + if (module->functions[i]->local_offsets) + wasm_runtime_free(module->functions[i]->local_offsets); +#if WASM_ENABLE_FAST_INTERP != 0 + if (module->functions[i]->code_compiled) + wasm_runtime_free(module->functions[i]->code_compiled); + if (module->functions[i]->consts) + wasm_runtime_free(module->functions[i]->consts); +#endif + wasm_runtime_free(module->functions[i]); + } + } + wasm_runtime_free(module->functions); + } + + if (module->tables) + wasm_runtime_free(module->tables); + + if (module->memories) + wasm_runtime_free(module->memories); + + if (module->globals) + wasm_runtime_free(module->globals); + + if (module->exports) + wasm_runtime_free(module->exports); + + if (module->table_segments) { + for (i = 0; i < module->table_seg_count; i++) { + if (module->table_segments[i].func_indexes) + wasm_runtime_free(module->table_segments[i].func_indexes); + } + wasm_runtime_free(module->table_segments); + } + + if (module->data_segments) { + for (i = 0; i < module->data_seg_count; i++) { + if (module->data_segments[i]) + wasm_runtime_free(module->data_segments[i]); + } + wasm_runtime_free(module->data_segments); + } + + if (module->const_str_list) { + StringNode *node = module->const_str_list, *node_next; + while (node) { + node_next = node->next; + wasm_runtime_free(node); + node = node_next; + } + } + + wasm_runtime_free(module); +} + +bool +wasm_loader_find_block_addr(BlockAddr *block_addr_cache, + const uint8 *start_addr, + const uint8 *code_end_addr, + uint8 block_type, + uint8 **p_else_addr, + uint8 **p_end_addr, + char *error_buf, + uint32 error_buf_size) +{ + const uint8 *p = start_addr, *p_end = code_end_addr; + uint8 *else_addr = NULL; + uint32 block_nested_depth = 1, count, i, j, t; + uint8 opcode, u8; + BlockAddr block_stack[16] = { 0 }, *block; + + i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE; + block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; + + for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) { + if (block[j].start_addr == start_addr) { + /* Cache hit */ + *p_else_addr = block[j].else_addr; + *p_end_addr = block[j].end_addr; + return true; + } + } + + /* Cache unhit */ + block_stack[0].start_addr = start_addr; + + while (p < code_end_addr) { + opcode = *p++; + + switch (opcode) { + case WASM_OP_UNREACHABLE: + case WASM_OP_NOP: + break; + + case WASM_OP_BLOCK: + case WASM_OP_LOOP: + case WASM_OP_IF: + CHECK_BUF(p, p_end, 1); + /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */ + u8 = read_uint8(p); + if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) { + block_stack[block_nested_depth].start_addr = p; + block_stack[block_nested_depth].else_addr = NULL; + } + block_nested_depth++; + break; + + case WASM_OP_ELSE: + if (block_type == BLOCK_TYPE_IF && block_nested_depth == 1) + else_addr = (uint8*)(p - 1); + if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr)) + block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1); + break; + + case WASM_OP_END: + if (block_nested_depth == 1) { + if (block_type == BLOCK_TYPE_IF) + *p_else_addr = else_addr; + *p_end_addr = (uint8*)(p - 1); + + block_stack[0].end_addr = (uint8*)(p - 1); + for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) { + start_addr = block_stack[t].start_addr; + if (start_addr) { + i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE; + block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i; + for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) + if (!block[j].start_addr) + break; + + if (j == BLOCK_ADDR_CONFLICT_SIZE) { + memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) * + sizeof(BlockAddr)); + j = 0; + + } + block[j].start_addr = block_stack[t].start_addr; + block[j].else_addr = block_stack[t].else_addr; + block[j].end_addr = block_stack[t].end_addr; + } + else + break; + } + return true; + } + else { + block_nested_depth--; + if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) + block_stack[block_nested_depth].end_addr = (uint8*)(p - 1); + } + break; + + case WASM_OP_BR: + case WASM_OP_BR_IF: + skip_leb_uint32(p, p_end); /* labelidx */ + break; + + case WASM_OP_BR_TABLE: + read_leb_uint32(p, p_end, count); /* lable num */ + for (i = 0; i <= count; i++) /* lableidxs */ + skip_leb_uint32(p, p_end); + break; + + case WASM_OP_RETURN: + break; + + case WASM_OP_CALL: + skip_leb_uint32(p, p_end); /* funcidx */ + break; + + case WASM_OP_CALL_INDIRECT: + skip_leb_uint32(p, p_end); /* typeidx */ + CHECK_BUF(p, p_end, 1); + u8 = read_uint8(p); /* 0x00 */ + break; + + case WASM_OP_DROP: + case WASM_OP_SELECT: + case WASM_OP_DROP_64: + case WASM_OP_SELECT_64: + break; + + case WASM_OP_GET_LOCAL: + case WASM_OP_SET_LOCAL: + case WASM_OP_TEE_LOCAL: + case WASM_OP_GET_GLOBAL: + case WASM_OP_SET_GLOBAL: + skip_leb_uint32(p, p_end); /* localidx */ + break; + + case EXT_OP_GET_LOCAL_FAST: + case EXT_OP_SET_LOCAL_FAST: + case EXT_OP_TEE_LOCAL_FAST: + CHECK_BUF(p, p_end, 1); + p++; + break; + + case WASM_OP_I32_LOAD: + case WASM_OP_I64_LOAD: + case WASM_OP_F32_LOAD: + case WASM_OP_F64_LOAD: + case WASM_OP_I32_LOAD8_S: + case WASM_OP_I32_LOAD8_U: + case WASM_OP_I32_LOAD16_S: + case WASM_OP_I32_LOAD16_U: + case WASM_OP_I64_LOAD8_S: + case WASM_OP_I64_LOAD8_U: + case WASM_OP_I64_LOAD16_S: + case WASM_OP_I64_LOAD16_U: + case WASM_OP_I64_LOAD32_S: + case WASM_OP_I64_LOAD32_U: + case WASM_OP_I32_STORE: + case WASM_OP_I64_STORE: + case WASM_OP_F32_STORE: + case WASM_OP_F64_STORE: + case WASM_OP_I32_STORE8: + case WASM_OP_I32_STORE16: + case WASM_OP_I64_STORE8: + case WASM_OP_I64_STORE16: + case WASM_OP_I64_STORE32: + skip_leb_uint32(p, p_end); /* align */ + skip_leb_uint32(p, p_end); /* offset */ + break; + + case WASM_OP_MEMORY_SIZE: + case WASM_OP_MEMORY_GROW: + skip_leb_uint32(p, p_end); /* 0x00 */ + break; + + case WASM_OP_I32_CONST: + skip_leb_int32(p, p_end); + break; + case WASM_OP_I64_CONST: + skip_leb_int64(p, p_end); + break; + case WASM_OP_F32_CONST: + p += sizeof(float32); + break; + case WASM_OP_F64_CONST: + p += sizeof(float64); + break; + + case WASM_OP_I32_EQZ: + case WASM_OP_I32_EQ: + case WASM_OP_I32_NE: + case WASM_OP_I32_LT_S: + case WASM_OP_I32_LT_U: + case WASM_OP_I32_GT_S: + case WASM_OP_I32_GT_U: + case WASM_OP_I32_LE_S: + case WASM_OP_I32_LE_U: + case WASM_OP_I32_GE_S: + case WASM_OP_I32_GE_U: + case WASM_OP_I64_EQZ: + case WASM_OP_I64_EQ: + case WASM_OP_I64_NE: + case WASM_OP_I64_LT_S: + case WASM_OP_I64_LT_U: + case WASM_OP_I64_GT_S: + case WASM_OP_I64_GT_U: + case WASM_OP_I64_LE_S: + case WASM_OP_I64_LE_U: + case WASM_OP_I64_GE_S: + case WASM_OP_I64_GE_U: + case WASM_OP_F32_EQ: + case WASM_OP_F32_NE: + case WASM_OP_F32_LT: + case WASM_OP_F32_GT: + case WASM_OP_F32_LE: + case WASM_OP_F32_GE: + case WASM_OP_F64_EQ: + case WASM_OP_F64_NE: + case WASM_OP_F64_LT: + case WASM_OP_F64_GT: + case WASM_OP_F64_LE: + case WASM_OP_F64_GE: + case WASM_OP_I32_CLZ: + case WASM_OP_I32_CTZ: + case WASM_OP_I32_POPCNT: + case WASM_OP_I32_ADD: + case WASM_OP_I32_SUB: + case WASM_OP_I32_MUL: + case WASM_OP_I32_DIV_S: + case WASM_OP_I32_DIV_U: + case WASM_OP_I32_REM_S: + case WASM_OP_I32_REM_U: + case WASM_OP_I32_AND: + case WASM_OP_I32_OR: + case WASM_OP_I32_XOR: + case WASM_OP_I32_SHL: + case WASM_OP_I32_SHR_S: + case WASM_OP_I32_SHR_U: + case WASM_OP_I32_ROTL: + case WASM_OP_I32_ROTR: + case WASM_OP_I64_CLZ: + case WASM_OP_I64_CTZ: + case WASM_OP_I64_POPCNT: + case WASM_OP_I64_ADD: + case WASM_OP_I64_SUB: + case WASM_OP_I64_MUL: + case WASM_OP_I64_DIV_S: + case WASM_OP_I64_DIV_U: + case WASM_OP_I64_REM_S: + case WASM_OP_I64_REM_U: + case WASM_OP_I64_AND: + case WASM_OP_I64_OR: + case WASM_OP_I64_XOR: + case WASM_OP_I64_SHL: + case WASM_OP_I64_SHR_S: + case WASM_OP_I64_SHR_U: + case WASM_OP_I64_ROTL: + case WASM_OP_I64_ROTR: + case WASM_OP_F32_ABS: + case WASM_OP_F32_NEG: + case WASM_OP_F32_CEIL: + case WASM_OP_F32_FLOOR: + case WASM_OP_F32_TRUNC: + case WASM_OP_F32_NEAREST: + case WASM_OP_F32_SQRT: + case WASM_OP_F32_ADD: + case WASM_OP_F32_SUB: + case WASM_OP_F32_MUL: + case WASM_OP_F32_DIV: + case WASM_OP_F32_MIN: + case WASM_OP_F32_MAX: + case WASM_OP_F32_COPYSIGN: + case WASM_OP_F64_ABS: + case WASM_OP_F64_NEG: + case WASM_OP_F64_CEIL: + case WASM_OP_F64_FLOOR: + case WASM_OP_F64_TRUNC: + case WASM_OP_F64_NEAREST: + case WASM_OP_F64_SQRT: + case WASM_OP_F64_ADD: + case WASM_OP_F64_SUB: + case WASM_OP_F64_MUL: + case WASM_OP_F64_DIV: + case WASM_OP_F64_MIN: + case WASM_OP_F64_MAX: + case WASM_OP_F64_COPYSIGN: + case WASM_OP_I32_WRAP_I64: + case WASM_OP_I32_TRUNC_S_F32: + case WASM_OP_I32_TRUNC_U_F32: + case WASM_OP_I32_TRUNC_S_F64: + case WASM_OP_I32_TRUNC_U_F64: + case WASM_OP_I64_EXTEND_S_I32: + case WASM_OP_I64_EXTEND_U_I32: + case WASM_OP_I64_TRUNC_S_F32: + case WASM_OP_I64_TRUNC_U_F32: + case WASM_OP_I64_TRUNC_S_F64: + case WASM_OP_I64_TRUNC_U_F64: + case WASM_OP_F32_CONVERT_S_I32: + case WASM_OP_F32_CONVERT_U_I32: + case WASM_OP_F32_CONVERT_S_I64: + case WASM_OP_F32_CONVERT_U_I64: + case WASM_OP_F32_DEMOTE_F64: + case WASM_OP_F64_CONVERT_S_I32: + case WASM_OP_F64_CONVERT_U_I32: + case WASM_OP_F64_CONVERT_S_I64: + case WASM_OP_F64_CONVERT_U_I64: + case WASM_OP_F64_PROMOTE_F32: + case WASM_OP_I32_REINTERPRET_F32: + case WASM_OP_I64_REINTERPRET_F64: + case WASM_OP_F32_REINTERPRET_I32: + case WASM_OP_F64_REINTERPRET_I64: + case WASM_OP_I32_EXTEND8_S: + case WASM_OP_I32_EXTEND16_S: + case WASM_OP_I64_EXTEND8_S: + case WASM_OP_I64_EXTEND16_S: + case WASM_OP_I64_EXTEND32_S: + break; + case WASM_OP_MISC_PREFIX: + { + opcode = read_uint8(p); + switch (opcode) { + case WASM_OP_I32_TRUNC_SAT_S_F32: + case WASM_OP_I32_TRUNC_SAT_U_F32: + case WASM_OP_I32_TRUNC_SAT_S_F64: + case WASM_OP_I32_TRUNC_SAT_U_F64: + case WASM_OP_I64_TRUNC_SAT_S_F32: + case WASM_OP_I64_TRUNC_SAT_U_F32: + case WASM_OP_I64_TRUNC_SAT_S_F64: + case WASM_OP_I64_TRUNC_SAT_U_F64: + break; +#if WASM_ENABLE_BULK_MEMORY != 0 + case WASM_OP_MEMORY_INIT: + skip_leb_uint32(p, p_end); + /* skip memory idx */ + p++; + break; + case WASM_OP_DATA_DROP: + skip_leb_uint32(p, p_end); + break; + case WASM_OP_MEMORY_COPY: + /* skip two memory idx */ + p += 2; + break; + case WASM_OP_MEMORY_FILL: + /* skip memory idx */ + p++; + break; +#endif + default: + bh_assert(0); + break; + } + break; + } + + default: + bh_assert(0); + break; + } + } + + (void)u8; + return false; +} + +#define REF_I32 VALUE_TYPE_I32 +#define REF_F32 VALUE_TYPE_F32 +#define REF_I64_1 VALUE_TYPE_I64 +#define REF_I64_2 VALUE_TYPE_I64 +#define REF_F64_1 VALUE_TYPE_F64 +#define REF_F64_2 VALUE_TYPE_F64 +#define REF_ANY VALUE_TYPE_ANY + +#if WASM_ENABLE_FAST_INTERP != 0 + +#if WASM_DEBUG_PREPROCESSOR != 0 +#define LOG_OP(...) os_printf(__VA_ARGS__) +#else +#define LOG_OP(...) +#endif + +#define PATCH_ELSE 0 +#define PATCH_END 1 +typedef struct BranchBlockPatch { + struct BranchBlockPatch *next; + uint8 patch_type; + uint8 *code_compiled; +} BranchBlockPatch; +#endif + +typedef struct BranchBlock { + uint8 block_type; + uint8 return_type; + uint8 *start_addr; + uint8 *else_addr; + uint8 *end_addr; + uint32 stack_cell_num; +#if WASM_ENABLE_FAST_INTERP != 0 + uint16 dynamic_offset; + uint8 *code_compiled; + BranchBlockPatch *patch_list; +#endif + + /* Indicate the operand stack is in polymorphic state. + * If the opcode is one of unreachable/br/br_table/return, stack is marked + * to polymorphic state until the block's 'end' opcode is processed. + * If stack is in polymorphic state and stack is empty, instruction can + * pop any type of value directly without decreasing stack top pointer + * and stack cell num. */ + bool is_stack_polymorphic; +} BranchBlock; + +typedef struct WASMLoaderContext { + /* frame ref stack */ + uint8 *frame_ref; + uint8 *frame_ref_bottom; + uint8 *frame_ref_boundary; + uint32 frame_ref_size; + uint32 stack_cell_num; + uint32 max_stack_cell_num; + + /* frame csp stack */ + BranchBlock *frame_csp; + BranchBlock *frame_csp_bottom; + BranchBlock *frame_csp_boundary; + uint32 frame_csp_size; + uint32 csp_num; + uint32 max_csp_num; + +#if WASM_ENABLE_FAST_INTERP != 0 + /* frame offset stack */ + int16 *frame_offset; + int16 *frame_offset_bottom; + int16 *frame_offset_boundary; + uint32 frame_offset_size; + int16 dynamic_offset; + int16 start_dynamic_offset; + int16 max_dynamic_offset; + + /* preserved local offset */ + int16 preserved_local_offset; + + /* const buffer */ + uint8 *const_buf; + uint16 num_const; + uint16 const_buf_size; + uint16 const_cell_num; + + /* processed code */ + uint8 *p_code_compiled; + uint8 *p_code_compiled_end; + uint32 code_compiled_size; +#endif +} WASMLoaderContext; + +typedef struct Const { + WASMValue value; + uint16 slot_index; + uint8 value_type; +} Const; + +static void* +memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, + char *error_buf, uint32 error_buf_size) +{ + uint8 *mem_new; + bh_assert(size_new > size_old); + if ((mem_new = loader_malloc + (size_new, error_buf, error_buf_size))) { + bh_memcpy_s(mem_new, size_new, mem_old, size_old); + memset(mem_new + size_old, 0, size_new - size_old); + wasm_runtime_free(mem_old); + } + return mem_new; +} + +#define MEM_REALLOC(mem, size_old, size_new) do { \ + void *mem_new = memory_realloc(mem, size_old, size_new, \ + error_buf, error_buf_size); \ + if (!mem_new) \ + goto fail; \ + mem = mem_new; \ + } while (0) + +#define CHECK_CSP_PUSH() do { \ + if (ctx->frame_csp >= ctx->frame_csp_boundary) { \ + MEM_REALLOC(ctx->frame_csp_bottom, ctx->frame_csp_size, \ + (uint32)(ctx->frame_csp_size \ + + 8 * sizeof(BranchBlock))); \ + ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \ + ctx->frame_csp_boundary = ctx->frame_csp_bottom + \ + ctx->frame_csp_size / sizeof(BranchBlock); \ + ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \ + } \ + } while (0) + +#define CHECK_CSP_POP() do { \ + bh_assert(ctx->csp_num >= 1); \ + } while (0) + +#if WASM_ENABLE_FAST_INTERP != 0 +static bool +check_offset_push(WASMLoaderContext *ctx, + char *error_buf, uint32 error_buf_size) +{ + uint32 cell_num = (ctx->frame_offset - ctx->frame_offset_bottom); + if (ctx->frame_offset >= ctx->frame_offset_boundary) { + MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size, + ctx->frame_offset_size + 16); + ctx->frame_offset_size += 16; + ctx->frame_offset_boundary = ctx->frame_offset_bottom + + ctx->frame_offset_size / sizeof(int16); + ctx->frame_offset = ctx->frame_offset_bottom + cell_num; + } + return true; +fail: + return false; +} + +static bool +check_offset_pop(WASMLoaderContext *ctx, uint32 cells) +{ + if (ctx->frame_offset - cells < ctx->frame_offset_bottom) + return false; + return true; +} + +static void +free_label_patch_list(BranchBlock *frame_csp) +{ + BranchBlockPatch *label_patch = frame_csp->patch_list; + BranchBlockPatch *next; + while (label_patch != NULL) { + next = label_patch->next; + wasm_runtime_free(label_patch); + label_patch = next; + } + frame_csp->patch_list = NULL; +} + +static void +free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num) +{ + BranchBlock *tmp_csp = frame_csp; + + for (uint32 i = 0; i < csp_num; i++) { + free_label_patch_list(tmp_csp); + tmp_csp ++; + } +} + +#endif + +static bool +check_stack_push(WASMLoaderContext *ctx, + char *error_buf, uint32 error_buf_size) +{ + if (ctx->frame_ref >= ctx->frame_ref_boundary) { + MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size, + ctx->frame_ref_size + 16); + ctx->frame_ref_size += 16; + ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size; + ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num; + } + return true; +fail: + return false; +} + + +static bool +check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, + char *error_buf, uint32 error_buf_size) +{ + bh_assert(!(((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) + && stack_cell_num < 1) + || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) + && stack_cell_num < 2))); + + bh_assert(!((type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32) + || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32) + || (type == VALUE_TYPE_I64 + && (*(frame_ref - 2) != REF_I64_1 + || *(frame_ref - 1) != REF_I64_2)) + || (type == VALUE_TYPE_F64 + && (*(frame_ref - 2) != REF_F64_1 + || *(frame_ref - 1) != REF_F64_2)))); + return true; +} + +static bool +check_stack_pop(WASMLoaderContext *ctx, uint8 type, + char *error_buf, uint32 error_buf_size) +{ + int32 block_stack_cell_num = (int32) + (ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num); + + if (block_stack_cell_num > 0 + && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) { + /* the stack top is a value of any type, return success */ + return true; + } + + if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, + type, error_buf, error_buf_size)) + return false; + + return true; +} + +static void +wasm_loader_ctx_destroy(WASMLoaderContext *ctx) +{ + if (ctx) { + if (ctx->frame_ref_bottom) + wasm_runtime_free(ctx->frame_ref_bottom); + if (ctx->frame_csp_bottom) { +#if WASM_ENABLE_FAST_INTERP != 0 + free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num); +#endif + wasm_runtime_free(ctx->frame_csp_bottom); + } +#if WASM_ENABLE_FAST_INTERP != 0 + if (ctx->frame_offset_bottom) + wasm_runtime_free(ctx->frame_offset_bottom); + if (ctx->const_buf) + wasm_runtime_free(ctx->const_buf); +#endif + wasm_runtime_free(ctx); + } +} + +static WASMLoaderContext* +wasm_loader_ctx_init(WASMFunction *func) +{ + WASMLoaderContext *loader_ctx = + wasm_runtime_malloc(sizeof(WASMLoaderContext)); + if (!loader_ctx) + return false; + memset(loader_ctx, 0, sizeof(WASMLoaderContext)); + + loader_ctx->frame_ref_size = 32; + if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = + wasm_runtime_malloc(loader_ctx->frame_ref_size))) + goto fail; + memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size); + loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + + loader_ctx->frame_ref_size; + + loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8; + if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = + wasm_runtime_malloc(loader_ctx->frame_csp_size))) + goto fail; + memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size); + loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8; + +#if WASM_ENABLE_FAST_INTERP != 0 + loader_ctx->frame_offset_size = sizeof(int16) * 32; + if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset = + wasm_runtime_malloc(loader_ctx->frame_offset_size))) + goto fail; + memset(loader_ctx->frame_offset_bottom, 0, + loader_ctx->frame_offset_size); + loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32; + + loader_ctx->num_const = 0; + loader_ctx->const_buf_size = sizeof(Const) * 8; + if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size))) + goto fail; + memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size); + + loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset = + loader_ctx->max_dynamic_offset = func->param_cell_num + + func->local_cell_num; +#endif + return loader_ctx; + +fail: + wasm_loader_ctx_destroy(loader_ctx); + return NULL; +} + +static bool +wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, + char *error_buf, uint32 error_buf_size) +{ + if (type == VALUE_TYPE_VOID) + return true; + + if (!check_stack_push(ctx, error_buf, error_buf_size)) + return false; + + *ctx->frame_ref++ = type; + ctx->stack_cell_num++; + if (ctx->stack_cell_num > ctx->max_stack_cell_num) + ctx->max_stack_cell_num = ctx->stack_cell_num; + + if (type == VALUE_TYPE_I32 + || type == VALUE_TYPE_F32 + || type == VALUE_TYPE_ANY) + return true; + + if (!check_stack_push(ctx, error_buf, error_buf_size)) + return false; + *ctx->frame_ref++ = type; + ctx->stack_cell_num++; + if (ctx->stack_cell_num > ctx->max_stack_cell_num) + ctx->max_stack_cell_num = ctx->stack_cell_num; + return true; +} + +static bool +wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, + char *error_buf, uint32 error_buf_size) +{ + BranchBlock *cur_block = ctx->frame_csp - 1; + int32 available_stack_cell = (int32) + (ctx->stack_cell_num - cur_block->stack_cell_num); + + /* Directly return success if current block is in stack + * polymorphic state while stack is empty. */ + if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic) + return true; + + if (type == VALUE_TYPE_VOID) + return true; + + if (!check_stack_pop(ctx, type, error_buf, error_buf_size)) + return false; + + ctx->frame_ref--; + ctx->stack_cell_num--; + + if (type == VALUE_TYPE_I32 + || type == VALUE_TYPE_F32 + || *ctx->frame_ref == VALUE_TYPE_ANY) + return true; + + ctx->frame_ref--; + ctx->stack_cell_num--; + return true; +} + +static bool +wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt, + uint8 type_push, uint8 type_pop, + char *error_buf, uint32 error_buf_size) +{ + for (int i = 0; i < pop_cnt; i++) { + if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf, error_buf_size)) + return false; + } + if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size)) + return false; + return true; +} + +static bool +wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 type, + uint8 ret_type, uint8* start_addr, + char *error_buf, uint32 error_buf_size) +{ + CHECK_CSP_PUSH(); + memset(ctx->frame_csp, 0, sizeof(BranchBlock)); + ctx->frame_csp->block_type = type; + ctx->frame_csp->return_type = ret_type; + ctx->frame_csp->start_addr = start_addr; + ctx->frame_csp->stack_cell_num = ctx->stack_cell_num; +#if WASM_ENABLE_FAST_INTERP != 0 + ctx->frame_csp->dynamic_offset = ctx->dynamic_offset; + ctx->frame_csp->patch_list = NULL; +#endif + ctx->frame_csp++; + ctx->csp_num++; + if (ctx->csp_num > ctx->max_csp_num) + ctx->max_csp_num = ctx->csp_num; + return true; +fail: + return false; +} + +static bool +wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, + char *error_buf, uint32 error_buf_size) +{ + CHECK_CSP_POP(); + ctx->frame_csp--; + ctx->csp_num--; + return true; +} + +static bool +wasm_loader_check_br(WASMLoaderContext *ctx, uint32 depth, + char *error_buf, uint32 error_buf_size) +{ + BranchBlock *target_block, *cur_block; + int32 available_stack_cell; + + bh_assert(ctx->csp_num >= depth + 1); + + target_block = ctx->frame_csp - (depth + 1); + cur_block = ctx->frame_csp - 1; + + available_stack_cell = (int32) + (ctx->stack_cell_num - cur_block->stack_cell_num); + + if (available_stack_cell <= 0 && target_block->is_stack_polymorphic) + return true; + + if (target_block->block_type != BLOCK_TYPE_LOOP) { + uint8 type = target_block->return_type; + if (!check_stack_top_values(ctx->frame_ref, available_stack_cell, + type, error_buf, error_buf_size)) + return false; + } + return true; +} + +#if WASM_ENABLE_FAST_INTERP != 0 + +#if WASM_ENABLE_ABS_LABEL_ADDR != 0 + +#define emit_label(opcode) do { \ + wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) + +#define skip_label() do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) + +#else + +#define emit_label(opcode) do { \ + int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \ + bh_assert(offset >= INT16_MIN && offset < INT16_MAX); \ + wasm_loader_emit_int16(loader_ctx, offset); \ + LOG_OP("\nemit_op [%02x]\t", opcode); \ + } while (0) + +// drop local.get / const / block / loop / end +#define skip_label() do { \ + wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \ + LOG_OP("\ndelete last op\n"); \ + } while (0) + +#endif /* WASM_ENABLE_ABS_LABEL_ADDR */ + +#define emit_empty_label_addr_and_frame_ip(type) do { \ + if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \ + loader_ctx->p_code_compiled, \ + error_buf, error_buf_size)) \ + goto fail; \ + /* label address, to be patched */ \ + wasm_loader_emit_ptr(loader_ctx, NULL); \ + } while (0) + +#define emit_br_info(frame_csp) do { \ + if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define LAST_OP_OUTPUT_I32() (last_op >= WASM_OP_I32_EQZ \ + && last_op <= WASM_OP_I32_ROTR) \ + || (last_op == WASM_OP_I32_LOAD \ + || last_op == WASM_OP_F32_LOAD) \ + || (last_op >= WASM_OP_I32_LOAD8_S \ + && last_op <= WASM_OP_I32_LOAD16_U) \ + || (last_op >= WASM_OP_F32_ABS \ + && last_op <= WASM_OP_F32_COPYSIGN) \ + || (last_op >= WASM_OP_I32_WRAP_I64 \ + && last_op <= WASM_OP_I32_TRUNC_U_F64) \ + || (last_op >= WASM_OP_F32_CONVERT_S_I32 \ + && last_op <= WASM_OP_F32_DEMOTE_F64) \ + || (last_op == WASM_OP_I32_REINTERPRET_F32) \ + || (last_op == WASM_OP_F32_REINTERPRET_I32) \ + || (last_op == EXT_OP_COPY_STACK_TOP) + +#define LAST_OP_OUTPUT_I64() (last_op >= WASM_OP_I64_CLZ \ + && last_op <= WASM_OP_I64_ROTR) \ + || (last_op >= WASM_OP_F64_ABS \ + && last_op <= WASM_OP_F64_COPYSIGN) \ + || (last_op == WASM_OP_I64_LOAD \ + || last_op == WASM_OP_F64_LOAD) \ + || (last_op >= WASM_OP_I64_LOAD8_S \ + && last_op <= WASM_OP_I64_LOAD32_U) \ + || (last_op >= WASM_OP_I64_EXTEND_S_I32 \ + && last_op <= WASM_OP_I64_TRUNC_U_F64) \ + || (last_op >= WASM_OP_F64_CONVERT_S_I32 \ + && last_op <= WASM_OP_F64_PROMOTE_F32) \ + || (last_op == WASM_OP_I64_REINTERPRET_F64) \ + || (last_op == WASM_OP_F64_REINTERPRET_I64) \ + || (last_op == EXT_OP_COPY_STACK_TOP_I64) + +#define GET_CONST_OFFSET(type, val) do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, \ + &val, &operand_offset, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define GET_CONST_F32_OFFSET(type, fval) do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, \ + &fval, &operand_offset, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define GET_CONST_F64_OFFSET(type, fval) do { \ + if (!(wasm_loader_get_const_offset(loader_ctx, type, \ + &fval, &operand_offset, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define emit_operand(ctx, offset) do { \ + wasm_loader_emit_int16(ctx, offset); \ + LOG_OP("%d\t", offset); \ + } while (0) + +#define emit_byte(ctx, byte) do { \ + wasm_loader_emit_uint8(ctx, byte); \ + LOG_OP("%d\t", byte); \ + } while (0) + +#define emit_leb() do { \ + wasm_loader_emit_leb(loader_ctx, p_org, p); \ + } while (0) + +#define emit_const(value) do { \ + GET_CONST_OFFSET(VALUE_TYPE_I32, value); \ + emit_operand(loader_ctx, operand_offset); \ + } while (0) + +static bool +wasm_loader_ctx_reinit(WASMLoaderContext *ctx) +{ + if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size))) + return false; + memset(ctx->p_code_compiled, 0, ctx->code_compiled_size); + ctx->p_code_compiled_end = ctx->p_code_compiled + + ctx->code_compiled_size; + + /* clean up frame ref */ + memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size); + ctx->frame_ref = ctx->frame_ref_bottom; + ctx->stack_cell_num = 0; + + /* clean up frame csp */ + memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size); + ctx->frame_csp = ctx->frame_csp_bottom; + ctx->csp_num = 0; + ctx->max_csp_num = 0; + + /* clean up frame offset */ + memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size); + ctx->frame_offset = ctx->frame_offset_bottom; + ctx->dynamic_offset = ctx->start_dynamic_offset; + + /* init preserved local offsets */ + ctx->preserved_local_offset = ctx->max_dynamic_offset; + + /* const buf is reserved */ + return true; +} + +static void +wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value) +{ + if (ctx->p_code_compiled) { + *(int16*)(ctx->p_code_compiled) = value; + ctx->p_code_compiled += sizeof(int16); + } + else + ctx->code_compiled_size += sizeof(int16); +} + +static void +wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value) +{ + if (ctx->p_code_compiled) { + *(ctx->p_code_compiled) = value; + ctx->p_code_compiled += sizeof(uint8); + } + else + ctx->code_compiled_size += sizeof(uint8); +} + +static void +wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value) +{ + if (ctx->p_code_compiled) { + *(uint8**)(ctx->p_code_compiled) = value; + ctx->p_code_compiled += sizeof(void *); + } + else + ctx->code_compiled_size += sizeof(void *); +} + +static void +wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size) +{ + if (ctx->p_code_compiled) { + ctx->p_code_compiled -= size; + } + else + ctx->code_compiled_size -= size; +} + +static void +wasm_loader_emit_leb(WASMLoaderContext *ctx, uint8* start, uint8* end) +{ + if (ctx->p_code_compiled) { + bh_memcpy_s(ctx->p_code_compiled, + ctx->p_code_compiled_end - ctx->p_code_compiled, + start, end - start); + ctx->p_code_compiled += (end - start); + } + else { + ctx->code_compiled_size += (end - start); + } + +} + +static bool +preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode, + uint32 local_index, uint32 local_type, bool *preserved, + char *error_buf, uint32 error_buf_size) +{ + int16 preserved_offset = (int16)local_index; + *preserved = false; + for (uint32 i = 0; i < loader_ctx->stack_cell_num; i++) { + /* move previous local into dynamic space before a set/tee_local opcode */ + if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) { + if (preserved_offset == (int16)local_index) { + *preserved = true; + skip_label(); + if (local_type == VALUE_TYPE_I32 + || local_type == VALUE_TYPE_F32) { + preserved_offset = loader_ctx->preserved_local_offset; + /* Only increase preserve offset in the second traversal */ + if (loader_ctx->p_code_compiled) + loader_ctx->preserved_local_offset++; + emit_label(EXT_OP_COPY_STACK_TOP); + } + else { + preserved_offset = loader_ctx->preserved_local_offset; + if (loader_ctx->p_code_compiled) + loader_ctx->preserved_local_offset += 2; + emit_label(EXT_OP_COPY_STACK_TOP_I64); + } + emit_operand(loader_ctx, local_index); + emit_operand(loader_ctx, preserved_offset); + emit_label(opcode); + } + loader_ctx->frame_offset_bottom[i] = preserved_offset; + } + } + + return true; + +#if WASM_ENABLE_ABS_LABEL_ADDR == 0 +fail: + return false; +#endif +} + +static bool +add_label_patch_to_list(BranchBlock *frame_csp, + uint8 patch_type, uint8 *p_code_compiled, + char *error_buf, uint32 error_buf_size) +{ + BranchBlockPatch *patch = loader_malloc + (sizeof(BranchBlockPatch), error_buf, error_buf_size); + if (!patch) { + return false; + } + patch->patch_type = patch_type; + patch->code_compiled = p_code_compiled; + if (!frame_csp->patch_list) { + frame_csp->patch_list = patch; + patch->next = NULL; + } + else { + patch->next = frame_csp->patch_list; + frame_csp->patch_list = patch; + } + return true; +} + +static void +apply_label_patch(WASMLoaderContext *ctx, uint8 depth, + uint8 patch_type) +{ + BranchBlock *frame_csp = ctx->frame_csp - depth; + BranchBlockPatch *node = frame_csp->patch_list; + BranchBlockPatch *node_prev = NULL, *node_next; + + if (!ctx->p_code_compiled) + return; + + while (node) { + node_next = node->next; + if (node->patch_type == patch_type) { + *((uint8**)node->code_compiled) = ctx->p_code_compiled; + if (node_prev == NULL) { + frame_csp->patch_list = node_next; + } + else { + node_prev->next = node_next; + } + wasm_runtime_free(node); + } + else { + node_prev = node; + } + node = node_next; + } +} + +static bool +wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, + char *error_buf, uint32 error_buf_size) +{ + emit_operand(ctx, frame_csp->dynamic_offset); + if (frame_csp->block_type == BLOCK_TYPE_LOOP || + frame_csp->return_type == VALUE_TYPE_VOID) { + emit_byte(ctx, 0); + emit_operand(ctx, 0); + } + else if (frame_csp->return_type == VALUE_TYPE_I32 + || frame_csp->return_type == VALUE_TYPE_F32) { + emit_byte(ctx, 1); + emit_operand(ctx, *(int16*)(ctx->frame_offset - 1)); + } + else if (frame_csp->return_type == VALUE_TYPE_I64 + || frame_csp->return_type == VALUE_TYPE_F64) { + emit_byte(ctx, 2); + emit_operand(ctx, *(int16*)(ctx->frame_offset - 2)); + } + + if (frame_csp->block_type == BLOCK_TYPE_LOOP) { + wasm_loader_emit_ptr(ctx, frame_csp->code_compiled); + } + else { + if (!add_label_patch_to_list(frame_csp, PATCH_END, + ctx->p_code_compiled, + error_buf, error_buf_size)) + return false; + /* label address, to be patched */ + wasm_loader_emit_ptr(ctx, NULL); + } + return true; +} + +static bool +wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type, + bool disable_emit, int16 operand_offset, + char *error_buf, uint32 error_buf_size) +{ + if (type == VALUE_TYPE_VOID) + return true; + + // only check memory overflow in first traverse + if (ctx->p_code_compiled == NULL) { + if (!check_offset_push(ctx, error_buf, error_buf_size)) + return false; + } + + if (disable_emit) + *(ctx->frame_offset)++ = operand_offset; + else { + emit_operand(ctx, ctx->dynamic_offset); + *(ctx->frame_offset)++ = ctx->dynamic_offset; + ctx->dynamic_offset++; + if (ctx->dynamic_offset > ctx->max_dynamic_offset) + ctx->max_dynamic_offset = ctx->dynamic_offset; + } + + if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) + return true; + + if (ctx->p_code_compiled == NULL) { + if (!check_offset_push(ctx, error_buf, error_buf_size)) + return false; + } + + ctx->frame_offset++; + if (!disable_emit) { + ctx->dynamic_offset++; + if (ctx->dynamic_offset > ctx->max_dynamic_offset) + ctx->max_dynamic_offset = ctx->dynamic_offset; + } + return true; +} + +/* This function should be in front of wasm_loader_pop_frame_ref + as they both use ctx->stack_cell_num, and ctx->stack_cell_num + will be modified by wasm_loader_pop_frame_ref */ +static bool +wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type, + char *error_buf, uint32 error_buf_size) +{ + /* if ctx->frame_csp equals ctx->frame_csp_bottom, + then current block is the function block */ + uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0; + BranchBlock *cur_block = ctx->frame_csp - depth; + int32 available_stack_cell = (int32) + (ctx->stack_cell_num - cur_block->stack_cell_num); + + /* Directly return success if current block is in stack + * polymorphic state while stack is empty. */ + if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic) + return true; + + if (type == VALUE_TYPE_VOID) + return true; + + if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) { + /* Check the offset stack bottom to ensure the frame offset + stack will not go underflow. But we don't thrown error + and return true here, because the error msg should be + given in wasm_loader_pop_frame_ref */ + if (!check_offset_pop(ctx, 1)) + return true; + + ctx->frame_offset -= 1; + if ((*(ctx->frame_offset) > ctx->start_dynamic_offset) + && (*(ctx->frame_offset) < ctx->max_dynamic_offset)) + ctx->dynamic_offset -= 1; + } + else { + if (!check_offset_pop(ctx, 2)) + return true; + + ctx->frame_offset -= 2; + if ((*(ctx->frame_offset) > ctx->start_dynamic_offset) + && (*(ctx->frame_offset) < ctx->max_dynamic_offset)) + ctx->dynamic_offset -= 2; + } + emit_operand(ctx, *(ctx->frame_offset)); + return true; +} + +static bool +wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt, + uint8 type_push, uint8 type_pop, + bool disable_emit, int16 operand_offset, + char *error_buf, uint32 error_buf_size) +{ + for (int i = 0; i < pop_cnt; i++) { + if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf, error_buf_size)) + return false; + } + if (!wasm_loader_push_frame_offset(ctx, type_push, + disable_emit, operand_offset, + error_buf, error_buf_size)) + return false; + + return true; +} + +static bool +wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type, + bool disable_emit, int16 operand_offset, + char *error_buf, uint32 error_buf_size) +{ + if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset, + error_buf, error_buf_size))) + return false; + if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size))) + return false; + + return true; +} + +static bool +wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type, + char *error_buf, uint32 error_buf_size) +{ + /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */ + if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size)) + return false; + if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) + return false; + + return true; +} + +static bool +wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt, + uint8 type_push, uint8 type_pop, + bool disable_emit, int16 operand_offset, + char *error_buf, uint32 error_buf_size) +{ + if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop, + disable_emit, operand_offset, + error_buf, error_buf_size)) + return false; + if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop, + error_buf, error_buf_size)) + return false; + + return true; +} + +static bool +wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, + void *value, int16 *offset, + char *error_buf, uint32 error_buf_size) +{ + int16 operand_offset = 0; + Const *c; + for (c = (Const *)ctx->const_buf; + (uint8*)c < ctx->const_buf + ctx->num_const * sizeof(Const); c ++) { + if ((type == c->value_type) + && ((type == VALUE_TYPE_I64 && *(int64*)value == c->value.i64) + || (type == VALUE_TYPE_I32 && *(int32*)value == c->value.i32) + || (type == VALUE_TYPE_F64 + && (0 == memcmp(value, &(c->value.f64), sizeof(float64)))) + || (type == VALUE_TYPE_F32 + && (0 == memcmp(value, &(c->value.f32), sizeof(float32)))))) { + operand_offset = c->slot_index; + break; + } + if (c->value_type == VALUE_TYPE_I64 + || c->value_type == VALUE_TYPE_F64) + operand_offset += 2; + else + operand_offset += 1; + } + if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) { + if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) { + MEM_REALLOC(ctx->const_buf, + ctx->const_buf_size, + ctx->const_buf_size + 4 * sizeof(Const)); + ctx->const_buf_size += 4 * sizeof(Const); + c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const)); + } + c->value_type = type; + switch (type) { + case VALUE_TYPE_F64: + bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value, sizeof(float64)); + ctx->const_cell_num += 2; + /* The const buf will be reversed, we use the second cell */ + /* of the i64/f64 const so the finnal offset is corrent */ + operand_offset ++; + break; + case VALUE_TYPE_I64: + c->value.i64 = *(int64*)value; + ctx->const_cell_num += 2; + operand_offset ++; + break; + case VALUE_TYPE_F32: + bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value, sizeof(float32)); + ctx->const_cell_num ++; + break; + case VALUE_TYPE_I32: + c->value.i32 = *(int32*)value; + ctx->const_cell_num ++; + break; + default: + break; + } + c->slot_index = operand_offset; + ctx->num_const ++; + LOG_OP("#### new const [%d]: %ld\n", + ctx->num_const, (int64)c->value.i64); + } + /* use negetive index for const */ + operand_offset = -(operand_offset + 1); + *offset = operand_offset; + return true; +fail: + return false; +} + +/* + PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset + -- Mostly used for the binary / compare operation + PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack + -- Mostly used in block / control instructions + + The POP will always emit the offset on the top of the frame_offset stack + PUSH can be used in two ways: + 1. directly PUSH: + PUSH_XXX(); + will allocate a dynamic space and emit + 2. silent PUSH: + operand_offset = xxx; disable_emit = true; + PUSH_XXX(); + only push the frame_offset stack, no emit +*/ +#define PUSH_I32() do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \ + disable_emit, operand_offset,\ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_F32() do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \ + disable_emit, operand_offset,\ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_I64() do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \ + disable_emit, operand_offset,\ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_F64() do { \ + if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \ + disable_emit, operand_offset,\ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_I32() do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_F32() do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_I64() do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_F64() do { \ + if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_OFFSET_TYPE(type) do { \ + if (!(wasm_loader_push_frame_offset(loader_ctx, type, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_OFFSET_TYPE(type) do { \ + if (!(wasm_loader_pop_frame_offset(loader_ctx, type, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_AND_PUSH(type_pop, type_push) do { \ + if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 1, \ + type_push, type_pop, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +/* type of POPs should be the same */ +#define POP2_AND_PUSH(type_pop, type_push) do { \ + if (!(wasm_loader_push_pop_frame_ref_offset(loader_ctx, 2, \ + type_push, type_pop, \ + disable_emit, operand_offset, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#else /* WASM_ENABLE_FAST_INTERP */ + +#define PUSH_I32() do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_F32() do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_I64() do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_F64() do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_I32() do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_F32() do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_I64() do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_F64() do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_AND_PUSH(type_pop, type_push) do { \ + if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, \ + type_push, type_pop, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +/* type of POPs should be the same */ +#define POP2_AND_PUSH(type_pop, type_push) do { \ + if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, \ + type_push, type_pop, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) +#endif /* WASM_ENABLE_FAST_INTERP */ + +#if WASM_ENABLE_FAST_INTERP != 0 + +static bool +reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, bool disable_emit, + char *error_buf, uint32 error_buf_size) +{ + int16 operand_offset = 0; + uint8 block_depth = 0; + if (opcode == WASM_OP_ELSE) + block_depth = 1; + else + block_depth = 0; + + if ((loader_ctx->frame_csp - block_depth)->return_type != VALUE_TYPE_VOID) { + uint8 return_cells; + if ((loader_ctx->frame_csp - block_depth)->return_type == VALUE_TYPE_I32 + || (loader_ctx->frame_csp - block_depth)->return_type == VALUE_TYPE_F32) + return_cells = 1; + else + return_cells = 2; + if ((loader_ctx->frame_csp - block_depth)->dynamic_offset != + *(loader_ctx->frame_offset - return_cells)) { + + /* insert op_copy before else opcode */ + if (opcode == WASM_OP_ELSE) + skip_label(); + + if (return_cells == 1) + emit_label(EXT_OP_COPY_STACK_TOP); + else + emit_label(EXT_OP_COPY_STACK_TOP_I64); + emit_operand(loader_ctx, *(loader_ctx->frame_offset - return_cells)); + emit_operand(loader_ctx, (loader_ctx->frame_csp - block_depth)->dynamic_offset); + + if (opcode == WASM_OP_ELSE) { + *(loader_ctx->frame_offset - return_cells) = + (loader_ctx->frame_csp - block_depth)->dynamic_offset; + } + else { + loader_ctx->frame_offset -= return_cells; + loader_ctx->dynamic_offset = loader_ctx->frame_csp->dynamic_offset; + PUSH_OFFSET_TYPE((loader_ctx->frame_csp - block_depth)->return_type); + wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); + } + if (opcode == WASM_OP_ELSE) + emit_label(opcode); + } + } + + return true; + +fail: + return false; +} + +#endif /* WASM_ENABLE_FAST_INTERP */ + +#define RESERVE_BLOCK_RET() do { \ + if (!reserve_block_ret(loader_ctx, opcode, disable_emit, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define PUSH_TYPE(type) do { \ + if (!(wasm_loader_push_frame_ref(loader_ctx, type, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define POP_TYPE(type) do { \ + if (!(wasm_loader_pop_frame_ref(loader_ctx, type, \ + error_buf, error_buf_size))) \ + goto fail; \ + } while (0) + +#define PUSH_CSP(type, ret_type, _start_addr) do { \ + if (!wasm_loader_push_frame_csp(loader_ctx, type, ret_type, \ + _start_addr, error_buf, \ + error_buf_size)) \ + goto fail; \ + } while (0) + +#define POP_CSP() do { \ + if (!wasm_loader_pop_frame_csp(loader_ctx, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \ + read_leb_uint32(p, p_end, local_idx); \ + bh_assert(local_idx < param_count + local_count);\ + local_type = local_idx < param_count \ + ? param_types[local_idx] \ + : local_types[local_idx - param_count]; \ + local_offset = local_offsets[local_idx]; \ + } while (0) + +#define CHECK_BR(depth) do { \ + if (!wasm_loader_check_br(loader_ctx, depth, \ + error_buf, error_buf_size)) \ + goto fail; \ + } while (0) + +#define CHECK_MEMORY() do { \ + bh_assert(module->import_memory_count \ + + module->memory_count > 0); \ + } while (0) + +#define CHECK_BLOCK_TYPE(type) do { \ + bh_assert(type == VALUE_TYPE_I32 \ + || type == VALUE_TYPE_I64 \ + || type == VALUE_TYPE_F32 \ + || type == VALUE_TYPE_F64 \ + || type == VALUE_TYPE_VOID); \ + } while (0) + +static BranchBlock * +check_branch_block(WASMLoaderContext *loader_ctx, + uint8 **p_buf, uint8 *buf_end, + char *error_buf, uint32 error_buf_size) +{ + uint8 *p = *p_buf, *p_end = buf_end; + BranchBlock *frame_csp_tmp; + uint32 depth; + + read_leb_uint32(p, p_end, depth); + CHECK_BR(depth); + frame_csp_tmp = loader_ctx->frame_csp - depth - 1; +#if WASM_ENABLE_FAST_INTERP != 0 + emit_br_info(frame_csp_tmp); +#endif + + *p_buf = p; + return frame_csp_tmp; +fail: + return NULL; +} + +static bool +check_branch_block_ret(WASMLoaderContext *loader_ctx, + BranchBlock *frame_csp_tmp, + char *error_buf, uint32 error_buf_size) +{ +#if WASM_ENABLE_FAST_INTERP != 0 + BranchBlock *cur_block = loader_ctx->frame_csp - 1; + bool disable_emit = true; + int16 operand_offset = 0; +#endif + if (frame_csp_tmp->block_type != BLOCK_TYPE_LOOP) { + uint8 block_return_type = frame_csp_tmp->return_type; +#if WASM_ENABLE_FAST_INTERP != 0 + /* If the stack is in polymorphic state, do fake pop and push on + offset stack to keep the depth of offset stack to be the same + with ref stack */ + if (cur_block->is_stack_polymorphic) { + POP_OFFSET_TYPE(block_return_type); + PUSH_OFFSET_TYPE(block_return_type); + } +#endif + POP_TYPE(block_return_type); + PUSH_TYPE(block_return_type); + } + return true; +fail: + return false; +} + +static bool +check_block_stack(WASMLoaderContext *ctx, BranchBlock *block, + char *error_buf, uint32 error_buf_size) +{ + uint8 type = block->return_type; + int32 available_stack_cell = (int32) + (ctx->stack_cell_num - block->stack_cell_num); + + if (type != VALUE_TYPE_VOID + && available_stack_cell <= 0 + && block->is_stack_polymorphic) { + if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)) +#if WASM_ENABLE_FAST_INTERP != 0 + || !(wasm_loader_push_frame_offset(ctx, type, true, 0, error_buf, error_buf_size)) +#endif + ) + return false; + return true; + } + + if (type != VALUE_TYPE_VOID + && available_stack_cell == 1 + && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) { + if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) { + /* If the stack top is a value of any type, change its type to the + * same as block return type and return success */ + *(ctx->frame_ref - 1) = type; + } + else { + if (!(wasm_loader_push_frame_ref(ctx, VALUE_TYPE_I32, + error_buf, error_buf_size)) +#if WASM_ENABLE_FAST_INTERP != 0 + || !(wasm_loader_push_frame_offset(ctx, VALUE_TYPE_I32, + true, 0, + error_buf, error_buf_size)) +#endif + ) + return false; + *(ctx->frame_ref - 1) = *(ctx->frame_ref - 2) = type; + } + return true; + } + + bh_assert(!(((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) + && available_stack_cell != 1) + || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) + && available_stack_cell != 2) + || (type == VALUE_TYPE_VOID + && available_stack_cell > 0))); + + if (!check_stack_top_values(ctx->frame_ref, available_stack_cell, + type, error_buf, error_buf_size)) + return false; + + return true; +} + +/* reset the stack to the state of before entering the last block */ +#if WASM_ENABLE_FAST_INTERP != 0 +#define RESET_STACK() do { \ + loader_ctx->stack_cell_num = \ + (loader_ctx->frame_csp - 1)->stack_cell_num; \ + loader_ctx->frame_ref = \ + loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ + loader_ctx->frame_offset = \ + loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \ +} while (0) +#else +#define RESET_STACK() do { \ + loader_ctx->stack_cell_num = \ + (loader_ctx->frame_csp - 1)->stack_cell_num; \ + loader_ctx->frame_ref = \ + loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \ +} while (0) +#endif + +/* set current block's stack polymorphic state */ +#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) do { \ + BranchBlock *cur_block = loader_ctx->frame_csp - 1; \ + cur_block->is_stack_polymorphic = flag; \ +} while (0) + +static bool +wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, + BlockAddr *block_addr_cache, + char *error_buf, uint32 error_buf_size) +{ + uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org; + uint32 param_count, local_count, global_count; + uint8 *param_types, ret_type, *local_types, local_type, global_type; + uint16 *local_offsets, local_offset; + uint32 count, i, local_idx, global_idx, u32, align, mem_offset; + int32 i32, i32_const = 0; + int64 i64; + uint8 opcode, u8, block_return_type; + bool return_value = false; + WASMLoaderContext *loader_ctx; + BranchBlock *frame_csp_tmp; +#if WASM_ENABLE_BULK_MEMORY != 0 + uint32 segment_index; +#endif +#if WASM_ENABLE_FAST_INTERP != 0 + uint8 *func_const_end, *func_const; + int16 operand_offset; + uint8 last_op = 0; + bool disable_emit, preserve_local = false; + float32 f32; + float64 f64; + + LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n", + func->param_cell_num, + func->local_cell_num, + func->ret_cell_num); +#endif + + global_count = module->import_global_count + module->global_count; + + param_count = func->func_type->param_count; + param_types = func->func_type->types; + ret_type = func->func_type->result_count + ? param_types[param_count] : VALUE_TYPE_VOID; + + local_count = func->local_count; + local_types = func->local_types; + local_offsets = func->local_offsets; + + if (!(loader_ctx = wasm_loader_ctx_init(func))) { + set_error_buf(error_buf, error_buf_size, + "WASM loader prepare bytecode failed: " + "allocate memory failed"); + goto fail; + } + +#if WASM_ENABLE_FAST_INTERP != 0 +re_scan: + if (loader_ctx->code_compiled_size > 0) { + if (!wasm_loader_ctx_reinit(loader_ctx)) { + set_error_buf(error_buf, error_buf_size, + "WASM loader prepare bytecode failed: " + "allocate memory failed"); + goto fail; + } + p = func->code; + func->code_compiled = loader_ctx->p_code_compiled; + } +#endif + + PUSH_CSP(BLOCK_TYPE_FUNCTION, ret_type, p); + + while (p < p_end) { + opcode = *p++; +#if WASM_ENABLE_FAST_INTERP != 0 + p_org = p; + disable_emit = false; + emit_label(opcode); +#endif + + switch (opcode) { + case WASM_OP_UNREACHABLE: + RESET_STACK(); + SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true); + break; + + case WASM_OP_NOP: +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); +#endif + break; + + case WASM_OP_BLOCK: + /* 0x40/0x7F/0x7E/0x7D/0x7C */ + block_return_type = read_uint8(p); + CHECK_BLOCK_TYPE(block_return_type); + PUSH_CSP(BLOCK_TYPE_BLOCK, block_return_type, p); +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); +#endif + break; + + case WASM_OP_LOOP: + /* 0x40/0x7F/0x7E/0x7D/0x7C */ + block_return_type = read_uint8(p); + CHECK_BLOCK_TYPE(block_return_type); + PUSH_CSP(BLOCK_TYPE_LOOP, block_return_type, p); +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + (loader_ctx->frame_csp - 1)->code_compiled = + loader_ctx->p_code_compiled; +#endif + break; + + case WASM_OP_IF: + POP_I32(); + /* 0x40/0x7F/0x7E/0x7D/0x7C */ + block_return_type = read_uint8(p); + CHECK_BLOCK_TYPE(block_return_type); + PUSH_CSP(BLOCK_TYPE_IF, block_return_type, p); +#if WASM_ENABLE_FAST_INTERP != 0 + emit_empty_label_addr_and_frame_ip(PATCH_ELSE); + emit_empty_label_addr_and_frame_ip(PATCH_END); +#endif + break; + + case WASM_OP_ELSE: + bh_assert(loader_ctx->csp_num >= 2 + && (loader_ctx->frame_csp - 1)->block_type + == BLOCK_TYPE_IF); + + /* check whether if branch's stack matches its result type */ + if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1, + error_buf, error_buf_size)) + goto fail; + + (loader_ctx->frame_csp - 1)->else_addr = p - 1; + +#if WASM_ENABLE_FAST_INTERP != 0 + /* if the result of if branch is in local or const area, add a copy op */ + RESERVE_BLOCK_RET(); + + emit_empty_label_addr_and_frame_ip(PATCH_END); + apply_label_patch(loader_ctx, 1, PATCH_ELSE); +#endif + RESET_STACK(); + SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false); + break; + + case WASM_OP_END: + { + + /* check whether block stack matches its result type */ + if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1, + error_buf, error_buf_size)) + goto fail; + + /* if has return value, but no else branch, fail */ + bh_assert(!((loader_ctx->frame_csp - 1)->block_type == BLOCK_TYPE_IF + && (loader_ctx->frame_csp - 1)->return_type != VALUE_TYPE_VOID + && !(loader_ctx->frame_csp - 1)->else_addr)); + + POP_CSP(); + +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + /* copy the result to the block return address */ + RESERVE_BLOCK_RET(); + + apply_label_patch(loader_ctx, 0, PATCH_END); + free_label_patch_list(loader_ctx->frame_csp); + if (loader_ctx->frame_csp->block_type == BLOCK_TYPE_FUNCTION) { + emit_label(WASM_OP_RETURN); + POP_OFFSET_TYPE(loader_ctx->frame_csp->return_type); + } +#endif + if (loader_ctx->csp_num > 0) { + loader_ctx->frame_csp->end_addr = p - 1; + } + else { + /* end of function block, function will return, + ignore the following bytecodes */ + p = p_end; + + continue; + } + + SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false); + break; + } + + case WASM_OP_BR: + { + if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end, + error_buf, error_buf_size))) + goto fail; + + if (!check_branch_block_ret(loader_ctx, frame_csp_tmp, + error_buf, error_buf_size)) + goto fail; + + RESET_STACK(); + SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true); + break; + } + + case WASM_OP_BR_IF: + { + POP_I32(); + + if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end, + error_buf, error_buf_size))) + goto fail; + + if (!check_branch_block_ret(loader_ctx, frame_csp_tmp, + error_buf, error_buf_size)) + goto fail; + + break; + } + + case WASM_OP_BR_TABLE: + { + uint8 ret_type; + + read_leb_uint32(p, p_end, count); +#if WASM_ENABLE_FAST_INTERP != 0 + emit_const(count); +#endif + POP_I32(); + + /* TODO: check the const */ + for (i = 0; i <= count; i++) { + if (!(frame_csp_tmp = check_branch_block(loader_ctx, &p, p_end, + error_buf, error_buf_size))) + goto fail; + + if (!check_branch_block_ret(loader_ctx, frame_csp_tmp, + error_buf, error_buf_size)) + goto fail; + + if (i == 0) { + ret_type = frame_csp_tmp->block_type == BLOCK_TYPE_LOOP ? + VALUE_TYPE_VOID : frame_csp_tmp->return_type; + } + else { + /* Check whether all table items have the same return type */ + uint8 tmp_ret_type = frame_csp_tmp->block_type == BLOCK_TYPE_LOOP ? + VALUE_TYPE_VOID : frame_csp_tmp->return_type; + bh_assert(ret_type == tmp_ret_type); + (void)tmp_ret_type; + } + } + + RESET_STACK(); + SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true); + (void)ret_type; + break; + } + + case WASM_OP_RETURN: + { + POP_TYPE(ret_type); + PUSH_TYPE(ret_type); + +#if WASM_ENABLE_FAST_INTERP != 0 + // emit the offset after return opcode + POP_OFFSET_TYPE(ret_type); +#endif + + RESET_STACK(); + SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true); + break; + } + + case WASM_OP_CALL: + { + WASMType *func_type; + uint32 func_idx; + int32 idx; + + read_leb_uint32(p, p_end, func_idx); +#if WASM_ENABLE_FAST_INTERP != 0 + // we need to emit func_idx before arguments + emit_const(func_idx); +#endif + + bh_assert(func_idx < module->import_function_count + + module->function_count); + + if (func_idx < module->import_function_count) + func_type = module->import_functions[func_idx].u.function.func_type; + else + func_type = + module->functions[func_idx - module->import_function_count]->func_type; + + if (func_type->param_count > 0) { + for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) { + POP_TYPE(func_type->types[idx]); +#if WASM_ENABLE_FAST_INTERP != 0 + POP_OFFSET_TYPE(func_type->types[idx]); +#endif + } + } + + if (func_type->result_count) { + PUSH_TYPE(func_type->types[func_type->param_count]); +#if WASM_ENABLE_FAST_INTERP != 0 + PUSH_OFFSET_TYPE(func_type->types[func_type->param_count]); +#endif + } + + func->has_op_func_call = true; + break; + } + + case WASM_OP_CALL_INDIRECT: + { + int32 idx; + WASMType *func_type; + uint32 type_idx; + + bh_assert(module->import_table_count + + module->table_count > 0); + + read_leb_uint32(p, p_end, type_idx); +#if WASM_ENABLE_FAST_INTERP != 0 + // we need to emit func_idx before arguments + emit_const(type_idx); +#endif + + /* reserved byte 0x00 */ + bh_assert(*p == 0x00); + p++; + + POP_I32(); + + bh_assert(type_idx < module->type_count); + + func_type = module->types[type_idx]; + + if (func_type->param_count > 0) { + for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) { + POP_TYPE(func_type->types[idx]); +#if WASM_ENABLE_FAST_INTERP != 0 + POP_OFFSET_TYPE(func_type->types[idx]); +#endif + } + } + + if (func_type->result_count > 0) { + PUSH_TYPE(func_type->types[func_type->param_count]); +#if WASM_ENABLE_FAST_INTERP != 0 + PUSH_OFFSET_TYPE(func_type->types[func_type->param_count]); +#endif + } + + func->has_op_func_call = true; + break; + } + + case WASM_OP_DROP: + case WASM_OP_DROP_64: + { + BranchBlock *cur_block = loader_ctx->frame_csp - 1; + int32 available_stack_cell = (int32) + (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + + bh_assert(!(available_stack_cell <= 0 + && !cur_block->is_stack_polymorphic)); + + if (available_stack_cell > 0) { + if (*(loader_ctx->frame_ref - 1) == REF_I32 + || *(loader_ctx->frame_ref - 1) == REF_F32) { + loader_ctx->frame_ref--; + loader_ctx->stack_cell_num--; +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + loader_ctx->frame_offset--; + if (*(loader_ctx->frame_offset) > + loader_ctx->start_dynamic_offset) + loader_ctx->dynamic_offset --; +#endif + } + else { + loader_ctx->frame_ref -= 2; + loader_ctx->stack_cell_num -= 2; +#if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0) + *(p - 1) = WASM_OP_DROP_64; +#endif +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + loader_ctx->frame_offset -= 2; + if (*(loader_ctx->frame_offset) > + loader_ctx->start_dynamic_offset) + loader_ctx->dynamic_offset -= 2; +#endif + } + } + else { +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); +#endif + } + break; + } + + case WASM_OP_SELECT: + case WASM_OP_SELECT_64: + { + uint8 ref_type; + BranchBlock *cur_block = loader_ctx->frame_csp - 1; + int32 available_stack_cell; + + POP_I32(); + + available_stack_cell = (int32) + (loader_ctx->stack_cell_num - cur_block->stack_cell_num); + + bh_assert(!(available_stack_cell <= 0 + && !cur_block->is_stack_polymorphic)); + + if (available_stack_cell > 0) { + switch (*(loader_ctx->frame_ref - 1)) { + case REF_I32: + case REF_F32: + break; + case REF_I64_2: + case REF_F64_2: +#if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0) + *(p - 1) = WASM_OP_SELECT_64; +#endif +#if WASM_ENABLE_FAST_INTERP != 0 + if (loader_ctx->p_code_compiled) { +#if WASM_ENABLE_ABS_LABEL_ADDR != 0 + *(void**)(loader_ctx->p_code_compiled - 2 - sizeof(void*)) = + handle_table[WASM_OP_SELECT_64]; +#else + *((int16*)loader_ctx->p_code_compiled - 2) = (int16) + (handle_table[WASM_OP_SELECT_64] - handle_table[0]); +#endif + } +#endif + break; + } + + ref_type = *(loader_ctx->frame_ref - 1); +#if WASM_ENABLE_FAST_INTERP != 0 + POP_OFFSET_TYPE(ref_type); +#endif + POP_TYPE(ref_type); +#if WASM_ENABLE_FAST_INTERP != 0 + POP_OFFSET_TYPE(ref_type); +#endif + POP_TYPE(ref_type); +#if WASM_ENABLE_FAST_INTERP != 0 + PUSH_OFFSET_TYPE(ref_type); +#endif + PUSH_TYPE(ref_type); + } + else { +#if WASM_ENABLE_FAST_INTERP != 0 + PUSH_OFFSET_TYPE(VALUE_TYPE_ANY); +#endif + PUSH_TYPE(VALUE_TYPE_ANY); + } + break; + } + + case WASM_OP_GET_LOCAL: + { + p_org = p - 1; + GET_LOCAL_INDEX_TYPE_AND_OFFSET(); + PUSH_TYPE(local_type); + +#if WASM_ENABLE_FAST_INTERP != 0 + /* Get Local is optimized out */ + skip_label(); + disable_emit = true; + operand_offset = local_offset; + PUSH_OFFSET_TYPE(local_type); +#else +#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) + if (local_offset < 0x80) { + *p_org++ = EXT_OP_GET_LOCAL_FAST; + if (local_type == VALUE_TYPE_I32 + || local_type == VALUE_TYPE_F32) + *p_org++ = (uint8)local_offset; + else + *p_org++ = (uint8)(local_offset | 0x80); + while (p_org < p) + *p_org++ = WASM_OP_NOP; + } +#endif +#endif + break; + } + + case WASM_OP_SET_LOCAL: + { + p_org = p - 1; + GET_LOCAL_INDEX_TYPE_AND_OFFSET(); + POP_TYPE(local_type); + +#if WASM_ENABLE_FAST_INTERP != 0 + if (!(preserve_referenced_local(loader_ctx, opcode, local_offset, + local_type, &preserve_local, + error_buf, error_buf_size))) + goto fail; + + if (local_offset < 256) { + skip_label(); + if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) { + if (loader_ctx->p_code_compiled) + *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset; + loader_ctx->frame_offset --; + loader_ctx->dynamic_offset --; + } + else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) { + if (loader_ctx->p_code_compiled) + *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset; + loader_ctx->frame_offset -= 2; + loader_ctx->dynamic_offset -= 2; + } + else { + if (local_type == VALUE_TYPE_I32 + || local_type == VALUE_TYPE_F32) { + emit_label(EXT_OP_SET_LOCAL_FAST); + emit_byte(loader_ctx, local_offset); + } + else { + emit_label(EXT_OP_SET_LOCAL_FAST_I64); + emit_byte(loader_ctx, local_offset); + } + POP_OFFSET_TYPE(local_type); + } + } + else { /* local index larger than 255, reserve leb */ + p_org ++; + emit_leb(); + POP_OFFSET_TYPE(local_type); + } +#else +#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) + if (local_offset < 0x80) { + *p_org++ = EXT_OP_SET_LOCAL_FAST; + if (local_type == VALUE_TYPE_I32 + || local_type == VALUE_TYPE_F32) + *p_org++ = (uint8)local_offset; + else + *p_org++ = (uint8)(local_offset | 0x80); + while (p_org < p) + *p_org++ = WASM_OP_NOP; + } +#endif +#endif + break; + } + + case WASM_OP_TEE_LOCAL: + { + p_org = p - 1; + GET_LOCAL_INDEX_TYPE_AND_OFFSET(); +#if WASM_ENABLE_FAST_INTERP != 0 + /* If the stack is in polymorphic state, do fake pop and push on + offset stack to keep the depth of offset stack to be the same + with ref stack */ + BranchBlock *cur_block = loader_ctx->frame_csp - 1; + if (cur_block->is_stack_polymorphic) { + POP_OFFSET_TYPE(local_type); + PUSH_OFFSET_TYPE(local_type); + } +#endif + POP_TYPE(local_type); + PUSH_TYPE(local_type); + +#if WASM_ENABLE_FAST_INTERP != 0 + if (!(preserve_referenced_local(loader_ctx, opcode, local_offset, + local_type, &preserve_local, + error_buf, error_buf_size))) + goto fail; + + if (local_offset < 256) { + skip_label(); + if (local_type == VALUE_TYPE_I32 + || local_type == VALUE_TYPE_F32) { + emit_label(EXT_OP_TEE_LOCAL_FAST); + emit_byte(loader_ctx, local_offset); + } + else { + emit_label(EXT_OP_TEE_LOCAL_FAST_I64); + emit_byte(loader_ctx, local_offset); + } + } + else { /* local index larger than 255, reserve leb */ + p_org ++; + emit_leb(); + } + emit_operand(loader_ctx, *(loader_ctx->frame_offset - + wasm_value_type_cell_num(local_type))); +#else +#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) + if (local_offset < 0x80) { + *p_org++ = EXT_OP_TEE_LOCAL_FAST; + if (local_type == VALUE_TYPE_I32 + || local_type == VALUE_TYPE_F32) + *p_org++ = (uint8)local_offset; + else + *p_org++ = (uint8)(local_offset | 0x80); + while (p_org < p) + *p_org++ = WASM_OP_NOP; + } +#endif +#endif + break; + } + + case WASM_OP_GET_GLOBAL: + { + read_leb_uint32(p, p_end, global_idx); + bh_assert(global_idx < global_count); + + global_type = global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.type + :module->globals[global_idx - module->import_global_count].type; + + PUSH_TYPE(global_type); +#if WASM_ENABLE_FAST_INTERP != 0 + emit_const(global_idx); + PUSH_OFFSET_TYPE(global_type); +#endif + break; + } + + case WASM_OP_SET_GLOBAL: + { + bool is_mutable = false; + read_leb_uint32(p, p_end, global_idx); + bh_assert(global_idx < global_count); + + is_mutable = + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.is_mutable + : module->globals[global_idx - module->import_global_count] + .is_mutable; + bh_assert(is_mutable); + + global_type = + global_idx < module->import_global_count + ? module->import_globals[global_idx].u.global.type + : module->globals[global_idx - module->import_global_count] + .type; + + POP_TYPE(global_type); +#if WASM_ENABLE_FAST_INTERP != 0 + emit_const(global_idx); + POP_OFFSET_TYPE(global_type); +#endif + (void)is_mutable; + break; + } + + /* load */ + case WASM_OP_I32_LOAD: + case WASM_OP_I32_LOAD8_S: + case WASM_OP_I32_LOAD8_U: + case WASM_OP_I32_LOAD16_S: + case WASM_OP_I32_LOAD16_U: + case WASM_OP_I64_LOAD: + case WASM_OP_I64_LOAD8_S: + case WASM_OP_I64_LOAD8_U: + case WASM_OP_I64_LOAD16_S: + case WASM_OP_I64_LOAD16_U: + case WASM_OP_I64_LOAD32_S: + case WASM_OP_I64_LOAD32_U: + case WASM_OP_F32_LOAD: + case WASM_OP_F64_LOAD: + /* store */ + case WASM_OP_I32_STORE: + case WASM_OP_I32_STORE8: + case WASM_OP_I32_STORE16: + case WASM_OP_I64_STORE: + case WASM_OP_I64_STORE8: + case WASM_OP_I64_STORE16: + case WASM_OP_I64_STORE32: + case WASM_OP_F32_STORE: + case WASM_OP_F64_STORE: + { +#if WASM_ENABLE_FAST_INTERP != 0 + /* change F32/F64 into I32/I64 */ + if (opcode == WASM_OP_F32_LOAD) { + skip_label(); + emit_label(WASM_OP_I32_LOAD); + } + else if (opcode == WASM_OP_F64_LOAD) { + skip_label(); + emit_label(WASM_OP_I64_LOAD); + } + else if (opcode == WASM_OP_F32_STORE) { + skip_label(); + emit_label(WASM_OP_I32_STORE); + } + else if (opcode == WASM_OP_F64_STORE) { + skip_label(); + emit_label(WASM_OP_I64_STORE); + } +#endif + CHECK_MEMORY(); + read_leb_uint32(p, p_end, align); /* align */ + read_leb_uint32(p, p_end, mem_offset); /* offset */ +#if WASM_ENABLE_FAST_INTERP != 0 + emit_const(mem_offset); +#endif + switch (opcode) + { + /* load */ + case WASM_OP_I32_LOAD: + case WASM_OP_I32_LOAD8_S: + case WASM_OP_I32_LOAD8_U: + case WASM_OP_I32_LOAD16_S: + case WASM_OP_I32_LOAD16_U: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32); + break; + case WASM_OP_I64_LOAD: + case WASM_OP_I64_LOAD8_S: + case WASM_OP_I64_LOAD8_U: + case WASM_OP_I64_LOAD16_S: + case WASM_OP_I64_LOAD16_U: + case WASM_OP_I64_LOAD32_S: + case WASM_OP_I64_LOAD32_U: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64); + break; + case WASM_OP_F32_LOAD: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32); + break; + case WASM_OP_F64_LOAD: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64); + break; + /* store */ + case WASM_OP_I32_STORE: + case WASM_OP_I32_STORE8: + case WASM_OP_I32_STORE16: + POP_I32(); + POP_I32(); + break; + case WASM_OP_I64_STORE: + case WASM_OP_I64_STORE8: + case WASM_OP_I64_STORE16: + case WASM_OP_I64_STORE32: + POP_I64(); + POP_I32(); + break; + case WASM_OP_F32_STORE: + POP_F32(); + POP_I32(); + break; + case WASM_OP_F64_STORE: + POP_F64(); + POP_I32(); + break; + default: + break; + } + break; + } + + case WASM_OP_MEMORY_SIZE: + CHECK_MEMORY(); + /* reserved byte 0x00 */ + bh_assert(*p == 0x00); + p++; + PUSH_I32(); + break; + + case WASM_OP_MEMORY_GROW: + CHECK_MEMORY(); + /* reserved byte 0x00 */ + bh_assert(*p == 0x00); + p++; + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32); + + func->has_op_memory_grow = true; + module->possible_memory_grow = true; + break; + + case WASM_OP_I32_CONST: + read_leb_int32(p, p_end, i32_const); +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + disable_emit = true; + GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const); +#else + (void)i32_const; +#endif + PUSH_I32(); + break; + + case WASM_OP_I64_CONST: + read_leb_int64(p, p_end, i64); +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + disable_emit = true; + GET_CONST_OFFSET(VALUE_TYPE_I64, i64); +#endif + PUSH_I64(); + break; + + case WASM_OP_F32_CONST: + p += sizeof(float32); +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + disable_emit = true; + bh_memcpy_s((uint8*)&f32, sizeof(float32), p_org, sizeof(float32)); + GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32); +#endif + PUSH_F32(); + break; + + case WASM_OP_F64_CONST: + p += sizeof(float64); +#if WASM_ENABLE_FAST_INTERP != 0 + skip_label(); + disable_emit = true; + /* Some MCU may require 8-byte align */ + bh_memcpy_s((uint8*)&f64, sizeof(float64), p_org, sizeof(float64)); + GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64); +#endif + PUSH_F64(); + break; + + case WASM_OP_I32_EQZ: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32); + break; + + case WASM_OP_I32_EQ: + case WASM_OP_I32_NE: + case WASM_OP_I32_LT_S: + case WASM_OP_I32_LT_U: + case WASM_OP_I32_GT_S: + case WASM_OP_I32_GT_U: + case WASM_OP_I32_LE_S: + case WASM_OP_I32_LE_U: + case WASM_OP_I32_GE_S: + case WASM_OP_I32_GE_U: + POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32); + break; + + case WASM_OP_I64_EQZ: + POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32); + break; + + case WASM_OP_I64_EQ: + case WASM_OP_I64_NE: + case WASM_OP_I64_LT_S: + case WASM_OP_I64_LT_U: + case WASM_OP_I64_GT_S: + case WASM_OP_I64_GT_U: + case WASM_OP_I64_LE_S: + case WASM_OP_I64_LE_U: + case WASM_OP_I64_GE_S: + case WASM_OP_I64_GE_U: + POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32); + break; + + case WASM_OP_F32_EQ: + case WASM_OP_F32_NE: + case WASM_OP_F32_LT: + case WASM_OP_F32_GT: + case WASM_OP_F32_LE: + case WASM_OP_F32_GE: + POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); + break; + + case WASM_OP_F64_EQ: + case WASM_OP_F64_NE: + case WASM_OP_F64_LT: + case WASM_OP_F64_GT: + case WASM_OP_F64_LE: + case WASM_OP_F64_GE: + POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); + break; + + case WASM_OP_I32_CLZ: + case WASM_OP_I32_CTZ: + case WASM_OP_I32_POPCNT: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32); + break; + + case WASM_OP_I32_ADD: + case WASM_OP_I32_SUB: + case WASM_OP_I32_MUL: + case WASM_OP_I32_DIV_S: + case WASM_OP_I32_DIV_U: + case WASM_OP_I32_REM_S: + case WASM_OP_I32_REM_U: + case WASM_OP_I32_AND: + case WASM_OP_I32_OR: + case WASM_OP_I32_XOR: + case WASM_OP_I32_SHL: + case WASM_OP_I32_SHR_S: + case WASM_OP_I32_SHR_U: + case WASM_OP_I32_ROTL: + case WASM_OP_I32_ROTR: + POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32); + break; + + case WASM_OP_I64_CLZ: + case WASM_OP_I64_CTZ: + case WASM_OP_I64_POPCNT: + POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64); + break; + + case WASM_OP_I64_ADD: + case WASM_OP_I64_SUB: + case WASM_OP_I64_MUL: + case WASM_OP_I64_DIV_S: + case WASM_OP_I64_DIV_U: + case WASM_OP_I64_REM_S: + case WASM_OP_I64_REM_U: + case WASM_OP_I64_AND: + case WASM_OP_I64_OR: + case WASM_OP_I64_XOR: + case WASM_OP_I64_SHL: + case WASM_OP_I64_SHR_S: + case WASM_OP_I64_SHR_U: + case WASM_OP_I64_ROTL: + case WASM_OP_I64_ROTR: + POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64); + break; + + case WASM_OP_F32_ABS: + case WASM_OP_F32_NEG: + case WASM_OP_F32_CEIL: + case WASM_OP_F32_FLOOR: + case WASM_OP_F32_TRUNC: + case WASM_OP_F32_NEAREST: + case WASM_OP_F32_SQRT: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32); + break; + + case WASM_OP_F32_ADD: + case WASM_OP_F32_SUB: + case WASM_OP_F32_MUL: + case WASM_OP_F32_DIV: + case WASM_OP_F32_MIN: + case WASM_OP_F32_MAX: + case WASM_OP_F32_COPYSIGN: + POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32); + break; + + case WASM_OP_F64_ABS: + case WASM_OP_F64_NEG: + case WASM_OP_F64_CEIL: + case WASM_OP_F64_FLOOR: + case WASM_OP_F64_TRUNC: + case WASM_OP_F64_NEAREST: + case WASM_OP_F64_SQRT: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64); + break; + + case WASM_OP_F64_ADD: + case WASM_OP_F64_SUB: + case WASM_OP_F64_MUL: + case WASM_OP_F64_DIV: + case WASM_OP_F64_MIN: + case WASM_OP_F64_MAX: + case WASM_OP_F64_COPYSIGN: + POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64); + break; + + case WASM_OP_I32_WRAP_I64: + POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32); + break; + + case WASM_OP_I32_TRUNC_S_F32: + case WASM_OP_I32_TRUNC_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); + break; + + case WASM_OP_I32_TRUNC_S_F64: + case WASM_OP_I32_TRUNC_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); + break; + + case WASM_OP_I64_EXTEND_S_I32: + case WASM_OP_I64_EXTEND_U_I32: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64); + break; + + case WASM_OP_I64_TRUNC_S_F32: + case WASM_OP_I64_TRUNC_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64); + break; + + case WASM_OP_I64_TRUNC_S_F64: + case WASM_OP_I64_TRUNC_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64); + break; + + case WASM_OP_F32_CONVERT_S_I32: + case WASM_OP_F32_CONVERT_U_I32: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32); + break; + + case WASM_OP_F32_CONVERT_S_I64: + case WASM_OP_F32_CONVERT_U_I64: + POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32); + break; + + case WASM_OP_F32_DEMOTE_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32); + break; + + case WASM_OP_F64_CONVERT_S_I32: + case WASM_OP_F64_CONVERT_U_I32: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64); + break; + + case WASM_OP_F64_CONVERT_S_I64: + case WASM_OP_F64_CONVERT_U_I64: + POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64); + break; + + case WASM_OP_F64_PROMOTE_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64); + break; + + case WASM_OP_I32_REINTERPRET_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); + break; + + case WASM_OP_I64_REINTERPRET_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64); + break; + + case WASM_OP_F32_REINTERPRET_I32: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32); + break; + + case WASM_OP_F64_REINTERPRET_I64: + POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64); + break; + + case WASM_OP_I32_EXTEND8_S: + case WASM_OP_I32_EXTEND16_S: + POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32); + break; + + case WASM_OP_I64_EXTEND8_S: + case WASM_OP_I64_EXTEND16_S: + case WASM_OP_I64_EXTEND32_S: + POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64); + break; + + case WASM_OP_MISC_PREFIX: + { + opcode = read_uint8(p); +#if WASM_ENABLE_FAST_INTERP != 0 + emit_byte(loader_ctx, opcode); +#endif + switch (opcode) + { + case WASM_OP_I32_TRUNC_SAT_S_F32: + case WASM_OP_I32_TRUNC_SAT_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32); + break; + case WASM_OP_I32_TRUNC_SAT_S_F64: + case WASM_OP_I32_TRUNC_SAT_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32); + break; + case WASM_OP_I64_TRUNC_SAT_S_F32: + case WASM_OP_I64_TRUNC_SAT_U_F32: + POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64); + break; + case WASM_OP_I64_TRUNC_SAT_S_F64: + case WASM_OP_I64_TRUNC_SAT_U_F64: + POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64); + break; +#if WASM_ENABLE_BULK_MEMORY != 0 + case WASM_OP_MEMORY_INIT: + read_leb_uint32(p, p_end, segment_index); +#if WASM_ENABLE_FAST_INTERP != 0 + emit_const(segment_index); +#endif + bh_assert(module->import_memory_count + + module->memory_count > 0); + + bh_assert(*p == 0x00); + p++; + + bh_assert(segment_index < module->data_seg_count); + bh_assert(module->data_seg_count1 > 0); + + POP_I32(); + POP_I32(); + POP_I32(); + break; + case WASM_OP_DATA_DROP: + read_leb_uint32(p, p_end, segment_index); +#if WASM_ENABLE_FAST_INTERP != 0 + emit_const(segment_index); +#endif + bh_assert(segment_index < module->data_seg_count); + bh_assert(module->data_seg_count1 > 0); + break; + case WASM_OP_MEMORY_COPY: + /* both src and dst memory index should be 0 */ + bh_assert(*(int16*)p != 0x0000); + p += 2; + + bh_assert(module->import_memory_count + + module->memory_count > 0); + + POP_I32(); + POP_I32(); + POP_I32(); + break; + case WASM_OP_MEMORY_FILL: + bh_assert(*p == 0); + p++; + + bh_assert(module->import_memory_count + + module->memory_count > 0); + + POP_I32(); + POP_I32(); + POP_I32(); + break; + /* TODO: to support bulk table operation */ +#endif /* WASM_ENABLE_BULK_MEMORY */ + default: + bh_assert(0); + break; + } + break; + } + + default: + bh_assert(0); + break; + } + +#if WASM_ENABLE_FAST_INTERP != 0 + last_op = opcode; +#endif + } + + if (loader_ctx->csp_num > 0) { + set_error_buf(error_buf, error_buf_size, + "WASM module load failed: " + "function body must end with END opcode."); + goto fail; + } + +#if WASM_ENABLE_FAST_INTERP != 0 + if (loader_ctx->p_code_compiled == NULL) + goto re_scan; + + func->const_cell_num = loader_ctx->const_cell_num; + if (!(func->consts = func_const = + loader_malloc(func->const_cell_num * 4, + error_buf, error_buf_size))) { + goto fail; + } + func_const_end = func->consts + func->const_cell_num * 4; + // reverse the const buf + for (int i = loader_ctx->num_const - 1; i >= 0; i--) { + Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const)); + if (c->value_type == VALUE_TYPE_F64 + || c->value_type == VALUE_TYPE_I64) { + bh_memcpy_s(func_const, func_const_end - func_const, + &(c->value.f64), sizeof(int64)); + func_const += sizeof(int64); + } else { + bh_memcpy_s(func_const, func_const_end - func_const, + &(c->value.f32), sizeof(int32)); + func_const += sizeof(int32); + } + } + + func->max_stack_cell_num = loader_ctx->preserved_local_offset - + loader_ctx->start_dynamic_offset + 1; +#else + func->max_stack_cell_num = loader_ctx->max_stack_cell_num; +#endif + func->max_block_num = loader_ctx->max_csp_num; + return_value = true; + +fail: + wasm_loader_ctx_destroy(loader_ctx); + + (void)u8; + (void)u32; + (void)i32; + (void)i64; + (void)global_count; + (void)local_count; + (void)local_offset; + (void)p_org; + (void)mem_offset; + (void)align; + return return_value; +} diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 923005a4..827691d2 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -39,6 +39,23 @@ wasm_unload(WASMModule *module) wasm_loader_unload(module); } +static void * +runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size) +{ + void *mem; + + if (size >= UINT32_MAX + || !(mem = wasm_runtime_malloc((uint32)size))) { + set_error_buf(error_buf, error_buf_size, + "WASM module instantiate failed: " + "allocate memory failed."); + return NULL; + } + + memset(mem, 0, (uint32)size); + return mem; +} + #if WASM_ENABLE_MULTI_MODULE != 0 static WASMModuleInstance * get_sub_module_inst(const WASMModuleInstance *parent_module_inst, @@ -93,14 +110,11 @@ memory_instantiate(uint32 num_bytes_per_page, num_bytes_per_page * (uint64)init_page_count; /* Allocate memory space, addr data and global data */ - if (total_size >= UINT32_MAX - || !(memory = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate memory failed: allocate memory failed."); + if (!(memory = runtime_malloc(total_size, + error_buf, error_buf_size))) { return NULL; } - memset(memory, 0, (uint32)total_size); memory->num_bytes_per_page = num_bytes_per_page; memory->cur_page_count = init_page_count; memory->max_page_count = max_page_count; @@ -144,16 +158,11 @@ memories_instantiate(const WASMModule *module, total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count; - if (total_size >= UINT32_MAX - || !(memories = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate memory failed: " - "allocate memory failed."); + if (!(memories = runtime_malloc(total_size, + error_buf, error_buf_size))) { return NULL; } - memset(memories, 0, (uint32)total_size); - /* instantiate memories from import section */ import = module->import_memories; for (i = 0; i < module->import_memory_count; i++, import++) { @@ -271,16 +280,11 @@ tables_instantiate(const WASMModule *module, uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count; WASMTableInstance **tables, *table; - if (total_size >= UINT32_MAX - || !(tables = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate table failed: " - "allocate memory failed."); + if (!(tables = runtime_malloc(total_size, + error_buf, error_buf_size))) { return NULL; } - memset(tables, 0, (uint32)total_size); - /* instantiate tables from import section */ import = module->import_tables; for (i = 0; i < module->import_table_count; i++, import++) { @@ -310,12 +314,8 @@ tables_instantiate(const WASMModule *module, + sizeof(uint32) * (uint64)import->u.table.init_size; } - if (total_size >= UINT32_MAX - || !(table = tables[table_index++] = - wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate table failed: " - "allocate memory failed."); + if (!(table = tables[table_index++] = runtime_malloc + (total_size, error_buf, error_buf_size))) { tables_deinstantiate(tables, table_count); return NULL; } @@ -342,12 +342,8 @@ tables_instantiate(const WASMModule *module, for (i = 0; i < module->table_count; i++) { total_size = offsetof(WASMTableInstance, base_addr) + sizeof(uint32) * (uint64)module->tables[i].init_size; - if (total_size >= UINT32_MAX - || !(table = tables[table_index++] = - wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate table failed: " - "allocate memory failed."); + if (!(table = tables[table_index++] = runtime_malloc + (total_size, error_buf, error_buf_size))) { tables_deinstantiate(tables, table_count); return NULL; } @@ -392,16 +388,11 @@ functions_instantiate(const WASMModule *module, uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count; WASMFunctionInstance *functions, *function; - if (total_size >= UINT32_MAX - || !(functions = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate function failed: " - "allocate memory failed."); + if (!(functions = runtime_malloc(total_size, + error_buf, error_buf_size))) { return NULL; } - memset(functions, 0, (uint32)total_size); - /* instantiate functions from import section */ function = functions; import = module->import_functions; @@ -555,16 +546,11 @@ globals_instantiate(const WASMModule *module, uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count; WASMGlobalInstance *globals, *global; - if (total_size >= UINT32_MAX - || !(globals = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate global failed: " - "allocate memory failed."); + if (!(globals = runtime_malloc(total_size, + error_buf, error_buf_size))) { return NULL; } - memset(globals, 0, (uint32)total_size); - /* instantiate globals from import section */ global = globals; import = module->import_globals; @@ -727,16 +713,11 @@ export_functions_instantiate(const WASMModule *module, uint32 i; uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count; - if (total_size >= UINT32_MAX - || !(export_func = export_funcs = wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate export function failed: " - "allocate memory failed."); + if (!(export_func = export_funcs = runtime_malloc + (total_size, error_buf, error_buf_size))) { return NULL; } - memset(export_funcs, 0, (uint32)total_size); - for (i = 0; i < module->export_count; i++, export++) if (export->kind == EXPORT_KIND_FUNC) { export_func->name = export->name; @@ -767,17 +748,11 @@ export_globals_instantiate(const WASMModule *module, uint32 i; uint64 total_size = sizeof(WASMExportGlobInstance) * (uint64)export_glob_count; - if (total_size >= UINT32_MAX - || !(export_global = export_globals = - wasm_runtime_malloc((uint32)total_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate export global failed: " - "allocate memory failed."); + if (!(export_global = export_globals = runtime_malloc + (total_size, error_buf, error_buf_size))) { return NULL; } - memset(export_globals, 0, (uint32)total_size); - for (i = 0; i < module->export_count; i++, export++) if (export->kind == EXPORT_KIND_GLOBAL) { export_global->name = export->name; @@ -853,12 +828,11 @@ sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst, return false; } - WASMSubModInstNode *sub_module_inst_list_node = - wasm_runtime_malloc(sizeof(WASMSubModInstNode)); + WASMSubModInstNode *sub_module_inst_list_node = runtime_malloc + (sizeof(WASMSubModInstNode), error_buf, error_buf_size); if (!sub_module_inst_list_node) { LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d", sizeof(WASMSubModInstNode)); - set_error_buf_v(error_buf, error_buf_size, "malloc failed"); wasm_deinstantiate(sub_module_inst); return false; } @@ -921,9 +895,8 @@ wasm_instantiate(WASMModule *module, heap_size = APP_HEAP_SIZE_MAX; /* Allocate the memory */ - if (!(module_inst = wasm_runtime_malloc((uint32)sizeof(WASMModuleInstance)))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate module failed: allocate memory failed."); + if (!(module_inst = runtime_malloc(sizeof(WASMModuleInstance), + error_buf, error_buf_size))) { return NULL; } @@ -971,12 +944,11 @@ wasm_instantiate(WASMModule *module, #endif if (global_count > 0) { - if (!(module_inst->global_data = - wasm_runtime_malloc(global_data_size))) { + if (!(module_inst->global_data = runtime_malloc + (global_data_size, error_buf, error_buf_size))) { wasm_deinstantiate(module_inst); return NULL; } - memset(module_inst->global_data, 0, global_data_size); } /* Instantiate memories/tables/functions */ @@ -1546,13 +1518,17 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) return false; } - /* Destroy heap's lock firstly, if its memory is re-allocated, - we cannot access its lock again. */ - mem_allocator_destroy_lock(memory->heap_handle); + if (heap_size > 0) { + /* Destroy heap's lock firstly, if its memory is re-allocated, + we cannot access its lock again. */ + mem_allocator_destroy_lock(memory->heap_handle); + } if (!(new_memory = wasm_runtime_realloc(memory, (uint32)total_size))) { if (!(new_memory = wasm_runtime_malloc((uint32)total_size))) { - /* Restore heap's lock if memory re-alloc failed */ - mem_allocator_reinit_lock(memory->heap_handle); + if (heap_size > 0) { + /* Restore heap's lock if memory re-alloc failed */ + mem_allocator_reinit_lock(memory->heap_handle); + } wasm_set_exception(module, "fail to enlarge memory."); return false; } @@ -1564,12 +1540,14 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) memset((uint8*)new_memory + total_size_old, 0, (uint32)total_size - total_size_old); - new_memory->heap_handle = (uint8*)heap_handle_old + - ((uint8*)new_memory - (uint8*)memory); - if (mem_allocator_migrate(new_memory->heap_handle, - heap_handle_old) != 0) { - wasm_set_exception(module, "fail to enlarge memory."); - return false; + if (heap_size > 0) { + new_memory->heap_handle = (uint8*)heap_handle_old + + ((uint8*)new_memory - (uint8*)memory); + if (mem_allocator_migrate(new_memory->heap_handle, + heap_handle_old) != 0) { + wasm_set_exception(module, "fail to enlarge memory."); + return false; + } } new_memory->cur_page_count = total_page_count; @@ -1582,7 +1560,6 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) return true; } - bool wasm_call_indirect(WASMExecEnv *exec_env, uint32_t element_indices, @@ -1599,7 +1576,7 @@ wasm_call_indirect(WASMExecEnv *exec_env, table_inst = module_inst->default_table; if (!table_inst) { - wasm_set_exception(module_inst, "there is no table"); + wasm_set_exception(module_inst, "unknown table"); goto got_exception; } diff --git a/doc/build_wamr.md b/doc/build_wamr.md index eb4f7e6f..8d33ee6b 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -52,6 +52,11 @@ The script `runtime_lib.cmake` defined a number of variables for configuring the - **WAMR_BUILD_MULTI_MODULE**=1/0, default to disable if not set +#### **Enable WASM mini loader** + +- **WAMR_BUILD_MINI_LOADER**=1/0, default to disable if not set +Note: the mini loader doesn't check the integrity of the WASM binary file, user must ensure that the WASM file is not mal-formed. + **Combination of configurations:** We can combine the configurations. For example, if we want to disable interpreter, enable AOT and WASI, we can run command: diff --git a/product-mini/platforms/linux-sgx/CMakeLists.txt b/product-mini/platforms/linux-sgx/CMakeLists.txt index d3063686..a8da1f75 100644 --- a/product-mini/platforms/linux-sgx/CMakeLists.txt +++ b/product-mini/platforms/linux-sgx/CMakeLists.txt @@ -65,17 +65,8 @@ set (APP_FRAMEWORK_DIR ${WAMR_ROOT_DIR}/core/app-framework) # include the build config template file include (${WAMR_ROOT_DIR}/build-scripts/config_common.cmake) -if ("$ENV{SGX_SDK}" STREQUAL "") - set (SGX_SDK_DIR "/opt/intel/sgxsdk") -else() - set (SGX_SDK_DIR $ENV{SGX_SDK}) -endif() - include_directories (${SHARED_DIR}/include - ${IWASM_DIR}/include - ${SGX_SDK_DIR}/include - ${SGX_SDK_DIR}/include/tlibc - ${SGX_SDK_DIR}/include/libcxx) + ${IWASM_DIR}/include) enable_language (ASM) diff --git a/product-mini/platforms/linux/CMakeLists.txt b/product-mini/platforms/linux/CMakeLists.txt index fc2cb2a0..642155f4 100644 --- a/product-mini/platforms/linux/CMakeLists.txt +++ b/product-mini/platforms/linux/CMakeLists.txt @@ -65,6 +65,11 @@ if (NOT DEFINED WAMR_BUILD_MULTI_MODULE) set (WAMR_BUILD_MULTI_MODULE 0) endif () +if (NOT DEFINED WAMR_BUILD_MINI_LOADER) + # Disable wasm mini loader by default + set (WAMR_BUILD_MINI_LOADER 0) +endif () + set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..) include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)