Skip to content

Commit 035f758

Browse files
committed
code refactor
1 parent 948d981 commit 035f758

File tree

5 files changed

+64
-119
lines changed

5 files changed

+64
-119
lines changed

core/iwasm/common/wasm_memory.c

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
596596
return get_shared_heap(module_inst_comm);
597597
}
598598

599-
static bool
599+
bool
600600
is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
601601
bool is_memory64, uint64 app_offset, uint32 bytes)
602602
{
@@ -641,7 +641,6 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
641641
}
642642
}
643643

644-
return true;
645644
fail:
646645
return false;
647646
}
@@ -1220,11 +1219,6 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
12201219

12211220
bounds_checks = is_bounds_checks_enabled(module_inst_comm);
12221221

1223-
#if WASM_ENABLE_SHARED_HEAP != 0
1224-
/* If shared heap is enabled, bounds check is always needed */
1225-
bounds_checks = true;
1226-
#endif
1227-
12281222
memory_inst = wasm_get_default_memory(module_inst);
12291223
if (!memory_inst) {
12301224
return 0;
@@ -1350,33 +1344,14 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
13501344
if (is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module_inst,
13511345
memory_inst->is_memory64, app_buf_addr,
13521346
app_buf_size)) {
1347+
const char *str, *str_end;
13531348
shared_heap_base_addr_adj = get_last_used_shared_heap_base_addr_adj(
13541349
(WASMModuleInstanceCommon *)module_inst);
13551350
shared_heap_end_off = get_last_used_shared_heap_end_offset(
13561351
(WASMModuleInstanceCommon *)module_inst);
13571352
native_addr = shared_heap_base_addr_adj + app_buf_addr;
1358-
}
1359-
else
1360-
#endif
1361-
{
1362-
native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
1363-
}
1364-
1365-
bounds_checks =
1366-
is_bounds_checks_enabled((WASMModuleInstanceCommon *)module_inst);
1367-
1368-
if (!bounds_checks) {
1369-
if (app_buf_addr == 0) {
1370-
native_addr = NULL;
1371-
}
1372-
goto success;
1373-
}
13741353

1375-
#if WASM_ENABLE_SHARED_HEAP != 0
1376-
if (shared_heap_base_addr_adj) {
1377-
const char *str, *str_end;
1378-
1379-
/* The whole string must be in the linear memory */
1354+
/* The whole string must be in the shared heap */
13801355
str = (const char *)native_addr;
13811356
str_end = (const char *)shared_heap_base_addr_adj + shared_heap_end_off;
13821357
while (str < str_end && *str != '\0')
@@ -1390,6 +1365,17 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
13901365
}
13911366
#endif
13921367

1368+
native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
1369+
bounds_checks =
1370+
is_bounds_checks_enabled((WASMModuleInstanceCommon *)module_inst);
1371+
1372+
if (!bounds_checks) {
1373+
if (app_buf_addr == 0) {
1374+
native_addr = NULL;
1375+
}
1376+
goto success;
1377+
}
1378+
13931379
/* No need to check the app_offset and buf_size if memory access
13941380
boundary check with hardware trap is enabled */
13951381
#ifndef OS_ENABLE_HW_BOUND_CHECK

core/iwasm/common/wasm_memory.h

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,51 @@ SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size)
4141
#define SET_LINEAR_MEMORY_SIZE(memory, size) memory->memory_data_size = size
4242
#endif
4343

