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