]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union-ub-fat-ptr.rs
Auto merge of #53584 - mcr431:Fix-#53525, r=varkor
[rust.git] / src / test / ui / union-ub-fat-ptr.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // normalize-stderr-test "alignment \d+" -> "alignment N"
12 // normalize-stderr-test "offset \d+" -> "offset N"
13 // normalize-stderr-test "allocation \d+" -> "allocation N"
14 // normalize-stderr-test "size \d+" -> "size N"
15
16 union BoolTransmute {
17   val: u8,
18   bl: bool,
19 }
20
21 #[repr(C)]
22 #[derive(Copy, Clone)]
23 struct SliceRepr {
24     ptr: *const u8,
25     len: usize,
26 }
27
28 #[repr(C)]
29 #[derive(Copy, Clone)]
30 struct BadSliceRepr {
31     ptr: *const u8,
32     len: &'static u8,
33 }
34
35 union SliceTransmute {
36     repr: SliceRepr,
37     bad: BadSliceRepr,
38     slice: &'static [u8],
39     str: &'static str,
40     my_str: &'static Str,
41 }
42
43 #[repr(C)]
44 #[derive(Copy, Clone)]
45 struct DynRepr {
46     ptr: *const u8,
47     vtable: *const u8,
48 }
49
50 #[repr(C)]
51 #[derive(Copy, Clone)]
52 struct DynRepr2 {
53     ptr: *const u8,
54     vtable: *const u64,
55 }
56
57 #[repr(C)]
58 #[derive(Copy, Clone)]
59 struct BadDynRepr {
60     ptr: *const u8,
61     vtable: usize,
62 }
63
64 union DynTransmute {
65     repr: DynRepr,
66     repr2: DynRepr2,
67     bad: BadDynRepr,
68     rust: &'static Trait,
69 }
70
71 trait Trait {}
72 impl Trait for bool {}
73
74 struct Str(str);
75
76 // OK
77 const A: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
78 // bad str
79 const B: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
80 //~^ ERROR this constant likely exhibits undefined behavior
81 // bad str
82 const C: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
83 //~^ ERROR this constant likely exhibits undefined behavior
84 // bad str in Str
85 const C2: &Str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.my_str};
86 //~^ ERROR this constant likely exhibits undefined behavior
87
88 // OK
89 const A2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.slice};
90 // bad slice
91 const B2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
92 //~^ ERROR this constant likely exhibits undefined behavior
93 // bad slice
94 const C3: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
95 //~^ ERROR this constant likely exhibits undefined behavior
96
97 // bad trait object
98 const D: &Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
99 //~^ ERROR this constant likely exhibits undefined behavior
100 // bad trait object
101 const E: &Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
102 //~^ ERROR this constant likely exhibits undefined behavior
103 // bad trait object
104 const F: &Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
105 //~^ ERROR this constant likely exhibits undefined behavior
106
107 // bad data *inside* the trait object
108 const G: &Trait = &unsafe { BoolTransmute { val: 3 }.bl };
109 //~^ ERROR this constant likely exhibits undefined behavior
110
111 // bad data *inside* the slice
112 const H: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
113 //~^ ERROR this constant likely exhibits undefined behavior
114
115 fn main() {
116 }