Skip to content

Commit eeecb56

Browse files
Represent the raw pointer for a array length check as a new kind of fake borrow
1 parent 057313b commit eeecb56

File tree

26 files changed

+199
-89
lines changed

26 files changed

+199
-89
lines changed

compiler/rustc_borrowck/src/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1284,15 +1284,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
12841284
);
12851285
}
12861286

1287-
&Rvalue::RawPtr(mutability, place) => {
1288-
let access_kind = match mutability {
1289-
Mutability::Mut => (
1287+
&Rvalue::RawPtr(kind, place) => {
1288+
let access_kind = match kind {
1289+
RawPtrKind::Mut => (
12901290
Deep,
12911291
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
12921292
kind: MutBorrowKind::Default,
12931293
})),
12941294
),
1295-
Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1295+
RawPtrKind::Const => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1296+
RawPtrKind::FakeForPtrMetadata => {
1297+
(Shallow(Some(ArtificialField::ArrayLength)), Read(ReadKind::Copy))
1298+
}
12961299
};
12971300

12981301
self.access_place(

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use std::ops::ControlFlow;
33
use rustc_data_structures::graph::dominators::Dominators;
44
use rustc_middle::bug;
55
use rustc_middle::mir::visit::Visitor;
6-
use rustc_middle::mir::{
7-
self, BasicBlock, Body, BorrowKind, FakeBorrowKind, InlineAsmOperand, Location, Mutability,
8-
NonDivergingIntrinsic, Operand, Place, Rvalue, Statement, StatementKind, Terminator,
9-
TerminatorKind,
10-
};
6+
use rustc_middle::mir::*;
117
use rustc_middle::ty::TyCtxt;
128
use tracing::debug;
139

@@ -60,7 +56,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
6056
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
6157
self.consume_operand(location, op);
6258
}
63-
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
59+
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
6460
src,
6561
dst,
6662
count,
@@ -273,15 +269,18 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
273269
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
274270
}
275271

276-
&Rvalue::RawPtr(mutability, place) => {
277-
let access_kind = match mutability {
278-
Mutability::Mut => (
272+
&Rvalue::RawPtr(kind, place) => {
273+
let access_kind = match kind {
274+
RawPtrKind::Mut => (
279275
Deep,
280276
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
281-
kind: mir::MutBorrowKind::Default,
277+
kind: MutBorrowKind::Default,
282278
})),
283279
),
284-
Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
280+
RawPtrKind::Const => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
281+
RawPtrKind::FakeForPtrMetadata => {
282+
(Shallow(Some(ArtificialField::ArrayLength)), Read(ReadKind::Copy))
283+
}
285284
};
286285

287286
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
612612
mir::Rvalue::CopyForDeref(place) => {
613613
self.codegen_operand(bx, &mir::Operand::Copy(place))
614614
}
615-
mir::Rvalue::RawPtr(mutability, place) => {
616-
let mk_ptr =
617-
move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| Ty::new_ptr(tcx, ty, mutability);
615+
mir::Rvalue::RawPtr(kind, place) => {
616+
let mk_ptr = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
617+
Ty::new_ptr(tcx, ty, kind.to_mutbl_lossy())
618+
};
618619
self.codegen_place_to_pointer(bx, place, mk_ptr)
619620
}
620621

compiler/rustc_const_eval/src/check_consts/check.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
518518
}
519519

520520
Rvalue::Ref(_, BorrowKind::Mut { .. }, place)
521-
| Rvalue::RawPtr(Mutability::Mut, place) => {
521+
| Rvalue::RawPtr(RawPtrKind::Mut, place) => {
522522
// Inside mutable statics, we allow arbitrary mutable references.
523523
// We've allowed `static mut FOO = &mut [elements];` for a long time (the exact
524524
// reasons why are lost to history), and there is no reason to restrict that to
@@ -536,7 +536,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
536536
}
537537

538538
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Fake(_), place)
539-
| Rvalue::RawPtr(Mutability::Not, place) => {
539+
| Rvalue::RawPtr(RawPtrKind::Const, place) => {
540540
let borrowed_place_has_mut_interior = qualifs::in_place::<HasMutInterior, _>(
541541
self.ccx,
542542
&mut |local| self.qualifs.has_mut_interior(self.ccx, local, location),
@@ -548,6 +548,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
548548
}
549549
}
550550

551+
Rvalue::RawPtr(RawPtrKind::FakeForPtrMetadata, place) => {
552+
// These are only inserted for slice length, so the place must already be indirect.
553+
// This implies we do not have to worry about whether the borrow escapes.
554+
assert!(place.is_indirect(), "fake borrows are always indirect");
555+
}
556+
551557
Rvalue::Cast(
552558
CastKind::PointerCoercion(
553559
PointerCoercion::MutToConstPointer
@@ -600,12 +606,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
600606
}
601607
}
602608
UnOp::PtrMetadata => {
603-
if !ty.is_ref() && !ty.is_unsafe_ptr() {
604-
span_bug!(
605-
self.span,
606-
"non-pointer type in `Rvalue::UnaryOp({op:?})`: {ty:?}",
607-
);
608-
}
609+
// Getting the metadata from a pointer is always const.
610+
// We already validated the type is valid in the validator.
609611
}
610612
}
611613
}

