Files
rea/src/ghidra/GhidraSessionValues.test.ts
T
Aurelio BernalandGitHub 5e219de441 feat(ghidra): admit soft-float MIPS32r2 ELF with integer-register float storage (#1949)
* feat(ghidra): admit soft-float MIPS32r2 ELF with integer-register float storage

Soft-float o32 executables (Linux kernel builds, bare-metal firmware) were
refused because their ABI flags record declares no CPR1 and FP ABI 3.
Ghidra's MIPS32 compiler specifications model only hard-float o32, so typed
float/double signatures (DWARF) would be bound to $f12/$f14/$f0 and the
decompiler loses the real dataflow: `return sel ? a : b` decompiled to
`return in_f0;`, and callers passed phantom FPU inputs.

Admit ELF32 ET_EXEC o32 MIPS32r2 targets whose ABI record declares soft-float
with no CPR1, and commit `mips_soft_float_storage: o32-integer-registers-v1`
in their analysis profile only (hard-float profile digests are unchanged).
ReaGhidraMipsSoftFloat.java runs after auto-analysis and, for each function
with a float/double formal type, either keeps the types with the storage
Ghidra's o32 model assigns to a same-size integer (pair alignment, stack
spill), or, for auto-parameters such as hidden struct-return pointers, swaps
float types for same-size integers under dynamic storage. No function is left
on hard-float storage. The report is written last; the provider client
requires it exactly when the profile commits the policy and refuses the
session before any operation otherwise.

Verification: real Ghidra 12.1.4 lane (verify:ghidra:mips) now builds
hard- and soft-float fixtures in both byte orders and checks exact
parameter/return storage through inspect_native_api, caller constants,
snapshot profile and Evidence outcome; test:fast, verify:package and an
installed-tarball soft-float run pass.

* fix(ghidra): model soft-float MIPS as a prototype for every typed callable

Review feedback on #1949: rewriting storage per Function left external
functions and indirect calls through typed function pointers on Ghidra's
hard-float o32 prototype, which still read $f12/$f14/$f0 while the session
reported success. A negative control confirms it: without the change,
`(*rea_mips_soft_callback)(8.5f, 9.5f, sel)` decompiles to
`(*rea_mips_soft_callback)(in_f12, in_f14, sel)`.

ReaGhidraMipsSoftFloat.java now derives a `__rea_soft_float` prototype from
the program's own default o32 model (encoded, float pentries and
float-consuming rules removed, so byte-order-specific aggregate rules and the
hidden struct-return pointer are kept), installs it as a specification
extension, and assigns it with dynamic storage to every non-thunk function,
external function and function-pointer definition whose formal types use
float or double. Thunks follow their targets. Float types are kept
everywhere, so the integer-retype fallback is removed. The script fails
without writing its report if the derived prototype or any reassigned
function still uses an FPU register.

The policy becomes `o32-soft-float-prototype-v1` with a
{functions, definitions} report. The soft-float fixture adds an indirect
call through a typed pointer; the real-Ghidra lane requires its constants
with no FPU inputs and the struct return's `float scale` in $a1.

* test(ghidra): pin ODDSPREG acceptance for soft-float MIPS ABI records

Review on #1949 suggested requiring flags1 == 0 for soft-float, since MIPS_AFL_FLAGS1_ODDSPREG names CPR1 registers. GCC 5.2 kernel modules, LLVM/Rust firmware and Clang -msoft-float objects all record flags1 = 1 with FP ABI soft-float: it is the MIPS32 o32 toolchain default and selects nothing without CPR1. Keep accepting it, say why at the check, and test that soft-float with ODDSPREG is admitted while any other general flag is refused.
2026-10-12 04:54:38 +08:00

