Skip to content

Analysis-tier: RISC-V64 PLT import-name e2e (linked fixture) - #16

Merged
mahaloz merged 2 commits into
mainfrom
riscv64-plt-e2e
Jun 23, 2026
Merged

mahaloz merged 2 commits into
mainfrom
riscv64-plt-e2e

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

What

Proves elf_plt.rs's RISC-V PLT-veneer decoder (decode_riscv) end-to-end on a real, linked, dynamically-linked RISC-V64 executable. The RISC-V arm of the per-arch PLT decoder shipped with the original plt-got increment (plus a synthetic unit test pinning its arithmetic), but the decode e2e had only ever run on x86 fixtures. A dev container now carries a RISC-V64 toolchain + linker, so this PR vendors a linked RISC-V64 fixture and adds the e2e gate.

No elf_plt.rs change was needed — the RISC-V decoder, build_got_name_map (R_RISCV_JUMP_SLOT → .dynsym name), and the bootstrap_from_elf arch auto-detect (RISCV:LE:64:default → riscv.lp64d.sla) resolve the imports as-is. This is purely additive coverage (one linked fixture + one console e2e); the XML datatest oracle is structurally untouched (the <binaryimage> path never constructs an ObjectLoadImage).

BEFORE

PLT decode was unproven on RISC-V — no linked RISC-V fixture existed in-tree, so the full ELF parse → GOT-name-map → stub-decode → funcsym install → decode → print path was never exercised on a real RISC-V binary (only a synthetic-bytes unit test of the decoder arithmetic + x86 e2e fixtures).

AFTER

Linked RISC-V64 binary plt_riscv64 (dynamic PIE, RVC, lp64d, not stripped; riscv64-linux-gnu-gcc 11.4.0 -O0). main (0x6b8) calls puts@plt (0x5e0 → GOT 0x2020) and printf@plt (0x5f0 → GOT 0x2028); both are R_RISCV_JUMP_SLOT relocs in .rela.plt. kuna decompile plt_riscv64 main:

unsigned long main(int4 a0)

{
  puts("hello");
  printf("%d\n",(int8)a0);
  return 0;
}

The imports render as puts/printf (not sub_5e0/sub_5f0); the .rodata string constants are recovered too. The e2e actually decodes (real RISC-V/RVC instruction stream, not a skip): with the RISC-V .sla absent the bootstrap returns early (visible skip); reaching the assertions means the engine decoded main.

Changes

  • decompiler/crates/kuna-analysis/tests/fixtures/plt_riscv64 (+ plt_riscv64.c source) — vendored linked fixture (8520 bytes).
  • decompiler/crates/kuna-console/tests/verify_riscv64_plt.rs — the e2e gate, modeled on verify_w11_elf_plt_names.rs (x86), same specs-absent skip guard.
  • decompiler/crates/kuna-analysis/tests/fixtures/README.md — fixture row + provenance (exact build command, riscv64-linux-gnu-gcc 11.4.0).
  • docs/analysis-port-log.md — Increment 20 entry + the plt-got work-list row now notes RISC-V e2e coverage.

Gates

  • make test → 675/675 PARITY OK
  • make test-stages → 158/158 PARITY OK
  • make rust-test → green (incl. the new verify_riscv64_plt e2e: 1 passed, ran a real decode)

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 23, 2026 15:34
Proves the elf_plt.rs RISC-V PLT-veneer decoder (decode_riscv) end-to-end on a
real, linked, dynamically-linked RISC-V64 executable. The RISC-V decoder shipped
with the original plt-got increment (with a synthetic unit test), but the proof
had run only on x86 fixtures; no linked RISC-V binary existed in-tree to exercise
the full ELF parse -> GOT-name-map -> stub-decode -> funcsym install -> decode ->
print path.

No elf_plt.rs change was needed: the RISC-V decoder, build_got_name_map
(R_RISCV_JUMP_SLOT -> .dynsym name), and the bootstrap_from_elf arch auto-detect
(RISCV:LE:64:default -> riscv.lp64d.sla) resolve the imports as-is. Purely
additive coverage (one linked fixture + one console e2e); the XML datatest oracle
is structurally untouched.

