Skip to content

[AUTOMATED] fix(p9): (NOVEL) realtypes keeps an unknown pointee size (DIV-48) - #232

Merged
mahaloz merged 4 commits into
mainfrom
fix/realtypes-pointee-size
Aug 1, 2026
Merged

mahaloz merged 4 commits into
mainfrom
fix/realtypes-pointee-size

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

The bug

realtype_unknown_base (decompiler/crates/kuna-decomp/src/p9_emit/printc.rs) returned
Some("void") unconditionally when the residual TYPE_UNKNOWN base sat under a pointer
modifier. declarator_parts sets that flag for any pointer-to-unknown, so undefined1 *,
undefined4 * and undefined8 * all printed void *.

Only the declaration was relabelled. opLoad / opPtradd keep building index and cast
expressions from the ORIGINAL pointee size, so the declaration and the expressions disagreed
about the stride. void is also the one pointee spelling that carries no width, so a store
cast down to *(void *) lost its access width entirely.

realtypes is a P9 presentation policy — P5 never mis-typed anything. --option realtypes off already produced the stride-consistent xunknown8 *a3 alongside the same a3[1].

Provenance: inspiration = "decbench:O2-noinline-libacl-libacl.so.1.1-set_acl_fd; NOVEL pool"
(kuna already ties or beats every rival decompiler on this source function and still emitted
non-compiling C, so no rival surfaced it).

Reproduction 1 — libacl set_acl_fd @ 0x56c0

kuna decompile results/full_run/O2-noinline/libacl/stripped/libacl.so.1.1.2301 --addr 0x56c0

Before / after (unchanged lines elided; the only difference in the whole function is the
parameter type):

