Skip to content

Commit 9867e22

Browse files
Yonghong SongKernel Patches Daemon
authored andcommitted
selftests/bpf: Fix some incorrect inline asm codes
In one of upstream thread ([1]), there is a discussion about the below inline asm code: if r1 == 0xdeadbeef goto +2; ... In actual llvm backend, the above 0xdeadbeef will actually do sign extension to 64bit value and then compare to register r1. But the code itself does not imply the above semantics. It looks like the comparision is between r1 and 0xdeadbeef. For example, let us at a simple C code: $ cat t1.c int foo(long a) { return a == 0xdeadbeef ? 2 : 3; } $ clang --target=bpf -O2 -c t1.c && llvm-objdump -d t1.o ... w0 = 0x2 r2 = 0xdeadbeef ll if r1 == r2 goto +0x1 w0 = 0x3 exit It does try to compare r1 and 0xdeadbeef. To address the above confusing inline asm issue, llvm backend ([2]) added some range checking for such insns and beyond. For the above insn asm, the warning like below warning: immediate out of range, shall fit in int range will be issued. If -Werror is in the compilation flags, the error will be issued. To avoid the above warning/error, the afore-mentioned inline asm should be rewritten to if r1 == -559038737 goto +2; ... Fix a few selftest cases like the above based on insn range checking requirement in [2]. [1] https://lore.kernel.org/bpf/[email protected]/ [2] llvm/llvm-project#142989 Signed-off-by: Yonghong Song <[email protected]>
1 parent b048344 commit 9867e22

16 files changed

+32
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int BPF_PROG(test_1, struct bpf_dummy_ops_state *state)
1919
*/
2020
asm volatile (
2121
"if %[state] != 0 goto +2;"
22-
"r0 = 0xf2f3f4f5;"
22+
"r0 = -218893067;"
2323
"exit;"
2424
::[state]"p"(state));
2525

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ int dynptr_pruning_type_confusion(struct __sk_buff *ctx)
761761
*(u64 *)(r2 + 0) = r9; \
762762
r3 = r10; \
763763
r3 += -24; \
764-
r9 = 0xeB9FeB9F; \
764+
r9 = -341840993; \
765765
*(u64 *)(r10 - 16) = r9; \
766766
*(u64 *)(r10 - 24) = r9; \
767767
r9 = 0; \

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ __naked int delayed_read_mark(void)
774774
"3:"
775775
"r1 = r7;"
776776
"r2 = 8;"
777-
"r3 = 0xdeadbeef;"
777+
"r3 = -559038737;"
778778
"call %[bpf_probe_read_user];"
779779
"goto 1b;"
780780
"2:"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ __naked void known_subreg_with_unknown_reg(void)
9999
call %[bpf_get_prandom_u32]; \
100100
r0 <<= 32; \
101101
r0 += 1; \
102-
r0 &= 0xFFFF1234; \
102+
r0 &= -60876; \
103103
/* Upper bits are unknown but AND above masks out 1 zero'ing lower bits */\
104104
if w0 < 1 goto l0_%=; \
105105
r1 = *(u32*)(r1 + 512); \

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ __naked void on_sign_extended_mov_test1(void)
152152
call %[bpf_map_lookup_elem]; \
153153
if r0 == 0 goto l0_%=; \
154154
/* r2 = 0xffff'ffff'ffff'ffff */ \
155-
r2 = 0xffffffff; \
155+
r2 = -1; \
156156
/* r2 = 0xffff'ffff */ \
157157
r2 >>= 32; \
158158
/* r0 = <oob pointer> */ \
@@ -183,7 +183,7 @@ __naked void on_sign_extended_mov_test2(void)
183183
call %[bpf_map_lookup_elem]; \
184184
if r0 == 0 goto l0_%=; \
185185
/* r2 = 0xffff'ffff'ffff'ffff */ \
186-
r2 = 0xffffffff; \
186+
r2 = -1; \
187187
/* r2 = 0xfff'ffff */ \
188188
r2 >>= 36; \
189189
/* r0 = <oob pointer> */ \

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ __naked void test20_x_pkt_ptr_1(void)
485485
asm volatile (" \
486486
r2 = *(u32*)(r1 + %[__sk_buff_data]); \
487487
r3 = *(u32*)(r1 + %[__sk_buff_data_end]); \
488-
r0 = 0xffffffff; \
488+
r0 = -1; \
489489
*(u64*)(r10 - 8) = r0; \
490490
r0 = *(u64*)(r10 - 8); \
491491
r0 &= 0x7fff; \
@@ -515,7 +515,7 @@ __naked void test21_x_pkt_ptr_2(void)
515515
r0 = r2; \
516516
r0 += 8; \
517517
if r0 > r3 goto l0_%=; \
518-
r4 = 0xffffffff; \
518+
r4 = -1; \
519519
*(u64*)(r10 - 8) = r4; \
520520
r4 = *(u64*)(r10 - 8); \
521521
r4 &= 0x7fff; \
@@ -548,7 +548,7 @@ __naked void test22_x_pkt_ptr_3(void)
548548
r3 = *(u64*)(r10 - 16); \
549549
if r0 > r3 goto l0_%=; \
550550
r2 = *(u64*)(r10 - 8); \
551-
r4 = 0xffffffff; \
551+
r4 = -1; \
552552
lock *(u64 *)(r10 - 8) += r4; \
553553
r4 = *(u64*)(r10 - 8); \
554554
r4 >>= 49; \
@@ -605,7 +605,7 @@ __naked void test24_x_pkt_ptr_5(void)
605605
asm volatile (" \
606606
r2 = *(u32*)(r1 + %[__sk_buff_data]); \
607607
r3 = *(u32*)(r1 + %[__sk_buff_data_end]); \
608-
r0 = 0xffffffff; \
608+
r0 = -1; \
609609
*(u64*)(r10 - 8) = r0; \
610610
r0 = *(u64*)(r10 - 8); \
611611
r0 &= 0xff; \

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ __success __success_unpriv __retval(-1)
5050
__naked void ldsx_s32(void)
5151
{
5252
asm volatile (
53-
"r1 = 0xfffffffe;"
53+
"r1 = -2;"
5454
"*(u64 *)(r10 - 8) = r1;"
5555
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
5656
"r0 = *(s32 *)(r10 - 8);"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ __success __success_unpriv __retval(0)
171171
__naked void test_out_of_bounds_9(void)
172172
{
173173
asm volatile (" \
174-
r1 = 0xffffffff; \
174+
r1 = -1; \
175175
w2 = %[__imm_0]; \
176176
r2 -= r1; \
177177
r2 |= r1; \
@@ -191,7 +191,7 @@ __success __success_unpriv __retval(0)
191191
__naked void test_out_of_bounds_10(void)
192192
{
193193
asm volatile (" \
194-
r1 = 0xffffffff; \
194+
r1 = -1; \
195195
w2 = %[__imm_0]; \
196196
r2 -= r1; \
197197
r2 |= r1; \

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ __success __success_unpriv __retval(-1)
6464
__naked void mov64sx_s32(void)
6565
{
6666
asm volatile (" \
67-
r0 = 0xfffffffe; \
67+
r0 = -2; \
6868
r0 = (s32)r0; \
6969
r0 >>= 1; \
7070
exit; \

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ __msg("R0 invalid mem access 'scalar'")
1111
__naked void or_jmp32_k(void)
1212
{
1313
asm volatile (" \
14-
r0 = 0xffffffff; \
14+
r0 = -1; \
1515
r0 /= 1; \
1616
r1 = 0; \
1717
w1 = -1; \

0 commit comments

Comments
 (0)