Added integration tests for ignoring maps by type and by name

pull/968/head
martinsoees 3 months ago
parent aad4ad4fd3
commit b49c3001f1

@ -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"

@ -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<u64> = 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 {}
}

@ -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;

@ -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.

@ -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();

Loading…
Cancel
Save