Skip to content

Commit 0c9fe61

Browse files
authored
Improve shared_heap test cases. (#4834)
Aligned allocation size can be significantly greater than the original size, and page size varies across platforms. > macOS on M1 (Apple Silicon) uses a memory page size of 16,384 bytes (16 KB). > This differs from the traditional 4 KB page size used on Intel Macs and many > other ARM64 systems, and is designed to improve performance by reducing page > table overhead and allowing for better cache utilization.
1 parent 6db91d7 commit 0c9fe61

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
241241
}
242242

243243
size = align_uint(size, os_getpagesize());
244+
if (size != init_args->size) {
245+
LOG_WARNING("Shared heap size aligned from %u to %u", init_args->size,
246+
size);
247+
}
248+
244249
if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
245250
LOG_WARNING("Invalid size of shared heap");
246251
goto fail2;

tests/unit/shared-heap/shared_heap_test.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "bh_read_file.h"
1010
#include "wasm_runtime_common.h"
11+
#include "bh_platform.h"
1112

1213
#include <gtest/gtest-spi.h>
1314

@@ -145,7 +146,7 @@ TEST_F(shared_heap_test, test_shared_heap_basic)
145146
WASMSharedHeap *shared_heap = nullptr;
146147
uint32 argv[1] = {};
147148

148-
args.size = 1024;
149+
args.size = os_getpagesize();
149150
shared_heap = wasm_runtime_create_shared_heap(&args);
150151

151152
if (!shared_heap) {
@@ -168,20 +169,23 @@ TEST_F(shared_heap_test, test_shared_heap_malloc_fail)
168169
WASMSharedHeap *shared_heap = nullptr;
169170
uint32 argv[1] = {};
170171

171-
args.size = 1024;
172+
args.size = os_getpagesize();
172173
shared_heap = wasm_runtime_create_shared_heap(&args);
173174

174175
if (!shared_heap) {
175176
FAIL() << "Failed to create shared heap";
176177
}
177178

178-
test_shared_heap(shared_heap, "test.wasm", "test_malloc_fail", 0, argv);
179+
argv[0] = os_getpagesize();
180+
test_shared_heap(shared_heap, "test.wasm", "test_malloc_fail", 1, argv);
179181
EXPECT_EQ(1, argv[0]);
180182

181-
test_shared_heap(shared_heap, "test.aot", "test_malloc_fail", 0, argv);
183+
argv[0] = os_getpagesize();
184+
test_shared_heap(shared_heap, "test.aot", "test_malloc_fail", 1, argv);
182185
EXPECT_EQ(1, argv[0]);
183186

184-
test_shared_heap(shared_heap, "test_chain.aot", "test_malloc_fail", 0,
187+
argv[0] = os_getpagesize();
188+
test_shared_heap(shared_heap, "test_chain.aot", "test_malloc_fail", 1,
185189
argv);
186190
EXPECT_EQ(1, argv[0]);
187191
}

tests/unit/shared-heap/wasm-apps/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ test()
2222
}
2323

2424
int
25-
test_malloc_fail()
25+
test_malloc_fail(int size)
2626
{
27-
int *ptr = (int *)shared_heap_malloc(8192);
27+
int *ptr = (int *)shared_heap_malloc(size + 1);
2828

2929
if (ptr == NULL) {
3030
return 1;

0 commit comments

Comments
 (0)