Skip to content

Commit 30a83ce

Browse files
cupermirKernel Patches Daemon
authored andcommitted
selftests/bpf: add verifier sign extension bound computation tests.
This commit adds 3 tests to verify a common compiler generated pattern for sign extension (r1 <<= 32; r1 s>>= 32). The tests make sure the register bounds are correctly computed both for positive and negative register values. Signed-off-by: Cupertino Miranda <[email protected]> Signed-off-by: Andrew Pinski <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Cc: David Faust <[email protected]> Cc: Jose Marchesi <[email protected]> Cc: Elena Zannoni <[email protected]>
1 parent 1a4b1a6 commit 30a83ce

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,76 @@ __naked void arsh32_imm_zero_extend_check(void)
531531
: __clobber_all);
532532
}
533533

534+
SEC("socket")
535+
__description("arsh32 imm sign positive extend check")
536+
__success __retval(0)
537+
__naked void arsh32_imm_sign_extend_positive_check(void)
538+
__log_level(2)
539+
__msg("2: (57) r6 &= 4095 ; R6=scalar(smin=smin32=0,smax=umax=smax32=umax32=4095,var_off=(0x0; 0xfff))")
540+
__msg("3: (67) r6 <<= 32 ; R6=scalar(smin=smin32=0,smax=umax=0xfff00000000,smax32=umax32=0,var_off=(0x0; 0xfff00000000))")
541+
__msg("4: (c7) r6 s>>= 32 ; R6=scalar(smin=smin32=0,smax=umax=smax32=umax32=4095,var_off=(0x0; 0xfff))")
542+
543+
{
544+
asm volatile (" \
545+
call %[bpf_get_prandom_u32]; \
546+
r6 = r0; \
547+
r6 &= 4095; \
548+
r6 <<= 32; \
549+
r6 s>>= 32; \
550+
r0 = 0; \
551+
exit; \
552+
" :
553+
: __imm(bpf_get_prandom_u32)
554+
: __clobber_all);
555+
}
556+
557+
SEC("socket")
558+
__description("arsh32 imm sign negative extend check")
559+
__success __retval(0)
560+
__naked void arsh32_imm_sign_extend_negative_check(void)
561+
__log_level(2)
562+
__msg("3: (17) r6 -= 4095 ; R6=scalar(smin=smin32=-4095,smax=smax32=0)")
563+
__msg("4: (67) r6 <<= 32 ; R6=scalar(smin=0xfffff00100000000,smax=smax32=umax32=0,umax=0xffffffff00000000,smin32=0,var_off=(0x0; 0xffffffff00000000))")
564+
__msg("5: (c7) r6 s>>= 32 ; R6=scalar(smin=smin32=-4095,smax=smax32=0)")
565+
566+
{
567+
asm volatile (" \
568+
call %[bpf_get_prandom_u32]; \
569+
r6 = r0; \
570+
r6 &= 4095; \
571+
r6 -= 4095; \
572+
r6 <<= 32; \
573+
r6 s>>= 32; \
574+
r0 = 0; \
575+
exit; \
576+
" :
577+
: __imm(bpf_get_prandom_u32)
578+
: __clobber_all);
579+
}
580+
581+
SEC("socket")
582+
__description("arsh32 imm sign extend check")
583+
__success __retval(0)
584+
__naked void arsh32_imm_sign_extend_check(void)
585+
__log_level(2)
586+
__msg("3: (17) r6 -= 2047 ; R6=scalar(smin=smin32=-2047,smax=smax32=2048)")
587+
__msg("4: (67) r6 <<= 32 ; R6=scalar(smin=0xfffff80100000000,smax=0x80000000000,umax=0xffffffff00000000,smin32=0,smax32=umax32=0,var_off=(0x0; 0xffffffff00000000))")
588+
__msg("5: (c7) r6 s>>= 32 ; R6=scalar(smin=smin32=-2047,smax=smax32=2048)")
589+
{
590+
asm volatile (" \
591+
call %[bpf_get_prandom_u32]; \
592+
r6 = r0; \
593+
r6 &= 4095; \
594+
r6 -= 2047; \
595+
r6 <<= 32; \
596+
r6 s>>= 32; \
597+
r0 = 0; \
598+
exit; \
599+
" :
600+
: __imm(bpf_get_prandom_u32)
601+
: __clobber_all);
602+
}
603+
534604
SEC("socket")
535605
__description("end16 (to_le) reg zero extend check")
536606
__success __success_unpriv __retval(0)

0 commit comments

Comments
 (0)