@@ -25,7 +25,7 @@ static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
25
25
26
26
static mem_allocator_t pool_allocator = NULL ;
27
27
28
- static memory_grow_error_callback memory_grow_error_cb ;
28
+ static enlarge_memory_error_callback_t enlarge_memory_error_cb ;
29
29
30
30
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
31
31
static void * allocator_user_data = NULL ;
@@ -573,13 +573,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
573
573
{
574
574
WASMMemoryInstance * memory = wasm_get_default_memory (module );
575
575
uint8 * memory_data_old , * memory_data_new , * heap_data_old ;
576
- uint32 num_bytes_per_page , heap_size , total_size_old ;
576
+ uint32 num_bytes_per_page , heap_size , total_size_old = 0 ;
577
577
uint32 cur_page_count , max_page_count , total_page_count ;
578
578
uint64 total_size_new ;
579
579
bool ret = true;
580
+ enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR ;
580
581
581
- if (!memory )
582
- return false;
582
+ if (!memory ) {
583
+ ret = false;
584
+ goto return_func ;
585
+ }
583
586
584
587
heap_data_old = memory -> heap_data ;
585
588
heap_size = (uint32 )(memory -> heap_data_end - memory -> heap_data );
@@ -597,9 +600,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
597
600
/* No need to enlarge memory */
598
601
return true;
599
602
600
- if (total_page_count < cur_page_count /* integer overflow */
601
- || total_page_count > max_page_count ) {
602
- return false;
603
+ if (total_page_count < cur_page_count ) { /* integer overflow */
604
+ ret = false;
605
+ goto return_func ;
606
+ }
607
+
608
+ if (total_page_count > max_page_count ) {
609
+ failure_reason = MAX_SIZE_REACHED ;
610
+ ret = false;
611
+ goto return_func ;
603
612
}
604
613
605
614
bh_assert (total_size_new <= 4 * (uint64 )BH_GB );
@@ -625,14 +634,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
625
634
if (heap_size > 0 ) {
626
635
if (mem_allocator_is_heap_corrupted (memory -> heap_handle )) {
627
636
wasm_runtime_show_app_heap_corrupted_prompt ();
628
- return false;
637
+ ret = false;
638
+ goto return_func ;
629
639
}
630
640
}
631
641
632
642
if (!(memory_data_new =
633
643
wasm_runtime_realloc (memory_data_old , (uint32 )total_size_new ))) {
634
644
if (!(memory_data_new = wasm_runtime_malloc ((uint32 )total_size_new ))) {
635
- return false;
645
+ ret = false;
646
+ goto return_func ;
636
647
}
637
648
if (memory_data_old ) {
638
649
bh_memcpy_s (memory_data_new , (uint32 )total_size_new ,
@@ -688,19 +699,30 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
688
699
os_writegsbase (memory_data_new );
689
700
#endif
690
701
702
+ return_func :
703
+ if (!ret && enlarge_memory_error_cb )
704
+ enlarge_memory_error_cb (inc_page_count , total_size_old , 0 ,
705
+ failure_reason , (wasm_module_inst_t )module ,
706
+ wasm_runtime_get_exec_env_singleton (
707
+ (WASMModuleInstanceCommon * )module ));
708
+
691
709
return ret ;
692
710
}
693
711
#else
694
712
bool
695
713
wasm_enlarge_memory_internal (WASMModuleInstance * module , uint32 inc_page_count )
696
714
{
697
715
WASMMemoryInstance * memory = wasm_get_default_memory (module );
698
- uint32 num_bytes_per_page , total_size_old ;
716
+ uint32 num_bytes_per_page , total_size_old = 0 ;
699
717
uint32 cur_page_count , max_page_count , total_page_count ;
700
718
uint64 total_size_new ;
719
+ bool ret = true;
720
+ enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR ;
701
721
702
- if (!memory )
703
- return false;
722
+ if (!memory ) {
723
+ ret = false;
724
+ goto return_func ;
725
+ }
704
726
705
727
num_bytes_per_page = memory -> num_bytes_per_page ;
706
728
cur_page_count = memory -> cur_page_count ;
@@ -713,9 +735,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
713
735
/* No need to enlarge memory */
714
736
return true;
715
737
716
- if (total_page_count < cur_page_count /* integer overflow */
717
- || total_page_count > max_page_count ) {
718
- return false;
738
+ if (total_page_count < cur_page_count ) { /* integer overflow */
739
+ ret = false;
740
+ goto return_func ;
741
+ }
742
+
743
+ if (total_page_count > max_page_count ) {
744
+ failure_reason = MAX_SIZE_REACHED ;
745
+ ret = false;
746
+ goto return_func ;
719
747
}
720
748
721
749
bh_assert (total_size_new <= 4 * (uint64 )BH_GB );
@@ -730,7 +758,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
730
758
if (!os_mem_commit (memory -> memory_data_end ,
731
759
(uint32 )total_size_new - total_size_old ,
732
760
MMAP_PROT_READ | MMAP_PROT_WRITE )) {
733
- return false;
761
+ ret = false;
762
+ goto return_func ;
734
763
}
735
764
#endif
736
765
@@ -742,7 +771,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
742
771
os_mem_decommit (memory -> memory_data_end ,
743
772
(uint32 )total_size_new - total_size_old );
744
773
#endif
745
- return false;
774
+ ret = false;
775
+ goto return_func ;
746
776
}
747
777
748
778
/* The increased pages are filled with zero by the OS when os_mmap,
@@ -762,14 +792,22 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
762
792
memory -> mem_bound_check_16bytes .u64 = total_size_new - 16 ;
763
793
#endif
764
794
765
- return true;
795
+ return_func :
796
+ if (!ret && enlarge_memory_error_cb )
797
+ enlarge_memory_error_cb (inc_page_count , total_size_old , 0 ,
798
+ failure_reason , (wasm_module_inst_t )module ,
799
+ wasm_runtime_get_exec_env_singleton (
800
+ (WASMModuleInstanceCommon * )module ));
801
+
802
+ return ret ;
766
803
}
767
804
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
768
805
769
806
void
770
- set_memory_grow_error_callback (const memory_grow_error_callback callback )
807
+ set_enlarge_memory_error_callback (
808
+ const enlarge_memory_error_callback_t callback )
771
809
{
772
- memory_grow_error_cb = callback ;
810
+ enlarge_memory_error_cb = callback ;
773
811
}
774
812
775
813
bool
@@ -787,8 +825,5 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
787
825
shared_memory_unlock (module -> memories [0 ]);
788
826
#endif
789
827
790
- if (!ret && memory_grow_error_cb )
791
- memory_grow_error_cb (inc_page_count );
792
-
793
828
return ret ;
794
829
}
0 commit comments