Skip to content

Commit 2eb637a

Browse files
committed
Extend check for UnsafeCell in consts to cover unions
A validity companion to changes from #90373.
1 parent 8baeddf commit 2eb637a

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::mir::interpret::InterpError;
1414
use rustc_middle::ty;
1515
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1616
use rustc_span::symbol::{sym, Symbol};
17+
use rustc_span::DUMMY_SP;
1718
use rustc_target::abi::{Abi, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange};
1819

1920
use std::hash::Hash;
@@ -736,9 +737,15 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
736737
#[inline(always)]
737738
fn visit_union(
738739
&mut self,
739-
_op: &OpTy<'tcx, M::PointerTag>,
740+
op: &OpTy<'tcx, M::PointerTag>,
740741
_fields: NonZeroUsize,
741742
) -> InterpResult<'tcx> {
743+
// Special check preventing `UnsafeCell` inside unions in the inner part of constants.
744+
if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { inner: true, .. })) {
745+
if !op.layout.ty.is_freeze(self.ecx.tcx.at(DUMMY_SP), self.ecx.param_env) {
746+
throw_validation_failure!(self.path, { "`UnsafeCell` in a `const`" });
747+
}
748+
}
742749
Ok(())
743750
}
744751

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/invalid-union.rs:41:1
3+
|
4+
LL | fn main() {
5+
| ^^^^^^^^^ type validation failed at .<deref>.y.<enum-variant(B)>.0: encountered `UnsafeCell` in a `const`
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: 4, align: 4) {
9+
╾─alloc7──╼ │ ╾──╼
10+
}
11+
12+
error: erroneous constant used
13+
--> $DIR/invalid-union.rs:42:25
14+
|
15+
LL | let _: &'static _ = &C;
16+
| ^^ referenced constant has errors
17+
|
18+
= note: `#[deny(const_err)]` on by default
19+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
20+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/invalid-union.rs:41:1
3+
|
4+
LL | fn main() {
5+
| ^^^^^^^^^ type validation failed at .<deref>.y.<enum-variant(B)>.0: encountered `UnsafeCell` in a `const`
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: 8, align: 8) {
9+
╾───────alloc7────────╼ │ ╾──────╼
10+
}
11+
12+
error: erroneous constant used
13+
--> $DIR/invalid-union.rs:42:25
14+
|
15+
LL | let _: &'static _ = &C;
16+
| ^^ referenced constant has errors
17+
|
18+
= note: `#[deny(const_err)]` on by default
19+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
20+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/invalid-union.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Check that constants with interior mutability inside unions are rejected
2+
// during validation.
3+
//
4+
// Note that this test case relies on undefined behaviour to construct a
5+
// constant with interior mutability that is "invisible" to the static checks.
6+
// If for some reason this approach no longer works, it is should be fine to
7+
// remove the test case.
8+
//
9+
// build-fail
10+
// stderr-per-bitwidth
11+
#![feature(const_mut_refs)]
12+
#![feature(const_ptr_offset)]
13+
#![feature(untagged_unions)]
14+
use std::cell::Cell;
15+
16+
#[repr(C)]
17+
struct S {
18+
x: u32,
19+
y: E,
20+
}
21+
22+
#[repr(u32)]
23+
enum E {
24+
A,
25+
B(U)
26+
}
27+
28+
union U {
29+
cell: Cell<u32>,
30+
}
31+
32+
const C: S = {
33+
let s = S { x: 0, y: E::A };
34+
// Go through an &u32 reference which is definitely not allowed to mutate anything.
35+
let p = &s.x as *const u32 as *mut u32;
36+
// Change enum tag to E::B.
37+
unsafe { *p.add(1) = 1 };
38+
s
39+
};
40+
41+
fn main() { //~ ERROR it is undefined behavior to use this value
42+
let _: &'static _ = &C; //~ ERROR erroneous constant used
43+
//~^ WARN this was previously accepted
44+
}

0 commit comments

Comments
 (0)