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.
pull/727/head
Tamir Duberstein 1 year ago
parent 0904cd089e
commit b46fb616be
No known key found for this signature in database

@ -51,44 +51,48 @@ __attribute__((noinline)) int struct_flavors_global() {
if (bpf_core_field_exists(s.a)) { if (bpf_core_field_exists(s.a)) {
return set_output(__builtin_preserve_access_index(s.a)); return set_output(__builtin_preserve_access_index(s.a));
} else { } 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(); } 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() { __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) { SEC("uprobe") int enum_unsigned_32(void *ctx) {
return enum_unsigned_32_global(); 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() { __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(); } 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() { __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) { SEC("uprobe") int enum_unsigned_64(void *ctx) {
return enum_unsigned_64_global(); 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() { __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(); } SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); }

@ -4,9 +4,17 @@
#include <bpf/bpf_core_read.h> #include <bpf/bpf_core_read.h>
// clang-format on // clang-format on
#include <stdlib.h> struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
long set_output(__u64 value) { exit((int)value); } __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 { struct relocated_struct_with_scalars {
__u8 b; __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() { __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() { __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() { __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() { __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. // Avoids dead code elimination by the compiler.

@ -2,20 +2,20 @@ use test_case::test_case;
use aya::{maps::Array, programs::UProbe, util::KernelVersion, BpfLoader, Btf, Endianness}; use aya::{maps::Array, programs::UProbe, util::KernelVersion, BpfLoader, Btf, Endianness};
#[test_case("field", false, None, 2)] #[test_case("enum_signed_32", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7AAAAAAAi32 as u64)]
#[test_case("field", true, None, 1)] #[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", false, None, 0xAAAAAAAA)]
#[test_case("enum_unsigned_32", true, None, 0xBBBBBBBB)] #[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", false, None, 42)]
#[test_case("pointer", true, None, 21)] #[test_case("pointer", true, None, 21)]
#[test_case("struct_flavors", false, None, 1)] #[test_case("struct_flavors", false, None, 1)]
#[test_case("struct_flavors", true, None, 1)] #[test_case("struct_flavors", true, None, 2)]
#[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( fn relocation_tests(
program: &str, program: &str,
with_relocations: bool, with_relocations: bool,

Loading…
Cancel
Save