compiler/rustc_const_eval/src/interpret/step.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::ty::layout::FnAbiOf;
99
use rustc_middle::ty::{self, Instance, Ty};
1010
use rustc_middle::{bug, mir, span_bug};
1111
use rustc_span::source_map::Spanned;
12-
use rustc_span::{DesugaringKind, Span};
1312
use rustc_target::callconv::FnAbi;
1413
use tracing::{info, instrument, trace};
1514

@@ -81,9 +80,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
8180
use rustc_middle::mir::StatementKind::*;
8281

8382
match &stmt.kind {
84-
Assign(box (place, rvalue)) => {
85-
self.eval_rvalue_into_place(rvalue, *place, stmt.source_info.span)?
86-
}
83+
Assign(box (place, rvalue)) => self.eval_rvalue_into_place(rvalue, *place)?,
8784

8885
SetDiscriminant { place, variant_index } => {
8986
let dest = self.eval_place(**place)?;
@@ -162,7 +159,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
162159
&mut self,
163160
rvalue: &mir::Rvalue<'tcx>,
164161
place: mir::Place<'tcx>,
165-
span: Span,
166162
) -> InterpResult<'tcx> {
167163
let dest = self.eval_place(place)?;
168164
// FIXME: ensure some kind of non-aliasing between LHS and RHS?
@@ -241,7 +237,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
241237
self.write_immediate(*val, &dest)?;
242238
}
243239

