loader: handle missing basic blocks in compute_static_layout#2888
loader: handle missing basic blocks in compute_static_layout#2888reyyanxahmed wants to merge 4 commits intomandiant:masterfrom
Conversation
There was a problem hiding this comment.
Please add bug fixes, new features, breaking changes and anything else you think is worthwhile mentioning to the master (unreleased) section of CHANGELOG.md. If no CHANGELOG update is needed add the following to the PR description: [x] No CHANGELOG update needed
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
…t#2734) Extractors such as Binary Ninja may discover basic blocks during feature extraction that are no longer present when re-enumerating basic blocks during layout computation, because the IL can be recomputed between the two passes, shifting basic block boundaries. Replace the hard assertion with a defensive check that logs a warning and skips the missing address. This prevents the AssertionError crash while preserving all valid layout data. Add a unit test that reproduces the scenario with NullStaticFeatureExtractor by removing a basic block between find_capabilities() and compute_static_layout(). Fixes mandiant#2734.
b3f504b to
e9b1e71
Compare
CHANGELOG updated or no update needed, thanks! 😄
There was a problem hiding this comment.
Code Review
This pull request resolves a crash in compute_static_layout by replacing a strict assertion with a more robust check that logs a warning and continues. Specifically, it addresses an AssertionError in compute_static_layout in capa/loader.py when using the Binary Ninja extractor, improving the robustness of the tool. A new unit test in tests/test_layout.py has been added to reproduce and verify the fix. My security audit confirmed that no new security vulnerabilities were introduced. I've also provided minor suggestions for code clarity and to remove unused code in the new test file.
tests/test_layout.py
Outdated
| import capa.features.basicblock | ||
| import capa.features.extractors.null | ||
| from capa.features.address import AbsoluteVirtualAddress | ||
| from capa.features.extractors.base_extractor import BBHandle, SampleHashes, FunctionHandle |
There was a problem hiding this comment.
There are a few unused imports here. capa.features.basicblock, BBHandle, and FunctionHandle are imported but not used. It's good practice to remove them to keep the code clean.
| import capa.features.basicblock | |
| import capa.features.extractors.null | |
| from capa.features.address import AbsoluteVirtualAddress | |
| from capa.features.extractors.base_extractor import BBHandle, SampleHashes, FunctionHandle | |
| import capa.features.extractors.null | |
| from capa.features.address import AbsoluteVirtualAddress | |
| from capa.features.extractors.base_extractor import SampleHashes |
tests/test_layout.py
Outdated
|
|
||
| See #2734. | ||
| """ | ||
| # Create an extractor with two basic blocks at 0x401000 and 0x401010 |
There was a problem hiding this comment.
There was a problem hiding this comment.
Thanks for the review! Both suggestions are addressed in commit aadf32e:
Unused imports removed dropped [capa.features.basicblock], BBHandle, and FunctionHandle as suggested.
Comment fixed: changed "two basic blocks" to "one basic block at 0x401000" to accurately reflect the test setup. /gemini-code-assist can you review?
There was a problem hiding this comment.
Thank you for addressing the feedback. I've reviewed the changes in commit aadf32e and can confirm that:
- The comment in
tests/test_layout.pyon line 37 has been updated to accurately reflect the creation of "one basic block at 0x401000". - The unused imports
capa.features.basicblock,BBHandle, andFunctionHandlehave been successfully removed fromtests/test_layout.py.
These changes resolve the previously identified issues. The new test test_compute_static_layout_with_missing_basic_block also appears to be well-structured and correctly validates the fix for the described scenario.
|
@mike-hunhoff this PR is ready for review. It fixes the Binary Ninja extractor crash in #2734 by replacing the hard assert with a defensive check, consistent with the approach in #2523. CI is green and bot feedback has been addressed. |
Problem
compute_static_layoutcrashes with anAssertionErrorwhen using the Binary Ninja extractor:Binary Ninja may recompute its IL between the feature-extraction pass and the layout-computation pass, causing basic block boundaries to shift. A basic block address that existed when
find_capabilities()discovered a match may no longer be present whencompute_static_layout()re-enumerates basic blocks viaextractor.get_basic_blocks().Fix
Replace the hard
assert addr in functions_by_bbwith a conditional check that:continue, preserving all other valid layout data.This is consistent with the defensive approach taken in #2523 for the closely related issue #2516.
Testing
Added
tests/test_layout.pywith a unit test that reproduces the scenario usingNullStaticFeatureExtractor:find_capabilities()to match a BB-scoped rule.compute_static_layout()and verifies it completes without error.Fixes #2734.