Skip to content

Commit 463ea64

Browse files
image-dragonAlexei Starovoitov
authored andcommitted
selftests/bpf: add testcase to verifier_bounds.c for BPF_JNE
Add testcase for the logic that the verifier tracks the BPF_JNE for regs. The assembly function "reg_not_equal_const()" and "reg_equal_const" that we add is exactly converted from the following case: u32 a = bpf_get_prandom_u32(); u64 b = 0; a %= 8; /* the "a > 0" here will be optimized to "a != 0" */ if (a > 0) { /* now the range of a should be [1, 7] */ bpf_skb_store_bytes(skb, 0, &b, a, 0); } Signed-off-by: Menglong Dong <menglong8.dong@gmail.com> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20231219134800.1550388-5-menglong8.dong@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 31d9cc9 commit 463ea64

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tools/testing/selftests/bpf/progs/verifier_bounds.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,4 +1075,66 @@ l0_%=: r0 = 0; \
10751075
: __clobber_all);
10761076
}
10771077

1078+
SEC("tc")
1079+
__description("bounds check with JMP_NE for reg edge")
1080+
__success __retval(0)
1081+
__naked void reg_not_equal_const(void)
1082+
{
1083+
asm volatile (" \
1084+
r6 = r1; \
1085+
r1 = 0; \
1086+
*(u64*)(r10 - 8) = r1; \
1087+
call %[bpf_get_prandom_u32]; \
1088+
r4 = r0; \
1089+
r4 &= 7; \
1090+
if r4 != 0 goto l0_%=; \
1091+
r0 = 0; \
1092+
exit; \
1093+
l0_%=: r1 = r6; \
1094+
r2 = 0; \
1095+
r3 = r10; \
1096+
r3 += -8; \
1097+
r5 = 0; \
1098+
/* The 4th argument of bpf_skb_store_bytes is defined as \
1099+
* ARG_CONST_SIZE, so 0 is not allowed. The 'r4 != 0' \
1100+
* is providing us this exclusion of zero from initial \
1101+
* [0, 7] range. \
1102+
*/ \
1103+
call %[bpf_skb_store_bytes]; \
1104+
r0 = 0; \
1105+
exit; \
1106+
" :
1107+
: __imm(bpf_get_prandom_u32),
1108+
__imm(bpf_skb_store_bytes)
1109+
: __clobber_all);
1110+
}
1111+
1112+
SEC("tc")
1113+
__description("bounds check with JMP_EQ for reg edge")
1114+
__success __retval(0)
1115+
__naked void reg_equal_const(void)
1116+
{
1117+
asm volatile (" \
1118+
r6 = r1; \
1119+
r1 = 0; \
1120+
*(u64*)(r10 - 8) = r1; \
1121+
call %[bpf_get_prandom_u32]; \
1122+
r4 = r0; \
1123+
r4 &= 7; \
1124+
if r4 == 0 goto l0_%=; \
1125+
r1 = r6; \
1126+
r2 = 0; \
1127+
r3 = r10; \
1128+
r3 += -8; \
1129+
r5 = 0; \
1130+
/* Just the same as what we do in reg_not_equal_const() */ \
1131+
call %[bpf_skb_store_bytes]; \
1132+
l0_%=: r0 = 0; \
1133+
exit; \
1134+
" :
1135+
: __imm(bpf_get_prandom_u32),
1136+
__imm(bpf_skb_store_bytes)
1137+
: __clobber_all);
1138+
}
1139+
10781140
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)