Skip to content

Commit 9690725

Browse files
Address cr comments and some refactor (#4439)
1 parent ccdc836 commit 9690725

File tree

9 files changed

+55
-47
lines changed

9 files changed

+55
-47
lines changed

core/iwasm/aot/aot_reloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ typedef struct {
187187

188188
#if WASM_ENABLE_SHARED_HEAP != 0
189189
#define REG_SHARED_HEAP_SYM() \
190-
REG_SYM(wasm_runtime_update_last_used_shared_heap),
190+
REG_SYM(wasm_runtime_check_and_update_last_used_shared_heap),
191191
#else
192192
#define REG_SHARED_HEAP_SYM()
193193
#endif

core/iwasm/common/wasm_memory.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,16 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
178178
}
179179

180180
size = align_uint(size, os_getpagesize());
181-
heap->size = size;
182-
heap->start_off_mem64 = UINT64_MAX - heap->size + 1;
183-
heap->start_off_mem32 = UINT32_MAX - heap->size + 1;
184-
heap->attached_count = 0;
185-
186181
if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
187182
LOG_WARNING("Invalid size of shared heap");
188183
goto fail2;
189184
}
190185

186+
heap->size = size;
187+
heap->start_off_mem64 = UINT64_MAX - heap->size + 1;
188+
heap->start_off_mem32 = UINT32_MAX - heap->size + 1;
189+
heap->attached_count = 0;
190+
191191
if (init_args->pre_allocated_addr != NULL) {
192192
/* Create shared heap from a pre allocated buffer, its size need to
193193
* align with system page */
@@ -275,6 +275,13 @@ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
275275
os_mutex_unlock(&shared_heap_list_lock);
276276
return NULL;
277277
}
278+
if (cur == head && cur->chain_next) {
279+
LOG_WARNING(
280+
"To create shared heap chain, the 'head' shared heap can't "
281+
"already be the 'head' in another a chain");
282+
os_mutex_unlock(&shared_heap_list_lock);
283+
return NULL;
284+
}
278285
}
279286
for (cur = body; cur; cur = cur->chain_next) {
280287
if (cur->heap_handle && heap_handle_exist) {
@@ -519,6 +526,10 @@ wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
519526
void
520527
wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
521528
{
529+
/* Reset shared_heap_end_off = UINT64/32_MAX - 1 to handling a corner case,
530+
app_offset >= shared_heap_start && app_offset <= shared_heap_end-bytes+1
531+
when bytes=1 and both e->shared_heap_start_off and e->shared_heap_end_off
532+
is 0xffffffff */
522533
#if WASM_ENABLE_INTERP != 0
523534
if (module_inst->module_type == Wasm_Module_Bytecode) {
524535
WASMModuleInstanceExtra *e =
@@ -614,19 +625,17 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
614625
shared_heap_start =
615626
(uint64)get_last_used_shared_heap_start_offset(module_inst);
616627
shared_heap_end = (uint64)get_last_used_shared_heap_end_offset(module_inst);
617-
if (app_offset >= shared_heap_start
618-
&& app_offset <= shared_heap_end - bytes + 1
619-
&& bytes - 1 <= shared_heap_end) {
628+
if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
629+
&& app_offset <= shared_heap_end - bytes + 1) {
620630
return true;
621631
}
622632

623633
/* Early stop for app start address not in the shared heap(chain) at all */
624634
shared_heap_start =
625635
is_memory64 ? heap->start_off_mem64 : heap->start_off_mem32;
626636
shared_heap_end = is_memory64 ? UINT64_MAX : UINT32_MAX;
627-
if (app_offset < shared_heap_start
628-
|| app_offset > shared_heap_end - bytes + 1
629-
|| bytes - 1 > shared_heap_end) {
637+
if (bytes - 1 > shared_heap_end || app_offset < shared_heap_start
638+
|| app_offset > shared_heap_end - bytes + 1) {
630639
goto fail;
631640
}
632641

@@ -636,9 +645,8 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
636645
shared_heap_start =
637646
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
638647
shared_heap_end = shared_heap_start - 1 + cur->size;
639-
if (app_offset >= shared_heap_start
640-
&& app_offset <= shared_heap_end - bytes + 1
641-
&& bytes - 1 <= shared_heap_end) {
648+
if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
649+
&& app_offset <= shared_heap_end - bytes + 1) {
642650
update_last_used_shared_heap(module_inst, cur, is_memory64);
643651
return true;
644652
}
@@ -1075,7 +1083,7 @@ wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
10751083
shared_heap_base_addr_adj =
10761084
(char *)get_last_used_shared_heap_base_addr_adj(module_inst_comm);
10771085
str = shared_heap_base_addr_adj + app_str_offset;
1078-
str_end = shared_heap_base_addr_adj + shared_heap_end_off;
1086+
str_end = shared_heap_base_addr_adj + shared_heap_end_off + 1;
10791087
}
10801088
else
10811089
#endif
@@ -1358,7 +1366,8 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
13581366

13591367
/* The whole string must be in the shared heap */
13601368
str = (const char *)native_addr;
1361-
str_end = (const char *)shared_heap_base_addr_adj + shared_heap_end_off;
1369+
str_end =
1370+
(const char *)shared_heap_base_addr_adj + shared_heap_end_off + 1;
13621371
while (str < str_end && *str != '\0')
13631372
str++;
13641373
if (str == str_end) {

core/iwasm/common/wasm_runtime_common.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7886,12 +7886,10 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module)
78867886

78877887
#if WASM_ENABLE_SHARED_HEAP != 0
78887888
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)
7889+
wasm_runtime_check_and_update_last_used_shared_heap(
7890+
WASMModuleInstanceCommon *module_inst, uintptr_t app_offset, size_t bytes,
7891+
uintptr_t *shared_heap_start_off_p, uintptr_t *shared_heap_end_off_p,
7892+
uint8 **shared_heap_base_addr_adj_p, bool is_memory64)
78957893
{
78967894
WASMSharedHeap *heap = wasm_runtime_get_shared_heap(module_inst), *cur;
78977895
uint64 shared_heap_start, shared_heap_end;
@@ -7906,9 +7904,8 @@ wasm_runtime_update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
79067904
shared_heap_start =
79077905
is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
79087906
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) {
7907+
if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
7908+
&& app_offset <= shared_heap_end - bytes + 1) {
79127909
*shared_heap_start_off_p = (uintptr_t)shared_heap_start;
79137910
*shared_heap_end_off_p = (uintptr_t)shared_heap_end;
79147911
*shared_heap_base_addr_adj_p =

core/iwasm/common/wasm_runtime_common.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,12 +1338,10 @@ wasm_runtime_set_linux_perf(bool flag);
13381338

13391339
#if WASM_ENABLE_SHARED_HEAP != 0
13401340
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);
1341+
wasm_runtime_check_and_update_last_used_shared_heap(
1342+
WASMModuleInstanceCommon *module_inst, uintptr_t app_offset, size_t bytes,
1343+
uintptr_t *shared_heap_start_off_p, uintptr_t *shared_heap_end_off_p,
1344+
uint8 **shared_heap_base_addr_adj_p, bool is_memory64);
13471345
#endif
13481346

13491347
#ifdef __cplusplus

core/iwasm/compilation/aot_emit_memory.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ get_module_inst_extra_offset(AOTCompContext *comp_ctx);
162162
* 1. shared_heap_start_off 2. shared_heap_end_off 3. shared_heap_base_addr_adj
163163
*/
164164
bool
165-
aot_check_shared_heap_chain(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
166-
LLVMBasicBlockRef check_succ,
167-
LLVMValueRef start_offset, LLVMValueRef bytes,
168-
bool is_memory64)
165+
aot_check_shared_heap_chain_and_update(AOTCompContext *comp_ctx,
166+
AOTFuncContext *func_ctx,
167+
LLVMBasicBlockRef check_succ,
168+
LLVMValueRef start_offset,
169+
LLVMValueRef bytes, bool is_memory64)
169170
{
170171
LLVMValueRef param_values[7], ret_value, func, value, cmp;
171172
LLVMTypeRef param_types[7], ret_type, func_type, func_ptr_type;
@@ -179,7 +180,7 @@ aot_check_shared_heap_chain(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
179180
param_types[6] = INT8_TYPE;
180181
ret_type = INT8_TYPE;
181182

182-
GET_AOT_FUNCTION(wasm_runtime_update_last_used_shared_heap, 7);
183+
GET_AOT_FUNCTION(wasm_runtime_check_and_update_last_used_shared_heap, 7);
183184

184185
/* Call function */
185186
param_values[0] = func_ctx->aot_inst;
@@ -285,7 +286,7 @@ build_check_app_addr_in_shared_heap_chain(
285286
/* Use start_offset > func_ctx->shared_heap_head_start_off to test
286287
* start_off falls in shared heap chain memory region. The shared heap
287288
* chain oob will be detected in app_addr_in_shared_heap block or
288-
* aot_check_shared_heap_chain function
289+
* aot_check_shared_heap_chain_and_update function
289290
*/
290291
BUILD_ICMP(LLVMIntUGT, start_offset, func_ctx->shared_heap_head_start_off,
291292
is_in_shared_heap, "shared_heap_lb_cmp");
@@ -327,9 +328,9 @@ build_shared_heap_conditional_branching(
327328
BUILD_COND_BR(cmp, app_addr_in_cache_shared_heap,
328329
check_shared_heap_chain);
329330
SET_BUILD_POS(check_shared_heap_chain);
330-
if (!aot_check_shared_heap_chain(comp_ctx, func_ctx,
331-
app_addr_in_cache_shared_heap,
332-
start_offset, bytes, is_memory64))
331+
if (!aot_check_shared_heap_chain_and_update(
332+
comp_ctx, func_ctx, app_addr_in_cache_shared_heap, start_offset,
333+
bytes, is_memory64))
333334
goto fail;
334335
}
335336
return true;

core/iwasm/compilation/aot_llvm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,9 +1648,10 @@ create_shared_heap_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
16481648

16491649
/* if there is attached shared heap(s), the value will be valid start_off-1,
16501650
* otherwise it will be UINT32_MAX/UINT64_MAX, so during the bounds checks,
1651-
* when has attached shared heap: offset > start_off - 1 => offset >= offset
1652-
* when no attached shared heap: offset > UINT32_MAX/UINT64_MAX is always
1653-
* false
1651+
* when has attached shared heap:
1652+
* offset > start_off - 1 => offset >= start_off
1653+
* when no attached shared heap:
1654+
* offset > UINT32_MAX/UINT64_MAX is always false
16541655
* */
16551656
if (!(func_ctx->shared_heap_head_start_off = LLVMBuildSelect(
16561657
comp_ctx->builder, cmp, shared_heap_head_start_off_minus_one,

samples/shared-heap/src/shared_heap_chain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ produce_data(wasm_module_inst_t module_inst, wasm_exec_env_t exec_env,
2424
wasm_runtime_get_exception(module_inst));
2525
return false;
2626
}
27-
if (free_on_fail && argv[0] == 0) {
27+
if (argv[0] == 0) {
2828
printf("Failed to allocate memory from shared heap\n");
2929
return false;
3030
}

tests/unit/shared-heap/shared_heap_test.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static bool
3535
load_wasm(char *wasm_file_tested, unsigned int app_heap_size,
3636
ret_env &ret_module_env)
3737
{
38-
const char *wasm_file = strdup(wasm_file_tested);
38+
char *wasm_file = strdup(wasm_file_tested);
3939
unsigned int wasm_file_size = 0;
4040
unsigned int stack_size = 16 * 1024, heap_size = app_heap_size;
4141
char error_buf[128] = { 0 };
@@ -68,8 +68,10 @@ load_wasm(char *wasm_file_tested, unsigned int app_heap_size,
6868
goto fail;
6969
}
7070

71+
free(wasm_file);
7172
return true;
7273
fail:
74+
free(wasm_file);
7375
destroy_module_env(ret_module_env);
7476
return false;
7577
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
*/
55

6-
#define NULL 0
6+
#include <stdio.h>
77

88
extern void *
99
shared_heap_malloc(int size);

0 commit comments

Comments
 (0)