]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Add tests for sdiv32/smod32 with INT_MIN dividend
authorJenny Guanni Qu <qguanni@gmail.com>
Wed, 11 Mar 2026 01:11:16 +0000 (01:11 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 21 Mar 2026 20:12:17 +0000 (13:12 -0700)
Add tests to verify that signed 32-bit division and modulo operations
produce correct results when the dividend is INT_MIN (0x80000000).

The bug fixed in the previous commit only affects the BPF interpreter
path. When JIT is enabled (the default on most architectures), the
native CPU division instruction produces the correct result and these
tests pass regardless. With bpf_jit_enable=0, the interpreter is used
and without the previous fix, INT_MIN / 2 incorrectly returns
0x40000000 instead of 0xC0000000 due to abs(S32_MIN) undefined
behavior, causing these tests to fail.

Test cases:
  - SDIV32 INT_MIN / 2 = -1073741824 (imm and reg divisor)
  - SMOD32 INT_MIN % 2 = 0 (positive and negative divisor)

Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Jenny Guanni Qu <qguanni@gmail.com>
Link: https://lore.kernel.org/r/20260311011116.2108005-3-qguanni@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/verifier_sdiv.c

index 148d2299e5b4c82a2de571df195024580174652f..fd59d57e8e3763cb2216f04b98314c2956ef3407 100644 (file)
@@ -1209,6 +1209,64 @@ __naked void smod32_ri_divisor_neg_1(void)
        : __clobber_all);
 }
 
+SEC("socket")
+__description("SDIV32, INT_MIN divided by 2, imm")
+__success __success_unpriv __retval(-1073741824)
+__naked void sdiv32_int_min_div_2_imm(void)
+{
+       asm volatile ("                                 \
+       w0 = %[int_min];                                \
+       w0 s/= 2;                                       \
+       exit;                                           \
+"      :
+       : __imm_const(int_min, INT_MIN)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("SDIV32, INT_MIN divided by 2, reg")
+__success __success_unpriv __retval(-1073741824)
+__naked void sdiv32_int_min_div_2_reg(void)
+{
+       asm volatile ("                                 \
+       w0 = %[int_min];                                \
+       w1 = 2;                                         \
+       w0 s/= w1;                                      \
+       exit;                                           \
+"      :
+       : __imm_const(int_min, INT_MIN)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("SMOD32, INT_MIN modulo 2, imm")
+__success __success_unpriv __retval(0)
+__naked void smod32_int_min_mod_2_imm(void)
+{
+       asm volatile ("                                 \
+       w0 = %[int_min];                                \
+       w0 s%%= 2;                                      \
+       exit;                                           \
+"      :
+       : __imm_const(int_min, INT_MIN)
+       : __clobber_all);
+}
+
+SEC("socket")
+__description("SMOD32, INT_MIN modulo -2, imm")
+__success __success_unpriv __retval(0)
+__naked void smod32_int_min_mod_neg2_imm(void)
+{
+       asm volatile ("                                 \
+       w0 = %[int_min];                                \
+       w0 s%%= -2;                                     \
+       exit;                                           \
+"      :
+       : __imm_const(int_min, INT_MIN)
+       : __clobber_all);
+}
+
+
 #else
 
 SEC("socket")