Skip to content

Commit 640e288

Browse files
committed
Panic on mutable allocs in constants
1 parent a916ac2 commit 640e288

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

src/librustc_mir/interpret/intern.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,19 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
320320
// We can't call the `intern_shallow` method here, as its logic is tailored to safe
321321
// references and a `leftover_allocations` set (where we only have a todo-list here).
322322
// So we hand-roll the interning logic here again.
323-
if base_intern_mode != InternMode::Static {
324-
// If it's not a static, it *must* be immutable.
325-
// We cannot have mutable memory inside a constant.
326-
// FIXME: ideally we would assert that they already are immutable, to double-
327-
// check our static checks.
328-
alloc.mutability = Mutability::Not;
323+
match base_intern_mode {
324+
InternMode::Static => {}
325+
InternMode::Const | InternMode::ConstBase => {
326+
// If it's not a static, it *must* be immutable.
327+
// We cannot have mutable memory inside a constant.
328+
// We use `delay_span_bug` here, because this can be reached in the presence
329+
// of fancy transmutes.
330+
if alloc.mutability == Mutability::Mut {
331+
// For better errors later, mark the allocation as immutable
332+
alloc.mutability = Mutability::Not;
333+
ecx.tcx.sess.delay_span_bug(ecx.tcx.span, "mutable allocation in constant");
334+
}
335+
}
329336
}
330337
let alloc = tcx.intern_const_alloc(alloc);
331338
tcx.alloc_map.lock().set_alloc_id_memory(alloc_id, alloc);
@@ -337,6 +344,8 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>(
337344
} else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) {
338345
// dangling pointer
339346
throw_unsup!(ValidationFailure("encountered dangling pointer in final constant".into()))
347+
} else if ecx.tcx.alloc_map.lock().get(alloc_id).is_none() {
348+
span_bug!(ecx.tcx.span, "encountered unknown alloc id {:?}", alloc_id);
340349
}
341350
}
342351
Ok(())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-flags: -Zunleash-the-miri-inside-of-you
2+
// failure-status: 101
3+
// rustc-env:RUST_BACKTRACE=0
4+
// normalize-stderr-test "note: rustc 1.* running on .*" -> "note: rustc VERSION running on TARGET"
5+
// normalize-stderr-test "note: compiler flags: .*" -> "note: compiler flags: FLAGS"
6+
// normalize-stderr-test "interpret/intern.rs:[0-9]*:[0-9]*" -> "interpret/intern.rs:LL:CC"
7+
8+
#![feature(const_raw_ptr_deref)]
9+
#![feature(const_mut_refs)]
10+
#![deny(const_err)]
11+
12+
use std::cell::UnsafeCell;
13+
14+
// make sure we do not just intern this as mutable
15+
const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
16+
//~^ WARN: skipping const checks
17+
//~| ERROR: mutable allocation in constant
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
warning: skipping const checks
2+
--> $DIR/mutable_const2.rs:15:38
3+
|
4+
LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
7+
error: internal compiler error: mutable allocation in constant
8+
--> $DIR/mutable_const2.rs:15:1
9+
|
10+
LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:347:17
14+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
15+
16+
error: internal compiler error: unexpected panic
17+
18+
note: the compiler unexpectedly panicked. this is a bug.
19+
20+
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
21+
22+
note: rustc VERSION running on TARGET
23+
24+
note: compiler flags: FLAGS
25+

0 commit comments

Comments
 (0)