feat(dotnet): add managed native boundary inspection

This commit is contained in:
N0zoM1z0
2026-07-16 18:57:56 +09:00
committed by N0zoM1z0
parent 55794d6083
commit 466b1bbebe
88 changed files with 1394 additions and 195 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ describe("browser MCP tools", () => {
const connected = await connectBrowser(browser);
const tools = await connected.client.listTools();
expect(tools.tools).toHaveLength(86);
expect(tools.tools).toHaveLength(87);
expect(tools.tools.map(({ name }) => name)).toEqual(
expect.arrayContaining([
"list_browser_targets",
+3 -3
View File
@@ -66,7 +66,7 @@ describe("tool contract surface", () => {
...ELECTRON_TOOL_CONTRACTS,
...APPLICATION_TOOL_CONTRACTS,
];
expect(contracts).toHaveLength(68);
expect(contracts).toHaveLength(69);
for (const contract of contracts) {
const inputSchema = contractJsonSchema(contract.inputSchema);
const outputSchema = contractJsonSchema(contract.outputSchema);
@@ -164,7 +164,7 @@ describe("tool contract surface", () => {
}
});
it("publishes no unconstrained output-schema holes across all 86 tools", () => {
it("publishes no unconstrained output-schema holes across all 87 tools", () => {
const contracts = [
...OFFICIAL_TOOL_CONTRACTS,
...ENHANCED_TOOL_CONTRACTS,
@@ -177,7 +177,7 @@ describe("tool contract surface", () => {
...APPLICATION_TOOL_CONTRACTS,
...SESSION_TOOL_CONTRACTS,
];
expect(contracts).toHaveLength(86);
expect(contracts).toHaveLength(87);
for (const contract of contracts) {
const schema = contractJsonSchema(contract.outputSchema);
expect(emptySchemaPaths(schema), contract.name).toEqual([]);
+64 -4
View File
@@ -6,6 +6,11 @@ interface ManagedPeFixtureOptions {
readonly metadataValidMaskExtra?: bigint;
readonly methodName?: string;
readonly mvid?: Buffer;
readonly pinvoke?: {
readonly importName?: string;
readonly mappingFlags?: number;
readonly moduleName?: string;
};
readonly readyToRun?: boolean;
readonly references?: readonly string[];
readonly resourceData?: Buffer;
@@ -104,11 +109,17 @@ const typeDefRow = (
const fieldRow = (name: number, signature: number): Buffer =>
Buffer.concat([u16(0x0001), u16(name), u16(signature)]);
const methodDefRow = (name: number, signature: number, rva = 0): Buffer =>
const methodDefRow = (
name: number,
signature: number,
rva = 0,
flags = 0x0016,
implFlags = 0,
): Buffer =>
Buffer.concat([
u32(rva),
u16(0),
u16(0x0016),
u16(implFlags),
u16(flags),
u16(name),
u16(signature),
u16(0),
@@ -117,6 +128,20 @@ const methodDefRow = (name: number, signature: number, rva = 0): Buffer =>
const memberRefRow = (name: number, signature: number): Buffer =>
Buffer.concat([u16((1 << 3) | 1), u16(name), u16(signature)]);
const moduleRefRow = (name: number): Buffer => Buffer.concat([u16(name)]);
const implMapRow = (
importName: number,
importScope: number,
mappingFlags: number,
): Buffer =>
Buffer.concat([
u16(mappingFlags),
u16((1 << 1) | 1),
u16(importName),
u16(importScope),
]);
const assemblyRow = (name: number): Buffer =>
Buffer.concat([
u32(0x0000_8004),
@@ -253,6 +278,14 @@ export const buildManagedPeFixture = (
const typeNamespace = strings.add(options.typeNamespace ?? "Fixture");
const fieldName = strings.add(options.fieldName ?? "counter");
const methodName = strings.add(options.methodName ?? "Main");
const pinvokeModuleName =
options.pinvoke === undefined
? null
: strings.add(options.pinvoke.moduleName ?? "user32.dll");
const pinvokeImportName =
options.pinvoke === undefined
? null
: strings.add(options.pinvoke.importName ?? "MessageBoxW");
const resourceName = strings.add("Fixture.resources");
const referenceNames = options.references ?? ["System.Runtime"];
const referenceStringIndexes = referenceNames.map((name) =>
@@ -272,9 +305,36 @@ export const buildManagedPeFixture = (
[1, [typeRefRow(attributeName, attributeNamespace)]],
[2, [typeDefRow(typeName, typeNamespace, 1, 1)]],
[4, [fieldRow(fieldName, fieldSignature)]],
[6, [methodDefRow(methodName, methodSignature, 0x2800)]],
[
6,
[
methodDefRow(
methodName,
methodSignature,
0x2800,
options.pinvoke === undefined ? 0x0016 : 0x2016,
),
],
],
[10, [memberRefRow(constructorName, constructorSignature)]],
[12, [customAttributeRow(attributeBlob)]],
...(pinvokeModuleName === null
? []
: ([[26, [moduleRefRow(pinvokeModuleName)]]] as const)),
...(pinvokeImportName === null
? []
: ([
[
28,
[
implMapRow(
pinvokeImportName,
1,
options.pinvoke?.mappingFlags ?? 0x0344,
),
],
],
] as const)),
[32, [assemblyRow(assemblyName)]],
[35, referenceStringIndexes.map((name) => assemblyRefRow(name, tokenBlob))],
[40, [manifestResourceRow(resourceName)]],
+2 -2
View File
@@ -131,7 +131,7 @@ describe("full MCP integration with multi-tool sequences", () => {
});
});
it("preserves the complete 86-tool inventory with a session", async () => {
it("preserves the complete 87-tool inventory with a session", async () => {
const session = new BinarySession(
(_path) =>
({
@@ -154,7 +154,7 @@ describe("full MCP integration with multi-tool sequences", () => {
await client.connect(clientTransport);
const listed = await client.listTools();
expect(listed.tools).toHaveLength(86);
expect(listed.tools).toHaveLength(87);
const names = listed.tools.map((t) => t.name);
expect(names).toContain("open_binary");
expect(names).toContain("close_binary");
+108
View File
@@ -10,6 +10,7 @@ import type { AnalysisExecution } from "../src/application/AnalysisProvider.js";
import {
managedArtifactInspectionSchema,
managedMemberInspectionSchema,
managedNativeBoundaryInspectionSchema,
} from "../src/domain/managedArtifact.js";
import type { BinaryTarget } from "../src/domain/binaryTarget.js";
import { parseBinaryTarget } from "../src/domain/binaryTarget.js";
@@ -17,6 +18,7 @@ import { parseEvidence } from "../src/domain/evidence.js";
import { ManagedStaticProvider } from "../src/dotnet/ManagedStaticProvider.js";
import { inspectManagedArtifactBytes } from "../src/dotnet/ManagedArtifactInspector.js";
import { inspectManagedMembersBytes } from "../src/dotnet/ManagedMemberInspector.js";
import { inspectManagedNativeBoundariesBytes } from "../src/dotnet/ManagedNativeBoundaryInspector.js";
import {
buildManagedPeFixture,
buildNativePeFixture,
@@ -53,6 +55,18 @@ const memberLimits = {
maxMethodInstructions: 1_000,
};
const nativeBoundaryLimits = {
moduleRefOffset: 0,
moduleRefLimit: 100,
importOffset: 0,
importLimit: 100,
implementationOffset: 0,
implementationLimit: 100,
maxMetadataBytes: 1024 * 1024,
maxTableRows: 1_000,
maxHeapItemBytes: 1024 * 1024,
};
describe("managed PE/CLI static provider", () => {
it("inventories module, assembly, framework, references, and resources without loading CLR code", () => {
const resource = Buffer.from("source-owned resource");
@@ -221,6 +235,71 @@ describe("managed PE/CLI static provider", () => {
expect(result.coverage).toMatchObject({ state: "complete", issues: [] });
});
it("inspects managed/native PInvoke declarations without verifying native exports", () => {
const bytes = buildManagedPeFixture({
pinvoke: {
moduleName: "user32.dll",
importName: "MessageBoxW",
mappingFlags: 0x0345,
},
readyToRun: true,
});
const result = inspectManagedNativeBoundariesBytes(
bytes,
target(bytes),
nativeBoundaryLimits,
);
expect(result.cli_native).toMatchObject({
il_only: true,
ready_to_run_signature: true,
managed_native_header_rva: 0x2700,
managed_native_header_size: 4,
});
expect(result.module_refs.items).toEqual([
{
token: "0x1a000001",
row_offset: expect.any(Number),
name: "user32.dll",
},
]);
expect(result.pinvoke_imports.items).toEqual([
expect.objectContaining({
token: "0x1c000001",
member_token: "0x06000001",
member_kind: "method",
member_name: "Main",
import_name: "MessageBoxW",
import_scope_token: "0x1a000001",
import_scope_name: "user32.dll",
no_mangle: true,
char_set: "unicode",
call_convention: "stdcall",
supports_last_error: true,
verification: "managed-declaration-only",
}),
]);
expect(result.native_implementations.items).toEqual([
expect.objectContaining({
token: "0x06000001",
name: "Main",
pinvoke_declared: true,
boundary_kind: "pinvoke",
body_interpretation: "native-or-runtime",
}),
]);
expect(result.summary).toMatchObject({
module_ref_count: 1,
pinvoke_import_count: 1,
native_implementation_count: 1,
ready_to_run: true,
mixed_mode_or_native_header: true,
});
expect(result.limitations).toContain(
"P/Invoke rows prove managed import declarations only; this inspection does not verify that a native library, export, thunk, or provider-qualified function exists.",
);
});
it("keeps member evidence bounded and typed for pagination and unavailable metadata", () => {
const bytes = buildManagedPeFixture();
const paged = inspectManagedMembersBytes(bytes, target(bytes), {
@@ -334,6 +413,19 @@ describe("managed PE/CLI static provider", () => {
field_accesses: { total: 1 },
});
const boundaries = await client.execute(
"inspect_managed_native_boundaries",
{},
);
expect(boundaries.ok).toBe(true);
if (!boundaries.ok) return;
expect(asManagedNativeBoundaryResult(boundaries.value)).toMatchObject({
artifact: { path, sha256: parsed.value.sha256, format: "pe" },
module_refs: { total: 0 },
pinvoke_imports: { total: 0 },
native_implementations: { total: 0 },
});
const cancelled = new AbortController();
cancelled.abort();
const cancelledResult = await client.execute(
@@ -400,6 +492,19 @@ describe("managed PE/CLI static provider", () => {
methods: { total: 1 },
},
});
const boundaryEvidence = parseEvidence(
await runProviderAnalysis(path, "inspect_managed_native_boundaries", {}),
);
expect(boundaryEvidence).toMatchObject({
operation: "inspect_managed_native_boundaries",
provider: { id: "rea-dotnet-static" },
subject: { local_path: path, format: "pe" },
normalized_result: {
identity_scope: { token_identity: "build-local" },
pinvoke_imports: { total: 0 },
},
});
});
});
@@ -417,3 +522,6 @@ const asManagedResult = (execution: AnalysisExecution) =>
const asManagedMemberResult = (execution: AnalysisExecution) =>
managedMemberInspectionSchema.parse(execution.result);
const asManagedNativeBoundaryResult = (execution: AnalysisExecution) =>
managedNativeBoundaryInspectionSchema.parse(execution.result);
+21
View File
@@ -43,6 +43,9 @@ describe("managed artifact MCP tools", () => {
expect(tools.tools.map(({ name }) => name)).toContain(
"inspect_managed_members",
);
expect(tools.tools.map(({ name }) => name)).toContain(
"inspect_managed_native_boundaries",
);
expect(tools.tools.map(({ name }) => name)).toContain(
"compare_managed_members",
);
@@ -90,6 +93,24 @@ describe("managed artifact MCP tools", () => {
},
});
const boundaries = structured(
await client.callTool({
name: "inspect_managed_native_boundaries",
arguments: { import_limit: 1 },
}),
);
expect(boundaries).toMatchObject({
operation: "inspect_managed_native_boundaries",
provider: { id: "rea-dotnet-static" },
subject: { local_path: path, format: "pe" },
normalized_result: {
identity_scope: { token_identity: "build-local" },
pinvoke_imports: { total: 0, limit: 1 },
native_implementations: { total: 0 },
},
});
await client.callTool({
name: "open_binary",
arguments: { path: rightPath },
+1 -1
View File
@@ -54,7 +54,7 @@ describe("guided prompts over MCP", () => {
),
);
}
expect((await client.listTools()).tools).toHaveLength(86);
expect((await client.listTools()).tools).toHaveLength(87);
const result = await client.getPrompt({
name: "investigate_feature",
arguments: {
+3 -3
View File
@@ -10,7 +10,7 @@ const fixturePath = fileURLToPath(
);
describe("production stdio runtime", () => {
it("starts the built entrypoint, lists 86 tools, calls one, and shuts down", async () => {
it("starts the built entrypoint, lists 87 tools, calls one, and shuts down", async () => {
const transport = new StdioClientTransport({
command: process.execPath,
args: [mainPath],
@@ -32,7 +32,7 @@ describe("production stdio runtime", () => {
try {
await client.connect(transport);
const tools = await client.listTools();
expect(tools.tools).toHaveLength(86);
expect(tools.tools).toHaveLength(87);
const result = await client.callTool({
name: "current_document",
arguments: {},
@@ -78,7 +78,7 @@ describe("production stdio runtime", () => {
try {
await client.connect(transport);
expect((await client.listTools()).tools).toHaveLength(86);
expect((await client.listTools()).tools).toHaveLength(87);
} finally {
await client.close();
await transport.close();
+5 -5
View File
@@ -24,11 +24,11 @@ describe("server and catalog identity", () => {
});
expect(PRODUCT_IDENTITY.packageVersion).toBe(packageJson.version);
expect(SDK_IDENTITY.server).toBe("2.0.0-beta.4");
expect(CLI_COMMAND_NAMES).toHaveLength(46);
expect(new Set(CLI_COMMAND_NAMES).size).toBe(46);
expect(CLI_COMMAND_NAMES).toHaveLength(47);
expect(new Set(CLI_COMMAND_NAMES).size).toBe(47);
expect(CATALOG_IDENTITY.counts).toEqual({
cli_commands: 46,
mcp_tools: 86,
cli_commands: 47,
mcp_tools: 87,
mcp_prompts: 6,
mcp_resources: 2,
mcp_resource_templates: 7,
@@ -162,7 +162,7 @@ describe("server and catalog identity", () => {
expect(status.structuredContent).toMatchObject({
result: {
server_identity: {
catalog: { counts: { mcp_tools: 86, cli_commands: 46 } },
catalog: { counts: { mcp_tools: 87, cli_commands: 47 } },
alignment: { state: "mcp_server_restart_required" },
},
tool_availability: expect.arrayContaining([
+1 -1
View File
@@ -142,7 +142,7 @@ describe("target-free MCP lifecycle", () => {
expect(text(before)).toBe(
"No app is open. Ask the user which app to investigate, then call open_binary with its local path.",
);
expect((await mcp.listTools()).tools).toHaveLength(86);
expect((await mcp.listTools()).tools).toHaveLength(87);
const deniedCapture = await mcp.callTool({
name: "capture_process_scenario",
arguments: {
+1 -1
View File
@@ -31,7 +31,7 @@ describe("canonical skill transaction", () => {
expect(await readFile(`${destination}.rea.backup`, "utf8")).toBe(
"stale managed skill\n",
);
expect(await readFile(destination, "utf8")).toContain('version: "19"');
expect(await readFile(destination, "utf8")).toContain('version: "20"');
expect(await readFile(destination, "utf8")).toContain(
`tool_count: ${String(TOOL_CONTRACTS.length)}`,
);
+4 -3
View File
@@ -15,7 +15,7 @@ import { ELECTRON_TOOL_CONTRACTS } from "../src/contracts/electronToolContracts.
import { APPLICATION_TOOL_CONTRACTS } from "../src/contracts/applicationToolContracts.js";
describe("tool contract inventory", () => {
it("publishes 68 analysis contracts and eighteen session tools", () => {
it("publishes 69 analysis contracts and eighteen session tools", () => {
expect(OFFICIAL_TOOL_CONTRACTS).toHaveLength(33);
expect(ENHANCED_TOOL_CONTRACTS).toHaveLength(10);
expect(NATIVE_TOOL_CONTRACTS).toHaveLength(5);
@@ -23,6 +23,7 @@ describe("tool contract inventory", () => {
expect(MANAGED_TOOL_CONTRACTS.map(({ name }) => name)).toEqual([
"inspect_managed_artifact",
"inspect_managed_members",
"inspect_managed_native_boundaries",
]);
expect(MANAGED_WORKFLOW_TOOL_CONTRACTS.map(({ name }) => name)).toEqual([
"compare_managed_members",
@@ -68,8 +69,8 @@ describe("tool contract inventory", () => {
"update_unknown",
"verify_unknown_resolution",
]);
expect(TOOL_CONTRACTS).toHaveLength(86);
expect(new Set(TOOL_CONTRACTS.map(({ name }) => name)).size).toBe(86);
expect(TOOL_CONTRACTS).toHaveLength(87);
expect(new Set(TOOL_CONTRACTS.map(({ name }) => name)).size).toBe(87);
});
it("retains documented enhanced-tool limits at the input boundary", () => {