all memory behind a constant must be immutable
This commit is contained in:
parent
6137691933
commit
7b8693eff8
3 changed files with 53 additions and 1 deletions
|
|
@ -304,9 +304,14 @@ pub fn intern_const_alloc_recursive(
|
|||
|
||||
let mut todo: Vec<_> = leftover_relocations.iter().cloned().collect();
|
||||
while let Some(alloc_id) = todo.pop() {
|
||||
if let Some((_, alloc)) = ecx.memory_mut().alloc_map.remove(&alloc_id) {
|
||||
if let Some((_, mut alloc)) = ecx.memory_mut().alloc_map.remove(&alloc_id) {
|
||||
// We can't call the `intern_shallow` method here, as its logic is tailored to safe
|
||||
// references. So we hand-roll the interning logic here again.
|
||||
if base_intern_mode != InternMode::Static {
|
||||
// If it's not a static, it *must* be immutable.
|
||||
// We cannot have mutable memory inside a constant.
|
||||
alloc.mutability = Mutability::Immutable;
|
||||
}
|
||||
let alloc = tcx.intern_const_alloc(alloc);
|
||||
tcx.alloc_map.lock().set_alloc_id_memory(alloc_id, alloc);
|
||||
for &(_, ((), reloc)) in alloc.relocations().iter() {
|
||||
|
|
|
|||
20
src/test/ui/consts/miri_unleashed/mutable_const.rs
Normal file
20
src/test/ui/consts/miri_unleashed/mutable_const.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// compile-flags: -Zunleash-the-miri-inside-of-you
|
||||
|
||||
#![feature(const_raw_ptr_deref)]
|
||||
#![deny(const_err)]
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
|
||||
// make sure we do not just intern this as mutable
|
||||
const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
|
||||
|
||||
const MUTATING_BEHIND_RAW: () = {
|
||||
// Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
|
||||
unsafe {
|
||||
*MUTABLE_BEHIND_RAW = 99 //~ WARN skipping const checks
|
||||
//~^ ERROR any use of this value will cause an error
|
||||
//~^^ tried to modify constant memory
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {}
|
||||
27
src/test/ui/consts/miri_unleashed/mutable_const.stderr
Normal file
27
src/test/ui/consts/miri_unleashed/mutable_const.stderr
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
warning: skipping const checks
|
||||
--> $DIR/mutable_const.rs:14:9
|
||||
|
|
||||
LL | *MUTABLE_BEHIND_RAW = 99
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: any use of this value will cause an error
|
||||
--> $DIR/mutable_const.rs:14:9
|
||||
|
|
||||
LL | / const MUTATING_BEHIND_RAW: () = {
|
||||
LL | | // Test that `MUTABLE_BEHIND_RAW` is actually immutable, by doing this at const time.
|
||||
LL | | unsafe {
|
||||
LL | | *MUTABLE_BEHIND_RAW = 99
|
||||
| | ^^^^^^^^^^^^^^^^^^^^^^^^ tried to modify constant memory
|
||||
... |
|
||||
LL | | }
|
||||
LL | | };
|
||||
| |__-
|
||||
|
|
||||
note: lint level defined here
|
||||
--> $DIR/mutable_const.rs:4:9
|
||||
|
|
||||
LL | #![deny(const_err)]
|
||||
| ^^^^^^^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue