Implement wasm mini loader and refine footprint of loader and runtime (#276)

This commit is contained in:
wenyongh 2020-06-08 11:19:09 +08:00 committed by GitHub
parent 002f3b7ac4
commit 7a287fd1a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 5285 additions and 431 deletions

View File

@ -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 ()

View File

@ -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

View File

@ -59,13 +59,24 @@ static union {
#define is_little_endian() (__ue.b == 1)
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 (buf + length > buf_end) { \
set_error_buf(error_buf, error_buf_size, \
"Read data failed: unexpected end."); \
if (!check_buf(buf, buf_end, length, \
error_buf, error_buf_size)) { \
goto fail; \
} \
} while (0)
} 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;

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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}
)

View File

@ -24,19 +24,41 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
snprintf(error_buf, error_buf_size, "%s", string);
}
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 (buf + length > buf_end) { \
set_error_buf(error_buf, error_buf_size, \
"WASM module load failed: " \
"unexpected end of section or function"); \
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");\
if (!check_buf1(buf, buf_end, length, \
error_buf, error_buf_size)) { \
return false; \
} \
} while (0)
@ -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,19 +2444,20 @@ 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);
/* Resolve llvm auxiliary data/stack/heap info and reset memory info */
@ -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--) {

File diff suppressed because it is too large Load Diff

View File

@ -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;
}
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))) {
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,6 +1540,7 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
memset((uint8*)new_memory + total_size_old,
0, (uint32)total_size - total_size_old);
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,
@ -1571,6 +1548,7 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
wasm_set_exception(module, "fail to enlarge memory.");
return false;
}
}
new_memory->cur_page_count = total_page_count;
new_memory->heap_data = new_memory->base_addr;
@ -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;
}

View File

@ -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:

View File

@ -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)

View File

@ -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)