Skip to content

Commit b03e194

Browse files
anakryikoAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Fix btfgen tests
There turned out to be a few problems with btfgen selftests. First, core_btfgen tests are failing in BPF CI due to the use of full-featured bpftool, which has extra dependencies on libbfd, libcap, etc, which are present in BPF CI's build environment, but those shared libraries are missing in QEMU image in which test_progs is running. To fix this problem, use minimal bootstrap version of bpftool instead. It only depend on libelf and libz, same as libbpf, so doesn't add any new requirements (and bootstrap bpftool still implementes entire `bpftool gen` functionality, which is quite convenient). Second problem is even more interesting. Both core_btfgen and core_reloc reuse the same set of struct core_reloc_test_case array of test case definitions. That in itself is not a problem, but btfgen test replaces test_case->btf_src_file property with the path to temporary file into which minimized BTF is output by bpftool. This interferes with original core_reloc tests, depending on order of tests execution (core_btfgen is run first in sequential mode and skrews up subsequent core_reloc run by pointing to already deleted temporary file, instead of the original BTF files) and whether those two runs share the same process (in parallel mode the chances are high for them to run in two separate processes and so not interfere with each other). To prevent this interference, create and use local copy of a test definition. Mark original array as constant to catch accidental modifcations. Note that setup_type_id_case_success() and setup_type_id_case_success() still modify common test_case->output memory area, but it is ok as each setup function has to re-initialize it completely anyways. In sequential mode it leads to deterministic and correct initialization. In parallel mode they will either each have their own process, or if core_reloc and core_btfgen happen to be run by the same worker process, they will still do that sequentially within the worker process. If they are sharded across multiple processes, they don't really share anything anyways. Also, rename core_btfgen into core_reloc_btfgen, as it is indeed just a "flavor" of core_reloc test, not an independent set of tests. So make it more obvious. Last problem that needed solving was that location of bpftool differs between test_progs and test_progs' flavors (e.g., test_progs-no_alu32). To keep it simple, create a symlink to bpftool both inside selftests/bpf/ directory and selftests/bpf/<flavor> subdirectory. That way, from inside core_reloc test, location to bpftool is just "./bpftool". v2->v3: - fix bpftool location relative the test_progs-no_alu32; v1->v2: - fix corruption of core_reloc_test_case. Fixes: 704c91e ("selftests/bpf: Test "bpftool gen min_core_btf") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yucong Sun <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d0b3822 commit b03e194

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2+
bpftool
23
bpf-helpers*
34
bpf-syscall*
45
test_verifier

tools/testing/selftests/bpf/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
470470
$$(call msg,BINARY,,$$@)
471471
$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
472472
$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.o $$@
473+
$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/bootstrap/bpftool $(if $2,$2/)bpftool
473474

474475
endef
475476

@@ -555,7 +556,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \
555556

556557
EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
557558
prog_tests/tests.h map_tests/tests.h verifier/tests.h \
558-
feature \
559+
feature bpftool \
559560
$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h no_alu32 bpf_gcc bpf_testmod.ko)
560561

561562
.PHONY: docs docs-clean

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static int __trigger_module_test_read(const struct core_reloc_test_case *test)
512512
}
513513

514514

515-
static struct core_reloc_test_case test_cases[] = {
515+
static const struct core_reloc_test_case test_cases[] = {
516516
/* validate we can find kernel image and use its BTF for relocs */
517517
{
518518
.case_name = "kernel",
@@ -843,7 +843,7 @@ static int run_btfgen(const char *src_btf, const char *dst_btf, const char *objp
843843
int n;
844844

845845
n = snprintf(command, sizeof(command),
846-
"./tools/build/bpftool/bpftool gen min_core_btf %s %s %s",
846+
"./bpftool gen min_core_btf %s %s %s",
847847
src_btf, dst_btf, objpath);
848848
if (n < 0 || n >= sizeof(command))
849849
return -1;
@@ -855,7 +855,7 @@ static void run_core_reloc_tests(bool use_btfgen)
855855
{
856856
const size_t mmap_sz = roundup_page(sizeof(struct data));
857857
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
858-
struct core_reloc_test_case *test_case;
858+
struct core_reloc_test_case *test_case, test_case_copy;
859859
const char *tp_name, *probe_name;
860860
int err, i, equal, fd;
861861
struct bpf_link *link = NULL;
@@ -870,7 +870,10 @@ static void run_core_reloc_tests(bool use_btfgen)
870870

871871
for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
872872
char btf_file[] = "/tmp/core_reloc.btf.XXXXXX";
873-
test_case = &test_cases[i];
873+
874+
test_case_copy = test_cases[i];
875+
test_case = &test_case_copy;
876+
874877
if (!test__start_subtest(test_case->case_name))
875878
continue;
876879

@@ -881,6 +884,7 @@ static void run_core_reloc_tests(bool use_btfgen)
881884

882885
/* generate a "minimal" BTF file and use it as source */
883886
if (use_btfgen) {
887+
884888
if (!test_case->btf_src_file || test_case->fails) {
885889
test__skip();
886890
continue;
@@ -989,7 +993,8 @@ static void run_core_reloc_tests(bool use_btfgen)
989993
CHECK_FAIL(munmap(mmap_data, mmap_sz));
990994
mmap_data = NULL;
991995
}
992-
remove(btf_file);
996+
if (use_btfgen)
997+
remove(test_case->btf_src_file);
993998
bpf_link__destroy(link);
994999
link = NULL;
9951000
bpf_object__close(obj);
@@ -1001,7 +1006,7 @@ void test_core_reloc(void)
10011006
run_core_reloc_tests(false);
10021007
}
10031008

1004-
void test_core_btfgen(void)
1009+
void test_core_reloc_btfgen(void)
10051010
{
10061011
run_core_reloc_tests(true);
10071012
}

0 commit comments

Comments
 (0)