Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not consider thread-local allocations read-only #2074

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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