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
12 changed files with 5285 additions and 431 deletions

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,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--) {

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;
}
/* 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;
}