Repository navigation
[AUTOMATED] feat(p2): cleanupcode - delete the Rust drop/deallocate call sites (DIV-81) - #326
Merged
Merged
Conversation
…all sites (DIV-81)
A decompiled Rust function is mostly resource-management glue that is nowhere in
the source. Rust emits a drop call at every scope exit and every `?` early
return, so the showcase FakeCrypt::fileops::encrypt_file renders eight
`core::ptr::drop_in_place<...>(v)` statements and a four-deep if/else pyramid
built around them. Port of the idea in SEFCOM Oxidizer's CleanupCodeRemover.
core::ptr::drop_in_place<std::fs::File>(v13); --> (gone)
core::ptr::drop_in_place<alloc::vec::Vec<u8>>(&v8); --> (gone)
THE SEAM IS PRE-SSA, and that is the mechanism, not an implementation detail. A
new `deadcode`-group Action at the TOP of mainloop, self-gated on
get_heritage_pass() == 0 - the window kuna_loweredswitch/kuna_outline already
use, and the analogue of Oxidizer's STAGE = BEFORE_VARIABLE_RECOVERY. Before
heritage there are no INDIRECT call effects to unpick and no MULTIEQUAL to
patch, CALL is not a control-transfer op so no edge moves, and the register/
stack writes that existed only to set up the drop's arguments lose their last
reader and are collected by the ordinary ActionDeadCode fixpoint. Removing the
call after SSA would leave all of that behind as unexplained assignments. Each
victim goes with the stock delete_call_specs + op_destroy pair
block_remove_internal already uses.
MATCHING IS NORMALIZE-THEN-COMPARE-EXACTLY, never starts_with. kuna's demangler
hands over `core::ptr::drop_in_place$LT$std..fs..File$GT$` and
`_$LT$alloc..vec..Vec$LT$T$C$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop`,
so normalize_rust_name reproduces Oxidizer's
normalize(monopolize=True, use_trait_name=True): un-escape the legacy `$..$`
sequences and the `..` separator, then collapse the INNERMOST angle-bracket group
repeatedly - deleting a plain generic argument list, replacing a `<T as Trait>`
qualified path with the TRAIT name. FakeCrypt::fileops::drop_ransom_note and
`<T as crossbeam_epoch::atomic::Pointable>::drop` are correctly untouched.
TWO DELIBERATE DEPARTURES FROM OXIDIZER.
1. Its `free`, `close` and `_close` entries are NOT carried. kuna's primary
corpus is C, and deleting free() from C output would be a catastrophically
wrong answer. What remains cannot occur in a C program, and THAT is the
entire default-ON argument: the pass is structurally inert on a C binary, so
no compiler-detection channel from the loader to the engine is needed (there
is none). Its `smallvec::deallocate` is dropped as unwitnessed.
2. The pass declines outright inside the glue itself. A drop routine's body is
calls to other drop routines, and that IS its content; unguarded, 25 of the
26 functions that grew were a `<RawVec<T,A> as Drop>::drop` whose bare tail
call was its only statement, leaving the argument registers rendered as a
`-> [u8; 16]` return.
rustc's v0 mangling renders the drop shim without its `core::ptr::` prefix, so a
bare `drop_in_place<T>` is matched too - but only when it really carries generic
arguments, which no C symbol does.
MEASURED. Showcase FakeCrypt::fileops::encrypt_file @0x55300: 81 -> 68 lines
(-16.0%), 8 drop calls -> 0, and the four-deep pyramid the shared drop tail
forced collapses into four flat early returns. Whole binary FakeCrypt (rustc
1.90, 4.9 MB, 4194 functions): 227 change (5.4%), 363 cleanup call statements
removed, 126,670 -> 125,720 lines; within the changed functions 19,363 -> 18,413
(-4.91%). 217 shrink, 9 keep their length (all structural improvements), 1 grows
by a line; errors 3 -> 3. Second Rust witness, a rustc -O demo (605 functions):
36 change, 69 call statements removed, -17.49% within them.
COLLATERAL ON C IS EXACTLY ZERO: 0 differing `code` fields across 7,429 C
functions (faillog, grep, sort, libselinux.so.1, the ET_REL fid/lib.o, and the
ARM Cortex-M betaflight_STM32F405.elf).
Speed, interleaved min-of-N `kuna decompile-all --json`: C (inert) grep 9 pairs
min 14.181 s -> 13.959 s (-1.56%); Rust demo 5 pairs min 85.399 s -> 85.933 s
(+0.63%). Both inside the 5% budget. One honest cost: the single pathological
2,400-line std::backtrace_rs::symbolize::gimli::Context::new goes 10.4 s ->
11.4 s and crosses --mode fast's 10 s per-function budget.
Option `cleanupcode on|off`, default on, destructive, ELEM 4131, settable count
111 -> 112.
Gates: make test 675/675 PARITY OK (no re-pin) - make test-stages PARITY OK
(498/498, 5 new) - make rust-test 4694 passed / 0 failed - make check-spec green
lenient and strict - kuna catalog --check OK.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9
mahaloz
force-pushed
the
feat/rust-cleanupcode
branch
from
August 22, 2026 05:05
8f7a92c to
0e1668c
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.
Rust's automatic resource management litters decompiled output with calls that carry no meaning for a
reader. This is a port of the idea in SEFCOM Oxidizer's
CleanupCodeRemover(
angr/rust/optimization_passes/cleanup_code_remover.py), with the C-safety restriction that lets itship default-ON.
The showcase,
FakeCrypt::fileops::encrypt_file@0x55300 — 81 → 68 lines, 8drop_in_placecalls → 0,and the four-deep
if/elsepyramid the shared drop tail forced collapses into four flat early returns:The seam is pre-SSA, and that is the mechanism
A new
deadcode-group Action at the top ofmainloop, self-gated onget_heritage_pass() == 0—the pre-SSA window
kuna_loweredswitch/kuna_outlinealready use, and the analogue of Oxidizer'sSTAGE = BEFORE_VARIABLE_RECOVERY. Before heritage there are noINDIRECTcall effects to unpick andno
MULTIEQUALto patch,CALLis not a control-transfer op so no edge moves, and — the payoff — theregister/stack writes that existed only to set up the drop's arguments lose their last reader and are
collected by the ordinary
ActionDeadCodefixpoint. Removing the call after SSA would leave all ofthat behind as unexplained assignments. Each victim goes with the stock
delete_call_specs+op_destroypairblock_remove_internalalready uses.Matching: normalize, then compare exactly — never
starts_withkuna's demangler hands over
core::ptr::drop_in_place$LT$std..fs..File$GT$and_$LT$alloc..vec..Vec$LT$T$C$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop, so equality against thelist would match nothing and a prefix test would match
FakeCrypt::fileops::drop_ransom_note.normalize_rust_namereproduces Oxidizer'snormalize(monopolize=True, use_trait_name=True):un-escape the legacy
$LT$/$GT$/$C$/$uXX$sequences and the..separator, then collapse theinnermost angle-bracket group repeatedly — deleting a plain generic argument list, replacing a
<T as Trait>qualified path with the trait name.<T as crossbeam_epoch::atomic::Pointable>::dropcorrectly does not match.
Two deliberate departures from Oxidizer
free,closeand_closeare NOT carried. kuna's primary corpus is C binaries, and deletingfree()from C output would be a catastrophically wrong answer. What remains cannot occur in a Cprogram, and that is the entire default-ON argument: the pass is structurally inert on a C binary,
so no compiler-detection channel from the loader to the engine is needed (there is none today).
smallvec::deallocateis dropped as unwitnessed — an unverifiable entry in a delete list is worsethan a missing one.
and that IS its content. Unguarded, 25 of the 26 functions that grew were a
<RawVec<T,A> as Drop>::dropwhose bare tail call toRawVecInner::deallocatewas its onlystatement, leaving the argument registers rendered as a
-> [u8; 16]return.rustc's v0 mangling renders the drop shim without its
core::ptr::prefix, so a baredrop_in_place<T>is matched too — but only when it really carries generic arguments, which no C symbol does.
Measured
encrypt_file@0x55300FakeCryptwhole binary (rustc 1.90, 4.9 MB, 4194 fns)Iterator::foldflattens to an early return) · 1 grows by a line · decompile errors 3 → 3rustc -Odemo (3.9 MB, 605 fns)codefields across 7,429 C functions — faillog 0/69, grep 0/449, sort 0/490, libselinux.so.1 0/621, ET_RELfid/lib.o0/3, ARM Cortex-Mbetaflight_STM32F405.elf0/5797Speed (interleaved min-of-N
kuna decompile-all --json): C, where the pass is inert —grep, 9pairs, min 14.181 s → 13.959 s (-1.56%), median -0.62%. Rust, where it does all its work — the demo,
5 pairs, min 85.399 s → 85.933 s (+0.63%), median -0.15%. Both inside the 5% budget.
One honest cost: on the Rust demo the single pathological 2,400-line
std::backtrace_rs::symbolize::gimli::Context::newgoes 10.4 s → 11.4 s (removing a CALL removes acaller-save/
INDIRECTbarrier, so values flow further and heritage/merge/types do more work) and crosses--mode fast's 10 s per-function budget, becoming anerrorrecord. FakeCrypt's error count isunchanged at 3/4194.
Option
option cleanupcode on|off, default on (DIV-81), destructive, ELEM 4131. Settable count 111 → 112(
source_decompiler = "oxidizer",change_kind = "presentation-default",tier = "transform"— chosenso none of the count-pinned catalog assertions move).
Gates
make testmake test-stagesmake rust-testmake check-spec--strictkuna catalog --checkcatalog OKTest:
tests/stages/oxidizer-cleanupcode.xml, two-pass — asserts the drop call, the dealloc call andboth argument constants appear exactly once across the two
print Cpasses (present with the optionoff, gone by default) while the real neighbouring call appears in both. The argument constants are the
load-bearing assertions: they only vanish because the removal happens pre-SSA.
🤖 Generated with Claude Code
https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9