Skip to content

Fix SPARC support for anvill - #789

Open
mrexodia wants to merge 16 commits into
lifting-bits:masterfrom
LLVMParty:sparc-sleigh-v2
Open

mrexodia wants to merge 16 commits into
lifting-bits:masterfrom
LLVMParty:sparc-sleigh-v2

Conversation

@mrexodia

@mrexodia mrexodia commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

SPARC has been broken for quite a while. This switches SPARC to use Sleigh semantics unconditionally and adds some tests.

Note: this is 100% clanked, but I did guide it to purge aggressively and test end-to-end with anvill and SPARC32+SPARC64 executables.

…tion flows

ControlFlowStructureAnalysis::ComputeCategory failed for two shapes of
p-code that occur in SPARC (and that the p-code CFG already lifts fine):

- Ops after a CALL/CALLIND. The p-code CFG sends a call straight to the
  instruction exit, so those ops never execute unless an intra-instruction
  branch targets them. SPARC's call is

    o7 = inst_start; didrestore = 0; delayslot(1); call target;
    if (didrestore == 0) goto inst_next; return [o7];

  and its trailing CBRANCH/RETURN made the instruction uncategorizable.
  They are now ignored when no earlier branch lands after the call.

- Intra-instruction branches mixed with exits, e.g. ret with restore in
  its delay slot (restore branches over the register window spill). Such
  an instruction is now categorized by its exits, provided no decoding
  context register is written (exit contexts are computed linearly).

Also factor the existing context-update check into HasContextUpdates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
…overlays

- Provide Ghidra's fake DECOMPILE_MODE register as a decoding context
  value of 1, as in SparcV9.pspec. Otherwise it was read from the state
  (usually 0), and save/restore also spilled and filled the register
  window backing store through computed register-space addresses, which
  were lifted as memory accesses to bogus addresses.
- Name the FP condition code registers FCC0-FCC3 (Ghidra's fcc0-fcc3,
  upper-cased). They were registered as ccf_fcc0-3, which never matched,
  so each instruction got a fresh local and e.g. fcmp followed by fbne
  lost the condition.
- Ghidra numbers the FP registers big-endian (fs0 is the high half of
  fd0, which is the high half of fq0). The state holds host-order values,
  so place the FS/FD sub-registers accordingly; they overlapped at the
  same offsets before.
- Arch::IsSPARC32 also returns true for kArchSparc32_SLEIGH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
The hand-written SPARC64 semantics depend on SPARC32 semantics files
that lifting-bits#681 removed, so their runtime has not been built since and
Arch::Build for sparc64 aborts (the register window type only exists in
a semantics module). Arch.h declared GetSPARC64Sleigh, but nothing
defined it.

Remove the hand-written decoder, semantics, runtime and State, and make
kArchSparc64 ("sparc64") a Sleigh-based architecture using Ghidra's
SparcV9_64.sla, as SPARC32 is:

- SPARC64/Runtime/State.h: a State laid out after the Sleigh register
  space (64-bit GPRs, PC/NPC, FSR, ancillary and privileged registers,
  condition codes, FP registers). HyperCall.cpp, which includes it for
  the runtime, now sees the same State as the rest of the runtime.
- Sleigh/SPARC64Arch.{h,cpp}: the decoder and the register table, using
  Ghidra's (upper-cased) register names. As on SPARC32, DECOMPILE_MODE is
  a decoding context value of 1, and the FP sub-registers follow Ghidra's
  big-endian numbering. Sleigh decodes delayed control transfers together
  with their delay slot, so instructions are up to 8 bytes and no
  separate delay slots are reported. PC is the address of the instruction.
- SPARC64/Runtime builds sparc64.bc (State, intrinsics and the
  unsupported/invalid instruction handlers); REMILL_BUILD_SPARC64_RUNTIME
  turns it off. The remill_arch_sparc64 library is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
