-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
safe transmute: require that src referent is smaller than dst
The source referent absolutely must be smaller than the destination referent of a ref-to-ref transmute; the excess bytes referenced cannot arise from thin air, even if those bytes are uninitialized.
- 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<Two>` (2 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