about summary refs log tree commit diff
path: root/src/tools/miri/tests/fail/uninit/padding-pair.rs
blob: 70ae48ff77d9d64f04ee102490ec23c6c0085155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//@ normalize-stderr-test: "(\n)ALLOC \(.*\) \{\n(.*\n)*\}(\n)" -> "${1}ALLOC DUMP${3}"
//@ normalize-stderr-test: "\[0x[0-9a-z]..0x[0-9a-z]\]" -> "[0xX..0xY]"

#![feature(core_intrinsics)]

use std::mem::{self, MaybeUninit};

fn main() {
    // This constructs a `(usize, bool)` pair: 9 bytes initialized, the rest not.
    // Ensure that these 9 bytes are indeed initialized, and the rest is indeed not.
    // This should be the case even if we write into previously initialized storage.
    let mut x: MaybeUninit<Box<[u8]>> = MaybeUninit::zeroed();
    let z = std::intrinsics::add_with_overflow(0usize, 0usize);
    unsafe { x.as_mut_ptr().cast::<(usize, bool)>().write(z) };
    // Now read this bytewise. There should be (`ptr_size + 1`) def bytes followed by
    // (`ptr_size - 1`) undef bytes (the padding after the bool) in there.
    let z: *const u8 = &x as *const _ as *const _;
    let first_undef = mem::size_of::<usize>() as isize + 1;
    for i in 0..first_undef {
        let byte = unsafe { *z.offset(i) };
        assert_eq!(byte, 0);
    }
    let v = unsafe { *z.offset(first_undef) };
    //~^ ERROR: uninitialized
    if v == 0 {
        println!("it is zero");
    }
}