Skip to content

Commit 0bf7f73

Browse files
no1wudiwenyongh
andauthored
Add NULL check for memory inst in aot/wasm module malloc/free (#403)
* Add NULL check for memory page in aot/wasm module malloc/free Signed-off-by: Huang Qi <[email protected]> * Update aot_runtime.c * Update wasm_runtime.c Co-authored-by: Huang Qi <[email protected]> Co-authored-by: Wenyong Huang <[email protected]>
1 parent 4bfcbc2 commit 0bf7f73

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,11 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
12801280
uint8 *addr = NULL;
12811281
uint32 offset = 0;
12821282

1283+
if (!memory_inst) {
1284+
aot_set_exception(module_inst, "uninitialized memory");
1285+
return 0;
1286+
}
1287+
12831288
if (memory_inst->heap_handle.ptr) {
12841289
addr = mem_allocator_malloc(memory_inst->heap_handle.ptr, size);
12851290
}
@@ -1313,6 +1318,10 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
13131318
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
13141319
AOTModule *module = (AOTModule *)module_inst->aot_module.ptr;
13151320

1321+
if (!memory_inst) {
1322+
return;
1323+
}
1324+
13161325
if (ptr) {
13171326
uint8 *addr = (uint8 *)memory_inst->memory_data.ptr + ptr;
13181327
if (memory_inst->heap_handle.ptr

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,11 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
15751575
uint8 *addr = NULL;
15761576
uint32 offset = 0;
15771577

1578+
if (!memory) {
1579+
wasm_set_exception(module_inst, "uninitialized memory");
1580+
return 0;
1581+
}
1582+
15781583
if (memory->heap_handle) {
15791584
addr = mem_allocator_malloc(memory->heap_handle, size);
15801585
}
@@ -1606,7 +1611,13 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
16061611
{
16071612
if (ptr) {
16081613
WASMMemoryInstance *memory = module_inst->default_memory;
1609-
uint8 *addr = memory->memory_data + ptr;
1614+
uint8* addr;
1615+
1616+
if (!memory) {
1617+
return;
1618+
}
1619+
1620+
addr = memory->memory_data + ptr;
16101621

16111622
if (memory->heap_handle
16121623
&& memory->heap_data <= addr

0 commit comments

Comments
 (0)