backtrace/symbolize/
gimli.rs

1//! Support for symbolication using the `gimli` crate on crates.io
2//!
3//! This is the default symbolication implementation for Rust.
4
5use self::gimli::read::EndianSlice;
6use self::gimli::NativeEndian as Endian;
7use self::mmap::Mmap;
8use self::stash::Stash;
9use super::BytesOrWideString;
10use super::ResolveWhat;
11use super::SymbolName;
12use addr2line::gimli;
13use core::convert::TryInto;
14use core::mem;
15use core::u32;
16use libc::c_void;
17use mystd::ffi::OsString;
18use mystd::fs::File;
19use mystd::path::Path;
20use mystd::prelude::v1::*;
21
22#[cfg(backtrace_in_libstd)]
23mod mystd {
24    pub use crate::*;
25}
26#[cfg(not(backtrace_in_libstd))]
27extern crate std as mystd;
28
29cfg_if::cfg_if! {
30    if #[cfg(windows)] {
31        #[path = "gimli/mmap_windows.rs"]
32        mod mmap;
33    } else if #[cfg(target_vendor = "apple")] {
34        #[path = "gimli/mmap_unix.rs"]
35        mod mmap;
36    } else if #[cfg(any(
37        target_os = "android",
38        target_os = "freebsd",
39        target_os = "fuchsia",
40        target_os = "haiku",
41        target_os = "hurd",
42        target_os = "linux",
43        target_os = "openbsd",
44        target_os = "solaris",
45        target_os = "illumos",
46        target_os = "aix",
47    ))] {
48        #[path = "gimli/mmap_unix.rs"]
49        mod mmap;
50    } else {
51        #[path = "gimli/mmap_fake.rs"]
52        mod mmap;
53    }
54}
55
56mod stash;
57
58const MAPPINGS_CACHE_SIZE: usize = 4;
59
60struct Mapping {
61    // 'static lifetime is a lie to hack around lack of support for self-referential structs.
62    cx: Context<'static>,
63    _map: Mmap,
64    stash: Stash,
65}
66
67enum Either<A, B> {
68    #[allow(dead_code)]
69    A(A),
70    B(B),
71}
72
73impl Mapping {
74    /// Creates a `Mapping` by ensuring that the `data` specified is used to
75    /// create a `Context` and it can only borrow from that or the `Stash` of
76    /// decompressed sections or auxiliary data.
77    fn mk<F>(data: Mmap, mk: F) -> Option<Mapping>
78    where
79        F: for<'a> FnOnce(&'a [u8], &'a Stash) -> Option<Context<'a>>,
80    {
81        Mapping::mk_or_other(data, move |data, stash| {
82            let cx = mk(data, stash)?;
83            Some(Either::B(cx))
84        })
85    }
86
87    /// Creates a `Mapping` from `data`, or if the closure decides to, returns a
88    /// different mapping.
89    fn mk_or_other<F>(data: Mmap, mk: F) -> Option<Mapping>
90    where
91        F: for<'a> FnOnce(&'a [u8], &'a Stash) -> Option<Either<Mapping, Context<'a>>>,
92    {
93        let stash = Stash::new();
94        let cx = match mk(&data, &stash)? {
95            Either::A(mapping) => return Some(mapping),
96            Either::B(cx) => cx,
97        };
98        Some(Mapping {
99            // Convert to 'static lifetimes since the symbols should
100            // only borrow `map` and `stash` and we're preserving them below.
101            cx: unsafe { core::mem::transmute::<Context<'_>, Context<'static>>(cx) },
102            _map: data,
103            stash,
104        })
105    }
106}
107
108struct Context<'a> {
109    dwarf: addr2line::Context<EndianSlice<'a, Endian>>,
110    object: Object<'a>,
111    package: Option<gimli::DwarfPackage<EndianSlice<'a, Endian>>>,
112}
113
114impl<'data> Context<'data> {
115    fn new(
116        stash: &'data Stash,
117        object: Object<'data>,
118        sup: Option<Object<'data>>,
119        dwp: Option<Object<'data>>,
120    ) -> Option<Context<'data>> {
121        let mut sections = gimli::Dwarf::load(|id| -> Result<_, ()> {
122            if cfg!(not(target_os = "aix")) {
123                let data = object.section(stash, id.name()).unwrap_or(&[]);
124                Ok(EndianSlice::new(data, Endian))
125            } else if let Some(name) = id.xcoff_name() {
126                let data = object.section(stash, name).unwrap_or(&[]);
127                Ok(EndianSlice::new(data, Endian))
128            } else {
129                Ok(EndianSlice::new(&[], Endian))
130            }
131        })
132        .ok()?;
133
134        if let Some(sup) = sup {
135            sections
136                .load_sup(|id| -> Result<_, ()> {
137                    let data = sup.section(stash, id.name()).unwrap_or(&[]);
138                    Ok(EndianSlice::new(data, Endian))
139                })
140                .ok()?;
141        }
142        let dwarf = addr2line::Context::from_dwarf(sections).ok()?;
143
144        let mut package = None;
145        if let Some(dwp) = dwp {
146            package = Some(
147                gimli::DwarfPackage::load(
148                    |id| -> Result<_, gimli::Error> {
149                        let data = id
150                            .dwo_name()
151                            .and_then(|name| dwp.section(stash, name))
152                            .unwrap_or(&[]);
153                        Ok(EndianSlice::new(data, Endian))
154                    },
155                    EndianSlice::new(&[], Endian),
156                )
157                .ok()?,
158            );
159        }
160
161        Some(Context {
162            dwarf,
163            object,
164            package,
165        })
166    }
167
168    fn find_frames(
169        &'_ self,
170        stash: &'data Stash,
171        probe: u64,
172    ) -> gimli::Result<addr2line::FrameIter<'_, EndianSlice<'data, Endian>>> {
173        use addr2line::{LookupContinuation, LookupResult};
174
175        let mut l = self.dwarf.find_frames(probe);
176        loop {
177            let (load, continuation) = match l {
178                LookupResult::Output(output) => break output,
179                LookupResult::Load { load, continuation } => (load, continuation),
180            };
181
182            l = continuation.resume(handle_split_dwarf(self.package.as_ref(), stash, load));
183        }
184    }
185}
186
187fn mmap(path: &Path) -> Option<Mmap> {
188    let file = File::open(path).ok()?;
189    let len = file.metadata().ok()?.len().try_into().ok()?;
190    unsafe { Mmap::map(&file, len) }
191}
192
193cfg_if::cfg_if! {
194    if #[cfg(windows)] {
195        mod coff;
196        use self::coff::{handle_split_dwarf, Object};
197    } else if #[cfg(any(target_vendor = "apple"))] {
198        mod macho;
199        use self::macho::{handle_split_dwarf, Object};
200    } else if #[cfg(target_os = "aix")] {
201        mod xcoff;
202        use self::xcoff::{handle_split_dwarf, Object};
203    } else {
204        mod elf;
205        use self::elf::{handle_split_dwarf, Object};
206    }
207}
208
209cfg_if::cfg_if! {
210    if #[cfg(windows)] {
211        mod libs_windows;
212        use libs_windows::native_libraries;
213    } else if #[cfg(target_vendor = "apple")] {
214        mod libs_macos;
215        use libs_macos::native_libraries;
216    } else if #[cfg(target_os = "illumos")] {
217        mod libs_illumos;
218        use libs_illumos::native_libraries;
219    } else if #[cfg(all(
220        any(
221            target_os = "linux",
222            target_os = "fuchsia",
223            target_os = "freebsd",
224            target_os = "hurd",
225            target_os = "openbsd",
226            target_os = "netbsd",
227            target_os = "nto",
228            target_os = "android",
229        ),
230        not(target_env = "uclibc"),
231    ))] {
232        mod libs_dl_iterate_phdr;
233        use libs_dl_iterate_phdr::native_libraries;
234        #[path = "gimli/parse_running_mmaps_unix.rs"]
235        mod parse_running_mmaps;
236    } else if #[cfg(target_env = "libnx")] {
237        mod libs_libnx;
238        use libs_libnx::native_libraries;
239    } else if #[cfg(target_os = "haiku")] {
240        mod libs_haiku;
241        use libs_haiku::native_libraries;
242    } else if #[cfg(target_os = "aix")] {
243        mod libs_aix;
244        use libs_aix::native_libraries;
245    } else {
246        // Everything else should doesn't know how to load native libraries.
247        fn native_libraries() -> Vec<Library> {
248            Vec::new()
249        }
250    }
251}
252
253#[derive(Default)]
254struct Cache {
255    /// All known shared libraries that have been loaded.
256    libraries: Vec<Library>,
257
258    /// Mappings cache where we retain parsed dwarf information.
259    ///
260    /// This list has a fixed capacity for its entire lifetime which never
261    /// increases. The `usize` element of each pair is an index into `libraries`
262    /// above where `usize::max_value()` represents the current executable. The
263    /// `Mapping` is corresponding parsed dwarf information.
264    ///
265    /// Note that this is basically an LRU cache and we'll be shifting things
266    /// around in here as we symbolize addresses.
267    mappings: Vec<(usize, Mapping)>,
268}
269
270struct Library {
271    name: OsString,
272    #[cfg(target_os = "aix")]
273    /// On AIX, the library mmapped can be a member of a big-archive file.
274    /// For example, with a big-archive named libfoo.a containing libbar.so,
275    /// one can use `dlopen("libfoo.a(libbar.so)", RTLD_MEMBER | RTLD_LAZY)`
276    /// to use the `libbar.so` library. In this case, only `libbar.so` is
277    /// mmapped, not the whole `libfoo.a`.
278    member_name: OsString,
279    /// Segments of this library loaded into memory, and where they're loaded.
280    segments: Vec<LibrarySegment>,
281    /// The "bias" of this library, typically where it's loaded into memory.
282    /// This value is added to each segment's stated address to get the actual
283    /// virtual memory address that the segment is loaded into. Additionally
284    /// this bias is subtracted from real virtual memory addresses to index into
285    /// debuginfo and the symbol table.
286    bias: usize,
287}
288
289struct LibrarySegment {
290    /// The stated address of this segment in the object file. This is not
291    /// actually where the segment is loaded, but rather this address plus the
292    /// containing library's `bias` is where to find it.
293    stated_virtual_memory_address: usize,
294    /// The size of this segment in memory.
295    len: usize,
296}
297
298#[cfg(target_os = "aix")]
299fn create_mapping(lib: &Library) -> Option<Mapping> {
300    let name = &lib.name;
301    let member_name = &lib.member_name;
302    Mapping::new(name.as_ref(), member_name)
303}
304
305#[cfg(not(target_os = "aix"))]
306fn create_mapping(lib: &Library) -> Option<Mapping> {
307    let name = &lib.name;
308    Mapping::new(name.as_ref())
309}
310
311// unsafe because this is required to be externally synchronized
312pub unsafe fn clear_symbol_cache() {
313    Cache::with_global(|cache| cache.mappings.clear());
314}
315
316impl Cache {
317    fn new() -> Cache {
318        Cache {
319            mappings: Vec::with_capacity(MAPPINGS_CACHE_SIZE),
320            libraries: native_libraries(),
321        }
322    }
323
324    // unsafe because this is required to be externally synchronized
325    unsafe fn with_global(f: impl FnOnce(&mut Self)) {
326        // A very small, very simple LRU cache for debug info mappings.
327        //
328        // The hit rate should be very high, since the typical stack doesn't cross
329        // between many shared libraries.
330        //
331        // The `addr2line::Context` structures are pretty expensive to create. Its
332        // cost is expected to be amortized by subsequent `locate` queries, which
333        // leverage the structures built when constructing `addr2line::Context`s to
334        // get nice speedups. If we didn't have this cache, that amortization would
335        // never happen, and symbolicating backtraces would be ssssllllooooowwww.
336        static mut MAPPINGS_CACHE: Option<Cache> = None;
337
338        f(MAPPINGS_CACHE.get_or_insert_with(|| Cache::new()))
339    }
340
341    fn avma_to_svma(&self, addr: *const u8) -> Option<(usize, *const u8)> {
342        self.libraries
343            .iter()
344            .enumerate()
345            .filter_map(|(i, lib)| {
346                // First up, test if this `lib` has any segment containing the
347                // `addr` (handling relocation). If this check passes then we
348                // can continue below and actually translate the address.
349                //
350                // Note that we're using `wrapping_add` here to avoid overflow
351                // checks. It's been seen in the wild that the SVMA + bias
352                // computation overflows. It seems a bit odd that would happen
353                // but there's not a huge amount we can do about it other than
354                // probably just ignore those segments since they're likely
355                // pointing off into space. This originally came up in
356                // rust-lang/backtrace-rs#329.
357                if !lib.segments.iter().any(|s| {
358                    let svma = s.stated_virtual_memory_address;
359                    let start = svma.wrapping_add(lib.bias);
360                    let end = start.wrapping_add(s.len);
361                    let address = addr as usize;
362                    start <= address && address < end
363                }) {
364                    return None;
365                }
366
367                // Now that we know `lib` contains `addr`, we can offset with
368                // the bias to find the stated virtual memory address.
369                let svma = (addr as usize).wrapping_sub(lib.bias);
370                Some((i, svma as *const u8))
371            })
372            .next()
373    }
374
375    fn mapping_for_lib<'a>(&'a mut self, lib: usize) -> Option<(&'a mut Context<'a>, &'a Stash)> {
376        let idx = self.mappings.iter().position(|(idx, _)| *idx == lib);
377
378        // Invariant: after this conditional completes without early returning
379        // from an error, the cache entry for this path is at index 0.
380
381        if let Some(idx) = idx {
382            // When the mapping is already in the cache, move it to the front.
383            if idx != 0 {
384                let entry = self.mappings.remove(idx);
385                self.mappings.insert(0, entry);
386            }
387        } else {
388            // When the mapping is not in the cache, create a new mapping,
389            // insert it into the front of the cache, and evict the oldest cache
390            // entry if necessary.
391            let mapping = create_mapping(&self.libraries[lib])?;
392
393            if self.mappings.len() == MAPPINGS_CACHE_SIZE {
394                self.mappings.pop();
395            }
396
397            self.mappings.insert(0, (lib, mapping));
398        }
399
400        let mapping = &mut self.mappings[0].1;
401        let cx: &'a mut Context<'static> = &mut mapping.cx;
402        let stash: &'a Stash = &mapping.stash;
403        // don't leak the `'static` lifetime, make sure it's scoped to just
404        // ourselves
405        Some((
406            unsafe { mem::transmute::<&'a mut Context<'static>, &'a mut Context<'a>>(cx) },
407            stash,
408        ))
409    }
410}
411
412pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) {
413    let addr = what.address_or_ip();
414    let mut call = |sym: Symbol<'_>| {
415        // Extend the lifetime of `sym` to `'static` since we are unfortunately
416        // required to here, but it's only ever going out as a reference so no
417        // reference to it should be persisted beyond this frame anyway.
418        let sym = mem::transmute::<Symbol<'_>, Symbol<'static>>(sym);
419        (cb)(&super::Symbol { inner: sym });
420    };
421
422    Cache::with_global(|cache| {
423        let (lib, addr) = match cache.avma_to_svma(addr.cast_const().cast::<u8>()) {
424            Some(pair) => pair,
425            None => return,
426        };
427
428        // Finally, get a cached mapping or create a new mapping for this file, and
429        // evaluate the DWARF info to find the file/line/name for this address.
430        let (cx, stash) = match cache.mapping_for_lib(lib) {
431            Some((cx, stash)) => (cx, stash),
432            None => return,
433        };
434        let mut any_frames = false;
435        if let Ok(mut frames) = cx.find_frames(stash, addr as u64) {
436            while let Ok(Some(frame)) = frames.next() {
437                any_frames = true;
438                let name = match frame.function {
439                    Some(f) => Some(f.name.slice()),
440                    None => cx.object.search_symtab(addr as u64),
441                };
442                call(Symbol::Frame {
443                    addr: addr as *mut c_void,
444                    location: frame.location,
445                    name,
446                });
447            }
448        }
449        if !any_frames {
450            if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) {
451                if let Ok(mut frames) = object_cx.find_frames(stash, object_addr) {
452                    while let Ok(Some(frame)) = frames.next() {
453                        any_frames = true;
454                        call(Symbol::Frame {
455                            addr: addr as *mut c_void,
456                            location: frame.location,
457                            name: frame.function.map(|f| f.name.slice()),
458                        });
459                    }
460                }
461            }
462        }
463        if !any_frames {
464            if let Some(name) = cx.object.search_symtab(addr as u64) {
465                call(Symbol::Symtab { name });
466            }
467        }
468    });
469}
470
471pub enum Symbol<'a> {
472    /// We were able to locate frame information for this symbol, and
473    /// `addr2line`'s frame internally has all the nitty gritty details.
474    Frame {
475        addr: *mut c_void,
476        location: Option<addr2line::Location<'a>>,
477        name: Option<&'a [u8]>,
478    },
479    /// Couldn't find debug information, but we found it in the symbol table of
480    /// the elf executable.
481    Symtab { name: &'a [u8] },
482}
483
484impl Symbol<'_> {
485    pub fn name(&self) -> Option<SymbolName<'_>> {
486        match self {
487            Symbol::Frame { name, .. } => {
488                let name = name.as_ref()?;
489                Some(SymbolName::new(name))
490            }
491            Symbol::Symtab { name, .. } => Some(SymbolName::new(name)),
492        }
493    }
494
495    pub fn addr(&self) -> Option<*mut c_void> {
496        match self {
497            Symbol::Frame { addr, .. } => Some(*addr),
498            Symbol::Symtab { .. } => None,
499        }
500    }
501
502    pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
503        match self {
504            Symbol::Frame { location, .. } => {
505                let file = location.as_ref()?.file?;
506                Some(BytesOrWideString::Bytes(file.as_bytes()))
507            }
508            Symbol::Symtab { .. } => None,
509        }
510    }
511
512    pub fn filename(&self) -> Option<&Path> {
513        match self {
514            Symbol::Frame { location, .. } => {
515                let file = location.as_ref()?.file?;
516                Some(Path::new(file))
517            }
518            Symbol::Symtab { .. } => None,
519        }
520    }
521
522    pub fn lineno(&self) -> Option<u32> {
523        match self {
524            Symbol::Frame { location, .. } => location.as_ref()?.line,
525            Symbol::Symtab { .. } => None,
526        }
527    }
528
529    pub fn colno(&self) -> Option<u32> {
530        match self {
531            Symbol::Frame { location, .. } => location.as_ref()?.column,
532            Symbol::Symtab { .. } => None,
533        }
534    }
535}