Skip to content

Commit 10c4956

Browse files
committed
Auto merge of #1811 - RalfJung:less-rc, r=RalfJung
get rid of some `Rc` Now that the memory access hooks get references to `MemoryExtra`, we can avoid refcounting for the global state of Stacked Borrows and the data race detector.
2 parents 3a24958 + c73f8b1 commit 10c4956

File tree

4 files changed

+92
-75
lines changed

4 files changed

+92
-75
lines changed

src/data_race.rs

Lines changed: 31 additions & 28 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,21 +883,19 @@ 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>(
887+
&self,
888+
pointer: Pointer<Tag>,
889+
len: Size,
890+
global: &GlobalState,
891+
) -> InterpResult<'tcx> {
892+
if global.multi_threaded.get() {
893+
let (index, clocks) = global.current_thread_state();
894894
let mut alloc_ranges = self.alloc_ranges.borrow_mut();
895895
for (_, range) in alloc_ranges.iter_mut(pointer.offset, len) {
896896
if let Err(DataRace) = range.read_race_detect(&*clocks, index) {
897897
// Report data-race.
898-
return Self::report_data_race(
899-
&self.global,
900-
range,
901-
"Read",
902-
false,
903-
pointer,
904-
len,
905-
);
898+
return Self::report_data_race(global, range, "Read", false, pointer, len);
906899
}
907900
}
908901
Ok(())
@@ -917,14 +910,15 @@ impl VClockAlloc {
917910
pointer: Pointer<Tag>,
918911
len: Size,
919912
write_type: WriteType,
913+
global: &mut GlobalState,
920914
) -> InterpResult<'tcx> {
921-
if self.global.multi_threaded.get() {
922-
let (index, clocks) = self.global.current_thread_state();
915+
if global.multi_threaded.get() {
916+
let (index, clocks) = global.current_thread_state();
923917
for (_, range) in self.alloc_ranges.get_mut().iter_mut(pointer.offset, len) {
924918
if let Err(DataRace) = range.write_race_detect(&*clocks, index, write_type) {
925919
// Report data-race
926920
return Self::report_data_race(
927-
&self.global,
921+
global,
928922
range,
929923
write_type.get_descriptor(),
930924
false,
@@ -943,16 +937,26 @@ impl VClockAlloc {
943937
/// data-race threads if `multi-threaded` is false, either due to no threads
944938
/// being created or if it is temporarily disabled during a racy read or write
945939
/// operation
946-
pub fn write<'tcx>(&mut self, pointer: Pointer<Tag>, len: Size) -> InterpResult<'tcx> {
947-
self.unique_access(pointer, len, WriteType::Write)
940+
pub fn write<'tcx>(
941+
&mut self,
942+
pointer: Pointer<Tag>,
943+
len: Size,
944+
global: &mut GlobalState,
945+
) -> InterpResult<'tcx> {
946+
self.unique_access(pointer, len, WriteType::Write, global)
948947
}
949948

950949
/// Detect data-races for an unsynchronized deallocate operation, will not perform
951950
/// data-race threads if `multi-threaded` is false, either due to no threads
952951
/// being created or if it is temporarily disabled during a racy read or write
953952
/// operation
954-
pub fn deallocate<'tcx>(&mut self, pointer: Pointer<Tag>, len: Size) -> InterpResult<'tcx> {
955-
self.unique_access(pointer, len, WriteType::Deallocate)
953+
pub fn deallocate<'tcx>(
954+
&mut self,
955+
pointer: Pointer<Tag>,
956+
len: Size,
957+
global: &mut GlobalState,
958+
) -> InterpResult<'tcx> {
959+
self.unique_access(pointer, len, WriteType::Deallocate, global)
956960
}
957961
}
958962

@@ -1035,15 +1039,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: MiriEvalContextExt<'mir, 'tcx> {
10351039
);
10361040

10371041
// Perform the atomic operation.
1038-
let data_race = &alloc_meta.global;
10391042
data_race.maybe_perform_sync_operation(|index, mut clocks| {
10401043
for (_, range) in
10411044
alloc_meta.alloc_ranges.borrow_mut().iter_mut(place_ptr.offset, size)
10421045
{
10431046
if let Err(DataRace) = op(range, &mut *clocks, index, atomic) {
10441047
mem::drop(clocks);
10451048
return VClockAlloc::report_data_race(
1046-
&alloc_meta.global,
1049+
data_race,
10471050
range,
10481051
description,
10491052
true,

src/machine.rs

Lines changed: 22 additions & 18 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;
@@ -116,7 +115,7 @@ pub struct AllocExtra {
116115
}
117116

118117
/// Extra global memory data
119-
#[derive(Clone, Debug)]
118+
#[derive(Debug)]
120119
pub struct MemoryExtra {
121120
pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
122121
pub data_race: Option<data_race::MemoryExtra>,
@@ -144,19 +143,16 @@ impl MemoryExtra {
144143
pub fn new(config: &MiriConfig) -> Self {
145144
let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
146145
let stacked_borrows = if config.stacked_borrows {
147-
Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(
146+
Some(RefCell::new(stacked_borrows::GlobalState::new(
148147
config.tracked_pointer_tag,
149148
config.tracked_call_id,
150149
config.track_raw,
151-
))))
152-
} else {
153-
None
154-
};
155-
let data_race = if config.data_race_detector {
156-
Some(Rc::new(data_race::GlobalState::new()))
150+
)))
157151
} else {
158152
None
159153
};
154+
let data_race =
155+
if config.data_race_detector { Some(data_race::GlobalState::new()) } else { None };
160156
MemoryExtra {
161157
stacked_borrows,
162158
data_race,
@@ -478,7 +474,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
478474
let alloc = alloc.into_owned();
479475
let (stacks, base_tag) = if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
480476
let (stacks, base_tag) =
481-
Stacks::new_allocation(id, alloc.size(), Rc::clone(stacked_borrows), kind);
477+
Stacks::new_allocation(id, alloc.size(), stacked_borrows, kind);
482478
(Some(stacks), base_tag)
483479
} else {
484480
// No stacks, no tag.
@@ -507,33 +503,37 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
507503

508504
#[inline(always)]
509505
fn memory_read(
510-
_memory_extra: &Self::MemoryExtra,
506+
memory_extra: &Self::MemoryExtra,
511507
alloc_extra: &AllocExtra,
512508
ptr: Pointer<Tag>,
513509
size: Size,
514510
) -> InterpResult<'tcx> {
515511
if let Some(data_race) = &alloc_extra.data_race {
516-
data_race.read(ptr, size)?;
512+
data_race.read(ptr, size, memory_extra.data_race.as_ref().unwrap())?;
517513
}
518514
if let Some(stacked_borrows) = &alloc_extra.stacked_borrows {
519-
stacked_borrows.memory_read(ptr, size)
515+
stacked_borrows.memory_read(ptr, size, memory_extra.stacked_borrows.as_ref().unwrap())
520516
} else {
521517
Ok(())
522518
}
523519
}
524520

525521
#[inline(always)]
526522
fn memory_written(
527-
_memory_extra: &mut Self::MemoryExtra,
523+
memory_extra: &mut Self::MemoryExtra,
528524
alloc_extra: &mut AllocExtra,
529525
ptr: Pointer<Tag>,
530526
size: Size,
531527
) -> InterpResult<'tcx> {
532528
if let Some(data_race) = &mut alloc_extra.data_race {
533-
data_race.write(ptr, size)?;
529+
data_race.write(ptr, size, memory_extra.data_race.as_mut().unwrap())?;
534530
}
535531
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
536-
stacked_borrows.memory_written(ptr, size)
532+
stacked_borrows.memory_written(
533+
ptr,
534+
size,
535+
memory_extra.stacked_borrows.as_mut().unwrap(),
536+
)
537537
} else {
538538
Ok(())
539539
}
@@ -550,10 +550,14 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
550550
register_diagnostic(NonHaltingDiagnostic::FreedAlloc(ptr.alloc_id));
551551
}
552552
if let Some(data_race) = &mut alloc_extra.data_race {
553-
data_race.deallocate(ptr, size)?;
553+
data_race.deallocate(ptr, size, memory_extra.data_race.as_mut().unwrap())?;
554554
}
555555
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
556-
stacked_borrows.memory_deallocated(ptr, size)
556+
stacked_borrows.memory_deallocated(
557+
ptr,
558+
size,
559+
memory_extra.stacked_borrows.as_mut().unwrap(),
560+
)
557561
} else {
558562
Ok(())
559563
}

0 commit comments

Comments
 (0)