Files
reverse-engineering-anything/src/application/native/NativeValueTrace.test.ts
T
morluto cef87c498a refactor(architecture): add the ports and resources layers, move ports and shared resource owners there, and ratchet the layer baseline (#11208)
* test(architecture): fail on a stale layer baseline and document the declared layers

verify:module-boundaries reported a layer baseline entry whose imports were
gone but still passed, so main kept process -> artifacts at 20 after it fell
to 17 and a later change could add three imports back unnoticed. A stale
entry now fails like growth; --shrink-layer-baseline lowers it in the change
that removes the imports. The architecture map and CONTRIBUTING name
scripts/lib/module-layers.mjs as the per-directory layer declaration.

* test(architecture): refuse to lower the layer baseline after an inspection failure

A file that cannot be parsed contributes no imports, so a shrink run would record counts that fail as growth once the file is fixed. Whole-command tests cover stale rejection, shrinking, refused growth and the inspection failure.

* refactor(ports): move the progress, work-scope and inspection-scope ports to src/ports (#11214)

* refactor(ports): move the progress, work-scope and inspection-scope ports to src/ports

Pure move with git mv and import specifier updates.

* test(architecture): declare the ports layer and lower the layer baseline

src/ports sits between the foundation and the providers. Moving the three
ports removes 11 provider -> application imports.

* refactor(ports): move AnalysisProvider to src/ports (#11217)

* refactor(ports): move AnalysisProvider to src/ports

Pure move with git mv and import specifier updates in its importers, the
built-fixture path, the CI scope glob and the lint-rule test spellings.

* test(architecture): lower the layer baseline after moving AnalysisProvider

* refactor(ports): move the per-feature provider ports to src/ports (#11226)

* refactor(ports): move the per-feature provider ports to src/ports

Pure move with git mv and import specifier updates: the 47 *Port.ts and
*Ports.ts files that providers implement, plus OptionalObservationProviders,
which only composes the observation ports.

* test(architecture): lower the layer baseline after moving the feature ports

* refactor(providers): move closeAnalysisClient and the Frida session input errors to their owners (#11233)

* refactor(providers): move closeAnalysisClient to src/providers and the Frida session input errors to src/frida

Pure move with git mv and import specifier updates.

* test(architecture): drop the application-providers and application-frida cycles from the layer baseline

* fix(ci): keep provider lanes selecting the ports moved to src/ports

Each lane glob that matched a moved port under src/application gains its src/ports counterpart; the change-scope test pins one moved port per lane.

* refactor(domain): declare the localPath host exception and read HOST_PLATFORM there (#11256)

absoluteLocalPathStringSchema decides which paths are absolute on the running host, so it reads HOST_PLATFORM instead of process.platform. Domain may not import outside domain/contracts; module-layers now declares LAYER_EXCEPTIONS, exact file-to-file imports with a reason, and both the layer count and the pure-layer boundary skip only that import. Any other domain -> platform import still fails.

* refactor(ports): move the doctor check and provider inspection types to src/ports (#11246)

* refactor(ports): move the doctor check and provider inspection types to src/ports

Declaration move from application/Doctor.ts (unchanged text) and import updates; no re-export from Doctor.ts.

* test(architecture): lower the layer baseline after moving the doctor provider types

* refactor(ports): move the packet capture and signal recording ports to src/ports (#11249)

* refactor(ports): move the packet capture and signal recording ports to src/ports

Declaration move (unchanged text) out of the two application services, with import updates.

* test(architecture): lower the layer baseline after moving the capture and signal ports

* refactor(domain): move the analysis error projection schema from contracts to domain (#11251)

* refactor(domain): move the analysis error projection schema from contracts to domain

Declaration move (unchanged text) of the projection schemas that domain modules embed; contracts/errorSchemas.ts keeps the CLI envelope and the published JSON Schema. Import updates only.

* test(architecture): drop the contracts-domain cycle from the layer baseline

* refactor(ports): drop the imports the moved capture, signal and doctor types left behind

Also give the packet capture and signal recording lanes the new port paths, with change-scope cases.

* refactor(resources): add src/resources and move AnalysisCleanupLease and ArtifactReader there (#11259)

* refactor(resources): move AnalysisCleanupLease to src/resources

Pure move: git mv plus import specifier rewrites (source, tests, and the dist paths the verifier scripts load).

* refactor(resources): declare src/resources as a foundation unit

src/resources owns the shared analysis resource modules that process, windows and providers hold; AnalysisCleanupLease is the first. Its tests join the adapters project, the apk signature lane selects it beside src/artifacts, and the layer baseline drops the process and windows imports of the lease.

* test(ci): pin the apk signature lane on the moved cleanup lease

* refactor(resources): move ArtifactReader to src/resources

Pure move: git mv plus import specifier rewrites (source, tests, and the dist path a verifier script loads).

* test(architecture): lower the layer baseline after moving ArtifactReader

windows no longer imports src/artifacts; process -> artifacts 11 -> 9. The CI lanes that selected src/artifacts/ArtifactReader.ts select its new path, pinned in the change-scope test.

* refactor(resources): move ArtifactResourceScope to src/resources (#11265)

* refactor(artifacts): name the output tree and private root capabilities the resource scope releases

ArtifactResourceScope typed its output-tree and private-runtime-root owners with SafeOutputTree (src/artifacts) and PrivateRuntimeRoot (src/process). It only calls rollback() and close(), so it now declares those capabilities itself and names neither unit, which lets it move to src/resources without a resources <-> process cycle. The withheld-export test narrows to SafeOutputTree where it reads the published flag.

* refactor(resources): move ArtifactResourceScope to src/resources

Pure move: git mv plus import specifier rewrites (source, tests, the dist paths verifier scripts load, and the FIFO probe's module-relative URL).

* test(architecture): lower the layer baseline after moving ArtifactResourceScope

process -> artifacts 9 -> 5, and the artifacts <-> dotnet mutual pair is gone. The sqlite and agentenv lanes select the scope at its new path, pinned in the change-scope test.

* refactor(resources): move ArtifactFileOperation to src/resources (#11269)

* refactor(resources): move ArtifactFileOperation to src/resources

Pure move: git mv plus import specifier rewrites (source, tests, and the dist paths verifier scripts load).

* test(architecture): lower the layer baseline after moving ArtifactFileOperation

process -> artifacts 5 -> 1.

* refactor(resources): move readStableArtifact to src/resources (#11271)

* refactor(resources): move readStableArtifact to src/resources

Pure move: git mv plus import specifier rewrites.

* test(architecture): lower the layer baseline after moving readStableArtifact

process no longer imports src/artifacts: process -> artifacts 1 -> 0 and the artifacts <-> process mutual pair is gone. The seven lanes that selected readStableArtifact select its new path, pinned in the change-scope test; the architecture map lists what src/resources owns.

* refactor(hopper): take Linux launcher verification from composition through a port (#11278)

The Hopper launcher imported inspectLinuxHopperLauncher and retryLinuxHopperPublisherCleanup from src/application (the two hopper -> application imports). src/ports/LinuxHopperLauncherVerification.ts now names that capability; src/application/LinuxHopper.ts provides LINUX_HOPPER_LAUNCHER_VERIFICATION, binary composition passes it to HopperProvider, and the provider hands it to the launcher. A launcher built without one refuses a Linux demo launch with reason verification_unavailable before running the launcher. Layer baseline: hopper -> application 2 -> 0.

* refactor(config): move Hopper launcher discovery to a src/hopperPaths foundation unit (#11292)

* refactor(hopper): move the launcher discovery and macOS Hopper paths to src/hopperPaths

Pure move: git mv plus import specifier rewrites.

* refactor(hopper): declare src/hopperPaths as a foundation unit with the Linux launcher candidates

Hopper install paths and launcher discovery are pure path facts that the
Hopper provider, doctor and setup all read. Declare src/hopperPaths in the
foundation layer, add it to the adapters test project, the architecture
map and the package-baseline CI lane, and move the Linux launcher
candidates out of src/application/LinuxHopper.ts into
src/hopperPaths/LinuxHopperPaths.ts so discovery no longer imports
application code.

* refactor(config): leave Hopper launcher discovery to the Hopper provider

parseConfig resolved an unset HOPPER_LAUNCHER_PATH by probing the
filesystem through application-owned discovery, so config imported
application (config -> application 2, application <-> config cycle) and
read process.platform ambiently.

AppConfig.hopperLauncherPath is now the caller's HOPPER_LAUNCHER_PATH
unchanged (string | undefined). src/hopperPaths/HopperLauncherDiscovery.ts
owns the selection (selectedHopperLauncherPath: the configured spelling,
else discovery from the caller environment's home on the provider's
platform); HopperProvider and the two real-Hopper verifiers read it.
Discovery tests move from config.test.ts to src/hopperPaths; a provider
test covers discovery from the provider's own platform and home.

Module layers: config -> application 2 -> 0; application <-> config removed.
Architecture guards: ambient platform reads in parseConfig.ts 2 -> 0.

* refactor(providers): move owned-process prerequisite availability out of application (#11294)

* refactor(providers): move the owned-process prerequisite availability mapping to src/providers

Pure move: git mv plus import specifier rewrites.

* test(architecture): lower the layer baseline after moving owned-process availability

android -> application 1 -> 0; apktool -> application 1 -> 0.

* refactor(process): move the regular-file read owner out of application (#11299)

* refactor(process): move the regular-file read owner from src/application to src/process

Pure move: git mv plus import specifier rewrites.

* test(architecture): lower the layer baseline after moving the regular-file read owner

desktop -> application 5 -> 4; dotnet -> application 2 -> 1.

The agentenv lane listed src/application/RegularFileRead.ts. At its new
path it is a src/process/ shared boundary: fast qualification selects the
code and runtime checks (and the single owning lane, web_source_map, via
src/process/**), and full qualification selects every lane, as for every
other src/process/ module. A second owner entry would only suppress the
web_source_map selection, so the agentenv entry is removed, not renamed.

* refactor(resources): give atomic text writes their own owner (#11302)

* refactor(resources): give atomic text writes their own owner in src/resources

src/application/JsonFiles.ts owned both JSON input reads and the atomic
text write (writeTextFile / writeTextParts and their staged-output
publication). The write side depends only on domain, filesystem and
resource owners, but il2cpp's Ghidra seed writer had to import
application to reach it (il2cpp -> application).

Move the write side verbatim to src/resources/TextFileWrite.ts
(TextWriteCancellation, writeTextFile, writeTextParts and their private
staging, publication and failure classification helpers). JsonFiles keeps
the JSON read owner and reads the shared errno-to-reason classifier
(fileAccessFailureReason) from the write owner instead of a copy.
Importers (il2cpp seed writer, evidence bundle files, desktop build
solves, analysis snapshot files and four boundary tests) read the new
path; no re-export is left in JsonFiles.

* test(architecture): lower the layer baseline after moving the text write owner

il2cpp -> application 1 -> 0.

* refactor(ports): move the desktop build host contract to src/ports (#11303)

* refactor(domain): move the setup cancellation check from src/application to src/domain

Pure move: git mv plus import specifier rewrites.

* refactor(ports): move the desktop build host contract to src/ports

The desktop provider implements doctor's desktop build facts reader and
setup's desktop build host, but the contract it implements lived in
application (DesktopBuildDoctor, SetupDesktopBuild, Doctor's
clientRegistrations signature, ClientRegistrationEnvironment's setting
types), so desktop imported application four times and the two units
were a recorded mutual pair.

src/ports/DesktopBuildHost.ts now holds that contract, moved verbatim:
DesktopBuildCommand, DesktopBuildFacts, DesktopBuildFactsInput,
DesktopBuildSetupFacts, DesktopBuildSetupHost, RecordedProviderSettings
and OwnedProviderSetting. Two names are new: DesktopBuildRegistration,
the registration facts the host reads (client, command and the
recorded-settings names; client registration status satisfies it), and
DesktopBuildRegistrationReader, which replaces
DoctorHost["clientRegistrations"] in the setup host. The facts input's
registration reader returns DesktopBuildRegistration instead of the full
application status; the setup facts' settings are spelled
Readonly<Record<string, string>> (SetupProviderEnvironment's definition).
Application imports the types from the port; no re-export is left.

* test(architecture): lower the layer baseline after the desktop build host port

desktop -> application 4 -> 0; the application <-> desktop mutual pair is gone.

* refactor(domain): define the resolved app bundle resource root beside its schema (#11304)

* refactor(domain): define the resolved app bundle resource root beside its provenance schema

ResolvedAppBundleResourceRoot (an app directory's resource-only identity)
is a pure type over BundleResourceRoot's layout, but it lived in
src/application/AppBundleExecutable.ts, so the native and artifact
providers that receive it imported application.

Move the interface verbatim to src/domain/apple/bundleResourceRoot.ts,
which owns BundleResourceRoot, and point its eight readers there (native
macOS provider and plist source, artifact provider, binary composition,
and the binary session, status, port and bundle-resource session). The
resolver that produces it stays in application.

* test(architecture): lower the layer baseline after moving the resource root type

native -> application 2 -> 0; artifacts -> application 2 -> 1 (the
artifact provider still constructs BundleResourceSession).

* refactor(config): move the pwntools input bound parser to src/config (#11306)

* refactor(config): move the pwntools input bound parser from src/native to src/config

Pure move: git mv plus import specifier rewrites, and the module path in docs/design/rea-limit-inventory.md (table realigned by oxfmt).

* test(architecture): lower the layer baseline after moving the pwntools input bound

config -> native 1 -> 0. The binary_layout and recorded_crash lanes, which
select src/native/pwntools/**, also select src/config/pwntoolsInputLimit*.ts;
ciChangeScope.test.ts pins both.

* refactor(filesystem): move REA cache directory helpers out of config (#11309)

* refactor(filesystem): move the REA cache directory and lexical directory helpers from src/config to src/filesystem

Pure move: git mv plus import specifier rewrites.

* test(architecture): drop the config and filesystem cycle from the layer baseline

config no longer imports filesystem; filesystem still reads the Windows
ChangeTime settle budget from config.

* refactor(windows): break the process and windows import cycle (#11317)

* refactor(domain): move the runtime-control and private-runtime-root refusals from src/process to src/domain

Pure move: git mv plus import specifier rewrites (including the dist path scripts/verify-windows-native.mjs loads).

* refactor(windows): type Job Object cleanup by what a Windows job reports

WindowsOwnedProcess.cleanup() returned process-ownership's
ProcessCleanupResult, so src/windows imported src/process for a type while
process imports windows to supervise Windows trees. A Job Object stop only
ever reports settled-and-signalled or a reason it did not settle; name that
subset WindowsJobCleanupResult in windows. It stays assignable to
ProcessCleanupResult, so the process supervisor reads it unchanged.

* test(architecture): drop the process and windows cycle from the layer baseline

* test(architecture): ratchet cross-provider imports and imports past a provider entry (#11386)

* test(architecture): ratchet cross-provider imports and imports past a provider entry

A provider folder may import the domain, contracts, foundation and ports layers, the shared provider core (src/providers) and its own files; an import of another provider folder is a recorded exception. Application, composition and the CLI/MCP adapters reach a provider only through src/<provider>/index.ts. verify:module-boundaries counts both kinds of import per unit pair in the layer baseline (crossProvider, providerEntry); like the upward counts they may only shrink, and a pair missing from the baseline fails.

* refactor(resources): move StableArtifactFailure and StableArtifactObservation into src/resources (#11387)

* refactor(resources): move StableArtifactFailure and StableArtifactObservation into src/resources

Providers project stable-read failures through StableArtifactFailure next to the other shared provider toolkit owners (ArtifactReader, ArtifactResourceScope, ArtifactFileOperation, AnalysisCleanupLease, readStableArtifact) that already live in src/resources. Under src/artifacts it made every provider that reads a stable artifact import the artifacts provider. Pure move: git mv plus import specifier and baseline updates; the layer baseline drops the provider-boundary counts the move removes.

* refactor(resources): reach the shared provider toolkit through src/resources/index.ts (#11390)

* refactor(resources): import the shared provider toolkit through src/resources/index.ts

Adds src/resources/index.ts, which exports the toolkit names that production code outside src/resources uses, and rewrites those importers (420 files) to import from it, merging each importer's toolkit imports into one declaration. Mechanical: import declarations only.

* test(architecture): reach src/resources only through its entry file

verify:module-boundaries now counts any production import of a src/resources file other than src/resources/index.ts, from any unit, with the imports past a provider entry. The count is renamed from providerEntry to entry ("import past the entry file"); it may only shrink, and src/resources starts at zero.

* test(architecture): cover provider boundary counts through the verifier command

* refactor(resources): move the safe output tree and artifact path owners into src/resources (#11398)

* refactor(resources): move the safe output tree and artifact path owners into src/resources

SafeOutputTree (with its admission, creation failure, readback, cleanup and identity files), StreamBytes, ArtifactPaths and readArtifactFileChunks depend only on domain, filesystem, platform and the toolkit, and browser, firmware, ghidra, javascript, android and apktool use them to publish outputs. Under src/artifacts every such provider imported the artifacts provider. Pure move: git mv, importers moved onto src/resources/index.ts (names added to the entry), the moved files import toolkit modules directly, path keys in the CI scopes and guard baseline follow the move, and the layer baseline drops the removed imports.

* test(architecture): give the shared provider core its own layer below the providers (#11401)

src/providers (deferred client owners and failure attribution) now imports only domain, ports, process, filesystem and the resources toolkit. Declaring it as the providerCore layer, between ports and providers, makes any import from it into a provider folder an upward import that verify:module-boundaries rejects, and lets every unit in the providers layer be a provider folder.

* test(architecture): declare each provider's entry files instead of a provider barrel (#11407)

* chore: ignore the source map parser move (#11348) in blame

* test(architecture): declare inspector's use of the browser CDP client as a provider dependency (#11420)
2026-11-16 03:56:10 +08:00

1946 lines
62 KiB
TypeScript

import { createAnalysisExecutionEvidence } from "../../domain/analysisObservation.js";
import { describe, expect, it } from "vitest";
import { traceNativeValues as traceNativeValuesCore } from "./NativeValueTrace.js";
import { executeComposedWorkflow } from "../WorkflowEvidence.js";
import {
createAnalysisExecution,
type AnalysisOperationPort,
} from "../../ports/AnalysisProvider.js";
import { ghidraFunctionDossier } from "../../domain/ghidraValues.fixture.js";
import { functionDossierSchema } from "../../domain/native/functionDossier.js";
import { jsonValueSchema } from "../../domain/jsonValue.js";
import { nativeValueTraceSchema } from "../../domain/native/nativeValueTrace.js";
import {
AnalysisCancelledError,
AnalysisOutputError,
AnalysisInputError,
AnalysisResourceConstraintError,
} from "../../domain/analysisErrorCore.js";
import { NoBinaryOpenError } from "../../domain/configurationErrors.js";
import { ok, err } from "../../domain/result.js";
import {
EXPECTED_UNRECORDED_VALUE_FLOW,
nativeFlowDossier,
pcodeOperation as node,
pcodeScalar as scalar,
staticValueGraph,
valueTraceDiamond,
valueTraceUnboundArguments,
valueTraceProvider as provider,
valueTraceSubject as subject,
} from "./NativeValueTrace.fixture.js";
const valueFlowLimits = {
max_operations: 3_000,
max_inputs_per_operation: 64,
max_edges: 12_000,
};
const traceNativeValues: typeof traceNativeValuesCore = async (
source,
parameters,
signal,
) => {
const result = await executeComposedWorkflow(source, (observed) =>
traceNativeValuesCore(observed, parameters, signal),
);
return result.ok ? ok(result.value.result) : result;
};
it("does not return a source ID without an Evidence owner", async () => {
const result = await traceNativeValuesCore(valueTraceDiamond().port, {
procedure: "0x1000",
});
expect(result.ok).toBe(false);
if (!result.ok)
expect(result.error._tag).toBe("AnalysisCapabilityUnavailableError");
});
const analysis: AnalysisOperationPort = {
execute: async (operation, parameters) => {
if (operation === "resolve_native_call_targets")
return ok(
createAnalysisExecution(
{
call_site: "0x1000",
procedure: "0x1000",
status: "direct",
mechanism: "direct",
targets: [
{
address: "0x2000",
procedure: "0x2000",
status: "direct",
basis: "provider-reference",
references: [],
},
],
limitations: [],
},
provider,
{ subject },
),
);
const callee = parameters.procedure === "0x2000";
return ok(
createAnalysisExecution(
jsonValueSchema.parse({
...functionDossierSchema.parse(ghidraFunctionDossier()),
procedure: {
address: callee ? "0x2000" : "0x1000",
name: callee ? "callee" : "caller",
classification: null,
body: {
available: false,
reason:
"The provider did not report complete function body ranges.",
},
signature: null,
locals: [],
},
native_value_flow: {
available: true,
provenance: "ghidra-high-pcode",
limits: valueFlowLimits,
limits_reached: [],
operations: callee
? [node("return", "0x2000", "RETURN")]
: [node("call", "0x1000", "CALL")],
def_use: [],
effects: [],
parameters: callee
? [{ ordinal: 0, name: "value", data_type: "/int" }]
: [],
parameter_uses: callee
? [{ ordinal: 0, use: "return", input_index: 1 }]
: [],
truncated: false,
omitted_operations_lower_bound: 0,
known_omitted_inputs: 0,
known_omitted_edges: 0,
limitations: [],
},
}),
provider,
{ subject },
),
);
},
};
describe("native value trace seed admission", () => {
it("preserves the exact abort reason before attempting a seed", async () => {
const reason = { request: "cancel before native trace" };
const attempted: unknown[] = [];
const result = await traceNativeValues(
{
execute: async (operation, parameters) => {
attempted.push([operation, parameters]);
return analysis.execute(operation, parameters);
},
},
{ procedure: "0x1000" },
AbortSignal.abort(reason),
);
if (result.ok) throw new Error("Expected pre-seed cancellation");
expect(result.error._tag).toBe("AnalysisCancelledError");
expect(result.error.cause).toBe(reason);
expect(attempted).toEqual([]);
});
it.each([
new AnalysisInputError("analyze_function", {
userMessage: "Unknown selector: missing",
}),
new NoBinaryOpenError(),
new AnalysisResourceConstraintError(
"analyze_function",
"capacity",
"Provider is busy",
null,
{ retryable: true },
),
new AnalysisCancelledError("analyze_function"),
])("preserves the original seed failure object: $_tag", async (failure) => {
const attempted: unknown[] = [];
const result = await traceNativeValues(
{
execute: async (operation, parameters) => {
attempted.push([operation, parameters]);
return err(failure);
},
},
{ procedure: "missing" },
);
if (result.ok) throw new Error("Expected seed failure");
expect(result.error).toBe(failure);
expect(attempted).toEqual([["analyze_function", { procedure: "missing" }]]);
});
it("retains a successful seed without def-use as a useful unknown trace", async () => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const result = await traceNativeValues(
{
execute: async () =>
ok(
createAnalysisExecution(
{ ...base, native_value_flow: null },
provider,
{ subject },
),
),
},
{ procedure: base.procedure.address },
);
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value)).toMatchObject({
target_sha256: subject.sha256,
decompilations: 1,
procedures: [
expect.objectContaining({ address: base.procedure.address }),
],
unknowns: [
{
procedure: base.procedure.address,
address: null,
reason: EXPECTED_UNRECORDED_VALUE_FLOW,
},
],
});
});
it("preserves an explicit unavailable reason for that procedure", async () => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const providerReason = "selected procedure has no p-code model";
const result = await traceNativeValues(
{
execute: async () =>
ok(
createAnalysisExecution(
{
...base,
native_value_flow: {
available: false,
reason: providerReason,
limitations: [],
},
},
provider,
{ subject },
),
),
},
{ procedure: base.procedure.address },
);
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value).unknowns).toEqual([
{
procedure: base.procedure.address,
address: null,
reason: providerReason,
},
]);
});
});
describe("native value parameter-use node membership", () => {
it("retains only parameter uses whose parameter nodes fit the node budget", async () => {
const operationCount = 3_000;
const parameterUseCount = 12_000;
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const procedureAddress = base.procedure.address;
const largeFlow = {
available: true as const,
provenance: "ghidra-high-pcode" as const,
limits: valueFlowLimits,
limits_reached: [],
operations: Array.from({ length: operationCount }, (_, index) => ({
...node(`operation-${String(index)}`, procedureAddress, "MULTIEQUAL"),
inputs: [scalar, scalar, scalar, scalar],
})),
def_use: [],
effects: [],
parameters: [{ ordinal: 0, name: "value", data_type: "/int" }],
parameter_uses: Array.from({ length: parameterUseCount }, (_, index) => ({
ordinal: 0,
use: `operation-${String(index % operationCount)}`,
input_index: Math.floor(index / operationCount),
})),
truncated: false,
omitted_operations_lower_bound: 0,
known_omitted_inputs: 0,
known_omitted_edges: 0,
limitations: [],
};
const largeDossier = { ...base, native_value_flow: largeFlow };
const largeAnalysis: AnalysisOperationPort = {
execute: async () =>
ok(createAnalysisExecution(largeDossier, provider, { subject })),
};
const includedParameter = await traceNativeValues(largeAnalysis, {
procedure: procedureAddress,
max_nodes: 20_000,
max_edges: 40_000,
offset: operationCount,
limit: 1,
});
if (!includedParameter.ok) throw includedParameter.error;
const includedResult = nativeValueTraceSchema.parse(
includedParameter.value,
);
expect(includedResult).toMatchObject({
total_nodes: operationCount + 1,
total_edges: parameterUseCount,
nodes: [
expect.objectContaining({
id: `${procedureAddress}/parameter:0`,
kind: "parameter",
}),
],
page_truncated: true,
traversal: { status: "complete" },
});
expect(includedResult.edges).toHaveLength(parameterUseCount);
const omittedParameter = await traceNativeValues(largeAnalysis, {
procedure: procedureAddress,
max_nodes: operationCount,
max_edges: 40_000,
offset: operationCount - 1,
limit: 1,
});
if (!omittedParameter.ok) throw omittedParameter.error;
const omittedResult = nativeValueTraceSchema.parse(omittedParameter.value);
expect(omittedResult).toMatchObject({
total_nodes: operationCount,
total_edges: 0,
nodes: [expect.objectContaining({ kind: "operation" })],
edges: [],
page_truncated: true,
traversal: { status: "truncated", reasons: ["max_nodes"] },
});
expect(omittedResult.unknowns).toEqual([
{
procedure: procedureAddress,
address: null,
reason: "Node budget reached",
},
]);
});
it.each([3, 4])(
"keeps parameter identity separate from colliding operation IDs with max_nodes=%i",
async (max_nodes) => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const address = base.procedure.address;
const execution = createAnalysisExecution(
{
...base,
native_value_flow: {
available: true,
provenance: "ghidra-high-pcode",
limits: valueFlowLimits,
limits_reached: [],
operations: [
node("parameter:0", address, "COPY"),
node("parameter:0#1", address, "COPY"),
node("use", address, "COPY"),
],
def_use: [],
effects: [],
parameters: [{ ordinal: 0, name: "value", data_type: "/int" }],
parameter_uses: [{ ordinal: 0, use: "use", input_index: 1 }],
truncated: false,
omitted_operations_lower_bound: 0,
known_omitted_inputs: 0,
known_omitted_edges: 0,
limitations: [],
},
},
provider,
{ subject },
);
const result = await traceNativeValues(
{ execute: async () => ok(execution) },
{ procedure: address, max_nodes },
);
if (!result.ok) throw result.error;
const parsedResult = nativeValueTraceSchema.parse(result.value);
expect(parsedResult).toMatchObject({
total_nodes: max_nodes,
total_edges: max_nodes === 3 ? 0 : 1,
edges:
max_nodes === 3
? []
: [
{
source: `${address}/parameter:0#2`,
target: `${address}/use`,
kind: "parameter-use",
},
],
});
expect(
parsedResult.nodes
.filter(({ kind }) => kind === "operation")
.map(({ id }) => id),
).toEqual([
`${address}/parameter:0`,
`${address}/parameter:0#1`,
`${address}/use`,
]);
expect(new Set(parsedResult.nodes.map(({ id }) => id)).size).toBe(
max_nodes,
);
},
);
});
const callBindingAnalysis = (input: {
readonly callerAddress: string;
readonly calleeAddress: string;
readonly caller: ReturnType<typeof jsonValueSchema.parse>;
readonly callee: ReturnType<typeof jsonValueSchema.parse>;
readonly targetLimitations?: readonly string[];
}): AnalysisOperationPort => ({
execute: async (operation, parameters) => {
if (operation === "resolve_native_call_targets")
return ok(
createAnalysisExecution(
{
call_site: String(parameters.address),
procedure: input.calleeAddress,
status: "direct",
mechanism: "direct",
targets: [
{
address: input.calleeAddress,
procedure: input.calleeAddress,
status: "direct",
basis: "provider-reference",
references: [],
},
],
limitations: [...(input.targetLimitations ?? [])],
},
provider,
{ subject },
),
);
if (operation !== "analyze_function")
throw new Error(`Unexpected native trace operation: ${operation}`);
if (
parameters.procedure !== input.callerAddress &&
parameters.procedure !== input.calleeAddress
)
throw new Error(`Unexpected procedure: ${String(parameters.procedure)}`);
return ok(
createAnalysisExecution(
parameters.procedure === input.callerAddress
? input.caller
: input.callee,
provider,
{ subject },
),
);
},
});
describe("native value failed-callee traversal", () => {
it("attempts a repeated failed selector once and still analyzes a later callee", async () => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const caller = nativeFlowDossier(base, {
address: "0x1000",
name: "caller",
operations: [
node("bad-1", "0x1100", "CALL"),
node("bad-2", "0x1104", "CALL"),
node("good", "0x1108", "CALL"),
],
parameters: [],
});
const good = nativeFlowDossier(base, {
address: "0x3000",
name: "good",
operations: [],
parameters: [],
});
const attempts: string[] = [];
const port: AnalysisOperationPort = {
execute: async (operation, parameters) => {
if (operation === "resolve_native_call_targets") {
const destination =
parameters.address === "0x1108" ? "0x3000" : "0x2000";
return ok(
createAnalysisExecution(
{
call_site: String(parameters.address),
procedure: "0x1000",
status: "direct",
mechanism: "direct",
targets: [
{
address: destination,
procedure: destination,
status: "direct",
basis: "provider-reference",
references: [],
},
],
limitations: [],
},
provider,
{ subject },
),
);
}
expect(operation).toBe("analyze_function");
const selector = String(parameters.procedure);
attempts.push(selector);
if (selector === "0x2000")
return err(
new AnalysisOutputError("analyze_function", "Callee unavailable"),
);
return ok(
createAnalysisExecution(
selector === "0x1000" ? caller : good,
provider,
{ subject },
),
);
},
};
const expectedCalls = [
{ source: "0x1000/bad-1", target: "0x2000" },
{ source: "0x1000/bad-2", target: "0x2000" },
{ source: "0x1000/good", target: "0x3000" },
];
// A new invocation retries the failed selector: failures are not cached
// across traces, and successful procedure identity remains separate.
for (let run = 0; run < 2; run++) {
attempts.length = 0;
const result = await traceNativeValues(port, {
procedure: "0x1000",
max_functions: 3,
});
if (!result.ok) throw result.error;
const trace = nativeValueTraceSchema.parse(result.value);
expect(attempts).toEqual(["0x1000", "0x2000", "0x3000"]);
expect(trace.decompilations).toBe(3);
expect(trace.procedures.map(({ address }) => address)).toEqual([
"0x1000",
"0x3000",
]);
expect(trace.unknowns).toContainEqual({
procedure: "0x2000",
address: null,
reason: expect.stringContaining("Callee unavailable"),
});
expect(
trace.edges
.filter(({ kind }) => kind === "call")
.map(({ source, target }) => ({ source, target })),
).toEqual(expectedCalls);
// A failed callee is an explicit unknown, not a traversal budget stop.
expect(trace).toMatchObject({
page_truncated: false,
traversal: { status: "complete" },
truncated: false,
});
}
const capped = await traceNativeValues(port, {
procedure: "0x1000",
max_functions: 3,
max_edges: 2,
});
if (!capped.ok) throw capped.error;
const cappedTrace = nativeValueTraceSchema.parse(capped.value);
expect(
cappedTrace.edges.map(({ source, target }) => ({ source, target })),
).toEqual(expectedCalls.slice(0, 2));
expect(cappedTrace).toMatchObject({
page_truncated: false,
traversal: { status: "truncated", reasons: ["max_edges"] },
truncated: true,
});
expect(cappedTrace.unknowns).toContainEqual({
procedure: "0x1000",
address: null,
reason: "Edge budget reached",
});
});
});
it("preserves and deduplicates source value-flow limitations across analyzed functions", async () => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const caller = functionDossierSchema.parse(
nativeFlowDossier(base, {
address: "0x1000",
name: "caller",
operations: [node("call", "0x1000", "CALL")],
parameters: [],
}),
);
const callee = functionDossierSchema.parse(
nativeFlowDossier(base, {
address: "0x2000",
name: "callee",
operations: [],
parameters: [],
}),
);
if (
!caller.native_value_flow?.available ||
!callee.native_value_flow?.available
)
throw new Error("Expected available fixture value flow");
caller.native_value_flow.limitations = ["Shared provider caveat."];
callee.native_value_flow.limitations = [
"Shared provider caveat.",
"Callee-specific caveat.",
];
const result = await traceNativeValues(
callBindingAnalysis({
callerAddress: "0x1000",
calleeAddress: "0x2000",
caller,
callee,
}),
{ procedure: "0x1000", max_functions: 2 },
);
if (!result.ok) throw result.error;
const limitations = nativeValueTraceSchema.parse(result.value).limitations;
expect(limitations).toContain(
"Fixture (fixture 1) analyze_function: Callee-specific caveat.",
);
expect(
limitations.filter((item) => item.includes("Shared provider caveat.")),
).toEqual(["Fixture (fixture 1) analyze_function: Shared provider caveat."]);
});
describe("native value call-binding fan-out", () => {
it("retains limitations embedded in resolved call-target results", async () => {
const callerAddress = "0x1000";
const calleeAddress = "0x2000";
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const caller = nativeFlowDossier(base, {
address: callerAddress,
name: "caller",
operations: [node("call", callerAddress, "CALL")],
parameters: [],
});
const callee = nativeFlowDossier(base, {
address: calleeAddress,
name: "callee",
operations: [],
parameters: [],
});
const caveat = "Only statically resolved call sites are included.";
const result = await traceNativeValues(
callBindingAnalysis({
callerAddress,
calleeAddress,
caller,
callee,
targetLimitations: [caveat],
}),
{ procedure: callerAddress, max_functions: 2 },
);
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value).limitations).toContain(
`Fixture (fixture 1) resolve_native_call_targets: ${caveat}`,
);
});
it("preserves binding order and edge-budget behavior for repeated callees", async () => {
const callCount = 128;
const callerAddress = "0x1000";
const calleeAddress = "0x2000";
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const caller = nativeFlowDossier(base, {
address: callerAddress,
name: "caller",
operations: Array.from({ length: callCount }, (_, index) => ({
...node(
`call-${index}`,
`0x${(0x1000 + index * 4).toString(16)}`,
"CALL",
),
sequence: index,
inputs: [scalar, scalar],
})),
parameters: [],
});
const callee = nativeFlowDossier(base, {
address: calleeAddress,
name: "callee",
operations: [node("return", calleeAddress, "RETURN")],
parameters: [{ ordinal: 0, name: "value", data_type: "/int" }],
});
const fanoutAnalysis = callBindingAnalysis({
callerAddress,
calleeAddress,
caller,
callee,
});
const expectedEdges = [
...Array.from({ length: callCount }, (_, index) => ({
source: `${callerAddress}/call-${index}`,
target: calleeAddress,
input_index: null,
kind: "call",
})),
...Array.from({ length: callCount }, (_, index) => [
{
source: `${callerAddress}/call-${index}`,
target: `${calleeAddress}/parameter:0`,
input_index: 1,
kind: "argument-binding",
},
{
source: `${calleeAddress}/return`,
target: `${callerAddress}/call-${index}`,
input_index: 1,
kind: "return-binding",
},
]).flat(),
];
const request = {
procedure: callerAddress,
max_functions: 2,
max_call_sites: callCount,
max_nodes: callCount + 2,
max_edges: callCount * 3,
limit: callCount + 2,
};
const complete = await traceNativeValues(fanoutAnalysis, request);
if (!complete.ok) throw complete.error;
const completeResult = nativeValueTraceSchema.parse(complete.value);
const edgeShape = (edges: typeof completeResult.edges) =>
edges.map(({ source, target, input_index, kind }) => ({
source,
target,
input_index,
kind,
}));
expect(completeResult).toMatchObject({
total_nodes: callCount + 2,
total_edges: callCount * 3,
traversal: { status: "complete" },
unknowns: [],
});
expect(edgeShape(completeResult.edges)).toEqual(expectedEdges);
const capped = await traceNativeValues(fanoutAnalysis, {
...request,
max_edges: callCount * 3 - 1,
});
if (!capped.ok) throw capped.error;
const cappedResult = nativeValueTraceSchema.parse(capped.value);
expect(cappedResult).toMatchObject({
total_nodes: callCount + 2,
total_edges: callCount * 3 - 1,
traversal: { status: "truncated", reasons: ["max_edges"] },
unknowns: [budgetUnknown(callerAddress, "Edge budget reached")],
});
expect(edgeShape(cappedResult.edges)).toEqual(
expectedEdges.slice(0, callCount * 3 - 1),
);
const nodeCapped = await traceNativeValues(fanoutAnalysis, {
...request,
max_nodes: callCount + 1,
});
if (!nodeCapped.ok) throw nodeCapped.error;
const nodeCappedResult = nativeValueTraceSchema.parse(nodeCapped.value);
expect(nodeCappedResult).toMatchObject({
total_nodes: callCount + 1,
total_edges: callCount * 2,
traversal: { status: "truncated", reasons: ["max_nodes"] },
unknowns: [
budgetUnknown(calleeAddress, "Node budget reached"),
...Array.from({ length: callCount }, () => ({
procedure: callerAddress,
address: null,
reason: `Parameter bindings for ${calleeAddress} are unavailable or omitted`,
})),
],
});
expect(edgeShape(nodeCappedResult.edges)).toEqual(
expectedEdges.filter(({ kind }) => kind !== "argument-binding"),
);
});
});
describe("bounded native value dependency composition", () => {
it("reports work truncation and stable pagination without traversing ambiguous calls", async () => {
const bounded = await traceNativeValues(analysis, {
procedure: "0x1000",
max_functions: 1,
});
expect(bounded.ok).toBe(true);
if (bounded.ok)
expect(bounded.value).toMatchObject({
page_truncated: false,
traversal: { status: "truncated", reasons: ["max_functions"] },
truncated: true,
decompilations: 1,
});
const page = await traceNativeValues(analysis, {
procedure: "0x1000",
limit: 1,
});
expect(page.ok).toBe(true);
if (page.ok)
expect(page.value).toMatchObject({
total_nodes: 3,
next_offset: 1,
page_truncated: true,
traversal: { status: "complete" },
});
const ambiguous = await traceNativeValues(
{
execute: async (operation, parameters, options) => {
if (operation !== "resolve_native_call_targets")
return analysis.execute(operation, parameters, options);
return ok(
createAnalysisExecution(
{
call_site: "0x1000",
procedure: "0x1000",
status: "ambiguous",
mechanism: "computed",
targets: [
{
address: "0x2000",
procedure: "0x2000",
status: "candidate",
basis: "provider-reference",
references: [],
},
],
limitations: [],
},
provider,
{ subject },
),
);
},
},
{ procedure: "0x1000" },
);
expect(ambiguous.ok).toBe(true);
if (ambiguous.ok)
expect(ambiguous.value).toMatchObject({
decompilations: 1,
edges: [expect.objectContaining({ status: "candidate" })],
traversal: {
status: "truncated",
reasons: ["ambiguous_call_candidates"],
},
});
});
it("propagates cancellation and rejects cross-target dependencies", async () => {
const controller = new AbortController();
controller.abort();
expect(
await traceNativeValues(
analysis,
{ procedure: "0x1000" },
controller.signal,
),
).toMatchObject({ ok: false, error: { _tag: "AnalysisCancelledError" } });
const cancelled = await traceNativeValues(
{
execute: async () =>
err(new AnalysisCancelledError("analyze_function")),
},
{ procedure: "0x1000" },
);
expect(cancelled.ok).toBe(false);
const mismatch = await traceNativeValues(
{
execute: async (operation, parameters, options) => {
const value = await analysis.execute(operation, parameters, options);
return value.ok && parameters.procedure === "0x2000"
? ok({
...value.value,
subject: { ...subject, sha256: "b".repeat(64) },
})
: value;
},
},
{ procedure: "0x1000" },
);
expect(mismatch).toMatchObject({
ok: false,
error: { _tag: "AnalysisOutputError" },
});
});
it("returns typed cancellation when the caller aborts with the final provider reply", async () => {
const controller = new AbortController();
const reason = { request: "stop trace" };
const result = await traceNativeValues(
{
execute: async (operation, parameters, options) => {
const value = await analysis.execute(operation, parameters, options);
if (parameters.procedure === "0x2000") controller.abort(reason);
return value;
},
},
{ procedure: "0x1000" },
controller.signal,
);
if (result.ok) throw new Error("Expected cancellation");
expect(result.error._tag).toBe("AnalysisCancelledError");
expect(result.error.cause).toBe(reason);
});
});
describe("native value trace selector origin", () => {
it("marks only the initial procedure as caller-supplied", async () => {
const analyzed: { procedure: unknown; origin: unknown }[] = [];
const result = await traceNativeValues(
{
execute: async (operation, parameters, options) => {
if (operation === "analyze_function")
analyzed.push({
procedure: parameters.procedure,
origin: options?.selectorOrigin,
});
return analysis.execute(operation, parameters, options);
},
},
{ procedure: "0x1000" },
);
expect(result.ok, "the trace completes").toBe(true);
expect(
analyzed,
"the caller's procedure is unmarked and the discovered callee is provider-derived",
).toEqual([
{ procedure: "0x1000", origin: undefined },
{ procedure: "0x2000", origin: "provider" },
]);
});
});
describe("native value node byte budgets", () => {
it("stops admitting parameters after an operation exceeds the byte budget", async () => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const value = nativeFlowDossier(base, {
address: "0x1000",
name: "caller",
operations: [node("use", "0x1000", "a".repeat(9 * 1024 * 1024))],
parameters: [{ ordinal: 0, name: "value", data_type: "/int" }],
});
const result = await traceNativeValues(
{
execute: async () =>
ok(createAnalysisExecution(value, provider, { subject })),
},
{ procedure: "0x1000" },
);
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value)).toMatchObject({
traversal: { status: "truncated", reasons: ["node_payload_bytes"] },
truncated: true,
total_nodes: 0,
total_edges: 0,
nodes: [],
edges: [],
unknowns: [
{
procedure: "0x1000",
address: null,
reason: "Node payload budget reached",
},
],
});
});
it.each([
{ label: "plain text", name: "a".repeat(9 * 1024 * 1024) },
{ label: "JSON escapes", name: "\0".repeat(2 * 1024 * 1024) },
])("rejects an oversized parameter encoded with $label", async ({ name }) => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const value = nativeFlowDossier(base, {
address: "0x1000",
name: "caller",
operations: [
node("parameter:0", "0x1000", "COPY"),
node("use", "0x1000", "MULTIEQUAL"),
],
parameters: [{ ordinal: 0, name, data_type: "/int" }],
});
const parsed = functionDossierSchema.parse(value);
if (!parsed.native_value_flow?.available)
throw new Error("Invalid fixture");
parsed.native_value_flow.parameter_uses = [
{ ordinal: 0, use: "use", input_index: 0 },
];
const result = await traceNativeValues(
{
execute: async () =>
ok(createAnalysisExecution(parsed, provider, { subject })),
},
{ procedure: "0x1000" },
);
if (!result.ok) throw result.error;
const trace = nativeValueTraceSchema.parse(result.value);
expect(trace).toMatchObject({
traversal: { status: "truncated", reasons: ["node_payload_bytes"] },
truncated: true,
total_nodes: 2,
total_edges: 0,
nodes: [
expect.objectContaining({ kind: "operation" }),
expect.objectContaining({ kind: "operation" }),
],
});
});
it("shares the byte budget across operation and parameter nodes", async () => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const value = nativeFlowDossier(base, {
address: "0x1000",
name: "caller",
operations: [node("use", "0x1000", "MULTIEQUAL")],
parameters: [0, 1].map((ordinal) => ({
ordinal,
name: "a".repeat(5 * 1024 * 1024),
data_type: "/int",
})),
});
const parsed = functionDossierSchema.parse(value);
if (!parsed.native_value_flow?.available)
throw new Error("Invalid fixture");
parsed.native_value_flow.parameter_uses = [0, 1].map((ordinal) => ({
ordinal,
use: "use",
input_index: ordinal,
}));
const result = await traceNativeValues(
{
execute: async () =>
ok(createAnalysisExecution(parsed, provider, { subject })),
},
{ procedure: "0x1000" },
);
if (!result.ok) throw result.error;
const trace = nativeValueTraceSchema.parse(result.value);
expect(trace).toMatchObject({
traversal: { status: "truncated", reasons: ["node_payload_bytes"] },
truncated: true,
total_nodes: 2,
total_edges: 1,
});
expect(trace.nodes.map(({ id }) => id)).toEqual([
"0x1000/use",
"0x1000/parameter:0",
]);
expect(trace.edges[0]?.source).toBe("0x1000/parameter:0");
const retainedBytes = trace.nodes.reduce(
(total, value) => total + Buffer.byteLength(JSON.stringify(value)),
0,
);
expect(retainedBytes).toBeLessThanOrEqual(8 * 1024 * 1024);
});
});
describe("native value byte-bound call bindings", () => {
it("omits argument bindings to rejected callee parameter nodes", async () => {
const base = functionDossierSchema.parse(ghidraFunctionDossier());
const caller = nativeFlowDossier(base, {
address: "0x1000",
name: "caller",
operations: [node("call", "0x1000", "CALL")],
parameters: [],
});
const callee = nativeFlowDossier(base, {
address: "0x2000",
name: "callee",
operations: [node("return", "0x2000", "RETURN")],
parameters: [
{ ordinal: 0, name: "a".repeat(9 * 1024 * 1024), data_type: "/int" },
],
});
const result = await traceNativeValues(
callBindingAnalysis({
callerAddress: "0x1000",
calleeAddress: "0x2000",
caller,
callee,
}),
{ procedure: "0x1000" },
);
if (!result.ok) throw result.error;
const trace = nativeValueTraceSchema.parse(result.value);
expect(trace).toMatchObject({
traversal: { status: "truncated", reasons: ["node_payload_bytes"] },
truncated: true,
});
expect(trace.nodes.map(({ kind }) => kind)).toEqual([
"operation",
"operation",
]);
expect(trace.edges.map(({ kind }) => kind)).toEqual([
"call",
"return-binding",
]);
expect(trace.unknowns).toContainEqual(
expect.objectContaining({
reason: "Parameter bindings for 0x2000 are unavailable or omitted",
}),
);
});
});
describe("native value validation failure provenance", () => {
it.each(["call-targets", "target-digest"] as const)(
"retains the exact provider observation for invalid %s",
async (failure) => {
let expected:
| ReturnType<typeof createAnalysisExecutionEvidence>
| undefined;
const result = await traceNativeValues(
{
execute: async (operation, parameters, options) => {
const execution = await analysis.execute(
operation,
parameters,
options,
);
if (!execution.ok) return execution;
if (
(failure === "call-targets" &&
operation === "resolve_native_call_targets") ||
(failure === "target-digest" && parameters.procedure === "0x2000")
) {
const observation = {
...execution.value,
...(failure === "call-targets"
? { result: { malformed: "call targets" } }
: { subject: { ...subject, sha256: "b".repeat(64) } }),
rawResult: { original: "provider output" },
limitations: ["Provider coverage is partial"],
};
expected = createAnalysisExecutionEvidence(
operation,
parameters,
observation,
"observed",
);
return ok(observation);
}
return execution;
},
},
{ procedure: "0x1000" },
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error._tag).toBe("AnalysisOutputError");
expect(expected).toBeDefined();
expect(result.error.partialObservation).toEqual(expected);
},
);
});
const budgetUnknown = (procedure: string, reason: string) => ({
procedure,
address: null,
reason,
});
const traced = async (
port: AnalysisOperationPort,
request: Record<string, unknown>,
) => {
const result = await traceNativeValues(port, request);
if (!result.ok) throw result.error;
return nativeValueTraceSchema.parse(result.value);
};
const omittedBindings = (callee: string, callers: readonly string[]) =>
callers.map((procedure) =>
budgetUnknown(
procedure,
`Parameter bindings for ${callee} are unavailable or omitted`,
),
);
const edgeKey = (edge: { source: string; target: string; kind: string }) =>
`${edge.kind} ${edge.source} ${edge.target}`;
describe("native value trace page omission", () => {
it("assembles every page of a complete trace without reporting traversal truncation", async () => {
const { port } = valueTraceDiamond();
const request = { procedure: "0x1000", max_depth: 1 };
const complete = await traced(port, request);
expect(complete).toMatchObject({
total_nodes: 11,
total_edges: 9,
next_offset: null,
page_truncated: false,
traversal: { status: "complete" },
truncated: false,
unknowns: [],
});
const pages: (typeof complete)[] = [];
for (let offset: number | null = 0; offset !== null;) {
const page = await traced(port, { ...request, offset, limit: 2 });
pages.push(page);
offset = page.next_offset;
}
expect(pages).toHaveLength(6);
for (const page of pages)
expect(page).toMatchObject({
total_nodes: 11,
total_edges: 9,
page_truncated: true,
traversal: { status: "complete" },
truncated: true,
unknowns: [],
});
expect(pages.at(-1)).toMatchObject({ next_offset: null, nodes: [{}] });
expect(pages.flatMap(({ nodes }) => nodes)).toEqual(complete.nodes);
expect(pages.flatMap(({ edges }) => edges.map(edgeKey)).sort()).toEqual(
complete.edges.map(edgeKey).sort(),
);
expect(await traced(port, { ...request, limit: 11 })).toMatchObject({
next_offset: null,
page_truncated: false,
truncated: false,
});
});
it("keeps a page beyond the retained nodes separate from traversal truncation", async () => {
const { port } = valueTraceDiamond();
expect(
await traced(port, { procedure: "0x1000", max_depth: 1, offset: 100 }),
).toMatchObject({
nodes: [],
edges: [],
total_nodes: 11,
next_offset: null,
page_truncated: true,
traversal: { status: "complete" },
});
});
it("does not report an exactly filled node or edge budget as truncation", async () => {
const { port } = valueTraceDiamond();
expect(
await traced(port, {
procedure: "0x1000",
max_depth: 1,
max_nodes: 11,
max_edges: 9,
}),
).toMatchObject({
total_nodes: 11,
total_edges: 9,
traversal: { status: "complete" },
truncated: false,
unknowns: [],
});
});
});
describe("native value traversal budgets", () => {
it.each([
{
label: "node",
budgets: { max_nodes: 9 },
reasons: ["max_nodes"],
unknowns: [
{ procedure: "0x3000", address: null, reason: "Node budget reached" },
...omittedBindings("0x3000", ["0x1000", "0x2000"]),
],
nodes: 9,
},
{
label: "edge",
budgets: { max_edges: 2 },
reasons: ["max_edges"],
unknowns: [
{ procedure: "0x1000", address: null, reason: "Edge budget reached" },
...omittedBindings("0x2000", ["0x1000"]),
],
nodes: 4,
},
{
label: "node and edge",
budgets: { max_nodes: 9, max_edges: 4 },
reasons: ["max_nodes", "max_edges"],
unknowns: [
{ procedure: "0x3000", address: null, reason: "Node budget reached" },
{ procedure: "0x3000", address: null, reason: "Edge budget reached" },
...omittedBindings("0x3000", ["0x1000", "0x2000"]),
],
nodes: 9,
},
])(
"names the observed $label budget on every page, including the last",
async ({ budgets, reasons, unknowns, nodes }) => {
const { port } = valueTraceDiamond();
const request = { procedure: "0x1000", max_depth: 1, ...budgets };
const unpaged = await traced(port, request);
expect(unpaged).toMatchObject({
total_nodes: nodes,
next_offset: null,
page_truncated: false,
traversal: { status: "truncated", reasons },
truncated: true,
unknowns,
});
const last = await traced(port, {
...request,
offset: nodes - 1,
limit: 3,
});
expect(last).toMatchObject({
nodes: [{ id: unpaged.nodes.at(-1)?.id }],
next_offset: null,
page_truncated: true,
traversal: { status: "truncated", reasons },
truncated: true,
unknowns,
});
},
);
it("stops resolving call sites once the edge budget rejects a call", async () => {
const { port, resolved, analyzed } = valueTraceDiamond();
const trace = await traced(port, {
procedure: "0x1000",
max_depth: 1,
max_edges: 1,
});
expect(trace).toMatchObject({
traversal: { status: "truncated", reasons: ["max_edges"] },
unknowns: [
{ procedure: "0x1000", address: null, reason: "Edge budget reached" },
],
});
expect(trace.edges.map(({ kind }) => kind)).toEqual(["def-use"]);
expect(resolved).toEqual(["0x1004"]);
expect(analyzed).toEqual(["0x1000"]);
});
});
describe("native value traversal limits and policies", () => {
it("reports a depth limit only when no shallower path analyzes the destination", async () => {
const shared = valueTraceDiamond();
expect(
await traced(shared.port, { procedure: "0x1000", max_depth: 1 }),
).toMatchObject({ traversal: { status: "complete" }, unknowns: [] });
// 0x2000 reaches 0x3000 at the depth limit before the seed's own call does.
expect(shared.analyzed).toEqual(["0x1000", "0x2000", "0x3000"]);
expect(
await traced(valueTraceDiamond().port, {
procedure: "0x1000",
max_depth: 0,
}),
).toMatchObject({
traversal: { status: "truncated", reasons: ["max_depth"] },
unknowns: [
{
procedure: "0x1000",
address: "0x1004",
reason: "Call depth budget reached",
},
{
procedure: "0x1000",
address: "0x1008",
reason: "Call depth budget reached",
},
...omittedBindings("0x2000", ["0x1000"]),
...omittedBindings("0x3000", ["0x1000"]),
],
});
});
it("attributes a queued destination to the decompilation budget, not depth", async () => {
expect(
await traced(valueTraceDiamond().port, {
procedure: "0x1000",
max_depth: 1,
max_functions: 2,
}),
).toMatchObject({
traversal: { status: "truncated", reasons: ["max_functions"] },
unknowns: [
{
procedure: "0x3000",
address: null,
reason: "decompilation budget reached",
},
...omittedBindings("0x3000", ["0x1000", "0x2000"]),
],
});
});
it("names call-site, provider p-code and ambiguous-candidate limits", async () => {
expect(
await traced(valueTraceDiamond().port, {
procedure: "0x1000",
max_depth: 1,
max_call_sites: 1,
}),
).toMatchObject({
traversal: { status: "truncated", reasons: ["max_call_sites"] },
unknowns: [
{
procedure: "0x1000",
address: "0x1008",
reason: "Call-site resolution budget reached",
},
{
procedure: "0x2000",
address: "0x2004",
reason: "Call-site resolution budget reached",
},
],
});
const limited = staticValueGraph(
{
"0x1000": {
operations: [node("call", "0x1004", "CALL")],
pcodeTruncated: true,
},
},
{ "0x1004": [{ destination: "0x2000", status: "candidate" }] },
);
expect(await traced(limited.port, { procedure: "0x1000" })).toMatchObject({
traversal: {
status: "truncated",
reasons: ["provider_pcode_truncated", "ambiguous_call_candidates"],
},
unknowns: [
{
procedure: "0x1000",
address: null,
reason: "Provider p-code was truncated",
},
{
procedure: "0x1000",
address: "0x1004",
reason: "Ambiguous call candidates were not traversed",
},
],
});
expect(limited.analyzed).toEqual(["0x1000"]);
});
});
describe("recovered call input bindings", () => {
it("locates recovered operands without inventing formal or variadic parameters", async () => {
const trace = await traced(valueTraceUnboundArguments().port, {
procedure: "0x1000",
});
const call = trace.nodes.find(({ id }) => id === "0x1000/call")!;
expect(
trace.edges.filter(({ kind }) => kind === "argument-binding"),
).toMatchObject([
{ source: call.id, target: "0x2000/parameter:0", input_index: 1 },
]);
expect(trace.unknowns).toContainEqual({
procedure: "0x1000",
address: "0x1010",
reason: expect.stringContaining("no recovered parameter binding"),
call_inputs: {
call_node_id: call.id,
destination: "0x2000",
evidence_id: call.evidence_id,
input_indices: [2, 3],
},
});
expect(trace.traversal).toEqual({ status: "complete" });
});
it("keeps binding facts when a second alias resolves to an already analyzed callee", async () => {
const graph = staticValueGraph(
{
"0x1000": {
operations: [
node("call", "0x1010", "CALL"),
node("again", "0x1020", "CALL"),
],
},
"0x2000": {
operations: [node("return", "0x2010", "RETURN")],
parameters: [{ ordinal: 0, name: "value", data_type: "/int" }],
},
},
{
"0x1010": [{ destination: "callee", status: "direct" }],
"0x1020": [{ destination: "also_callee", status: "direct" }],
},
{ procedureAliases: { callee: "0x2000", also_callee: "0x2000" } },
);
const trace = await traced(graph.port, { procedure: "0x1000" });
expect(
trace.edges.filter(({ kind }) => kind === "argument-binding"),
).toMatchObject([
{ source: "0x1000/call", target: "0x2000/parameter:0", input_index: 1 },
{ source: "0x1000/again", target: "0x2000/parameter:0", input_index: 1 },
]);
expect(
trace.edges.filter(({ kind }) => kind === "return-binding"),
).toMatchObject([
{ source: "0x2000/return", target: "0x1000/call" },
{ source: "0x2000/return", target: "0x1000/again" },
]);
expect(trace.procedures.map(({ address }) => address)).toEqual([
"0x1000",
"0x2000",
]);
expect(graph.analyzed).toEqual(["0x1000", "callee", "also_callee"]);
expect(trace.unknowns).toEqual([]);
});
});
it.each([
{ max_depth: 0 },
{ max_functions: 1 },
{ max_nodes: 2 },
{ max_edges: 1 },
])(
"does not claim examined binding gaps after traversal refusal %j",
async (limits) => {
const trace = await traced(valueTraceUnboundArguments().port, {
procedure: "0x1000",
...limits,
});
expect(trace.traversal.status).toBe("truncated");
expect(
trace.unknowns.some(({ call_inputs }) => call_inputs !== undefined),
).toBe(false);
},
);
it.each([false, true])(
"keeps partial parameter recovery unknown (provider truncated=%s)",
async (truncated) => {
const graph = staticValueGraph(
{
"0x1000": { operations: [node("call", "0x1010", "CALL")] },
"0x2000": { operations: [], pcodeTruncated: truncated },
},
{ "0x1010": [{ destination: "0x2000", status: "direct" }] },
);
const trace = await traced(graph.port, { procedure: "0x1000" });
const located = trace.unknowns.filter(
({ call_inputs }) => call_inputs !== undefined,
);
expect(located).toHaveLength(truncated ? 0 : 1);
if (!truncated) expect(located[0]?.call_inputs?.input_indices).toEqual([1]);
},
);
it("does not invent a binding unknown for a fully examined zero-argument call", async () => {
const graph = staticValueGraph(
{
"0x1000": {
operations: [{ ...node("call", "0x1010", "CALL"), inputs: [scalar] }],
},
"0x2000": { operations: [] },
},
{ "0x1010": [{ destination: "0x2000", status: "direct" }] },
);
expect((await traced(graph.port, { procedure: "0x1000" })).unknowns).toEqual(
[],
);
});
it("retains binding-gap indices even when paging omits their call node", async () => {
const trace = await traced(valueTraceUnboundArguments().port, {
procedure: "0x1000",
offset: 1,
limit: 1,
});
expect(trace.nodes.every(({ id }) => id !== "0x1000/call")).toBe(true);
expect(trace.unknowns).toContainEqual(
expect.objectContaining({
call_inputs: expect.objectContaining({
call_node_id: "0x1000/call",
input_indices: [2, 3],
}),
}),
);
expect(trace.page_truncated).toBe(true);
expect(trace.traversal.status).toBe("complete");
});
it("binds each exact call destination once when resolution repeats it", async () => {
const graph = staticValueGraph(
{
"0x1000": {
operations: [
{
...node("call", "0x1010", "CALL"),
inputs: Array.from({ length: 1_001 }, () => scalar),
},
],
},
"0x2000": {
operations: [],
parameters: [{ ordinal: 0, name: "fixed", data_type: "/int" }],
},
},
{
"0x1010": Array.from({ length: 16 }, () => ({
destination: "0x2000",
status: "direct" as const,
})),
},
);
const trace = await traced(graph.port, { procedure: "0x1000" });
const gaps = trace.unknowns.flatMap(({ call_inputs }) =>
call_inputs === undefined ? [] : [call_inputs],
);
expect(gaps).toHaveLength(1);
expect(gaps[0]?.input_indices).toEqual(
Array.from({ length: 999 }, (_, index) => index + 2),
);
expect(
trace.edges.filter(({ kind }) => kind === "argument-binding"),
).toHaveLength(1);
expect(trace.edges.filter(({ kind }) => kind === "call")).toHaveLength(16);
expect(graph.analyzed).toEqual(["0x1000", "0x2000"]);
});
it("requires an Evidence owner for aliases of an already analyzed procedure", async () => {
const graph = staticValueGraph(
{
"0x1000": { operations: [node("call", "0x1010", "CALL")] },
"0x2000": { operations: [] },
},
{
"0x1010": [
{ destination: "callee", status: "direct" },
{ destination: "also_callee", status: "direct" },
],
},
{ procedureAliases: { callee: "0x2000", also_callee: "0x2000" } },
);
let expected: ReturnType<typeof createAnalysisExecutionEvidence> | undefined;
const result = await executeComposedWorkflow(graph.port, (owned) =>
traceNativeValuesCore(
{
execute: async (operation, parameters, options) => {
const execution = await owned.execute(operation, parameters, options);
if (
!execution.ok ||
operation !== "analyze_function" ||
parameters.procedure !== "also_callee"
)
return execution;
const { evidenceId: _evidenceId, ...unowned } = execution.value;
expected = createAnalysisExecutionEvidence(
operation,
parameters,
unowned,
"observed",
);
return ok(unowned);
},
},
{ procedure: "0x1000" },
),
);
expect(result).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCapabilityUnavailableError",
operation: "trace_native_values",
},
});
if (result.ok)
throw new Error("An unowned alias must not establish binding identity");
expect(expected).toBeDefined();
expect(result.error.partialObservation).toEqual(expected);
expect(graph.analyzed).toEqual(["0x1000", "callee", "also_callee"]);
});
const thunkDossier = (
thunkTarget: string,
operations: ReturnType<typeof node>[],
) => {
const dossier = functionDossierSchema.parse(
nativeFlowDossier(functionDossierSchema.parse(ghidraFunctionDossier()), {
address: "0xc000",
name: "thunk",
operations,
parameters: [],
}),
);
return jsonValueSchema.parse({
...dossier,
procedure: {
...dossier.procedure,
classification: {
external: false,
thunk: true,
thunk_target: thunkTarget,
provenance: "ghidra-function-manager",
},
},
});
};
const thunkPort = (dossier: ReturnType<typeof jsonValueSchema.parse>) => {
const attempted: unknown[] = [];
const port: AnalysisOperationPort = {
execute: async (operation, parameters) => {
attempted.push([operation, parameters]);
if (operation !== "analyze_function")
throw new Error(`Unexpected native trace operation: ${operation}`);
return ok(createAnalysisExecution(dossier, provider, { subject }));
},
};
return { port, attempted };
};
// Rows are matched by their structured fields; the reason is any explanation.
const externalTargetUnknown = {
procedure: "0xc000",
address: null,
related_procedure: "EXTERNAL:0x1",
reason: expect.any(String),
};
const thunkTargetRows = (unknowns: { related_procedure?: string }[]) =>
unknowns.filter(({ related_procedure }) => related_procedure !== undefined);
describe("native value trace thunk targets", () => {
it("reports an external import target that the synthetic thunk model never reaches", async () => {
const { port, attempted } = thunkPort(
thunkDossier("EXTERNAL:0x1", [node("return", "0xc000", "RETURN")]),
);
const result = await traceNativeValues(port, { procedure: "0xc000" });
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value)).toMatchObject({
decompilations: 1,
edges: [],
traversal: { status: "complete" },
unknowns: [externalTargetUnknown],
});
expect(attempted).toEqual([["analyze_function", { procedure: "0xc000" }]]);
});
it("keeps an internal thunk whose model borrows its target's operations", async () => {
const { port, attempted } = thunkPort(
thunkDossier("0x2b8", [
node("copy", "0x2b8", "COPY"),
node("return", "0x2bc", "RETURN"),
]),
);
const result = await traceNativeValues(port, { procedure: "0xc000" });
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value)).toMatchObject({
traversal: { status: "complete" },
unknowns: [],
});
expect(attempted).toEqual([["analyze_function", { procedure: "0xc000" }]]);
});
it("reports an internal thunk target whose only operation is not admitted", async () => {
const targetUnknown = {
...externalTargetUnknown,
related_procedure: "0x2b8",
};
for (const block_membership of ["detached", "unavailable"]) {
const { port } = thunkPort(
thunkDossier("0x2b8", [
{ ...node("copy", "0x2b8", "COPY"), block_membership },
node("return", "0x2bc", "RETURN"),
]),
);
const result = await traceNativeValues(port, { procedure: "0xc000" });
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value)).toMatchObject({
traversal: { status: "complete" },
unknowns: [targetUnknown],
});
}
const { port } = thunkPort(
thunkDossier("0x2b8", [
node("return", "0x2bc", "RETURN"),
node("copy", "0x2b8", "COPY"),
]),
);
const limited = await traceNativeValues(port, {
procedure: "0xc000",
max_nodes: 1,
});
if (!limited.ok) throw limited.error;
const trace = nativeValueTraceSchema.parse(limited.value);
expect(trace.traversal).toEqual({
status: "truncated",
reasons: ["max_nodes"],
});
expect(trace.unknowns).toContainEqual(targetUnknown);
});
it("keeps a thunk whose resolved call reaches its target and traverses it", async () => {
const graph = staticValueGraph(
{ "0x2000": { operations: [node("return", "0x2000", "RETURN")] } },
{ "0xc000": [{ destination: "0x2000", status: "direct" }] },
);
const thunk = thunkDossier("0x2000", [
node("call", "0xc000", "CALL"),
node("return", "0xc004", "RETURN"),
]);
const port: AnalysisOperationPort = {
execute: (operation, parameters) =>
operation === "analyze_function" && parameters.procedure === "0xc000"
? Promise.resolve(
ok(createAnalysisExecution(thunk, provider, { subject })),
)
: graph.port.execute(operation, parameters),
};
const result = await traceNativeValues(port, { procedure: "0xc000" });
if (!result.ok) throw result.error;
const parsed = nativeValueTraceSchema.parse(result.value);
expect(parsed).toMatchObject({
decompilations: 2,
traversal: { status: "complete" },
edges: expect.arrayContaining([
expect.objectContaining({
source: "0xc000/call",
target: "0x2000",
kind: "call",
status: "direct",
}),
]),
});
expect(thunkTargetRows(parsed.unknowns)).toEqual([]);
expect(graph.analyzed).toEqual(["0x2000"]);
});
});
describe("native value trace thunk target ordering and limits", () => {
it("keeps a thunk whose target another path analyzes after the thunk", async () => {
const graph = staticValueGraph(
{
"0x1000": {
operations: [
node("call-thunk", "0x1000", "CALL"),
node("call-target", "0x1004", "CALL"),
node("return", "0x1008", "RETURN"),
],
},
"0x2000": { operations: [node("return", "0x2000", "RETURN")] },
},
{
"0x1000": [{ destination: "0xc000", status: "direct" }],
"0x1004": [{ destination: "0x2000", status: "direct" }],
},
);
// The thunk's own model neither contains nor calls its target.
const thunk = thunkDossier("0x2000", [node("return", "0xc000", "RETURN")]);
const port: AnalysisOperationPort = {
execute: (operation, parameters) =>
operation === "analyze_function" && parameters.procedure === "0xc000"
? Promise.resolve(
ok(createAnalysisExecution(thunk, provider, { subject })),
)
: graph.port.execute(operation, parameters),
};
const result = await traceNativeValues(port, { procedure: "0x1000" });
if (!result.ok) throw result.error;
const parsed = nativeValueTraceSchema.parse(result.value);
expect(parsed.procedures.map(({ address }) => address)).toEqual([
"0x1000",
"0xc000",
"0x2000",
]);
expect(parsed.traversal).toEqual({ status: "complete" });
expect(thunkTargetRows(parsed.unknowns)).toEqual([]);
});
it("reports a thunk target whose call edge an exactly filled edge budget rejects", async () => {
const graph = staticValueGraph(
{},
{ "0xc008": [{ destination: "0x2000", status: "direct" }] },
);
const dossier = functionDossierSchema.parse(
thunkDossier("0x2000", [
node("copy", "0xc000", "COPY"),
node("use", "0xc004", "COPY"),
node("call", "0xc008", "CALL"),
node("return", "0xc00c", "RETURN"),
]),
);
if (!dossier.native_value_flow?.available)
throw new Error("Invalid fixture");
dossier.native_value_flow.def_use = [
{ definition: "copy", use: "use", input_index: 0 },
];
const thunk = jsonValueSchema.parse(dossier);
const port: AnalysisOperationPort = {
execute: (operation, parameters) =>
operation === "analyze_function"
? parameters.procedure === "0xc000"
? Promise.resolve(
ok(createAnalysisExecution(thunk, provider, { subject })),
)
: Promise.reject(
new Error(`Unexpected ${String(parameters.procedure)}`),
)
: graph.port.execute(operation, parameters),
};
// The def-use edge fills the budget exactly, so the call is resolved and
// then rejected: the target is neither an edge nor queued, so it stays unexamined.
const result = await traceNativeValues(port, {
procedure: "0xc000",
max_edges: 1,
});
if (!result.ok) throw result.error;
const parsed = nativeValueTraceSchema.parse(result.value);
expect(parsed).toMatchObject({
decompilations: 1,
traversal: { status: "truncated", reasons: ["max_edges"] },
});
expect(parsed.edges.map(({ kind }) => kind)).toEqual(["def-use"]);
expect(parsed.unknowns).toContainEqual({
procedure: "0xc000",
address: null,
reason: "Edge budget reached",
});
expect(parsed.unknowns).toContainEqual({
procedure: "0xc000",
address: null,
related_procedure: "0x2000",
reason: expect.any(String),
});
expect(graph.resolved).toEqual(["0xc008"]);
});
it("reports the thunk target alongside a missing def-use model", async () => {
const { port } = thunkPort(
jsonValueSchema.parse({
...functionDossierSchema.parse(thunkDossier("EXTERNAL:0x1", [])),
native_value_flow: null,
}),
);
const result = await traceNativeValues(port, { procedure: "0xc000" });
if (!result.ok) throw result.error;
expect(nativeValueTraceSchema.parse(result.value).unknowns).toEqual([
{
procedure: "0xc000",
address: null,
reason: "Native def-use was not recorded for this procedure.",
},
externalTargetUnknown,
]);
});
});