feat(dotnet): add managed native boundary inspection
This commit is contained in:
+66
@@ -717,6 +717,72 @@ const registerManagedCommands = (
|
||||
),
|
||||
),
|
||||
});
|
||||
cli.command(CLI_COMMANDS.inspectManagedNativeBoundaries, {
|
||||
description:
|
||||
"Inspect PE/CLI PInvoke and native implementation boundary declarations without loading target code",
|
||||
args: z.object({
|
||||
path: z.string().describe("Managed PE executable or assembly path"),
|
||||
}),
|
||||
options: z.object({
|
||||
moduleRefOffset: z.number().int().min(0).default(0),
|
||||
moduleRefLimit: z.number().int().min(1).max(500).default(100),
|
||||
importOffset: z.number().int().min(0).default(0),
|
||||
importLimit: z.number().int().min(1).max(500).default(100),
|
||||
implementationOffset: z.number().int().min(0).default(0),
|
||||
implementationLimit: z.number().int().min(1).max(500).default(100),
|
||||
maxFileBytes: z
|
||||
.number()
|
||||
.int()
|
||||
.min(4_096)
|
||||
.max(1_073_741_824)
|
||||
.default(268_435_456),
|
||||
maxMetadataBytes: z
|
||||
.number()
|
||||
.int()
|
||||
.min(256)
|
||||
.max(268_435_456)
|
||||
.default(67_108_864),
|
||||
maxTableRows: z.number().int().min(1).max(1_000_000).default(100_000),
|
||||
maxHeapItemBytes: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(16_777_216)
|
||||
.default(1_048_576),
|
||||
}),
|
||||
alias: {
|
||||
moduleRefOffset: "module-ref-offset",
|
||||
moduleRefLimit: "module-ref-limit",
|
||||
importOffset: "import-offset",
|
||||
importLimit: "import-limit",
|
||||
implementationOffset: "implementation-offset",
|
||||
implementationLimit: "implementation-limit",
|
||||
maxFileBytes: "max-file-bytes",
|
||||
maxMetadataBytes: "max-metadata-bytes",
|
||||
maxTableRows: "max-table-rows",
|
||||
maxHeapItemBytes: "max-heap-item-bytes",
|
||||
},
|
||||
run: ({ args, options }) =>
|
||||
logCliCommand(logger, "inspect-managed-native-boundaries", () =>
|
||||
runProviderAnalysis(
|
||||
args.path,
|
||||
"inspect_managed_native_boundaries",
|
||||
{
|
||||
module_ref_offset: options.moduleRefOffset,
|
||||
module_ref_limit: options.moduleRefLimit,
|
||||
import_offset: options.importOffset,
|
||||
import_limit: options.importLimit,
|
||||
implementation_offset: options.implementationOffset,
|
||||
implementation_limit: options.implementationLimit,
|
||||
max_file_bytes: options.maxFileBytes,
|
||||
max_metadata_bytes: options.maxMetadataBytes,
|
||||
max_table_rows: options.maxTableRows,
|
||||
max_heap_item_bytes: options.maxHeapItemBytes,
|
||||
},
|
||||
logger,
|
||||
),
|
||||
),
|
||||
});
|
||||
cli.command(CLI_COMMANDS.compareManagedMembers, {
|
||||
description:
|
||||
"Compare two managed PE/CLI member inventories without name-based matching",
|
||||
|
||||
@@ -18,6 +18,7 @@ export const CLI_COMMANDS = Object.freeze({
|
||||
extractArtifact: "extract-artifact",
|
||||
inspectManagedArtifact: "inspect-managed-artifact",
|
||||
inspectManagedMembers: "inspect-managed-members",
|
||||
inspectManagedNativeBoundaries: "inspect-managed-native-boundaries",
|
||||
compareManagedMembers: "compare-managed-members",
|
||||
inspectMacho: "inspect-macho",
|
||||
inspectSignature: "inspect-signature",
|
||||
|
||||
@@ -73,12 +73,47 @@ export const managedMemberInputSchema = z.object({
|
||||
max_method_instructions: z.number().int().min(1).max(100_000).default(10_000),
|
||||
});
|
||||
|
||||
/** Exact caller boundary for execution-free managed/native boundary inspection. */
|
||||
export const managedNativeBoundaryInputSchema = z.object({
|
||||
module_ref_offset: z.number().int().min(0).default(0),
|
||||
module_ref_limit: z.number().int().min(1).max(500).default(100),
|
||||
import_offset: z.number().int().min(0).default(0),
|
||||
import_limit: z.number().int().min(1).max(500).default(100),
|
||||
implementation_offset: z.number().int().min(0).default(0),
|
||||
implementation_limit: z.number().int().min(1).max(500).default(100),
|
||||
max_file_bytes: z
|
||||
.number()
|
||||
.int()
|
||||
.min(4_096)
|
||||
.max(1_073_741_824)
|
||||
.default(268_435_456),
|
||||
max_metadata_bytes: z
|
||||
.number()
|
||||
.int()
|
||||
.min(256)
|
||||
.max(268_435_456)
|
||||
.default(67_108_864),
|
||||
max_table_rows: z.number().int().min(1).max(1_000_000).default(100_000),
|
||||
max_heap_item_bytes: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(16_777_216)
|
||||
.default(1_048_576),
|
||||
});
|
||||
|
||||
const outputSchema = managedOutputSchemas.inspect_managed_artifact;
|
||||
if (outputSchema === undefined)
|
||||
throw new Error("Missing managed output schema for inspect_managed_artifact");
|
||||
const memberOutputSchema = managedOutputSchemas.inspect_managed_members;
|
||||
if (memberOutputSchema === undefined)
|
||||
throw new Error("Missing managed output schema for inspect_managed_members");
|
||||
const nativeBoundaryOutputSchema =
|
||||
managedOutputSchemas.inspect_managed_native_boundaries;
|
||||
if (nativeBoundaryOutputSchema === undefined)
|
||||
throw new Error(
|
||||
"Missing managed output schema for inspect_managed_native_boundaries",
|
||||
);
|
||||
|
||||
/** Read-only managed artifact contracts. */
|
||||
export const MANAGED_TOOL_CONTRACTS = [
|
||||
@@ -122,6 +157,26 @@ export const MANAGED_TOOL_CONTRACTS = [
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "inspect_managed_native_boundaries",
|
||||
description:
|
||||
"Inspect PE/CLI ModuleRef, ImplMap/PInvoke declarations, CLI native-header indicators, and non-IL method implementation flags without loading or executing target code. Results are managed declarations and degraded native-boundary observations, not verified native exports or addresses.",
|
||||
kind: "managed-provider",
|
||||
inputSchema: managedNativeBoundaryInputSchema,
|
||||
outputSchema: nativeBoundaryOutputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
},
|
||||
examples: [
|
||||
{
|
||||
title: "Example inspect managed/native boundary request",
|
||||
input: { import_limit: 50, implementation_limit: 50 },
|
||||
},
|
||||
],
|
||||
},
|
||||
] as const satisfies readonly ToolContract[];
|
||||
|
||||
/** Names of execution-free managed static operations. */
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
import {
|
||||
managedArtifactInspectionSchema,
|
||||
managedMemberInspectionSchema,
|
||||
managedNativeBoundaryInspectionSchema,
|
||||
} from "../domain/managedArtifact.js";
|
||||
import { managedMemberComparisonResultSchema } from "../domain/managedMemberComparison.js";
|
||||
import { artifactComparisonResultSchema } from "../domain/artifactComparison.js";
|
||||
@@ -538,6 +539,9 @@ export const artifactOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
|
||||
export const managedOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
|
||||
inspect_managed_artifact: resultOf(managedArtifactInspectionSchema),
|
||||
inspect_managed_members: resultOf(managedMemberInspectionSchema),
|
||||
inspect_managed_native_boundaries: resultOf(
|
||||
managedNativeBoundaryInspectionSchema,
|
||||
),
|
||||
};
|
||||
|
||||
/** Exact Evidence v2 schema for provider-neutral managed workflows. */
|
||||
|
||||
@@ -197,6 +197,70 @@ const managedMemberReferenceSchema = z.object({
|
||||
signature: managedSignatureSchema,
|
||||
});
|
||||
|
||||
const managedModuleReferenceSchema = z.object({
|
||||
token: tokenSchema,
|
||||
row_offset: offsetSchema,
|
||||
name: z.string(),
|
||||
});
|
||||
|
||||
const managedNativeImportSchema = z.object({
|
||||
token: tokenSchema,
|
||||
row_offset: offsetSchema,
|
||||
mapping_flags: z.number().int().min(0).max(0xffff),
|
||||
mapping_flags_hex: z.string().regex(/^0x[0-9a-f]{4}$/u),
|
||||
member_token: tokenSchema.nullable(),
|
||||
member_kind: z.enum(["method", "field", "unknown"]),
|
||||
member_name: z.string().nullable(),
|
||||
import_name: z.string(),
|
||||
import_scope_token: tokenSchema.nullable(),
|
||||
import_scope_name: z.string().nullable(),
|
||||
no_mangle: z.boolean(),
|
||||
char_set: z.enum(["not-specified", "ansi", "unicode", "auto", "unknown"]),
|
||||
call_convention: z.enum([
|
||||
"not-specified",
|
||||
"winapi",
|
||||
"cdecl",
|
||||
"stdcall",
|
||||
"thiscall",
|
||||
"fastcall",
|
||||
"unknown",
|
||||
]),
|
||||
supports_last_error: z.boolean(),
|
||||
best_fit: z.enum(["assembly-default", "enabled", "disabled", "unknown"]),
|
||||
throw_on_unmappable_char: z.enum([
|
||||
"assembly-default",
|
||||
"enabled",
|
||||
"disabled",
|
||||
"unknown",
|
||||
]),
|
||||
verification: z.literal("managed-declaration-only"),
|
||||
});
|
||||
|
||||
const managedNativeImplementationSchema = z.object({
|
||||
token: tokenSchema,
|
||||
row_offset: offsetSchema,
|
||||
name: z.string(),
|
||||
rva: z.number().int().min(0).max(0xffff_ffff),
|
||||
flags: z.number().int().min(0).max(0xffff),
|
||||
impl_flags: z.number().int().min(0).max(0xffff),
|
||||
code_type: z.enum(["il", "native", "optil", "runtime", "unknown"]),
|
||||
managed_kind: z.enum(["managed", "unmanaged", "unknown"]),
|
||||
pinvoke_declared: z.boolean(),
|
||||
boundary_kind: z.enum([
|
||||
"pinvoke",
|
||||
"native-body",
|
||||
"runtime-provided",
|
||||
"unmanaged-method",
|
||||
"mixed-or-unknown",
|
||||
]),
|
||||
body_interpretation: z.enum([
|
||||
"managed-cil",
|
||||
"native-or-runtime",
|
||||
"not-file-backed",
|
||||
"unknown",
|
||||
]),
|
||||
});
|
||||
|
||||
const managedCallEdgeSchema = z.object({
|
||||
caller_token: tokenSchema,
|
||||
caller: z.string().nullable(),
|
||||
@@ -367,10 +431,62 @@ export const managedMemberInspectionSchema = z.object({
|
||||
limitations: z.array(z.string()),
|
||||
});
|
||||
|
||||
/** Provider-neutral managed/native boundary observations from PE/CLI metadata. */
|
||||
export const managedNativeBoundaryInspectionSchema = z.object({
|
||||
schema_version: z.literal(1),
|
||||
artifact: z.object({
|
||||
path: z.string().min(1),
|
||||
sha256: digestSchema,
|
||||
byte_length: z.number().int().min(0),
|
||||
format: z.literal("pe"),
|
||||
}),
|
||||
module: moduleIdentitySchema.nullable(),
|
||||
metadata: z.object({
|
||||
status: z.enum(["absent", "complete", "partial", "malformed"]),
|
||||
version: z.string().nullable(),
|
||||
table_row_counts: z.record(
|
||||
z.string(),
|
||||
z.number().int().min(0).max(0xffff_ffff),
|
||||
),
|
||||
}),
|
||||
identity_scope: z.object({
|
||||
token_identity: z.literal("build-local"),
|
||||
requires_artifact_sha256: digestSchema,
|
||||
requires_mvid: z.string().uuid().nullable(),
|
||||
}),
|
||||
cli_native: z.object({
|
||||
il_only: z.boolean(),
|
||||
requires_32bit: z.boolean(),
|
||||
strong_name_signed: z.boolean(),
|
||||
native_entry_point: z.boolean(),
|
||||
ready_to_run_signature: z.boolean(),
|
||||
managed_native_header_rva: z.number().int().min(0).max(0xffff_ffff),
|
||||
managed_native_header_size: z.number().int().min(0).max(0xffff_ffff),
|
||||
}),
|
||||
module_refs: managedPage(managedModuleReferenceSchema),
|
||||
pinvoke_imports: managedPage(managedNativeImportSchema),
|
||||
native_implementations: managedPage(managedNativeImplementationSchema),
|
||||
summary: z.object({
|
||||
module_ref_count: z.number().int().min(0),
|
||||
pinvoke_import_count: z.number().int().min(0),
|
||||
native_implementation_count: z.number().int().min(0),
|
||||
ready_to_run: z.boolean(),
|
||||
mixed_mode_or_native_header: z.boolean(),
|
||||
}),
|
||||
coverage: z.object({
|
||||
state: z.enum(["complete", "partial", "unavailable"]),
|
||||
issues: z.array(managedParseIssueSchema),
|
||||
}),
|
||||
limitations: z.array(z.string()),
|
||||
});
|
||||
|
||||
export type ManagedArtifactInspection = z.infer<
|
||||
typeof managedArtifactInspectionSchema
|
||||
>;
|
||||
export type ManagedMemberInspection = z.infer<
|
||||
typeof managedMemberInspectionSchema
|
||||
>;
|
||||
export type ManagedNativeBoundaryInspection = z.infer<
|
||||
typeof managedNativeBoundaryInspectionSchema
|
||||
>;
|
||||
export type ManagedParseIssue = z.infer<typeof managedParseIssueSchema>;
|
||||
|
||||
@@ -0,0 +1,600 @@
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import {
|
||||
managedNativeBoundaryInspectionSchema,
|
||||
type ManagedNativeBoundaryInspection,
|
||||
type ManagedParseIssue,
|
||||
} from "../domain/managedArtifact.js";
|
||||
import {
|
||||
metadataRowOffset,
|
||||
type ManagedMetadataLayout,
|
||||
readManagedMetadataLayout,
|
||||
} from "./ManagedMetadataLayout.js";
|
||||
import {
|
||||
MetadataRowCursor,
|
||||
metadataToken,
|
||||
readMetadataString,
|
||||
} from "./ManagedMetadataHeaps.js";
|
||||
import {
|
||||
readManagedMetadataInventory,
|
||||
managedTableRowCounts,
|
||||
} from "./ManagedMetadataInventory.js";
|
||||
import {
|
||||
readManagedPeLayout,
|
||||
type ManagedPeLayout,
|
||||
} from "./ManagedPeReader.js";
|
||||
import {
|
||||
ManagedReaderFailure,
|
||||
managedFailure,
|
||||
} from "./ManagedReaderFailure.js";
|
||||
|
||||
/** Bounds for execution-free managed/native boundary inspection. */
|
||||
export interface ManagedNativeBoundaryInspectionLimits {
|
||||
readonly moduleRefOffset: number;
|
||||
readonly moduleRefLimit: number;
|
||||
readonly importOffset: number;
|
||||
readonly importLimit: number;
|
||||
readonly implementationOffset: number;
|
||||
readonly implementationLimit: number;
|
||||
readonly maxMetadataBytes: number;
|
||||
readonly maxTableRows: number;
|
||||
readonly maxHeapItemBytes: number;
|
||||
}
|
||||
|
||||
type Page<Item> = {
|
||||
readonly items: readonly Item[];
|
||||
readonly offset: number;
|
||||
readonly limit: number;
|
||||
readonly total: number;
|
||||
readonly returned: number;
|
||||
readonly dropped: number;
|
||||
readonly complete: boolean;
|
||||
};
|
||||
type ModuleRef =
|
||||
ManagedNativeBoundaryInspection["module_refs"]["items"][number];
|
||||
type NativeImport =
|
||||
ManagedNativeBoundaryInspection["pinvoke_imports"]["items"][number];
|
||||
type NativeImplementation =
|
||||
ManagedNativeBoundaryInspection["native_implementations"]["items"][number];
|
||||
|
||||
interface MemberCore {
|
||||
readonly token: string;
|
||||
readonly rowOffset: number;
|
||||
readonly kind: NativeImport["member_kind"];
|
||||
readonly name: string;
|
||||
readonly flags: number;
|
||||
readonly implFlags: number | null;
|
||||
readonly rva: number | null;
|
||||
}
|
||||
|
||||
const rowCursor = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
table: number,
|
||||
row: number,
|
||||
): MetadataRowCursor => {
|
||||
const descriptor = layout.table(table);
|
||||
const start = metadataRowOffset(layout, table, row);
|
||||
if (descriptor === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-row",
|
||||
`metadata.${String(table)}`,
|
||||
"Metadata table is absent",
|
||||
start,
|
||||
);
|
||||
return new MetadataRowCursor(
|
||||
bytes,
|
||||
start,
|
||||
start + descriptor.rowSize,
|
||||
`metadata.${descriptor.name}`,
|
||||
);
|
||||
};
|
||||
|
||||
const codedToken = (
|
||||
raw: number,
|
||||
tagBits: number,
|
||||
tables: readonly number[],
|
||||
): string | null => {
|
||||
const tagMask = (1 << tagBits) - 1;
|
||||
const table = tables[raw & tagMask];
|
||||
const row = raw >> tagBits;
|
||||
if (table === undefined || row === 0) return null;
|
||||
return metadataToken(table, row);
|
||||
};
|
||||
|
||||
const flagsHex = (value: number): string =>
|
||||
`0x${value.toString(16).padStart(4, "0")}`;
|
||||
|
||||
const page = <Item>(
|
||||
items: readonly Item[],
|
||||
offset: number,
|
||||
limit: number,
|
||||
): Page<Item> => {
|
||||
const safeOffset = Math.min(offset, items.length);
|
||||
const selected = items.slice(safeOffset, safeOffset + limit);
|
||||
return {
|
||||
items: selected,
|
||||
offset,
|
||||
limit,
|
||||
total: items.length,
|
||||
returned: selected.length,
|
||||
dropped: Math.max(0, items.length - safeOffset - selected.length),
|
||||
complete: safeOffset === 0 && selected.length === items.length,
|
||||
};
|
||||
};
|
||||
|
||||
const parseModuleRefs = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
maxBytes: number,
|
||||
): readonly ModuleRef[] => {
|
||||
const refs: ModuleRef[] = [];
|
||||
const table = layout.table(26);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 26, row);
|
||||
refs.push({
|
||||
token: metadataToken(26, row),
|
||||
row_offset: cursor.start,
|
||||
name: readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
maxBytes,
|
||||
),
|
||||
});
|
||||
}
|
||||
return refs;
|
||||
};
|
||||
|
||||
const parseFields = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
maxBytes: number,
|
||||
): ReadonlyMap<string, MemberCore> => {
|
||||
const fields = new Map<string, MemberCore>();
|
||||
const table = layout.table(4);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 4, row);
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
maxBytes,
|
||||
);
|
||||
cursor.readIndex(layout.blobIndexSize);
|
||||
const token = metadataToken(4, row);
|
||||
fields.set(token, {
|
||||
token,
|
||||
rowOffset: cursor.start,
|
||||
kind: "field",
|
||||
name,
|
||||
flags,
|
||||
implFlags: null,
|
||||
rva: null,
|
||||
});
|
||||
}
|
||||
return fields;
|
||||
};
|
||||
|
||||
const parseMethods = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
maxBytes: number,
|
||||
): ReadonlyMap<string, MemberCore> => {
|
||||
const methods = new Map<string, MemberCore>();
|
||||
const table = layout.table(6);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 6, row);
|
||||
const rva = cursor.readUInt32();
|
||||
const implFlags = cursor.readUInt16();
|
||||
const flags = cursor.readUInt16();
|
||||
const name = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
maxBytes,
|
||||
);
|
||||
cursor.readIndex(layout.blobIndexSize);
|
||||
cursor.readIndex(layout.tableIndexSize(8));
|
||||
const token = metadataToken(6, row);
|
||||
methods.set(token, {
|
||||
token,
|
||||
rowOffset: cursor.start,
|
||||
kind: "method",
|
||||
name,
|
||||
flags,
|
||||
implFlags,
|
||||
rva,
|
||||
});
|
||||
}
|
||||
return methods;
|
||||
};
|
||||
|
||||
const parseImplMaps = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedMetadataLayout,
|
||||
maxBytes: number,
|
||||
modules: readonly ModuleRef[],
|
||||
members: ReadonlyMap<string, MemberCore>,
|
||||
): readonly NativeImport[] => {
|
||||
const moduleNames = new Map(modules.map((module) => [module.token, module]));
|
||||
const imports: NativeImport[] = [];
|
||||
const table = layout.table(28);
|
||||
for (let row = 1; row <= (table?.rowCount ?? 0); row += 1) {
|
||||
const cursor = rowCursor(bytes, layout, 28, row);
|
||||
const mappingFlags = cursor.readUInt16();
|
||||
const memberToken = codedToken(
|
||||
cursor.readIndex(layout.codedIndexSize("MemberForwarded")),
|
||||
1,
|
||||
[4, 6],
|
||||
);
|
||||
const importName = readMetadataString(
|
||||
bytes,
|
||||
layout,
|
||||
cursor.readIndex(layout.stringIndexSize),
|
||||
maxBytes,
|
||||
);
|
||||
const importScopeRow = cursor.readIndex(layout.tableIndexSize(26));
|
||||
const importScopeToken =
|
||||
importScopeRow === 0 ? null : metadataToken(26, importScopeRow);
|
||||
const member = memberToken === null ? undefined : members.get(memberToken);
|
||||
imports.push({
|
||||
token: metadataToken(28, row),
|
||||
row_offset: cursor.start,
|
||||
mapping_flags: mappingFlags,
|
||||
mapping_flags_hex: flagsHex(mappingFlags),
|
||||
member_token: memberToken,
|
||||
member_kind: member?.kind ?? "unknown",
|
||||
member_name: member?.name ?? null,
|
||||
import_name: importName,
|
||||
import_scope_token: importScopeToken,
|
||||
import_scope_name:
|
||||
importScopeToken === null
|
||||
? null
|
||||
: (moduleNames.get(importScopeToken)?.name ?? null),
|
||||
no_mangle: (mappingFlags & 0x0001) !== 0,
|
||||
char_set: charSet(mappingFlags),
|
||||
call_convention: callConvention(mappingFlags),
|
||||
supports_last_error: (mappingFlags & 0x0040) !== 0,
|
||||
best_fit: bestFit(mappingFlags),
|
||||
throw_on_unmappable_char: throwOnUnmappable(mappingFlags),
|
||||
verification: "managed-declaration-only",
|
||||
});
|
||||
}
|
||||
return imports;
|
||||
};
|
||||
|
||||
const charSet = (flags: number): NativeImport["char_set"] => {
|
||||
switch (flags & 0x0006) {
|
||||
case 0x0000:
|
||||
return "not-specified";
|
||||
case 0x0002:
|
||||
return "ansi";
|
||||
case 0x0004:
|
||||
return "unicode";
|
||||
case 0x0006:
|
||||
return "auto";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
};
|
||||
|
||||
const callConvention = (flags: number): NativeImport["call_convention"] => {
|
||||
switch (flags & 0x0700) {
|
||||
case 0x0000:
|
||||
return "not-specified";
|
||||
case 0x0100:
|
||||
return "winapi";
|
||||
case 0x0200:
|
||||
return "cdecl";
|
||||
case 0x0300:
|
||||
return "stdcall";
|
||||
case 0x0400:
|
||||
return "thiscall";
|
||||
case 0x0500:
|
||||
return "fastcall";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
};
|
||||
|
||||
const bestFit = (flags: number): NativeImport["best_fit"] => {
|
||||
switch (flags & 0x0030) {
|
||||
case 0x0000:
|
||||
return "assembly-default";
|
||||
case 0x0010:
|
||||
return "enabled";
|
||||
case 0x0020:
|
||||
return "disabled";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
};
|
||||
|
||||
const throwOnUnmappable = (
|
||||
flags: number,
|
||||
): NativeImport["throw_on_unmappable_char"] => {
|
||||
switch (flags & 0x3000) {
|
||||
case 0x0000:
|
||||
return "assembly-default";
|
||||
case 0x1000:
|
||||
return "enabled";
|
||||
case 0x2000:
|
||||
return "disabled";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
};
|
||||
|
||||
const nativeImplementations = (
|
||||
methods: Iterable<MemberCore>,
|
||||
pinvokeTokens: ReadonlySet<string>,
|
||||
): readonly NativeImplementation[] => {
|
||||
const implementations: NativeImplementation[] = [];
|
||||
for (const method of methods) {
|
||||
const implFlags = method.implFlags ?? 0;
|
||||
const codeType = codeTypeFor(implFlags);
|
||||
const managedKind = managedKindFor(implFlags);
|
||||
const pinvokeDeclared =
|
||||
pinvokeTokens.has(method.token) || (method.flags & 0x2000) !== 0;
|
||||
if (!pinvokeDeclared && codeType === "il" && managedKind === "managed")
|
||||
continue;
|
||||
implementations.push({
|
||||
token: method.token,
|
||||
row_offset: method.rowOffset,
|
||||
name: method.name,
|
||||
rva: method.rva ?? 0,
|
||||
flags: method.flags,
|
||||
impl_flags: implFlags,
|
||||
code_type: codeType,
|
||||
managed_kind: managedKind,
|
||||
pinvoke_declared: pinvokeDeclared,
|
||||
boundary_kind: boundaryKind(pinvokeDeclared, codeType, managedKind),
|
||||
body_interpretation:
|
||||
codeType === "il" && managedKind === "managed" && !pinvokeDeclared
|
||||
? "managed-cil"
|
||||
: method.rva === 0
|
||||
? "not-file-backed"
|
||||
: "native-or-runtime",
|
||||
});
|
||||
}
|
||||
return implementations;
|
||||
};
|
||||
|
||||
const codeTypeFor = (implFlags: number): NativeImplementation["code_type"] => {
|
||||
switch (implFlags & 0x0003) {
|
||||
case 0x0000:
|
||||
return "il";
|
||||
case 0x0001:
|
||||
return "native";
|
||||
case 0x0002:
|
||||
return "optil";
|
||||
case 0x0003:
|
||||
return "runtime";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
};
|
||||
|
||||
const managedKindFor = (
|
||||
implFlags: number,
|
||||
): NativeImplementation["managed_kind"] => {
|
||||
switch (implFlags & 0x0004) {
|
||||
case 0x0000:
|
||||
return "managed";
|
||||
case 0x0004:
|
||||
return "unmanaged";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
};
|
||||
|
||||
const boundaryKind = (
|
||||
pinvokeDeclared: boolean,
|
||||
codeType: NativeImplementation["code_type"],
|
||||
managedKind: NativeImplementation["managed_kind"],
|
||||
): NativeImplementation["boundary_kind"] => {
|
||||
if (pinvokeDeclared) return "pinvoke";
|
||||
if (codeType === "native") return "native-body";
|
||||
if (codeType === "runtime") return "runtime-provided";
|
||||
if (managedKind === "unmanaged") return "unmanaged-method";
|
||||
return "mixed-or-unknown";
|
||||
};
|
||||
|
||||
const cliNative = (
|
||||
pe: ManagedPeLayout,
|
||||
): ManagedNativeBoundaryInspection["cli_native"] => {
|
||||
if (pe.cli === null)
|
||||
return {
|
||||
il_only: false,
|
||||
requires_32bit: false,
|
||||
strong_name_signed: false,
|
||||
native_entry_point: false,
|
||||
ready_to_run_signature: false,
|
||||
managed_native_header_rva: 0,
|
||||
managed_native_header_size: 0,
|
||||
};
|
||||
return {
|
||||
il_only: (pe.cli.flags & 0x0000_0001) !== 0,
|
||||
requires_32bit: (pe.cli.flags & 0x0000_0002) !== 0,
|
||||
strong_name_signed: (pe.cli.flags & 0x0000_0008) !== 0,
|
||||
native_entry_point: (pe.cli.flags & 0x0000_0010) !== 0,
|
||||
ready_to_run_signature: pe.cli.readyToRunSignature,
|
||||
managed_native_header_rva: pe.cli.managedNativeHeader.rva,
|
||||
managed_native_header_size: pe.cli.managedNativeHeader.size,
|
||||
};
|
||||
};
|
||||
|
||||
const unavailable = (
|
||||
target: BinaryTarget,
|
||||
bytes: Buffer,
|
||||
limits: ManagedNativeBoundaryInspectionLimits,
|
||||
issue: ManagedParseIssue | null,
|
||||
): ManagedNativeBoundaryInspection =>
|
||||
managedNativeBoundaryInspectionSchema.parse({
|
||||
schema_version: 1,
|
||||
artifact: {
|
||||
path: target.path,
|
||||
sha256: target.sha256,
|
||||
byte_length: bytes.length,
|
||||
format: "pe",
|
||||
},
|
||||
module: null,
|
||||
metadata: { status: "absent", version: null, table_row_counts: {} },
|
||||
identity_scope: {
|
||||
token_identity: "build-local",
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: null,
|
||||
},
|
||||
cli_native: {
|
||||
il_only: false,
|
||||
requires_32bit: false,
|
||||
strong_name_signed: false,
|
||||
native_entry_point: false,
|
||||
ready_to_run_signature: false,
|
||||
managed_native_header_rva: 0,
|
||||
managed_native_header_size: 0,
|
||||
},
|
||||
module_refs: page([], limits.moduleRefOffset, limits.moduleRefLimit),
|
||||
pinvoke_imports: page([], limits.importOffset, limits.importLimit),
|
||||
native_implementations: page(
|
||||
[],
|
||||
limits.implementationOffset,
|
||||
limits.implementationLimit,
|
||||
),
|
||||
summary: {
|
||||
module_ref_count: 0,
|
||||
pinvoke_import_count: 0,
|
||||
native_implementation_count: 0,
|
||||
ready_to_run: false,
|
||||
mixed_mode_or_native_header: false,
|
||||
},
|
||||
coverage: {
|
||||
state: "unavailable",
|
||||
issues: issue === null ? [] : [issue],
|
||||
},
|
||||
limitations: [
|
||||
"The PE has no admitted CLI metadata; managed/native boundary inspection is unavailable.",
|
||||
],
|
||||
});
|
||||
|
||||
/** Inspect PE/CLI managed/native boundary declarations without executing code. */
|
||||
export const inspectManagedNativeBoundariesBytes = (
|
||||
bytes: Buffer,
|
||||
target: BinaryTarget,
|
||||
limits: ManagedNativeBoundaryInspectionLimits,
|
||||
): ManagedNativeBoundaryInspection => {
|
||||
const pe = readManagedPeLayout(bytes);
|
||||
if (pe.cli === null) return unavailable(target, bytes, limits, pe.cliIssue);
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
try {
|
||||
const rootOffset = pe.rvaToOffset(
|
||||
pe.cli.metadata.rva,
|
||||
pe.cli.metadata.size,
|
||||
"cli.metadata",
|
||||
);
|
||||
if (pe.cli.metadata.size > limits.maxMetadataBytes)
|
||||
throw managedFailure(
|
||||
"limit-exceeded",
|
||||
"metadata.root",
|
||||
`CLI metadata size exceeds max_metadata_bytes ${String(limits.maxMetadataBytes)}`,
|
||||
rootOffset,
|
||||
);
|
||||
const layout = readManagedMetadataLayout(
|
||||
bytes,
|
||||
rootOffset,
|
||||
pe.cli.metadata.size,
|
||||
limits.maxTableRows,
|
||||
);
|
||||
const inventory = readManagedMetadataInventory(
|
||||
bytes,
|
||||
layout,
|
||||
{
|
||||
referenceOffset: 0,
|
||||
referenceLimit: 1,
|
||||
resourceOffset: 0,
|
||||
resourceLimit: 1,
|
||||
attributeOffset: 0,
|
||||
attributeLimit: 1,
|
||||
maxHeapItemBytes: limits.maxHeapItemBytes,
|
||||
},
|
||||
null,
|
||||
);
|
||||
issues.push(...inventory.issues);
|
||||
const moduleRefs = parseModuleRefs(bytes, layout, limits.maxHeapItemBytes);
|
||||
const fields = parseFields(bytes, layout, limits.maxHeapItemBytes);
|
||||
const methods = parseMethods(bytes, layout, limits.maxHeapItemBytes);
|
||||
const members = new Map([...fields, ...methods]);
|
||||
const imports = parseImplMaps(
|
||||
bytes,
|
||||
layout,
|
||||
limits.maxHeapItemBytes,
|
||||
moduleRefs,
|
||||
members,
|
||||
);
|
||||
const implementations = nativeImplementations(
|
||||
methods.values(),
|
||||
new Set(
|
||||
imports
|
||||
.map(({ member_token: token }) => token)
|
||||
.filter((token): token is string => token !== null),
|
||||
),
|
||||
);
|
||||
const native = cliNative(pe);
|
||||
return managedNativeBoundaryInspectionSchema.parse({
|
||||
schema_version: 1,
|
||||
artifact: {
|
||||
path: target.path,
|
||||
sha256: target.sha256,
|
||||
byte_length: bytes.length,
|
||||
format: "pe",
|
||||
},
|
||||
module: inventory.module,
|
||||
metadata: {
|
||||
status: issues.length === 0 ? "complete" : "partial",
|
||||
version: layout.version,
|
||||
table_row_counts: managedTableRowCounts(layout),
|
||||
},
|
||||
identity_scope: {
|
||||
token_identity: "build-local",
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: inventory.module?.mvid ?? null,
|
||||
},
|
||||
cli_native: native,
|
||||
module_refs: page(
|
||||
moduleRefs,
|
||||
limits.moduleRefOffset,
|
||||
limits.moduleRefLimit,
|
||||
),
|
||||
pinvoke_imports: page(imports, limits.importOffset, limits.importLimit),
|
||||
native_implementations: page(
|
||||
implementations,
|
||||
limits.implementationOffset,
|
||||
limits.implementationLimit,
|
||||
),
|
||||
summary: {
|
||||
module_ref_count: moduleRefs.length,
|
||||
pinvoke_import_count: imports.length,
|
||||
native_implementation_count: implementations.length,
|
||||
ready_to_run: native.ready_to_run_signature,
|
||||
mixed_mode_or_native_header:
|
||||
native.managed_native_header_rva !== 0 ||
|
||||
native.managed_native_header_size !== 0 ||
|
||||
native.native_entry_point,
|
||||
},
|
||||
coverage: {
|
||||
state: issues.length === 0 ? "complete" : "partial",
|
||||
issues,
|
||||
},
|
||||
limitations: [
|
||||
"P/Invoke rows prove managed import declarations only; this inspection does not verify that a native library, export, thunk, or provider-qualified function exists.",
|
||||
"Managed metadata tokens are build-local coordinates and are only meaningful with the reported artifact SHA-256 and MVID.",
|
||||
"ReadyToRun, NativeAOT, C++/CLI, and IL2CPP native semantics require separately selected native-provider evidence; this tool does not translate managed tokens into native addresses.",
|
||||
],
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof ManagedReaderFailure)
|
||||
return unavailable(target, bytes, limits, cause.issue);
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
MANAGED_TOOL_CONTRACTS,
|
||||
managedArtifactInputSchema,
|
||||
managedMemberInputSchema,
|
||||
managedNativeBoundaryInputSchema,
|
||||
type ManagedToolName,
|
||||
} from "../contracts/managedToolContracts.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
@@ -29,10 +30,12 @@ import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type {
|
||||
ManagedArtifactInspection,
|
||||
ManagedMemberInspection,
|
||||
ManagedNativeBoundaryInspection,
|
||||
} from "../domain/managedArtifact.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { inspectManagedArtifactBytes } from "./ManagedArtifactInspector.js";
|
||||
import { inspectManagedMembersBytes } from "./ManagedMemberInspector.js";
|
||||
import { inspectManagedNativeBoundariesBytes } from "./ManagedNativeBoundaryInspector.js";
|
||||
|
||||
const IDENTITY: ProviderIdentity = Object.freeze(MANAGED_STATIC_PROVIDER);
|
||||
|
||||
@@ -177,10 +180,15 @@ const limitationsFor = (operation: ManagedToolName): readonly string[] =>
|
||||
"This capability inventories PE/CLI identity only; inspect_managed_members admits bounded metadata members, signatures, and CIL anchors.",
|
||||
"It never loads the target assembly, resolves dependencies through a CLR, decompiles C#, or executes target code.",
|
||||
]
|
||||
: [
|
||||
"This capability decodes bounded PE/CLI metadata members, signatures, and file-backed method bodies; decompiled C# and cross-build matching are separate future contracts.",
|
||||
"It never loads the target assembly, resolves dependencies through a CLR, decompiles C#, or executes target code.",
|
||||
],
|
||||
: operation === "inspect_managed_native_boundaries"
|
||||
? [
|
||||
"This capability inventories managed declarations for native boundaries only; native exports, thunks, and addresses require separate native-provider evidence.",
|
||||
"It never loads the target assembly, resolves dependencies through a CLR, decompiles C#, or executes target code.",
|
||||
]
|
||||
: [
|
||||
"This capability decodes bounded PE/CLI metadata members, signatures, and file-backed method bodies; decompiled C# and cross-build matching are separate future contracts.",
|
||||
"It never loads the target assembly, resolves dependencies through a CLR, decompiles C#, or executes target code.",
|
||||
],
|
||||
);
|
||||
|
||||
const maxRequestedFileBytes = (
|
||||
@@ -189,6 +197,8 @@ const maxRequestedFileBytes = (
|
||||
): number => {
|
||||
if (operation === "inspect_managed_artifact")
|
||||
return managedArtifactInputSchema.parse(parameters).max_file_bytes;
|
||||
if (operation === "inspect_managed_native_boundaries")
|
||||
return managedNativeBoundaryInputSchema.parse(parameters).max_file_bytes;
|
||||
return managedMemberInputSchema.parse(parameters).max_file_bytes;
|
||||
};
|
||||
|
||||
@@ -197,7 +207,10 @@ const inspectManagedOperation = (
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
bytes: Buffer,
|
||||
target: BinaryTarget,
|
||||
): ManagedArtifactInspection | ManagedMemberInspection => {
|
||||
):
|
||||
| ManagedArtifactInspection
|
||||
| ManagedMemberInspection
|
||||
| ManagedNativeBoundaryInspection => {
|
||||
if (operation === "inspect_managed_artifact") {
|
||||
const input = managedArtifactInputSchema.parse(parameters);
|
||||
return inspectManagedArtifactBytes(bytes, target, {
|
||||
@@ -212,6 +225,20 @@ const inspectManagedOperation = (
|
||||
maxHeapItemBytes: input.max_heap_item_bytes,
|
||||
});
|
||||
}
|
||||
if (operation === "inspect_managed_native_boundaries") {
|
||||
const input = managedNativeBoundaryInputSchema.parse(parameters);
|
||||
return inspectManagedNativeBoundariesBytes(bytes, target, {
|
||||
moduleRefOffset: input.module_ref_offset,
|
||||
moduleRefLimit: input.module_ref_limit,
|
||||
importOffset: input.import_offset,
|
||||
importLimit: input.import_limit,
|
||||
implementationOffset: input.implementation_offset,
|
||||
implementationLimit: input.implementation_limit,
|
||||
maxMetadataBytes: input.max_metadata_bytes,
|
||||
maxTableRows: input.max_table_rows,
|
||||
maxHeapItemBytes: input.max_heap_item_bytes,
|
||||
});
|
||||
}
|
||||
const input = managedMemberInputSchema.parse(parameters);
|
||||
return inspectManagedMembersBytes(bytes, target, {
|
||||
typeOffset: input.type_offset,
|
||||
@@ -234,7 +261,10 @@ const inspectManagedOperation = (
|
||||
};
|
||||
|
||||
const managedLocations = (
|
||||
result: ManagedArtifactInspection | ManagedMemberInspection,
|
||||
result:
|
||||
| ManagedArtifactInspection
|
||||
| ManagedMemberInspection
|
||||
| ManagedNativeBoundaryInspection,
|
||||
): readonly EvidenceLocation[] => {
|
||||
if ("pe" in result) {
|
||||
if (result.pe.cli === null) return [{ kind: "file-offset", offset: 0 }];
|
||||
@@ -252,16 +282,26 @@ const managedLocations = (
|
||||
})),
|
||||
];
|
||||
}
|
||||
if ("methods" in result)
|
||||
return [
|
||||
...(result.module === null
|
||||
? [{ kind: "file-offset" as const, offset: 0 }]
|
||||
: [{ kind: "file-offset" as const, offset: result.module.row_offset }]),
|
||||
...result.methods.items
|
||||
.filter((method) => method.body.file_offset !== null)
|
||||
.slice(0, 8)
|
||||
.map((method) => ({
|
||||
kind: "file-offset" as const,
|
||||
offset: method.body.file_offset ?? 0,
|
||||
})),
|
||||
];
|
||||
return [
|
||||
...(result.module === null
|
||||
? [{ kind: "file-offset" as const, offset: 0 }]
|
||||
: [{ kind: "file-offset" as const, offset: result.module.row_offset }]),
|
||||
...result.methods.items
|
||||
.filter((method) => method.body.file_offset !== null)
|
||||
.slice(0, 8)
|
||||
.map((method) => ({
|
||||
kind: "file-offset" as const,
|
||||
offset: method.body.file_offset ?? 0,
|
||||
})),
|
||||
...result.pinvoke_imports.items.slice(0, 8).map((mapping) => ({
|
||||
kind: "file-offset" as const,
|
||||
offset: mapping.row_offset,
|
||||
})),
|
||||
];
|
||||
};
|
||||
|
||||
@@ -5,5 +5,5 @@ export const PACKAGE_METADATA = {
|
||||
serverSdkVersion: "2.0.0-beta.4",
|
||||
clientSdkVersion: "2.0.0-beta.4",
|
||||
coreSdkVersion: "2.0.0-beta.4",
|
||||
skillVersion: "19",
|
||||
skillVersion: "20",
|
||||
} as const;
|
||||
|
||||
Reference in New Issue
Block a user