Skip to content

Commit a9d776e

Browse files
committed
cr suggestions and some refactor
1 parent 3296925 commit a9d776e

5 files changed

Lines changed: 66 additions & 101 deletions

File tree

core/iwasm/common/wasm_memory.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
185185

186186
if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
187187
LOG_WARNING("Invalid size of shared heap");
188-
goto fail1;
188+
goto fail2;
189189
}
190190

191191
if (init_args->pre_allocated_addr != NULL) {
@@ -194,7 +194,7 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
194194
if (size != init_args->size) {
195195
LOG_WARNING("Pre allocated size need to be aligned with system "
196196
"page size to create shared heap");
197-
goto fail1;
197+
goto fail2;
198198
}
199199

200200
heap->heap_handle = NULL;
@@ -252,12 +252,13 @@ WASMSharedHeap *
252252
wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
253253
{
254254
WASMSharedHeap *cur;
255-
bool heap_handle_exist = head->heap_handle != NULL;
255+
bool heap_handle_exist = false;
256256

257257
if (!head || !body) {
258258
LOG_WARNING("Invalid shared heap to chain.");
259259
return NULL;
260260
}
261+
heap_handle_exist = head->heap_handle != NULL;
261262

262263
os_mutex_lock(&shared_heap_list_lock);
263264
if (head->attached_count != 0 || body->attached_count != 0) {
@@ -543,16 +544,16 @@ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
543544
uint8 *addr, uint32 bytes,
544545
WASMSharedHeap **target_heap)
545546
{
546-
WASMSharedHeap *cur, *heap_head = get_shared_heap(module_inst);
547+
WASMSharedHeap *cur, *heap = get_shared_heap(module_inst);
547548
uintptr_t base_addr, addr_int, end_addr;
548549

549-
if (!heap_head) {
550+
if (!heap) {
550551
goto fail;
551552
}
552553

553554
/* Iterate through shared heap chain to find whether native addr in one of
554555
* shared heap */
555-
for (cur = heap_head; cur != NULL; cur = cur->chain_next) {
556+
for (cur = heap; cur != NULL; cur = cur->chain_next) {
556557
base_addr = (uintptr_t)cur->base_addr;
557558
addr_int = (uintptr_t)addr;
558559
if (addr_int < base_addr)

core/iwasm/common/wasm_shared_memory.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,54 @@ uint32
5656
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
5757
uint32 count);
5858

59+
#if WASM_ENABLE_MULTI_MEMORY != 0
60+
/* Only enable shared heap for the default memory */
61+
#define is_default_memory (memidx == 0)
62+
#else
63+
#define is_default_memory true
64+
#endif
65+
#if WASM_ENABLE_MEMORY64 != 0
66+
#define get_shared_heap_start_off(shared_heap) \
67+
(is_memory64 ? shared_heap->start_off_mem64 : shared_heap->start_off_mem32)
68+
#else
69+
#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
70+
#endif
71+
/* Check whether the app addr in the last visited shared heap, if not, check the
72+
* shared heap chain to find which(if any) shared heap the app addr in, and
73+
* update the last visited shared heap info if found. */
74+
#define app_addr_in_shared_heap(app_addr, bytes) \
75+
(shared_heap && is_default_memory && (app_addr) >= shared_heap_start_off \
76+
&& (app_addr) <= shared_heap_end_off - bytes + 1) \
77+
|| ({ \
78+
bool in_chain = false; \
79+
WASMSharedHeap *cur; \
80+
uint64 cur_shared_heap_start_off, cur_shared_heap_end_off; \
81+
for (cur = shared_heap; cur; cur = cur->chain_next) { \
82+
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
83+
cur_shared_heap_end_off = \
84+
cur_shared_heap_start_off - 1 + cur->size; \
85+
if ((app_addr) >= cur_shared_heap_start_off \
86+
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
87+
shared_heap_start_off = cur_shared_heap_start_off; \
88+
shared_heap_end_off = cur_shared_heap_end_off; \
89+
shared_heap_base_addr = cur->base_addr; \
90+
in_chain = true; \
91+
break; \
92+
} \
93+
} \
94+
in_chain; \
95+
})
96+
97+
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
98+
native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
99+
100+
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
101+
if (app_addr_in_shared_heap(app_addr, bytes)) \
102+
shared_heap_addr_app_to_native(app_addr, native_addr); \
103+
else
104+
#else
105+
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
106+
59107
#ifdef __cplusplus
60108
}
61109
#endif

core/iwasm/include/wasm_export.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,11 +2259,11 @@ WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
22592259
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
22602260

22612261
/**
2262-
* This function links two shared heaps, `head` and `body`, where `head` is a
2263-
* shared heap and `body` can be shared heap chain head, into a single chain.
2264-
* The `head` heap will be the head of the chain, and the `body` heap will be
2265-
* appended to it. At most one shared heap in shared heap chain can be
2266-
* dynamically allocated, the rest have to be the pre-allocated shared heap *
2262+
* This function links two shared heap(lists), `head` and `body` in to a single
2263+
* shared heap list, where `head` becomes the new shared heap list head. The
2264+
* shared heap list remains one continuous shared heap in wasm app's point of
2265+
* view. At most one shared heap in shared heap list can be dynamically
2266+
* allocated, the rest have to be the pre-allocated shared heap. *
22672267
*
22682268
* @param head The head of the shared heap chain.
22692269
* @param body The body of the shared heap chain to be appended.

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,56 +46,6 @@ typedef float64 CellType_F64;
4646
#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
4747
#endif
4848

49-
#if WASM_ENABLE_SHARED_HEAP != 0
50-
#if WASM_ENABLE_MULTI_MEMORY != 0
51-
/* Only enable shared heap for the default memory */
52-
#define is_default_memory (memidx == 0)
53-
#else
54-
#define is_default_memory true
55-
#endif
56-
#if WASM_ENABLE_MEMORY64 != 0
57-
#define get_shared_heap_start_off(shared_heap) \
58-
(is_memory64 ? shared_heap->start_off_mem64 : shared_heap->start_off_mem32)
59-
#else
60-
#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
61-
#endif
62-
/* Check whether the app addr in the last visited shared heap, if not, check the
63-
* shared heap chain to find which(if any) shared heap the app addr in, and
64-
* update the last visited shared heap info if found. */
65-
#define app_addr_in_shared_heap(app_addr, bytes) \
66-
(shared_heap && is_default_memory && (app_addr) >= shared_heap_start_off \
67-
&& (app_addr) <= shared_heap_end_off - bytes + 1) \
68-
|| ({ \
69-
bool in_chain = false; \
70-
WASMSharedHeap *cur; \
71-
uint64 cur_shared_heap_start_off, cur_shared_heap_end_off; \
72-
for (cur = shared_heap; cur; cur = cur->chain_next) { \
73-
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
74-
cur_shared_heap_end_off = \
75-
cur_shared_heap_start_off - 1 + cur->size; \
76-
if ((app_addr) >= cur_shared_heap_start_off \
77-
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
78-
shared_heap_start_off = cur_shared_heap_start_off; \
79-
shared_heap_end_off = cur_shared_heap_end_off; \
80-
shared_heap_base_addr = cur->base_addr; \
81-
in_chain = true; \
82-
break; \
83-
} \
84-
} \
85-
in_chain; \
86-
})
87-
88-
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
89-
native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
90-
91-
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
92-
if (app_addr_in_shared_heap(app_addr, bytes)) \
93-
shared_heap_addr_app_to_native(app_addr, native_addr); \
94-
else
95-
#else
96-
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
97-
#endif
98-
9949
#if WASM_ENABLE_MEMORY64 == 0
10050

10151
#if (!defined(OS_ENABLE_HW_BOUND_CHECK) \

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,46 +37,6 @@ typedef float64 CellType_F64;
3737
#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
3838
#endif
3939

40-
#if WASM_ENABLE_SHARED_HEAP != 0
41-
/* TODO: add code for 64 bit version when memory64 is enabled for fast-interp */
42-
#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
43-
/* Check whether the app addr in the last visited shared heap, if not, check the
44-
* shared heap chain to find which(if any) shared heap the app addr in, and
45-
* update the last visited shared heap info if found. */
46-
#define app_addr_in_shared_heap(app_addr, bytes) \
47-
(shared_heap && (app_addr) >= shared_heap_start_off \
48-
&& (app_addr) <= shared_heap_end_off - bytes + 1) \
49-
|| ({ \
50-
bool in_chain = false; \
51-
WASMSharedHeap *cur; \
52-
uint64 cur_shared_heap_start_off, cur_shared_heap_end_off; \
53-
for (cur = shared_heap; cur; cur = cur->chain_next) { \
54-
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
55-
cur_shared_heap_end_off = \
56-
cur_shared_heap_start_off - 1 + cur->size; \
57-
if ((app_addr) >= cur_shared_heap_start_off \
58-
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
59-
shared_heap_start_off = cur_shared_heap_start_off; \
60-
shared_heap_end_off = cur_shared_heap_end_off; \
61-
shared_heap_base_addr = cur->base_addr; \
62-
in_chain = true; \
63-
break; \
64-
} \
65-
} \
66-
in_chain; \
67-
})
68-
69-
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
70-
native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
71-
72-
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
73-
if (app_addr_in_shared_heap(app_addr, bytes)) \
74-
shared_heap_addr_app_to_native(app_addr, native_addr); \
75-
else
76-
#else
77-
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
78-
#endif
79-
8040
#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
8141
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
8242
#define CHECK_MEMORY_OVERFLOW(bytes) \
@@ -1562,6 +1522,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
15621522
bool is_return_call = false;
15631523
#endif
15641524
#if WASM_ENABLE_SHARED_HEAP != 0
1525+
/* TODO: currently flowing two variables are only dummy for shared heap
1526+
* boundary check, need to be updated when multi-memory or memory64
1527+
* proposals are to be implemented */
1528+
bool is_memory64 = false;
1529+
uint32 memidx = 0;
1530+
15651531
WASMSharedHeap *shared_heap = module->e ? module->e->shared_heap : NULL;
15661532
uint8 *shared_heap_base_addr = shared_heap ? shared_heap->base_addr : NULL;
15671533
uint64 shared_heap_start_off =

0 commit comments

Comments
 (0)