Files
Entrpiandmorluto 782b12a826 fix(ghidra)!: refuse Xtensa ELF unless .xtensa.info declares PC-relative literals (#16664)
* fix(ghidra)!: refuse Xtensa ELF unless .xtensa.info declares PC-relative literals

Ghidra's Xtensa language computes every L32R address from the instruction's
own address and never reads LITBASE, so a build with absolute (LITBASE)
literals decodes to wrong literal values and references without any sign of
error. Main admitted every Xtensa ELF without reading the build declaration.

Target resolution now reads the `.xtensa.info` note once, by section name,
and records its USE_ABSOLUTE_LITERALS and ABI declarations beside the header
facts. Ghidra admission refuses absolute literals and any declaration it
cannot establish, with architecture_unsupported and a reason that names the
declaration. Call0 builds stay admitted and carry a limitation naming the
calling-convention mismatch until a call0 lane assigns Ghidra's __call0
prototype. The header-selected language and profile from #11178 are kept.

The reader, note parser, fixture and real-Ghidra lane come from #7486; its
header gates, pinned language and separate guide page are not ported.

BREAKING CHANGE: Ghidra now refuses Xtensa ELF files whose .xtensa.info
section declares absolute literals (USE_ABSOLUTE_LITERALS=1), declares a
literal or ABI value other than 0 or 1, cannot be read, or is missing. Main
opened them and decoded every L32R as PC-relative. No setting restores the
old behavior.

* refactor(ghidra): move the processor dispatch out of the PSP profile

ghidraProcessorUnsupportedReason selects each architecture's profile for
every Ghidra target, so it now lives in GhidraProcessorAdmission.ts.
GhidraPspProfile.ts keeps only the Allegrex predicate and the PSPSDK
declaration checks, as ghidraPspUnsupportedReason. The callers' result
is renamed from mipsReason to processorReason.

* refactor(targets): read .xtensa.info through the shared ELF section walk

The .xtensa.info lookup had its own batched section-header loop and its
own cancellable file reader. It is now a declaration reader like the
RISC-V one: readXtensaElfDeclarations walks the headers with
visitElfSectionHeaders, and target resolution drives it with
readElfDeclarations, which owns the descriptor reads and cancellation.
The section-name table is copied in reads of at most 64 KiB, within the
driver's window.

The boundary tests drive the reader with a recording source for the
bounded-request check, and check cancellation through parseBinaryTarget
at the note payload read.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-12-05 06:19:17 +00:00

98 lines
3.9 KiB
TypeScript

import { fileIdentity } from "../fixtures/evidenceSubject.js";
import { goBinarySchema } from "../../src/domain/go/goBinary.js";
import { rustBinarySchema } from "../../src/domain/rust/rustBinary.js";
import { expect } from "vitest";
import { toolContract } from "../../src/contracts/toolContracts.js";
import { createGoBinaryFixture } from "../fixtures/go/image.js";
import { createRustImage } from "../fixtures/rust/image.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
import { join } from "node:path";
import { writeFile } from "node:fs/promises";
import { cliTest } from "../support/cli/cliFixture.js";
cliTest(
"preserves classifier-known Hexagon and Xtensa ELF identity through Go and Rust CLI/MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-machine-identity-");
const { client, call } = await connectLocalToolsMcp();
for (const language of ["go", "rust"] as const) {
const operation =
language === "go" ? "inspect_go_binary" : "inspect_rust_binary";
const command =
language === "go" ? "inspect-go-binary" : "inspect-rust-binary";
for (const item of [
{ machine: 164, architecture: "hexagon", subject: "hexagon" },
{ machine: 94, architecture: "xtensa", subject: "xtensa" },
{ machine: 62, architecture: "amd64", subject: "x86_64" },
]) {
const bytes =
language === "go"
? createGoBinaryFixture({ omitBuildInfo: true }).bytes
: createRustImage({
format: "elf",
omitSection: true,
includeRustc: true,
});
bytes.writeUInt16LE(item.machine, 18);
const path = join(root, `${language}-${item.machine}.elf`);
await writeFile(path, bytes);
const cliResponse = await cli.run({
arguments: [command, path, "--json"],
});
expect(
cliResponse.exitCode,
`${cliResponse.stderr}\n${JSON.stringify(cliResponse.json)}`,
).toBe(0);
const cliEvidence = toolContract(operation).outputSchema.parse(
cliResponse.json,
);
const cliReport =
language === "go"
? goBinarySchema.parse(cliEvidence.normalized_result)
: rustBinarySchema.parse(cliEvidence.normalized_result);
expect(cliReport.architecture).toBe(item.architecture);
expect(fileIdentity(cliEvidence.subject).architecture).toBe(
item.subject,
);
const mcpResponse = await call(operation, { path });
expect(mcpResponse.isError, JSON.stringify(mcpResponse)).not.toBe(true);
const mcpEvidence = toolContract(operation).outputSchema.parse(
mcpResponse.structuredContent,
);
const mcpReport =
language === "go"
? goBinarySchema.parse(mcpEvidence.normalized_result)
: rustBinarySchema.parse(mcpEvidence.normalized_result);
expect(mcpReport.architecture).toBe(item.architecture);
expect(fileIdentity(mcpEvidence.subject).architecture).toBe(
item.subject,
);
}
const malformed =
language === "go"
? createGoBinaryFixture({ omitBuildInfo: true }).bytes
: createRustImage({
format: "elf",
omitSection: true,
includeRustc: true,
});
malformed[0] = 0;
const path = join(root, `${language}-malformed.elf`);
await writeFile(path, malformed);
const cliFailure = await cli.run({
arguments: [command, path, "--json"],
});
expect(cliFailure.exitCode).not.toBe(0);
expect(cliFailure.json).not.toHaveProperty("normalized_result");
const mcpFailure = await call(operation, { path });
expect(mcpFailure.isError).toBe(true);
expect(mcpFailure.structuredContent).toBeUndefined();
}
await client.ping();
},
);