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

@ -792,7 +792,7 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint8 wasm_ret_type, *wasm_ret_types;
uint64 total_size;
char buf[32];
bool ret;
bool ret = false;
/* Check function type index */
if (type_idx >= comp_ctx->comp_data->func_type_count) {
@ -1105,13 +1105,15 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_call_import);
/* Allocate memory for result values */
total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
if (total_size >= UINT32_MAX
|| !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("allocate memory failed.");
goto fail;
if (func_result_count > 0) {
total_size = sizeof(LLVMValueRef) * (uint64)func_result_count;
if (total_size >= UINT32_MAX
|| !(value_rets = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("allocate memory failed.");
goto fail;
}
memset(value_rets, 0, (uint32)total_size);
}
memset(value_rets, 0, total_size);
param_cell_num = func_type->param_cell_num;
wasm_ret_types = func_type->types + func_type->param_count;