feat(wasm): inspect local artifacts with WABT (#1533)

* feat(wasm): inspect local artifacts with WABT

* fix(wasm): preserve custom names and failed cleanup ownership

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Thang Luu
2026-10-11 19:49:00 +08:00
committed by GitHub
co-authored by morluto
parent e4407dbed5
commit ffaf2c12f1
46 changed files with 2309 additions and 7 deletions
@@ -0,0 +1,84 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect, it, onTestFinished, vi } from "vitest";
import { WasmArtifactService } from "../../../src/application/wasm/WasmArtifactService.js";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { ok } from "../../../src/domain/result.js";
import { createServer } from "../../../src/server/createServer.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import {
wasmArtifactFixture,
wasmArtifactExecution,
} from "../../fixtures/wasm/artifact.js";
it("advertises exact valid schemas and records inline WASM interface evidence without a binary target", async () => {
const value = wasmArtifactFixture();
const close = vi.fn(() => Promise.resolve());
const service = new WasmArtifactService({
close,
inspect: () => Promise.resolve(ok(wasmArtifactExecution(value))),
});
const session = createTestBinarySession(() => {
throw new Error("deep provider must not start");
});
const server = createServer(
{ kind: "session", session },
{ wasmArtifact: service },
);
const client = new Client({ name: "wasm-artifact-contract", version: "1" });
onTestFinished(async () => {
await client.close();
await server.close();
await session.close();
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const advertised = (await client.listTools()).tools.find(
(tool) => tool.name === "inspect_wasm_artifact",
);
if (advertised?.outputSchema === undefined)
throw new Error("WASM interface inspection must publish both schemas");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
expect(ajv.validate(inputSchema, { path: value.artifact.path })).toBe(true);
expect(
ajv.validate(inputSchema, {
path: value.artifact.path,
approval: true,
}),
).toBe(false);
const response = await client.callTool({
name: "inspect_wasm_artifact",
arguments: { path: value.artifact.path },
});
expect(response.isError).not.toBe(true);
expect(
ajv.validate(outputSchema, response.structuredContent),
JSON.stringify(ajv.errors),
).toBe(true);
const parsed = toolContract("inspect_wasm_artifact").outputSchema.parse(
response.structuredContent,
);
const evidence = parseEvidence(parsed);
expect(evidence.confidence).toBe("observed");
expect(evidence.subject?.digest.sha256).toBe(value.artifact.sha256);
expect(parsed.normalized_result).toEqual(value);
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
expect(
(
await client.callTool({
name: "inspect_wasm_artifact",
arguments: { path: "relative.hex" },
})
).isError,
).toBe(true);
await server.close();
expect(close).toHaveBeenCalled();
});
+13
View File
@@ -0,0 +1,13 @@
# WABT producer fixtures
`module.wat` is source-owned. WABT 1.0.42 `wat2wasm module.wat -o module.wasm`
produced the binary used for `wasm-objdump -h -x module.wasm` (`objdump.txt`) and
`wasm2wat --enable-all module.wasm` (`decoded.wat`). No binary is checked in.
The module has an imported function, exported memory/function and a `test`
custom section. The custom payload is retained as an `@custom` annotation in decoded WAT.
Upstream tag commit: `ff0ef7e0009402740c805a9744c09b05be063e48`.
The separate real-tool verifier generates its own fixtures with caller-supplied
WABT and compares exact producer output against the public CLI/MCP surfaces.
`custom-names.wat` covers newlines, quotes, Unicode, backslashes and embedded
section markers in custom names; objdump displays those names without escaping.
+52
View File
@@ -0,0 +1,52 @@
import { createHash } from "node:crypto";
import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js";
/** Source-owned transport seam; upstream binaries are verified in a separate lane. */
export const wasmArtifactFixture = (path = "/artifacts/module.wasm") => ({
artifact: { path, sha256: "a".repeat(64), bytes: 8 },
validation: "valid" as const,
sections: [],
imports: [],
exports: [],
headers: "",
details: "",
wat: {
text: "(module)\n",
sha256: createHash("sha256").update("(module)\n").digest("hex"),
representation: "decoded-wat" as const,
},
tool_profile: {
source: "https://github.com/WebAssembly/wabt" as const,
release: "1.0.42" as const,
source_commit: "ff0ef7e0009402740c805a9744c09b05be063e48" as const,
commands: ["wasm-validate", "wasm-objdump", "wasm2wat"].map((tool) => ({
tool,
path: `/tools/${tool}`,
sha256: "b".repeat(64),
version_banner: "1.0.42\n",
arguments:
tool === "wasm-objdump"
? ["-h", "-x", "module.wasm"]
: ["--enable-all", "module.wasm"],
})),
timeout_ms: 30_000,
output_budget_bytes: 32 * 1024 * 1024,
},
candidates: [{ path, sha256: "a".repeat(64), bytes: 8 }],
glue: [],
runtime_execution: "not-performed" as const,
network_fetch: "not-performed" as const,
limitations: ["Source-owned transport seam."],
});
export const wasmArtifactExecution = (value = wasmArtifactFixture()) =>
createAnalysisExecution(
value,
{ id: "fixture-wabt", name: "Source-owned WABT seam", version: "1" },
{
subject: {
path: value.artifact.path,
format: "file",
sha256: value.artifact.sha256,
},
limitations: value.limitations,
},
);
+4
View File
@@ -0,0 +1,4 @@
(module
(@custom "test\0aline" "payload")
(@custom "fake\22\0aSection Details:\0a Type start=0x0000000a end=0x0000000b (size=0x00000001) count: 0\0a\22end" "payload")
(@custom "unicode-\c3\a9\\\t" "payload"))
+9
View File
@@ -0,0 +1,9 @@
(module
(type (;0;) (func))
(import "env" "foo" (func (;0;) (type 0)))
(func (;1;) (type 0)
call 0)
(memory (;0;) 1)
(export "memory" (memory 0))
(export "run" (func 1))
(@custom "test" "payload"))
+1
View File
@@ -0,0 +1 @@
(module (@custom "test" "payload") (import "env" "foo" (func $foo)) (memory (export "memory") 1) (func (export "run") call $foo))
+30
View File
@@ -0,0 +1,30 @@
module.wasm: file format wasm 0x1
Sections:
Type start=0x0000000a end=0x0000000e (size=0x00000004) count: 1
Import start=0x00000010 end=0x0000001b (size=0x0000000b) count: 1
Function start=0x0000001d end=0x0000001f (size=0x00000002) count: 1
Memory start=0x00000021 end=0x00000024 (size=0x00000003) count: 1
Export start=0x00000026 end=0x00000036 (size=0x00000010) count: 2
Code start=0x00000038 end=0x0000003e (size=0x00000006) count: 1
Custom start=0x00000040 end=0x0000004c (size=0x0000000c) "test"
Section Details:
Type[1]:
- type[0] () -> nil
Import[1]:
- func[0] sig=0 <env.foo> <- env.foo
Function[1]:
- func[1] sig=0 <run>
Memory[1]:
- memory[0] pages: initial=1
Export[2]:
- memory[0] -> "memory"
- func[1] <run> -> "run"
Code[1]:
- func[1] size=4 <run>
Custom:
- name: "test"