Skip to content

[AUTOMATED] Preserve declared indirect-call prototypes and ABI - #804

Merged
mahaloz merged 18 commits into
Noelo-Lab:mainfrom
mgostIH:fix/typed-indirect-call-prototypes
Oct 2, 2026
Merged

mahaloz merged 18 commits into
Noelo-Lab:mainfrom
mgostIH:fix/typed-indirect-call-prototypes

Conversation

@mgostIH

@mgostIH mgostIH commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Accepted function-pointer fields lose their receiver, float argument and float
return type when called through an object's vtable. This synthetic ELF needs
only Python's standard library to reproduce it:

python3 - <<'PY'
import struct
code = bytes.fromhex('53 488b07 ff10 5b c3 53 488b07 ff5008 5b c3')
ident = b'\x7fELF\x02\x01\x01' + bytes(9)
size = 0x1000 + len(code)
header = struct.pack('<16sHHIQQQIHHHHHH', ident, 2, 62, 1, 0x401000, 64, 0, 0, 64, 56, 1, 0, 0, 0)
segment = struct.pack('<IIQQQQQQ', 1, 5, 0, 0x400000, 0x400000, size, size, 0x1000)
open('virtual.elf', 'wb').write((header + segment).ljust(0x1000, b'\0') + code)
PY
kuna decompile-all virtual.elf --addr 0x401000 --addr 0x401008 --mode reliable --assert-strict \
  --assert 'function 0x401000-0x401008=read_scale' \
  --assert 'function 0x401008-0x401011=set_scale' \
  --assert 'typedef struct VirtualTable { float (*get_scale)(void *node); void (*set_scale)(void *node, float scale); };' \
  --assert 'typedef struct VirtualNode { struct VirtualTable *vtable; };' \
  --assert 'prototype read_scale float read_scale(struct VirtualNode *node)' \
  --assert 'prototype set_scale void set_scale(struct VirtualNode *node, float scale)'

The setter currently emits (*node->vtable->set_scale)(); despite strict
acceptance. It should emit (*node->vtable->set_scale)(node,scale);.

Fix

  • Apply attached code-pointer prototypes to indirect calls, including input and
    output commitment after late restriction.
  • Preserve the complete prototype across restarts, including its ABI/storage;
    retain explicitly named calling conventions when parsing pointer fields.
  • Honor existing call-site input locks and leave untyped pointers unchanged.
  • A 64-bit parameter in a 32-bit register pair (ARM, MIPS, PowerPC) binds only
    to the function input, so reusing one of its registers no longer prints as a
    write into the parameter. Direct calls had this on main too:
    v._0_4_ = *o; return wide3(v,(int)v) + 1; is now k = *o; return wide3(v,k) + 1;.
  • A prototype that puts a value in a floating-point register is forced only
    where the ELF states a double-precision hard-float convention (x86 and
    AArch64 always). On soft-float ARM it is rebuilt under __stdcall_softfp:
    main's (*o->f)(0,0x40040000,k + 1) becomes (*o->f)(2.5,k + 1).
    Soft- or single-float MIPS, PowerPC and RISC-V, and images that state no
    convention (raw images, clang PowerPC or attribute-less ARM objects), keep
    the arguments kuna recovers without the declaration.
  • Those recovered arguments are also kept for a variadic prototype whose return
    or a fixed parameter is floating-point or a register pair, and for a return
    narrower than its register where neither the caller (x86, ELF or PE arm64)
    nor the model (ARM, PowerPC) extends it: char/short/bool on MIPS,
    RISC-V, Apple arm64 and arm64 images that state no platform, and int on
    RV64. Forcing those printed a dropped 0.5, a stale register-pair word,
    CONCAT31(v1,(*v2)(k)) with v1 unset, or a negative result zero-extended
    ((unsigned int)(unsigned char)(*v1)(k) * 3 on Apple arm64).

