Skip to content

Commit 591a17d

Browse files
committed
Auto merge of #53831 - TheDarkula:pointer-check, r=oli-obk
Added pointer checking to sanity checks r? @oli-obk
2 parents f3bb231 + de0cd23 commit 591a17d

File tree

5 files changed

+361
-52
lines changed

5 files changed

+361
-52
lines changed

src/librustc_mir/interpret/validity.rs

+18
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
118118
bits
119119
},
120120
Scalar::Ptr(_) => {
121+
match ty.sty {
122+
ty::Bool |
123+
ty::Char |
124+
ty::Float(_) |
125+
ty::Int(_) |
126+
ty::Uint(_) => {
127+
return validation_failure!(
128+
"a pointer",
129+
path,
130+
format!("the type {}", ty.sty)
131+
);
132+
}
133+
ty::RawPtr(_) |
134+
ty::Ref(_, _, _) |
135+
ty::FnPtr(_) => {}
136+
_ => { unreachable!(); }
137+
}
138+
121139
let ptr_size = self.pointer_size();
122140
let ptr_max = u128::max_value() >> (128 - ptr_size.bits());
123141
return if lo > hi {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// only-x86_64
2+
3+
union Nonsense {
4+
u: usize,
5+
int_32_ref: &'static i32,
6+
uint_8: u8,
7+
uint_16: u16,
8+
uint_32: u32,
9+
uint_64: u64,
10+
uint_128: u128,
11+
int_8: i8,
12+
int_16: i16,
13+
int_32: i32,
14+
int_64: i64,
15+
int_128: i128,
16+
float_32: f32,
17+
float_64: f64,
18+
truthy_falsey: bool,
19+
character: char,
20+
stringy: &'static str,
21+
}
22+
23+
fn main() {
24+
const I32_REF_USIZE_UNION: usize = unsafe { Nonsense { int_32_ref: &3 }.u };
25+
//~^ ERROR this constant likely exhibits undefined behavior
26+
27+
const I32_REF_U8_UNION: u8 = unsafe { Nonsense { int_32_ref: &3 }.uint_8 };
28+
//~^ ERROR this constant cannot be used
29+
30+
const I32_REF_U16_UNION: u16 = unsafe { Nonsense { int_32_ref: &3 }.uint_16 };
31+
//~^ ERROR this constant cannot be used
32+
33+
const I32_REF_U32_UNION: u32 = unsafe { Nonsense { int_32_ref: &3 }.uint_32 };
34+
//~^ ERROR this constant cannot be used
35+
36+
const I32_REF_U64_UNION: u64 = unsafe { Nonsense { int_32_ref: &3 }.uint_64 };
37+
//~^ ERROR this constant likely exhibits undefined behavior
38+
39+
const I32_REF_U128_UNION: u128 = unsafe { Nonsense { int_32_ref: &3 }.uint_128 };
40+
//~^ ERROR this constant cannot be used
41+
42+
const I32_REF_I8_UNION: i8 = unsafe { Nonsense { int_32_ref: &3 }.int_8 };
43+
//~^ ERROR this constant cannot be used
44+
45+
const I32_REF_I16_UNION: i16 = unsafe { Nonsense { int_32_ref: &3 }.int_16 };
46+
//~^ ERROR this constant cannot be used
47+
48+
const I32_REF_I32_UNION: i32 = unsafe { Nonsense { int_32_ref: &3 }.int_32 };
49+
//~^ ERROR this constant cannot be used
50+
51+
const I32_REF_I64_UNION: i64 = unsafe { Nonsense { int_32_ref: &3 }.int_64 };
52+
//~^ ERROR this constant likely exhibits undefined behavior
53+
54+
const I32_REF_I128_UNION: i128 = unsafe { Nonsense { int_32_ref: &3 }.int_128 };
55+
//~^ ERROR this constant cannot be used
56+
57+
const I32_REF_F32_UNION: f32 = unsafe { Nonsense { int_32_ref: &3 }.float_32 };
58+
//~^ ERROR this constant cannot be used
59+
60+
const I32_REF_F64_UNION: f64 = unsafe { Nonsense { int_32_ref: &3 }.float_64 };
61+
//~^ ERROR this constant likely exhibits undefined behavior
62+
63+
const I32_REF_BOOL_UNION: bool = unsafe { Nonsense { int_32_ref: &3 }.truthy_falsey };
64+
//~^ ERROR this constant cannot be used
65+
66+
const I32_REF_CHAR_UNION: char = unsafe { Nonsense { int_32_ref: &3 }.character };
67+
//~^ ERROR this constant cannot be used
68+
69+
const STR_U8_UNION: u8 = unsafe { Nonsense { stringy: "3" }.uint_8 };
70+
//~^ ERROR this constant cannot be used
71+
72+
const STR_U16_UNION: u16 = unsafe { Nonsense { stringy: "3" }.uint_16 };
73+
//~^ ERROR this constant cannot be used
74+
75+
const STR_U32_UNION: u32 = unsafe { Nonsense { stringy: "3" }.uint_32 };
76+
//~^ ERROR this constant cannot be used
77+
78+
const STR_U64_UNION: u64 = unsafe { Nonsense { stringy: "3" }.uint_64 };
79+
//~^ ERROR this constant likely exhibits undefined behavior
80+
81+
const STR_U128_UNION: u128 = unsafe { Nonsense { stringy: "3" }.uint_128 };
82+
//~^ ERROR this constant cannot be used
83+
84+
const STR_I8_UNION: i8 = unsafe { Nonsense { stringy: "3" }.int_8 };
85+
//~^ ERROR this constant cannot be used
86+
87+
const STR_I16_UNION: i16 = unsafe { Nonsense { stringy: "3" }.int_16 };
88+
//~^ ERROR this constant cannot be used
89+
90+
const STR_I32_UNION: i32 = unsafe { Nonsense { stringy: "3" }.int_32 };
91+
//~^ ERROR this constant cannot be used
92+
93+
const STR_I64_UNION: i64 = unsafe { Nonsense { stringy: "3" }.int_64 };
94+
//~^ ERROR this constant likely exhibits undefined behavior
95+
96+
const STR_I128_UNION: i128 = unsafe { Nonsense { stringy: "3" }.int_128 };
97+
//~^ ERROR this constant cannot be used
98+
99+
const STR_F32_UNION: f32 = unsafe { Nonsense { stringy: "3" }.float_32 };
100+
//~^ ERROR this constant cannot be used
101+
102+
const STR_F64_UNION: f64 = unsafe { Nonsense { stringy: "3" }.float_64 };
103+
//~^ ERROR this constant likely exhibits undefined behavior
104+
105+
const STR_BOOL_UNION: bool = unsafe { Nonsense { stringy: "3" }.truthy_falsey };
106+
//~^ ERROR this constant cannot be used
107+
108+
const STR_CHAR_UNION: char = unsafe { Nonsense { stringy: "3" }.character };
109+
//~^ ERROR this constant cannot be used
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
error[E0080]: this constant likely exhibits undefined behavior
2+
--> $DIR/const-pointer-values-in-various-types.rs:24:5
3+
|
4+
LL | const I32_REF_USIZE_UNION: usize = unsafe { Nonsense { int_32_ref: &3 }.u };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type usize
6+
|
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
8+
9+
error: this constant cannot be used
10+
--> $DIR/const-pointer-values-in-various-types.rs:27:5
11+
|
12+
LL | const I32_REF_U8_UNION: u8 = unsafe { Nonsense { int_32_ref: &3 }.uint_8 };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^
14+
| |
15+
| a raw memory access tried to access part of a pointer value as raw bytes
16+
|
17+
= note: #[deny(const_err)] on by default
18+
19+
error: this constant cannot be used
20+
--> $DIR/const-pointer-values-in-various-types.rs:30:5
21+
|
22+
LL | const I32_REF_U16_UNION: u16 = unsafe { Nonsense { int_32_ref: &3 }.uint_16 };
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^
24+
| |
25+
| a raw memory access tried to access part of a pointer value as raw bytes
26+
27+
error: this constant cannot be used
28+
--> $DIR/const-pointer-values-in-various-types.rs:33:5
29+
|
30+
LL | const I32_REF_U32_UNION: u32 = unsafe { Nonsense { int_32_ref: &3 }.uint_32 };
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^
32+
| |
33+
| a raw memory access tried to access part of a pointer value as raw bytes
34+
35+
error[E0080]: this constant likely exhibits undefined behavior
36+
--> $DIR/const-pointer-values-in-various-types.rs:36:5
37+
|
38+
LL | const I32_REF_U64_UNION: u64 = unsafe { Nonsense { int_32_ref: &3 }.uint_64 };
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type u64
40+
|
41+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
42+
43+
error: this constant cannot be used
44+
--> $DIR/const-pointer-values-in-various-types.rs:39:5
45+
|
46+
LL | const I32_REF_U128_UNION: u128 = unsafe { Nonsense { int_32_ref: &3 }.uint_128 };
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes
48+
49+
error: this constant cannot be used
50+
--> $DIR/const-pointer-values-in-various-types.rs:42:5
51+
|
52+
LL | const I32_REF_I8_UNION: i8 = unsafe { Nonsense { int_32_ref: &3 }.int_8 };
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^
54+
| |
55+
| a raw memory access tried to access part of a pointer value as raw bytes
56+
57+
error: this constant cannot be used
58+
--> $DIR/const-pointer-values-in-various-types.rs:45:5
59+
|
60+
LL | const I32_REF_I16_UNION: i16 = unsafe { Nonsense { int_32_ref: &3 }.int_16 };
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^
62+
| |
63+
| a raw memory access tried to access part of a pointer value as raw bytes
64+
65+
error: this constant cannot be used
66+
--> $DIR/const-pointer-values-in-various-types.rs:48:5
67+
|
68+
LL | const I32_REF_I32_UNION: i32 = unsafe { Nonsense { int_32_ref: &3 }.int_32 };
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^
70+
| |
71+
| a raw memory access tried to access part of a pointer value as raw bytes
72+
73+
error[E0080]: this constant likely exhibits undefined behavior
74+
--> $DIR/const-pointer-values-in-various-types.rs:51:5
75+
|
76+
LL | const I32_REF_I64_UNION: i64 = unsafe { Nonsense { int_32_ref: &3 }.int_64 };
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type i64
78+
|
79+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
80+
81+
error: this constant cannot be used
82+
--> $DIR/const-pointer-values-in-various-types.rs:54:5
83+
|
84+
LL | const I32_REF_I128_UNION: i128 = unsafe { Nonsense { int_32_ref: &3 }.int_128 };
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes
86+
87+
error: this constant cannot be used
88+
--> $DIR/const-pointer-values-in-various-types.rs:57:5
89+
|
90+
LL | const I32_REF_F32_UNION: f32 = unsafe { Nonsense { int_32_ref: &3 }.float_32 };
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^
92+
| |
93+
| a raw memory access tried to access part of a pointer value as raw bytes
94+
95+
error[E0080]: this constant likely exhibits undefined behavior
96+
--> $DIR/const-pointer-values-in-various-types.rs:60:5
97+
|
98+
LL | const I32_REF_F64_UNION: f64 = unsafe { Nonsense { int_32_ref: &3 }.float_64 };
99+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type f64
100+
|
101+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
102+
103+
error: this constant cannot be used
104+
--> $DIR/const-pointer-values-in-various-types.rs:63:5
105+
|
106+
LL | const I32_REF_BOOL_UNION: bool = unsafe { Nonsense { int_32_ref: &3 }.truthy_falsey };
107+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------------^^^
108+
| |
109+
| a raw memory access tried to access part of a pointer value as raw bytes
110+
111+
error: this constant cannot be used
112+
--> $DIR/const-pointer-values-in-various-types.rs:66:5
113+
|
114+
LL | const I32_REF_CHAR_UNION: char = unsafe { Nonsense { int_32_ref: &3 }.character };
115+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^
116+
| |
117+
| a raw memory access tried to access part of a pointer value as raw bytes
118+
119+
error: this constant cannot be used
120+
--> $DIR/const-pointer-values-in-various-types.rs:69:5
121+
|
122+
LL | const STR_U8_UNION: u8 = unsafe { Nonsense { stringy: "3" }.uint_8 };
123+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------^^^
124+
| |
125+
| a raw memory access tried to access part of a pointer value as raw bytes
126+
127+
error: this constant cannot be used
128+
--> $DIR/const-pointer-values-in-various-types.rs:72:5
129+
|
130+
LL | const STR_U16_UNION: u16 = unsafe { Nonsense { stringy: "3" }.uint_16 };
131+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^
132+
| |
133+
| a raw memory access tried to access part of a pointer value as raw bytes
134+
135+
error: this constant cannot be used
136+
--> $DIR/const-pointer-values-in-various-types.rs:75:5
137+
|
138+
LL | const STR_U32_UNION: u32 = unsafe { Nonsense { stringy: "3" }.uint_32 };
139+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^
140+
| |
141+
| a raw memory access tried to access part of a pointer value as raw bytes
142+
143+
error[E0080]: this constant likely exhibits undefined behavior
144+
--> $DIR/const-pointer-values-in-various-types.rs:78:5
145+
|
146+
LL | const STR_U64_UNION: u64 = unsafe { Nonsense { stringy: "3" }.uint_64 };
147+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type u64
148+
|
149+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
150+
151+
error: this constant cannot be used
152+
--> $DIR/const-pointer-values-in-various-types.rs:81:5
153+
|
154+
LL | const STR_U128_UNION: u128 = unsafe { Nonsense { stringy: "3" }.uint_128 };
155+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^
156+
| |
157+
| a raw memory access tried to access part of a pointer value as raw bytes
158+
159+
error: this constant cannot be used
160+
--> $DIR/const-pointer-values-in-various-types.rs:84:5
161+
|
162+
LL | const STR_I8_UNION: i8 = unsafe { Nonsense { stringy: "3" }.int_8 };
163+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------^^^
164+
| |
165+
| a raw memory access tried to access part of a pointer value as raw bytes
166+
167+
error: this constant cannot be used
168+
--> $DIR/const-pointer-values-in-various-types.rs:87:5
169+
|
170+
LL | const STR_I16_UNION: i16 = unsafe { Nonsense { stringy: "3" }.int_16 };
171+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------^^^
172+
| |
173+
| a raw memory access tried to access part of a pointer value as raw bytes
174+
175+
error: this constant cannot be used
176+
--> $DIR/const-pointer-values-in-various-types.rs:90:5
177+
|
178+
LL | const STR_I32_UNION: i32 = unsafe { Nonsense { stringy: "3" }.int_32 };
179+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------^^^
180+
| |
181+
| a raw memory access tried to access part of a pointer value as raw bytes
182+
183+
error[E0080]: this constant likely exhibits undefined behavior
184+
--> $DIR/const-pointer-values-in-various-types.rs:93:5
185+
|
186+
LL | const STR_I64_UNION: i64 = unsafe { Nonsense { stringy: "3" }.int_64 };
187+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type i64
188+
|
189+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
190+
191+
error: this constant cannot be used
192+
--> $DIR/const-pointer-values-in-various-types.rs:96:5
193+
|
194+
LL | const STR_I128_UNION: i128 = unsafe { Nonsense { stringy: "3" }.int_128 };
195+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^
196+
| |
197+
| a raw memory access tried to access part of a pointer value as raw bytes
198+
199+
error: this constant cannot be used
200+
--> $DIR/const-pointer-values-in-various-types.rs:99:5
201+
|
202+
LL | const STR_F32_UNION: f32 = unsafe { Nonsense { stringy: "3" }.float_32 };
203+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^
204+
| |
205+
| a raw memory access tried to access part of a pointer value as raw bytes
206+
207+
error[E0080]: this constant likely exhibits undefined behavior
208+
--> $DIR/const-pointer-values-in-various-types.rs:102:5
209+
|
210+
LL | const STR_F64_UNION: f64 = unsafe { Nonsense { stringy: "3" }.float_64 };
211+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type f64
212+
|
213+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
214+
215+
error: this constant cannot be used
216+
--> $DIR/const-pointer-values-in-various-types.rs:105:5
217+
|
218+
LL | const STR_BOOL_UNION: bool = unsafe { Nonsense { stringy: "3" }.truthy_falsey };
219+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------------^^^
220+
| |
221+
| a raw memory access tried to access part of a pointer value as raw bytes
222+
223+
error: this constant cannot be used
224+
--> $DIR/const-pointer-values-in-various-types.rs:108:5
225+
|
226+
LL | const STR_CHAR_UNION: char = unsafe { Nonsense { stringy: "3" }.character };
227+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^
228+
| |
229+
| a raw memory access tried to access part of a pointer value as raw bytes
230+
231+
error: aborting due to 29 previous errors
232+
233+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)