Skip to content

Commit a00b977

Browse files
authored
Unrolled build for #143140
Rollup merge of #143140 - RalfJung:ptr-into-parts, r=oli-obk give Pointer::into_parts a more scary name and offer a safer alternative `into_parts` is a bit too innocent of a name for a somewhat subtle operation. r? `@oli-obk`
2 parents f26e580 + 75c6e14 commit a00b977

File tree

20 files changed

+55
-46
lines changed

20 files changed

+55
-46
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub(crate) fn codegen_const_value<'tcx>(
133133
}
134134
}
135135
Scalar::Ptr(ptr, _size) => {
136-
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
136+
let (prov, offset) = ptr.prov_and_relative_offset();
137137
let alloc_id = prov.alloc_id();
138138
let base_addr = match fx.tcx.global_alloc(alloc_id) {
139139
GlobalAlloc::Memory(alloc) => {

compiler/rustc_codegen_gcc/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
240240
}
241241
}
242242
Scalar::Ptr(ptr, _size) => {
243-
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
243+
let (prov, offset) = ptr.prov_and_relative_offset();
244244
let alloc_id = prov.alloc_id();
245245
let base_addr = match self.tcx.global_alloc(alloc_id) {
246246
GlobalAlloc::Memory(alloc) => {

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
268268
}
269269
}
270270
Scalar::Ptr(ptr, _size) => {
271-
let (prov, offset) = ptr.into_parts();
271+
let (prov, offset) = ptr.prov_and_relative_offset();
272272
let global_alloc = self.tcx.global_alloc(prov.alloc_id());
273273
let base_addr = match global_alloc {
274274
GlobalAlloc::Memory(alloc) => {

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ pub(super) fn op_to_const<'tcx>(
209209

210210
match immediate {
211211
Left(ref mplace) => {
212-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
213-
let (prov, offset) = mplace.ptr().into_parts();
214-
let alloc_id = prov.expect("cannot have `fake` place for non-ZST type").alloc_id();
212+
let (prov, offset) =
213+
mplace.ptr().into_pointer_or_addr().unwrap().prov_and_relative_offset();
214+
let alloc_id = prov.alloc_id();
215215
ConstValue::Indirect { alloc_id, offset }
216216
}
217217
// see comment on `let force_as_immediate` above
@@ -232,9 +232,10 @@ pub(super) fn op_to_const<'tcx>(
232232
imm.layout.ty,
233233
);
234234
let msg = "`op_to_const` on an immediate scalar pair must only be used on slice references to the beginning of an actual allocation";
235-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
236-
let (prov, offset) = a.to_pointer(ecx).expect(msg).into_parts();
237-
let alloc_id = prov.expect(msg).alloc_id();
235+
let ptr = a.to_pointer(ecx).expect(msg);
236+
let (prov, offset) =
237+
ptr.into_pointer_or_addr().expect(msg).prov_and_relative_offset();
238+
let alloc_id = prov.alloc_id();
238239
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
239240
assert!(offset == abi::Size::ZERO, "{}", msg);
240241
let meta = b.to_target_usize(ecx).expect(msg);

compiler/rustc_const_eval/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
574574
if addr != 0 {
575575
diag.arg(
576576
"pointer",
577-
Pointer::<Option<CtfeProvenance>>::from_addr_invalid(addr).to_string(),
577+
Pointer::<Option<CtfeProvenance>>::without_provenance(addr).to_string(),
578578
);
579579
}
580580

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
747747
// Allow these casts, but make the pointer not dereferenceable.
748748
// (I.e., they behave like transmutation.)
749749
// This is correct because no pointers can ever be exposed in compile-time evaluation.
750-
interp_ok(Pointer::from_addr_invalid(addr))
750+
interp_ok(Pointer::without_provenance(addr))
751751
}
752752

753753
#[inline(always)]
@@ -756,8 +756,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
756756
ptr: Pointer<CtfeProvenance>,
757757
_size: i64,
758758
) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
759-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
760-
let (prov, offset) = ptr.into_parts();
759+
let (prov, offset) = ptr.prov_and_relative_offset();
761760
Some((prov.alloc_id(), offset, prov.immutable()))
762761
}
763762

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
15961596
Some((alloc_id, offset, extra)) => Ok((alloc_id, offset, extra)),
15971597
None => {
15981598
assert!(M::Provenance::OFFSET_IS_ADDR);
1599-
let (_, addr) = ptr.into_parts();
1599+
// Offset is absolute, as we just asserted.
1600+
let (_, addr) = ptr.into_raw_parts();
16001601
Err(addr.bytes())
16011602
}
16021603
},

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx, Prov: Provenance> MPlaceTy<'tcx, Prov> {
118118
pub fn fake_alloc_zst(layout: TyAndLayout<'tcx>) -> Self {
119119
assert!(layout.is_zst());
120120
let align = layout.align.abi;
121-
let ptr = Pointer::from_addr_invalid(align.bytes()); // no provenance, absolute address
121+
let ptr = Pointer::without_provenance(align.bytes()); // no provenance, absolute address
122122
MPlaceTy { mplace: MemPlace { ptr, meta: MemPlaceMeta::None, misaligned: None }, layout }
123123
}
124124

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
518518
Ub(DanglingIntPointer { addr: i, .. }) => DanglingPtrNoProvenance {
519519
ptr_kind,
520520
// FIXME this says "null pointer" when null but we need translate
521-
pointer: format!("{}", Pointer::<Option<AllocId>>::from_addr_invalid(i))
521+
pointer: format!("{}", Pointer::<Option<AllocId>>::without_provenance(i))
522522
},
523523
Ub(PointerOutOfBounds { .. }) => DanglingPtrOutOfBounds {
524524
ptr_kind
@@ -868,7 +868,9 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
868868
fn add_data_range(&mut self, ptr: Pointer<Option<M::Provenance>>, size: Size) {
869869
if let Some(data_bytes) = self.data_bytes.as_mut() {
870870
// We only have to store the offset, the rest is the same for all pointers here.
871-
let (_prov, offset) = ptr.into_parts();
871+
// The logic is agnostic to wether the offset is relative or absolute as long as
872+
// it is consistent.
873+
let (_prov, offset) = ptr.into_raw_parts();
872874
// Add this.
873875
data_bytes.add_range(offset, size);
874876
};
@@ -894,7 +896,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
894896
.as_mplace_or_imm()
895897
.expect_left("place must be in memory")
896898
.ptr();
897-
let (_prov, offset) = ptr.into_parts();
899+
let (_prov, offset) = ptr.into_raw_parts();
898900
offset
899901
}
900902

@@ -903,7 +905,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
903905
// Our value must be in memory, otherwise we would not have set up `data_bytes`.
904906
let mplace = self.ecx.force_allocation(place)?;
905907
// Determine starting offset and size.
906-
let (_prov, start_offset) = mplace.ptr().into_parts();
908+
let (_prov, start_offset) = mplace.ptr().into_raw_parts();
907909
let (size, _align) = self
908910
.ecx
909911
.size_and_align_of_val(&mplace)?

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ impl<'tcx> ConstValue<'tcx> {
168168
return Some(&[]);
169169
}
170170
// Non-empty slice, must have memory. We know this is a relative pointer.
171-
let (inner_prov, offset) = ptr.into_parts();
172-
let data = tcx.global_alloc(inner_prov?.alloc_id()).unwrap_memory();
171+
let (inner_prov, offset) =
172+
ptr.into_pointer_or_addr().ok()?.prov_and_relative_offset();
173+
let data = tcx.global_alloc(inner_prov.alloc_id()).unwrap_memory();
173174
(data, offset.bytes(), offset.bytes() + len)
174175
}
175176
};

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl Allocation {
526526
let ptr_bytes = &mut bytes[idx..idx + ptr_size];
527527
let bits = read_target_uint(endian, ptr_bytes).unwrap();
528528
let (ptr_prov, ptr_offset) =
529-
adjust_ptr(Pointer::new(alloc_id, Size::from_bytes(bits)))?.into_parts();
529+
adjust_ptr(Pointer::new(alloc_id, Size::from_bytes(bits)))?.into_raw_parts();
530530
write_target_uint(endian, ptr_bytes, ptr_offset.bytes().into()).unwrap();
531531
new_provenance.push((offset, ptr_prov));
532532
}
@@ -769,7 +769,7 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
769769
// as-is into memory. This also double-checks that `val.size()` matches `range.size`.
770770
let (bytes, provenance) = match val.to_bits_or_ptr_internal(range.size)? {
771771
Right(ptr) => {
772-
let (provenance, offset) = ptr.into_parts();
772+
let (provenance, offset) = ptr.into_raw_parts();
773773
(u128::from(offset.bytes()), Some(provenance))
774774
}
775775
Left(data) => (data, None),

compiler/rustc_middle/src/mir/interpret/pointer.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl From<CtfeProvenance> for Pointer {
288288
impl<Prov> From<Pointer<Prov>> for Pointer<Option<Prov>> {
289289
#[inline(always)]
290290
fn from(ptr: Pointer<Prov>) -> Self {
291-
let (prov, offset) = ptr.into_parts();
291+
let (prov, offset) = ptr.into_raw_parts();
292292
Pointer::new(Some(prov), offset)
293293
}
294294
}
@@ -314,19 +314,17 @@ impl<Prov> Pointer<Option<Prov>> {
314314
assert!(Prov::OFFSET_IS_ADDR);
315315
self.offset
316316
}
317-
}
318317

319-
impl<Prov> Pointer<Option<Prov>> {
320318
/// Creates a pointer to the given address, with invalid provenance (i.e., cannot be used for
321319
/// any memory access).
322320
#[inline(always)]
323-
pub fn from_addr_invalid(addr: u64) -> Self {
321+
pub fn without_provenance(addr: u64) -> Self {
324322
Pointer { provenance: None, offset: Size::from_bytes(addr) }
325323
}
326324

327325
#[inline(always)]
328326
pub fn null() -> Self {
329-
Pointer::from_addr_invalid(0)
327+
Pointer::without_provenance(0)
330328
}
331329
}
332330

@@ -336,11 +334,11 @@ impl<Prov> Pointer<Prov> {
336334
Pointer { provenance, offset }
337335
}
338336

339-
/// Obtain the constituents of this pointer. Not that the meaning of the offset depends on the type `Prov`!
340-
/// This function must only be used in the implementation of `Machine::ptr_get_alloc`,
341-
/// and when a `Pointer` is taken apart to be stored efficiently in an `Allocation`.
337+
/// Obtain the constituents of this pointer. Note that the meaning of the offset depends on the
338+
/// type `Prov`! This is a low-level function that should only be used when absolutely
339+
/// necessary. Prefer `prov_and_relative_offset` if possible.
342340
#[inline(always)]
343-
pub fn into_parts(self) -> (Prov, Size) {
341+
pub fn into_raw_parts(self) -> (Prov, Size) {
344342
(self.provenance, self.offset)
345343
}
346344

@@ -361,3 +359,12 @@ impl<Prov> Pointer<Prov> {
361359
self.wrapping_offset(Size::from_bytes(i as u64), cx)
362360
}
363361
}
362+
363+
impl Pointer<CtfeProvenance> {
364+
/// Return the provenance and relative offset stored in this pointer. Safer alternative to
365+
/// `into_raw_parts` since the type ensures that the offset is indeed relative.
366+
#[inline(always)]
367+
pub fn prov_and_relative_offset(self) -> (CtfeProvenance, Size) {
368+
(self.provenance, self.offset)
369+
}
370+
}

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<Prov> Scalar<Prov> {
109109
/// Create a Scalar from a pointer with an `Option<_>` provenance (where `None` represents a
110110
/// plain integer / "invalid" pointer).
111111
pub fn from_maybe_pointer(ptr: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
112-
match ptr.into_parts() {
112+
match ptr.into_raw_parts() {
113113
(Some(prov), offset) => Scalar::from_pointer(Pointer::new(prov, offset), cx),
114114
(None, offset) => {
115115
Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
@@ -276,7 +276,7 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
276276
Right(ptr) => interp_ok(ptr.into()),
277277
Left(bits) => {
278278
let addr = u64::try_from(bits).unwrap();
279-
interp_ok(Pointer::from_addr_invalid(addr))
279+
interp_ok(Pointer::without_provenance(addr))
280280
}
281281
}
282282
}
@@ -299,7 +299,7 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
299299
Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
300300
} else {
301301
// We know `offset` is relative, since `OFFSET_IS_ADDR == false`.
302-
let (prov, offset) = ptr.into_parts();
302+
let (prov, offset) = ptr.into_raw_parts();
303303
// Because `OFFSET_IS_ADDR == false`, this unwrap can never fail.
304304
Err(Scalar::Ptr(Pointer::new(prov.get_alloc_id().unwrap(), offset), sz))
305305
}

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
17551755
) -> Result<(), PrintError> {
17561756
define_scoped_cx!(self);
17571757

1758-
let (prov, offset) = ptr.into_parts();
1758+
let (prov, offset) = ptr.prov_and_relative_offset();
17591759
match ty.kind() {
17601760
// Byte strings (&[u8; N])
17611761
ty::Ref(_, inner, _) => {

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ fn op_to_prop_const<'tcx>(
16361636
}
16371637

16381638
let pointer = mplace.ptr().into_pointer_or_addr().ok()?;
1639-
let (prov, offset) = pointer.into_parts();
1639+
let (prov, offset) = pointer.prov_and_relative_offset();
16401640
let alloc_id = prov.alloc_id();
16411641
intern_const_alloc_for_constprop(ecx, alloc_id).discard_err()?;
16421642

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
390390
) -> InterpResult<'tcx, interpret::Pointer<Provenance>> {
391391
let this = self.eval_context_ref();
392392

393-
let (prov, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
393+
let (prov, offset) = ptr.prov_and_relative_offset();
394394
let alloc_id = prov.alloc_id();
395395

396396
// Get a pointer to the beginning of this allocation.
@@ -447,7 +447,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
447447
) -> Option<(AllocId, Size)> {
448448
let this = self.eval_context_ref();
449449

450-
let (tag, addr) = ptr.into_parts(); // addr is absolute (Tag provenance)
450+
let (tag, addr) = ptr.into_raw_parts(); // addr is absolute (Miri provenance)
451451

452452
let alloc_id = if let Provenance::Concrete { alloc_id, .. } = tag {
453453
alloc_id

src/tools/miri/src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl interpret::Provenance for Provenance {
285285
}
286286

287287
fn fmt(ptr: &interpret::Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
288-
let (prov, addr) = ptr.into_parts(); // address is absolute
288+
let (prov, addr) = ptr.into_raw_parts(); // offset is absolute address
289289
write!(f, "{:#x}", addr.bytes())?;
290290
if f.alternate() {
291291
write!(f, "{prov:#?}")?;

src/tools/miri/src/provenance_gc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,13 @@ impl VisitProvenance for Provenance {
6868

6969
impl VisitProvenance for StrictPointer {
7070
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
71-
let (prov, _offset) = self.into_parts();
72-
prov.visit_provenance(visit);
71+
self.provenance.visit_provenance(visit);
7372
}
7473
}
7574

7675
impl VisitProvenance for Pointer {
7776
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
78-
let (prov, _offset) = self.into_parts();
79-
prov.visit_provenance(visit);
77+
self.provenance.visit_provenance(visit);
8078
}
8179
}
8280

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
411411
AlignFromBytesError::TooLarge(_) => Align::MAX,
412412
}
413413
});
414-
let (_, addr) = ptr.into_parts(); // we know the offset is absolute
414+
let addr = ptr.addr();
415415
// Cannot panic since `align` is a power of 2 and hence non-zero.
416416
if addr.bytes().strict_rem(align.bytes()) != 0 {
417417
throw_unsup_format!(

src/tools/miri/src/shims/unix/mem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4949
&& matches!(&*this.tcx.sess.target.os, "macos" | "solaris" | "illumos")
5050
&& (flags & map_fixed) != 0
5151
{
52-
return interp_ok(Scalar::from_maybe_pointer(Pointer::from_addr_invalid(addr), this));
52+
return interp_ok(Scalar::from_maybe_pointer(Pointer::without_provenance(addr), this));
5353
}
5454

5555
let prot_read = this.eval_libc_i32("PROT_READ");

0 commit comments

Comments
 (0)