Skip to content

Commit

Permalink
cr suggestions and some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TianlongLiang committed Feb 12, 2025
1 parent 3296925 commit a9d776e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 101 deletions.
13 changes: 7 additions & 6 deletions core/iwasm/common/wasm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)

if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
LOG_WARNING("Invalid size of shared heap");
goto fail1;
goto fail2;
}

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

heap->heap_handle = NULL;
Expand Down Expand Up @@ -252,12 +252,13 @@ WASMSharedHeap *
wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
{
WASMSharedHeap *cur;
bool heap_handle_exist = head->heap_handle != NULL;
bool heap_handle_exist = false;

if (!head || !body) {
LOG_WARNING("Invalid shared heap to chain.");
return NULL;
}
heap_handle_exist = head->heap_handle != NULL;

os_mutex_lock(&shared_heap_list_lock);
if (head->attached_count != 0 || body->attached_count != 0) {
Expand Down Expand Up @@ -543,16 +544,16 @@ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
uint8 *addr, uint32 bytes,
WASMSharedHeap **target_heap)
{
WASMSharedHeap *cur, *heap_head = get_shared_heap(module_inst);
WASMSharedHeap *cur, *heap = get_shared_heap(module_inst);
uintptr_t base_addr, addr_int, end_addr;

if (!heap_head) {
if (!heap) {
goto fail;
}

/* Iterate through shared heap chain to find whether native addr in one of
* shared heap */
for (cur = heap_head; cur != NULL; cur = cur->chain_next) {
for (cur = heap; cur != NULL; cur = cur->chain_next) {
base_addr = (uintptr_t)cur->base_addr;
addr_int = (uintptr_t)addr;
if (addr_int < base_addr)
Expand Down
48 changes: 48 additions & 0 deletions core/iwasm/common/wasm_shared_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,54 @@ uint32
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
uint32 count);

#if WASM_ENABLE_MULTI_MEMORY != 0
/* Only enable shared heap for the default memory */
#define is_default_memory (memidx == 0)
#else
#define is_default_memory true
#endif
#if WASM_ENABLE_MEMORY64 != 0
#define get_shared_heap_start_off(shared_heap) \
(is_memory64 ? shared_heap->start_off_mem64 : shared_heap->start_off_mem32)
#else
#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
#endif
/* Check whether the app addr in the last visited shared heap, if not, check the
* shared heap chain to find which(if any) shared heap the app addr in, and
* update the last visited shared heap info if found. */
#define app_addr_in_shared_heap(app_addr, bytes) \
(shared_heap && is_default_memory && (app_addr) >= shared_heap_start_off \
&& (app_addr) <= shared_heap_end_off - bytes + 1) \
|| ({ \
bool in_chain = false; \
WASMSharedHeap *cur; \
uint64 cur_shared_heap_start_off, cur_shared_heap_end_off; \
for (cur = shared_heap; cur; cur = cur->chain_next) { \
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
cur_shared_heap_end_off = \
cur_shared_heap_start_off - 1 + cur->size; \
if ((app_addr) >= cur_shared_heap_start_off \
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
shared_heap_start_off = cur_shared_heap_start_off; \
shared_heap_end_off = cur_shared_heap_end_off; \
shared_heap_base_addr = cur->base_addr; \
in_chain = true; \
break; \
} \
} \
in_chain; \
})

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

#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
if (app_addr_in_shared_heap(app_addr, bytes)) \
shared_heap_addr_app_to_native(app_addr, native_addr); \
else
#else
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 5 additions & 5 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -2259,11 +2259,11 @@ WASM_RUNTIME_API_EXTERN wasm_shared_heap_t
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);

/**
* This function links two shared heaps, `head` and `body`, where `head` is a
* shared heap and `body` can be shared heap chain head, into a single chain.
* The `head` heap will be the head of the chain, and the `body` heap will be
* appended to it. At most one shared heap in shared heap chain can be
* dynamically allocated, the rest have to be the pre-allocated shared heap *
* This function links two shared heap(lists), `head` and `body` in to a single
* shared heap list, where `head` becomes the new shared heap list head. The
* shared heap list remains one continuous shared heap in wasm app's point of
* view. At most one shared heap in shared heap list can be dynamically
* allocated, the rest have to be the pre-allocated shared heap. *
*
* @param head The head of the shared heap chain.
* @param body The body of the shared heap chain to be appended.
Expand Down
50 changes: 0 additions & 50 deletions core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,56 +46,6 @@ typedef float64 CellType_F64;
#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
#endif

#if WASM_ENABLE_SHARED_HEAP != 0
#if WASM_ENABLE_MULTI_MEMORY != 0
/* Only enable shared heap for the default memory */
#define is_default_memory (memidx == 0)
#else
#define is_default_memory true
#endif
#if WASM_ENABLE_MEMORY64 != 0
#define get_shared_heap_start_off(shared_heap) \
(is_memory64 ? shared_heap->start_off_mem64 : shared_heap->start_off_mem32)
#else
#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
#endif
/* Check whether the app addr in the last visited shared heap, if not, check the
* shared heap chain to find which(if any) shared heap the app addr in, and
* update the last visited shared heap info if found. */
#define app_addr_in_shared_heap(app_addr, bytes) \
(shared_heap && is_default_memory && (app_addr) >= shared_heap_start_off \
&& (app_addr) <= shared_heap_end_off - bytes + 1) \
|| ({ \
bool in_chain = false; \
WASMSharedHeap *cur; \
uint64 cur_shared_heap_start_off, cur_shared_heap_end_off; \
for (cur = shared_heap; cur; cur = cur->chain_next) { \
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
cur_shared_heap_end_off = \
cur_shared_heap_start_off - 1 + cur->size; \
if ((app_addr) >= cur_shared_heap_start_off \
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
shared_heap_start_off = cur_shared_heap_start_off; \
shared_heap_end_off = cur_shared_heap_end_off; \
shared_heap_base_addr = cur->base_addr; \
in_chain = true; \
break; \
} \
} \
in_chain; \
})

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

#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
if (app_addr_in_shared_heap(app_addr, bytes)) \
shared_heap_addr_app_to_native(app_addr, native_addr); \
else
#else
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
#endif

#if WASM_ENABLE_MEMORY64 == 0

#if (!defined(OS_ENABLE_HW_BOUND_CHECK) \
Expand Down
46 changes: 6 additions & 40 deletions core/iwasm/interpreter/wasm_interp_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,6 @@ typedef float64 CellType_F64;
#define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
#endif

#if WASM_ENABLE_SHARED_HEAP != 0
/* TODO: add code for 64 bit version when memory64 is enabled for fast-interp */
#define get_shared_heap_start_off(shared_heap) (shared_heap->start_off_mem32)
/* Check whether the app addr in the last visited shared heap, if not, check the
* shared heap chain to find which(if any) shared heap the app addr in, and
* update the last visited shared heap info if found. */
#define app_addr_in_shared_heap(app_addr, bytes) \
(shared_heap && (app_addr) >= shared_heap_start_off \
&& (app_addr) <= shared_heap_end_off - bytes + 1) \
|| ({ \
bool in_chain = false; \
WASMSharedHeap *cur; \
uint64 cur_shared_heap_start_off, cur_shared_heap_end_off; \
for (cur = shared_heap; cur; cur = cur->chain_next) { \
cur_shared_heap_start_off = get_shared_heap_start_off(cur); \
cur_shared_heap_end_off = \
cur_shared_heap_start_off - 1 + cur->size; \
if ((app_addr) >= cur_shared_heap_start_off \
&& (app_addr) <= cur_shared_heap_end_off - bytes + 1) { \
shared_heap_start_off = cur_shared_heap_start_off; \
shared_heap_end_off = cur_shared_heap_end_off; \
shared_heap_base_addr = cur->base_addr; \
in_chain = true; \
break; \
} \
} \
in_chain; \
})

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

#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
if (app_addr_in_shared_heap(app_addr, bytes)) \
shared_heap_addr_app_to_native(app_addr, native_addr); \
else
#else
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
#endif

#if !defined(OS_ENABLE_HW_BOUND_CHECK) \
|| WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
#define CHECK_MEMORY_OVERFLOW(bytes) \
Expand Down Expand Up @@ -1562,6 +1522,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
bool is_return_call = false;
#endif
#if WASM_ENABLE_SHARED_HEAP != 0
/* TODO: currently flowing two variables are only dummy for shared heap
* boundary check, need to be updated when multi-memory or memory64
* proposals are to be implemented */
bool is_memory64 = false;
uint32 memidx = 0;

WASMSharedHeap *shared_heap = module->e ? module->e->shared_heap : NULL;
uint8 *shared_heap_base_addr = shared_heap ? shared_heap->base_addr : NULL;
uint64 shared_heap_start_off =
Expand Down

0 comments on commit a9d776e

Please sign in to comment.