From b46fb616beb205cd5686d06dbf3238cf4b4a5c94 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 8 Aug 2023 12:00:36 -0400 Subject: [PATCH] integration-test: fix BTF relocation test The struct_flavors test previously expected the same thing with and without relocations. It now expects different values. Also rename an enum variant "u64" to "S64". This was a typo. Turns out that U32 is a type that exists in kernel headers, so all enum values are suffixed with "_VAL". Remove stdlib.h and the call to exit(). This alone makes the test fail with a poisoned relocation. Bringing over the map definition makes the test work again. --- test/integration-test/bpf/reloc.bpf.c | 22 +++++++----- test/integration-test/bpf/reloc.btf.c | 34 +++++++++++++------ .../src/tests/btf_relocations.rs | 18 +++++----- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/test/integration-test/bpf/reloc.bpf.c b/test/integration-test/bpf/reloc.bpf.c index dbc464a4..f426874f 100644 --- a/test/integration-test/bpf/reloc.bpf.c +++ b/test/integration-test/bpf/reloc.bpf.c @@ -51,44 +51,48 @@ __attribute__((noinline)) int struct_flavors_global() { if (bpf_core_field_exists(s.a)) { return set_output(__builtin_preserve_access_index(s.a)); } else { - return set_output(__builtin_preserve_access_index(s.b)); + return set_output(__builtin_preserve_access_index(s.c)); } } SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); } -enum relocated_enum_unsigned_32 { U32 = 0xAAAAAAAA }; +enum relocated_enum_unsigned_32 { U32_VAL = 0xAAAAAAAA }; __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, U32_VAL)); } 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 { S32_VAL = -0x7AAAAAAA }; __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, S32_VAL)); } 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 { U64_VAL = 0xAAAAAAAABBBBBBBB }; __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, U64_VAL)); } 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 { S64_VAL = -0xAAAAAAABBBBBBBB }; __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, S64_VAL)); } 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..ec396552 100644 --- a/test/integration-test/bpf/reloc.btf.c +++ b/test/integration-test/bpf/reloc.btf.c @@ -4,9 +4,17 @@ #include // clang-format on -#include - -long set_output(__u64 value) { exit((int)value); } +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, __u32); + __type(value, __u64); + __uint(max_entries, 1); +} output_map SEC(".maps"); + +long set_output(__u64 value) { + __u32 key = 0; + return bpf_map_update_elem(&output_map, &key, &value, BPF_ANY); +} struct relocated_struct_with_scalars { __u8 b; @@ -41,28 +49,32 @@ __attribute__((noinline)) int struct_flavors_global() { } } -enum relocated_enum_unsigned_32 { U32 = 0xBBBBBBBB }; +enum relocated_enum_unsigned_32 { U32_VAL = 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, U32_VAL)); } -enum relocated_enum_signed_32 { S32 = -0x7BBBBBBB }; +enum relocated_enum_signed_32 { S32_VAL = -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, S32_VAL)); } -enum relocated_enum_unsigned_64 { U64 = 0xCCCCCCCCDDDDDDDD }; +enum relocated_enum_unsigned_64 { U64_VAL = 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, U64_VAL)); } -enum relocated_enum_signed_64 { u64 = -0xCCCCCCCDDDDDDDD }; +enum relocated_enum_signed_64 { S64_VAL = -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, S64_VAL)); } // Avoids dead code elimination by the compiler. diff --git a/test/integration-test/src/tests/btf_relocations.rs b/test/integration-test/src/tests/btf_relocations.rs index 7e1020ba..79eb3e52 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)] +#[test_case("struct_flavors", true, None, 2)] fn relocation_tests( program: &str, with_relocations: bool,