-
Notifications
You must be signed in to change notification settings - Fork 44
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
Support bpf2bpf calls from ELF files #693
Conversation
Signed-off-by: Dave Thaler <[email protected]>
WalkthroughThe changes involve significant modifications to the Changes
Sequence Diagram(s)sequenceDiagram
participant MainProgram
participant ELFProcessor
participant FunctionRelocationQueue
MainProgram->>ELFProcessor: Read ELF file
ELFProcessor->>FunctionRelocationQueue: Queue relocation requests
FunctionRelocationQueue->>ELFProcessor: Process queued relocations
ELFProcessor->>MainProgram: Return processed ELF
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🧰 Additional context used📓 Learnings (1)
🔇 Additional comments (8)
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (2)
- src/asm_files.cpp (7 hunks)
- src/test/test_verify.cpp (2 hunks)
🔇 Additional comments (7)
src/test/test_verify.cpp (4)
79-82
: LGTM: New macro for testing programs expected to fail verification.The
TEST_PROGRAM_REJECT
macro is well-defined and consistent with the existing testing framework. It correctly sets up a test case for programs that should fail verification, which enhances the test coverage.
505-505
: LGTM: New test case for subprogram expected to fail verification.This test case correctly uses the new
TEST_PROGRAM_REJECT
macro to test the "plus1" subprogram, which is expected to fail verification. This adds valuable coverage to the test suite.
506-506
: LGTM: New test case for subprogram expected to pass verification.This test case appropriately uses the
TEST_PROGRAM
macro to test the "func" program, which is expected to pass verification. The comment clarifies that this subprogram can be called from the main program, explaining its expected behavior.
79-82
: Summary: Enhancements to eBPF program verification testing.The changes in this file improve the test coverage for eBPF program verification:
- A new macro
TEST_PROGRAM_REJECT
has been added to facilitate testing of programs expected to fail verification.- Two new test cases have been added:
- One using
TEST_PROGRAM_REJECT
to test a subprogram expected to fail verification.- Another using
TEST_PROGRAM
to test a subprogram expected to pass verification.These additions enhance the robustness of the test suite by explicitly covering both positive and negative test scenarios for bpf2bpf calls across separate program objects.
Also applies to: 505-506
src/asm_files.cpp (3)
Line range hint
63-76
: Consistent Type Usage for Index ParameterThe function
get_symbol_name_and_section_index
now usesELFIO::Elf_Xword
for theindex
parameter instead ofELFIO::Elf_Word
. This change allows the function to handle larger symbol tables, which is consistent with the ELF64 specification and ensures correctness when processing files with a large number of symbols.
Line range hint
77-90
: Update Index Parameter Type to ELFIO::Elf_XwordThe
get_value
function'sindex
parameter has been updated fromELFIO::Elf_Word
toELFIO::Elf_Xword
. This adjustment aligns with the changes made inget_symbol_name_and_section_index
and ensures that the function can handle symbol tables with a larger number of entries, enhancing compatibility with ELF64 files.
411-421
: Verify Correctness ofprog_index
AssignmentIn the creation of
function_relocation
,prog_index
is set usingres.size()
beforeprog
is pushed intores
:function_relocation fr{.prog_index = res.size(), /* ... */ };Since
prog
is added tores
after this point, there is a possibility thatprog_index
might not correctly reference the current program.Please confirm that
res.size()
accurately represents the index of the currentprog
. If not, consider adjusting the code to addprog
tores
before this assignment or modifyprog_index
accordingly.
Signed-off-by: Dave Thaler <[email protected]>
There was a problem hiding this 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
📒 Files selected for processing (1)
- src/asm_files.cpp (7 hunks)
🔇 Additional comments (3)
src/asm_files.cpp (3)
202-207
: Declaration offunction_relocation
struct is appropriateThe new
struct function_relocation
neatly encapsulates the data required for queuing function relocations, enhancing code clarity and maintainability.
443-450
: Logic for appending subprograms is correctly implementedThe
append_subprograms
function appropriately processes function relocations and appends subprograms to ensure that all calls are resolved within the same program for verification.
Line range hint
63-77
: Verify usage of updated function parameter typesThe functions
get_symbol_name_and_section_index
andget_value
now have theindex
parameter type changed fromELFIO::Elf_Word
toELFIO::Elf_Xword
. Ensure that all calls to these functions throughout the codebase are updated to useELFIO::Elf_Xword
to prevent potential type mismatches or truncation.Run the following script to find and review all usages:
Signed-off-by: Dave Thaler <[email protected]>
Signed-off-by: Dave Thaler <[email protected]>
There was a problem hiding this 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
📒 Files selected for processing (1)
- src/asm_files.cpp (7 hunks)
🔇 Additional comments (5)
src/asm_files.cpp (5)
209-229
:append_subprogram
Function ImplementationThe
append_subprogram
function correctly locates and appends the subprogram instructions to the main program. The checks for zero-size subprograms and proper error handling are appropriate.
231-259
: Efficient Management of Function Relocations inappend_subprograms
The
append_subprograms
function efficiently handles function relocations and ensures that each subprogram is appended only once. The use ofsubprogram_offsets
to track appended subprograms improves performance by avoiding duplicate processing.
421-428
: Confirm Correctness of Function Relocation HandlingThe code queues up function relocations for local call instructions. Ensure that the
relocation_entry_index
corresponds correctly to the target function, and that the function name resolution is accurate.
445-445
: Avoid Storing References in ContainersThe
segment_to_program
map stores references toraw_program
objects:std::map<std::string, raw_program&> segment_to_program;Storing references in standard containers can lead to dangling references if the referenced objects are moved or if the container is copied. Consider storing pointers or indices instead to ensure the references remain valid.
379-384
:⚠️ Potential issueRemove Unused Variable
subprograms
The variable
subprograms
is declared andprogram_name
is inserted into it, but it is not used elsewhere in the code. This unused variable can be safely removed to clean up the code.Apply this diff to remove the unused variable:
- std::set<string> subprograms{}; - subprograms.insert(program_name);Likely invalid or redundant comment.
Signed-off-by: Dave Thaler <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (1)
- src/asm_files.cpp (7 hunks)
🧰 Additional context used
📓 Learnings (1)
src/asm_files.cpp (1)
Learnt from: dthaler PR: vbpf/ebpf-verifier#693 File: src/asm_files.cpp:63-63 Timestamp: 2024-09-27T03:48:52.280Z Learning: When working with the ELFIO library, `ELFIO::Elf_Half` is the correct type for return values of 16-bit fields provided by the library and should not be changed to `ELFIO::Elf_Xword`.
🔇 Additional comments (3)
src/asm_files.cpp (3)
202-209
: Well-documentedfunction_relocation
structureThe added documentation above the
function_relocation
struct definition enhances code readability and maintainability.
63-63
: Consistency in ELFIO Types for Symbol IndicesThe functions
get_symbol_name_and_section_index
andget_value
now useELFIO::Elf_Xword
for theindex
parameter, supporting larger symbol tables. This change improves compatibility with ELF files containing more symbols.Also applies to: 77-77
253-258
: Check for Potential Integer Overflow in Offset CalculationThe code correctly checks for overflow when calculating
offset_diff
:int64_t offset_diff = (int64_t)(target_offset - reloc.source_offset - 1); if (offset_diff < INT32_MIN || offset_diff > INT32_MAX) { throw std::runtime_error("Offset difference out of int32_t range for instruction at source offset " + std::to_string(reloc.source_offset)); } prog.prog[reloc.source_offset].imm = (int32_t)offset_diff;This ensures that the cast to
int32_t
is safe and prevents potential errors.
Signed-off-by: Dave Thaler <[email protected]>
PR #608 added support for bpf2bpf calls when instructions were contiguous, including YAML tests for it.
However PR #638 added support for multiple programs per section, and in that support, instructions for different programs (or subprograms) are not contiguous but are in separate program objects.
This PR adds support for bpf2bpf calls across program objects, including ELF tests for it (using an exising ebpf-sample sample). In the existing bpf2bpf.o sample, "func" calls "prog1" in a valid way. "prog1" is not verifiable on its own since its prototype doesn't match the program type, and a negative test is added for this case.
(The type change to the get_value() argument is to resolve a compiler warning.)
Summary by CodeRabbit
New Features
Bug Fixes
Documentation