rustc_middle/mir/interpret/
mod.rs

1//! An interpreter for MIR used in CTFE and by miri.
2
3#[macro_use]
4mod error;
5
6mod allocation;
7mod pointer;
8mod queries;
9mod value;
10
11use std::io::{Read, Write};
12use std::num::NonZero;
13use std::{fmt, io};
14
15use rustc_abi::{AddressSpace, Align, Endian, HasDataLayout, Size};
16use rustc_ast::{LitKind, Mutability};
17use rustc_data_structures::fx::FxHashMap;
18use rustc_data_structures::sharded::ShardedHashMap;
19use rustc_data_structures::sync::{AtomicU64, Lock};
20use rustc_hir::def::DefKind;
21use rustc_hir::def_id::{DefId, LocalDefId};
22use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
23use rustc_serialize::{Decodable, Encodable};
24use tracing::{debug, trace};
25// Also make the error macros available from this module.
26pub use {
27    err_exhaust, err_inval, err_machine_stop, err_ub, err_ub_custom, err_ub_format, err_unsup,
28    err_unsup_format, throw_exhaust, throw_inval, throw_machine_stop, throw_ub, throw_ub_custom,
29    throw_ub_format, throw_unsup, throw_unsup_format,
30};
31
32pub use self::allocation::{
33    AllocBytes, AllocError, AllocInit, AllocRange, AllocResult, Allocation, ConstAllocation,
34    InitChunk, InitChunkIter, alloc_range,
35};
36pub use self::error::{
37    BadBytesAccess, CheckAlignMsg, CheckInAllocMsg, ErrorHandled, EvalStaticInitializerRawResult,
38    EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, ExpectedKind,
39    InterpErrorInfo, InterpErrorKind, InterpResult, InvalidMetaKind, InvalidProgramInfo,
40    MachineStopType, Misalignment, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo,
41    ScalarSizeMismatch, UndefinedBehaviorInfo, UnsupportedOpInfo, ValTreeCreationError,
42    ValidationErrorInfo, ValidationErrorKind, interp_ok,
43};
44pub use self::pointer::{CtfeProvenance, Pointer, PointerArithmetic, Provenance};
45pub use self::value::Scalar;
46use crate::mir;
47use crate::ty::codec::{TyDecoder, TyEncoder};
48use crate::ty::print::with_no_trimmed_paths;
49use crate::ty::{self, Instance, Ty, TyCtxt};
50
51/// Uniquely identifies one of the following:
52/// - A constant
53/// - A static
54#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, TyEncodable, TyDecodable)]
55#[derive(HashStable, TypeFoldable, TypeVisitable)]
56pub struct GlobalId<'tcx> {
57    /// For a constant or static, the `Instance` of the item itself.
58    /// For a promoted global, the `Instance` of the function they belong to.
59    pub instance: ty::Instance<'tcx>,
60
61    /// The index for promoted globals within their function's `mir::Body`.
62    pub promoted: Option<mir::Promoted>,
63}
64
65impl<'tcx> GlobalId<'tcx> {
66    pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
67        let instance_name = with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id()));
68        if let Some(promoted) = self.promoted {
69            format!("{instance_name}::{promoted:?}")
70        } else {
71            instance_name
72        }
73    }
74}
75
76/// Input argument for `tcx.lit_to_const`.
77#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
78pub struct LitToConstInput<'tcx> {
79    /// The absolute value of the resultant constant.
80    pub lit: LitKind,
81    /// The type of the constant.
82    pub ty: Ty<'tcx>,
83    /// If the constant is negative.
84    pub neg: bool,
85}
86
87#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
88pub struct AllocId(pub NonZero<u64>);
89
90// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
91// all the Miri types.
92impl fmt::Debug for AllocId {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        if f.alternate() { write!(f, "a{}", self.0) } else { write!(f, "alloc{}", self.0) }
95    }
96}
97
98// No "Display" since AllocIds are not usually user-visible.
99
100#[derive(TyDecodable, TyEncodable)]
101enum AllocDiscriminant {
102    Alloc,
103    Fn,
104    VTable,
105    Static,
106    Type,
107}
108
109pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>(
110    encoder: &mut E,
111    tcx: TyCtxt<'tcx>,
112    alloc_id: AllocId,
113) {
114    match tcx.global_alloc(alloc_id) {
115        GlobalAlloc::Memory(alloc) => {
116            trace!("encoding {:?} with {:#?}", alloc_id, alloc);
117            AllocDiscriminant::Alloc.encode(encoder);
118            alloc.encode(encoder);
119        }
120        GlobalAlloc::Function { instance } => {
121            trace!("encoding {:?} with {:#?}", alloc_id, instance);
122            AllocDiscriminant::Fn.encode(encoder);
123            instance.encode(encoder);
124        }
125        GlobalAlloc::VTable(ty, poly_trait_ref) => {
126            trace!("encoding {:?} with {ty:#?}, {poly_trait_ref:#?}", alloc_id);
127            AllocDiscriminant::VTable.encode(encoder);
128            ty.encode(encoder);
129            poly_trait_ref.encode(encoder);
130        }
131        GlobalAlloc::TypeId { ty } => {
132            trace!("encoding {alloc_id:?} with {ty:#?}");
133            AllocDiscriminant::Type.encode(encoder);
134            ty.encode(encoder);
135        }
136        GlobalAlloc::Static(did) => {
137            assert!(!tcx.is_thread_local_static(did));
138            // References to statics doesn't need to know about their allocations,
139            // just about its `DefId`.
140            AllocDiscriminant::Static.encode(encoder);
141            // Cannot use `did.encode(encoder)` because of a bug around
142            // specializations and method calls.
143            Encodable::<E>::encode(&did, encoder);
144        }
145    }
146}
147
148#[derive(Clone)]
149enum State {
150    Empty,
151    Done(AllocId),
152}
153
154pub struct AllocDecodingState {
155    // For each `AllocId`, we keep track of which decoding state it's currently in.
156    decoding_state: Vec<Lock<State>>,
157    // The offsets of each allocation in the data stream.
158    data_offsets: Vec<u64>,
159}
160
161impl AllocDecodingState {
162    #[inline]
163    pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
164        AllocDecodingSession { state: self }
165    }
166
167    pub fn new(data_offsets: Vec<u64>) -> Self {
168        let decoding_state =
169            std::iter::repeat_with(|| Lock::new(State::Empty)).take(data_offsets.len()).collect();
170
171        Self { decoding_state, data_offsets }
172    }
173}
174
175#[derive(Copy, Clone)]
176pub struct AllocDecodingSession<'s> {
177    state: &'s AllocDecodingState,
178}
179
180impl<'s> AllocDecodingSession<'s> {
181    /// Decodes an `AllocId` in a thread-safe way.
182    pub fn decode_alloc_id<'tcx, D>(&self, decoder: &mut D) -> AllocId
183    where
184        D: TyDecoder<'tcx>,
185    {
186        // Read the index of the allocation.
187        let idx = usize::try_from(decoder.read_u32()).unwrap();
188        let pos = usize::try_from(self.state.data_offsets[idx]).unwrap();
189
190        // Decode the `AllocDiscriminant` now so that we know if we have to reserve an
191        // `AllocId`.
192        let (alloc_kind, pos) = decoder.with_position(pos, |decoder| {
193            let alloc_kind = AllocDiscriminant::decode(decoder);
194            (alloc_kind, decoder.position())
195        });
196
197        // We are going to hold this lock during the entire decoding of this allocation, which may
198        // require that we decode other allocations. This cannot deadlock for two reasons:
199        //
200        // At the time of writing, it is only possible to create an allocation that contains a pointer
201        // to itself using the const_allocate intrinsic (which is for testing only), and even attempting
202        // to evaluate such consts blows the stack. If we ever grow a mechanism for producing
203        // cyclic allocations, we will need a new strategy for decoding that doesn't bring back
204        // https://github.com/rust-lang/rust/issues/126741.
205        //
206        // It is also impossible to create two allocations (call them A and B) where A is a pointer to B, and B
207        // is a pointer to A, because attempting to evaluate either of those consts will produce a
208        // query cycle, failing compilation.
209        let mut entry = self.state.decoding_state[idx].lock();
210        // Check the decoding state to see if it's already decoded or if we should
211        // decode it here.
212        if let State::Done(alloc_id) = *entry {
213            return alloc_id;
214        }
215
216        // Now decode the actual data.
217        let alloc_id = decoder.with_position(pos, |decoder| match alloc_kind {
218            AllocDiscriminant::Alloc => {
219                trace!("creating memory alloc ID");
220                let alloc = <ConstAllocation<'tcx> as Decodable<_>>::decode(decoder);
221                trace!("decoded alloc {:?}", alloc);
222                decoder.interner().reserve_and_set_memory_alloc(alloc)
223            }
224            AllocDiscriminant::Fn => {
225                trace!("creating fn alloc ID");
226                let instance = ty::Instance::decode(decoder);
227                trace!("decoded fn alloc instance: {:?}", instance);
228                decoder.interner().reserve_and_set_fn_alloc(instance, CTFE_ALLOC_SALT)
229            }
230            AllocDiscriminant::VTable => {
231                trace!("creating vtable alloc ID");
232                let ty = Decodable::decode(decoder);
233                let poly_trait_ref = Decodable::decode(decoder);
234                trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
235                decoder.interner().reserve_and_set_vtable_alloc(ty, poly_trait_ref, CTFE_ALLOC_SALT)
236            }
237            AllocDiscriminant::Type => {
238                trace!("creating typeid alloc ID");
239                let ty = Decodable::decode(decoder);
240                trace!("decoded typid: {ty:?}");
241                decoder.interner().reserve_and_set_type_id_alloc(ty)
242            }
243            AllocDiscriminant::Static => {
244                trace!("creating extern static alloc ID");
245                let did = <DefId as Decodable<D>>::decode(decoder);
246                trace!("decoded static def-ID: {:?}", did);
247                decoder.interner().reserve_and_set_static_alloc(did)
248            }
249        });
250
251        *entry = State::Done(alloc_id);
252
253        alloc_id
254    }
255}
256
257/// An allocation in the global (tcx-managed) memory can be either a function pointer,
258/// a static, or a "real" allocation with some data in it.
259#[derive(Debug, Clone, Eq, PartialEq, Hash, TyDecodable, TyEncodable, HashStable)]
260pub enum GlobalAlloc<'tcx> {
261    /// The alloc ID is used as a function pointer.
262    Function { instance: Instance<'tcx> },
263    /// This alloc ID points to a symbolic (not-reified) vtable.
264    /// We remember the full dyn type, not just the principal trait, so that
265    /// const-eval and Miri can detect UB due to invalid transmutes of
266    /// `dyn Trait` types.
267    VTable(Ty<'tcx>, &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>),
268    /// The alloc ID points to a "lazy" static variable that did not get computed (yet).
269    /// This is also used to break the cycle in recursive statics.
270    Static(DefId),
271    /// The alloc ID points to memory.
272    Memory(ConstAllocation<'tcx>),
273    /// The first pointer-sized segment of a type id. On 64 bit systems, the 128 bit type id
274    /// is split into two segments, on 32 bit systems there are 4 segments, and so on.
275    TypeId { ty: Ty<'tcx> },
276}
277
278impl<'tcx> GlobalAlloc<'tcx> {
279    /// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
280    #[track_caller]
281    #[inline]
282    pub fn unwrap_memory(&self) -> ConstAllocation<'tcx> {
283        match *self {
284            GlobalAlloc::Memory(mem) => mem,
285            _ => bug!("expected memory, got {:?}", self),
286        }
287    }
288
289    /// Panics if the `GlobalAlloc` is not `GlobalAlloc::Function`
290    #[track_caller]
291    #[inline]
292    pub fn unwrap_fn(&self) -> Instance<'tcx> {
293        match *self {
294            GlobalAlloc::Function { instance, .. } => instance,
295            _ => bug!("expected function, got {:?}", self),
296        }
297    }
298
299    /// Panics if the `GlobalAlloc` is not `GlobalAlloc::VTable`
300    #[track_caller]
301    #[inline]
302    pub fn unwrap_vtable(&self) -> (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
303        match *self {
304            GlobalAlloc::VTable(ty, dyn_ty) => (ty, dyn_ty.principal()),
305            _ => bug!("expected vtable, got {:?}", self),
306        }
307    }
308
309    /// The address space that this `GlobalAlloc` should be placed in.
310    #[inline]
311    pub fn address_space(&self, cx: &impl HasDataLayout) -> AddressSpace {
312        match self {
313            GlobalAlloc::Function { .. } => cx.data_layout().instruction_address_space,
314            GlobalAlloc::TypeId { .. }
315            | GlobalAlloc::Static(..)
316            | GlobalAlloc::Memory(..)
317            | GlobalAlloc::VTable(..) => AddressSpace::ZERO,
318        }
319    }
320
321    pub fn mutability(&self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Mutability {
322        // Let's see what kind of memory we are.
323        match self {
324            GlobalAlloc::Static(did) => {
325                let DefKind::Static { safety: _, mutability, nested } = tcx.def_kind(did) else {
326                    bug!()
327                };
328                if nested {
329                    // Nested statics in a `static` are never interior mutable,
330                    // so just use the declared mutability.
331                    if cfg!(debug_assertions) {
332                        let alloc = tcx.eval_static_initializer(did).unwrap();
333                        assert_eq!(alloc.0.mutability, mutability);
334                    }
335                    mutability
336                } else {
337                    let mutability = match mutability {
338                        Mutability::Not
339                            if !tcx
340                                .type_of(did)
341                                .no_bound_vars()
342                                .expect("statics should not have generic parameters")
343                                .is_freeze(tcx, typing_env) =>
344                        {
345                            Mutability::Mut
346                        }
347                        _ => mutability,
348                    };
349                    mutability
350                }
351            }
352            GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
353            GlobalAlloc::TypeId { .. } | GlobalAlloc::Function { .. } | GlobalAlloc::VTable(..) => {
354                // These are immutable.
355                Mutability::Not
356            }
357        }
358    }
359
360    pub fn size_and_align(
361        &self,
362        tcx: TyCtxt<'tcx>,
363        typing_env: ty::TypingEnv<'tcx>,
364    ) -> (Size, Align) {
365        match self {
366            GlobalAlloc::Static(def_id) => {
367                let DefKind::Static { nested, .. } = tcx.def_kind(def_id) else {
368                    bug!("GlobalAlloc::Static is not a static")
369                };
370
371                if nested {
372                    // Nested anonymous statics are untyped, so let's get their
373                    // size and alignment from the allocation itself. This always
374                    // succeeds, as the query is fed at DefId creation time, so no
375                    // evaluation actually occurs.
376                    let alloc = tcx.eval_static_initializer(def_id).unwrap();
377                    (alloc.0.size(), alloc.0.align)
378                } else {
379                    // Use size and align of the type for everything else. We need
380                    // to do that to
381                    // * avoid cycle errors in case of self-referential statics,
382                    // * be able to get information on extern statics.
383                    let ty = tcx
384                        .type_of(def_id)
385                        .no_bound_vars()
386                        .expect("statics should not have generic parameters");
387                    let layout = tcx.layout_of(typing_env.as_query_input(ty)).unwrap();
388                    assert!(layout.is_sized());
389                    (layout.size, layout.align.abi)
390                }
391            }
392            GlobalAlloc::Memory(alloc) => {
393                let alloc = alloc.inner();
394                (alloc.size(), alloc.align)
395            }
396            GlobalAlloc::Function { .. } => (Size::ZERO, Align::ONE),
397            GlobalAlloc::VTable(..) => {
398                // No data to be accessed here. But vtables are pointer-aligned.
399                (Size::ZERO, tcx.data_layout.pointer_align().abi)
400            }
401            // Fake allocation, there's nothing to access here
402            GlobalAlloc::TypeId { .. } => (Size::ZERO, Align::ONE),
403        }
404    }
405}
406
407pub const CTFE_ALLOC_SALT: usize = 0;
408
409pub(crate) struct AllocMap<'tcx> {
410    /// Maps `AllocId`s to their corresponding allocations.
411    // Note that this map on rustc workloads seems to be rather dense, but in miri workloads should
412    // be pretty sparse. In #136105 we considered replacing it with a (dense) Vec-based map, but
413    // since there are workloads where it can be sparse we decided to go with sharding for now. At
414    // least up to 32 cores the one workload tested didn't exhibit much difference between the two.
415    //
416    // Should be locked *after* locking dedup if locking both to avoid deadlocks.
417    to_alloc: ShardedHashMap<AllocId, GlobalAlloc<'tcx>>,
418
419    /// Used to deduplicate global allocations: functions, vtables, string literals, ...
420    ///
421    /// The `usize` is a "salt" used by Miri to make deduplication imperfect, thus better emulating
422    /// the actual guarantees.
423    dedup: Lock<FxHashMap<(GlobalAlloc<'tcx>, usize), AllocId>>,
424
425    /// The `AllocId` to assign to the next requested ID.
426    /// Always incremented; never gets smaller.
427    next_id: AtomicU64,
428}
429
430impl<'tcx> AllocMap<'tcx> {
431    pub(crate) fn new() -> Self {
432        AllocMap {
433            to_alloc: Default::default(),
434            dedup: Default::default(),
435            next_id: AtomicU64::new(1),
436        }
437    }
438    fn reserve(&self) -> AllocId {
439        // Technically there is a window here where we overflow and then another thread
440        // increments `next_id` *again* and uses it before we panic and tear down the entire session.
441        // We consider this fine since such overflows cannot realistically occur.
442        let next_id = self.next_id.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
443        AllocId(NonZero::new(next_id).unwrap())
444    }
445}
446
447impl<'tcx> TyCtxt<'tcx> {
448    /// Obtains a new allocation ID that can be referenced but does not
449    /// yet have an allocation backing it.
450    ///
451    /// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
452    /// an `AllocId` from a query.
453    pub fn reserve_alloc_id(self) -> AllocId {
454        self.alloc_map.reserve()
455    }
456
457    /// Reserves a new ID *if* this allocation has not been dedup-reserved before.
458    /// Should not be used for mutable memory.
459    fn reserve_and_set_dedup(self, alloc: GlobalAlloc<'tcx>, salt: usize) -> AllocId {
460        if let GlobalAlloc::Memory(mem) = alloc {
461            if mem.inner().mutability.is_mut() {
462                bug!("trying to dedup-reserve mutable memory");
463            }
464        }
465        let alloc_salt = (alloc, salt);
466        // Locking this *before* `to_alloc` also to ensure correct lock order.
467        let mut dedup = self.alloc_map.dedup.lock();
468        if let Some(&alloc_id) = dedup.get(&alloc_salt) {
469            return alloc_id;
470        }
471        let id = self.alloc_map.reserve();
472        debug!("creating alloc {:?} with id {id:?}", alloc_salt.0);
473        let had_previous = self.alloc_map.to_alloc.insert(id, alloc_salt.0.clone()).is_some();
474        // We just reserved, so should always be unique.
475        assert!(!had_previous);
476        dedup.insert(alloc_salt, id);
477        id
478    }
479
480    /// Generates an `AllocId` for a memory allocation. If the exact same memory has been
481    /// allocated before, this will return the same `AllocId`.
482    pub fn reserve_and_set_memory_dedup(self, mem: ConstAllocation<'tcx>, salt: usize) -> AllocId {
483        self.reserve_and_set_dedup(GlobalAlloc::Memory(mem), salt)
484    }
485
486    /// Generates an `AllocId` for a static or return a cached one in case this function has been
487    /// called on the same static before.
488    pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId {
489        let salt = 0; // Statics have a guaranteed unique address, no salt added.
490        self.reserve_and_set_dedup(GlobalAlloc::Static(static_id), salt)
491    }
492
493    /// Generates an `AllocId` for a function. Will get deduplicated.
494    pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>, salt: usize) -> AllocId {
495        self.reserve_and_set_dedup(GlobalAlloc::Function { instance }, salt)
496    }
497
498    /// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
499    pub fn reserve_and_set_vtable_alloc(
500        self,
501        ty: Ty<'tcx>,
502        dyn_ty: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
503        salt: usize,
504    ) -> AllocId {
505        self.reserve_and_set_dedup(GlobalAlloc::VTable(ty, dyn_ty), salt)
506    }
507
508    /// Generates an [AllocId] for a [core::any::TypeId]. Will get deduplicated.
509    pub fn reserve_and_set_type_id_alloc(self, ty: Ty<'tcx>) -> AllocId {
510        self.reserve_and_set_dedup(GlobalAlloc::TypeId { ty }, 0)
511    }
512
513    /// Interns the `Allocation` and return a new `AllocId`, even if there's already an identical
514    /// `Allocation` with a different `AllocId`.
515    /// Statics with identical content will still point to the same `Allocation`, i.e.,
516    /// their data will be deduplicated through `Allocation` interning -- but they
517    /// are different places in memory and as such need different IDs.
518    pub fn reserve_and_set_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
519        let id = self.reserve_alloc_id();
520        self.set_alloc_id_memory(id, mem);
521        id
522    }
523
524    /// Returns `None` in case the `AllocId` is dangling. An `InterpretCx` can still have a
525    /// local `Allocation` for that `AllocId`, but having such an `AllocId` in a constant is
526    /// illegal and will likely ICE.
527    /// This function exists to allow const eval to detect the difference between evaluation-
528    /// local dangling pointers and allocations in constants/statics.
529    #[inline]
530    pub fn try_get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
531        self.alloc_map.to_alloc.get(&id)
532    }
533
534    #[inline]
535    #[track_caller]
536    /// Panics in case the `AllocId` is dangling. Since that is impossible for `AllocId`s in
537    /// constants (as all constants must pass interning and validation that check for dangling
538    /// ids), this function is frequently used throughout rustc, but should not be used within
539    /// the interpreter.
540    pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> {
541        match self.try_get_global_alloc(id) {
542            Some(alloc) => alloc,
543            None => bug!("could not find allocation for {id:?}"),
544        }
545    }
546
547    /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
548    /// call this function twice, even with the same `Allocation` will ICE the compiler.
549    pub fn set_alloc_id_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
550        if let Some(old) = self.alloc_map.to_alloc.insert(id, GlobalAlloc::Memory(mem)) {
551            bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
552        }
553    }
554
555    /// Freezes an `AllocId` created with `reserve` by pointing it at a static item. Trying to
556    /// call this function twice, even with the same `DefId` will ICE the compiler.
557    pub fn set_nested_alloc_id_static(self, id: AllocId, def_id: LocalDefId) {
558        if let Some(old) =
559            self.alloc_map.to_alloc.insert(id, GlobalAlloc::Static(def_id.to_def_id()))
560        {
561            bug!("tried to set allocation ID {id:?}, but it was already existing as {old:#?}");
562        }
563    }
564}
565
566////////////////////////////////////////////////////////////////////////////////
567// Methods to access integers in the target endianness
568////////////////////////////////////////////////////////////////////////////////
569
570#[inline]
571pub fn write_target_uint(
572    endianness: Endian,
573    mut target: &mut [u8],
574    data: u128,
575) -> Result<(), io::Error> {
576    // This u128 holds an "any-size uint" (since smaller uints can fits in it)
577    // So we do not write all bytes of the u128, just the "payload".
578    match endianness {
579        Endian::Little => target.write(&data.to_le_bytes())?,
580        Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
581    };
582    debug_assert!(target.len() == 0); // We should have filled the target buffer.
583    Ok(())
584}
585
586#[inline]
587pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
588    // This u128 holds an "any-size uint" (since smaller uints can fits in it)
589    let mut buf = [0u8; size_of::<u128>()];
590    // So we do not read exactly 16 bytes into the u128, just the "payload".
591    let uint = match endianness {
592        Endian::Little => {
593            source.read_exact(&mut buf[..source.len()])?;
594            Ok(u128::from_le_bytes(buf))
595        }
596        Endian::Big => {
597            source.read_exact(&mut buf[16 - source.len()..])?;
598            Ok(u128::from_be_bytes(buf))
599        }
600    };
601    debug_assert!(source.len() == 0); // We should have consumed the source buffer.
602    uint
603}