@@ -6,7 +6,7 @@ use std::borrow::{Borrow, Cow};
66use std:: hash:: Hash ;
77
88use rustc_middle:: mir;
9- use rustc_middle:: ty:: { self , Ty } ;
9+ use rustc_middle:: ty:: { self , query :: TyCtxtAt , Ty } ;
1010use rustc_span:: def_id:: DefId ;
1111
1212use 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
0 commit comments