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