1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use std::alloc::Layout;
use std::collections::{HashMap, HashSet};
use std::ffi::c_void;
use std::fmt::{self, Debug, Formatter};
use std::{mem, ptr};

use backtrace::{BacktraceFmt, Frame, PrintFmt};

#[derive(Clone)]
struct TracedAllocation {
    layout: Layout,
    caller: usize,
}

#[derive(Clone)]
struct TracedError {
    ptr: *const u8,
    alloc: Option<TracedAllocation>,
    free: TracedAllocation,
}

pub(super) struct TracingState {
    base_ip: *mut c_void,
    callers: Vec<Vec<Frame>>,
    callers_map: HashMap<Vec<*mut c_void>, usize>,
    allocations: HashMap<*const u8, TracedAllocation>,
    ip_buffer: Vec<*mut c_void>,
    errors: Vec<TracedError>,
    free_callers: HashMap<usize, HashSet<usize>>,
}

/// A reference to a backtrace captured whilst tracing.
///
/// Backtraces are captured in an extremely efficient manner, such that we avoid
/// the full cost of a backtrace if it would end up the same as a backtrace we've
/// already captured.
///
/// That said, this is still a significant cost, and can easily slow down a program
/// by a factor of 10x or more depending on how allocation-heavy it is.
///
/// Currently the only thing you can do with a backtrace is format it using its
/// `Debug` implementation.
#[derive(Copy, Clone)]
pub struct Backtrace<'a> {
    frames: &'a [Frame],
}

impl Debug for Backtrace<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let cwd = std::env::current_dir();
        let mut print_path =
            move |fmt: &mut fmt::Formatter<'_>, path: backtrace::BytesOrWideString<'_>| {
                let path = path.into_path_buf();
                if let Ok(cwd) = &cwd {
                    if let Ok(suffix) = path.strip_prefix(cwd) {
                        return fmt::Display::fmt(&suffix.display(), fmt);
                    }
                }
                fmt::Display::fmt(&path.display(), fmt)
            };
        let mut bt = BacktraceFmt::new(f, PrintFmt::Full, &mut print_path);
        bt.add_context()?;
        for frame in self.frames {
            let mut res = Ok(());
            backtrace::resolve_frame(frame, |symbol| res = bt.frame().symbol(frame, symbol));
            res?;
        }
        bt.finish()
    }
}

/// A reference to information about a leak.
#[derive(Debug, Clone)]
pub struct LeakInfo<'a> {
    ptr: *const u8,
    layout: Layout,
    caller: Backtrace<'a>,
    free_callers: Vec<Backtrace<'a>>,
}

impl<'a> LeakInfo<'a> {
    /// The address of the allocation that was leaked.
    pub fn ptr(&self) -> *const u8 {
        self.ptr
    }
    /// The layout of the allocation that was leaked.
    pub fn layout(&self) -> Layout {
        self.layout
    }
    /// Returns a backtrace recorded when the memory was allocated.
    pub fn alloc_backtrace(&self) -> Backtrace<'a> {
        self.caller
    }
    /// Returns a list of possible backtraces which might have been expected to
    /// free the memory.
    ///
    /// This list is generated by looking for other allocations (which *were* freed
    /// correctly) which have the same allocation backtrace.
    /// From that list, we return all the distinct backtraces recorded from the
    /// corresponding deallocations.
    pub fn expected_dealloc_backtraces(&'a self) -> impl Iterator<Item = Backtrace<'a>> {
        self.free_callers.iter().copied()
    }
}

/// A reference to information about an allocation error.
#[derive(Debug, Copy, Clone)]
pub struct ErrorInfo<'a> {
    ptr: *const u8,
    alloc_layout: Option<Layout>,
    alloc_caller: Option<Backtrace<'a>>,
    free_layout: Layout,
    free_caller: Backtrace<'a>,
}

