test(go): consolidate image metadata and extent checks (#12859)

* test(go): consolidate image metadata and extent checks

* test(go): assert structured extent classification
This commit is contained in:
morluto authored and GitHub committed 2026-11-20 15:15:23 +00:00
1 parent c6fe1e43e8
commit 96d79460db
1 file changed
+24 -45
+24 -45
View File
@@ -12,7 +12,6 @@ import { readMachoImage } from "../../../src/domain/apple/machoImage.js";
import { readBinaryContainer } from "../../../src/domain/native/readBinaryContainer.js";
import {
createGoBinaryFixture,
createGoBinaryByteStringFixture,
createGoPeHeaderPointerFixture,
createGoSplitElfFixture,
createGoOverlappingElfFixture,
@@ -160,7 +159,7 @@ it("does not skip malformed earlier metadata to report a later valid record", ()
);
});
it.each([
const nativeImageLayouts = [
["elf", 32, "little"],
["elf", 32, "big"],
["elf", 64, "little"],
@@ -171,7 +170,9 @@ it.each([
["macho", 32, "big"],
["macho", 64, "little"],
["macho", 64, "big"],
] as const)(
] as const;
it.each(nativeImageLayouts)(
"reads %s %d-bit %s inline build metadata",
(format, bits, byteOrder) => {
const fixture = createGoBinaryFixture({ format, bits, byteOrder });
@@ -269,36 +270,34 @@ it("does not treat a code/overlay decoy or unaligned marker as Go metadata", ()
expect(readGoBinaryImage(unaligned.bytes).build_info).toBeNull();
});
it("refuses mapped pointer lengths outside the file instead of rounding 64-bit addresses", () => {
it("refuses an unmapped 64-bit pointer string address", () => {
const fixture = createGoBinaryFixture({ encoding: "pointer" });
fixture.bytes.writeBigUInt64LE(0x20000000000001n, 1536);
expect(() => readGoBinaryImage(fixture.bytes)).toThrowError(
/mapped|address/i,
expect(() => readGoBinaryImage(fixture.bytes)).toThrow(
expect.objectContaining({
kind: "malformed",
reason: "unmapped",
}),
);
const length = createGoBinaryFixture({ encoding: "pointer" });
length.bytes.writeBigUInt64LE(0xffffffffffffffffn, 1544);
expect(() => readGoBinaryImage(length.bytes)).toThrowError(/limit|length/i);
});
it.each(["inline", "pointer"] as const)(
"classifies impossible %s string extents before the decoding allowance",
(encoding) => {
for (const length of [1048577n, 0xffffffffffffffffn]) {
const fixture = createGoBinaryFixture({ encoding });
if (encoding === "pointer") fixture.bytes.writeBigUInt64LE(length, 1544);
else {
let value = length;
let at = fixture.headerOffset + 32;
do {
const byte = Number(value & 127n);
value >>= 7n;
fixture.bytes[at++] = byte | (value === 0n ? 0 : 128);
} while (value !== 0n);
}
expect(() => readGoBinaryImage(fixture.bytes)).toThrowError(
expect.objectContaining({ kind: "malformed" }),
);
const fixture = createGoBinaryFixture({ encoding });
if (encoding === "pointer") {
fixture.bytes.writeBigUInt64LE(0xffffffffffffffffn, 1544);
} else {
// Maximum ULEB64: valid length encoding, impossible physical extent.
const at = fixture.headerOffset + 32;
fixture.bytes.fill(0xff, at, at + 9);
fixture.bytes[at + 9] = 1;
}
expect(() =>
readGoBinaryImage(fixture.bytes, {
budgets: { max_build_info_bytes: 1 },
}),
).toThrow(expect.objectContaining({ kind: "malformed" }));
},
);
@@ -409,13 +408,6 @@ it("preserves non-UTF8 compiler and module bytes without rejecting other observe
});
});
it("retains parsed neighboring module records and exact invalid UTF-8 lines at the image boundary", () => {
const fixture = createGoBinaryByteStringFixture();
expect(readGoBinaryImage(fixture.bytes).build_info).toEqual(
fixture.expectedBuildInfo,
);
});
it.each([0, 4, 16])("ignores unused inline pointer width %s", (width) => {
const fixture = createGoBinaryFixture();
fixture.bytes[fixture.headerOffset + 14] = width;
@@ -600,20 +592,7 @@ const extendedElfFixture = (): Buffer => {
return bytes;
};
const legacyLayouts = [
["elf", 32, "little"],
["elf", 32, "big"],
["elf", 64, "little"],
["elf", 64, "big"],
["pe", 32, "little"],
["pe", 64, "little"],
["macho", 32, "little"],
["macho", 32, "big"],
["macho", 64, "little"],
["macho", 64, "big"],
] as const;
it.each(legacyLayouts)(
it.each(nativeImageLayouts)(
"reads moduleless legacy %s %d-bit %s metadata with a zero-filled module header",
(format, bits, byteOrder) => {
const fixture = zeroFilledModuleFixture(format, bits, byteOrder);