Skip to content

Commit e911813

Browse files
committed
format and update shared heap boundary check in runtime API
1 parent add6958 commit e911813

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

core/iwasm/common/wasm_memory.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -496,25 +496,38 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
496496
uint64 shared_heap_start, shared_heap_end;
497497

498498
if (!heap) {
499-
return false;
499+
goto fail;
500500
}
501501

502502
if (bytes == 0) {
503503
bytes = 1;
504504
}
505505

506-
for (cur = heap; cur; cur = cur->chain_next) {
507-
shared_heap_start =
508-
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
509-
shared_heap_end = shared_heap_start - 1 + cur->size;
510-
if (app_offset >= shared_heap_start
511-
&& app_offset <= shared_heap_end - bytes + 1) {
512-
if (target_heap)
506+
/* Early stop for app start address not in the shared heap(chain) at all */
507+
shared_heap_start =
508+
is_memory64 ? heap->start_off_mem64 : heap->start_off_mem32;
509+
shared_heap_end = is_memory64 ? UINT64_MAX : UINT32_MAX;
510+
if (app_offset < shared_heap_start
511+
|| app_offset > shared_heap_end - bytes + 1) {
512+
goto fail;
513+
}
514+
515+
/* Find the exact shared heap that app addr is in */
516+
if (target_heap) {
517+
for (cur = heap; cur; cur = cur->chain_next) {
518+
shared_heap_start =
519+
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
520+
shared_heap_end = shared_heap_start - 1 + cur->size;
521+
if (app_offset >= shared_heap_start
522+
&& app_offset <= shared_heap_end - bytes + 1) {
513523
*target_heap = cur;
514-
return true;
524+
return true;
525+
}
515526
}
516527
}
517528

529+
return true;
530+
fail:
518531
if (target_heap)
519532
*target_heap = NULL;
520533
return false;
@@ -529,7 +542,7 @@ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
529542
uintptr_t base_addr, addr_int, end_addr;
530543

531544
if (!heap_head) {
532-
return false;
545+
goto fail;
533546
}
534547

535548
/* Iterate through shared heap chain to find whether native addr in one of
@@ -553,6 +566,7 @@ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
553566
return true;
554567
}
555568

569+
fail:
556570
if (target_heap)
557571
*target_heap = NULL;
558572
return false;

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,10 +1719,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
17191719
goto got_exception;
17201720
}
17211721

1722-
HANDLE_OP(WASM_OP_NOP)
1723-
{
1724-
HANDLE_OP_END();
1725-
}
1722+
HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); }
17261723

17271724
#if WASM_ENABLE_EXCE_HANDLING != 0
17281725
HANDLE_OP(WASM_OP_RETHROW)
@@ -5659,10 +5656,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
56595656
HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
56605657
HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
56615658
HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
5662-
HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
5663-
{
5664-
HANDLE_OP_END();
5665-
}
5659+
HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); }
56665660

56675661
HANDLE_OP(WASM_OP_I32_EXTEND8_S)
56685662
{

0 commit comments

Comments
 (0)