Skip to content

Commit d7f157d

Browse files
Jenny Guanni QuKernel Patches Daemon
authored andcommitted
selftests/bpf: Add tests for sdiv32/smod32 with INT_MIN dividend
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) Signed-off-by: Jenny Guanni Qu <qguanni@gmail.com>
1 parent 8fbb921 commit d7f157d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,64 @@ __naked void smod32_ri_divisor_neg_1(void)
12091209
: __clobber_all);
12101210
}
12111211

1212+
SEC("socket")
1213+
__description("SDIV32, INT_MIN divided by 2, imm")
1214+
__success __success_unpriv __retval(-1073741824)
1215+
__naked void sdiv32_int_min_div_2_imm(void)
1216+
{
1217+
asm volatile (" \
1218+
w0 = %[int_min]; \
1219+
w0 s/= 2; \
1220+
exit; \
1221+
" :
1222+
: __imm_const(int_min, INT_MIN)
1223+
: __clobber_all);
1224+
}
1225+
1226+
SEC("socket")
1227+
__description("SDIV32, INT_MIN divided by 2, reg")
1228+
__success __success_unpriv __retval(-1073741824)
1229+
__naked void sdiv32_int_min_div_2_reg(void)
1230+
{
1231+
asm volatile (" \
1232+
w0 = %[int_min]; \
1233+
w1 = 2; \
1234+
w0 s/= w1; \
1235+
exit; \
1236+
" :
1237+
: __imm_const(int_min, INT_MIN)
1238+
: __clobber_all);
1239+
}
1240+
1241+
SEC("socket")
1242+
__description("SMOD32, INT_MIN modulo 2, imm")
1243+
__success __success_unpriv __retval(0)
1244+
__naked void smod32_int_min_mod_2_imm(void)
1245+
{
1246+
asm volatile (" \
1247+
w0 = %[int_min]; \
1248+
w0 s%%= 2; \
1249+
exit; \
1250+
" :
1251+
: __imm_const(int_min, INT_MIN)
1252+
: __clobber_all);
1253+
}
1254+
1255+
SEC("socket")
1256+
__description("SMOD32, INT_MIN modulo -2, imm")
1257+
__success __success_unpriv __retval(0)
1258+
__naked void smod32_int_min_mod_neg2_imm(void)
1259+
{
1260+
asm volatile (" \
1261+
w0 = %[int_min]; \
1262+
w0 s%%= -2; \
1263+
exit; \
1264+
" :
1265+
: __imm_const(int_min, INT_MIN)
1266+
: __clobber_all);
1267+
}
1268+
1269+
12121270
#else
12131271

12141272
SEC("socket")

0 commit comments

Comments
 (0)