The hand-written SPARC32 semantics were removed in lifting-bits#681, but the decoder
(lib/Arch/SPARC32/Arch.cpp, Decode.h, Extract.cpp) stayed: with no
semantics behind it, kArchSparc32 lifted every instruction as a
kSPARC32EmulateInstruction hyper call. Remove it, and make kArchSparc32
("sparc32") the Sleigh-based architecture, as for aarch32, thumb2 and
ppc. kArchSparc32_SLEIGH ("sparc32_sleigh") remains as an alias of the
same architecture. lib/Arch/SPARC32 now only builds the runtime
(sparc32.bc and sparc32_sleigh.bc).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
tests/SPARC lifts single instructions with sparc32 and sparc64, runs
them with the test runner's JIT on a randomized state, and checks the
decoded category and the resulting registers: add, retl and call with
delay slots, save, ret with restore in its delay slot, brz with its delay
slot (taken and not taken), fcmps setting FCC0 and fmovd read back
through the FS sub-registers. Against the previous SPARC32 code the
call, save, ret+restore, fcmps and fmovd cases fail.

Enabled with REMILL_ENABLE_TESTING_SLEIGH_SPARC (on with testing). The CI
smoketests also lift a little code for both architectures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
Sleigh accesses a part of a register that has no name of its own as a
register varnode inside it; e.g. a SPARC V9 `st %o0` stores the low word of
the 64-bit `o0` as `(register, o0 + 4, 4)`. `getRegisterName` returns the
containing register, which was then accessed as if the varnode started at
the register's first byte. On a big-endian target such as SPARC64 that is
the high word, so lifted code stored the high word of `o0`.

Access such a varnode through the value of the whole register instead:
load it, shift by the varnode's position in Sleigh's register space byte
order, and truncate; a write merges the part into the register's value.
The result does not depend on the byte order of the state structure.

The test runner executes lifted code with the host's byte order, under
which the old code happened to read the low word, so the new SPARC64 test
checks the lifted code: every load of `o0` loads all of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
The SPARC semantics and lifted modules use SPARC's big-endian data layout,
in which `fs(4q)` and `fd(4q)` (the high halves of `fd(4q)` and `fq(4q)`)
are the first bytes of `fq(4q)`, as the original SPARC32 table had them.
An earlier change in this series swapped the halves for host (little-
endian) byte order, which only the test runner, executing with the host's
byte order, agrees with; consumers that use the module's data layout saw
the halves of `fq` swapped. Restore the big-endian offsets for SPARC32 and
use them for SPARC64.

The test runner cannot check how the FP registers overlay each other, so
the tests access them at the register table's offsets, and the overlay
test is replaced by one that moves a double.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
A function returning a structure returns past the `unimp <size>` that
follows the delay slot of its call, with `jmpl %o7 + 12, %g0` in a leaf
function or `jmpl %i7 + 12, %g0` otherwise. Ghidra treats only `+ 8` as a
return and these as indirect jumps, so lifters such as anvill saw a jump
to an unknown target where the hand-written SPARC decoder had a return.

