1use alloc::boxed::Box;
2use alloc::vec::Vec;
3use core::ffi::c_void;
4
5extern "Rust" {
6 fn miri_backtrace_size(flags: u64) -> usize;
7 fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
8 fn miri_resolve_frame(ptr: *mut (), flags: u64) -> MiriFrame;
9 fn miri_resolve_frame_names(ptr: *mut (), flags: u64, name_buf: *mut u8, filename_buf: *mut u8);
10}
11
12#[repr(C)]
13pub struct MiriFrame {
14 pub name_len: usize,
15 pub filename_len: usize,
16 pub lineno: u32,
17 pub colno: u32,
18 pub fn_ptr: *mut c_void,
19}
20
21#[derive(Clone, Debug)]
22pub struct FullMiriFrame {
23 pub name: Box<[u8]>,
24 pub filename: Box<[u8]>,
25 pub lineno: u32,
26 pub colno: u32,
27 pub fn_ptr: *mut c_void,
28}
29
30#[derive(Debug, Clone)]
31pub struct Frame {
32 pub addr: *mut c_void,
33 pub inner: FullMiriFrame,
34}
35
36// SAFETY: Miri guarantees that the returned pointer
37// can be used from any thread.
38unsafe impl Send for Frame {}
39unsafe impl Sync for Frame {}
40
41impl Frame {
42 pub fn ip(&self) -> *mut c_void {
43 self.addr
44 }
45
46 pub fn sp(&self) -> *mut c_void {
47 core::ptr::null_mut()
48 }
49
50 pub fn symbol_address(&self) -> *mut c_void {
51 self.inner.fn_ptr
52 }
53
54 pub fn module_base_address(&self) -> Option<*mut c_void> {
55 None
56 }
57}
58
59pub fn trace<F: FnMut(&super::Frame) -> bool>(cb: F) {
60 // SAFETY: Miri guarantees that the backtrace API functions
61 // can be called from any thread.
62 unsafe { trace_unsynchronized(cb) };
63}
64
65pub fn resolve_addr(ptr: *mut c_void) -> Frame {
66 // SAFETY: Miri will stop execution with an error if this pointer
67 // is invalid.
68 let frame = unsafe { miri_resolve_frame(ptr as *mut (), 1) };
69
70 let mut name = Vec::with_capacity(frame.name_len);
71 let mut filename = Vec::with_capacity(frame.filename_len);
72
73 // SAFETY: name and filename have been allocated with the amount
74 // of memory miri has asked for, and miri guarantees it will initialize it
75 unsafe {
76 miri_resolve_frame_names(ptr as *mut (), 0, name.as_mut_ptr(), filename.as_mut_ptr());
77
78 name.set_len(frame.name_len);
79 filename.set_len(frame.filename_len);
80 }
81
82 Frame {
83 addr: ptr,
84 inner: FullMiriFrame {
85 name: name.into(),
86 filename: filename.into(),
87 lineno: frame.lineno,
88 colno: frame.colno,
89 fn_ptr: frame.fn_ptr,
90 },
91 }
92}
93
94unsafe fn trace_unsynchronized<F: FnMut(&super::Frame) -> bool>(mut cb: F) {
95 let len: usize = miri_backtrace_size(flags:0);
96
97 let mut frames: Vec<*mut ()> = Vec::with_capacity(len);
98
99 miri_get_backtrace(flags:1, buf:frames.as_mut_ptr());
100
101 frames.set_len(new_len:len);
102
103 for ptr: &*mut () in frames.iter() {
104 let frame: Frame = resolve_addr(*ptr as *mut c_void);
105 if !cb(&super::Frame { inner: frame }) {
106 return;
107 }
108 }
109}
110