Skip to content

Add a feature gate for allowing integers as the address of references in constants #72042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/librustc_mir/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,23 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
// alignment and size determined by the layout (size will be 0,
// alignment should take attributes into account).
.unwrap_or_else(|| (place.layout.size, place.layout.align.abi));

// Direct call to `check_ptr_access_align` checks alignment even on CTFE machines.
let ptr = self.ecx.memory.check_ptr_access_align(
place.ptr,
size,
Some(align),
CheckInAllocMsg::InboundsTest,
);
if let Err(InterpErrorInfo { kind: err_unsup!(ReadBytesAsPointer), ..}) = ptr {
if self.ecx.tcx.features().const_int_ref {
return Ok(());
// In CTFE we allow integers as the address of references.
// See https://github.com/rust-lang/rust/issues/63197 for details.
if self.ref_tracking_for_consts.is_some() {
if let Err(InterpErrorInfo { kind: err_unsup!(ReadBytesAsPointer), ..}) = ptr {
if self.ecx.tcx.features().const_int_ref {
return Ok(());
}
}
}
// Direct call to `check_ptr_access_align` checks alignment even on CTFE machines.
let ptr: Option<_> = try_validation!(
ptr,
self.path,
Expand All @@ -410,8 +415,15 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
{ "a dangling {} (address 0x{:x} is unallocated)", kind, i },
err_ub!(PointerOutOfBounds { .. }) =>
{ "a dangling {} (going beyond the bounds of its allocation)", kind },
err_unsup!(ReadBytesAsPointer) =>
{ "a dangling {} (created from integer). Add `#![feature(const_int_ref)]` to the crate attributes to enable", kind },
err_unsup!(ReadBytesAsPointer) => {
"a dangling {} (created from integer).{}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The . should also be moved to the nightly-only part.

kind,
if self.ecx.tcx.sess.opts.unstable_features.is_nightly_build() {
" Add `#![feature(const_int_ref)]` to the crate attributes to enable"
} else {
""
}
},
// This cannot happen during const-eval (because interning already detects
// dangling pointers), but it can happen in Miri.
err_ub!(PointerUseAfterFree(..)) =>
Expand Down