Skip to content

Commit 7e6dbd2

Browse files
author
Vytautas Astrauskas
committed
Address reviewers' comments: replace resolve_thread_local_allocation_id with resolve_maybe_global_alloc, clarify comments.
1 parent c53210c commit 7e6dbd2

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

src/librustc_mir/interpret/machine.rs

+26-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::borrow::{Borrow, Cow};
66
use std::hash::Hash;
77

88
use rustc_middle::mir;
9-
use rustc_middle::ty::{self, Ty};
9+
use rustc_middle::ty::{self, query::TyCtxtAt, Ty};
1010
use rustc_span::def_id::DefId;
1111

1212
use super::{
@@ -229,29 +229,41 @@ pub trait Machine<'mir, 'tcx>: Sized {
229229
Ok(())
230230
}
231231

232-
/// Called for *every* memory access to determine the real ID of the given allocation.
233-
/// This provides a way for the machine to "redirect" certain allocations as it sees fit.
232+
/// Called for *every* memory access to determine the real ID of the given
233+
/// allocation. This provides a way for the machine to "redirect" certain
234+
/// allocations as it sees fit.
234235
///
235-
/// This is used by Miri to redirect extern statics to real allocations.
236+
/// This is used by Miri for two purposes:
237+
/// 1. Redirecting extern statics to real allocations.
238+
/// 2. Creating unique allocation ids for thread locals.
239+
///
240+
/// In Rust, one way for creating a thread local is by marking a static
241+
/// with `#[thread_local]`. On supported platforms this gets translated
242+
/// to a LLVM thread local. The problem with supporting these thread
243+
/// locals in Miri is that in the internals of the compiler they look as
244+
/// normal statics, except that they have the `thread_local` attribute.
245+
/// However, in Miri we want to have a property that each allocation has
246+
/// a unique id and, therefore, for these thread locals we generate a
247+
/// fresh allocation id for each thread.
236248
///
237249
/// This function must be idempotent.
238250
#[inline]
239251
fn canonical_alloc_id(_mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
240252
id
241253
}
242254

243-
/// In Rust, thread locals are just special statics. Therefore, the compiler
244-
/// uses the same code for allocating both. However, in Miri we want to have
245-
/// a property that each allocation has a unique id and, therefore, we
246-
/// generate a fresh allocation id for each thread. This function takes a
247-
/// potentially thread local allocation id and resolves the original static
248-
/// allocation id that can be used to compute the value of the static.
249-
#[inline]
250-
fn resolve_thread_local_allocation_id(
255+
/// Called to obtain the `GlobalAlloc` associated with the allocation id.
256+
///
257+
/// Miri uses this callback to resolve the information about the original
258+
/// thread local static for which `canonical_alloc_id` generated a fresh
259+
/// allocation id.
260+
#[inline(always)]
261+
fn resolve_maybe_global_alloc(
262+
tcx: TyCtxtAt<'tcx>,
251263
_memory_extra: &Self::MemoryExtra,
252264
id: AllocId,
253-
) -> AllocId {
254-
id
265+
) -> Option<mir::interpret::GlobalAlloc<'tcx>> {
266+
tcx.alloc_map.lock().get(id)
255267
}
256268

257269
/// Called to initialize the "extra" state of an allocation and make the pointers

src/librustc_mir/interpret/memory.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
429429
id: AllocId,
430430
is_write: bool,
431431
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
432-
let alloc =
433-
tcx.alloc_map.lock().get(M::resolve_thread_local_allocation_id(memory_extra, id));
434-
let (alloc, def_id) = match alloc {
432+
let (alloc, def_id) = match M::resolve_maybe_global_alloc(tcx, memory_extra, id) {
435433
Some(GlobalAlloc::Memory(mem)) => {
436434
// Memory of a constant or promoted or anonymous memory referenced by a static.
437435
(mem, None)
@@ -591,11 +589,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
591589
}
592590

593591
// # Statics
594-
// Can't do this in the match argument, we may get cycle errors since the lock would
595-
// be held throughout the match.
596-
let alloc =
597-
self.tcx.alloc_map.lock().get(M::resolve_thread_local_allocation_id(&self.extra, id));
598-
match alloc {
592+
match M::resolve_maybe_global_alloc(self.tcx, &self.extra, id) {
599593
Some(GlobalAlloc::Static(did)) => {
600594
// Use size and align of the type.
601595
let ty = self.tcx.type_of(did);

0 commit comments

Comments
 (0)