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:
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user