Skip to content

Commit be00557

Browse files
committed
Add a flag to check the validity of a Stack
1 parent 3cd53eb commit be00557

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

src/bin/miri.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ fn main() {
397397
);
398398
miri_config.tag_raw = true;
399399
}
400+
"-Zmiri-verify-stack-consistency" => {
401+
miri_config.verify_stack_cache = true;
402+
}
400403
"--" => {
401404
after_dashdash = true;
402405
}

src/eval.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ pub struct MiriConfig {
119119
/// Whether to ignore any output by the program. This is helpful when debugging miri
120120
/// as its messages don't get intermingled with the program messages.
121121
pub mute_stdout_stderr: bool,
122+
/// Whether we should check the internal validity requirements of the Stack data structure
123+
/// itself (in addition to checking the program being executed).
124+
pub verify_stack_cache: bool,
122125
}
123126

124127
impl Default for MiriConfig {
@@ -146,6 +149,7 @@ impl Default for MiriConfig {
146149
backtrace_style: BacktraceStyle::Short,
147150
strict_provenance: false,
148151
mute_stdout_stderr: false,
152+
verify_stack_cache: false,
149153
}
150154
}
151155
}

src/machine.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ pub struct Evaluator<'mir, 'tcx> {
295295

296296
/// Corresponds to -Zmiri-mute-stdout-stderr and doesn't write the output but acts as if it succeeded.
297297
pub(crate) mute_stdout_stderr: bool,
298+
299+
/// Controls whether the borrow stack checks the integrity of its internal caches.
300+
pub(crate) verify_stack_cache: bool,
298301
}
299302

300303
impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
@@ -349,6 +352,7 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
349352
check_alignment: config.check_alignment,
350353
cmpxchg_weak_failure_rate: config.cmpxchg_weak_failure_rate,
351354
mute_stdout_stderr: config.mute_stdout_stderr,
355+
verify_stack_cache: config.verify_stack_cache,
352356
}
353357
}
354358

@@ -585,6 +589,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
585589
stacked_borrows,
586590
kind,
587591
ecx.machine.current_span(),
592+
ecx.machine.verify_stack_cache,
588593
))
589594
} else {
590595
None

src/stacked_borrows.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,9 @@ impl<'tcx> Stack {
503503
/// Map per-stack operations to higher-level per-location-range operations.
504504
impl<'tcx> Stacks {
505505
/// Creates new stack with initial tag.
506-
fn new(size: Size, perm: Permission, tag: SbTag) -> Self {
506+
fn new(size: Size, perm: Permission, tag: SbTag, verify: bool) -> Self {
507507
let item = Item { perm, tag, protector: None };
508-
let stack = Stack::new(item);
508+
let stack = Stack::new(item, verify);
509509
Stacks {
510510
stacks: RefCell::new(RangeMap::new(size, stack)),
511511
history: RefCell::new(AllocHistory::new()),
@@ -549,6 +549,7 @@ impl Stacks {
549549
state: &GlobalState,
550550
kind: MemoryKind<MiriMemoryKind>,
551551
mut current_span: CurrentSpan<'_, '_, '_>,
552+
verify: bool,
552553
) -> Self {
553554
let mut extra = state.borrow_mut();
554555
let (base_tag, perm) = match kind {
@@ -582,7 +583,7 @@ impl Stacks {
582583
(tag, Permission::SharedReadWrite)
583584
}
584585
};
585-
let stacks = Stacks::new(size, perm, base_tag);
586+
let stacks = Stacks::new(size, perm, base_tag, verify);
586587
stacks.history.borrow_mut().log_creation(
587588
None,
588589
base_tag,

src/stacked_borrows/stack.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Stack {
1717
/// On a read, we need to disable all `Unique` above the granting item. We can avoid most of
1818
/// this scan by keeping track of the region of the borrow stack that may contain `Unique`s.
1919
unique_range: Range<usize>,
20-
validate_cache: bool,
20+
verify_cache: bool,
2121
}
2222

2323
#[derive(Clone, Debug)]
@@ -49,7 +49,7 @@ impl Stack {
4949
/// - The StackCache indices don't refer to the parallel tags,
5050
/// - If top_untagged is Some it is the topmost Untagged
5151
/// - There are no Unique tags outside of first_unique..last_unique
52-
fn check_cache_validity(&self) {
52+
fn verify_cache_consistency(&self) {
5353
for (tag, stack_idx) in self.cache.tags.iter().zip(self.cache.idx.iter()) {
5454
assert_eq!(self.borrows[*stack_idx].tag, *tag);
5555
}
@@ -72,8 +72,8 @@ impl Stack {
7272
/// Find the item granting the given kind of access to the given tag, and return where
7373
/// it is on the stack.
7474
pub fn find_granting(&mut self, access: AccessKind, tag: SbTag) -> Option<usize> {
75-
if self.validate_cache {
76-
self.check_cache_validity();
75+
if self.verify_cache {
76+
self.verify_cache_consistency();
7777
}
7878

7979
match tag {
@@ -111,8 +111,8 @@ impl Stack {
111111
}
112112

113113
pub fn insert(&mut self, new_idx: usize, new: Item) {
114-
if self.validate_cache {
115-
self.check_cache_validity();
114+
if self.verify_cache {
115+
self.verify_cache_consistency();
116116
}
117117

118118
self.borrows.insert(new_idx, new);
@@ -152,13 +152,13 @@ impl Stack {
152152
}
153153
}
154154

155-
pub fn new(item: Item) -> Self {
155+
pub fn new(item: Item, verify: bool) -> Self {
156156
Stack {
157157
borrows: vec![item],
158158
cache: StackCache { idx: [0; CACHE_LEN], tags: [item.tag; CACHE_LEN] },
159159
unique_range: if item.perm == Permission::Unique { 0..1 } else { 0..0 },
160160
top_untagged: None,
161-
validate_cache: false,
161+
verify_cache: verify,
162162
}
163163
}
164164

@@ -175,8 +175,8 @@ impl Stack {
175175
granting_idx: usize,
176176
mut visitor: V,
177177
) -> crate::InterpResult<'static> {
178-
if self.validate_cache {
179-
self.check_cache_validity();
178+
if self.verify_cache {
179+
self.verify_cache_consistency();
180180
}
181181

182182
if granting_idx < self.unique_range.end {
@@ -205,8 +205,8 @@ impl Stack {
205205
}
206206

207207
pub fn drain_rev(&mut self, range: std::ops::Range<usize>) -> DrainRev<'_> {
208-
if self.validate_cache {
209-
self.check_cache_validity();
208+
if self.verify_cache {
209+
self.verify_cache_consistency();
210210
}
211211

212212
DrainRev { start: range.start, stack: self, cursor: range.end - 1 }

0 commit comments

Comments
 (0)