Skip to content

[AUTOMATED] fix(p5): float returns printed as integers and callee results printed as void - #757

Merged
mahaloz merged 18 commits into
mainfrom
fix/floatret
Oct 2, 2026
Merged

mahaloz merged 18 commits into
mainfrom
fix/floatret

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

The problem

A function returning a float in xmm0 or s0 prints as unsigned int, and one returning its callee's result (call g; ret) prints void while its callers read the result. The C then does not compile, or converts the float's bits by value.

$ cat fret.c
#include <math.h>
#include <stdio.h>
__attribute__((noinline)) float qnan(void) { return nanf(""); }
__attribute__((noinline)) float pick(float x) { return x < 0 ? qnan() : x * 2; }
int main(void) { printf("%f %f\n", pick(3.0f), pick(-3.0f)); return 0; }
$ clang -O0 -o fret fret.c -lm && strip fret && kuna decompile-all fret
void sub_1150(void)
{
  nanf((char *)0x2016);
}
    v1 = (float)sub_1150();

After: float sub_1150(void) { return nanf((char *)0x2016); } and v1 = sub_1150();.

The fix

  • A value returned in a float-class register (xmm0, s0, a whole ST0) is a float of that width, unless a value returned there is a parameter, a global, an integer computation, a NaN NAN cannot spell, or a call result no declaration makes a float.
  • In decompile-all, a void function whose result a caller reads is decompiled again to return it in the storage the callers read, no wider than every path sets. It stays void where a path hands on a wider callee result it cannot return whole, or a float from a callee nothing declares (strtod in a .o).
  • A float return is undone when any caller keeps the result as another type, together with the callees whose float it hands on.
  • float/double join the libc signature table (strtod, pow, sqrt, ...) on x86 and AArch64 only. ARM gets none: a soft-float or softfp image's callers hold a double's r0:r1 as an integer, and a hard-float d0 return would be recovered at half its width.

It does not fix: a .o wrapper of strtod, and a wrapper whose paths join before one ret after naming eax of a rax it would return, stay void, so their readers still read a void result; ARM calls to those libc functions print as before; a float a wrapper hands on from a declared return or its callee's arithmetic, when a reader keeps it as an integer (main prints the same conversion).

The tests

Ten round trips compile the printed C with gcc and clang and compare it with the binary; six fail on main, among them the repro above, a Cortex-M4F NaN and a pointer handed on past an int call (call key_type; call type_name; ret). Text tests pin a .o wrapper of strtod (x86-64 and soft-float ARM), a softfp ARM image's pow(a2,a3,a0,a1), soft- and hard-float ARM images that print a function returning strtod's result as before, a two-path wrapper that stays void rather than store half a pointer into dat_4, and a PE function whose redo restarts.

On 45 binaries (coreutils, grep, gzip, diffutils, tar and find at -O0, -O2 and -O2-noinline), casts go from 31,805 to 31,375 and uses of a void result from 2,123 to 400. On those and 76 others (more coreutils, openssh, gnutls, PE malware, ARM firmware), float conversions of an integer call go from 394 to 336 and no other wrong-output counter rises. decompile-all CPU time, as the median of the per-pair ratio over 11 paired runs on a shared machine: -O2 fmt +3.4%, ls -0.1%, sort -0.6%, grep +3.0%, bash +0.1%; -O0 fmt +10.2%, ls +3.3%, sort +4.0%, grep +6.1%, tar +2.7%. fmt -O0 decompiles 42 of its 191 functions a second time.

🤖 Generated with Claude Code

https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt

@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Sep 29, 2026
@mahaloz
mahaloz force-pushed the fix/floatret branch 5 times, most recently from 0aec6b5 to 80367b4 Compare September 29, 2026 23:19
@mahaloz mahaloz added full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) and removed full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) labels Sep 30, 2026
@mahaloz
mahaloz force-pushed the fix/floatret branch 17 times, most recently from f4ad690 to b66c372 Compare October 2, 2026 18:58
mahaloz and others added 5 commits October 2, 2026 20:06
…ults printed as void

A value a function returns in a float-class register is a float of that
width; a void function whose result a caller reads is decompiled again
returning it in decompile-all; a float return any reader keeps as another
type (an integer, a pair of floats copied into a uint64_t) is withdrawn,
together with the callees whose float a withdrawn wrapper hands on; every
value returned in the register answers for the float vote; a caller
converts a refused statement only between an integer and a pointer of one
width. Strict correctness fix, no option.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…eturned whole or not at all

