Skip to content

[AUTOMATED] Read PowerPC64 ELFv2 stack arguments after the 32-byte frame header - #1592

Open
thotypous wants to merge 4 commits into
Noelo-Lab:mainfrom
thotypous:fix/ppc64le-elfv2-stack-params
Open

thotypous wants to merge 4 commits into
Noelo-Lab:mainfrom
thotypous:fix/ppc64le-elfv2-stack-params

Conversation

@thotypous

Copy link
Copy Markdown
Contributor

Problem

On ppc64le, a call's ninth integer argument (the first one passed on the stack) is lost, and a callee reads its ninth parameter from an unwritten slot. This independent example is clang -O1 for powerpc64le-linux-gnu, with f9 and caller in separate translation units:

unsigned f9(unsigned a, unsigned b, unsigned c, unsigned d, unsigned e,
            unsigned f, unsigned g, unsigned h, unsigned i) { return a + i; }
unsigned caller(unsigned n) { return f9(n, 2, 3, 4, 5, 6, 7, 8, n + 9); }
f9:     lwz   r4,96(r1)          caller: addi  r4,r3,9
        add   r3,r4,r3                   clrldi r4,r4,32
        clrldi r3,r3,32                  std   r4,96(r1)
        blr                              ...  bl f9

Before the fix:

uint4 f9(...)      { int4 v1; // r4 | stack + 0x60
                     return v1 + a; }
uint4 caller(uint4 n) { ... return f9(n,2,3,4,5,6,7,8,v1); }   // v1: stack + 0x0, never written

Cause

ppc_64_le.cspec puts the first stack parameter at SP+112, the ELFv1 offset: a 48-byte header (back chain, CR, LR, two reserved doublewords, TOC) followed by the 64-byte parameter save area for the eight register arguments. Little-endian PowerPC64 uses the ELFv2 ABI, whose header is 32 bytes (back chain, CR, LR, TOC), so the ninth doubleword is at SP+96. The big-endian ELFv1 spec is unchanged.

Fix

ppc_64_le.cspec: the stack pentry starts at offset 96.

After the fix:

uint4 f9(...)         { return i + a; }
uint4 caller(uint4 n) { return f9(n,2,3,4,5,6,7,8,n + 9); }

Tests

  • Stage test kuna-ppc64le-elfv2-stack-args.xml (the example above): both assertions fail on main and pass here.
  • Behaviour: in the stack-allocation ISA harness (C cases compiled with clang for powerpc64le-linux-gnu, decompiled, recompiled and compared against the native build under qemu-ppc64le), vla_variadic(0,n,2,3,4,5,6,7,8) printed its ninth argument as an uninitialised local and its later call read the wrong slot; with this spec both calls match the native results.
  • Debian corpus, ppc64el (121 executables from bookworm gzip, bzip2, sed, grep, tar, diffutils, findutils, coreutils; 24,612 functions; a second base run showed no nondeterministic function; no errors on either side): 1,993 functions change. In 420 a call gains the stack arguments it was missing (e.g. sub_75b8(…,*(unsigned long *)(v9 + 0x16b68),*(unsigned long *)(v9 + 0x16b70)), previously one argument short); in 2 spurious register-save arguments that filled the misplaced slots disappear; the remaining 1,571 keep every call's argument count, and the ones I sampled differ only in renumbered variables and in locals that now hold the argument values. The big-endian ppc64 corpus is unaffected (the change is in the LE spec only).
  • Decbench and byte-match: not run; their corpora contain no PowerPC binaries.
  • CI command set: datatests, stages (baseline re-recorded: adds this test's two assertions, removes none), tooling, CLI probes, workspace, kuna-ghidra including ignored, lint, spec, catalog, options and counters all pass. The WASI/dev-viz step needs git history and could not run in the cluster's export.

…ame header

- ppc_64_le.cspec placed the first stack parameter at SP+112, the ELFv1 offset after a 48-byte header.
  ELFv2 (every ppc64le target) has a 32-byte header (back chain, CR, LR, TOC), so the ninth doubleword is at SP+96.
  Callers lost their stack arguments and callees read an unwritten slot.
- Stage test kuna-ppc64le-elfv2-stack-args: callee `return i + a;`, caller `f9(n,2,3,4,5,6,7,8,n + 9)`; both fail on main.
… version 2

- Big-endian ELFv2 (musl, -mabi=elfv2) has the same 32-byte frame header as ppc64le, so stack parameters start at SP+96; the default big-endian spec stays ELFv1 (SP+112).
- New compiler spec ppc_64_be_elfv2.cspec (id elfv2), chosen by the loader when e_flags & 3 == 2.
- Stage test from real clang -mabi=elfv2 output (fails without the selector); loader unit test; baseline re-recorded (4344/4344).
- ELFv2 saves r2 at 0x18(r1); the copied ELFv1 inject wrote 0x28, inside the parameter save area. Only used under callmechanisms on.
@thotypous

Copy link
Copy Markdown
Contributor Author

[AUTOMATED] Update after adversarial review (gpt-6.1-sol, deepseek-v4.1-flash; two rounds):

  • Big-endian ELFv2 (musl, -mabi=elfv2) had the same bug. It is now covered by a new compiler spec, ppc_64_be_elfv2.cspec (id elfv2 under PowerPC:BE:64:default), which places stack parameters at SP+96.
    • The loader chooses it for a big-endian PowerPC64 ELF whose e_flags & 3 == 2. Unspecified flags and ELFv1 stay on the default (SP+112) spec.
    • Within the new spec, the uponreturn TOC restore uses ELFv2's 0x18 (ELFv1's 0x28 was copied in by mistake). It only matters under callmechanisms on.
  • Tests added:
    • Stage test kuna-ppc64be-elfv2-stack-args built from real clang --target=powerpc64-linux-gnu -mabi=elfv2 -O1 output (callee lwz r4,100(r1), caller std r11,96(r1)). Without the selector both assertions fail; with it both pass.
    • Loader unit test: BE+v2 selects elfv2; BE+v1, BE+unspecified and LE+v2 do not.
    • The ISA-selection contract test now expects :elfv2 for the BE ABI-v2 synthetic image.
  • Gate: stage baseline re-recorded (4344/4344, stage count 758). The CI command set passes except the WASI/dev-viz step, which needs git history. In this gate run the full workspace step's one failure was that ISA-selection contract test; it is updated in this push and passes on its own; the full workspace step has not been rerun.
  • Known gaps, out of scope here:
    • Both ELFv2 specs still convert every struct/union return to a hidden pointer. ELFv2 returns aggregates of ≤16 bytes in r3/r4 and homogeneous float aggregates in f1–f8.
    • The little-endian spec's callmechanisms on TOC restore still targets 0x28.

This branch has not been deployed

No deployments
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