Skip to contentSkip to conversation

selftests/bpf: add helper retval linked scalar pruning selftest

PATCH In review Zhenzhong Wu 4 participants 7 messages Jun 11 – Jun 14

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: add helper retval linked scalar pruning selftest

Add a verifier runtime test for a branch pattern where a helper return value and a related scalar stay live across the same control-flow sequence. Rust/Aya-generated eBPF can naturally produce this shape when a match on a helper status keeps data derived before the helper call live across the same branches. Such code commonly uses the helper return value in r0, where 0 means success, producing an r0 == 0 / r0 != 0 branch shape.

The test preserves that branch shape but shifts the success value to 1 before branching. Using r0 == 1 / r0 != 1 avoids depending on the verifier's not-equal-zero refinement, so the test exercises linked scalar precision and pruning behavior directly instead of being masked by zero-specific range refinement.

On affected kernels the verifier can explore an impossible path where r0 and r7 are linked by scalar ID, keep the wrong branch, and make the test return 1. With linked scalar precision tracked per instruction, state pruning keeps the real success path, and the test returns 0.

---
 .../selftests/bpf/progs/verifier_scalar_ids.c | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +35 -0
index 70ae14d60..de71d547f 100644
@@ -448,6 +448,41 @@ __naked void linked_regs_broken_link_2(void)
 	: __clobber_all);
 }
 
+SEC("tc")
+__description("helper retval linked scalar pruning")
+__success __retval(0)
+__naked void helper_retval_linked_scalar_pruning(void)
+{
+	asm volatile (
+	"r7 = *(u32 *)(r1 + %[__sk_buff_data_end]);"
+	"r5 = *(u32 *)(r1 + %[__sk_buff_data]);"
+	"r7 -= r5;"
+	"r2 = 0;"
+	"r3 = r10;"
+	"r3 += -8;"
+	"r4 = 1;"
+	"call %[bpf_skb_load_bytes];"
+	"r0 += 1;"
+	"r6 = 1;"
+	/* success path keeps r7 independent; failure path links r7 to r0. */
+	"if r0 == 1 goto l0_%=;"
+	"r7 = r0;"
+"l0_%=: if r0 != 1 goto l1_%=;"
+	"r7 <<= 32;"
+	"r7 >>= 32;"
+	"if r7 != %[test_data_len] goto l1_%=;"
+	"r0 = 0;"
+	"exit;"
+"l1_%=: r0 = r6;"
+	"exit;"
+	:
+	: __imm(bpf_skb_load_bytes),
+	  __imm_const(__sk_buff_data, offsetof(struct __sk_buff, data)),
+	  __imm_const(__sk_buff_data_end, offsetof(struct __sk_buff, data_end)),
+	  __imm_const(test_data_len, TEST_DATA_LEN)
+	: __clobber_all);
+}
+
 /* Check that mark_chain_precision() for one of the conditional jump
  * operands does not trigger equal scalars precision propagation.
  */

base-commit: 30dee2c176e7954f63d1fa3e52d172f30beb9bfb

Signature · 1 line
2.43.0
#1 lore raw
Quoted 41 lines · Zhenzhong Wu

Add a verifier runtime test for a branch pattern where a helper return value and a related scalar stay live across the same control-flow sequence. Rust/Aya-generated eBPF can naturally produce this shape when a match on a helper status keeps data derived before the helper call live across the same branches. Such code commonly uses the helper return value in r0, where 0 means success, producing an r0 == 0 / r0 != 0 branch shape.

The test preserves that branch shape but shifts the success value to 1 before branching. Using r0 == 1 / r0 != 1 avoids depending on the verifier's not-equal-zero refinement, so the test exercises linked scalar precision and pruning behavior directly instead of being masked by zero-specific range refinement.

On affected kernels the verifier can explore an impossible path where r0 and r7 are linked by scalar ID, keep the wrong branch, and make the test return 1. With linked scalar precision tracked per instruction, state pruning keeps the real success path, and the test returns 0.

---
 .../selftests/bpf/progs/verifier_scalar_ids.c | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +8 -0
index 70ae14d60..de71d547f 100644
@@ -448,6 +448,41 @@ __naked void linked_regs_broken_link_2(void)
 	: __clobber_all);
 }
 
