Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Make linked_list failure test more robust

The linked list failure test 'pop_front_off' and 'pop_back_off'
currently rely on matching exact instruction and register values. The
purpose of the test is to ensure the offset is correctly incremented for
the returned pointers from list pop helpers, which can then be used with
container_of to obtain the real object. Hence, somehow obtaining the
information that the offset is 48 will work for us. Make the test more
robust by relying on verifier error string of bpf_spin_lock and remove
dependence on fragile instruction index or register number, which can be
affected by different clang versions used to build the selftests.

Fixes: 300f19dcdb99 ("selftests/bpf: Add BPF linked list API tests")
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20231020144839.2734006-1-memxor@gmail.com

authored by

Kumar Kartikeya Dwivedi and committed by
Andrii Nakryiko
da1055b6 bab8ac3c

+5 -9
+2 -8
tools/testing/selftests/bpf/prog_tests/linked_list.c
··· 94 94 { "incorrect_head_var_off2", "variable ptr_ access var_off=(0x0; 0xffffffff) disallowed" }, 95 95 { "incorrect_head_off1", "bpf_list_head not found at offset=25" }, 96 96 { "incorrect_head_off2", "bpf_list_head not found at offset=1" }, 97 - { "pop_front_off", 98 - "15: (bf) r1 = r6 ; R1_w=ptr_or_null_foo(id=4,ref_obj_id=4,off=48,imm=0) " 99 - "R6_w=ptr_or_null_foo(id=4,ref_obj_id=4,off=48,imm=0) refs=2,4\n" 100 - "16: (85) call bpf_this_cpu_ptr#154\nR1 type=ptr_or_null_ expected=percpu_ptr_" }, 101 - { "pop_back_off", 102 - "15: (bf) r1 = r6 ; R1_w=ptr_or_null_foo(id=4,ref_obj_id=4,off=48,imm=0) " 103 - "R6_w=ptr_or_null_foo(id=4,ref_obj_id=4,off=48,imm=0) refs=2,4\n" 104 - "16: (85) call bpf_this_cpu_ptr#154\nR1 type=ptr_or_null_ expected=percpu_ptr_" }, 97 + { "pop_front_off", "off 48 doesn't point to 'struct bpf_spin_lock' that is at 40" }, 98 + { "pop_back_off", "off 48 doesn't point to 'struct bpf_spin_lock' that is at 40" }, 105 99 }; 106 100 107 101 static void test_linked_list_fail_prog(const char *prog_name, const char *err_msg)
+3 -1
tools/testing/selftests/bpf/progs/linked_list_fail.c
··· 591 591 n = op(&p->head); 592 592 bpf_spin_unlock(&p->lock); 593 593 594 - bpf_this_cpu_ptr(n); 594 + if (!n) 595 + return 0; 596 + bpf_spin_lock((void *)n); 595 597 return 0; 596 598 } 597 599