diff --git a/src/dotnet/ManagedMemberInstructionDecoder.ts b/src/dotnet/ManagedMemberInstructionDecoder.ts index f638e3e83..7ef518733 100644 --- a/src/dotnet/ManagedMemberInstructionDecoder.ts +++ b/src/dotnet/ManagedMemberInstructionDecoder.ts @@ -265,6 +265,10 @@ type OperandResult = { type OperandReader = (il: Buffer, offset: number) => OperandResult; +// String(-0) loses a CIL constant's sign in both anchors and normalized hashes. +const floatingPointOperand = (value: number): string => + Object.is(value, -0) ? "-0" : String(value); + const FIXED_OPERAND_READERS = new Map([ [ "none", @@ -315,7 +319,7 @@ const FIXED_OPERAND_READERS = new Map([ (il, offset) => ({ next: offset + 4, anchorKind: "constant", - value: String(il.readFloatLE(offset)), + value: floatingPointOperand(il.readFloatLE(offset)), }), ], [ @@ -323,7 +327,7 @@ const FIXED_OPERAND_READERS = new Map([ (il, offset) => ({ next: offset + 8, anchorKind: "constant", - value: String(il.readDoubleLE(offset)), + value: floatingPointOperand(il.readDoubleLE(offset)), }), ], [ diff --git a/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts b/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts index 7f4408811..56014fbd4 100644 --- a/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts +++ b/tests/boundary/providers/managed/managedMemberComparisonFromMetadata.test.ts @@ -116,6 +116,66 @@ describe("managed member comparison", () => { }); }); +describe("managed member comparison floating-point constants", () => { + it.each([ + { + opcode: "ldc.r4", + positive: "22000000002a", + negative: "22000000802a", + type: 0x0c, + }, + { + opcode: "ldc.r8", + positive: "2300000000000000002a", + negative: "2300000000000000802a", + type: 0x0d, + }, + ])( + "detects a change from positive to negative zero in $opcode", + ({ opcode, positive, negative, type }) => { + const observe = (hex: string, path: string) => { + const il = Buffer.from(hex, "hex"); + return inspect( + buildManagedPeFixture({ + methodSignature: Buffer.from([0x00, 0x00, type]), + ilBody: Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]), + }), + path, + ); + }; + const left = observe(positive, "/tmp/positive-zero.dll"); + const right = observe(negative, "/tmp/negative-zero.dll"); + const comparison = compareManagedMembers(left, right); + + expect(comparison.methods[0]).toMatchObject({ + status: "changed", + match: { status: "matched", basis: "exact-signature" }, + dimensions: ["cil"], + }); + for (const [observation, operand] of [ + [left, "0"], + [right, "-0"], + ] as const) { + expect(observation.result.methods[0]?.body).toMatchObject({ + status: "present", + anchors: [ + { il_offset: 0, opcode, operand_kind: "constant", operand }, + ], + }); + } + expect(left.result.methods[0]?.body.normalized_il_sha256).not.toBe( + right.result.methods[0]?.body.normalized_il_sha256, + ); + const repeated = observe(negative, "/tmp/negative-zero-copy.dll"); + expect(compareManagedMembers(right, repeated).methods[0]).toMatchObject({ + status: "unchanged", + match: { status: "matched", basis: "exact-il-signature" }, + dimensions: [], + }); + }, + ); +}); + describe("managed member comparison uncertainty", () => { it("does not infer a structural method match from undecoded signature fields", () => { const left = inspect(