Skip to content

Commit 704c91e

Browse files
mauriciovasquezbernalanakryiko
authored andcommitted
selftests/bpf: Test "bpftool gen min_core_btf"
This commit reuses the core_reloc test to check if the BTF files generated with "bpftool gen min_core_btf" are correct. This introduces test_core_btfgen() that runs all the core_reloc tests, but this time the source BTF files are generated by using "bpftool gen min_core_btf". The goal of this test is to check that the generated files are usable, and not to check if the algorithm is creating an optimized BTF file. Signed-off-by: Mauricio Vásquez <[email protected]> Signed-off-by: Rafael David Tinoco <[email protected]> Signed-off-by: Lorenzo Fontana <[email protected]> Signed-off-by: Leonardo Di Donato <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 1d1ffbf commit 704c91e

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

tools/testing/selftests/bpf/prog_tests/core_reloc.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <test_progs.h>
33
#include "progs/core_reloc_types.h"
44
#include "bpf_testmod/bpf_testmod.h"
5+
#include <linux/limits.h>
56
#include <sys/mman.h>
67
#include <sys/syscall.h>
78
#include <bpf/btf.h>
@@ -836,13 +837,27 @@ static size_t roundup_page(size_t sz)
836837
return (sz + page_size - 1) / page_size * page_size;
837838
}
838839

839-
void test_core_reloc(void)
840+
static int run_btfgen(const char *src_btf, const char *dst_btf, const char *objpath)
841+
{
842+
char command[4096];
843+
int n;
844+
845+
n = snprintf(command, sizeof(command),
846+
"./tools/build/bpftool/bpftool gen min_core_btf %s %s %s",
847+
src_btf, dst_btf, objpath);
848+
if (n < 0 || n >= sizeof(command))
849+
return -1;
850+
851+
return system(command);
852+
}
853+
854+
static void run_core_reloc_tests(bool use_btfgen)
840855
{
841856
const size_t mmap_sz = roundup_page(sizeof(struct data));
842857
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
843858
struct core_reloc_test_case *test_case;
844859
const char *tp_name, *probe_name;
845-
int err, i, equal;
860+
int err, i, equal, fd;
846861
struct bpf_link *link = NULL;
847862
struct bpf_map *data_map;
848863
struct bpf_program *prog;
@@ -854,6 +869,7 @@ void test_core_reloc(void)
854869
my_pid_tgid = getpid() | ((uint64_t)syscall(SYS_gettid) << 32);
855870

856871
for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
872+
char btf_file[] = "/tmp/core_reloc.btf.XXXXXX";
857873
test_case = &test_cases[i];
858874
if (!test__start_subtest(test_case->case_name))
859875
continue;
@@ -863,6 +879,25 @@ void test_core_reloc(void)
863879
continue;
864880
}
865881

882+
/* generate a "minimal" BTF file and use it as source */
883+
if (use_btfgen) {
884+
if (!test_case->btf_src_file || test_case->fails) {
885+
test__skip();
886+
continue;
887+
}
888+
889+
fd = mkstemp(btf_file);
890+
if (!ASSERT_GE(fd, 0, "btf_tmp"))
891+
goto cleanup;
892+
close(fd); /* we only need the path */
893+
err = run_btfgen(test_case->btf_src_file, btf_file,
894+
test_case->bpf_obj_file);
895+
if (!ASSERT_OK(err, "run_btfgen"))
896+
goto cleanup;
897+
898+
test_case->btf_src_file = btf_file;
899+
}
900+
866901
if (test_case->setup) {
867902
err = test_case->setup(test_case);
868903
if (CHECK(err, "test_setup", "test #%d setup failed: %d\n", i, err))
@@ -954,8 +989,19 @@ void test_core_reloc(void)
954989
CHECK_FAIL(munmap(mmap_data, mmap_sz));
955990
mmap_data = NULL;
956991
}
992+
remove(btf_file);
957993
bpf_link__destroy(link);
958994
link = NULL;
959995
bpf_object__close(obj);
960996
}
961997
}
998+
999+
void test_core_reloc(void)
1000+
{
1001+
run_core_reloc_tests(false);
1002+
}
1003+
1004+
void test_core_btfgen(void)
1005+
{
1006+
run_core_reloc_tests(true);
1007+
}

0 commit comments

Comments
 (0)