Skip to content

Commit a3460e2

Browse files
committed
Auto merge of rust-lang#3732 - JoJoDeveloping:tree-borrows-protector-end-write, r=RalfJung
TB: Refine protector end semantics Tree Borrows has protector end tag semantics, namely that protectors ending cause a [special implicit read](https://perso.crans.org/vanille/treebor/diff.0.html) on all locations protected by that protector that have actually been accessed. See also rust-lang#3067. While this is enough for ensuring protectors allow adding/reordering reads, it does not prove that one can reorder writes. For this, we need to make this stronger, by making this implicit read be a write in cases when there was a write to the location protected by that protector, i.e. if the permission is `Active`. There is a test that shows why this behavior is necessary, see `tests/fail/tree_borrows/protector-write-lazy.rs`.
2 parents 4a4a81a + 02bec40 commit a3460e2

File tree

6 files changed

+100
-36
lines changed

6 files changed

+100
-36
lines changed

src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum AccessCause {
1919
Explicit(AccessKind),
2020
Reborrow,
2121
Dealloc,
22-
FnExit,
22+
FnExit(AccessKind),
2323
}
2424

2525
impl fmt::Display for AccessCause {
@@ -28,7 +28,11 @@ impl fmt::Display for AccessCause {
2828
Self::Explicit(kind) => write!(f, "{kind}"),
2929
Self::Reborrow => write!(f, "reborrow"),
3030
Self::Dealloc => write!(f, "deallocation"),
31-
Self::FnExit => write!(f, "protector release"),
31+
// This is dead code, since the protector release access itself can never
32+
// cause UB (while the protector is active, if some other access invalidates
33+
// further use of the protected tag, that is immediate UB).
34+
// Describing the cause of UB is the only time this function is called.
35+
Self::FnExit(_) => unreachable!("protector accesses can never be the source of UB"),
3236
}
3337
}
3438
}
@@ -40,7 +44,7 @@ impl AccessCause {
4044
Self::Explicit(kind) => format!("{rel} {kind}"),
4145
Self::Reborrow => format!("reborrow (acting as a {rel} read access)"),
4246
Self::Dealloc => format!("deallocation (acting as a {rel} write access)"),
43-
Self::FnExit => format!("protector release (acting as a {rel} read access)"),
47+
Self::FnExit(kind) => format!("protector release (acting as a {rel} {kind})"),
4448
}
4549
}
4650
}

src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ impl<'tcx> Tree {
6868
let global = machine.borrow_tracker.as_ref().unwrap();
6969
let span = machine.current_span();
7070
self.perform_access(
71-
access_kind,
7271
tag,
73-
Some(range),
72+
Some((range, access_kind, diagnostics::AccessCause::Explicit(access_kind))),
7473
global,
7574
alloc_id,
7675
span,
77-
diagnostics::AccessCause::Explicit(access_kind),
7876
)
7977
}
8078

@@ -115,15 +113,8 @@ impl<'tcx> Tree {
115113
alloc_id: AllocId, // diagnostics
116114
) -> InterpResult<'tcx> {
117115
let span = machine.current_span();
118-
self.perform_access(
119-
AccessKind::Read,
120-
tag,
121-
None, // no specified range because it occurs on the entire allocation
122-
global,
123-
alloc_id,
124-
span,
125-
diagnostics::AccessCause::FnExit,
126-
)
116+
// `None` makes it the magic on-protector-end operation
117+
self.perform_access(tag, None, global, alloc_id, span)
127118
}
128119
}
129120

@@ -297,13 +288,11 @@ trait EvalContextPrivExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
297288

298289
// All reborrows incur a (possibly zero-sized) read access to the parent
299290
tree_borrows.perform_access(
300-
AccessKind::Read,
301291
orig_tag,
302-
Some(range),
292+
Some((range, AccessKind::Read, diagnostics::AccessCause::Reborrow)),
303293
this.machine.borrow_tracker.as_ref().unwrap(),
304294
alloc_id,
305295
this.machine.current_span(),
306-
diagnostics::AccessCause::Reborrow,
307296
)?;
308297
// Record the parent-child pair in the tree.
309298
tree_borrows.new_child(orig_tag, new_tag, new_perm.initial_state, range, span)?;

src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs

