Implement wasm mini loader and refine footprint of loader and runtime (#276)
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user