selftests/bpf: Add verifier tests for code pattern '<const> <cond_op> <non_const>'
authorYonghong Song <yhs@fb.com>
Thu, 6 Apr 2023 16:45:10 +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 '<const> <cond_op> <non_const>' to
exercise the previous verifier patch.

The following are veristat changed number of processed insns stat
comparing the previous patch vs. this patch:

File                                                   Program                                               Insns (A)  Insns (B)  Insns  (DIFF)
-----------------------------------------------------  ----------------------------------------------------  ---------  ---------  -------------
test_seg6_loop.bpf.linked3.o                           __add_egr_x                                               12423      12314  -109 (-0.88%)

Only one program is affected with minor change.

Signed-off-by: Yonghong Song <yhs@fb.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230406164510.1047757-1-yhs@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/verifier_bounds_deduction_non_const.c

index fe570d8..823f727 100644 (file)
@@ -176,4 +176,464 @@ l1_%=:                                                    \
        : __clobber_all);
 }
 
+SEC("socket")
+__description("check deducing bounds from non-const, jmp64, <const> > <non_const>, 1")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_9(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       r2 = 0;                                         \
+       if r2 > r0 goto l0_%=;                          \
+       r0 = 0;                                         \
+       exit;                                           \
+l0_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp64, <const> > <non_const>, 2")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_10(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 < 4 goto l0_%=;                           \
+       r2 = 4;                                         \
+       if r2 > r0 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, <const> >= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_11(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 < 4 goto l0_%=;                           \
+       r2 = 3;                                         \
+       if r2 >= r0 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, <const> < <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_12(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 > 4 goto l0_%=;                           \
+       r2 = 4;                                         \
+       if r2 < r0 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, <const> <= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_13(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 >= 4 goto l0_%=;                          \
+       r2 = 4;                                         \
+       if r2 <= r0 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, <const> == <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_14(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 < 3 goto l0_%=;                           \
+       r2 = 2;                                         \
+       if r2 == r0 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, <const> s> <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_15(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 s< 4 goto l0_%=;                          \
+       r2 = 4;                                         \
+       if r2 s> r0 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, <const> s>= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_16(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 s< 4 goto l0_%=;                          \
+       r2 = 3;                                         \
+       if r2 s>= r0 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, <const> s< <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_17(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 s> 4 goto l0_%=;                          \
+       r2 = 4;                                         \
+       if r2 s< r0 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, <const> s<= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_18(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 s> 4 goto l0_%=;                          \
+       r2 = 5;                                         \
+       if r2 s<= r0 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, <const> != <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_19(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if r0 < 3 goto l0_%=;                           \
+       r2 = 2;                                         \
+       if r2 != r0 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, <const> > <non_const>, 1")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_20(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       w2 = 0;                                         \
+       if w2 > w0 goto l0_%=;                          \
+       r0 = 0;                                         \
+       exit;                                           \
+l0_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("check deducing bounds from non-const, jmp32, <const> > <non_const>, 2")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_21(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 < 4 goto l0_%=;                           \
+       w2 = 4;                                         \
+       if w2 > w0 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, <const> >= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_22(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 < 4 goto l0_%=;                           \
+       w2 = 3;                                         \
+       if w2 >= w0 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, <const> < <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_23(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 > 4 goto l0_%=;                           \
+       w2 = 4;                                         \
+       if w2 < w0 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, <const> <= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_24(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 >= 4 goto l0_%=;                          \
+       w2 = 4;                                         \
+       if w2 <= w0 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, <const> == <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_25(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 < 4 goto l0_%=;                           \
+       w2 = 3;                                         \
+       if w2 == w0 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, <const> s> <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_26(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 s< 4 goto l0_%=;                          \
+       w2 = 4;                                         \
+       if w2 s> w0 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, <const> s>= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_27(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 s< 4 goto l0_%=;                          \
+       w2 = 3;                                         \
+       if w2 s>= w0 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, <const> s< <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_28(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 s> 4 goto l0_%=;                          \
+       w2 = 5;                                         \
+       if w2 s< w0 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, <const> s<= <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_29(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 s>= 4 goto l0_%=;                         \
+       w2 = 4;                                         \
+       if w2 s<= w0 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, <const> != <non_const>")
+__success __retval(0)
+__naked void deducing_bounds_from_non_const_30(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_ktime_get_ns];                       \
+       if w0 < 3 goto l0_%=;                           \
+       w2 = 2;                                         \
+       if w2 != w0 goto l0_%=;                         \
+       goto l1_%=;                                     \
+l0_%=:                                                 \
+       r0 = 0;                                         \
+       exit;                                           \
+l1_%=:                                                 \
+       r0 -= r1;                                       \
+       exit;                                           \
+"      :
+       : __imm(bpf_ktime_get_ns)
+       : __clobber_all);
+}
+
 char _license[] SEC("license") = "GPL";