All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/1] selftests/bpf: cover shift counts over all operand register pairs
@ 2026-12-02 22:18 Ramin Moussavi
  2026-12-02 22:18 ` [PATCH bpf-next 1/1] selftests/bpf: Check shift counts across " Ramin Moussavi
  0 siblings, 1 reply; 3+ messages in thread
From: Ramin Moussavi @ 2026-12-02 22:18 UTC (permalink / raw)
  To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Dave Thaler

I measured what the 32-bit JITs do with a shift whose count comes from
a register, and the answer differs from the interpreter on four of the
six targets I could run.

Method: one small program per case, loaded as BPF_PROG_TYPE_SOCKET_FILTER
and run through BPF_PROG_TEST_RUN, twice per boot -- once with
/proc/sys/net/core/bpf_jit_enable at 0 and once at 1.  Same kernel, same
program text; everything that differs between the two passes is the JIT.
bpf-next at fcd1857c35ebe, under qemu.

Two parts of the tree say different things about a count beyond the
width, and the older one is the one the JITs follow.

kernel/bpf/verifier.c has carried "Shifts greater than 31 or 63 are
undefined.  This includes shifts by a negative number" since 2017; it
sits in is_safe_to_compute_dst_reg_range() today.  The ISA document
says the opposite, and says why it was changed: commit 8819495a754e7
("bpf, docs: Shift operations are defined to use a mask") added "Shift
operations use a mask of 0x3F (63) for 64-bit operations and 0x1F (31)
for 32-bit operations", with the reasoning "since otherwise shifting by
a value out of range (like negative) is undefined".  The interpreter
has done the masking all along, on every target I measured.

Neither side rejects such a program: the predicate returns false, the
caller marks the destination unknown, and the count reaching the shift
is whatever the register holds.

So either the 2023 definition is what a JIT owes, and four of the
targets below are wrong, or "undefined" still holds and the sentence in
the ISA document is the thing to fix.  The measurements are the same
under both readings.

  1) ALU64, count 64

        r6 = 0x0123456789abcdef
        r7 = 64
        r6 >>= r7

     64 & 63 == 0, so the value must come out unchanged.

        interpreter, everywhere 0x0123456789abcdef
        mips32, arc, powerpc32  0x0123456789abcdef
        x86-32, riscv32         0x0000000001234567   shifted by 32
        arm32                   0x0000000000000000

  2) ALU64, count -1

        r6 = 0xfedcba9876543210
        r7 = -1
        r6 s>>= r7

     -1 & 63 == 63, so all sign bits.

        interpreter, everywhere         0xffffffffffffffff
        mips32, arc, powerpc32, x86-32  0xffffffffffffffff
        riscv32                         0xfffffffffdb97530  shifted by 31
        arm32                           0xffffffff00000000

  3) ALU32, count 32

        r6 = 0x89abcdef
        r7 = 32
        w6 >>= w7               (and s>>=)

                                        >>=             s>>=
        interpreter, everywhere         0x89abcdef      0x89abcdef
        mips32, arc, x86-32, riscv32    0x89abcdef      0x89abcdef
        powerpc32, arm32                0x00000000      0xffffffff

What I think should change in jit_insns
======================================

The existing shift_counts subtest already runs counts beyond the width
-- 32, 33, 63, 64, 65, 127, -1, -3 and 2^32+3 -- so it catches the
plain masking failure on arm32, x86-32 and powerpc32 today.  What it
does not vary is the registers: the value is always r1 and the count is
always r2.  That hides two things.

Sweep the operand registers instead of fixing them.  Two reasons, and
the second one is the one that surprised me:

  a) Different pairs compile to different code, and the JITs do not
     agree on which.  arm32 keeps r0, r1 and r6 in hardware registers
     and spills the rest to a stack scratch area; x86-32 keeps every
     BPF register on the stack; powerpc32 keeps them all in registers.
     shift_counts puts the count in r2, so on arm32 it only ever
     exercises the spilled path, and nothing says whether the other one
     behaves the same.

  b) rd == rs is a case of its own, and on powerpc32 it is the only
     64-bit failure there is:

        powerpc32, alu64 >>= and s>>=, count -1
              rs: 0123456789
           rd 0: J.........
           rd 1: .J........
           ...
           rd 9: .........J

        (J = JIT differs from the interpreter, over all 100 pairs)

     Ten of a hundred for each of the two operations, exactly the
     diagonal -- every other 64-bit case on that target is correct.
     Both shifts are open coded in the destination registers, and both
     write the low word as the second of eight instructions:

        1  SUBFIC(_R0, src_reg, 32)
        2  SRW(dst_reg, src2_reg, src_reg)       <- writes dst_reg
        3  ADDI(tmp_reg, src_reg, 32)            <- reads src_reg
        ...
        7  SRW(dst_reg_h, src2_reg_h, src_reg)   <- reads src_reg

     src2_reg defaults to dst_reg, so with rd == rs that one register
     is value, count and destination at once.  Step 2 overwrites the
     count, and steps 3 and 7 read what is left.

     That this is an ordering mistake and not something the aliasing
     forces can be read off the same file: <<= is open coded the same
     way, but writes the low word last, after src_reg is read for the
     last time -- and it is correct for every pair.  arm32 and x86-32
     cannot have the problem at all, because they load the operands
     into scratch registers and write the destination back only at the
     end.

     A fixed r1/r2 reaches none of this.

A hundred pairs times three counts is still a small test.  The patch
below adds the sweep.  jit_insns/shift_counts is already red on arm32,
x86-32 and powerpc32 for part of the counts, so no green subtest turns
red -- the cases the sweep adds are failures in the JITs, not in the
test.  The probe used for the measurements is a single self-contained
C file with no libbpf dependency; happy to post it too.

Ramin Moussavi (1):
  selftests/bpf: Check shift counts across operand register pairs

 .../selftests/bpf/prog_tests/jit_insns.c      | 68 ++++++++++++++++++-
 1 file changed, 66 insertions(+), 2 deletions(-)


base-commit: fcd1857c35ebe058dde310cbe7d350682e008037
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 22:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 22:18 [PATCH bpf-next 0/1] selftests/bpf: cover shift counts over all operand register pairs Ramin Moussavi
2026-12-02 22:18 ` [PATCH bpf-next 1/1] selftests/bpf: Check shift counts across " Ramin Moussavi
2026-12-02 22:21   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.