Files
reverse-engineering-anything/scripts/verify-real-hopper-fat.mjs
T
morluto 4126698407 feat(mcp): project compact input schemas into the portable advertised dialect (#10632)
* test(mcp): check advertised schemas against a portable dialect in every profile

* fix(mcp): re-emit advertised patterns in a portable regex grammar

* fix(mcp): present discover_tools input through the selected profile

The hot-cold catalog registered discover_tools directly with the SDK, so
the default compact input profile still advertised its canonical schema,
including page-size defaults clients apply on their own (#5322). Route
every listed input schema, transport discovery included, through one
server-owned presentation, and pass the selected presentation to the
deferred server, which serves recorded tool schemas but not discovery.

* fix(mcp): keep the Evidence union on described borrowed inputs

borrowEvidenceInput installed its advertised projection on one schema
instance. A call site's .describe() rebuilds the schema from its
definition, so match_native_functions advertised left and right as
unconstrained values in every input profile. Install the projection in
the borrowed parser's constructor so derived schemas keep it, for both
borrowed inputs and borrowed arrays.

* fix(mcp): validate advertised dialect by selected profile

* fix(mcp): preserve schema widening through references

* feat(mcp): project compact input schemas into the portable advertised dialect

One projection (advertisedDialectProjection.ts) owns the compact input
advertisement: one type per node, items on every array, string literals
as enums, oneOf widened to anyOf, allOf merged, tuples as element unions,
exclusive bounds made inclusive, and every keyword outside the dialect
dropped. Each rewrite is exact or widening; server validation is
unchanged. The root description and sentences for dropped cross-field
constraints move to the tool description, which Codex and Zed keep.

The compact baseline falls to nullable values only (29), which the
dialect cannot express without narrowing an input. The gateway shape
repairs (typeArrayItemUnions, hasGatewayIncompatibleShape), the second
dependentRequired-to-prose copy and the strict-gateway test denylist are
removed; the dialect checker gates the inline presentation instead.

* fix(mcp): refuse zero-padded repeat bounds and record host-specific path patterns

* fix(mcp): keep projected schemas permissive

* fix(mcp): keep references valid when presentation widens their targets

* fix(mcp): keep a single union branch's closure scoped to its properties

* fix(mcp): keep an allOf member's tuple tail with its own positions

* fix(mcp): relax references into negations and conditionals presentation may remove

* test(mcp): select the borrowed Evidence contract from its family

* fix(mcp): keep astral literals and dynamic references widening in every mode

* fix(mcp): omit astral literals and relax untracked references when presentation widens

* fix(mcp): read reference pointers by schema position, not segment spelling

* fix(mcp): protect references into collapsing boolean unions and keep referencing branches out of the collapse

* fix(mcp): keep reduced schemas open when their root admits names by pattern

* test(mcp): keep the reduced-schema pattern case outside the size-limited block

* fix(mcp): omit patterns whose classes name astral characters

* fix(mcp): decide union collapse and pointer protection from canonical branches

* docs(mcp): document non-u astral matching and pin BMP agreement across V8 modes

* fix(mcp): keep prototype-named properties as own properties in the dialect projection

* test(mcp): expect the advertised base64 schema to omit the unbounded pad-bit probe

* fix(mcp): protect references into unevaluated keywords lost evaluation removes

* fix(mcp): emit stack-safe classes with non-ASCII members and protect draft-07 reference siblings

* fix(mcp): protect references into literal siblings of relaxed draft-07 references

* feat(mcp): omit output schemas from tools/list by default (#10646)

* feat(mcp): omit output schemas from tools/list by default

No surveyed model API reads an output schema, MCP client validators
repeat REA's own result validation, and complete output schemas were
about 92% of the default tools/list. REA_MCP_OUTPUT_SCHEMA_PROFILE now
resolves to off through one owner (resolveMcpOutputSchemaProfile);
compact and full remain selectable. structuredContent is still
delivered, and the deferred server serves both off and full from the
recorded catalog with the matching recorded identity.

Tests and artifact verifiers that validate results against complete
advertised output contracts select full explicitly.

* fix(mcp): select schemas in verification workflows

* fix(mcp): select complete output schemas in the Electron verifier

* fix(mcp): select complete output schemas in standalone verifier probes

* fix(mcp): select complete output schemas in every verifier transport

* test(mcp): select full output schemas in packet and signal schema checks

* fix(mcp): validate the doctor probe reply against the canonical output contract (#10655)

* fix(mcp): validate the doctor probe reply against the canonical output contract

* test(mcp): replay doctor probe validation under every output profile

* test(mcp): accept typed-branch nullable unions in the advertised dialect (#10649)

* test(mcp): select full output schemas in HTTP session workflows

* fix(mcp): keep the Evidence union on described borrowed inputs (#10597)

* fix(mcp): keep the Evidence union on described borrowed inputs

borrowEvidenceInput installed its advertised projection on one schema
instance. A call site's .describe() rebuilds the schema from its
definition, so match_native_functions advertised left and right as
unconstrained values in every input profile. Install the projection in
the borrowed parser's constructor so derived schemas keep it, for both
borrowed inputs and borrowed arrays.

* test(mcp): select the borrowed Evidence contract from its family

* docs(mcp): admit baseline growth only for restored contract declarations

* fix(mcp): state reduced multi-type properties as typed branches, not type lists

The reduced fallback derived a property's types and emitted them as a type
list, which Gemini, Moonshot and Ark reject. A property whose canonical
schema is anyOf string and number therefore traded a typed union for a
rejected construct when the inline projection fell back. Emit one type, or
anyOf typed branches, so [T, null] becomes the dialect's nullable union.
2026-11-15 18:26:13 +08:00

485 lines
15 KiB
JavaScript

import { schemaVerifierEnvironment } from "./lib/mcp-verification-environment.mjs";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { execFile } from "node:child_process";
import {
access,
mkdtemp,
readFile,
realpath,
rm,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { promisify } from "node:util";
import { fileURLToPath } from "node:url";
import { constants } from "node:fs";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import Ajv from "ajv/dist/2020.js";
import addFormats from "ajv-formats";
import {
mcpTextValue,
requireMcpOperationResult,
} from "./lib/mcp-verifier-results.mjs";
import { snapshotHopperRuntime } from "./lib/real-hopper-cleanup.mjs";
import {
closeVerifierResources,
withVerifierCleanup,
} from "./lib/verifier-cleanup.mjs";
import { HOPPER_TARGET_LEASE_DIRECTORY } from "../dist/process/HopperLease.js";
import { parseConfig } from "../dist/config/parseConfig.js";
import {
verifyHopperSourceBinding,
relocateSingleFatSlice,
} from "./lib/real-hopper-source-binding.mjs";
const run = promisify(execFile);
const verifyFat64 = process.argv[2] === "--fat64";
assert.deepEqual(process.argv.slice(2), verifyFat64 ? ["--fat64"] : []);
if (process.platform !== "darwin" || !["arm64", "x64"].includes(process.arch))
throw new Error(
"Hopper FAT verification requires macOS arm64 or x64 and its installed clang/lipo toolchain",
);
const config = parseConfig(process.env);
if (!config.ok) throw config.error;
await access(config.value.hopperLauncherPath, constants.X_OK).catch((cause) => {
throw new Error(
`Hopper FAT verification requires the configured executable: ${config.value.hopperLauncherPath}`,
{ cause },
);
});
const toolPaths = {};
for (const command of ["clang", "lipo"])
toolPaths[command] = (
await run("xcrun", ["--find", command]).catch((cause) => {
throw new Error(`Hopper FAT verification requires ${command}`, { cause });
})
).stdout.trim();
const sdk = (
await run("xcrun", ["--sdk", "macosx", "--show-sdk-path"]).catch((cause) => {
throw new Error(
"Hopper FAT verification requires the installed macOS SDK",
{ cause },
);
})
).stdout.trim();
const directory = await realpath(await mkdtemp(join(tmpdir(), "hopper-fat-")));
const source = fileURLToPath(
new URL("../tests/conformance/c/fixture.c", import.meta.url),
);
const arm = join(directory, "c-arm64");
const intel = join(directory, "c-x86_64");
const fat = join(directory, "c-fat");
const fat64 = join(directory, "c-fat64");
const singleFat = join(directory, "c-single-fat");
const extraFat64 = [];
const invalidFat64 = [];
let preparedImagePath;
const before = (
await snapshotHopperRuntime("/tmp", HOPPER_TARGET_LEASE_DIRECTORY)
).paths;
const client = new Client({ name: "hopper-fat-verifier", version: "1" });
const transport = new StdioClientTransport({
command: process.execPath,
args: ["dist/main.js"],
env: schemaVerifierEnvironment({
...process.env,
REA_ANALYSIS_PROVIDER: "hopper",
HOPPER_LOADER_ARGS_JSON: "[]",
}),
stderr: "pipe",
});
transport.stderr?.on("data", (chunk) => process.stderr.write(chunk));
const ajv = new Ajv({ strict: false });
addFormats(ajv);
const observations = [];
const sourceBinding = [];
await withVerifierCleanup(async () => {
await run(toolPaths.clang, [
"-isysroot",
sdk,
"-arch",
"arm64",
"-O0",
"-fno-inline",
source,
"-o",
arm,
]);
await run(toolPaths.clang, [
"-isysroot",
sdk,
"-arch",
"x86_64",
"-O0",
"-fno-inline",
source,
"-o",
intel,
]);
await run(toolPaths.lipo, ["-create", arm, intel, "-output", fat]);
if (verifyFat64)
await run(toolPaths.lipo, [
"-create",
arm,
intel,
"-fat64",
"-output",
fat64,
]);
await run(toolPaths.lipo, [
"-create",
process.arch === "arm64" ? arm : intel,
"-output",
singleFat,
]);
if (verifyFat64) {
const original = await readFile(fat64);
const swapped = Buffer.from(original);
swapped.writeUInt32BE(0xbfbafeca, 0);
swapped.writeUInt32LE(original.readUInt32BE(4), 4);
for (let index = 0; index < original.readUInt32BE(4); index++) {
const offset = 8 + index * 32;
for (const field of [0, 4, 24, 28])
swapped.writeUInt32LE(
original.readUInt32BE(offset + field),
offset + field,
);
for (const field of [8, 16])
swapped.writeBigUInt64LE(
original.readBigUInt64BE(offset + field),
offset + field,
);
}
const little = join(directory, "c-fat64-little-endian");
await writeFile(little, swapped);
extraFat64.push(little);
const single = join(directory, "c-single-fat64");
await run(toolPaths.lipo, [
"-create",
process.arch === "arm64" ? arm : intel,
"-fat64",
"-output",
single,
]);
extraFat64.push(single);
for (const [name, reason, mutate] of [
[
"reserved",
/nonzero reserved field/u,
(bytes) => bytes.writeUInt32BE(1, 36),
],
[
"range",
/extends beyond the file/u,
(bytes) => bytes.writeBigUInt64BE(BigInt(bytes.length), 24),
],
[
"identity",
/disagrees with its Mach-O header/u,
(bytes) => bytes.writeUInt32BE(bytes.readUInt32BE(12) + 1, 12),
],
[
"overlap",
/slices overlap/u,
(bytes) => {
const firstOffset = bytes.readBigUInt64BE(16);
const secondOffset = bytes.readBigUInt64BE(48);
const earlier = firstOffset < secondOffset ? 8 : 40;
const laterOffset =
firstOffset < secondOffset ? secondOffset : firstOffset;
const earlierOffset =
firstOffset < secondOffset ? firstOffset : secondOffset;
bytes.writeBigUInt64BE(
laterOffset - earlierOffset + 16n,
earlier + 16,
);
},
],
]) {
const bytes = Buffer.from(original);
mutate(bytes);
const path = join(directory, `c-fat64-invalid-${name}`);
await writeFile(path, bytes);
invalidFat64.push({ path, reason });
}
const hostBytes = await readFile(process.arch === "arm64" ? arm : intel);
const secondOffset = Math.ceil((16384 + hostBytes.length) / 16384) * 16384;
const ambiguous = Buffer.alloc(secondOffset + hostBytes.length);
ambiguous.writeUInt32BE(0xcafebabf, 0);
ambiguous.writeUInt32BE(2, 4);
for (const [index, offset] of [16384, secondOffset].entries()) {
const base = 8 + index * 32;
ambiguous.writeUInt32BE(hostBytes.readUInt32LE(4), base);
ambiguous.writeUInt32BE(hostBytes.readUInt32LE(8), base + 4);
ambiguous.writeBigUInt64BE(BigInt(offset), base + 8);
ambiguous.writeBigUInt64BE(BigInt(hostBytes.length), base + 16);
ambiguous.writeUInt32BE(14, base + 24);
hostBytes.copy(ambiguous, offset);
}
const path = join(directory, "c-fat64-ambiguous");
await writeFile(path, ambiguous);
invalidFat64.push({
path,
reason:
/FAT binary declares several (?:arm64|x86_64) slices; no slice was selected/u,
});
}
await client.connect(transport);
assert.ok(transport.pid !== null);
const catalog = await client.listTools();
const call = async (name, args = {}) => {
const reply = await client.callTool(
{ name, arguments: args },
{ timeout: 180_000 },
);
const result = requireMcpOperationResult(reply, name);
const schema = catalog.tools.find(
(tool) => tool.name === name,
)?.outputSchema;
assert.ok(schema, `${name} omitted its output schema`);
assert.equal(
ajv.validate(schema, reply.structuredContent),
true,
JSON.stringify(ajv.errors),
);
return result;
};
const cliMappings = new Map();
for (const path of [
arm,
intel,
fat,
singleFat,
...(verifyFat64 ? [fat64, ...extraFat64] : []),
]) {
console.error(`Verifying Hopper source-file mappings for ${path}`);
const file = await readFile(path);
const sourceHash = createHash("sha256").update(file).digest("hex");
await call("open_binary", { path });
const procedures = await call("list_procedures");
const entry = procedures.find((item) => item.value.endsWith("rea_entry"));
assert.ok(entry, "compiler fixture omitted rea_entry");
let entryMapping;
for (const address of [
entry.address,
`0x${(BigInt(entry.address) + 1n).toString(16)}`,
]) {
const mapping = await call("address_to_file_offset", { address });
if (address === entry.address) entryMapping = mapping;
const bytes = await call("read_bytes", { address, length: 16 });
assert.equal(
mapping.file_offset,
mapping.image_base_file_offset + mapping.provider_file_offset,
);
assert.equal(mapping.source_path, path);
assert.equal(bytes.complete, true);
assert.equal(
file
.subarray(mapping.file_offset, mapping.file_offset + 16)
.toString("hex"),
bytes.bytes_hex,
);
if (
path === fat ||
path === singleFat ||
path === fat64 ||
extraFat64.includes(path)
) {
assert.ok(
mapping.image_base_file_offset > 0,
"FAT mapping omitted its physical slice offset",
);
assert.notEqual(
file
.subarray(
mapping.provider_file_offset,
mapping.provider_file_offset + 16,
)
.toString("hex"),
bytes.bytes_hex,
"fixture did not distinguish image-relative and original-file coordinates",
);
} else assert.equal(mapping.image_base_file_offset, 0);
if (path === fat || path === fat64) {
cliMappings.set(path, { mapping, address });
}
observations.push({ path, address, ...mapping, matched_bytes: 16 });
}
if (path === fat64 || extraFat64.includes(path)) {
const session = await call("binary_session");
assert.equal(session.path, path);
assert.equal(session.sha256, sourceHash);
const profile = session.analysis_provider_binding.analysis_profile;
assert.equal(
profile.parameters.prepared_image.method,
"fat64_thin_slice",
);
assert.equal(
profile.parameters.prepared_image.source_offset,
observations.at(-1).image_base_file_offset,
);
const document = await call("current_document");
await call("set_comment", {
address: entry.address,
comment: "source-bound FAT64 preparation",
});
await call("open_binary", { path });
assert.deepEqual(
(await call("binary_session")).analysis_provider_binding
.analysis_profile,
profile,
);
assert.equal(await call("current_document"), document);
assert.equal(
await call("comment", { address: entry.address }),
"source-bound FAT64 preparation",
);
}
assert.equal(
createHash("sha256")
.update(await readFile(path))
.digest("hex"),
sourceHash,
"native analysis modified source executable bytes",
);
sourceBinding.push({
path,
...(await verifyHopperSourceBinding({
client,
call,
path,
file,
address: entry.address,
mapping: entryMapping,
...(path === singleFat || path.endsWith("c-single-fat64")
? { relocate: relocateSingleFatSlice }
: {}),
})),
});
await call("close_binary");
}
for (const { path, reason } of invalidFat64) {
const reply = await client.callTool({
name: "open_binary",
arguments: { path },
});
assert.equal(reply.isError, true, "malformed FAT64 unexpectedly launched");
assert.match(mcpTextValue(reply), reason);
}
if (verifyFat64) {
await call("open_binary", { path: fat64 });
await call("list_procedures");
const session = await call("binary_session");
const runId = session.analysis_run?.run_id;
assert.ok(
typeof runId === "string" && runId.length > 0,
"Missing analysis run identity",
);
const resources = await snapshotHopperRuntime(
"/tmp",
HOPPER_TARGET_LEASE_DIRECTORY,
runId,
);
const unresolvedNew = [...resources.unresolvedSessions].filter(
(path) => !before.has(path),
);
assert.deepEqual(
unresolvedNew,
[],
"Cannot identify a prepared image while new Hopper resource ownership is unresolved",
);
for (const root of resources.confirmedSessions) {
if (before.has(root)) continue;
const image = join(root, "image.macho");
if (
await access(image).then(
() => true,
() => false,
)
)
preparedImagePath = image;
}
assert.ok(
preparedImagePath,
"prepared native image was not owned by this session",
);
await client.close();
await transport.close();
await assert.rejects(access(preparedImagePath), { code: "ENOENT" });
}
for (const [path, expected] of cliMappings) {
const pending = run(
process.execPath,
[
"scripts/rea.mjs",
"address-to-file-offset",
path,
expected.address,
"--provider",
"hopper",
"--format",
"json",
],
{
timeout: 180_000,
env: { ...process.env, HOPPER_LOADER_ARGS_JSON: "[]" },
},
);
assert.ok(pending.child.pid !== undefined);
const cli = JSON.parse((await pending).stdout);
assert.deepEqual(cli.normalized_result, expected.mapping);
}
}, [
async () => {
try {
await closeVerifierResources([
() => client.close(),
() => transport.close(),
]);
} catch (cause) {
throw new Error(
`Hopper cleanup failed; fixture retained at ${directory}`,
{
cause,
},
);
}
try {
const retained = [
...(await snapshotHopperRuntime("/tmp", HOPPER_TARGET_LEASE_DIRECTORY))
.paths,
].filter((path) => !before.has(path));
assert.deepEqual(
retained,
[],
"Hopper resources remain or are unresolved",
);
} catch (cause) {
throw new Error(
`Hopper resource verification failed; fixture retained at ${directory}`,
{ cause },
);
}
await rm(directory, { recursive: true, force: true });
},
]);
console.log(
JSON.stringify(
{
observations,
cliMcpParity: true,
sourceBinding,
fat64: verifyFat64 ? "verified" : "not_run",
malformedFat64Rejected: invalidFat64.length,
preparedImageRemovedOnMcpShutdown: preparedImagePath !== undefined,
cleanShutdown: true,
},
null,
2,
),
);