Skip to content

[AUTOMATED] feat(p2): cleanupcode - delete the Rust drop/deallocate call sites (DIV-81) - #326

Merged
mahaloz merged 1 commit into
mainfrom
feat/rust-cleanupcode
Aug 22, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/rust-cleanupcode

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

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 it
ship default-ON.

The showcase, FakeCrypt::fileops::encrypt_file @0x55300 — 81 → 68 lines, 8 drop_in_place calls → 0,
and the four-deep if/else pyramid the shared drop tail forced collapses into four flat early returns:

-    if v2 != 0 { // branch-flip
-      core__ptr__drop_in_place_LT_core__result__Result_LT_usize_C_std__io__error__Error_GT__GT_(v2,v4);
-    }
-    else {
-      core__ptr__drop_in_place_LT_core__result__Result_LT_usize_C_std__io__error__Error_GT__GT_(0,v4);
-      v1 = v16;
+  if v2 != 0 {
+    return;
+  }
...
-          core__ptr__drop_in_place_LT_std__fs__File_GT_(v13 as u64 & 0xffffffff);

The seam is pre-SSA, and that is the mechanism

A new deadcode-group Action at the top of mainloop, self-gated on get_heritage_pass() == 0 —
the pre-SSA 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 payoff — 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: 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 equality against the
list would match nothing and a prefix test would match FakeCrypt::fileops::drop_ransom_note.
normalize_rust_name reproduces Oxidizer's normalize(monopolize=True, use_trait_name=True):
un-escape the legacy $LT$/$GT$/$C$/$uXX$ 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. <T as crossbeam_epoch::atomic::Pointable>::drop
correctly does not match.

Two deliberate departures from Oxidizer

  1. free, close and _close are NOT carried. kuna's primary corpus is C binaries, 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 today).
    smallvec::deallocate is dropped as unwitnessed — an unverifiable entry in a delete list is worse
    than a missing one.
  2. The pass declines 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 to RawVecInner::deallocate 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 encrypt_file @0x55300 81 → 68 lines (-16.0%), 8 drop calls → 0
FakeCrypt whole binary (rustc 1.90, 4.9 MB, 4194 fns) 227 change (5.4%), 363 cleanup call statements removed, 126,670 → 125,720 lines; -4.91% within the changed functions
… breakdown 217 shrink · 9 same-length (all structural wins — Iterator::fold flattens to an early return) · 1 grows by a line · decompile errors 3 → 3
rustc -O demo (3.9 MB, 605 fns) 36 change (6.0%), 69 call statements removed, -17.49% within the changed functions
C collateral 0 differing code fields across 7,429 C functions — faillog 0/69, grep 0/449, sort 0/490, libselinux.so.1 0/621, ET_REL fid/lib.o 0/3, ARM Cortex-M betaflight_STM32F405.elf 0/5797

Speed (interleaved min-of-N kuna decompile-all --json): C, where the pass is inert — grep, 9
pairs, 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::new goes 10.4 s → 11.4 s (removing a CALL removes a
caller-save/INDIRECT barrier, so values flow further and heritage/merge/types do more work) and crosses
--mode fast's 10 s per-function budget, becoming an error record. FakeCrypt's error count is
unchanged 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" — chosen
so none of the count-pinned catalog assertions move).

Gates

Gate Result
make test 675/675, PARITY OK (no re-pin — no datatest binary carries a Rust drop symbol, so the flip is structurally byte-identical)
make test-stages PARITY OK, 498/498 (5 new)
make rust-test 4694 passed / 0 failed / 38 ignored
make check-spec green, lenient and --strict
kuna catalog --check catalog OK

Test: tests/stages/oxidizer-cleanupcode.xml, two-pass — asserts the drop call, the dealloc call and
both argument constants
appear exactly once across the two print C passes (present with the option
off, 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

…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
mahaloz force-pushed the feat/rust-cleanupcode branch from 8f7a92c to 0e1668c Compare August 22, 2026 05:05
@mahaloz
mahaloz merged commit 007aaa7 into main Aug 22, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/rust-cleanupcode branch August 22, 2026 05:10
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