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 local function calls with stack usage #784

Merged
merged 4 commits into from
Nov 9, 2024

Conversation

dthaler
Copy link
Contributor

@dthaler dthaler commented Nov 9, 2024

Turns out clang/gcc generate code that depends on r10 being updated by the platform across function calls. This PR corrects the verifier to have the expected behavior and tests it with an actual sample ELF generated by clang.

Summary by CodeRabbit

  • New Features

    • Updated test cases for local function calls, recursion, and stack manipulation, enhancing error handling and state preservation.
    • Added new test cases for subtraction operations involving various types of registers and pointers, improving error messaging and validation.
  • Bug Fixes

    • Improved handling of stack and memory operations in the ebpf_domain_t class, simplifying access checks and ensuring correct memory management.
  • Refactor

    • Simplified method signatures by removing unnecessary parameters, enhancing clarity and usability.
  • Documentation

    • Updated error messages in test cases for clarity, particularly regarding stack access and subtraction operations.

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

coderabbitai bot commented Nov 9, 2024

Walkthrough

The changes in this pull request involve updates to the ebpf-samples subproject commit reference, modifications to the return logic of functions in src/asm_ostream.hpp, changes to the return type in src/asm_syntax.hpp, and refinements in the ebpf_domain_t class methods in src/crab/ebpf_domain.cpp and src/crab/ebpf_domain.hpp. Additionally, new test cases related to local function calls and stack manipulation have been added in test-data/calllocal.yaml.

Changes

File Change Summary
ebpf-samples Subproject commit updated: 325cce1bc528a8b70e02ea914d407b4e4f89731d33f01fafa414520ad56ab219db343422bb655b32
src/asm_ostream.hpp Modified label_to_offset16 and label_to_offset32 functions to use conditional expressions for return values.
src/asm_syntax.hpp Updated call_stack_depth method to explicitly use gsl::narrow<int> for return type instead of implicit conversion.
src/crab/ebpf_domain.cpp Simplified check_access_stack method by removing call_stack_depth parameter; adjusted stack access logic and updated do_load_stack and do_store_stack methods.
src/crab/ebpf_domain.hpp Updated check_access_stack method signature to remove call_stack_depth parameter.
test-data/calllocal.yaml Added multiple test cases for local function calls, recursion, and stack manipulation, including handling of nested calls and error scenarios for excessive recursion.
test-data/stack.yaml Updated error message for invalid stack access to clarify conditions for valid access.
test-data/subtract.yaml Added and modified test cases related to subtraction operations, including new conditions and error messages for pointer types and bounds.

Possibly related PRs

🐇 In the land of code, where rabbits hop,
Changes abound, and we never stop.
From stacks to calls, we refine with care,
Each test a tale, in the code we share.
With every update, our project grows bright,
Hopping along, in the soft moonlight! 🌙✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 3f58c29 and ed32a06.

📒 Files selected for processing (3)
  • ebpf-samples (1 hunks)
  • test-data/stack.yaml (1 hunks)
  • test-data/subtract.yaml (1 hunks)
🔇 Additional comments (3)
ebpf-samples (1)

1-1: Verify dependency and commit existence.

The subproject commit update aligns with the PR objectives to include test cases for bpf2bpf local function calls.

Let's verify the dependency and commit:

test-data/subtract.yaml (1)

162-162: LGTM! The new error message enhances stack bounds validation.

The added error message properly complements the existing upper bound check by ensuring the lower bound of stack access stays within the subprogram's stack frame. This aligns well with the PR's objective of fixing stack usage in bpf2bpf local function calls.

Let's verify the consistency of stack size constants across the codebase:

✅ Verification successful

Stack size constants and bound checks are consistently defined and used across the codebase

The verification shows that:

  • EBPF_SUBPROGRAM_STACK_SIZE is defined as 512 bytes in src/ebpf_base.h
  • The error message in test-data/subtract.yaml matches the implementation in src/crab/ebpf_domain.cpp
  • Stack bound checks are consistently enforced:
    • Lower bound: r10.stack_offset - EBPF_SUBPROGRAM_STACK_SIZE
    • Upper bound: EBPF_TOTAL_STACK_SIZE
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify stack size constants are consistently defined and used
# Expected: Find definitions of EBPF_TOTAL_STACK_SIZE and EBPF_SUBPROGRAM_STACK_SIZE
# and their usage in stack bound checks

# Search for stack size constant definitions
echo "=== Stack size constant definitions ==="
rg "EBPF_(TOTAL|SUBPROGRAM)_STACK_SIZE"

# Search for stack bound checking patterns
echo "=== Stack bound checking patterns ==="
rg "r10\.stack_offset.*EBPF.*STACK_SIZE"

Length of output: 5237

test-data/stack.yaml (1)

597-597: LGTM! Error message simplified to better reflect platform behavior.

The updated error message now directly references EBPF_SUBPROGRAM_STACK_SIZE instead of including call stack depth in the calculation. This change aligns well with the PR's objective of modifying the verifier to match platform behavior regarding r10 register updates during function calls.


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.

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

Coverage Status

coverage: 90.532% (+0.002%) from 90.53%
when pulling ed32a06 on dthaler:bpf2bpf-stack
into 71a34c7 on vbpf:main.

@elazarg elazarg merged commit 41fbd5a into vbpf:main Nov 9, 2024
19 checks passed
@dthaler dthaler deleted the bpf2bpf-stack branch November 9, 2024 20:43
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