Categorize them as returns again in both SPARC decoders, and test both
forms and that other displacements stay indirect jumps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
The SPARC32 option told users to turn the runtime off on errors about
<bits/c++config.h> (missing 32-bit libstdc++ headers) and pointed to a
README fix. Since the semantics are compiled against remill's own sysroot
with -nostdinc++, the runtimes use only that sysroot and clang's
freestanding headers, so the error cannot occur, and the README section
is gone. Turning a runtime off only made the architecture fail when its
semantics were loaded, and no other architecture has such an option, so
always build the SPARC runtimes (SPARC64 still only on 64-bit hosts, like
the other 64-bit runtimes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
It only apt-installed packages for the old build: Python 2.7, multilib
GCC and i386 zlib, the 32-bit headers the semantics no longer need since
they are compiled against remill's own sysroot. Several of the packages
are gone from current Ubuntu, nothing references the script, and the
README and docs/DEPENDENCIES.md describe the current setup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T21:45:40.107840Z 2e2c45e New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pgoodman

Copy link
Copy Markdown
Contributor

My precious semantics 😢 🤣

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 59992fdff7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/Arch/Arch.cpp Resolved
From review of the mixed-flow categorization:

- A constant-space branch to the end of the p-code (relative target equal
  to the number of ops) leaves the instruction: it falls through. It was
  treated as an intra-instruction flow, so the new handling of mixed flows
  dropped it: `CBRANCH <end>, cond; RETURN` became an unconditional return.
  Treat it as a (conditional) fallthrough, giving a conditional return.
  The p-code CFG also gave the empty block at the end the exit of the
  instruction's last op; it now just exits.
- `VisitBlock` counted ops from zero in every p-code block, but the branch
  taken variable is identified by its index among all of the instruction's
  ops. A conditional exit that follows an intra-instruction branch (now
  accepted) would not store `BRANCH_TAKEN`. Count from the block's base
  index.

The new test runs the categorization on synthetic p-code (no SPARC
instruction is known to produce it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
Sleigh decodes a delayed control transfer together with the instruction in
its delay slot, so decoding one needs 8 bytes. With fusion disabled,
MaxInstructionSize still returned 4 (from the hand-written decoder), so a
client walking up from MinInstructionSize to MaxInstructionSize(false), as
Arch.h suggests, never offered enough bytes and rejected every delayed
call and branch. Return 8, as SPARC64 does, and test it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
mrexodia pushed a commit to mrexodia/anvill that referenced this pull request Nov 10, 2026
Branches to the end of the p-code are fallthroughs, the branch-taken op is
found by its instruction-wide index, and SPARC32 reports 8-byte instructions
without fusion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7b1d2503ac

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

When an instruction's conditional exit is a branch to the fallthrough
followed by an unconditional exit, its branch-taken variable is the
condition of the branch to the fallthrough, and `BranchTakenVar::invert`
is set. `LiftBranchTaken` never read `invert`, so `BRANCH_TAKEN` held the
opposite of the truth. SPARC's annulled branches have this shape
(`brz,a`: if the condition is false, skip the delay slot by branching to
the fallthrough), so a lifter dispatching on `BRANCH_TAKEN` took them
exactly when they were not taken. Thumb and AArch32 conditional branches
branch to their target, and were not affected.

The SPARC test runner's lifted function now sets `PC` the way a trace
lifter would for a conditional instruction (the taken target when
`BRANCH_TAKEN` is set, the fallthrough otherwise), so the expected `PC`
also checks `BRANCH_TAKEN`. New tests: `brz,a`, taken and not taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
When intra-instruction flows are dropped to categorize an instruction by
its exits, the resulting conditional exit sets `BRANCH_TAKEN`. If an
earlier intra-instruction branch can jump over it (`CBRANCH +2, a;
CBRANCH <address>, b; ...`), `BRANCH_TAKEN` is not set on that path and a
lifter dispatching on it uses a stale value. Only accept the conditional
category when no earlier branch can skip the conditional exit (the same
check `DropOpsAfterFirstCall` makes for calls, now shared), and test it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
mrexodia pushed a commit to mrexodia/anvill that referenced this pull request Nov 10, 2026
…ulled-branch goldens

remill now applies inverted branch-taken variables, which SPARC annulled
branches use: anvill dispatches on BRANCH_TAKEN, so brz,a went the wrong
way (the new goldens return 7 for x == 0 without the fix). It also rejects
mixed flows whose conditional exit can be skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e2c45e7ce

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/Arch/Sleigh/SPARC64Arch.cpp Resolved
Comment thread lib/Arch/Arch.cpp Resolved
A p-code block ends either at a control-flow op or because the next op is
the target of an intra-instruction branch, as in
`if (!cond) goto <end>; x = y; <end>: ...`. In the second case the CFG
gave the block the exit of its last op, which for an ordinary op is the
instruction exit, so on the path through `x = y` every op after the label
was skipped. Such a block now falls through to the next one.

Found with anvill: SPARC's conditional moves use such a label, and in the
delay slot of `retl` (which compilers emit, e.g. `retl; movl %fcc0, 10,
%o0`) the label is followed by the return, so the path that moved did not
return. New tests: that instruction with the move done and skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
Ghidra's SparcV9.sinc divides by the operand ANDed with 0xffffffff, i.e.
the zero-extended low word, while SPARC divides by its signed low word.
On SPARC64 `7 / -2` therefore divided by 4294967294 and gave 0 instead of
-3. (On 32-bit SPARC the operand is one word and the AND had no effect.)
Found with anvill's compiled-C cases. Add a patch, applied like remill's
other Ghidra patches, and tests that divide by and into a negative number
(they need a new `y` accessor).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dar7bjgfJRt6c9Aj86Kv4H
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.

4 participants