rustc_middle/mir/interpret/allocation/
provenance_map.rs

1//! Store the provenance for each byte in the range, with a more efficient
2//! representation for the common case where PTR_SIZE consecutive bytes have the same provenance.
3
4use std::cmp;
5use std::ops::Range;
6
7use rustc_abi::{HasDataLayout, Size};
8use rustc_data_structures::sorted_map::SortedMap;
9use rustc_macros::HashStable;
10use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
11use tracing::trace;
12
13use super::{AllocRange, CtfeProvenance, Provenance, alloc_range};
14
15/// Stores the provenance information of pointers stored in memory.
16#[derive(Clone, PartialEq, Eq, Hash, Debug)]
17#[derive(HashStable)]
18pub struct ProvenanceMap<Prov = CtfeProvenance> {
19    /// `Provenance` in this map applies from the given offset for an entire pointer-size worth of
20    /// bytes. Two entries in this map are always at least a pointer size apart.
21    ptrs: SortedMap<Size, Prov>,
22    /// This stores byte-sized provenance fragments.
23    /// The `u8` indicates the position of this byte inside its original pointer.
24    /// If the bytes are re-assembled in their original order, the pointer can be used again.
25    /// Wildcard provenance is allowed to have index 0 everywhere.
26    bytes: Option<Box<SortedMap<Size, (Prov, u8)>>>,
27}
28
29// These impls are generic over `Prov` since `CtfeProvenance` is only decodable/encodable
30// for some particular `D`/`S`.
31impl<D: Decoder, Prov: Provenance + Decodable<D>> Decodable<D> for ProvenanceMap<Prov> {
32    fn decode(d: &mut D) -> Self {
33        // `bytes` is not in the serialized format
34        Self { ptrs: Decodable::decode(d), bytes: None }
35    }
36}
37impl<S: Encoder, Prov: Provenance + Encodable<S>> Encodable<S> for ProvenanceMap<Prov> {
38    fn encode(&self, s: &mut S) {
39        let Self { ptrs, bytes } = self;
40        assert!(bytes.is_none()); // interning refuses allocations with pointer fragments
41        ptrs.encode(s)
42    }
43}
44
45impl<Prov> ProvenanceMap<Prov> {
46    pub fn new() -> Self {
47        ProvenanceMap { ptrs: SortedMap::new(), bytes: None }
48    }
49
50    /// The caller must guarantee that the given provenance list is already sorted
51    /// by address and contain no duplicates.
52    pub fn from_presorted_ptrs(r: Vec<(Size, Prov)>) -> Self {
53        ProvenanceMap { ptrs: SortedMap::from_presorted_elements(r), bytes: None }
54    }
55}
56
57impl ProvenanceMap {
58    /// Give access to the ptr-sized provenances (which can also be thought of as relocations, and
59    /// indeed that is how codegen treats them).
60    ///
61    /// Only use on interned allocations, as other allocations may have per-byte provenance!
62    #[inline]
63    pub fn ptrs(&self) -> &SortedMap<Size, CtfeProvenance> {
64        assert!(self.bytes.is_none(), "`ptrs()` called on non-interned allocation");
65        &self.ptrs
66    }
67}
68
69impl<Prov: Provenance> ProvenanceMap<Prov> {
70    fn adjusted_range_ptrs(range: AllocRange, cx: &impl HasDataLayout) -> Range<Size> {
71        // We have to go back `pointer_size - 1` bytes, as that one would still overlap with
72        // the beginning of this range.
73        let adjusted_start = Size::from_bytes(
74            range.start.bytes().saturating_sub(cx.data_layout().pointer_size().bytes() - 1),
75        );
76        adjusted_start..range.end()
77    }
78
79    /// Returns all ptr-sized provenance in the given range.
80    /// If the range has length 0, returns provenance that crosses the edge between `start-1` and
81    /// `start`.
82    pub(super) fn range_ptrs_get(
83        &self,
84        range: AllocRange,
85        cx: &impl HasDataLayout,
86    ) -> &[(Size, Prov)] {
87        self.ptrs.range(Self::adjusted_range_ptrs(range, cx))
88    }
89
90    /// `pm.range_ptrs_is_empty(r, cx)` == `pm.range_ptrs_get(r, cx).is_empty()`, but is faster.
91    fn range_ptrs_is_empty(&self, range: AllocRange, cx: &impl HasDataLayout) -> bool {
92        self.ptrs.range_is_empty(Self::adjusted_range_ptrs(range, cx))
93    }
94
95    /// Returns all byte-wise provenance in the given range.
96    fn range_bytes_get(&self, range: AllocRange) -> &[(Size, (Prov, u8))] {
97        if let Some(bytes) = self.bytes.as_ref() {
98            bytes.range(range.start..range.end())
99        } else {
100            &[]
101        }
102    }
103
104    /// Same as `range_bytes_get(range).is_empty()`, but faster.
105    fn range_bytes_is_empty(&self, range: AllocRange) -> bool {
106        self.bytes.as_ref().is_none_or(|bytes| bytes.range_is_empty(range.start..range.end()))
107    }
108
109    /// Get the provenance of a single byte.
110    pub fn get_byte(&self, offset: Size, cx: &impl HasDataLayout) -> Option<(Prov, u8)> {
111        let prov = self.range_ptrs_get(alloc_range(offset, Size::from_bytes(1)), cx);
112        debug_assert!(prov.len() <= 1);
113        if let Some(entry) = prov.first() {
114            // If it overlaps with this byte, it is on this byte.
115            debug_assert!(self.bytes.as_ref().is_none_or(|b| !b.contains_key(&offset)));
116            Some((entry.1, (offset - entry.0).bytes() as u8))
117        } else {
118            // Look up per-byte provenance.
119            self.bytes.as_ref().and_then(|b| b.get(&offset).copied())
120        }
121    }
122
123    /// Gets the provenances of all bytes (including from pointers) in a range.
124    pub fn get_range(
125        &self,
126        cx: &impl HasDataLayout,
127        range: AllocRange,
128    ) -> impl Iterator<Item = Prov> {
129        let ptr_provs = self.range_ptrs_get(range, cx).iter().map(|(_, p)| *p);
130        let byte_provs = self.range_bytes_get(range).iter().map(|(_, (p, _))| *p);
131        ptr_provs.chain(byte_provs)
132    }
133
134    /// Attempt to merge per-byte provenance back into ptr chunks, if the right fragments
135    /// sit next to each other. Return `false` is that is not possible due to partial pointers.
136    pub fn merge_bytes(&mut self, cx: &impl HasDataLayout) -> bool {
137        let Some(bytes) = self.bytes.as_deref_mut() else {
138            return true;
139        };
140        let ptr_size = cx.data_layout().pointer_size();
141        while let Some((offset, (prov, _))) = bytes.iter().next().copied() {
142            // Check if this fragment starts a pointer.
143            let range = offset..offset + ptr_size;
144            let frags = bytes.range(range.clone());
145            if frags.len() != ptr_size.bytes_usize() {
146                return false;
147            }
148            for (idx, (_offset, (frag_prov, frag_idx))) in frags.iter().copied().enumerate() {
149                if frag_prov != prov || frag_idx != idx as u8 {
150                    return false;
151                }
152            }
153            // Looks like a pointer! Move it over to the ptr provenance map.
154            bytes.remove_range(range);
155            self.ptrs.insert(offset, prov);
156        }
157        // We managed to convert everything into whole pointers.
158        self.bytes = None;
159        true
160    }
161
162    /// Check if there is ptr-sized provenance at the given index.
163    /// Does not mean anything for bytewise provenance! But can be useful as an optimization.
164    pub fn get_ptr(&self, offset: Size) -> Option<Prov> {
165        self.ptrs.get(&offset).copied()
166    }
167
168    /// Returns whether this allocation has provenance overlapping with the given range.
169    ///
170    /// Note: this function exists to allow `range_get_provenance` to be private, in order to somewhat
171    /// limit access to provenance outside of the `Allocation` abstraction.
172    ///
173    pub fn range_empty(&self, range: AllocRange, cx: &impl HasDataLayout) -> bool {
174        self.range_ptrs_is_empty(range, cx) && self.range_bytes_is_empty(range)
175    }
176
177    /// Yields all the provenances stored in this map.
178    pub fn provenances(&self) -> impl Iterator<Item = Prov> {
179        let bytes = self.bytes.iter().flat_map(|b| b.values().map(|(p, _i)| p));
180        self.ptrs.values().chain(bytes).copied()
181    }
182
183    pub fn insert_ptr(&mut self, offset: Size, prov: Prov, cx: &impl HasDataLayout) {
184        debug_assert!(self.range_empty(alloc_range(offset, cx.data_layout().pointer_size()), cx));
185        self.ptrs.insert(offset, prov);
186    }
187
188    /// Removes all provenance inside the given range.
189    /// If there is provenance overlapping with the edges, might result in an error.
190    pub fn clear(&mut self, range: AllocRange, cx: &impl HasDataLayout) {
191        let start = range.start;
192        let end = range.end();
193        // Clear the bytewise part -- this is easy.
194        if let Some(bytes) = self.bytes.as_mut() {
195            bytes.remove_range(start..end);
196        }
197
198        let pointer_size = cx.data_layout().pointer_size();
199
200        // For the ptr-sized part, find the first (inclusive) and last (exclusive) byte of
201        // provenance that overlaps with the given range.
202        let (first, last) = {
203            // Find all provenance overlapping the given range.
204            if self.range_ptrs_is_empty(range, cx) {
205                // No provenance in this range, we are done. This is the common case.
206                return;
207            }
208
209            // This redoes some of the work of `range_get_ptrs_is_empty`, but this path is much
210            // colder than the early return above, so it's worth it.
211            let provenance = self.range_ptrs_get(range, cx);
212            (provenance.first().unwrap().0, provenance.last().unwrap().0 + pointer_size)
213        };
214
215        // We need to handle clearing the provenance from parts of a pointer.
216        if first < start {
217            // Insert the remaining part in the bytewise provenance.
218            let prov = self.ptrs[&first];
219            let bytes = self.bytes.get_or_insert_with(Box::default);
220            for offset in first..start {
221                bytes.insert(offset, (prov, (offset - first).bytes() as u8));
222            }
223        }
224        if last > end {
225            let begin_of_last = last - pointer_size;
226            // Insert the remaining part in the bytewise provenance.
227            let prov = self.ptrs[&begin_of_last];
228            let bytes = self.bytes.get_or_insert_with(Box::default);
229            for offset in end..last {
230                bytes.insert(offset, (prov, (offset - begin_of_last).bytes() as u8));
231            }
232        }
233
234        // Forget all the provenance.
235        // Since provenance do not overlap, we know that removing until `last` (exclusive) is fine,
236        // i.e., this will not remove any other provenance just after the ones we care about.
237        self.ptrs.remove_range(first..last);
238    }
239
240    /// Overwrites all provenance in the given range with wildcard provenance.
241    /// Pointers partially overwritten will have their provenances preserved
242    /// bytewise on their remaining bytes.
243    ///
244    /// Provided for usage in Miri and panics otherwise.
245    pub fn write_wildcards(&mut self, cx: &impl HasDataLayout, range: AllocRange) {
246        let wildcard = Prov::WILDCARD.unwrap();
247
248        let bytes = self.bytes.get_or_insert_with(Box::default);
249
250        // Remove pointer provenances that overlap with the range, then readd the edge ones bytewise.
251        let ptr_range = Self::adjusted_range_ptrs(range, cx);
252        let ptrs = self.ptrs.range(ptr_range.clone());
253        if let Some((offset, prov)) = ptrs.first().copied() {
254            for byte_ofs in offset..range.start {
255                bytes.insert(byte_ofs, (prov, (byte_ofs - offset).bytes() as u8));
256            }
257        }
258        if let Some((offset, prov)) = ptrs.last().copied() {
259            for byte_ofs in range.end()..offset + cx.data_layout().pointer_size() {
260                bytes.insert(byte_ofs, (prov, (byte_ofs - offset).bytes() as u8));
261            }
262        }
263        self.ptrs.remove_range(ptr_range);
264
265        // Overwrite bytewise provenance.
266        for offset in range.start..range.end() {
267            // The fragment index does not matter for wildcard provenance.
268            bytes.insert(offset, (wildcard, 0));
269        }
270    }
271}
272
273/// A partial, owned list of provenance to transfer into another allocation.
274///
275/// Offsets are already adjusted to the destination allocation.
276pub struct ProvenanceCopy<Prov> {
277    dest_ptrs: Option<Box<[(Size, Prov)]>>,
278    dest_bytes: Option<Box<[(Size, (Prov, u8))]>>,
279}
280
281impl<Prov: Provenance> ProvenanceMap<Prov> {
282    pub fn prepare_copy(
283        &self,
284        src: AllocRange,
285        dest: Size,
286        count: u64,
287        cx: &impl HasDataLayout,
288    ) -> ProvenanceCopy<Prov> {
289        let shift_offset = move |idx, offset| {
290            // compute offset for current repetition
291            let dest_offset = dest + src.size * idx; // `Size` operations
292            // shift offsets from source allocation to destination allocation
293            (offset - src.start) + dest_offset // `Size` operations
294        };
295        let ptr_size = cx.data_layout().pointer_size();
296
297        // # Pointer-sized provenances
298        // Get the provenances that are entirely within this range.
299        // (Different from `range_get_ptrs` which asks if they overlap the range.)
300        // Only makes sense if we are copying at least one pointer worth of bytes.
301        let mut dest_ptrs_box = None;
302        if src.size >= ptr_size {
303            let adjusted_end = Size::from_bytes(src.end().bytes() - (ptr_size.bytes() - 1));
304            let ptrs = self.ptrs.range(src.start..adjusted_end);
305            // If `count` is large, this is rather wasteful -- we are allocating a big array here, which
306            // is mostly filled with redundant information since it's just N copies of the same `Prov`s
307            // at slightly adjusted offsets. The reason we do this is so that in `mark_provenance_range`
308            // we can use `insert_presorted`. That wouldn't work with an `Iterator` that just produces
309            // the right sequence of provenance for all N copies.
310            // Basically, this large array would have to be created anyway in the target allocation.
311            let mut dest_ptrs = Vec::with_capacity(ptrs.len() * (count as usize));
312            for i in 0..count {
313                dest_ptrs
314                    .extend(ptrs.iter().map(|&(offset, reloc)| (shift_offset(i, offset), reloc)));
315            }
316            debug_assert_eq!(dest_ptrs.len(), dest_ptrs.capacity());
317            dest_ptrs_box = Some(dest_ptrs.into_boxed_slice());
318        };
319
320        // # Byte-sized provenances
321        // This includes the existing bytewise provenance in the range, and ptr provenance
322        // that overlaps with the begin/end of the range.
323        let mut dest_bytes_box = None;
324        let begin_overlap = self.range_ptrs_get(alloc_range(src.start, Size::ZERO), cx).first();
325        let end_overlap = self.range_ptrs_get(alloc_range(src.end(), Size::ZERO), cx).first();
326        // We only need to go here if there is some overlap or some bytewise provenance.
327        if begin_overlap.is_some() || end_overlap.is_some() || self.bytes.is_some() {
328            let mut bytes: Vec<(Size, (Prov, u8))> = Vec::new();
329            // First, if there is a part of a pointer at the start, add that.
330            if let Some(entry) = begin_overlap {
331                trace!("start overlapping entry: {entry:?}");
332                // For really small copies, make sure we don't run off the end of the `src` range.
333                let entry_end = cmp::min(entry.0 + ptr_size, src.end());
334                for offset in src.start..entry_end {
335                    bytes.push((offset, (entry.1, (offset - entry.0).bytes() as u8)));
336                }
337            } else {
338                trace!("no start overlapping entry");
339            }
340
341            // Then the main part, bytewise provenance from `self.bytes`.
342            bytes.extend(self.range_bytes_get(src));
343
344            // And finally possibly parts of a pointer at the end.
345            if let Some(entry) = end_overlap {
346                trace!("end overlapping entry: {entry:?}");
347                // For really small copies, make sure we don't start before `src` does.
348                let entry_start = cmp::max(entry.0, src.start);
349                for offset in entry_start..src.end() {
350                    if bytes.last().is_none_or(|bytes_entry| bytes_entry.0 < offset) {
351                        // The last entry, if it exists, has a lower offset than us, so we
352                        // can add it at the end and remain sorted.
353                        bytes.push((offset, (entry.1, (offset - entry.0).bytes() as u8)));
354                    } else {
355                        // There already is an entry for this offset in there! This can happen when the
356                        // start and end range checks actually end up hitting the same pointer, so we
357                        // already added this in the "pointer at the start" part above.
358                        assert!(entry.0 <= src.start);
359                    }
360                }
361            } else {
362                trace!("no end overlapping entry");
363            }
364            trace!("byte provenances: {bytes:?}");
365
366            // And again a buffer for the new list on the target side.
367            let mut dest_bytes = Vec::with_capacity(bytes.len() * (count as usize));
368            for i in 0..count {
369                dest_bytes
370                    .extend(bytes.iter().map(|&(offset, reloc)| (shift_offset(i, offset), reloc)));
371            }
372            debug_assert_eq!(dest_bytes.len(), dest_bytes.capacity());
373            dest_bytes_box = Some(dest_bytes.into_boxed_slice());
374        }
375
376        ProvenanceCopy { dest_ptrs: dest_ptrs_box, dest_bytes: dest_bytes_box }
377    }
378
379    /// Applies a provenance copy.
380    /// The affected range, as defined in the parameters to `prepare_copy` is expected
381    /// to be clear of provenance.
382    pub fn apply_copy(&mut self, copy: ProvenanceCopy<Prov>) {
383        if let Some(dest_ptrs) = copy.dest_ptrs {
384            self.ptrs.insert_presorted(dest_ptrs.into());
385        }
386        if let Some(dest_bytes) = copy.dest_bytes
387            && !dest_bytes.is_empty()
388        {
389            self.bytes.get_or_insert_with(Box::default).insert_presorted(dest_bytes.into());
390        }
391    }
392}