Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(managed): retain operand-free instruction anchors

morluto483678bf9f43
diff --git a/src/dotnet/ManagedMemberInspector.test.ts b/src/dotnet/ManagedMemberInspector.test.ts
index e059579b3..8b65fc592 100644
--- a/src/dotnet/ManagedMemberInspector.test.ts
+++ b/src/dotnet/ManagedMemberInspector.test.ts
@@ -41,6 +41,55 @@ const ldstrBody = (tokens: readonly string[]): Buffer => {
return Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]);
};
+describe("managed method body anchors", () => {
+ it("keeps operand-free instructions in anchors so branch comparison constants stay recoverable", () => {
+ // ldloc.s 4; ldc.i4.4; bne.un.s +0; ret. The constant this branch compares
+ // against is carried by the operand-free ldc.i4.4 opcode itself, so the
+ // anchors must retain the instruction rather than only its operand.
+ const il = Buffer.from([0x11, 0x04, 0x1a, 0x33, 0x00, 0x2a]);
+ const bytes = buildManagedPeFixture({
+ ilBody: Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]),
+ });
+ const result = inspectManagedMembersBytes(
+ bytes,
+ managedPeFixtureTarget(bytes),
+ );
+
+ expect(result.methods[0]?.body.opcode_counts).toEqual({
+ "ldloc.s": 1,
+ "ldc.i4.4": 1,
+ "bne.un.s": 1,
+ ret: 1,
+ });
+ expect(result.methods[0]?.body.anchors).toEqual([
+ {
+ il_offset: 0,
+ opcode: "ldloc.s",
+ operand_kind: "variable",
+ operand: "4",
+ },
+ {
+ il_offset: 2,
+ opcode: "ldc.i4.4",
+ operand_kind: "none",
+ operand: null,
+ },
+ {
+ il_offset: 3,
+ opcode: "bne.un.s",
+ operand_kind: "branch",
+ operand: "5",
+ },
+ {
+ il_offset: 5,
+ opcode: "ret",
+ operand_kind: "none",
+ operand: null,
+ },
+ ]);
+ });
+});
+
describe("managed member inspection", () => {
it("keeps metadata handles distinct from calls and field accesses", () => {
const il = Buffer.from([
@@ -56,7 +105,7 @@ describe("managed member inspection", () => {
);
expect(result.methods[0]?.body.anchors.map(({ opcode }) => opcode)).toEqual(
- ["ldtoken", "ldtoken", "call", "ldfld"],
+ ["ldtoken", "pop", "ldtoken", "pop", "call", "ldfld", "pop", "ret"],
);
expect(result.call_edges.map(({ opcode }) => opcode)).toEqual(["call"]);
expect(result.field_accesses.map(({ opcode }) => opcode)).toEqual([
@@ -118,6 +167,12 @@ describe("managed member inspection", () => {
truncated_instructions: 0,
opcode_counts: { "ldarg.0": 1, ldfld: 1, call: 1, ret: 1 },
anchors: [
+ {
+ il_offset: 0,
+ opcode: "ldarg.0",
+ operand_kind: "none",
+ operand: null,
+ },
{
il_offset: 1,
opcode: "ldfld",
@@ -130,6 +185,12 @@ describe("managed member inspection", () => {
operand_kind: "method",
operand: "0x0a000001",
},
+ {
+ il_offset: 11,
+ opcode: "ret",
+ operand_kind: "none",
+ operand: null,
+ },
],
}),
}),
@@ -768,12 +829,20 @@ describe("managed user strings", () => {
issues: [],
});
const body = result.methods[0]?.body;
+ // Each ldstr is followed by an operand-free pop, and ret ends the body;
+ // anchors keep those null-operand instructions too.
expect(body?.anchors.map(({ operand }) => operand)).toEqual([
unicode,
+ null,
path,
+ null,
lone,
+ null,
path,
+ null,
empty,
+ null,
+ null,
]);
expect(body?.normalized_il_sha256).toBe(
inspect({ ilBody }).methods[0]?.body.normalized_il_sha256,
diff --git a/src/dotnet/ManagedMethodBodyReader.ts b/src/dotnet/ManagedMethodBodyReader.ts
index 81f3543a2..e30463910 100644
--- a/src/dotnet/ManagedMethodBodyReader.ts
+++ b/src/dotnet/ManagedMethodBodyReader.ts
@@ -121,14 +121,14 @@ export const methodBody = (
for (const instruction of decoded.parsed)
opcodeCounts[instruction.opcode] =
(opcodeCounts[instruction.opcode] ?? 0) + 1;
- const anchors = decoded.parsed
- .filter((instruction) => instruction.operandKind !== "none")
- .map((instruction) => ({
- il_offset: instruction.offset,
- opcode: instruction.opcode,
- operand_kind: instruction.operandKind,
- operand: instruction.operand,
- }));
+ // Keep every decoded instruction: implicit constants such as ldc.i4.N
+ // are encoded in opcodes whose operand kind is "none".
+ const anchors = decoded.parsed.map((instruction) => ({
+ il_offset: instruction.offset,
+ opcode: instruction.opcode,
+ operand_kind: instruction.operandKind,
+ operand: instruction.operand,
+ }));
const normalized = Buffer.from(
JSON.stringify(
decoded.parsed.map(({ opcode, operandKind, operand }) => [
diff --git a/tests/acceptance/managedCliOutput.test.ts b/tests/acceptance/managedCliOutput.test.ts
index 26ef7b85d..5e164c0cf 100644
--- a/tests/acceptance/managedCliOutput.test.ts
+++ b/tests/acceptance/managedCliOutput.test.ts
@@ -128,7 +128,12 @@ cliTest(
async ({ cli }) => {
const root = await createTestTempDirectory("rea-managed-cli-output-");
const path = join(root, "fixture.dll");
- await writeFile(path, buildManagedPeFixture());
+ await writeFile(
+ path,
+ buildManagedPeFixture({
+ ilBody: Buffer.from([0x0e, 0x17, 0x26, 0x2a]),
+ }),
+ );
const argv = ["inspect-managed-members", path, "--json"];
const result = await cli.run({ arguments: argv });
expect(result.exitCode).toBe(0);
@@ -137,7 +142,35 @@ cliTest(
provider: { id: "rea-dotnet-static" },
subject: { local_path: path },
normalized_result: {
- methods: [expect.objectContaining({ token: expect.any(String) })],
+ methods: [
+ expect.objectContaining({
+ token: expect.any(String),
+ body: expect.objectContaining({
+ status: "present",
+ decoded_instruction_count: 3,
+ anchors: [
+ {
+ il_offset: 0,
+ opcode: "ldc.i4.1",
+ operand_kind: "none",
+ operand: null,
+ },
+ {
+ il_offset: 1,
+ opcode: "pop",
+ operand_kind: "none",
+ operand: null,
+ },
+ {
+ il_offset: 2,
+ opcode: "ret",
+ operand_kind: "none",
+ operand: null,
+ },
+ ],
+ }),
+ }),
+ ],
},
});
const filtered = await cli.run({
diff --git a/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts b/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts
index 29cc3cb79..1f33b447c 100644
--- a/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts
+++ b/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts
@@ -13,6 +13,94 @@ import {
managedPeFixtureTarget,
} from "../../../../src/dotnet/ManagedPe.fixture.js";
+describe("managed operand-free instruction structural matching", () => {
+ it("preserves operand-free opcode order when remapping renamed methods", () => {
+ const observe = (
+ methodName: string,
+ fieldToken: number,
+ swapImplicitConstants: boolean,
+ path: string,
+ ) => {
+ const il = Buffer.from([
+ 0x7b,
+ 0,
+ 0,
+ 0,
+ 0, // ldfld token
+ 0x26, // pop
+ swapImplicitConstants ? 0x18 : 0x17, // ldc.i4.1 or ldc.i4.2
+ 0x26, // pop
+ swapImplicitConstants ? 0x17 : 0x18, // the other implicit constant
+ 0x26, // pop
+ 0x2a, // ret
+ ]);
+ il.writeUInt32LE(fieldToken, 1);
+ return inspect(
+ buildManagedPeFixture({
+ methodName,
+ ilBody: Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]),
+ }),
+ path,
+ );
+ };
+ const left = observe(
+ "OldMethod",
+ 0x04000001,
+ false,
+ "/tmp/implicit-order-left.dll",
+ );
+ const sameOrder = observe(
+ "NewMethod",
+ 0x04000002,
+ false,
+ "/tmp/implicit-order-same.dll",
+ );
+ const swapped = observe(
+ "NewMethod",
+ 0x04000002,
+ true,
+ "/tmp/implicit-order-swapped.dll",
+ );
+
+ expect(left.result.methods[0]?.body.opcode_counts).toEqual(
+ swapped.result.methods[0]?.body.opcode_counts,
+ );
+ expect(left.result.methods[0]?.body.il_sha256).not.toBe(
+ sameOrder.result.methods[0]?.body.il_sha256,
+ );
+ expect(left.result.methods[0]?.body.il_sha256).not.toBe(
+ swapped.result.methods[0]?.body.il_sha256,
+ );
+ const sameOrderComparison = compareManagedMembers(left, sameOrder);
+ expect(sameOrderComparison.methods).toContainEqual(
+ expect.objectContaining({
+ status: "changed",
+ match: expect.objectContaining({
+ status: "matched",
+ basis: "structural-method-shape",
+ }),
+ }),
+ );
+
+ const swappedComparison = compareManagedMembers(left, swapped);
+ expect(swappedComparison.methods.map(({ status }) => status)).toEqual([
+ "removed",
+ "added",
+ ]);
+ expect(
+ swappedComparison.methods.some(
+ ({ match }) => match.basis === "structural-method-shape",
+ ),
+ ).toBe(false);
+ expect(
+ left.result.methods[0]?.body.anchors.map(({ opcode }) => opcode),
+ ).toEqual(["ldfld", "pop", "ldc.i4.1", "pop", "ldc.i4.2", "pop", "ret"]);
+ expect(
+ swapped.result.methods[0]?.body.anchors.map(({ opcode }) => opcode),
+ ).toEqual(["ldfld", "pop", "ldc.i4.2", "pop", "ldc.i4.1", "pop", "ret"]);
+ });
+});
+
describe("managed member structural matching", () => {
it("keeps unequal constants from becoming a structural remap", () => {
const observe = (
@@ -287,6 +375,13 @@ describe("managed member comparison floating-point constants", () => {
status: "present",
anchors: [
{ il_offset: 0, opcode, operand_kind: "constant", operand },
+ // ldc.r4 spans 5 bytes and ldc.r8 spans 9 before the ret.
+ {
+ il_offset: opcode === "ldc.r4" ? 5 : 9,
+ opcode: "ret",
+ operand_kind: "none",
+ operand: null,
+ },
],
});
}