-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Ensure ptr::read
gets all the same LLVM load
metadata that dereferencing does
#109035
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
Changes from 1 commit
b2c717f
0b96fee
1f70bb8
87696fd
e7c6ad8
dfc3377
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2020,6 +2020,20 @@ extern "rust-intrinsic" { | |||||||||||
#[rustc_safe_intrinsic] | ||||||||||||
pub fn saturating_sub<T: Copy>(a: T, b: T) -> T; | ||||||||||||
|
||||||||||||
/// This is a *typed* read, `copy *p` in MIR. | ||||||||||||
/// | ||||||||||||
/// The stabilized form of this intrinsic is [`crate::ptr::read`], so | ||||||||||||
/// that can be implemented without needing to do an *untyped* copy | ||||||||||||
/// via [`copy_nonoverlapping`], and thus can get proper metadata. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
/// | ||||||||||||
/// This intrinsic can *only* be called with a copy or move of a local. | ||||||||||||
/// (It allows neither constants nor projections.) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This confused me a bit — at first I though that Maybe the docs can be clarified a bit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a strange (and very syntactic) restriction? Isn't there a high risk that some other program transformation might, for instance, turn let x = s.f;
read_via_copy(x) into read_via_copy(s.f) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That can be fixed via introducing a temporary if it becomes a problem (and Jakob mentioned that on... Zulip?), but it seems the current tendency in the MIR is to aggressively desugar everything and introduce temporaries everywhere, then roll them back up in opt passes. If the implementation works and produces less MIR than the alternative, I think there's a merit in not introducing One More Temporary for the crab to claw through. Maybe we should note why this "bug" was not "fixed", though, so that if anyone comes by and it needs to be fixed, they can immediately change it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is about as big an optimization footgun as can exist. What saves us is that this runs before optimizations, so they don't have to care There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still seems rather fragile, and needs at least a comment explaining the situation. |
||||||||||||
/// | ||||||||||||
/// To avoid introducing any `noalias` requirements, it just takes a pointer. | ||||||||||||
#[cfg(not(bootstrap))] | ||||||||||||
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")] | ||||||||||||
pub fn read_via_copy<T>(p: *const T) -> T; | ||||||||||||
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
/// Returns the value of the discriminant for the variant in 'v'; | ||||||||||||
/// if `T` has no discriminant, returns `0`. | ||||||||||||
/// | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1137,25 +1137,33 @@ pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T { | |
pub const unsafe fn read<T>(src: *const T) -> T { | ||
// We are calling the intrinsics directly to avoid function calls in the generated code | ||
// as `intrinsics::copy_nonoverlapping` is a wrapper function. | ||
#[cfg(bootstrap)] | ||
extern "rust-intrinsic" { | ||
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")] | ||
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize); | ||
} | ||
|
||
let mut tmp = MaybeUninit::<T>::uninit(); | ||
// SAFETY: the caller must guarantee that `src` is valid for reads. | ||
// `src` cannot overlap `tmp` because `tmp` was just allocated on | ||
// the stack as a separate allocated object. | ||
// | ||
// Also, since we just wrote a valid value into `tmp`, it is guaranteed | ||
// to be properly initialized. | ||
unsafe { | ||
assert_unsafe_precondition!( | ||
"ptr::read requires that the pointer argument is aligned and non-null", | ||
[T](src: *const T) => is_aligned_and_not_null(src) | ||
); | ||
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1); | ||
tmp.assume_init() | ||
|
||
#[cfg(bootstrap)] | ||
{ | ||
let mut tmp = MaybeUninit::<T>::uninit(); | ||
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1); | ||
tmp.assume_init() | ||
} | ||
#[cfg(not(bootstrap))] | ||
{ | ||
// This uses a dedicated intrinsic, not `copy_nonoverlapping`, | ||
// so that it gets a *typed* copy, not an *untyped* one. | ||
crate::intrinsics::read_via_copy(src) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JakobDegen it would be super nice if this entire PR was just mir!({
RET = *src;
Return()
}) Are there any plans to allow defining intrinsics with custom MIR? Maybe it's difficult because both are in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's https://stdrs.dev/nightly/x86_64-unknown-linux-gnu/std/intrinsics/mir/macro.mir.html (err, which of course you know because you used it in the example 🤦), but I don't know if that's something we'd ever want to use for productized things, rather than just in tests. |
||
} | ||
scottmcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// compile-flags: -O -Z merge-functions=disabled | ||
// no-system-llvm | ||
// ignore-debug (the extra assertions get in the way) | ||
|
||
#![crate_type = "lib"] | ||
|
||
// Ensure that various forms of reading pointers correctly annotate the `load`s | ||
// with `!noundef` metadata to enable extra optimization. The functions return | ||
// `MaybeUninit` to keep it from being inferred from the function type. | ||
scottmcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use std::mem::MaybeUninit; | ||
|
||
// CHECK-LABEL: define i8 @copy_byte( | ||
#[no_mangle] | ||
pub unsafe fn copy_byte(p: *const u8) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
MaybeUninit::new(*p) | ||
} | ||
|
||
// CHECK-LABEL: define i8 @read_byte( | ||
#[no_mangle] | ||
pub unsafe fn read_byte(p: *const u8) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
MaybeUninit::new(p.read()) | ||
} | ||
|
||
// CHECK-LABEL: define i8 @read_byte_maybe_uninit( | ||
#[no_mangle] | ||
pub unsafe fn read_byte_maybe_uninit(p: *const MaybeUninit<u8>) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-NOT: noundef | ||
// CHECK-NOT: load | ||
p.read() | ||
} | ||
|
||
// CHECK-LABEL: define i8 @read_byte_assume_init( | ||
#[no_mangle] | ||
pub unsafe fn read_byte_assume_init(p: &MaybeUninit<u8>) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
MaybeUninit::new(p.assume_init_read()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
- // MIR for `read_via_copy_primitive` before LowerIntrinsics | ||
+ // MIR for `read_via_copy_primitive` after LowerIntrinsics | ||
|
||
fn read_via_copy_primitive(_1: &i32) -> i32 { | ||
debug r => _1; // in scope 0 at $DIR/lower_intrinsics.rs:+0:32: +0:33 | ||
let mut _0: i32; // return place in scope 0 at $DIR/lower_intrinsics.rs:+0:44: +0:47 | ||
let mut _2: *const i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
scope 1 { | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
_2 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
- _0 = read_via_copy::<i32>(move _2) -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
- // mir::Constant | ||
- // + span: $DIR/lower_intrinsics.rs:85:14: 85:45 | ||
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const i32) -> i32 {read_via_copy::<i32>}, val: Value(<ZST>) } | ||
+ _0 = (*_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
+ goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
} | ||
|
||
bb1: { | ||
StorageDead(_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:47: +1:48 | ||
return; // scope 0 at $DIR/lower_intrinsics.rs:+2:2: +2:2 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- // MIR for `read_via_copy_uninhabited` before LowerIntrinsics | ||
+ // MIR for `read_via_copy_uninhabited` after LowerIntrinsics | ||
|
||
fn read_via_copy_uninhabited(_1: &Never) -> Never { | ||
debug r => _1; // in scope 0 at $DIR/lower_intrinsics.rs:+0:34: +0:35 | ||
let mut _0: Never; // return place in scope 0 at $DIR/lower_intrinsics.rs:+0:48: +0:53 | ||
let mut _2: *const Never; // in scope 0 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
scope 1 { | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
_2 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
- _0 = read_via_copy::<Never>(move _2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
- // mir::Constant | ||
- // + span: $DIR/lower_intrinsics.rs:90:14: 90:45 | ||
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const Never) -> Never {read_via_copy::<Never>}, val: Value(<ZST>) } | ||
+ _0 = (*_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
+ unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,11 +148,11 @@ LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) }; | |
HEX_DUMP | ||
} | ||
|
||
error: accessing memory with alignment 1, but alignment 4 is required | ||
error[E0080]: evaluation of constant value failed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, these future incompat warnings were because we wanted to make moving away from dubious const-eval patterns smoother as part of the Const UB Armistice of #99923, so that const UB doesn't immediately turn into const-break-the-build due to compiler changes. If people feel it's been enough time we can switch this off. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular PR just affects There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, general temperature was that for these UB-in- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amusingly, the "see issue" is pointing at #68585, which is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Purely if it was up to me: go for it. It seems like this will be a hugely beneficial change, crates impacted were still technically "doing it wrong", we're landing this no sooner than 1.70 (so they've had 1.68 and will have 1.69 to fix it), and "const-stable since 1.63" actually means that we should cut it off sooner rather than later due to the "Lindy effect" that bad code patterns have (i.e. the longer a pattern exists, the longer it is expected to continue existing). "More time to migrate" is something we should be considering for const fn stabilizations with version numbers like 1.49 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this is fine, we can by now probably make that entire lint into a hard error. What I don't understand immediately is why this PR changes behavior here though... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It changes behavior because unaligned
(and this PR changes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's the thing, unaligned derefs should also be temporarily allowed... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616> | ||
= note: accessing memory with alignment 1, but alignment 4 is required | ||
| | ||
note: inside `std::ptr::read::<u32>` | ||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
note: inside `ptr::const_ptr::<impl *const u32>::read` | ||
|
@@ -162,25 +162,7 @@ note: inside `UNALIGNED_READ` | |
| | ||
LL | ptr.read(); | ||
| ^^^^^^^^^^ | ||
= note: `#[deny(invalid_alignment)]` on by default | ||
|
||
error: aborting due to 15 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. | ||
Future incompatibility report: Future breakage diagnostic: | ||
error: accessing memory with alignment 1, but alignment 4 is required | ||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616> | ||
note: inside `std::ptr::read::<u32>` | ||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
note: inside `ptr::const_ptr::<impl *const u32>::read` | ||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | ||
note: inside `UNALIGNED_READ` | ||
--> $DIR/ub-ref-ptr.rs:67:5 | ||
| | ||
LL | ptr.read(); | ||
| ^^^^^^^^^^ | ||
= note: `#[deny(invalid_alignment)]` on by default | ||
|
Uh oh!
There was an error while loading. Please reload this page.