* feat(native): admit bounded MIPS ELF analysis through Ghidra Retain MIPS ELF identity and interpretation through Evidence and snapshots. Keep provider admission bounded to ELF32 ET_EXEC o32 MIPS32r2, without implicitly widening Hopper, IDA or Windows P0 coverage. Add source-owned fixtures and an optional real-provider verifier; real Ghidra verification remains pending in this contribution environment. Refs #718 * fix(ghidra): bound MIPS floating-point mode admission Reject explicit EF_MIPS_FP64 and EF_MIPS_NAN2008 declarations until their semantics have a real-provider verification lane. Add PSP PRX/Allegrex, o64/EABI and floating-point-mode refusal regressions. Document that the header gate is not a full .MIPS.abiflags check and advance the profile lane identity to invalidate older interpretation bindings. Validation: 24 standalone checks against the transpiled production profile module passed; the two new flag cases failed before this change. Clang 17 and LLD compiled the source-owned fixture for both endian targets. The complete Vitest/typecheck suite and real Ghidra verification remain pending. Refs morluto/rea#718; PSP support remains follow-up morluto/rea#1330. * fix(native): validate MIPS ABI records before Ghidra admission Read bounded ELF32 PT_MIPS_ABIFLAGS and SHT_MIPS_ABIFLAGS records through the already-open target handle. Preserve declared ABI fields, reject conflicting/duplicate/truncated records, and keep missing records unknown. Wire inspection into target resolution before provider selection. Require compatible declarations for the deliberately narrow Ghidra lane and bind all ABI fields into profile identity (v3) for snapshot invalidation. Clang 17 produced eight source-owned fixtures spanning both byte orders and FPXX/FP32/FP64/soft-float modes with identical ELF header flags. The new reader and policy distinguish them. 24 focused regression test bodies passed after TypeScript transpilation using node:test in place of Vitest; strict isolated TypeScript checking of the reader and target/result types passed. These are limited checks, not full Vitest/project typechecking or real-Ghidra verification, which remain pending. Advances morluto/rea#718. PSP/Allegrex remains morluto/rea#1330 follow-up. * fix(native): repair MIPS integration boundaries and test resolver behavior Keep ELF targets out of Hopper Mach-O preparation and scope existing PE fixture types correctly. Move ABI tests into the filesystem boundary lane, use shared cleanup, add resolver regressions, apply the pinned formatter, and register the real MIPS verifier through the existing package-command pattern. Validated with repository-pinned Node/npm, check:ci, actual focused Vitest, discovery and docs:check. Real Ghidra conformance remains pending. * fix(test): honor caller-selected JDK in real MIPS verification * test(native): strengthen MIPS Ghidra conformance Cross-check the source-owned fixture with bounded GNU readelf inspection, compare production ELF/ABI interpretation and independent symbol addresses, and verify effective Ghidra language/compiler identity through existing public operations. Add a source-owned fixed-encoding probe for immediate and conditional branch checks plus global/string byte assertions. Add 16 assertion/producer-boundary regression cases without widening production admission, changing provider contracts, or introducing proprietary dependencies. Local focused suite: 99 passed, one existing root-permission test skipped. Typecheck, lint/module boundaries, formatting and 700-file discovery pass; full hosted checks and real Ghidra verification remain pending for this checkpoint. * fix(ghidra): preserve native-tool architecture checks after MIPS integration The conflict-free upstream merge failed TypeScript compilation because includes on the union of MIPS-only and other architecture arrays requires never. Use equality-based membership without casts or changing accepted native hosts. Extend the existing native-decompiler rejection table with a MIPS executable at a Linux x64 tool path; retain upstream FAT and unknown-header behavior. Hosted verification follows this commit; no success is claimed yet. --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
310 lines
10 KiB
TypeScript
310 lines
10 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { createHash } from "node:crypto";
|
|
import { open, readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { it } from "vitest";
|
|
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
|
|
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import type { MipsElfMetadata } from "../../../src/domain/binaryTargetTypes.js";
|
|
import { readMipsElfAbiFlags } from "../../../src/application/MipsElfAbiFlags.js";
|
|
import {
|
|
ghidraMipsProfileParameters,
|
|
ghidraMipsUnsupportedReason,
|
|
} from "../../../src/ghidra/GhidraMipsProfile.js";
|
|
|
|
const metadata = (little = true): MipsElfMetadata => ({
|
|
elfClass: 32,
|
|
byteOrder: little ? "little" : "big",
|
|
type: 2,
|
|
flags: 0x70001001,
|
|
});
|
|
const abiOffset = 8192; // Deliberately outside BinaryTargetResolver's 4 KiB probe.
|
|
const fixture = (little = true): Buffer => {
|
|
const bytes = Buffer.alloc(abiOffset + 48);
|
|
bytes.set([0x7f, 0x45, 0x4c, 0x46, 1, little ? 1 : 2, 1]);
|
|
const u16 = (value: number, offset: number) =>
|
|
little
|
|
? bytes.writeUInt16LE(value, offset)
|
|
: bytes.writeUInt16BE(value, offset);
|
|
const u32 = (value: number, offset: number) =>
|
|
little
|
|
? bytes.writeUInt32LE(value, offset)
|
|
: bytes.writeUInt32BE(value, offset);
|
|
u16(2, 16);
|
|
u16(8, 18);
|
|
u32(1, 20);
|
|
u32(52, 28);
|
|
u32(128, 32);
|
|
u32(0x70001001, 36);
|
|
u16(52, 40);
|
|
u16(32, 42);
|
|
u16(1, 44);
|
|
u16(40, 46);
|
|
u16(2, 48);
|
|
u32(0x70000003, 52);
|
|
u32(abiOffset, 56);
|
|
u32(24, 68);
|
|
u32(24, 72);
|
|
u32(0x7000002a, 172);
|
|
u32(abiOffset, 184);
|
|
u32(24, 188);
|
|
bytes.set([0, 0, 32, 2, 1, 1, 0, 5], abiOffset);
|
|
return bytes;
|
|
};
|
|
const inspect = async (
|
|
bytes: Buffer,
|
|
little = true,
|
|
checkCancelled: () => void = () => {},
|
|
) => {
|
|
const directory = await createTestTempDirectory("rea-mips-abi-test-");
|
|
const path = join(directory, "fixture.elf");
|
|
await writeFile(path, bytes);
|
|
const handle = await open(path, "r");
|
|
try {
|
|
return await readMipsElfAbiFlags(handle, metadata(little), checkCancelled);
|
|
} finally {
|
|
await handle.close();
|
|
}
|
|
};
|
|
const target = (mips: MipsElfMetadata) => ({
|
|
path: "/fixture.elf",
|
|
sha256: "a".repeat(64),
|
|
kind: "executable" as const,
|
|
format: "elf" as const,
|
|
architecture: "mips" as const,
|
|
availableArchitectures: ["mips"] as const,
|
|
mips,
|
|
});
|
|
|
|
for (const little of [true, false]) {
|
|
it(`reads a remote ABI record through both ELF tables (${little ? "LE" : "BE"})`, async () => {
|
|
const bytes = fixture(little);
|
|
const result = await inspect(bytes, little);
|
|
assert.ok(result.ok);
|
|
assert.deepEqual(result.value, {
|
|
version: 0,
|
|
isaLevel: 32,
|
|
isaRevision: 2,
|
|
gprSize: 1,
|
|
cpr1Size: 1,
|
|
cpr2Size: 0,
|
|
fpAbi: 5,
|
|
isaExtension: 0,
|
|
ases: 0,
|
|
flags1: 0,
|
|
flags2: 0,
|
|
});
|
|
const resolved = target({ ...metadata(little), abiFlags: result.value });
|
|
assert.equal(ghidraMipsUnsupportedReason(resolved), null);
|
|
const before = ghidraMipsProfileParameters(resolved);
|
|
bytes[abiOffset + 5] = 2;
|
|
bytes[abiOffset + 7] = 6;
|
|
const fp64 = await inspect(bytes, little);
|
|
assert.ok(fp64.ok);
|
|
const changed = target({ ...metadata(little), abiFlags: fp64.value });
|
|
assert.match(ghidraMipsUnsupportedReason(changed) ?? "", /register modes/u);
|
|
assert.notDeepEqual(ghidraMipsProfileParameters(changed), before);
|
|
});
|
|
}
|
|
|
|
it("supports stripped section tables using PT_MIPS_ABIFLAGS", async () => {
|
|
const bytes = fixture();
|
|
bytes.writeUInt32LE(0, 32);
|
|
bytes.writeUInt16LE(0, 48);
|
|
const result = await inspect(bytes);
|
|
assert.ok(result.ok);
|
|
assert.equal(result.value?.fpAbi, 5);
|
|
});
|
|
it("supports a section-only ABI record without assuming its name", async () => {
|
|
const bytes = fixture();
|
|
bytes.writeUInt32LE(0, 28);
|
|
bytes.writeUInt16LE(0, 44);
|
|
const result = await inspect(bytes);
|
|
assert.ok(result.ok);
|
|
assert.equal(result.value?.fpAbi, 5);
|
|
});
|
|
it("keeps absent ABI declarations unknown and refuses provider admission", async () => {
|
|
const bytes = fixture();
|
|
bytes.writeUInt32LE(0, 52);
|
|
bytes.writeUInt32LE(0, 172);
|
|
const result = await inspect(bytes);
|
|
assert.ok(result.ok);
|
|
assert.equal(result.value, null);
|
|
assert.match(
|
|
ghidraMipsUnsupportedReason(target({ ...metadata(), abiFlags: null })) ??
|
|
"",
|
|
/inspected ABI/u,
|
|
);
|
|
assert.match(
|
|
ghidraMipsUnsupportedReason(target(metadata())) ?? "",
|
|
/inspected ABI/u,
|
|
);
|
|
});
|
|
it("rejects contradictory program and section ABI declarations", async () => {
|
|
const bytes = fixture();
|
|
bytes.copy(bytes, abiOffset + 24, abiOffset, abiOffset + 24);
|
|
bytes[abiOffset + 24 + 7] = 6;
|
|
bytes.writeUInt32LE(abiOffset + 24, 184);
|
|
const result = await inspect(bytes);
|
|
assert.ok(!result.ok);
|
|
assert.match(result.error, /conflicting/u);
|
|
});
|
|
it("rejects duplicate ABI declarations in one table", async () => {
|
|
const bytes = fixture();
|
|
bytes.copy(bytes, 84, 52, 84);
|
|
bytes.writeUInt16LE(2, 44);
|
|
const result = await inspect(bytes);
|
|
assert.ok(!result.ok);
|
|
assert.match(result.error, /duplicate/u);
|
|
});
|
|
const malformed: [string, (bytes: Buffer) => void, RegExp][] = [
|
|
["record outside file", (b) => b.writeUInt32LE(0xfffffff0, 56), /outside/u],
|
|
["table outside file", (b) => b.writeUInt32LE(0xfffffff0, 28), /outside/u],
|
|
["short record", (b) => b.writeUInt32LE(23, 68), /record size/u],
|
|
["oversized record", (b) => b.writeUInt32LE(0xffffffff, 68), /record size/u],
|
|
["invalid table stride", (b) => b.writeUInt16LE(31, 42), /entry size/u],
|
|
["extended program count", (b) => b.writeUInt16LE(0xffff, 44), /numbering/u],
|
|
["extended section count", (b) => b.writeUInt16LE(0, 48), /extended/u],
|
|
["changed identity", (b) => b.writeUInt32LE(0x60001001, 36), /identity/u],
|
|
["invalid ELF version", (b) => b.writeUInt32LE(0, 20), /version/u],
|
|
];
|
|
for (const [name, change, reason] of malformed) {
|
|
it(`rejects ${name} instead of using a guessed ABI`, async () => {
|
|
const bytes = fixture();
|
|
change(bytes);
|
|
const result = await inspect(bytes);
|
|
assert.ok(!result.ok);
|
|
assert.match(result.error, reason);
|
|
});
|
|
}
|
|
const unsupported: [string, (bytes: Buffer) => void, RegExp][] = [
|
|
["new record version", (b) => b.writeUInt16LE(1, abiOffset), /version 0/u],
|
|
[
|
|
"conflicting ISA",
|
|
(b) => {
|
|
b[abiOffset + 3] = 6;
|
|
},
|
|
/MIPS32r2/u,
|
|
],
|
|
[
|
|
"soft-float",
|
|
(b) => {
|
|
b[abiOffset + 5] = 0;
|
|
b[abiOffset + 7] = 3;
|
|
},
|
|
/register modes/u,
|
|
],
|
|
[
|
|
"unspecified FP ABI",
|
|
(b) => {
|
|
b[abiOffset + 7] = 0;
|
|
},
|
|
/unspecified/u,
|
|
],
|
|
["ISA extension", (b) => b.writeUInt32LE(1, abiOffset + 8), /extension/u],
|
|
["ASE", (b) => b.writeUInt32LE(0x800, abiOffset + 12), /ASE/u],
|
|
[
|
|
"reserved flags",
|
|
(b) => b.writeUInt32LE(2, abiOffset + 16),
|
|
/general flags/u,
|
|
],
|
|
];
|
|
for (const [name, change, reason] of unsupported) {
|
|
it(`retains ${name} while rejecting its unverified provider interpretation`, async () => {
|
|
const bytes = fixture();
|
|
change(bytes);
|
|
const result = await inspect(bytes);
|
|
assert.ok(result.ok);
|
|
assert.ok(result.value !== null);
|
|
assert.match(
|
|
ghidraMipsUnsupportedReason(
|
|
target({ ...metadata(), abiFlags: result.value }),
|
|
) ?? "",
|
|
reason,
|
|
);
|
|
});
|
|
}
|
|
it("preserves the caller's cancellation error during table inspection", async () => {
|
|
const cancelled = new Error("caller cancellation sentinel");
|
|
let checks = 0;
|
|
await assert.rejects(
|
|
inspect(fixture(), true, () => {
|
|
checks += 1;
|
|
if (checks === 12) throw cancelled;
|
|
}),
|
|
(error) => error === cancelled,
|
|
);
|
|
});
|
|
|
|
for (const little of [true, false]) {
|
|
it(`resolves full-file MIPS ABI identity before provider selection (${little ? "LE" : "BE"})`, async () => {
|
|
const directory = await createTestTempDirectory("rea-mips-resolution-");
|
|
const path = join(directory, "fixture.elf");
|
|
const bytes = fixture(little);
|
|
await writeFile(path, bytes);
|
|
const result = await parseBinaryTarget(path);
|
|
if (!result.ok) throw result.error;
|
|
assert.equal(result.value.architecture, "mips");
|
|
assert.equal(result.value.format, "elf");
|
|
assert.equal(
|
|
result.value.sha256,
|
|
createHash("sha256").update(bytes).digest("hex"),
|
|
);
|
|
if (
|
|
result.value.kind !== "executable" ||
|
|
result.value.architecture !== "mips"
|
|
)
|
|
throw new Error("Expected a resolved MIPS executable");
|
|
assert.equal(result.value.mips.byteOrder, little ? "little" : "big");
|
|
assert.equal(result.value.mips.abiFlags?.fpAbi, 5);
|
|
assert.equal(ghidraMipsUnsupportedReason(result.value), null);
|
|
assert.deepEqual(await readFile(path), bytes);
|
|
});
|
|
}
|
|
|
|
it("keeps missing ABI data unknown through the public target resolver", async () => {
|
|
const directory = await createTestTempDirectory("rea-mips-resolution-");
|
|
const path = join(directory, "fixture.elf");
|
|
const bytes = fixture();
|
|
bytes.writeUInt32LE(0, 52);
|
|
bytes.writeUInt32LE(0, 172);
|
|
await writeFile(path, bytes);
|
|
const result = await parseBinaryTarget(path);
|
|
if (!result.ok) throw result.error;
|
|
if (
|
|
result.value.kind !== "executable" ||
|
|
result.value.architecture !== "mips"
|
|
)
|
|
throw new Error("Expected a resolved MIPS executable");
|
|
assert.equal(result.value.mips.abiFlags, null);
|
|
assert.match(
|
|
ghidraMipsUnsupportedReason(result.value) ?? "",
|
|
/inspected ABI/u,
|
|
);
|
|
});
|
|
|
|
it("projects a malformed out-of-probe ABI record as a target error", async () => {
|
|
const directory = await createTestTempDirectory("rea-mips-resolution-");
|
|
const path = join(directory, "fixture.elf");
|
|
const bytes = fixture();
|
|
bytes.writeUInt32LE(0xfffffff0, 56);
|
|
await writeFile(path, bytes);
|
|
const result = await parseBinaryTarget(path);
|
|
if (result.ok) throw new Error("Malformed ABI extent was accepted");
|
|
assert.equal(result.error._tag, "BinaryTargetError");
|
|
assert.match(result.error.message, /ABI record lies outside the file/u);
|
|
assert.deepEqual(await readFile(path), bytes);
|
|
});
|
|
|
|
it("preserves cancellation instead of turning it into malformed target data", async () => {
|
|
const directory = await createTestTempDirectory("rea-mips-resolution-");
|
|
const path = join(directory, "fixture.elf");
|
|
await writeFile(path, fixture());
|
|
const controller = new AbortController();
|
|
controller.abort();
|
|
const result = await parseBinaryTarget(path, { signal: controller.signal });
|
|
if (result.ok) throw new Error("Cancelled target resolution succeeded");
|
|
assert.ok(result.error instanceof AnalysisCancelledError);
|
|
});
|