* 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>
253 lines
9.7 KiB
TypeScript
253 lines
9.7 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { open, type FileHandle } from "node:fs/promises";
|
|
import { z } from "zod";
|
|
|
|
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
|
import { BinaryTargetError } from "../domain/configurationErrors.js";
|
|
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
|
import {
|
|
readFatSliceDeclarations,
|
|
validateFatSlice,
|
|
} from "../domain/apple/machoContainer.js";
|
|
import {
|
|
HopperCancelledError,
|
|
HopperStartError,
|
|
} from "../domain/hopperErrors.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
|
|
/** Stable source coordinates committed before preparing an owned thin image. */
|
|
export const hopperMachOImageSchema = z.strictObject({
|
|
method: z.literal("fat64_thin_slice"),
|
|
architecture_count: z.number().int().min(1).max(128),
|
|
container_table_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
|
source_offset: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
|
|
byte_length: z.number().int().positive().max(Number.MAX_SAFE_INTEGER),
|
|
cpu_type: z.number().int().nonnegative(),
|
|
cpu_subtype: z.number().int().nonnegative(),
|
|
});
|
|
|
|
export type HopperMachOImage = z.infer<typeof hopperMachOImageSchema>;
|
|
|
|
/** Validate the selected FAT64 slice without depending on Apple's lipo tool. */
|
|
export const resolveHopperMachOImage = async (
|
|
target: BinaryTarget & { readonly kind: "executable" },
|
|
signal?: AbortSignal,
|
|
): Promise<
|
|
Result<HopperMachOImage, BinaryTargetError | AnalysisCancelledError>
|
|
> => {
|
|
const invalid = (reason: string, cause?: unknown) =>
|
|
err(
|
|
new BinaryTargetError(
|
|
target.path,
|
|
reason,
|
|
cause === undefined ? undefined : { cause },
|
|
),
|
|
);
|
|
if (signal?.aborted) return err(new AnalysisCancelledError("open_binary"));
|
|
if (target.format !== "mach-o")
|
|
return invalid("Hopper FAT64 image preparation requires a Mach-O target");
|
|
let file: FileHandle;
|
|
try {
|
|
file = await open(target.path, "r");
|
|
} catch (cause: unknown) {
|
|
// Keep the OS error so a read-permission denial reports access_denied.
|
|
return invalid(
|
|
`Cannot read FAT64 source for Hopper image preparation: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
cause,
|
|
);
|
|
}
|
|
try {
|
|
const read = async (offset: number, length: number): Promise<Buffer> => {
|
|
const bytes = Buffer.alloc(length);
|
|
const { bytesRead } = await file.read(bytes, 0, length, offset);
|
|
return bytes.subarray(0, bytesRead);
|
|
};
|
|
const header = await read(0, 8);
|
|
const magic = header.length === 8 ? header.readUInt32BE(0) : 0;
|
|
if (magic !== 0xcafebabf && magic !== 0xbfbafeca)
|
|
return invalid(
|
|
"FAT64 source header changed before Hopper image preparation",
|
|
);
|
|
const littleEndian = magic === 0xbfbafeca;
|
|
const count = littleEndian
|
|
? header.readUInt32LE(4)
|
|
: header.readUInt32BE(4);
|
|
const size = (await file.stat()).size;
|
|
const tableEnd = 8 + count * 32;
|
|
if (count === 0 || count > 128 || tableEnd > size)
|
|
return invalid(
|
|
"FAT64 architecture table is truncated or exceeds the supported 128 entries",
|
|
);
|
|
const table = await read(8, count * 32);
|
|
const parsed = readFatSliceDeclarations(table, {
|
|
count,
|
|
wide: true,
|
|
littleEndian,
|
|
});
|
|
if (parsed.status === "malformed") return invalid(parsed.reason);
|
|
const cpuTypes = { x86: 7, x86_64: 0x1000007, arm: 12, arm64: 0x100000c };
|
|
const matches = [];
|
|
const ranges = [];
|
|
for (const slice of parsed.slices) {
|
|
if (signal?.aborted)
|
|
return err(new AnalysisCancelledError("open_binary"));
|
|
const rangeError = validateFatSlice(slice, size, tableEnd);
|
|
if (rangeError !== null) return invalid(rangeError);
|
|
const reserved = littleEndian
|
|
? table.readUInt32LE(slice.index * 32 + 28)
|
|
: table.readUInt32BE(slice.index * 32 + 28);
|
|
if (reserved !== 0)
|
|
return invalid(
|
|
`FAT64 architecture ${slice.index} has a nonzero reserved field`,
|
|
);
|
|
if (slice.size < 12)
|
|
return invalid(
|
|
`FAT64 architecture ${slice.index} has a truncated Mach-O header`,
|
|
);
|
|
const embedded = await read(slice.offset, 12);
|
|
if (embedded.length !== 12)
|
|
return invalid(`FAT64 architecture ${slice.index} became truncated`);
|
|
const thinMagic = embedded.readUInt32BE(0);
|
|
if (![0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(thinMagic))
|
|
return invalid(
|
|
`FAT64 architecture ${slice.index} does not contain a Mach-O image`,
|
|
);
|
|
const little = thinMagic === 0xcefaedfe || thinMagic === 0xcffaedfe;
|
|
const identity = {
|
|
cpuType: little ? embedded.readUInt32LE(4) : embedded.readUInt32BE(4),
|
|
cpuSubtype: little
|
|
? embedded.readUInt32LE(8)
|
|
: embedded.readUInt32BE(8),
|
|
};
|
|
const identityError = validateFatSlice(slice, size, tableEnd, identity);
|
|
if (identityError !== null) return invalid(identityError);
|
|
ranges.push(slice);
|
|
if (slice.cpuType === cpuTypes[target.architecture]) matches.push(slice);
|
|
}
|
|
ranges.sort((a, b) => a.offset - b.offset);
|
|
for (let index = 1; index < ranges.length; index++) {
|
|
const previous = ranges[index - 1];
|
|
const current = ranges[index];
|
|
if (
|
|
previous !== undefined &&
|
|
current !== undefined &&
|
|
current.offset < previous.offset + previous.size
|
|
)
|
|
return invalid("FAT64 slices overlap");
|
|
}
|
|
const selected = matches.length === 1 ? matches[0] : undefined;
|
|
if (selected === undefined)
|
|
return invalid(
|
|
`FAT64 selection for ${target.architecture} is ${matches.length === 0 ? "missing" : "ambiguous"}; extract the exact architecture into a thin image before opening it`,
|
|
);
|
|
return ok({
|
|
method: "fat64_thin_slice",
|
|
architecture_count: count,
|
|
container_table_sha256: createHash("sha256")
|
|
.update(header)
|
|
.update(table)
|
|
.digest("hex"),
|
|
source_offset: selected.offset,
|
|
byte_length: selected.size,
|
|
cpu_type: selected.cpuType,
|
|
cpu_subtype: selected.cpuSubtype,
|
|
});
|
|
} catch (cause: unknown) {
|
|
return invalid(
|
|
`Cannot validate FAT64 image: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
);
|
|
} finally {
|
|
await file.close();
|
|
}
|
|
};
|
|
|
|
/** Copy a selected slice and verify the entire original source in the same pass. */
|
|
export const prepareHopperMachOImage = async (
|
|
sourcePath: string,
|
|
sourceSha256: string,
|
|
image: HopperMachOImage,
|
|
destination: string,
|
|
signal?: AbortSignal,
|
|
): Promise<Result<string, HopperStartError | HopperCancelledError>> => {
|
|
if (signal?.aborted) return err(new HopperCancelledError());
|
|
try {
|
|
const source = await open(sourcePath, "r");
|
|
try {
|
|
const output = await open(destination, "wx", 0o600);
|
|
try {
|
|
const hash = createHash("sha256");
|
|
const tableHash = createHash("sha256");
|
|
const tableEnd = 8 + image.architecture_count * 32;
|
|
const thinHeader: Buffer[] = [];
|
|
let position = 0;
|
|
let copied = 0;
|
|
for await (const chunk of source.createReadStream({
|
|
autoClose: false,
|
|
})) {
|
|
if (signal?.aborted) return err(new HopperCancelledError());
|
|
const bytes = Buffer.from(chunk);
|
|
hash.update(bytes);
|
|
if (position < tableEnd)
|
|
tableHash.update(
|
|
bytes.subarray(0, Math.min(bytes.length, tableEnd - position)),
|
|
);
|
|
const start = Math.max(0, image.source_offset - position);
|
|
const end = Math.min(
|
|
bytes.length,
|
|
image.source_offset + image.byte_length - position,
|
|
);
|
|
if (end > start) {
|
|
if (copied < 12)
|
|
thinHeader.push(
|
|
bytes.subarray(start, Math.min(end, start + 12 - copied)),
|
|
);
|
|
await output.writeFile(bytes.subarray(start, end));
|
|
copied += end - start;
|
|
}
|
|
position += bytes.length;
|
|
}
|
|
const observed = hash.digest("hex");
|
|
const header = Buffer.concat(thinHeader);
|
|
const magic = header.length === 12 ? header.readUInt32BE(0) : 0;
|
|
const little = magic === 0xcefaedfe || magic === 0xcffaedfe;
|
|
const headerMatches =
|
|
header.length === 12 &&
|
|
[0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(magic) &&
|
|
(little ? header.readUInt32LE(4) : header.readUInt32BE(4)) ===
|
|
image.cpu_type &&
|
|
(little ? header.readUInt32LE(8) : header.readUInt32BE(8)) ===
|
|
image.cpu_subtype;
|
|
if (
|
|
tableHash.digest("hex") !== image.container_table_sha256 ||
|
|
!headerMatches
|
|
)
|
|
return err(
|
|
new HopperStartError({
|
|
userMessage: `FAT64 source ${sourcePath} no longer agrees with its committed architecture table or selected Mach-O header. Open the stable source again.`,
|
|
}),
|
|
);
|
|
if (observed !== sourceSha256 || copied !== image.byte_length)
|
|
return err(
|
|
new HopperStartError({
|
|
userMessage: `FAT64 source ${sourcePath} changed before launch: expected SHA-256 ${sourceSha256}, observed ${observed}; copied ${copied} of ${image.byte_length} selected bytes. Open the stable source again.`,
|
|
}),
|
|
);
|
|
return ok(destination);
|
|
} finally {
|
|
await output.close();
|
|
}
|
|
} finally {
|
|
await source.close();
|
|
}
|
|
} catch (cause: unknown) {
|
|
return signal?.aborted
|
|
? err(new HopperCancelledError())
|
|
: err(
|
|
new HopperStartError({
|
|
cause,
|
|
userMessage: `Cannot prepare the selected FAT64 image from ${sourcePath}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
}),
|
|
);
|
|
}
|
|
};
|