Skip to content

Commit 4ec0ba9

Browse files
committed
Auto merge of rust-lang#55716 - RalfJung:escape-to-raw, r=oli-obk
Add escape-to-raw MIR statement Add a new MIR "ghost state statement": Escaping a ptr to permit raw accesses. ~~This includes rust-lang#55549, [click here](RalfJung/rust@miri-visitor...RalfJung:escape-to-raw) for just the new commits.~~
2 parents 7d3b9b1 + b891a81 commit 4ec0ba9

31 files changed

+267
-163
lines changed

src/librustc/ich/impls_mir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ for mir::StatementKind<'gcx> {
220220
mir::StatementKind::EndRegion(ref region_scope) => {
221221
region_scope.hash_stable(hcx, hasher);
222222
}
223+
mir::StatementKind::EscapeToRaw(ref place) => {
224+
place.hash_stable(hcx, hasher);
225+
}
223226
mir::StatementKind::Retag { fn_entry, ref place } => {
224227
fn_entry.hash_stable(hcx, hasher);
225228
place.hash_stable(hcx, hasher);

src/librustc/mir/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,13 @@ pub enum StatementKind<'tcx> {
17821782
place: Place<'tcx>,
17831783
},
17841784

1785+
/// Escape the given reference to a raw pointer, so that it can be accessed
1786+
/// without precise provenance tracking. These statements are currently only interpreted
1787+
/// by miri and only generated when "-Z mir-emit-retag" is passed.
1788+
/// See <https://internals.rust-lang.org/t/stacked-borrows-an-aliasing-model-for-rust/8153/>
1789+
/// for more details.
1790+
EscapeToRaw(Operand<'tcx>),
1791+
17851792
/// Mark one terminating point of a region scope (i.e. static region).
17861793
/// (The starting point(s) arise implicitly from borrows.)
17871794
EndRegion(region::Scope),
@@ -1843,6 +1850,7 @@ impl<'tcx> Debug for Statement<'tcx> {
18431850
EndRegion(ref ce) => write!(fmt, "EndRegion({})", ty::ReScope(*ce)),
18441851
Retag { fn_entry, ref place } =>
18451852
write!(fmt, "Retag({}{:?})", if fn_entry { "[fn entry] " } else { "" }, place),
1853+
EscapeToRaw(ref place) => write!(fmt, "EscapeToRaw({:?})", place),
18461854
StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place),
18471855
StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place),
18481856
SetDiscriminant {
@@ -3019,6 +3027,7 @@ EnumTypeFoldableImpl! {
30193027
(StatementKind::StorageDead)(a),
30203028
(StatementKind::InlineAsm) { asm, outputs, inputs },
30213029
(StatementKind::Retag) { fn_entry, place },
3030+
(StatementKind::EscapeToRaw)(place),
30223031
(StatementKind::EndRegion)(a),
30233032
(StatementKind::AscribeUserType)(a, v, b),
30243033
(StatementKind::Nop),

src/librustc/mir/visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ macro_rules! make_mir_visitor {
385385
location
386386
);
387387
}
388+
StatementKind::EscapeToRaw(ref $($mutability)* op) => {
389+
self.visit_operand(op, location);
390+
}
388391
StatementKind::StorageLive(ref $($mutability)* local) => {
389392
self.visit_local(
390393
local,
@@ -1022,7 +1025,7 @@ pub enum MutatingUseContext<'tcx> {
10221025
/// f(&mut x.y);
10231026
///
10241027
Projection,
1025-
/// Retagging (updating the "Stacked Borrows" tag)
1028+
/// Retagging, a "Stacked Borrows" shadow state operation
10261029
Retag,
10271030
}
10281031

src/librustc_codegen_llvm/mir/statement.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ impl FunctionCx<'a, 'll, 'tcx> {
105105
bx
106106
}
107107
mir::StatementKind::FakeRead(..) |
108-
mir::StatementKind::EndRegion(_) |
108+
mir::StatementKind::EndRegion(..) |
109109
mir::StatementKind::Retag { .. } |
110+
mir::StatementKind::EscapeToRaw { .. } |
110111
mir::StatementKind::AscribeUserType(..) |
111112
mir::StatementKind::Nop => bx,
112113
}

src/librustc_mir/borrow_check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
599599
StatementKind::Nop
600600
| StatementKind::AscribeUserType(..)
601601
| StatementKind::Retag { .. }
602+
| StatementKind::EscapeToRaw { .. }
602603
| StatementKind::StorageLive(..) => {
603604
// `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
604605
// to borrow check.

src/librustc_mir/borrow_check/nll/invalidation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
137137
StatementKind::Nop |
138138
StatementKind::AscribeUserType(..) |
139139
StatementKind::Retag { .. } |
140+
StatementKind::EscapeToRaw { .. } |
140141
StatementKind::StorageLive(..) => {
141142
// `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
142143
// to borrow check.

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,11 +1311,12 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
13111311
}
13121312
}
13131313
StatementKind::FakeRead(..)
1314-
| StatementKind::StorageLive(_)
1315-
| StatementKind::StorageDead(_)
1314+
| StatementKind::StorageLive(..)
1315+
| StatementKind::StorageDead(..)
13161316
| StatementKind::InlineAsm { .. }
13171317
| StatementKind::EndRegion(_)
13181318
| StatementKind::Retag { .. }
1319+
| StatementKind::EscapeToRaw { .. }
13191320
| StatementKind::Nop => {}
13201321
}
13211322
}

src/librustc_mir/build/expr/as_place.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
8686
// region_scope=None so place indexes live forever. They are scalars so they
8787
// do not need storage annotations, and they are often copied between
8888
// places.
89+
// Making this a *fresh* temporary also means we do not have to worry about
90+
// the index changing later: Nothing will ever change this temporary.
91+
// The "retagging" transformation (for Stacked Borrows) relies on this.
8992
let idx = unpack!(block = this.as_temp(block, None, index, Mutability::Mut));
9093

9194
// bounds check:

src/librustc_mir/const_eval.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
351351
type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
352352

353353
const STATIC_KIND: Option<!> = None; // no copying of statics allowed
354-
const ENABLE_PTR_TRACKING_HOOKS: bool = false; // we don't have no provenance
355354

356355
#[inline(always)]
357356
fn enforce_validity(_ecx: &EvalContext<'a, 'mir, 'tcx, Self>) -> bool {

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
339339
mir::StatementKind::SetDiscriminant { .. } |
340340
mir::StatementKind::StorageLive(..) |
341341
mir::StatementKind::Retag { .. } |
342+
mir::StatementKind::EscapeToRaw { .. } |
342343
mir::StatementKind::AscribeUserType(..) |
343344
mir::StatementKind::Nop => {}
344345

0 commit comments

Comments
 (0)