@@ -120,16 +120,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
120120 /// Whether memory accesses should be alignment-checked.
121121 fn enforce_alignment ( memory_extra : & Self :: MemoryExtra ) -> bool ;
122122
123- /// Borrow the current thread's stack.
124- fn stack (
125- ecx : & ' a InterpCx < ' mir , ' tcx , Self > ,
126- ) -> & ' a [ Frame < ' mir , ' tcx , Self :: PointerTag , Self :: FrameExtra > ] ;
127-
128- /// Mutably borrow the current thread's stack.
129- fn stack_mut (
130- ecx : & ' a mut InterpCx < ' mir , ' tcx , Self > ,
131- ) -> & ' a mut Vec < Frame < ' mir , ' tcx , Self :: PointerTag , Self :: FrameExtra > > ;
132-
133123 /// Whether to enforce the validity invariant
134124 fn enforce_validity ( ecx : & InterpCx < ' mir , ' tcx , Self > ) -> bool ;
135125
@@ -229,32 +219,43 @@ pub trait Machine<'mir, 'tcx>: Sized {
229219 Ok ( ( ) )
230220 }
231221
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.
222+ /// Called for *every* memory access to determine the real ID of the given allocation.
223+ /// This provides a way for the machine to "redirect" certain allocations as it sees fit.
235224 ///
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. Therefore, for these thread locals in
247- /// `canonical_alloc_id` we reserve fresh allocation ids for each
248- /// thread. Please note that `canonical_alloc_id` only reserves the
249- /// allocation ids, the actual allocation for the thread local statics
250- /// is done in the same way as for regular statics.
225+ /// This is used by Miri to redirect extern statics to real allocations.
251226 ///
252227 /// This function must be idempotent.
253228 #[ inline]
254229 fn canonical_alloc_id ( _mem : & Memory < ' mir , ' tcx , Self > , id : AllocId ) -> AllocId {
255230 id
256231 }
257232
233+ /// Called when converting a `ty::Const` to an operand.
234+ ///
235+ /// Miri uses this callback for creating unique allocation ids for thread
236+ /// locals. In Rust, one way for creating a thread local is by marking a
237+ /// static with `#[thread_local]`. On supported platforms this gets
238+ /// translated to a LLVM thread local for which LLVM automatically ensures
239+ /// that each thread gets its own copy. Since LLVM automatically handles
240+ /// thread locals, the Rust compiler just treats thread local statics as
241+ /// regular statics even though accessing a thread local static should be an
242+ /// effectful computation that depends on the current thread. The long term
243+ /// plan is to change MIR to make accesses to thread locals explicit
244+ /// (https://github.com/rust-lang/rust/issues/70685). While the issue 70685
245+ /// is not fixed, our current workaround in Miri is to use this function to
246+ /// reserve fresh allocation ids for each thread. Please note that here we
247+ /// only **reserve** the allocation ids; the actual allocation for the
248+ /// thread local statics is done in `Memory::get_global_alloc`, which uses
249+ /// `resolve_maybe_global_alloc` to retrieve information about the
250+ /// allocation id we generated.
251+ #[ inline]
252+ fn eval_maybe_thread_local_static_const (
253+ _ecx : & InterpCx < ' mir , ' tcx , Self > ,
254+ val : mir:: interpret:: ConstValue < ' tcx > ,
255+ ) -> InterpResult < ' tcx , mir:: interpret:: ConstValue < ' tcx > > {
256+ Ok ( val)
257+ }
258+
258259 /// Called to obtain the `GlobalAlloc` associated with the allocation id.
259260 ///
260261 /// Miri uses this callback to resolve the information about the original
@@ -326,6 +327,16 @@ pub trait Machine<'mir, 'tcx>: Sized {
326327 frame : Frame < ' mir , ' tcx , Self :: PointerTag > ,
327328 ) -> InterpResult < ' tcx , Frame < ' mir , ' tcx , Self :: PointerTag , Self :: FrameExtra > > ;
328329
330+ /// Borrow the current thread's stack.
331+ fn stack (
332+ ecx : & ' a InterpCx < ' mir , ' tcx , Self > ,
333+ ) -> & ' a [ Frame < ' mir , ' tcx , Self :: PointerTag , Self :: FrameExtra > ] ;
334+
335+ /// Mutably borrow the current thread's stack.
336+ fn stack_mut (
337+ ecx : & ' a mut InterpCx < ' mir , ' tcx , Self > ,
338+ ) -> & ' a mut Vec < Frame < ' mir , ' tcx , Self :: PointerTag , Self :: FrameExtra > > ;
339+
329340 /// Called immediately after a stack frame got pushed and its locals got initialized.
330341 fn after_stack_push ( _ecx : & mut InterpCx < ' mir , ' tcx , Self > ) -> InterpResult < ' tcx > {
331342 Ok ( ( ) )
0 commit comments