-int4 sub_56c0(unsigned long a0,unsigned int a1,unsigned int a2,void *a3)
+int4 sub_56c0(unsigned long a0,unsigned int a1,unsigned int a2,unsigned long *a3)
 {
       if ((code *)a3[1])
         a0 = (*(code *)a3[1])(a3,a0);
       v1 = (code *)*a3;
       if ((code *)a3[2])
         (*(code *)a3[2])(a3,a0);

Stride 8 is what the source says: struct error_context is three function pointers at
+0/+8/+16
(results/full_run/O0/libacl/compiled/la-perm_copy_fd.i:5348). --option realtypes off
independently agrees: xunknown8 *a3, same a3[1].

Reproduction 2 — libacl acl_calc_mask @ 0x2820 (the width loss)

 label_287d:
-    *(void *)__errno_location() = 0x16;
+    *(unsigned int *)__errno_location() = 0x16;
...
-  *(void *)(v3 + 0x20) = 0x10;
+  *(unsigned int *)(v3 + 0x20) = 0x10;

Both are 4-byte stores; *(void *) claimed no width at all. --option realtypes off shows
*(xunknown4 *) in both places — the exact width the relabel now preserves.

gcc -fsyntax-only demonstration

Reproduction 1's emitted body, with a 12-line prelude declaring int4/int8/code and the
six libc callees, no other edits:

$ gcc -std=gnu11 -fsyntax-only before.c
before.c:24:18: warning: dereferencing 'void *' pointer
before.c:24:10: error: invalid use of void expression
...
(12 errors, 12 warnings)
exit=1

$ gcc -std=gnu11 -fsyntax-only after.c
exit=0

(The one remaining diagnostic in both is my prelude's dcgettext declaration disagreeing with
the GCC builtin — not kuna's output.)

The fix

void becomes the fallback, not the answer: a TYPE_UNKNOWN pointee now runs through the
same size table a scalar unknown uses (char / unsigned short / unsigned int /
unsigned long|unsigned long long), and only residual sizes with no natural single C type
(0, 3, 5, 6, 7, 10, …) degrade to void under a pointer. As a scalar those sizes still keep
undefined<N>, exactly as before. One call site, no second mapping introduced.

RealTypeCtx::OFF and the whole --option realtypes off path are untouched.

Genuine TYPE_VOID pointers are unaffected. They are not residual unknowns, so
realtype_relabel already returned None for them — provable from the fact that they render
void * with realtypes off too. In the libacl whole-library run the 35 surviving void *
occurrences are byte-identical to the realtypes off run (free(void *),
memcpy(void *, void *, uint8), malloc, realloc, void *v1; // rax, …), the only
difference being the one parameter this PR fixes. Pinned by a new unit test,
realtypes_keeps_genuine_void_pointers, which asserts void / void * / void ** are
identical with the gate ON and OFF.

Whole-binary scale

kuna decompile-all --mode reliable, same build except the one-line policy:

binary *(void *) casts before after void * occurrences before after emitted lines
libacl.so.1.1.2301 150 0 260 35 3,552 (unchanged)
gzip 187 1 461 73 9,093 (unchanged)
mydoom.exe 61 0 145 0 5,489 (unchanged)
mirai 315 0 606 43 9,396 (unchanged)
coreutils fmt 24 0 113 47 3,898 (unchanged)
total 737 1 1,585 198 —

Line counts are identical binary-for-binary, as expected for a pure rendering change.

The single residual cast is gzip's *(void *)&v3[4] = *v4;, where the pointee is a 10-byte
x87 long double (realtypes off renders it *(undefined10 *)). Size 10 has no natural
single C type, so void is the designed fallback there.

For libacl specifically the post-fix *(...) cast census (93 *(unsigned long *),
54 *(unsigned int *), 3 *(char *)) matches the realtypes off census
(93 *(xunknown8 *), 54 *(xunknown4 *), 3 *(xunknown1 *)) one-for-one — the declaration
and the expressions now agree everywhere the off path already did.

Why no new flag

realtypes is already a named, default-ON option (DIV-6). This PR does not add a decision
point; it corrects what that option does. Per CLAUDE.md, "a strict bug fix that only corrects
wrong output needs no flag", and the output being corrected does not compile (error: invalid use of void expression) and silently discards access width. There is also no coherent
"keep the old behavior" setting to expose: the old rendering was self-contradictory with
expressions the same printer emitted, and the stride-preserving spelling is already what
--option realtypes off produced. Recorded as DIV-47 in docs/history.md.

Tests

tests/stages/ghdec-realtypes-pointee.xml — real machine code (libacl set_acl_fd @ 0x56c0
and acl_calc_mask @ 0x2820, callees stubbed at their real addresses), two passes: default,
then option realtypes off.

tests/stages/gh6930-inferfuncentry.xml — two assertion regexes updated. They pin
bufferevent_setcb(*(void *)(v2 + 0x10),0,…); the cast is incidental context for that test
(its subject is 0x100000 resolving to evhttp_write_cb) and is exactly the width-losing
form this PR fixes, so it now reads *(unsigned long *)(v2 + 0x10). Both assertions still
pass under their original names, so docs/baseline-stages.json records no rename.

realtypes_relabels_unknown_bases (unit) updated for pointer-to-unknown;
realtypes_keeps_genuine_void_pointers (unit) added.

docs/baseline.json is not re-pinned — the datatest corpus does not move.

Gates

Gate Result
make test PARITY OK — 675/675 (docs/baseline.json untouched)
make test-stages PARITY OK — 321/321 (+7 newly passing, all from the new file)
make rust-test green (exit 0)
make check-spec OK, lenient and --strict
kuna catalog --check catalog OK: documents exactly the registered kuna options

decompiler/crates/kuna-base/src/xml.rs corpus file count bumped 176 → 177.
docs/spec/09-emission.md (§9.2 literal format) describes the new pointee rule, the void
fallback and the TYPE_VOID exemption in prose.

Speed

decompile-all --mode reliable, 7 interleaved before/after pairs. The box was heavily
contended (several concurrent builds), which makes the medians bimodal; the least-contended
run per side is the robust estimator:

binary before (min) after (min) delta
gzip 2,924 ms 2,939 ms +0.51%
libacl 792 ms 793 ms +0.13%
mirai 4,651 ms 4,582 ms -1.48%

Well inside the +5% budget — as expected, the change is one match arm on a rendering path.

🤖 Generated with Claude Code

https://claude.ai/code/session_011RXVU58TKJxb518oViChwo

mahaloz and others added 3 commits August 1, 2026 19:40
…ealtypes relabel

`realtype_unknown_base` short-circuited to `void` for EVERY size when the
residual `TYPE_UNKNOWN` base sat under a pointer modifier, so `undefined1 *`,
`undefined4 *` and `undefined8 *` all rendered `void *`.

Only the DECLARATION was relabelled.  `opLoad` / `opPtradd` keep building index
and cast expressions from the ORIGINAL pointee size, so the declaration and the
expressions disagreed about the stride:

    int4 set_acl_fd(unsigned long a0,uint a1,uint a2,void *a3)
          if ((code *)a3[1])              // byte offset 1 under GCC's
            a0 = (*(code *)a3[1])(a3,a0); // void-arithmetic extension; the
                                          // expression was built for offset 8

`gcc -std=gnu11 -fsyntax-only` on that body reports 12 "invalid use of void
expression" errors and 12 "dereferencing 'void *' pointer" warnings.  The same
short-circuit dropped the access width from stores (`*(void *)
__errno_location() = 0x16;` for a 4-byte store).

The relabel now runs a `TYPE_UNKNOWN` pointee through the SAME size table a
scalar uses -- `undefined8 *` -> `unsigned long *`, `undefined4 *` ->
`unsigned int *` -- and keeps `void` only as the fallback for residual sizes
with no natural single C type (0, 3, 5, 6, 7, 10, ...).

P5 never mis-typed anything: `--option realtypes off` already produced the
stride-consistent `xunknown8 *a3` with the same `a3[1]`.  This was purely the
P9 presentation policy, and the off path is untouched.  A genuine `TYPE_VOID`
pointee is not a residual unknown and never enters the relabel, so
`free`/`malloc`/`memcpy`/`realloc` keep their opaque `void *` (pinned by a new
unit test).

Strict bug fix inside an existing default-on option (DIV-6), so no new flag;
recorded as DIV-47.

Measured on 5 whole-binary `decompile-all --mode reliable` runs (libacl, gzip,
mydoom, mirai, coreutils fmt): `*(void *)` cast sites 737 -> 1, `void *`
occurrences 1,585 -> 198, emitted line counts byte-for-byte unchanged.  The one
residual is a 10-byte x87 `long double` pointee, where `void` is the designed
fallback.  Least-contended-run timings over 7 interleaved pairs: gzip +0.51%,
libacl +0.13%, mirai -1.48%.

Gates: make test PARITY OK (675/675, baseline untouched), make test-stages
PARITY OK (321/321), make rust-test green, make check-spec OK (strict),
kuna catalog --check OK.

inspiration = "decbench:O2-noinline-libacl-libacl.so.1.1-set_acl_fd; NOVEL pool"

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011RXVU58TKJxb518oViChwo
@mahaloz mahaloz changed the title [AUTOMATED] fix(p9): (NOVEL) keep the unknown pointee's size in the realtypes relabel [AUTOMATED] fix(p9): (NOVEL) realtypes keeps an unknown pointee size (DIV-48) Aug 1, 2026
…-size

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
#	tests/stages/README.md
@mahaloz
mahaloz merged commit 41c2692 into main Aug 1, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/realtypes-pointee-size branch August 1, 2026 19:59
mahaloz added a commit that referenced this pull request Aug 1, 2026
…#235)

* [AUTOMATED] fix(p8): (NOVEL) print the entry component first (DIV-48)

Port `BlockGraph::orderBlocks` (block.hh:437) and call it where upstream
does, as the first line of `ActionFinalStructure::apply` (blockaction.cc:2192).

Structuring does not always end with a single top-level component; when it
leaves several, the printer walks the root component list in order.  kuna had
never ported the sort that puts that list in final order, so the list stayed in
COLLAPSE order -- whichever component the collapse happened to append first
printed first.  A function could therefore open in the middle of its body and
reach its real entry only after an unconditional `goto`, i.e. emit the entry as
unreachable dead code.  On the campaign case (libopencm3 sdram `main`) the body
opened on the 'p'/'P' command handler and printed
`clock_setup(); console_setup(); sdram_init(); ...` after `goto label_8000792;`.

The producer of the ordering key was already ported -- `BlockGraph::add_block`
keeps a composite's `index` at the minimum over its members -- so only the sort
was missing.  It now runs with upstream's key (`FlowBlock::compareFinalOrder`,
block.cc:709): entry component (`index == 0`) first, RETURN-terminated
components last, everything else by ascending index; root graph only (upstream
does not recurse).  `FlowBlock::lastOp` gains its missing `t_copy` /
`t_goto` / `t_multigoto` overrides so the RETURN half of the key can resolve a
structured component through the `BlockCopy` mirror into `bblocks`; the sort is
stable, so the several RETURN-terminated components upstream's comparator
reports as equal keep a deterministic order rather than `std::sort`'s
unspecified one.  Both structuring engines are covered: `CollapseStructure` and
the default-on region structurer hand `ActionFinalStructure` the same root list.

Strict bug fix restoring upstream behavior -- no option (the current output is
unambiguously wrong C).

Gates: 675/675 PARITY OK, 324/324 stages PARITY OK (delta is purely the 6 new
keys -- no re-pin, no per-test opt-out), rust-test green, check-spec OK
(strict), catalog OK.

Measured over 12 decbench binaries / 3,434 functions: "unconditional goto
followed by a statement" 25 -> 0, "body opens with a label" 29 -> 3 (the 3
residual labels sit at the entry block's own address, a legitimate back-edge
target).  Blast radius 30 of 3,434 functions (0.87%) change text at all, and
the whole changed set keeps its goto count and line count exactly (1,172 gotos
/ 16,123 lines before and after) -- the statements are a permutation.  Speed,
interleaved medians of `decompile-all --mode reliable`: O0 diffutils `diff`
7.38 s -> 7.07 s, O2 `bzip2` 15.06 s -> 15.18 s (+0.8%).

inspiration = "decbench:O0-libopencm3-sdram-main; NOVEL pool"

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011RXVU58TKJxb518oViChwo

* [AUTOMATED] renumber DIV-48 -> DIV-49 (DIV-48 taken by #232)

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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