]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/rec-align-u64.rs
Rollup merge of #93112 - pietroalbini:pa-cve-2022-21658-nightly, r=pietroalbini
[rust.git] / src / test / ui / structs-enums / rec-align-u64.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(unused_unsafe)]
4 // ignore-wasm32-bare seems unimportant to test
5
6 // Issue #2303
7
8 #![feature(intrinsics)]
9
10 use std::mem;
11
12 mod rusti {
13     extern "rust-intrinsic" {
14         pub fn pref_align_of<T>() -> usize;
15         pub fn min_align_of<T>() -> usize;
16     }
17 }
18
19 // This is the type with the questionable alignment
20 #[derive(Debug)]
21 struct Inner {
22     c64: u64
23 }
24
25 // This is the type that contains the type with the
26 // questionable alignment, for testing
27 #[derive(Debug)]
28 struct Outer {
29     c8: u8,
30     t: Inner
31 }
32
33
34 #[cfg(any(target_os = "android",
35           target_os = "dragonfly",
36           target_os = "emscripten",
37           target_os = "freebsd",
38           target_os = "fuchsia",
39           target_os = "illumos",
40           target_os = "linux",
41           target_os = "macos",
42           target_os = "netbsd",
43           target_os = "openbsd",
44           target_os = "solaris",
45           target_os = "vxworks"))]
46 mod m {
47     #[cfg(target_arch = "x86")]
48     pub mod m {
49         pub fn align() -> usize { 4 }
50         pub fn size() -> usize { 12 }
51     }
52
53     #[cfg(not(target_arch = "x86"))]
54     pub mod m {
55         pub fn align() -> usize { 8 }
56         pub fn size() -> usize { 16 }
57     }
58 }
59
60 #[cfg(target_env = "sgx")]
61 mod m {
62     #[cfg(target_arch = "x86_64")]
63     pub mod m {
64         pub fn align() -> usize { 8 }
65         pub fn size() -> usize { 16 }
66     }
67 }
68
69 #[cfg(target_os = "windows")]
70 mod m {
71     pub mod m {
72         pub fn align() -> usize { 8 }
73         pub fn size() -> usize { 16 }
74     }
75 }
76
77 pub fn main() {
78     unsafe {
79         let x = Outer {c8: 22, t: Inner {c64: 44}};
80
81         let y = format!("{:?}", x);
82
83         println!("align inner = {:?}", rusti::min_align_of::<Inner>());
84         println!("size outer = {:?}", mem::size_of::<Outer>());
85         println!("y = {:?}", y);
86
87         // per clang/gcc the alignment of `Inner` is 4 on x86.
88         assert_eq!(rusti::min_align_of::<Inner>(), m::m::align());
89
90         // per clang/gcc the size of `Outer` should be 12
91         // because `Inner`s alignment was 4.
92         assert_eq!(mem::size_of::<Outer>(), m::m::size());
93
94         assert_eq!(y, "Outer { c8: 22, t: Inner { c64: 44 } }".to_string());
95     }
96 }