Skip to content

WIP: Optimization pass - #380

Open
trufae wants to merge 7 commits into
wargio:masterfrom
trufae:optimization-pass
Open

WIP: Optimization pass#380
trufae wants to merge 7 commits into
wargio:masterfrom
trufae:optimization-pass

Conversation

@trufae

@trufae trufae commented Dec 21, 2025

Copy link
Copy Markdown
Contributor

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.

Screenshot 2025-12-21 at 18 44 08

@wargio wargio left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

can't merge this. optimization is not that simple, on top of that this works on arm only.

@trufae trufae changed the title Optimization pass WIP: Optimization pass Dec 22, 2025
@trufae

trufae commented Dec 22, 2025

Copy link
Copy Markdown
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

radare added 3 commits April 9, 2026 02:56
  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
trufae force-pushed the optimization-pass branch from 6714847 to 8b052e8 Compare April 9, 2026 01:52
@trufae
trufae force-pushed the optimization-pass branch from e9de9aa to 9e02d80 Compare April 10, 2026 09:56
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.

3 participants