@@ -200,6 +200,14 @@ impl GenmcCtx {
200
200
fn get_alloc_data_races ( & self ) -> bool {
201
201
self . exec_state . allow_data_races . get ( )
202
202
}
203
+
204
+ /// Get the id of the currently active thread mapped to it's thread ID for GenMC.
205
+ #[ must_use]
206
+ fn active_thread_genmc_tid < ' tcx > ( & self , machine : & MiriMachine < ' tcx > ) -> i32 {
207
+ let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
208
+ let curr_thread = machine. threads . active_thread ( ) ;
209
+ thread_infos. get_genmc_tid ( curr_thread)
210
+ }
203
211
}
204
212
205
213
/// GenMC event handling. These methods are used to inform GenMC about events happening in the program, and to handle scheduling decisions.
@@ -309,12 +317,10 @@ impl GenmcCtx {
309
317
ordering : AtomicFenceOrd ,
310
318
) -> InterpResult < ' tcx > {
311
319
assert ! ( !self . get_alloc_data_races( ) , "atomic fence with data race checking disabled." ) ;
312
-
313
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
314
- let curr_thread = machine. threads . active_thread ( ) ;
315
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread) ;
316
-
317
- self . handle . borrow_mut ( ) . pin_mut ( ) . handle_fence ( genmc_tid, ordering. to_genmc ( ) ) ;
320
+ self . handle
321
+ . borrow_mut ( )
322
+ . pin_mut ( )
323
+ . handle_fence ( self . active_thread_genmc_tid ( machine) , ordering. to_genmc ( ) ) ;
318
324
interp_ok ( ( ) )
319
325
}
320
326
@@ -425,12 +431,8 @@ impl GenmcCtx {
425
431
debug ! (
426
432
"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}"
427
433
) ;
428
-
429
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
430
- let genmc_tid = thread_infos. get_genmc_tid ( ecx. machine . threads . active_thread ( ) ) ;
431
-
432
434
let cas_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_compare_exchange (
433
- genmc_tid ,
435
+ self . active_thread_genmc_tid ( & ecx . machine ) ,
434
436
address. bytes ( ) ,
435
437
size. bytes ( ) ,
436
438
scalar_to_genmc_scalar ( ecx, self , expected_old_value) ?,
@@ -591,14 +593,10 @@ impl GenmcCtx {
591
593
return ecx
592
594
. get_global_allocation_address ( & self . global_state . global_allocations , alloc_id) ;
593
595
}
594
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
595
- let curr_thread = machine. threads . active_thread ( ) ;
596
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread) ;
597
596
// GenMC doesn't support ZSTs, so we set the minimum size to 1 byte
598
597
let genmc_size = size. bytes ( ) . max ( 1 ) ;
599
-
600
598
let chosen_address = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_malloc (
601
- genmc_tid ,
599
+ self . active_thread_genmc_tid ( machine ) ,
602
600
genmc_size,
603
601
alignment. bytes ( ) ,
604
602
) ;
@@ -632,11 +630,12 @@ impl GenmcCtx {
632
630
!self . get_alloc_data_races( ) ,
633
631
"memory deallocation with data race checking disabled."
634
632
) ;
635
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
636
- let curr_thread = machine. threads . active_thread ( ) ;
637
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread) ;
638
-
639
- if self . handle . borrow_mut ( ) . pin_mut ( ) . handle_free ( genmc_tid, address. bytes ( ) ) {
633
+ if self
634
+ . handle
635
+ . borrow_mut ( )
636
+ . pin_mut ( )
637
+ . handle_free ( self . active_thread_genmc_tid ( machine) , address. bytes ( ) )
638
+ {
640
639
// FIXME(genmc): improve error handling.
641
640
// An error was detected, so we get the error string from GenMC.
642
641
throw_ub_format ! ( "{}" , self . try_get_error( ) . unwrap( ) ) ;
@@ -690,7 +689,7 @@ impl GenmcCtx {
690
689
let genmc_tid = thread_infos. get_genmc_tid ( curr_thread_id) ;
691
690
692
691
debug ! ( "GenMC: thread {curr_thread_id:?} ({genmc_tid:?}) finished." ) ;
693
- // NOTE: Miri doesn't support return values for threads, but GenMC expects one, so we return 0
692
+ // NOTE: Miri doesn't support return values for threads, but GenMC expects one, so we return 0.
694
693
self . handle . borrow_mut ( ) . pin_mut ( ) . handle_thread_finish ( genmc_tid, /* ret_val */ 0 ) ;
695
694
}
696
695
@@ -752,17 +751,12 @@ impl GenmcCtx {
752
751
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes." ,
753
752
) ;
754
753
}
755
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
756
- let curr_thread_id = machine. threads . active_thread ( ) ;
757
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread_id) ;
758
-
759
754
debug ! (
760
- "GenMC: load, thread: {curr_thread_id:?} ({genmc_tid:?}), address: {addr} == {addr:#x}, size: {size:?}, ordering: {memory_ordering:?}, old_value: {genmc_old_value:x?}" ,
755
+ "GenMC: load, address: {addr} == {addr:#x}, size: {size:?}, ordering: {memory_ordering:?}, old_value: {genmc_old_value:x?}" ,
761
756
addr = address. bytes( )
762
757
) ;
763
-
764
758
let load_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_load (
765
- genmc_tid ,
759
+ self . active_thread_genmc_tid ( machine ) ,
766
760
address. bytes ( ) ,
767
761
size. bytes ( ) ,
768
762
memory_ordering,
@@ -803,17 +797,12 @@ impl GenmcCtx {
803
797
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes."
804
798
) ;
805
799
}
806
- let thread_infos = self . exec_state . thread_id_manager . borrow ( ) ;
807
- let curr_thread_id = machine. threads . active_thread ( ) ;
808
- let genmc_tid = thread_infos. get_genmc_tid ( curr_thread_id) ;
809
-
810
800
debug ! (
811
- "GenMC: store, thread: {curr_thread_id:?} ({genmc_tid:?}), address: {addr} = {addr:#x}, size: {size:?}, ordering {memory_ordering:?}, value: {genmc_value:?}" ,
801
+ "GenMC: store, address: {addr} = {addr:#x}, size: {size:?}, ordering {memory_ordering:?}, value: {genmc_value:?}" ,
812
802
addr = address. bytes( )
813
803
) ;
814
-
815
804
let store_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_store (
816
- genmc_tid ,
805
+ self . active_thread_genmc_tid ( machine ) ,
817
806
address. bytes ( ) ,
818
807
size. bytes ( ) ,
819
808
genmc_value,
@@ -854,14 +843,11 @@ impl GenmcCtx {
854
843
MAX_ACCESS_SIZE ,
855
844
size. bytes( )
856
845
) ;
857
-
858
- let curr_thread_id = ecx. machine . threads . active_thread ( ) ;
859
- let genmc_tid = self . exec_state . thread_id_manager . borrow ( ) . get_genmc_tid ( curr_thread_id) ;
860
846
debug ! (
861
- "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:?}" ,
847
+ "GenMC: atomic_rmw_op (op: {genmc_rmw_op:?}, rhs value: {genmc_rhs_scalar:?}), address: {address:?}, size: {size:?}, ordering: {ordering:?}" ,
862
848
) ;
863
849
let rmw_result = self . handle . borrow_mut ( ) . pin_mut ( ) . handle_read_modify_write (
864
- genmc_tid ,
850
+ self . active_thread_genmc_tid ( & ecx . machine ) ,
865
851
address. bytes ( ) ,
866
852
size. bytes ( ) ,
867
853
genmc_rmw_op,
@@ -891,11 +877,11 @@ impl GenmcCtx {
891
877
/// This may happen due to an manual `assume` statement added by a user
892
878
/// or added by some automated program transformation, e.g., for spinloops.
893
879
fn handle_assume_block < ' tcx > ( & self , machine : & MiriMachine < ' tcx > ) -> InterpResult < ' tcx > {
894
- let curr_thread = machine . threads . active_thread ( ) ;
895
- let genmc_curr_thread =
896
- self . exec_state . thread_id_manager . borrow ( ) . get_genmc_tid ( curr_thread ) ;
897
- debug ! ( "GenMC: assume statement, blocking thread {curr_thread:?} ({genmc_curr_thread:?})" ) ;
898
- self . handle . borrow_mut ( ) . pin_mut ( ) . handle_assume_block ( genmc_curr_thread ) ;
880
+ debug ! ( "GenMC: assume statement, blocking active thread." ) ;
881
+ self . handle
882
+ . borrow_mut ( )
883
+ . pin_mut ( )
884
+ . handle_assume_block ( self . active_thread_genmc_tid ( machine ) ) ;
899
885
interp_ok ( ( ) )
900
886
}
901
887
}
0 commit comments