feat(ghidra): admit RISC-V ELF analysis through a pinned generic lane (#2151)
* feat(ghidra): admit RISC-V ELF analysis through a pinned generic lane Refs #2063. Recognize `EM_RISCV` (243) as a provider-neutral family that carries its own ELF facts, and bound the first Ghidra lane to little-endian `ET_EXEC` images that declare the double-float ABI, pinned to `RISCV:LE:32:default` or `RISCV:LE:64:default` with the `gcc` compiler specification. Ghidra 12.1.4 is not reliable without the pin: its `riscv.opinion` pins `variant="default"` for the 64-bit constraint and leaves the 32-bit constraint with the comment that the loader "must choose between 'default' and 'AndeStar' variants". Measured on the installed release tree, a source-owned generic RV32 ELF with no Andes declaration imports as `RISCV:LE:32:AndeStar_v5:gcc`, while RV64 resolves to `RISCV:LE:64:default:gcc`. The lane therefore states the language per width and refuses any caller-selected processor outside it instead of inheriting the header guess. Refusals name the declaration rather than falling back to a guessed profile: big-endian images (Ghidra 12.1.4 ships no big-endian RISC-V language), non-`ET_EXEC` images, `EF_RISCV_RVE`, `EF_RISCV_TSO`, and every float ABI other than double, which the pinned generic compiler specifications model. The raw e_flags bits stay in Evidence as declarations, not as instruction semantics. Family recognition now has one owner for the ELF-metadata families: `ELF_METADATA_ARCHITECTURES`, `isElfMetadataArchitecture`, and `HeaderArchitecture` replace ten `Exclude<BinaryArchitecture, "mips">` sites that would each have needed a second hand-edited member. `ghidraPinnedLanguage` is that owner for the import commitment: the session readback previously re-derived only the MIPS pin, so a profile committing any second pinned language failed with a language-mismatch error before the bridge started. Hopper and IDA refuse RISC-V by name, matching the MIPS wording. Caller-supplied raw-binary entry keeps its existing four-family list; this lane is ELF-header admission only. Verification on macOS 15.7.7 arm64, Node 24.18.0, Ghidra 12.1.4 PUBLIC and JDK 21.0.12.1, with the host decompiler built from the release sources for `os/mac_arm_64`: `npm run check` passes; `npm run test:fast` passes 396 test files; `npm run verify:test-discovery` discovers all 917 test files exactly once. Source-owned RV32 and RV64 double-float `ET_EXEC` fixtures complete `rea analyze --provider ghidra` and `rea decompile rea_riscv_entry`, committing `RISCV:LE:32:default` and `RISCV:LE:64:default` with `language_selection: provider-pinned-v1`; the soft-float RV64 shape is refused with the named float-ABI reason, and the full refusal matrix including the RV32 and width-mismatch shapes is covered by unit tests. No `verify:ghidra:riscv` lane script is included in this change; the runs above were performed manually against the installed engine. Fork pull requests receive no CI check runs, so the real-provider scope above is the evidence available for review. * fix(ghidra): bound the RISC-V lane by declared flags and document it Address review feedback on #2151. `e_flags` bits outside `EF_RISCV_RVC | EF_RISCV_FLOAT_ABI` are now refused with the offending value named, so a reserved or newer producer declaration cannot enter the verified generic lane by falling through the RVE, TSO and float-ABI checks. Admission reads `e_type` only, so the refusal text no longer calls `ET_EXEC` static: a fixed-address declaration does not establish static linkage, and `PT_INTERP` and dynamic dependencies are not inspected. The session records that boundary as a limitation rather than inferring a load model. Add `docs/ghidra-riscv.md` in the shape of the MIPS lane guide (admission boundary, refusal table, why the language is pinned per class, Evidence fields, and what the fixtures do not establish), and correct `docs/firmware-analysis.md`, which still told users that MIPS and RISC-V decompilation had not been added. Verification after the change: `npm run check` and `npm run docs:check` pass; the RISC-V module tests pass (17 lane cases plus 9 parser cases); on Ghidra 12.1.4 with JDK 21.0.12.1 the RV32 and RV64 double-float fixtures still analyze and decompile with `RISCV:LE:32:default` / `RISCV:LE:64:default` and `language_selection: provider-pinned-v1`, the soft-float fixture is still refused by name, and the new linkage limitation appears in the emitted Evidence. * fix(ghidra): align RISC-V pinning with admission Reuse the complete lane admission boundary when selecting a language, retain unsigned unsupported flag bits in diagnostics, and derive float ABI metadata from the source declaration. * test(ghidra): verify RISC-V CLI and MCP workflows Build source-owned RV32/RV64 fixtures with Clang and LLD, establish independent ELF and symbol facts with LLVM readelf, and verify real provider evidence, instruction bytes, calls, decompilation, schema projections, snapshot replay, refusal diagnostics, source immutability and cleanup. Verified on macOS ARM64 with Ghidra 12.1.4, JDK 21 and LLVM/LLD 23.1.2. Dynamic linkage, big-endian, Andes and RVE/TSO workflows remain unverified; ISA attributes are not inspected. --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
@@ -17,6 +17,10 @@ import {
|
||||
ghidraMipsProfileParameters,
|
||||
ghidraMipsSharedLanguageOverride,
|
||||
} from "./GhidraMipsProfile.js";
|
||||
import {
|
||||
ghidraRiscvLanguageOverride,
|
||||
ghidraRiscvProfileParameters,
|
||||
} from "./GhidraRiscvProfile.js";
|
||||
import { ghidraRawBinaryLanguage } from "./GhidraRawBinaryLanguage.js";
|
||||
|
||||
import {
|
||||
@@ -32,6 +36,17 @@ import {
|
||||
NATIVE_AOT_PROFILE_CONTRACT_REVISION,
|
||||
} from "../domain/native/nativeAotPe.js";
|
||||
|
||||
/**
|
||||
* The processor language REA pins for this target, or undefined when Ghidra may
|
||||
* select from the header. The committed profile and the session readback both
|
||||
* compare against this single owner rather than re-deriving a lane.
|
||||
*/
|
||||
export const ghidraPinnedLanguage = (
|
||||
target: BinaryTarget,
|
||||
): GhidraLanguageOverride | undefined =>
|
||||
ghidraMipsSharedLanguageOverride(target) ??
|
||||
ghidraRiscvLanguageOverride(target);
|
||||
|
||||
/** Resolve version-bound, deterministic semantics before Ghidra imports a target. */
|
||||
export const resolveGhidraAnalysisProfile = async (
|
||||
target: BinaryTarget,
|
||||
@@ -136,22 +151,26 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
const dosMz = target.format === "dos-mz";
|
||||
const dosCom = target.format === "dos-com";
|
||||
const dos = dosMz || dosCom;
|
||||
const mipsLanguage = ghidraMipsSharedLanguageOverride(target);
|
||||
const pinnedLanguage = ghidraPinnedLanguage(target);
|
||||
if (
|
||||
mipsLanguage !== undefined &&
|
||||
pinnedLanguage !== undefined &&
|
||||
languageOverride !== undefined &&
|
||||
(languageOverride.languageId !== mipsLanguage.languageId ||
|
||||
(languageOverride.languageId !== pinnedLanguage.languageId ||
|
||||
(languageOverride.compilerSpecId !== undefined &&
|
||||
languageOverride.compilerSpecId !== mipsLanguage.compilerSpecId))
|
||||
languageOverride.compilerSpecId !== pinnedLanguage.compilerSpecId))
|
||||
)
|
||||
return err(
|
||||
new AnalysisUnsupportedTargetError(
|
||||
"resolve_analysis_profile",
|
||||
target.path,
|
||||
"The caller-selected processor or compiler specification is outside the verified endian-matched MIPS32 shared-image profile. The requested override has not been changed.",
|
||||
`The caller-selected processor or compiler specification is outside the verified ${
|
||||
target.architecture === "riscv"
|
||||
? "generic little-endian RISC-V"
|
||||
: "endian-matched MIPS32 shared-image"
|
||||
} profile. The requested override has not been changed.`,
|
||||
),
|
||||
);
|
||||
const selectedLanguage = languageOverride ?? mipsLanguage;
|
||||
const selectedLanguage = languageOverride ?? pinnedLanguage;
|
||||
// DOS imports already force an admitted loader, language, and context.
|
||||
if (dos && languageOverride !== undefined)
|
||||
return Promise.resolve(
|
||||
@@ -227,7 +246,7 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
? "auto-default"
|
||||
: (selectedLanguage.compilerSpecId ?? "language-default"),
|
||||
...(languageOverride === undefined
|
||||
? mipsLanguage === undefined
|
||||
? pinnedLanguage === undefined
|
||||
? {}
|
||||
: { language_selection: "provider-pinned-v1" }
|
||||
: { language_selection: "configured-v1" }),
|
||||
@@ -251,6 +270,7 @@ export const resolveGhidraAnalysisProfile = async (
|
||||
: {}),
|
||||
...analyzerParameters,
|
||||
...ghidraMipsProfileParameters(target),
|
||||
...ghidraRiscvProfileParameters(target),
|
||||
...(pspExtension?.ok === true
|
||||
? {
|
||||
mips_support_lane: "psp-elf32-exec-eabi32-allegrex-v2",
|
||||
|
||||
@@ -66,6 +66,7 @@ const SUPPORTED_ARCHITECTURES = new Set([
|
||||
"arm",
|
||||
"arm64",
|
||||
"mips",
|
||||
"riscv",
|
||||
]);
|
||||
|
||||
/** Ghidra candidate backed by an isolated ephemeral headless import. */
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
ghidraSeedLimitations,
|
||||
} from "./GhidraAnalysisSeeds.js";
|
||||
import {
|
||||
ghidraPinnedLanguage,
|
||||
ghidraProfileDisabledAnalyzers,
|
||||
ghidraProfileLanguageOverride,
|
||||
sameGhidraDisabledAnalyzers,
|
||||
@@ -61,10 +62,10 @@ import {
|
||||
ghidraMipsSoftFloatFailure,
|
||||
ghidraMipsRelocatableLimitations,
|
||||
ghidraMipsSharedLimitations,
|
||||
ghidraMipsSharedLanguageOverride,
|
||||
ghidraMipsSoftFloatLimitations,
|
||||
isGhidraMipsSoftFloatTarget,
|
||||
} from "./GhidraMipsProfile.js";
|
||||
import { ghidraRiscvLimitations } from "./GhidraRiscvProfile.js";
|
||||
import { unverifiedGhidraBuildLimitation } from "./GhidraInstallationPolicy.js";
|
||||
import { GhidraHeadlessLauncher } from "./GhidraLauncher.js";
|
||||
import { attestGhidraNativeLoadImage } from "./GhidraLoadImageAttest.js";
|
||||
@@ -453,7 +454,7 @@ export const createGhidraProviderClient = (input: {
|
||||
if (
|
||||
!sameGhidraLanguageOverride(
|
||||
languageOverride,
|
||||
config.ghidraLanguageOverride ?? ghidraMipsSharedLanguageOverride(target),
|
||||
config.ghidraLanguageOverride ?? ghidraPinnedLanguage(target),
|
||||
)
|
||||
)
|
||||
return unavailableClient(
|
||||
@@ -523,6 +524,7 @@ export const createGhidraProviderClient = (input: {
|
||||
: []),
|
||||
...ghidraMipsRelocatableLimitations(target),
|
||||
...ghidraMipsSharedLimitations(target),
|
||||
...ghidraRiscvLimitations(target),
|
||||
];
|
||||
const providerLimitations =
|
||||
installation.platform === "win32"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
||||
import { ghidraMipsUnsupportedReason } from "./GhidraMipsProfile.js";
|
||||
import { ghidraRiscvUnsupportedReason } from "./GhidraRiscvProfile.js";
|
||||
|
||||
/** Allegrex's ELF machine declaration, not a filename or inferred instruction. */
|
||||
export const isGhidraPspTarget = (target: BinaryTarget): boolean =>
|
||||
@@ -13,6 +14,8 @@ export const isGhidraPspTarget = (target: BinaryTarget): boolean =>
|
||||
export const ghidraProcessorUnsupportedReason = (
|
||||
target: BinaryTarget,
|
||||
): string | null => {
|
||||
if (target.kind === "executable" && target.architecture === "riscv")
|
||||
return ghidraRiscvUnsupportedReason(target);
|
||||
if (
|
||||
target.kind !== "executable" ||
|
||||
target.architecture !== "mips" ||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { BinaryArchitecture } from "../domain/binaryTargetTypes.js";
|
||||
import type { HeaderArchitecture } from "../domain/binaryTargetTypes.js";
|
||||
|
||||
/** Bound Ghidra language for a caller-supplied raw-binary architecture. */
|
||||
export interface GhidraRawBinaryLanguage {
|
||||
@@ -13,7 +13,7 @@ export interface GhidraRawBinaryLanguage {
|
||||
* Big-endian, Thumb-only, and Cortex-M variants are out of scope.
|
||||
*/
|
||||
export const ghidraRawBinaryLanguage = (
|
||||
architecture: Exclude<BinaryArchitecture, "mips">,
|
||||
architecture: HeaderArchitecture,
|
||||
): GhidraRawBinaryLanguage => {
|
||||
switch (architecture) {
|
||||
case "x86":
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { parseConfig } from "../config/parseConfig.js";
|
||||
import type {
|
||||
BinaryTarget,
|
||||
RiscvElfMetadata,
|
||||
} from "../domain/binaryTargetTypes.js";
|
||||
import { silentLogger } from "../logger.js";
|
||||
import { HopperProvider } from "../hopper/HopperProvider.js";
|
||||
import { hopperLoaderArgsForTarget } from "../hopper/HopperAnalysisProfile.js";
|
||||
import { IdaProvider } from "../ida/IdaProvider.js";
|
||||
import { GhidraProvider } from "./GhidraProvider.js";
|
||||
import { ghidraRiscvLimitations } from "./GhidraRiscvProfile.js";
|
||||
import type { GhidraInstallationHost } from "./GhidraInstallation.js";
|
||||
import {
|
||||
ghidraPinnedLanguage,
|
||||
ghidraProfileLanguageOverride,
|
||||
sameGhidraLanguageOverride,
|
||||
} from "./GhidraAnalysisProfile.js";
|
||||
|
||||
// Abbreviated from Ghidra 12.1.4 Processors/RISCV/data/languages/riscv.ldefs.
|
||||
// The release tree ships no big-endian or ELF-class-mismatched RISC-V language.
|
||||
const RISCV_LDEFS = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<language_definitions>
|
||||
<language processor="RISCV"
|
||||
endian="little"
|
||||
size="64"
|
||||
variant="default"
|
||||
version="1.4"
|
||||
slafile="riscv.lp64d.sla"
|
||||
processorspec="RV64.pspec"
|
||||
id="RISCV:LE:64:default">
|
||||
<description>RISC-V 64 little default</description>
|
||||
<compiler name="gcc" spec="riscv64-fp.cspec" id="gcc"/>
|
||||
</language>
|
||||
<language processor="RISCV"
|
||||
endian="little"
|
||||
size="32"
|
||||
variant="default"
|
||||
version="1.4"
|
||||
slafile="riscv.ilp32d.sla"
|
||||
processorspec="RV32.pspec"
|
||||
id="RISCV:LE:32:default">
|
||||
<description>RISC-V 32 little default</description>
|
||||
<compiler name="gcc" spec="riscv32-fp.cspec" id="gcc"/>
|
||||
</language>
|
||||
</language_definitions>
|
||||
`;
|
||||
|
||||
// Fake installation inspection tests admission/profile contracts, not Ghidra.
|
||||
const host: GhidraInstallationHost = {
|
||||
platform: "linux",
|
||||
architecture: "x64",
|
||||
readText: () => "application.version=12.1.4\n",
|
||||
executable: () => true,
|
||||
probeJava: () => ({
|
||||
version: "21.0.11",
|
||||
major: 21,
|
||||
home: "/jdk-21",
|
||||
bits: 64,
|
||||
runtime: "jdk",
|
||||
}),
|
||||
};
|
||||
const configOf = (
|
||||
installDir: string,
|
||||
environment: Record<string, string> = {},
|
||||
) => {
|
||||
const parsed = parseConfig({
|
||||
GHIDRA_INSTALL_DIR: installDir,
|
||||
...environment,
|
||||
});
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
return parsed.value;
|
||||
};
|
||||
const target = (change: Partial<RiscvElfMetadata> = {}): BinaryTarget => ({
|
||||
path: "/fixture.elf",
|
||||
sha256: "a".repeat(64),
|
||||
kind: "executable",
|
||||
format: "elf",
|
||||
architecture: "riscv",
|
||||
availableArchitectures: ["riscv"],
|
||||
riscv: {
|
||||
elfClass: 64,
|
||||
byteOrder: "little",
|
||||
type: 2,
|
||||
flags: 0x5,
|
||||
...change,
|
||||
},
|
||||
});
|
||||
|
||||
let installDir = "";
|
||||
beforeAll(async () => {
|
||||
installDir = await mkdtemp(join(tmpdir(), "rea-ghidra-riscv-languages-"));
|
||||
const languages = join(installDir, "Ghidra/Processors/RISCV/data/languages");
|
||||
await mkdir(languages, { recursive: true });
|
||||
await writeFile(join(languages, "riscv.ldefs"), RISCV_LDEFS);
|
||||
});
|
||||
afterAll(async () => {
|
||||
await rm(installDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const ghidra = (environment: Record<string, string> = {}) =>
|
||||
new GhidraProvider(configOf(installDir, environment), silentLogger, {}, host);
|
||||
|
||||
describe("bounded Ghidra RISC-V admission", () => {
|
||||
it("pins the generic language per width instead of accepting the header guess", async () => {
|
||||
const provider = ghidra();
|
||||
const rv64 = target();
|
||||
const rv32 = target({ elfClass: 32, flags: 0x4 });
|
||||
expect(provider.inspectTargetSupport(rv64).status).toBe("supported");
|
||||
expect(provider.inspectTargetSupport(rv32).status).toBe("supported");
|
||||
expect(ghidraPinnedLanguage(rv64)).toEqual({
|
||||
languageId: "RISCV:LE:64:default",
|
||||
compilerSpecId: "gcc",
|
||||
});
|
||||
expect(ghidraPinnedLanguage(rv32)).toEqual({
|
||||
languageId: "RISCV:LE:32:default",
|
||||
compilerSpecId: "gcc",
|
||||
});
|
||||
const wide = await provider.resolveAnalysisProfile(rv64);
|
||||
const narrow = await provider.resolveAnalysisProfile(rv32);
|
||||
if (!wide.ok || !narrow.ok)
|
||||
throw new Error("expected admitted RISC-V profiles");
|
||||
expect(wide.value.profile?.parameters.riscv_elf).toEqual({
|
||||
elf_class: 64,
|
||||
byte_order: "little",
|
||||
type: 2,
|
||||
flags: 5,
|
||||
float_abi_double: true,
|
||||
rvc_declared: true,
|
||||
rve_declared: false,
|
||||
tso_declared: false,
|
||||
});
|
||||
expect(wide.value.profile?.parameters.riscv_support_lane).toBe(
|
||||
"elf64-exec-le-double-float-generic-v1",
|
||||
);
|
||||
expect(narrow.value.profile?.parameters.riscv_support_lane).toBe(
|
||||
"elf32-exec-le-double-float-generic-v1",
|
||||
);
|
||||
expect(narrow.value.profile?.parameters.riscv_elf).toMatchObject({
|
||||
elf_class: 32,
|
||||
rvc_declared: false,
|
||||
});
|
||||
expect(wide.value.profile?.digest).not.toBe(narrow.value.profile?.digest);
|
||||
});
|
||||
|
||||
it("commits the pinned language the session readback expects", async () => {
|
||||
// A pinned lane has one owner: the committed profile and the open_binary
|
||||
// readback must agree without either re-deriving a different family's pin.
|
||||
const provider = ghidra();
|
||||
for (const value of [target(), target({ elfClass: 32 })]) {
|
||||
const resolved = await provider.resolveAnalysisProfile(value);
|
||||
if (!resolved.ok || resolved.value.profile === null)
|
||||
throw new Error("expected a RISC-V profile");
|
||||
expect(
|
||||
sameGhidraLanguageOverride(
|
||||
ghidraProfileLanguageOverride(resolved.value.profile.parameters),
|
||||
ghidraPinnedLanguage(value),
|
||||
),
|
||||
).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
[{ byteOrder: "big" }, "little-endian"],
|
||||
[{ type: 3 }, "fixed-address ET_EXEC only"],
|
||||
[{ type: 1 }, "fixed-address ET_EXEC only"],
|
||||
[{ type: 0xffa0 }, "e_type 65440"],
|
||||
[{ flags: 0xc }, "EF_RISCV_RVE"],
|
||||
[{ flags: 0x14 }, "EF_RISCV_TSO"],
|
||||
[{ flags: 0x1 }, "soft-float convention"],
|
||||
[{ flags: 0x3 }, "single-float"],
|
||||
[{ flags: 0x7 }, "quad-float"],
|
||||
[{ flags: 0x8000_0005 }, "(0x80000000)"],
|
||||
[{ flags: 0x24 }, "bits outside the verified lane"],
|
||||
] satisfies [Partial<RiscvElfMetadata>, string][])(
|
||||
"refuses an unverified RISC-V interpretation %j",
|
||||
async (change, reason) => {
|
||||
const provider = ghidra();
|
||||
const value = target(change);
|
||||
expect(provider.inspectTargetSupport(value)).toMatchObject({
|
||||
status: "unsupported",
|
||||
code: "architecture_unsupported",
|
||||
reason: expect.stringContaining(reason),
|
||||
});
|
||||
expect(await provider.resolveAnalysisProfile(value)).toEqual(
|
||||
expect.objectContaining({ ok: false }),
|
||||
);
|
||||
expect(ghidraPinnedLanguage(value)).toBeUndefined();
|
||||
},
|
||||
);
|
||||
|
||||
it("refuses a caller-selected processor outside the verified generic lane", async () => {
|
||||
const resolved = await ghidra({
|
||||
REA_GHIDRA_LANGUAGE_ID: "RISCV:LE:32:AndeStar_v5",
|
||||
}).resolveAnalysisProfile(target({ elfClass: 32 }));
|
||||
expect(resolved.ok).toBe(false);
|
||||
if (resolved.ok) throw new Error("expected a refusal");
|
||||
expect(resolved.error.message).toContain("generic little-endian RISC-V");
|
||||
});
|
||||
|
||||
it("keeps a caller-selected language that matches the pin as configured", async () => {
|
||||
const resolved = await ghidra({
|
||||
REA_GHIDRA_LANGUAGE_ID: "RISCV:LE:64:default",
|
||||
REA_GHIDRA_COMPILER_SPEC_ID: "gcc",
|
||||
}).resolveAnalysisProfile(target());
|
||||
if (!resolved.ok || resolved.value.profile === null)
|
||||
throw new Error("expected a configured profile");
|
||||
expect(resolved.value.profile.parameters.language_selection).toBe(
|
||||
"configured-v1",
|
||||
);
|
||||
expect(resolved.value.profile.parameters.language_id).toBe(
|
||||
"RISCV:LE:64:default",
|
||||
);
|
||||
});
|
||||
|
||||
it("states the linkage claim boundary in the lane limitations", () => {
|
||||
const notes = ghidraRiscvLimitations(target());
|
||||
expect(notes).toHaveLength(3);
|
||||
expect(notes.join(" ")).toContain("does not establish static linkage");
|
||||
expect(notes[0]).toContain("RISCV:LE:64:default");
|
||||
expect(
|
||||
ghidraRiscvLimitations({
|
||||
path: "/other.elf",
|
||||
sha256: "b".repeat(64),
|
||||
kind: "executable",
|
||||
format: "elf",
|
||||
architecture: "x86_64",
|
||||
availableArchitectures: ["x86_64"],
|
||||
}),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps RISC-V out of unverified Hopper and IDA adapter lanes", () => {
|
||||
const hopper = new HopperProvider(
|
||||
configOf("/hopper"),
|
||||
silentLogger,
|
||||
{},
|
||||
"linux",
|
||||
);
|
||||
expect(hopper.inspectTargetSupport(target()).status).toBe("unsupported");
|
||||
expect(hopperLoaderArgsForTarget(target()).ok).toBe(false);
|
||||
expect(hopperLoaderArgsForTarget(target())).toEqual(
|
||||
expect.objectContaining({
|
||||
ok: false,
|
||||
error: expect.objectContaining({
|
||||
message: expect.stringContaining("RISC-V"),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const ida = new IdaProvider(configOf("/ida"), {});
|
||||
expect(ida.inspectTargetSupport(target()).status).toBe("unsupported");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,129 @@
|
||||
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { GhidraLanguageOverride } from "../config/ghidraLanguageOverride.js";
|
||||
|
||||
/**
|
||||
* Ghidra's generic RISC-V languages model the double-float ABI: the shipped
|
||||
* `riscv.ldefs` maps `RISCV:LE:{32,64}:default` to the `riscv{32,64}-fp.cspec`
|
||||
* compiler specification. Soft, single and quad ABI declarations therefore need
|
||||
* their own verification rather than inheriting this lane.
|
||||
*/
|
||||
const EF_RISCV_RVC = 0x0001;
|
||||
const EF_RISCV_FLOAT_ABI = 0x0006;
|
||||
const EF_RISCV_FLOAT_ABI_DOUBLE = 0x0004;
|
||||
const EF_RISCV_RVE = 0x0008;
|
||||
const EF_RISCV_TSO = 0x0010;
|
||||
|
||||
const ET_REL = 1;
|
||||
const ET_EXEC = 2;
|
||||
const ET_DYN = 3;
|
||||
|
||||
/**
|
||||
* Bits this lane reads. Anything else in `e_flags` — reserved today or defined by
|
||||
* a newer psABI — is refused rather than assumed compatible.
|
||||
*/
|
||||
const EF_RISCV_DECLARED_BITS = EF_RISCV_RVC | EF_RISCV_FLOAT_ABI;
|
||||
|
||||
/** Header declarations outside the verified generic-language lane. */
|
||||
const riscvUnsupportedFloatAbi = (abi: number): string =>
|
||||
abi === 0
|
||||
? "unspecified (soft-float convention)"
|
||||
: abi === 0x0002
|
||||
? "single-float"
|
||||
: "quad-float";
|
||||
|
||||
/**
|
||||
* Keep family recognition separate from the deliberately bounded Ghidra lane.
|
||||
*
|
||||
* Ghidra 12.1.4 selects `RISCV:LE:32:AndeStar_v5` for an ordinary RV32 ELF: its
|
||||
* `riscv.opinion` pins `variant="default"` for the 64-bit constraint and leaves
|
||||
* the 32-bit one with the comment that the loader "must choose between 'default'
|
||||
* and 'AndeStar' variants". The lane therefore pins the language instead of
|
||||
* accepting the header-based guess.
|
||||
*/
|
||||
export const ghidraRiscvUnsupportedReason = (
|
||||
target: BinaryTarget,
|
||||
): string | null => {
|
||||
if (target.kind !== "executable" || target.architecture !== "riscv")
|
||||
return null;
|
||||
const metadata = target.riscv;
|
||||
if (target.format !== "elf")
|
||||
return "RISC-V analysis requires ELF header metadata; a family name alone does not select an ISA or ABI.";
|
||||
if (metadata.byteOrder !== "little")
|
||||
return "This Ghidra RISC-V lane supports little-endian images only; Ghidra 12.1.4 ships no big-endian RISC-V language.";
|
||||
if (metadata.elfClass !== 32 && metadata.elfClass !== 64)
|
||||
return "This Ghidra RISC-V lane requires ELF32 or ELF64; the class decides which generic language is pinned.";
|
||||
if ((metadata.flags & EF_RISCV_RVE) !== 0)
|
||||
return "EF_RISCV_RVE declares the embedded 15-register ABI, which needs its own processor specification and compiler verification.";
|
||||
if ((metadata.flags & EF_RISCV_TSO) !== 0)
|
||||
return "EF_RISCV_TSO declares the total-store-order memory model; this lane verifies the baseline model only.";
|
||||
if (metadata.type !== ET_EXEC)
|
||||
return `This Ghidra RISC-V lane supports fixed-address ET_EXEC only; ${
|
||||
metadata.type === ET_REL
|
||||
? "relocatable"
|
||||
: metadata.type === ET_DYN
|
||||
? "shared and position-independent"
|
||||
: `e_type ${metadata.type}`
|
||||
} images need separate load verification. ET_EXEC alone does not establish static linkage, which this lane does not inspect.`;
|
||||
const abi = metadata.flags & EF_RISCV_FLOAT_ABI;
|
||||
if (abi !== EF_RISCV_FLOAT_ABI_DOUBLE)
|
||||
return `This Ghidra RISC-V lane requires the EF_RISCV_FLOAT_ABI_DOUBLE declaration (${riscvUnsupportedFloatAbi(
|
||||
abi,
|
||||
)} observed); the pinned generic language uses the double-float compiler specification.`;
|
||||
const unsupportedFlags = (metadata.flags & ~EF_RISCV_DECLARED_BITS) >>> 0;
|
||||
if (unsupportedFlags !== 0)
|
||||
return `RISC-V e_flags declares bits outside the verified lane (0x${unsupportedFlags.toString(16)}); reserved or newer declarations are not treated as compatible with this profile.`;
|
||||
return null;
|
||||
};
|
||||
|
||||
/** Pin a language only for declarations admitted by the same lane boundary. */
|
||||
export const ghidraRiscvLanguageOverride = (
|
||||
target: BinaryTarget,
|
||||
): GhidraLanguageOverride | undefined =>
|
||||
target.kind === "executable" &&
|
||||
target.architecture === "riscv" &&
|
||||
ghidraRiscvUnsupportedReason(target) === null
|
||||
? {
|
||||
languageId: `RISCV:LE:${target.riscv.elfClass}:default`,
|
||||
compilerSpecId: "gcc",
|
||||
}
|
||||
: undefined;
|
||||
|
||||
/** Commit source ELF interpretation without confusing target ISA with host CPU. */
|
||||
export const ghidraRiscvProfileParameters = (
|
||||
target: BinaryTarget,
|
||||
): Readonly<Record<string, JsonValue>> =>
|
||||
target.kind === "executable" &&
|
||||
target.architecture === "riscv" &&
|
||||
target.riscv !== undefined
|
||||
? {
|
||||
riscv_elf: {
|
||||
elf_class: target.riscv.elfClass,
|
||||
byte_order: target.riscv.byteOrder,
|
||||
type: target.riscv.type,
|
||||
flags: target.riscv.flags,
|
||||
float_abi_double:
|
||||
(target.riscv.flags & EF_RISCV_FLOAT_ABI) ===
|
||||
EF_RISCV_FLOAT_ABI_DOUBLE,
|
||||
rvc_declared: (target.riscv.flags & EF_RISCV_RVC) !== 0,
|
||||
rve_declared: (target.riscv.flags & EF_RISCV_RVE) !== 0,
|
||||
tso_declared: (target.riscv.flags & EF_RISCV_TSO) !== 0,
|
||||
},
|
||||
riscv_support_lane:
|
||||
target.riscv.elfClass === 32
|
||||
? "elf32-exec-le-double-float-generic-v1"
|
||||
: "elf64-exec-le-double-float-generic-v1",
|
||||
}
|
||||
: {};
|
||||
|
||||
/** The pinned language is an analysis convention, not proof of the producer. */
|
||||
export const ghidraRiscvLimitations = (
|
||||
target: BinaryTarget,
|
||||
): readonly string[] =>
|
||||
target.kind === "executable" && target.architecture === "riscv"
|
||||
? [
|
||||
`RISC-V ELF: REA pins Ghidra's generic \`RISCV:LE:${target.riscv.elfClass}:default\` language with the \`gcc\` compiler specification instead of accepting the loader's header-based choice, which selects \`RISCV:LE:32:AndeStar_v5\` for ordinary RV32 images on Ghidra 12.1.4.`,
|
||||
"The pinned generic language models the double-float ABI declared by `e_flags`; the declaration is recorded as an ELF fact and is not evidence of the original compiler or of verified instruction semantics.",
|
||||
"Admission reads `e_type` only: a fixed-address ET_EXEC declaration does not establish static linkage, and program headers (`PT_INTERP`, dynamic dependencies) are not inspected by this lane.",
|
||||
]
|
||||
: [];
|
||||
Reference in New Issue
Block a user