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::{AllocError, AllocRange, AllocResult, 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    /// Provenance in this map only applies to the given single byte.
23    /// This map is disjoint from the previous. It will always be empty when
24    /// `Prov::OFFSET_IS_ADDR` is false.
25    bytes: Option<Box<SortedMap<Size, Prov>>>,
26}
27
28// These impls are generic over `Prov` since `CtfeProvenance` is only decodable/encodable
29// for some particular `D`/`S`.
30impl<D: Decoder, Prov: Provenance + Decodable<D>> Decodable<D> for ProvenanceMap<Prov> {
31    fn decode(d: &mut D) -> Self {
32        assert!(!Prov::OFFSET_IS_ADDR); // only `CtfeProvenance` is ever serialized
33        Self { ptrs: Decodable::decode(d), bytes: None }
34    }
35}
36impl<S: Encoder, Prov: Provenance + Encodable<S>> Encodable<S> for ProvenanceMap<Prov> {
37    fn encode(&self, s: &mut S) {
38        let Self { ptrs, bytes } = self;
39        assert!(!Prov::OFFSET_IS_ADDR); // only `CtfeProvenance` is ever serialized
40        debug_assert!(bytes.is_none()); // without `OFFSET_IS_ADDR`, this is always empty
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 exposed with `CtfeProvenance` provenance, since it panics if there is bytewise provenance.
62    #[inline]
63    pub fn ptrs(&self) -> &SortedMap<Size, CtfeProvenance> {
64        debug_assert!(self.bytes.is_none()); // `CtfeProvenance::OFFSET_IS_ADDR` is false so this cannot fail
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    pub(super) 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)] {
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(&self, offset: Size, cx: &impl HasDataLayout) -> Option<Prov> {
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)
117        } else {
118            // Look up per-byte provenance.
119            self.bytes.as_ref().and_then(|b| b.get(&offset).copied())
120        }
121    }
122
123    /// Check if there is ptr-sized provenance at the given index.
124    /// Does not mean anything for bytewise provenance! But can be useful as an optimization.
125    pub fn get_ptr(&self, offset: Size) -> Option<Prov> {
126        self.ptrs.get(&offset).copied()
127    }
128
129    /// Returns whether this allocation has provenance overlapping with the given range.
130    ///
131    /// Note: this function exists to allow `range_get_provenance` to be private, in order to somewhat
132    /// limit access to provenance outside of the `Allocation` abstraction.
133    ///
134    pub fn range_empty(&self, range: AllocRange, cx: &impl HasDataLayout) -> bool {
135        self.range_ptrs_is_empty(range, cx) && self.range_bytes_is_empty(range)
136    }
137
138    /// Yields all the provenances stored in this map.
139    pub fn provenances(&self) -> impl Iterator<Item = Prov> {
140        let bytes = self.bytes.iter().flat_map(|b| b.values());
141        self.ptrs.values().chain(bytes).copied()
142    }
143
144    pub fn insert_ptr(&mut self, offset: Size, prov: Prov, cx: &impl HasDataLayout) {
145        debug_assert!(self.range_empty(alloc_range(offset, cx.data_layout().pointer_size()), cx));
146        self.ptrs.insert(offset, prov);
147    }
148
149    /// Removes all provenance inside the given range.
150    /// If there is provenance overlapping with the edges, might result in an error.
151    pub fn clear(&mut self, range: AllocRange, cx: &impl HasDataLayout) -> AllocResult {
152        let start = range.start;
153        let end = range.end();
154        // Clear the bytewise part -- this is easy.
155        if Prov::OFFSET_IS_ADDR {
156            if let Some(bytes) = self.bytes.as_mut() {
157                bytes.remove_range(start..end);
158            }
159        } else {
160            debug_assert!(self.bytes.is_none());
161        }
162
163        let pointer_size = cx.data_layout().pointer_size();
164
165        // For the ptr-sized part, find the first (inclusive) and last (exclusive) byte of
166        // provenance that overlaps with the given range.
167        let (first, last) = {
168            // Find all provenance overlapping the given range.
169            if self.range_ptrs_is_empty(range, cx) {
170                // No provenance in this range, we are done. This is the common case.
171                return Ok(());
172            }
173
174            // This redoes some of the work of `range_get_ptrs_is_empty`, but this path is much
175            // colder than the early return above, so it's worth it.
176            let provenance = self.range_ptrs_get(range, cx);
177            (provenance.first().unwrap().0, provenance.last().unwrap().0 + pointer_size)
178        };
179
180        // We need to handle clearing the provenance from parts of a pointer.
181        if first < start {
182            if !Prov::OFFSET_IS_ADDR {
183                // We can't split up the provenance into less than a pointer.
184                return Err(AllocError::OverwritePartialPointer(first));
185            }
186            // Insert the remaining part in the bytewise provenance.
187            let prov = self.ptrs[&first];
188            let bytes = self.bytes.get_or_insert_with(Box::default);
189            for offset in first..start {
190                bytes.insert(offset, prov);
191            }
192        }
193        if last > end {
194            let begin_of_last = last - pointer_size;
195            if !Prov::OFFSET_IS_ADDR {
196                // We can't split up the provenance into less than a pointer.
197                return Err(AllocError::OverwritePartialPointer(begin_of_last));
198            }
199            // Insert the remaining part in the bytewise provenance.
200            let prov = self.ptrs[&begin_of_last];
201            let bytes = self.bytes.get_or_insert_with(Box::default);
202            for offset in end..last {
203                bytes.insert(offset, prov);
204            }
205        }
206
207        // Forget all the provenance.
208        // Since provenance do not overlap, we know that removing until `last` (exclusive) is fine,
209        // i.e., this will not remove any other provenance just after the ones we care about.
210        self.ptrs.remove_range(first..last);
211
212        Ok(())
213    }
214
215    /// Overwrites all provenance in the given range with wildcard provenance.
216    /// Pointers partially overwritten will have their provenances preserved
217    /// bytewise on their remaining bytes.
218    ///
219    /// Provided for usage in Miri and panics otherwise.
220    pub fn write_wildcards(&mut self, cx: &impl HasDataLayout, range: AllocRange) {
221        assert!(
222            Prov::OFFSET_IS_ADDR,
223            "writing wildcard provenance is not supported when `OFFSET_IS_ADDR` is false"
224        );
225        let wildcard = Prov::WILDCARD.unwrap();
226
227        let bytes = self.bytes.get_or_insert_with(Box::default);
228
229        // Remove pointer provenances that overlap with the range, then readd the edge ones bytewise.
230        let ptr_range = Self::adjusted_range_ptrs(range, cx);
231        let ptrs = self.ptrs.range(ptr_range.clone());
232        if let Some((offset, prov)) = ptrs.first() {
233            for byte_ofs in *offset..range.start {
234                bytes.insert(byte_ofs, *prov);
235            }
236        }
237        if let Some((offset, prov)) = ptrs.last() {
238            for byte_ofs in range.end()..*offset + cx.data_layout().pointer_size() {
239                bytes.insert(byte_ofs, *prov);
240            }
241        }
242        self.ptrs.remove_range(ptr_range);
243
244        // Overwrite bytewise provenance.
245        for offset in range.start..range.end() {
246            bytes.insert(offset, wildcard);
247        }
248    }
249}
250
251/// A partial, owned list of provenance to transfer into another allocation.
252///
253/// Offsets are already adjusted to the destination allocation.
254pub struct ProvenanceCopy<Prov> {
255    dest_ptrs: Option<Box<[(Size, Prov)]>>,
256    dest_bytes: Option<Box<[(Size, Prov)]>>,
257}
258
259impl<Prov: Provenance> ProvenanceMap<Prov> {
260    pub fn prepare_copy(
261        &self,
262        src: AllocRange,
263        dest: Size,
264        count: u64,
265        cx: &impl HasDataLayout,
266    ) -> AllocResult<ProvenanceCopy<Prov>> {
267        let shift_offset = move |idx, offset| {
268            // compute offset for current repetition
269            let dest_offset = dest + src.size * idx; // `Size` operations
270            // shift offsets from source allocation to destination allocation
271            (offset - src.start) + dest_offset // `Size` operations
272        };
273        let ptr_size = cx.data_layout().pointer_size();
274
275        // # Pointer-sized provenances
276        // Get the provenances that are entirely within this range.
277        // (Different from `range_get_ptrs` which asks if they overlap the range.)
278        // Only makes sense if we are copying at least one pointer worth of bytes.
279        let mut dest_ptrs_box = None;
280        if src.size >= ptr_size {
281            let adjusted_end = Size::from_bytes(src.end().bytes() - (ptr_size.bytes() - 1));
282            let ptrs = self.ptrs.range(src.start..adjusted_end);
283            // If `count` is large, this is rather wasteful -- we are allocating a big array here, which
284            // is mostly filled with redundant information since it's just N copies of the same `Prov`s
285            // at slightly adjusted offsets. The reason we do this is so that in `mark_provenance_range`
286            // we can use `insert_presorted`. That wouldn't work with an `Iterator` that just produces
287            // the right sequence of provenance for all N copies.
288            // Basically, this large array would have to be created anyway in the target allocation.
289            let mut dest_ptrs = Vec::with_capacity(ptrs.len() * (count as usize));
290            for i in 0..count {
291                dest_ptrs
292                    .extend(ptrs.iter().map(|&(offset, reloc)| (shift_offset(i, offset), reloc)));
293            }
294            debug_assert_eq!(dest_ptrs.len(), dest_ptrs.capacity());
295            dest_ptrs_box = Some(dest_ptrs.into_boxed_slice());
296        };
297
298        // # Byte-sized provenances
299        // This includes the existing bytewise provenance in the range, and ptr provenance
300        // that overlaps with the begin/end of the range.
301        let mut dest_bytes_box = None;
302        let begin_overlap = self.range_ptrs_get(alloc_range(src.start, Size::ZERO), cx).first();
303        let end_overlap = self.range_ptrs_get(alloc_range(src.end(), Size::ZERO), cx).first();
304        if !Prov::OFFSET_IS_ADDR {
305            // There can't be any bytewise provenance, and we cannot split up the begin/end overlap.
306            if let Some(entry) = begin_overlap {
307                return Err(AllocError::ReadPartialPointer(entry.0));
308            }
309            if let Some(entry) = end_overlap {
310                return Err(AllocError::ReadPartialPointer(entry.0));
311            }
312            debug_assert!(self.bytes.is_none());
313        } else {
314            let mut bytes = Vec::new();
315            // First, if there is a part of a pointer at the start, add that.
316            if let Some(entry) = begin_overlap {
317                trace!("start overlapping entry: {entry:?}");
318                // For really small copies, make sure we don't run off the end of the `src` range.
319                let entry_end = cmp::min(entry.0 + ptr_size, src.end());
320                for offset in src.start..entry_end {
321                    bytes.push((offset, entry.1));
322                }
323            } else {
324                trace!("no start overlapping entry");
325            }
326
327            // Then the main part, bytewise provenance from `self.bytes`.
328            bytes.extend(self.range_bytes_get(src));
329
330            // And finally possibly parts of a pointer at the end.
331            if let Some(entry) = end_overlap {
332                trace!("end overlapping entry: {entry:?}");
333                // For really small copies, make sure we don't start before `src` does.
334                let entry_start = cmp::max(entry.0, src.start);
335                for offset in entry_start..src.end() {
336                    if bytes.last().is_none_or(|bytes_entry| bytes_entry.0 < offset) {
337                        // The last entry, if it exists, has a lower offset than us.
338                        bytes.push((offset, entry.1));
339                    } else {
340                        // There already is an entry for this offset in there! This can happen when the
341                        // start and end range checks actually end up hitting the same pointer, so we
342                        // already added this in the "pointer at the start" part above.
343                        assert!(entry.0 <= src.start);
344                    }
345                }
346            } else {
347                trace!("no end overlapping entry");
348            }
349            trace!("byte provenances: {bytes:?}");
350
351            // And again a buffer for the new list on the target side.
352            let mut dest_bytes = Vec::with_capacity(bytes.len() * (count as usize));
353            for i in 0..count {
354                dest_bytes
355                    .extend(bytes.iter().map(|&(offset, reloc)| (shift_offset(i, offset), reloc)));
356            }
357            debug_assert_eq!(dest_bytes.len(), dest_bytes.capacity());
358            dest_bytes_box = Some(dest_bytes.into_boxed_slice());
359        }
360
361        Ok(ProvenanceCopy { dest_ptrs: dest_ptrs_box, dest_bytes: dest_bytes_box })
362    }
363
364    /// Applies a provenance copy.
365    /// The affected range, as defined in the parameters to `prepare_copy` is expected
366    /// to be clear of provenance.
367    pub fn apply_copy(&mut self, copy: ProvenanceCopy<Prov>) {
368        if let Some(dest_ptrs) = copy.dest_ptrs {
369            self.ptrs.insert_presorted(dest_ptrs.into());
370        }
371        if Prov::OFFSET_IS_ADDR {
372            if let Some(dest_bytes) = copy.dest_bytes
373                && !dest_bytes.is_empty()
374            {
375                self.bytes.get_or_insert_with(Box::default).insert_presorted(dest_bytes.into());
376            }
377        } else {
378            debug_assert!(copy.dest_bytes.is_none());
379        }
380    }
381}