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;