WIP: Optimization pass - #380
Open
trufae wants to merge 7 commits into
Open
Conversation
wargio
requested changes
Dec 22, 2025
wargio
left a comment
Owner
There was a problem hiding this comment.
can't merge this. optimization is not that simple, on top of that this works on arm only.
Contributor
Author
|
The code have some arm64-specific logic (but works on others archs), also it have too many regexps postprocessing the text. I tried to make it fit into a separate file to not mess with the rest. But I agree that the code quality is far from ideal. I’ll do some iterations to add optimization steps in a clean and generic way. So yeah better not to merge it like this, hopefully it will serve as a base of inspiration for what r2dec output can be |
Removed (ARM64-specific code) 1. simplifyStackCanaryChecks() - Entirely ARM64-specific; hardcoded w8, x8, x9, x29-8 register patterns and reloc.__stack_chk_fail patterns 2. simplifyStackCanaryScopes() - Same ARM64-specific issues; was also matching regular non-canary comparisons as false positives 3. buildGuardValueExprFromWindow() - Helper hardcoded for ARM64 x8 register chains 4. matchStackSaveSlot() - Hardcoded x29 - 8 ARM64 stack save pattern 5. isStackChkFailStatement() - Only used by removed stack canary passes 6. findInstructionIndexAtOrAfterAddress() - Only used by removed stack canary scope pass 7. hasSubsequentReturn() - Unused function Fixed (portability) 8. getBasePointerRegisters() - Removed hardcoded x29, rbp, ebp fallback; empty list if r2 query fails (conservative) 9. isReturnRegisterName() - Replaced hardcoded [w0, x0, r0, eax, rax, v0] list with r2 query arp~R0[1] (works across all architectures) 10. isRegisterIdentifier() - Replaced hardcoded [xw]\d+, r\d+, (e|r)?ax patterns with r2 query arp~gpr[1] 11. All Set uses - Replaced with Object.create(null) for QuickJS compatibility Fixed (correctness bugs) 12. parseWriteStatement() - Fixed regex = to =(?!=) to prevent x == y being parsed as assignment x = "= y" 13. simplifyTrivialConstants() - Removed unsafe (0)→0 and (1)→1 paren removal that broke function call parens (e.g., isatty (1) → isatty 1) 14. propagateConstants() - Added readOnly mode after first branch to prevent facts from inside conditional blocks leaking past merge points 15. inlineSingleUseAssignments() - Added eventPastBranch pre-computation to prevent inlining definitions from inside conditional blocks to code outside 16. removeDeadAssignments() - Added pastBranch tracking to prevent removing assignments where the killing write is behind a conditional branch Test results - ARM64 (/bin/ls main): 782 → 749 lines (4% reduction) - x86-64 (bash main): 1469 → 1303 lines (11% reduction) - MIPS (boa-mips main): 417 → 332 lines (20% reduction) - ARM32 (arm-init main): works correctly - optimize=0 produces identical output to no optimization (no regression)
trufae
force-pushed
the
optimization-pass
branch
from
April 9, 2026 01:52
6714847 to
8b052e8
Compare
trufae
force-pushed
the
optimization-pass
branch
from
April 10, 2026 09:56
e9de9aa to
9e02d80
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
i have vibecoded with gpt-5.2 an optional optimization pass at the end of the decompilation pipeline. this can be configured with 'r2dec.optimize=6' . as you can see the current output is pretty close to r2ghidra, the main problem here is that r2 var analysis is not correct and its still inlining some stack arithmetics that can be optimized properly with manual var definitions. Hopefully addressed after the next release in the r2 side.