Skip to content

Commit 89d0ccc

Browse files
committed
Add helper function for getting GenMC thread ID.
1 parent 0752a12 commit 89d0ccc

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

src/concurrency/genmc/intercept.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
3333
// FIXME(genmc): assert that we have at least 1 byte.
3434
// FIXME(genmc): maybe use actual size of mutex here?.
3535

36-
let thread_infos = genmc_ctx.exec_state.thread_id_manager.borrow();
37-
let curr_thread = this.machine.threads.active_thread();
38-
let genmc_curr_thread = thread_infos.get_genmc_tid(curr_thread);
36+
let genmc_curr_thread = genmc_ctx.active_thread_genmc_tid(&this.machine);
3937
interp_ok((genmc_curr_thread, addr, 1))
4038
};
4139

src/concurrency/genmc/mod.rs

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ impl GenmcCtx {
210210
fn get_alloc_data_races(&self) -> bool {
211211
self.exec_state.allow_data_races.get()
212212
}
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+
}
213221
}
214222

215223
/// 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 {
319327
ordering: AtomicFenceOrd,
320328
) -> InterpResult<'tcx> {
321329
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());
328334
interp_ok(())
329335
}
330336

@@ -438,12 +444,8 @@ impl GenmcCtx {
438444
debug!(
439445
"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}"
440446
);
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-
445447
let cas_result = self.handle.borrow_mut().pin_mut().handle_compare_exchange(
446-
genmc_tid,
448+
self.active_thread_genmc_tid(&ecx.machine),
447449
address.bytes(),
448450
size.bytes(),
449451
scalar_to_genmc_scalar(ecx, self, expected_old_value)?,
@@ -604,14 +606,10 @@ impl GenmcCtx {
604606
return ecx
605607
.get_global_allocation_address(&self.global_state.global_allocations, alloc_id);
606608
}
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);
610609
// GenMC doesn't support ZSTs, so we set the minimum size to 1 byte
611610
let genmc_size = size.bytes().max(1);
612-
613611
let chosen_address = self.handle.borrow_mut().pin_mut().handle_malloc(
614-
genmc_tid,
612+
self.active_thread_genmc_tid(machine),
615613
genmc_size,
616614
alignment.bytes(),
617615
);
@@ -645,11 +643,12 @@ impl GenmcCtx {
645643
!self.get_alloc_data_races(),
646644
"memory deallocation with data race checking disabled."
647645
);
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+
{
653652
// FIXME(genmc): improve error handling.
654653
// An error was detected, so we get the error string from GenMC.
655654
throw_ub_format!("{}", self.try_get_error().unwrap());
@@ -703,7 +702,7 @@ impl GenmcCtx {
703702
let genmc_tid = thread_infos.get_genmc_tid(curr_thread_id);
704703

705704
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.
707706
self.handle.borrow_mut().pin_mut().handle_thread_finish(genmc_tid, /* ret_val */ 0);
708707
}
709708

@@ -765,17 +764,12 @@ impl GenmcCtx {
765764
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes.",
766765
);
767766
}
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-
772767
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?}",
774769
addr = address.bytes()
775770
);
776-
777771
let load_result = self.handle.borrow_mut().pin_mut().handle_load(
778-
genmc_tid,
772+
self.active_thread_genmc_tid(machine),
779773
address.bytes(),
780774
size.bytes(),
781775
memory_ordering,
@@ -816,17 +810,12 @@ impl GenmcCtx {
816810
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes."
817811
);
818812
}
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-
823813
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:?}",
825815
addr = address.bytes()
826816
);
827-
828817
let store_result = self.handle.borrow_mut().pin_mut().handle_store(
829-
genmc_tid,
818+
self.active_thread_genmc_tid(machine),
830819
address.bytes(),
831820
size.bytes(),
832821
genmc_value,
@@ -867,14 +856,11 @@ impl GenmcCtx {
867856
MAX_ACCESS_SIZE,
868857
size.bytes()
869858
);
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);
873859
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:?}",
875861
);
876862
let rmw_result = self.handle.borrow_mut().pin_mut().handle_read_modify_write(
877-
genmc_tid,
863+
self.active_thread_genmc_tid(&ecx.machine),
878864
address.bytes(),
879865
size.bytes(),
880866
genmc_rmw_op,
@@ -904,11 +890,11 @@ impl GenmcCtx {
904890
/// This may happen due to an manual `assume` statement added by a user
905891
/// or added by some automated program transformation, e.g., for spinloops.
906892
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));
912898
interp_ok(())
913899
}
914900
}

0 commit comments

Comments
 (0)