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

selftests/bpf: Add tests for non-constant cond_op NE/EQ bound deduction

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>

authored by

Yonghong Song and committed by
Alexei Starovoitov
aec08d67 13fbcee5

+181
+2
tools/testing/selftests/bpf/prog_tests/verifier.c
··· 7 7 #include "verifier_array_access.skel.h" 8 8 #include "verifier_basic_stack.skel.h" 9 9 #include "verifier_bounds_deduction.skel.h" 10 + #include "verifier_bounds_deduction_non_const.skel.h" 10 11 #include "verifier_bounds_mix_sign_unsign.skel.h" 11 12 #include "verifier_cfg.skel.h" 12 13 #include "verifier_cgroup_inv_retcode.skel.h" ··· 71 70 void test_verifier_array_access(void) { RUN(verifier_array_access); } 72 71 void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); } 73 72 void test_verifier_bounds_deduction(void) { RUN(verifier_bounds_deduction); } 73 + void test_verifier_bounds_deduction_non_const(void) { RUN(verifier_bounds_deduction_non_const); } 74 74 void test_verifier_bounds_mix_sign_unsign(void) { RUN(verifier_bounds_mix_sign_unsign); } 75 75 void test_verifier_cfg(void) { RUN(verifier_cfg); } 76 76 void test_verifier_cgroup_inv_retcode(void) { RUN(verifier_cgroup_inv_retcode); }
+179
tools/testing/selftests/bpf/progs/verifier_bounds_deduction_non_const.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #include <linux/bpf.h> 4 + #include <bpf/bpf_helpers.h> 5 + #include "bpf_misc.h" 6 + 7 + SEC("socket") 8 + __description("check deducing bounds from non-const, jmp64, <non_const> == <const>, 1") 9 + __success __retval(0) 10 + __naked void deducing_bounds_from_non_const_1(void) 11 + { 12 + asm volatile (" \ 13 + call %[bpf_ktime_get_ns]; \ 14 + if r0 < 3 goto l0_%=; \ 15 + r2 = 2; \ 16 + if r0 == r2 goto l1_%=; \ 17 + l0_%=: \ 18 + r0 = 0; \ 19 + exit; \ 20 + l1_%=: \ 21 + r0 -= r1; \ 22 + exit; \ 23 + " : 24 + : __imm(bpf_ktime_get_ns) 25 + : __clobber_all); 26 + } 27 + 28 + SEC("socket") 29 + __description("check deducing bounds from non-const, jmp64, <non_const> == <const>, 2") 30 + __success __retval(0) 31 + __naked void deducing_bounds_from_non_const_2(void) 32 + { 33 + asm volatile (" \ 34 + call %[bpf_ktime_get_ns]; \ 35 + if r0 > 3 goto l0_%=; \ 36 + r2 = 4; \ 37 + if r0 == r2 goto l1_%=; \ 38 + l0_%=: \ 39 + r0 = 0; \ 40 + exit; \ 41 + l1_%=: \ 42 + r0 -= r1; \ 43 + exit; \ 44 + " : 45 + : __imm(bpf_ktime_get_ns) 46 + : __clobber_all); 47 + } 48 + 49 + SEC("socket") 50 + __description("check deducing bounds from non-const, jmp64, <non_const> != <const>, 1") 51 + __success __retval(0) 52 + __naked void deducing_bounds_from_non_const_3(void) 53 + { 54 + asm volatile (" \ 55 + call %[bpf_ktime_get_ns]; \ 56 + if r0 < 3 goto l0_%=; \ 57 + r2 = 2; \ 58 + if r0 != r2 goto l0_%=; \ 59 + goto l1_%=; \ 60 + l0_%=: \ 61 + r0 = 0; \ 62 + exit; \ 63 + l1_%=: \ 64 + r0 -= r1; \ 65 + exit; \ 66 + " : 67 + : __imm(bpf_ktime_get_ns) 68 + : __clobber_all); 69 + } 70 + 71 + SEC("socket") 72 + __description("check deducing bounds from non-const, jmp64, <non_const> != <const>, 2") 73 + __success __retval(0) 74 + __naked void deducing_bounds_from_non_const_4(void) 75 + { 76 + asm volatile (" \ 77 + call %[bpf_ktime_get_ns]; \ 78 + if r0 > 3 goto l0_%=; \ 79 + r2 = 4; \ 80 + if r0 != r2 goto l0_%=; \ 81 + goto l1_%=; \ 82 + l0_%=: \ 83 + r0 = 0; \ 84 + exit; \ 85 + l1_%=: \ 86 + r0 -= r1; \ 87 + exit; \ 88 + " : 89 + : __imm(bpf_ktime_get_ns) 90 + : __clobber_all); 91 + } 92 + 93 + SEC("socket") 94 + __description("check deducing bounds from non-const, jmp32, <non_const> == <const>, 1") 95 + __success __retval(0) 96 + __naked void deducing_bounds_from_non_const_5(void) 97 + { 98 + asm volatile (" \ 99 + call %[bpf_ktime_get_ns]; \ 100 + if w0 < 4 goto l0_%=; \ 101 + w2 = 3; \ 102 + if w0 == w2 goto l1_%=; \ 103 + l0_%=: \ 104 + r0 = 0; \ 105 + exit; \ 106 + l1_%=: \ 107 + r0 -= r1; \ 108 + exit; \ 109 + " : 110 + : __imm(bpf_ktime_get_ns) 111 + : __clobber_all); 112 + } 113 + 114 + SEC("socket") 115 + __description("check deducing bounds from non-const, jmp32, <non_const> == <const>, 2") 116 + __success __retval(0) 117 + __naked void deducing_bounds_from_non_const_6(void) 118 + { 119 + asm volatile (" \ 120 + call %[bpf_ktime_get_ns]; \ 121 + if w0 > 4 goto l0_%=; \ 122 + w2 = 5; \ 123 + if w0 == w2 goto l1_%=; \ 124 + l0_%=: \ 125 + r0 = 0; \ 126 + exit; \ 127 + l1_%=: \ 128 + r0 -= r1; \ 129 + exit; \ 130 + " : 131 + : __imm(bpf_ktime_get_ns) 132 + : __clobber_all); 133 + } 134 + 135 + SEC("socket") 136 + __description("check deducing bounds from non-const, jmp32, <non_const> != <const>, 1") 137 + __success __retval(0) 138 + __naked void deducing_bounds_from_non_const_7(void) 139 + { 140 + asm volatile (" \ 141 + call %[bpf_ktime_get_ns]; \ 142 + if w0 < 3 goto l0_%=; \ 143 + w2 = 2; \ 144 + if w0 != w2 goto l0_%=; \ 145 + goto l1_%=; \ 146 + l0_%=: \ 147 + r0 = 0; \ 148 + exit; \ 149 + l1_%=: \ 150 + r0 -= r1; \ 151 + exit; \ 152 + " : 153 + : __imm(bpf_ktime_get_ns) 154 + : __clobber_all); 155 + } 156 + 157 + SEC("socket") 158 + __description("check deducing bounds from non-const, jmp32, <non_const> != <const>, 2") 159 + __success __retval(0) 160 + __naked void deducing_bounds_from_non_const_8(void) 161 + { 162 + asm volatile (" \ 163 + call %[bpf_ktime_get_ns]; \ 164 + if w0 > 3 goto l0_%=; \ 165 + w2 = 4; \ 166 + if w0 != w2 goto l0_%=; \ 167 + goto l1_%=; \ 168 + l0_%=: \ 169 + r0 = 0; \ 170 + exit; \ 171 + l1_%=: \ 172 + r0 -= r1; \ 173 + exit; \ 174 + " : 175 + : __imm(bpf_ktime_get_ns) 176 + : __clobber_all); 177 + } 178 + 179 + char _license[] SEC("license") = "GPL";