@@ -210,6 +210,14 @@ impl GenmcCtx {
210
210
fn get_alloc_data_races ( & self ) -> bool {
211
211
self . exec_state . allow_data_races . get ( )
212
212
}
213
+
214
+ /// Get the id of the currently active thread mapped to it's thread ID for GenMC.
215
+ #[ must_use]
216
+ fn active_thread_genmc_tid < ' tcx > ( & self , machine : & MiriMachine < ' tcx > ) -> i32 {
217
+ let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
218
+ let curr_thread = machine. threads . active_thread ( ) ;
219
+ thread_infos. get_genmc_tid ( curr_thread)
220
+ }
213
221
}
214
222
215
223
/// GenMC event handling. These methods are used to inform GenMC about events happening in the program, and to handle scheduling decisions.
@@ -319,12 +327,10 @@ impl GenmcCtx {
319
327
ordering : AtomicFenceOrd ,
320
328
) -> InterpResult < ' tcx > {
321
329
assert ! ( !self . get_alloc_data_races( ) , "atomic fence with data race checking disabled." ) ;
322
-
323
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
324
- let curr_thread = machine. threads . active_thread ( ) ;
325
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread) ;
326
-
327
- self . handle . borrow_mut ( ) . pin_mut ( ) . handle_fence ( genmc_tid, ordering. to_genmc ( ) ) ;
330
+ self . handle
331
+ . borrow_mut ( )
332
+ . pin_mut ( )
333
+ . handle_fence ( self . active_thread_genmc_tid ( machine) , ordering. to_genmc ( ) ) ;
328
334
interp_ok ( ( ) )
329
335
}
330
336
@@ -438,12 +444,8 @@ impl GenmcCtx {
438
444
debug ! (
439
445
"GenMC: atomic_compare_exchange, address: {address:?}, size: {size:?} (expect: {expected_old_value:?}, new: {new_value:?}, old_value: {old_value:?}, {success:?}, orderings: {fail:?}), can fail spuriously: {can_fail_spuriously}"
440
446
) ;
441
-
442
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
443
- let genmc_tid = thread_infos. get_genmc_tid ( ecx. machine . threads . active_thread ( ) ) ;
444
-
445
447
let cas_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_compare_exchange (
446
- genmc_tid ,
448
+ self . active_thread_genmc_tid ( & ecx . machine ) ,
447
449
address. bytes ( ) ,
448
450
size. bytes ( ) ,
449
451
scalar_to_genmc_scalar ( ecx, self , expected_old_value) ?,
@@ -604,14 +606,10 @@ impl GenmcCtx {
604
606
return ecx
605
607
. get_global_allocation_address ( & self . global_state . global_allocations , alloc_id) ;
606
608
}
607
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
608
- let curr_thread = machine. threads . active_thread ( ) ;
609
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread) ;
610
609
// GenMC doesn't support ZSTs, so we set the minimum size to 1 byte
611
610
let genmc_size = size. bytes ( ) . max ( 1 ) ;
612
-
613
611
let chosen_address = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_malloc (
614
- genmc_tid ,
612
+ self . active_thread_genmc_tid ( machine ) ,
615
613
genmc_size,
616
614
alignment. bytes ( ) ,
617
615
) ;
@@ -645,11 +643,12 @@ impl GenmcCtx {
645
643
!self . get_alloc_data_races( ) ,
646
644
"memory deallocation with data race checking disabled."
647
645
) ;
648
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
649
- let curr_thread = machine. threads . active_thread ( ) ;
650
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread) ;
651
-
652
- if self . handle . borrow_mut ( ) . pin_mut ( ) . handle_free ( genmc_tid, address. bytes ( ) ) {
646
+ if self
647
+ . handle
648
+ . borrow_mut ( )
649
+ . pin_mut ( )
650
+ . handle_free ( self . active_thread_genmc_tid ( machine) , address. bytes ( ) )
651
+ {
653
652
// FIXME(genmc): improve error handling.
654
653
// An error was detected, so we get the error string from GenMC.
655
654
throw_ub_format ! ( "{}" , self . try_get_error( ) . unwrap( ) ) ;
@@ -703,7 +702,7 @@ impl GenmcCtx {
703
702
let genmc_tid = thread_infos. get_genmc_tid ( curr_thread_id) ;
704
703
705
704
debug ! ( "GenMC: thread {curr_thread_id:?} ({genmc_tid:?}) finished." ) ;
706
- // NOTE: Miri doesn't support return values for threads, but GenMC expects one, so we return 0
705
+ // NOTE: Miri doesn't support return values for threads, but GenMC expects one, so we return 0.
707
706
self . handle . borrow_mut ( ) . pin_mut ( ) . handle_thread_finish ( genmc_tid, /* ret_val */ 0 ) ;
708
707
}
709
708
@@ -765,17 +764,12 @@ impl GenmcCtx {
765
764
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes." ,
766
765
) ;
767
766
}
768
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
769
- let curr_thread_id = machine. threads . active_thread ( ) ;
770
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread_id) ;
771
-
772
767
debug ! (
773
- "GenMC: load, thread: {curr_thread_id:?} ({genmc_tid:?}), address: {addr} == {addr:#x}, size: {size:?}, ordering: {memory_ordering:?}, old_value: {genmc_old_value:x?}" ,
768
+ "GenMC: load, address: {addr} == {addr:#x}, size: {size:?}, ordering: {memory_ordering:?}, old_value: {genmc_old_value:x?}" ,
774
769
addr = address. bytes( )
775
770
) ;
776
-
777
771
let load_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_load (
778
- genmc_tid ,
772
+ self . active_thread_genmc_tid ( machine ) ,
779
773
address. bytes ( ) ,
780
774
size. bytes ( ) ,
781
775
memory_ordering,
@@ -816,17 +810,12 @@ impl GenmcCtx {
816
810
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes."
817
811
) ;
818
812
}
819
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
820
- let curr_thread_id = machine. threads . active_thread ( ) ;
821
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread_id) ;
822
-
823
813
debug ! (
824
- "GenMC: store, thread: {curr_thread_id:?} ({genmc_tid:?}), address: {addr} = {addr:#x}, size: {size:?}, ordering {memory_ordering:?}, value: {genmc_value:?}" ,
814
+ "GenMC: store, address: {addr} = {addr:#x}, size: {size:?}, ordering {memory_ordering:?}, value: {genmc_value:?}" ,
825
815
addr = address. bytes( )
826
816
) ;
827
-
828
817
let store_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_store (
829
- genmc_tid ,
818
+ self . active_thread_genmc_tid ( machine ) ,
830
819
address. bytes ( ) ,
831
820
size. bytes ( ) ,
832
821
genmc_value,
@@ -867,14 +856,11 @@ impl GenmcCtx {
867
856
MAX_ACCESS_SIZE ,
868
857
size. bytes( )
869
858
) ;
870
-
871
- let curr_thread_id = ecx. machine . threads . active_thread ( ) ;
872
- let genmc_tid = self . exec_state . thread_id_manager . borrow ( ) . get_genmc_tid ( curr_thread_id) ;
873
859
debug ! (
874
- "GenMC: atomic_rmw_op, thread: {curr_thread_id:?} ({genmc_tid:?}) (op: {genmc_rmw_op:?}, rhs value: {genmc_rhs_scalar:?}), address: {address:?}, size: {size:?}, ordering: {ordering:?}" ,
860
+ "GenMC: atomic_rmw_op (op: {genmc_rmw_op:?}, rhs value: {genmc_rhs_scalar:?}), address: {address:?}, size: {size:?}, ordering: {ordering:?}" ,
875
861
) ;
876
862
let rmw_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_read_modify_write (
877
- genmc_tid ,
863
+ self . active_thread_genmc_tid ( & ecx . machine ) ,
878
864
address. bytes ( ) ,
879
865
size. bytes ( ) ,
880
866
genmc_rmw_op,
@@ -904,11 +890,11 @@ impl GenmcCtx {
904
890
/// This may happen due to an manual `assume` statement added by a user
905
891
/// or added by some automated program transformation, e.g., for spinloops.
906
892
fn handle_assume_block < ' tcx > ( & self , machine : & MiriMachine < ' tcx > ) -> InterpResult < ' tcx > {
907
- let curr_thread = machine . threads . active_thread ( ) ;
908
- let genmc_curr_thread =
909
- self . exec_state . thread_id_manager . borrow ( ) . get_genmc_tid ( curr_thread ) ;
910
- debug ! ( "GenMC: assume statement, blocking thread {curr_thread:?} ({genmc_curr_thread:?})" ) ;
911
- self . handle . borrow_mut ( ) . pin_mut ( ) . handle_assume_block ( genmc_curr_thread ) ;
893
+ debug ! ( "GenMC: assume statement, blocking active thread." ) ;
894
+ self . handle
895
+ . borrow_mut ( )
896
+ . pin_mut ( )
897
+ . handle_assume_block ( self . active_thread_genmc_tid ( machine ) ) ;
912
898
interp_ok ( ( ) )
913
899
}
914
900
}
0 commit comments