Add checks to avoid wasm_runtime_malloc memory with size 0 (#507)

In some platforms, allocating memory with size 0 may return NULL but not an empty memory block, which causes runtime load, instantiate or execute wasm/aot file failed. We add checks to try to avoid allocating memory in runtime if the size is 0. And in wasm_runtime_malloc/free, output warning if allocate memory with size 0 and free memory with NULL ptr.
Also fix some coding style issues, fix handle riscv32 ilp32d issue, and fix several wasm-c-api issues.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-01-28 02:16:02 -06:00
committed by GitHub
parent efd648959c
commit a5188f5574
20 changed files with 240 additions and 131 deletions

View File

@ -2148,7 +2148,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 (!(table_segment->func_indexes = (uint32 *)
if (total_size > 0
&& !(table_segment->func_indexes = (uint32 *)
loader_malloc(total_size, error_buf, error_buf_size))) {
return false;
}
@ -2444,7 +2445,7 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end,
previous_func_index = func_index;
read_leb_uint32(p, p_end, func_name_len);
CHECK_BUF(p, p_end, func_name_len);
// Skip the import functions
/* Skip the import functions */
if (func_index >= module->import_count) {
func_index -= module->import_count;
if (func_index >= module->function_count) {
@ -5697,7 +5698,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
uint32 segment_index;
#endif
#if WASM_ENABLE_FAST_INTERP != 0
uint8 *func_const_end, *func_const;
uint8 *func_const_end, *func_const = NULL;
int16 operand_offset;
uint8 last_op = 0;
bool disable_emit, preserve_local = false;
@ -7710,7 +7711,8 @@ fail_data_cnt_sec_require:
goto re_scan;
func->const_cell_num = loader_ctx->const_cell_num;
if (!(func->consts = func_const =
if (func->const_cell_num > 0
&& !(func->consts = func_const =
loader_malloc(func->const_cell_num * 4,
error_buf, error_buf_size))) {
goto fail;

View File

@ -1167,7 +1167,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 (!(table_segment->func_indexes = (uint32 *)
if (total_size > 0
&& !(table_segment->func_indexes = (uint32 *)
loader_malloc(total_size, error_buf, error_buf_size))) {
return false;
}
@ -1391,7 +1392,7 @@ handle_name_section(const uint8 *buf, const uint8 *buf_end,
previous_func_index = func_index;
read_leb_uint32(p, p_end, func_name_len);
CHECK_BUF(p, p_end, func_name_len);
// Skip the import functions
/* Skip the import functions */
if (func_index >= module->import_count) {
func_index -= module->import_count;
bh_assert(func_index < module->function_count);
@ -4257,7 +4258,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
uint32 segment_index;
#endif
#if WASM_ENABLE_FAST_INTERP != 0
uint8 *func_const_end, *func_const;
uint8 *func_const_end, *func_const = NULL;
int16 operand_offset;
uint8 last_op = 0;
bool disable_emit, preserve_local = false;
@ -5733,7 +5734,8 @@ handle_op_block_and_loop:
goto re_scan;
func->const_cell_num = loader_ctx->const_cell_num;
if (!(func->consts = func_const =
if (func->const_cell_num > 0
&& !(func->consts = func_const =
loader_malloc(func->const_cell_num * 4,
error_buf, error_buf_size))) {
goto fail;

View File

@ -110,7 +110,8 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
wasm_runtime_free(memories[i]->heap_handle);
memories[i]->heap_handle = NULL;
}
wasm_runtime_free(memories[i]->memory_data);
if (memories[i]->memory_data)
wasm_runtime_free(memories[i]->memory_data);
wasm_runtime_free(memories[i]);
}
}
@ -248,8 +249,10 @@ memory_instantiate(WASMModuleInstance *module_inst,
return NULL;
}
if (!(memory->memory_data =
runtime_malloc(memory_data_size, error_buf, error_buf_size))) {
if (memory_data_size > 0
&& !(memory->memory_data =
runtime_malloc(memory_data_size,
error_buf, error_buf_size))) {
goto fail1;
}
@ -307,7 +310,8 @@ fail3:
if (heap_size > 0)
wasm_runtime_free(memory->heap_handle);
fail2:
wasm_runtime_free(memory->memory_data);
if (memory->memory_data)
wasm_runtime_free(memory->memory_data);
fail1:
wasm_runtime_free(memory);
return NULL;
@ -1293,9 +1297,8 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
bh_assert(memory);
memory_data = memory->memory_data;
bh_assert(memory_data);
memory_size = memory->num_bytes_per_page * memory->cur_page_count;
bh_assert(memory_data || memory_size == 0);
bh_assert(data_seg->base_offset.init_expr_type
== INIT_EXPR_TYPE_I32_CONST
@ -1337,8 +1340,10 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
goto fail;
}
bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
data_seg->data, length);
if (memory_data) {
bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
data_seg->data, length);
}
}
/* Initialize the table data with table segment section */
@ -1970,9 +1975,11 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) {
return false;
}
bh_memcpy_s(new_memory_data, (uint32)total_size,
memory_data, total_size_old);
wasm_runtime_free(memory_data);
if (memory_data) {
bh_memcpy_s(new_memory_data, (uint32)total_size,
memory_data, total_size_old);
wasm_runtime_free(memory_data);
}
}
memset(new_memory_data + total_size_old,