On a 32-bit target a redone function returned eax alone where its callers read
edx:eax: the planted eax trial kept heritage from giving the edx the function
writes its own trial (i386 `call zsum; or $0xff,%edx` lost its 0xff), and the
model dropped the edx of `call ins16; ret` as a clobbered register, leaving the
low half of a 64-bit value. Heritage now skips only the pieces planted, and a
forced return the model maps in part is dropped whole.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…er a restart is void

A forced redo whose first pass returned eax and whose pass after a restart
refused it kept the first pass's output: Funcdata::clear does not clear an
unlocked output in the port, and ActionOutputPrototype leaves the output alone
when the RETURN has no value, so minipig O0 sub_401937 printed
`unsigned int sub_401937(..) { ... return; }`. void_unless_returned clears the
unlocked output of a forced function whose first live RETURN carries no value
once the returns are built, as upstream's updateOutputTypes does.

Fixture floatret_restart_pe_i386.exe (hand-assembled PE32, generator .py) and
test a_return_refused_after_a_restart_leaves_the_function_void.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
… speed, gates)

Castbench and the 54 disjoint binaries print byte-identically to the
363123e round on both arms. The restart fix changes 4 of 73,880
functions, the minipig O0 sub_401937 chain, each to main's text. Paired
speed vs main 94ce931: fmt +3.6% min / +3.7% pair median, ls +0.4%,
sort +0.3%, bash +2.5%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz and others added 13 commits October 2, 2026 20:06
…ning its spelling

The MIPS half of a_float_handed_on_through_wrappers_to_an_integer_round_trips
pinned `dat_4120e0 = sub_400898(a0,a1);`. Since #758 the reader keeps the
call's result in its own variable before the store and the shift
(`v2 = sub_400898(a0,a1); dat_4120e0 = v2; dat_4120e8 = v2 >> 0x10;`), which
is right and still float-free. The test now compiles the printed chain with
gcc and clang, maps the fixture's float array at a 32-bit address, and checks
the reader stores the bits of 2.25f (`40100000 0 4010 0`); the float chain the
withdrawal fixes printed stores 2 (`2 0 0 0`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…nt, typesweep, speed, gates)

Re-measured against main 3c08fcf (#758): castbench 31,989 -> 31,559
(0.846x -> 0.834x IDA); every function #758 leaves alone prints as on the
94ce931 round on both arms, and the ones it changes carry the same delta;
typesweep 1,674 -> 1,674 (4 better, 2 worse, grep kwsinit as before); paired
speed fmt +3.3%, ls -2.3%, sort +0.8%, bash +0.6% (min of 11). The fixture
README row describes the MIPS round trip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…-object libc wrapper residue

The rebase onto #821 changes no output on either arm (castbench 45 and the
76 disjoint binaries byte-identical), so the 3c08fcf numbers stand; gates
re-run on 2f02b3f. Known residue: in a relocatable object kuna has no
prototype for strtod, so a wrapper whose reader keeps the bits as an integer
returns `unsigned long` around `return strtod(..)`, the conversion main
already prints for a direct reader there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…ed float callees' wrappers void, no double libc rows on softfp ARM

- A forced return is planted at the storage its callers read unless an op
  names storage reaching outside it: `call key_type_plain; call type_name;
  ret` named only eax and returned `unsigned int` around a pointer.
