From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Tianci Cao" <ziye@zju.edu.cn>, <bpf@vger.kernel.org>
Cc: <ast@kernel.org>, <daniel@iogearbox.net>,
<john.fastabend@gmail.com>, <andrii@kernel.org>,
<martin.lau@linux.dev>, <eddyz87@gmail.com>, <song@kernel.org>,
<yonghong.song@linux.dev>, <kpsingh@kernel.org>,
<sdf@fomichev.me>, <haoluo@google.com>, <jolsa@kernel.org>,
<tangyazhou518@outlook.com>, <shenghaoyuan0928@163.com>
Subject: Re: [PATCH bpf-next 1/2] bpf: support shift operations with non-const src operand
Date: Fri, 12 Jun 2026 09:09:00 -0700 [thread overview]
Message-ID: <DJ7793EEL4P5.1HFCXFENJM2VT@gmail.com> (raw)
In-Reply-To: <20260612093818.18609-2-ziye@zju.edu.cn>
On Fri Jun 12, 2026 at 2:38 AM PDT, Tianci Cao wrote:
> Currently, the BPF verifier only allows shift operations when the shift
> amount is a known constant. This is overly restrictive for cases where
> the shift amount is bounded but not fully determined at verification time.
> For example, the following code is rejected by the verifier even though
> the shift amount is bounded to [1, 4]:
>
> u32 shift = bpf_get_prandom_u32();
> shift &= 3; // shift is in range [0, 3]
> shift += 1; // shift is in range [1, 4]
> r1 <<= shift; // non-const but bounded shift amount
Rejected? I don't believe so. The verifier accepts it, but falls
back to conservative.
> Modify the shift helper functions (scalar_min_max_lsh,
> scalar32_min_max_lsh, scalar_min_max_rsh, scalar32_min_max_rsh,
> scalar_min_max_arsh, scalar32_min_max_arsh) to handle non-const
> but bounded shift amounts.
>
> Update is_safe_to_compute_dst_reg_range() to remove the src_is_const
> check for shift operations. This approach ensures the verifier
> remains sound while allowing more programs to pass verification.
>
> Also modify the comment on is_safe_to_compute_dst_reg_range.
> Shifts by more than insn bitness are legal in the BPF ISA; they are
> currently implementation-defined behaviour of the underlying architecture,
> rather than UB, and have been made legal for performance reasons.
> See: https://lore.kernel.org/bpf/20210706112502.2064236-47-sashal@kernel.org
What this is for?
Do you see such code generated by compiler in real programs?
Does it cause issues with accepting such _real_ programs ?
If not, sorry, we should not complicate the verifier for theoretical case.
pw-bot: cr
next prev parent reply other threads:[~2026-06-12 16:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-12 9:38 [PATCH bpf-next 0/2] bpf: support shift operations with non-const source operand Tianci Cao
2026-06-12 9:38 ` [PATCH bpf-next 1/2] bpf: support shift operations with non-const src operand Tianci Cao
2026-06-12 10:19 ` bot+bpf-ci
2026-06-12 16:09 ` Alexei Starovoitov [this message]
2026-06-12 21:20 ` Eduard Zingerman
2026-06-12 21:17 ` Eduard Zingerman
2026-06-12 9:38 ` [PATCH bpf-next 2/2] selftests/bpf: Add tests for non-const src shift Tianci Cao
2026-06-12 21:29 ` Eduard Zingerman
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=DJ7793EEL4P5.1HFCXFENJM2VT@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=shenghaoyuan0928@163.com \
--cc=song@kernel.org \
--cc=tangyazhou518@outlook.com \
--cc=yonghong.song@linux.dev \
--cc=ziye@zju.edu.cn \
/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