16
16
#include "../libraries/thread-mgr/thread_manager.h"
17
17
#endif
18
18
19
+ #include "wasm_interp.h"
20
+ #if WASM_ENABLE_CHECKPOINT_RESTORE != 0
21
+ #include "../libraries/ckpt-restore/ckpt_restore.h"
22
+ #endif
23
+
19
24
/*
20
25
* Note: These offsets need to match the values hardcoded in
21
26
* AoT compilation code: aot_create_func_context, check_suspend_flags.
@@ -72,6 +77,11 @@ bh_static_assert(offsetof(AOTFrame, sp) == sizeof(uintptr_t) * 5);
72
77
bh_static_assert (offsetof(AOTFrame , frame_ref ) == sizeof (uintptr_t ) * 6 );
73
78
bh_static_assert (offsetof(AOTFrame , lp ) == sizeof (uintptr_t ) * 7 );
74
79
80
+ bh_static_assert (offsetof(AOTFrame , ip_offset ) == sizeof (uintptr_t ) * 4 );
81
+ bh_static_assert (offsetof(AOTFrame , sp ) == sizeof (uintptr_t ) * 5 );
82
+ bh_static_assert (offsetof(AOTFrame , frame_ref ) == sizeof (uintptr_t ) * 6 );
83
+ bh_static_assert (offsetof(AOTFrame , lp ) == sizeof (uintptr_t ) * 7 );
84
+
75
85
static void
76
86
set_error_buf (char * error_buf , uint32 error_buf_size , const char * string )
77
87
{
@@ -804,6 +814,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
804
814
bh_assert (memory_idx == 0 );
805
815
bh_assert (parent -> memory_count > memory_idx );
806
816
shared_memory_instance = parent -> memories [memory_idx ];
817
+ shared_memory_instance -> ref_count ++ ;
807
818
shared_memory_inc_reference (shared_memory_instance );
808
819
return shared_memory_instance ;
809
820
}
@@ -992,6 +1003,9 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
992
1003
AOTMemoryInstance * memories , * memory_inst ;
993
1004
AOTMemInitData * data_seg ;
994
1005
uint64 total_size ;
1006
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1007
+ bool is_shared_memory ;
1008
+ #endif
995
1009
996
1010
module_inst -> memory_count = memory_count ;
997
1011
total_size = sizeof (AOTMemoryInstance * ) * (uint64 )memory_count ;
@@ -1019,6 +1033,15 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
1019
1033
return true;
1020
1034
}
1021
1035
1036
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1037
+ /* Currently we have only one memory instance */
1038
+ is_shared_memory = module -> memories [0 ].memory_flags & 0x02 ? true : false;
1039
+ if (is_shared_memory && parent != NULL ) {
1040
+ /* Ignore setting memory init data if the memory has been initialized */
1041
+ return true;
1042
+ }
1043
+ #endif
1044
+
1022
1045
for (i = 0 ; i < module -> mem_init_data_count ; i ++ ) {
1023
1046
data_seg = module -> mem_init_data_list [i ];
1024
1047
#if WASM_ENABLE_BULK_MEMORY != 0
@@ -1860,6 +1883,7 @@ destroy_c_api_frames(Vector *frames)
1860
1883
void
1861
1884
aot_deinstantiate (AOTModuleInstance * module_inst , bool is_sub_inst )
1862
1885
{
1886
+ #if WASM_ENABLE_CHECKPOINT_RESTORE == 0
1863
1887
WASMModuleInstanceExtraCommon * common =
1864
1888
& ((AOTModuleInstanceExtra * )module_inst -> e )-> common ;
1865
1889
if (module_inst -> exec_env_singleton ) {
@@ -1934,6 +1958,7 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
1934
1958
#endif
1935
1959
1936
1960
wasm_runtime_free (module_inst );
1961
+ #endif
1937
1962
}
1938
1963
1939
1964
AOTFunctionInstance *
@@ -2225,6 +2250,7 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
2225
2250
while (exec_env -> cur_frame != prev_frame )
2226
2251
aot_free_frame (exec_env );
2227
2252
#endif
2253
+ // checkpoint
2228
2254
if (!ret ) {
2229
2255
if (argv1 != argv1_buf )
2230
2256
wasm_runtime_free (argv1 );
@@ -2789,6 +2815,17 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
2789
2815
}
2790
2816
2791
2817
tbl_elem_val = ((table_elem_type_t * )tbl_inst -> elems )[table_elem_idx ];
2818
+ #if WASM_ENABLE_CHECKPOINT_RESTORE != 0
2819
+ if (exec_env -> is_restore && exec_env -> restore_call_chain ) {
2820
+ struct AOTFrame * rcc = * (exec_env -> restore_call_chain );
2821
+ while (rcc -> prev_frame ) {
2822
+ rcc = rcc -> prev_frame ;
2823
+ }
2824
+ LOG_DEBUG ("func_idx: %d instead of %d of thread %ld\n" , rcc -> func_index ,
2825
+ func_idx , exec_env -> handle );
2826
+ func_idx = rcc -> func_index ;
2827
+ }
2828
+ #endif
2792
2829
if (tbl_elem_val == NULL_REF ) {
2793
2830
aot_set_exception_with_id (module_inst , EXCE_UNINITIALIZED_ELEMENT );
2794
2831
goto fail ;
@@ -3449,6 +3486,12 @@ get_func_name_from_index(const AOTModuleInstance *module_inst,
3449
3486
#endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 || \
3450
3487
WASM_ENABLE_PERF_PROFILING != 0 */
3451
3488
3489
+ void
3490
+ aot_raise (WASMExecEnv * exec_env , int sig )
3491
+ {
3492
+ raise (sig );
3493
+ }
3494
+
3452
3495
#if WASM_ENABLE_GC == 0
3453
3496
bool
3454
3497
aot_alloc_frame (WASMExecEnv * exec_env , uint32 func_index )
@@ -3528,6 +3571,9 @@ aot_free_frame(WASMExecEnv *exec_env)
3528
3571
bool
3529
3572
aot_alloc_frame (WASMExecEnv * exec_env , uint32 func_index )
3530
3573
{
3574
+ #if WASM_ENABLE_CHECKPOINT_RESTORE != 0
3575
+ LOG_DEBUG ("aot_alloc_frame %u thread %d\n" , func_index , exec_env -> handle );
3576
+ #endif
3531
3577
AOTModuleInstance * module_inst = (AOTModuleInstance * )exec_env -> module_inst ;
3532
3578
AOTModule * module = (AOTModule * )module_inst -> module ;
3533
3579
#if WASM_ENABLE_PERF_PROFILING != 0
@@ -3537,6 +3583,27 @@ aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
3537
3583
AOTFrame * frame ;
3538
3584
uint32 max_local_cell_num , max_stack_cell_num , all_cell_num ;
3539
3585
uint32 aot_func_idx , frame_size ;
3586
+ #if WASM_ENABLE_CHECKPOINT_RESTORE != 0
3587
+ if (exec_env -> restore_call_chain ) {
3588
+ frame = exec_env -> restore_call_chain [exec_env -> call_chain_size - 1 ];
3589
+ LOG_DEBUG ("frame restored, func idx %zu\n" , frame -> func_index );
3590
+ exec_env -> call_chain_size -- ;
3591
+ frame -> prev_frame = (AOTFrame * )exec_env -> cur_frame ;
3592
+ exec_env -> cur_frame = (struct WASMInterpFrame * )frame ;
3593
+ if (exec_env -> call_chain_size == 0 ) {
3594
+ // TODO: fix memory leak
3595
+ exec_env -> restore_call_chain = NULL ;
3596
+ }
3597
+ LOG_DEBUG ("restore call chain %zu==%u, %p, %p, %d\n" ,
3598
+ ((AOTFrame * )exec_env -> cur_frame )-> func_index , func_index ,
3599
+ exec_env , exec_env -> restore_call_chain , exec_env -> handle );
3600
+ if (((AOTFrame * )exec_env -> cur_frame )-> func_index != func_index ) {
3601
+ LOG_DEBUG ("NOT MATCH!!!\n" );
3602
+ exit (1 );
3603
+ }
3604
+ return true;
3605
+ }
3606
+ #endif
3540
3607
3541
3608
if (func_index >= module -> import_func_count ) {
3542
3609
aot_func_idx = func_index - module -> import_func_count ;
@@ -3568,6 +3635,11 @@ aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
3568
3635
frame -> time_started = (uintptr_t )os_time_thread_cputime_us ();
3569
3636
frame -> func_perf_prof_info = func_perf_prof ;
3570
3637
#endif
3638
+ frame -> ip_offset = 0 ;
3639
+ frame -> sp = frame -> lp + max_local_cell_num ;
3640
+ #if WASM_ENABLE_GC != 0
3641
+ frame -> frame_ref = frame -> sp + max_stack_cell_num ;
3642
+ #endif
3571
3643
3572
3644
#if WASM_ENABLE_GC != 0
3573
3645
frame -> sp = frame -> lp + max_local_cell_num ;
@@ -3584,6 +3656,10 @@ aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
3584
3656
static inline void
3585
3657
aot_free_frame_internal (WASMExecEnv * exec_env )
3586
3658
{
3659
+ #if WASM_ENABLE_CHECKPOINT_RESTORE != 0
3660
+ int func_index = ((AOTFrame * )exec_env -> cur_frame )-> func_index ;
3661
+ LOG_DEBUG ("aot_free_frame %zu %d\n" , func_index , exec_env -> handle );
3662
+ #endif
3587
3663
AOTFrame * cur_frame = (AOTFrame * )exec_env -> cur_frame ;
3588
3664
AOTFrame * prev_frame = cur_frame -> prev_frame ;
3589
3665
@@ -3598,7 +3674,6 @@ aot_free_frame_internal(WASMExecEnv *exec_env)
3598
3674
if (prev_frame )
3599
3675
prev_frame -> func_perf_prof_info -> children_exec_time += time_elapsed ;
3600
3676
#endif
3601
-
3602
3677
wasm_exec_env_free_wasm_frame (exec_env , cur_frame );
3603
3678
exec_env -> cur_frame = (struct WASMInterpFrame * )prev_frame ;
3604
3679
}
0 commit comments