8
8
#include "../aot/aot_runtime.h"
9
9
#include "bh_platform.h"
10
10
#include "mem_alloc.h"
11
+ #include "wasm_memory.h"
11
12
12
13
#if WASM_ENABLE_SHARED_MEMORY != 0
13
14
#include "../common/wasm_shared_memory.h"
@@ -24,6 +25,8 @@ static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
24
25
25
26
static mem_allocator_t pool_allocator = NULL ;
26
27
28
+ static enlarge_memory_error_callback_t enlarge_memory_error_cb ;
29
+
27
30
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
28
31
static void * allocator_user_data = NULL ;
29
32
static void * (* malloc_func )(void * user_data , unsigned int size ) = NULL ;
@@ -570,13 +573,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
570
573
{
571
574
WASMMemoryInstance * memory = wasm_get_default_memory (module );
572
575
uint8 * memory_data_old , * memory_data_new , * heap_data_old ;
573
- uint32 num_bytes_per_page , heap_size , total_size_old ;
576
+ uint32 num_bytes_per_page , heap_size , total_size_old = 0 ;
574
577
uint32 cur_page_count , max_page_count , total_page_count ;
575
578
uint64 total_size_new ;
576
579
bool ret = true;
580
+ enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR ;
577
581
578
- if (!memory )
579
- return false;
582
+ if (!memory ) {
583
+ ret = false;
584
+ goto return_func ;
585
+ }
580
586
581
587
heap_data_old = memory -> heap_data ;
582
588
heap_size = (uint32 )(memory -> heap_data_end - memory -> heap_data );
@@ -594,9 +600,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
594
600
/* No need to enlarge memory */
595
601
return true;
596
602
597
- if (total_page_count < cur_page_count /* integer overflow */
598
- || total_page_count > max_page_count ) {
599
- 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 ;
600
612
}
601
613
602
614
bh_assert (total_size_new <= 4 * (uint64 )BH_GB );
@@ -622,14 +634,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
622
634
if (heap_size > 0 ) {
623
635
if (mem_allocator_is_heap_corrupted (memory -> heap_handle )) {
624
636
wasm_runtime_show_app_heap_corrupted_prompt ();
625
- return false;
637
+ ret = false;
638
+ goto return_func ;
626
639
}
627
640
}
628
641
629
642
if (!(memory_data_new =
630
643
wasm_runtime_realloc (memory_data_old , (uint32 )total_size_new ))) {
631
644
if (!(memory_data_new = wasm_runtime_malloc ((uint32 )total_size_new ))) {
632
- return false;
645
+ ret = false;
646
+ goto return_func ;
633
647
}
634
648
if (memory_data_old ) {
635
649
bh_memcpy_s (memory_data_new , (uint32 )total_size_new ,
@@ -685,19 +699,43 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
685
699
os_writegsbase (memory_data_new );
686
700
#endif
687
701
702
+ return_func :
703
+ if (!ret && enlarge_memory_error_cb ) {
704
+ WASMExecEnv * exec_env = NULL ;
705
+
706
+ #if WASM_ENABLE_INTERP != 0
707
+ if (module -> module_type == Wasm_Module_Bytecode )
708
+ exec_env =
709
+ ((WASMModuleInstanceExtra * )module -> e )-> common .cur_exec_env ;
710
+ #endif
711
+ #if WASM_ENABLE_AOT != 0
712
+ if (module -> module_type == Wasm_Module_AoT )
713
+ exec_env =
714
+ ((AOTModuleInstanceExtra * )module -> e )-> common .cur_exec_env ;
715
+ #endif
716
+
717
+ enlarge_memory_error_cb (inc_page_count , total_size_old , 0 ,
718
+ failure_reason ,
719
+ (WASMModuleInstanceCommon * )module , exec_env );
720
+ }
721
+
688
722
return ret ;
689
723
}
690
724
#else
691
725
bool
692
726
wasm_enlarge_memory_internal (WASMModuleInstance * module , uint32 inc_page_count )
693
727
{
694
728
WASMMemoryInstance * memory = wasm_get_default_memory (module );
695
- uint32 num_bytes_per_page , total_size_old ;
729
+ uint32 num_bytes_per_page , total_size_old = 0 ;
696
730
uint32 cur_page_count , max_page_count , total_page_count ;
697
731
uint64 total_size_new ;
732
+ bool ret = true;
733
+ enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR ;
698
734
699
- if (!memory )
700
- return false;
735
+ if (!memory ) {
736
+ ret = false;
737
+ goto return_func ;
738
+ }
701
739
702
740
num_bytes_per_page = memory -> num_bytes_per_page ;
703
741
cur_page_count = memory -> cur_page_count ;
@@ -710,9 +748,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
710
748
/* No need to enlarge memory */
711
749
return true;
712
750
713
- if (total_page_count < cur_page_count /* integer overflow */
714
- || total_page_count > max_page_count ) {
715
- return false;
751
+ if (total_page_count < cur_page_count ) { /* integer overflow */
752
+ ret = false;
753
+ goto return_func ;
754
+ }
755
+
756
+ if (total_page_count > max_page_count ) {
757
+ failure_reason = MAX_SIZE_REACHED ;
758
+ ret = false;
759
+ goto return_func ;
716
760
}
717
761
718
762
bh_assert (total_size_new <= 4 * (uint64 )BH_GB );
@@ -727,7 +771,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
727
771
if (!os_mem_commit (memory -> memory_data_end ,
728
772
(uint32 )total_size_new - total_size_old ,
729
773
MMAP_PROT_READ | MMAP_PROT_WRITE )) {
730
- return false;
774
+ ret = false;
775
+ goto return_func ;
731
776
}
732
777
#endif
733
778
@@ -739,7 +784,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
739
784
os_mem_decommit (memory -> memory_data_end ,
740
785
(uint32 )total_size_new - total_size_old );
741
786
#endif
742
- return false;
787
+ ret = false;
788
+ goto return_func ;
743
789
}
744
790
745
791
/* The increased pages are filled with zero by the OS when os_mmap,
@@ -759,10 +805,37 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
759
805
memory -> mem_bound_check_16bytes .u64 = total_size_new - 16 ;
760
806
#endif
761
807
762
- return true;
808
+ return_func :
809
+ if (!ret && enlarge_memory_error_cb ) {
810
+ WASMExecEnv * exec_env = NULL ;
811
+
812
+ #if WASM_ENABLE_INTERP != 0
813
+ if (module -> module_type == Wasm_Module_Bytecode )
814
+ exec_env =
815
+ ((WASMModuleInstanceExtra * )module -> e )-> common .cur_exec_env ;
816
+ #endif
817
+ #if WASM_ENABLE_AOT != 0
818
+ if (module -> module_type == Wasm_Module_AoT )
819
+ exec_env =
820
+ ((AOTModuleInstanceExtra * )module -> e )-> common .cur_exec_env ;
821
+ #endif
822
+
823
+ enlarge_memory_error_cb (inc_page_count , total_size_old , 0 ,
824
+ failure_reason ,
825
+ (WASMModuleInstanceCommon * )module , exec_env );
826
+ }
827
+
828
+ return ret ;
763
829
}
764
830
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
765
831
832
+ void
833
+ wasm_runtime_set_enlarge_mem_error_callback (
834
+ const enlarge_memory_error_callback_t callback )
835
+ {
836
+ enlarge_memory_error_cb = callback ;
837
+ }
838
+
766
839
bool
767
840
wasm_enlarge_memory (WASMModuleInstance * module , uint32 inc_page_count )
768
841
{
0 commit comments