Skip to content

Commit 8ec6b66

Browse files
committed
feat: provide more info to enlarge memory error callback
1 parent 79b4dba commit 8ec6b66

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

core/iwasm/common/wasm_memory.c

+58-23
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
2525

2626
static mem_allocator_t pool_allocator = NULL;
2727

28-
static memory_grow_error_callback memory_grow_error_cb;
28+
static enlarge_memory_error_callback_t enlarge_memory_error_cb;
2929

3030
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
3131
static void *allocator_user_data = NULL;
@@ -573,13 +573,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
573573
{
574574
WASMMemoryInstance *memory = wasm_get_default_memory(module);
575575
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;
577577
uint32 cur_page_count, max_page_count, total_page_count;
578578
uint64 total_size_new;
579579
bool ret = true;
580+
enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
580581

581-
if (!memory)
582-
return false;
582+
if (!memory) {
583+
ret = false;
584+
goto return_func;
585+
}
583586

584587
heap_data_old = memory->heap_data;
585588
heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
@@ -597,9 +600,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
597600
/* No need to enlarge memory */
598601
return true;
599602

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;
603612
}
604613

605614
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
@@ -625,14 +634,16 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
625634
if (heap_size > 0) {
626635
if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
627636
wasm_runtime_show_app_heap_corrupted_prompt();
628-
return false;
637+
ret = false;
638+
goto return_func;
629639
}
630640
}
631641

632642
if (!(memory_data_new =
633643
wasm_runtime_realloc(memory_data_old, (uint32)total_size_new))) {
634644
if (!(memory_data_new = wasm_runtime_malloc((uint32)total_size_new))) {
635-
return false;
645+
ret = false;
646+
goto return_func;
636647
}
637648
if (memory_data_old) {
638649
bh_memcpy_s(memory_data_new, (uint32)total_size_new,
@@ -688,19 +699,30 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
688699
os_writegsbase(memory_data_new);
689700
#endif
690701

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+
691709
return ret;
692710
}
693711
#else
694712
bool
695713
wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
696714
{
697715
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;
699717
uint32 cur_page_count, max_page_count, total_page_count;
700718
uint64 total_size_new;
719+
bool ret = true;
720+
enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
701721

702-
if (!memory)
703-
return false;
722+
if (!memory) {
723+
ret = false;
724+
goto return_func;
725+
}
704726

705727
num_bytes_per_page = memory->num_bytes_per_page;
706728
cur_page_count = memory->cur_page_count;
@@ -713,9 +735,15 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
713735
/* No need to enlarge memory */
714736
return true;
715737

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;
719747
}
720748

721749
bh_assert(total_size_new <= 4 * (uint64)BH_GB);
@@ -730,7 +758,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
730758
if (!os_mem_commit(memory->memory_data_end,
731759
(uint32)total_size_new - total_size_old,
732760
MMAP_PROT_READ | MMAP_PROT_WRITE)) {
733-
return false;
761+
ret = false;
762+
goto return_func;
734763
}
735764
#endif
736765

@@ -742,7 +771,8 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
742771
os_mem_decommit(memory->memory_data_end,
743772
(uint32)total_size_new - total_size_old);
744773
#endif
745-
return false;
774+
ret = false;
775+
goto return_func;
746776
}
747777

748778
/* 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)
762792
memory->mem_bound_check_16bytes.u64 = total_size_new - 16;
763793
#endif
764794

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;
766803
}
767804
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
768805

769806
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)
771809
{
772-
memory_grow_error_cb = callback;
810+
enlarge_memory_error_cb = callback;
773811
}
774812

775813
bool
@@ -787,8 +825,5 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
787825
shared_memory_unlock(module->memories[0]);
788826
#endif
789827

790-
if (!ret && memory_grow_error_cb)
791-
memory_grow_error_cb(inc_page_count);
792-
793828
return ret;
794829
}

core/iwasm/common/wasm_memory.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ unsigned
2525
wasm_runtime_memory_pool_size();
2626

2727
void
28-
set_memory_grow_error_callback(const memory_grow_error_callback callback);
28+
set_enlarge_memory_error_callback(
29+
const enlarge_memory_error_callback_t callback);
2930

3031
#ifdef __cplusplus
3132
}

core/iwasm/include/wasm_export.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -1439,13 +1439,22 @@ WASM_RUNTIME_API_EXTERN bool
14391439
wasm_runtime_is_import_global_linked(const char *module_name,
14401440
const char *global_name);
14411441

1442-
typedef void (*memory_grow_error_callback)(uint32_t inc_page_count);
1442+
typedef enum {
1443+
INTERNAL_ERROR,
1444+
MAX_SIZE_REACHED,
1445+
} enlarge_memory_error_reason_t;
1446+
1447+
typedef void (*enlarge_memory_error_callback_t)(
1448+
uint32_t inc_page_count, uint64 current_memory_size, uint32_t memory_index,
1449+
enlarge_memory_error_reason_t failure_reason, wasm_module_inst_t instance,
1450+
wasm_exec_env_t exec_env);
14431451

14441452
/**
14451453
* Setup callback invoked when memory.grow fails
14461454
*/
14471455
WASM_RUNTIME_API_EXTERN void
1448-
set_memory_grow_error_callback(const memory_grow_error_callback callback);
1456+
set_enlarge_memory_error_callback(
1457+
const enlarge_memory_error_callback_t callback);
14491458

14501459
/* clang-format on */
14511460

0 commit comments

Comments
 (0)