]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Add tests for s>>=31 and s>>=63
authorAlexei Starovoitov <ast@kernel.org>
Mon, 12 Jan 2026 20:13:58 +0000 (12:13 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 13 Jan 2026 17:33:38 +0000 (09:33 -0800)
Add tests for special arithmetic shift right.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Co-developed-by: Puranjay Mohan <puranjay@kernel.org>
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20260112201424.816836-3-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/verifier_subreg.c

index b3e1c3eef9ae4470b5144e883d321c9587ec0874..be328100ba531b6a0cb9294b0f043e2650131b1e 100644 (file)
@@ -738,4 +738,89 @@ __naked void ldx_w_zero_extend_check(void)
        : __clobber_all);
 }
 
+SEC("socket")
+__success __success_unpriv __retval(0)
+__naked void arsh_31_and(void)
+{
+       /* Below is what LLVM generates in cilium's bpf_wiregard.o */
+       asm volatile ("                                 \
+       call %[bpf_get_prandom_u32];                    \
+       w2 = w0;                                        \
+       w2 s>>= 31;                                     \
+       w2 &= -134; /* w2 becomes 0 or -134 */          \
+       if w2 s> -1 goto +2;                            \
+       /* Branch always taken because w2 = -134 */     \
+       if w2 != -136 goto +1;                          \
+       w0 /= 0;                                        \
+       w0 = 0;                                         \
+       exit;                                           \
+"      :
+       : __imm(bpf_get_prandom_u32)
+       : __clobber_all);
+}
+
+SEC("socket")
+__success __success_unpriv __retval(0)
+__naked void arsh_63_and(void)
+{
+       /* Copy of arsh_31 with s/w/r/ */
+       asm volatile ("                                 \
+       call %[bpf_get_prandom_u32];                    \
+       r2 = r0;                                        \
+       r2 <<= 32;                                      \
+       r2 s>>= 63;                                     \
+       r2 &= -134;                                     \
+       if r2 s> -1 goto +2;                            \
+       /* Branch always taken because w2 = -134 */     \
+       if r2 != -136 goto +1;                          \
+       r0 /= 0;                                        \
+       r0 = 0;                                         \
+       exit;                                           \
+"      :
+       : __imm(bpf_get_prandom_u32)
+       : __clobber_all);
+}
+
+SEC("socket")
+__success __success_unpriv __retval(0)
+__naked void arsh_31_or(void)
+{
+       asm volatile ("                                 \
+       call %[bpf_get_prandom_u32];                    \
+       w2 = w0;                                        \
+       w2 s>>= 31;                                     \
+       w2 |= 134; /* w2 becomes -1 or 134 */           \
+       if w2 s> -1 goto +2;                            \
+       /* Branch always taken because w2 = -1 */       \
+       if w2 == -1 goto +1;                            \
+       w0 /= 0;                                        \
+       w0 = 0;                                         \
+       exit;                                           \
+"      :
+       : __imm(bpf_get_prandom_u32)
+       : __clobber_all);
+}
+
+SEC("socket")
+__success __success_unpriv __retval(0)
+__naked void arsh_63_or(void)
+{
+       /* Copy of arsh_31 with s/w/r/ */
+       asm volatile ("                                 \
+       call %[bpf_get_prandom_u32];                    \
+       r2 = r0;                                        \
+       r2 <<= 32;                                      \
+       r2 s>>= 63;                                     \
+       r2 |= 134; /* r2 becomes -1 or 134 */           \
+       if r2 s> -1 goto +2;                            \
+       /* Branch always taken because w2 = -1 */       \
+       if r2 == -1 goto +1;                            \
+       r0 /= 0;                                        \
+       r0 = 0;                                         \
+       exit;                                           \
+"      :
+       : __imm(bpf_get_prandom_u32)
+       : __clobber_all);
+}
+
 char _license[] SEC("license") = "GPL";