Skip to content

Commit d6f1837

Browse files
yonghong-songborkmann
authored andcommitted
selftests/bpf: fix segfault of test_progs when prog loading failed
The test_progs subtests, test_spin_lock() and test_map_lock(), requires BTF present to run successfully. Currently, when BTF failed to load, test_progs will segfault, $ ./test_progs ... 12: (bf) r1 = r8 13: (85) call bpf_spin_lock#93 map 'hash_map' has to have BTF in order to use bpf_spin_lock libbpf: -- END LOG -- libbpf: failed to load program 'map_lock_demo' libbpf: failed to load object './test_map_lock.o' test_map_lock:bpf_prog_load errno 13 Segmentation fault The segfault is caused by uninitialized variable "obj", which is used in bpf_object__close(obj), when bpf prog failed to load. Initializing variable "obj" to NULL in two occasions fixed the problem. $ ./test_progs ... Summary: 219 PASSED, 2 FAILED Fixes: b4d4556 ("selftests/bpf: add bpf_spin_lock verifier tests") Fixes: ba72a7b ("selftests/bpf: test for BPF_F_LOCK") Reported-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Yonghong Song <yhs@fb.com> Acked-by: Song Liu <songliubraving@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent f38a1f0 commit d6f1837

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

‎tools/testing/selftests/bpf/prog_tests/map_lock.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void test_map_lock(void)
3737
const char *file = "./test_map_lock.o";
3838
int prog_fd, map_fd[2], vars[17] = {};
3939
pthread_t thread_id[6];
40-
struct bpf_object *obj;
40+
struct bpf_object *obj = NULL;
4141
int err = 0, key = 0, i;
4242
void *ret;
4343

‎tools/testing/selftests/bpf/prog_tests/spinlock.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ void test_spinlock(void)
55
{
66
const char *file = "./test_spin_lock.o";
77
pthread_t thread_id[4];
8-
struct bpf_object *obj;
8+
struct bpf_object *obj = NULL;
99
int prog_fd;
1010
int err = 0, i;
1111
void *ret;

0 commit comments

Comments
 (0)