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

Fixes for multiple bpf2bpf stack levels #801

Merged
merged 6 commits into from
Nov 24, 2024

Conversation

dthaler
Copy link
Contributor

@dthaler dthaler commented Nov 23, 2024

  • Make read_elf() with a desired_section work correctly, including updating the BTF line_info
  • Update test

Fixes #792

Summary by CodeRabbit

  • New Features

    • Enhanced handling of subprograms and ELF file parsing for improved efficiency.
    • Added a new member variable to track the resolution state of subprograms.
  • Bug Fixes

    • Refined error handling for unresolved symbols and ELF section parsing.
  • Tests

    • Updated test cases to reflect changes in eBPF program behavior, including new tests for additional valid subprograms.
    • Removed a test case that is no longer relevant.

Copy link

coderabbitai bot commented Nov 23, 2024

Walkthrough

The pull request includes updates to the ebpf-samples subproject commit reference and significant modifications to the src/asm_files.cpp file, including changes in method signatures and logic for handling subprograms and ELF file parsing. Additionally, a new member variable is introduced in the raw_program struct in src/spec_type_descriptors.hpp to track resolved subprograms. The test cases in src/test/test_verify.cpp have been updated to reflect changes in expected behavior and to enhance test coverage for eBPF programs.

Changes

File Change Summary
ebpf-samples Updated subproject commit from 33f01fafa414520ad56ab219db343422bb655b32 to 69a84e10ca1d838d8c0f948b5459bce4965ab7af.
src/asm_files.cpp Replaced read_subprogram with find_subprogram, modified append_subprograms, and updated read_elf method signatures and logic. Enhanced error handling for ELF parsing.
src/spec_type_descriptors.hpp Added bool resolved_subprograms member variable to raw_program struct.
src/test/test_verify.cpp Removed and added test cases for bpf2bpf.o, updating expected behavior and enhancing test coverage.

Assessment against linked issues