+4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ impl Permission {
186186
pub fn is_disabled(&self) -> bool {
187187
self.inner == Disabled
188188
}
189+
/// Check if `self` is the post-child-write state of a pointer (is `Active`).
190+
pub fn is_active(&self) -> bool {
191+
self.inner == Active
192+
}
189193

190194
/// Default initial permission of the root of a new tree at inbounds positions.
191195
/// Must *only* be used for the root, this is not in general an "initial" permission!

src/tools/miri/src/borrow_tracker/tree_borrows/tree.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,11 @@ impl<'tcx> Tree {
530530
span: Span, // diagnostics
531531
) -> InterpResult<'tcx> {
532532
self.perform_access(
533-
AccessKind::Write,
534533
tag,
535-
Some(access_range),
534+
Some((access_range, AccessKind::Write, diagnostics::AccessCause::Dealloc)),
536535
global,
537536
alloc_id,
538537
span,
539-
diagnostics::AccessCause::Dealloc,
540538
)?;
541539
for (perms_range, perms) in self.rperms.iter_mut(access_range.start, access_range.size) {
542540
TreeVisitor { nodes: &mut self.nodes, tag_mapping: &self.tag_mapping, perms }
@@ -570,12 +568,16 @@ impl<'tcx> Tree {
570568
}
571569

