mirror of
https://github.com/morluto/rea
synced 2026-10-26 06:16:17 -04:00
34a4bb6508
Validate ELF identification/header versions and declared header bounds at the shared owner. Reuse parsed facts in target selection, Go, Dart and MIPS readers while preserving their stricter format policies and x32 support. Repair synthetic ELF fixtures and remove a superseded Go error-text assertion. Validation: 74 source tests, 202 filesystem/Go/MIPS boundary tests, typecheck and lint. No new real-provider claims.
84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { parseExecutableHeader } from "./binaryTarget.js";
|
|
|
|
const header = (little: boolean, bits: 32 | 64 = 32): Buffer => {
|
|
const bytes = Buffer.alloc(bits === 32 ? 52 : 64);
|
|
bytes.set([0x7f, 0x45, 0x4c, 0x46, bits === 32 ? 1 : 2, little ? 1 : 2, 1]);
|
|
if (little) {
|
|
bytes.writeUInt16LE(2, 16);
|
|
bytes.writeUInt16LE(8, 18);
|
|
bytes.writeUInt32LE(1, 20);
|
|
bytes.writeUInt16LE(bits === 32 ? 52 : 64, bits === 32 ? 40 : 52);
|
|
bytes.writeUInt32LE(0x70001001, bits === 32 ? 36 : 48);
|
|
} else {
|
|
bytes.writeUInt16BE(2, 16);
|
|
bytes.writeUInt16BE(8, 18);
|
|
bytes.writeUInt32BE(1, 20);
|
|
bytes.writeUInt16BE(bits === 32 ? 52 : 64, bits === 32 ? 40 : 52);
|
|
bytes.writeUInt32BE(0x70001001, bits === 32 ? 36 : 48);
|
|
}
|
|
return bytes;
|
|
};
|
|
|
|
describe("MIPS ELF header boundaries", () => {
|
|
it.each([true, false])(
|
|
"retains source endian and ABI/ISA flags (little=%s)",
|
|
(little) => {
|
|
const parsed = parseExecutableHeader(header(little), "x64");
|
|
if (!parsed.ok) throw new Error(parsed.error);
|
|
expect(parsed.value).toEqual({
|
|
format: "elf",
|
|
architecture: "mips",
|
|
availableArchitectures: ["mips"],
|
|
mips: {
|
|
elfClass: 32,
|
|
byteOrder: little ? "little" : "big",
|
|
type: 2,
|
|
flags: 0x70001001,
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it("reads ELF64 flags at their own offset without claiming o32 support", () => {
|
|
const bytes = header(false, 64);
|
|
bytes.writeUInt32BE(0xfeedface, 36);
|
|
bytes.writeUInt32BE(0x80000001, 48);
|
|
const parsed = parseExecutableHeader(bytes, "x64");
|
|
if (!parsed.ok) throw new Error(parsed.error);
|
|
expect(parsed.value).toMatchObject({
|
|
architecture: "mips",
|
|
mips: { elfClass: 64, byteOrder: "big", flags: 0x80000001 },
|
|
});
|
|
});
|
|
|
|
it("retains a PSP declaration without claiming Allegrex provider support", () => {
|
|
const bytes = header(true);
|
|
bytes.writeUInt32LE(0x10a23001, 36);
|
|
const parsed = parseExecutableHeader(bytes, "x64");
|
|
if (!parsed.ok) throw new Error(parsed.error);
|
|
expect(parsed.value).toMatchObject({
|
|
architecture: "mips",
|
|
mips: { flags: 0x10a23001 },
|
|
});
|
|
});
|
|
|
|
it("rejects a truncated header before reading flags", () => {
|
|
const parsed = parseExecutableHeader(header(true).subarray(0, 39), "x64");
|
|
expect(parsed).toEqual({
|
|
ok: false,
|
|
error:
|
|
"truncated ELF header: an ELF32 header needs 52 bytes; the file has 39",
|
|
});
|
|
});
|
|
|
|
it("does not turn invalid byte order into little-endian MIPS", () => {
|
|
const bytes = header(true);
|
|
bytes[5] = 0;
|
|
expect(parseExecutableHeader(bytes, "x64")).toEqual({
|
|
ok: false,
|
|
error: "unsupported ELF endianness",
|
|
});
|
|
});
|
|
});
|