Skip to content

Commit 8c63fc1

Browse files
committed
Merge main into dev/fast_jit
2 parents 738d569 + fd5030e commit 8c63fc1

File tree

8 files changed

+590
-100
lines changed

8 files changed

+590
-100
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
518518
#endif
519519

520520
if (os_mprotect(p, total_size, MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) {
521-
set_error_buf(error_buf, error_buf_size, "mprotec memory failed");
521+
set_error_buf(error_buf, error_buf_size, "mprotect memory failed");
522522
#ifdef BH_PLATFORM_WINDOWS
523523
os_mem_decommit(p, total_size);
524524
#endif
@@ -1186,12 +1186,12 @@ aot_lookup_function(const AOTModuleInstance *module_inst, const char *name,
11861186

11871187
#ifdef OS_ENABLE_HW_BOUND_CHECK
11881188

1189-
static os_thread_local_attribute WASMExecEnv *aot_exec_env = NULL;
1190-
11911189
#ifndef BH_PLATFORM_WINDOWS
1192-
static void
1193-
aot_signal_handler(void *sig_addr)
1190+
void
1191+
aot_signal_handler(WASMSignalInfo *sig_info)
11941192
{
1193+
WASMExecEnv *exec_env_tls = sig_info->exec_env_tls;
1194+
void *sig_addr = sig_info->sig_addr;
11951195
AOTModuleInstance *module_inst;
11961196
AOTMemoryInstance *memory_inst;
11971197
WASMJmpBuf *jmpbuf_node;
@@ -1202,10 +1202,10 @@ aot_signal_handler(void *sig_addr)
12021202
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
12031203

12041204
/* Check whether current thread is running aot function */
1205-
if (aot_exec_env && aot_exec_env->handle == os_self_thread()
1206-
&& (jmpbuf_node = aot_exec_env->jmpbuf_stack_top)) {
1205+
if (exec_env_tls && exec_env_tls->handle == os_self_thread()
1206+
&& (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
12071207
/* Get mapped mem info of current instance */
1208-
module_inst = (AOTModuleInstance *)aot_exec_env->module_inst;
1208+
module_inst = (AOTModuleInstance *)exec_env_tls->module_inst;
12091209
/* Get the default memory instance */
12101210
memory_inst = aot_get_default_memory(module_inst);
12111211
if (memory_inst) {
@@ -1222,7 +1222,7 @@ aot_signal_handler(void *sig_addr)
12221222
&& (mapped_mem_start_addr <= (uint8 *)sig_addr
12231223
&& (uint8 *)sig_addr < mapped_mem_end_addr)) {
12241224
/* The address which causes segmentation fault is inside
1225-
aot instance's guard regions */
1225+
the memory instance's guard regions */
12261226
aot_set_exception_with_id(module_inst,
12271227
EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS);
12281228
os_longjmp(jmpbuf_node->jmpbuf, 1);
@@ -1238,9 +1238,11 @@ aot_signal_handler(void *sig_addr)
12381238
}
12391239
}
12401240
#else /* else of BH_PLATFORM_WINDOWS */
1241-
static LONG
1242-
aot_exception_handler(EXCEPTION_POINTERS *exce_info)
1241+
LONG
1242+
aot_exception_handler(WASMSignalInfo *sig_info)
12431243
{
1244+
WASMExecEnv *exec_env_tls = sig_info->exec_env_tls;
1245+
EXCEPTION_POINTERS *exce_info = sig_info->exce_info;
12441246
PEXCEPTION_RECORD ExceptionRecord = exce_info->ExceptionRecord;
12451247
uint8 *sig_addr = (uint8 *)ExceptionRecord->ExceptionInformation[1];
12461248
AOTModuleInstance *module_inst;
@@ -1250,9 +1252,9 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info)
12501252
uint8 *mapped_mem_end_addr = NULL;
12511253
uint32 page_size = os_getpagesize();
12521254

1253-
if (aot_exec_env && aot_exec_env->handle == os_self_thread()
1254-
&& (jmpbuf_node = aot_exec_env->jmpbuf_stack_top)) {
1255-
module_inst = (AOTModuleInstance *)aot_exec_env->module_inst;
1255+
if (exec_env_tls && exec_env_tls->handle == os_self_thread()
1256+
&& (jmpbuf_node = exec_env_tls->jmpbuf_stack_top)) {
1257+
module_inst = (AOTModuleInstance *)exec_env_tls->module_inst;
12561258
if (ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
12571259
/* Get the default memory instance */
12581260
memory_inst = aot_get_default_memory(module_inst);
@@ -1293,40 +1295,14 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info)
12931295
}
12941296
#endif /* end of BH_PLATFORM_WINDOWS */
12951297

1296-
bool
1297-
aot_signal_init()
1298-
{
1299-
#ifndef BH_PLATFORM_WINDOWS
1300-
return os_thread_signal_init(aot_signal_handler) == 0 ? true : false;
1301-
#else
1302-
if (os_thread_signal_init() != 0)
1303-
return false;
1304-
1305-
if (!AddVectoredExceptionHandler(1, aot_exception_handler)) {
1306-
os_thread_signal_destroy();
1307-
return false;
1308-
}
1309-
#endif
1310-
return true;
1311-
}
1312-
1313-
void
1314-
aot_signal_destroy()
1315-
{
1316-
#ifdef BH_PLATFORM_WINDOWS
1317-
RemoveVectoredExceptionHandler(aot_exception_handler);
1318-
#endif
1319-
os_thread_signal_destroy();
1320-
}
1321-
13221298
static bool
13231299
invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
13241300
const WASMType *func_type,
13251301
const char *signature, void *attachment,
13261302
uint32 *argv, uint32 argc, uint32 *argv_ret)
13271303
{
13281304
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
1329-
WASMExecEnv **p_aot_exec_env = &aot_exec_env;
1305+
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
13301306
WASMJmpBuf jmpbuf_node = { 0 }, *jmpbuf_node_pop;
13311307
uint32 page_size = os_getpagesize();
13321308
uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
@@ -1348,7 +1324,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
13481324
return false;
13491325
}
13501326

1351-
if (aot_exec_env && (aot_exec_env != exec_env)) {
1327+
if (exec_env_tls && (exec_env_tls != exec_env)) {
13521328
aot_set_exception(module_inst, "invalid exec env");
13531329
return false;
13541330
}
@@ -1360,7 +1336,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
13601336

13611337
wasm_exec_env_push_jmpbuf(exec_env, &jmpbuf_node);
13621338

1363-
aot_exec_env = exec_env;
1339+
wasm_runtime_set_exec_env_tls(exec_env);
13641340
if (os_setjmp(jmpbuf_node.jmpbuf) == 0) {
13651341
/* Quick call with func_ptr if the function signature is simple */
13661342
if (!signature && param_count == 1 && types[0] == VALUE_TYPE_I32) {
@@ -1406,7 +1382,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
14061382
jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf(exec_env);
14071383
bh_assert(&jmpbuf_node == jmpbuf_node_pop);
14081384
if (!exec_env->jmpbuf_stack_top) {
1409-
*p_aot_exec_env = NULL;
1385+
wasm_runtime_set_exec_env_tls(NULL);
14101386
}
14111387
if (!ret) {
14121388
os_sigreturn();
@@ -1594,7 +1570,7 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
15941570
bool ret;
15951571

15961572
#if defined(OS_ENABLE_HW_BOUND_CHECK)
1597-
existing_exec_env = exec_env = aot_exec_env;
1573+
existing_exec_env = exec_env = wasm_runtime_get_exec_env_tls();
15981574
#elif WASM_ENABLE_THREAD_MGR != 0
15991575
existing_exec_env = exec_env =
16001576
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
@@ -1611,7 +1587,7 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
16111587

16121588
ret = aot_call_function(exec_env, func, argc, argv);
16131589

1614-
/* don't destroy the exec_env if it's searched from the cluster */
1590+
/* don't destroy the exec_env if it isn't created in this function */
16151591
if (!existing_exec_env)
16161592
wasm_exec_env_destroy(exec_env);
16171593

@@ -1707,6 +1683,9 @@ execute_malloc_function(AOTModuleInstance *module_inst,
17071683
AOTFunctionInstance *retain_func, uint32 size,
17081684
uint32 *p_result)
17091685
{
1686+
#ifdef OS_ENABLE_HW_BOUND_CHECK
1687+
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
1688+
#endif
17101689
uint32 argv[2], argc;
17111690
bool ret;
17121691

@@ -1718,13 +1697,13 @@ execute_malloc_function(AOTModuleInstance *module_inst,
17181697
}
17191698

17201699
#ifdef OS_ENABLE_HW_BOUND_CHECK
1721-
if (aot_exec_env != NULL) {
1722-
bh_assert(aot_exec_env->module_inst
1700+
if (exec_env_tls != NULL) {
1701+
bh_assert(exec_env_tls->module_inst
17231702
== (WASMModuleInstanceCommon *)module_inst);
1724-
ret = aot_call_function(aot_exec_env, malloc_func, argc, argv);
1703+
ret = aot_call_function(exec_env_tls, malloc_func, argc, argv);
17251704

17261705
if (retain_func && ret) {
1727-
ret = aot_call_function(aot_exec_env, retain_func, 1, argv);
1706+
ret = aot_call_function(exec_env_tls, retain_func, 1, argv);
17281707
}
17291708
}
17301709
else
@@ -1748,14 +1727,17 @@ static bool
17481727
execute_free_function(AOTModuleInstance *module_inst,
17491728
AOTFunctionInstance *free_func, uint32 offset)
17501729
{
1730+
#ifdef OS_ENABLE_HW_BOUND_CHECK
1731+
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
1732+
#endif
17511733
uint32 argv[2];
17521734

17531735
argv[0] = offset;
17541736
#ifdef OS_ENABLE_HW_BOUND_CHECK
1755-
if (aot_exec_env != NULL) {
1756-
bh_assert(aot_exec_env->module_inst
1737+
if (exec_env_tls != NULL) {
1738+
bh_assert(exec_env_tls->module_inst
17571739
== (WASMModuleInstanceCommon *)module_inst);
1758-
return aot_call_function(aot_exec_env, free_func, 1, argv);
1740+
return aot_call_function(exec_env_tls, free_func, 1, argv);
17591741
}
17601742
else
17611743
#endif
@@ -2197,8 +2179,8 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
21972179
return false;
21982180
}
21992181

2200-
memset(memory_inst->memory_data_end.ptr, 0,
2201-
num_bytes_per_page * inc_page_count);
2182+
/* The increased pages are filled with zero by the OS when os_mmap,
2183+
no need to memset it again here */
22022184

22032185
memory_inst->cur_page_count = total_page_count;
22042186
memory_inst->memory_data_size = (uint32)total_size;

core/iwasm/aot/aot_runtime.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,13 @@ aot_get_aux_stack(WASMExecEnv *exec_env, uint32 *start_offset, uint32 *size);
684684
#endif
685685

686686
#ifdef OS_ENABLE_HW_BOUND_CHECK
687-
bool
688-
aot_signal_init();
689-
687+
#ifndef BH_PLATFORM_WINDOWS
690688
void
691-
aot_signal_destroy();
689+
aot_signal_handler(WASMSignalInfo *sig_info);
690+
#else
691+
LONG
692+
aot_exception_handler(WASMSignalInfo *sig_info);
693+
#endif
692694
#endif
693695

694696
void

0 commit comments

Comments
 (0)