Skip to content

Commit 598ae74

Browse files
committed
Auto merge of #2074 - RalfJung:tls-ro, r=RalfJung
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
2 parents 9d47a56 + 763ff1c commit 598ae74

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/thread.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use log::trace;
1010
use rustc_data_structures::fx::FxHashMap;
1111
use rustc_hir::def_id::DefId;
1212
use rustc_index::vec::{Idx, IndexVec};
13+
use rustc_middle::mir::Mutability;
1314

1415
use crate::sync::SynchronizationState;
1516
use crate::*;
@@ -571,9 +572,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
571572
throw_unsup_format!("foreign thread-local statics are not supported");
572573
}
573574
let allocation = tcx.eval_static_initializer(def_id)?;
575+
let mut allocation = allocation.inner().clone();
576+
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
577+
allocation.mutability = Mutability::Mut;
574578
// Create a fresh allocation with this content.
575-
let new_alloc =
576-
this.allocate_raw_ptr(allocation.inner().clone(), MiriMemoryKind::Tls.into());
579+
let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into());
577580
this.machine.threads.set_thread_local_alloc(def_id, new_alloc);
578581
Ok(new_alloc)
579582
}

tests/run-pass/concurrency/thread_locals.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ static mut A: u8 = 0;
1616
static mut B: u8 = 0;
1717
static mut C: u8 = 0;
1818

19+
// Regression test for https://github.com/rust-lang/rust/issues/96191.
20+
#[thread_local]
21+
static READ_ONLY: u8 = 42;
22+
1923
unsafe fn get_a_ref() -> *mut u8 {
2024
&mut A
2125
}
@@ -25,6 +29,8 @@ struct Sender(*mut u8);
2529
unsafe impl Send for Sender {}
2630

2731
fn main() {
32+
let _val = READ_ONLY;
33+
2834
let ptr = unsafe {
2935
let x = get_a_ref();
3036
*x = 5;

0 commit comments

Comments
 (0)