Skip to content

Commit 1bbd6e6

Browse files
committed
get rid of Rc in data_race
1 parent ca7283d commit 1bbd6e6

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

src/data_race.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use std::{
6565
cell::{Cell, Ref, RefCell, RefMut},
6666
fmt::Debug,
6767
mem,
68-
rc::Rc,
6968
};
7069

7170
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -80,7 +79,7 @@ use crate::{
8079
};
8180

8281
pub type AllocExtra = VClockAlloc;
83-
pub type MemoryExtra = Rc<GlobalState>;
82+
pub type MemoryExtra = GlobalState;
8483

8584
/// Valid atomic read-write operations, alias of atomic::Ordering (not non-exhaustive).
8685
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -488,7 +487,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
488487
) -> InterpResult<'tcx, ScalarMaybeUninit<Tag>> {
489488
let this = self.eval_context_ref();
490489
let scalar = this.allow_data_races_ref(move |this| this.read_scalar(&place.into()))?;
491-
self.validate_atomic_load(place, atomic)?;
490+
this.validate_atomic_load(place, atomic)?;
492491
Ok(scalar)
493492
}
494493

@@ -501,7 +500,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
501500
) -> InterpResult<'tcx> {
502501
let this = self.eval_context_mut();
503502
this.allow_data_races_mut(move |this| this.write_scalar(val, &(*dest).into()))?;
504-
self.validate_atomic_store(dest, atomic)
503+
this.validate_atomic_store(dest, atomic)
505504
}
506505

507506
/// Perform a atomic operation on a memory location.
@@ -733,9 +732,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
733732
pub struct VClockAlloc {
734733
/// Assigning each byte a MemoryCellClocks.
735734
alloc_ranges: RefCell<RangeMap<MemoryCellClocks>>,
736-
737-
/// Pointer to global state.
738-
global: MemoryExtra,
739735
}
740736

