Skip to content

Commit 69a320f

Browse files
committed
also validate everything that has a Scalar layout, to catch NonNull
1 parent 0a2fae6 commit 69a320f

File tree

8 files changed

+146
-16
lines changed

8 files changed

+146
-16
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@ fn scalar_format(value: ScalarMaybeUndef) -> String {
140140
}
141141

142142
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
143-
fn validate_scalar(
143+
/// Make sure that `value` is valid for `ty`
144+
fn validate_scalar_type(
144145
&self,
145146
value: ScalarMaybeUndef,
146147
size: Size,
147148
path: &Vec<PathElem>,
148149
ty: Ty,
149150
) -> EvalResult<'tcx> {
150-
trace!("validate scalar: {:#?}, {:#?}, {}", value, size, ty);
151+
trace!("validate scalar by type: {:#?}, {:#?}, {}", value, size, ty);
151152

152153
// Go over all the primitive types
153154
match ty.sty {
@@ -189,6 +190,62 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
189190
Ok(())
190191
}
191192

193+
/// Make sure that `value` matches the
194+
fn validate_scalar_layout(
195+
&self,
196+
value: ScalarMaybeUndef,
197+
size: Size,
198+
path: &Vec<PathElem>,
199+
layout: &layout::Scalar,
200+
) -> EvalResult<'tcx> {
201+
trace!("validate scalar by layout: {:#?}, {:#?}, {:#?}", value, size, layout);
202+
let (lo, hi) = layout.valid_range.clone().into_inner();
203+
if lo == u128::min_value() && hi == u128::max_value() {
204+
// Nothing to check
205+
return Ok(());
206+
}
207+
// At least one value is excluded. Get the bits.
208+
let value = try_validation!(value.not_undef(),
209+
scalar_format(value), path, format!("something in the range {:?}", layout.valid_range));
210+
let bits = match value {
211+
Scalar::Ptr(_) => {
212+
// Comparing a ptr with a range is not meaningfully possible.
213+
// In principle, *if* the pointer is inbonds, we could exclude NULL, but
214+
// that does not seem worth it.
215+
return Ok(());
216+
}
217+
Scalar::Bits { bits, size: value_size } => {
218+
assert_eq!(value_size as u64, size.bytes());
219+
bits
220+
}
221+
};
222+
// Now compare. This is slightly subtle because this is a special "wrap-around" range.
223+
use std::ops::RangeInclusive;
224+
let in_range = |bound: RangeInclusive<u128>| bound.contains(&bits);
225+
if lo > hi {
226+
// wrapping around
227+
if in_range(0..=hi) || in_range(lo..=u128::max_value()) {
228+
Ok(())
229+
} else {
230+
validation_failure!(
231+
bits,
232+
path,
233+
format!("something in the range {:?} or {:?}", 0..=hi, lo..=u128::max_value())
234+
)
235+
}
236+
} else {
237+
if in_range(layout.valid_range.clone()) {
238+
Ok(())
239+
} else {
240+
validation_failure!(
241+
bits,
242+
path,
243+
format!("something in the range {:?}", layout.valid_range)
244+
)
245+
}
246+
}
247+
}
248+
192249
/// Validate a reference, potentially recursively. `place` is assumed to already be
193250
/// dereferenced, i.e. it describes the target.
194251
fn validate_ref(
@@ -297,6 +354,22 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
297354
// Remember the length, in case we need to truncate
298355
let path_len = path.len();
299356

357+
// If this is a scalar, validate the scalar layout.
358+
// Things can be aggregates and have scalar layout at the same time, and that
359+
// is very relevant for `NonNull` and similar structs: We need to validate them
360+
// at their scalar layout *before* descending into their fields.
361+
match dest.layout.abi {
362+
layout::Abi::Uninhabited =>
363+
return validation_failure!("a value of an uninhabited type", path),
364+
layout::Abi::Scalar(ref layout) => {
365+
let value = try_validation!(self.read_scalar(dest),
366+
"uninitialized or unrepresentable data", path);
367+
self.validate_scalar_layout(value, dest.layout.size, &path, layout)?;
368+
}
369+
// FIXME: Should we do something for ScalarPair? Vector?
370+
_ => {}
371+
}
372+
300373
// Validate all fields
301374
match dest.layout.fields {
302375
// Primitives appear as Union with 0 fields -- except for fat pointers.
@@ -305,21 +378,26 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
305378
// fields to get a proper `path`.
306379
layout::FieldPlacement::Union(0) => {
307380
match dest.layout.abi {
308-
// nothing to do, whatever the pointer points to, it is never going to be read
309-
layout::Abi::Uninhabited =>
310-
return validation_failure!("a value of an uninhabited type", path),
311381
// check that the scalar is a valid pointer or that its bit range matches the
312382
// expectation.
313383
layout::Abi::Scalar(_) => {
314384
let value = try_validation!(self.read_value(dest),
315385
"uninitialized or unrepresentable data", path);
316-
let scalar = value.to_scalar_or_undef();
317-
self.validate_scalar(scalar, dest.layout.size, &path, dest.layout.ty)?;
386+
self.validate_scalar_type(
387+
value.to_scalar_or_undef(),
388+
dest.layout.size,
389+
&path,
390+
dest.layout.ty
391+
)?;
318392
// Recursively check *safe* references
319393
if dest.layout.ty.builtin_deref(true).is_some() &&
320394
!dest.layout.ty.is_unsafe_ptr()
321395
{
322-
self.validate_ref(self.ref_to_mplace(value)?, path, ref_tracking)?;
396+
self.validate_ref(
397+
self.ref_to_mplace(value)?,
398+
path,
399+
ref_tracking
400+
)?;
323401
}
324402
},
325403
_ => bug!("bad abi for FieldPlacement::Union(0): {:#?}", dest.layout.abi),

src/test/ui/consts/const-eval/transmute-const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: this static likely exhibits undefined behavior
22
--> $DIR/transmute-const.rs:15:1
33
|
44
LL | static FOO: bool = unsafe { mem::transmute(3u8) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3, but expected a boolean
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3, but expected something in the range 0..=1
66
|
77
= 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
88

src/test/ui/consts/const-eval/ub-enum.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ error[E0080]: this constant likely exhibits undefined behavior
1818
--> $DIR/ub-enum.rs:45:1
1919
|
2020
LL | const BAD_ENUM_CHAR : Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 4294967295 at .Some.0.1, but expected a valid unicode codepoint
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 4294967295 at .Some.0.1, but expected something in the range 0..=1114111
2222
|
2323
= 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
2424

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#![feature(const_transmute)]
12+
13+
use std::mem;
14+
use std::ptr::NonNull;
15+
use std::num::{NonZeroU8, NonZeroUsize};
16+
17+
const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
18+
//~^ ERROR this constant likely exhibits undefined behavior
19+
20+
const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
21+
//~^ ERROR this constant likely exhibits undefined behavior
22+
const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
23+
//~^ ERROR this constant likely exhibits undefined behavior
24+
25+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0080]: this constant likely exhibits undefined behavior
2+
--> $DIR/ub-nonnull.rs:17:1
3+
|
4+
LL | const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something in the range 1..=18446744073709551615
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[E0080]: this constant likely exhibits undefined behavior
10+
--> $DIR/ub-nonnull.rs:20:1
11+
|
12+
LL | const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something in the range 1..=255
14+
|
15+
= 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
16+
17+
error[E0080]: this constant likely exhibits undefined behavior
18+
--> $DIR/ub-nonnull.rs:22:1
19+
|
20+
LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something in the range 1..=18446744073709551615
22+
|
23+
= 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
24+
25+
error: aborting due to 3 previous errors
26+
27+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/union-ice.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | / const FIELD_PATH: Struct = Struct { //~ ERROR this constant likely exhibi
1313
LL | | a: 42,
1414
LL | | b: unsafe { UNION.field3 },
1515
LL | | };
16-
| |__^ type validation failed: encountered uninitialized bytes at .b, but expected initialized plain bits
16+
| |__^ type validation failed: encountered uninitialized bytes at .b, but expected something in the range 0..=18446744073709551615
1717
|
1818
= 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
1919

src/test/ui/consts/const-eval/union-ub.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: this constant likely exhibits undefined behavior
22
--> $DIR/union-ub.rs:36:1
33
|
44
LL | const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected a boolean
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected something in the range 0..=1
66
|
77
= 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
88

src/test/ui/union-ub-fat-ptr.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,31 @@ error[E0080]: this constant likely exhibits undefined behavior
6666
--> $DIR/union-ub-fat-ptr.rs:116:1
6767
|
6868
LL | const G: &Trait = &unsafe { BoolTransmute { val: 3 }.bl };
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>, but expected a boolean
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>, but expected something in the range 0..=1
7070
|
7171
= 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
7272

7373
error[E0080]: this constant likely exhibits undefined behavior
7474
--> $DIR/union-ub-fat-ptr.rs:119:1
7575
|
7676
LL | const H: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>[0], but expected a boolean
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>[0], but expected something in the range 0..=1
7878
|
7979
= 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
8080

8181
error[E0080]: this constant likely exhibits undefined behavior
8282
--> $DIR/union-ub-fat-ptr.rs:125:1
8383
|
8484
LL | const I2: &MySliceBool = &MySlice(unsafe { BoolTransmute { val: 3 }.bl }, [false]);
85-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>.0, but expected a boolean
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>.0, but expected something in the range 0..=1
8686
|
8787
= 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
8888

8989
error[E0080]: this constant likely exhibits undefined behavior
9090
--> $DIR/union-ub-fat-ptr.rs:128:1
9191
|
9292
LL | const I3: &MySliceBool = &MySlice(true, [unsafe { BoolTransmute { val: 3 }.bl }]);
93-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>.1[0], but expected a boolean
93+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3 at .<deref>.1[0], but expected something in the range 0..=1
9494
|
9595
= 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
9696

0 commit comments

Comments
 (0)