From 041aac4f58b07ba10aa693c4d8fa9610f41ff5f0 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 7 Aug 2023 13:30:09 -0400 Subject: [PATCH] integration-test: avoid reliance on kernel headers This should allow us to build on any system. --- test/integration-test/Cargo.toml | 4 +-- test/integration-test/bpf/ext.bpf.c | 9 +++-- test/integration-test/bpf/main.bpf.c | 9 +++-- test/integration-test/bpf/multimap-btf.bpf.c | 36 +++++++++---------- test/integration-test/bpf/reloc.bpf.c | 20 ++++++----- test/integration-test/bpf/reloc.btf.c | 24 ++++++------- test/integration-test/bpf/text_64_64_reloc.c | 31 ++++++++-------- test/integration-test/build.rs | 35 +++++++++--------- .../src/tests/btf_relocations.rs | 16 ++++----- xtask/Cargo.toml | 4 +-- 10 files changed, 92 insertions(+), 96 deletions(-) diff --git a/test/integration-test/Cargo.toml b/test/integration-test/Cargo.toml index 876a9a86..c354a6c6 100644 --- a/test/integration-test/Cargo.toml +++ b/test/integration-test/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" publish = false [dependencies] -anyhow = { workspace = true, default-features = true } +anyhow = { workspace = true, features = ["std"] } assert_matches = { workspace = true } aya = { workspace = true } aya-log = { workspace = true } @@ -16,7 +16,7 @@ netns-rs = { workspace = true } object = { workspace = true } rbpf = { workspace = true } test-case = { workspace = true } -tokio = { workspace = true, default-features = false, features = [ +tokio = { workspace = true, features = [ "macros", "time", ] } diff --git a/test/integration-test/bpf/ext.bpf.c b/test/integration-test/bpf/ext.bpf.c index dc82f5cf..2996fcf7 100644 --- a/test/integration-test/bpf/ext.bpf.c +++ b/test/integration-test/bpf/ext.bpf.c @@ -1,10 +1,9 @@ -#include +// clang-format off +#include #include +// clang-format on SEC("xdp") -int xdp_drop(struct xdp_md *ctx) -{ - return XDP_DROP; -} +int xdp_drop(struct xdp_md *ctx) { return XDP_DROP; } char _license[] SEC("license") = "GPL"; diff --git a/test/integration-test/bpf/main.bpf.c b/test/integration-test/bpf/main.bpf.c index 2a30577f..b9365622 100644 --- a/test/integration-test/bpf/main.bpf.c +++ b/test/integration-test/bpf/main.bpf.c @@ -1,10 +1,9 @@ -#include +// clang-format off +#include #include +// clang-format on SEC("xdp") -int xdp_pass(struct xdp_md *ctx) -{ - return XDP_PASS; -} +int xdp_pass(struct xdp_md *ctx) { return XDP_PASS; } char _license[] SEC("license") = "GPL"; diff --git a/test/integration-test/bpf/multimap-btf.bpf.c b/test/integration-test/bpf/multimap-btf.bpf.c index 955a91d3..2bd7978b 100644 --- a/test/integration-test/bpf/multimap-btf.bpf.c +++ b/test/integration-test/bpf/multimap-btf.bpf.c @@ -1,30 +1,30 @@ -#include +// clang-format off +#include #include +// clang-format on struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __type(key, __u32); - __type(value, __u64); - __uint(max_entries, 1); + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, __u32); + __type(value, __u64); + __uint(max_entries, 1); } map_1 SEC(".maps"); struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __type(key, __u32); - __type(value, __u64); - __uint(max_entries, 1); + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, __u32); + __type(value, __u64); + __uint(max_entries, 1); } map_2 SEC(".maps"); - SEC("tracepoint") -int bpf_prog(void *ctx) -{ - __u32 key = 0; - __u64 twenty_four = 24; - __u64 forty_two = 42; - bpf_map_update_elem(&map_1, &key, &twenty_four, BPF_ANY); - bpf_map_update_elem(&map_2, &key, &forty_two, BPF_ANY); - return 0; +int bpf_prog(void *ctx) { + __u32 key = 0; + __u64 twenty_four = 24; + __u64 forty_two = 42; + bpf_map_update_elem(&map_1, &key, &twenty_four, BPF_ANY); + bpf_map_update_elem(&map_2, &key, &forty_two, BPF_ANY); + return 0; } char _license[] SEC("license") = "GPL"; diff --git a/test/integration-test/bpf/reloc.bpf.c b/test/integration-test/bpf/reloc.bpf.c index ca8b18b6..09500041 100644 --- a/test/integration-test/bpf/reloc.bpf.c +++ b/test/integration-test/bpf/reloc.bpf.c @@ -1,5 +1,5 @@ // clang-format off -#include +#include #include #include // clang-format on @@ -57,38 +57,40 @@ __noinline int struct_flavors_global() { SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); } -enum relocated_enum_unsigned_32 { U32 = 0xAAAAAAAA }; +enum relocated_enum_unsigned_32 { MyU32 = 0xAAAAAAAA }; __noinline int enum_unsigned_32_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_32, U32)); + return set_output( + bpf_core_enum_value(enum relocated_enum_unsigned_32, MyU32)); } SEC("uprobe") int enum_unsigned_32(void *ctx) { return enum_unsigned_32_global(); } -enum relocated_enum_signed_32 { S32 = -0x7AAAAAAA }; +enum relocated_enum_signed_32 { MyS32 = -0x7AAAAAAA }; __noinline int enum_signed_32_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, S32)); + return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, MyS32)); } SEC("uprobe") int enum_signed_32(void *ctx) { return enum_signed_32_global(); } -enum relocated_enum_unsigned_64 { U64 = 0xAAAAAAAABBBBBBBB }; +enum relocated_enum_unsigned_64 { MyU64 = 0xAAAAAAAABBBBBBBB }; __noinline int enum_unsigned_64_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_64, U64)); + return set_output( + bpf_core_enum_value(enum relocated_enum_unsigned_64, MyU64)); } SEC("uprobe") int enum_unsigned_64(void *ctx) { return enum_unsigned_64_global(); } -enum relocated_enum_signed_64 { u64 = -0xAAAAAAABBBBBBBB }; +enum relocated_enum_signed_64 { MyS64 = -0xAAAAAAABBBBBBBB }; __noinline int enum_signed_64_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, u64)); + return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, MyS64)); } SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); } diff --git a/test/integration-test/bpf/reloc.btf.c b/test/integration-test/bpf/reloc.btf.c index 86801061..e1ba3601 100644 --- a/test/integration-test/bpf/reloc.btf.c +++ b/test/integration-test/bpf/reloc.btf.c @@ -1,12 +1,10 @@ // clang-format off -#include +#include #include #include // clang-format on -#include - -long set_output(__u64 value) { exit((int)value); } +long set_output(__u64 value) { return 0; } struct relocated_struct_with_scalars { __u8 b; @@ -41,28 +39,30 @@ __attribute__((noinline)) int struct_flavors_global() { } } -enum relocated_enum_unsigned_32 { U32 = 0xBBBBBBBB }; +enum relocated_enum_unsigned_32 { MyU32 = 0xBBBBBBBB }; __attribute__((noinline)) int enum_unsigned_32_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_32, U32)); + return set_output( + bpf_core_enum_value(enum relocated_enum_unsigned_32, MyU32)); } -enum relocated_enum_signed_32 { S32 = -0x7BBBBBBB }; +enum relocated_enum_signed_32 { MyS32 = -0x7BBBBBBB }; __attribute__((noinline)) int enum_signed_32_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, S32)); + return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, MyS32)); } -enum relocated_enum_unsigned_64 { U64 = 0xCCCCCCCCDDDDDDDD }; +enum relocated_enum_unsigned_64 { MyU64 = 0xCCCCCCCCDDDDDDDD }; __attribute__((noinline)) int enum_unsigned_64_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_64, U64)); + return set_output( + bpf_core_enum_value(enum relocated_enum_unsigned_64, MyU64)); } -enum relocated_enum_signed_64 { u64 = -0xCCCCCCCDDDDDDDD }; +enum relocated_enum_signed_64 { MyS64 = -0xCCCCCCCDDDDDDDD }; __attribute__((noinline)) int enum_signed_64_global() { - return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, u64)); + return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, MyS64)); } // Avoids dead code elimination by the compiler. diff --git a/test/integration-test/bpf/text_64_64_reloc.c b/test/integration-test/bpf/text_64_64_reloc.c index 877c7628..067350cc 100644 --- a/test/integration-test/bpf/text_64_64_reloc.c +++ b/test/integration-test/bpf/text_64_64_reloc.c @@ -1,28 +1,25 @@ -#include +// clang-format off +#include #include +// clang-format on char _license[] SEC("license") = "GPL"; struct { - __uint(type, BPF_MAP_TYPE_ARRAY); - __type(key, __u32); - __type(value, __u64); - __uint(max_entries, 2); + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, __u32); + __type(value, __u64); + __uint(max_entries, 2); } RESULTS SEC(".maps"); -static __u64 -inc_cb(void *map, __u32 *key, void *val, - void *data) -{ - __u64 *value = val; - *value += 1; - return 0; +static __u64 inc_cb(void *map, __u32 *key, void *val, void *data) { + __u64 *value = val; + *value += 1; + return 0; } SEC("uprobe/test_text_64_64_reloc") -int test_text_64_64_reloc(struct pt_regs *ctx) -{ - bpf_for_each_map_elem(&RESULTS, inc_cb, NULL, 0); - return 0; +int test_text_64_64_reloc(struct pt_regs *ctx) { + bpf_for_each_map_elem(&RESULTS, inc_cb, NULL, 0); + return 0; } - diff --git a/test/integration-test/build.rs b/test/integration-test/build.rs index cd816164..a78b3836 100644 --- a/test/integration-test/build.rs +++ b/test/integration-test/build.rs @@ -116,34 +116,33 @@ fn main() { target_arch.push(arch); }; + let libbpf_vmlinux_dir = libbpf_dir.join(".github/actions/build-selftests"); + + let clang = || { + let mut cmd = Command::new("clang"); + cmd.arg("-nostdlibinc") + .arg("-I") + .arg(&libbpf_headers_dir) + .arg("-I") + .arg(&libbpf_vmlinux_dir) + .args(["-g", "-O2", "-target", target, "-c"]) + .arg(&target_arch); + cmd + }; + for (src, dst) in c_bpf { let src = bpf_dir.join(src); println!("cargo:rerun-if-changed={}", src.to_str().unwrap()); - exec( - Command::new("clang") - .arg("-I") - .arg(&libbpf_headers_dir) - .args(["-g", "-O2", "-target", target, "-c"]) - .arg(&target_arch) - .arg(src) - .arg("-o") - .arg(dst), - ) - .unwrap(); + exec(clang().arg(src).arg("-o").arg(dst)).unwrap(); } for (src, dst) in c_btf { let src = bpf_dir.join(src); println!("cargo:rerun-if-changed={}", src.to_str().unwrap()); - let mut cmd = Command::new("clang"); - cmd.arg("-I") - .arg(&libbpf_headers_dir) - .args(["-g", "-target", target, "-c"]) - .arg(&target_arch) - .arg(src) - .args(["-o", "-"]); + let mut cmd = clang(); + cmd.arg(src).args(["-o", "-"]); let mut child = cmd .stdout(Stdio::piped()) diff --git a/test/integration-test/src/tests/btf_relocations.rs b/test/integration-test/src/tests/btf_relocations.rs index 7e1020ba..9f1eca37 100644 --- a/test/integration-test/src/tests/btf_relocations.rs +++ b/test/integration-test/src/tests/btf_relocations.rs @@ -2,20 +2,20 @@ use test_case::test_case; use aya::{maps::Array, programs::UProbe, util::KernelVersion, BpfLoader, Btf, Endianness}; -#[test_case("field", false, None, 2)] -#[test_case("field", true, None, 1)] +#[test_case("enum_signed_32", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7AAAAAAAi32 as u64)] +#[test_case("enum_signed_32", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7BBBBBBBi32 as u64)] +#[test_case("enum_signed_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xAAAAAAABBBBBBBBi64 as u64)] +#[test_case("enum_signed_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xCCCCCCCDDDDDDDDi64 as u64)] #[test_case("enum_unsigned_32", false, None, 0xAAAAAAAA)] #[test_case("enum_unsigned_32", true, None, 0xBBBBBBBB)] +#[test_case("enum_unsigned_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xAAAAAAAABBBBBBBB)] +#[test_case("enum_unsigned_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xCCCCCCCCDDDDDDDD)] +#[test_case("field", false, None, 2)] +#[test_case("field", true, None, 1)] #[test_case("pointer", false, None, 42)] #[test_case("pointer", true, None, 21)] #[test_case("struct_flavors", false, None, 1)] #[test_case("struct_flavors", true, None, 1)] -#[test_case("enum_signed_32", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7AAAAAAAi32 as u64)] -#[test_case("enum_signed_32", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7BBBBBBBi32 as u64)] -#[test_case("enum_unsigned_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xAAAAAAAABBBBBBBB)] -#[test_case("enum_unsigned_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xCCCCCCCCDDDDDDDD)] -#[test_case("enum_signed_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xAAAAAAABBBBBBBBi64 as u64)] -#[test_case("enum_signed_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xCCCCCCCDDDDDDDDi64 as u64)] fn relocation_tests( program: &str, with_relocations: bool, diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 0349e022..1a2ae931 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -6,10 +6,10 @@ edition = "2021" publish = false [dependencies] -anyhow = { workspace = true, default-features = true } +anyhow = { workspace = true, features = ["std"] } aya-tool = { workspace = true } cargo_metadata = { workspace = true } -clap = { workspace = true, default-features = true, features = ["derive"] } +clap = { workspace = true, features = ["derive"] } dialoguer = { workspace = true } diff = { workspace = true } indoc = { workspace = true }