Skip to content

[AUTOMATED] Print the returns an unoptimized function was written with (returndup auto) - #1941

Merged
mahaloz merged 8 commits into
mainfrom
feat/o0returns
Nov 24, 2026
Merged

mahaloz merged 8 commits into
mainfrom
feat/o0returns

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

At -O0 the binary records which return statements a function was written with: every return compiles to its own value-setting code that jumps to the one epilogue. kuna throws that away during simplification and then guesses the form with rules tuned on optimized code. gcc -O0 output of int guard(int c) { if (!c) return 0; use(1); return 0; }, void vguard(int c) { if (c) return; use(4); } and int pick(int c, int a, int b) { if (c) return a; return b; }:

printf '554889e54883ec10897dfc837dfc007507b800000000eb0fbf01000000e866000000b800000000c9c3554889e54883ec10897dfc837dfc00740abf02000000e844000000b803000000c9c3554889e54883ec10897dfc837dfc00750cbf04000000e822000000eb0190c9c3554889e5897dfc8975f88955f4837dfc0074058b45f8eb038b45f45dc3554889e5897dfc905dc3' | xxd -r -p > fx.bin
for a in 0x100000 0x10004b 0x10006b; do kuna decompile fx.bin $a --raw-image --target x86:LE:64:default:gcc --base 0x100000; done

Before:

  if (a0)
    sub_100088(1);
  return 0;
...
  if (!a0)
    sub_100088(4);
...
  if (a0)
    a2 = a1;
  return a2;

After:

  if (!a0)
    return 0;
  sub_100088(1);
  return 0;
...
  if (a0)
    return;
  sub_100088(4);
...
  if (a0)
    return a1;
  return a2;
  • returndup gains the value auto, now the default (and what the aggressive preset sets). In a function the unoptimized detector flags, p8_structure/kuna_o0returns.rs reads the raw block graph at the first heritage pass and tags each edge into the epilogue with the return it came through: a predecessor that only computes a value is one return, an epilogue that sets the return register itself is one return shared by everything reaching it, and in a void function a return; is the nop a path lands on. The tags ride in high bits of the edge labels, which survive the removal of the emptied blocks.
  • When returndup runs, each recorded return gets its own copy of the epilogue and no more, constant or not; earlyreturn, switchreturn, rettriangle and tailform's size rule leave such functions alone (the no-return nesting still applies). on keeps the old rules everywhere; optimized functions are unchanged.
  • -O0 compiles return c ? a : b; exactly like if (c) return a; return b;; this prints the second.
  • On the decbench O0 corpus 1,545 more functions match the source's control flow exactly (gained 1,670, lost 125; +862 and +683 on the two halves of the projects); O2-noinline changes 7 bodies (+1). At O0 function-tail sites where exactly one form matches the source, the recorded form is the matching one 98% of the time against 73% for the size rule.

Tests: tests/stages/kuna-returndup-auto.xml (the three functions above plus a body that shares the epilogue's return; option returndup on reproduces the old output) and unit tests. Two datatests, ten stage tests, one CLI probe and the castobject, callrettype, rettriangle, ARM-interworking and local-name workspace tests check other features' -O0 text and pin option returndup on; the bejoin round trip now drops a trailing comment from a printed signature before compiling it. decompile-all wall time on the O2-noinline sshd, ls and bash: 1.000, 1.010 and 0.990 of main's.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 8 commits November 24, 2026 14:34
…fy recorded returning ifs, stage test

[AUTOMATED] work in progress; debug dump still present.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
… auto)

[AUTOMATED] New default value `auto` of option `returndup`: in functions the
unoptimized detector flags, the return statements read from the raw block
graph at the first heritage pass decide which shared epilogues are split, and
the rule-based return placement (const gate, rettriangle, earlyreturn,
switchreturn, tailform's size rule) stands down there. `on` keeps the old
rules everywhere. Void returns are told apart by the instruction a path lands
on (a `return;` is a nop), stripped canary chains are joined first, and an
epilogue holding only hoisted arithmetic may be split. Debug dump removed.
Stage test kuna-returndup-auto.xml; datatests and stage tests that pin other
features take `option returndup on`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED] Under the default returndup auto, main of the in-repo stripped PIE fixture prints its two -O0 returns, so argv[1] reaches the call through a local; the probe checks elfmain's typing, so it keeps the merged form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…epilogues

[AUTOMATED] docs/spec/08-structuring.md describes behind-nop returns, nop-run landings, the stripped-canary splice and pure-arithmetic epilogues of returndup auto.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
[AUTOMATED] castobject, callrettype, rettriangle, ARM interworking and local-name tests check text the old return placement printed; they pass --option returndup on. The bejoin truncation round trip drops a trailing comment from a printed signature before using it as a declaration, and the crackme rename test reads the strcmp result the recorded returns now assign.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…d optimized functions alone

[AUTOMATED] The guard re-spelling of a decided function's last returning if declines when the final return carries a label, and the skipped final return applies only in decided functions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
@mahaloz
mahaloz merged commit 2087bd3 into main Nov 24, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/o0returns branch November 24, 2026 16:52
mahaloz added a commit that referenced this pull request Nov 24, 2026
[AUTOMATED] Brings in main through 2087bd3 (#1937, #1938, #1940, #1941).
The corpus file count is 914 (83 datatests, 831 stage tests) and the
stage baseline is re-recorded: main's 5340 keys plus the 65 assertions
of this branch's six stage tests, with the two catalog rows that name
pushan.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTo941vG8RLpZJvswN
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.

1 participant