Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/ghidra/GhidraInstallation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { afterEach, describe, expect, it, vi } from "vitest";

import { elf, fat, thinMach } from "../domain/binaryTarget.fixture.js";
import {
dosMz,
elf,
fat,
pe,
thinMach,
} from "../domain/binaryTarget.fixture.js";
import { projectGhidraDoctorInspection } from "./GhidraDoctor.js";
import {
ghidraJavaEnvironment,
Expand Down Expand Up @@ -570,6 +576,38 @@ describe("Ghidra FAT native decompiler admission", () => {
});
});

it("keeps an out-of-prefix short-stub PE header unknown instead of rejecting it as DOS", () => {
const bytes = pe(0x8664, 8192);
dosMz(64).copy(bytes, 0, 0, 60);
const result = inspectGhidraInstallation(
{
environment: {},
installDir: WINDOWS_INSTALL,
platform: "win32",
architecture: "x64",
},
host({
readText: (path) =>
path === WINDOWS_PROPERTIES
? `application.version=${SUPPORTED_GHIDRA_VERSION}\n`
: undefined,
executable: (path) =>
path === WINDOWS_HEADLESS || path === WINDOWS_DECOMPILER,
executableHeader: () => ({
bytes: bytes.subarray(0, 4096),
size: bytes.length,
}),
}),
);
expect(result.status).toBe("available");
expect(
result.checks.find(({ name }) => name === "native_decompiler"),
).toMatchObject({
status: "passed",
detail: expect.stringContaining("could not be established"),
});
});

describe("Ghidra native decompiler admission", () => {
it.each([
{
Expand All @@ -595,7 +633,7 @@ describe("Ghidra native decompiler admission", () => {
},
executableHeader: (path: string) =>
path === MAC_DECOMPILER
? { bytes: elf(2, 1, 62), size: 52 }
? { bytes: elf(2, 1, 62), size: 64 }
: undefined,
},
{
Expand Down Expand Up @@ -633,7 +671,7 @@ describe("Ghidra native decompiler admission", () => {
host({
executableHeader: (path: string) =>
path === LINUX_DECOMPILER
? { bytes: elf(2, 1, 62), size: 52 }
? { bytes: elf(2, 1, 62), size: 64 }
: undefined,
}),
);
Expand Down Expand Up @@ -702,7 +740,7 @@ describe("Ghidra native decompiler admission", () => {
host({
executableHeader: (path: string) =>
path === MAC_DECOMPILER
? { bytes: elf(2, 1, 62), size: 52 }
? { bytes: elf(2, 1, 62), size: 64 }
: undefined,
}),
);
Expand Down
32 changes: 32 additions & 0 deletions tests/boundary/filesystem/artifactMzInventory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ import { classifyArtifactContent } from "../../../src/artifacts/inventory/Artifa
import { ARTIFACT_CLASSIFICATION_PREFIX_BYTES } from "../../../src/artifacts/ArtifactHash.js";
import { scanCanonicalArtifactInventory } from "../../fixtures/artifactInventory.js";
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
import { dosMz, pe } from "../../../src/domain/binaryTarget.fixture.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";

describe("MZ artifact classification", () => {
it("confirms a short DOS stub's PE signature before choosing the format", () => {
const bytes = pe(0x8664, 96, 0x2002);
dosMz(64).copy(bytes, 0, 0, 60);
expect(classifyArtifactContent("short.dll", bytes).format).toBe("pe");
expect(
classifyArtifactContent("short.dll", bytes.subarray(0, 96), bytes.length)
.format,
).toBe("unknown");
expect(
classifyArtifactContent("stub.exe", bytes.subarray(0, 96)).format,
).toBe("dos-mz");
});

it("rejects a Windows header pointer overlapping the DOS header", () => {
expect(classifyArtifactContent("invalid.exe", peFixture(32))).toEqual({
kind: "unknown",
Expand Down Expand Up @@ -76,6 +90,24 @@ describe("MZ artifact classification", () => {
expect.arrayContaining(["dos-mz", "pe"]),
);
});

it("preserves a short-stub PE identity through filesystem inventory", async () => {
const directory = await createTestTempDirectory(
"rea-short-stub-inventory-",
);
const path = join(directory, "short.dll");
const bytes = pe(0x8664, 96, 0x2002);
dosMz(64).copy(bytes, 0, 0, 60);
await writeFile(path, bytes);

const inventory = artifactInventoryResultSchema.parse(
await scanCanonicalArtifactInventory(path),
);
expect(inventory.manifest.root_format).toBe("pe");
expect(inventory.nodes).toContainEqual(
expect.objectContaining({ format: "pe" }),
);
});
});

const dosFixture = (): Buffer => {
Expand Down
14 changes: 14 additions & 0 deletions tests/boundary/go/goBinaryImage.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { expect, it } from "vitest";
import { readGoBinaryImage } from "../../../src/go/GoBinaryImage.js";
import { dosMz } from "../../../src/domain/binaryTarget.fixture.js";
import {
createGoBinaryFixture,
GO_MODULE_TEXT,
} from "../../fixtures/go/image.js";

it.each([32, 64] as const)(
"reads %d-bit PE build metadata after a short DOS stub",
(bits) => {
const fixture = createGoBinaryFixture({ format: "pe", bits });
dosMz(64).copy(fixture.bytes, 0, 0, 60);
expect(readGoBinaryImage(fixture.bytes)).toMatchObject({
format: "pe",
bits,
build_info: { go_version: "go1.26.0", module_text: GO_MODULE_TEXT },
});
},
);

it.each([
["elf", 32, "little"],
["elf", 32, "big"],
Expand Down