Skip to content

Commit 16daf7d

Browse files
committed
Ensure copy* intrinsics also perform the static self-init checks
1 parent 1d26f9a commit 16daf7d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,11 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
14011401
let src_parts = self.get_ptr_access(src, size)?;
14021402
let dest_parts = self.get_ptr_access(dest, size * num_copies)?; // `Size` multiplication
14031403

1404+
// And our own "do not read from static items while initializing them" checks
1405+
if let Ok((alloc_id, ..)) = self.ptr_try_get_alloc_id(src, size.bytes() as i64) {
1406+
M::before_alloc_read(self, alloc_id)?;
1407+
}
1408+
14041409
// FIXME: we look up both allocations twice here, once before for the `check_ptr_access`
14051410
// and once below to get the underlying `&[mut] Allocation`.
14061411

tests/ui/statics/read_before_init.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
//@ check-pass
2-
31
use std::mem::MaybeUninit;
42

53
pub static X: (i32, MaybeUninit<i32>) = (1, foo(&X.0));
4+
//~^ ERROR: encountered static that tried to initialize itself with itself
65

76
const fn foo(x: &i32) -> MaybeUninit<i32> {
87
let mut temp = MaybeUninit::<i32>::uninit();
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:3: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:9: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)