From b49c3001f19b396e1522fd147782f92ecca961c6 Mon Sep 17 00:00:00 2001 From: martinsoees Date: Wed, 19 Jun 2024 07:01:08 -0400 Subject: [PATCH] Added integration tests for ignoring maps by type and by name --- test/integration-ebpf/Cargo.toml | 4 ++ test/integration-ebpf/src/ignore_map.rs | 32 +++++++++ test/integration-test/src/lib.rs | 1 + test/integration-test/src/tests/rbpf.rs | 4 +- .../integration-test/src/tests/relocations.rs | 72 ++++++++++++++++++- 5 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 test/integration-ebpf/src/ignore_map.rs diff --git a/test/integration-ebpf/Cargo.toml b/test/integration-ebpf/Cargo.toml index b4146daa..ec900402 100644 --- a/test/integration-ebpf/Cargo.toml +++ b/test/integration-ebpf/Cargo.toml @@ -59,3 +59,7 @@ path = "src/xdp_sec.rs" [[bin]] name = "ring_buf" path = "src/ring_buf.rs" + +[[bin]] +name = "ignore_map" +path = "src/ignore_map.rs" \ No newline at end of file diff --git a/test/integration-ebpf/src/ignore_map.rs b/test/integration-ebpf/src/ignore_map.rs new file mode 100644 index 00000000..4255d1c9 --- /dev/null +++ b/test/integration-ebpf/src/ignore_map.rs @@ -0,0 +1,32 @@ +#![no_std] +#![no_main] + +use aya_ebpf::{ + macros::{map, uprobe}, + maps::{PerfEventArray, RingBuf}, + programs::ProbeContext, +}; + +#[no_mangle] +pub static RINGBUF_SUPPORTED: i32 = 0; + +#[map] +static mut RINGBUF: RingBuf = RingBuf::with_byte_size(0, 0); + +#[map] +static mut PERFBUF: PerfEventArray = PerfEventArray::with_max_entries(1, 0); + +#[uprobe] +pub fn test_ignored_map_relocation(ctx: ProbeContext) { + if unsafe { core::ptr::read_volatile(&RINGBUF_SUPPORTED) == 1 } { + let _ = unsafe { RINGBUF.output(&1, 0).map_err(|_| 1u32) }; + } else { + unsafe { PERFBUF.output(&ctx, &1, 0) }; + } +} + +#[cfg(not(test))] +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} diff --git a/test/integration-test/src/lib.rs b/test/integration-test/src/lib.rs index d4708033..a66afed1 100644 --- a/test/integration-test/src/lib.rs +++ b/test/integration-test/src/lib.rs @@ -22,6 +22,7 @@ pub const BPF_PROBE_READ: &[u8] = pub const REDIRECT: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/redirect")); pub const XDP_SEC: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/xdp_sec")); pub const RING_BUF: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/ring_buf")); +pub const IGNORE_MAP: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/ignore_map")); #[cfg(test)] mod tests; diff --git a/test/integration-test/src/tests/rbpf.rs b/test/integration-test/src/tests/rbpf.rs index b59c3dac..8a90697d 100644 --- a/test/integration-test/src/tests/rbpf.rs +++ b/test/integration-test/src/tests/rbpf.rs @@ -81,13 +81,13 @@ fn use_map_with_rbpf() { .iter() .map(|((section_index, _), _)| *section_index) .collect(); - let ignored_maps = HashMap::new(); + let disable_maps = HashMap::new(); object .relocate_maps( maps.iter() .map(|(s, (fd, map))| (s.as_ref() as &str, *fd, map)), &text_sections, - &ignored_maps, + &disable_maps, ) .expect("Relocation failed"); // Actually there is no local function call involved. diff --git a/test/integration-test/src/tests/relocations.rs b/test/integration-test/src/tests/relocations.rs index eb43466a..734cc5ba 100644 --- a/test/integration-test/src/tests/relocations.rs +++ b/test/integration-test/src/tests/relocations.rs @@ -1,6 +1,42 @@ -use aya::{programs::UProbe, util::KernelVersion, Ebpf}; +use std::collections::HashSet; + +use aya::{programs::UProbe, util::KernelVersion, Ebpf, EbpfLoader}; +use aya_obj::generated::bpf_map_type; use test_log::test; +enum DisableMapRelocation<'a> { + ByType(bpf_map_type), + ByName(&'a str), +} + +#[test] +fn test_ignored_map_relocation_by_type() { + let mut ebpf = relocation_load_and_attach("test_ignored_map_relocation", crate::IGNORE_MAP, DisableMapRelocation::ByType(bpf_map_type::BPF_MAP_TYPE_RINGBUF)); + + let perf = ebpf + .take_map("PERFBUF"); + + let ring = ebpf + .take_map("RINGBUF"); + + assert!(perf.is_some()); + assert!(ring.is_none()); +} + +#[test] +fn test_ignored_map_relocation_by_name() { + let mut ebpf = relocation_load_and_attach("test_ignored_map_relocation", crate::IGNORE_MAP, DisableMapRelocation::ByName("RINGBUF")); + + let perf = ebpf + .take_map("PERFBUF"); + + let ring = ebpf + .take_map("RINGBUF"); + + assert!(perf.is_some()); + assert!(ring.is_none()); +} + #[test] fn relocations() { let bpf = load_and_attach("test_64_32_call_relocs", crate::RELOCATIONS); @@ -33,6 +69,40 @@ fn text_64_64_reloc() { assert_eq!(m.get(&1, 0).unwrap(), 3); } +fn relocation_load_and_attach(name: &str, bytes: &[u8], disable_type: DisableMapRelocation) -> Ebpf { + let mut ebpf = match disable_type { + DisableMapRelocation::ByType(bmt) => { + let mut set = HashSet::new(); + set.insert(bmt); + EbpfLoader::new() + .ignore_maps_by_type(set) + .set_global("RINGBUF_SUPPORTED", &0, true) + .load(bytes) + .unwrap() + } + DisableMapRelocation::ByName(name) => { + EbpfLoader::new() + .ignore_maps_by_name(&[name]) + .set_global("RINGBUF_SUPPORTED", &0, true) + .load(bytes) + .unwrap() + } + }; + + let prog: &mut UProbe = ebpf.program_mut(name).unwrap().try_into().unwrap(); + prog.load().unwrap(); + + prog.attach( + Some("trigger_relocations_program"), + 0, + "/proc/self/exe", + None, + ) + .unwrap(); + + ebpf +} + fn load_and_attach(name: &str, bytes: &[u8]) -> Ebpf { let mut bpf = Ebpf::load(bytes).unwrap();