jit_insns covers the masking of shift counts in shift_counts, but every case uses the same two registers, r1 for the value and r2 for the count. A 32-bit JIT does not treat all BPF registers alike -- arm32 keeps r0, r1 and r6 in hardware registers and spills the rest to a stack scratch area -- so one fixed pair reaches the shift through one path, and the others go untested. The register pair sweep that would cover them, alu_reg_pairs, masks the count to 0..31 before it starts. Run the counts over all pairs as well. Three counts and one value are enough: the pairs are what is being varied, and 32, 64 and -1 each take a different branch in the JITs I looked at. That is 300 more cases per operation and width, 18 more program loads in all. Name the pair in the failure message, since with rd == rs the two inputs are the same value and nothing else tells the cases apart. Measured on bpf-next under qemu with the JIT enabled: arm32 x86-32 ppc32 alu*_*_x counts beyond the width 112 24 47 alu*_*_x counts over register pairs 1220 300 520 rd == rs earns the cases on its own. On ppc32 every one of the 47 the existing test finds is a 32-bit shift; the only 64-bit failures are in >>= and s>>=, on the diagonal, 10 pairs each. That JIT shifts into the destination registers and writes the low word before it has read the count for the last time, which can only go wrong when the two are the same register. No fixed pair reaches it. x86-32 is the other case: the sweep finds no new kind of failure there, only the same 64-bit one in all 100 pairs. Signed-off-by: Ramin Moussavi --- .../selftests/bpf/prog_tests/jit_insns.c | 68 ++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/jit_insns.c b/tools/testing/selftests/bpf/prog_tests/jit_insns.c index 0aaab23a2eef8..4d67e7db38a1b 100644 --- a/tools/testing/selftests/bpf/prog_tests/jit_insns.c +++ b/tools/testing/selftests/bpf/prog_tests/jit_insns.c @@ -2,6 +2,7 @@ /* Copyright (c) 2026 Isovalent */ #include +#include #include #include #include @@ -29,6 +30,7 @@ struct batch { struct sweep_ctx ctx; __u64 exp[NR_CASES][4]; __u8 nr_out[NR_CASES]; + char note[NR_CASES][12]; int nr; char desc[80]; }; @@ -102,8 +104,9 @@ static void batch_flush(struct batch *b) for (j = 0; j < b->nr_out[k]; j++) { if (b->ctx.out[k][j] == b->exp[k][j]) continue; - snprintf(buf, sizeof(buf), "%s: in0=0x%llx in1=0x%llx out%d", - b->desc, b->ctx.in[k][0], b->ctx.in[k][1], j); + snprintf(buf, sizeof(buf), "%s: %s%sin0=0x%llx in1=0x%llx out%d", + b->desc, b->note[k], b->note[k][0] ? " " : "", + b->ctx.in[k][0], b->ctx.in[k][1], j); ASSERT_EQ(b->ctx.out[k][j], b->exp[k][j], buf); } } @@ -124,10 +127,23 @@ static int batch_case(struct batch *b, int rc, __u64 in0, __u64 in1) b->ctx.in[k][0] = in0; b->ctx.in[k][1] = in1; b->nr_out[k] = 0; + b->note[k][0] = 0; EMIT(b, BPF_LDX_MEM(BPF_DW, rc, BPF_REG_10, CTX_SPILL)); return k; } +/* Name the case in its failure message, where the operands alone do not say + * which one it was + */ +__printf(3, 4) static void batch_note(struct batch *b, int k, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vsnprintf(b->note[k], sizeof(b->note[k]), fmt, ap); + va_end(ap); +} + static void batch_expect(struct batch *b, int k, int j, __u64 val) { b->exp[k][j] = val; @@ -826,6 +842,53 @@ static void test_shifts(void) batch_fini(&b); } +/* + * The same counts across all pairs of operand registers. A JIT that keeps + * only some of the BPF registers in hardware registers reaches the shift + * through two different paths, and the masking has to be in both. + */ +static void shift_count_pairs(struct batch *b, int op, bool alu32) +{ + static const __s64 counts[] = { 32, 64, -1 }; + int code = (alu32 ? BPF_ALU : BPF_ALU64) | op | BPF_X; + __u64 val = 0x0123456789abcdefULL; + int bits = alu32 ? 32 : 64; + int c, rd, rs, rc, k; + + snprintf(b->desc, sizeof(b->desc), "alu%d_%s_x counts over register pairs", + bits, alu_name(op)); + for (c = 0; c < ARRAY_SIZE(counts); c++) { + __u64 cnt = alu32 ? (__u32)counts[c] : counts[c]; + __u64 reg = val, self = cnt, res, same; + + if (alu32) { + reg = op == BPF_ARSH ? (__u64)(__s32)reg : (__u32)reg; + self = op == BPF_ARSH ? (__u64)(__s32)self : (__u32)self; + } + alu_result(&res, reg, cnt & (bits - 1), op, 0); + /* With one register for both operands the count is the value */ + alu_result(&same, self, cnt & (bits - 1), op, 0); + if (alu32) { + res = (__u32)res; + same = (__u32)same; + } + for (rd = BPF_REG_0; rd <= BPF_REG_9; rd++) { + for (rs = BPF_REG_0; rs <= BPF_REG_9; rs++) { + rc = scratch_reg(rd, rs); + k = batch_case(b, rc, rd == rs ? counts[c] : val, counts[c]); + batch_note(b, k, "r%d,r%d", rd, rs); + LD_IN(b, k, 0, rc, rd); + if (rs != rd) + LD_IN(b, k, 1, rc, rs); + EMIT(b, BPF_RAW_INSN(code, rd, rs, 0, 0)); + ST_OUT(b, k, 0, rc, rd); + batch_expect(b, k, 0, rd == rs ? same : res); + } + } + batch_flush(b); + } +} + /* * Shifts by register with counts at and above the width and negative ones, * which the ISA masks by the width. Apart in a subtest of its own, as not @@ -869,6 +932,7 @@ static void test_shift_counts(void) } } batch_flush(&b); + shift_count_pairs(&b, op, f); } } batch_fini(&b); -- 2.53.0