Skip to content

Commit ca7283d

Browse files
committed
get rid of Rc in Stacked Borrows
1 parent 3a24958 commit ca7283d

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

src/machine.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub struct AllocExtra {
116116
}
117117

118118
/// Extra global memory data
119-
#[derive(Clone, Debug)]
119+
#[derive(Debug)]
120120
pub struct MemoryExtra {
121121
pub stacked_borrows: Option<stacked_borrows::MemoryExtra>,
122122
pub data_race: Option<data_race::MemoryExtra>,
@@ -144,11 +144,11 @@ impl MemoryExtra {
144144
pub fn new(config: &MiriConfig) -> Self {
145145
let rng = StdRng::seed_from_u64(config.seed.unwrap_or(0));
146146
let stacked_borrows = if config.stacked_borrows {
147-
Some(Rc::new(RefCell::new(stacked_borrows::GlobalState::new(
147+
Some(RefCell::new(stacked_borrows::GlobalState::new(
148148
config.tracked_pointer_tag,
149149
config.tracked_call_id,
150150
config.track_raw,
151-
))))
151+
)))
152152
} else {
153153
None
154154
};
@@ -478,7 +478,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
478478
let alloc = alloc.into_owned();
479479
let (stacks, base_tag) = if let Some(stacked_borrows) = &memory_extra.stacked_borrows {
480480
let (stacks, base_tag) =
481-
Stacks::new_allocation(id, alloc.size(), Rc::clone(stacked_borrows), kind);
481+
Stacks::new_allocation(id, alloc.size(), stacked_borrows, kind);
482482
(Some(stacks), base_tag)
483483
} else {
484484
// No stacks, no tag.
@@ -507,7 +507,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
507507

508508
#[inline(always)]
509509
fn memory_read(
510-
_memory_extra: &Self::MemoryExtra,
510+
memory_extra: &Self::MemoryExtra,
511511
alloc_extra: &AllocExtra,
512512
ptr: Pointer<Tag>,
513513
size: Size,
@@ -516,15 +516,15 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
516516
data_race.read(ptr, size)?;
517517
}
518518
if let Some(stacked_borrows) = &alloc_extra.stacked_borrows {
519-
stacked_borrows.memory_read(ptr, size)
519+
stacked_borrows.memory_read(ptr, size, memory_extra.stacked_borrows.as_ref().unwrap())
520520
} else {
521521
Ok(())
522522
}
523523
}
524524

525525
#[inline(always)]
526526
fn memory_written(
527-
_memory_extra: &mut Self::MemoryExtra,
527+
memory_extra: &mut Self::MemoryExtra,
528528
alloc_extra: &mut AllocExtra,
529529
ptr: Pointer<Tag>,
530530
size: Size,
@@ -533,7 +533,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
533533
data_race.write(ptr, size)?;
534534
}
535535
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
536-
stacked_borrows.memory_written(ptr, size)
536+
stacked_borrows.memory_written(ptr, size, memory_extra.stacked_borrows.as_mut().unwrap())
537537
} else {
538538
Ok(())
539539
}
@@ -553,7 +553,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
553553
data_race.deallocate(ptr, size)?;
554554
}
555555
if let Some(stacked_borrows) = &mut alloc_extra.stacked_borrows {
556-
stacked_borrows.memory_deallocated(ptr, size)
556+
stacked_borrows.memory_deallocated(ptr, size, memory_extra.stacked_borrows.as_mut().unwrap())
557557
} else {
558558
Ok(())
559559
}

src/stacked_borrows.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use std::cell::RefCell;
55
use std::fmt;
66
use std::num::NonZeroU64;
7-
use std::rc::Rc;
8-
97
use log::trace;
108

119
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -87,8 +85,6 @@ pub struct Stack {
8785
pub struct Stacks {
8886
// Even reading memory can have effects on the stack, so we need a `RefCell` here.
8987
stacks: RefCell<RangeMap<Stack>>,
90-
// Pointer to global state
91-
global: MemoryExtra,
9288
}
9389

9490
/// Extra global state, available to the memory access hooks.
@@ -112,7 +108,7 @@ pub struct GlobalState {
112108
track_raw: bool,
113109
}
114110
/// Memory extra state gives us interior mutable access to the global state.
115-
pub type MemoryExtra = Rc<RefCell<GlobalState>>;
111+
pub type MemoryExtra = RefCell<GlobalState>;
116112

117113
/// Indicates which kind of access is being performed.
118114
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
@@ -449,21 +445,21 @@ impl<'tcx> Stack {
449445
/// Map per-stack operations to higher-level per-location-range operations.
450446
impl<'tcx> Stacks {
451447
/// Creates new stack with initial tag.
452-
fn new(size: Size, perm: Permission, tag: Tag, extra: MemoryExtra) -> Self {
448+
fn new(size: Size, perm: Permission, tag: Tag) -> Self {
453449
let item = Item { perm, tag, protector: None };
454450
let stack = Stack { borrows: vec![item] };
455451

456-
Stacks { stacks: RefCell::new(RangeMap::new(size, stack)), global: extra }
452+
Stacks { stacks: RefCell::new(RangeMap::new(size, stack)) }
457453
}
458454

459455
/// Call `f` on every stack in the range.
460456
fn for_each(
461457
&self,
462458
ptr: Pointer<Tag>,
463459
size: Size,
460+
global: &GlobalState,
464461
f: impl Fn(Pointer<Tag>, &mut Stack, &GlobalState) -> InterpResult<'tcx>,
465462
) -> InterpResult<'tcx> {
466-
let global = self.global.borrow();
467463
let mut stacks = self.stacks.borrow_mut();
468464
for (offset, stack) in stacks.iter_mut(ptr.offset, size) {
469465
let mut cur_ptr = ptr;
@@ -479,16 +475,17 @@ impl Stacks {
479475
pub fn new_allocation(
480476
id: AllocId,
481477
size: Size,
482-
extra: MemoryExtra,
478+
extra: &MemoryExtra,
483479
kind: MemoryKind<MiriMemoryKind>,
484480
) -> (Self, Tag) {
481+
let mut extra = extra.borrow_mut();
485482
let (tag, perm) = match kind {
486483
// New unique borrow. This tag is not accessible by the program,
487484
// so it will only ever be used when using the local directly (i.e.,
488485
// not through a pointer). That is, whenever we directly write to a local, this will pop
489486
// everything else off the stack, invalidating all previous pointers,
490487
// and in particular, *all* raw pointers.
491-
MemoryKind::Stack => (Tag::Tagged(extra.borrow_mut().new_ptr()), Permission::Unique),
488+
MemoryKind::Stack => (Tag::Tagged(extra.new_ptr()), Permission::Unique),
492489
// `Global` memory can be referenced by global pointers from `tcx`.
493490
// Thus we call `global_base_ptr` such that the global pointers get the same tag
494491
// as what we use here.
@@ -500,38 +497,38 @@ impl Stacks {
500497
| MiriMemoryKind::ExternStatic
501498
| MiriMemoryKind::Tls
502499
| MiriMemoryKind::Env,
503-
) => (extra.borrow_mut().global_base_ptr(id), Permission::SharedReadWrite),
500+
) => (extra.global_base_ptr(id), Permission::SharedReadWrite),
504501
// Everything else we handle like raw pointers for now.
505502
_ => {
506-
let mut extra = extra.borrow_mut();
507503
let tag =
508504
if extra.track_raw { Tag::Tagged(extra.new_ptr()) } else { Tag::Untagged };
509505
(tag, Permission::SharedReadWrite)
510506
}
511507
};
512-
(Stacks::new(size, perm, tag, extra), tag)
508+
(Stacks::new(size, perm, tag), tag)
513509
}
514510

515511
#[inline(always)]
516-
pub fn memory_read<'tcx>(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
512+
pub fn memory_read<'tcx>(&self, ptr: Pointer<Tag>, size: Size, extra: &MemoryExtra) -> InterpResult<'tcx> {
517513
trace!("read access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
518-
self.for_each(ptr, size, |ptr, stack, global| stack.access(AccessKind::Read, ptr, global))
514+
self.for_each(ptr, size, &*extra.borrow(), |ptr, stack, global| stack.access(AccessKind::Read, ptr, global))
519515
}
520516

521517
#[inline(always)]
522-
pub fn memory_written<'tcx>(&mut self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
518+
pub fn memory_written<'tcx>(&mut self, ptr: Pointer<Tag>, size: Size, extra: &mut MemoryExtra) -> InterpResult<'tcx> {
523519
trace!("write access with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
524-
self.for_each(ptr, size, |ptr, stack, global| stack.access(AccessKind::Write, ptr, global))
520+
self.for_each(ptr, size, extra.get_mut(), |ptr, stack, global| stack.access(AccessKind::Write, ptr, global))
525521
}
526522

527523
#[inline(always)]
528524
pub fn memory_deallocated<'tcx>(
529525
&mut self,
530526
ptr: Pointer<Tag>,
531527
size: Size,
528+
extra: &mut MemoryExtra,
532529
) -> InterpResult<'tcx> {
533530
trace!("deallocation with tag {:?}: {:?}, size {}", ptr.tag, ptr.erase_tag(), size.bytes());
534-
self.for_each(ptr, size, |ptr, stack, global| stack.dealloc(ptr, global))
531+
self.for_each(ptr, size, extra.get_mut(), |ptr, stack, global| stack.dealloc(ptr, global))
535532
}
536533
}
537534

@@ -560,10 +557,12 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
560557
size.bytes()
561558
);
562559

563-
// Get the allocation. It might not be mutable, so we cannot use `get_mut`.
560+
// Get the allocation. We need both the allocation and the MemoryExtra, so we cannot use `&mut`.
561+
// FIXME: make `get_alloc_extra_mut` also return `&mut MemoryExtra`.
564562
let extra = this.memory.get_alloc_extra(ptr.alloc_id)?;
565563
let stacked_borrows =
566564
extra.stacked_borrows.as_ref().expect("we should have Stacked Borrows data");
565+
let global = this.memory.extra.stacked_borrows.as_ref().unwrap().borrow();
567566
// Update the stacks.
568567
// Make sure that raw pointers and mutable shared references are reborrowed "weak":
569568
// There could be existing unique pointers reborrowed from them that should remain valid!
@@ -583,14 +582,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
583582
Permission::SharedReadWrite
584583
};
585584
let item = Item { perm, tag: new_tag, protector };
586-
stacked_borrows.for_each(cur_ptr, size, |cur_ptr, stack, global| {
585+
stacked_borrows.for_each(cur_ptr, size, &*global, |cur_ptr, stack, global| {
587586
stack.grant(cur_ptr, item, global)
588587
})
589588
});
590589
}
591590
};
592591
let item = Item { perm, tag: new_tag, protector };
593-
stacked_borrows.for_each(ptr, size, |ptr, stack, global| stack.grant(ptr, item, global))
592+
stacked_borrows.for_each(ptr, size, &*global, |ptr, stack, global| stack.grant(ptr, item, global))
594593
}
595594

596595
/// Retags an indidual pointer, returning the retagged version.

0 commit comments

Comments
 (0)