@@ -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;
0 commit comments