Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(erlang): accept uninterpreted BEAM alignment bytes

morluto860b3dd6b012
diff --git a/src/erlang/BeamParser.ts b/src/erlang/BeamParser.ts
index bac7568d4..694b2c6d0 100644
--- a/src/erlang/BeamParser.ts
+++ b/src/erlang/BeamParser.ts
@@ -192,9 +192,6 @@ export const parseErlangBeam = (
requireRange(bytes, payloadOffset, size);
const padded = payloadOffset + size + ((4 - (size % 4)) % 4);
if (padded > formEnd) fail("BEAM chunk padding exceeds the form.");
- const padding = bytes.subarray(payloadOffset + size, padded);
- if (padding.some((byte) => byte !== 0))
- fail("BEAM chunk padding is non-zero.");
const payload = bytes.subarray(payloadOffset, payloadOffset + size);
chunks.push({
index: chunks.length,
diff --git a/tests/acceptance/erlangBeamCli.test.ts b/tests/acceptance/erlangBeamCli.test.ts
index 883c448d0..03b170757 100644
--- a/tests/acceptance/erlangBeamCli.test.ts
+++ b/tests/acceptance/erlangBeamCli.test.ts
@@ -6,6 +6,21 @@ import { erlangBeamFixture } from "../../src/erlang/Beam.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
+const oneAtomBeam = (paddingByte: number): Buffer => {
+ const bytes = Buffer.alloc(28);
+ bytes.write("FOR1", 0, "latin1");
+ bytes.writeUInt32BE(20, 4);
+ bytes.write("BEAM", 8, "latin1");
+ bytes.write("AtU8", 12, "latin1");
+ bytes.writeUInt32BE(6, 16);
+ bytes.writeUInt32BE(1, 20);
+ bytes[24] = 1;
+ bytes[25] = "m".charCodeAt(0);
+ bytes[26] = paddingByte;
+ bytes[27] = paddingByte === 0 ? 0 : 1;
+ return bytes;
+};
+
cliTest("reports BEAM exports through the public CLI", async ({ cli }) => {
const root = await createTestTempDirectory("rea-beam-cli-");
const path = join(root, "app.beam");
@@ -25,6 +40,58 @@ cliTest("reports BEAM exports through the public CLI", async ({ cli }) => {
).toBe(true);
});
+cliTest(
+ "inspects BEAM chunks with nonzero alignment padding and preserves payload identity",
+ async ({ cli }) => {
+ const root = await createTestTempDirectory("rea-beam-padding-");
+ const cleanPath = join(root, "padding-zero.beam");
+ const paddedPath = join(root, "padding-nonzero.beam");
+ await writeFile(cleanPath, oneAtomBeam(0));
+ await writeFile(paddedPath, oneAtomBeam(0x7f));
+
+ 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 clean = await inspect(cleanPath);
+ const padded = await inspect(paddedPath);
+ expect(padded.normalized_result.module).toBe("m");
+ expect(padded.normalized_result.chunks).toEqual(
+ clean.normalized_result.chunks,
+ );
+ expect(padded.normalized_result.artifact.bytes).toBe(
+ clean.normalized_result.artifact.bytes,
+ );
+ expect(padded.normalized_result.artifact.sha256).not.toBe(
+ clean.normalized_result.artifact.sha256,
+ );
+ },
+);
+
+cliTest(
+ "rejects a BEAM chunk whose required alignment bytes are truncated",
+ async ({ cli }) => {
+ const root = await createTestTempDirectory("rea-beam-padding-short-");
+ const path = join(root, "padding-truncated.beam");
+ const bytes = oneAtomBeam(0x7f).subarray(0, 27);
+ bytes.writeUInt32BE(19, 4);
+ await writeFile(path, bytes);
+ const result = await cli.run({
+ arguments: ["inspect-erlang-beam", path, "--json"],
+ });
+ expect(result.exitCode).toBe(1);
+ expect(result.json).toMatchObject({
+ code: "invalid_request",
+ details: { operation: "inspect_erlang_beam" },
+ });
+ },
+);
+
cliTest(
"resolves a relative BEAM path against the working directory",
async ({ cli }) => {