Skip to content

[AUTOMATED] Seed r12 with the entry address at PowerPC64 ELFv2 global entries - #1595

Open
thotypous wants to merge 3 commits into
Noelo-Lab:mainfrom
thotypous:fix/ppc64-elfv2-global-entry-r12
Open

thotypous wants to merge 3 commits into
Noelo-Lab:mainfrom
thotypous:fix/ppc64-elfv2-global-entry-r12

Conversation

@thotypous

Copy link
Copy Markdown
Contributor

Problem

On ppc64le, a function entered at its global entry reaches every global through an uninitialised register. This independent example is clang -O1 for powerpc64le-linux-gnu:

unsigned char *escaped;
void publish(unsigned char *p) { escaped = p; }
publish:  addis r2,r12,2        ; global entry: TOC from r12
          addi  r2,r2,-32736
          addis r4,r2,1         ; local entry is publish+8
          std   r3,-32760(r4)
          blr

Before the fix:

void publish(unsigned long a0)
{
  long v1; // r12 | r2
  *(unsigned long *)(v1 + 0x20028) = a0;
}

Cause

The OpenPOWER ELFv2 ABI requires a caller that enters at the global entry to pass the entry address in r12. The prologue derives the TOC pointer from it. Nothing in kuna stated that, so r2 stayed an unknown function input and every TOC-relative access printed as an offset from it (it also does not recompile).

Fix

New P1 analysis pass ppc64r12 (kuna-analysis/src/loader/kuna_ppc64r12.rs), the analog of mips_gp's t9 = entry seed: it records r12 = entry as a tracked register value at each defined PPC64 STT_FUNC whose st_other declares a distinct local entry (the annotation that says the function begins with the r12-based TOC setup). Self-gated: empty on every other image.

After the fix:

void publish(unsigned long a0)
{
  escaped = a0;
}

Tests

  • CLI test ppc64_global_entry_toc with the 1.4 KB fixture above: fails on main (*(unsigned long *)(v1 + 0x20028) = a0;), passes here.
  • prototype_call_mechanisms::default_keeps_a_local_in_the_elfv1_toc_slot_of_an_elfv2_frame: with callmechanisms on the r2Save store now writes the resolved TOC constant instead of printing r2save. The assertion now checks the behaviour it is about (the local is clobbered under on; off and auto are unchanged and still compared verbatim).
  • Behaviour: in the stack-allocation ISA harness (clang powerpc64le-linux-gnu, decompiled, recompiled, compared with the native build under qemu-ppc64le), sample_vla_escape wrote escaped_vla_address through r12 + 0x22068 and crashed; with this pass it writes the global and matches.
  • Debian corpus, ppc64 + ppc64el (12 big-endian and 121 little-endian executables from bookworm; 27,044 functions; a second base run showed no nondeterministic function; no errors on either side): 35 ppc64el functions change, each replacing an r12-relative access with the named global (e.g. (**(void (**)(void))(v2 + 0x234c0))(); becomes (*obstack_alloc_failed_handler)();). Big-endian ELFv1 images have no local-entry annotations and do not change.
  • Decbench and byte-match: not run; their corpora contain no PowerPC binaries and the pass is inert elsewhere.
  • CI command set: datatests, stages, 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.

… entries

- The ELFv2 ABI passes a function's own address in r12 to its global entry, which derives the TOC pointer from it
  (addis r2,r12,hi; addi r2,r2,lo). Without that fact r2 was an unknown input and TOC-relative globals printed as
  offsets from an uninitialised register.
- New P1 pass ppc64r12 (the analog of mips_gp's t9 seed): r12 = entry at each defined STT_FUNC whose st_other declares
  a distinct local entry.
- CLI test ppc64_global_entry_toc with a 1.4 KB clang fixture; prototype_call_mechanisms' `on` assertion now checks
  the clobber, since the stored TOC resolves to a constant instead of printing as r2save.
- st_other entry bits mean nothing under ELFv1; the ELFv1 descriptor reader already gates on e_flags & 3.
- The on arm now checks the store's RHS is the constant the return reads back, not only that the local is clobbered.
@thotypous

Copy link
Copy Markdown
Contributor Author

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

  • The pass now skips images whose e_flags & 3 == 1 (explicit ELFv1), the same ABI gate the ELFv1 descriptor reader uses. Unspecified flags keep the annotation-backed seed.
  • prototype_call_mechanisms pins the value again under callmechanisms on: the r2Save store's right-hand side must be the TOC constant that the return reads back (v2 = 0x408100 / + 0x408100), not merely clobber the local.
  • Gate on the updated branch: everything passes except the WASI/dev-viz step, which needs git history.
  • Not done here (recovery extensions, not regressions):
    • seeding stripped functions from unwind/FDE entry starts;
    • seeding r2 = TOC at local entries (ppclocalentry off);
    • an option to switch the pass off. Note that mips_gp/arm_markers, which this follows, are always on too.

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