44+
#if WASM_ENABLE_INTERP != 0
4445
#if WASM_ENABLE_SHARED_HEAP != 0
46+
47+
#if WASM_ENABLE_MULTI_MEMORY != 0
48+
/* Only enable shared heap for the default memory */
49+
#define is_default_memory (memidx == 0)
50+
#else
51+
#define is_default_memory true
52+
#endif
53+
54+
#if UINTPTR_MAX == UINT64_MAX
55+
#define get_shared_heap_end_off() module->e->shared_heap_end_off.u64
56+
#else
57+
#define get_shared_heap_end_off() \
58+
(uint64)(module->e->shared_heap_end_off.u32[0])
59+
#endif
60+
61+
#if WASM_ENABLE_MEMORY64 != 0
62+
#define shared_heap_is_memory64 is_memory64
63+
#else
64+
#define shared_heap_is_memory64 false
65+
#endif
66+
67+
#define app_addr_in_shared_heap(app_addr, bytes) \
68+
(is_default_memory \
69+
&& is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module, \
70+
shared_heap_is_memory64, (uint64)app_addr, \
71+
bytes))
72+
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
73+
native_addr = module->e->shared_heap_base_addr_adj + app_addr
74+
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
75+
if (app_addr_in_shared_heap(app_addr, bytes)) \
76+
shared_heap_addr_app_to_native(app_addr, native_addr); \
77+
else
78+
79+
#else /* else of WASM_ENABLE_SHARED_HEAP != 0 */
80+
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
81+
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
82+
#endif /* end of WASM_ENABLE_INTERP != 0 */
83+
84+
#if WASM_ENABLE_SHARED_HEAP != 0
85+
bool
86+
is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
87+
bool is_memory64, uint64 app_offset, uint32 bytes);
88+
4589
WASMSharedHeap *
4690
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
4791

@@ -74,7 +118,7 @@ wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
74118
void
75119
wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst,
76120
uint64 ptr);
77-
#endif
121+
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
78122

79123
bool
80124
wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,9 +1622,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
16221622
if (memory)
16231623
is_memory64 = memory->is_memory64;
16241624
#endif
1625-
#if WASM_ENABLE_SHARED_HEAP != 0
1626-
WASMModuleInstanceExtra *e = module->e;
1627-
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
16281625
#if WASM_ENABLE_MULTI_MEMORY != 0
16291626
uint32 memidx = 0;
16301627
uint32 memidx_cached = (uint32)-1;
@@ -2399,7 +2396,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
23992396
else
24002397
cur_func_type = cur_func->u.func->func_type;
24012398

2402-
/* clang-format off */
2399+
/* clang-format off */
24032400
#if WASM_ENABLE_GC == 0
24042401
if (cur_type != cur_func_type) {
24052402
wasm_set_exception(module, "indirect call type mismatch");
@@ -5773,15 +5770,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
57735770
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
57745771
#if WASM_ENABLE_SHARED_HEAP != 0
57755772
if (app_addr_in_shared_heap((uint64)dst, len))
5776-
dlen =
5777-
get_last_used_shared_heap_end_off() - dst + 1;
5773+
dlen = get_shared_heap_end_off() - dst + 1;
57785774
#endif
57795775
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
57805776
#if WASM_ENABLE_SHARED_HEAP != 0
57815777
if (app_addr_in_shared_heap((uint64)dst, len)) {
57825778
shared_heap_addr_app_to_native((uint64)dst, mdst);
5783-
dlen =
5784-
get_last_used_shared_heap_end_off() - dst + 1;
5779+
dlen = get_shared_heap_end_off() - dst + 1;
57855780
}
57865781
else
57875782
#endif

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
15251525
/* TODO: currently flowing two variables are only dummy for shared heap
15261526
* boundary check, need to be updated when multi-memory or memory64
15271527
* proposals are to be implemented */
1528-
WASMModuleInstanceExtra *e = module->e;
15291528
bool is_memory64 = false;
15301529
uint32 memidx = 0;
15311530
(void)is_memory64;
@@ -5079,8 +5078,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
50795078
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
50805079
#if WASM_ENABLE_SHARED_HEAP != 0
50815080
if (app_addr_in_shared_heap((uint64)dst, len))
5082-
dlen = (uint64)get_last_used_shared_heap_end_off()
5083-
- dst + 1;
5081+
dlen = (uint64)get_shared_heap_end_off() - dst + 1;
50845082
#endif
50855083
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
50865084
#if WASM_ENABLE_SHARED_HEAP != 0
@@ -5097,8 +5095,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
50975095
#if WASM_ENABLE_SHARED_HEAP != 0
50985096
if (app_addr_in_shared_heap((uint64)dst, len)) {
50995097
shared_heap_addr_app_to_native((uint64)dst, mdst);
5100-
dlen = (uint64)get_last_used_shared_heap_end_off()
5101-
- dst + 1;
5098+
dlen = (uint64)get_shared_heap_end_off() - dst + 1;
51025099
}
51035100
else
51045101
#endif

core/iwasm/interpreter/wasm_loader.h

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -73,83 +73,6 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
7373
uint8 block_type, uint8 **p_else_addr,
7474
uint8 **p_end_addr);
7575

