fix: ensure eBPF verifier compliance by adding explicit fallback returns after tail calls#16
Open
SiyuanSun0736 wants to merge 1 commit intomultikernel:mainfrom
Open
fix: ensure eBPF verifier compliance by adding explicit fallback returns after tail calls#16SiyuanSun0736 wants to merge 1 commit intomultikernel:mainfrom
SiyuanSun0736 wants to merge 1 commit intomultikernel:mainfrom
Conversation
…l paths have an explicit return in the event that `bpf_tail_call()` fails. Add relevant tests.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).