Skip to content

Commit

Permalink
Use OnceCell in KsymResolver
Browse files Browse the repository at this point in the history
Switch KsymResolver over to using OnceCell as opposed to RefCell.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o committed Nov 10, 2023
1 parent 2324fbf commit 9e8a073
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/elf/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ impl Debug for Cache<'_> {
#[derive(Debug)]
pub(crate) struct ElfParser {
/// A cache for relevant parts of the ELF file.
/// SAFETY: We must not hand out references with a 'static lifetime to
/// this member. Rather, they should never outlive `self`.
/// Furthermore, this member has to be listed before `mmap`
/// to make sure we never end up with a dangling reference.
// SAFETY: We must not hand out references with a 'static lifetime to
// this member. Rather, they should never outlive `self`.
// Furthermore, this member has to be listed before `mmap`
// to make sure we never end up with a dangling reference.
cache: RefCell<Cache<'static>>,
/// The memory mapped file.
_mmap: Mmap,
Expand Down
42 changes: 23 additions & 19 deletions src/ksym.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::Formatter;
Expand All @@ -13,6 +12,7 @@ use std::path::PathBuf;
use crate::inspect::FindAddrOpts;
use crate::inspect::SymInfo;
use crate::inspect::SymType;
use crate::once::OnceCell;
use crate::symbolize::AddrCodeInfo;
use crate::symbolize::IntSym;
use crate::symbolize::SrcLang;
Expand Down Expand Up @@ -50,8 +50,12 @@ impl<'ksym> From<&'ksym Ksym> for IntSym<'ksym> {
/// The users should provide the path of kallsyms, so you can provide
/// a copy from other devices.
pub struct KSymResolver {
// SAFETY: We must not hand out strings with a 'static lifetime to
// callers. Rather, they should never outlive `self`.
// Furthermore, this member has to be listed before `syms`
// to make sure we never end up with dangling references.
sym_to_addr: OnceCell<HashMap<&'static str, Addr>>,
syms: Vec<Ksym>,
sym_to_addr: RefCell<HashMap<&'static str, Addr>>,
file_name: PathBuf,
}

Expand Down Expand Up @@ -88,24 +92,12 @@ impl KSymResolver {

let slf = Self {
syms,
sym_to_addr: RefCell::default(),
sym_to_addr: OnceCell::new(),
file_name: filename,
};
Ok(slf)
}

fn ensure_sym_to_addr(&self) {
if self.sym_to_addr.borrow().len() > 0 {
return
}
let mut sym_to_addr = self.sym_to_addr.borrow_mut();
for Ksym { name, addr } in self.syms.iter() {
// Performance & lifetime hacking
let name_static = unsafe { &*(name as *const String) };
sym_to_addr.insert(name_static, *addr);
}
}

fn find_ksym(&self, addr: Addr) -> Option<&Ksym> {
find_match_or_lower_bound_by_key(&self.syms, addr, |ksym: &Ksym| ksym.addr)
.and_then(|idx| self.syms.get(idx))
Expand All @@ -127,9 +119,21 @@ impl SymResolver for KSymResolver {
if let SymType::Variable = opts.sym_type {
return Ok(Vec::new())
}
let () = self.ensure_sym_to_addr();

let sym_to_addr = self.sym_to_addr.borrow();
let sym_to_addr = self.sym_to_addr.get_or_init(|| {
self.syms
.iter()
.map(|Ksym { name, addr }| {
// SAFETY: We ensure that all `Ksym` objects outlive the
// `syms` member, so conjuring up a 'static
// lifetime is fine.
let name =
unsafe { (name.as_ref() as *const str).as_ref::<'static>().unwrap() };
(name, *addr)
})
.collect()
});

if let Some((name, addr)) = sym_to_addr.get_key_value(name) {
Ok(vec![SymInfo {
name: Cow::Borrowed(name),
Expand Down Expand Up @@ -170,7 +174,7 @@ mod tests {
fn debug_repr() {
let resolver = KSymResolver {
syms: Vec::new(),
sym_to_addr: RefCell::default(),
sym_to_addr: OnceCell::new(),
file_name: PathBuf::new(),
};
assert_ne!(format!("{resolver:?}"), "");
Expand Down Expand Up @@ -249,7 +253,7 @@ mod tests {
name: "3".to_string(),
},
],
sym_to_addr: RefCell::default(),
sym_to_addr: OnceCell::new(),
file_name: PathBuf::new(),
};

Expand Down

0 comments on commit 9e8a073

Please sign in to comment.