miri/alloc_addresses/
reuse_pool.rs

1//! Manages a pool of addresses that can be reused.
2
3use rand::Rng;
4use rustc_abi::{Align, Size};
5
6use crate::concurrency::VClock;
7use crate::helpers::ToUsize as _;
8use crate::{MemoryKind, MiriConfig, ThreadId};
9
10const MAX_POOL_SIZE: usize = 64;
11
12/// The pool strikes a balance between exploring more possible executions and making it more likely
13/// to find bugs. The hypothesis is that bugs are more likely to occur when reuse happens for
14/// allocations with the same layout, since that can trigger e.g. ABA issues in a concurrent data
15/// structure. Therefore we only reuse allocations when size and alignment match exactly.
16#[derive(Debug)]
17pub struct ReusePool {
18    address_reuse_rate: f64,
19    address_reuse_cross_thread_rate: f64,
20    /// The i-th element in `pool` stores allocations of alignment `2^i`. We store these reusable
21    /// allocations as address-size pairs, the list must be sorted by the size and then the thread ID.
22    ///
23    /// Each of these maps has at most MAX_POOL_SIZE elements, and since alignment is limited to
24    /// less than 64 different possible values, that bounds the overall size of the pool.
25    ///
26    /// We also store the ID and the data-race clock of the thread that donated this pool element,
27    /// to ensure synchronization with the thread that picks up this address.
28    pool: Vec<Vec<(u64, Size, ThreadId, VClock)>>,
29}
30
31impl ReusePool {
32    pub fn new(config: &MiriConfig) -> Self {
33        ReusePool {
34            address_reuse_rate: config.address_reuse_rate,
35            address_reuse_cross_thread_rate: config.address_reuse_cross_thread_rate,
36            pool: vec![],
37        }
38    }
39
40    /// Call this when we are using up a lot of the address space: if memory reuse is enabled at all,
41    /// this will bump the intra-thread reuse rate to 100% so that we can keep running this program as
42    /// long as possible.
43    pub fn address_space_shortage(&mut self) {
44        if self.address_reuse_rate > 0.0 {
45            self.address_reuse_rate = 1.0;
46        }
47    }
48
49    fn subpool(&mut self, align: Align) -> &mut Vec<(u64, Size, ThreadId, VClock)> {
50        let pool_idx: usize = align.bytes().trailing_zeros().to_usize();
51        if self.pool.len() <= pool_idx {
52            self.pool.resize(pool_idx + 1, Vec::new());
53        }
54        &mut self.pool[pool_idx]
55    }
56
57    pub fn add_addr(
58        &mut self,
59        rng: &mut impl Rng,
60        addr: u64,
61        size: Size,
62        align: Align,
63        kind: MemoryKind,
64        thread: ThreadId,
65        clock: impl FnOnce() -> VClock,
66    ) {
67        // Let's see if we even want to remember this address.
68        // We don't remember stack addresses since there's so many of them (so the perf impact is big).
69        if kind == MemoryKind::Stack || !rng.random_bool(self.address_reuse_rate) {
70            return;
71        }
72        let clock = clock();
73        // Determine the pool to add this to, and where in the pool to put it.
74        let subpool = self.subpool(align);
75        let pos = subpool.partition_point(|(_addr, other_size, other_thread, _)| {
76            (*other_size, *other_thread) < (size, thread)
77        });
78        // Make sure the pool does not grow too big.
79        if subpool.len() >= MAX_POOL_SIZE {
80            // Pool full. Replace existing element, or last one if this would be even bigger.
81            let clamped_pos = pos.min(subpool.len() - 1);
82            subpool[clamped_pos] = (addr, size, thread, clock);
83            return;
84        }
85        // Add address to pool, at the right position.
86        subpool.insert(pos, (addr, size, thread, clock));
87    }
88
89    /// Returns the address to use and optionally a clock we have to synchronize with.
90    pub fn take_addr(
91        &mut self,
92        rng: &mut impl Rng,
93        size: Size,
94        align: Align,
95        kind: MemoryKind,
96        thread: ThreadId,
97    ) -> Option<(u64, Option<VClock>)> {
98        // Determine whether we'll even attempt a reuse. As above, we don't do reuse for stack addresses.
99        if kind == MemoryKind::Stack || !rng.random_bool(self.address_reuse_rate) {
100            return None;
101        }
102        let cross_thread_reuse = rng.random_bool(self.address_reuse_cross_thread_rate);
103        // Determine the pool to take this from.
104        let subpool = self.subpool(align);
105        // Let's see if we can find something of the right size. We want to find the full range of
106        // such items, beginning with the first, so we can't use `binary_search_by_key`. If we do
107        // *not* want to consider other thread's allocations, we effectively use the lexicographic
108        // order on `(size, thread)`.
109        let begin = subpool.partition_point(|(_addr, other_size, other_thread, _)| {
110            *other_size < size
111                || (*other_size == size && !cross_thread_reuse && *other_thread < thread)
112        });
113        let mut end = begin;
114        while let Some((_addr, other_size, other_thread, _)) = subpool.get(end) {
115            if *other_size != size {
116                break;
117            }
118            if !cross_thread_reuse && *other_thread != thread {
119                // We entered the allocations of another thread.
120                break;
121            }
122            end += 1;
123        }
124        if end == begin {
125            // Could not find any item of the right size.
126            return None;
127        }
128        // Pick a random element with the desired size.
129        let idx = rng.random_range(begin..end);
130        // Remove it from the pool and return.
131        let (chosen_addr, chosen_size, chosen_thread, clock) = subpool.remove(idx);
132        debug_assert!(chosen_size >= size && chosen_addr % align.bytes() == 0);
133        debug_assert!(cross_thread_reuse || chosen_thread == thread);
134        // No synchronization needed if we reused from the current thread.
135        Some((chosen_addr, if chosen_thread == thread { None } else { Some(clock) }))
136    }
137}