selftests/bpf: Add tests for s>>=31 and s>>=63

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>
This commit is contained in:
Alexei Starovoitov 2026-01-12 12:13:58 -08:00
parent bffacdb80b
commit 9160335317

View 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";