Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/adb/AdbCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
AnalysisCapturedOutput,
AnalysisError,
} from "../domain/analysisErrorBase.js";
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
import { err, ok, type Result } from "../domain/result.js";
import {
execFileBytes,
Expand Down Expand Up @@ -139,7 +140,13 @@ export const runAdbCapture = async (
"memory",
`adb output exceeded the ${String(limits.maxOutputBytes)}-byte capture budget`,
{ max_output_bytes: limits.maxOutputBytes },
{ cause },
{
cause,
...fixedAllowanceOptions(
"The adb output capture allowance",
"capture",
),
},
),
);
const stderr = failure.stderr.trim();
Expand Down
9 changes: 8 additions & 1 deletion src/adb/AdbConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "../process/ExecFileOutput.js";
import type { ProviderAvailability } from "../application/AnalysisProvider.js";
import type { ProviderRejectionCode } from "../contracts/providerSelection.js";
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { adbCommandCapturedOutput } from "./AdbCommand.js";

Expand Down Expand Up @@ -146,7 +147,13 @@ export const inspectAdbClient = async (
"memory",
`adb version output exceeded the ${String(VERSION_MAX_OUTPUT_BYTES)}-byte capture budget`,
{ max_output_bytes: VERSION_MAX_OUTPUT_BYTES },
options,
{
...options,
...fixedAllowanceOptions(
"The adb version-output capture allowance",
"capture",
),
},
);
if (failure?.code === "ETIMEDOUT")
throw new AnalysisTimeoutError(
Expand Down
5 changes: 5 additions & 0 deletions src/apktool/ApktoolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
AnalysisUnsupportedTargetError,
} from "../domain/analysisErrorCore.js";
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
import { err, ok, type Result } from "../domain/result.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
Expand Down Expand Up @@ -194,6 +195,10 @@ const apktoolCommandFailure = (
{
cause,
...(capturedOutput === undefined ? {} : { capturedOutput }),
...fixedAllowanceOptions(
"The apktool output capture allowance",
"capture",
),
},
);
// A physical launcher/capture failure does not establish target refusal.
Expand Down
71 changes: 70 additions & 1 deletion src/application/wasm/WasmInterfaceComparisonService.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { expect, it } from "vitest";
import { createEvidence } from "../../domain/evidence.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import { wasmInterfaceComparisonSchema } from "../../contracts/wasm/wasmComparisonContracts.js";
import { compareWasmInterfaces } from "./WasmInterfaceComparisonService.js";
import type { WasmInterface } from "../../domain/wasm/wasmInterface.js";
import {
WASM_INTERFACE_REPRESENTATION_BYTES,
wasmInterfaceEntryRepresentationBytes,
type WasmInterface,
} from "../../domain/wasm/wasmInterface.js";
import { wasmArtifactFixture } from "../../../tests/fixtures/wasm/artifact.js";

const facts = (
Expand Down Expand Up @@ -179,6 +184,70 @@ it("resolves exact retained references and permits cancellation between comparis
});
});

/** One inventory under the comparison cap; two of them exceed it together. */
const admittedInventory = (label: string): WasmInterface => {
const base = facts(label).entries[0]!;
const minimum = Math.floor(WASM_INTERFACE_REPRESENTATION_BYTES / 2) + 4096;
let low = 0;
let high = 4_000_000;
let best = 0;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const name = `${label}-${"n".repeat(mid)}`;
const bytes = wasmInterfaceEntryRepresentationBytes({
...base,
name,
wat_form: `(export "${name}" (func 0))`,
});
if (bytes >= minimum) {
best = mid;
high = mid - 1;
} else low = mid + 1;
}
const name = `${label}-${"n".repeat(best)}`;
const entry = {
...base,
name,
wat_form: `(export "${name}" (func 0))`,
};
const bytes = wasmInterfaceEntryRepresentationBytes(entry);
if (
bytes >= WASM_INTERFACE_REPRESENTATION_BYTES ||
bytes + 512 >= WASM_INTERFACE_REPRESENTATION_BYTES
)
throw new Error("Sized inventory is not individually admitted");
return { state: "complete", issues: [], entries: [entry] };
};

it("refuses two admitted inventories that together exceed the comparison cap", async () => {
const left = admittedInventory("left");
const right = admittedInventory("right");
const empty: WasmInterface = { state: "complete", issues: [], entries: [] };
const alone = await compareWasmInterfaces({
left: evidence(left),
right: evidence(empty),
scope: "both",
});
expect(alone.ok).toBe(true);
const combined = await compareWasmInterfaces({
left: evidence(left),
right: evidence(right),
scope: "both",
});
expect(combined.ok).toBe(false);
if (combined.ok) throw new Error("Expected comparison allowance refusal");
const projected = projectAnalysisError(combined.error);
expect(projected.retryable).toBe(false);
expect(projected.code).toBe("resource_constraint");
expect(combined.error).toMatchObject({ resource: "capacity" });
expect(projected.remediation.action).toContain(
"The WASM interface comparison inventory",
);
expect(projected.remediation.action).not.toMatch(
/Wait for an active analysis/u,
);
});