572570
/// Map the per-node and per-location `LocationState::perform_access`
573-
/// to each location of `access_range`, on every tag of the allocation.
571+
/// to each location of the first component of `access_range_and_kind`,
572+
/// on every tag of the allocation.
574573
///
575-
/// If `access_range` is `None`, this is interpreted as the special
574+
/// If `access_range_and_kind` is `None`, this is interpreted as the special
576575
/// access that is applied on protector release:
577576
/// - the access will be applied only to initialized locations of the allocation,
578-
/// - and it will not be visible to children.
577+
/// - it will not be visible to children,
578+
/// - it will be recorded as a `FnExit` diagnostic access
579+
/// - and it will be a read except if the location is `Active`, i.e. has been written to,
580+
/// in which case it will be a write.
579581
///
580582
/// `LocationState::perform_access` will take care of raising transition
581583
/// errors and updating the `initialized` status of each location,
@@ -585,13 +587,11 @@ impl<'tcx> Tree {
585587
/// - recording the history.
586588
pub fn perform_access(
587589
&mut self,
588-
access_kind: AccessKind,
589590
tag: BorTag,
590-
access_range: Option<AllocRange>,
591+
access_range_and_kind: Option<(AllocRange, AccessKind, diagnostics::AccessCause)>,
591592
global: &GlobalState,
592-
alloc_id: AllocId, // diagnostics
593-
span: Span, // diagnostics
594-
access_cause: diagnostics::AccessCause, // diagnostics
593+
alloc_id: AllocId, // diagnostics
594+
span: Span, // diagnostics
595595
) -> InterpResult<'tcx> {
596596
use std::ops::Range;
597597
// Performs the per-node work:
@@ -605,6 +605,8 @@ impl<'tcx> Tree {
605605
// `perms_range` is only for diagnostics (it is the range of
606606
// the `RangeMap` on which we are currently working).
607607
let node_app = |perms_range: Range<u64>,
608+
access_kind: AccessKind,
609+
access_cause: diagnostics::AccessCause,
608610
args: NodeAppArgs<'_>|
609611
-> Result<ContinueTraversal, TransitionError> {
610612
let NodeAppArgs { node, mut perm, rel_pos } = args;
@@ -618,14 +620,13 @@ impl<'tcx> Tree {
618620

619621
let protected = global.borrow().protected_tags.contains_key(&node.tag);
620622
let transition = old_state.perform_access(access_kind, rel_pos, protected)?;
621-
622623
// Record the event as part of the history
623624
if !transition.is_noop() {
624625
node.debug_info.history.push(diagnostics::Event {
625626
transition,
626627
is_foreign: rel_pos.is_foreign(),
627628
access_cause,
628-
access_range,
629+
access_range: access_range_and_kind.map(|x| x.0),
629630
transition_range: perms_range,
630631
span,
631632
});
@@ -636,6 +637,7 @@ impl<'tcx> Tree {
636637
// Error handler in case `node_app` goes wrong.
637638
// Wraps the faulty transition in more context for diagnostics.
638639
let err_handler = |perms_range: Range<u64>,
640+
access_cause: diagnostics::AccessCause,
639641
args: ErrHandlerArgs<'_, TransitionError>|
640642
-> InterpError<'tcx> {
641643
let ErrHandlerArgs { error_kind, conflicting_info, accessed_info } = args;
@@ -650,16 +652,16 @@ impl<'tcx> Tree {
650652
.build()
651653
};
652654

653-
if let Some(access_range) = access_range {
655+
if let Some((access_range, access_kind, access_cause)) = access_range_and_kind {
654656
// Default branch: this is a "normal" access through a known range.
655657
// We iterate over affected locations and traverse the tree for each of them.
656658
for (perms_range, perms) in self.rperms.iter_mut(access_range.start, access_range.size)
657659
{
658660
TreeVisitor { nodes: &mut self.nodes, tag_mapping: &self.tag_mapping, perms }
659661
.traverse_parents_this_children_others(
660662
tag,
661-
|args| node_app(perms_range.clone(), args),
662-
|args| err_handler(perms_range.clone(), args),
663+
|args| node_app(perms_range.clone(), access_kind, access_cause, args),
664+
|args| err_handler(perms_range.clone(), access_cause, args),
663665
)?;
664666
}
665667
} else {
@@ -678,11 +680,14 @@ impl<'tcx> Tree {
678680
if let Some(p) = perms.get(idx)
679681
&& p.initialized
680682
{
683+
let access_kind =
684+
if p.permission.is_active() { AccessKind::Write } else { AccessKind::Read };
685+
let access_cause = diagnostics::AccessCause::FnExit(access_kind);
681686
TreeVisitor { nodes: &mut self.nodes, tag_mapping: &self.tag_mapping, perms }
682687
.traverse_nonchildren(
683688
tag,
684-
|args| node_app(perms_range.clone(), args),
685-
|args| err_handler(perms_range.clone(), args),
689+
|args| node_app(perms_range.clone(), access_kind, access_cause, args),
690+
|args| err_handler(perms_range.clone(), access_cause, args),
686691
)?;
687692
}
688693
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@compile-flags: -Zmiri-tree-borrows
2+
// This test tests that TB's protector end semantics correctly ensure
3+
// that protected activated writes can be reordered.
4+
fn the_other_function(ref_to_fst_elem: &mut i32, ptr_to_vec: *mut i32) -> *mut i32 {
5+
// Activate the reference. Afterwards, we should be able to reorder arbitrary writes.
6+
*ref_to_fst_elem = 0;
7+
// Here is such an arbitrary write.
8+
// It could be moved down after the retag, in which case the `funky_ref` would be invalidated.
9+
// We need to ensure that the `funky_ptr` is unusable even if the write to `ref_to_fst_elem`
10+
// happens before the retag.
11+
*ref_to_fst_elem = 42;
12+
// this creates a reference that is Reserved Lazy on the first element (offset 0).
13+
// It does so by doing a proper retag on the second element (offset 1), which is fine
14+
// since nothing else happens at that offset, but the lazy init mechanism means it's
15+
// also reserved at offset 0, but not initialized.
16+
let funky_ptr_lazy_on_fst_elem =
17+
unsafe { (&mut *(ptr_to_vec.wrapping_add(1))) as *mut i32 }.wrapping_sub(1);
18+
// If we write to `ref_to_fst_elem` here, then any further access to `funky_ptr_lazy_on_fst_elem` would
19+
// definitely be UB. Since the compiler ought to be able to reorder the write of `42` above down to
20+
// here, that means we want this program to also be UB.
21+
return funky_ptr_lazy_on_fst_elem;
22+
}
23+
24+
fn main() {
25+
let mut v = vec![0, 1];
26+
// get a pointer to the root of the allocation
27+
// note that it's not important it's the actual root, what matters is that it's a parent
28+
// of both references that will be created
29+
let ptr_to_vec = v.as_mut_ptr();
30+
let ref_to_fst_elem = unsafe { &mut *ptr_to_vec };
31+
let funky_ptr_lazy_on_fst_elem = the_other_function(ref_to_fst_elem, ptr_to_vec);
32+
// now we try to use the funky lazy pointer.
33+
// It should be UB, since the write-on-protector-end should disable it.
34+
unsafe { println!("Value of funky: {}", *funky_ptr_lazy_on_fst_elem) } //~ ERROR: /reborrow through .* is forbidden/
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: Undefined Behavior: reborrow through <TAG> at ALLOC[0x0] is forbidden
2+
--> $DIR/protector-write-lazy.rs:LL:CC
3+
|
4+
LL | unsafe { println!("Value of funky: {}", *funky_ptr_lazy_on_fst_elem) }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ reborrow through <TAG> at ALLOC[0x0] is forbidden
6+
|
7+
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental
8+
= help: the accessed tag <TAG> has state Disabled which forbids this reborrow (acting as a child read access)
9+
help: the accessed tag <TAG> was created here, in the initial state Reserved
10+
--> $DIR/protector-write-lazy.rs:LL:CC
11+
|
12+
LL | unsafe { (&mut *(ptr_to_vec.wrapping_add(1))) as *mut i32 }.wrapping_sub(1);
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
help: the accessed tag <TAG> later transitioned to Disabled due to a protector release (acting as a foreign write access) on every location previously accessed by this tag
15+
--> $DIR/protector-write-lazy.rs:LL:CC
16+
|
17+
LL | }
18+
| ^
19+
= help: this transition corresponds to a loss of read and write permissions
20+
= note: BACKTRACE (of the first span):
21+
= note: inside `main` at $DIR/protector-write-lazy.rs:LL:CC
22+
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
24+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
25+
26+
error: aborting due to 1 previous error
27+

0 commit comments

Comments
 (0)