Skip to content

Commit 8f07a4f

Browse files
committed
const validation: fix ICE on dangling ZST reference
1 parent f158600 commit 8f07a4f

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_target::abi::{
2929
use std::hash::Hash;
3030

3131
use super::{
32-
err_ub, format_interp_error, machine::AllocMap, throw_ub, AllocId, CheckInAllocMsg,
32+
err_ub, format_interp_error, machine::AllocMap, throw_ub, AllocId, AllocKind, CheckInAllocMsg,
3333
GlobalAlloc, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy, Machine, MemPlaceMeta, OpTy,
3434
Pointer, Projectable, Scalar, ValueVisitor,
3535
};
@@ -413,8 +413,6 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
413413
Ub(PointerOutOfBounds { .. }) => DanglingPtrOutOfBounds {
414414
ptr_kind
415415
},
416-
// This cannot happen during const-eval (because interning already detects
417-
// dangling pointers), but it can happen in Miri.
418416
Ub(PointerUseAfterFree(..)) => DanglingPtrUseAfterFree {
419417
ptr_kind,
420418
},
@@ -493,9 +491,15 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
493491
}
494492
}
495493

496-
// Mutability check.
494+
// Dangling and Mutability check.
495+
let (size, _align, alloc_kind) = self.ecx.get_alloc_info(alloc_id);
496+
if alloc_kind == AllocKind::Dead {
497+
// This can happen for zero-sized references. We can't have *any* references to non-existing
498+
// allocations though, interning rejects them all as the rest of rustc isn't happy with them...
499+
// so we throw an error, even though this isn't really UB.
500+
throw_validation_failure!(self.path, DanglingPtrUseAfterFree { ptr_kind });
501+
}
497502
// If this allocation has size zero, there is no actual mutability here.
498-
let (size, _align, _alloc_kind) = self.ecx.get_alloc_info(alloc_id);
499503
if size != Size::ZERO {
500504
let alloc_actual_mutbl = mutability(self.ecx, alloc_id);
501505
// Mutable pointer to immutable memory is no good.

tests/ui/consts/dangling-alloc-id-ice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ union Foo<'a> {
1010
}
1111

1212
const FOO: &() = {
13-
//~^ ERROR encountered dangling pointer
13+
//~^ ERROR it is undefined behavior to use this value
1414
let y = ();
1515
unsafe { Foo { y: &y }.long_live_the_unit }
1616
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
error: encountered dangling pointer in final value of constant
1+
error[E0080]: it is undefined behavior to use this value
22
--> $DIR/dangling-alloc-id-ice.rs:12:1
33
|
44
LL | const FOO: &() = {
5-
| ^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (use-after-free)
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+
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
9+
HEX_DUMP
10+
}
611

712
error: aborting due to 1 previous error
813

14+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Strip out raw byte dumps to make comparison platform-independent:
2+
//@ normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
3+
//@ normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*A(LLOC)?[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
4+
//@ normalize-stderr-test "HEX_DUMP\s*\n\s*HEX_DUMP" -> "HEX_DUMP"
5+
6+
pub struct Wrapper;
7+
pub static MAGIC_FFI_REF: &'static Wrapper = unsafe {
8+
//~^ERROR: it is undefined behavior to use this value
9+
std::mem::transmute(&{
10+
let y = 42;
11+
y
12+
})
13+
};
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/dangling-zst-ice-issue-126393.rs:7:1
3+
|
4+
LL | pub static MAGIC_FFI_REF: &'static Wrapper = unsafe {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (use-after-free)
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+
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
9+
HEX_DUMP
10+
}
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)