Skip to content

Fix load_elf to support non-file istream paths and add regression tests#1048

Merged
elazarg merged 3 commits intovbpf:mainfrom
mikeagun:load-elf-fix
Mar 9, 2026
Merged

Fix load_elf to support non-file istream paths and add regression tests#1048
elazarg merged 3 commits intovbpf:mainfrom
mikeagun:load-elf-fix

Conversation

@mikeagun
Copy link
Contributor

@mikeagun mikeagun commented Mar 3, 2026

Fix load_elf istream fallback and add Assume regression tests

load_elf fix: The ELF loader overhaul (#1026) added section-bounds validation using file_size(path) in
load_elf(istream, path). This breaks callers that pass in-memory data via std::stringstream with a non-file path
like "memory" — file_size("memory") fails with ERROR_FILE_NOT_FOUND. Fix: when file_size(path) fails, fall back to
seekg(0, end) + tellg() on the input stream. The section-bounds check still runs; only the size source changes.

Assume regression tests: The assertion crash fix (#1012, aa31b0e) shipped without test coverage. Added 3 YAML test
cases for reg-vs-reg Assume with differing inferred types (ctx==number, packet!=number, stack<number) and 1 Catch2
test for the istream non-file-path path.

Closes #1049

Summary by CodeRabbit

  • Bug Fixes

    • Improved ELF data-size determination and section bounds validation to support stream-based inputs when file-size is unavailable, preventing incorrect bounds checks.
  • Tests

    • Added a regression test for loading ELF data from non-file streams.
    • Added regression tests covering assume operations that compare operands with differing inferred types.

load_elf(istream, path) calls filesystem::file_size(path) for section
bounds validation, but this fails when path does not refer to an actual
file (e.g. path="memory" for in-memory ELF data). Fall back to computing
the data size from the input stream via seekg/tellg.

Also add regression tests for the assertion crash fix (aa31b0e) in
Assume with operands of different inferred types.

Signed-off-by: Michael Agun <danielagun@microsoft.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 060b700d-8ef1-4a76-a26c-83a4ce16ac48

📥 Commits

Reviewing files that changed from the base of the PR and between 2d8bfc8 and 333029a.

📒 Files selected for processing (1)
  • src/io/elf_reader.cpp

Walkthrough

Replace direct use of std::filesystem::file_size for istream-based ELF input with a new get_data_size helper that falls back to measuring the stream; use data_size for ELF section bounds validation. Add a regression test for reading an ELF from a non-file path stream and add assume-type-mismatch test cases in YAML.

Changes

Cohort / File(s) Summary
ELF Reader Core
src/io/elf_reader.cpp
Add static std::uintmax_t get_data_size(std::istream& input_stream, const std::string& path) that tries std::filesystem::file_size(path) then falls back to seek-to-end on input_stream; replace direct file_size usage with data_size for ELF loading and section bounds checks; throw MalformedElf if size cannot be determined.
ELF Loader Tests
src/test/test_elf_loader.cpp
Add #include <sstream> and #include "io/elf_reader.hpp" and a new test TEST_CASE("read_elf succeeds with istream and non-file path", "[elf]") that loads ELF from an in-memory stream and asserts non-empty programs.
Assume Operation Test Data
test-data/jump.yaml
Add regression cases covering assume comparisons with mismatched operand types (ctx vs number, packet vs number, stack vs number) including pre-state, code, expected post-state and diagnostic expectations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • elazarg
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Out of Scope Changes check ❓ Inconclusive Changes in elf_reader.cpp and test_elf_loader.cpp directly address #1049. The test-data/jump.yaml additions for Assume operations with mismatched types appear related to a separate assertion crash fix (#1012) but are included as regression tests per the PR summary. Clarify whether the jump.yaml Assume regression test cases are necessary for this PR scope or should be split into a separate change addressing #1012.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: fixing load_elf to support non-file istream paths and adding regression tests, matching the primary objectives in the PR.
Linked Issues check ✅ Passed The PR implementation directly addresses all coding requirements from #1049: introduces get_data_size helper that falls back to stream seek/tell when file_size fails, replaces file_size usage with this helper for data size determination, preserves section-bounds validation, and adds corresponding regression tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mikeagun mikeagun marked this pull request as draft March 3, 2026 19:36
mikeagun pushed a commit to mikeagun/ebpf-for-windows that referenced this pull request Mar 3, 2026
Bump external/ebpf-verifier from e68a347b to b9010eee (main), which
includes the fix for the verifier_fuzzer CI timeout (issue microsoft#5055).

Handle breaking API changes:
- Include io/elf_reader.hpp (header split in microsoft#1026)
- Re-implement _instype()/collect_stats() locally (removed in microsoft#1042
  but needed for ebpf_stat_t public API)
- Add resolve_ksym_btf_id nullptr to platform structs
- Patch load_elf stream-size fallback for in-memory ELF loading
  (vbpf/prevail#1048)
Signed-off-by: Michael Agun <danielagun@microsoft.com>
@mikeagun mikeagun marked this pull request as ready for review March 3, 2026 21:09
mikeagun pushed a commit to mikeagun/ebpf-for-windows that referenced this pull request Mar 3, 2026
Bump external/ebpf-verifier from e68a347b to b9010eee (main), which
includes the fix for the verifier_fuzzer CI timeout (issue microsoft#5055).

Handle breaking API changes:
- Include io/elf_reader.hpp (header split in microsoft#1026)
- Re-implement _instype()/collect_stats() locally (removed in microsoft#1042
  but needed for ebpf_stat_t public API)
- Add resolve_ksym_btf_id nullptr to platform structs
- Patch load_elf stream-size fallback for in-memory ELF loading
  (vbpf/prevail#1048)
@mikeagun
Copy link
Contributor Author

mikeagun commented Mar 6, 2026

@elazarg Hi Elazar, would you have time to review this soon?

@elazarg
Copy link
Collaborator

elazarg commented Mar 6, 2026

@elazarg Hi Elazar, would you have time to review this soon?

Hi, thanks for the PR! I plan to review it next week.

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.

Thanks for the commit! A small suggestion, and a note - this looks like two unrelated PRs. Please separate PRs in the future (no need to split this one now).

@elazarg
Copy link
Collaborator

elazarg commented Mar 9, 2026

@mikeagun note the DSO check failure - please amend-sign-off and force-push

Signed-off-by: Michael Agun <danielagun@microsoft.com>
@mikeagun mikeagun requested a review from elazarg March 9, 2026 19:18
@elazarg elazarg merged commit b24faf5 into vbpf:main Mar 9, 2026
16 checks passed
@elazarg
Copy link
Collaborator

elazarg commented Mar 9, 2026

Thanks!

mikeagun pushed a commit to mikeagun/ebpf-for-windows that referenced this pull request Mar 9, 2026
Bump external/ebpf-verifier from e68a347b to b9010eee (main), which
includes the fix for the verifier_fuzzer CI timeout (issue microsoft#5055).

Handle breaking API changes:
- Include io/elf_reader.hpp (header split in microsoft#1026)
- Re-implement _instype()/collect_stats() locally (removed in microsoft#1042
  but needed for ebpf_stat_t public API)
- Add resolve_ksym_btf_id nullptr to platform structs
- Patch load_elf stream-size fallback for in-memory ELF loading
  (vbpf/prevail#1048)
github-merge-queue bot pushed a commit to microsoft/ebpf-for-windows that referenced this pull request Mar 10, 2026
…changes (#5078)

* Bump PREVAIL verifier to b9010eee and handle breaking changes

Bump external/ebpf-verifier from e68a347b to b9010eee (main), which
includes the fix for the verifier_fuzzer CI timeout (issue #5055).

Handle breaking API changes:
- Include io/elf_reader.hpp (header split in #1026)
- Re-implement _instype()/collect_stats() locally (removed in #1042
  but needed for ebpf_stat_t public API)
- Add resolve_ksym_btf_id nullptr to platform structs
- Patch load_elf stream-size fallback for in-memory ELF loading
  (vbpf/prevail#1048)

* bump submodule

* Pass Instruction by const ref in _instype

* Update DLL dependency baselines for PREVAIL verifier bump

---------

Co-authored-by: Michael Agun <danielagun@microsoft.com>
mikeagun added a commit to mikeagun/ebpf-for-windows that referenced this pull request Mar 10, 2026
…changes (microsoft#5078)

* Bump PREVAIL verifier to b9010eee and handle breaking changes

Bump external/ebpf-verifier from e68a347b to b9010eee (main), which
includes the fix for the verifier_fuzzer CI timeout (issue microsoft#5055).

Handle breaking API changes:
- Include io/elf_reader.hpp (header split in microsoft#1026)
- Re-implement _instype()/collect_stats() locally (removed in microsoft#1042
  but needed for ebpf_stat_t public API)
- Add resolve_ksym_btf_id nullptr to platform structs
- Patch load_elf stream-size fallback for in-memory ELF loading
  (vbpf/prevail#1048)

* bump submodule

* Pass Instruction by const ref in _instype

* Update DLL dependency baselines for PREVAIL verifier bump

---------

Co-authored-by: Michael Agun <danielagun@microsoft.com>
mikeagun added a commit to microsoft/ebpf-for-windows that referenced this pull request Mar 10, 2026
…changes (#5078)

* Bump PREVAIL verifier to b9010eee and handle breaking changes

Bump external/ebpf-verifier from e68a347b to b9010eee (main), which
includes the fix for the verifier_fuzzer CI timeout (issue #5055).

Handle breaking API changes:
- Include io/elf_reader.hpp (header split in #1026)
- Re-implement _instype()/collect_stats() locally (removed in #1042
  but needed for ebpf_stat_t public API)
- Add resolve_ksym_btf_id nullptr to platform structs
- Patch load_elf stream-size fallback for in-memory ELF loading
  (vbpf/prevail#1048)

* bump submodule

* Pass Instruction by const ref in _instype

* Update DLL dependency baselines for PREVAIL verifier bump

---------

Co-authored-by: Michael Agun <danielagun@microsoft.com>
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.

load_elf(istream, path) fails when path is not a real file

2 participants