diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 24d814363..29fff2795 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -229,7 +229,7 @@ jobs: with: submodules: true - name: Install Rust - run: rustup update 1.42.0 && rustup default 1.42.0 + run: rustup update 1.55.0 && rustup default 1.55.0 - run: cargo build miri: diff --git a/Cargo.toml b/Cargo.toml index 477909111..7e48a0026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ rustc-serialize = { version = "0.3", optional = true } # Optionally demangle C++ frames' symbols in backtraces. cpp_demangle = { default-features = false, version = "0.4.0", optional = true, features = ["alloc"] } -addr2line = { version = "0.19.0", default-features = false } +addr2line = { version = "0.20.0", default-features = false } miniz_oxide = { version = "0.6.0", default-features = false } [dependencies.object] diff --git a/crates/as-if-std/Cargo.toml b/crates/as-if-std/Cargo.toml index c763227f2..655f47f90 100644 --- a/crates/as-if-std/Cargo.toml +++ b/crates/as-if-std/Cargo.toml @@ -15,7 +15,7 @@ bench = false cfg-if = "1.0" rustc-demangle = "0.1.4" libc = { version = "0.2.45", default-features = false } -addr2line = { version = "0.16.0", default-features = false, optional = true } +addr2line = { version = "0.20.0", default-features = false, optional = true } miniz_oxide = { version = "0.4.0", default-features = false } [dependencies.object] diff --git a/src/symbolize/gimli.rs b/src/symbolize/gimli.rs index cd4cec58c..a48c6860d 100644 --- a/src/symbolize/gimli.rs +++ b/src/symbolize/gimli.rs @@ -58,7 +58,7 @@ struct Mapping { // 'static lifetime is a lie to hack around lack of support for self-referential structs. cx: Context<'static>, _map: Mmap, - _stash: Stash, + stash: Stash, } enum Either { @@ -97,7 +97,7 @@ impl Mapping { // only borrow `map` and `stash` and we're preserving them below. cx: unsafe { core::mem::transmute::, Context<'static>>(cx) }, _map: data, - _stash: stash, + stash: stash, }) } } @@ -105,6 +105,7 @@ impl Mapping { struct Context<'a> { dwarf: addr2line::Context>, object: Object<'a>, + package: Option>>, } impl<'data> Context<'data> { @@ -112,6 +113,7 @@ impl<'data> Context<'data> { stash: &'data Stash, object: Object<'data>, sup: Option>, + dwp: Option>, ) -> Option> { let mut sections = gimli::Dwarf::load(|id| -> Result<_, ()> { let data = object.section(stash, id.name()).unwrap_or(&[]); @@ -129,7 +131,46 @@ impl<'data> Context<'data> { } let dwarf = addr2line::Context::from_dwarf(sections).ok()?; - Some(Context { dwarf, object }) + let mut package = None; + if let Some(dwp) = dwp { + package = Some( + gimli::DwarfPackage::load( + |id| -> Result<_, gimli::Error> { + let data = id + .dwo_name() + .and_then(|name| dwp.section(stash, name)) + .unwrap_or(&[]); + Ok(EndianSlice::new(data, Endian)) + }, + EndianSlice::new(&[], Endian), + ) + .ok()?, + ); + } + + Some(Context { + dwarf, + object, + package, + }) + } + + fn find_frames( + &'_ self, + stash: &'data Stash, + probe: u64, + ) -> gimli::Result>> { + use addr2line::{LookupContinuation, LookupResult}; + + let mut l = self.dwarf.find_frames(probe); + loop { + let (load, continuation) = match l { + LookupResult::Output(output) => break output, + LookupResult::Load { load, continuation } => (load, continuation), + }; + + l = continuation.resume(handle_split_dwarf(self.package.as_ref(), stash, load)); + } } } @@ -142,7 +183,7 @@ fn mmap(path: &Path) -> Option { cfg_if::cfg_if! { if #[cfg(windows)] { mod coff; - use self::coff::Object; + use self::coff::{handle_split_dwarf, Object}; } else if #[cfg(any( target_os = "macos", target_os = "ios", @@ -150,10 +191,10 @@ cfg_if::cfg_if! { target_os = "watchos", ))] { mod macho; - use self::macho::Object; + use self::macho::{handle_split_dwarf, Object}; } else { mod elf; - use self::elf::Object; + use self::elf::{handle_split_dwarf, Object}; } } @@ -302,7 +343,7 @@ impl Cache { .next() } - fn mapping_for_lib<'a>(&'a mut self, lib: usize) -> Option<&'a mut Context<'a>> { + fn mapping_for_lib<'a>(&'a mut self, lib: usize) -> Option<(&'a mut Context<'a>, &'a Stash)> { let idx = self.mappings.iter().position(|(idx, _)| *idx == lib); // Invariant: after this conditional completes without early returning @@ -328,10 +369,15 @@ impl Cache { self.mappings.insert(0, (lib, mapping)); } - let cx: &'a mut Context<'static> = &mut self.mappings[0].1.cx; + let mapping = &mut self.mappings[0].1; + let cx: &'a mut Context<'static> = &mut mapping.cx; + let stash: &'a Stash = &mapping.stash; // don't leak the `'static` lifetime, make sure it's scoped to just // ourselves - Some(unsafe { mem::transmute::<&'a mut Context<'static>, &'a mut Context<'a>>(cx) }) + Some(( + unsafe { mem::transmute::<&'a mut Context<'static>, &'a mut Context<'a>>(cx) }, + stash, + )) } } @@ -353,12 +399,12 @@ pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) // Finally, get a cached mapping or create a new mapping for this file, and // evaluate the DWARF info to find the file/line/name for this address. - let cx = match cache.mapping_for_lib(lib) { - Some(cx) => cx, + let (cx, stash) = match cache.mapping_for_lib(lib) { + Some((cx, stash)) => (cx, stash), None => return, }; let mut any_frames = false; - if let Ok(mut frames) = cx.dwarf.find_frames(addr as u64) { + if let Ok(mut frames) = cx.find_frames(stash, addr as u64) { while let Ok(Some(frame)) = frames.next() { any_frames = true; let name = match frame.function { @@ -374,7 +420,7 @@ pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) } if !any_frames { if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) { - if let Ok(mut frames) = object_cx.dwarf.find_frames(object_addr) { + if let Ok(mut frames) = object_cx.find_frames(stash, object_addr) { while let Ok(Some(frame)) = frames.next() { any_frames = true; call(Symbol::Frame { diff --git a/src/symbolize/gimli/coff.rs b/src/symbolize/gimli/coff.rs index 84d334207..7b78ca297 100644 --- a/src/symbolize/gimli/coff.rs +++ b/src/symbolize/gimli/coff.rs @@ -1,4 +1,5 @@ -use super::{Context, Mapping, Path, Stash, Vec}; +use super::{gimli, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec}; +use alloc::sync::Arc; use core::convert::TryFrom; use object::pe::{ImageDosHeader, ImageSymbol}; use object::read::pe::{ImageNtHeaders, ImageOptionalHeader, SectionTable}; @@ -14,7 +15,7 @@ impl Mapping { pub fn new(path: &Path) -> Option { let map = super::mmap(path)?; Mapping::mk(map, |data, stash| { - Context::new(stash, Object::parse(data)?, None) + Context::new(stash, Object::parse(data)?, None, None) }) } } @@ -106,3 +107,11 @@ impl<'a> Object<'a> { None } } + +pub(super) fn handle_split_dwarf<'data>( + _package: Option<&gimli::DwarfPackage>>, + _stash: &'data Stash, + _load: addr2line::SplitDwarfLoad>, +) -> Option>>> { + None +} diff --git a/src/symbolize/gimli/elf.rs b/src/symbolize/gimli/elf.rs index bc71ee2c9..b0eec0762 100644 --- a/src/symbolize/gimli/elf.rs +++ b/src/symbolize/gimli/elf.rs @@ -3,7 +3,8 @@ use super::mystd::fs; use super::mystd::os::unix::ffi::{OsStrExt, OsStringExt}; use super::mystd::path::{Path, PathBuf}; use super::Either; -use super::{Context, Mapping, Stash, Vec}; +use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash, Vec}; +use alloc::sync::Arc; use core::convert::{TryFrom, TryInto}; use core::str; use object::elf::{ELFCOMPRESS_ZLIB, ELF_NOTE_GNU, NT_GNU_BUILD_ID, SHF_COMPRESSED}; @@ -24,24 +25,26 @@ impl Mapping { // Try to locate an external debug file using the build ID. if let Some(path_debug) = object.build_id().and_then(locate_build_id) { - if let Some(mapping) = Mapping::new_debug(path_debug, None) { + if let Some(mapping) = Mapping::new_debug(path, path_debug, None) { return Some(Either::A(mapping)); } } // Try to locate an external debug file using the GNU debug link section. if let Some((path_debug, crc)) = object.gnu_debuglink_path(path) { - if let Some(mapping) = Mapping::new_debug(path_debug, Some(crc)) { + if let Some(mapping) = Mapping::new_debug(path, path_debug, Some(crc)) { return Some(Either::A(mapping)); } } - Context::new(stash, object, None).map(Either::B) + let dwp = Mapping::load_dwarf_package(path, stash); + + Context::new(stash, object, None, dwp).map(Either::B) }) } /// Load debuginfo from an external debug file. - fn new_debug(path: PathBuf, crc: Option) -> Option { + fn new_debug(original_path: &Path, path: PathBuf, crc: Option) -> Option { let map = super::mmap(&path)?; Mapping::mk(map, |map, stash| { let object = Object::parse(&map)?; @@ -51,20 +54,45 @@ impl Mapping { } // Try to locate a supplementary object file. + let mut sup = None; if let Some((path_sup, build_id_sup)) = object.gnu_debugaltlink_path(&path) { if let Some(map_sup) = super::mmap(&path_sup) { - let map_sup = stash.set_mmap_aux(map_sup); - if let Some(sup) = Object::parse(map_sup) { - if sup.build_id() == Some(build_id_sup) { - return Context::new(stash, object, Some(sup)); + let map_sup = stash.cache_mmap(map_sup); + if let Some(sup_) = Object::parse(map_sup) { + if sup_.build_id() == Some(build_id_sup) { + sup = Some(sup_); } } } } - Context::new(stash, object, None) + let dwp = Mapping::load_dwarf_package(original_path, stash); + + Context::new(stash, object, sup, dwp) }) } + + /// Try to locate a DWARF package file. + fn load_dwarf_package<'data>(path: &Path, stash: &'data Stash) -> Option> { + let mut path_dwp = path.to_path_buf(); + let dwp_extension = path + .extension() + .map(|previous_extension| { + let mut previous_extension = previous_extension.to_os_string(); + previous_extension.push(".dwp"); + previous_extension + }) + .unwrap_or_else(|| "dwp".into()); + path_dwp.set_extension(dwp_extension); + if let Some(map_dwp) = super::mmap(&path_dwp) { + let map_dwp = stash.cache_mmap(map_dwp); + if let Some(dwp_) = Object::parse(map_dwp) { + return Some(dwp_); + } + } + + None + } } struct ParsedSym { @@ -421,3 +449,47 @@ fn locate_debugaltlink(path: &Path, filename: &[u8], build_id: &[u8]) -> Option< locate_build_id(build_id) } + +fn convert_path(r: &R) -> Result { + let bytes = r.to_slice()?; + Ok(PathBuf::from(OsStr::from_bytes(&bytes))) +} + +pub(super) fn handle_split_dwarf<'data>( + package: Option<&gimli::DwarfPackage>>, + stash: &'data Stash, + load: addr2line::SplitDwarfLoad>, +) -> Option>>> { + if let Some(dwp) = package.as_ref() { + if let Ok(Some(cu)) = dwp.find_cu(load.dwo_id, &load.parent) { + return Some(Arc::new(cu)); + } + } + + let mut path = PathBuf::new(); + if let Some(p) = load.comp_dir.as_ref() { + path.push(convert_path(p).ok()?); + } + + path.push(convert_path(load.path.as_ref()?).ok()?); + + if let Some(map_dwo) = super::mmap(&path) { + let map_dwo = stash.cache_mmap(map_dwo); + if let Some(dwo) = Object::parse(map_dwo) { + return gimli::Dwarf::load(|id| -> Result<_, ()> { + let data = id + .dwo_name() + .and_then(|name| dwo.section(stash, name)) + .unwrap_or(&[]); + Ok(EndianSlice::new(data, Endian)) + }) + .ok() + .map(|mut dwo_dwarf| { + dwo_dwarf.make_dwo(&load.parent); + Arc::new(dwo_dwarf) + }); + } + } + + None +} diff --git a/src/symbolize/gimli/macho.rs b/src/symbolize/gimli/macho.rs index adea97a09..74ed8091a 100644 --- a/src/symbolize/gimli/macho.rs +++ b/src/symbolize/gimli/macho.rs @@ -1,4 +1,5 @@ -use super::{Box, Context, Mapping, Path, Stash, Vec}; +use super::{gimli, Box, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec}; +use alloc::sync::Arc; use core::convert::TryInto; use object::macho; use object::read::macho::{MachHeader, Nlist, Section, Segment as _}; @@ -45,7 +46,7 @@ impl Mapping { let (macho, data) = find_header(data)?; let endian = macho.endian().ok()?; let obj = Object::parse(macho, endian, data)?; - Context::new(stash, obj, None) + Context::new(stash, obj, None, None) }) } @@ -82,7 +83,7 @@ impl Mapping { return None; } let obj = Object::parse(macho, endian, data)?; - Context::new(stash, obj, None) + Context::new(stash, obj, None, None) }); if let Some(candidate) = candidate { return Some(candidate); @@ -309,7 +310,7 @@ fn object_mapping(path: &[u8]) -> Option { let (macho, data) = find_header(data)?; let endian = macho.endian().ok()?; let obj = Object::parse(macho, endian, data)?; - Context::new(stash, obj, None) + Context::new(stash, obj, None, None) }) } @@ -322,3 +323,11 @@ fn split_archive_path(path: &[u8]) -> Option<(&[u8], &[u8])> { let (archive, rest) = path.split_at(index); Some((archive, &rest[1..])) } + +pub(super) fn handle_split_dwarf<'data>( + _package: Option<&gimli::DwarfPackage>>, + _stash: &'data Stash, + _load: addr2line::SplitDwarfLoad>, +) -> Option>>> { + None +} diff --git a/src/symbolize/gimli/stash.rs b/src/symbolize/gimli/stash.rs index 3adfc598a..792f9a6f8 100644 --- a/src/symbolize/gimli/stash.rs +++ b/src/symbolize/gimli/stash.rs @@ -9,14 +9,14 @@ use core::cell::UnsafeCell; /// A simple arena allocator for byte buffers. pub struct Stash { buffers: UnsafeCell>>, - mmap_aux: UnsafeCell>, + mmaps: UnsafeCell>, } impl Stash { pub fn new() -> Stash { Stash { buffers: UnsafeCell::new(Vec::new()), - mmap_aux: UnsafeCell::new(None), + mmaps: UnsafeCell::new(Vec::new()), } } @@ -35,18 +35,16 @@ impl Stash { /// Stores a `Mmap` for the lifetime of this `Stash`, returning a pointer /// which is scoped to just this lifetime. - pub fn set_mmap_aux(&self, map: Mmap) -> &[u8] { + pub fn cache_mmap(&self, map: Mmap) -> &[u8] { // SAFETY: this is the only location for a mutable pointer to - // `mmap_aux`, and this structure isn't threadsafe to shared across - // threads either. This also is careful to store at most one `mmap_aux` - // since overwriting a previous one would invalidate the previous - // pointer. Given that though we can safely return a pointer to our - // interior-owned contents. + // `mmaps`, and this structure isn't threadsafe to shared across + // threads either. We also never remove elements from `self.mmaps`, + // so a reference to the data inside the map will live as long as + // `self` does. unsafe { - let mmap_aux = &mut *self.mmap_aux.get(); - assert!(mmap_aux.is_none()); - *mmap_aux = Some(map); - mmap_aux.as_ref().unwrap() + let mmaps = &mut *self.mmaps.get(); + mmaps.push(map); + mmaps.last().unwrap() } } }