Skip to content

Commit ccdc836

Browse files
Shared heap enhancement for AOT and update tests and samples (#4226)
* shared heap enhancement: modify memory check for aot_check_memory_overflow to accomodate shared heap chain * shared heap enhancement in AOT * use alloca for func ctx shared heap cache value * use correct alloca for func ctx shared heap cache value * enable shared heap chain aot test and bug fix * Fix a missing argument on 32bits platform, still has the shared heap chain iteration problem * Fix shared heap chain iteration problem on 32bits platform * fix AOT bulk memory bounds checks compliation issue * fix AOT bulk memory bounds checks on 64 bits platform * refactor aot memory check * refactor AOT bulk memory bounds checks * add more unit test for shared heap * finished organizing unit test for shared heap and enable x86_32 for shared heap unit test * cover a corner case for bulk memory overflow check * try func call to replace shared heap chain traverse * fix compilation error in JIT and potentially load nullptr * add option for wamrc to enable single shared heap/multi shared heap, and update shared heap unit tests and sample * cr suggestions: 1. check potiential underflow 2. refactor and use separate function for bulk memory and normal memroy 3. static assert 4. add more comments 5. remove unused code
1 parent 1394b8a commit ccdc836

26 files changed

+1356
-398
lines changed

core/iwasm/aot/aot_reloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ typedef struct {
185185
#define REG_STRINGREF_SYM()
186186
#endif
187187

188+
#if WASM_ENABLE_SHARED_HEAP != 0
189+
#define REG_SHARED_HEAP_SYM() \
190+
REG_SYM(wasm_runtime_update_last_used_shared_heap),
191+
#else
192+
#define REG_SHARED_HEAP_SYM()
193+
#endif
194+
188195
#define REG_COMMON_SYMBOLS \
189196
REG_SYM(aot_set_exception_with_id), \
190197
REG_SYM(aot_invoke_native), \
@@ -218,6 +225,7 @@ typedef struct {
218225
REG_LLVM_PGO_SYM() \
219226
REG_GC_SYM() \
220227
REG_STRINGREF_SYM() \
228+
REG_SHARED_HEAP_SYM() \
221229

222230
#define CHECK_RELOC_OFFSET(data_size) do { \
223231
if (!check_reloc_offset(target_section_size, \

core/iwasm/aot/aot_runtime.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_start_off) == 16);
6363
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap_end_off) == 24);
6464
bh_static_assert(offsetof(AOTModuleInstanceExtra, shared_heap) == 32);
6565

66+
bh_static_assert(offsetof(WASMSharedHeap, next) == 0);
67+
bh_static_assert(offsetof(WASMSharedHeap, chain_next) == 8);
68+
bh_static_assert(offsetof(WASMSharedHeap, heap_handle) == 16);
69+
bh_static_assert(offsetof(WASMSharedHeap, base_addr) == 24);
70+
bh_static_assert(offsetof(WASMSharedHeap, size) == 32);
71+
bh_static_assert(offsetof(WASMSharedHeap, start_off_mem64) == 40);
72+
bh_static_assert(offsetof(WASMSharedHeap, start_off_mem32) == 48);
73+
6674
bh_static_assert(sizeof(CApiFuncImport) == sizeof(uintptr_t) * 3);
6775

6876
bh_static_assert(sizeof(wasm_val_t) == 16);
@@ -1991,6 +1999,8 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
19911999
#else
19922000
extra->shared_heap_start_off.u32[0] = UINT32_MAX;
19932001
#endif
2002+
/* After shared heap chain, will early stop if shared heap is NULL */
2003+
extra->shared_heap = NULL;
19942004

19952005
#if WASM_ENABLE_PERF_PROFILING != 0
19962006
total_size = sizeof(AOTFuncPerfProfInfo)

core/iwasm/common/wasm_memory.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
615615
(uint64)get_last_used_shared_heap_start_offset(module_inst);
616616
shared_heap_end = (uint64)get_last_used_shared_heap_end_offset(module_inst);
617617
if (app_offset >= shared_heap_start
618-
&& app_offset <= shared_heap_end - bytes + 1) {
618+
&& app_offset <= shared_heap_end - bytes + 1
619+
&& bytes - 1 <= shared_heap_end) {
619620
return true;
620621
}
621622

@@ -624,7 +625,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
624625
is_memory64 ? heap->start_off_mem64 : heap->start_off_mem32;
625626
shared_heap_end = is_memory64 ? UINT64_MAX : UINT32_MAX;
626627
if (app_offset < shared_heap_start
627-
|| app_offset > shared_heap_end - bytes + 1) {
628+
|| app_offset > shared_heap_end - bytes + 1
629+
|| bytes - 1 > shared_heap_end) {
628630
goto fail;
629631
}
630632

@@ -635,7 +637,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
635637
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
636638
shared_heap_end = shared_heap_start - 1 + cur->size;
637639
if (app_offset >= shared_heap_start
638-
&& app_offset <= shared_heap_end - bytes + 1) {
640+
&& app_offset <= shared_heap_end - bytes + 1
641+
&& bytes - 1 <= shared_heap_end) {
639642
update_last_used_shared_heap(module_inst, cur, is_memory64);
640643
return true;
641644
}
@@ -1227,7 +1230,9 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
12271230
#if WASM_ENABLE_SHARED_HEAP != 0
12281231
if (is_native_addr_in_shared_heap(module_inst_comm,
12291232
memory_inst->is_memory64, addr, 1)) {
1230-
return addr - get_last_used_shared_heap_base_addr_adj(module_inst_comm);
1233+
return (uint64)(uintptr_t)(addr
1234+
- get_last_used_shared_heap_base_addr_adj(
1235+
module_inst_comm));
12311236
}
12321237
#endif
12331238

@@ -1349,7 +1354,7 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
13491354
(WASMModuleInstanceCommon *)module_inst);
13501355
shared_heap_end_off = get_last_used_shared_heap_end_offset(
13511356
(WASMModuleInstanceCommon *)module_inst);
1352-
native_addr = shared_heap_base_addr_adj + app_buf_addr;
1357+
native_addr = shared_heap_base_addr_adj + (uintptr_t)app_buf_addr;
13531358

