forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#122438 - jswrenn:check-referent-size, r=compiler-errors Safe Transmute: Require that source referent is smaller than destination `BikeshedIntrinsicFrom` currently models transmute-via-union; i.e., it attempts to provide a `where` bound for this function: ```rust pub unsafe fn transmute_via_union<Src, Dst>(src: Src) -> Dst { use core::mem::*; #[repr(C)] union Transmute<T, U> { src: ManuallyDrop<T>, dst: ManuallyDrop<U>, } let transmute = Transmute { src: ManuallyDrop::new(src) }; // SAFETY: The caller must guarantee that the transmutation is safe. let dst = transmute.dst; ManuallyDrop::into_inner(dst) } ``` A quirk of this model is that it admits padding extensions in value-to-value transmutation: The destination type can be bigger than the source type, so long as the excess consists of uninitialized bytes. However, this isn't permissible for reference-to-reference transmutations (introduced in rust-lang#110662) — extra referent bytes cannot come from thin air. This PR patches our analysis for reference-to-reference transmutations to require that the destination referent is no larger than the source referent. r? `@compiler-errors`
- Loading branch information
Showing
8 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//@ check-fail | ||
|
||
//! Reject extensions behind references. | ||
#![crate_type = "lib"] | ||
#![feature(transmutability)] | ||
|
||
mod assert { | ||
use std::mem::{Assume, BikeshedIntrinsicFrom}; | ||
|
||
pub fn is_transmutable<Src, Dst>() | ||
where | ||
Dst: BikeshedIntrinsicFrom< | ||
Src, | ||
{ | ||
Assume { | ||
alignment: true, | ||
lifetimes: true, | ||
safety: true, | ||
validity: true, | ||
} | ||
}, | ||
>, | ||
{ | ||
} | ||
} | ||
|
||
#[repr(C, packed)] | ||
struct Packed<T>(T); | ||
|
||
fn reject_extension() { | ||
#[repr(C, align(2))] | ||
struct Two(u8); | ||
|
||
#[repr(C, align(4))] | ||
struct Four(u8); | ||
|
||
// These two types differ in the number of trailing padding bytes they have. | ||
type Src = Packed<Two>; | ||
type Dst = Packed<Four>; | ||
|
||
const _: () = { | ||
use std::mem::size_of; | ||
assert!(size_of::<Src>() == 2); | ||
assert!(size_of::<Dst>() == 4); | ||
}; | ||
|
||
assert::is_transmutable::<&Src, &Dst>(); //~ ERROR cannot be safely transmuted | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/ui/transmutability/references/reject_extension.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0277]: `&Packed<Two>` cannot be safely transmuted into `&Packed<Four>` | ||
--> $DIR/reject_extension.rs:48:37 | ||
| | ||
LL | assert::is_transmutable::<&Src, &Dst>(); | ||
| ^^^^ The referent size of `&Packed<Two>` (2 bytes) is smaller than that of `&Packed<Four>` (4 bytes) | ||
| | ||
note: required by a bound in `is_transmutable` | ||
--> $DIR/reject_extension.rs:13:14 | ||
| | ||
LL | pub fn is_transmutable<Src, Dst>() | ||
| --------------- required by a bound in this function | ||
LL | where | ||
LL | Dst: BikeshedIntrinsicFrom< | ||
| ______________^ | ||
LL | | Src, | ||
LL | | { | ||
LL | | Assume { | ||
... | | ||
LL | | }, | ||
LL | | >, | ||
| |_________^ required by this bound in `is_transmutable` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters