@@ -429,6 +429,14 @@ 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+ // The call to `resolve_maybe_global_alloc` is needed to enable Miri to
433+ // support thread local statics. In `M::canonical_alloc_id`, for a
434+ // thread local static, Miri reserves a fresh allocation id, but the
435+ // actual allocation is left to the code that handles statics which
436+ // calls this function (`get_global_alloc`). Since the allocation id is
437+ // fresh, it has no information about the original static. The call to
438+ // `resolve_maybe_global_alloc` allows Miri to retrieve this information
439+ // for us.
432440 let ( alloc, def_id) = match M :: resolve_maybe_global_alloc ( tcx, memory_extra, id) {
433441 Some ( GlobalAlloc :: Memory ( mem) ) => {
434442 // Memory of a constant or promoted or anonymous memory referenced by a static.
@@ -589,6 +597,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
589597 }
590598
591599 // # Statics
600+ // The call to `resolve_maybe_global_alloc` is needed here because Miri
601+ // via the call to `canonical_alloc_id` above reserves fresh allocation
602+ // ids for thread local statics. However, the actual allocation is done
603+ // not in `canonical_alloc_id`, but in `get_raw` and `get_raw_mut`.
604+ // Since this function may get called before `get_raw`, we need to allow
605+ // Miri to retrieve the information about the static for us.
592606 match M :: resolve_maybe_global_alloc ( self . tcx , & self . extra , id) {
593607 Some ( GlobalAlloc :: Static ( did) ) => {
594608 // Use size and align of the type.
0 commit comments