Skip to content

Commit def0e9b

Browse files
committed
Fix ICE with ReadPointerAsBytes validation error
1 parent 5e91c4e commit def0e9b

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

compiler/rustc_mir/src/interpret/validity.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
515515
Ok(true)
516516
}
517517
ty::Float(_) | ty::Int(_) | ty::Uint(_) => {
518-
let value = self.ecx.read_scalar(value)?;
518+
let value = try_validation!(
519+
self.ecx.read_scalar(value),
520+
self.path,
521+
err_unsup!(ReadPointerAsBytes) => { "read of part of a pointer" },
522+
);
519523
// NOTE: Keep this in sync with the array optimization for int/float
520524
// types below!
521525
if self.ctfe_mode.is_some() {

src/test/ui/issues/issue-79690.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
union Transmute<T: Copy, U: Copy> {
2+
t: T,
3+
u: U,
4+
}
5+
trait Bar {
6+
fn bar(&self) -> u32;
7+
}
8+
struct Foo {
9+
foo: u32,
10+
bar: bool,
11+
}
12+
impl Bar for Foo {
13+
fn bar(&self) -> u32 {
14+
self.foo
15+
}
16+
}
17+
#[derive(Copy, Clone)]
18+
struct Fat<'a>(&'a Foo, &'static VTable);
19+
struct VTable {
20+
size: Foo,
21+
}
22+
const FOO: &dyn Bar = &Foo {
23+
foo: 128,
24+
bar: false,
25+
};
26+
const G: Fat = unsafe { Transmute { t: FOO }.u };
27+
//~^ ERROR it is undefined behavior to use this value
28+
29+
fn main() {}

src/test/ui/issues/issue-79690.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/issue-79690.rs:26:1
3+
|
4+
LL | const G: Fat = unsafe { Transmute { t: FOO }.u };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered read of part of a pointer at .1.<deref>.size.foo
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 rustc repository if you believe it should not be considered undefined behavior.
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)