Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(erlang): accept extended BEAM atom lengths
morluto933aa480d30f
diff --git a/src/erlang/BeamParser.test.ts b/src/erlang/BeamParser.test.tsindex fb3e2ee32..3a29fcc64 100644--- a/src/erlang/BeamParser.test.ts+++ b/src/erlang/BeamParser.test.ts@@ -114,6 +114,39 @@ it("reads a compiler-produced OTP 29 BEAM with the compact atom table", () => {});});+it("accepts extended compact atom lengths and rejects truncated or overflowing values", () => {+ const compactAtom = (length: Buffer, name: Buffer): Buffer =>+ singleChunkBeam(+ "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);});diff --git a/src/erlang/BeamParser.ts b/src/erlang/BeamParser.tsindex 694b2c6d0..e255f21fb 100644--- a/src/erlang/BeamParser.ts+++ b/src/erlang/BeamParser.ts@@ -71,11 +71,77 @@ const atomLength = (if (at >= payload.length) fail("BEAM atom table is truncated.");const first = payload[at]!;if (!compact) return [first, at + 1];- if ((first & 0x07) !== 0) fail("BEAM atom length is not a u tag.");- if ((first & 0x08) === 0) return [first >> 4, at + 1];- if ((first & 0x10) !== 0) fail("BEAM atom length encoding is unsupported.");- if (at + 1 >= payload.length) fail("BEAM atom table is truncated.");- return [((first & 0xe0) << 3) | payload[at + 1]!, at + 2];+ const decoded = compactUnsigned(payload, at);+ if (decoded.value > BigInt(payload.length - decoded.next))+ fail("BEAM atom table is truncated.");+ return [Number(decoded.value), decoded.next];+};++/** Decode OTP's compact unsigned `u` tag without allocating from its value. */+const compactUnsigned = (+ payload: Buffer,+ at: number,+): { value: bigint; next: number } => {+ let cursor = at;+ let longByteCounts = 0;+ let value: bigint;+ for (;;) {+ if (cursor >= payload.length) fail("BEAM atom table is truncated.");+ const first = payload[cursor]!;+ if ((first & 0x07) !== 0) fail("BEAM atom length is not a u tag.");+ if ((first & 0x08) === 0) {+ value = BigInt(first >> 4);+ cursor += 1;+ break;+ }+ if ((first & 0x10) === 0) {+ if (cursor + 1 >= payload.length) fail("BEAM atom table is truncated.");+ value = BigInt(((first >> 5) << 8) | payload[cursor + 1]!);+ cursor += 2;+ break;+ }+ const byteCountCode = first >> 5;+ cursor += 1;+ if (byteCountCode === 7) {+ // OTP encodes lengths of nine or more bytes as a compact `u` byte+ // count followed by that many bytes. Keep this iterative so nested+ // size prefixes cannot exhaust the JavaScript call stack.+ longByteCounts += 1;+ continue;+ }+ value = readCompactUnsignedBytes(payload, cursor, byteCountCode + 2);+ cursor += byteCountCode + 2;+ break;+ }+ while (longByteCounts > 0) {+ if (value >= 2_147_483_638n) fail("BEAM atom length encoding overflows.");+ const byteCount = Number(value) + 9;+ value = readCompactUnsignedBytes(payload, cursor, byteCount);+ cursor += byteCount;+ longByteCounts -= 1;+ }+ return { value, next: cursor };+};++const readCompactUnsignedBytes = (+ payload: Buffer,+ at: number,+ byteCount: number,+): bigint => {+ if (byteCount > payload.length - at) fail("BEAM atom table is truncated.");+ // OTP's unpack_varint only accepts values that fit a signed machine word.+ // REA's supported Node runtimes are 64-bit, so longer inputs overflow even+ // when their leading bytes are zero.+ if (byteCount > 8) fail("BEAM atom length encoding overflows.");+ const maxValue = 9_223_372_036_854_775_807n;+ let value = 0n;+ for (let index = 0; index < byteCount; index += 1) {+ const byte = BigInt(payload[at + index]!);+ if (value > (maxValue - byte) >> 8n)+ fail("BEAM atom length encoding overflows.");+ value = (value << 8n) | byte;+ }+ return value;};const parseAtoms = (diff --git a/tests/acceptance/erlangBeamCli.test.ts b/tests/acceptance/erlangBeamCli.test.tsindex 03b170757..ffe694534 100644--- a/tests/acceptance/erlangBeamCli.test.ts+++ b/tests/acceptance/erlangBeamCli.test.ts@@ -2,7 +2,10 @@ import { writeFile } from "node:fs/promises";import { join } from "node:path";import { expect } from "vitest";import { toolContract } from "../../src/contracts/toolContracts.js";-import { erlangBeamFixture } from "../../src/erlang/Beam.fixture.js";+import {+ erlangBeamFixture,+ erlangBeamOtp29Fixture,+} from "../../src/erlang/Beam.fixture.js";import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";import { cliTest } from "../support/cli/cliFixture.js";@@ -21,6 +24,35 @@ const oneAtomBeam = (paddingByte: number): Buffer => {return bytes;};+const extendedFirstAtomLength = (): Buffer => {+ const control = erlangBeamOtp29Fixture();+ const chunkOffset = control.indexOf(Buffer.from("AtU8", "latin1"));+ const payloadOffset = chunkOffset + 8;+ const payloadSize = control.readUInt32BE(chunkOffset + 4);+ const payloadEnd = payloadOffset + payloadSize;+ expect(control.subarray(payloadOffset + 4, payloadOffset + 6)).toEqual(+ Buffer.from([0x08, 0x10]),+ );+ const payload = Buffer.concat([+ control.subarray(payloadOffset, payloadOffset + 4),+ Buffer.from([0x18, 0x00, 0x10]),+ control.subarray(payloadOffset + 6, payloadEnd),+ ]);+ const paddingBytes = (4 - (payload.length % 4)) % 4;+ const chunkHeader = Buffer.from(control.subarray(chunkOffset, payloadOffset));+ chunkHeader.writeUInt32BE(payload.length, 4);+ const suffixOffset = payloadEnd + ((4 - (payloadSize % 4)) % 4);+ const extended = Buffer.concat([+ control.subarray(0, chunkOffset),+ chunkHeader,+ payload,+ Buffer.alloc(paddingBytes),+ control.subarray(suffixOffset),+ ]);+ extended.writeUInt32BE(extended.length - 8, 4);+ return extended;+};+cliTest("reports BEAM exports through the public CLI", async ({ cli }) => {const root = await createTestTempDirectory("rea-beam-cli-");const path = join(root, "app.beam");@@ -40,6 +72,50 @@ cliTest("reports BEAM exports through the public CLI", async ({ cli }) => {).toBe(true);});+cliTest(+ "inspects an OTP 29 BEAM with an extended compact atom length and preserves source identity",+ async ({ cli }) => {+ const root = await createTestTempDirectory("rea-beam-extended-atom-");+ const controlPath = join(root, "control.beam");+ const extendedPath = join(root, "extended.beam");+ const controlBytes = erlangBeamOtp29Fixture();+ const extendedBytes = extendedFirstAtomLength();+ await writeFile(controlPath, controlBytes);+ await writeFile(extendedPath, extendedBytes);++ const inspect = async (path: string) => {+ const result = await cli.run({+ arguments: ["inspect-erlang-beam", path, "--json"],+ });+ expect(result.exitCode, result.stderr).toBe(0);+ return toolContract("inspect_erlang_beam").outputSchema.parse(+ result.json,+ );+ };+ const control = await inspect(controlPath);+ const extended = await inspect(extendedPath);+ expect(extended.normalized_result.module).toBe("rea_beam_fixture");+ expect(extended.normalized_result.atoms).toEqual(+ control.normalized_result.atoms,+ );+ expect(extended.normalized_result.exports).toEqual(+ control.normalized_result.exports,+ );+ expect(extended.normalized_result.imports).toEqual(+ control.normalized_result.imports,+ );+ expect(extended.normalized_result.artifact.bytes).toBe(+ control.normalized_result.artifact.bytes,+ );+ expect(extended.normalized_result.artifact.sha256).not.toBe(+ control.normalized_result.artifact.sha256,+ );+ expect(extended.normalized_result.chunks[0]?.payload.sha256).not.toBe(+ control.normalized_result.chunks[0]?.payload.sha256,+ );+ },+);+cliTest("inspects BEAM chunks with nonzero alignment padding and preserves payload identity",async ({ cli }) => {