Skip to content

Commit 7b8693e

Browse files
committed
all memory behind a constant must be immutable
1 parent 6137691 commit 7b8693e

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/librustc_mir/interpret/intern.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,14 @@ pub fn intern_const_alloc_recursive(
304304

305305
let mut todo: Vec<_> = leftover_relocations.iter().cloned().collect();
306306
while let Some(alloc_id) = todo.pop() {
307-
if let Some((_, alloc)) = ecx.memory_mut().alloc_map.remove(&alloc_id) {
307+
if let Some((_, mut alloc)) = ecx.memory_mut().alloc_map.remove(&alloc_id) {
308308
// We can't call the `intern_shallow` method here, as its logic is tailored to safe
309309
// references. So we hand-roll the interning logic here again.
310+
if base_intern_mode != InternMode::Static {
311+
// If it's not a static, it *must* be immutable.
312+
// We cannot have mutable memory inside a constant.
313+
alloc.mutability = Mutability::Immutable;
314+
}
310315
let alloc = tcx.intern_const_alloc(alloc);
311316
tcx.alloc_map.lock().set_alloc_id_memory(alloc_id, alloc);
312317
for &(_, ((), reloc)) in alloc.relocations().iter() {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: -Zunleash-the-miri-inside-of-you
2+
3+
#![feature(const_raw_ptr_deref)]
4+
#![deny(const_err)]
5+
6+
use std::cell::UnsafeCell;
7+
8+
// make sure we do not just intern this as mutable
9+
const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
10+
11+
const MUTATING_BEHIND_RAW: () = {
12+
// Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
13+
unsafe {
14+
*MUTABLE_BEHIND_RAW = 99 //~ WARN skipping const checks
15+
//~^ ERROR any use of this value will cause an error
16+
//~^^ tried to modify constant memory
17+
}
18+
};
19+
20+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
warning: skipping const checks
2+
--> $DIR/mutable_const.rs:14:9
3+
|
4+
LL | *MUTABLE_BEHIND_RAW = 99
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: any use of this value will cause an error
8+
--> $DIR/mutable_const.rs:14:9
9+
|
10+
LL | / const MUTATING_BEHIND_RAW: () = {
11+
LL | | // Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
12+
LL | | unsafe {
13+
LL | | *MUTABLE_BEHIND_RAW = 99
14+
| | ^^^^^^^^^^^^^^^^^^^^^^^^ tried to modify constant memory
15+
... |
16+
LL | | }
17+
LL | | };
18+
| |__-
19+
|
20+
note: lint level defined here
21+
--> $DIR/mutable_const.rs:4:9
22+
|
23+
LL | #![deny(const_err)]
24+
| ^^^^^^^^^
25+
26+
error: aborting due to previous error
27+

0 commit comments

Comments
 (0)