+SEC("tc")
+__description("helper retval linked scalar pruning")
+__success __retval(0)
+__naked void helper_retval_linked_scalar_pruning(void)
+{
+	asm volatile (
+	"r7 = *(u32 *)(r1 + %[__sk_buff_data_end]);"
+	"r5 = *(u32 *)(r1 + %[__sk_buff_data]);"

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c __naked void linked_regs_broken_link_2(void) · line 468

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +10 -0
+	"r7 -= r5;"
+	"r2 = 0;"
+	"r3 = r10;"
+	"r3 += -8;"
+	"r4 = 1;"
+	"call %[bpf_skb_load_bytes];"
+	"r0 += 1;"
+	"r6 = 1;"
+	/* success path keeps r7 independent; failure path links r7 to r0. */
+	"if r0 == 1 goto l0_%=;"

this exercises linked registers with BPF_ADD_CONST logic. We already have such tests. Why do we need this one? How is it different?

pw-bot: cr

#2 lore raw
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Zhenzhong Wu

Add a verifier runtime test for a branch pattern where a helper return value and a related scalar stay live across the same control-flow sequence. Rust/Aya-generated eBPF can naturally produce this shape when a match on a helper status keeps data derived before the helper call live across the same branches. Such code commonly uses the helper return value in r0, where 0 means success, producing an r0 == 0 / r0 != 0 branch shape.

[...]

Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +14 -0
+SEC("tc")
+__description("helper retval linked scalar pruning")
+__success __retval(0)
+__naked void helper_retval_linked_scalar_pruning(void)
+{
+	asm volatile (
+	"r7 = *(u32 *)(r1 + %[__sk_buff_data_end]);"
+	"r5 = *(u32 *)(r1 + %[__sk_buff_data]);"
+	"r7 -= r5;"
+	"r2 = 0;"
+	"r3 = r10;"
+	"r3 += -8;"
+	"r4 = 1;"
+	"call %[bpf_skb_load_bytes];"
Quoted 8 lines

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c __naked void linked_regs_broken_link_2(void) · line 468

Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +4 -0
+	"r0 += 1;"
+	"r6 = 1;"
+	/* success path keeps r7 independent; failure path links r7 to r0. */
+	"if r0 == 1 goto l0_%=;"

this exercises linked registers with BPF_ADD_CONST logic. We already have such tests. Why do we need this one? How is it different?

BPF_ADD_CONST wasn't what was meant to be tested.

The main logic is r7.id == r0.id only happens on "if r0 == 1 goto l0_%=" fall through, and does not have such link otherwise. I only check tests added in commit c0087d59e504 ("selftests/bpf: tests for per-insn sync_linked_regs() precision tracking"), but it doesn't seem like such conditional linking was tested.

The other rational is that this seem like a common pattern that is genereated from Rust-based BPF program.

Quoted 3 lines

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c __naked void linked_regs_broken_link_2(void) · line 469

Quoted 3 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +3 -0
+	/* success path keeps r7 independent; failure path links r7 to r0. */
+	"if r0 == 1 goto l0_%=;"
+	"r7 = r0;"

^^^^^^^ conditional scalar linking

Quoted 15 lines
Quoted 15 lines

+"l0_%=: if r0 != 1 goto l1_%=;"

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +14 -0
+	"r7 <<= 32;"
+	"r7 >>= 32;"
+	"if r7 != %[test_data_len] goto l1_%=;"
+	"r0 = 0;"
+	"exit;"
+"l1_%=: r0 = r6;"
+	"exit;"
+	:
+	: __imm(bpf_skb_load_bytes),
+	  __imm_const(__sk_buff_data, offsetof(struct __sk_buff, data)),
+	  __imm_const(__sk_buff_data_end, offsetof(struct __sk_buff, data_end)),
+	  __imm_const(test_data_len, TEST_DATA_LEN)
+	: __clobber_all);
+}

[...]

#3 lore raw
Quoted 41 lines · Shung-Hsi Yu
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Zhenzhong Wu

Add a verifier runtime test for a branch pattern where a helper return value and a related scalar stay live across the same control-flow sequence. Rust/Aya-generated eBPF can naturally produce this shape when a match on a helper status keeps data derived before the helper call live across the same branches. Such code commonly uses the helper return value in r0, where 0 means success, producing an r0 == 0 / r0 != 0 branch shape.

[...]

Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +14 -0
+SEC("tc")
+__description("helper retval linked scalar pruning")
+__success __retval(0)
+__naked void helper_retval_linked_scalar_pruning(void)
+{
+	asm volatile (
+	"r7 = *(u32 *)(r1 + %[__sk_buff_data_end]);"
+	"r5 = *(u32 *)(r1 + %[__sk_buff_data]);"
+	"r7 -= r5;"
+	"r2 = 0;"
+	"r3 = r10;"
+	"r3 += -8;"
+	"r4 = 1;"
+	"call %[bpf_skb_load_bytes];"
Quoted 8 lines

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c __naked void linked_regs_broken_link_2(void) · line 468

Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +4 -0
+	"r0 += 1;"
+	"r6 = 1;"
+	/* success path keeps r7 independent; failure path links r7 to r0. */
+	"if r0 == 1 goto l0_%=;"

this exercises linked registers with BPF_ADD_CONST logic. We already have such tests. Why do we need this one? How is it different?

BPF_ADD_CONST wasn't what was meant to be tested.

The main logic is r7.id == r0.id only happens on "if r0 == 1 goto l0_%=" fall through, and does not have such link otherwise. I only check tests added in commit c0087d59e504 ("selftests/bpf: tests for per-insn sync_linked_regs() precision tracking"), but it doesn't seem like such conditional linking was tested.

Quoted 7 lines

The other rational is that this seem like a common pattern that is genereated from Rust-based BPF program.

Quoted 3 lines

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c __naked void linked_regs_broken_link_2(void) · line 469

Quoted 3 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +3 -0
+	/* success path keeps r7 independent; failure path links r7 to r0. */
+	"if r0 == 1 goto l0_%=;"
+	"r7 = r0;"

^^^^^^^ conditional scalar linking

Fine, it's a regular register linking without BPF_ADD_CONST. Still the question remains. I believe:
"We already have such tests. Why do we need this one? How is it different?"

#4 lore raw

Let me add one more data point beyond Shung-Hsi's explanation.

I first tried running the current bpf-next verifier_scalar_ids tests on a 6.6.y kernel as a rough filter. I then realized that the result was not a clean comparison, because many of those tests validate verifier log strings through __msg(), and verifier logs are not stable across kernel versions. So I did not use the pass/fail result as evidence; I only used it to pick the closest existing tests for source-level review.

The closest candidates I looked at were:

precision_two_ids
linked_regs_broken_link_2
linked_regs_too_many_regs
two_nil_old_ids_one_cur_id
linked_regs_and_subreg_def

Some of them are pruning-related and some cover linked-register precision, but I still do not see one that covers the same helper-status/r7 conditional-link pruning scenario as this selftest.

So my current understanding is that the selftest adds distinct coverage. If this still does not address the concern, I am fine with dropping this selftest patch.

Quoted 55 lines · Alexei Starovoitov
Quoted 41 lines · Shung-Hsi Yu
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Zhenzhong Wu

Add a verifier runtime test for a branch pattern where a helper return value and a related scalar stay live across the same control-flow sequence. Rust/Aya-generated eBPF can naturally produce this shape when a match on a helper status keeps data derived before the helper call live across the same branches. Such code commonly uses the helper return value in r0, where 0 means success, producing an r0 == 0 / r0 != 0 branch shape.

[...]

Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +14 -0
+SEC("tc")
+__description("helper retval linked scalar pruning")
+__success __retval(0)
+__naked void helper_retval_linked_scalar_pruning(void)
+{
+  asm volatile (
+  "r7 = *(u32 *)(r1 + %[__sk_buff_data_end]);"
+  "r5 = *(u32 *)(r1 + %[__sk_buff_data]);"
+  "r7 -= r5;"
+  "r2 = 0;"
+  "r3 = r10;"
+  "r3 += -8;"
+  "r4 = 1;"
+  "call %[bpf_skb_load_bytes];"
Quoted 8 lines

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c __naked void linked_regs_broken_link_2(void) · line 468

Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +4 -0
+  "r0 += 1;"
+  "r6 = 1;"
+  /* success path keeps r7 independent; failure path links r7 to r0. */
+  "if r0 == 1 goto l0_%=;"

this exercises linked registers with BPF_ADD_CONST logic. We already have such tests. Why do we need this one? How is it different?

BPF_ADD_CONST wasn't what was meant to be tested.

The main logic is r7.id == r0.id only happens on "if r0 == 1 goto l0_%=" fall through, and does not have such link otherwise. I only check tests added in commit c0087d59e504 ("selftests/bpf: tests for per-insn sync_linked_regs() precision tracking"), but it doesn't seem like such conditional linking was tested.

Quoted 7 lines

The other rational is that this seem like a common pattern that is genereated from Rust-based BPF program.

Quoted 3 lines

tools/testing/selftests/bpf/progs/verifier_scalar_ids.c __naked void linked_regs_broken_link_2(void) · line 469

Quoted 3 lines
tools/testing/selftests/bpf/progs/verifier_scalar_ids.c +3 -0
+  /* success path keeps r7 independent; failure path links r7 to r0. */
+  "if r0 == 1 goto l0_%=;"
+  "r7 = r0;"

^^^^^^^ conditional scalar linking

Fine, it's a regular register linking without BPF_ADD_CONST. Still the question remains. I believe:
"We already have such tests. Why do we need this one? How is it different?"

#5 lore raw
In reply to Zhenzhong Wu
This adds a regression test for a verifier state-pruning issue with scalars linked by scalar ID, but there is no Fixes: tag.
Quoted 16 lines

selftests/bpf: add helper retval linked scalar pruning selftest

Add a verifier runtime test for a branch pattern where a helper return value and a related scalar stay live across the same control-flow sequence. Rust/Aya-generated eBPF can naturally produce this shape when a match on a helper status keeps data derived before the helper call live across the same branches. Such code commonly uses the helper return value in r0, where 0 means success, producing an r0 == 0 / r0 != 0 branch shape.

The test preserves that branch shape but shifts the success value to 1 before branching. Using r0 == 1 / r0 != 1 avoids depending on the verifier's not-equal-zero refinement, so the test exercises linked scalar precision and pruning behavior directly instead of being masked by zero-specific range refinement.

Quoted 7 lines

On affected kernels the verifier can explore an impossible path where r0 and r7 are linked by scalar ID, keep the wrong branch, and make the test return 1. With linked scalar precision tracked per instruction, state pruning keeps the real success path, and the test returns 0.

This adds a regression test for a verifier state-pruning issue with scalars linked by scalar ID, but there is no Fixes: tag.

The test structure (success path keeps r7 independent with id=0, failure path links r7 to r0) matches the "registers with id=0 in cached state vs linked in current state" pruning scenario that was addressed by changing check_scalar_ids() and regsafe() to treat rold->id == 0 as independent.

Should this carry:

Fixes b0388bafa494 ("bpf: Relax scalar id equivalence for state pruning")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27361218656

#6 lore raw

Thanks for taking a look.

I used r0 += 1 only to shift the helper success value from 0 to 1, so this test is not masked by the verifier's not-equal-zero refinement. In this reproducer, r0 += 1 does not enter the internal BPF_ADD_CONST linked-scalar path.

The helper returns an integer with R0 id=0, r0 += 1 keeps R0 id=0, and the r0/r7 scalar-id relation is established later by the r7 = r0 move on the failure path.

The success path skips that move and keeps r7 independent. The test is intended to cover pruning between those two different state shapes, not ADD_CONST delta propagation.

Separately, I don't think the Fixes tag suggested by the bot is appropriate. I do not have evidence that b0388bafa494 introduced this issue. I can reproduce the issue on v5.10.258, v5.15.209, v6.1.91 and v6.6.142. The upstream change that fixes this state shape is the later linked-scalar precision tracking change:

4bf79f9be434
("bpf: Track equal scalars history on per-instruction level")

More background is in the v2 thread:

https://lore.kernel.org/r/[email protected]/

BR,
Zhenzhong

Quoted 43 lines · On Fri, Jun 12, 2026 at 12:58 AM <[email protected]> wrote:
Quoted 17 lines

selftests/bpf: add helper retval linked scalar pruning selftest

Add a verifier runtime test for a branch pattern where a helper return value and a related scalar stay live across the same control-flow sequence. Rust/Aya-generated eBPF can naturally produce this shape when a match on a helper status keeps data derived before the helper call live across the same branches. Such code commonly uses the helper return value in r0, where 0 means success, producing an r0 == 0 / r0 != 0 branch shape.

The test preserves that branch shape but shifts the success value to 1 before branching. Using r0 == 1 / r0 != 1 avoids depending on the verifier's not-equal-zero refinement, so the test exercises linked scalar precision and pruning behavior directly instead of being masked by zero-specific range refinement.

Quoted 7 lines

On affected kernels the verifier can explore an impossible path where r0 and r7 are linked by scalar ID, keep the wrong branch, and make the test return 1. With linked scalar precision tracked per instruction, state pruning keeps the real success path, and the test returns 0.

This adds a regression test for a verifier state-pruning issue with scalars linked by scalar ID, but there is no Fixes: tag.

The test structure (success path keeps r7 independent with id=0, failure path links r7 to r0) matches the "registers with id=0 in cached state vs linked in current state" pruning scenario that was addressed by changing check_scalar_ids() and regsafe() to treat rold->id == 0 as independent.

Should this carry:

Fixes b0388bafa494 ("bpf: Relax scalar id equivalence for state pruning")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27361218656