Skip to content

Commit

Permalink
Fix tests assuming /proc/kallsyms
Browse files Browse the repository at this point in the history
When /proc/kallsyms is not present on the system, three tests fail
execution. Fix them by making them use a proper kallsyms file below
data/.

Fixes: libbpf#86

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Mar 17, 2023
1 parent daecf63 commit 0900a29
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/data/*.bin
/data/*.gsym
/data/kallsyms
/data/vmlinux-5.17.12-100.fc34.x86_64
/target
Cargo.lock
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Kui-Feng <thinker.li@gmail.com>"]
license-file = "LICENSE"
repository = "https://github.com/libbpf/blazesym"
edition = "2021"
exclude = ["data/dwarf-example"]
exclude = ["data/dwarf-example", "data/kallsyms.xz"]
autobenches = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down Expand Up @@ -44,7 +44,7 @@ xz2 = {version = "0.1.7", optional = true}
generate-c-header = ["cbindgen"]
# Enable this feature to opt in to the generation of test files. Having test
# files created is necessary for running tests.
generate-test-files = []
generate-test-files = ["xz2"]
# Enable this feature to opt in to the generation of benchmark files.
# This feature is required for some of the benchmarks. Note that git-lfs
# needs to be installed in this case.
Expand Down
11 changes: 8 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ fn unpack_xz(_src: &Path, _dst: &Path) {
unimplemented!()
}

/// Build the various test binaries.
fn build_test_bins(crate_root: &Path) {
/// Prepare the various test files.
fn prepare_test_files(crate_root: &Path) {
let src = crate_root.join("data").join("test.c");
cc(&src, "test-no-debug.bin", &["-g0"]);
cc(&src, "test-dwarf-v4.bin", &["-gdwarf-4"]);
Expand All @@ -172,6 +172,11 @@ fn build_test_bins(crate_root: &Path) {
let src = crate_root.join("data").join("test-stable-addresses.bin");
gsym(&src, "test.gsym");
dwarf_mostly(&src, "test-dwarf.bin");

let src = crate_root.join("data").join("kallsyms.xz");
let mut dst = src.clone();
assert!(dst.set_extension(""));
unpack_xz(&src, &dst);
}

/// Prepare benchmark files.
Expand All @@ -195,7 +200,7 @@ fn main() {
let crate_dir = env!("CARGO_MANIFEST_DIR");

if cfg!(feature = "generate-test-files") && !cfg!(feature = "dont-generate-test-files") {
build_test_bins(crate_dir.as_ref());
prepare_test_files(crate_dir.as_ref());
}

if cfg!(feature = "generate-bench-files") {
Expand Down
Binary file added data/kallsyms.xz
Binary file not shown.
7 changes: 5 additions & 2 deletions src/ksym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,12 @@ mod tests {

#[test]
fn ksym_cache() {
let kallsyms = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("kallsyms");
let cache = KSymCache::new();
let resolver = cache.get_resolver(Path::new(KALLSYMS));
let resolver1 = cache.get_resolver(Path::new(KALLSYMS));
let resolver = cache.get_resolver(&kallsyms);
let resolver1 = cache.get_resolver(&kallsyms);
assert!(resolver.is_ok());
assert!(resolver1.is_ok());
}
Expand Down
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ mod tests {

#[test]
fn hello_world_stack() {
// A stack sample from a Hello World proram.
// A stack sample from a Hello World program.
let stack = vec![
0xb0, 0xd5, 0xff, 0xff, 0xff, 0x7f, 0x0, 0x0, 0xaf, 0x5, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0,
0xd0, 0xd5, 0xff, 0xff, 0xff, 0x7f, 0x0, 0x0, 0xcb, 0x5, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0,
Expand Down Expand Up @@ -989,12 +989,16 @@ mod tests {

#[test]
fn load_symbolfilecfg_processkernel() {
let kallsyms = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("kallsyms");

// Check if SymbolSrcCfg::Process & SymbolSrcCfg::Kernel expands to
// ELFResolvers and a KernelResolver.
let srcs = vec![
SymbolSrcCfg::Process { pid: None },
SymbolSrcCfg::Kernel {
kallsyms: None,
kallsyms: Some(kallsyms),
kernel_image: None,
},
];
Expand All @@ -1016,10 +1020,14 @@ mod tests {

#[test]
fn load_symbolfilecfg_invalid_kernel() {
let kallsyms = Path::new(&env!("CARGO_MANIFEST_DIR"))
.join("data")
.join("kallsyms");

// Check if SymbolSrcCfg::Kernel expands to a KernelResolver
// even if kernel_image is invalid.
let srcs = vec![SymbolSrcCfg::Kernel {
kallsyms: None,
kallsyms: Some(kallsyms.clone()),
kernel_image: Some(PathBuf::from("/dev/null")),
}];
let cache_holder = CacheHolder::new(CacheHolderOpts {
Expand All @@ -1033,9 +1041,8 @@ mod tests {
let signatures: Vec<_> = resolver_map.resolvers.iter().map(|x| x.1.repr()).collect();
assert!(signatures.iter().any(|x| x.contains("KernelResolver")));

let kallsyms = Path::new(ksym::KALLSYMS);
let kernel_image = Path::new("/dev/null");
let kresolver = KernelResolver::new(kallsyms, kernel_image, &cache_holder).unwrap();
let kresolver = KernelResolver::new(&kallsyms, kernel_image, &cache_holder).unwrap();
assert!(kresolver.ksymresolver.is_some());
assert!(kresolver.kernelresolver.is_none());
}
Expand Down

0 comments on commit 0900a29

Please sign in to comment.