it.each([false, true])(
"keeps a one-sided unsupported interface unknown with reverse=%s",
async (reverse) => {
Expand Down
5 changes: 5 additions & 0 deletions src/application/wasm/WasmInterfaceComparisonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
AnalysisInputError,
AnalysisResourceConstraintError,
} from "../../domain/analysisErrorCore.js";
import { fixedAllowanceOptions } from "../../domain/fixedAllowanceRemediation.js";
import { AnalysisError } from "../../domain/analysisErrorBase.js";
import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js";
import { err, ok, type Result } from "../../domain/result.js";
Expand Down Expand Up @@ -133,6 +134,10 @@ export const compareWasmInterfaces = async (
"capacity",
"WASM comparison exceeds typed interface representation capacity",
{ maximum_bytes: WASM_INTERFACE_REPRESENTATION_BYTES },
fixedAllowanceOptions(
"The WASM interface comparison inventory",
"count",
),
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
);
if (++work % 256 === 0) {
await setImmediate();
Expand Down
13 changes: 12 additions & 1 deletion src/captures/TsharkCaptureProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
AnalysisResourceConstraintError,
} from "../domain/analysisErrorCore.js";
import { createEvidence } from "../domain/evidence.js";
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import {
type InspectPacketCaptureInput,
Expand Down Expand Up @@ -480,6 +481,10 @@ export class TsharkCaptureProvider implements PacketCapturePort {
...(observedOutput === undefined
? {}
: { capturedOutput: observedOutput }),
...fixedAllowanceOptions(
"The packet-field discovery record allowance",
"capture",
),
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
},
);
const reason = cause instanceof Error ? cause.message : String(cause);
Expand Down Expand Up @@ -561,7 +566,13 @@ export class TsharkCaptureProvider implements PacketCapturePort {
"file-size",
reason,
{ input_bytes: LIMITS.input_bytes },
{ cause },
{
cause,
...fixedAllowanceOptions(
"the fixed packet-capture input allowance",
"input",
),
},
);
if (cause instanceof ArtifactReaderFailure && cause.reason === "integrity")
return new AnalysisArtifactChangedError(operation, path, reason, {
Expand Down
5 changes: 5 additions & 0 deletions src/domain/analysisView/WebNetworkProjectionAccumulator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from "zod";
import { fixedAllowanceOptions } from "../fixedAllowanceRemediation.js";
import {
AnalysisResourceConstraintError,
AnalysisUnsupportedTargetError,
Expand Down Expand Up @@ -58,6 +59,10 @@ export abstract class WebNetworkProjectionAccumulator {
"memory",
`${this.budgetSubject} retained state and temporary work exceed the projection representation budget.`,
{ output_bytes: this.maximumBytes },
fixedAllowanceOptions(
"The analysis-view projection allowance",
"parser",
),
),
);
}
Expand Down
51 changes: 51 additions & 0 deletions src/domain/fixedAllowanceRemediation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from "vitest";
import { AnalysisResourceConstraintError } from "./analysisErrorCore.js";
import { projectAnalysisError } from "./analysisErrorProjection.js";
import {
fixedAllowanceOptions,
fixedAllowanceRemediation,
type FixedAllowanceKind,
} from "./fixedAllowanceRemediation.js";

const forbidden = {
parser: [
/available host memory\. Retry/u,
/worker memory limits and available/u,
],
count: [/Wait for an active analysis/u, /\bretry\b/iu],
report: [/get_evidence_bundle/u, /complete CLI JSON output/u],
input: [/evidence reply/u, /file-size allowance for the evidence/u],
capture: [/available host memory\. Retry/u],
work: [/worker CPU limits and observed signal/u],
} as const;

describe("fixed allowance recovery", () => {
it.each([
["parser", "memory"],
["count", "capacity"],
["report", "transport"],
["input", "file-size"],
["capture", "memory"],
["work", "cpu"],
] as const)(
"projects %s allowances without the generic %s fallback",
(kind: FixedAllowanceKind, resource) => {
const allowance = `The ${kind} allowance`;
const error = new AnalysisResourceConstraintError(
"inspect_fixture",
resource,
"allowance exceeded",
{ maximum: 1 },
fixedAllowanceOptions(allowance, kind),
);
const projected = projectAnalysisError(error);
expect(projected.retryable).toBe(false);
expect(projected.remediation.action).toBe(
fixedAllowanceRemediation(allowance, kind),
);
expect(projected.remediation.action).toContain(allowance);
for (const pattern of forbidden[kind])
expect(projected.remediation.action).not.toMatch(pattern);
},
);
});
43 changes: 43 additions & 0 deletions src/domain/fixedAllowanceRemediation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Recovery for a deterministic allowance. Host memory, worker limits, waiting,
* and a different output channel do not raise it.
*/
export type FixedAllowanceKind =
| "parser"
| "count"
| "report"
| "input"
| "capture"
| "work";

const unchangedRequest = "Repeating the unchanged request fails the same way.";

/** Name the fixed allowance and the recovery that cannot admit the same request. */
export const fixedAllowanceRemediation = (
allowance: string,
kind: FixedAllowanceKind,
): string => {
switch (kind) {
case "parser":
return `${allowance} is a fixed allowance. Worker memory limits, inherited limits, and additional host memory do not raise it. Select another complete input within the allowance when that preserves the needed observation. ${unchangedRequest}`;
case "count":
return `${allowance} is a fixed count. Waiting for an active analysis to finish does not raise it. ${unchangedRequest}`;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
case "report":
return `${allowance} is a fixed report allowance. This refusal does not leave a completed report or Evidence record to retrieve, and complete CLI JSON does not raise it. ${unchangedRequest}`;
case "input":
return `Select a suitable complete artifact within ${allowance}. Increasing an evidence-reply file-size allowance does not admit this input. Do not truncate required bytes. ${unchangedRequest}`;
case "capture":
return `${allowance} is a fixed capture allowance. Worker memory limits, inherited limits, and additional host memory do not raise it. ${unchangedRequest}`;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
case "work":
return `${allowance} is a fixed work allowance. Worker CPU limits and additional CPU time do not raise it. Select another complete artifact within the allowance. ${unchangedRequest}`;
}
};

/** Non-retryable options for a fixed allowance. Occupied capacity stays retryable. */
export const fixedAllowanceOptions = (
allowance: string,
kind: FixedAllowanceKind,
): { readonly retryable: false; readonly remediationAction: string } => ({
retryable: false,
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
remediationAction: fixedAllowanceRemediation(allowance, kind),
});
2 changes: 2 additions & 0 deletions src/dotnet/ManagedSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AnalysisResourceConstraintError,
} from "../domain/analysisErrorCore.js";
import { EvidenceIntegrityError } from "../domain/evidenceErrors.js";
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { RegularFileChangedError } from "../filesystem/RegularFile.js";
import { readBoundedFileBytes } from "../process/BoundedFileBytes.js";
Expand All @@ -42,6 +43,7 @@ const managedSnapshotCapacityError = (
"memory",
"Managed static inspection requires an eagerly retained PE snapshot within its input byte budget.",
{ max_snapshot_bytes: MAX_MANAGED_SNAPSHOT_BYTES },
fixedAllowanceOptions("The managed PE snapshot allowance", "parser"),
);

export const readManagedSnapshot = async (
Expand Down
11 changes: 10 additions & 1 deletion src/electron/ElectronFuseFailures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AnalysisResourceConstraintError,
AnalysisUnsupportedTargetError,
} from "../domain/analysisErrorCore.js";
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { ZodError } from "zod";
import {
Expand Down Expand Up @@ -93,7 +94,15 @@ export const electronFuseFailure = (
cause.boundary === "file-size"
? { maximum_input_bytes: 1024 * 1024 * 1024 }
: { maximum_sentinels_per_slice: 32 },
{ cause },
{
cause,
...fixedAllowanceOptions(
cause.boundary === "file-size"
? "the 1 GiB Electron fuse input allowance"
: "The Electron fuse sentinel inventory",
cause.boundary === "file-size" ? "input" : "count",
),
},
);
if (cause instanceof ElectronFuseFormatFailure)
return cause.kind === "unsupported"
Expand Down
11 changes: 11 additions & 0 deletions src/electron/ElectronFuseProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,15 @@ it("enforces the 32 sentinel boundary with typed and public limit details", asyn
expect(result.error._tag).toBe("AnalysisResourceConstraintError");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "resource_constraint",
retryable: false,
details: { reported_limits: { maximum_sentinels_per_slice: 32 } },
remediation: {
action: expect.stringContaining("sentinel inventory"),
},
});
expect(
projectAnalysisError(result.error).remediation.action,
).not.toMatch(/Wait for an active analysis/u);
}
}
}
Expand All @@ -593,7 +600,11 @@ it("rejects a sparse artifact over 1 GiB before scanning", async () => {
expect(result.error._tag).toBe("AnalysisResourceConstraintError");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "resource_constraint",
retryable: false,
details: { reported_limits: { maximum_input_bytes: 1024 * 1024 * 1024 } },
remediation: {
action: expect.stringContaining("does not admit this input"),
},
});
}
});
Expand Down
Loading
Loading