chore: merge main into feat/signature-posture

This commit is contained in:
morluto
2026-10-08 15:49:33 +08:00
85 changed files with 6711 additions and 105 deletions
+22 -2
View File
@@ -138,7 +138,11 @@ const char *core_chain(void) { return sibling_value() == 7 ? chain_variant() : "
return join(version, "Core");
};
/** The main executable, with a weak dependency deleted after linking. */
/**
* The main executable. It searches a missing `Overrides` rpath before
* `Frameworks`, weakly links a library deleted after linking, and
* delay-links a library, which the linker encodes as `dylib_use_command`.
*/
const buildMainExecutable = async (root, contents, coreBinary) => {
const gone = join(contents, "Frameworks", "libgone.dylib");
await clang(root, {
@@ -147,22 +151,33 @@ const buildMainExecutable = async (root, contents, coreBinary) => {
output: gone,
flags: ["-dynamiclib", "-install_name", "@rpath/libgone.dylib"],
});
const delayed = join(contents, "Frameworks", "libdelay.dylib");
await clang(root, {
name: "delay",
source: `int delay_value(void) { return 3; }\n`,
output: delayed,
flags: ["-dynamiclib", "-install_name", "@rpath/libdelay.dylib"],
});
const executable = join(contents, "MacOS", "MacFixture");
await clang(root, {
name: "main",
source: `#include <stdio.h>
const char *core_chain(void);
int delay_value(void);
extern int gone_value(void) __attribute__((weak_import));
int main(void) {
if (delay_value() != 3) return 1;
printf("chain=%s weak=%s\\n", core_chain(), gone_value ? "present" : "absent");
return 0;
}
`,
output: executable,
flags: [
"-Wl,-rpath,@executable_path/../Overrides",
"-Wl,-rpath,@executable_path/../Frameworks",
coreBinary,
`-Wl,-weak_library,${gone}`,
`-Wl,-delay_library,${delayed}`,
],
});
await rm(gone);
@@ -191,7 +206,12 @@ const buildNestedBundles = async (root, contents, coreBinary) => {
int main(void) { return core_chain()[0] == 'b'; }
`,
output: join(service, "MacOS", "Svc"),
flags: ["-Wl,-rpath,@executable_path/../../../../Frameworks", coreBinary],
// An absolute rpath outside the bundle precedes the bundled framework.
flags: [
"-Wl,-rpath,/opt/rea-fixture-absent/lib",
"-Wl,-rpath,@executable_path/../../../../Frameworks",
coreBinary,
],
});
await write(
join(service, "Info.plist"),
+12
View File
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;
// Public source-owned fixture; generated runtime bytecode is never tracked.
contract InterfaceFixture {
uint256 public stored;
function set(uint256 value) external { stored = value; }
function read() external view returns (uint256) { return stored; }
function pair(uint32 a, address b) external pure returns (uint32, address) { return (a, b); }
receive() external payable {}
fallback() external {}
}
+8 -1
View File
@@ -24,7 +24,10 @@ export const toolFamilyCatalog = (sources) => {
{
id: "artifact",
surface: "artifact-provider",
contracts: sources.artifactContracts.ARTIFACT_TOOL_CONTRACTS,
contracts: [
...sources.artifactContracts.ARTIFACT_TOOL_CONTRACTS,
...sources.evmContracts.EVM_TOOL_CONTRACTS,
],
},
{
id: "managed",
@@ -151,6 +154,10 @@ export const providerCatalog = (sources) => {
contracts:
sources.binaryDiagnosticsContracts.BINARY_DIAGNOSTICS_TOOL_CONTRACTS,
},
{
identity: sources.evmProvider.EVMOLE_PROVIDER_IDENTITY,
contracts: sources.evmContracts.EVM_TOOL_CONTRACTS,
},
{
identity: sources.artifactProviders.ARTIFACT_GRAPH_PROVIDER,
contracts: sources.artifactContracts.ARTIFACT_TOOL_CONTRACTS,
+2
View File
@@ -44,6 +44,8 @@ const SOURCE_PATHS = {
"dist/contracts/native/binaryDiagnosticsToolContracts.js",
pwntoolsProvider: "dist/native/pwntools/PwntoolsRelease.js",
nativeContracts: "dist/contracts/native/nativeToolContracts.js",
evmContracts: "dist/contracts/evm/evmToolContracts.js",
evmProvider: "dist/evm/EvmoleRelease.js",
artifactContracts: "dist/contracts/artifactToolContracts.js",
managedContracts: "dist/contracts/managed/managedToolContracts.js",
firmwareContracts: "dist/contracts/firmware/firmwareToolContracts.js",
+1
View File
@@ -5,6 +5,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
const PURE_LAYERS = new Set(["domain", "contracts"]);
const MIGRATED_PROVIDER_ROOTS = new Set([
"evm",
"android",
"firmware",
"ghidra",
+577
View File
@@ -0,0 +1,577 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { access, readFile, readdir, writeFile } from "node:fs/promises";
import { constants } from "node:fs";
import { isAbsolute, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { PrivateRuntimeRoot } from "../dist/process/PrivateRuntimeRoot.js";
import { parseEvidence } from "../dist/domain/evidence.js";
import { mcpTextValue } from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
const solcModule = process.env.REA_VERIFY_SOLC_MODULE;
const strace = process.env.REA_VERIFY_STRACE_COMMAND;
if (
process.platform !== "linux" ||
process.arch !== "x64" ||
!isAbsolute(solcModule ?? "") ||
!isAbsolute(strace ?? "")
)
throw new Error(
"verify:evm:interface requires Linux x64, caller-supplied util-linux prlimit and absolute REA_VERIFY_SOLC_MODULE pointing to unchanged solc 0.8.30 and absolute REA_VERIFY_STRACE_COMMAND. No target or chain is executed.",
);
try {
await access(
process.env.REA_EVM_PRLIMIT_COMMAND ?? "/usr/bin/prlimit",
constants.X_OK,
);
await access(solcModule, constants.R_OK);
await access(strace, constants.X_OK);
} catch (cause) {
throw new Error(
"verify:evm:interface prerequisite unavailable: selected solc module, strace or util-linux prlimit",
{ cause },
);
}
const solc = (await import(pathToFileURL(solcModule).href)).default;
assert.equal(solc.version(), "0.8.30+commit.73712a01.Emscripten.clang");
const source = await readFile(
new URL("./fixtures/evm-interface.sol", import.meta.url),
"utf8",
);
const entrypoint =
process.argv[2] ?? fileURLToPath(new URL("./rea.mjs", import.meta.url));
const execute = promisify(execFile);
const environment = Object.fromEntries(
Object.entries(process.env).filter(([, value]) => typeof value === "string"),
);
const run = createVerifierRun();
const root = await PrivateRuntimeRoot.create({ prefix: "rea-evm-verifier-" });
const client = new Client({ name: "evm-interface-verifier", version: "1" });
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
env: environment,
stderr: "pipe",
});
const failures = [];
let cases = 0;
try {
await client.connect(transport);
const tools = await client.listTools();
assert.ok(tools.tools.some(({ name }) => name === "inspect_evm_interface"));
const inspect = async (
mode,
path,
encoding,
expectedCategory,
scope = { command: process.execPath, prefix: [], client },
) => {
let value;
if (mode === "cli") {
let stdout;
try {
({ stdout } = await execute(
scope.command,
[
...scope.prefix,
entrypoint,
"inspect-evm-interface",
path,
encoding,
"--json",
],
{
env: scope.environment ?? environment,
timeout: 45_000,
maxBuffer: 32 * 1024 * 1024,
},
));
} catch (cause) {
if (typeof cause.code !== "number" || typeof cause.stdout !== "string")
throw cause;
stdout = cause.stdout;
}
value = JSON.parse(stdout);
} else {
const reply = await scope.client.callTool({
name: "inspect_evm_interface",
arguments: { path, encoding },
});
value = JSON.parse(mcpTextValue(reply));
if (expectedCategory !== undefined) {
assert.equal(reply.isError, true);
value = value.error;
} else assert.notEqual(reply.isError, true, mcpTextValue(reply));
}
if (expectedCategory !== undefined) {
assert.equal(value.category, expectedCategory, JSON.stringify(value));
return value;
}
assert.equal(
value.category,
undefined,
`Unexpected public failure: ${JSON.stringify(value)}`,
);
const evidence = parseEvidence(mode === "cli" ? value : value.evidence);
const report = evidence.normalized_result;
assert.equal(report.diagnostics.truncated, false);
assert.equal(evidence.provider.version, "evmole@0.9.3");
assert.equal(evidence.confidence, "inferred");
assert.equal(report.artifact.path, path);
assert.equal(report.artifact.encoding, encoding);
const carrier = await readFile(path);
assert.equal(
report.artifact.sha256,
createHash("sha256").update(carrier).digest("hex"),
);
assert.equal(evidence.subject.digest.sha256, report.artifact.sha256);
assert.equal(report.artifact.bytes, carrier.length);
assert.equal(
report.bytecode.sha256,
createHash("sha256")
.update(Buffer.from(report.bytecode.hex, "hex"))
.digest("hex"),
);
assert.equal(report.bytecode.bytes, report.bytecode.hex.length / 2);
for (const key of ["kind", "hardfork", "deployment_authenticity"])
assert.equal(report.bytecode[key], "unknown");
assert.equal(report.discovery_completeness, "unknown");
assert.equal(report.runtime_execution, "not-performed");
assert.deepEqual(
report.functions.map((fn) => ({
selector: fn.selector.slice(2),
bytecodeOffset: fn.bytecode_offset,
dispatch: fn.dispatch,
...(fn.inferred_arguments === null
? {}
: { arguments: fn.inferred_arguments }),
...(fn.inferred_state_mutability === null
? {}
: { stateMutability: fn.inferred_state_mutability }),
})),
evidence.raw_result.functions ?? [],
);
if (mode === "mcp") assert.deepEqual(value.result, report);
return report;
};
for (const [name, optimizer, viaIR] of [
["plain", false, false],
["optimized", true, false],
["via-ir", true, true],
]) {
const output = JSON.parse(
solc.compile(
JSON.stringify({
language: "Solidity",
sources: { "Interface.sol": { content: source } },
settings: {
optimizer: { enabled: optimizer, runs: 200 },
viaIR,
evmVersion: "cancun",
outputSelection: {
"*": {
"*": [
"evm.deployedBytecode.object",
"evm.methodIdentifiers",
"abi",
],
},
},
},
}),
),
);
assert.ok(
!output.errors?.some(({ severity }) => severity === "error"),
JSON.stringify(output.errors),
);
const artifact = output.contracts["Interface.sol"].InterfaceFixture;
const hex = artifact.evm.deployedBytecode.object;
const expected = Object.values(artifact.evm.methodIdentifiers)
.map((selector) => "0x" + selector)
.sort();
const forms = [
["hex", ` \t0x${hex.toUpperCase()}\r\n`],
["raw", Buffer.from(hex, "hex")],
];
for (const [encoding, data] of forms) {
const path = join(root.path, `${name}.${encoding}`);
await writeFile(path, data);
const original = await readFile(path);
for (const mode of ["cli", "mcp"]) {
const report = await inspect(mode, path, encoding);
assert.equal(report.bytecode.hex, hex);
assert.deepEqual(
report.functions
.filter(({ dispatch }) => dispatch === "abi")
.map(({ selector }) => selector)
.sort(),
expected,
);
assert.ok(
report.functions.every(
({ bytecode_offset }) => bytecode_offset < report.bytecode.bytes,
),
);
const set = report.functions.find(
({ selector }) =>
selector === "0x" + artifact.evm.methodIdentifiers["set(uint256)"],
);
assert.equal(set.inferred_arguments, "uint256");
assert.equal(set.inferred_state_mutability, "nonpayable");
cases++;
}
assert.deepEqual(await readFile(path), original);
}
}
for (const [name, data, encoding] of [
["empty", "0x", "hex"],
["empty-raw", Buffer.alloc(0), "raw"],
["incomplete-push", Buffer.from([0x7f]), "raw"],
["no-dispatch", "60006000f3", "hex"],
]) {
const path = join(root.path, name);
await writeFile(path, data);
for (const mode of ["cli", "mcp"]) {
const report = await inspect(mode, path, encoding);
assert.equal(report.functions.length, 0);
cases++;
}
}
for (const [name, hex, expected] of [
["zero-selector", "60003560e01c630000000014601057005b00", ["0x00000000"]],
["push-immediate", "63123456785060006000f3", []],
]) {
const path = join(root.path, name);
await writeFile(path, hex);
for (const mode of ["cli", "mcp"]) {
const report = await inspect(mode, path, "hex");
assert.deepEqual(
report.functions.map(({ selector }) => selector),
expected,
);
cases++;
}
}
for (const [encoding, data] of [
["raw", Buffer.from("ef0001", "hex")],
["hex", "0xef0001"],
]) {
const path = join(root.path, "eof." + encoding);
await writeFile(path, data);
for (const mode of ["cli", "mcp"]) {
const failure = await inspect(mode, path, encoding, "unsupported_target");
assert.equal(failure.code, "unsupported_target");
assert.equal(failure.details.path, path);
assert.match(failure.details.reason, /EF00/);
assert.match(failure.remediation.action, /target format/);
cases++;
}
}
for (const [name, data] of [
["odd", "0x1"],
["invalid-hex", "xyz"],
["interior-space", "60 00"],
["bom", "\uFEFF6000"],
["utf8", Buffer.from([0xff])],
]) {
const path = join(root.path, name);
await writeFile(path, data);
for (const mode of ["cli", "mcp"]) {
await inspect(mode, path, "hex", "invalid_input");
cases++;
}
}
for (const mode of ["cli", "mcp"]) {
await inspect(mode, join(root.path, "absent"), "raw", "invalid_input");
cases++;
}
for (const [option, expected] of [
["--as=2147483648", "address_space_bytes=2147483648"],
["--cpu=20", "cpu_seconds=20"],
["--fsize=8388608", "file_size_bytes=8388608"],
]) {
const limiter = environment.REA_EVM_PRLIMIT_COMMAND ?? "/usr/bin/prlimit";
const prefix = [
option,
"--",
process.execPath,
"--disable-wasm-trap-handler",
];
const limitedClient = new Client({
name: "limited-evm-verifier",
version: "1",
});
const limitedTransport = new StdioClientTransport({
command: limiter,
args: [...prefix, entrypoint, "mcp"],
env: environment,
stderr: "pipe",
});
try {
await limitedClient.connect(limitedTransport);
const scope = { command: limiter, prefix, client: limitedClient };
for (const mode of ["cli", "mcp"]) {
const report = await inspect(
mode,
join(root.path, "zero-selector"),
"hex",
undefined,
scope,
);
assert.ok(report.limitations.some((item) => item.includes(expected)));
cases++;
}
} finally {
try {
await limitedClient.close();
} finally {
await limitedTransport.close();
}
}
}
const brokenLimiter = join(root.path, "broken-limiter");
await writeFile(brokenLimiter, "#!/rea-verifier-missing-interpreter\n", {
mode: 0o700,
});
const signalLimiter = join(root.path, "signal-limiter");
await writeFile(signalLimiter, '#!/bin/sh\nulimit -c 0\nkill -XCPU "$$"\n', {
mode: 0o700,
});
const fileLimiter = join(root.path, "file-size-limiter");
const unmarkedLimiter = join(root.path, "unmarked-file-size-limiter");
await writeFile(
unmarkedLimiter,
'#!/bin/sh\nprintf "launcher exit 76\\n" >&2\nexit 76\n',
{ mode: 0o700 },
);
// Fixture-only launcher: run the actual Node/WASM worker with a one-byte
// file allowance, keeping the usual address-space, CPU and core budgets.
await writeFile(
fileLimiter,
'#!/bin/sh\nwhile [ "$#" -gt 0 ] && [ "$1" != "--" ]; do shift; done\n[ "$#" -gt 0 ] || exit 64\nshift\nexec /usr/bin/prlimit --as=3221225472: --cpu=30: --fsize=1: --core=0: -- "$@"\n',
{ mode: 0o700 },
);
for (const [limiter, category, resource] of [
[root.path, "unsupported_provider"],
[brokenLimiter, "unsupported_provider"],
[signalLimiter, "resource_constraint", "cpu"],
[fileLimiter, "resource_constraint", "file-size"],
[unmarkedLimiter, "execution_failure"],
]) {
const selectedEnvironment = {
...environment,
REA_EVM_PRLIMIT_COMMAND: limiter,
};
const selectedClient = new Client({
name: "unavailable-evm-limiter-verifier",
version: "1",
});
const selectedTransport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
env: selectedEnvironment,
stderr: "pipe",
});
try {
await selectedClient.connect(selectedTransport);
const scope = {
command: process.execPath,
prefix: [],
client: selectedClient,
environment: selectedEnvironment,
};
for (const mode of ["cli", "mcp"]) {
const error = await inspect(
mode,
join(root.path, "zero-selector"),
"hex",
category,
scope,
);
if (category === "resource_constraint") {
assert.equal(error.code, "resource_constraint");
assert.equal(error.details.resource, resource);
assert.equal(
error.details.reported_limits.effective_soft_limits,
null,
);
assert.equal(
typeof error.details.reported_limits.configured_soft_limits
.cpu_seconds,
"number",
);
assert.ok(
JSON.stringify(error).includes(
resource === "cpu" ? "SIGXCPU" : "EFBIG",
),
);
} else if (category === "execution_failure") {
assert.equal(error.code, "execution_failure");
assert.ok(JSON.stringify(error).includes("launcher exit 76"));
assert.ok(JSON.stringify(error).includes("file_size_failure_marker"));
} else {
assert.equal(error.code, "capability_unavailable");
assert.ok(JSON.stringify(error).includes(limiter));
}
cases++;
}
} finally {
try {
await selectedClient.close();
} finally {
await selectedTransport.close();
}
}
}
for (const [name, encoding] of [
["plain.hex", "hex"],
["via-ir.raw", "raw"],
]) {
await execute(
strace,
[
"-ff",
"-e",
"trace=network,execve,execveat",
"-s",
"4096",
"-o",
join(root.path, `offline.trace.${name}`),
process.execPath,
entrypoint,
"inspect-evm-interface",
join(root.path, name),
encoding,
"--json",
],
{
env: { ...environment, PATH: "/usr/bin:/bin" },
timeout: 45_000,
maxBuffer: 32 * 1024 * 1024,
},
);
}
const observedExecutables = new Set();
for (const file of await readdir(root.path)) {
if (!file.startsWith("offline.trace.")) continue;
const localSocketDescriptors = new Set();
for (const line of (await readFile(join(root.path, file), "utf8")).split(
"\n",
)) {
if (/execve(?:at)?\(/.test(line)) {
const match = /^execve\("([^"]+)"/.exec(line);
assert.notEqual(match, null, `Unresolved executable identity: ${line}`);
assert.ok(
line.endsWith(" = 0") || / = -1 [A-Z]+/.test(line),
`Incomplete exec observation: ${line}`,
);
const ownershipInspection =
["/usr/bin/ps", "/bin/ps"].includes(match[1]) &&
line.includes('["ps", "-axo", "pid=,ppid=,pgid=,stat=,command="]');
assert.ok(
[
process.execPath,
environment.REA_EVM_PRLIMIT_COMMAND ?? "/usr/bin/prlimit",
].includes(match[1]) || ownershipInspection,
`Unexpected host execution: ${line}`,
);
observedExecutables.add(match[1]);
} else if (/^[a-z]+\(/.test(line)) {
assert.ok(
permitsStdioSocketObservation(line, localSocketDescriptors),
`Unexpected attempted network operation: ${line}`,
);
}
}
}
assert.ok(observedExecutables.has(process.execPath));
assert.ok(
observedExecutables.has(
environment.REA_EVM_PRLIMIT_COMMAND ?? "/usr/bin/prlimit",
),
);
cases += 2;
} catch (cause) {
failures.push(cause);
} finally {
for (const close of [
() => client.close(),
() => transport.close(),
() => root.close(),
]) {
try {
await close();
} catch (cause) {
failures.push(cause);
}
}
}
const verifier = await completeVerifierRun(run);
if (
verifier.process_lineage.status !== "verified" ||
verifier.process_lineage.descendants.length !== 0
)
failures.push(
new Error(
"Verifier process lineage was not fully released: " +
JSON.stringify(verifier),
),
);
console.log(
JSON.stringify(
{
status: failures.length === 0 ? "passed" : "failed",
public_cases: cases,
compiler: solc.version(),
engine: "evmole@0.9.3",
...(failures.length === 0
? {
target_execution: "exec-syscalls-verified-absent",
network_requests: "socket-syscalls-verified-absent",
}
: {}),
verifier,
},
null,
2,
),
);
if (failures.length !== 0)
throw new AggregateError(
failures,
"Offline EVM interface verification failed; cleanup failures retained.",
);
function permitsStdioSocketObservation(line, localDescriptors) {
if (line.startsWith("socketpair(AF_UNIX,")) {
const pair = /\[(\d+), (\d+)\]\)\s+= 0$/.exec(line);
if (pair !== null) {
localDescriptors.add(pair[1]);
localDescriptors.add(pair[2]);
}
return true;
}
const name = /^getsockname\((\d+), \{sa_family=AF_UNIX\}/.exec(line);
if (name !== null) {
localDescriptors.add(name[1]);
return true;
}
// A failed descriptor query performs no request and identifies no socket.
if (line.startsWith("getsockname(") && / = -1 ENOTSOCK/.test(line))
return true;
const metadata =
/^(?:getsockopt\((\d+), SOL_SOCKET, SO_TYPE,|setsockopt\((\d+), SOL_SOCKET, SO_(?:RCVBUF|SNDBUF),|shutdown\((\d+), SHUT_WR\))/.exec(
line,
);
const descriptor = metadata?.[1] ?? metadata?.[2] ?? metadata?.[3];
// Allow descriptor queries/buffer configuration only after observing a local
// stdio socket. Other socket creation, connection and data operations fail.
return descriptor !== undefined && localDescriptors.has(descriptor);
}
+3
View File
@@ -14,6 +14,7 @@ import {
buildMacosBundleFixture,
preflightMacosBundleFixture,
} from "../../fixtures/apple/macos-bundle.mjs";
import { verifyDylibResolution } from "./macos-dylib-e2e.mjs";
const exec = promisify(execFile);
@@ -150,6 +151,7 @@ try {
assert.deepEqual(anatomies[2], anatomies[0], "DMG anatomy differs");
const { stdout: attached } = await exec("/usr/bin/hdiutil", ["info"]);
assert.ok(!attached.includes(dmg), "DMG remained attached after inventory");
const dylibs = await verifyDylibResolution(app);
process.stdout.write(
`${JSON.stringify({
@@ -160,6 +162,7 @@ try {
containers: containers.map(({ format }) => format),
bundles: EXPECTED_BUNDLES.length,
dmg_detached: true,
dylib_resolution: dylibs,
})}\n`,
);
} finally {
+209
View File
@@ -0,0 +1,209 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { realpath } from "node:fs/promises";
import { join, relative } from "node:path";
import { promisify } from "node:util";
import {
artifactCliEvidence,
artifactMcpResult,
withArtifactMcp,
} from "../../lib/artifact-e2e.mjs";
const exec = promisify(execFile);
const MAIN = "Contents/MacOS/MacFixture";
const SERVICE = "Contents/XPCServices/Svc.xpc/Contents/MacOS/Svc";
const CORE = "Contents/Frameworks/Core.framework/Versions/A/Core";
const DEPENDENCY_COMMANDS = new Set([
"LC_LOAD_DYLIB",
"LC_LOAD_WEAK_DYLIB",
"LC_REEXPORT_DYLIB",
"LC_LAZY_LOAD_DYLIB",
"LC_LOAD_UPWARD_DYLIB",
]);
/** Dependencies, rpaths and install name of one slice as Apple's otool reports them. */
const otoolLoadCommands = async (path, architecture) => {
const { stdout } = await exec(
"/usr/bin/xcrun",
["otool", "-arch", architecture, "-l", path],
{ maxBuffer: 64 * 1024 * 1024 },
);
const result = { dependencies: [], rpaths: [], install_name: null };
for (const block of stdout.split(/\nLoad command \d+\n/u)) {
const command = /^\s*cmd (\S+)/mu.exec(block)?.[1];
const name = /^\s*name (.+?) \(offset \d+\)$/mu.exec(block)?.[1];
if (command === "LC_RPATH")
result.rpaths.push(/^\s*path (.+?) \(offset \d+\)$/mu.exec(block)?.[1]);
else if (command === "LC_ID_DYLIB") result.install_name = name;
else if (DEPENDENCY_COMMANDS.has(command))
result.dependencies.push(`${command} ${name}`);
}
return result;
};
/** In-bundle images dyld actually loaded, in load order. */
const loadedImages = async (app, executable) => {
const { stderr } = await exec(join(app, executable), [], {
env: { PATH: "/usr/bin:/bin", DYLD_PRINT_LIBRARIES: "1" },
});
const appRoot = await realpath(app);
const loaded = [];
for (const line of stderr.split("\n")) {
const path = /^dyld\[\d+\]: <[0-9A-F-]+> (.+)$/u.exec(line)?.[1];
if (path === undefined) continue;
// Shared-cache libraries are reported by path but are not files on disk.
const real = await realpath(path).catch(() => undefined);
if (real?.startsWith(`${appRoot}/`) === true)
loaded.push(relative(appRoot, real));
}
return loaded;
};
/** Images a process root loads according to the trace, in first-load order. */
const predictedImages = (trace, root) => {
const loaded = [root];
for (const edge of trace.edges)
if (
edge.root === root &&
["resolved", "conditional"].includes(edge.resolution.status) &&
!loaded.includes(edge.resolution.image)
)
loaded.push(edge.resolution.image);
return loaded;
};
const findEdge = (trace, root, loader, installName) => {
const edge = trace.edges.find(
(candidate) =>
candidate.root === root &&
candidate.loader === loader &&
candidate.install_name === installName,
);
assert.ok(edge, `missing edge ${root}: ${loader} -> ${installName}`);
return edge;
};
const verifyResolutionShapes = (trace) => {
const core = findEdge(
trace,
MAIN,
MAIN,
"@rpath/Core.framework/Versions/A/Core",
);
assert.deepEqual(
core.candidates.map(({ source, outcome }) => [source, outcome]),
[
["rpath", "absent"],
["rpath", "resolved"],
],
);
assert.deepEqual(core.resolution, { status: "resolved", image: CORE });
const weak = findEdge(trace, MAIN, MAIN, "@rpath/libgone.dylib");
assert.equal(weak.weak, true);
assert.equal(weak.resolution.status, "unresolved");
const delayed = findEdge(trace, MAIN, MAIN, "@rpath/libdelay.dylib");
assert.equal(delayed.encoding, "dylib_use_command");
assert.equal(delayed.delayed_init, true);
const chain = findEdge(trace, MAIN, CORE, "@rpath/libchain.dylib");
assert.equal(
chain.resolution.image,
"Contents/Frameworks/Core.framework/Versions/A/Libraries/libchain.dylib",
"the loading image's own LC_RPATH must be searched first",
);
const service = findEdge(
trace,
SERVICE,
SERVICE,
"@rpath/Core.framework/Versions/A/Core",
);
assert.equal(service.candidates[0]?.outcome, "outside-target");
assert.deepEqual(service.resolution, { status: "conditional", image: CORE });
const system = trace.edges.filter(({ install_name: name }) =>
name.startsWith("/usr/lib/"),
);
assert.ok(system.length > 0);
assert.ok(
system.every(({ resolution }) => resolution.status === "undetermined"),
);
const kinds = trace.findings.map(({ kind, image }) => `${kind}:${image}`);
assert.ok(kinds.includes(`weak-load-unresolved:${MAIN}`));
assert.ok(kinds.includes(`earlier-rpath-candidate-absent:${MAIN}`));
};
const verifyParserConformance = async (app, trace) => {
let slices = 0;
for (const image of trace.images)
for (const slice of image.slices) {
const expected = await otoolLoadCommands(
join(app, image.path),
slice.architecture,
);
const edges = trace.edges.filter(
({ loader, architecture, root }) =>
loader === image.path &&
architecture === slice.architecture &&
root ===
trace.edges.find(
(edge) =>
edge.loader === image.path &&
edge.architecture === slice.architecture,
)?.root,
);
assert.deepEqual(
{
dependencies: edges.map(
({ command, install_name: name }) => `${command} ${name}`,
),
rpaths: slice.rpaths,
install_name: slice.install_name,
},
expected,
`${image.path} (${slice.architecture}) differs from otool -l`,
);
slices += 1;
}
return slices;
};
/** Verify trace_dylib_resolution against otool and the real dyld on the fixture app. */
export async function verifyDylibResolution(app) {
const trace = (await artifactCliEvidence("trace-dylib-resolution", app))
.normalized_result;
await withArtifactMcp(app, async (client) => {
assert.deepEqual(
await artifactMcpResult(client, "trace_dylib_resolution"),
trace,
);
const invalid = await client.callTool({
name: "trace_dylib_resolution",
arguments: { roots: ["../outside"] },
});
assert.equal(invalid.isError, true);
});
assert.equal(trace.coverage.status, "complete");
assert.deepEqual(trace.roots.map(({ image }) => image).sort(), [
"Contents/Frameworks/Core.framework/Versions/A/XPCServices/FwSvc.xpc/Contents/MacOS/FwSvc",
"Contents/Helpers/rea-tool",
"Contents/Library/LaunchServices/com.example.rea.helper",
"Contents/Library/LoginItems/Login.app/Contents/MacOS/Login",
MAIN,
"Contents/PlugIns/Ext.appex/Contents/MacOS/Ext",
SERVICE,
]);
verifyResolutionShapes(trace);
const slices = await verifyParserConformance(app, trace);
for (const root of [MAIN, SERVICE])
assert.deepEqual(
await loadedImages(app, root),
predictedImages(trace, root),
`${root} load order differs from DYLD_PRINT_LIBRARIES`,
);
return {
roots: trace.roots.length,
edges: trace.edges.length,
otool_slices: slices,
dyld_runtime_roots: 2,
};
}