From adf16e2102d94e46630572c6d18d10893697106c Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Fri, 1 Nov 2024 11:27:55 +0000 Subject: [PATCH] test: Reproduce relocation bug Users have reported issues with programs failing the verifier when they are attempting to read or write to variables that the compiler places in the .bss section. Add a test that places variables in each section and exercises read and write operations on them. Signed-off-by: Dave Tucker --- .../bpf/variables_reloc.bpf.c | 21 +++++++++++++++++++ test/integration-test/build.rs | 1 + test/integration-test/src/lib.rs | 2 ++ .../integration-test/src/tests/relocations.rs | 17 ++++++++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/integration-test/bpf/variables_reloc.bpf.c diff --git a/test/integration-test/bpf/variables_reloc.bpf.c b/test/integration-test/bpf/variables_reloc.bpf.c new file mode 100644 index 00000000..08712937 --- /dev/null +++ b/test/integration-test/bpf/variables_reloc.bpf.c @@ -0,0 +1,21 @@ +// clang-format off +#include +#include +// clang-format on + +volatile unsigned int key1 = 0; // .bss +volatile unsigned int key2 = 1; // .data +volatile const unsigned int key3 = 2; // .rodata + +SEC("xdp") +int variables_reloc(struct xdp_md *ctx) { + if (key1 == 0 && key2 != 1 && key3 != 2) { + key1 += 1; + key2 += 1; + return XDP_DROP; + } else { + return XDP_PASS; + } +} + +char _license[] SEC("license") = "GPL"; diff --git a/test/integration-test/build.rs b/test/integration-test/build.rs index 8e53b5a9..5266a8ea 100644 --- a/test/integration-test/build.rs +++ b/test/integration-test/build.rs @@ -71,6 +71,7 @@ fn main() { ("multimap-btf.bpf.c", false), ("reloc.bpf.c", true), ("text_64_64_reloc.c", false), + ("variables_reloc.bpf.c", false), ]; if build_integration_bpf { diff --git a/test/integration-test/src/lib.rs b/test/integration-test/src/lib.rs index c20926dd..ffed5dd4 100644 --- a/test/integration-test/src/lib.rs +++ b/test/integration-test/src/lib.rs @@ -9,6 +9,8 @@ pub const RELOC_BTF: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/reloc.bpf.target.o")); pub const TEXT_64_64_RELOC: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/text_64_64_reloc.o")); +pub const VARIABLES_RELOC: &[u8] = + include_bytes_aligned!(concat!(env!("OUT_DIR"), "/variables_reloc.bpf.o")); pub const LOG: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/log")); pub const MAP_TEST: &[u8] = include_bytes_aligned!(concat!(env!("OUT_DIR"), "/map_test")); diff --git a/test/integration-test/src/tests/relocations.rs b/test/integration-test/src/tests/relocations.rs index eb43466a..2dce6a60 100644 --- a/test/integration-test/src/tests/relocations.rs +++ b/test/integration-test/src/tests/relocations.rs @@ -1,4 +1,8 @@ -use aya::{programs::UProbe, util::KernelVersion, Ebpf}; +use aya::{ + programs::{UProbe, Xdp}, + util::KernelVersion, + Ebpf, +}; use test_log::test; #[test] @@ -33,6 +37,17 @@ fn text_64_64_reloc() { assert_eq!(m.get(&1, 0).unwrap(), 3); } +#[test] +fn variables_reloc() { + let mut bpf = Ebpf::load(crate::VARIABLES_RELOC).unwrap(); + let prog: &mut Xdp = bpf + .program_mut("variables_reloc") + .unwrap() + .try_into() + .unwrap(); + prog.load().unwrap(); +} + fn load_and_attach(name: &str, bytes: &[u8]) -> Ebpf { let mut bpf = Ebpf::load(bytes).unwrap();