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:
@ -161,8 +161,14 @@ wasm_runtime_realloc_internal(void *ptr, unsigned int size)
|
||||
static inline void
|
||||
wasm_runtime_free_internal(void *ptr)
|
||||
{
|
||||
if (!ptr) {
|
||||
LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (memory_mode == MEMORY_MODE_UNKNOWN) {
|
||||
LOG_WARNING("wasm_runtime_free failed: memory hasn't been initialize.\n");
|
||||
LOG_WARNING("warning: wasm_runtime_free failed: "
|
||||
"memory hasn't been initialize.\n");
|
||||
}
|
||||
else if (memory_mode == MEMORY_MODE_POOL) {
|
||||
mem_allocator_free(pool_allocator, ptr);
|
||||
@ -175,6 +181,12 @@ wasm_runtime_free_internal(void *ptr)
|
||||
void *
|
||||
wasm_runtime_malloc(unsigned int size)
|
||||
{
|
||||
if (size == 0) {
|
||||
LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
|
||||
/* At lease alloc 1 byte to avoid malloc failed */
|
||||
size = 1;
|
||||
}
|
||||
|
||||
return wasm_runtime_malloc_internal(size);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user