Skip to content

Commit

Permalink
Add more tests for ELF and DWARF symbolization
Browse files Browse the repository at this point in the history
This change extends our ELF and DWARF symbolization tests. ELF
symbolization was not actually exercised from Rust, while DWARF
symbolization was lacking in the C API tests. So fix that. Also extend
the tests to check for source directories, file names, and line numbers
(or lack thereof).

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Aug 3, 2023
1 parent 4b4bb16 commit 3508671
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 58 deletions.
92 changes: 62 additions & 30 deletions tests/blazesym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,14 @@ fn symbolize_gsym() {
test(src);
}

/// Check that we can symbolize an address using DWARF.
/// Check that we can symbolize an address using ELF or DWARF.
#[test]
fn symbolize_dwarf() {
let test_dwarf = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-dwarf-only.bin");
let src = symbolize::Source::Elf(symbolize::Elf::new(&test_dwarf));
let symbolizer = Symbolizer::new();
let results = symbolizer
.symbolize(&src, &[0x2000100])
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<_>>();
assert_eq!(results.len(), 1);

let result = results.first().unwrap();
assert_eq!(result.name, "factorial");
assert_eq!(result.addr, 0x2000100);
assert_eq!(result.offset, 0);
assert_eq!(result.line, Some(8));

// Inquire symbol size.
let size = find_function_size("factorial", &test_dwarf);
assert_ne!(size, 0);

// Now check that we can symbolize addresses at a positive offset from the
// start of the function.
for offset in 1..size {
fn symbolize_elf_dwarf() {
fn test(path: &Path, dwarf: bool) {
let src = symbolize::Source::Elf(symbolize::Elf::new(path));
let symbolizer = Symbolizer::new();
let results = symbolizer
.symbolize(&src, &[0x2000100 + offset])
.symbolize(&src, &[0x2000100])
.unwrap()
.into_iter()
.flatten()
Expand All @@ -144,8 +121,63 @@ fn symbolize_dwarf() {
let result = results.first().unwrap();
assert_eq!(result.name, "factorial");
assert_eq!(result.addr, 0x2000100);
assert_eq!(result.offset, offset);
assert_eq!(result.offset, 0);

if dwarf {
assert!(result
.path
.as_ref()
.unwrap()
.ends_with("test-stable-addresses.c"));
assert_eq!(result.line, Some(8));
} else {
assert_eq!(result.path, None);
assert_eq!(result.line, None);
}

// Inquire symbol size.
let size = find_function_size("factorial", path);
assert_ne!(size, 0);

// Now check that we can symbolize addresses at a positive offset from the
// start of the function.
for offset in 1..size {
let results = symbolizer
.symbolize(&src, &[0x2000100 + offset])
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<_>>();
assert_eq!(results.len(), 1);

let result = results.first().unwrap();
assert_eq!(result.name, "factorial");
assert_eq!(result.addr, 0x2000100);
assert_eq!(result.offset, offset);

if dwarf {
assert!(result
.path
.as_ref()
.unwrap()
.ends_with("test-stable-addresses.c"));
assert!(result.line.is_some());
} else {
assert_eq!(result.path, None);
assert_eq!(result.line, None);
}
}
}

let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-no-dwarf.bin");
test(&path, false);

let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-dwarf-only.bin");
test(&path, true);
}

/// Check that we can symbolize the `abort_creds` function inside a
Expand Down
80 changes: 52 additions & 28 deletions tests/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,61 @@ fn symbolizer_creation_with_opts() {

/// Make sure that we can symbolize an address in an ELF file.
#[test]
fn symbolize_from_elf() {
let test_dwarf = Path::new(&env!("CARGO_MANIFEST_DIR"))
fn symbolize_elf_dwarf() {
fn test(path: &CStr, dwarf: bool) {
let elf_src = blaze_symbolize_src_elf {
path: path.as_ptr(),
};

let symbolizer = blaze_symbolizer_new();
let addrs = [0x2000100];
let result =
unsafe { blaze_symbolize_elf(symbolizer, &elf_src, addrs.as_ptr(), addrs.len()) };

assert!(!result.is_null());

let result = unsafe { &*result };
assert_eq!(result.size, 1);
let entries = unsafe { slice::from_raw_parts(result.entries.as_ptr(), result.size) };
let entry = &entries[0];
assert_eq!(entry.size, 1);

let syms = unsafe { slice::from_raw_parts(entry.syms, entry.size) };
let sym = &syms[0];
assert_eq!(
unsafe { CStr::from_ptr(sym.name) },
CStr::from_bytes_with_nul(b"factorial\0").unwrap()
);
assert_eq!(sym.addr, 0x2000100);
assert_eq!(sym.offset, 0);

if dwarf {
assert!(!sym.path.is_null());
assert!(unsafe { CStr::from_ptr(sym.path) }
.to_str()
.unwrap()
.ends_with("test-stable-addresses.c"));
assert_eq!(sym.line, 8);
} else {
assert!(sym.path.is_null());
assert_eq!(sym.line, 0);
}

let () = unsafe { blaze_result_free(result) };
let () = unsafe { blaze_symbolizer_free(symbolizer) };
}

let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-no-dwarf.bin");
let test_dwarf_c = CString::new(test_dwarf.to_str().unwrap()).unwrap();

let elf_src = blaze_symbolize_src_elf {
path: test_dwarf_c.as_ptr(),
};
let path_c = CString::new(path.to_str().unwrap()).unwrap();
test(&path_c, false);

let symbolizer = blaze_symbolizer_new();
let addrs = [0x2000100];
let result = unsafe { blaze_symbolize_elf(symbolizer, &elf_src, addrs.as_ptr(), addrs.len()) };

assert!(!result.is_null());

let result = unsafe { &*result };
assert_eq!(result.size, 1);
let entries = unsafe { slice::from_raw_parts(result.entries.as_ptr(), result.size) };
let entry = &entries[0];
assert_eq!(entry.size, 1);

let syms = unsafe { slice::from_raw_parts(entry.syms, entry.size) };
let sym = &syms[0];
assert_eq!(
unsafe { CStr::from_ptr(sym.name) },
CStr::from_bytes_with_nul(b"factorial\0").unwrap()
);

let () = unsafe { blaze_result_free(result) };
let () = unsafe { blaze_symbolizer_free(symbolizer) };
let path = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("test-stable-addresses-dwarf-only.bin");
let path_c = CString::new(path.to_str().unwrap()).unwrap();
test(&path_c, true);
}


Expand Down

0 comments on commit 3508671

Please sign in to comment.