Skip to content

Ensure copy* intrinsics also perform the static self-init checks #142575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,12 @@ 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");

// Do our own "do not read from static items while initializing them" checks
if let Ok((alloc_id, ..)) = self.ptr_try_get_alloc_id(src, size.bytes() as i64) {
M::before_alloc_read(self, alloc_id)?;
}

// For the overlapping case, it is crucial that we trigger the read hook
// before the write hook -- the aliasing model cares about the order.
M::before_memory_read(
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/statics/read_before_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! 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<i32>) = (1, foo(&X.0));
//~^ ERROR: encountered static that tried to initialize itself with itself

const fn foo(x: &i32) -> MaybeUninit<i32> {
let mut temp = MaybeUninit::<i32>::uninit();
unsafe {
std::ptr::copy(x, temp.as_mut_ptr(), 1);
}
temp
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/statics/read_before_init.stderr
Original file line number Diff line number Diff line change
@@ -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<i32>) = (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::<i32>`
--> $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`.
Loading