do not consider thread-local allocations read-only

This commit is contained in:
Ralf Jung 2022-04-19 14:56:07 -04:00
parent 9d47a5633a
commit 763ff1c49f
2 changed files with 11 additions and 2 deletions

View File

@ -10,6 +10,7 @@ use log::trace;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::DefId;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::mir::Mutability;
use crate::sync::SynchronizationState;
use crate::*;
@ -571,9 +572,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
throw_unsup_format!("foreign thread-local statics are not supported");
}
let allocation = tcx.eval_static_initializer(def_id)?;
let mut allocation = allocation.inner().clone();
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
allocation.mutability = Mutability::Mut;
// Create a fresh allocation with this content.
let new_alloc =
this.allocate_raw_ptr(allocation.inner().clone(), MiriMemoryKind::Tls.into());
let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into());
this.machine.threads.set_thread_local_alloc(def_id, new_alloc);
Ok(new_alloc)
}

View File

@ -16,6 +16,10 @@ static mut A: u8 = 0;
static mut B: u8 = 0;
static mut C: u8 = 0;
// Regression test for https://github.com/rust-lang/rust/issues/96191.
#[thread_local]
static READ_ONLY: u8 = 42;
unsafe fn get_a_ref() -> *mut u8 {
&mut A
}
@ -25,6 +29,8 @@ struct Sender(*mut u8);
unsafe impl Send for Sender {}
fn main() {
let _val = READ_ONLY;
let ptr = unsafe {
let x = get_a_ref();
*x = 5;