Skip to content

Commit 79b4dba

Browse files
committed
feat: add callback to handle memory.grow failures
1 parent ff151fb commit 79b4dba

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

core/iwasm/common/wasm_memory.c

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../aot/aot_runtime.h"
99
#include "bh_platform.h"
1010
#include "mem_alloc.h"
11+
#include "wasm_memory.h"
1112

1213
#if WASM_ENABLE_SHARED_MEMORY != 0
1314
#include "../common/wasm_shared_memory.h"
@@ -24,6 +25,8 @@ static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
2425

2526
static mem_allocator_t pool_allocator = NULL;
2627

28+
static memory_grow_error_callback memory_grow_error_cb;
29+
2730
#if WASM_MEM_ALLOC_WITH_USER_DATA != 0
2831
static void *allocator_user_data = NULL;
2932
static void *(*malloc_func)(void *user_data, unsigned int size) = NULL;
@@ -763,6 +766,12 @@ wasm_enlarge_memory_internal(WASMModuleInstance *module, uint32 inc_page_count)
763766
}
764767
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
765768

769+
void
770+
set_memory_grow_error_callback(const memory_grow_error_callback callback)
771+
{
772+
memory_grow_error_cb = callback;
773+
}
774+
766775
bool
767776
wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
768777
{
@@ -778,5 +787,8 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
778787
shared_memory_unlock(module->memories[0]);
779788
#endif
780789

790+
if (!ret && memory_grow_error_cb)
791+
memory_grow_error_cb(inc_page_count);
792+
781793
return ret;
782794
}

core/iwasm/common/wasm_memory.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ wasm_runtime_memory_destroy();
2424
unsigned
2525
wasm_runtime_memory_pool_size();
2626

27+
void
28+
set_memory_grow_error_callback(const memory_grow_error_callback callback);
29+
2730
#ifdef __cplusplus
2831
}
2932
#endif

core/iwasm/include/wasm_export.h

+8
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,14 @@ 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);
1443+
1444+
/**
1445+
* Setup callback invoked when memory.grow fails
1446+
*/
1447+
WASM_RUNTIME_API_EXTERN void
1448+
set_memory_grow_error_callback(const memory_grow_error_callback callback);
1449+
14421450
/* clang-format on */
14431451

14441452
#ifdef __cplusplus

0 commit comments

Comments
 (0)