Skip to content

Commit add6958

Browse files
committed
fix shared heap enhancement bugs and format code
1 parent aebb391 commit add6958

5 files changed

Lines changed: 61 additions & 52 deletions

File tree

core/iwasm/common/wasm_memory.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
183183
heap->start_off_mem32 = UINT32_MAX - heap->size + 1;
184184
heap->attached_count = 0;
185185

186+
if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
187+
LOG_WARNING("Invalid size of shared heap");
188+
goto fail1;
189+
}
190+
186191
if (init_args->pre_allocated_addr != NULL) {
187192
/* Create shared heap from a pre allocated buffer, its size need to
188193
* align with system page */
@@ -193,18 +198,14 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
193198
}
194199

195200
heap->heap_handle = NULL;
201+
heap->base_addr = init_args->pre_allocated_addr;
196202
}
197203
else {
198204
if (!(heap->heap_handle =
199205
runtime_malloc(mem_allocator_get_heap_struct_size()))) {
200206
goto fail2;
201207
}
202208

203-
if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
204-
LOG_WARNING("Invalid size of shared heap");
205-
goto fail3;
206-
}
207-
208209
#ifndef OS_ENABLE_HW_BOUND_CHECK
209210
map_size = size;
210211
#else
@@ -286,8 +287,8 @@ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
286287
heap_handle_exist = true;
287288
}
288289