741737
impl VClockAlloc {
@@ -767,7 +763,6 @@ impl VClockAlloc {
767763
| MemoryKind::Vtable => (0, VectorIdx::MAX_INDEX),
768764
};
769765
VClockAlloc {
770-
global: Rc::clone(global),
771766
alloc_ranges: RefCell::new(RangeMap::new(
772767
len,
773768
MemoryCellClocks::new(alloc_timestamp, alloc_index),
@@ -888,15 +883,15 @@ impl VClockAlloc {
888883
/// being created or if it is temporarily disabled during a racy read or write
889884
/// operation for which data-race detection is handled separately, for example
890885
/// atomic read operations.
891-
pub fn read<'tcx>(&self, pointer: Pointer<Tag>, len: Size) -> InterpResult<'tcx> {
892-
if self.global.multi_threaded.get() {
893-
let (index, clocks) = self.global.current_thread_state();
886+
pub fn read<'tcx>(&self, pointer: Pointer<Tag>, len: Size, global: &GlobalState) -> InterpResult<'tcx> {
887+
if global.multi_threaded.get() {
888+
let (index, clocks) = global.current_thread_state();
894889
let mut alloc_ranges = self.alloc_ranges.borrow_mut();
895890
for (_, range) in alloc_ranges.iter_mut(pointer.offset, len) {
896891
if let Err(DataRace) = range.read_race_detect(&*clocks, index) {
897892
// Report data-race.
898893
return Self::report_data_race(
899-
&self.global,
894+
global,
900895
range,
901896
"Read",
902897
false,
@@ -917,14 +912,15 @@ impl VClockAlloc {
917912
pointer: Pointer<Tag>,
918913
len: Size,
919914
write_type: WriteType,
915+
global: &mut GlobalState,
920916
) -> InterpResult<'tcx> {
921-
if self.global.multi_threaded.get() {
922-
let (index, clocks) = self.global.current_thread_state();
917+
if global.multi_threaded.get() {
918+
let (index, clocks) = global.current_thread_state();
923919
for (_, range) in self.alloc_ranges.get_mut().iter_mut(pointer.offset, len) {
924920
if let Err(DataRace) = range.write_race_detect(&*clocks, index, write_type) {
925921
// Report data-race
926922
return Self::report_data_race(
927-
&self.global,
923+
global,
928924
range,
929925
write_type.get_descriptor(),
930926
false,
@@ -943,16 +939,16 @@ impl VClockAlloc {
943939
/// data-race threads if `multi-threaded` is false, either due to no threads
944940
/// being created or if it is temporarily disabled during a racy read or write
945941
/// operation
946-
pub fn write<'tcx>(&mut self, pointer: Pointer<Tag>, len: Size) -> InterpResult<'tcx> {
947-
self.unique_access(pointer, len, WriteType::Write)
942+
pub fn write<'tcx>(&mut self, pointer: Pointer<Tag>, len: Size, global: &mut GlobalState) -> InterpResult<'tcx> {
943+
self.unique_access(pointer, len, WriteType::Write, global)
948944
}
949945

950946
/// Detect data-races for an unsynchronized deallocate operation, will not perform
951947
/// data-race threads if `multi-threaded` is false, either due to no threads
952948
/// being created or if it is temporarily disabled during a racy read or write
953949
/// operation
954-
pub fn deallocate<'tcx>(&mut self, pointer: Pointer<Tag>, len: Size) -> InterpResult<'tcx> {
955-
self.unique_access(pointer, len, WriteType::Deallocate)
950+
pub fn deallocate<'tcx>(&mut self, pointer: Pointer<Tag>, len: Size, global: &mut GlobalState) -> InterpResult<'tcx> {
951+
self.unique_access(pointer, len, WriteType::Deallocate, global)
956952
}
957953
}
958954

@@ -1035,15 +1031,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
10351031
);
10361032

10371033
// Perform the atomic operation.
1038-
let data_race = &alloc_meta.global;
10391034
data_race.maybe_perform_sync_operation(|index, mut clocks| {
10401035
for (_, range) in
10411036
alloc_meta.alloc_ranges.borrow_mut().iter_mut(place_ptr.offset, size)
10421037
{
10431038
if let Err(DataRace) = op(range, &mut *clocks, index, atomic) {
10441039
mem::drop(clocks);
10451040
return VClockAlloc::report_data_race(
1046-
&alloc_meta.global,
1041+
data_race,
10471042
range,
10481043
description,
10491044
true,

src/machine.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::borrow::Cow;
55
use std::cell::RefCell;
66
use std::fmt;
77
use std::num::NonZeroU64;
8-
use std::rc::Rc;
98
use std::time::Instant;
109

1110
use log::trace;
@@ -153,7 +152,7 @@ impl MemoryExtra {
153152
None
154153
};
155154
let data_race = if config.data_race_detector {
156-
Some(Rc::new(data_race::GlobalState::new()))
155+
Some(data_race::GlobalState::new())
157156
} else {
158157
None
159158
};
@@ -513,7 +512,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
513512
size: Size,
514513
) -> InterpResult<'tcx> {
515514
if let Some(data_race) = &alloc_extra.data_race {
516-
data_race.read(ptr, size)?;
515+
data_race.read(ptr, size, memory_extra.data_race.as_ref().unwrap())?;
517516
}
518517
if let Some(stacked_borrows) = &alloc_extra.stacked_borrows {
519518
stacked_borrows.memory_read(ptr, size, memory_extra.stacked_borrows.as_ref().unwrap())
@@ -530,7 +529,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
530529
size: Size,
531530
) -> InterpResult<'tcx> {
532531
if let Some(data_race) = &mut alloc_extra.data_race {
533-
data_race.write(ptr, size)?;
532+
data_race.write(ptr, size, memory_extra.data_race.as_mut().unwrap())?;
534533
}
535534
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
536535
stacked_borrows.memory_written(ptr, size, memory_extra.stacked_borrows.as_mut().unwrap())
@@ -550,7 +549,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
550549
register_diagnostic(NonHaltingDiagnostic::FreedAlloc(ptr.alloc_id));
551550
}
552551
if let Some(data_race) = &mut alloc_extra.data_race {
553-
data_race.deallocate(ptr, size)?;
552+
data_race.deallocate(ptr, size, memory_extra.data_race.as_mut().unwrap())?;
554553
}
555554
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
556555
stacked_borrows.memory_deallocated(ptr, size, memory_extra.stacked_borrows.as_mut().unwrap())

src/thread.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::cell::RefCell;
44
use std::collections::hash_map::Entry;
55
use std::convert::TryFrom;
66
use std::num::TryFromIntError;
7-
use std::rc::Rc;
87
use std::time::{Duration, Instant, SystemTime};
98

109
use log::trace;
@@ -333,7 +332,7 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
333332
fn join_thread(
334333
&mut self,
335334
joined_thread_id: ThreadId,
336-
data_race: &Option<Rc<data_race::GlobalState>>,
335+
data_race: &Option<data_race::GlobalState>,
337336
) -> InterpResult<'tcx> {
338337
if self.threads[joined_thread_id].join_status != ThreadJoinStatus::Joinable {
339338
throw_ub_format!("trying to join a detached or already joined thread");
@@ -439,7 +438,7 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
439438
/// The `AllocId` that can now be freed is returned.
440439
fn thread_terminated(
441440
&mut self,
442-
data_race: &Option<Rc<data_race::GlobalState>>,
441+
data_race: &Option<data_race::GlobalState>,
443442
) -> Vec<AllocId> {
444443
let mut free_tls_statics = Vec::new();
445444
{
@@ -481,7 +480,7 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
481480
/// blocked, terminated, or has explicitly asked to be preempted).
482481
fn schedule(
483482
&mut self,
484-
data_race: &Option<Rc<data_race::GlobalState>>,
483+
data_race: &Option<data_race::GlobalState>,
485484
) -> InterpResult<'tcx, SchedulingAction> {
486485
// Check whether the thread has **just** terminated (`check_terminated`
487486
// checks whether the thread has popped all its stack and if yes, sets

0 commit comments

Comments
 (0)