-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Update transmute_copy to ub_checks and ?Sized
#155989
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ use crate::clone::TrivialClone; | |
| use crate::cmp::Ordering; | ||
| use crate::marker::{Destruct, DiscriminantKind}; | ||
| use crate::panic::const_assert; | ||
| use crate::ub_checks::assert_unsafe_precondition; | ||
| use crate::{clone, cmp, fmt, hash, intrinsics, ptr}; | ||
|
|
||
| mod alignment; | ||
|
|
@@ -1077,6 +1078,18 @@ pub const fn copy<T: Copy>(x: &T) -> T { | |
| /// | ||
| /// [ub]: ../../reference/behavior-considered-undefined.html | ||
| /// | ||
| /// If you have a raw pointer instead of a reference, you might be looking for | ||
| /// [`ptr::read_unaligned`]`::<Dst>(ptr.cast::<Dst>())` instead. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// - Requires `size_of_val::<Src>(src) >= size_of::<Dst>()` | ||
| /// - The first `size_of::<Dst>()` bytes behind `src` must be *readable* | ||
| /// - The first `size_of::<Dst>()` bytes behind `src` must be *valid* | ||
|
Member
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. Can you make "valid" a link to this so that the term is clearly defined? |
||
| /// when interpreted as a `Dst`. | ||
| /// - All safety invariants of the `Dst` type must be upheld. (For example, | ||
|
Member
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. "Safety invariants" is not a standard term (yet). See this for how we word the same concern elsewhere. |
||
| /// `{ transmute_copy::<String, String>(&x); x; }` is UB for the double-drop.) | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
|
|
@@ -1101,20 +1114,32 @@ pub const fn copy<T: Copy>(x: &T) -> T { | |
| /// | ||
| /// // The contents of 'foo_array' should not have changed | ||
| /// assert_eq!(foo_array, [10]); | ||
| /// | ||
| /// let bytes: &[u8] = &[1, 2, 3, 4, 5, 6, 7]; | ||
| /// assert_eq!( | ||
| /// unsafe { mem::transmute_copy::<[u8], u32>(bytes) }, | ||
| /// u32::from_ne_bytes(*bytes.first_chunk().unwrap()), | ||
| /// ); | ||
| /// ``` | ||
| #[inline] | ||
| #[must_use] | ||
| #[track_caller] | ||
| #[stable(feature = "rust1", since = "1.0.0")] | ||
| #[rustc_const_stable(feature = "const_transmute_copy", since = "1.74.0")] | ||
| pub const unsafe fn transmute_copy<Src, Dst>(src: &Src) -> Dst { | ||
| assert!( | ||
| size_of::<Src>() >= size_of::<Dst>(), | ||
| "cannot transmute_copy if Dst is larger than Src" | ||
| pub const unsafe fn transmute_copy<Src: ?Sized, Dst>(src: &Src) -> Dst { | ||
| // library UB because it's possible for the `Src` to be only a subset of the allocation | ||
| // and thus for a failure to not be immediate language UB | ||
| assert_unsafe_precondition!( | ||
| check_library_ub, | ||
| "cannot transmute_copy if Dst is larger than Src", | ||
| ( | ||
| src_size: usize = size_of_val::<Src>(src), | ||
| dst_size: usize = Dst::SIZE, | ||
| ) => src_size >= dst_size | ||
| ); | ||
|
|
||
| // If Dst has a higher alignment requirement, src might not be suitably aligned. | ||
| if align_of::<Dst>() > align_of::<Src>() { | ||
| if align_of::<Dst>() > align_of_val::<Src>(src) { | ||
|
Member
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. For unsized
Member
Author
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. Hmm, interesting question. I guess I could condition it on (We can't specialize on Sized-ness, right?) EDIT later: Note to self, include some codegen tests to demonstrate one way or the other.
Member
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. @RalfJung For my own edification, can you say more about why this comparison might not be optimized out in the unsized case?
Member
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. Because
Member
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 I think that makes sense. What is the alternative you're suggesting? You mention not having both branches, but which branch are you suggesting be eliminated? (To be clear, I am nearly certain that these questions are coming from a lack of understanding on my part.)
Member
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. Originally I meant to suggest that we should just always use But as @scottmcm mentioned there's an alternative: use
Member
Author
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. Good news, even at low opt-levels LLVM is smart and merges the loads or the memcpys so even with the branch in the rust code the right thing happens 🎉 Test added. (I still have doc fixes to do, though, so this PR is not ready yet.) |
||
| // SAFETY: `src` is a reference which is guaranteed to be valid for reads. | ||
| // The caller must guarantee that the actual transmutation is safe. | ||
| unsafe { ptr::read_unaligned(src as *const Src as *const Dst) } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| //@ compile-flags: -Copt-level=1 | ||
| //@ only-64bit | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| // Check that we don't have runtime alignment checks, just a safe alignment. | ||
| // (Rust emits a branch, but LLVM merges the loads from the arms.) | ||
|
|
||
| use std::mem::transmute_copy; | ||
|
|
||
| // CHECK-LABEL: @transmute_copy_i16_from_i32_slice | ||
| // CHECK-SAME: ptr{{.+}}%_0, | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: i64{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i16_from_i32_slice(x: &[i32]) -> [i16; 7] { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: @llvm.memcpy | ||
| // CHECK-SAME: align 2{{.+}}%_0, | ||
| // CHECK-SAME: align 4{{.+}}%x.0, | ||
| // CHECK-SAME: i64 14, | ||
| // CHECK-NEXT: ret void | ||
| transmute_copy(x) | ||
| } | ||
|
|
||
| // CHECK-LABEL: @transmute_copy_i32_from_i16_slice | ||
| // CHECK-SAME: ptr{{.+}}%_0, | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: i64{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i32_from_i16_slice(x: &[i16]) -> [i32; 3] { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: @llvm.memcpy | ||
| // CHECK-SAME: align 4{{.+}}%_0, | ||
| // CHECK-SAME: align 2{{.+}}%x.0, | ||
| // CHECK-SAME: i64 12, | ||
| // CHECK-NEXT: ret void | ||
| transmute_copy(x) | ||
| } | ||
|
|
||
| // CHECK-LABEL: i32 @transmute_copy_i32_from_dyn | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: ptr{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i32_from_dyn(x: &dyn std::fmt::Debug) -> i32 { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: [[TEMP:%.+]] = load i32, ptr %x.0, align 1 | ||
| // CHECK-NEXT: ret i32 [[TEMP]] | ||
| transmute_copy(x) | ||
| } | ||
|
|
||
| // CHECK-LABEL: @transmute_copy_i16_array_from_dyn | ||
| // CHECK-SAME: ptr{{.+}}%_0, | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: ptr{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i16_array_from_dyn(x: &dyn std::fmt::Debug) -> [i16; 7] { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: @llvm.memcpy | ||
| // CHECK-SAME: align 2{{.+}}%_0, | ||
| // CHECK-SAME: align 1{{.+}}%x.0, | ||
| // CHECK-SAME: i64 14, | ||
| // CHECK-NEXT: ret void | ||
| transmute_copy(x) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //@ run-crash | ||
| //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes | ||
| //@ error-pattern: unsafe precondition(s) violated: cannot transmute_copy if Dst is larger than Src | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| let _unused: u64 = std::mem::transmute_copy(&1_u8); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an odd syntax to recommend, with
Dstbeing duplicated. Why notptr.cast::<Dst>().read_unaligned()?