From 7898af9f21c5f0623c1b555bbd952b158cc9acc4 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Tue, 11 Nov 2025 16:22:45 +0800 Subject: [PATCH] fix bug and add unit test case for runtime api when shared heap is enabled (#4695) --- core/iwasm/common/wasm_memory.c | 8 ++-- core/iwasm/common/wasm_memory.h | 2 +- core/iwasm/common/wasm_shared_memory.c | 6 ++- tests/unit/shared-heap/shared_heap_test.cc | 56 ++++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index 04e3b9976..10c651bc9 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -616,12 +616,12 @@ wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm) bool is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst, - bool is_memory64, uint64 app_offset, uint32 bytes) + bool is_memory64, uint64 app_offset, uint64 bytes) { WASMSharedHeap *heap = get_shared_heap(module_inst), *cur; uint64 shared_heap_start, shared_heap_end; - if (!heap) { + if (!heap || bytes > APP_HEAP_SIZE_MAX) { goto fail; } @@ -665,12 +665,12 @@ fail: static bool is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst, - bool is_memory64, uint8 *addr, uint32 bytes) + bool is_memory64, uint8 *addr, uint64 bytes) { WASMSharedHeap *cur, *heap = get_shared_heap(module_inst); uintptr_t base_addr, addr_int, end_addr; - if (!heap) { + if (!heap || bytes > APP_HEAP_SIZE_MAX) { goto fail; } diff --git a/core/iwasm/common/wasm_memory.h b/core/iwasm/common/wasm_memory.h index 48bd2179f..b79ed4a6c 100644 --- a/core/iwasm/common/wasm_memory.h +++ b/core/iwasm/common/wasm_memory.h @@ -84,7 +84,7 @@ SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size) #if WASM_ENABLE_SHARED_HEAP != 0 bool is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst, - bool is_memory64, uint64 app_offset, uint32 bytes); + bool is_memory64, uint64 app_offset, uint64 bytes); WASMSharedHeap * wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args); diff --git a/core/iwasm/common/wasm_shared_memory.c b/core/iwasm/common/wasm_shared_memory.c index 2027a5720..c1aa18f5c 100644 --- a/core/iwasm/common/wasm_shared_memory.c +++ b/core/iwasm/common/wasm_shared_memory.c @@ -249,10 +249,14 @@ map_try_release_wait_info(HashMap *wait_hash_map, AtomicWaitInfo *wait_info, #if WASM_ENABLE_SHARED_HEAP != 0 static bool is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst, - uint8 *addr, uint32 bytes) + uint8 *addr, uint64 bytes) { WASMSharedHeap *shared_heap = NULL; + if (bytes > APP_HEAP_SIZE_MAX) { + return false; + } + #if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { shared_heap = ((WASMModuleInstance *)module_inst)->e->shared_heap; diff --git a/tests/unit/shared-heap/shared_heap_test.cc b/tests/unit/shared-heap/shared_heap_test.cc index 47d2eb3e8..434b4c389 100644 --- a/tests/unit/shared-heap/shared_heap_test.cc +++ b/tests/unit/shared-heap/shared_heap_test.cc @@ -217,6 +217,62 @@ TEST_F(shared_heap_test, test_preallocated_shared_heap_malloc_fail) EXPECT_EQ(0, argv[0]); } +TEST_F(shared_heap_test, test_preallocated_shared_runtime_api) +{ + struct ret_env tmp_module_env; + SharedHeapInitArgs args = { 0 }; + WASMSharedHeap *shared_heap = nullptr; + uint32 argv[1] = { 0 }; + void *native_ptr; + uint64 offset, size; + bool ret; + + args.size = 0x4000; + shared_heap = wasm_runtime_create_shared_heap(&args); + + if (!shared_heap) { + FAIL() << "Failed to create shared heap"; + } + + if (!load_wasm("test.wasm", 0, tmp_module_env)) { + FAIL() << "Failed to load wasm file\n"; + } + + if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst, + shared_heap)) { + ADD_FAILURE() << "Failed to attach shared heap\n"; + goto fail1; + } + + offset = wasm_runtime_shared_heap_malloc(tmp_module_env.wasm_module_inst, + 32, &native_ptr); + if (!offset) { + ADD_FAILURE() << "Failed to attach shared heap\n"; + goto fail2; + } + + size = (uint64_t)UINT32_MAX + 0x2000; + printf("offset %lx size: %lx\n", offset, size); + ASSERT_EQ(false, wasm_runtime_validate_app_addr( + tmp_module_env.wasm_module_inst, offset, size)); + + ASSERT_EQ(NULL, wasm_runtime_addr_app_to_native( + tmp_module_env.wasm_module_inst, offset + size)); + + size = (uint64_t)10; + ASSERT_EQ(true, wasm_runtime_validate_app_addr( + tmp_module_env.wasm_module_inst, offset, size)); + + ASSERT_EQ(native_ptr + size, + wasm_runtime_addr_app_to_native(tmp_module_env.wasm_module_inst, + offset + size)); + +fail2: + wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst); +fail1: + destroy_module_env(tmp_module_env); +} + static void create_test_shared_heap(uint8 *preallocated_buf, size_t size, WASMSharedHeap **shared_heap_res)