impl<'a> ErrorInfo<'a> {
    /// The address of the (supposed) allocation.
    pub fn ptr(&self) -> *const u8 {
        self.ptr
    }
    /// The layout passed to `alloc`. This is `None` if this
    /// memory was never returned from `alloc`.
    pub fn alloc_layout(&self) -> Option<Layout> {
        self.alloc_layout
    }
    /// A backtrace recorded when the memory was allocated. This
    /// is `None` if this memory was never returned from `alloc`.
    pub fn alloc_backtrace(&'a self) -> Option<Backtrace<'a>> {
        self.alloc_caller
    }
    /// The layout passed to `dealloc`.
    pub fn dealloc_layout(&self) -> Layout {
        self.free_layout
    }
    /// A backtrace recorded when the memory was deallocated.
    pub fn dealloc_backtrace(&'a self) -> Backtrace<'a> {
        self.free_caller
    }
}

/// Stores all information about memory leaks and errors.
#[derive(Clone, Default)]
pub struct TracingInfo {
    callers: HashMap<usize, Vec<Frame>>,
    leaks: HashMap<*const u8, TracedAllocation>,
    errors: Vec<TracedError>,
    free_callers: HashMap<usize, HashSet<usize>>,
}

impl TracingInfo {
    fn backtrace(&self, id: usize) -> Backtrace {
        Backtrace {
            frames: self.callers[&id].as_slice(),
        }
    }

    /// Returns an iterator over the detected leaks
    pub fn leaks(&self) -> impl Iterator<Item = LeakInfo> {
        self.leaks.iter().map(move |(&ptr, v)| LeakInfo {
            ptr,
            layout: v.layout,
            caller: self.backtrace(v.caller),
            free_callers: self
                .free_callers
                .get(&v.caller)
                .map(|free_callers| {
                    free_callers
                        .iter()
                        .map(|&free_caller| self.backtrace(free_caller))
                        .collect()
                })
                .unwrap_or_default(),
        })
    }

    /// Returns an iterator over the detected errors
    pub fn errors(&self) -> impl Iterator<Item = ErrorInfo> {
        self.errors.iter().map(move |e| ErrorInfo {
            ptr: e.ptr,
            alloc_layout: e.alloc.as_ref().map(|x| x.layout),
            alloc_caller: e.alloc.as_ref().map(|x| self.backtrace(x.caller)),
            free_layout: e.free.layout,
            free_caller: self.backtrace(e.free.caller),
        })
    }
}

impl Debug for TracingInfo {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("TracingInfo")
            .field("leaks", &self.leaks().collect::<Vec<_>>())
            .field("errors", &self.errors().collect::<Vec<_>>())
            .finish()
    }
}

impl Default for TracingState {
    fn default() -> Self {
        Self {
            base_ip: ptr::null_mut(),
            callers: Default::default(),
            callers_map: Default::default(),
            allocations: Default::default(),
            ip_buffer: Default::default(),
            errors: Default::default(),
            free_callers: Default::default(),
        }
    }
}

impl TracingState {
    pub(super) fn start(&mut self) {
        // Find a base frame for our traces
        let mut frames_to_skip = 9;
        #[cfg(not(miri))]
        backtrace::trace(|frame| {
            if frames_to_skip == 0 {
                self.base_ip = frame.ip();
                false
            } else {
                frames_to_skip -= 1;
                true
            }
        });
    }
    pub(super) fn finish(&mut self) -> TracingInfo {
        let mut callers = HashMap::new();

        let self_callers = &mut self.callers;
        let mut_callers = &mut callers;
        let mut visit_caller = move |caller| {
            mut_callers
                .entry(caller)
                .or_insert_with(|| mem::replace(&mut self_callers[caller], Default::default()));
        };
        for leak in self.allocations.values() {
            visit_caller(leak.caller);
            if let Some(free_callers) = self.free_callers.get(&leak.caller) {
                for &free_caller in free_callers {
                    visit_caller(free_caller);
                }
            }
        }
        for error in &self.errors {
            if let Some(alloc) = &error.alloc {
                visit_caller(alloc.caller);
            }
            visit_caller(error.free.caller);
        }

        TracingInfo {
            callers,
            leaks: mem::replace(&mut self.allocations, Default::default()),
            errors: mem::replace(&mut self.errors, Default::default()),
            free_callers: mem::replace(&mut self.free_callers, Default::default()),
        }
    }
    fn trace_caller(&mut self) -> usize {
        let mut frames_to_skip = 2;
        #[cfg(not(miri))]
        backtrace::trace(|frame| {
            if frames_to_skip > 0 {
                frames_to_skip -= 1;
                return true;
            }
            let ip = frame.ip();
            if ip == self.base_ip {
                false
            } else {
                self.ip_buffer.push(ip);
                true
            }
        });

        if let Some(&id) = self.callers_map.get(&self.ip_buffer) {
            // Fast path
            self.ip_buffer.clear();
            id
        } else {
            // Slow path (new caller)
            let mut frames = Vec::with_capacity(self.ip_buffer.len());
            let mut frames_to_skip = 2;
            #[cfg(not(miri))]
            backtrace::trace(|frame| {
                if frames_to_skip > 0 {
                    frames_to_skip -= 1;
                    return true;
                }
                let ip = frame.ip();
                if ip == self.base_ip {
                    false
                } else {
                    frames.push(frame.clone());
                    true
                }
            });
            let id = self.callers.len();
            self.callers.push(frames);
            self.callers_map.insert(self.ip_buffer.clone(), id);
            self.ip_buffer.clear();
            id
        }
    }
    pub(super) fn record_alloc(&mut self, ptr: *const u8, layout: Layout) {
        let caller = self.trace_caller();
        self.allocations
            .insert(ptr, TracedAllocation { layout, caller });
    }
    pub(super) fn record_free(&mut self, ptr: *const u8, layout: Layout) {
        let caller = self.trace_caller();
        if let Some(alloc) = self.allocations.remove(&ptr) {
            // Record the pairing between alloc/free calls.
            self.free_callers
                .entry(alloc.caller)
                .or_default()
                .insert(caller);
            if alloc.layout != layout {
                self.errors.push(TracedError {
                    ptr,
                    alloc: Some(alloc),
                    free: TracedAllocation { caller, layout },
                });
            }
        } else {
            self.errors.push(TracedError {
                ptr,
                alloc: None,
                free: TracedAllocation { caller, layout },
            });
        }
    }
}