selftests/bpf: Add tests for non-constant cond_op NE/EQ bound deduction
authorYonghong Song <yhs@fb.com>
Thu, 6 Apr 2023 16:45:00 +0000 (09:45 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 6 Apr 2023 22:26:08 +0000 (15:26 -0700)
Add various tests for code pattern '<non-const> NE/EQ <const>' implemented
in the previous verifier patch. Without the verifier patch, these new
tests will fail.

Signed-off-by: Yonghong Song <yhs@fb.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230406164500.1045715-1-yhs@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/verifier.c
tools/testing/selftests/bpf/progs/verifier_bounds_deduction_non_const.c [new file with mode: 0644]

index efc8cf2e18d014e6d8e0689b628f7c9e0c005d54..73dff693d41163bc410604462c3d2aec35c734a5 100644 (file)
@@ -7,6 +7,7 @@
 #include "verifier_array_access.skel.h"
 #include "verifier_basic_stack.skel.h"
 #include "verifier_bounds_deduction.skel.h"
+#include "verifier_bounds_deduction_non_const.skel.h"
 #include "verifier_bounds_mix_sign_unsign.skel.h"
 #include "verifier_cfg.skel.h"
 #include "verifier_cgroup_inv_retcode.skel.h"
@@ -70,6 +71,7 @@ void test_verifier_and(void)                  { RUN(verifier_and); }
 void test_verifier_array_access(void)         { RUN(verifier_array_access); }
 void test_verifier_basic_stack(void)          { RUN(verifier_basic_stack); }
 void test_verifier_bounds_deduction(void)     { RUN(verifier_bounds_deduction); }
+void test_verifier_bounds_deduction_non_const(void)     { RUN(verifier_bounds_deduction_non_const); }
 void test_verifier_bounds_mix_sign_unsign(void) { RUN(verifier_bounds_mix_sign_unsign); }
 void test_verifier_cfg(void)                  { RUN(verifier_cfg); }
 void test_verifier_cgroup_inv_retcode(void)   { RUN(verifier_cgroup_inv_retcode); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_bounds_deduction_non_const.c b/tools/testing/selftests/bpf/progs/verifier_bounds_deduction_non_const.c
new file mode 100644 (file)
index 0000000..fe570d8
--- /dev/null
@@ -0,0 +1,179 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp64, <non_const> == <const>, 1")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_1(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 < 3 goto l0_%=;                           \
+       r2 = 2;                                         \
+       if r0 == r2 goto l1_%=;                         \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp64, <non_const> == <const>, 2")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_2(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 > 3 goto l0_%=;                           \
+       r2 = 4;                                         \
+       if r0 == r2 goto l1_%=;                         \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp64, <non_const> != <const>, 1")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_3(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 < 3 goto l0_%=;                           \
+       r2 = 2;                                         \
+       if r0 != r2 goto l0_%=;                         \
+       goto l1_%=;                                     \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp64, <non_const> != <const>, 2")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_4(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 > 3 goto l0_%=;                           \
+       r2 = 4;                                         \
+       if r0 != r2 goto l0_%=;                         \
+       goto l1_%=;                                     \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp32, <non_const> == <const>, 1")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_5(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 < 4 goto l0_%=;                           \
+       w2 = 3;                                         \
+       if w0 == w2 goto l1_%=;                         \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp32, <non_const> == <const>, 2")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_6(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 > 4 goto l0_%=;                           \
+       w2 = 5;                                         \
+       if w0 == w2 goto l1_%=;                         \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp32, <non_const> != <const>, 1")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_7(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 < 3 goto l0_%=;                           \
+       w2 = 2;                                         \
+       if w0 != w2 goto l0_%=;                         \
+       goto l1_%=;                                     \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp32, <non_const> != <const>, 2")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_8(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 > 3 goto l0_%=;                           \
+       w2 = 4;                                         \
+       if w0 != w2 goto l0_%=;                         \
+       goto l1_%=;                                     \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+char _license[] SEC("license") = "GPL";