Skip to content

Commit e557755

Browse files
committed
Add helper function for getting GenMC thread ID.
1 parent 7d52e27 commit e557755

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
@@ -200,6 +200,14 @@ impl GenmcCtx {
200200
fn get_alloc_data_races(&self) -> bool {
201201
self.exec_state.allow_data_races.get()
202202
}
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+
}
203211
}
204212

205213
/// 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 {
309317
ordering: AtomicFenceOrd,
310318
) -> InterpResult<'tcx> {
311319
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());
318324
interp_ok(())
319325
}
320326

@@ -425,12 +431,8 @@ impl GenmcCtx {
425431
debug!(
426432
"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}"
427433
);
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-
432434
let cas_result = self.handle.borrow_mut().pin_mut().handle_compare_exchange(
433-
genmc_tid,
435+
self.active_thread_genmc_tid(&ecx.machine),
434436
address.bytes(),
435437
size.bytes(),
436438
scalar_to_genmc_scalar(ecx, self, expected_old_value)?,
@@ -591,14 +593,10 @@ impl GenmcCtx {
591593
return ecx
592594
.get_global_allocation_address(&self.global_state.global_allocations, alloc_id);
593595
}
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);
597596
// GenMC doesn't support ZSTs, so we set the minimum size to 1 byte
598597
let genmc_size = size.bytes().max(1);
599-
600598
let chosen_address = self.handle.borrow_mut().pin_mut().handle_malloc(
601-
genmc_tid,
599+
self.active_thread_genmc_tid(machine),
602600
genmc_size,
603601
alignment.bytes(),
604602
);
@@ -632,11 +630,12 @@ impl GenmcCtx {
632630
!self.get_alloc_data_races(),
633631
"memory deallocation with data race checking disabled."
634632
);
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+
{
640639
// FIXME(genmc): improve error handling.
641640
// An error was detected, so we get the error string from GenMC.
642641
throw_ub_format!("{}", self.try_get_error().unwrap());
@@ -690,7 +689,7 @@ impl GenmcCtx {
690689
let genmc_tid = thread_infos.get_genmc_tid(curr_thread_id);
691690

692691
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.
694693
self.handle.borrow_mut().pin_mut().handle_thread_finish(genmc_tid, /* ret_val */ 0);
695694
}
696695

@@ -752,17 +751,12 @@ impl GenmcCtx {
752751
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes.",
753752
);
754753
}
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-
759754
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?}",
761756
addr = address.bytes()
762757
);
763-
764758
let load_result = self.handle.borrow_mut().pin_mut().handle_load(
765-
genmc_tid,
759+
self.active_thread_genmc_tid(machine),
766760
address.bytes(),
767761
size.bytes(),
768762
memory_ordering,
@@ -803,17 +797,12 @@ impl GenmcCtx {
803797
"GenMC mode currently does not support atomics larger than {MAX_ACCESS_SIZE} bytes."
804798
);
805799
}
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-
810800
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:?}",
812802
addr = address.bytes()
813803
);
814-
815804
let store_result = self.handle.borrow_mut().pin_mut().handle_store(
816-
genmc_tid,
805+
self.active_thread_genmc_tid(machine),
817806
address.bytes(),
818807
size.bytes(),
819808
genmc_value,
@@ -854,14 +843,11 @@ impl GenmcCtx {
854843
MAX_ACCESS_SIZE,
855844
size.bytes()
856845
);
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);
860846
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:?}",
862848
);
863849
let rmw_result = self.handle.borrow_mut().pin_mut().handle_read_modify_write(
864-
genmc_tid,
850+
self.active_thread_genmc_tid(&ecx.machine),
865851
address.bytes(),
866852
size.bytes(),
867853
genmc_rmw_op,
@@ -891,11 +877,11 @@ impl GenmcCtx {
891877
/// This may happen due to an manual `assume` statement added by a user
892878
/// or added by some automated program transformation, e.g., for spinloops.
893879
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));
899885
interp_ok(())
900886
}
901887
}

0 commit comments

Comments
 (0)