- Fixture: plt_riscv64 (8520 bytes, source plt_riscv64.c) -- a dynamic RISC-V64
  PIE built with riscv64-linux-gnu-gcc 11.4.0 (-O0, not stripped). main (0x6b8)
  calls puts@plt (0x5e0 -> GOT 0x2020) and printf@plt (0x5f0 -> GOT 0x2028);
  both are R_RISCV_JUMP_SLOT relocs in .rela.plt naming puts/printf, and the
  stubs are the textbook auipc t3; ld t3,lo(t3); jalr t1,t3; nop veneer.
- Test: kuna-console/tests/verify_riscv64_plt.rs, modeled on
  verify_w11_elf_plt_names.rs (same specs-absent skip guard). Asserts main
  decoded (real, not skipped) and that puts(/printf( are named while
  sub_5e0/sub_5f0 are gone.
- Docs: fixtures/README.md row + provenance (build command); analysis-port-log.md
  Increment 20 + the plt-got work-list row notes RISC-V e2e coverage.

Gates: make test 675/675 PARITY OK; make test-stages 158/158 PARITY OK;
make rust-test green.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit 0929bb4 into main Jun 23, 2026
@mahaloz
mahaloz deleted the riscv64-plt-e2e branch July 29, 2026 08:01
mahaloz added a commit that referenced this pull request Sep 25, 2026
… buffer; castternary reads a typed global's element; a stored counter keeps its sign

- A negation (p * -1, the right-hand side of a pointer difference) is not a
  number use, and an add partner dereferenced through copies or a small
  literal offset is the base: put1(char *buf, size_t len, ...) keeps len a
  number (libedit keymacro__decode_str too).
- A later type pass that finds a parameter or call return an earlier pass
  typed indexing another base blocks it and restarts the function.
- castimplied reads the element type of dat_X[i] from the header declaration
  of an unnamed global typed a pointer, so castternary drops the (int) the
  conditional performs.
- A value stored through a pointer elemptr typed keeps its own sign (an int
  counter stored into an unsigned table stays int).
- Fixture gains w_put and w_ctr (stage test #12-#16, round trip line 3).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01V6bguCkPS65cu9ejj1fbpy
mahaloz added a commit that referenced this pull request Sep 29, 2026
…re a fold rewrites, and a carry

w_fold16/w_fold8/w_neg16/w_sub16/w_not16/w_xor16 compare a value computed
with + - ~ ^ against a constant a fold moves onto the stored value;
w_carry16 tests a carry. gcc -O0 fixture: main and the previous head DIFFER
(`hsink = a0 * 3; v1 = hsink == -1;`), this head AGREEs. Stage assertions
#15/#16: previous head 13/16, this head 16/16.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JiRmwW65ibgRi4sd1CkDiE
mahaloz added a commit that referenced this pull request Sep 29, 2026
/#16 (1502 -> 1504); footprint for readers a later rule makes sign-sensitive

The stages baseline gains only the two new globalvalue keys. Footprint: the
previous revision to this one changes 0 of 11,656 functions on 39 disjoint
binaries and no castbench file; main to this revision on the 12 added
binaries is 14 of 3,287, each classified, including the equivalent extra
store in scp O2 0xb730.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JiRmwW65ibgRi4sd1CkDiE
mahaloz added a commit that referenced this pull request Oct 2, 2026
…s its zero-extension (#794)

## The problem

On a 32-bit target, a function that returns a 64-bit integer with its high word set to zero is printed as returning only its low word, typed as a signed `int`. A caller that widens the result then sign-extends it.

```sh
printf 'typedef unsigned long long u64;\nu64 ins16(unsigned a, unsigned short b) { return ((a + 1) << 16) | b; }\n' > ins16.c
clang --target=arm-linux-gnueabi -O2 -c ins16.c -o ins16.o   # orr r0,r1,r0,lsl #16; mov r1,#0; add r0,r0,#65536
kuna decompile-all ins16.o
```
```c
int ins16(int a0,unsigned int a1)
{
  return (a1 | a0 << 0x10) + 0x10000;
}
```

`ins16(0x7fff, 0)` is `0x80000000`, but through the printed prototype `(unsigned long long)ins16(0x7fff, 0)` is `0xffffffff80000000`. MIPS and PowerPC little-endian, RISC-V 32 and i386 (`xor %edx,%edx; ret`) print the same `int`, and `u64 f(int a, int b) { return (unsigned)(a + b); }` prints `int f(...) { return a1 + a0; }`.

## The fix

- Sub-variable flow no longer trims a RETURN that reads a 64-bit integer held in two output registers: a value of at most eight bytes, wider than any single integer output register of the prototype model, with its high word where the register file's byte order puts it. That covers the joins `r1:r0`, `$3:$2` and `EDX:EAX`, and SPARC's `o0_1`, a register that names both halves. With the high register zero the RETURN reads `ZEXT(lo)`, and `RuleSubvarZext` used to cut it back to the low register.
- The function now prints `unsigned long long ins16(int a0,unsigned int a1)`. A word whose C type is signed gets the cast that makes C zero-extend it: `return (unsigned int)(a1 + a0);`.
- 16-byte pairs (x86-64 `RDX:RAX`) keep the trim, because a zeroed `RDX` there is almost always `-fzero-call-used-regs` after an `int` function. Where a 32-bit function's bytes are exactly those of a zero-extended return, it now reads `unsigned long long` though the source returns a word: an i386 `int` function scrubbed that way, gcc -O0's `int f(u64 a) { return a >> 32; }`, or ChibiOS's `chEvtGetAndClearEvents`, which zeroes `r1` for `msr basepri` before returning. The value is unchanged, and `--option returnpair single` gives the word back.

What this does not fix:

- Big-endian pairs (ARM, MIPS, PowerPC, SPARC) are joined with their halves swapped on main, which #763 fixes. With this PR alone their output does not change. On a merge with #763, `ins16` round-trips on all four.
- A caller that returns the callee's low word with only the high word changed, `return zsum(a, b) | 0xff00000000ULL;`, now prints `void` in `decompile-all`, as single-function `kuna decompile` already does on main and as main does for any 64-bit callee (#808). Main's `decompile-all` printed a return, but one that sign-extends the low word.
- `u64 zx(unsigned a) { return a; }` on ARM (`mov r1,#0; bx lr`) and `long long sx(int a) { return a + 3; }` print `void` before and after (#807).

## Tests

- `tests/stages/kuna-zextpair.xml` (ARM): 5 of 6 assertions fail on main. The control passes on both.
- `kuna-cli/tests/zext_pair_returns.rs` builds ARM, MIPS and PowerPC little-endian and i386 objects. It compiles the printed C, callers included, with gcc and clang at -O0/-O2 and compares it with the source over 289 input pairs (544 wrong values on main for ARM). It also checks under `-m32` that every return is eight bytes on ARM, i386 and SPARC.
- In `passthrough_own_return.rs`, the forwarder `bl provider; mov r1,#0` now returns `unsigned long long`.
- `decompile-all` changes no function over Lua 5.3 built for nine 32-bit targets (5,015 functions, including SPARC, ARM hard-float, MIPS and PowerPC big-endian). Over 15 decbench ARM firmware and i386 PE binaries (14,181 functions) it changes 7: one 64-bit return now right (betaflight, `lsrs r0,r0,#8; movs r1,#0`), the ChibiOS function above and four of its callers (variable numbering only), and one stretch of data decoded as code. Datatests are unchanged at 675/675. CPU time (min of 21 to 31 interleaved runs, five witnesses) moves between −4.9% and +1.4%.

Fixes #769

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

https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 6, 2026
…value

A hoisted `add x8,sp,#16` can be a leftover at one call and the real result
buffer at a later call it reaches through a phi; remembering it from the
first veto dropped the buffer at the second. Frame addresses are now left
out of the vetoed-value record; the comparison temporaries and constants
the record exists for are unaffected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Qihrih8NnnexavqvZxp8CF
mahaloz added a commit that referenced this pull request Oct 7, 2026
…follow

A guarded wider store whose pointer may also be alloca'd stack
(`p = n >= 16 ? alloca(n * 8) : buf; p[j % 4] = 3`) was checked by no
layout check, so the guard left buf as scalars and the C indexed past one
(`v1 = &v3; v1[a1 & 3] = 3`), wrong where main is right. A pointer that
comes from the stack base along a path pointer_pieces cannot resolve now
withdraws the guard, and a choice of stack addresses (two frame arrays, a
frame array or a pointer from a call or a global) is checked at every
stack address it may index from, as a single base is. Loop walks are left
to the final check.

New stage asserts: m8 with clang -O2 on x86-64 (#16) and AArch64 (#9)
store through the frame array, not a scalar, in both passes. Spec
chapters 03 and 06 and the option summary describe the check; stages
baseline re-recorded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
mahaloz pushed a commit that referenced this pull request Oct 18, 2026
Windows i386 wide arguments are placed four bytes too far into the stack after an integer or hidden result pointer.

```sh
cat > sample.c <<'C'
struct wide { double real, imag; };
struct wide copy_wide(struct wide value) { return value; }
C
i686-w64-mingw32-gcc -O0 -g -shared sample.c -o sample.dll
kuna decompile sample.dll copy_wide --sleighpath specs --json
```

The argument's incorrect JSON storage is:

```json
"stack_offset": 12
```

The machine code reads it at entry ESP+8. Consequently, generated component stores use an uninitialized word and shifted fields.

- Let stack resources disable datatype alignment with `stackalign="false"`.
- Apply it to Windows i386 argument resources, preserving four-byte slots and the datatype's object alignment.
- Keep other compiler specifications unchanged and reject malformed resource annotations.

Tests check allocator/parser behavior and real PE argument offsets, then execute regenerated hidden-result and leading-integer cases with GCC and Clang at two optimization levels.


Stage-test expectation changes: MSVCBYVAL #16 now expects Take4(5,v1) (previously declined due to the wrong padding) and #8 expects the two Pair copy-constructor lines.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018dDZ5j7DLzPHKAXC7p8wtX
mahaloz added a commit to coleleavitt/kuna that referenced this pull request Nov 9, 2026
…rn-address test

ptrauthtrap (default on, now on every AArch64 path) cut the CBRANCH edge into
a brk #0xc470..0xc473 block whenever the guard was the x30 bit test, without
asking which arm the trap sat on or where x30 came from:

- Polarity: `tbz x16, Noelo-Lab#62, trap` (trap when bits 62 and 61 agree, i.e. on
  every authentic return address) lost its trap, so a function that always
  crashes printed as its never-taken arm (`secret(); return 7;`). The
  condition's polarity is now followed through the INT_EQUAL/NOTEQUAL/
  BOOL_NEGATE chain and the CBRANCH's boolean flip, and only the arm taken
  when the bits disagree is cut.
- Provenance: "unwritten in the block" let a `mov x30, x1` (or arithmetic)
  one block earlier through, and any LOAD counted as the frame reload, so
  `ldr x30, [x0]` passed too. x30 must now be unwritten anywhere in the
  function, or reloaded by a LOAD whose address is sp or x29 plus constants.

The real shapes (ldp x29,x30 post-index or offset, ldr x30 [sp],Noelo-Lab#16,
ldur x30 [x29,#8], mov sp,x29 epilogue, tbz and tbnz forms, leaf with x30
untouched) are still cut; all 52 AArch64 repo samples decompile byte-
identically to the previous head. Tests: tests/stages/kuna-ptrauthtrap.xml
#14 (inverted polarity), Noelo-Lab#15 (forgery in an earlier block), Noelo-Lab#16 (non-frame
load); all three fail on the previous head. Stages baseline: the 3 new keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKS1xSN6UHyTiGivmGe
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