feat(managed): select declaring types in inspect_managed_members
Select exact declaring types before managed signatures and CIL bodies are decoded, preserving caller choices in CLI/MCP Evidence. Keep unmatched names and selected coverage explicit; comparisons and graphs retain unknowns outside their observed scope. Reuse existing metadata ownership/accounting and advertise unique selections through the actual SDK Schema. Validated with 229 focused tests across 21 files, 11 managed conformance controls, fullstatic/docs checks and real Linux CLI and fresh MCP inspection of two unchanged DLLs. All 118 selected CIL hashes matched original PE-mapped bytes; result/caller-argument parity and unmatched controls passed. Full artifact/native-boundary metadata is complete; whole-assembly members retain the unchanged 64 MiB resource constraint. No target code executes. Refs #1878 Co-authored-by: skared <190618464+skoodster@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Ysopwnable <161784452+N0zoM1z0@users.noreply.github.com> Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
co-authored by
skared
Claude Opus 5.5
Ysopwnable
factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Kaoru0822
parent
f28f338283
commit
a149d183ef
@@ -177,40 +177,54 @@ describe("managed artifact coded indexes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("reports null for the required CustomAttribute constructor coded index", () => {
|
||||
const bytes = buildManagedPeFixture({ customAttributeTypeRaw: 0 });
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
const text = Buffer.from(".NETCoreApp,Version=v8.0", "utf8");
|
||||
const attributeValue = Buffer.concat([
|
||||
Buffer.from([1, 0, text.length]),
|
||||
text,
|
||||
Buffer.alloc(2),
|
||||
]);
|
||||
it.each([
|
||||
[
|
||||
0,
|
||||
"CustomAttribute constructor coded index 0x0 is null, but Type must reference a MethodDef or MemberRef row",
|
||||
],
|
||||
[
|
||||
19,
|
||||
"coded index 0x13 is invalid: coded index selects row 2 in table 10, which has 1 rows",
|
||||
],
|
||||
] as const)(
|
||||
"preserves readable attribute facts for constructor index %s",
|
||||
(rawType, detail) => {
|
||||
const bytes = buildManagedPeFixture({ customAttributeTypeRaw: rawType });
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
const text = Buffer.from(".NETCoreApp,Version=v8.0", "utf8");
|
||||
const attributeValue = Buffer.concat([
|
||||
Buffer.from([1, 0, text.length]),
|
||||
text,
|
||||
Buffer.alloc(2),
|
||||
]);
|
||||
|
||||
expect(result.attributes).toMatchObject([
|
||||
{
|
||||
parent_token: "0x20000001",
|
||||
constructor_token: null,
|
||||
type_name: null,
|
||||
value_length: attributeValue.length,
|
||||
value_sha256: createHash("sha256").update(attributeValue).digest("hex"),
|
||||
token: "0x0c000001",
|
||||
},
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
code: "invalid-row",
|
||||
detail:
|
||||
"CustomAttribute constructor coded index 0x0 is null, but Type must reference a MethodDef or MemberRef row",
|
||||
}),
|
||||
],
|
||||
});
|
||||
});
|
||||
expect(result.attributes).toMatchObject([
|
||||
{
|
||||
parent_token: "0x20000001",
|
||||
constructor_token: null,
|
||||
type_name: null,
|
||||
value_length: attributeValue.length,
|
||||
value_sha256: createHash("sha256")
|
||||
.update(attributeValue)
|
||||
.digest("hex"),
|
||||
token: "0x0c000001",
|
||||
},
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: "partial",
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
code: "invalid-row",
|
||||
scope: "metadata.CustomAttribute:0x0c000001",
|
||||
detail: expect.stringContaining(detail),
|
||||
}),
|
||||
],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("reports coded-index column offsets after widened string and coded-index columns", () => {
|
||||
const references = Array.from(
|
||||
|
||||
@@ -859,3 +859,68 @@ it.each([
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
describe("managed member type selection", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const target = managedPeFixtureTarget(bytes);
|
||||
const everything = inspectManagedMembersBytes(bytes, target);
|
||||
|
||||
it("reports no selection when every type is inspected", () => {
|
||||
expect(everything.selection).toBeNull();
|
||||
});
|
||||
|
||||
it("decodes the selected type's members and reports unmatched names", () => {
|
||||
const result = inspectManagedMembersBytes(bytes, target, {
|
||||
typeNames: ["Fixture.Program", "Missing.Type"],
|
||||
});
|
||||
|
||||
expect(result.selection).toEqual({
|
||||
type_names: ["Fixture.Program", "Missing.Type"],
|
||||
unmatched_type_names: ["Missing.Type"],
|
||||
});
|
||||
expect(result.types).toEqual(everything.types);
|
||||
expect(result.fields).toEqual(everything.fields);
|
||||
expect(result.methods).toEqual(everything.methods);
|
||||
expect(result.call_edges).toEqual(everything.call_edges);
|
||||
expect(result.field_accesses).toEqual(everything.field_accesses);
|
||||
expect(result.coverage).toEqual(everything.coverage);
|
||||
expect(result.limitations).toContain(
|
||||
"Types, fields, methods, call edges, and field accesses cover only the selected declaring types; coverage describes that selection. Edge targets outside it keep their names but are not described.",
|
||||
);
|
||||
});
|
||||
|
||||
it("does not treat a name as absent when a TypeDef name is unreadable", () => {
|
||||
const corrupt = Buffer.from(bytes);
|
||||
const row = everything.types[0]?.row_offset ?? 0;
|
||||
// Point the TypeDef Name past the #Strings heap.
|
||||
corrupt.writeUInt16LE(0xffff, row + 4);
|
||||
const result = inspectManagedMembersBytes(
|
||||
corrupt,
|
||||
managedPeFixtureTarget(corrupt),
|
||||
{ typeNames: ["Fixture.Program"] },
|
||||
);
|
||||
|
||||
expect(result.selection?.unmatched_type_names).toEqual(["Fixture.Program"]);
|
||||
expect(result.coverage.state).toBe("partial");
|
||||
expect(result.limitations).toContain(
|
||||
"1 TypeDef name(s) could not be read; a name in selection.unmatched_type_names may belong to one of them, so its absence is not observed.",
|
||||
);
|
||||
});
|
||||
|
||||
it("omits members of unselected types without dropping member references", () => {
|
||||
const result = inspectManagedMembersBytes(bytes, target, {
|
||||
typeNames: ["Missing.Type"],
|
||||
});
|
||||
|
||||
expect(result.selection).toEqual({
|
||||
type_names: ["Missing.Type"],
|
||||
unmatched_type_names: ["Missing.Type"],
|
||||
});
|
||||
expect(result.types).toEqual([]);
|
||||
expect(result.fields).toEqual([]);
|
||||
expect(result.methods).toEqual([]);
|
||||
expect(result.call_edges).toEqual([]);
|
||||
expect(result.field_accesses).toEqual([]);
|
||||
expect(result.member_refs).toEqual(everything.member_refs);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,19 +22,25 @@ import {
|
||||
ManagedReaderFailure,
|
||||
uniqueManagedParseIssues,
|
||||
} from "./ManagedReaderFailure.js";
|
||||
import { parseTypes, typeRanges } from "./ManagedMemberInspectorCore.js";
|
||||
import {
|
||||
parseTypes,
|
||||
typeRanges,
|
||||
type TypeRange,
|
||||
} from "./ManagedMemberInspectorCore.js";
|
||||
import {
|
||||
edges,
|
||||
parseExportedTypes,
|
||||
parseFields,
|
||||
parseMemberRefs,
|
||||
parseMethods,
|
||||
type SelectedTypeTokens,
|
||||
} from "./ManagedMemberRows.js";
|
||||
|
||||
const unavailable = (
|
||||
target: BinaryTarget,
|
||||
bytes: Buffer,
|
||||
issue: ManagedParseIssue | null,
|
||||
typeNames: readonly string[] | undefined,
|
||||
): ManagedMemberInspection =>
|
||||
managedMemberInspectionSchema.parse(
|
||||
admitManagedProjection({
|
||||
@@ -67,6 +73,11 @@ const unavailable = (
|
||||
member_refs: [],
|
||||
call_edges: [],
|
||||
field_accesses: [],
|
||||
// Nothing was examined, so no requested name is known to be unmatched.
|
||||
selection:
|
||||
typeNames === undefined
|
||||
? null
|
||||
: { type_names: [...typeNames], unmatched_type_names: [] },
|
||||
coverage: {
|
||||
state: "unavailable",
|
||||
issues: issue === null ? [] : [issue],
|
||||
@@ -105,23 +116,61 @@ const readMemberInventory = (
|
||||
return { layout, inventory };
|
||||
};
|
||||
|
||||
/** Inspect metadata members and method bodies without loading target code. */
|
||||
const selectTypes = (
|
||||
ranges: readonly TypeRange[],
|
||||
typeNames: readonly string[] | undefined,
|
||||
): {
|
||||
readonly tokens: SelectedTypeTokens;
|
||||
readonly selection: ManagedMemberInspection["selection"];
|
||||
readonly unreadableNames: number;
|
||||
} => {
|
||||
if (typeNames === undefined)
|
||||
return { tokens: null, selection: null, unreadableNames: 0 };
|
||||
const requested = new Set(typeNames);
|
||||
const tokens = new Set<string>();
|
||||
const matched = new Set<string>();
|
||||
for (const range of ranges) {
|
||||
if (range.fullName === null || !requested.has(range.fullName)) continue;
|
||||
tokens.add(range.token);
|
||||
matched.add(range.fullName);
|
||||
}
|
||||
return {
|
||||
tokens,
|
||||
unreadableNames: ranges.filter(({ fullName }) => fullName === null).length,
|
||||
selection: {
|
||||
type_names: [...typeNames],
|
||||
unmatched_type_names: typeNames.filter((name) => !matched.has(name)),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Inspect metadata members and method bodies without loading target code.
|
||||
* Selected type names restrict full decoding to members of those exact types.
|
||||
*/
|
||||
export const inspectManagedMembersBytes = (
|
||||
bytes: Buffer,
|
||||
target: BinaryTarget,
|
||||
{ typeNames }: { readonly typeNames?: readonly string[] } = {},
|
||||
): ManagedMemberInspection => {
|
||||
const pe = readManagedPeLayout(bytes);
|
||||
if (pe.cli === null) return unavailable(target, bytes, pe.cliIssue);
|
||||
if (pe.cli === null)
|
||||
return unavailable(target, bytes, pe.cliIssue, typeNames);
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
try {
|
||||
const { layout, inventory } = readMemberInventory(bytes, pe, issues);
|
||||
issues.push(...inventory.issues);
|
||||
const ranges = typeRanges(bytes, layout, issues);
|
||||
const {
|
||||
tokens: selected,
|
||||
selection,
|
||||
unreadableNames,
|
||||
} = selectTypes(ranges, typeNames);
|
||||
const parsedTypes = parseTypes(bytes, layout, ranges);
|
||||
issues.push(...parsedTypes.issues);
|
||||
const exportedTypes = parseExportedTypes(bytes, layout);
|
||||
issues.push(...exportedTypes.issues);
|
||||
const fields = parseFields(bytes, layout, ranges);
|
||||
const fields = parseFields(bytes, layout, ranges, selected);
|
||||
const memberRefs = parseMemberRefs(bytes, layout);
|
||||
issues.push(...memberRefs.issues);
|
||||
const methods = parseMethods({
|
||||
@@ -129,6 +178,7 @@ export const inspectManagedMembersBytes = (
|
||||
layout,
|
||||
pe,
|
||||
ranges,
|
||||
selected,
|
||||
});
|
||||
issues.push(...fields.issues, ...methods.issues);
|
||||
const coverageIssues = uniqueManagedParseIssues(issues);
|
||||
@@ -168,13 +218,17 @@ export const inspectManagedMembersBytes = (
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: inventory.module?.mvid ?? null,
|
||||
},
|
||||
types: parsedTypes.types,
|
||||
types:
|
||||
selected === null
|
||||
? parsedTypes.types
|
||||
: parsedTypes.types.filter((type) => selected.has(type.token)),
|
||||
exported_types: exportedTypes.exportedTypes,
|
||||
fields: fields.fields,
|
||||
methods: methods.methods,
|
||||
member_refs: memberRefs.refs,
|
||||
call_edges: related.callEdges,
|
||||
field_accesses: related.fieldAccesses,
|
||||
selection,
|
||||
coverage: {
|
||||
state:
|
||||
issues.length === 0 && !incompleteMembers
|
||||
@@ -187,6 +241,16 @@ export const inspectManagedMembersBytes = (
|
||||
"CIL instruction anchors are decoded from file-backed method bodies only; no target assembly is loaded or executed.",
|
||||
"Exported types are reported from ExportedType rows as declared; forwarded types are not resolved in their implementing assemblies.",
|
||||
"Member signatures decode ECMA-335 primitive, class, valuetype, generic instantiation, generic variable, pointer, byref, single-dimension and general array, function pointer, custom modifier, and typed reference forms; class and valuetype operands keep their encoded TypeDefOrRefOrSpec values, and unsupported forms such as vararg sentinels retain raw signature hashes.",
|
||||
...(selection === null
|
||||
? []
|
||||
: [
|
||||
"Types, fields, methods, call edges, and field accesses cover only the selected declaring types; coverage describes that selection. Edge targets outside it keep their names but are not described.",
|
||||
]),
|
||||
...(unreadableNames === 0
|
||||
? []
|
||||
: [
|
||||
`${String(unreadableNames)} TypeDef name(s) could not be read; a name in selection.unmatched_type_names may belong to one of them, so its absence is not observed.`,
|
||||
]),
|
||||
],
|
||||
},
|
||||
managedDecodeBudget(layout),
|
||||
@@ -194,7 +258,7 @@ export const inspectManagedMembersBytes = (
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof ManagedReaderFailure)
|
||||
return unavailable(target, bytes, cause.issue);
|
||||
return unavailable(target, bytes, cause.issue, typeNames);
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -32,10 +32,23 @@ import { readManagedValue } from "./ManagedReaderFailure.js";
|
||||
import { methodBody } from "./ManagedMethodBodyReader.js";
|
||||
import { memberDeclaringTypeRowLookup } from "./ManagedMemberOwnership.js";
|
||||
|
||||
/**
|
||||
* Declaring-type tokens whose members are decoded in full; null selects every
|
||||
* type. Unselected members keep only their name so edges stay labelled.
|
||||
*/
|
||||
export type SelectedTypeTokens = ReadonlySet<string> | null;
|
||||
|
||||
const isSelected = (
|
||||
selected: SelectedTypeTokens,
|
||||
declared: { readonly token: string } | undefined,
|
||||
): boolean =>
|
||||
selected === null || (declared !== undefined && selected.has(declared.token));
|
||||
|
||||
export const parseFields = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
ranges: readonly TypeRange[],
|
||||
selected: SelectedTypeTokens = null,
|
||||
): {
|
||||
readonly fields: readonly ManagedField[];
|
||||
readonly core: ReadonlyMap<string, FieldCore>;
|
||||
@@ -59,18 +72,20 @@ export const parseFields = (
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
const owner =
|
||||
ownerRow === undefined
|
||||
? 0
|
||||
: (readManagedValue(() => ownerRow(row), issues) ?? 0);
|
||||
const declared = ranges[owner - 1];
|
||||
const token = metadataToken(4, row);
|
||||
core.set(token, { token, name });
|
||||
if (!isSelected(selected, declared)) return;
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.blobIndexSize),
|
||||
layout.blob.size,
|
||||
);
|
||||
fields.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
@@ -80,7 +95,6 @@ export const parseFields = (
|
||||
flags,
|
||||
signature: budgetedSignature(sig, layout),
|
||||
});
|
||||
core.set(token, { token, name });
|
||||
}, issues);
|
||||
}
|
||||
return { fields, core, issues };
|
||||
@@ -227,6 +241,7 @@ interface ParseMethodsInput {
|
||||
readonly layout: ManagedMetadataLayout;
|
||||
readonly pe: ManagedPeLayout;
|
||||
readonly ranges: readonly TypeRange[];
|
||||
readonly selected?: SelectedTypeTokens;
|
||||
}
|
||||
|
||||
export const parseMethods = ({
|
||||
@@ -234,6 +249,7 @@ export const parseMethods = ({
|
||||
layout,
|
||||
pe,
|
||||
ranges,
|
||||
selected = null,
|
||||
}: ParseMethodsInput): {
|
||||
readonly methods: readonly ManagedMethod[];
|
||||
readonly core: ReadonlyMap<string, MethodCore>;
|
||||
@@ -259,6 +275,18 @@ export const parseMethods = ({
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
layout.strings.size,
|
||||
);
|
||||
const owner =
|
||||
ownerRow === undefined
|
||||
? 0
|
||||
: (readManagedValue(() => ownerRow(row), issues) ?? 0);
|
||||
const declared = ranges[owner - 1];
|
||||
const token = metadataToken(6, row);
|
||||
core.set(token, {
|
||||
token,
|
||||
name,
|
||||
declaringType: declared?.fullName ?? null,
|
||||
});
|
||||
if (!isSelected(selected, declared)) return;
|
||||
const sig = readMetadataBlob(
|
||||
bytes,
|
||||
layout,
|
||||
@@ -266,12 +294,6 @@ export const parseMethods = ({
|
||||
layout.blob.size,
|
||||
);
|
||||
cursor.readIndex(layout.tableIndexSize(8));
|
||||
const owner =
|
||||
ownerRow === undefined
|
||||
? 0
|
||||
: (readManagedValue(() => ownerRow(row), issues) ?? 0);
|
||||
const declared = ranges[owner - 1];
|
||||
const token = metadataToken(6, row);
|
||||
methods.push({
|
||||
token,
|
||||
row_offset: cursor.start,
|
||||
@@ -290,11 +312,6 @@ export const parseMethods = ({
|
||||
managedDecodeBudget(layout),
|
||||
),
|
||||
});
|
||||
core.set(token, {
|
||||
token,
|
||||
name,
|
||||
declaringType: declared?.fullName ?? null,
|
||||
});
|
||||
}, issues);
|
||||
}
|
||||
return { methods, core, issues };
|
||||
|
||||
@@ -13,6 +13,7 @@ import { managedStaticCapabilities } from "./ManagedStaticProviderMetadata.js";
|
||||
import { MANAGED_STATIC_PROVIDER } from "../application/InvestigationProviders.js";
|
||||
import {
|
||||
MANAGED_TOOL_CONTRACTS,
|
||||
managedMembersInputSchema,
|
||||
managedTargetInputSchema,
|
||||
type ManagedToolName,
|
||||
} from "../contracts/managed/managedToolContracts.js";
|
||||
@@ -235,14 +236,20 @@ const inspectManagedOperation = (
|
||||
| ManagedArtifactInspection
|
||||
| ManagedMemberInspection
|
||||
| ManagedNativeBoundaryInspection => {
|
||||
if (operation === "inspect_managed_members") {
|
||||
const { type_names: typeNames } =
|
||||
managedMembersInputSchema.parse(parameters);
|
||||
return inspectManagedMembersBytes(
|
||||
bytes,
|
||||
target,
|
||||
typeNames === undefined ? {} : { typeNames },
|
||||
);
|
||||
}
|
||||
managedTargetInputSchema.parse(parameters);
|
||||
if (operation === "inspect_managed_artifact") {
|
||||
return inspectManagedArtifactBytes(bytes, target);
|
||||
}
|
||||
if (operation === "inspect_managed_native_boundaries") {
|
||||
return inspectManagedNativeBoundariesBytes(bytes, target);
|
||||
}
|
||||
return inspectManagedMembersBytes(bytes, target);
|
||||
return inspectManagedNativeBoundariesBytes(bytes, target);
|
||||
};
|
||||
|
||||
const managedLocations = (
|
||||
|
||||
Reference in New Issue
Block a user