Skip to content

Commit

Permalink
Auto merge of #2074 - RalfJung:tls-ro, r=RalfJung
Browse files Browse the repository at this point in the history
do not consider thread-local allocations read-only

They are not in read-only memory the way regular `static`s are.

Fixes rust-lang/rust#96191
  • Loading branch information
bors committed Apr 19, 2022
2 parents 9d47a56 + 763ff1c commit 598ae74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/run-pass/concurrency/thread_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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;
Expand Down

0 comments on commit 598ae74

Please sign in to comment.