mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 23:09:34 +00:00
* feat(native): identify Nim, Zig, GHC, OCaml and D toolchains Add identify_native_toolchain, a static, read-only reader that reports whether a local ELF, PE or thin or universal Mach-O image was built by Nim, Zig, Haskell (GHC), OCaml or D, including stripped images without a DWARF producer. The reader searches a bounded part of the selected image for a fixed table of runtime symbol names, runtime messages, mangled names, source paths and producer strings, and returns each observed marker with its file offset, section or segment, matched text and occurrence count. Data sections are searched before bytes outside sections and before instruction sections, so a bounded scan reaches strings first; coverage reports the bytes examined. The verdict is derived from the listed markers only: a language needs two distinct markers, one strong. Conflicting, too few or no markers give unknown, and a version is reported only when a marker states it. * perf(native): order toolchain scan ranges in linear time Intersect instruction sections with the bytes outside data sections in one pass, so an image with tens of thousands of sections does not take quadratic time before the scan starts. * fix(native): bound stated toolchain versions and describe scan order Track distinct versions per marker in a Set and list at most eight, counting the rest in versions_omitted, so an image with many version strings costs constant time per match and a bounded result. A candidate reports a version only when exactly one was stated. Describe max_scan_bytes and examined_bytes as a total in scan order rather than a prefix from the image start, and place the chunk-boundary test's marker across an actual read boundary. * fix(native): keep every stated toolchain version in the report * fix(native): bound stated toolchain version bytes before keeping them * feat(native): make the stated version budget a per-call control * fix(native): give no larger-limit retry once a toolchain control is at its maximum * fix(native): do not match toolchain markers on truncated runs * fix(native): keep exact markers on a final run at the image end * style(native): format the toolchain changes --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
164 lines
4.9 KiB
TypeScript
164 lines
4.9 KiB
TypeScript
import {
|
|
fatImage,
|
|
machoImage,
|
|
} from "../../../../src/artifacts/apple/MachoImage.fixture.js";
|
|
import { buildManagedPeFixture } from "../../../../src/dotnet/ManagedPe.fixture.js";
|
|
import { peCertificateFixture } from "../../../../src/native/pe/PeCertificates.fixture.js";
|
|
import { appleIpsBytes } from "../../../fixtures/binaryDiagnostics/appleIps.js";
|
|
import { targetFailureReaders, type TargetFailureCase } from "../registry.js";
|
|
|
|
/** An ELF magic with nothing after it: every ELF-reading owner refuses it. */
|
|
const truncatedElfHeader: TargetFailureCase = {
|
|
name: "elf-magic-only",
|
|
kind: "malformed",
|
|
bytes: () => Buffer.from("\x7fELF"),
|
|
identity: true,
|
|
};
|
|
|
|
/** A managed PE whose first section's raw data starts past the end of the file. */
|
|
const managedSectionPastEof = (): Buffer => {
|
|
const bytes = buildManagedPeFixture();
|
|
const pe = bytes.readUInt32LE(0x3c);
|
|
const sections = pe + 24 + bytes.readUInt16LE(pe + 20);
|
|
bytes.writeUInt32LE(bytes.length + 0x1000, sections + 20);
|
|
return bytes;
|
|
};
|
|
|
|
/** A universal Mach-O whose first declared slice runs into the second. */
|
|
const overlappingUniversalMacho = (): Buffer => {
|
|
const slice = (type: number, subtype: number) => ({
|
|
cpu: { type, subtype },
|
|
bytes: machoImage({ cpu: { type, subtype }, wide: true }),
|
|
});
|
|
const bytes = Buffer.from(
|
|
fatImage([slice(0x01000007, 3), slice(0x0100000c, 0)]),
|
|
);
|
|
const first = bytes.readUInt32BE(8 + 8);
|
|
const second = bytes.readUInt32BE(8 + 20 + 8);
|
|
bytes.writeUInt32BE(second - first + 16, 8 + 12);
|
|
return bytes;
|
|
};
|
|
|
|
/** A universal Mach-O cut off inside its FAT table: 20 of its 48 table bytes. */
|
|
const truncatedUniversalMacho = (): Buffer => {
|
|
const slice = (type: number, subtype: number) => ({
|
|
cpu: { type, subtype },
|
|
bytes: machoImage({ cpu: { type, subtype }, wide: true }),
|
|
});
|
|
return Buffer.from(
|
|
fatImage([slice(0x01000007, 3), slice(0x0100000c, 0)]),
|
|
).subarray(0, 20);
|
|
};
|
|
|
|
/** A PE image whose first WIN_CERTIFICATE declares less than its own header. */
|
|
const shortCertificateHeader = (): Buffer => {
|
|
const bytes = peCertificateFixture();
|
|
bytes.writeUInt32LE(4, bytes.readUInt32LE(0x98 + 96 + 4 * 8));
|
|
return bytes;
|
|
};
|
|
|
|
const truncatedSlices: TargetFailureCase = {
|
|
name: "macho-fat-table-cut-off",
|
|
kind: "truncated",
|
|
bytes: truncatedUniversalMacho,
|
|
facts: { format: "macho", available_bytes: 20, required_bytes: 48 },
|
|
cli: ["--architecture", "arm64"],
|
|
mcp: { architecture: "arm64" },
|
|
identity: true,
|
|
};
|
|
|
|
const managedCase: TargetFailureCase = {
|
|
name: "pe-cli-section-past-eof",
|
|
kind: "malformed",
|
|
bytes: managedSectionPastEof,
|
|
facts: { format: "pe-cli", field: "pe.section-data" },
|
|
identity: false,
|
|
};
|
|
|
|
const overlappingSlices: TargetFailureCase = {
|
|
name: "macho-overlapping-slices",
|
|
kind: "malformed",
|
|
bytes: overlappingUniversalMacho,
|
|
facts: { format: "macho", reason: "Mach-O slices 0 and 1 overlap." },
|
|
cli: ["--architecture", "arm64"],
|
|
mcp: { architecture: "arm64" },
|
|
identity: true,
|
|
};
|
|
|
|
/**
|
|
* Selected-file readers whose format owner raises its parse failure once.
|
|
* The selected file's bytes are at fault, not the request.
|
|
*/
|
|
export default targetFailureReaders([
|
|
...[
|
|
"inspect_python_pyc",
|
|
"inspect_elf_notes",
|
|
"inspect_elf_dependencies",
|
|
"inspect_elf_debug_link",
|
|
"inspect_elf_unwind_ranges",
|
|
"inspect_devicetree",
|
|
"inspect_pe_imports",
|
|
"inspect_kernel_module",
|
|
"inspect_pe_load_config",
|
|
"inspect_pe_resources",
|
|
"inspect_native_exports",
|
|
"inspect_binary_hardening",
|
|
].map((operation) => ({ operation, cases: [truncatedElfHeader] })),
|
|
...[
|
|
"inspect_go_binary",
|
|
"inspect_rust_binary",
|
|
"inspect_native_initializers",
|
|
].map((operation) => ({
|
|
operation,
|
|
cases: [truncatedElfHeader, overlappingSlices, truncatedSlices],
|
|
})),
|
|
...["inspect_dwarf_units", "identify_native_toolchain"].map((operation) => ({
|
|
operation,
|
|
cases: [overlappingSlices, truncatedSlices],
|
|
})),
|
|
{
|
|
operation: "inspect_apple_ips",
|
|
cases: [
|
|
truncatedElfHeader,
|
|
{
|
|
// The JSON body ends inside its declared report object.
|
|
name: "ips-truncated-body",
|
|
kind: "malformed",
|
|
bytes: () => appleIpsBytes({ truncateBody: true }),
|
|
identity: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
operation: "inspect_erlang_beam",
|
|
cases: [
|
|
truncatedElfHeader,
|
|
{
|
|
name: "beam-form-header-only",
|
|
kind: "truncated",
|
|
bytes: () => Buffer.from("FOR1"),
|
|
facts: { format: "beam" },
|
|
identity: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
operation: "inspect_pe_certificates",
|
|
cases: [
|
|
truncatedElfHeader,
|
|
{
|
|
name: "short-certificate-header",
|
|
kind: "malformed",
|
|
bytes: shortCertificateHeader,
|
|
facts: { format: "pe-certificate-table" },
|
|
identity: true,
|
|
},
|
|
],
|
|
},
|
|
...[
|
|
"inspect_managed_artifact",
|
|
"inspect_managed_members",
|
|
"inspect_managed_native_boundaries",
|
|
].map((operation) => ({ operation, cases: [managedCase] })),
|
|
]);
|