From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: Jenny Guanni Qu <qguanni@gmail.com>, bpf@vger.kernel.org
Cc: daniel@iogearbox.net, ast@kernel.org, andrii@kernel.org,
mykyta.yatsenko5@gmail.com, yonghong.song@linux.dev,
lkp@intel.com
Subject: Re: [PATCH v4 2/2] selftests/bpf: Add tests for sdiv32/smod32 with INT_MIN dividend
Date: Tue, 10 Mar 2026 10:05:04 +0800 [thread overview]
Message-ID: <9a212cc9-46cf-4c0a-a5a6-98544f46465b@linux.dev> (raw)
In-Reply-To: <20260309215910.4131143-3-qguanni@gmail.com>
On 3/10/26 5:59 AM, Jenny Guanni Qu wrote:
> 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>
> ---
> .../selftests/bpf/progs/verifier_sdiv.c | 58 +++++++++++++++++++
> 1 file changed, 58 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/verifier_sdiv.c b/tools/testing/selftests/bpf/progs/verifier_sdiv.c
> index 148d2299e5b4..fd59d57e8e37 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_sdiv.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_sdiv.c
> @@ -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
>
Minor nits: extra blank line before #else, and smod32 tests only cover
the imm
form — a reg variant (w0 s%%= w1) would make coverage symmetric with sdiv32.
Neither worth a respin.
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
> SEC("socket")
next prev parent reply other threads:[~2026-03-10 2:05 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-09 21:59 [PATCH v4 0/2] bpf: Fix abs(INT_MIN) undefined behavior in interpreter sdiv/smod Jenny Guanni Qu
2026-03-09 21:59 ` [PATCH v4 1/2] bpf: Fix undefined behavior in interpreter sdiv/smod for INT_MIN Jenny Guanni Qu
2026-03-10 0:28 ` Mykyta Yatsenko
2026-03-10 19:03 ` Alexei Starovoitov
2026-03-09 21:59 ` [PATCH v4 2/2] selftests/bpf: Add tests for sdiv32/smod32 with INT_MIN dividend Jenny Guanni Qu
2026-03-10 2:05 ` Jiayuan Chen [this message]
2026-03-10 2:20 ` Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9a212cc9-46cf-4c0a-a5a6-98544f46465b@linux.dev \
--to=jiayuan.chen@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=lkp@intel.com \
--cc=mykyta.yatsenko5@gmail.com \
--cc=qguanni@gmail.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox