From: Alexei Starovoitov The HTTP/2 filter of the Datadog agent counts the frames that it keeps in a __u8 (find_relevant_headers() of release 7.50): __u8 interesting_frame_index = 0; .. /* in a loop that is unrolled */ if (interesting_frame_index < HTTP2_MAX_FRAMES_ITERATIONS && ..) { frames_array[interesting_frame_index].frame = ..; frames_array[interesting_frame_index].offset = ..; interesting_frame_index++; } The count is in a 64-bit register, and clang masks a copy at every use: 2802: r1 = r6 2803: r1 &= 255 2806: if r1 > 0x1d goto pc+27 2818: r1 = r6 2819: r1 &= 255 2820: r1 <<= 4 2822: r3 += r1 2829: *(u8 *)(r3 +8) = r1 invalid access to map value, value_size=488 off=488 size=1 The states of the unrolled iterations are joined: r6 is [0, 30]. The jump bounds r1. (& r6 255) is a base of its own, so nothing links r1 to r6, and the second copy is [0, 30] again. (& x 0xff) is (zext8 x) and (& x 0xffff) is (zext16 x), as in LLVM's ScalarEvolution: simplify() rewrites them. The base below the extension is r6: r1 == r6 modulo 2^8, and refine_linked_regs() takes two registers that fit in a u8 for equal. The main pass does not change. 'w1 &= 0xff' is (zext32 (zext8 x)): a zero extension of a zero extension that is not wider is dropped. In a prog with an arena the ALU64 insn has a def. There is no link through it, as before. Two tests of verifier_bounds_step.c want a range in '(u8)r6' after a jump on r6. 'if r6 > 0' leaves the constant 0 there now. Test 'r6 > 0xff'. Signed-off-by: Alexei Starovoitov --- kernel/bpf/scev.c | 14 ++++++++++++++ .../selftests/bpf/progs/verifier_bounds_step.c | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/scev.c b/kernel/bpf/scev.c index 991f800b1b15..af437e9286cb 100644 --- a/kernel/bpf/scev.c +++ b/kernel/bpf/scev.c @@ -478,6 +478,11 @@ static u32 op_low_bits(u32 op) return 0; } +static bool is_zext(u32 op) +{ + return op == ZEXT8 || op == ZEXT16 || op == ZEXT32; +} + /* The low @bits bits of the slot are those of @left. The other bits are not known. */ static bool is_spill(struct scev *scev, int id, u32 *bits, u32 *left) { @@ -2714,6 +2719,11 @@ static int simplify(struct scev *scev, u32 id) is_imm(scev, b, &imm1) && imm1 == 32 && !scev->exprs[l].def.set) return expr1(scev, ZEXT32, a); + /* 'r1 &= 0xff' is (u8)r1, 'r1 &= 0xffff' is (u16)r1: (& x 0xff) -> (zext8 x) */ + if (is_binop(scev, BPF_AND, id, &l, &r) && + is_imm(scev, r, &imm1) && (imm1 == 0xff || imm1 == 0xffff)) + return expr1(scev, imm1 == 0xff ? ZEXT8 : ZEXT16, l); + /* (+ (linear base slope) imm) -> (linear (+ base imm) slope) */ if (is_add(scev, id, &l, &r) && is_linear(scev, l, &base, &slope) && @@ -2741,6 +2751,10 @@ static int simplify(struct scev *scev, u32 id) if (!bits) return id; t = scev->exprs[id].params[0]; + /* 'w1 &= 0xff': (zext32 (zext8 $a)) -> (zext8 $a) */ + if (is_zext(op) && is_zext(scev->exprs[t].op) && !scev->exprs[t].def.set && + op_low_bits(scev->exprs[t].op) <= bits) + return t; if (is_low_bits(scev, t, bits, &a)) return expr1(scev, op, a); if (is_add(scev, t, &l, &r) && !scev->exprs[t].def.phi && diff --git a/tools/testing/selftests/bpf/progs/verifier_bounds_step.c b/tools/testing/selftests/bpf/progs/verifier_bounds_step.c index b42717a825c6..2b8ff0f44c36 100644 --- a/tools/testing/selftests/bpf/progs/verifier_bounds_step.c +++ b/tools/testing/selftests/bpf/progs/verifier_bounds_step.c @@ -473,7 +473,7 @@ __naked void step_prune_hit_const_on_line(void) r7 = r0; \ r1 = r6; \ r1 &= 0xff; \ - if r6 > 0 goto l_cur_%=; \ + if r6 > 0xff goto l_cur_%=; \ r1 *= 3; /* step 3 */ \ goto l_join_%=; \ l_cur_%=: \ @@ -523,7 +523,7 @@ __naked void step_prune_miss_const_off_line(void) r7 = r0; \ r1 = r6; \ r1 &= 0xff; \ - if r6 > 0 goto l_cur_%=; \ + if r6 > 0xff goto l_cur_%=; \ r1 *= 3; /* step 3 */ \ goto l_join_%=; \ l_cur_%=: \ -- 2.55.0