13541359
/* The whole string must be in the shared heap */
13551360
str = (const char *)native_addr;

core/iwasm/common/wasm_runtime_common.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7883,3 +7883,40 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)
78837883

78847884
return true;
78857885
}
7886+
7887+
#if WASM_ENABLE_SHARED_HEAP != 0
7888+
bool
7889+
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
7890+
uintptr_t app_offset, size_t bytes,
7891+
uintptr_t *shared_heap_start_off_p,
7892+
uintptr_t *shared_heap_end_off_p,
7893+
uint8 **shared_heap_base_addr_adj_p,
7894+
bool is_memory64)
7895+
{
7896+
WASMSharedHeap *heap = wasm_runtime_get_shared_heap(module_inst), *cur;
7897+
uint64 shared_heap_start, shared_heap_end;
7898+
7899+
if (bytes == 0) {
7900+
bytes = 1;
7901+
}
7902+
7903+
/* Find the exact shared heap that app addr is in, and update last used
7904+
* shared heap info in func context */
7905+
for (cur = heap; cur; cur = cur->chain_next) {
7906+
shared_heap_start =
7907+
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
7908+
shared_heap_end = shared_heap_start - 1 + cur->size;
7909+
if (app_offset >= shared_heap_start
7910+
&& app_offset <= shared_heap_end - bytes + 1
7911+
&& bytes - 1 <= shared_heap_end) {
7912+
*shared_heap_start_off_p = (uintptr_t)shared_heap_start;
7913+
*shared_heap_end_off_p = (uintptr_t)shared_heap_end;
7914+
*shared_heap_base_addr_adj_p =
7915+
cur->base_addr - (uintptr_t)shared_heap_start;
7916+
return true;
7917+
}
7918+
}
7919+
7920+
return false;
7921+
}
7922+
#endif

core/iwasm/common/wasm_runtime_common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,16 @@ void
13361336
wasm_runtime_set_linux_perf(bool flag);
13371337
#endif
13381338

1339+
#if WASM_ENABLE_SHARED_HEAP != 0
1340+
bool
1341+
wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
1342+
uintptr_t app_offset, size_t bytes,
1343+
uintptr_t *shared_heap_start_off_p,
1344+
uintptr_t *shared_heap_end_off_p,
1345+
uint8 **shared_heap_base_addr_adj_p,
1346+
bool is_memory64);
1347+
#endif
1348+
13391349
#ifdef __cplusplus
13401350
}
13411351
#endif

0 commit comments

Comments
 (0)