integration-test: avoid reliance on kernel headers

This should allow us to build on any system.
reviewable/pr727/r1
Tamir Duberstein 2 years ago
parent f56effedd3
commit 041aac4f58

@ -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",
] }

@ -1,10 +1,9 @@
#include <linux/bpf.h>
// clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
// 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";

@ -1,10 +1,9 @@
#include <linux/bpf.h>
// clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
// 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";

@ -1,5 +1,7 @@
#include <linux/bpf.h>
// clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
// clang-format on
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
@ -15,10 +17,8 @@ struct {
__uint(max_entries, 1);
} map_2 SEC(".maps");
SEC("tracepoint")
int bpf_prog(void *ctx)
{
int bpf_prog(void *ctx) {
__u32 key = 0;
__u64 twenty_four = 24;
__u64 forty_two = 42;

@ -1,5 +1,5 @@
// clang-format off
#include <linux/bpf.h>
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
// 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(); }

@ -1,12 +1,10 @@
// clang-format off
#include <linux/bpf.h>
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
// clang-format on
#include <stdlib.h>
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.

@ -1,5 +1,7 @@
#include <linux/bpf.h>
// clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
// clang-format on
char _license[] SEC("license") = "GPL";
@ -10,19 +12,14 @@ struct {
__uint(max_entries, 2);
} RESULTS SEC(".maps");
static __u64
inc_cb(void *map, __u32 *key, void *val,
void *data)
{
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)
{
int test_text_64_64_reloc(struct pt_regs *ctx) {
bpf_for_each_map_elem(&RESULTS, inc_cb, NULL, 0);
return 0;
}

@ -116,34 +116,33 @@ fn main() {
target_arch.push(arch);
};
for (src, dst) in c_bpf {
let src = bpf_dir.join(src);
println!("cargo:rerun-if-changed={}", src.to_str().unwrap());
let libbpf_vmlinux_dir = libbpf_dir.join(".github/actions/build-selftests");
exec(
Command::new("clang")
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)
.arg(src)
.arg("-o")
.arg(dst),
)
.unwrap();
.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(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())

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

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

Loading…
Cancel
Save