151 lines
4.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
GHIDRA_SESSION_CAPABILITIES,
GHIDRA_MUTATING_OPERATIONS,
parseGhidraSessionInfo,
} from "./GhidraSessionValues.js";
const expected = {
runId: "d6fcbb66-e829-4ff6-a535-0035aec63139",
providerVersion: "12.1.4",
profileDigest: "a".repeat(64),
targetSha256: "b".repeat(64),
expectedLanguageId: "x86:LE:16:Real Mode",
expectedCompilerSpecId: "default",
};
const session = (
language = expected.expectedLanguageId,
compiler = "default",
) => ({
name: "REA Ghidra bridge",
run_id: expected.runId,
profile_digest: expected.profileDigest,
provider: { id: "ghidra", version: expected.providerVersion },
read_only: false,
analysis_complete: true,
analysis_timed_out: false,
capabilities: [...GHIDRA_SESSION_CAPABILITIES],
target: {
name: "legacy.exe",
language_id: language,
compiler_spec_id: compiler,
image_base: "0x0",
default_address_space: "ram",
sha256: expected.targetSha256,
},
});
describe("Ghidra DOS import commitment", () => {
it("accepts the exact admitted real-mode language and compiler", () => {
expect(parseGhidraSessionInfo(session(), expected).ok).toBe(true);
});
it("rejects a 32-bit interpretation even when artifact and profile hashes match", () => {
expect(
parseGhidraSessionInfo(session("x86:LE:32:default"), expected).ok,
).toBe(false);
});
it("rejects compiler drift independently of the language", () => {
expect(
parseGhidraSessionInfo(session(undefined, "windows"), expected).ok,
).toBe(false);
});
});
describe("Ghidra MIPS soft-float storage report", () => {
const valid = {
policy: "o32-soft-float-prototype-v1",
functions: 2,
definitions: 1,
};
it("accepts the exact committed policy outcome", () => {
const parsed = parseGhidraSessionInfo(
{ ...session(), mips_soft_float_storage: valid },
expected,
);
if (!parsed.ok) throw parsed.error;
expect(parsed.value.mips_soft_float_storage).toEqual(valid);
});
it.each([
[
"the superseded storage policy",
{ policy: "o32-integer-registers-v1", remapped: 2, retyped: 1 },
],
["a negative count", { ...valid, definitions: -1 }],
["a fractional count", { ...valid, functions: 1.5 }],
["a missing count", { policy: valid.policy, functions: 2 }],
["an undeclared field", { ...valid, skipped: 0 }],
["a non-object report", "functions"],
])("rejects %s", (_label, report) => {
expect(
parseGhidraSessionInfo(
{ ...session(), mips_soft_float_storage: report },
expected,
).ok,
).toBe(false);
});
});
describe("Ghidra mutation handshake", () => {
it("requires the exact mutation authority and rejects duplicate capabilities", () => {
const value = session();
expect(
parseGhidraSessionInfo({ ...value, read_only: true }, expected).ok,
).toBe(false);
expect(
parseGhidraSessionInfo(
{ ...value, capabilities: [...value.capabilities, "ping"] },
expected,
).ok,
).toBe(false);
expect(
parseGhidraSessionInfo(
{
...value,
capabilities: value.capabilities.filter(
(c) => c !== "annotate_native_function",
),
},
expected,
).ok,
).toBe(false);
});
it("excludes database mutation from the Windows transport handshake", () => {
const value = session();
const windows = {
...value,
read_only: true,
capabilities: value.capabilities.filter(
(c) => !GHIDRA_MUTATING_OPERATIONS.has(c),
),
};
expect(
parseGhidraSessionInfo(windows, { ...expected, expectedReadOnly: true })
.ok,
).toBe(true);
expect(
parseGhidraSessionInfo(value, { ...expected, expectedReadOnly: true }).ok,
).toBe(false);
});
it("treats address naming as database mutation", () => {
expect([...GHIDRA_MUTATING_OPERATIONS].sort()).toEqual([
"annotate_native_function",
"set_address_name",
"set_addresses_names",
]);
const value = session();
for (const naming of ["set_address_name", "set_addresses_names"]) {
expect(value.capabilities).toContain(naming);
expect(
parseGhidraSessionInfo(
{
...value,
capabilities: value.capabilities.filter((c) => c !== naming),
},
expected,
).ok,
).toBe(false);
}
});
});