Skip to content

Commit 5280d15

Browse files
authored
Rollup merge of #71164 - RalfJung:uninit-not-undef, r=oli-obk
reword Miri validity errors: undefined -> uninitialized I don't think we say "undefined value" or anything like that anywhere in the docs or so, but we do use the term "uninitialized memory", so I think we should do the same here. Longer-term, I think we should also internally rename "undef" to "uninit". r? @oli-obk
2 parents d194587 + 69423bf commit 5280d15

File tree

6 files changed

+126
-11
lines changed

6 files changed

+126
-11
lines changed

src/librustc_mir/interpret/validity.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,11 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
322322
let value = self.ecx.read_immediate(value)?;
323323
// Handle wide pointers.
324324
// Check metadata early, for better diagnostics
325-
let place = try_validation!(self.ecx.ref_to_mplace(value), "undefined pointer", self.path);
325+
let place = try_validation!(
326+
self.ecx.ref_to_mplace(value),
327+
format_args!("uninitialized {}", kind),
328+
self.path
329+
);
326330
if place.layout.is_unsized() {
327331
self.check_wide_ptr_meta(place.meta, place.layout)?;
328332
}
@@ -334,7 +338,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
334338
format_args!("invalid {} metadata: {}", kind, msg),
335339
self.path
336340
),
337-
_ => bug!("Unexpected error during ptr size_and_align_of: {}", err),
341+
_ => bug!("unexpected error during ptr size_and_align_of: {}", err),
338342
},
339343
};
340344
let (size, align) = size_and_align
@@ -477,10 +481,11 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
477481
}
478482
ty::RawPtr(..) => {
479483
// We are conservative with undef for integers, but try to
480-
// actually enforce our current rules for raw pointers.
484+
// actually enforce the strict rules for raw pointers (mostly because
485+
// that lets us re-use `ref_to_mplace`).
481486
let place = try_validation!(
482487
self.ecx.ref_to_mplace(self.ecx.read_immediate(value)?),
483-
"undefined pointer",
488+
"uninitialized raw pointer",
484489
self.path
485490
);
486491
if place.layout.is_unsized() {
@@ -776,14 +781,14 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
776781
// For some errors we might be able to provide extra information
777782
match err.kind {
778783
err_ub!(InvalidUndefBytes(Some(ptr))) => {
779-
// Some byte was undefined, determine which
784+
// Some byte was uninitialized, determine which
780785
// element that byte belongs to so we can
781786
// provide an index.
782787
let i = usize::try_from(ptr.offset.bytes() / layout.size.bytes())
783788
.unwrap();
784789
self.path.push(PathElem::ArrayElem(i));
785790

786-
throw_validation_failure!("undefined bytes", self.path)
791+
throw_validation_failure!("uninitialized bytes", self.path)
787792
}
788793
// Other errors shouldn't be possible
789794
_ => return Err(err),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#![feature(const_transmute)]
2+
#![allow(const_err)] // make sure we cannot allow away the errors tested here
3+
4+
//! Test the "array of int" fast path in validity checking, and in particular whether it
5+
//! points at the right array element.
6+
7+
use std::mem;
8+
9+
#[repr(C)]
10+
union MaybeUninit<T: Copy> {
11+
uninit: (),
12+
init: T,
13+
}
14+
15+
const UNINIT_INT_0: [u32; 3] = unsafe {
16+
//~^ ERROR it is undefined behavior to use this value
17+
//~| type validation failed: encountered uninitialized bytes at [0]
18+
[
19+
MaybeUninit { uninit: () }.init,
20+
1,
21+
2,
22+
]
23+
};
24+
const UNINIT_INT_1: [u32; 3] = unsafe {
25+
//~^ ERROR it is undefined behavior to use this value
26+
//~| type validation failed: encountered uninitialized bytes at [1]
27+
mem::transmute(
28+
[
29+
0u8,
30+
0u8,
31+
0u8,
32+
0u8,
33+
1u8,
34+
MaybeUninit { uninit: () }.init,
35+
1u8,
36+
1u8,
37+
2u8,
38+
2u8,
39+
MaybeUninit { uninit: () }.init,
40+
2u8,
41+
]
42+
)
43+
};
44+
const UNINIT_INT_2: [u32; 3] = unsafe {
45+
//~^ ERROR it is undefined behavior to use this value
46+
//~| type validation failed: encountered uninitialized bytes at [2]
47+
mem::transmute(
48+
[
49+
0u8,
50+
0u8,
51+
0u8,
52+
0u8,
53+
1u8,
54+
1u8,
55+
1u8,
56+
1u8,
57+
2u8,
58+
2u8,
59+
2u8,
60+
MaybeUninit { uninit: () }.init,
61+
]
62+
)
63+
};
64+
65+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/ub-int-array.rs:15:1
3+
|
4+
LL | / const UNINIT_INT_0: [u32; 3] = unsafe {
5+
LL | |
6+
LL | |
7+
LL | | [
8+
... |
9+
LL | | ]
10+
LL | | };
11+
| |__^ type validation failed: encountered uninitialized bytes at [0]
12+
|
13+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
14+
15+
error[E0080]: it is undefined behavior to use this value
16+
--> $DIR/ub-int-array.rs:24:1
17+
|
18+
LL | / const UNINIT_INT_1: [u32; 3] = unsafe {
19+
LL | |
20+
LL | |
21+
LL | | mem::transmute(
22+
... |
23+
LL | | )
24+
LL | | };
25+
| |__^ type validation failed: encountered uninitialized bytes at [1]
26+
|
27+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
28+
29+
error[E0080]: it is undefined behavior to use this value
30+
--> $DIR/ub-int-array.rs:44:1
31+
|
32+
LL | / const UNINIT_INT_2: [u32; 3] = unsafe {
33+
LL | |
34+
LL | |
35+
LL | | mem::transmute(
36+
... |
37+
LL | | )
38+
LL | | };
39+
| |__^ type validation failed: encountered uninitialized bytes at [2]
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 rustc repository if you believe it should not be considered undefined behavior.
42+
43+
error: aborting due to 3 previous errors
44+
45+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/ub-ref.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::mem;
66

77
const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
88
//~^ ERROR it is undefined behavior to use this value
9-
//~^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
9+
//~| type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
1010

1111
const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
1212
//~^ ERROR it is undefined behavior to use this value
13-
//~^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
13+
//~| type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
1414

1515
const NULL: &u16 = unsafe { mem::transmute(0usize) };
1616
//~^ ERROR it is undefined behavior to use this value

src/test/ui/consts/const-eval/ub-wide-ptr.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ LL | |
6262
LL | | let uninit_len = MaybeUninit::<usize> { uninit: () };
6363
LL | | mem::transmute((42, uninit_len))
6464
LL | | };
65-
| |__^ type validation failed: encountered undefined pointer
65+
| |__^ type validation failed: encountered uninitialized reference
6666
|
6767
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
6868

@@ -130,7 +130,7 @@ LL | |
130130
LL | | let uninit_len = MaybeUninit::<usize> { uninit: () };
131131
LL | | mem::transmute((42, uninit_len))
132132
LL | | };
133-
| |__^ type validation failed: encountered undefined pointer
133+
| |__^ type validation failed: encountered uninitialized raw pointer
134134
|
135135
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
136136

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LL | | unsafe { UNION.field3 },
2727
... |
2828
LL | | a: 42,
2929
LL | | };
30-
| |__^ type validation failed: encountered undefined bytes at .b[1]
30+
| |__^ type validation failed: encountered uninitialized bytes at .b[1]
3131
|
3232
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
3333

0 commit comments

Comments
 (0)