@@ -518,14 +518,15 @@ 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
525525 os_munmap (mapped_mem , map_size );
526526 return NULL ;
527527 }
528- memset (p , 0 , (uint32 )total_size );
528+ /* Newly allocated pages are filled with zero by the OS, we don't fill it
529+ * again here */
529530#endif /* end of OS_ENABLE_HW_BOUND_CHECK */
530531
531532 memory_inst -> module_type = Wasm_Module_AoT ;
@@ -1185,12 +1186,12 @@ aot_lookup_function(const AOTModuleInstance *module_inst, const char *name,
11851186
11861187#ifdef OS_ENABLE_HW_BOUND_CHECK
11871188
1188- static os_thread_local_attribute WASMExecEnv * aot_exec_env = NULL ;
1189-
11901189#ifndef BH_PLATFORM_WINDOWS
1191- static void
1192- aot_signal_handler (void * sig_addr )
1190+ void
1191+ aot_signal_handler (WASMSignalInfo * sig_info )
11931192{
1193+ WASMExecEnv * exec_env_tls = sig_info -> exec_env_tls ;
1194+ void * sig_addr = sig_info -> sig_addr ;
11941195 AOTModuleInstance * module_inst ;
11951196 AOTMemoryInstance * memory_inst ;
11961197 WASMJmpBuf * jmpbuf_node ;
@@ -1201,10 +1202,10 @@ aot_signal_handler(void *sig_addr)
12011202 uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT ;
12021203
12031204 /* Check whether current thread is running aot function */
1204- if (aot_exec_env && aot_exec_env -> handle == os_self_thread ()
1205- && (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 )) {
12061207 /* Get mapped mem info of current instance */
1207- module_inst = (AOTModuleInstance * )aot_exec_env -> module_inst ;
1208+ module_inst = (AOTModuleInstance * )exec_env_tls -> module_inst ;
12081209 /* Get the default memory instance */
12091210 memory_inst = aot_get_default_memory (module_inst );
12101211 if (memory_inst ) {
@@ -1221,7 +1222,7 @@ aot_signal_handler(void *sig_addr)
12211222 && (mapped_mem_start_addr <= (uint8 * )sig_addr
12221223 && (uint8 * )sig_addr < mapped_mem_end_addr )) {
12231224 /* The address which causes segmentation fault is inside
1224- aot instance's guard regions */
1225+ the memory instance's guard regions */
12251226 aot_set_exception_with_id (module_inst ,
12261227 EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS );
12271228 os_longjmp (jmpbuf_node -> jmpbuf , 1 );
@@ -1237,9 +1238,11 @@ aot_signal_handler(void *sig_addr)
12371238 }
12381239}
12391240#else /* else of BH_PLATFORM_WINDOWS */
1240- static LONG
1241- aot_exception_handler (EXCEPTION_POINTERS * exce_info )
1241+ LONG
1242+ aot_exception_handler (WASMSignalInfo * sig_info )
12421243{
1244+ WASMExecEnv * exec_env_tls = sig_info -> exec_env_tls ;
1245+ EXCEPTION_POINTERS * exce_info = sig_info -> exce_info ;
12431246 PEXCEPTION_RECORD ExceptionRecord = exce_info -> ExceptionRecord ;
12441247 uint8 * sig_addr = (uint8 * )ExceptionRecord -> ExceptionInformation [1 ];
12451248 AOTModuleInstance * module_inst ;
@@ -1249,9 +1252,9 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info)
12491252 uint8 * mapped_mem_end_addr = NULL ;
12501253 uint32 page_size = os_getpagesize ();
12511254
1252- if (aot_exec_env && aot_exec_env -> handle == os_self_thread ()
1253- && (jmpbuf_node = aot_exec_env -> jmpbuf_stack_top )) {
1254- 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 ;
12551258 if (ExceptionRecord -> ExceptionCode == EXCEPTION_ACCESS_VIOLATION ) {
12561259 /* Get the default memory instance */
12571260 memory_inst = aot_get_default_memory (module_inst );
@@ -1292,40 +1295,14 @@ aot_exception_handler(EXCEPTION_POINTERS *exce_info)
12921295}
12931296#endif /* end of BH_PLATFORM_WINDOWS */
12941297
1295- bool
1296- aot_signal_init ()
1297- {
1298- #ifndef BH_PLATFORM_WINDOWS
1299- return os_thread_signal_init (aot_signal_handler ) == 0 ? true : false;
1300- #else
1301- if (os_thread_signal_init () != 0 )
1302- return false;
1303-
1304- if (!AddVectoredExceptionHandler (1 , aot_exception_handler )) {
1305- os_thread_signal_destroy ();
1306- return false;
1307- }
1308- #endif
1309- return true;
1310- }
1311-
1312- void
1313- aot_signal_destroy ()
1314- {
1315- #ifdef BH_PLATFORM_WINDOWS
1316- RemoveVectoredExceptionHandler (aot_exception_handler );
1317- #endif
1318- os_thread_signal_destroy ();
1319- }
1320-
13211298static bool
13221299invoke_native_with_hw_bound_check (WASMExecEnv * exec_env , void * func_ptr ,
13231300 const WASMType * func_type ,
13241301 const char * signature , void * attachment ,
13251302 uint32 * argv , uint32 argc , uint32 * argv_ret )
13261303{
13271304 AOTModuleInstance * module_inst = (AOTModuleInstance * )exec_env -> module_inst ;
1328- WASMExecEnv * * p_aot_exec_env = & aot_exec_env ;
1305+ WASMExecEnv * exec_env_tls = wasm_runtime_get_exec_env_tls () ;
13291306 WASMJmpBuf jmpbuf_node = { 0 }, * jmpbuf_node_pop ;
13301307 uint32 page_size = os_getpagesize ();
13311308 uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT ;
@@ -1347,7 +1324,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
13471324 return false;
13481325 }
13491326
1350- if (aot_exec_env && (aot_exec_env != exec_env )) {
1327+ if (exec_env_tls && (exec_env_tls != exec_env )) {
13511328 aot_set_exception (module_inst , "invalid exec env" );
13521329 return false;
13531330 }
@@ -1359,7 +1336,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
13591336
13601337 wasm_exec_env_push_jmpbuf (exec_env , & jmpbuf_node );
13611338
1362- aot_exec_env = exec_env ;
1339+ wasm_runtime_set_exec_env_tls ( exec_env ) ;
13631340 if (os_setjmp (jmpbuf_node .jmpbuf ) == 0 ) {
13641341 /* Quick call with func_ptr if the function signature is simple */
13651342 if (!signature && param_count == 1 && types [0 ] == VALUE_TYPE_I32 ) {
@@ -1405,7 +1382,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
14051382 jmpbuf_node_pop = wasm_exec_env_pop_jmpbuf (exec_env );
14061383 bh_assert (& jmpbuf_node == jmpbuf_node_pop );
14071384 if (!exec_env -> jmpbuf_stack_top ) {
1408- * p_aot_exec_env = NULL ;
1385+ wasm_runtime_set_exec_env_tls ( NULL ) ;
14091386 }
14101387 if (!ret ) {
14111388 os_sigreturn ();
@@ -1593,7 +1570,7 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
15931570 bool ret ;
15941571
15951572#if defined(OS_ENABLE_HW_BOUND_CHECK )
1596- existing_exec_env = exec_env = aot_exec_env ;
1573+ existing_exec_env = exec_env = wasm_runtime_get_exec_env_tls () ;
15971574#elif WASM_ENABLE_THREAD_MGR != 0
15981575 existing_exec_env = exec_env =
15991576 wasm_clusters_search_exec_env ((WASMModuleInstanceCommon * )module_inst );
@@ -1610,7 +1587,7 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
16101587
16111588 ret = aot_call_function (exec_env , func , argc , argv );
16121589
1613- /* 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 */
16141591 if (!existing_exec_env )
16151592 wasm_exec_env_destroy (exec_env );
16161593
@@ -1706,6 +1683,9 @@ execute_malloc_function(AOTModuleInstance *module_inst,
17061683 AOTFunctionInstance * retain_func , uint32 size ,
17071684 uint32 * p_result )
17081685{
1686+ #ifdef OS_ENABLE_HW_BOUND_CHECK
1687+ WASMExecEnv * exec_env_tls = wasm_runtime_get_exec_env_tls ();
1688+ #endif
17091689 uint32 argv [2 ], argc ;
17101690 bool ret ;
17111691
@@ -1717,13 +1697,13 @@ execute_malloc_function(AOTModuleInstance *module_inst,
17171697 }
17181698
17191699#ifdef OS_ENABLE_HW_BOUND_CHECK
1720- if (aot_exec_env != NULL ) {
1721- bh_assert (aot_exec_env -> module_inst
1700+ if (exec_env_tls != NULL ) {
1701+ bh_assert (exec_env_tls -> module_inst
17221702 == (WASMModuleInstanceCommon * )module_inst );
1723- ret = aot_call_function (aot_exec_env , malloc_func , argc , argv );
1703+ ret = aot_call_function (exec_env_tls , malloc_func , argc , argv );
17241704
17251705 if (retain_func && ret ) {
1726- ret = aot_call_function (aot_exec_env , retain_func , 1 , argv );
1706+ ret = aot_call_function (exec_env_tls , retain_func , 1 , argv );
17271707 }
17281708 }
17291709 else
@@ -1747,14 +1727,17 @@ static bool
17471727execute_free_function (AOTModuleInstance * module_inst ,
17481728 AOTFunctionInstance * free_func , uint32 offset )
17491729{
1730+ #ifdef OS_ENABLE_HW_BOUND_CHECK
1731+ WASMExecEnv * exec_env_tls = wasm_runtime_get_exec_env_tls ();
1732+ #endif
17501733 uint32 argv [2 ];
17511734
17521735 argv [0 ] = offset ;
17531736#ifdef OS_ENABLE_HW_BOUND_CHECK
1754- if (aot_exec_env != NULL ) {
1755- bh_assert (aot_exec_env -> module_inst
1737+ if (exec_env_tls != NULL ) {
1738+ bh_assert (exec_env_tls -> module_inst
17561739 == (WASMModuleInstanceCommon * )module_inst );
1757- return aot_call_function (aot_exec_env , free_func , 1 , argv );
1740+ return aot_call_function (exec_env_tls , free_func , 1 , argv );
17581741 }
17591742 else
17601743#endif
@@ -2196,8 +2179,8 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
21962179 return false;
21972180 }
21982181
2199- memset ( memory_inst -> memory_data_end . ptr , 0 ,
2200- 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 */
22012184
22022185 memory_inst -> cur_page_count = total_page_count ;
22032186 memory_inst -> memory_data_size = (uint32 )total_size ;
0 commit comments