From e159cf0c9e943c7f9b95f7c931abc825ee33f958 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 16 Jun 2025 10:35:59 +0000 Subject: [PATCH 1/2] Add regression test --- tests/ui/statics/read_before_init.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/ui/statics/read_before_init.rs diff --git a/tests/ui/statics/read_before_init.rs b/tests/ui/statics/read_before_init.rs new file mode 100644 index 0000000000000..02af783063ae9 --- /dev/null +++ b/tests/ui/statics/read_before_init.rs @@ -0,0 +1,15 @@ +//@ check-pass + +use std::mem::MaybeUninit; + +pub static X: (i32, MaybeUninit) = (1, foo(&X.0)); + +const fn foo(x: &i32) -> MaybeUninit { + let mut temp = MaybeUninit::::uninit(); + unsafe { + std::ptr::copy(x, temp.as_mut_ptr(), 1); + } + temp +} + +fn main() {} From cfc22cfffb7a75229752de6a76e2fc94791b5203 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 16 Jun 2025 10:52:29 +0000 Subject: [PATCH 2/2] Ensure copy* intrinsics also perform the static self-init checks --- .../rustc_const_eval/src/interpret/memory.rs | 7 ++++++- tests/ui/statics/read_before_init.rs | 9 ++++++++- tests/ui/statics/read_before_init.stderr | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/ui/statics/read_before_init.stderr diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 99a4bc1b7d6e8..36d1a413598db 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -1412,8 +1412,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { let src_alloc = self.get_alloc_raw(src_alloc_id)?; let src_range = alloc_range(src_offset, size); assert!(!self.memory.validation_in_progress.get(), "we can't be copying during validation"); - // For the overlapping case, it is crucial that we trigger the read hook + + // Trigger read hooks. + // For the overlapping case, it is crucial that we trigger the read hooks // before the write hook -- the aliasing model cares about the order. + if let Ok((alloc_id, ..)) = self.ptr_try_get_alloc_id(src, size.bytes() as i64) { + M::before_alloc_read(self, alloc_id)?; + } M::before_memory_read( tcx, &self.machine, diff --git a/tests/ui/statics/read_before_init.rs b/tests/ui/statics/read_before_init.rs index 02af783063ae9..d779ef6dffab2 100644 --- a/tests/ui/statics/read_before_init.rs +++ b/tests/ui/statics/read_before_init.rs @@ -1,8 +1,15 @@ -//@ check-pass +//! This test checks the one code path that does not go through +//! the regular CTFE memory access (as an optimization). We forgot +//! to duplicate the static item self-initialization check, allowing +//! reading from the uninitialized static memory before it was +//! initialized at the end of the static initializer. +//! +//! https://github.com/rust-lang/rust/issues/142532 use std::mem::MaybeUninit; pub static X: (i32, MaybeUninit) = (1, foo(&X.0)); +//~^ ERROR: encountered static that tried to initialize itself with itself const fn foo(x: &i32) -> MaybeUninit { let mut temp = MaybeUninit::::uninit(); diff --git a/tests/ui/statics/read_before_init.stderr b/tests/ui/statics/read_before_init.stderr new file mode 100644 index 0000000000000..aeebcf7d9ce5a --- /dev/null +++ b/tests/ui/statics/read_before_init.stderr @@ -0,0 +1,17 @@ +error[E0080]: encountered static that tried to initialize itself with itself + --> $DIR/read_before_init.rs:11:45 + | +LL | pub static X: (i32, MaybeUninit) = (1, foo(&X.0)); + | ^^^^^^^^^ evaluation of `X` failed inside this call + | +note: inside `foo` + --> $DIR/read_before_init.rs:17:9 + | +LL | std::ptr::copy(x, temp.as_mut_ptr(), 1); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: inside `std::ptr::copy::` + --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0080`.