File tree 5 files changed +41
-3
lines changed
src/tools/miri/tests/fail/uninit
5 files changed +41
-3
lines changed Original file line number Diff line number Diff line change @@ -773,15 +773,20 @@ fn offset_of_addr() {
773
773
#[ test]
774
774
fn const_maybe_uninit_zeroed ( ) {
775
775
// Sanity check for `MaybeUninit::zeroed` in a realistic const situation (plugin array term)
776
+
777
+ // It is crucial that this type has no padding!
776
778
#[ repr( C ) ]
777
779
struct Foo {
778
- a : Option < & ' static str > ,
780
+ a : Option < & ' static u8 > ,
779
781
b : Bar ,
780
782
c : f32 ,
783
+ _pad : u32 ,
781
784
d : * const u8 ,
782
785
}
786
+
783
787
#[ repr( C ) ]
784
788
struct Bar ( usize ) ;
789
+
785
790
struct FooPtr ( * const Foo ) ;
786
791
unsafe impl Sync for FooPtr { }
787
792
Original file line number Diff line number Diff line change @@ -18,6 +18,6 @@ fn main() { unsafe {
18
18
// Turns out the discriminant is (currently) stored
19
19
// in the 2nd pointer, so the first half is padding.
20
20
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.
22
22
//~^ERROR: uninitialized
23
23
} }
Original file line number Diff line number Diff line change 1
1
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
2
2
--> $DIR/padding-enum.rs:LL:CC
3
3
|
4
- LL | let _val = *c.add(0); // Get the padding byte.
4
+ LL | let _val = *c.add(0); // Get a padding byte.
5
5
| ^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
6
6
|
7
7
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
Original file line number Diff line number Diff line change
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
+ } }
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments