diff --git a/src/domain/managed/managedMemberInstructionFacts.ts b/src/domain/managed/managedMemberInstructionFacts.ts index fc2c87feda..2aff8228f8 100644 --- a/src/domain/managed/managedMemberInstructionFacts.ts +++ b/src/domain/managed/managedMemberInstructionFacts.ts @@ -16,3 +16,7 @@ export const isManagedMemberReferenceInstruction = ( instruction.operand !== null && (instruction.operand_kind === "method" || instruction.operand_kind === "field"); + +/** A string load's operand is a #US coordinate regardless of its encoded table byte. */ +export const isManagedUserStringLoad = (opcode: string): boolean => + opcode === "ldstr"; diff --git a/src/dotnet/ManagedCilDecoding.test.ts b/src/dotnet/ManagedCilDecoding.test.ts index 1107b66846..8396fb5183 100644 --- a/src/dotnet/ManagedCilDecoding.test.ts +++ b/src/dotnet/ManagedCilDecoding.test.ts @@ -511,14 +511,18 @@ describe("managed complete operand decoding", () => { }); }); - it("retains named references and loaded literals before a truncated operand", () => { - const { heap, tokens } = fixtureUserStringHeap(["observed"]); - const literalToken = tokens[0]!; + it("retains named references and literals only before the invalid opcode", () => { + const { heap, tokens } = fixtureUserStringHeap([ + "observed", + "after failure", + ]); + const [literalToken, decoyToken] = tokens as [string, string]; const il = Buffer.from([ 0x28, 1, 0, 0, 0x0a, 0x7e, 1, 0, 0, 0x04, 0x26, 0x72, 0, 0, 0, 0, 0x26, - 0x20, + 0xfe, 0xff, 0x72, 0, 0, 0, 0, 0x26, 0x2a, ]); il.writeUInt32LE(Number.parseInt(literalToken, 16), 12); + il.writeUInt32LE(Number.parseInt(decoyToken, 16), 20); const bytes = buildManagedPeFixture({ memberRefParentRaw: 8, memberRefName: "ObservedCall", @@ -544,7 +548,7 @@ describe("managed complete operand decoding", () => { { opcode: "ldstr", operand: literalToken }, { opcode: "pop", operand: null }, ], - issue: "Instruction ldc.i4 at IL offset 17 leaves method body", + issue: "Unsupported CIL opcode 0xfeff at IL offset 17", }); expect(result.call_edges).toMatchObject([ { diff --git a/src/dotnet/ManagedMemberInspector.test.ts b/src/dotnet/ManagedMemberInspector.test.ts index d43a371a65..06728b4b71 100644 --- a/src/dotnet/ManagedMemberInspector.test.ts +++ b/src/dotnet/ManagedMemberInspector.test.ts @@ -1,5 +1,4 @@ -import * as inventoryProducer from "../domain/managed/managedMetadataInventory.js"; -import { describe, expect, it, vi } from "vitest"; +import { describe, expect, it } from "vitest"; import { inspectManagedMembersBytes } from "./ManagedMemberInspector.js"; import { InternalInvariantError } from "../domain/internalInvariant.js"; @@ -1478,7 +1477,7 @@ describe("managed exported types", () => { expect(result.coverage).toEqual({ state: "complete", issues: [] }); }); - it("leaves exported types unknown when CLI metadata is unavailable", () => { + it("leaves exported types and user strings unknown without CLI metadata", () => { const bytes = buildNativePeFixture(); const result = inspectManagedMembersBytes( bytes, @@ -1487,6 +1486,7 @@ describe("managed exported types", () => { expect(result.coverage.state).toBe("unavailable"); expect(result.exported_types).toBeNull(); + expect(result.user_strings).toBeNull(); }); it("refuses member Evidence without the exported-type inventory", () => { @@ -1533,39 +1533,7 @@ describe("managed exported types", () => { }); }); -it("keeps decoded members when only the loaded literals exceed capacity", () => { - const { heap, tokens } = fixtureUserStringHeap( - Array.from({ length: 8 }, (_, index) => String(index).repeat(8_000)), - ); - const bytes = buildManagedPeFixture({ - ilBody: ldstrBody(tokens), - userStringHeap: heap, - }); - const target = managedPeFixtureTarget(bytes); - // The members fit 40 KB; the eight 8,000-character literals do not. - const result = inspectManagedMembersBytes(bytes, target, { - maxRepresentationBytes: 40_000, - }); - - expect(result.methods).toHaveLength(1); - expect(result.coverage).toEqual({ state: "complete", issues: [] }); - expect(result.user_strings).toMatchObject({ - state: "partial", - literals: [], - issues: [{ code: "resource-limit", scope: "metadata.#US" }], - }); - expect(result.limitations.join(" ")).toMatch(/literals are not examined/); - expect( - inspectManagedMembersBytes(bytes, target).user_strings?.literals, - ).toHaveLength(8); -}); - describe("managed user strings", () => { - const inspect = (options: Parameters[0]) => { - const bytes = buildManagedPeFixture(options); - return inspectManagedMembersBytes(bytes, managedPeFixtureTarget(bytes)); - }; - it("decodes each loaded literal once, in token order, without changing CIL tokens", () => { const { heap, tokens } = fixtureUserStringHeap([ "quicksave.peak", @@ -1710,42 +1678,31 @@ describe("managed user strings", () => { })), }); }); - - it("leaves user strings unexamined when CLI metadata is unavailable", () => { - const native = buildNativePeFixture(); - expect( - inspectManagedMembersBytes(native, managedPeFixtureTarget(native)) - .user_strings, - ).toBeNull(); - }); }); describe("managed user string issues", () => { - const inspect = (options: Parameters[0]) => { - const bytes = buildManagedPeFixture(options); - return inspectManagedMembersBytes(bytes, managedPeFixtureTarget(bytes)); - }; - it("reports a missing #US stream once and keeps the method", () => { const result = inspect({ - // A MethodDef operand is not a #US reference, so it is reported alone. + // A MethodSpec operand is not a #US reference, so it is reported alone. ilBody: ldstrBody([ "0x70000001", "0x70000005", "0x70000001", - "0x06000001", + "0x2b000001", ]), }); - expect(result.methods).toHaveLength(1); - expect(result.coverage.state).toBe("complete"); + expect(result.methods[0]?.body.anchors).toContainEqual( + expect.objectContaining({ opcode: "ldstr", operand: "0x2b000001" }), + ); + expect(result.coverage).toEqual({ state: "complete", issues: [] }); expect(result.user_strings).toEqual({ state: "partial", literals: [], issues: [ { code: "invalid-heap-index", - scope: "metadata.#US:0x06000001", + scope: "metadata.#US:0x2b000001", offset: null, detail: expect.stringMatching(/ldstr operand.*does not name.*#US/i), }, @@ -1864,42 +1821,18 @@ describe("managed user string issues", () => { }); describe("managed user string scan completeness", () => { - const inspect = (options: Parameters[0]) => { - const bytes = buildManagedPeFixture(options); - return inspectManagedMembersBytes(bytes, managedPeFixtureTarget(bytes)); - }; - - it("marks user strings partial when a method body is not fully decoded", () => { - const { heap, tokens } = fixtureUserStringHeap(["ok", "later"]); - const [ok, later] = tokens as [string, string]; - // ldstr "ok", then an undefined opcode before ldstr "later". - const il = Buffer.from([ - 0x72, 0, 0, 0, 0, 0x26, 0xfe, 0xff, 0x72, 0, 0, 0, 0, 0x26, 0x2a, - ]); - il.writeUInt32LE(Number.parseInt(ok, 16), 1); - il.writeUInt32LE(Number.parseInt(later, 16), 9); - const result = inspect({ - ilBody: Buffer.concat([Buffer.from([(il.length << 2) | 2]), il]), - userStringHeap: heap, - }); - - expect(result.methods[0]?.body.status).toBe("malformed"); - expect(result.user_strings).toEqual({ - state: "partial", - literals: [{ token: ok, offset: expect.any(Number), value: "ok" }], - issues: [], - }); - }); - it.each([ - ["outside", 1, "complete"], - ["inside", 2, "partial"], + ["outside", 1, false, "complete"], + ["inside", 2, false, "partial"], + ["outside with its token in a string load", 1, true, "partial"], ])( "judges an unreadable method row %s the selection by its owner", - (_where, badRow, state) => { + (_where, badRow, invalidString, state) => { const bytes = buildManagedPeFixture({ additionalEmptyTypes: 1, methodRowCount: 2, + firstTypeMethodRowCount: 1, + ...(invalidString ? { ilBody: ldstrBody(["0x06000001"]) } : {}), }); const cli = readManagedPeLayout(bytes); if (cli.cli === null) throw new Error("fixture has no CLI header"); @@ -1909,25 +1842,11 @@ describe("managed user string scan completeness", () => { cli.rvaToOffset(rva, size, "cli.metadata"), size, ); - const write = (offset: number, width: number, value: number) => - width === 2 - ? bytes.writeUInt16LE(value, offset) - : bytes.writeUInt32LE(value, offset); - // Fixture.Program's methods now start at row 2, so Fixture.Empty0 owns row 1. - write( - metadataRowOffset(layout, 2, 2) + - 4 + - 2 * layout.stringIndexSize + - layout.codedIndexSize("TypeDefOrRef") + - layout.tableIndexSize(4), - layout.tableIndexSize(6), - 2, - ); // An out-of-range name index makes that method row unreadable. - write( + bytes.writeUIntLE( + 0xffff, metadataRowOffset(layout, 6, badRow) + 8, layout.stringIndexSize, - 0xffff, ); const result = inspectManagedMembersBytes( bytes, @@ -1935,7 +1854,15 @@ describe("managed user string scan completeness", () => { { typeNames: ["Fixture.Program"] }, ); - expect(result.user_strings?.state).toBe(state); + expect(result.user_strings).toMatchObject({ + state, + literals: [], + issues: invalidString + ? [{ code: "invalid-heap-index", scope: "metadata.#US:0x06000001" }] + : [], + }); + if (invalidString) + expect(result.coverage).toEqual({ state: "complete", issues: [] }); }, ); @@ -1963,152 +1890,112 @@ describe("managed user string scan completeness", () => { literals: [{ value: "ok" }], }); }); - - it("marks user strings partial when selected type identity is unreadable", () => { - const layout = { - userStrings: { - kind: "available", - stream: { name: "#US", offset: 0, size: 1 }, - issue: null, - }, - } as unknown as ManagedMetadataLayout; - expect( - parseUserStrings(Buffer.from([0]), layout, { - methods: [], - unscannedMethods: 0, - unreadableSelectedTypes: 1, - }), - ).toEqual({ state: "partial", literals: [], issues: [] }); - }); - - it("marks user strings partial when an inspected method row was not read", () => { - const layout = { - userStrings: { - kind: "available", - stream: { name: "#US", offset: 0, size: 1 }, - issue: null, - }, - } as unknown as ManagedMetadataLayout; - // The omitted row's body, and any ldstr in it, was never scanned. - expect( - parseUserStrings(Buffer.from([0]), layout, { - methods: [], - unscannedMethods: 1, - }), - ).toEqual({ state: "partial", literals: [], issues: [] }); - }); }); describe("managed member decoding budget", () => { - it("admits a wide-instruction method whose actual records fit the budget", () => { - // Five-byte branches all target the next instruction. A final ret leaves - // the method stack-neutral, despite over 256 KiB of valid IL bytes. - const il = Buffer.alloc(270_001); - for (let offset = 0; offset < il.length - 1; offset += 5) { - il[offset] = 0x38; - } - il[il.length - 1] = 0x2a; + const capacity = 128 * 1024; + const inspectBudget = ( + il: Buffer, + options: { + readonly methodSignature?: Buffer; + readonly section?: Buffer; + } = {}, + ) => { + const { section = Buffer.alloc(0), methodSignature } = options; const header = Buffer.alloc(12); - header.writeUInt16LE(0x3003, 0); + header.writeUInt16LE(section.length === 0 ? 0x3003 : 0x300b, 0); header.writeUInt16LE(8, 2); header.writeUInt32LE(il.length, 4); const bytes = buildManagedPeFixture({ - ilBody: Buffer.concat([header, il]), + ilBody: Buffer.concat([header, il, section]), + ...(methodSignature === undefined ? {} : { methodSignature }), }); - const result = inspectManagedMembersBytes( - bytes, - managedPeFixtureTarget(bytes), - ); + return inspectManagedMembersBytes(bytes, managedPeFixtureTarget(bytes), { + maxRepresentationBytes: capacity, + }); + }; - expect(result.coverage).toMatchObject({ state: "complete", issues: [] }); - expect(result.types).toHaveLength(1); - expect(result.methods[0]?.body).toMatchObject({ + it("charges actual instruction records under equal source byte lengths", () => { + const wide = Buffer.alloc(601); + for (let offset = 0; offset < wide.length - 1; offset += 5) + wide[offset] = 0x38; + wide[600] = 0x2a; + const dense = Buffer.alloc(601); + dense[600] = 0x2a; + const admitted = inspectBudget(wide); + expect(admitted.coverage).toEqual({ state: "complete", issues: [] }); + expect(admitted.methods[0]?.body).toMatchObject({ status: "present", - il_size: il.length, - decoded_instruction_count: 54_001, - opcode_counts: { br: 54_000, ret: 1 }, + il_size: 601, + decoded_instruction_count: 121, + opcode_counts: { br: 120, ret: 1 }, }); - }); - - it("rejects dense one-byte instructions while retaining admitted types", () => { - // All NOPs: unlike wide instructions, each IL byte adds a decoded record. - // A true oversized transient must remain an actionable resource limit. - const il = Buffer.alloc(270_001); - il[il.length - 1] = 0x2a; - const header = Buffer.alloc(12); - header.writeUInt16LE(0x3003, 0); - header.writeUInt16LE(8, 2); - header.writeUInt32LE(il.length, 4); - const bytes = buildManagedPeFixture({ - ilBody: Buffer.concat([header, il]), - }); - const result = inspectManagedMembersBytes( - bytes, - managedPeFixtureTarget(bytes), - ); - - expect(result.coverage).toMatchObject({ + const refused = inspectBudget(dense); + expect(refused.coverage).toMatchObject({ state: "partial", issues: [{ code: "resource-limit", scope: "metadata.representation" }], }); - // Report the originating IL coordinate, not a generic decoding error. - expect(result.coverage.issues[0]?.offset).toBeGreaterThan(0); - expect(result.types).toHaveLength(1); - expect(result.methods).toEqual([]); + expect(refused.coverage.issues[0]?.offset).toBeGreaterThan(0); + expect(refused.types.map(({ full_name }) => full_name)).toEqual([ + "Fixture.Program", + ]); + expect(refused.methods).toEqual([]); }); - it("charges decoded output rather than accumulated decoding bounds", () => { - // Each bound fits alone, but 256 bytes per IL and signature byte together - // exceed the 64 MiB budget although the retained members are small. - const il = Buffer.concat([Buffer.alloc(200_000), Buffer.from([0x2a])]); - const header = Buffer.alloc(12); - header.writeUInt16LE(0x3003, 0); - header.writeUInt16LE(8, 2); - header.writeUInt32LE(il.length, 4); - const bytes = buildManagedPeFixture({ - ilBody: Buffer.concat([header, il]), + it("releases signature bookkeeping before charging the method body", () => { + const il = Buffer.concat([Buffer.alloc(340), Buffer.from([0x2a])]); + const result = inspectBudget(il, { methodSignature: Buffer.concat([ Buffer.from([0x00, 0x00, 0x01]), - Buffer.alloc(80_000), + Buffer.alloc(180), ]), }); - const result = inspectManagedMembersBytes( - bytes, - managedPeFixtureTarget(bytes), + expect(result.coverage.issues).not.toContainEqual( + expect.objectContaining({ code: "resource-limit" }), ); - - expect(result.coverage.state).not.toBe("unavailable"); - expect(result.methods[0]?.body).toMatchObject({ - status: "present", - il_size: il.length, - opcode_counts: { nop: 200_000, ret: 1 }, + expect(result.methods[0]).toMatchObject({ + signature: { + parse_status: "partial", + parameter_count: 0, + return_type: "void", + }, + body: { + status: "present", + il_size: 341, + opcode_counts: { nop: 340, ret: 1 }, + }, }); }); - it("bounds a method's IL and exception sections together", () => { - // Each bound fits alone; both stay live while one method is decoded. - const il = Buffer.concat([Buffer.alloc(159_999), Buffer.from([0x2a])]); - const header = Buffer.alloc(12); - header.writeUInt16LE(0x300b, 0); - header.writeUInt16LE(8, 2); - header.writeUInt32LE(il.length, 4); - const clauses = 110_000; - const section = Buffer.alloc(4 + clauses * 24); - section.writeUInt32LE(0x41 | ((4 + clauses * 24) << 8), 0); - const bytes = buildManagedPeFixture({ - ilBody: Buffer.concat([header, il, section]), - }); - const result = inspectManagedMembersBytes( - bytes, - managedPeFixtureTarget(bytes), - ); + it("charges method instructions and exception sections while both are live", () => { + const il = Buffer.concat([Buffer.alloc(399), Buffer.from([0x2a])]); + const count = 120; + const section = Buffer.alloc(4 + count * 24); + section.writeUInt32LE(0x41 | (section.length << 8), 0); + for (let index = 0; index < count; index++) { + const offset = 4 + index * 24; + // A finally clause does not introduce a second catch-token charge. + section.writeUInt32LE(2, offset); + section.writeUInt32LE(1, offset + 8); + section.writeUInt32LE(1, offset + 12); + section.writeUInt32LE(1, offset + 16); + } + const result = inspectBudget(il, { section }); expect(result.coverage).toMatchObject({ state: "partial", - issues: [{ code: "resource-limit" }], + issues: [ + { + code: "resource-limit", + scope: "metadata.representation", + offset: 0x0a00 + 12 + il.length, // The fixture's fat header and IL precede the section. + }, + ], }); + expect(result.types.map(({ full_name }) => full_name)).toEqual([ + "Fixture.Program", + ]); expect(result.methods).toEqual([]); - expect(result.types).toHaveLength(1); }); }); @@ -3127,8 +3014,11 @@ describe("managed member type selection", () => { "Fixture.Program", ]); expect(result.coverage.state).toBe("partial"); - // Its methods were never inspected, so neither were their literals. - expect(result.user_strings?.state).toBe("partial"); + expect(result.user_strings).toEqual({ + state: "partial", + literals: [], + issues: [], + }); expect(result.limitations).toContainEqual( expect.stringMatching( /1 TypeDef name.*could not be read.*selection\.unmatched_type_names.*absence is not observed/iu, @@ -3214,9 +3104,9 @@ describe("managed member method paging", () => { it("returns only the requested MethodDef rows", () => { const image = buildMultiMethodManagedPeFixture({ methods: [ - { name: "First", body: Buffer.from([0x2a]) }, - { name: "Second", body: Buffer.from([0x2a]) }, - { name: "Third", body: Buffer.from([0x2a]) }, + { name: "First", body: tokenBody([]) }, + { name: "Second", body: tokenBody([]) }, + { name: "Third", body: tokenBody([]) }, ], }); const all = inspectManagedMembersBytes( @@ -3239,6 +3129,9 @@ describe("managed member method paging", () => { { name: "Second", token: "0x06000002" }, ]); expect(page.coverage.state).toBe("complete"); + expect(page.methods[0]?.body.anchors).toEqual([ + { il_offset: 0, opcode: "ret", operand_kind: "none", operand: null }, + ]); const offsetOnly = inspectManagedMembersBytes( image, @@ -3430,65 +3323,49 @@ it.each([false, true])( }, ); -describe("user string capacity overflow preserves decoded members", () => { - it("retains already-decoded members in one pass when #US literals overflow capacity", () => { - const { heap, tokens } = fixtureUserStringHeap([ - "small", - "x".repeat(8_000), - ]); - const bytes = buildManagedPeFixture({ - ilBody: ldstrBody(tokens), - userStringHeap: heap, - }); - const target = managedPeFixtureTarget(bytes); - const spy = vi.spyOn(inventoryProducer, "readManagedMetadataInventory"); - try { - const result = inspectManagedMembersBytes(bytes, target, { - maxRepresentationBytes: 40_000, - }); - - expect(spy).toHaveBeenCalledTimes(1); - expect(result.types).toHaveLength(1); - expect(result.fields).toHaveLength(1); - expect(result.methods).toHaveLength(1); - expect( - result.methods[0]?.body.anchors.map(({ opcode }) => opcode), - ).toContain("ldstr"); - expect(result.coverage).toEqual({ state: "complete", issues: [] }); - expect(result.user_strings).toMatchObject({ - state: "partial", - literals: [], - issues: [{ code: "resource-limit", scope: "metadata.#US" }], - }); - expect(result.limitations.join(" ")).toMatch(/literals are not examined/); - } finally { - spy.mockRestore(); - } +it("retains the admitted members when only #US decoding exceeds capacity", () => { + const values = ["small", "x".repeat(8_000)]; + const { heap, tokens } = fixtureUserStringHeap(values); + const bytes = buildManagedPeFixture({ + ilBody: ldstrBody(tokens), + userStringHeap: heap, }); + const target = managedPeFixtureTarget(bytes); + const complete = inspectManagedMembersBytes(bytes, target); + expect(complete.coverage).toEqual({ state: "complete", issues: [] }); + expect(complete.user_strings?.literals.map(({ value }) => value)).toEqual( + values, + ); + expect(complete.methods[0]?.body.anchors).toContainEqual( + expect.objectContaining({ opcode: "ldstr", operand: tokens[0] }), + ); - it("falls back to type-inventory recovery when members themselves exceed capacity", () => { - const { heap, tokens } = fixtureUserStringHeap(["small"]); - const bytes = buildManagedPeFixture({ - ilBody: ldstrBody(tokens), - userStringHeap: heap, - }); - const result = inspectManagedMembersBytes( - bytes, - managedPeFixtureTarget(bytes), - { maxRepresentationBytes: 6_000 }, - ); - - expect(result.types).toHaveLength(1); - expect(result.methods).toEqual([]); - expect(result.user_strings).toBeNull(); - expect(result.coverage).toMatchObject({ + const limitedLiterals = inspectManagedMembersBytes(bytes, target, { + maxRepresentationBytes: 40_000, + }); + expect(limitedLiterals).toMatchObject({ + types: complete.types, + fields: complete.fields, + methods: complete.methods, + coverage: complete.coverage, + user_strings: { state: "partial", - issues: [ - expect.objectContaining({ - code: "resource-limit", - scope: "metadata.representation", - }), - ], - }); + literals: [], + issues: [{ code: "resource-limit", scope: "metadata.#US" }], + }, + }); + expect(limitedLiterals.limitations.join(" ")).toMatch( + /literals are not examined/, + ); + + const limitedMembers = inspectManagedMembersBytes(bytes, target, { + maxRepresentationBytes: 6_000, + }); + expect(limitedMembers.types).toEqual(complete.types); + expect(limitedMembers.methods).toEqual([]); + expect(limitedMembers.user_strings).toBeNull(); + expect(limitedMembers.coverage).toMatchObject({ + state: "partial", + issues: [{ code: "resource-limit", scope: "metadata.representation" }], }); }); diff --git a/src/dotnet/ManagedMemberInspector.ts b/src/dotnet/ManagedMemberInspector.ts index 1a18a0a6a0..68c775d436 100644 --- a/src/dotnet/ManagedMemberInspector.ts +++ b/src/dotnet/ManagedMemberInspector.ts @@ -1,3 +1,4 @@ +import { isManagedUserStringLoad } from "../domain/managed/managedMemberInstructionFacts.js"; import { admitManagedProjection, managedDecodeBudget, @@ -365,7 +366,8 @@ export const inspectManagedMembersBytes = ( // retain their cores; only missing edge targets need the recovery pass. if (selected !== null) for (const { tokenInstructions } of methods.methods) - for (const { reference } of tokenInstructions) { + for (const { opcode, reference } of tokenInstructions) { + if (isManagedUserStringLoad(opcode)) continue; if (needsCore(reference)) referenced.set(reference.token, reference); else if ( reference.table === 0x2b && diff --git a/src/dotnet/ManagedMethodSpecs.ts b/src/dotnet/ManagedMethodSpecs.ts index 5408616493..eddcb14186 100644 --- a/src/dotnet/ManagedMethodSpecs.ts +++ b/src/dotnet/ManagedMethodSpecs.ts @@ -1,3 +1,4 @@ +import { isManagedUserStringLoad } from "../domain/managed/managedMemberInstructionFacts.js"; import { readMemberReferenceRowFacts } from "../domain/managed/managedMemberReferenceRows.js"; import { readMethodDefinitionRow } from "./ManagedMethodDefinitionRows.js"; import { createHash } from "node:crypto"; @@ -77,8 +78,13 @@ export const edgeTargets = ( // Generic arity by method token, so instantiations of one method decode it once. const arities = new Map(); for (const { tokenInstructions } of methods) - for (const { reference } of tokenInstructions) { - if (reference.table !== 0x2b || seen.has(reference.token)) continue; + for (const { opcode, reference } of tokenInstructions) { + if ( + reference.table !== 0x2b || + isManagedUserStringLoad(opcode) || + seen.has(reference.token) + ) + continue; seen.add(reference.token); readManagedValue( () => diff --git a/src/dotnet/ManagedUserStrings.ts b/src/dotnet/ManagedUserStrings.ts index b6a2ab90d4..5c28c6b686 100644 --- a/src/dotnet/ManagedUserStrings.ts +++ b/src/dotnet/ManagedUserStrings.ts @@ -1,3 +1,4 @@ +import { isManagedUserStringLoad } from "../domain/managed/managedMemberInstructionFacts.js"; import { admitManagedProjection, managedDecodeBudget, @@ -65,7 +66,7 @@ export const parseUserStrings = ( // Only ldstr loads a literal; a malformed ldtoken can carry a 0x70 token. for (const { tokenInstructions } of methods) for (const { opcode, reference } of tokenInstructions) - if (opcode === "ldstr" && !tokens.has(reference.raw)) { + if (isManagedUserStringLoad(opcode) && !tokens.has(reference.raw)) { budget.admitTransient(USER_STRING_TOKEN_BYTES, null); tokens.set(reference.raw, reference); } diff --git a/tests/acceptance/managedMemberRecovery.test.ts b/tests/acceptance/managedMemberRecovery.test.ts index df16029dd5..5efef5ad81 100644 --- a/tests/acceptance/managedMemberRecovery.test.ts +++ b/tests/acceptance/managedMemberRecovery.test.ts @@ -40,6 +40,26 @@ const createTransport = () => stderr: "pipe", }); +const expectDefaultMemberRecovery = (value: unknown) => { + const report = managedMemberInspectionSchema.parse(value); + expect(report.coverage).toMatchObject({ + state: "partial", + issues: [ + { + code: "resource-limit", + scope: "metadata.representation", + limit: { + parameter: "max_metadata_representation_bytes", + effective_value: 64 * 1024 * 1024, + }, + }, + ], + }); + expect(report.types).toHaveLength(3); + expect(report.methods).toEqual([]); + return report; +}; + cliTest( "CLI and stdio MCP recover type discovery and preserve partial retained pages", async ({ cli }) => { @@ -87,12 +107,7 @@ cliTest( ); expect(fileIdentity(mcpEvidence.subject).sha256).toBe(sha256); expect(fileIdentity(cliEvidence.subject).sha256).toBe(sha256); - const report = managedMemberInspectionSchema.parse( - mcpEvidence.normalized_result, - ); - expect(report.coverage.state).toBe("partial"); - expect(report.types).toHaveLength(3); - expect(report.methods).toEqual([]); + const report = expectDefaultMemberRecovery(mcpEvidence.normalized_result); const empty = report.types.find((type) => type.method_list.count === 0); if (empty === undefined) throw new Error("Missing empty fixture type"); const selectedCli = await cli.run({