Not fixed here: direct calls and a function's own prototype on soft-float ARM
(#813), struct parameters split between registers and the stack (#814), and
direct calls with the narrow returns (#816, Apple arm64 included) or variadic
prototypes (#817) above.

Tests

  • typed_vtable_cli.rs: SysV receiver and float, explicit MSABI on a SysV
    image, and an untyped control.
  • register_pair_params.rs: direct and typed calls on ARM and big-endian
    PowerPC. All four fail on main.
  • soft_float_typed_calls.rs: soft-, single-, hard- and unstated-convention
    ARM, PowerPC and RISC-V, plus variadic calls on hard-float ARM and narrow
    returns on RISC-V, MIPS, Apple arm64 and a raw arm64 image.

Fixes #802

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU

Local Kuna Build and others added 3 commits October 1, 2026 18:24
…ed prototypes

- An unregistered calling convention on a function-pointer declarator now
  falls back to the default model, as upstream TypeDeclarator::getModel does,
  instead of failing the parse. The grammar unit tests now build a factory
  with models registered and check the convention the code type carries.
- The type factory's default and named models are re-shared whenever the
  extrapop or defaultprototype option replaces one, so they cannot go stale.
- Function-pointer code types no longer carry parameter names (as upstream):
  code types compare equal without them, so a name could leak from another
  declaration into local variable naming.
- ActionDeindirect reports the forced-prototype change so the stackstall loop
  folds the committed inputs (no more `(int)1` constant casts).
- Single override lookup in build_call_specs; temp dirs removed by the CLI
  test; spec 04 describes the forced-prototype arm, the saved FuncProto
  override and the kept conventions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz

mahaloz commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Thanks for this fix. I merged main into the branch and added one commit on top:

  • A calling convention on a function-pointer declarator that the architecture does not register now falls back to the default model, as Ghidra's TypeDeclarator::getModel does, instead of failing the parse. This was what broke grammar::tests::a_convention_on_a_function_pointer_parameter_parses_as_it_did_without_one. The grammar tests now build a factory with models registered and check which convention the cb code type carries.
  • The type factory's default and named models are shared again when the extrapop or defaultprototype option replaces one, so its copy cannot go stale.
  • Function-pointer code types no longer carry parameter names, as upstream. Code types compare equal regardless of names, so a name from one declaration could end up naming locals at calls through another.
  • ActionDeindirect now reports the change when it forces a prototype, so the loop runs its rules again over the new inputs. This removes casts like (*fp)((int)1,(int)2).
  • docs/spec/04 now describes the forced-prototype arm, the saved FuncProto override and the kept conventions. The CLI test cleans up its temp directories. The PR body now says Fixes #802, and I dropped the line saying the full suite had not been run.

mahaloz and others added 2 commits October 1, 2026 20:26
…reused

A parameter in a register pair (a join of registers) got an unlimited use
point, so its per-piece map entries linked any later write of one of the
pair's registers to a piece of the parameter. On 32-bit ARM, MIPS and PowerPC
a call that reused one of the registers printed a write into half of the
parameter, then passed the modified parameter. Limit the join's entries to
the function input, as for a single register.

Also port the calculated-bool arm of commitNewOutputs, which forced
prototypes now reach.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz

mahaloz commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] I pushed one more commit (ff8c7e4) on top of a merge of current main.

On 32-bit ARM, MIPS and PowerPC, a typed indirect call that takes a long long printed a write into half of the caller's parameter and then passed the modified value. For example, o->wide(v, 5) on armv7 printed v._4_4_ = o->wide; return (*v._4_4_)(v,5) + 1;. Direct calls with a declared prototype already did the same on main. The cause was the parameter symbol of a register pair. Unlike a single register, it was valid across the whole function, so a later write of one of its registers became a piece of the parameter. It is now limited to the function input, as for a single register. That call now prints v1 = o->wide; return (*v1)(v,5) + 1;.

The same commit makes a forced bool return a calculated boolean, as Ghidra's commitNewOutputs does. It also adds register_pair_params.rs with ARM and big-endian PowerPC cases, and a paragraph in docs/spec/06. I added a short section about this to the PR description. x86-64 output is unchanged.

mahaloz and others added 3 commits October 1, 2026 21:29
…oat convention

A function-pointer prototype that passes or returns a float was forced under
the default model, which puts floats in FP registers. On a soft-float image
that printed uninitialized d0/s0/f1 locals as the arguments and dropped the
ones the caller set. The loader now reports what the container states about
float argument passing (ARM attributes and header flags, MIPS abiflags and
GNU attributes, PowerPC GNU attributes, RISC-V header flags). A soft-float
ARM image gets the prototype rebuilt under __stdcall_softfp; other
soft-float images keep the recovered arguments; an image that states
nothing gets the declared prototype only when the function computes with
the model's float registers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
A float the default model already passes in integer registers or on the
stack (a MIPS double after an int, a variadic argument) has the same
storage under the soft-float convention, so that prototype is still
forced. Only a prototype that puts a parameter or its return value in a
floating-point entry of its model waits for the image's evidence.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz

mahaloz commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] I merged current main and pushed two commits on top (ad58ba0, 9e452d7). On soft-float images (ARM -mfloat-abi=soft or softfp, Cortex-M without an FPU, and soft-float MIPS or PowerPC), a forced prototype with float parameters put them in FP registers that the caller never sets. Calls then printed uninitialized d0/s0 locals and dropped the real arguments. The fix:

  • The loader now reports the float convention the ELF states.
  • On soft-float ARM, these prototypes are rebuilt under __stdcall_softfp.
  • Other soft-float images keep the recovered arguments.
  • An image that states no convention gets the prototype only when the function itself uses FP registers.

I added soft_float_typed_calls.rs and a section to the description. Two related pre-existing cases are tracked in #813 and #814.

mahaloz and others added 4 commits October 1, 2026 22:32
…e-precision convention

A single-float image (RISC-V lp64f/ilp32f, MIPS fp_abi 2, PowerPC
single-precision) passes doubles in integer registers, and an image that
states no convention may be ARM softfp, single-float or soft-float code
that still computes in floating-point registers, so counting FP-register
use as hard-float evidence forced double storage the caller never set.
Both now keep the arguments model recovery finds; hard-float images and
the ARM soft-float rebuild are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz

mahaloz commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Narrowed the floating-point gate and merged main again.

  • A declared prototype that puts a value in a floating-point register is now forced only where the ELF states a double-precision hard-float convention, or on x86 and AArch64. Before, single-float conventions counted as unstated, and on an unstated image any floating-point register use counted as hard-float. Both forced storage the caller never set. On RISC-V lp64f, *res = o->f(4, 2.5) printed a CONCAT44 built from an unset fa0 and stored an unset a0 into *res. An attribute-less ARM softfp object read an unset d0. A clang soft-float PowerPC function whose own prototype takes a float passed unset f2-f5. These calls now keep the arguments kuna recovers on its own, as main does.
  • This has a cost: clang PowerPC objects state no convention, so their typed float calls also keep the recovered arguments. Hard-float ARM, MIPS, RISC-V lp64d, x86 and AArch64 output is unchanged.
  • soft_float_typed_calls.rs adds RISC-V lp64f/ilp32f/lp64d, attribute-less ARM and unstated PowerPC cases. The new single-float and unstated cases fail on the previous head.
  • Shortened the description.

mahaloz and others added 2 commits October 1, 2026 23:48
…nd unextended narrow returns

A declared variadic prototype whose return or a fixed parameter sits in a
floating-point register or a register pair is no longer forced at an
indirect call: ARM passes every variadic value in core registers, a locked
variadic prototype with a floating-point return drops the floating-point
arguments past its fixed ones (x86-64, AArch64), and a register-pair fixed
parameter printed a stale word (32-bit ARM, PowerPC, RISC-V, MIPS).

A return value narrower than its register is forced only where the caller
extends it (x86, AArch64) or the model extends it by type (inttype). MIPS
states no extension (the rest of v0 printed as an unassigned piece) and
RISC-V states zero extension (a negative char/short, or int on RV64, became
a large positive value).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz

mahaloz commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Merged current main (#793, #798) and added cf20ba7, which narrows when a declared prototype is forced at an indirect call:

  • A variadic prototype whose return or a fixed parameter is floating-point or a register pair now keeps the recovered arguments. Forcing it dropped arguments: (int)o->vr(k, k + 1, 0.5) lost the 0.5 on x86-64 and AArch64, ARM hard-float discarded the call result (variadic calls use core registers there), and vj(5LL, k, 1.5) printed a stale word for 5LL on 32-bit ARM, PowerPC, RISC-V and MIPS. int (*)(int n, ...) is still forced.
  • A return narrower than its register is forced only where the caller (x86, AArch64) or the model (ARM, PowerPC, extension="inttype") extends it. On MIPS the rest of v0 printed as an unset piece, CONCAT31(v1,(*v2)(k)), and on RISC-V a signed char/short, or an int on RV64, was zero-extended, e.g. p[(unsigned int)(*v1)(k)].

Both cases print main's output again. Direct calls on main have the same two defects; they are filed as #816 and #817. Two tests were added to soft_float_typed_calls.rs, and spec §04 and the body's Fix section were updated.

mahaloz and others added 3 commits October 2, 2026 00:40
…arm64

An Apple arm64 callee extends a signed char or short return to 32 bits and
its caller reads w0 as it is, so forcing the declared prototype applied the
AArch64 model's zero extension to a sign-extended value and printed a
negative result as a large positive one. A Mach-O arm64 image (and the
AppleSilicon language) now counts as caller-extended only from 32 bits up;
narrower typed returns keep the recovered call, and an int or wider return
is still forced.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
The same caller bytes (a blr read as w0, no sxtb) are correct only when
the callee extended a narrow return, so a raw AArch64 image or one the
Ghidra front-end supplies now keeps the recovered call for a typed return
narrower than 32 bits, as a Mach-O one does. Only an ELF or PE arm64
image, whose AAPCS64 caller extends, still gets a narrow return forced.
The container fact moves to its own loader module and becomes tri-state.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz

mahaloz commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] One more change on top, for narrow typed returns on arm64.

  • An Apple arm64 callee extends a signed char or short return to 32 bits, and its caller reads w0 as it is. Forcing the declared prototype applied the AArch64 model's zero extension to that value, so return o->sc(k) * 3; printed (unsigned int)(unsigned char)(*v1)(k) * 3, which is 765 where the binary returns -3.
  • A typed return narrower than 32 bits now keeps the recovered call on a Mach-O arm64 image. It does the same on an arm64 image whose container names no platform (a raw image, the Ghidra front-end), because the same bytes are only correct under Apple's convention. ELF and PE arm64, x86, and int or wider returns on Apple arm64 are still forced as before.
  • The loader reads the platform from the container (loader/kuna_returnext.rs). I corrected the spec 04 sentence and added a Mach-O arm64 case and a raw arm64 case to soft_float_typed_calls.rs. The direct-call version of this bug is noted on [AUTOMATED] A declared narrow return is extended wrongly on MIPS and RISC-V #816.

@mahaloz
mahaloz merged commit 94ce931 into Noelo-Lab:main Oct 2, 2026
2 checks passed
mahaloz added a commit that referenced this pull request Oct 3, 2026
…d the stack is printed as overwritten (#863)

## The problem

On ARM, MIPS and RISC-V, a by-value struct parameter whose fields span registers and the stack is printed as assigned to when the function reuses one of its registers, and copying its register fields to memory is dropped.

```sh
cat > d.c <<'EOF2'
struct s3 { int x, y, z; };
extern int take3(struct s3, int);
extern int use3(struct s3 *);
int d1(int *k, int a, struct s3 s) { return take3(s, *k) + a; }
int d4(int a, int b, struct s3 s) { return use3(&s) + a; }
EOF2
clang --target=armv7-linux-gnueabihf -O2 -c d.c
kuna decompile-all d.o --assert-strict \
  --assert 'typedef struct s3 { int x; int y; int z; };' \
  --assert 'prototype take3 int take3(struct s3 s, int k)' \
  --assert 'prototype use3 int use3(struct s3 *p)' \
  --assert 'prototype d1 int d1(int *k, int a, struct s3 s)' \
  --assert 'prototype d4 int d4(int a, int b, struct s3 s)'
```

```
  s.z = s.z;
  s.y = *k;
  v2 = s.y;
  v1 = s.x;
  v3 = CONCAT44(v2,v1);
  s.x = s.z;
  return take3((s3)CONCAT48(s.x,v3),s.y) + a;
...
  s.z = s.z;
  v1.z = s.z;
  return use3(&v1) + a;
```

`d1` passes `s` unchanged in r0-r2 and `*k` in r3. `d4` stores `s.x` and `s.y` next to `s.z` before passing `&v1`. With this change they print `k_00 = *k; return take3(s,k_00) + a;` and `v1.z = s.z; v1.y = s.y; v1.x = s.x;`.

## The fix

- A struct parameter split between registers and the stack binds to its symbol only at the function input, as a register pair has since #804 and as upstream does for every join. A later write of one of its registers is a new value.
- Heritage splits a join into its pieces on the first pass that heritages any of them. On little-endian targets the first piece is the stack word, so the split waited for the stack pass; by then every read of the register fields was a bare input that nothing kept alive, so the stores of them were removed as dead.

Not fixed: when the function stores the register fields just below the stack part and passes pointers into the result to a call (gcc and clang do this when the parameter's address is taken), the stack part still prints as a separate local, and a value the call writes through a pointer is read back from that local (#861). AArch64 should never split such a struct (#859).

## Tests

`split_struct_params.rs` decompiles the two functions above for ARM and MIPS o32, and a RISC-V lp64d struct held in `a7` and the stack whose `a7` is reloaded for a call. All five fail on main. `decompile-all` over 14 stripped binaries (4,315 functions) is byte-identical. Over 10 unstripped ARM firmware images, where DWARF supplies the prototypes, 3 functions change: `fopencookie` and `_fopencookie_r` lose `functions.seek = functions.seek;` lines and a field-by-field copy, and `gyroReadSensorTemperature` is the case above. Decompile-all time, interleaved min of 15: +0.9% (chibios with DWARF), -2.7% (nuttx), +0.7% (cat).

Fixes #814

🤖 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AUTOMATED] Accepted function-pointer fields lose indirect-call arguments

2 participants