Skip to content

Commit 0a70924

Browse files
committed
fix UB in a test
also add an explicit test for the fact that a Option<WidePtr> has padding when it is None
1 parent 65c7090 commit 0a70924

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

Diff for: library/core/tests/mem.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,20 @@ fn offset_of_addr() {
773773
#[test]
774774
fn const_maybe_uninit_zeroed() {
775775
// Sanity check for `MaybeUninit::zeroed` in a realistic const situation (plugin array term)
776+
777+
// It is crucial that this type has no padding!
776778
#[repr(C)]
777779
struct Foo {
778-
a: Option<&'static str>,
780+
a: Option<&'static u8>,
779781
b: Bar,
780782
c: f32,
783+
_pad: u32,
781784
d: *const u8,
782785
}
786+
783787
#[repr(C)]
784788
struct Bar(usize);
789+
785790
struct FooPtr(*const Foo);
786791
unsafe impl Sync for FooPtr {}
787792

Diff for: src/tools/miri/tests/fail/uninit/padding-enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ fn main() { unsafe {
1818
// Turns out the discriminant is (currently) stored
1919
// in the 2nd pointer, so the first half is padding.
2020
let c = &p as *const _ as *const u8;
21-
let _val = *c.add(0); // Get the padding byte.
21+
let _val = *c.add(0); // Get a padding byte.
2222
//~^ERROR: uninitialized
2323
} }

Diff for: src/tools/miri/tests/fail/uninit/padding-enum.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
22
--> $DIR/padding-enum.rs:LL:CC
33
|
4-
LL | let _val = *c.add(0); // Get the padding byte.
4+
LL | let _val = *c.add(0); // Get a padding byte.
55
| ^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior

Diff for: src/tools/miri/tests/fail/uninit/padding-wide-ptr.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::mem;
2+
3+
// If this is `None`, the metadata becomes padding.
4+
type T = Option<&'static str>;
5+
6+
fn main() { unsafe {
7+
let mut p: mem::MaybeUninit<T> = mem::MaybeUninit::zeroed();
8+
// The copy when `T` is returned from `transmute` should destroy padding
9+
// (even when we use `write_unaligned`, which under the hood uses an untyped copy).
10+
p.as_mut_ptr().write_unaligned(mem::transmute((0usize, 0usize)));
11+
// Null epresents `None`.
12+
assert!(matches!(*p.as_ptr(), None));
13+
14+
// The second part, with the length, becomes padding.
15+
let c = &p as *const _ as *const u8;
16+
let _val = *c.add(mem::size_of::<*const u8>()); // Get a padding byte.
17+
//~^ERROR: uninitialized
18+
} }
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
2+
--> $DIR/padding-wide-ptr.rs:LL:CC
3+
|
4+
LL | let _val = *c.add(mem::size_of::<*const u8>()); // Get a padding byte.
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/padding-wide-ptr.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)