mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-11 20:21:11 +00:00
code refactor
This commit is contained in:
parent
948d981a5c
commit
035f758734
|
@ -596,7 +596,7 @@ wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
|
|||
return get_shared_heap(module_inst_comm);
|
||||
}
|
||||
|
||||
static bool
|
||||
bool
|
||||
is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
|
||||
bool is_memory64, uint64 app_offset, uint32 bytes)
|
||||
{
|
||||
|
@ -641,7 +641,6 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
|
|||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
fail:
|
||||
return false;
|
||||
}
|
||||
|
@ -1220,11 +1219,6 @@ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
|
|||
|
||||
bounds_checks = is_bounds_checks_enabled(module_inst_comm);
|
||||
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
/* If shared heap is enabled, bounds check is always needed */
|
||||
bounds_checks = true;
|
||||
#endif
|
||||
|
||||
memory_inst = wasm_get_default_memory(module_inst);
|
||||
if (!memory_inst) {
|
||||
return 0;
|
||||
|
@ -1350,33 +1344,14 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
|
|||
if (is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module_inst,
|
||||
memory_inst->is_memory64, app_buf_addr,
|
||||
app_buf_size)) {
|
||||
const char *str, *str_end;
|
||||
shared_heap_base_addr_adj = get_last_used_shared_heap_base_addr_adj(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
shared_heap_end_off = get_last_used_shared_heap_end_offset(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
native_addr = shared_heap_base_addr_adj + app_buf_addr;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
|
||||
}
|
||||
|
||||
bounds_checks =
|
||||
is_bounds_checks_enabled((WASMModuleInstanceCommon *)module_inst);
|
||||
|
||||
if (!bounds_checks) {
|
||||
if (app_buf_addr == 0) {
|
||||
native_addr = NULL;
|
||||
}
|
||||
goto success;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
if (shared_heap_base_addr_adj) {
|
||||
const char *str, *str_end;
|
||||
|
||||
/* The whole string must be in the linear memory */
|
||||
/* The whole string must be in the shared heap */
|
||||
str = (const char *)native_addr;
|
||||
str_end = (const char *)shared_heap_base_addr_adj + shared_heap_end_off;
|
||||
while (str < str_end && *str != '\0')
|
||||
|
@ -1390,6 +1365,17 @@ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
|
|||
}
|
||||
#endif
|
||||
|
||||
native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
|
||||
bounds_checks =
|
||||
is_bounds_checks_enabled((WASMModuleInstanceCommon *)module_inst);
|
||||
|
||||
if (!bounds_checks) {
|
||||
if (app_buf_addr == 0) {
|
||||
native_addr = NULL;
|
||||
}
|
||||
goto success;
|
||||
}
|
||||
|
||||
/* No need to check the app_offset and buf_size if memory access
|
||||
boundary check with hardware trap is enabled */
|
||||
#ifndef OS_ENABLE_HW_BOUND_CHECK
|
||||
|
|
|
@ -41,7 +41,51 @@ SET_LINEAR_MEMORY_SIZE(WASMMemoryInstance *memory, uint64 size)
|
|||
#define SET_LINEAR_MEMORY_SIZE(memory, size) memory->memory_data_size = size
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
#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 UINTPTR_MAX == UINT64_MAX
|
||||
#define get_shared_heap_end_off() module->e->shared_heap_end_off.u64
|
||||
#else
|
||||
#define get_shared_heap_end_off() \
|
||||
(uint64)(module->e->shared_heap_end_off.u32[0])
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_MEMORY64 != 0
|
||||
#define shared_heap_is_memory64 is_memory64
|
||||
#else
|
||||
#define shared_heap_is_memory64 false
|
||||
#endif
|
||||
|
||||
#define app_addr_in_shared_heap(app_addr, bytes) \
|
||||
(is_default_memory \
|
||||
&& is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module, \
|
||||
shared_heap_is_memory64, (uint64)app_addr, \
|
||||
bytes))
|
||||
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
|
||||
native_addr = module->e->shared_heap_base_addr_adj + app_addr
|
||||
#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 /* else of WASM_ENABLE_SHARED_HEAP != 0 */
|
||||
#define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
|
||||
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
|
||||
#endif /* end of WASM_ENABLE_INTERP != 0 */
|
||||
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
bool
|
||||
is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
|
||||
bool is_memory64, uint64 app_offset, uint32 bytes);
|
||||
|
||||
WASMSharedHeap *
|
||||
wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args);
|
||||
|
||||
|
@ -74,7 +118,7 @@ wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
|
|||
void
|
||||
wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst,
|
||||
uint64 ptr);
|
||||
#endif
|
||||
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
|
||||
|
||||
bool
|
||||
wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
|
||||
|
|
|
@ -1622,9 +1622,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
if (memory)
|
||||
is_memory64 = memory->is_memory64;
|
||||
#endif
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
WASMModuleInstanceExtra *e = module->e;
|
||||
#endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
|
||||
#if WASM_ENABLE_MULTI_MEMORY != 0
|
||||
uint32 memidx = 0;
|
||||
uint32 memidx_cached = (uint32)-1;
|
||||
|
@ -2399,7 +2396,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
else
|
||||
cur_func_type = cur_func->u.func->func_type;
|
||||
|
||||
/* clang-format off */
|
||||
/* clang-format off */
|
||||
#if WASM_ENABLE_GC == 0
|
||||
if (cur_type != cur_func_type) {
|
||||
wasm_set_exception(module, "indirect call type mismatch");
|
||||
|
@ -5773,15 +5770,13 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
if (app_addr_in_shared_heap((uint64)dst, len))
|
||||
dlen =
|
||||
get_last_used_shared_heap_end_off() - dst + 1;
|
||||
dlen = get_shared_heap_end_off() - dst + 1;
|
||||
#endif
|
||||
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
if (app_addr_in_shared_heap((uint64)dst, len)) {
|
||||
shared_heap_addr_app_to_native((uint64)dst, mdst);
|
||||
dlen =
|
||||
get_last_used_shared_heap_end_off() - dst + 1;
|
||||
dlen = get_shared_heap_end_off() - dst + 1;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
|
|
|
@ -1525,7 +1525,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
/* 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 */
|
||||
WASMModuleInstanceExtra *e = module->e;
|
||||
bool is_memory64 = false;
|
||||
uint32 memidx = 0;
|
||||
(void)is_memory64;
|
||||
|
@ -5079,8 +5078,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
if (app_addr_in_shared_heap((uint64)dst, len))
|
||||
dlen = (uint64)get_last_used_shared_heap_end_off()
|
||||
- dst + 1;
|
||||
dlen = (uint64)get_shared_heap_end_off() - dst + 1;
|
||||
#endif
|
||||
#else /* else of OS_ENABLE_HW_BOUND_CHECK */
|
||||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
|
@ -5097,8 +5095,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
|
|||
#if WASM_ENABLE_SHARED_HEAP != 0
|
||||
if (app_addr_in_shared_heap((uint64)dst, len)) {
|
||||
shared_heap_addr_app_to_native((uint64)dst, mdst);
|
||||
dlen = (uint64)get_last_used_shared_heap_end_off()
|
||||
- dst + 1;
|
||||
dlen = (uint64)get_shared_heap_end_off() - dst + 1;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
|
|
|
@ -73,83 +73,6 @@ wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
|
|||
uint8 block_type, uint8 **p_else_addr,
|
||||
uint8 **p_end_addr);
|
||||
|
||||
#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
|
||||
|
||||
#if UINTPTR_MAX == UINT64_MAX
|
||||
#define update_last_used_shared_heap(shared_heap) \
|
||||
do { \
|
||||
e->shared_heap_start_off.u64 = get_shared_heap_start_off(shared_heap); \
|
||||
e->shared_heap_end_off.u64 = \
|
||||
e->shared_heap_start_off.u64 - 1 + shared_heap->size; \
|
||||
e->shared_heap_base_addr_adj = \
|
||||
shared_heap->base_addr - e->shared_heap_start_off.u64; \
|
||||
} while (0)
|
||||
#define get_last_used_shared_heap_start_off() e->shared_heap_start_off.u64
|
||||
#define get_last_used_shared_heap_end_off() e->shared_heap_end_off.u64
|
||||
#else
|
||||
#define update_last_used_shared_heap(shared_heap) \
|
||||
do { \
|
||||
e->shared_heap_start_off.u32[0] = \
|
||||
(uint32)shared_heap->start_off_mem32; \
|
||||
e->shared_heap_end_off.u32[0] = \
|
||||
e->shared_heap_start_off.u32[0] - 1 + shared_heap->size; \
|
||||
e->shared_heap_base_addr_adj = \
|
||||
shared_heap->base_addr - e->shared_heap_start_off.u32[0]; \
|
||||
} while (0)
|
||||
#define get_last_used_shared_heap_start_off() \
|
||||
(uint64) e->shared_heap_start_off.u32[0]
|
||||
#define get_last_used_shared_heap_end_off() \
|
||||
(uint64) e->shared_heap_end_off.u32[0]
|
||||
#endif /* end of UINTPTR_MAX == UINT64_MAX */
|
||||
|
||||
/* 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) \
|
||||
(e->shared_heap && is_default_memory \
|
||||
&& (app_addr) >= get_last_used_shared_heap_start_off() \
|
||||
&& (app_addr) <= get_last_used_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 = e->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) { \
|
||||
update_last_used_shared_heap(cur); \
|
||||
in_chain = true; \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
in_chain; \
|
||||
})
|
||||
#define shared_heap_addr_app_to_native(app_addr, native_addr) \
|
||||
native_addr = e->shared_heap_base_addr_adj + app_addr
|
||||
#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 /* end of WASM_ENABLE_SHARED_HEAP != 0 */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue
Block a user