Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(managed): share reference instruction semantics

morluto749ae11b1571
diff --git a/src/domain/managed/managedMemberComparisonItems.ts b/src/domain/managed/managedMemberComparisonItems.ts
index b4ae7500a..583f2d3be 100644
--- a/src/domain/managed/managedMemberComparisonItems.ts
+++ b/src/domain/managed/managedMemberComparisonItems.ts
@@ -8,6 +8,7 @@ import type {
ManagedMethodMatches,
} from "./managedMemberComparisonMatch.js";
import { signatureIdentity } from "./managedMemberSignatureIdentity.js";
+import { isManagedMemberReferenceInstruction } from "./managedMemberInstructionFacts.js";
type Method = ManagedMemberInspection["methods"][number];
type Field = ManagedMemberInspection["fields"][number];
@@ -162,8 +163,9 @@ const compareCallTargets = (
): { readonly changed: boolean; readonly unknown: boolean } => {
const callAnchors = (method: Method) =>
method.body.anchors.filter(
- ({ opcode, operand_kind }) =>
- opcode !== "ldtoken" && operand_kind === "method",
+ (instruction) =>
+ isManagedMemberReferenceInstruction(instruction) &&
+ instruction.operand_kind === "method",
);
const leftAnchors = callAnchors(left);
const rightAnchors = callAnchors(right);
@@ -203,14 +205,22 @@ const compareCallTargets = (
const callShape = (anchors: Method["body"]["anchors"]): string =>
JSON.stringify(
anchors
- .filter(({ operand_kind }) => operand_kind === "method")
+ .filter(
+ (instruction) =>
+ isManagedMemberReferenceInstruction(instruction) &&
+ instruction.operand_kind === "method",
+ )
.map(({ opcode, operand_kind }) => [opcode, operand_kind]),
);
const fieldShape = (anchors: Method["body"]["anchors"]): string =>
JSON.stringify(
anchors
- .filter(({ operand_kind }) => operand_kind === "field")
+ .filter(
+ (instruction) =>
+ isManagedMemberReferenceInstruction(instruction) &&
+ instruction.operand_kind === "field",
+ )
.map(({ opcode, operand_kind }) => [opcode, operand_kind]),
);
diff --git a/src/domain/managed/managedMemberInstructionFacts.ts b/src/domain/managed/managedMemberInstructionFacts.ts
new file mode 100644
index 000000000..fc2c87fed
--- /dev/null
+++ b/src/domain/managed/managedMemberInstructionFacts.ts
@@ -0,0 +1,18 @@
+/** Facts available on decoded member-reference-bearing CIL instructions. */
+export interface ManagedMemberInstructionFact {
+ readonly opcode: string;
+ readonly operand_kind: string;
+ readonly operand: string | null;
+}
+
+/** Whether an instruction contributes a method or field operand fact to member analysis. */
+export const isManagedMemberReferenceInstruction = (
+ instruction: ManagedMemberInstructionFact,
+): instruction is ManagedMemberInstructionFact & {
+ readonly operand_kind: "method" | "field";
+ readonly operand: string;
+} =>
+ instruction.opcode !== "ldtoken" &&
+ instruction.operand !== null &&
+ (instruction.operand_kind === "method" ||
+ instruction.operand_kind === "field");
diff --git a/src/dotnet/ManagedMemberRows.ts b/src/dotnet/ManagedMemberRows.ts
index 1ab876e31..0f5cb8607 100644
--- a/src/dotnet/ManagedMemberRows.ts
+++ b/src/dotnet/ManagedMemberRows.ts
@@ -1,4 +1,5 @@
import { jsonStringByteLength } from "../domain/jsonStringByteLength.js";
+import { isManagedMemberReferenceInstruction } from "../domain/managed/managedMemberInstructionFacts.js";
import {
admitManagedProjection,
managedDecodeBudget,
@@ -445,18 +446,14 @@ export const edges = (
const fieldAccesses: ManagedFieldAccess[] = [];
for (const method of methods) {
const derives = method.body.anchors.some(
- (anchor) =>
- anchor.opcode !== "ldtoken" &&
- anchor.operand !== null &&
- (anchor.operand_kind === "method" || anchor.operand_kind === "field"),
+ isManagedMemberReferenceInstruction,
);
if (derives) budget.admitTransient(callerNameBytes(method), null);
// Built once per method, only after its exact size was admitted.
const caller = derives ? callerName(method) : method.name;
for (const anchor of method.body.anchors) {
- if (anchor.opcode === "ldtoken") continue;
+ if (!isManagedMemberReferenceInstruction(anchor)) continue;
const { operand } = anchor;
- if (operand === null) continue;
const target = namedTarget(members, anchor.operand_kind, operand);
if (anchor.operand_kind === "method") {
// Each derived record is admitted before the graph grows further.
diff --git a/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts b/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts
index 37b1dd7ac..8dd25e13a 100644
--- a/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts
+++ b/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts
@@ -101,6 +101,48 @@ describe("managed operand-free instruction structural matching", () => {
});
});
+describe("managed reference instruction comparison", () => {
+ it("does not count ldtoken MemberRefs as calls or field accesses", () => {
+ const observe = (signature: readonly number[], path: string) => {
+ const il = Buffer.from([0xd0, 1, 0, 0, 0x0a, 0x26, 0x2a]);
+ return inspect(
+ buildManagedPeFixture({
+ targetFramework: null,
+ memberRefName: "Counter",
+ memberRefParentRaw: 8,
+ ilBody: Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]),
+ memberRefSignature: Buffer.from(signature),
+ }),
+ path,
+ );
+ };
+ const methodHandle = observe(
+ [0x20, 0x01, 0x01, 0x0e],
+ "/tmp/ldtoken-method-handle.dll",
+ );
+ const fieldHandle = observe([0x06, 0x08], "/tmp/ldtoken-field-handle.dll");
+
+ expect(methodHandle.result.methods[0]?.body.anchors[0]).toMatchObject({
+ opcode: "ldtoken",
+ operand_kind: "method",
+ });
+ expect(fieldHandle.result.methods[0]?.body.anchors[0]).toMatchObject({
+ opcode: "ldtoken",
+ operand_kind: "field",
+ });
+ expect(methodHandle.result.call_edges).toEqual([]);
+ expect(fieldHandle.result.call_edges).toEqual([]);
+ expect(methodHandle.result.field_accesses).toEqual([]);
+ expect(fieldHandle.result.field_accesses).toEqual([]);
+
+ const comparison = compareManagedMembers(methodHandle, fieldHandle);
+ const dimensions = comparison.methods[0]?.dimensions ?? [];
+ expect(dimensions).not.toContain("call-shape");
+ expect(dimensions).not.toContain("field-shape");
+ expect(dimensions).not.toContain("call-target");
+ });
+});
+
describe("managed member structural matching", () => {
it("keeps unequal constants from becoming a structural remap", () => {
const observe = (