Objective Addressed Explanation
BTF loader code assumes 1 program per section (#792) The changes do not address the specific issue related to line_info.size() and prog.size() mismatch.

Possibly related PRs


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 7313c6a and ff109b2.

📒 Files selected for processing (1)
  • src/asm_files.cpp (7 hunks)
🧰 Additional context used
📓 Learnings (1)
src/asm_files.cpp (3)
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:518-522
Timestamp: 2024-11-24T00:30:54.206Z
Learning: In the `read_elf()` function in `src/asm_files.cpp`, the vector `res` is guaranteed to be non-empty before entering the loop that filters programs based on `desired_section`. Therefore, `res.size()` is always greater than zero when iterating in reverse.
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:483-483
Timestamp: 2024-11-23T17:05:19.380Z
Learning: In `src/asm_files.cpp`, when `program.info.line_info` is an `std::map`, the existing logic in the loop copying line info over `program.prog.size()` is correct and does not require additional resizing or checks.
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:483-483
Timestamp: 2024-11-23T17:02:37.673Z
Learning: In `src/asm_files.cpp`, the `program.info.line_info` is an `std::map`, which does not have a `resize()` method.
🔇 Additional comments (6)
src/asm_files.cpp (6)

208-217: find_subprogram implemented correctly

The function appropriately searches for a subprogram by name and section, returning a pointer to the subprogram if found.


Line range hint 220-281: Effective handling of subprograms in append_subprograms

The logic for appending subprograms is correctly implemented, ensuring that subprograms are resolved only once and handling recursion properly.


261-263: Correct merging of line_info from subprograms

Assigning subprogram->info.line_info[i] to prog.info.line_info[prog.info.line_info.size()] works correctly since instruction indices are sequentially increasing. The code effectively merges the line information from the subprogram into the main program.


491-491: Appropriate line information propagation

The logic to propagate line information when only the first instruction per source line has BTF data is valid. Copying the previous line info when the current instruction lacks it ensures that all instructions have associated line information.


500-512: Correctly appending subprograms in read_elf

The code correctly appends subprograms to the calling programs, ensuring the programs are fully verifiable and instruction offsets are properly adjusted.


516-521: Safe reverse iteration over res

The reverse iteration safely filters programs based on the desired section. The check !res.empty() ensures that res.size() is greater than zero, preventing underflow.


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: 5

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between e4750ee and 4ddc74a.

📒 Files selected for processing (4)
  • ebpf-samples (1 hunks)
  • src/asm_files.cpp (4 hunks)
  • src/spec_type_descriptors.hpp (1 hunks)
  • src/test/test_verify.cpp (1 hunks)
🔇 Additional comments (4)
ebpf-samples (1)

1-1: Verify the submodule commit and its contents

✅ Verification successful

Submodule update contains required test changes for BTF loader

The commit d8ae598 updates bpf2bpf.c to test multiple stack levels by:

  • Adding a new add2 function that creates additional stack frames
  • Modifying the test to call through multiple stack levels
  • Changing the expected result to account for multiple function calls

This change aligns with the PR objective of fixing BTF loader code issues with multiple programs per section, as it provides a test case with deeper stack manipulation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the submodule commit exists and contains required test changes
# Expected results: 
# 1. Commit should exist and be accessible
# 2. Changes should include test updates for BTF loader fixes

# Check if commit exists
git ls-remote https://github.com/vbpf/ebpf-samples.git d8ae598573421b336260502459b6e9e164e75eb3

# Look for BTF-related test changes in commit
gh api repos/vbpf/ebpf-samples/commits/d8ae598573421b336260502459b6e9e164e75eb3 | jq -r '.commit.message'

# Search for BTF-related files in the commit
gh api repos/vbpf/ebpf-samples/commits/d8ae598573421b336260502459b6e9e164e75eb3 | jq -r '.files[].filename' | grep -i "btf"

Length of output: 433


Script:

#!/bin/bash
# Let's check the actual files changed in the commit
gh api repos/vbpf/ebpf-samples/commits/d8ae598573421b336260502459b6e9e164e75eb3 | jq -r '.files[].filename'

# Get the patch to see the actual changes
gh api repos/vbpf/ebpf-samples/commits/d8ae598573421b336260502459b6e9e164e75eb3 | jq -r '.files[].patch'

Length of output: 1155

src/spec_type_descriptors.hpp (1)

73-73: Verify thread-safety with resolved_subprograms flag.

The presence of thread_local_program_info suggests concurrent usage. Need to verify thread-safety of subprogram resolution.

src/test/test_verify.cpp (2)

513-513: LGTM!

The test case for "func" remains unchanged as expected.


511-512: Test cases updated to handle multiple programs per section.

The changes reflect the fix for issue #792, where the test now correctly expects two programs in the .text section instead of one.

Run the following script to verify the test changes align with the actual program changes:

Make read_elf() with a desired_section work correctly

Fixes vbpf#792

Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: Dave Thaler <[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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between 903421e and 2c5c1b7.

📒 Files selected for processing (2)
  • src/asm_files.cpp (4 hunks)
  • src/spec_type_descriptors.hpp (1 hunks)
🧰 Additional context used
📓 Learnings (1)
src/asm_files.cpp (4)
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:483-483
Timestamp: 2024-11-23T17:05:19.380Z
Learning: In `src/asm_files.cpp`, when `program.info.line_info` is an `std::map`, the existing logic in the loop copying line info over `program.prog.size()` is correct and does not require additional resizing or checks.
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:483-483
Timestamp: 2024-11-23T17:02:37.673Z
Learning: In `src/asm_files.cpp`, the `program.info.line_info` is an `std::map`, which does not have a `resize()` method.
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:258-260
Timestamp: 2024-11-23T17:03:10.573Z
Learning: `prog.info.line_info` is an `std::map`, so when merging line information entries, use the `insert` method to add entries from `subprogram.info.line_info`.
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:208-213
Timestamp: 2024-11-23T16:59:47.517Z
Learning: In the `ebpf-verifier` codebase, the number of programs is generally very small, so optimizing the `find_subprogram` function with a map is unnecessary.
🔇 Additional comments (1)
src/spec_type_descriptors.hpp (1)

73-76: Verify the usage of resolved_subprograms flag.

The flag addition is correct and properly initialized. Let's verify its usage across the codebase.

✅ Verification successful

The resolved_subprograms flag is correctly used to prevent duplicate subprogram resolution

The flag is checked and set in src/asm_files.cpp before appending subprograms:

  • Guards against duplicate resolution with an early return if already resolved
  • Sets the flag to true before proceeding with subprogram resolution
  • Used in conjunction with append_subprograms function calls for proper control flow
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of resolved_subprograms flag to ensure it's properly checked and set.

# Find all references to resolved_subprograms
rg -A 5 'resolved_subprograms'

# Find potential subprogram resolution logic
ast-grep --pattern 'append_subprograms($$$)'

Length of output: 1283

Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: Dave Thaler <[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

Reviewing files that changed from the base of the PR and between 78a6762 and 7313c6a.

📒 Files selected for processing (1)
  • src/asm_files.cpp (7 hunks)
🧰 Additional context used
📓 Learnings (1)
src/asm_files.cpp (2)
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:483-483
Timestamp: 2024-11-23T17:05:19.380Z
Learning: In `src/asm_files.cpp`, when `program.info.line_info` is an `std::map`, the existing logic in the loop copying line info over `program.prog.size()` is correct and does not require additional resizing or checks.
Learnt from: dthaler
PR: vbpf/ebpf-verifier#801
File: src/asm_files.cpp:483-483
Timestamp: 2024-11-23T17:02:37.673Z
Learning: In `src/asm_files.cpp`, the `program.info.line_info` is an `std::map`, which does not have a `resize()` method.
🔇 Additional comments (3)
src/asm_files.cpp (3)

208-217: LGTM: Simple and effective subprogram lookup

Linear search is appropriate given the small number of programs typically present.


Line range hint 493-498: LGTM: Proper line info propagation

Correctly propagates line info to instructions that lack it.


502-523: LGTM: Clean program filtering implementation

Good use of reverse iteration for vector erasure and proper error handling for missing subprograms.

@coveralls
Copy link

coveralls commented Nov 23, 2024

Coverage Status

coverage: 89.716% (-0.09%) from 89.808%
when pulling ff109b2 on dthaler:bpf2bpf-multistack
into 22625ba on vbpf:main.

Copy link
Collaborator

@elazarg elazarg left a comment

Choose a reason for hiding this comment

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

Two stylistic comments.

@elazarg elazarg merged commit 5195871 into vbpf:main Nov 24, 2024
19 checks passed
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.

BTF loader code assumes 1 program per section
3 participants