Files
reverse-engineering-anything/src/erlang/BeamParser.test.ts
T
morluto c9caa0aa5b test(formats): consolidate BEAM and DOL boundary checks (#12832)
* test(erlang): consolidate BEAM path and atom budget checks

* test(dol): prove session contracts through the MCP client
2026-11-20 13:44:28 +00:00

466 lines
15 KiB
TypeScript

import { createHash } from "node:crypto";
import { expect, it, vi } from "vitest";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import {
MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS,
inspectErlangBeamInputSchema,
} from "../domain/erlang/beam.js";
import { ArtifactReaderFailure } from "../resources/ArtifactReader.js";
import {
erlangBeamChunksFixture,
erlangBeamRowsFixture,
erlangBeamFixture,
erlangBeamLegacyAtomsFixture,
erlangBeamOtp29Fixture,
} from "./Beam.fixture.js";
import { parseErlangBeam } from "./BeamParser.js";
import { BeamProvider } from "./BeamProvider.js";
import { ArtifactResourceScope } from "../resources/ArtifactResourceScope.js";
const defaults = inspectErlangBeamInputSchema.parse({
path: "/artifacts/app.beam",
});
const parse = (bytes: Buffer, input = defaults) =>
parseErlangBeam(
bytes,
{
path: input.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
},
input,
);
it("selects the last table and Code payload for every repeated chunk name", () => {
const original = erlangBeamFixture("selected");
const decoy = erlangBeamFixture("decoy");
const changedFields = new Map([
["Code", 16],
["ExpT", 8],
["ImpT", 12],
["FunT", 8],
]);
for (let at = 12; at < decoy.length;) {
const name = decoy.toString("latin1", at, at + 4);
const size = decoy.readUInt32BE(at + 4);
const field = changedFields.get(name);
if (field !== undefined) decoy.writeUInt32BE(9, at + 8 + field);
at += 8 + size + ((4 - (size % 4)) % 4);
}
const repeated = Buffer.concat([
original.subarray(0, 12),
decoy.subarray(12),
original.subarray(12),
]);
repeated.writeUInt32BE(repeated.length - 8, 4);
const report = parse(repeated);
const control = parse(original);
expect(control.module).toBe("selected");
expect(control.atoms?.map(({ name }) => name)).toEqual([
"selected",
"module_info",
"start",
"erlang",
"get_module_info",
]);
expect(control.exports).toHaveLength(3);
expect(control.imports).toHaveLength(1);
expect(control.lambdas).toHaveLength(1);
expect(control.code).toMatchObject({
opcode_max: 169,
labels: 4,
functions: 2,
});
expect(report.atoms).toEqual(control.atoms);
expect(report.exports).toEqual(control.exports);
expect(report.imports).toEqual(control.imports);
expect(report.lambdas).toEqual(control.lambdas);
expect(report.code).toEqual({
...control.code,
instructions: {
...control.code!.instructions,
offset: control.code!.instructions.offset + decoy.length - 12,
},
});
expect(report.chunks).toHaveLength(control.chunks.length * 2);
});
it("rejects a malformed selected atom table instead of using an earlier table", () => {
const original = erlangBeamFixture();
const invalid = erlangBeamChunksFixture([["AtU8", Buffer.from([0])]]);
const repeated = Buffer.concat([original, invalid.subarray(12)]);
repeated.writeUInt32BE(repeated.length - 8, 4);
expect(() => parse(repeated)).toThrow(/truncated/);
});
it("falls back from an empty AtU8 to legacy Atom, while nonempty alternates keep precedence", () => {
const control = parse(erlangBeamLegacyAtomsFixture());
const empty = parse(erlangBeamLegacyAtomsFixture(Buffer.alloc(0)));
expect(empty.atoms).toEqual(control.atoms);
expect(empty.atom_encoding).toBe("latin1");
expect(empty.module).toBe("demo");
expect(empty.exports?.[0]?.function).toBe("ÿ");
expect(empty.chunks.map(({ name }) => name)).toEqual([
"Atom",
"AtU8",
"ExpT",
]);
expect(empty.chunks[1]?.payload.bytes).toBe(0);
const alternate = parse(
erlangBeamLegacyAtomsFixture(
Buffer.concat([
Buffer.from([0, 0, 0, 2, 4]),
Buffer.from("demo"),
Buffer.from([3]),
Buffer.from("new"),
]),
),
);
expect(alternate.atom_encoding).toBe("utf8");
expect(alternate.exports?.[0]?.function).toBe("new");
const zeroCount = parse(erlangBeamLegacyAtomsFixture(Buffer.alloc(4)));
expect(zeroCount.atom_encoding).toBe("utf8");
expect(zeroCount.atoms).toEqual([]);
expect(zeroCount.exports?.[0]?.function).toBeNull();
});
it.each([Buffer.from([0]), Buffer.from([0, 0, 0, 1])])(
"rejects a malformed nonempty AtU8 without falling back: %j",
(payload) => {
expect(() => parse(erlangBeamLegacyAtomsFixture(payload))).toThrow(
/truncated/,
);
},
);
it.each(["AtU8", "Atom"])(
"does not turn an empty standalone %s atom table into decoded facts",
(name) => {
expect(() =>
parse(erlangBeamChunksFixture([[name, Buffer.alloc(0)]])),
).toThrow(/atom table is truncated/);
},
);
it("uses the last AtU8 occurrence even when empty", () => {
const control = erlangBeamLegacyAtomsFixture(Buffer.from([0, 0, 0, 0]));
const empty = erlangBeamChunksFixture([["AtU8", Buffer.alloc(0)]]);
const bytes = Buffer.concat([control, empty.subarray(12)]);
bytes.writeUInt32BE(bytes.length - 8, 4);
const report = parse(bytes);
expect(report.atom_encoding).toBe("latin1");
expect(report.exports?.[0]?.function).toBe("ÿ");
expect(
report.chunks
.filter(({ name }) => name === "AtU8")
.map(({ payload }) => payload.bytes),
).toEqual([4, 0]);
});
it.each([
{ payload: Buffer.alloc(0), expected: null },
{ payload: Buffer.alloc(4), expected: [] },
])(
"distinguishes empty optional FunT from a zero row count: $payload",
({ payload, expected }) => {
const original = erlangBeamOtp29Fixture();
const extension = erlangBeamChunksFixture([["FunT", payload]]);
const bytes = Buffer.concat([original, extension.subarray(12)]);
bytes.writeUInt32BE(bytes.length - 8, 4);
const control = parse(original);
const report = parse(bytes);
expect(control.lambdas).toBeNull();
expect(report.lambdas).toEqual(expected);
expect(report.atoms).toEqual(control.atoms);
expect(report.exports).toEqual(control.exports);
expect(report.imports).toEqual(control.imports);
expect(report.code).toEqual(control.code);
expect(report.chunks.at(-1)?.payload.bytes).toBe(payload.length);
},
);
it("does not reuse an earlier FunT when its last occurrence is empty", () => {
const original = erlangBeamFixture();
const extension = erlangBeamChunksFixture([["FunT", Buffer.alloc(0)]]);
const bytes = Buffer.concat([original, extension.subarray(12)]);
bytes.writeUInt32BE(bytes.length - 8, 4);
const report = parse(bytes);
expect(report.lambdas).toBeNull();
expect(
report.chunks
.filter(({ name }) => name === "FunT")
.map(({ payload }) => payload.bytes),
).toEqual([28, 0]);
});
it.each([Buffer.from([0]), Buffer.from([0, 0, 0, 1])])(
"rejects truncated nonempty FunT payloads: %j",
(payload) => {
expect(() => parse(erlangBeamChunksFixture([["FunT", payload]]))).toThrow(
/lambda table is truncated/,
);
},
);
it("accepts extended compact atom lengths and rejects truncated or overflowing values", () => {
const compactAtom = (length: Buffer, name: Buffer): Buffer =>
erlangBeamChunksFixture([
[
"AtU8",
Buffer.concat([Buffer.from([0xff, 0xff, 0xff, 0xff]), length, name]),
],
]);
const name = Buffer.from("0123456789abcdef", "ascii");
const extended = parse(compactAtom(Buffer.from([0x18, 0x00, 0x10]), name));
expect(extended.module).toBe(name.toString("ascii"));
expect(() => parse(compactAtom(Buffer.from([0x18, 0x00]), name))).toThrow(
/BEAM atom table is truncated/,
);
expect(() => parse(compactAtom(Buffer.from([0x19]), name))).toThrow(
/BEAM atom length is not a u tag/,
);
expect(() =>
parse(
compactAtom(
Buffer.from([0xd8, 0x80, 0, 0, 0, 0, 0, 0, 0]),
Buffer.alloc(0),
),
),
).toThrow(/BEAM atom length encoding overflows/);
expect(() =>
parse(
compactAtom(
Buffer.from([0xf8, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
Buffer.alloc(0),
),
),
).toThrow(/BEAM atom length encoding overflows/);
});
it("fails a truncated FOR1 container without partial success", () => {
expect(() => parse(Buffer.from("FOR1"))).toThrow(ArtifactReaderFailure);
});
it("fails a FOR1 form that is not BEAM", () => {
const bytes = Buffer.alloc(16);
bytes.write("FOR1");
bytes.writeUInt32BE(8, 4);
bytes.write("WAVE", 8);
expect(() => parse(bytes)).toThrow(ArtifactReaderFailure);
});
it("discloses bytes that trail the declared FOR1 form", () => {
const valid = erlangBeamFixture("trailing_probe");
const clean = parse(valid);
expect(clean.coverage.status).toBe("complete");
expect(clean.coverage.trailing_bytes).toBe(0);
// Append foreign bytes past the declared form; a conformant file ends there.
const trailing = Buffer.concat([valid, Buffer.from("APPENDEDOVERLAY!")]);
const report = parse(trailing);
expect(report.form.location.bytes).toBe(valid.length);
expect(report.coverage.status).toBe("partial");
expect(report.coverage.trailing_bytes).toBe(16);
// The surplus bytes are not misread as a chunk; examined facts are unchanged.
expect(report.chunks).toEqual(clean.chunks);
expect(report.coverage.examined_chunks).toBe(clean.coverage.examined_chunks);
expect(report.limitations).toEqual([
...clean.limitations,
`The declared FOR1 form ends at offset ${String(valid.length)}; 16 trailing bytes were not examined.`,
]);
const single = parse(Buffer.concat([valid, Buffer.from("!")]));
expect(single.coverage.status).toBe("partial");
expect(single.coverage.trailing_bytes).toBe(1);
expect(single.limitations.at(-1)).toBe(
`The declared FOR1 form ends at offset ${String(valid.length)}; 1 trailing byte was not examined.`,
);
});
it("rejects a short remainder inside the BEAM form", () => {
const bytes = Buffer.alloc(16);
bytes.write("FOR1");
bytes.writeUInt32BE(8, 4);
bytes.write("BEAM", 8);
expect(() => parse(bytes)).toThrow(/BEAM chunk header is truncated/);
});
it("rejects an AtU8 atom name that is not UTF-8", () => {
expect(() =>
parse(
erlangBeamChunksFixture([["AtU8", Buffer.from([0, 0, 0, 1, 1, 0xff])]]),
),
).toThrow(/BEAM atom name is not valid UTF-8/);
});
it("uses a one-byte length and preserves Latin-1 bytes in legacy Atom", () => {
const report = parse(
erlangBeamChunksFixture([
["Atom", Buffer.from([0, 0, 0, 1, 2, 0x61, 0xff])],
]),
);
expect(report).toMatchObject({
atom_encoding: "latin1",
module: "aÿ",
atoms: [{ index: 1, name: "aÿ" }],
coverage: { status: "complete" },
});
});
it.each([
["a negative count", [0xff, 0xff, 0xff, 0xff, 0x20, 0x61, 0x62]],
["a compact length", [0, 0, 0, 1, 0x20, 0x61, 0x62]],
])("rejects %s in legacy Atom", (_name, payload) => {
expect(() =>
parse(erlangBeamChunksFixture([["Atom", Buffer.from(payload)]])),
).toThrow(expect.objectContaining({ reason: "format" }));
});
it("rejects combined table rows before allocating them", () => {
const below = Math.floor(MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS / 2);
const overflow = MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS - below * 2 + 1;
expect(() =>
parse(
erlangBeamRowsFixture([
["ExpT", below],
["ImpT", below],
["FunT", overflow],
]),
),
).toThrow(
expect.objectContaining({
failure: expect.objectContaining({
kind: "limit",
limit: {
name: "max_table_rows",
maximum: MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS,
required: MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS + 1,
},
}),
}),
);
const complete = Buffer.alloc(
4 + (MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS + 1) * 12,
);
complete.writeUInt32BE(MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS + 1);
expect(() =>
parse(
erlangBeamChunksFixture([
["ExpT", complete],
["ImpT", Buffer.alloc(1)],
]),
),
).toThrow(/import table is truncated/);
const count = Buffer.alloc(4);
count.writeUInt32BE(MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS + 1);
expect(() => parse(erlangBeamChunksFixture([["ExpT", count]]))).toThrow(
/truncated/,
);
count.writeUInt32BE(MAX_INSPECT_ERLANG_BEAM_TABLE_ROWS);
expect(() => parse(erlangBeamChunksFixture([["ExpT", count]]))).toThrow(
/truncated/,
);
});
it.each([
["legacy Atom", "Atom", [0, 0, 0, 2], [1, 2]],
["classic AtU8", "AtU8", [0, 0, 0, 2], [1, 2]],
["compact AtU8", "AtU8", [0xff, 0xff, 0xff, 0xfe], [0x10, 0x20]],
] as const)(
"checks only the admitted atom lengths in %s",
(_name, chunk, count, [validLength, invalidLength]) => {
const fixture = (firstLength: number, lastByte: number) =>
erlangBeamChunksFixture([
[chunk, Buffer.from([...count, firstLength, 0x61, lastByte])],
]);
const input = { ...defaults, max_atoms: 1 };
const valid = fixture(validLength, 0);
expect(parse(valid).atoms).toEqual([
{ index: 1, name: "a" },
{ index: 2, name: "" },
]);
for (const bytes of [valid, fixture(validLength, validLength)]) {
// The second name is complete or truncated, beyond the admitted prefix.
expect(() => parse(bytes, input)).toThrow(
expect.objectContaining({
failure: expect.objectContaining({
kind: "limit",
format: "beam",
limit: { name: "max_atoms", maximum: 1, required: 2 },
}),
}),
);
}
// The first name would consume the second entry's required length byte.
expect(() => parse(fixture(invalidLength, 0x62), input)).toThrow(
expect.objectContaining({ reason: "format" }),
);
},
);
it("retains the exhausted chunk allowance", () => {
expect(() =>
parse(erlangBeamFixture(), { ...defaults, max_chunks: 1 }),
).toThrow(
expect.objectContaining({
failure: expect.objectContaining({
kind: "limit",
format: "beam",
limit: { name: "max_chunks", maximum: 1 },
}),
}),
);
});
it("does not infer a BEAM budget from diagnostic prose", async () => {
const provider = new BeamProvider();
const injection = vi
.spyOn(ArtifactResourceScope.prototype, "run")
.mockRejectedValueOnce(
new ArtifactReaderFailure(
"limit",
"atom row budget wording from another boundary",
{ boundary: "capacity" },
),
);
try {
const result = await provider.inspect(defaults);
if (result.ok) throw new Error("Expected unknown capacity failure");
expect(result.error).toBeInstanceOf(AnalysisResourceConstraintError);
if (!(result.error instanceof AnalysisResourceConstraintError))
throw new Error("Missing resource error");
expect(result.error.retryable).toBeUndefined();
expect(result.error.remediationAction).toBeUndefined();
} finally {
injection.mockRestore();
await provider.close();
}
});
it.each([
["Atom", Buffer.from([0, 0, 0, 1, 1, 0x61])],
["AtU8", Buffer.from([0, 0, 0, 1, 1, 0x61])],
["AtU8", Buffer.from([0xff, 0xff, 0xff, 0xff, 0x10, 0x61])],
["ExpT", Buffer.alloc(4)],
["ImpT", Buffer.alloc(4)],
["FunT", Buffer.alloc(4)],
] as const)(
"reports unexamined bytes inside a selected %s table",
(name, payload) => {
const control = parse(erlangBeamChunksFixture([[name, payload]]));
const extended = Buffer.concat([payload, Buffer.from([0x61])]);
const report = parse(erlangBeamChunksFixture([[name, extended]]));
expect(control.coverage.status).toBe("complete");
expect(report.coverage).toMatchObject({
status: "partial",
trailing_bytes: 0,
});
expect(report.limitations).toContain(
`The selected ${name} table has 1 trailing byte that was not examined.`,
);
for (const field of ["atoms", "exports", "imports", "lambdas"] as const)
expect(report[field]).toEqual(control[field]);
},
);