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:
@ -1712,9 +1712,11 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
|
||||
total_size = sizeof(char *) * (uint64)argc;
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(argv_list = wasm_runtime_malloc((uint32)total_size))
|
||||
|| (total_size > 0 &&
|
||||
!(argv_list = wasm_runtime_malloc((uint32)total_size)))
|
||||
|| argv_buf_size >= UINT32_MAX
|
||||
|| !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size))) {
|
||||
|| (argv_buf_size > 0 &&
|
||||
!(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"Init wasi environment failed: allocate memory failed");
|
||||
goto fail;
|
||||
@ -1730,11 +1732,13 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
for (i = 0; i < env_count; i++)
|
||||
env_buf_size += strlen(env[i]) + 1;
|
||||
|
||||
total_size = sizeof(char *) * (uint64)argc;
|
||||
total_size = sizeof(char *) * (uint64)env_count;
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(env_list = wasm_runtime_malloc((uint32)total_size))
|
||||
|| (total_size > 0
|
||||
&& !(env_list = wasm_runtime_malloc((uint32)total_size)))
|
||||
|| env_buf_size >= UINT32_MAX
|
||||
|| !(env_buf = wasm_runtime_malloc((uint32)env_buf_size))) {
|
||||
|| (env_buf_size > 0
|
||||
&& !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"Init wasi environment failed: allocate memory failed");
|
||||
goto fail;
|
||||
@ -2842,6 +2846,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
|
||||
n_stacks++;
|
||||
n_stacks += 2;
|
||||
}
|
||||
break;
|
||||
#endif /* BUILD_TARGET_RISCV32_ILP32D */
|
||||
default:
|
||||
bh_assert(0);
|
||||
|
||||
Reference in New Issue
Block a user