fix(managed): budget decoding before representation expansion

Validation: integrated changes passed formatting, typecheck, lint, resolved module-boundary verification and git diff --check. Tests and real-provider workflows were not run.
This commit is contained in:
morluto
2026-10-11 00:03:19 +08:00
parent 6d4cefc4f7
commit 0f0d6e4d43
18 changed files with 705 additions and 362 deletions
+108 -82
View File
@@ -1,3 +1,7 @@
import {
admitManagedProjection,
managedDecodeBudget,
} from "./ManagedDecodeBudget.js";
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
import {
managedArtifactInspectionSchema,
@@ -107,40 +111,44 @@ const unavailableResult = (
: [
"The PE has no CLI data directory; managed metadata and CIL are unavailable.",
];
return managedArtifactInspectionSchema.parse({
...base(target, bytes, layout),
classification: {
status: malformed ? "malformed" : "not-managed",
container: "pe",
runtime_family: "unknown",
implementation: malformed ? "unknown" : "not-managed",
managed_architecture: "unknown",
evidence: [
{
code: malformed ? "cli-directory-malformed" : "cli-directory-absent",
detail: limitations[0],
file_offset: layout.cliIssue?.offset ?? null,
},
],
},
metadata: {
status: malformed ? "malformed" : "absent",
version: null,
stream_names: [],
table_row_counts: {},
},
module: null,
assembly: null,
target_frameworks: [],
references: [],
resources: [],
attributes: [],
coverage: {
state: malformed ? "partial" : "unavailable",
issues,
},
limitations,
});
return managedArtifactInspectionSchema.parse(
admitManagedProjection({
...base(target, bytes, layout),
classification: {
status: malformed ? "malformed" : "not-managed",
container: "pe",
runtime_family: "unknown",
implementation: malformed ? "unknown" : "not-managed",
managed_architecture: "unknown",
evidence: [
{
code: malformed
? "cli-directory-malformed"
: "cli-directory-absent",
detail: limitations[0],
file_offset: layout.cliIssue?.offset ?? null,
},
],
},
metadata: {
status: malformed ? "malformed" : "absent",
version: null,
stream_names: [],
table_row_counts: {},
},
module: null,
assembly: null,
target_frameworks: [],
references: [],
resources: [],
attributes: [],
coverage: {
state: malformed ? "partial" : "unavailable",
issues,
},
limitations,
}),
);
};
interface RuntimeClassificationInput {
@@ -288,7 +296,7 @@ const partialMetadataResult = ({
}: PartialMetadataContext): ManagedArtifactInspection => ({
...base(target, bytes, layout),
classification: {
status: "malformed",
status: issue.code === "resource-limit" ? "managed" : "malformed",
container: "pe",
runtime_family:
(layout.cli?.flags ?? 1) & 1 ? "unknown" : "mixed-clr-native",
@@ -303,7 +311,7 @@ const partialMetadataResult = ({
],
},
metadata: {
status: "malformed",
status: issue.code === "resource-limit" ? "partial" : "malformed",
version: null,
stream_names: [],
table_row_counts: {},
@@ -316,7 +324,9 @@ const partialMetadataResult = ({
attributes: [],
coverage: { state: "partial", issues: [issue] },
limitations: [
"CLI metadata could not be read because the PE/CLI structure is malformed or unsupported.",
issue.code === "resource-limit"
? "CLI metadata inspection stopped at its representation budget; omitted facts are unknown."
: "CLI metadata could not be read because the PE/CLI structure is malformed or unsupported.",
],
});
@@ -343,12 +353,14 @@ export const inspectManagedArtifactBytes = (
} catch (cause: unknown) {
if (!(cause instanceof ManagedReaderFailure)) throw cause;
return managedArtifactInspectionSchema.parse(
partialMetadataResult({
target,
bytes,
layout,
issue: cause.issue,
}),
admitManagedProjection(
partialMetadataResult({
target,
bytes,
layout,
issue: cause.issue,
}),
),
);
}
const resourceIssues: ManagedParseIssue[] = [];
@@ -356,43 +368,57 @@ export const inspectManagedArtifactBytes = (
layout,
resourceIssues,
);
const inventory = readManagedMetadataInventory(
bytes,
metadata,
resourceDirectory,
);
const issues = [...resourceIssues, ...inventory.issues];
const limitations = [
...(issues.length === 0
? []
: [
"At least one metadata or resource item could not be read; coverage is partial.",
]),
"Static inspection did not load the assembly, resolve dependencies through a CLR, decompile C#, or execute target code.",
];
return managedArtifactInspectionSchema.parse({
...base(target, bytes, layout),
classification: {
status: "managed",
container: "pe",
...runtimeClassification({ layout, metadata, inventory }),
},
metadata: {
status: issues.length === 0 ? "complete" : "partial",
version: metadata.version,
stream_names: metadata.streamNames,
table_row_counts: managedTableRowCounts(metadata),
},
module: inventory.module,
assembly: inventory.assembly,
target_frameworks: inventory.targetFrameworks,
references: inventory.references,
resources: inventory.resources,
attributes: inventory.attributes,
coverage: {
state: issues.length === 0 ? "complete" : "partial",
issues,
},
limitations,
});
try {
const inventory = readManagedMetadataInventory(
bytes,
metadata,
resourceDirectory,
);
const issues = [...resourceIssues, ...inventory.issues];
const limitations = [
...(issues.length === 0
? []
: [
"At least one metadata or resource item could not be read; coverage is partial.",
]),
"Static inspection did not load the assembly, resolve dependencies through a CLR, decompile C#, or execute target code.",
];
return managedArtifactInspectionSchema.parse(
admitManagedProjection(
{
...base(target, bytes, layout),
classification: {
status: "managed",
container: "pe",
...runtimeClassification({ layout, metadata, inventory }),
},
metadata: {
status: issues.length === 0 ? "complete" : "partial",
version: metadata.version,
stream_names: metadata.streamNames,
table_row_counts: managedTableRowCounts(metadata),
},
module: inventory.module,
assembly: inventory.assembly,
target_frameworks: inventory.targetFrameworks,
references: inventory.references,
resources: inventory.resources,
attributes: inventory.attributes,
coverage: {
state: issues.length === 0 ? "complete" : "partial",
issues,
},
limitations,
},
managedDecodeBudget(metadata),
),
);
} catch (cause: unknown) {
if (!(cause instanceof ManagedReaderFailure)) throw cause;
return managedArtifactInspectionSchema.parse(
admitManagedProjection(
partialMetadataResult({ target, bytes, layout, issue: cause.issue }),
),
);
}
};