fix(managed): preserve Module identity with an unresolved name (#12265)

Admit exact string heap aliases and retain independent Module row facts for distinct declarations. Bind the optional partial row to the same scope checks as named Module Evidence.
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 06:08:59 -05:00
1 parent 519547d022
commit a4bf59f661
35 files changed
+712 -350

No files matched your search

@@ -0,0 +1,184 @@
import { expect, it } from "vitest";
import {
managedStringsAmbiguity,
STRING_AMBIGUITY_MVID,
} from "../../tests/fixtures/managedStringsAmbiguity.js";
import { validateManagedTokenScope } from "../domain/managed/managedInspectionEvidence.js";
import { compareManagedMembers } from "../domain/managed/managedMemberComparison.js";
import { inspectManagedArtifactBytes } from "./ManagedArtifactInspector.js";
import { inspectManagedMembersBytes } from "./ManagedMemberInspector.js";
import { inspectManagedNativeBoundariesBytes } from "./ManagedNativeBoundaryInspector.js";
import {
buildManagedPeFixture,
managedPeFixtureTarget,
} from "./ManagedPe.fixture.js";
it.each(["forward", "reverse"] as const)(
"keeps the unnamed Module row and its GUIDs with %s #Strings declarations",
(variant) => {
const { bytes, nameIndex, duplicateHeader } =
managedStringsAmbiguity(variant);
const target = managedPeFixtureTarget(bytes);
const artifact = inspectManagedArtifactBytes(bytes, target);
const members = inspectManagedMembersBytes(bytes, target, {
typeNames: ["Fixture.Program"],
});
const boundaries = inspectManagedNativeBoundariesBytes(bytes, target);
const unnamed = {
name: null,
name_index: nameIndex,
row_offset: expect.any(Number),
token: "0x00000001",
generation: 2,
mvid: STRING_AMBIGUITY_MVID,
enc_id: STRING_AMBIGUITY_MVID,
enc_base_id: null,
};
for (const report of [artifact, members, boundaries]) {
expect(report).toMatchObject({
module: null,
metadata: {
status: "partial",
table_row_counts: { Module: 1 },
unresolved_module_name: unnamed,
},
coverage: {
state: "partial",
issues: [
{
code: "invalid-stream",
scope: "metadata.#Strings",
offset: duplicateHeader,
detail: expect.stringContaining("distinct byte ranges"),
},
],
},
});
expect(
validateManagedTokenScope(
report,
"managed inspection",
"normalized result",
).ok,
).toBe(true);
}
expect(members.identity_scope.requires_mvid).toBe(STRING_AMBIGUITY_MVID);
expect(boundaries.identity_scope.requires_mvid).toBe(STRING_AMBIGUITY_MVID);
expect(members.types).toEqual([]);
expect(members.methods).toEqual([]);
},
);
it.each([
{ variant: "alias", nilName: false, expected: "Fixture.dll" },
{ variant: "forward", nilName: true, expected: "" },
] as const)(
"keeps the existing Module shape when its name is established ($variant)",
({ variant, nilName, expected }) => {
const { bytes } = managedStringsAmbiguity(variant, { nilName });
const report = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(report.module).toMatchObject({
name: expected,
mvid: STRING_AMBIGUITY_MVID,
});
expect(report.metadata).not.toHaveProperty("unresolved_module_name");
expect(report.identity_scope.requires_mvid).toBe(STRING_AMBIGUITY_MVID);
},
);
it("does not derive a token scope from conflicting GUID spans or multiple Module rows", () => {
for (const options of [{ ambiguousGuid: true }, { multipleModules: true }]) {
const { bytes } = managedStringsAmbiguity("forward", options);
const report = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(report.module).toBeNull();
expect(report.identity_scope.requires_mvid).toBeNull();
if (options.ambiguousGuid === true)
expect(report.metadata.unresolved_module_name).toMatchObject({
mvid: null,
enc_id: null,
});
else expect(report.metadata).not.toHaveProperty("unresolved_module_name");
}
});
it("keeps an independent MVID beside an invalid ENC index", () => {
const { bytes } = managedStringsAmbiguity("forward", { invalidEnc: true });
const report = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(report.metadata.unresolved_module_name).toMatchObject({
mvid: STRING_AMBIGUITY_MVID,
enc_id: null,
});
expect(report.coverage.issues).toContainEqual(
expect.objectContaining({
code: "invalid-guid",
scope: "metadata.#GUID",
}),
);
});
it("treats a duplicate #Strings extent outside the root as malformed", () => {
const { bytes, metadataEnd } = managedStringsAmbiguity("outside-root");
const report = inspectManagedArtifactBytes(
bytes,
managedPeFixtureTarget(bytes),
);
expect(report.metadata.status).toBe("malformed");
expect(report.metadata).not.toHaveProperty("unresolved_module_name");
expect(report.coverage.issues).toContainEqual({
code: "invalid-metadata-root",
scope: "metadata.#Strings",
offset: metadataEnd,
detail: "metadata.#Strings leaves the metadata byte range",
});
});
it("rejects conflicting Module projections while preserving known MVIDs in comparisons", () => {
const { bytes } = managedStringsAmbiguity("forward");
const unnamed = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes),
);
const namedBytes = buildManagedPeFixture();
const named = inspectManagedMembersBytes(
namedBytes,
managedPeFixtureTarget(namedBytes),
);
expect(named.metadata).not.toHaveProperty("unresolved_module_name");
for (const altered of [
{ ...unnamed, module: named.module },
{
...unnamed,
identity_scope: { ...unnamed.identity_scope, requires_mvid: null },
},
]) {
expect(
validateManagedTokenScope(altered, "inspect_managed_members", "altered")
.ok,
).toBe(false);
}
const comparison = compareManagedMembers(
{ evidenceId: "ev_" + "a".repeat(64), result: named },
{ evidenceId: "ev_" + "b".repeat(64), result: unnamed },
);
expect(comparison.right).toMatchObject({
mvid: STRING_AMBIGUITY_MVID,
module_name: null,
metadata_status: "partial",
});
expect(comparison.methods).toEqual([
expect.objectContaining({ status: "unknown" }),
]);
});