fix: ensure eBPF verifier compliance by adding explicit fallback returns after tail calls#16
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the eBPF C code generator to satisfy verifier requirements by emitting an explicit fallback return immediately after any bpf_tail_call() emitted from IRMatchReturn arms, ensuring all control-flow paths terminate even if the tail call fails at runtime.
Changes:
- Added
get_tail_call_fallback_returnto map the current function context (xdp/tc/other) to an appropriate return constant (XDP_PASS/TC_ACT_OK/0). - Updated
IRMatchReturncodegen for both constant and default arms soIRReturnCallandIRReturnTailCallemitreturn <fallback>; /* tail call fallback */afterbpf_tail_call(...). - Added unit tests covering XDP, TC, and generic contexts, and verifying the legacy “continue execution” comment is no longer emitted for these return-position tail calls.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/ebpf_c_codegen.ml |
Adds context-aware fallback return selection and emits explicit fallback returns after tail calls within IRMatchReturn arms. |
tests/test_ebpf_c_codegen.ml |
Adds regression tests validating tail call fallback return emission across XDP/TC/generic contexts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I merged |
Could you rebase? |
6633a82 to
e1ab651
Compare
|
I have finished the rebase. |
|
The standalone IRTailCall instruction (not inside a match arm) still has the old "/* If tail call fails, continue execution */" comment and no fallback return. This is the same bug, just in a different code path. The fix is incomplete. |
…l paths have an explicit return in the event that `bpf_tail_call()` fails. Add relevant tests. Also: fix standalone IRTailCall codegen path — emit a context-aware fallback return after bpf_tail_call() to satisfy the eBPF verifier. Added regression test test_standalone_tail_call_fallback_xdp and included it in the suite. Ran tests/test_ebpf_c_codegen.exe (41 tests) — all passed.
e1ab651 to
9463893
Compare
|
standalone IRTailCall code path has been updated to emit the same context-aware fallback return as the match-arm path, and a regression test was added for the non-match tail-call case. |
Problem Definition
The
bpf_tail_call()helper in eBPF is not guaranteed to succeed. If the call fails (e.g., due to an invalid index or exceeding the call stack limit), execution continues at the instruction immediately following the call site. The eBPF verifier requires that every code path reaches a definitive termination point. Without an explicitreturnafter a tail call, the verifier may reject the program as it cannot guarantee all paths exit.Mechanism of Change
A new helper function
get_tail_call_fallback_returnhas been implemented to determine the correct return value based on the program's execution context. This ensures that the generated C code includes a fallbackreturnstatement immediately following everybpf_tail_call.Context Mapping Logic
The return value is determined by the
current_function_context_typevariable:Some string)"xdp"XDP_PASS"tc"TC_ACT_OKNone/ Other0Technical Breakdown
IRReturnTailCallandIRReturnCall./* tail call fallback */to the emitted return statement for traceability and debugging.IRReturnTailCall: Explicit tail calls with specific indices.IRReturnCall: Generic calls mapped to tail call index 0.IRConstantPatternandIRDefaultPatternwithinIRMatchReturninstructions.Verification Results
The implementation is verified via the following OCaml unit tests:
test_tail_call_fallback_constant_arm_xdp: Confirms that in an XDP context, a constant match arm containing a tail call generatesreturn XDP_PASS;.test_tail_call_fallback_default_arm_tc: Confirms that in a TC context, a default match arm generatesreturn TC_ACT_OK;.test_return_call_fallback_generic_context: Confirms that when the context is undefined, the generator defaults toreturn 0;and correctly handles implicit tail calls (index 0).