Skip to content

Commit 2960a6c

Browse files
author
Vytautas Astrauskas
committed
Clarify the comments explaining the purpose of resolve_maybe_global_alloc.
1 parent 7e6dbd2 commit 2960a6c

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/librustc_mir/interpret/machine.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,11 @@ pub trait Machine<'mir, 'tcx>: Sized {
243243
/// locals in Miri is that in the internals of the compiler they look as
244244
/// normal statics, except that they have the `thread_local` attribute.
245245
/// 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.
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.
248251
///
249252
/// This function must be idempotent.
250253
#[inline]
@@ -255,8 +258,10 @@ pub trait Machine<'mir, 'tcx>: Sized {
255258
/// Called to obtain the `GlobalAlloc` associated with the allocation id.
256259
///
257260
/// 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.
261+
/// thread local static for which `canonical_alloc_id` reserved a fresh
262+
/// allocation id. Since `canonical_alloc_id` does not create the actual
263+
/// allocation and the reserved allocation id has no reference to its
264+
/// parent, we need to ask Miri to retrieve information for us.
260265
#[inline(always)]
261266
fn resolve_maybe_global_alloc(
262267
tcx: TyCtxtAt<'tcx>,

src/librustc_mir/interpret/memory.rs

+14
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)