-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also add an explicit test for the fact that a Option<WidePtr> has padding when it is None
- Loading branch information
Showing
4 changed files
with
35 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::mem; | ||
|
||
// If this is `None`, the metadata becomes padding. | ||
type T = Option<&'static str>; | ||
|
||
fn main() { unsafe { | ||
let mut p: mem::MaybeUninit<T> = mem::MaybeUninit::zeroed(); | ||
// The copy when `T` is returned from `transmute` should destroy padding | ||
// (even when we use `write_unaligned`, which under the hood uses an untyped copy). | ||
p.as_mut_ptr().write_unaligned(mem::transmute((0usize, 0usize))); | ||
// Null epresents `None`. | ||
assert!(matches!(*p.as_ptr(), None)); | ||
|
||
// The second part, with the length, becomes padding. | ||
let c = &p as *const _ as *const u8; | ||
let _val = *c.add(mem::size_of::<*const u8>()); // Get a padding byte. | ||
//~^ERROR: uninitialized | ||
} } |
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,15 @@ | ||
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory | ||
--> $DIR/padding-wide-ptr.rs:LL:CC | ||
| | ||
LL | let _val = *c.add(mem::size_of::<*const u8>()); // Get a padding byte. | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
= note: BACKTRACE: | ||
= note: inside `main` at $DIR/padding-wide-ptr.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|