Skip to content

Commit e5df574

Browse files
committed
strict provenance: rename addr → addr_without_provenance
1 parent e9f9594 commit e5df574

File tree

59 files changed

+256
-204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+256
-204
lines changed

compiler/rustc_arena/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ impl<T> TypedArena<T> {
172172
fn can_allocate(&self, additional: usize) -> bool {
173173
// FIXME: this should *likely* use `offset_from`, but more
174174
// investigation is needed (including running tests in miri).
175-
let available_bytes = self.end.get().addr() - self.ptr.get().addr();
175+
let available_bytes =
176+
self.end.get().addr_without_provenance() - self.ptr.get().addr_without_provenance();
176177
let additional_bytes = additional.checked_mul(mem::size_of::<T>()).unwrap();
177178
available_bytes >= additional_bytes
178179
}
@@ -245,7 +246,8 @@ impl<T> TypedArena<T> {
245246
if mem::needs_drop::<T>() {
246247
// FIXME: this should *likely* use `offset_from`, but more
247248
// investigation is needed (including running tests in miri).
248-
let used_bytes = self.ptr.get().addr() - last_chunk.start().addr();
249+
let used_bytes = self.ptr.get().addr_without_provenance()
250+
- last_chunk.start().addr_without_provenance();
249251
last_chunk.entries = used_bytes / mem::size_of::<T>();
250252
}
251253

@@ -271,9 +273,9 @@ impl<T> TypedArena<T> {
271273
// chunks.
272274
fn clear_last_chunk(&self, last_chunk: &mut ArenaChunk<T>) {
273275
// Determine how much was filled.
274-
let start = last_chunk.start().addr();
276+
let start = last_chunk.start().addr_without_provenance();
275277
// We obtain the value of the pointer to the first uninitialized element.
276-
let end = self.ptr.get().addr();
278+
let end = self.ptr.get().addr_without_provenance();
277279
// We then calculate the number of elements to be dropped in the last chunk,
278280
// which is the filled area's length.
279281
let diff = if mem::size_of::<T>() == 0 {
@@ -396,11 +398,11 @@ impl DroplessArena {
396398
self.start.set(chunk.start());
397399

398400
// Align the end to DROPLESS_ALIGNMENT.
399-
let end = align_down(chunk.end().addr(), DROPLESS_ALIGNMENT);
401+
let end = align_down(chunk.end().addr_without_provenance(), DROPLESS_ALIGNMENT);
400402

401403
// Make sure we don't go past `start`. This should not happen since the allocation
402404
// should be at least DROPLESS_ALIGNMENT - 1 bytes.
403-
debug_assert!(chunk.start().addr() <= end);
405+
debug_assert!(chunk.start().addr_without_provenance() <= end);
404406

405407
self.end.set(chunk.end().with_addr(end));
406408

@@ -415,9 +417,9 @@ impl DroplessArena {
415417
// This loop executes once or twice: if allocation fails the first
416418
// time, the `grow` ensures it will succeed the second time.
417419
loop {
418-
let start = self.start.get().addr();
420+
let start = self.start.get().addr_without_provenance();
419421
let old_end = self.end.get();
420-
let end = old_end.addr();
422+
let end = old_end.addr_without_provenance();
421423

422424
// Align allocated bytes so that `self.end` stays aligned to
423425
// DROPLESS_ALIGNMENT.

compiler/rustc_codegen_ssa/src/mono_item.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
138138
fn to_raw_string(&self) -> String {
139139
match *self {
140140
MonoItem::Fn(instance) => {
141-
format!("Fn({:?}, {})", instance.def, instance.args.as_ptr().addr())
141+
format!(
142+
"Fn({:?}, {})",
143+
instance.def,
144+
instance.args.as_ptr().addr_without_provenance()
145+
)
142146
}
143147
MonoItem::Static(id) => format!("Static({id:?})"),
144148
MonoItem::GlobalAsm(id) => format!("GlobalAsm({id:?})"),

compiler/rustc_data_structures/src/tagged_ptr/copy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ where
104104
#[inline]
105105
pub fn tag(&self) -> T {
106106
// Unpack the tag, according to the `self.packed` encoding scheme
107-
let tag = self.packed.addr().get() >> Self::TAG_BIT_SHIFT;
107+
let tag = self.packed.addr_without_provenance().get() >> Self::TAG_BIT_SHIFT;
108108

109109
// Safety:
110110
// The shift retrieves the original value from `T::into_usize`,

compiler/rustc_hir_typeck/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ hir_typeck_lossy_provenance_int2ptr =
9090
9191
hir_typeck_lossy_provenance_ptr2int =
9292
under strict provenance it is considered bad style to cast pointer `{$expr_ty}` to integer `{$cast_ty}`
93-
.suggestion = use `.addr()` to obtain the address of a pointer
94-
.help = if you can't comply with strict provenance and need to expose the pointer provenance you can use `.expose_addr()` instead
93+
.suggestion = use `.addr_without_provenance()` to obtain the address of a pointer without its provenance -- but note that this cannot be cast back to a pointer later; you need to use `with_addr` instead
94+
.help = if you need to cast the address back to an integer later, use `.expose_addr()` instead
9595
9696
hir_typeck_method_call_on_unknown_raw_pointee =
9797
cannot call a method on a raw pointer with an unknown pointee type

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,28 +256,32 @@ pub enum LossyProvenancePtr2IntSuggestion<'tcx> {
256256
NeedsParensCast {
257257
#[suggestion_part(code = "(")]
258258
expr_span: Span,
259-
#[suggestion_part(code = ").addr() as {cast_ty}")]
259+
#[suggestion_part(code = ").addr_without_provenance() as {cast_ty}")]
260260
cast_span: Span,
261261
cast_ty: Ty<'tcx>,
262262
},
263263
#[multipart_suggestion(hir_typeck_suggestion, applicability = "maybe-incorrect")]
264264
NeedsParens {
265265
#[suggestion_part(code = "(")]
266266
expr_span: Span,
267-
#[suggestion_part(code = ").addr()")]
267+
#[suggestion_part(code = ").addr_without_provenance()")]
268268
cast_span: Span,
269269
},
270270
#[suggestion(
271271
hir_typeck_suggestion,
272-
code = ".addr() as {cast_ty}",
272+
code = ".addr_without_provenance() as {cast_ty}",
273273
applicability = "maybe-incorrect"
274274
)]
275275
NeedsCast {
276276
#[primary_span]
277277
cast_span: Span,
278278
cast_ty: Ty<'tcx>,
279279
},
280-
#[suggestion(hir_typeck_suggestion, code = ".addr()", applicability = "maybe-incorrect")]
280+
#[suggestion(
281+
hir_typeck_suggestion,
282+
code = ".addr_without_provenance()",
283+
applicability = "maybe-incorrect"
284+
)]
281285
Other {
282286
#[primary_span]
283287
cast_span: Span,

compiler/rustc_middle/src/ty/generic_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'tcx> GenericArg<'tcx> {
149149
// pointers were originally created from `Interned` types in `pack()`,
150150
// and this is just going in the other direction.
151151
unsafe {
152-
match self.ptr.addr().get() & TAG_MASK {
152+
match self.ptr.addr_without_provenance().get() & TAG_MASK {
153153
REGION_TAG => GenericArgKind::Lifetime(ty::Region(Interned::new_unchecked(
154154
ptr.cast::<ty::RegionKind<'tcx>>().as_ref(),
155155
))),

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<'tcx> Term<'tcx> {
623623
// pointers were originally created from `Interned` types in `pack()`,
624624
// and this is just going in the other direction.
625625
unsafe {
626-
match self.ptr.addr().get() & TAG_MASK {
626+
match self.ptr.addr_without_provenance().get() & TAG_MASK {
627627
TYPE_TAG => TermKind::Ty(Ty(Interned::new_unchecked(
628628
ptr.cast::<WithCachedTypeInfo<ty::TyKind<'tcx>>>().as_ref(),
629629
))),

library/alloc/src/rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2840,7 +2840,7 @@ impl<T, A: Allocator> Weak<T, A> {
28402840
}
28412841

28422842
pub(crate) fn is_dangling<T: ?Sized>(ptr: *const T) -> bool {
2843-
(ptr.cast::<()>()).addr() == usize::MAX
2843+
(ptr.cast::<()>()).addr_without_provenance() == usize::MAX
28442844
}
28452845

28462846
/// Helper type to allow accessing the reference counts without

library/alloc/src/vec/into_iter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
222222
#[inline]
223223
fn size_hint(&self) -> (usize, Option<usize>) {
224224
let exact = if T::IS_ZST {
225-
self.end.addr().wrapping_sub(self.ptr.as_ptr().addr())
225+
self.end
226+
.addr_without_provenance()
227+
.wrapping_sub(self.ptr.as_ptr().addr_without_provenance())
226228
} else {
227229
unsafe { non_null!(self.end, T).sub_ptr(self.ptr) }
228230
};

library/core/src/hash/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ mod impls {
955955
#[inline]
956956
fn hash<H: Hasher>(&self, state: &mut H) {
957957
let (address, metadata) = self.to_raw_parts();
958-
state.write_usize(address.addr());
958+
state.write_usize(address.addr_without_provenance());
959959
metadata.hash(state);
960960
}
961961
}
@@ -965,7 +965,7 @@ mod impls {
965965
#[inline]
966966
fn hash<H: Hasher>(&self, state: &mut H) {
967967
let (address, metadata) = self.to_raw_parts();
968-
state.write_usize(address.addr());
968+
state.write_usize(address.addr_without_provenance());
969969
metadata.hash(state);
970970
}
971971
}

0 commit comments

Comments
 (0)