- A wrapper asks a void callee for all of what its own callers read.
- A trial narrower than the callers' read is refused where a path returns a
  callee result wider or of unstated width; a trial that may carry a float is
  refused where a path returns what a named callee nothing declares returns
  (a relocatable object's strtod). Each function's recovered return storage
  is recorded for its callers.
- The float/double libc rows are seeded only where a declared double is laid
  out in the registers the image passes it in (not -mfloat-abi=softfp ARM).
- Tests: a pointer handed on past an int call round trips; undeclared float
  callee wrappers stay void (x86-64 and soft-float ARM .o); softfp ARM
  imports keep their core-register arguments and results.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
The float/double libc rows are seeded only on x86 and AArch64, whose
return recovery reads a double whole. On a soft-float ARM image a double
travels in r0:r1 and callers hold it as a 64-bit integer: the rows made
`clampd` (strtod's result or 0) `double`, which its reader converted by
value, where without them it returned the right CONCAT44 bits. On a
hard-float image the default model has no d0 output, and the rows made the
same function `unsigned int` returning the low half of d0. ARM now prints
these imports and their callers as main does.

Test: arm_images_get_no_double_libc_rows (soft and hard linked images;
fails on the previous head, which printed `double sub_10224` and
`unsigned int sub_101f0 ... SUB84(v1,0)`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
gcc -O1's `wrap` tests `pick`'s int and returns one of two `getname`
calls' pointers through a shared `ret`. Heritage refines the planted rax at
the eax the body names, each half gets its own MULTIEQUAL, and no rule joins
them again: the redo returned CONCAT44(dat_4,v2) and stored the upper half
of the pointer into `dat_4`, a register piece printed as a global, on each
path. Such a trial is refused and the function stays void, as main prints
it. No function of the 121-binary footprint changes.

Test: a_return_merged_in_pieces_stays_void (floatret_twopath_gcc_O1).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz merged commit 1013672 into main Oct 2, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/floatret branch October 2, 2026 20:55
mahaloz added a commit to phix33/kuna that referenced this pull request Oct 2, 2026
Brings in Noelo-Lab#757. Corpus count 381 (main 380 + kuna-stackstoreguard);
Funcdata::clear() keeps both sides; stages baseline re-recorded
(1700 = main 1697 + the 3 STACK STORE keys).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
mahaloz added a commit that referenced this pull request Oct 2, 2026
…llee result prints void (#855)

A function that returns a callee's 64-bit result with only the high word changed printed as `void`, and the call's result was thrown away. The same happened to a sign-extended call result and to a call result next to a constant high word.

```sh
printf 'typedef unsigned long long u64;\nu64 full(unsigned a) { return ((u64)a << 32) | (a * 3u); }\nu64 full_or(unsigned a) { return full(a) | 0xff00000000ULL; }\n' > fo.c
clang --target=arm-linux-gnueabi -O2 -fno-inline -c fo.c -o fo.o
kuna decompile-all fo.o
```
```c
// Function: full_or @ 0x40000c
void full_or(int a0)
{
  full(a0);
}
```

`full_or` is `bl full; orr r1,r1,#255; pop {r11,lr}; bx lr`, so `r0` comes back from `full` as it is. With this change it prints `unsigned long long full_or(int a0) { return full(a0) | 0xff00000000; }`, and `kuna decompile fo.o full_or` (also `void` before) prints the same.

## The fix

- Return recovery refused `r0` because at the return it is the call's untouched result, and the output model never returns `r1` without `r0`. The new `p4_calls/kuna_retcallhalf.rs` accepts the first register when, at every return, it is a call's untouched result, the second register is computed and read only by the return, and the model then returns exactly those two registers.
- The second register must not be a value that also reaches the return of a function returning one register or none: the other output of a two-register instruction (an `smull` low word), a restore or reload from the frame (gcc's `pop {r1,r2,r4,pc}`, an `-O0` reload), an argument for a later call or `svc`, and the `-fzero-call-used-regs` zero.
- No option: the rule only fires where the function prints `void` today while computing a register only a pair return can use.

Not fixed here: a byte write such as gcc's i386 `or $0xff,%dl` (#852), i386 code that pushes its arguments and never names `%eax`, in single-function mode (#853), and big-endian pairs. On Cortex-M, a caller that builds the high word from a narrower piece of the callee's own (`uxtb r1,r1`) now returns eight bytes, but the call still loses that piece (#851, which already shows on main).

## Tests

- `tests/stages/kuna-retcallhalf.xml` (ARM): 5 of 7 assertions fail on main; the other two are the controls (the `smull` word, the hardening zero, the dummy pop and the `svc` argument stay `void`; a low-word-only caller keeps `int`).
- `kuna-cli/tests/call_result_pair_returns.rs` builds ARM, MIPS and i386 objects, compiles the printed C with gcc and clang at -O0 and -O2, and checks it against the source. It fails on main.
- `decompile-all` over 31 stripped ARM, i386, MIPS and x86-64 binaries (26,265 functions) changes one function: in cf2 (-O2 -fno-inline), a function that tail-calls libgcc's `__fixunssfdi` now returns its result. `__fixunsdfdi` and `__fixunssfdi` themselves already return theirs on main since #757. `decompile-all` time over five of them is unchanged (min of 11, user+sys: 39.1 s on main, 38.5 s with the change).

Fixes #808

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant