Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bpf2bpf bug when multiple branches in a subprogram go to exit #715

Merged
merged 2 commits into from
Oct 5, 2024

Conversation

dthaler
Copy link
Contributor

@dthaler dthaler commented Oct 4, 2024

The bug was that the successor label could be processed multiple times if there were multiple predecessors, resulting in multiple exit instructions appearing in the block, which in turn would result in restore_callee_saved_registers() being called multiple times, giving incorrect results.

Added a YAML test case for this.

Summary by CodeRabbit

  • New Features

    • Enhanced control-flow graph (CFG) processing for improved label management and error handling.
    • Added new test cases to validate function call behavior, including register preservation and recursion limits.
  • Bug Fixes

    • Improved error messages for specific failure scenarios in function calls and recursion.
  • Tests

    • Introduced comprehensive test cases for local function calls, including checks for tail calls and infinite recursion.

The bug was that the successor label could be processed multiple
times if there were multiple predecessors, resulting in multiple
exit instructions appearing in the block, which in turn would
result in restore_callee_saved_registers() being called multiple
times, giving incorrect results.

Added a YAML test case for this.

Signed-off-by: Dave Thaler <[email protected]>
Copy link

coderabbitai bot commented Oct 4, 2024

Walkthrough

The pull request introduces significant modifications to the src/asm_cfg.cpp file, focusing on the control-flow graph (CFG) node handling and label management. Key changes include the conversion of data structures from queues to sets for label processing, adjustments in label handling logic, and enhanced error handling for recursion. Additionally, new test cases have been added in test-data/calllocal.yaml to validate function call behavior, recursion limits, and register preservation, ensuring comprehensive testing of the updated functionality.

Changes

File Change Summary
src/asm_cfg.cpp - Changed macro_labels and seen_labels from std::queue to std::set.
- Modified label processing logic to access and remove labels using set operations.
- Updated insertion logic to check for existing labels before adding.
- Clarified error handling for illegal recursion.
- Adjusted control flow in add_cfg_nodes function.
test-data/calllocal.yaml - Added new test cases for register preservation, multiple branches, disallowed tail calls, and infinite recursion scenarios.
- Updated existing test cases with additional checks for R6 register values.
- Introduced new error messages for recursion limits and illegal recursion.

Possibly related PRs

Poem

In the land of code where rabbits play,
Labels hop and dance all day.
With sets and checks, we tidy the flow,
Preserving registers, watch them glow!
Recursion's limits, we now embrace,
In our CFG, we find our place! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range comments (3)
test-data/calllocal.yaml (3)

Line range hint 230-246: LGTM: Important test for disallowing tail calls in subprograms.

This test case effectively verifies that tail calls are not allowed within subprograms, which is crucial for maintaining the integrity of the call stack.

Consider enhancing the post-conditions to check the states of other registers (R1, R2, R3) to ensure they remain unchanged after the disallowed tail call attempt.


Line range hint 258-271: LGTM: Important test for preventing infinite recursion between subprograms.

This test case effectively verifies that infinite recursion between two subprograms is detected and prevented, which is crucial for avoiding stack overflow issues in more complex scenarios. The error message is clear and accurately describes the problem.

Consider adding a test case with a finite number of allowed recursive calls to ensure that legitimate recursive patterns are still permitted.


Line range hint 206-271: Overall assessment: Comprehensive enhancement of test coverage.

The new test cases significantly improve the robustness of the eBPF verifier by covering various edge cases and potential issues:

  1. Register preservation in subroutine calls
  2. Multiple branches within subprograms
  3. Disallowing tail calls in subprograms
  4. Prevention of infinite recursion (both self-recursion and between subprograms)

While not all test cases directly address the stated PR objective of fixing multiple branches in subprograms, they collectively contribute to a more thorough verification process. These additions will help ensure the correctness and safety of eBPF programs in various scenarios.

Consider adding more test cases that specifically target the interaction between multiple branches and register preservation to further address the main PR objective.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 00683a5 and 132da6d.

📒 Files selected for processing (2)
  • src/asm_cfg.cpp (2 hunks)
  • test-data/calllocal.yaml (1 hunks)
🔇 Additional comments (3)
test-data/calllocal.yaml (2)

206-213: LGTM: New test case for register preservation.

This test case effectively verifies that the callee-saved register R6 is preserved after a subroutine call. It aligns well with the PR objective of ensuring correct register handling in subroutine calls.


Line range hint 247-257: LGTM: Crucial test for preventing infinite self-recursion.

This test case effectively verifies that infinite self-recursion is detected and prevented, which is essential for avoiding stack overflow issues. The error message is clear and accurately describes the problem.

src/asm_cfg.cpp (1)

62-66: ⚠️ Potential issue

Potential impact of changing macro_labels from std::queue to std::set

The change from using a std::queue<label_t> to a std::set<label_t> for macro_labels (lines 62-66) alters the data structure managing pending labels. Unlike a queue, a set does not preserve insertion order, which could affect the order in which labels are processed. If the processing order is significant for the correctness of CFG construction or for performance reasons, this change might introduce unintended behavior.

To verify whether the order of label processing impacts the CFG construction, consider reviewing test cases that depend on specific traversal orders or add new tests to check for order-sensitive behavior.

test-data/calllocal.yaml Show resolved Hide resolved
src/asm_cfg.cpp Show resolved Hide resolved
src/asm_cfg.cpp Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Oct 4, 2024

Coverage Status

coverage: 90.291% (-0.1%) from 90.394%
when pulling 42d9a8e on dthaler:bpf2bpf-branches
into 00683a5 on vbpf:main.

Signed-off-by: Elazar Gershuni <[email protected]>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 132da6d and 42d9a8e.

📒 Files selected for processing (1)
  • src/asm_cfg.cpp (4 hunks)
🔇 Additional comments (3)
src/asm_cfg.cpp (3)

107-109: Logic for label tracking appears correct

The code correctly checks for the existence of next_macro_label in macro_labels before inserting it. The use of seen_labels ensures that each label is processed only once.


61-65: ⚠️ Potential issue

Potential issue with processing order due to change from std::queue to std::set

The change from std::queue to std::set for macro_labels changes the processing order from FIFO (First-In-First-Out) to an order determined by the set's sorting criteria. If the processing logic depends on the order in which labels are processed, this alteration could lead to unexpected behavior.

Consider verifying whether the processing order of macro_labels affects the correctness of the CFG construction. If order is significant, you might need to use an ordered container that maintains insertion order, like std::queue or std::deque, while still allowing efficient existence checks.


253-253: ⚠️ Potential issue

Ensure the basic block is not empty before dereferencing

When accessing *bb.rbegin(), there's an implicit assumption that bb is not empty. Dereferencing an iterator from an empty container leads to undefined behavior.

Consider adding a check to ensure that bb is not empty before dereferencing:

if (!bb.empty()) {
    auto jmp = std::get<Jmp>(*bb.rbegin());
    // Proceed with logic
} else {
    // Handle the empty case if necessary
}

Alternatively, ensure through program logic that bb cannot be empty at this point. If bb is guaranteed to be non-empty, a comment explaining this assumption can enhance code readability.

src/asm_cfg.cpp Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants