Skip to content

Commit 543e757

Browse files
authored
Unrolled build for #142575
Rollup merge of #142575 - oli-obk:sneaky-self-init, r=RalfJung Ensure copy* intrinsics also perform the static self-init checks fixes #142532 r? `@RalfJung`
2 parents 5526a2f + cfc22cf commit 543e757

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
14121412
let src_alloc = self.get_alloc_raw(src_alloc_id)?;
14131413
let src_range = alloc_range(src_offset, size);
14141414
assert!(!self.memory.validation_in_progress.get(), "we can't be copying during validation");
1415-
// For the overlapping case, it is crucial that we trigger the read hook
1415+
1416+
// Trigger read hooks.
1417+
// For the overlapping case, it is crucial that we trigger the read hooks
14161418
// before the write hook -- the aliasing model cares about the order.
1419+
if let Ok((alloc_id, ..)) = self.ptr_try_get_alloc_id(src, size.bytes() as i64) {
1420+
M::before_alloc_read(self, alloc_id)?;
1421+
}
14171422
M::before_memory_read(
14181423
tcx,
14191424
&self.machine,

tests/ui/statics/read_before_init.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! This test checks the one code path that does not go through
2+
//! the regular CTFE memory access (as an optimization). We forgot
3+
//! to duplicate the static item self-initialization check, allowing
4+
//! reading from the uninitialized static memory before it was
5+
//! initialized at the end of the static initializer.
6+
//!
7+
//! https://github.com/rust-lang/rust/issues/142532
8+
9+
use std::mem::MaybeUninit;
10+
11+
pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0));
12+
//~^ ERROR: encountered static that tried to initialize itself with itself
13+
14+
const fn foo(x: &i32) -> MaybeUninit<i32> {
15+
let mut temp = MaybeUninit::<i32>::uninit();
16+
unsafe {
17+
std::ptr::copy(x, temp.as_mut_ptr(), 1);
18+
}
19+
temp
20+
}
21+
22+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: encountered static that tried to initialize itself with itself
2+
--> $DIR/read_before_init.rs:11:45
3+
|
4+
LL | pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0));
5+
| ^^^^^^^^^ evaluation of `X` failed inside this call
6+
|
7+
note: inside `foo`
8+
--> $DIR/read_before_init.rs:17:9
9+
|
10+
LL | std::ptr::copy(x, temp.as_mut_ptr(), 1);
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
note: inside `std::ptr::copy::<i32>`
13+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)