mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
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:
1 parent
519547d022
commit
a4bf59f661
35 files changed
+712
-350
No files matched your search
@@ -98,6 +98,15 @@ a decoded blob is omitted with partial coverage. A separate blob-backed fact
|
||||
such as a method's local-variable signature remains unknown when other facts
|
||||
about that method are available. An out-of-root stream declaration is malformed.
|
||||
|
||||
Repeated `#Strings` declarations for the same range keep the existing name
|
||||
facts and disclose the duplicate. Distinct ranges leave nonzero string
|
||||
indices unresolved. When there is exactly one Module row, its token, file
|
||||
offset, generation, raw name index, and independently readable GUIDs remain
|
||||
in `metadata.unresolved_module_name`. The usual `module` object is null in
|
||||
this case. Index zero still reads as an empty string. A known MVID can bound
|
||||
build-local token identity even when the Module name is unresolved. Unreadable
|
||||
names do not supply nominal member or type lookup keys.
|
||||
|
||||
If the metadata root declares both compressed (`#~`) and uncompressed (`#-`)
|
||||
tables, REA reports both declarations and leaves the row layout unresolved.
|
||||
The metadata version and PE/CLI evidence remain available. Member inventories,
|
||||
|
||||
@@ -375,12 +375,7 @@ const inspectPeFile = async (
|
||||
await setImmediate();
|
||||
abortIfNeeded(signal);
|
||||
}
|
||||
const reference = readAssemblyReferenceName(
|
||||
bytes,
|
||||
layout,
|
||||
index,
|
||||
layout.strings.size,
|
||||
);
|
||||
const reference = readAssemblyReferenceName(bytes, layout, index);
|
||||
if (
|
||||
reference.name !== "UnityEngine" &&
|
||||
!reference.name.startsWith("UnityEngine.")
|
||||
|
||||
@@ -6,6 +6,8 @@ import {
|
||||
managedArtifactInspectionSchema,
|
||||
managedMemberInspectionSchema,
|
||||
} from "../managed/managedArtifact.js";
|
||||
import { managedModuleRow } from "../managed/managedModuleIdentity.js";
|
||||
import { validateManagedTokenScope } from "../managed/managedInspectionEvidence.js";
|
||||
import { err, ok } from "../result.js";
|
||||
import { parseAnalysisViewResult } from "./analysisViewParent.js";
|
||||
import type { AnalysisViewParent } from "./analysisViewParent.js";
|
||||
@@ -49,6 +51,8 @@ export const managedViewContextSchema = z.strictObject({
|
||||
identity_scope: managedMemberInspectionSchema.shape.identity_scope,
|
||||
source_metadata_status:
|
||||
managedArtifactInspectionSchema.shape.metadata.shape.status,
|
||||
unresolved_module_name:
|
||||
managedArtifactInspectionSchema.shape.metadata.shape.unresolved_module_name,
|
||||
source_coverage: managedArtifactInspectionSchema.shape.coverage,
|
||||
});
|
||||
|
||||
@@ -95,10 +99,16 @@ export const projectManagedArtifactView = (
|
||||
);
|
||||
if (!parsed.ok) return parsed;
|
||||
const report = parsed.value;
|
||||
const scope = validateManagedTokenScope(
|
||||
report,
|
||||
"inspect_managed_artifact",
|
||||
parent.evidenceId,
|
||||
);
|
||||
if (!scope.ok) return scope;
|
||||
const identity_scope = {
|
||||
token_identity: "build-local" as const,
|
||||
requires_artifact_sha256: report.artifact.sha256,
|
||||
requires_mvid: report.module?.mvid ?? null,
|
||||
requires_mvid: managedModuleRow(report)?.mvid ?? null,
|
||||
};
|
||||
const shared = {
|
||||
parent_evidence_id: parent.evidenceId,
|
||||
@@ -136,6 +146,11 @@ export const projectManagedArtifactView = (
|
||||
...shared,
|
||||
identity_scope,
|
||||
source_metadata_status: report.metadata.status,
|
||||
...(report.metadata.unresolved_module_name === undefined
|
||||
? {}
|
||||
: {
|
||||
unresolved_module_name: report.metadata.unresolved_module_name,
|
||||
}),
|
||||
source_coverage: report.coverage,
|
||||
coverage: pageViewCoverage(view.offset, item.length, collection.length),
|
||||
});
|
||||
|
||||
@@ -377,6 +377,11 @@ export const projectManagedMemberView = (
|
||||
selection: report.selection,
|
||||
method_rows: report.method_rows,
|
||||
source_metadata_status: report.metadata.status,
|
||||
...(report.metadata.unresolved_module_name === undefined
|
||||
? {}
|
||||
: {
|
||||
unresolved_module_name: report.metadata.unresolved_module_name,
|
||||
}),
|
||||
source_coverage:
|
||||
view.collection === "user_strings" && report.user_strings !== null
|
||||
? {
|
||||
|
||||
@@ -198,6 +198,7 @@ export const managedMembersViewContextSchema = z.strictObject({
|
||||
selection: member.selection,
|
||||
method_rows: member.method_rows,
|
||||
source_metadata_status: member.metadata.shape.status,
|
||||
unresolved_module_name: member.metadata.shape.unresolved_module_name,
|
||||
source_coverage: member.coverage,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { managedModuleRow } from "./managedModuleIdentity.js";
|
||||
import { z } from "zod";
|
||||
|
||||
import type { Evidence } from "../evidence.js";
|
||||
@@ -375,11 +376,15 @@ const artifactMismatch = (
|
||||
const moduleIdentityMismatch = (
|
||||
parsed: ParsedManagedGraphInput,
|
||||
): string | null => {
|
||||
const modules = [parsed.artifact, parsed.members, parsed.boundaries].flatMap(
|
||||
(source) => {
|
||||
const row = source === null ? null : managedModuleRow(source.result);
|
||||
return row === null ? [] : [row];
|
||||
},
|
||||
);
|
||||
const mvidClaims = [
|
||||
parsed.artifact?.result.module?.mvid,
|
||||
parsed.members?.result.module?.mvid,
|
||||
...modules.map(({ mvid }) => mvid),
|
||||
parsed.members?.result.identity_scope.requires_mvid,
|
||||
parsed.boundaries?.result.module?.mvid,
|
||||
parsed.boundaries?.result.identity_scope.requires_mvid,
|
||||
]
|
||||
.filter((mvid): mvid is string => mvid !== undefined && mvid !== null)
|
||||
@@ -387,16 +392,10 @@ const moduleIdentityMismatch = (
|
||||
if (new Set(mvidClaims).size > 1)
|
||||
return "Managed graph inputs report conflicting known module MVIDs or token scopes";
|
||||
|
||||
const modules = [
|
||||
parsed.artifact?.result.module,
|
||||
parsed.members?.result.module,
|
||||
parsed.boundaries?.result.module,
|
||||
].filter(
|
||||
(module): module is NonNullable<ManagedArtifactInspection["module"]> =>
|
||||
module !== undefined && module !== null,
|
||||
);
|
||||
for (const field of ["name", "generation", "token", "row_offset"] as const) {
|
||||
const values = new Set(modules.map((module) => module[field]));
|
||||
const values = new Set(
|
||||
modules.map((module) => module[field]).filter((value) => value !== null),
|
||||
);
|
||||
if (values.size > 1)
|
||||
return `Managed graph inputs report conflicting module ${field} values`;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,12 @@ type NativeImplementation =
|
||||
* Display label for a metadata name. Metadata may hold an empty name; the
|
||||
* graph then has no label, while the node's properties keep the source value.
|
||||
*/
|
||||
const metadataLabel = (name: string): string | null =>
|
||||
import {
|
||||
managedModuleRow,
|
||||
type ManagedModuleRow,
|
||||
} from "./managedModuleIdentity.js";
|
||||
|
||||
const metadataLabel = (name: string | null): string | null =>
|
||||
name === "" ? null : name;
|
||||
|
||||
/** Options for a managed contains relationship edge. */
|
||||
@@ -138,7 +143,7 @@ export const addArtifactIdentityNodes = (
|
||||
};
|
||||
|
||||
interface ManagedModuleSource {
|
||||
readonly module: NonNullable<ManagedArtifactInspection["module"]>;
|
||||
readonly module: ManagedModuleRow;
|
||||
readonly evidence: Evidence;
|
||||
readonly coverageState: ManagedArtifactInspection["coverage"]["state"];
|
||||
}
|
||||
@@ -146,31 +151,25 @@ interface ManagedModuleSource {
|
||||
const selectModuleSource = (
|
||||
parsed: ParsedManagedGraphInput,
|
||||
): ManagedModuleSource | null => {
|
||||
const sources = [
|
||||
parsed.artifact?.result.module == null
|
||||
? null
|
||||
: {
|
||||
module: parsed.artifact.result.module,
|
||||
evidence: parsed.artifact.evidence,
|
||||
coverageState: parsed.artifact.result.coverage.state,
|
||||
},
|
||||
parsed.members?.result.module == null
|
||||
? null
|
||||
: {
|
||||
module: parsed.members.result.module,
|
||||
evidence: parsed.members.evidence,
|
||||
coverageState: parsed.members.result.coverage.state,
|
||||
},
|
||||
parsed.boundaries?.result.module == null
|
||||
? null
|
||||
: {
|
||||
module: parsed.boundaries.result.module,
|
||||
evidence: parsed.boundaries.evidence,
|
||||
coverageState: parsed.boundaries.result.coverage.state,
|
||||
},
|
||||
].filter((source): source is ManagedModuleSource => source !== null);
|
||||
const sources: ManagedModuleSource[] = [];
|
||||
for (const source of [parsed.artifact, parsed.members, parsed.boundaries]) {
|
||||
if (source === null) continue;
|
||||
const module = managedModuleRow(source.result);
|
||||
if (module !== null)
|
||||
sources.push({
|
||||
module,
|
||||
evidence: source.evidence,
|
||||
coverageState: source.result.coverage.state,
|
||||
});
|
||||
}
|
||||
return (
|
||||
sources.find(({ module }) => module.mvid !== null) ?? sources[0] ?? null
|
||||
sources.find(
|
||||
({ module }) => module.mvid !== null && module.name !== null,
|
||||
) ??
|
||||
sources.find(({ module }) => module.mvid !== null) ??
|
||||
sources.find(({ module }) => module.name !== null) ??
|
||||
sources[0] ??
|
||||
null
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -56,16 +56,32 @@ const assemblyIdentitySchema = z.object({
|
||||
row_offset: offsetSchema,
|
||||
});
|
||||
|
||||
const moduleIdentitySchema = z.object({
|
||||
name: z.string(),
|
||||
const moduleRowCoordinates = {
|
||||
generation: z.int().min(0).max(0xffff),
|
||||
mvid: cliMetadataGuidSchema.nullable(),
|
||||
enc_id: cliMetadataGuidSchema.nullable(),
|
||||
enc_base_id: cliMetadataGuidSchema.nullable(),
|
||||
token: managedMetadataTokenSchema,
|
||||
row_offset: offsetSchema,
|
||||
};
|
||||
|
||||
const moduleIdentitySchema = z.object({
|
||||
name: z.string(),
|
||||
...moduleRowCoordinates,
|
||||
});
|
||||
|
||||
const unresolvedModuleNameSchema = z.strictObject({
|
||||
name: z.null(),
|
||||
name_index: z.int().min(1).max(0xffff_ffff),
|
||||
...moduleRowCoordinates,
|
||||
token: z.literal("0x00000001"),
|
||||
});
|
||||
|
||||
export type NamedManagedModule = z.infer<typeof moduleIdentitySchema>;
|
||||
export type UnresolvedManagedModuleName = z.infer<
|
||||
typeof unresolvedModuleNameSchema
|
||||
>;
|
||||
|
||||
/**
|
||||
* What a managed inspection established about the CLI metadata: none present,
|
||||
* read in full or in part, or present but unreadable.
|
||||
@@ -87,6 +103,7 @@ const managedMetadataSummarySchema = z.object({
|
||||
status: managedMetadataStatusSchema,
|
||||
version: z.string().nullable(),
|
||||
table_row_counts: z.record(z.string(), z.int().min(0).max(0xffff_ffff)),
|
||||
unresolved_module_name: unresolvedModuleNameSchema.exactOptional(),
|
||||
});
|
||||
|
||||
const managedTokenIdentityScopeSchema = z.object({
|
||||
@@ -532,11 +549,8 @@ export const managedArtifactInspectionSchema = z.object({
|
||||
}),
|
||||
),
|
||||
}),
|
||||
metadata: z.object({
|
||||
status: managedMetadataStatusSchema,
|
||||
version: z.string().nullable(),
|
||||
metadata: managedMetadataSummarySchema.extend({
|
||||
stream_names: z.array(z.string()),
|
||||
table_row_counts: z.record(z.string(), z.int().min(0).max(0xffff_ffff)),
|
||||
}),
|
||||
module: moduleIdentitySchema.nullable(),
|
||||
assembly: assemblyIdentitySchema.nullable(),
|
||||
|
||||
@@ -24,18 +24,13 @@ export class ManagedRepresentationFailure extends ManagedReaderFailure {
|
||||
}
|
||||
}
|
||||
|
||||
interface CachedString {
|
||||
readonly value: string;
|
||||
readonly encodedBytes: number;
|
||||
}
|
||||
|
||||
/** The layout owns decoding capacity and heap identity for all of its consumers. */
|
||||
export class ManagedDecodeBudget {
|
||||
readonly #bytes: Budget;
|
||||
/** Part of the charge that `releaseTransient` refunds. */
|
||||
#transient = 0;
|
||||
readonly #rows = new Map<number, Set<number>>();
|
||||
readonly #strings = new Map<number, CachedString>();
|
||||
readonly #strings = new Map<number, string>();
|
||||
readonly #digests = new WeakMap<ArrayBufferLike, Map<string, string>>();
|
||||
readonly #prepaid = new WeakSet<object>();
|
||||
|
||||
@@ -109,12 +104,12 @@ export class ManagedDecodeBudget {
|
||||
return this.#prepaid.delete(value);
|
||||
}
|
||||
|
||||
cachedString(index: number): CachedString | undefined {
|
||||
cachedString(index: number): string | undefined {
|
||||
return this.#strings.get(index);
|
||||
}
|
||||
|
||||
cacheString(index: number, value: string, encodedBytes: number): void {
|
||||
this.#strings.set(index, { value, encodedBytes });
|
||||
cacheString(index: number, value: string): void {
|
||||
this.#strings.set(index, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,19 @@ import { readEvidence, readEvidencePayload } from "../evidenceValidation.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import type { z } from "zod";
|
||||
|
||||
import {
|
||||
managedModuleRow,
|
||||
type ManagedModuleEvidenceFacts,
|
||||
} from "./managedModuleIdentity.js";
|
||||
|
||||
interface ManagedScopedEvidenceFacts extends ManagedModuleEvidenceFacts {
|
||||
readonly artifact: { readonly sha256: string };
|
||||
readonly identity_scope?: {
|
||||
readonly requires_artifact_sha256: string;
|
||||
readonly requires_mvid: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
/** Build-local metadata tokens join only under a known, matching module MVID. */
|
||||
export const managedTokenScopesMatch = (
|
||||
leftMvid: string | null,
|
||||
@@ -19,14 +32,7 @@ export const managedTokenScopesMatch = (
|
||||
* A null subject remains valid and means there is no digest to cross-check.
|
||||
*/
|
||||
export const readManagedInspectionEvidence = <
|
||||
Payload extends {
|
||||
readonly artifact: { readonly sha256: string };
|
||||
readonly identity_scope?: {
|
||||
readonly requires_artifact_sha256: string;
|
||||
readonly requires_mvid: string | null;
|
||||
};
|
||||
readonly module?: { readonly mvid: string | null } | null;
|
||||
},
|
||||
Payload extends ManagedScopedEvidenceFacts,
|
||||
>(
|
||||
rawEvidence: unknown,
|
||||
operation: string,
|
||||
@@ -77,14 +83,7 @@ export const readManagedInspectionEvidence = <
|
||||
|
||||
/** Bind recorded token scope to the artifact and module that gave it meaning. */
|
||||
export const validateManagedTokenScope = (
|
||||
result: {
|
||||
readonly artifact: { readonly sha256: string };
|
||||
readonly identity_scope?: {
|
||||
readonly requires_artifact_sha256: string;
|
||||
readonly requires_mvid: string | null;
|
||||
};
|
||||
readonly module?: { readonly mvid: string | null } | null;
|
||||
},
|
||||
result: ManagedScopedEvidenceFacts,
|
||||
operation: string,
|
||||
evidenceId: string,
|
||||
): Result<void, EvidenceIntegrityError> => {
|
||||
@@ -96,7 +95,19 @@ export const validateManagedTokenScope = (
|
||||
`Managed Evidence ${operation} (${evidenceId}) token scope SHA-256 ${scope.requires_artifact_sha256} does not match normalized artifact SHA-256 ${artifactSha256}`,
|
||||
),
|
||||
);
|
||||
const moduleMvid = result.module?.mvid ?? null;
|
||||
const metadata = result.metadata;
|
||||
if (
|
||||
metadata?.unresolved_module_name !== undefined &&
|
||||
(result.module != null ||
|
||||
metadata.table_row_counts.Module !== 1 ||
|
||||
metadata.status !== "partial")
|
||||
)
|
||||
return err(
|
||||
integrityFailure(
|
||||
`Managed Evidence ${operation} (${evidenceId}) has conflicting Module identity or coverage for an unresolved name`,
|
||||
),
|
||||
);
|
||||
const moduleMvid = managedModuleRow(result)?.mvid ?? null;
|
||||
const scopedMvid = scope?.requires_mvid;
|
||||
if (
|
||||
scopedMvid !== undefined &&
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { managedModuleRow } from "./managedModuleIdentity.js";
|
||||
import { joinCoverageStatus, type CoverageStatus } from "../coverage.js";
|
||||
import type {
|
||||
ManagedMemberComparisonResult,
|
||||
@@ -147,16 +148,19 @@ const resourceLimits = (
|
||||
|
||||
export const sideManifest = (
|
||||
side: ManagedMemberComparisonSide,
|
||||
): ManagedMemberComparisonResult["left"] => ({
|
||||
evidence_id: side.evidenceId,
|
||||
artifact_sha256: side.result.artifact.sha256,
|
||||
mvid: side.result.module?.mvid ?? null,
|
||||
module_name: side.result.module?.name ?? null,
|
||||
metadata_status: side.result.metadata.status,
|
||||
methods_total: side.result.methods.length,
|
||||
fields_total: side.result.fields.length,
|
||||
resource_limits: resourceLimits(side.result),
|
||||
});
|
||||
): ManagedMemberComparisonResult["left"] => {
|
||||
const module = managedModuleRow(side.result);
|
||||
return {
|
||||
evidence_id: side.evidenceId,
|
||||
artifact_sha256: side.result.artifact.sha256,
|
||||
mvid: module?.mvid ?? null,
|
||||
module_name: module?.name ?? null,
|
||||
metadata_status: side.result.metadata.status,
|
||||
methods_total: side.result.methods.length,
|
||||
fields_total: side.result.fields.length,
|
||||
resource_limits: resourceLimits(side.result),
|
||||
};
|
||||
};
|
||||
|
||||
export const buildComparisonSummary = (
|
||||
items: readonly (
|
||||
|
||||
@@ -189,12 +189,11 @@ const readTypeDefinition = (
|
||||
const { nameIndex, namespaceIndex } = source;
|
||||
// Keep both independent name failures, but decode each heap coordinate once.
|
||||
const namespace = readManagedValue(
|
||||
() =>
|
||||
readMetadataString(bytes, layout, namespaceIndex, layout.strings.size),
|
||||
() => readMetadataString(bytes, layout, namespaceIndex),
|
||||
issues,
|
||||
);
|
||||
const name = readManagedValue(
|
||||
() => readMetadataString(bytes, layout, nameIndex, layout.strings.size),
|
||||
() => readMetadataString(bytes, layout, nameIndex),
|
||||
issues,
|
||||
);
|
||||
return {
|
||||
|
||||
@@ -90,7 +90,11 @@ describe("managed metadata heap entries", () => {
|
||||
size: bytes.length,
|
||||
version: "",
|
||||
streamNames: ["#Blob"],
|
||||
strings: { name: "#Strings", offset: 0, size: 0 },
|
||||
strings: {
|
||||
kind: "available",
|
||||
stream: { name: "#Strings", offset: 0, size: 0 },
|
||||
issue: null,
|
||||
},
|
||||
guid: {
|
||||
kind: "available",
|
||||
stream: { name: "#GUID", offset: 0, size: 0 },
|
||||
@@ -173,7 +177,11 @@ it("does not re-charge the decode budget on metadata string cache hits", () => {
|
||||
size: strBytes.length,
|
||||
version: "",
|
||||
streamNames: ["#Strings"],
|
||||
strings: { name: "#Strings", offset: 0, size: strBytes.length },
|
||||
strings: {
|
||||
kind: "available",
|
||||
stream: { name: "#Strings", offset: 0, size: strBytes.length },
|
||||
issue: null,
|
||||
},
|
||||
guid: {
|
||||
kind: "available",
|
||||
stream: { name: "#GUID", offset: 0, size: 0 },
|
||||
@@ -191,10 +199,8 @@ it("does not re-charge the decode budget on metadata string cache hits", () => {
|
||||
codedIndexSize: () => 2,
|
||||
} as unknown as ManagedMetadataLayout;
|
||||
|
||||
const first = readMetadataString(strBytes, layout, 1, layout.strings.size);
|
||||
const first = readMetadataString(strBytes, layout, 1);
|
||||
expect(first).toHaveLength(strLen);
|
||||
|
||||
expect(() =>
|
||||
readMetadataString(strBytes, layout, 1, layout.strings.size),
|
||||
).not.toThrow();
|
||||
expect(() => readMetadataString(strBytes, layout, 1)).not.toThrow();
|
||||
});
|
||||
@@ -151,25 +151,23 @@ export const readMetadataString = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
index: number,
|
||||
heapExtent: number,
|
||||
): string => {
|
||||
if (index === 0) return "";
|
||||
if (index < 0 || index >= layout.strings.size)
|
||||
const declaration = layout.strings;
|
||||
if (declaration.kind === "ambiguous")
|
||||
throw new ManagedHeapAmbiguityFailure(declaration.issue);
|
||||
const heap = declaration.stream;
|
||||
if (index < 0 || index >= heap.size)
|
||||
throw managedFailure(
|
||||
"invalid-heap-index",
|
||||
"metadata.#Strings",
|
||||
"String heap index is outside #Strings",
|
||||
layout.strings.offset + Math.max(index, 0),
|
||||
heap.offset + Math.max(index, 0),
|
||||
);
|
||||
const start = layout.strings.offset + index;
|
||||
const start = heap.offset + index;
|
||||
const cached = managedDecodeBudget(layout).cachedString(index);
|
||||
if (cached !== undefined && cached.encodedBytes <= heapExtent) {
|
||||
return cached.value;
|
||||
}
|
||||
const maximum = Math.min(
|
||||
layout.strings.offset + layout.strings.size,
|
||||
start + heapExtent + 1,
|
||||
);
|
||||
if (cached !== undefined) return cached;
|
||||
const maximum = heap.offset + heap.size;
|
||||
let end = start;
|
||||
while (end < maximum && bytes[end] !== 0) end += 1;
|
||||
if (end === maximum)
|
||||
@@ -182,7 +180,7 @@ export const readMetadataString = (
|
||||
managedDecodeBudget(layout).reserveString(end - start, start);
|
||||
try {
|
||||
const value = decodeStrictUtf8(bytes.subarray(start, end), "preserve");
|
||||
managedDecodeBudget(layout).cacheString(index, value, end - start);
|
||||
managedDecodeBudget(layout).cacheString(index, value);
|
||||
return value;
|
||||
} catch (cause: unknown) {
|
||||
void cause;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import type {
|
||||
ManagedArtifactInspection,
|
||||
ManagedParseIssue,
|
||||
NamedManagedModule,
|
||||
UnresolvedManagedModuleName,
|
||||
} from "./managedArtifact.js";
|
||||
import {
|
||||
METADATA_TABLE_NAMES,
|
||||
@@ -25,7 +27,6 @@ import {
|
||||
} from "./managedReaderFailure.js";
|
||||
import type { ManagedPeLayout } from "./managedPeReader.js";
|
||||
|
||||
type ModuleIdentity = NonNullable<ManagedArtifactInspection["module"]>;
|
||||
type AssemblyIdentity = NonNullable<ManagedArtifactInspection["assembly"]>;
|
||||
type AssemblyReference = ManagedArtifactInspection["references"][number];
|
||||
type ManagedResource = ManagedArtifactInspection["resources"][number];
|
||||
@@ -55,7 +56,8 @@ export const readManagedResourceDirectory = (
|
||||
};
|
||||
|
||||
export interface ManagedMetadataInventory {
|
||||
readonly module: ModuleIdentity | null;
|
||||
readonly module: NamedManagedModule | null;
|
||||
readonly unresolvedModuleName: UnresolvedManagedModuleName | null;
|
||||
readonly assembly: AssemblyIdentity | null;
|
||||
readonly targetFrameworks: readonly string[];
|
||||
readonly referenceNames: readonly string[];
|
||||
@@ -105,17 +107,21 @@ const validateIdentityTableCounts = (
|
||||
export const readManagedModuleIdentity = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
): Pick<ManagedMetadataInventory, "module" | "issues"> => {
|
||||
): Pick<
|
||||
ManagedMetadataInventory,
|
||||
"module" | "unresolvedModuleName" | "issues"
|
||||
> => {
|
||||
const issues: ManagedParseIssue[] =
|
||||
layout.guid.issue === null ? [] : [layout.guid.issue];
|
||||
if (layout.blob?.issue) issues.push(layout.blob.issue);
|
||||
if (layout.strings.issue !== null) issues.push(layout.strings.issue);
|
||||
validateIdentityTableCounts(layout, issues);
|
||||
const module =
|
||||
readManagedValue(
|
||||
() => readModule(bytes, layout, layout.strings.size),
|
||||
issues,
|
||||
) ?? null;
|
||||
return { module, issues };
|
||||
const row = readManagedValue(() => readModule(bytes, layout, issues), issues);
|
||||
return {
|
||||
module: row?.name === null ? null : (row ?? null),
|
||||
unresolvedModuleName: row?.name === null ? row : null,
|
||||
issues,
|
||||
};
|
||||
};
|
||||
|
||||
const readReferences = (
|
||||
@@ -129,7 +135,7 @@ const readReferences = (
|
||||
const referenceNames = new Set<string>();
|
||||
const references = readRows(layout.table(35), (row) => {
|
||||
const observed = readManagedValue(
|
||||
() => readAssemblyReference(bytes, layout, row, layout.strings.size),
|
||||
() => readAssemblyReference(bytes, layout, row),
|
||||
issues,
|
||||
);
|
||||
if (observed === undefined) return undefined;
|
||||
@@ -202,18 +208,15 @@ export const readManagedMetadataInventory = (
|
||||
memberLists: TypeMemberListFacts,
|
||||
identity: Pick<
|
||||
ManagedMetadataInventory,
|
||||
"module" | "issues"
|
||||
"module" | "unresolvedModuleName" | "issues"
|
||||
> = readManagedModuleIdentity(bytes, layout),
|
||||
typeNames?: ManagedMetadataTypeNameLookup,
|
||||
): ManagedMetadataInventory => {
|
||||
const issues: ManagedParseIssue[] = [...memberLists.issues];
|
||||
appendManagedParseIssues(issues, identity.issues);
|
||||
const { module } = identity;
|
||||
const { module, unresolvedModuleName } = identity;
|
||||
const assembly =
|
||||
readManagedValue(
|
||||
() => readAssembly(bytes, layout, layout.strings.size),
|
||||
issues,
|
||||
) ?? null;
|
||||
readManagedValue(() => readAssembly(bytes, layout), issues) ?? null;
|
||||
const { references, referenceNames } = readReferences(bytes, layout, issues);
|
||||
const resources = readResources({
|
||||
bytes,
|
||||
@@ -230,6 +233,7 @@ export const readManagedMetadataInventory = (
|
||||
);
|
||||
return {
|
||||
module,
|
||||
unresolvedModuleName,
|
||||
assembly,
|
||||
targetFrameworks,
|
||||
referenceNames,
|
||||
@@ -240,6 +244,14 @@ export const readManagedMetadataInventory = (
|
||||
};
|
||||
};
|
||||
|
||||
/** Add row-local identity only when the checked Module row has an unresolved name. */
|
||||
export const unresolvedModuleMetadata = (
|
||||
identity: Pick<ManagedMetadataInventory, "unresolvedModuleName">,
|
||||
): { readonly unresolved_module_name?: UnresolvedManagedModuleName } =>
|
||||
identity.unresolvedModuleName === null
|
||||
? {}
|
||||
: { unresolved_module_name: identity.unresolvedModuleName };
|
||||
|
||||
/** Stable table-name/count projection for caller-visible coverage. */
|
||||
export const managedTableRowCounts = (
|
||||
layout: ManagedMetadataLayout,
|
||||
|
||||
@@ -11,6 +11,7 @@ import type { TypeMemberListFacts } from "./managedTypeMemberLists.js";
|
||||
import type {
|
||||
ManagedArtifactInspection,
|
||||
ManagedParseIssue,
|
||||
NamedManagedModule,
|
||||
} from "./managedArtifact.js";
|
||||
import {
|
||||
MANAGED_CODED_INDEXES,
|
||||
@@ -37,7 +38,7 @@ import type { ManagedTypeReferenceLookup } from "./managedTypeReferences.js";
|
||||
import type { ManagedResourceDirectory } from "./managedMetadataInventory.js";
|
||||
import type { ManagedType, parseTypes } from "./managedMemberInspectorCore.js";
|
||||
|
||||
type ModuleIdentity = NonNullable<ManagedArtifactInspection["module"]>;
|
||||
import type { ManagedModuleRow } from "./managedModuleIdentity.js";
|
||||
type AssemblyIdentity = NonNullable<ManagedArtifactInspection["assembly"]>;
|
||||
type AssemblyReference = ManagedArtifactInspection["references"][number];
|
||||
type ManagedResource = ManagedArtifactInspection["resources"][number];
|
||||
@@ -70,48 +71,49 @@ const publicKeyIdentity = (
|
||||
export const readModule = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
): ModuleIdentity | null => {
|
||||
issues: ManagedParseIssue[],
|
||||
): ManagedModuleRow | null => {
|
||||
const table = layout.table(0);
|
||||
if (table === undefined || table.rowCount === 0) return null;
|
||||
const cursor = metadataRowCursor(bytes, layout, 0, 1);
|
||||
const generation = cursor.u16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
const mvid = readMetadataGuid(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.guidIndexSize),
|
||||
);
|
||||
const encId = readMetadataGuid(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.guidIndexSize),
|
||||
);
|
||||
const encBaseId = readMetadataGuid(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.guidIndexSize),
|
||||
);
|
||||
const nameIndex = cursor.readIndex(layout.stringIndexSize);
|
||||
const mvidIndex = cursor.readIndex(layout.guidIndexSize);
|
||||
const encIndex = cursor.readIndex(layout.guidIndexSize);
|
||||
const encBaseIndex = cursor.readIndex(layout.guidIndexSize);
|
||||
|
||||
const readGuid = (index: number) =>
|
||||
readMetadataGuid(bytes, layout, index) ?? null;
|
||||
if (layout.strings.kind === "ambiguous" && nameIndex !== 0) {
|
||||
// Multiple Module rows cannot establish a new exact token scope.
|
||||
if (table.rowCount !== 1) return null;
|
||||
const independentGuid = (index: number) =>
|
||||
readManagedValue(() => readGuid(index), issues) ?? null;
|
||||
return {
|
||||
name: null,
|
||||
name_index: nameIndex,
|
||||
generation,
|
||||
mvid: independentGuid(mvidIndex),
|
||||
enc_id: independentGuid(encIndex),
|
||||
enc_base_id: independentGuid(encBaseIndex),
|
||||
token: "0x00000001",
|
||||
row_offset: table.offset,
|
||||
};
|
||||
}
|
||||
return {
|
||||
name,
|
||||
name: readMetadataString(bytes, layout, nameIndex),
|
||||
generation,
|
||||
mvid: mvid ?? null,
|
||||
enc_id: encId ?? null,
|
||||
enc_base_id: encBaseId ?? null,
|
||||
mvid: readGuid(mvidIndex),
|
||||
enc_id: readGuid(encIndex),
|
||||
enc_base_id: readGuid(encBaseIndex),
|
||||
token: metadataToken(0, 1),
|
||||
row_offset: table.offset,
|
||||
};
|
||||
} satisfies NamedManagedModule;
|
||||
};
|
||||
|
||||
export const readAssembly = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
): AssemblyIdentity | null => {
|
||||
const table = layout.table(32);
|
||||
if (table === undefined || table.rowCount === 0) return null;
|
||||
@@ -130,13 +132,11 @@ export const readAssembly = (
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
const culture = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
return {
|
||||
name,
|
||||
@@ -178,11 +178,10 @@ export const readAssemblyReferenceName = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
row: number,
|
||||
heapExtent: number,
|
||||
): Pick<AssemblyReference, "name" | "token" | "row_offset"> => {
|
||||
const { nameIndex } = assemblyReferenceHeader(bytes, layout, row);
|
||||
return {
|
||||
name: readMetadataString(bytes, layout, nameIndex, heapExtent),
|
||||
name: readMetadataString(bytes, layout, nameIndex),
|
||||
token: metadataToken(35, row),
|
||||
row_offset: metadataRowOffset(layout, 35, row),
|
||||
};
|
||||
@@ -192,18 +191,16 @@ export const readAssemblyReference = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
row: number,
|
||||
heapExtent: number,
|
||||
): { readonly name: string; readonly reference: AssemblyReference | null } => {
|
||||
const { cursor, version, flags, keyIndex, nameIndex } =
|
||||
assemblyReferenceHeader(bytes, layout, row);
|
||||
const name = readMetadataString(bytes, layout, nameIndex, heapExtent);
|
||||
const name = readMetadataString(bytes, layout, nameIndex);
|
||||
try {
|
||||
const keyOrToken = readMetadataBlob(bytes, layout, keyIndex);
|
||||
const culture = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
const hashValue = readMetadataBlob(
|
||||
bytes,
|
||||
@@ -319,18 +316,8 @@ export const managedMetadataTypeNames = (
|
||||
} else {
|
||||
const source = readTypeDefinitionRowFacts(bytes, layout, row);
|
||||
flags = source.flags;
|
||||
name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
source.nameIndex,
|
||||
layout.strings.size,
|
||||
);
|
||||
namespace = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
source.namespaceIndex,
|
||||
layout.strings.size,
|
||||
);
|
||||
name = readMetadataString(bytes, layout, source.nameIndex);
|
||||
namespace = readMetadataString(bytes, layout, source.namespaceIndex);
|
||||
}
|
||||
parentRow = nesting.enclosing.get(row) ?? null;
|
||||
const isNested = (flags & 0x7) >= 2;
|
||||
@@ -542,7 +529,6 @@ export const readResource = ({
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const implementationOffset = cursor.offset;
|
||||
const implementationRaw = cursor.readIndex(
|
||||
|
||||
@@ -81,7 +81,7 @@ type MetadataHeap =
|
||||
}
|
||||
| { readonly kind: "ambiguous"; readonly issue: ManagedParseIssue };
|
||||
|
||||
type TrackedHeapName = "#GUID" | "#US" | "#Blob";
|
||||
type TrackedHeapName = "#GUID" | "#US" | "#Blob" | "#Strings";
|
||||
|
||||
export interface MetadataTableLayout {
|
||||
readonly index: number;
|
||||
@@ -96,7 +96,7 @@ export interface ManagedMetadataLayout {
|
||||
readonly size: number;
|
||||
readonly version: string;
|
||||
readonly streamNames: readonly string[];
|
||||
readonly strings: MetadataStream;
|
||||
readonly strings: MetadataHeap;
|
||||
readonly guid: MetadataHeap;
|
||||
/** Empty blob heaps may omit the stream; index zero remains the empty blob. */
|
||||
readonly blob: MetadataHeap | null;
|
||||
@@ -194,20 +194,6 @@ const readStreamName = (
|
||||
};
|
||||
};
|
||||
|
||||
const requireStream = (
|
||||
streams: ReadonlyMap<string, MetadataStream>,
|
||||
name: string,
|
||||
): MetadataStream => {
|
||||
const stream = streams.get(name);
|
||||
if (stream === undefined)
|
||||
throw managedFailure(
|
||||
"missing-stream",
|
||||
`metadata.${name}`,
|
||||
`Required metadata stream ${name} is absent`,
|
||||
);
|
||||
return stream;
|
||||
};
|
||||
|
||||
const codedSize = (
|
||||
name: CodedIndexName,
|
||||
rowCounts: readonly number[],
|
||||
@@ -275,6 +261,7 @@ const readMetadataStreams = (
|
||||
"#GUID": [],
|
||||
"#US": [],
|
||||
"#Blob": [],
|
||||
"#Strings": [],
|
||||
};
|
||||
for (let index = 0; index < streamCount; index += 1) {
|
||||
requireRange(bytes, rootOffset, cursor, 8, "metadata.stream-header");
|
||||
@@ -283,7 +270,10 @@ const readMetadataStreams = (
|
||||
const streamSize = bytes.readUInt32LE(cursor + 4);
|
||||
const named = readStreamName(bytes, rootOffset, cursor + 8);
|
||||
const trackedHeap =
|
||||
named.name === "#GUID" || named.name === "#US" || named.name === "#Blob"
|
||||
named.name === "#GUID" ||
|
||||
named.name === "#US" ||
|
||||
named.name === "#Blob" ||
|
||||
named.name === "#Strings"
|
||||
? named.name
|
||||
: null;
|
||||
if (trackedHeap === null && streamNames.has(named.name))
|
||||
@@ -348,6 +338,7 @@ const resolveHeap = (
|
||||
"#GUID": "Nonzero GUID indices are unresolved.",
|
||||
"#US": "User-string literals are unresolved.",
|
||||
"#Blob": "Nonzero blob indices are unresolved.",
|
||||
"#Strings": "Nonzero string indices are unresolved.",
|
||||
}[name];
|
||||
const issue: ManagedParseIssue = {
|
||||
code: "invalid-stream",
|
||||
@@ -578,7 +569,13 @@ export const readManagedMetadataLayout = (
|
||||
valid,
|
||||
tablesOffset,
|
||||
});
|
||||
const strings = requireStream(streams, "#Strings");
|
||||
const strings = resolveHeap("#Strings", heapDeclarations["#Strings"]);
|
||||
if (strings === null)
|
||||
throw managedFailure(
|
||||
"missing-stream",
|
||||
"metadata.#Strings",
|
||||
"Required metadata stream #Strings is absent",
|
||||
);
|
||||
const guid = resolveHeap("#GUID", heapDeclarations["#GUID"]);
|
||||
if (guid === null)
|
||||
throw managedFailure(
|
||||
|
||||
@@ -132,7 +132,7 @@ describe.each(memberLists)("$name metadata layout", (members) => {
|
||||
const offset = metadataRowOffset(layout, members.target, 1);
|
||||
expect(offset).toBe(targetOffset);
|
||||
const name = bytes.readUInt16LE(offset + members.nameOffset);
|
||||
expect(readMetadataString(bytes, layout, name, 32)).toBe("subject");
|
||||
expect(readMetadataString(bytes, layout, name)).toBe("subject");
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import type {
|
||||
NamedManagedModule,
|
||||
UnresolvedManagedModuleName,
|
||||
} from "./managedArtifact.js";
|
||||
|
||||
export type ManagedModuleRow = NamedManagedModule | UnresolvedManagedModuleName;
|
||||
|
||||
export interface ManagedModuleEvidenceFacts {
|
||||
readonly module?: NamedManagedModule | null;
|
||||
readonly metadata?: {
|
||||
readonly unresolved_module_name?: UnresolvedManagedModuleName;
|
||||
readonly status: string;
|
||||
readonly table_row_counts: Readonly<Record<string, number>>;
|
||||
};
|
||||
}
|
||||
|
||||
/** Select the sole admitted Module row shape, independent of its name. */
|
||||
export const managedModuleRow = (
|
||||
result: ManagedModuleEvidenceFacts,
|
||||
): ManagedModuleRow | null =>
|
||||
result.module ?? result.metadata?.unresolved_module_name ?? null;
|
||||
@@ -1,3 +1,4 @@
|
||||
import { managedModuleRow } from "./managedModuleIdentity.js";
|
||||
import { digestCanonicalValue } from "../canonicalDigest.js";
|
||||
import { joinCoverageStatus } from "../coverage.js";
|
||||
import type { Evidence } from "../evidence.js";
|
||||
@@ -590,7 +591,7 @@ export const buildVerificationResult = ({
|
||||
evidence_id: managedEvidence.evidence_id,
|
||||
artifact_sha256: managed.artifact.sha256,
|
||||
artifact_path: managed.artifact.path,
|
||||
mvid: managed.module?.mvid ?? null,
|
||||
mvid: managedModuleRow(managed)?.mvid ?? null,
|
||||
metadata_status: managed.metadata.status,
|
||||
pinvoke_imports_total: managed.pinvoke_imports.length,
|
||||
native_implementations_total: managed.native_implementations.length,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { managedModuleRow } from "./managedModuleIdentity.js";
|
||||
import { z } from "zod";
|
||||
|
||||
import { evidenceSchema, type Evidence } from "../evidence.js";
|
||||
@@ -209,15 +210,18 @@ const buildReconstructionSummary = (
|
||||
const buildStaticObservation = (
|
||||
staticEvidence: Evidence,
|
||||
members: ManagedMemberInspection,
|
||||
): StaticObservation => ({
|
||||
evidence_id: staticEvidence.evidence_id,
|
||||
artifact_sha256: members.artifact.sha256,
|
||||
artifact_path: members.artifact.path,
|
||||
byte_length: members.artifact.byte_length,
|
||||
module_name: members.module?.name ?? null,
|
||||
mvid: members.module?.mvid ?? null,
|
||||
metadata_status: members.metadata.status,
|
||||
});
|
||||
): StaticObservation => {
|
||||
const module = managedModuleRow(members);
|
||||
return {
|
||||
evidence_id: staticEvidence.evidence_id,
|
||||
artifact_sha256: members.artifact.sha256,
|
||||
artifact_path: members.artifact.path,
|
||||
byte_length: members.artifact.byte_length,
|
||||
module_name: module?.name ?? null,
|
||||
mvid: module?.mvid ?? null,
|
||||
metadata_status: members.metadata.status,
|
||||
};
|
||||
};
|
||||
|
||||
const buildDecompilerRecord = (
|
||||
decompiler: ManagedReconstructionImportInput["decompiler"],
|
||||
|
||||
@@ -120,18 +120,8 @@ export const managedTypeReferences = (
|
||||
const named =
|
||||
readManagedValue(
|
||||
() => ({
|
||||
namespace: readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
value.namespaceIndex,
|
||||
layout.strings.size,
|
||||
),
|
||||
name: readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
value.nameIndex,
|
||||
layout.strings.size,
|
||||
),
|
||||
namespace: readMetadataString(bytes, layout, value.namespaceIndex),
|
||||
name: readMetadataString(bytes, layout, value.nameIndex),
|
||||
enclosingRow: value.enclosingRow,
|
||||
scopeValid,
|
||||
}),
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
} from "../domain/managed/managedPeReader.js";
|
||||
import {
|
||||
managedTableRowCounts,
|
||||
unresolvedModuleMetadata,
|
||||
readManagedMetadataInventory,
|
||||
type ManagedMetadataInventory,
|
||||
readManagedResourceDirectory,
|
||||
@@ -439,6 +440,7 @@ export const inspectManagedArtifactBytes = (
|
||||
version: metadata.version,
|
||||
stream_names: metadata.streamNames,
|
||||
table_row_counts: managedTableRowCounts(metadata),
|
||||
...unresolvedModuleMetadata(inventory),
|
||||
},
|
||||
module: inventory.module,
|
||||
assembly: inventory.assembly,
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
duplicateManagedHeap as duplicateHeap,
|
||||
type DuplicateHeapVariant as Variant,
|
||||
} from "../../tests/fixtures/managedDuplicateMetadataHeap.js";
|
||||
import { readManagedMetadataLayout } from "../domain/managed/managedMetadataLayout.js";
|
||||
import { readManagedPeLayout } from "../domain/managed/managedPeReader.js";
|
||||
import { inspectManagedArtifactBytes } from "./ManagedArtifactInspector.js";
|
||||
@@ -14,78 +18,6 @@ import {
|
||||
const FIRST_MVID = "11111111-1111-1111-1111-111111111111";
|
||||
const SECOND_MVID = "22222222-2222-2222-2222-222222222222";
|
||||
|
||||
type Variant = "forward" | "reverse" | "alias" | "outside-root";
|
||||
|
||||
const duplicateHeap = (
|
||||
bytes: Buffer,
|
||||
name: "#GUID" | "#US" | "#Blob",
|
||||
alternateHeap: Buffer,
|
||||
variant: Variant,
|
||||
) => {
|
||||
const pe = readManagedPeLayout(bytes);
|
||||
if (pe.cli === null) throw new Error("Fixture omitted a CLI header");
|
||||
const size = pe.cli.metadata.size;
|
||||
const start = pe.rvaToOffset(pe.cli.metadata.rva, size, "fixture metadata");
|
||||
const original = Buffer.from(bytes.subarray(start, start + size));
|
||||
const streamCount = original.readUInt16LE(30);
|
||||
const headers: number[] = [];
|
||||
let heapHeader = -1;
|
||||
let end = 32;
|
||||
for (let index = 0; index < streamCount; index += 1) {
|
||||
headers.push(end);
|
||||
const terminator = original.indexOf(0, end + 8);
|
||||
if (original.toString("ascii", end + 8, terminator) === name)
|
||||
heapHeader = end;
|
||||
end = Math.ceil((terminator + 1) / 4) * 4;
|
||||
}
|
||||
if (heapHeader < 0) throw new Error(`Fixture omitted ${name}`);
|
||||
const headerSize = 8 + Math.ceil((name.length + 1) / 4) * 4;
|
||||
const alternate = size + headerSize;
|
||||
const extended = Buffer.alloc(
|
||||
variant === "alias" ? alternate : alternate + alternateHeap.byteLength,
|
||||
);
|
||||
original.copy(extended, 0, 0, end);
|
||||
original.copy(extended, end + headerSize, end);
|
||||
extended.writeUInt16LE(streamCount + 1, 30);
|
||||
for (const header of headers)
|
||||
extended.writeUInt32LE(original.readUInt32LE(header) + headerSize, header);
|
||||
const originalHeap = extended.readUInt32LE(heapHeader);
|
||||
const [first, second] = (
|
||||
{
|
||||
forward: [originalHeap, alternate],
|
||||
reverse: [alternate, originalHeap],
|
||||
alias: [originalHeap, originalHeap],
|
||||
"outside-root": [originalHeap, extended.length],
|
||||
} as const
|
||||
)[variant];
|
||||
if (variant !== "alias") alternateHeap.copy(extended, alternate);
|
||||
extended.writeUInt32LE(first, heapHeader);
|
||||
extended.writeUInt32LE(second, end);
|
||||
const originalSize = original.readUInt32LE(heapHeader + 4);
|
||||
extended.writeUInt32LE(
|
||||
variant === "reverse" ? alternateHeap.byteLength : originalSize,
|
||||
heapHeader + 4,
|
||||
);
|
||||
extended.writeUInt32LE(
|
||||
variant === "alias" || variant === "reverse"
|
||||
? originalSize
|
||||
: alternateHeap.byteLength,
|
||||
end + 4,
|
||||
);
|
||||
extended.write(name, end + 8);
|
||||
bytes.set(extended, start);
|
||||
bytes.writeUInt32LE(extended.length, pe.cli.headerOffset + 12);
|
||||
|
||||
return {
|
||||
bytes,
|
||||
duplicateHeader: start + end,
|
||||
firstHeader: start + heapHeader,
|
||||
firstRange: start + first,
|
||||
secondRange: start + second,
|
||||
metadataEnd: start + extended.length,
|
||||
};
|
||||
};
|
||||
|
||||
const duplicateGuidFixture = (variant: Variant) =>
|
||||
duplicateHeap(
|
||||
buildManagedPeFixture({ mvid: Buffer.alloc(16, 0x11) }),
|
||||
|
||||
@@ -277,7 +277,7 @@ export const inspectManagedMembersBytes = (
|
||||
target,
|
||||
bytes.length,
|
||||
layout,
|
||||
identity.module,
|
||||
identity,
|
||||
issues,
|
||||
);
|
||||
const unnamedTypes = types.reduce(
|
||||
|
||||
@@ -110,12 +110,7 @@ export const parseFields = (
|
||||
readManagedValue(() => {
|
||||
const { token, rowOffset, flags, nameIndex, signatureIndex } =
|
||||
readFieldDefinitionRow(bytes, layout, row);
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
nameIndex,
|
||||
layout.strings.size,
|
||||
);
|
||||
const name = readMetadataString(bytes, layout, nameIndex);
|
||||
const owned = owner ?? 0;
|
||||
core.set(token, {
|
||||
token,
|
||||
@@ -182,13 +177,11 @@ export const parseExportedTypes = (
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const namespace = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const implementationOffset = cursor.offset;
|
||||
const implementationRaw = cursor.readIndex(
|
||||
@@ -260,12 +253,7 @@ export const parseMemberRefs = (
|
||||
readManagedValue(() => {
|
||||
const { token, rowOffset, parentRaw, parent, nameIndex, signatureIndex } =
|
||||
readMemberReferenceRowFacts(bytes, layout, row);
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
nameIndex,
|
||||
layout.strings.size,
|
||||
);
|
||||
const name = readMetadataString(bytes, layout, nameIndex);
|
||||
const sig = readMetadataBlob(bytes, layout, signatureIndex);
|
||||
if (parent.kind !== "valid")
|
||||
issues.push({
|
||||
@@ -407,12 +395,7 @@ export const parseMethods = ({
|
||||
signatureIndex,
|
||||
implementation,
|
||||
} = readMethodDefinitionRow(bytes, layout, row);
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
nameIndex,
|
||||
layout.strings.size,
|
||||
);
|
||||
const name = readMetadataString(bytes, layout, nameIndex);
|
||||
const member = {
|
||||
token,
|
||||
name,
|
||||
@@ -523,12 +506,7 @@ export const readReferencedMemberCores = (
|
||||
table === 4
|
||||
? readFieldDefinitionRow(bytes, layout, row)
|
||||
: readMethodDefinitionRow(bytes, layout, row);
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
nameIndex,
|
||||
layout.strings.size,
|
||||
);
|
||||
const name = readMetadataString(bytes, layout, nameIndex);
|
||||
core.set(token, {
|
||||
token,
|
||||
name,
|
||||
|
||||
@@ -63,7 +63,6 @@ const flagsHex = (value: number): string =>
|
||||
export const parseModuleRefs = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
issues: ManagedParseIssue[],
|
||||
): readonly ModuleRef[] => {
|
||||
const refs: ModuleRef[] = [];
|
||||
@@ -78,7 +77,6 @@ export const parseModuleRefs = (
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
),
|
||||
});
|
||||
}, issues);
|
||||
@@ -89,7 +87,6 @@ export const parseModuleRefs = (
|
||||
export const parseFields = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
issues: ManagedParseIssue[],
|
||||
): ReadonlyMap<string, MemberCore> => {
|
||||
const fields = new Map<string, MemberCore>();
|
||||
@@ -101,7 +98,7 @@ export const parseFields = (
|
||||
layout,
|
||||
row,
|
||||
);
|
||||
const name = readMetadataString(bytes, layout, nameIndex, heapExtent);
|
||||
const name = readMetadataString(bytes, layout, nameIndex);
|
||||
fields.set(token, {
|
||||
token,
|
||||
rowOffset,
|
||||
@@ -119,7 +116,6 @@ export const parseFields = (
|
||||
export const parseMethods = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
heapExtent: number,
|
||||
issues: ManagedParseIssue[],
|
||||
): ReadonlyMap<string, MemberCore> => {
|
||||
const methods = new Map<string, MemberCore>();
|
||||
@@ -135,7 +131,7 @@ export const parseMethods = (
|
||||
nameIndex,
|
||||
implementation,
|
||||
} = readMethodDefinitionRow(bytes, layout, row);
|
||||
const name = readMetadataString(bytes, layout, nameIndex, heapExtent);
|
||||
const name = readMetadataString(bytes, layout, nameIndex);
|
||||
methods.set(token, {
|
||||
token,
|
||||
rowOffset,
|
||||
@@ -154,7 +150,6 @@ export const parseMethods = (
|
||||
interface ParseImplMapsContext {
|
||||
readonly bytes: Buffer;
|
||||
readonly layout: ManagedMetadataLayout;
|
||||
readonly heapExtent: number;
|
||||
readonly modules: readonly ModuleRef[];
|
||||
readonly members: ReadonlyMap<string, MemberCore>;
|
||||
readonly issues: ManagedParseIssue[];
|
||||
@@ -163,7 +158,6 @@ interface ParseImplMapsContext {
|
||||
export const parseImplMaps = ({
|
||||
bytes,
|
||||
layout,
|
||||
heapExtent,
|
||||
modules,
|
||||
members,
|
||||
issues,
|
||||
@@ -201,7 +195,6 @@ export const parseImplMaps = ({
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
heapExtent,
|
||||
);
|
||||
const importScopeOffset = cursor.offset;
|
||||
const importScopeRow = cursor.readIndex(layout.tableIndexSize(26));
|
||||
@@ -431,7 +424,7 @@ export const buildNativeBoundaryInspection = ({
|
||||
target,
|
||||
bytes.length,
|
||||
layout,
|
||||
inventory.module,
|
||||
inventory,
|
||||
issues,
|
||||
),
|
||||
cli_native: cliNative(pe.cli),
|
||||
|
||||
@@ -166,17 +166,15 @@ export const inspectManagedNativeBoundariesBytes = (
|
||||
});
|
||||
}
|
||||
try {
|
||||
const heapExtent = layout.strings.size;
|
||||
appendManagedParseIssues(issues, inventory.issues);
|
||||
const moduleRefs = parseModuleRefs(bytes, layout, heapExtent, issues);
|
||||
const moduleRefs = parseModuleRefs(bytes, layout, issues);
|
||||
const members = new Map([
|
||||
...parseFields(bytes, layout, heapExtent, issues),
|
||||
...parseMethods(bytes, layout, heapExtent, issues),
|
||||
...parseFields(bytes, layout, issues),
|
||||
...parseMethods(bytes, layout, issues),
|
||||
]);
|
||||
const imports = parseImplMaps({
|
||||
bytes,
|
||||
layout,
|
||||
heapExtent,
|
||||
modules: moduleRefs,
|
||||
members,
|
||||
issues,
|
||||
|
||||
@@ -3,32 +3,40 @@ import type { ManagedParseIssue } from "../domain/managed/managedArtifact.js";
|
||||
import type { ManagedMetadataLayout } from "../domain/managed/managedMetadataLayout.js";
|
||||
import {
|
||||
managedTableRowCounts,
|
||||
unresolvedModuleMetadata,
|
||||
type ManagedMetadataInventory,
|
||||
} from "../domain/managed/managedMetadataInventory.js";
|
||||
|
||||
import { managedModuleRow } from "../domain/managed/managedModuleIdentity.js";
|
||||
|
||||
/** Shared artifact and token identity facts for successful managed PE projections. */
|
||||
export const managedPeInspectionEnvelope = (
|
||||
target: BinaryTarget,
|
||||
byteLength: number,
|
||||
layout: ManagedMetadataLayout,
|
||||
module: ManagedMetadataInventory["module"],
|
||||
identity: Pick<ManagedMetadataInventory, "module" | "unresolvedModuleName">,
|
||||
issues: readonly ManagedParseIssue[],
|
||||
) => ({
|
||||
artifact: {
|
||||
path: target.path,
|
||||
sha256: target.sha256,
|
||||
byte_length: byteLength,
|
||||
format: "pe" as const,
|
||||
},
|
||||
module,
|
||||
metadata: {
|
||||
) => {
|
||||
const { module } = identity;
|
||||
const metadata = {
|
||||
status: issues.length === 0 ? ("complete" as const) : ("partial" as const),
|
||||
version: layout.version,
|
||||
table_row_counts: managedTableRowCounts(layout),
|
||||
},
|
||||
identity_scope: {
|
||||
token_identity: "build-local" as const,
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: module?.mvid ?? null,
|
||||
},
|
||||
});
|
||||
...unresolvedModuleMetadata(identity),
|
||||
};
|
||||
return {
|
||||
artifact: {
|
||||
path: target.path,
|
||||
sha256: target.sha256,
|
||||
byte_length: byteLength,
|
||||
format: "pe" as const,
|
||||
},
|
||||
module,
|
||||
metadata,
|
||||
identity_scope: {
|
||||
token_identity: "build-local" as const,
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: managedModuleRow({ module, metadata })?.mvid ?? null,
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -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" }),
|
||||
]);
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { parseEvidence } from "../../src/domain/evidence.js";
|
||||
import { managedMemberInspectionSchema } from "../../src/domain/managed/managedArtifact.js";
|
||||
import {
|
||||
managedStringsAmbiguity,
|
||||
STRING_AMBIGUITY_MVID,
|
||||
} from "../fixtures/managedStringsAmbiguity.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
|
||||
cliTest(
|
||||
"retains unnamed Module identity through CLI, MCP and a retained member page",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-managed-module-name-");
|
||||
const path = join(root, "fixture.dll");
|
||||
const { bytes, nameIndex } = managedStringsAmbiguity("forward");
|
||||
await writeFile(path, bytes);
|
||||
|
||||
const output = await cli.run({
|
||||
arguments: ["inspect-managed-members", path, "--json"],
|
||||
});
|
||||
expect(output.exitCode, output.stderr).toBe(0);
|
||||
const report = managedMemberInspectionSchema.parse(
|
||||
parseEvidence(output.json).normalized_result,
|
||||
);
|
||||
expect(report).toMatchObject({
|
||||
module: null,
|
||||
metadata: {
|
||||
unresolved_module_name: {
|
||||
name: null,
|
||||
name_index: nameIndex,
|
||||
mvid: STRING_AMBIGUITY_MVID,
|
||||
},
|
||||
},
|
||||
identity_scope: { requires_mvid: STRING_AMBIGUITY_MVID },
|
||||
});
|
||||
|
||||
const call = await cli.sharedMcpCall!();
|
||||
const mcp = await call("inspect_managed_members", { path });
|
||||
expect(mcp.isError, JSON.stringify(mcp)).not.toBe(true);
|
||||
const parent = parseEvidence(mcp.structuredContent);
|
||||
expect(parent.normalized_result).toEqual(report);
|
||||
|
||||
const page = await call("inspect_analysis_view", {
|
||||
source: { kind: "retained-evidence", evidence_id: parent.evidence_id },
|
||||
view: { kind: "managed-members", collection: "methods", limit: 1 },
|
||||
});
|
||||
expect(page.isError, JSON.stringify(page)).not.toBe(true);
|
||||
expect(
|
||||
parseEvidence(page.structuredContent).normalized_result,
|
||||
).toMatchObject({
|
||||
source_metadata_status: "partial",
|
||||
unresolved_module_name: report.metadata.unresolved_module_name,
|
||||
identity_scope: report.identity_scope,
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1404,16 +1404,13 @@ it.each([
|
||||
metadataOffset,
|
||||
image.length - metadataOffset,
|
||||
);
|
||||
if (layout.blob?.kind !== "available")
|
||||
throw new Error("Fixture requires a #Blob stream");
|
||||
const blobSize = layout.blob.stream.size;
|
||||
const reference = layout.table(35)!;
|
||||
// ECMA-335 AssemblyRef: 8 version bytes, 4 flag bytes, then PublicKeyOrToken.
|
||||
// Keep its name valid while making that unrelated blob index invalid.
|
||||
if (layout.blobIndexSize === 2)
|
||||
image.writeUInt16LE(0xffff, reference.offset + 12);
|
||||
else image.writeUInt32LE(0xffff_ffff, reference.offset + 12);
|
||||
expect(() => readAssemblyReference(image, layout, 1, blobSize)).toThrow();
|
||||
expect(() => readAssemblyReference(image, layout, 1)).toThrow();
|
||||
await put(root, `${managedDirectory}/Assembly-CSharp.dll`, image);
|
||||
const { report } = await inspect(root);
|
||||
expect(report.candidates.map((candidate) => candidate.family)).toEqual([
|
||||
|
||||
@@ -24,6 +24,11 @@ import {
|
||||
managedPeFixtureTarget,
|
||||
} from "../../../src/dotnet/ManagedPe.fixture.js";
|
||||
|
||||
import {
|
||||
managedStringsAmbiguity,
|
||||
STRING_AMBIGUITY_MVID,
|
||||
} from "../../fixtures/managedStringsAmbiguity.js";
|
||||
|
||||
/** Tiny-format CIL method body. */
|
||||
const tinyIl = (il: readonly number[]): Buffer =>
|
||||
Buffer.from([(il.length << 2) | 2, ...il]);
|
||||
@@ -52,6 +57,25 @@ const memberGraph = (
|
||||
return { members, memberEvidence, result, graph };
|
||||
};
|
||||
|
||||
it("keeps an unnamed Module node bound to its independently observed MVID", () => {
|
||||
const { bytes } = managedStringsAmbiguity("forward");
|
||||
const { graph } = memberGraph(bytes);
|
||||
expect(
|
||||
graph.nodes.find(({ kind }) => kind === "managed-module"),
|
||||
).toMatchObject({
|
||||
observations: [
|
||||
{
|
||||
label: null,
|
||||
properties: {
|
||||
name: null,
|
||||
mvid: STRING_AMBIGUITY_MVID,
|
||||
token: "0x00000001",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
const createManagedInteropEvidence = () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
pinvoke: {
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
import { readManagedPeLayout } from "../../src/domain/managed/managedPeReader.js";
|
||||
|
||||
export type DuplicateHeapVariant =
|
||||
| "forward"
|
||||
| "reverse"
|
||||
| "alias"
|
||||
| "outside-root";
|
||||
|
||||
export const duplicateManagedHeap = (
|
||||
bytes: Buffer,
|
||||
name: "#GUID" | "#US" | "#Blob" | "#Strings",
|
||||
alternateHeap: Buffer,
|
||||
variant: DuplicateHeapVariant,
|
||||
) => {
|
||||
const pe = readManagedPeLayout(bytes);
|
||||
if (pe.cli === null) throw new Error("Fixture omitted a CLI header");
|
||||
const size = pe.cli.metadata.size;
|
||||
const start = pe.rvaToOffset(pe.cli.metadata.rva, size, "fixture metadata");
|
||||
const original = Buffer.from(bytes.subarray(start, start + size));
|
||||
const streamCount = original.readUInt16LE(30);
|
||||
const headers: number[] = [];
|
||||
let heapHeader = -1;
|
||||
let end = 32;
|
||||
for (let index = 0; index < streamCount; index += 1) {
|
||||
headers.push(end);
|
||||
const terminator = original.indexOf(0, end + 8);
|
||||
if (original.toString("ascii", end + 8, terminator) === name)
|
||||
heapHeader = end;
|
||||
end = Math.ceil((terminator + 1) / 4) * 4;
|
||||
}
|
||||
if (heapHeader < 0) throw new Error(`Fixture omitted ${name}`);
|
||||
const headerSize = 8 + Math.ceil((name.length + 1) / 4) * 4;
|
||||
const alternate = size + headerSize;
|
||||
const extended = Buffer.alloc(
|
||||
variant === "alias" ? alternate : alternate + alternateHeap.byteLength,
|
||||
);
|
||||
original.copy(extended, 0, 0, end);
|
||||
original.copy(extended, end + headerSize, end);
|
||||
extended.writeUInt16LE(streamCount + 1, 30);
|
||||
for (const header of headers)
|
||||
extended.writeUInt32LE(original.readUInt32LE(header) + headerSize, header);
|
||||
const originalHeap = extended.readUInt32LE(heapHeader);
|
||||
const [first, second] = (
|
||||
{
|
||||
forward: [originalHeap, alternate],
|
||||
reverse: [alternate, originalHeap],
|
||||
alias: [originalHeap, originalHeap],
|
||||
"outside-root": [originalHeap, extended.length],
|
||||
} as const
|
||||
)[variant];
|
||||
if (variant !== "alias") alternateHeap.copy(extended, alternate);
|
||||
extended.writeUInt32LE(first, heapHeader);
|
||||
extended.writeUInt32LE(second, end);
|
||||
const originalSize = original.readUInt32LE(heapHeader + 4);
|
||||
extended.writeUInt32LE(
|
||||
variant === "reverse" ? alternateHeap.byteLength : originalSize,
|
||||
heapHeader + 4,
|
||||
);
|
||||
extended.writeUInt32LE(
|
||||
variant === "alias" || variant === "reverse"
|
||||
? originalSize
|
||||
: alternateHeap.byteLength,
|
||||
end + 4,
|
||||
);
|
||||
extended.write(name, end + 8);
|
||||
bytes.set(extended, start);
|
||||
bytes.writeUInt32LE(extended.length, pe.cli.headerOffset + 12);
|
||||
|
||||
return {
|
||||
bytes,
|
||||
duplicateHeader: start + end,
|
||||
firstHeader: start + heapHeader,
|
||||
firstRange: start + first,
|
||||
secondRange: start + second,
|
||||
metadataEnd: start + extended.length,
|
||||
};
|
||||
};
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
import { buildManagedPeFixture } from "../../src/dotnet/ManagedPe.fixture.js";
|
||||
import { readManagedMetadataLayout } from "../../src/domain/managed/managedMetadataLayout.js";
|
||||
import { readManagedPeLayout } from "../../src/domain/managed/managedPeReader.js";
|
||||
import {
|
||||
duplicateManagedHeap,
|
||||
type DuplicateHeapVariant,
|
||||
} from "./managedDuplicateMetadataHeap.js";
|
||||
|
||||
export const STRING_AMBIGUITY_MVID = "11111111-1111-1111-1111-111111111111";
|
||||
|
||||
export const managedStringsAmbiguity = (
|
||||
variant: DuplicateHeapVariant,
|
||||
options: {
|
||||
readonly nilName?: boolean;
|
||||
readonly ambiguousGuid?: boolean;
|
||||
readonly multipleModules?: boolean;
|
||||
readonly invalidEnc?: boolean;
|
||||
} = {},
|
||||
) => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
mvid: Buffer.alloc(16, 0x11),
|
||||
moduleRowCount: options.multipleModules === true ? 2 : 1,
|
||||
targetFramework: null,
|
||||
});
|
||||
const pe = readManagedPeLayout(bytes);
|
||||
if (pe.cli === null) throw new Error("Fixture omitted CLI metadata");
|
||||
const { size, rva } = pe.cli.metadata;
|
||||
const root = pe.rvaToOffset(rva, size, "fixture metadata");
|
||||
const layout = readManagedMetadataLayout(bytes, root, size);
|
||||
const module = layout.table(0);
|
||||
if (module === undefined) throw new Error("Fixture omitted its Module row");
|
||||
// This small fixture uses 16-bit heap indices. Generation, Name and three
|
||||
// GUID indices occupy the first five words. Two fields refer to GUID slot 1.
|
||||
bytes.writeUInt16LE(2, module.offset);
|
||||
if (options.nilName === true) bytes.writeUInt16LE(0, module.offset + 2);
|
||||
bytes.writeUInt16LE(options.invalidEnc === true ? 2 : 1, module.offset + 6);
|
||||
const nameIndex = bytes.readUInt16LE(module.offset + 2);
|
||||
const source =
|
||||
options.ambiguousGuid === true
|
||||
? duplicateManagedHeap(bytes, "#GUID", Buffer.alloc(16, 0x22), "forward")
|
||||
.bytes
|
||||
: bytes;
|
||||
const alternate = Buffer.from("\0Alternate.dll\0", "utf8");
|
||||
return {
|
||||
...duplicateManagedHeap(source, "#Strings", alternate, variant),
|
||||
nameIndex,
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user