244-
RawPtr(_, place) => {
240+
RawPtr(kind, place) => {
245241
// Figure out whether this is an addr_of of an already raw place.
246242
let place_base_raw = if place.is_indirect_first_projection() {
247243
let ty = self.frame().body.local_decls[place.local].ty;
@@ -254,13 +250,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
254250
let src = self.eval_place(place)?;
255251
let place = self.force_allocation(&src)?;
256252
let mut val = ImmTy::from_immediate(place.to_ref(self), dest.layout);
257-
if !place_base_raw
258-
&& span.desugaring_kind() != Some(DesugaringKind::IndexBoundsCheckReborrow)
259-
{
260-
// If this was not already raw, it needs retagging.
261-
// As a special hack, we exclude the desugared `PtrMetadata(&raw const *_n)`
262-
// from indexing. (Really we should not do any retag on `&raw` but that does not
263-
// currently work with Stacked Borrows.)
253+
if !place_base_raw && !kind.is_fake() {
254+
// If this was not already raw, it needs retagging -- except for "fake"
255+
// raw borrows whose defining property is that they do not get retagged.
264256
val = M::retag_ptr_value(self, mir::RetagKind::Raw, &val)?;
265257
}
266258
self.write_immediate(*val, &dest)?;

compiler/rustc_middle/src/mir/syntax.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,59 @@ pub enum BorrowKind {
180180
Mut { kind: MutBorrowKind },
181181
}
182182

183+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
184+
#[derive(Hash, HashStable)]
185+
pub enum RawPtrKind {
186+
Mut,
187+
Const,
188+
/// Creates a raw pointer to a place that will only be used to access its metadata,
189+
/// not the data behind the pointer. Note that this limitation is *not* enforced
190+
/// by the validator.
191+
///
192+
/// The borrow checker allows overlap of these raw pointers with references to the
193+
/// data. This is sound even if the pointer is "misused" since any such use is anyway
194+
/// unsafe. In terms of the operational semantics (i.e., Miri), this is equivalent
195+
/// to `RawPtrKind::Mut`, but will never incur a retag.
196+
FakeForPtrMetadata,
197+
}
198+
199+
impl From<Mutability> for RawPtrKind {
200+
fn from(other: Mutability) -> Self {
201+
match other {
202+
Mutability::Mut => RawPtrKind::Mut,
203+
Mutability::Not => RawPtrKind::Const,
204+
}
205+
}
206+
}
207+
208+
impl RawPtrKind {
209+
pub fn is_fake(self) -> bool {
210+
match self {
211+
RawPtrKind::Mut | RawPtrKind::Const => false,
212+
RawPtrKind::FakeForPtrMetadata => true,
213+
}
214+
}
215+
216+
pub fn to_mutbl_lossy(self) -> Mutability {
217+
match self {
218+
RawPtrKind::Mut => Mutability::Mut,
219+
RawPtrKind::Const => Mutability::Not,
220+
221+
// We have no type corresponding to a fake borrow, so use
222+
// `*const` as an approximation.
223+
RawPtrKind::FakeForPtrMetadata => Mutability::Not,
224+
}
225+
}
226+
227+
pub fn ptr_str(self) -> &'static str {
228+
match self {
229+
RawPtrKind::Mut => "mut",
230+
RawPtrKind::Const => "const",
231+
RawPtrKind::FakeForPtrMetadata => "const (fake)",
232+
}
233+
}
234+
}
235+
183236
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
184237
#[derive(Hash, HashStable)]
185238
pub enum MutBorrowKind {
@@ -1356,7 +1409,7 @@ pub enum Rvalue<'tcx> {
13561409
///
13571410
/// Like with references, the semantics of this operation are heavily dependent on the aliasing
13581411
/// model.
1359-
RawPtr(Mutability, Place<'tcx>),
1412+
RawPtr(RawPtrKind, Place<'tcx>),
13601413

13611414
/// Yields the length of the place, as a `usize`.
13621415
///

compiler/rustc_middle/src/mir/tcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ impl<'tcx> Rvalue<'tcx> {
206206
let place_ty = place.ty(local_decls, tcx).ty;
207207
Ty::new_ref(tcx, reg, place_ty, bk.to_mutbl_lossy())
208208
}
209-
Rvalue::RawPtr(mutability, ref place) => {
209+
Rvalue::RawPtr(kind, ref place) => {
210210
let place_ty = place.ty(local_decls, tcx).ty;
211-
Ty::new_ptr(tcx, place_ty, mutability)
211+
Ty::new_ptr(tcx, place_ty, kind.to_mutbl_lossy())
212212
}
213213
Rvalue::Len(..) => tcx.types.usize,
214214
Rvalue::Cast(.., ty) => ty,

compiler/rustc_middle/src/mir/type_foldable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TrivialTypeTraversalImpls! {
1515
SourceScopeLocalData,
1616
UserTypeAnnotationIndex,
1717
BorrowKind,
18+
RawPtrKind,
1819
CastKind,
1920
BasicBlock,
2021
SwitchTargets,

compiler/rustc_middle/src/mir/visit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -685,12 +685,15 @@ macro_rules! make_mir_visitor {
685685

686686
Rvalue::RawPtr(m, path) => {
687687
let ctx = match m {
688-
Mutability::Mut => PlaceContext::MutatingUse(
688+
RawPtrKind::Mut => PlaceContext::MutatingUse(
689689
MutatingUseContext::RawBorrow
690690
),
691-
Mutability::Not => PlaceContext::NonMutatingUse(
691+
RawPtrKind::Const => PlaceContext::NonMutatingUse(
692692
NonMutatingUseContext::RawBorrow
693693
),
694+
RawPtrKind::FakeForPtrMetadata => PlaceContext::NonMutatingUse(
695+
NonMutatingUseContext::Inspect
696+
),
694697
};
695698
self.visit_place(path, ctx, location);
696699
}

compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
253253
Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?)
254254
),
255255
ExprKind::RawBorrow { mutability, arg } => Ok(
256-
Rvalue::RawPtr(*mutability, self.parse_place(*arg)?)
256+
Rvalue::RawPtr((*mutability).into(), self.parse_place(*arg)?)
257257
),
258258
ExprKind::Binary { op, lhs, rhs } => Ok(
259259
Rvalue::BinaryOp(*op, Box::new((self.parse_operand(*lhs)?, self.parse_operand(*rhs)?)))

compiler/rustc_mir_build/src/builder/expr/as_place.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::mir::*;
1111
use rustc_middle::thir::*;
1212
use rustc_middle::ty::{self, AdtDef, CanonicalUserTypeAnnotation, Ty, Variance};
1313
use rustc_middle::{bug, span_bug};
14-
use rustc_span::{DesugaringKind, Span};
14+
use rustc_span::Span;
1515
use tracing::{debug, instrument, trace};
1616

1717
use crate::builder::ForGuard::{OutsideGuard, RefWithinGuard};
@@ -643,8 +643,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
643643
source_info: SourceInfo,
644644
) -> Operand<'tcx> {
645645
let place_ty = place.ty(&self.local_decls, self.tcx).ty;
646-
let usize_ty = self.tcx.types.usize;
647-
648646
match place_ty.kind() {
649647
ty::Array(_elem_ty, len_const) => {
650648
// We know how long an array is, so just use that as a constant
@@ -668,27 +666,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
668666
// the MIR we're building here needs to pass NLL later.
669667
Operand::Copy(Place::from(place.local))
670668
} else {
671-
let len_span = self.tcx.with_stable_hashing_context(|hcx| {
672-
let span = source_info.span;
673-
span.mark_with_reason(
674-
None,
675-
DesugaringKind::IndexBoundsCheckReborrow,
676-
span.edition(),
677-
hcx,
678-
)
679-
});
680669
let ptr_ty = Ty::new_imm_ptr(self.tcx, place_ty);
681670
let slice_ptr = self.temp(ptr_ty, span);
682671
self.cfg.push_assign(
683672
block,
684-
SourceInfo { span: len_span, ..source_info },
673+
source_info,
685674
slice_ptr,
686-
Rvalue::RawPtr(Mutability::Not, place),
675+
Rvalue::RawPtr(RawPtrKind::FakeForPtrMetadata, place),
687676
);
688677
Operand::Move(slice_ptr)
689678
};
690679

691-
let len = self.temp(usize_ty, span);
680+
let len = self.temp(self.tcx.types.usize, span);
692681
self.cfg.push_assign(
693682
block,
694683
source_info,

compiler/rustc_mir_build/src/builder/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
303303
hir::Mutability::Not => this.as_read_only_place(block, arg),
304304
hir::Mutability::Mut => this.as_place(block, arg),
305305
};
306-
let address_of = Rvalue::RawPtr(mutability, unpack!(block = place));
306+
let address_of = Rvalue::RawPtr(mutability.into(), unpack!(block = place));
307307
this.cfg.push_assign(block, source_info, destination, address_of);
308308
block.unit()
309309
}

compiler/rustc_mir_dataflow/src/elaborate_drops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ where
700700
statements: vec![
701701
self.assign(
702702
ptr,
703-
Rvalue::RawPtr(Mutability::Mut, tcx.mk_place_index(self.place, cur)),
703+
Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_index(self.place, cur)),
704704
),
705705
self.assign(
706706
cur.into(),
@@ -816,7 +816,7 @@ where
816816

817817
let mut delegate_block = BasicBlockData {
818818
statements: vec![
819-
self.assign(Place::from(array_ptr), Rvalue::RawPtr(Mutability::Mut, self.place)),
819+
self.assign(Place::from(array_ptr), Rvalue::RawPtr(RawPtrKind::Mut, self.place)),
820820
self.assign(
821821
Place::from(slice_ptr),
822822
Rvalue::Cast(

compiler/rustc_mir_transform/src/gvn.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ enum AggregateTy<'tcx> {
192192
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
193193
enum AddressKind {
194194
Ref(BorrowKind),
195-
Address(Mutability),
195+
Address(RawPtrKind),
196196
}
197197

198198
#[derive(Debug, PartialEq, Eq, Hash)]
@@ -504,7 +504,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
504504
mplace.layout.ty,
505505
bk.to_mutbl_lossy(),
506506
),
507-
AddressKind::Address(mutbl) => Ty::new_ptr(self.tcx, mplace.layout.ty, mutbl),
507+
AddressKind::Address(mutbl) => {
508+
Ty::new_ptr(self.tcx, mplace.layout.ty, mutbl.to_mutbl_lossy())
509+
}
508510
};
509511
let layout = self.ecx.layout_of(ty).ok()?;
510512
ImmTy::from_immediate(pointer, layout).into()

0 commit comments

Comments
 (0)