289-
head->start_off_mem64 = body->start_off_mem64 - head->size + 1;
290-
head->start_off_mem32 = body->start_off_mem32 - head->size + 1;
290+
head->start_off_mem64 = body->start_off_mem64 - head->size;
291+
head->start_off_mem32 = body->start_off_mem32 - head->size;
291292
head->chain_next = body;
292293
os_mutex_unlock(&shared_heap_list_lock);
293294
return head;
@@ -505,8 +506,9 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
505506
for (cur = heap; cur; cur = cur->chain_next) {
506507
shared_heap_start =
507508
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
508-
shared_heap_end = shared_heap_start + cur->size;
509-
if (app_offset >= shared_heap_start && app_offset <= shared_heap_end) {
509+
shared_heap_end = shared_heap_start - 1 + cur->size;
510+
if (app_offset >= shared_heap_start
511+
&& app_offset <= shared_heap_end - bytes + 1) {
510512
if (target_heap)
511513
*target_heap = cur;
512514
return true;
@@ -535,19 +537,16 @@ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
535537
for (cur = heap_head; cur != NULL; cur = cur->chain_next) {
536538
base_addr = (uintptr_t)cur->base_addr;
537539
addr_int = (uintptr_t)addr;
538-
if (addr_int < base_addr) {
540+
if (addr_int < base_addr)
539541
continue;
540-
}
541542

542543
end_addr = addr_int + bytes;
543544
/* Check for overflow */
544-
if (end_addr <= addr_int) {
545+
if (end_addr <= addr_int)
545546
continue;
546-
}
547547

548-
if (end_addr > base_addr + cur->size) {
548+
if (end_addr > base_addr + cur->size)
549549
continue;
550-
}
551550

552551
if (target_heap)
553552
*target_heap = cur;
@@ -1041,9 +1040,11 @@ wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
10411040
{
10421041
WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
10431042
WASMMemoryInstance *memory_inst;
1044-
WASMSharedHeap *shared_heap;
10451043
uint8 *addr;
10461044
bool bounds_checks;
1045+
#if WASM_ENABLE_SHARED_HEAP != 0
1046+
WASMSharedHeap *shared_heap;
1047+
#endif
10471048

10481049
bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
10491050
|| module_inst_comm->module_type == Wasm_Module_AoT);

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef float64 CellType_F64;
7272
for (cur = shared_heap; cur; cur = cur->chain_next) { \
7373
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
7474
cur_shared_heap_end_off = \
75-
cur_shared_heap_start_off + cur->size - 1; \
75+
cur_shared_heap_start_off - 1 + cur->size; \
7676
if ((app_addr) >= cur_shared_heap_start_off \
7777
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
7878
shared_heap_start_off = cur_shared_heap_start_off; \
@@ -1679,7 +1679,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
16791679
shared_heap ? get_shared_heap_start_off(shared_heap) : 0;
16801680
uint64 shared_heap_end_off =
16811681
shared_heap
1682-
? (get_shared_heap_start_off(shared_heap) + shared_heap->size - 1)
1682+
? (get_shared_heap_start_off(shared_heap) - 1 + shared_heap->size)
16831683
: 0;
16841684
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
16851685
#if WASM_ENABLE_MULTI_MEMORY != 0
@@ -1719,7 +1719,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
17191719
goto got_exception;
17201720
}
17211721

1722-
HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); }
1722+
HANDLE_OP(WASM_OP_NOP)
1723+
{
1724+
HANDLE_OP_END();
1725+
}
17231726

17241727
#if WASM_ENABLE_EXCE_HANDLING != 0
17251728
HANDLE_OP(WASM_OP_RETHROW)
@@ -5656,7 +5659,10 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
56565659
HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
56575660
HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
56585661
HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
5659-
HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); }
5662+
HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
5663+
{
5664+
HANDLE_OP_END();
5665+
}
56605666

56615667
HANDLE_OP(WASM_OP_I32_EXTEND8_S)
56625668
{

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ typedef float64 CellType_F64;
5353
for (cur = shared_heap; cur; cur = cur->chain_next) { \
5454
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
5555
cur_shared_heap_end_off = \
56-
cur_shared_heap_start_off + cur->size - 1; \
56+
cur_shared_heap_start_off - 1 + cur->size; \
5757
if ((app_addr) >= cur_shared_heap_start_off \
5858
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
5959
shared_heap_start_off = cur_shared_heap_start_off; \
@@ -67,7 +67,7 @@ typedef float64 CellType_F64;
6767
})
6868

6969
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
70-
native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
70+
native_addr = shared_heap_base_addr + ((app_addr) - shared_heap_start_off)
7171

7272
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
7373
if (app_addr_in_shared_heap(app_addr, bytes)) \
@@ -1568,7 +1568,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
15681568
shared_heap ? get_shared_heap_start_off(shared_heap) : 0;
15691569
uint64 shared_heap_end_off =
15701570
shared_heap
1571-
? (get_shared_heap_start_off(shared_heap) + shared_heap->size - 1)
1571+
? (get_shared_heap_start_off(shared_heap) - 1 + shared_heap->size)
15721572
: 0;
15731573
/* #endif */
15741574
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */

samples/shared-heap/src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ thread1_callback(void *arg)
5555
i + 1);
5656

5757
printf("wasm app1 send buf: %s\n\n", buf);
58-
if (!bh_post_msg(queue, 1, buf, 1024 * i)) {
58+
if (!bh_post_msg(queue, 1, buf, 1024 * (i + 1))) {
5959
printf("Failed to post message to queue\n");
6060
wasm_runtime_shared_heap_free(module_inst, offset);
6161
break;
@@ -84,7 +84,7 @@ thread1_callback(void *arg)
8484
buf = wasm_runtime_addr_app_to_native(module_inst, argv[0]);
8585

8686
printf("wasm app1 send buf: %s\n\n", buf);
87-
if (!bh_post_msg(queue, 1, buf, 1024 * i)) {
87+
if (!bh_post_msg(queue, 1, buf, 1024 * (i+1))) {
8888
printf("Failed to post message to queue\n");
8989
wasm_runtime_shared_heap_free(module_inst, argv[0]);
9090
break;
@@ -268,7 +268,7 @@ main(int argc, char **argv)
268268
}
269269

270270
/* create thread 1 */
271-
struct thread_arg targ1 = { 0 };
271+
thread_arg targ1 = { 0 };
272272
korp_tid tid1;
273273
targ1.queue = queue;
274274
targ1.module_inst = module_inst1;
@@ -279,7 +279,7 @@ main(int argc, char **argv)
279279
}
280280

281281
/* create thread 2 */
282-
struct thread_arg targ2 = { 0 };
282+
thread_arg targ2 = { 0 };
283283
korp_tid tid2;
284284
targ2.queue = queue;
285285
targ2.module_inst = module_inst2;

tests/unit/shared-heap/shared_heap_test.cc

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ destroy_module_env(struct ret_env module_env)
9292
}
9393
}
9494

95-
static void test_shared_heap(WASMSharedHeap *shared_heap, const char *file, const char *func_name, uint32 argc, uint32 argv[])
95+
static void
96+
test_shared_heap(WASMSharedHeap *shared_heap, const char *file,
97+
const char *func_name, uint32 argc, uint32 argv[])
9698
{
9799
struct ret_env tmp_module_env;
98100
WASMFunctionInstanceCommon *func_test = nullptr;
@@ -101,7 +103,8 @@ static void test_shared_heap(WASMSharedHeap *shared_heap, const char *file, cons
101103

102104
tmp_module_env = load_wasm((char *)file, 0);
103105

104-
if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst, shared_heap)) {
106+
if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst,
107+
shared_heap)) {
105108
printf("Failed to attach shared heap\n");
106109
goto test_failed;
107110
}
@@ -116,7 +119,8 @@ static void test_shared_heap(WASMSharedHeap *shared_heap, const char *file, cons
116119
wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, argc, argv);
117120
if (!ret) {
118121
printf("\nFailed to wasm_runtime_call_wasm!\n");
119-
const char *s = wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
122+
const char *s =
123+
wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
120124
printf("exception: %s\n", s);
121125
goto test_failed;
122126
}
@@ -131,7 +135,7 @@ static void test_shared_heap(WASMSharedHeap *shared_heap, const char *file, cons
131135

132136
TEST_F(shared_heap_test, test_shared_heap_basic)
133137
{
134-
SharedHeapInitArgs args;
138+
SharedHeapInitArgs args = { 0 };
135139
WASMSharedHeap *shared_heap = nullptr;
136140
uint32 argv[1] = { 0 };
137141

@@ -150,12 +154,11 @@ TEST_F(shared_heap_test, test_shared_heap_basic)
150154
// test aot
151155
test_shared_heap(shared_heap, "test.aot", "test", 1, argv);
152156
EXPECT_EQ(10, argv[0]);
153-
154157
}
155158

156159
TEST_F(shared_heap_test, test_shared_heap_malloc_fail)
157160
{
158-
SharedHeapInitArgs args;
161+
SharedHeapInitArgs args = { 0 };
159162
WASMSharedHeap *shared_heap = nullptr;
160163
uint32 argv[1] = { 0 };
161164

@@ -177,36 +180,36 @@ TEST_F(shared_heap_test, test_shared_heap_malloc_fail)
177180
}
178181

179182
#ifndef native_function
183+
/* clang-format off */
180184
#define native_function(func_name, signature) \
181185
{ #func_name, (void *)glue_##func_name, signature, NULL }
182-
186+
/* clang-format on */
183187
#endif
184188
#ifndef nitems
185189
#define nitems(_a) (sizeof(_a) / sizeof(0 [(_a)]))
186190
#endif /* nitems */
187-
uintptr_t glue_test_addr_conv(wasm_exec_env_t env, uintptr_t addr)
191+
uintptr_t
192+
glue_test_addr_conv(wasm_exec_env_t env, uintptr_t addr)
188193
{
189-
wasm_module_inst_t module_inst = get_module_inst(env);
190-
uintptr_t ret;
191-
void *native_addr = (void *)addr;
192-
uintptr_t app_addr = addr_native_to_app(native_addr);
193-
194-
native_addr = addr_app_to_native(app_addr);
195-
if (native_addr != (void *)addr)
196-
{
197-
EXPECT_EQ(1, 0);
198-
}
199-
return app_addr;
194+
wasm_module_inst_t module_inst = get_module_inst(env);
195+
uintptr_t ret;
196+
void *native_addr = (void *)addr;
197+
uintptr_t app_addr = addr_native_to_app(native_addr);
198+
199+
native_addr = addr_app_to_native(app_addr);
200+
if (native_addr != (void *)addr) {
201+
EXPECT_EQ(1, 0);
202+
}
203+
return app_addr;
200204
}
201205

202-
static NativeSymbol g_test_native_symbols[] =
203-
{
204-
native_function(test_addr_conv,"(*)i"),
206+
static NativeSymbol g_test_native_symbols[] = {
207+
native_function(test_addr_conv, "(*)i"),
205208
};
206209

207210
TEST_F(shared_heap_test, test_addr_conv)
208211
{
209-
SharedHeapInitArgs args;
212+
SharedHeapInitArgs args = { 0 };
210213
WASMSharedHeap *shared_heap = nullptr;
211214
uint32 argv[1] = { 0 };
212215
struct ret_env tmp_module_env;
@@ -217,8 +220,7 @@ TEST_F(shared_heap_test, test_addr_conv)
217220

218221
ret = wasm_native_register_natives("env", g_test_native_symbols,
219222
nitems(g_test_native_symbols));
220-
if (!ret)
221-
{
223+
if (!ret) {
222224
EXPECT_EQ(1, 0);
223225
return;
224226
}

0 commit comments

Comments
 (0)