76-
#if WASM_ENABLE_SHARED_HEAP != 0
77-
78-
#if WASM_ENABLE_MULTI_MEMORY != 0
79-
/* Only enable shared heap for the default memory */
80-
#define is_default_memory (memidx == 0)
81-
#else
82-
#define is_default_memory true
83-
#endif
84-
85-
#if WASM_ENABLE_MEMORY64 != 0
86-
#define get_shared_heap_start_off(shared_heap) \
87-
(is_memory64 ? shared_heap->start_off_mem64 : shared_heap->start_off_mem32)
88-
#else
89-
#define get_shared_heap_start_off(shared_heap) shared_heap->start_off_mem32
90-
#endif
91-
92-
#if UINTPTR_MAX == UINT64_MAX
93-
#define update_last_used_shared_heap(shared_heap) \
94-
do { \
95-
e->shared_heap_start_off.u64 = get_shared_heap_start_off(shared_heap); \
96-
e->shared_heap_end_off.u64 = \
97-
e->shared_heap_start_off.u64 - 1 + shared_heap->size; \
98-
e->shared_heap_base_addr_adj = \
99-
shared_heap->base_addr - e->shared_heap_start_off.u64; \
100-
} while (0)
101-
#define get_last_used_shared_heap_start_off() e->shared_heap_start_off.u64
102-
#define get_last_used_shared_heap_end_off() e->shared_heap_end_off.u64
103-
#else
104-
#define update_last_used_shared_heap(shared_heap) \
105-
do { \
106-
e->shared_heap_start_off.u32[0] = \
107-
(uint32)shared_heap->start_off_mem32; \
108-
e->shared_heap_end_off.u32[0] = \
109-
e->shared_heap_start_off.u32[0] - 1 + shared_heap->size; \
110-
e->shared_heap_base_addr_adj = \
111-
shared_heap->base_addr - e->shared_heap_start_off.u32[0]; \
112-
} while (0)
113-
#define get_last_used_shared_heap_start_off() \
114-
(uint64) e->shared_heap_start_off.u32[0]
115-
#define get_last_used_shared_heap_end_off() \
116-
(uint64) e->shared_heap_end_off.u32[0]
117-
#endif /* end of UINTPTR_MAX == UINT64_MAX */
118-
119-
/* Check whether the app addr in the last visited shared heap, if not, check the
120-
* shared heap chain to find which(if any) shared heap the app addr in, and
121-
* update the last visited shared heap info if found. */
122-
#define app_addr_in_shared_heap(app_addr, bytes) \
123-
(e->shared_heap && is_default_memory \
124-
&& (app_addr) >= get_last_used_shared_heap_start_off() \
125-
&& (app_addr) <= get_last_used_shared_heap_end_off() - bytes + 1) \
126-
|| ({ \
127-
bool in_chain = false; \
128-
WASMSharedHeap *cur; \
129-
uint64 cur_shared_heap_start_off, cur_shared_heap_end_off; \
130-
for (cur = e->shared_heap; cur; cur = cur->chain_next) { \
131-
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
132-
cur_shared_heap_end_off = \
133-
cur_shared_heap_start_off - 1 + cur->size; \
134-
if ((app_addr) >= cur_shared_heap_start_off \
135-
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
136-
update_last_used_shared_heap(cur); \
137-
in_chain = true; \
138-
break; \
139-
} \
140-
} \
141-
in_chain; \
142-
})
143-
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
144-
native_addr = e->shared_heap_base_addr_adj + app_addr
145-
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
146-
if (app_addr_in_shared_heap(app_addr, bytes)) \
147-
shared_heap_addr_app_to_native(app_addr, native_addr); \
148-
else
149-
#else
150-
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
151-
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
152-
15376
#ifdef __cplusplus
15477
}
15578
#endif

0 commit comments

Comments
 (0)