You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aya/test/integration-test/src/tests/btf_relocations.rs

90 lines
5.0 KiB
Rust

use aya::{Btf, EbpfLoader, Endianness, maps::Array, programs::UProbe, util::KernelVersion};
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_32", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7AAAAAAAi32 as u64))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_32", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7BBBBBBBi32 as u64))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_32_checked_variants", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7AAAAAAAi32 as u64))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_32_checked_variants", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7BBBBBBBi32 as u64))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xAAAAAAABBBBBBBBi64 as u64))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xCCCCCCCDDDDDDDDi64 as u64))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_64_checked_variants", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xAAAAAAABBBBBBBi64 as u64))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_signed_64_checked_variants", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xCCCCCCCDDDDDDDi64 as u64))]
#[cfg_attr(
aya_integration_test,
test_case::test_case("enum_unsigned_32", false, None, 0xAAAAAAAA)
)]
#[cfg_attr(
aya_integration_test,
test_case::test_case("enum_unsigned_32", true, None, 0xBBBBBBBB)
)]
#[cfg_attr(
aya_integration_test,
test_case::test_case("enum_unsigned_32_checked_variants", false, None, 0xAAAAAAAA)
)]
#[cfg_attr(
aya_integration_test,
test_case::test_case("enum_unsigned_32_checked_variants", true, None, 0xBBBBBBBB)
)]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_unsigned_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xAAAAAAAABBBBBBBB))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_unsigned_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xCCCCCCCCDDDDDDDD))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_unsigned_64_checked_variants", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xAAAAAAAABBBBBBBB))]
#[cfg_attr(aya_integration_test, test_case::test_case("enum_unsigned_64_checked_variants", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xCCCCCCCCDDDDDDDD))]
#[cfg_attr(aya_integration_test, test_case::test_case("field", false, None, 2))]
#[cfg_attr(aya_integration_test, test_case::test_case("field", true, None, 1))]
#[cfg_attr(aya_integration_test, test_case::test_case("pointer", false, None, 42))]
#[cfg_attr(aya_integration_test, test_case::test_case("pointer", true, None, 21))]
#[cfg_attr(
aya_integration_test,
test_case::test_case("struct_flavors", false, None, 1)
)]
#[cfg_attr(
aya_integration_test,
test_case::test_case("struct_flavors", true, None, 2)
)]
#[cfg_attr(not(aya_integration_test), allow(dead_code))]
fn relocation_tests(
program: &str,
with_relocations: bool,
required_kernel_version: Option<(KernelVersion, &str)>,
expected: u64,
) {
if let Some((required_kernel_version, commit)) = required_kernel_version {
let current_kernel_version = KernelVersion::current().unwrap();
if current_kernel_version < required_kernel_version {
eprintln!(
"skipping test on kernel {current_kernel_version:?}, support for {program} was added in {required_kernel_version:?}; see {commit}"
);
return;
}
}
let mut bpf = EbpfLoader::new()
.btf(
with_relocations
.then(|| Btf::parse(crate::RELOC_BTF, Endianness::default()).unwrap())
.as_ref(),
)
.load(crate::RELOC_BPF)
.unwrap();
let program: &mut UProbe = bpf.program_mut(program).unwrap().try_into().unwrap();
program.load().unwrap();
program
.attach(
"trigger_btf_relocations_program",
"/proc/self/exe",
None,
None,
)
.unwrap();
trigger_btf_relocations_program();
let output_map: Array<_, u64> = bpf.take_map("output_map").unwrap().try_into().unwrap();
let key = 0;
assert_eq!(output_map.get(&key, 0).unwrap(), expected)
}
#[unsafe(no_mangle)]
#[inline(never)]
pub extern "C" fn trigger_btf_relocations_program() {
core::hint::black_box(trigger_btf_relocations_program);
}