Skip to content

Commit 405c6a8

Browse files
authored
Merge pull request aya-rs#485 from MatteoNardi/remove_libbpf_dependency
Remove libbpf dependency from relocation tests
2 parents e2b3be6 + 3000949 commit 405c6a8

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

test/integration-test/src/tests/relocations.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ fn relocate_field() {
2727
"#,
2828
relocation_code: r#"
2929
__u8 memory[] = {1, 2, 3, 4};
30-
__u32 value = BPF_CORE_READ((struct foo *)&memory, c);
30+
struct foo *ptr = (struct foo *) &memory;
31+
bpf_probe_read_kernel(&value,
32+
sizeof(__u8),
33+
__builtin_preserve_access_index(&ptr->c));
3134
"#,
3235
}
3336
.build()
@@ -46,7 +49,8 @@ fn relocate_enum() {
4649
enum foo { D = 4 } e1;
4750
"#,
4851
relocation_code: r#"
49-
__u32 value = bpf_core_enum_value(enum foo, D);
52+
#define BPF_ENUMVAL_VALUE 1
53+
value = __builtin_preserve_enum_value(*(typeof(enum foo) *)D, BPF_ENUMVAL_VALUE);
5054
"#,
5155
}
5256
.build()
@@ -68,8 +72,10 @@ fn relocate_pointer() {
6872
"#,
6973
relocation_code: r#"
7074
__u8 memory[] = {42, 0, 0, 0, 0, 0, 0, 0};
71-
struct foo *f = BPF_CORE_READ((struct bar *)&memory, f);
72-
__u32 value = ((__u64) f);
75+
struct bar* ptr = (struct bar *) &memory;
76+
bpf_probe_read_kernel(&value,
77+
sizeof(void *),
78+
__builtin_preserve_access_index(&ptr->f));
7379
"#,
7480
}
7581
.build()
@@ -114,27 +120,29 @@ impl RelocationTest {
114120
r#"
115121
#include <linux/bpf.h>
116122
117-
#include <bpf/bpf_core_read.h>
118-
#include <bpf/bpf_helpers.h>
119-
#include <bpf/bpf_tracing.h>
123+
static long (*bpf_map_update_elem)(void *map, const void *key, const void *value, __u64 flags) = (void *) 2;
124+
static long (*bpf_probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 113;
120125
121126
{local_definition}
122127
123128
struct {{
124-
__uint(type, BPF_MAP_TYPE_ARRAY);
125-
__type(key, __u32);
126-
__type(value, __u32);
127-
__uint(max_entries, 1);
128-
}} output_map SEC(".maps");
129+
int (*type)[BPF_MAP_TYPE_ARRAY];
130+
__u32 *key;
131+
__u32 *value;
132+
int (*max_entries)[1];
133+
}} output_map
134+
__attribute__((section(".maps"), used));
129135
130-
SEC("tracepoint/bpf_prog") int bpf_prog(void *ctx) {{
136+
__attribute__((section("tracepoint/bpf_prog"), used))
137+
int bpf_prog(void *ctx) {{
131138
__u32 key = 0;
139+
__u32 value = 0;
132140
{relocation_code}
133141
bpf_map_update_elem(&output_map, &key, &value, BPF_ANY);
134142
return 0;
135143
}}
136144
137-
char _license[] SEC("license") = "GPL";
145+
char _license[] __attribute__((section("license"), used)) = "GPL";
138146
"#
139147
))
140148
.context("Failed to compile eBPF program")?;
@@ -157,12 +165,11 @@ impl RelocationTest {
157165
r#"
158166
#include <linux/bpf.h>
159167
160-
#include <bpf/bpf_core_read.h>
161-
#include <bpf/bpf_helpers.h>
162-
#include <bpf/bpf_tracing.h>
168+
static long (*bpf_probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 113;
163169
164170
{target_btf}
165171
int main() {{
172+
__u32 value = 0;
166173
// This is needed to make sure to emit BTF for the defined types,
167174
// it could be dead code eliminated if we don't.
168175
{relocation_code};
@@ -195,12 +202,6 @@ fn compile(source_code: &str) -> Result<(TempDir, PathBuf)> {
195202
Command::new("clang")
196203
.current_dir(&tmp_dir)
197204
.args(["-c", "-g", "-O2", "-target", "bpf"])
198-
// NOTE: these tests depend on libbpf, LIBBPF_INCLUDE must point its headers.
199-
// This is set automatically by the integration-test xtask.
200-
.args([
201-
"-I",
202-
&std::env::var("LIBBPF_INCLUDE").context("LIBBPF_INCLUDE not set")?,
203-
])
204205
.arg(&source)
205206
.status()
206207
.context("Failed to run clang")?

test/run.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,5 @@ cargo xtask build-integration-test --musl --libbpf-dir "$1"
187187
scp_vm ../target/x86_64-unknown-linux-musl/debug/integration-test
188188
exec_vm sudo ./integration-test --skip relocations
189189

190-
# Relocation tests build the eBPF programs and require libbpf. We run them outside VM.
191-
export LIBBPF_INCLUDE="${AYA_TMPDIR}/libbpf/"
192-
mkdir -p "$LIBBPF_INCLUDE"
193-
(cd "$1/src" && make INCLUDEDIR="$LIBBPF_INCLUDE" install_headers)
190+
# Relocation tests build the eBPF programs themself. We run them outside VM.
194191
sudo -E ../target/x86_64-unknown-linux-musl/debug/integration-test relocations

xtask/src/build_ebpf.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,6 @@ fn build_c_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> {
103103

104104
let include_path = out_path.join("include");
105105
get_libbpf_headers(&opts.libbpf_dir, &include_path)?;
106-
// Export libbpf location as an env variable since it's needed for building
107-
// the relocation tests at test/integration-test/src/tests/relocations.rs
108-
// We decided to make an exception and build its eBPF programs at run-time
109-
// because of the many different permutations.
110-
std::env::set_var("LIBBPF_INCLUDE", &include_path);
111-
112106
let files = fs::read_dir(&src).unwrap();
113107
for file in files {
114108
let p = file.unwrap().path();

0 commit comments

Comments
 (0)