Skip to content

Commit 7898af9

Browse files
fix bug and add unit test case for runtime api when shared heap is enabled (#4695)
1 parent 4b42cfd commit 7898af9

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,12 @@ wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
616616

617617
bool
618618
is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
619-
bool is_memory64, uint64 app_offset, uint32 bytes)
619+
bool is_memory64, uint64 app_offset, uint64 bytes)
620620
{
621621
WASMSharedHeap *heap = get_shared_heap(module_inst), *cur;
622622
uint64 shared_heap_start, shared_heap_end;
623623

624-
if (!heap) {
624+
if (!heap || bytes > APP_HEAP_SIZE_MAX) {
625625
goto fail;
626626
}
627627

@@ -665,12 +665,12 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
665665

666666
static bool
667667
is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
668-
bool is_memory64, uint8 *addr, uint32 bytes)
668+
bool is_memory64, uint8 *addr, uint64 bytes)
669669
{
670670
WASMSharedHeap *cur, *heap = get_shared_heap(module_inst);
671671
uintptr_t base_addr, addr_int, end_addr;
672672

673-
if (!heap) {
673+
if (!heap || bytes > APP_HEAP_SIZE_MAX) {
674674
goto fail;
675675
}
676676

core/iwasm/common/wasm_memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size)
8484
#if WASM_ENABLE_SHARED_HEAP != 0
8585
bool
8686
is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
87-
bool is_memory64, uint64 app_offset, uint32 bytes);
87+
bool is_memory64, uint64 app_offset, uint64 bytes);
8888

8989
WASMSharedHeap *
9090
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);

core/iwasm/common/wasm_shared_memory.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,14 @@ map_try_release_wait_info(HashMap *wait_hash_map, AtomicWaitInfo *wait_info,
249249
#if WASM_ENABLE_SHARED_HEAP != 0
250250
static bool
251251
is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
252-
uint8 *addr, uint32 bytes)
252+
uint8 *addr, uint64 bytes)
253253
{
254254
WASMSharedHeap *shared_heap = NULL;
255255

256+
if (bytes > APP_HEAP_SIZE_MAX) {
257+
return false;
258+
}
259+
256260
#if WASM_ENABLE_INTERP != 0
257261
if (module_inst->module_type == Wasm_Module_Bytecode) {
258262
shared_heap = ((WASMModuleInstance *)module_inst)->e->shared_heap;

tests/unit/shared-heap/shared_heap_test.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,62 @@ TEST_F(shared_heap_test, test_preallocated_shared_heap_malloc_fail)
217217
EXPECT_EQ(0, argv[0]);
218218
}
219219

220+
TEST_F(shared_heap_test, test_preallocated_shared_runtime_api)
221+
{
222+
struct ret_env tmp_module_env;
223+
SharedHeapInitArgs args = { 0 };
224+
WASMSharedHeap *shared_heap = nullptr;
225+
uint32 argv[1] = { 0 };
226+
void *native_ptr;
227+
uint64 offset, size;
228+
bool ret;
229+
230+
args.size = 0x4000;
231+
shared_heap = wasm_runtime_create_shared_heap(&args);
232+
233+
if (!shared_heap) {
234+
FAIL() << "Failed to create shared heap";
235+
}
236+
237+
if (!load_wasm("test.wasm", 0, tmp_module_env)) {
238+
FAIL() << "Failed to load wasm file\n";
239+
}
240+
241+
if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
242+
shared_heap)) {
243+
ADD_FAILURE() << "Failed to attach shared heap\n";
244+
goto fail1;
245+
}
246+
247+
offset = wasm_runtime_shared_heap_malloc(tmp_module_env.wasm_module_inst,
248+
32, &native_ptr);
249+
if (!offset) {
250+
ADD_FAILURE() << "Failed to attach shared heap\n";
251+
goto fail2;
252+
}
253+
254+
size = (uint64_t)UINT32_MAX + 0x2000;
255+
printf("offset %lx size: %lx\n", offset, size);
256+
ASSERT_EQ(false, wasm_runtime_validate_app_addr(
257+
tmp_module_env.wasm_module_inst, offset, size));
258+
259+
ASSERT_EQ(NULL, wasm_runtime_addr_app_to_native(
260+
tmp_module_env.wasm_module_inst, offset + size));
261+
262+
size = (uint64_t)10;
263+
ASSERT_EQ(true, wasm_runtime_validate_app_addr(
264+
tmp_module_env.wasm_module_inst, offset, size));
265+
266+
ASSERT_EQ(native_ptr + size,
267+
wasm_runtime_addr_app_to_native(tmp_module_env.wasm_module_inst,
268+
offset + size));
269+
270+
fail2:
271+
wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst);
272+
fail1:
273+
destroy_module_env(tmp_module_env);
274+
}
275+
220276
static void
221277
create_test_shared_heap(uint8 *preallocated_buf, size_t size,
222278
WASMSharedHeap **shared_heap_res)

0 commit comments

Comments
 (0)