Files
reverse-engineering-anything/tests/boundary/filesystem/flutterBuildProvider.test.ts
T
38bf511a50 fix(errors)!: report unsupported and unrecognized targets on the TargetFailure model (#14281)
* feat(errors)!: add the TargetFailure model, its projection table and conformance registry

Introduce one domain-owned TargetFailure value whose kind alone derives
the public code, category, retryability, recovery and details, for CLI
and MCP alike. Selected-file malformed and truncated refusals are the
first area on it: AnalysisMalformedTargetError is replaced by
AnalysisTargetFailureError, and ParseFailure's byte-structure variants
are TargetFailure variants rather than a parallel type.

The shared stableArtifactParserFailureCli loop is replaced by a
declarative registry (tests/support/targetFailure/readers/*.ts) and one
generated suite that runs every registered case through the CLI and MCP.

BREAKING CHANGE: selected-file malformed and truncated refusals add
details.failure_kind, and a truncated target's remediation now says to
re-acquire the complete file instead of choosing another target. Codes
are unchanged. Affected operations: every operation that refuses a
selected file's malformed or truncated bytes, including the ELF, PE,
Mach-O, DWARF, Go, Rust, BEAM, pyc, devicetree, Apple IPS, kernel
module, binary hardening, managed PE/CLI and IL2CPP readers.

* test(managed): name the target failure error in the merged status test

* docs(reference): name the error CSV instead of linking a non-page file

* fix(tests): expect the path reason for non-regular dyld inputs

#14225 refuses a non-regular dyld input as ArtifactReaderFailure("path");
dylibFileLifetime still expected "format" and fails on main's shard 9.

* fix(tests): expect io, invariant and path where #14225 reclassified

stableArtifact expected "format" for an impossible descriptor byte count
(now io) and an invalid chunk size (now InternalInvariantError);
directoryArtifactReaderOpen expected "format" for a non-file entry (now
path, the same change as #14238). All three fail on main.

* fix(errors)!: raise container reader byte faults as malformed or truncated targets

Delete the ambiguous `format` reason from ArtifactReaderFailure and
ArtifactOperationError. Each container reader now raises its owner's
fact once, through malformedArtifact / truncatedArtifact, with format,
offset, field and measured extents. Short reads inside a measured extent
are read-coverage integrity failures, path-kind refusals are `path`,
foreign or recovery-requiring inputs are `unsupported`, host tool output
is `io`, and REA defects are InternalInvariantError.

The trace, NTFS journal, release exposure, web capture, native macOS and
web artifact adapters project the reader's parsed facts as
AnalysisTargetFailureError instead of invalid_request. Inventory
malformed, truncated, unsupported and cancelled reasons take their code
from the TargetFailure projection table, so a truncated container is
unsupported_target with failure_kind "truncated" on both paths.

BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact,
analyze_javascript_application, inspect_desktop_application,
inspect_keyed_archive, inspect_asset_catalog, inspect_nuget_nupkg,
trace_dylib_resolution, trace_web_module_imports and
trace_web_source_location report damaged container bytes as
unsupported_target (details.reason "malformed" or "truncated", with
failure_kind and format) instead of artifact_operation_failed with
reason "format", and a truncated container as unsupported_target instead
of truncated. inspect_playwright_trace, inspect_ntfs_journal,
audit_release_exposure, the web capture inspection operations and the
caller-selected import and source maps report damaged selected bytes as
unsupported_target instead of invalid_request. The `format` reason no
longer appears in details.reason.

* test(artifacts): expect target failure codes in compiled CLI and MCP suites

* fix(errors)!: refuse malformed SQLite, pwntools and EVM inputs as target failures

The SQLite engine's `format` reason is renamed to `malformed` at its
owner (SQLITE_INSPECTION_REASONS), and its refusal becomes an
AnalysisTargetFailureError at the selected path with format "sqlite".
The pwntools and EVM worker replies keep their wire reason "format",
which their workers define, and the adapters translate it once into a
malformed target failure with the profile's format (elf, elf-core,
evm-bytecode) instead of an invalid_format argument issue. SQLite joins
the target-failure conformance registry.

BREAKING CHANGE: inspect_sqlite_database, inspect_binary_layout,
inspect_recorded_crash and inspect_evm_interface report a selected
file's malformed bytes as unsupported_target with details.failure_kind
"malformed", format_failure, format and path, instead of invalid_request
with an invalid_format issue on path.

* fix(errors): give the integrity kind a schema-valid code and list the reference page

artifact_integrity_mismatch's projection schema requires declared and
calculated digests, which a TargetFailure of kind integrity does not
carry, so the kind now projects artifact_operation_failed with category
integrity_mismatch. The model test validates every kind's projection
against analysisErrorProjectionSchema. The reference page joins the
documentation sidebar.

* fix(artifacts): report a short carrier read as a changed file and keep unsupported desktop probes local

A Bun or Node SEA short read inside a range already checked against the
measured size meant the file changed, not that it was truncated. The
carrier range reader and Bun's readExact now raise read-coverage
integrity for it, and keep 'outside the file' as the truncated case.
DesktopFileInspection records an unsupported managed-metadata probe as a
row issue again, so later file checks still run.

* test(errors): expect target failure facts in the pwntools and EVM reply suites

The literal-replacement scenario now reads the failure's detail instead
of an issues[] message, and a malformed reply wrapped by cleanup failure
reports category unsupported_target.

* test(errors): register PE DOS-signature and truncated IPS refusals and drop their per-command copies

The non-MZ PE imports and load-config cases (PE32/PE32+, ZZ and high-bit
signatures) and the truncated Apple IPS body move into the conformance
registry, which runs them through the CLI and MCP and now also checks
that a refusal leaves the selected file unchanged. The per-command tests
keep only their valid-image controls.

* test(sqlite): drop the malformed-database provider test the conformance registry covers

inspect_sqlite_database/not-a-database in the target-failure registry runs
the same non-SQLite bytes through the CLI and MCP, asserts the malformed
target facts and checks the selected file is unchanged.

* feat(errors)!: make failure_kind the identifier and derive code and retry from it

Apply the owner review on #14239 to the TargetFailure model:
- remove details.format_failure; failure_kind is the only identifier;
- give integrity, conflict and io their own codes
  (target_integrity_mismatch, input_conflict, host_io_failed) and derive
  inventory malformed, truncated and unsupported codes from the same table;
- add a retry hint (none, backoff, after_change) derived from the kind,
  with io refined by its transient errno fact; retryable stays the
  unchanged-retry flag and is true exactly for backoff;
- rename the limit-cut result code and category truncated to partial;
- drop cancelled and resource from the target kinds and spell
  identifiers in American English (unrecognized);
- return a target failure a tool handler throws as a projected isError
  result instead of letting the SDK flatten it to text.

BREAKING CHANGE: details.format_failure is removed (read
details.failure_kind). The truncated result code and category are now
partial. Target integrity failures report target_integrity_mismatch,
host read failures host_io_failed and disagreeing inputs input_conflict.
Inventory operations (inventory_artifact, inspect_artifact,
extract_artifact and the other inventory callers) report truncated
container bytes as unsupported_target. Every target failure carries a
top-level retry hint.

* test(sqlite): expect a malformed target for non-database bytes in the CLI suite

* fix(artifacts): keep changed-file short reads, invalid offsets and EOF declarations apart

PyInstaller reads inside the measured size, and .NET bundle manifest
refills, now raise read-coverage integrity on a zero-byte read instead of
truncated. A .NET bundle field and a Godot PCK directory entry that extend
past the file are truncated (with offset and extents) instead of
malformed. An absent or invalid ASAR packed offset is malformed; only a
valid range past the archive is truncated. The .NET manifest string
offset is reported in file coordinates.

* fix(pwntools): report a short ELF header as a truncated target

The layout and recorded-crash workers raise a distinct truncated reason
when the file ends inside the ELF identification or header, and the
decoder projects it as failure_kind truncated instead of inferring it
from the message.

* fix(errors)!: report managed coverage gaps and IL2CPP pair conflicts as target failures

Managed readers interpret an issue code once, in managedIssueTargetFailure:
a coverage gap is an unsupported target, every other rule violation is a
malformed target, and the inventory metadata status is derived from that
same fact. managedInspectionTargetFailure now returns only
AnalysisTargetFailureError.

An IL2CPP binary that parses but does not link to its metadata is a
conflict target failure that lists both inputs and blames neither, in
place of an invalid_request with two issue entries. Metadata version 108
is an unsupported target. The CLI spells each conflict input's field by
the command's option name.

BREAKING CHANGE: inspect_managed_artifact, inspect_managed_members,
inspect_managed_native_boundaries and the managed comparison operations
report unsupported metadata or signature forms as an
AnalysisTargetFailureError (failure_kind unsupported, format pe-cli,
field, offset) instead of a bare unsupported target. inspect_il2cpp_metadata
reports a binary/metadata pair that does not link as code input_conflict
with failure_kind conflict and details.inputs, instead of invalid_request
with details.issues, and metadata version 108 with failure_kind
unsupported.

* fix(artifacts): verify the carrier before blaming a short read on the bytes

The NTFS $MFT decoder, positional members (FIT, uImage, Bun, Deno) and
.NET bundle members now re-verify the source before reporting a short
stream as malformed, so a file that changed during the read reports the
change. The truncation site builds the model's measured extent.

* fix(errors)!: report inventory refusals as target failures of the selected path

An inventory, inspection or extraction refused damaged or unsupported
container bytes as an ArtifactOperationError whose reason repeated the
format owner's kind. It carried no path or retry hint, so the conformance
registry could not express container cases.

translateArtifactFailure now takes the selected path and returns the
reader's TargetFailure as an AnalysisTargetFailureError. The malformed,
truncated and unsupported reasons, their classification rows and their
presentation branch are deleted from ArtifactOperationError. Readers'
unsupported refusals carry their format facts through unsupportedArtifact.

Container cases move into the conformance registry, which now opens the
target first for session tools such as inspect_artifact.

BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact and
the bundle readers report a refused container as a target failure of
details.path with a top-level retry. details.reason is the owner's
description and details.detail is removed.

* docs(il2cpp): describe pair conflicts as input_conflict with both inputs

* docs(android): name unsupported_target for truncated boot image structures

* fix(errors)!: report a target that changed during the read as failure_kind changed

Inventory integrity failures whose check means the target moved under the
read (container, member and unpacked-entry identity, read coverage,
traversal coverage) now take the model's changed projection: code
artifact_changed, retry backoff and details.failure_kind changed. A
selected target that no longer matches the one the session admitted gets
its own admitted-identity check and keeps its reopen recovery.

BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact and
the other inventory operations report a target that changed during the
read as artifact_changed (retryable, failure_kind changed) instead of
artifact_operation_failed. Admission-identity refusals report
integrity_check admitted-identity instead of container-identity.

* test(artifacts): raise the cleanup fixture's decode refusal with its format facts

* fix(errors)!: report unsupported and unrecognized targets on the TargetFailure model

AnalysisUnsupportedTargetError carried a free-text reason and remediation
beside the TargetFailure model, so unsupported refusals had no format,
failure_kind or retry hint, and their recovery was prose written at each
producer.

The class is deleted. Producers raise unsupportedTarget or
unrecognizedTarget with the owner's format, and consumers read
targetFailureOfKind. The projection table renders the recovery and names
the operations the owner lists in related.operations. The foreign-bytes
identity loop moves into the conformance registry.

BREAKING CHANGE: unsupported and unrecognized targets report
failure_kind, format and retry. Their remediation comes from the
target-failure table; producer-written remediation sentences are folded
into details.reason or replaced by details.related_operations.

* test(artifacts): expect admitted-identity for selections replaced before admission

A selected source replaced before admission fails the admitted-identity check rather than container-identity since the changed/admitted split.

* test(go): name the fixture and result when an MZ carrier is not refused through MCP

* style: format the migrated target-failure tests

* test(errors): expect the PE readers' native-binary format label for foreign bytes

* test(verify): expect target failures for damaged ELF, core and EVM inputs in real-provider verifiers

The binary-layout, recorded-crash and EVM verifiers still expected invalid_input for malformed or truncated selected bytes. The pwntools and EVM adapters now report those as unsupported_target with failure_kind; a missing path remains invalid_input.

* test(web): expect the partial code for a refused source-map admission

The limit-cut code is partial; this Windows-lane test arrived from main with the old truncated spelling.

* fix(artifacts): keep refusal facts on DMG verdicts and the selected bundle path

hdiutil's "image not recognized" verdict raised the unsupported reason
without the owner's facts, so the inventory translation treated it as a
reader defect. It now carries an unsupported dmg target failure, and the
reader-failure constructor requires facts for every target reason.

A bundle resource refusal names the caller's selected bundle path rather
than its resolved target.

The real-provider verifiers and the Android, source-location acceptance
tests expect the model's target-failure categories and codes.

* test(errors): assert unsupported refusals' kind and format, and align verifiers and the Electron guide

* test(errors): check the MCP target failure as the text clients receive

The stdio transport removes the private structured carrier from every isError result, so the conformance suite asserted a field that never reaches clients and failed every case. The projected diagnostic is checked from the JSON text instead.

* test(errors): raise main's new unsupported-target fixtures through the model

* test(artifacts): expect artifact_changed when selected bytes change after inventory

Bundle resources replaced after open and unselected JavaScript sources rewritten after inventory now fail a change check, so they report artifact_changed with retry backoff rather than artifact_operation_failed.

* fix(errors): report a session digest mismatch as admitted-identity

The digest the session admitted does not change when the same request is
repeated, so targetDigestChangedFailure was inviting a backoff retry that
fails the same way. It now names admitted-identity, which stays
artifact_operation_failed and not retryable, and tells the caller to reopen.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(artifacts): classify unused PyInstaller compressed input as layout

zlib stops at the first complete stream, so a declared span that arrives
intact but is not fully consumed is trailing junk or a second stream, not
a file that changed. A source that ends before that span stays read-coverage.
The cached-carrier test replaces the file with a different size so a shared
timestamp cannot hide the rewrite.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(applications): expect unsupported_target for a damaged trace stream

A trace member whose CRC fails is a malformed zip target. The partial
Evidence assertion now checks that code, failure_kind and format.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(artifacts): assert Android refusal kinds and drop the SEA registry case

details.reason is the owner's description, so the boot-image refusals
assert failure_kind and format. A malformed SEA blob inside an ELF stays
an outer-format inventory with a recognition limitation, which the Node
SEA reader tests already cover, so it is not an inspect_artifact refusal.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(artifacts): pass the selected path into translated target failures

translateArtifactFailure names the caller-selected path on a format
refusal. Directory inspection and a retained artifact scope both know
that path and now supply it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(artifacts): charge global pax records against the tar metadata budget

Repeated pax global headers were copied into one map without a charge, so
each header could stay inside the extended-header allowance while the map
grew without a bound. The retained metadata budget is charged for the
merged map's keys, values and entries before that map is allocated.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(dylib): expect admitted-identity when the opened target is replaced

The session keeps the digest it admitted, so replacing the executable and
repeating trace_dylib_resolution is not a retryable container change.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* style: format the inventory path translation and conformance suite

* fix(artifacts): keep the unsupported-target migration in files the inventory merge replaced

* test(errors): expect target failures for ZIP inventory refusals and input_conflict for an unlinked IL2CPP pair

* test(artifacts): expect a malformed target failure for an ISO 9660 directory cycle

* test(web): expect a malformed target failure on the captured source path

* test(artifacts): expect a malformed ZIP target failure for a corrupt Zstandard member

* test(javascript): read the owner's input facts from details.reason and recovery from the projection

* test(iso9660): assert the malformed target failure fields

details.reason is the owner's description. The compiled CLI refusal is
identified by failure_kind and format.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(errors): assert selection and unsupported view fields

A desktop root that is not a directory is a selection failure, so the
CLI reports invalid_request. An analysis view the parent cannot supply
is unsupported evidence; the tests read failure_kind, format, field and
related_operations.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(errors): keep binary plist UID facts on unsupported targets

Pass the reader's UnsupportedBinaryPlistUidError failure through instead of
rebuilding a bplist refusal that drops field and undecodedContent.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(errors): keep oversized binary plist UID facts on keyed archives

Assert the reader projects format, field, and undecoded content from the
parser failure instead of a rebuilt bplist refusal.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-24 23:25:08 -05:00

2318 lines
82 KiB
TypeScript

import { projectCliAnalysisResult } from "../../../src/cli/projectCliAnalysisResult.js";
import { createReadStream } from "node:fs";
import {
open,
readFile,
rmdir,
stat,
utimes,
writeFile,
} from "node:fs/promises";
import { createServer } from "node:net";
import { join, sep } from "node:path";
import { Readable } from "node:stream";
import { Uint8ArrayReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
import { expect, it, onTestFinished, vi } from "vitest";
import {
aotFixtureImage,
appendElfRanges,
engineFixtureImage,
buildElf64Image,
REFERENCE_HASH,
snapshotSection,
unifiedAotFixtureImage,
} from "../../../src/flutter/FlutterBuild.fixture.js";
import { FlutterBuildProvider } from "../../../src/flutter/FlutterBuildProvider.js";
import { createFlutterBuildAnalysisService } from "../../../src/application/ProviderOperationServices.js";
import { finishAnalysisCommand } from "../../../src/cli/finishAnalysisCommand.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
import {
AnalysisCancelledError,
AnalysisResourceConstraintError,
} from "../../../src/domain/analysisErrorCore.js";
import {
flutterResultSchemas,
type FlutterOperation,
} from "../../../src/domain/flutter/flutterBuildAnalysis.js";
import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js";
import {
createFlutterApk as buildApk,
createDuplicateFlutterApk,
} from "../../fixtures/flutterApk.js";
import * as regularFile from "../../../src/filesystem/RegularFile.js";
import * as fsPromises from "node:fs/promises";
import * as dartElf from "../../../src/flutter/DartElf.js";
import {
interceptFlutterRootRemoval,
realRm,
} from "../../fixtures/flutterCleanup.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { analysisRefusalRemediation } from "../../../src/domain/analysisRefusal.js";
import { ZIP_ZSTANDARD_WINDOW_CEILING } from "../../../src/resources/ZipArtifactReader.js";
import {
zstandardWindowFrameFixture,
zstandardZipFixture,
} from "../../fixtures/zipZstandard.js";
import { ZipArtifactReader } from "../../../src/resources/index.js";
vi.mock("node:fs", async (importOriginal) => {
const original = await importOriginal<typeof import("node:fs")>();
return { ...original, createReadStream: vi.fn(original.createReadStream) };
});
vi.mock("node:fs/promises", async (importOriginal) => {
const original = await importOriginal<typeof import("node:fs/promises")>();
return { ...original };
});
const engineBuildId = "e8d907ef7c175ec4313f47111019adc9588c5f61";
const buildIdNote = (): Buffer =>
Buffer.concat([
Buffer.from([4, 0, 0, 0, 20, 0, 0, 0, 3, 0, 0, 0, 0x47, 0x4e, 0x55, 0]),
Buffer.from([
0xe8, 0xd9, 0x07, 0xef, 0x7c, 0x17, 0x5e, 0xc4, 0x31, 0x3f, 0x47, 0x11,
0x10, 0x19, 0xad, 0xc9, 0x58, 0x8c, 0x5f, 0x61,
]),
]);
const TOOLCHAIN_LINE =
"Android (5900059 based on r365631c) clang version 9.0.8 (based on LLVM 9.0.8svn)";
const libapp = aotFixtureImage;
const libflutter = (): Buffer =>
engineFixtureImage({ notes: buildIdNote(), labels: [TOOLCHAIN_LINE] });
/**
* Self-contained Dart AOT image: a synthetic ELF64 carrying the four
* snapshot symbols, two snapshot sections, and a string pool, so goldens
* do not depend on any file outside the repository.
*/
const aotImage = aotFixtureImage;
/** Two data symbols at one offset and size: aliased exports. */
const aliasedDataImage = (): Buffer => {
const elf = buildElf64Image([
{ name: "_kDartVmSnapshotData", value: 0x1000, size: 52 },
{ name: "_kDartIsolateSnapshotData", value: 0x1000, size: 52 },
]);
return Buffer.concat([
elf,
Buffer.alloc(0x1000 - elf.length),
snapshotSection(REFERENCE_HASH),
]);
};
/** Two data symbols whose snapshot hashes disagree. */
const conflictingHashImage = (): Buffer => {
const elf = buildElf64Image([
{ name: "_kDartVmSnapshotData", value: 0x1000, size: 52 },
{ name: "_kDartIsolateSnapshotData", value: 0x1200, size: 52 },
]);
const vm = snapshotSection("11111111111111111111111111111111");
const isolate = snapshotSection("22222222222222222222222222222222");
return Buffer.concat([
elf,
Buffer.alloc(0x1000 - elf.length),
vm,
Buffer.alloc(0x1200 - 0x1000 - vm.length),
isolate,
]);
};
const analyzeImage = async (
bytes: Buffer,
operation: FlutterOperation,
engine = libflutter(),
) => {
const path = await buildApk("image.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes },
{ path: "lib/arm64-v8a/libflutter.so", bytes: engine },
]);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (!result.ok) throw result.error;
return result.value;
};
it("keeps byte-scanned build identification partial for a unified payload", async () => {
const identified = await analyzeImage(
unifiedAotFixtureImage(),
"identify_flutter_build",
);
const value = flutterResultSchemas.identify_flutter_build.parse(
identified.result,
);
expect(value.coverage).toBe("partial");
expect(value.abis[0]?.libapp).toMatchObject({
snapshot_hash: REFERENCE_HASH,
snapshot_hash_sources: 1,
});
expect(identified.limitations.join(" ")).toContain(
"supported legacy split VM/isolate profile",
);
expect(identified.limitations.join(" ")).not.toContain("truncated");
});
it.each([
{ includeText: false, kind: 2, expectedProfile: "unified" },
{ includeText: true, kind: 3, expectedProfile: null },
] as const)(
"retains partial unified observations (text=$includeText, kind=$kind)",
async ({ includeText, kind, expectedProfile }) => {
const result = await analyzeImage(
unifiedAotFixtureImage({ includeText, kind }),
"inspect_dart_aot",
);
const report = flutterResultSchemas.inspect_dart_aot.parse(result.result);
expect(report.libapp).toMatchObject({
coverage: "partial",
snapshot_profile: expectedProfile,
snapshot_hash_sources: 1,
sections: expect.arrayContaining([
expect.objectContaining({ name: "_kDartSnapshotData", kind }),
]),
});
expect(report.string_pool.package_uris).toEqual([
"package:app/unified.dart",
]);
},
);
it("keeps a lone unified text marker from overriding a complete split observation", async () => {
const image = aotFixtureImage(REFERENCE_HASH, [
{ name: "_kDartSnapshotText", value: 0x2600, size: 16 },
]);
const result = await analyzeImage(image, "inspect_dart_aot");
const report = flutterResultSchemas.inspect_dart_aot.parse(result.result);
expect(report.libapp).toMatchObject({
snapshot_profile: "legacy-split",
coverage: "partial",
snapshot_hash_sources: 2,
});
expect(report.libapp.sections).toHaveLength(5);
expect(report.string_pool.package_uri_count_at_least).toBe(2);
});
it.each(["conflicting", "truncated", "capacity"] as const)(
"preserves %s engine-note uncertainty",
async (shape) => {
const second = Buffer.from(buildIdNote());
second.fill(0x11, 16);
const notes =
shape === "conflicting"
? Buffer.concat([buildIdNote(), second])
: shape === "truncated"
? Buffer.concat([buildIdNote(), Buffer.from([1])])
: Buffer.concat(
Array.from({ length: 65 }, (_, index) => {
const note = Buffer.from(buildIdNote());
note.writeUInt32LE(index, 16);
return note;
}),
);
const result = await analyzeImage(
aotImage(),
"identify_flutter_build",
engineFixtureImage({ notes }),
);
const value = flutterResultSchemas.identify_flutter_build.parse(
result.result,
);
expect(value.coverage).toBe("partial");
expect(value.abis[0]!.libflutter.build_id).toBeNull();
const candidates = value.abis[0]!.libflutter.build_id_candidates;
if (shape === "capacity") {
expect(candidates).toHaveLength(64);
expect([candidates[0], candidates.at(-1)]).toEqual([
"000000007c175ec4313f47111019adc9588c5f61",
"3f0000007c175ec4313f47111019adc9588c5f61",
]);
expect(result.limitations.join(" ")).toContain(
"64-value evidence budget",
);
} else {
expect(candidates).toEqual(
shape === "conflicting"
? [engineBuildId, "11".repeat(20)]
: [engineBuildId],
);
}
},
);
it("retains earlier build IDs when a later note lacks required alignment padding", async () => {
const shortNote = Buffer.from([
4, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0x47, 0x4e, 0x55, 0, 0xaa,
]);
for (const [padded, prior] of [
[true, false],
[false, false],
[false, true],
] as const) {
const notes = Buffer.concat([
...(prior ? [buildIdNote()] : []),
shortNote,
...(padded ? [Buffer.alloc(3)] : []),
]);
const result = await analyzeImage(
aotImage(),
"identify_flutter_build",
engineFixtureImage({ notes }),
);
const value = flutterResultSchemas.identify_flutter_build.parse(
result.result,
);
expect(value.coverage).toBe(padded ? "complete" : "partial");
expect(value.abis[0]!.libflutter).toMatchObject({
build_id: padded ? "aa" : null,
build_id_candidates: padded ? ["aa"] : prior ? [engineBuildId] : [],
});
}
});
it("keeps unified kind 2 from qualifying a legacy split snapshot", async () => {
const image = aotImage();
image.writeBigUInt64LE(2n, 0x1000 + 12);
image.writeBigUInt64LE(2n, 0x2000 + 12);
const execution = await analyzeImage(image, "inspect_dart_aot");
const value = flutterResultSchemas.inspect_dart_aot.parse(execution.result);
expect(value).toMatchObject({
coverage: "partial",
libapp: {
coverage: "partial",
snapshot_profile: null,
},
string_pool: { coverage: "complete" },
});
expect(
value.libapp.sections
.filter(({ magic_valid }) => magic_valid)
.map(({ kind }) => kind),
).toEqual([2, 2]);
});
it.each(["absent", "range", "metadata-budget"] as const)(
"preserves %s engine-note coverage at the ELF boundary",
async (shape) => {
const engine = engineFixtureImage({
ownership: shape === "range" ? "section" : "program",
notes:
shape === "metadata-budget"
? Buffer.alloc(8 * 1024 * 1024)
: shape === "range"
? buildIdNote()
: Buffer.alloc(0),
});
const result = await analyzeImage(
aotImage(),
"identify_flutter_build",
shape === "range"
? appendElfRanges(engine, "program", [
{ type: 4, offset: engine.length * 100, size: 0 },
])
: engine,
);
const value = flutterResultSchemas.identify_flutter_build.parse(
result.result,
);
expect(value.coverage).toBe(shape === "absent" ? "complete" : "partial");
expect(value.abis[0]!.libflutter).toMatchObject({
build_id: null,
build_id_candidates: shape === "range" ? [engineBuildId] : [],
});
if (shape !== "absent")
expect(result.limitations.join(" ")).toContain(
shape === "range" ? "not file-backed" : "metadata budget",
);
},
);
it.each([
["root", "EACCES"],
["open", "EPERM"],
["write", "ENOSPC"],
["close", "EIO"],
["chmod", "EACCES"],
["stat", "EIO"],
["digest", "EACCES"],
["zip-library", "EIO"],
] as const)(
"classifies owned snapshot %s failure (%s) separately from source input",
async (stage, code) => {
const path = await buildApk("owned-io.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const cause = Object.assign(new Error("owned snapshot failure"), { code });
const native =
await vi.importActual<typeof import("node:fs/promises")>(
"node:fs/promises",
);
if (stage === "zip-library")
vi.spyOn(ZipArtifactReader.prototype, "open").mockRejectedValueOnce(
cause,
);
if (stage === "root")
vi.spyOn(fsPromises, "mkdtemp").mockRejectedValueOnce(cause);
if (stage === "chmod")
vi.spyOn(fsPromises, "chmod").mockRejectedValueOnce(cause);
if (stage === "stat")
vi.spyOn(fsPromises, "stat").mockImplementation((...args) => {
if (args[0] !== path) return Promise.reject(cause);
return native.stat(...args);
});
if (["open", "write", "close"].includes(stage))
vi.spyOn(fsPromises, "open").mockImplementation(async (...args) => {
if (args[1] === "wx" && stage === "open") throw cause;
const handle = await native.open(...args);
if (args[1] === "wx") {
if (stage === "write")
vi.spyOn(handle, "writeFile").mockRejectedValueOnce(cause);
if (stage === "close")
vi.spyOn(handle, "close").mockRejectedValueOnce(cause);
}
return handle;
});
if (stage === "digest")
vi.mocked(createReadStream).mockImplementation(
() =>
Readable.from(
(async function* () {
yield Buffer.alloc(0);
throw cause;
})(),
) as ReturnType<typeof createReadStream>,
);
onTestFinished(() => {
vi.restoreAllMocks();
vi.mocked(createReadStream).mockReset();
});
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
if (result.ok) throw new Error("expected owned I/O failure");
expect(result.error.cause).toBe(cause);
// Only storage exhaustion is a resource constraint; other owned-storage
// failures keep their system code instead of claiming a size allowance.
// These causes carry no errno path, so the failure is located by the owned
// workspace once allocated; before allocation no workspace exists.
const workspace =
stage === "root"
? null
: (expect.stringMatching(/[/\\]rea-flutter-[^/\\]+$/u) as unknown);
const location = { workspace_path: workspace, path: workspace };
expect(projectAnalysisError(result.error)).toMatchObject(
code === "ENOSPC"
? {
code: "resource_constraint",
details: { reported_limits: { ...location, system_code: code } },
}
: {
code: "execution_failure",
details: {
provider_id: "flutter",
diagnostics: {
stage: "workspace",
...location,
system_code: code,
},
},
},
);
},
);
it.each(["close", "elf", "pool", "zip-library", "cleanup"] as const)(
"honors cancellation during final %s work",
async (stage) => {
const path = await buildApk("late-cancellation.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const controller = new AbortController();
const reason = { caller: `stop during ${stage}` };
let imageWriter: Awaited<ReturnType<typeof fsPromises.open>> | undefined;
let writerCloseObserved = false;
if (stage === "close" || stage === "elf") {
const originalOpen = fsPromises.open;
vi.spyOn(fsPromises, "open").mockImplementation(async (...args) => {
const handle = await originalOpen(...args);
if (
typeof args[0] === "string" &&
args[0].endsWith(`${sep}libapp.so`) &&
args[1] === "wx"
)
imageWriter = handle;
return handle;
});
const originalClose = OwnedFileHandle.prototype.close;
vi.spyOn(OwnedFileHandle.prototype, "close").mockImplementation(
async function (this: OwnedFileHandle) {
await originalClose.call(this);
if (this.handle === imageWriter && !writerCloseObserved) {
writerCloseObserved = true;
if (stage === "close") controller.abort(reason);
else setImmediate(() => controller.abort(reason));
}
},
);
}
if (stage === "pool") {
const original = dartElf.readElfSymbols;
vi.spyOn(dartElf, "readElfSymbols").mockImplementation(
async (...args) => {
const value = await original(...args);
setImmediate(() => controller.abort(reason));
return value;
},
);
}
if (stage === "zip-library") {
const original = ZipArtifactReader.prototype.open;
vi.spyOn(ZipArtifactReader.prototype, "open").mockImplementation(
async function (this: ZipArtifactReader, ...args) {
controller.abort(reason);
return original.call(this, ...args);
},
);
}
onTestFinished(() => {
vi.restoreAllMocks();
});
interceptFlutterRootRemoval(async (root) => {
await realRm(root, { recursive: true, force: true });
if (stage === "cleanup") controller.abort(reason);
});
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute(
{ operation: "inspect_dart_aot", input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("late cancellation must prevent success");
expect(projectAnalysisError(result.error).code).toBe("cancelled");
expect(result.error.cause).toBe(reason);
if (stage === "close" || stage === "elf")
expect(writerCloseObserved).toBe(true);
},
);
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s keeps completed Evidence when cancelled during successful snapshot removal",
async (operation) => {
const path = await buildApk("removal-cancellation.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
{ path: "lib/arm64-v8a/libflutter.so", bytes: libflutter() },
]);
const controlProvider = new FlutterBuildProvider();
onTestFinished(() => controlProvider.close());
const control = await createFlutterBuildAnalysisService(
controlProvider,
).execute(operation, { path });
if (!control.ok) throw control.error;
const controller = new AbortController();
const reason = new Error("caller interrupted snapshot removal");
const removedRoots: string[] = [];
interceptFlutterRootRemoval(async (root) => {
await realRm(root, { recursive: true, force: true });
removedRoots.push(root);
controller.abort(reason);
});
const provider = new FlutterBuildProvider();
const result = await createFlutterBuildAnalysisService(provider).execute(
operation,
{ path },
{ signal: controller.signal },
);
if (result.ok) throw new Error("cancellation must prevent success");
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
expect(result.error.cause).toBe(reason);
expect(result.error.partialObservation).toEqual(control.value);
expect(removedRoots).toHaveLength(1);
await expect(stat(removedRoots[0]!)).rejects.toMatchObject({
code: "ENOENT",
});
await provider.close();
expect(removedRoots).toHaveLength(1);
},
);
it("projects each payload shape through the owned snapshot", async () => {
const hash = REFERENCE_HASH;
const agreeBoth = await buildApk("agree.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: libapp(hash) },
{ path: "lib/arm64-v8a/libflutter.so", bytes: libflutter() },
{ path: "lib/x86_64/libapp.so", bytes: libapp(hash) },
{ path: "lib/x86_64/libflutter.so", bytes: libflutter() },
]);
const half = await buildApk("half.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: libapp(hash) },
]);
const disagree = await buildApk("disagree.apk", [
{
path: "lib/arm64-v8a/libapp.so",
bytes: Buffer.concat([
snapshotSection("11111111111111111111111111111111"),
snapshotSection("22222222222222222222222222222222"),
]),
},
{ path: "lib/arm64-v8a/libflutter.so", bytes: libflutter() },
]);
const plain = await buildApk("plain.apk", [
{ path: "classes.dex", bytes: Buffer.from("dex\n0123456789") },
{ path: "lib/arm64-v8a/libnative.so", bytes: Buffer.from("plain-elf") },
]);
const provider = new FlutterBuildProvider();
const identify = async (apk: string) => {
const outcome = await provider.execute({
operation: "identify_flutter_build",
input: { path: apk },
});
if (!outcome.ok) throw new Error(outcome.error.message);
return outcome.value.result as {
flutter_detected: boolean;
coverage: string;
abis: {
abi: string;
libapp: {
present: boolean;
snapshot_hash: string | null;
snapshot_hash_sources: number;
snapshot_hash_candidates: string[];
};
libflutter: { present: boolean; build_id: string | null };
}[];
};
};
const both = await identify(agreeBoth);
expect(both.flutter_detected).toBe(true);
expect(both.coverage).toBe("complete");
expect(both.abis.map((abi) => abi.abi)).toEqual(["arm64-v8a", "x86_64"]);
for (const abi of both.abis) {
expect(abi.libapp.snapshot_hash).toBe(hash);
expect(abi.libapp.snapshot_hash_sources).toBe(2);
expect(abi.libflutter.build_id).toBe(
"e8d907ef7c175ec4313f47111019adc9588c5f61",
);
}
const engineless = await identify(half);
expect(engineless.coverage).toBe("partial");
expect(engineless.abis[0]?.libapp.present).toBe(true);
expect(engineless.abis[0]?.libflutter.present).toBe(false);
const conflicted = await identify(disagree);
expect(conflicted.coverage).toBe("partial");
expect(conflicted.abis[0]?.libapp.snapshot_hash).toBeNull();
expect(conflicted.abis[0]?.libapp.snapshot_hash_candidates).toEqual([
"11111111111111111111111111111111",
"22222222222222222222222222222222",
]);
const negative = await identify(plain);
expect(negative.flutter_detected).toBe(false);
expect(negative.abis).toEqual([]);
expect(negative.coverage).toBe("complete");
});
it("refuses an APK without a Dart payload", async () => {
const apk = await buildApk("noflutter.apk", [
{ path: "classes.dex", bytes: Buffer.from("dex\n0123456789") },
]);
const result = await new FlutterBuildProvider().execute({
operation: "inspect_dart_aot",
input: { path: apk },
});
expect(result.ok).toBe(false);
if (result.ok) return;
expect(projectAnalysisError(result.error)).toMatchObject({
code: "unsupported_target",
});
});
it("refuses a requested ABI the payload does not carry", async () => {
const apk = await buildApk("oneabi.apk", [
{
path: "lib/arm64-v8a/libapp.so",
bytes: libapp(REFERENCE_HASH),
},
]);
const result = await new FlutterBuildProvider().execute({
operation: "inspect_dart_aot",
input: { path: apk, abi: "x86_64" },
});
expect(result.ok).toBe(false);
if (result.ok) return;
expect(projectAnalysisError(result.error)).toMatchObject({
code: "unsupported_target",
});
expect(result.error.message).toContain("arm64-v8a");
});
it("reports aliased snapshot data symbols as partial coverage", async () => {
const apk = await buildApk("aliased.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aliasedDataImage() },
]);
const result = await new FlutterBuildProvider().execute({
operation: "inspect_dart_aot",
input: { path: apk },
});
if (!result.ok) throw new Error(result.error.message);
const value = result.value.result as {
coverage: string;
libapp: {
snapshot_hash: string | null;
snapshot_hash_candidates: string[];
snapshot_hash_sources: number;
};
};
// The aliased range is scanned once, so one candidate no longer counts
// as two agreeing sources.
expect(value.coverage).toBe("partial");
expect(value.libapp.snapshot_hash_sources).toBe(1);
expect(value.libapp.snapshot_hash_candidates).toEqual([REFERENCE_HASH]);
expect(result.value.limitations).toContain(
"Two or more snapshot data symbols share one header offset; its hash counts as one source instead of two agreeing ones",
);
});
it("preserves conflicting snapshot hash candidates from the AOT image", async () => {
const apk = await buildApk("conflicted.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: conflictingHashImage() },
]);
const result = await new FlutterBuildProvider().execute({
operation: "inspect_dart_aot",
input: { path: apk },
});
if (!result.ok) throw new Error(result.error.message);
const value = result.value.result as {
coverage: string;
libapp: {
snapshot_hash: string | null;
snapshot_hash_candidates: string[];
snapshot_hash_sources: number;
};
};
expect(value.coverage).toBe("partial");
expect(value.libapp.snapshot_hash).toBeNull();
expect(value.libapp.snapshot_hash_sources).toBe(2);
expect(value.libapp.snapshot_hash_candidates).toEqual([
"11111111111111111111111111111111",
"22222222222222222222222222222222",
]);
});
it("retains failed snapshot cleanup and retries it on close", async () => {
const apk = await buildApk("cleanup.apk", [
{
path: "lib/arm64-v8a/libapp.so",
bytes: libapp(REFERENCE_HASH),
},
]);
const roots: string[] = [];
let failing = true;
interceptFlutterRootRemoval(async (path) => {
roots.push(path);
if (failing) throw new Error("cleanup refused");
await realRm(path, { recursive: true, force: true });
});
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
failing = false;
await provider.close();
});
const service = createFlutterBuildAnalysisService(provider);
const result = await finishAnalysisCommand(
() =>
service.execute("identify_flutter_build", {
path: apk,
}),
{ close: () => provider.close() },
"flutter",
"identify_flutter_build",
new AbortController().signal,
).then(projectCliAnalysisResult);
expect(result).toMatchObject({
code: "cleanup_incomplete",
details: {
resources: [expect.stringContaining("rea-flutter-")],
partial_observation: {
operation: "identify_flutter_build",
subject: { local_path: apk },
normalized_result: { flutter_detected: true },
},
},
});
expect(result).toMatchObject({
details: { resources: [roots[0]] },
});
expect(roots).toHaveLength(2);
expect(roots[1]).toBe(roots[0]);
failing = false;
await provider.close();
expect(roots).toHaveLength(3);
expect(roots[2]).toBe(roots[0]);
});
it("cancels before creating any snapshot root", async () => {
const apk = await buildApk("cancel.apk", [
{
path: "lib/arm64-v8a/libapp.so",
bytes: libapp(REFERENCE_HASH),
},
]);
const controller = new AbortController();
controller.abort();
const provider = new FlutterBuildProvider();
const result = await provider.execute(
{ operation: "identify_flutter_build", input: { path: apk } },
{ signal: controller.signal },
);
expect(result.ok).toBe(false);
await provider.close();
});
it("identifies an ELF header split across the library stream", async () => {
const originalOpen = ZipArtifactReader.prototype.open;
const spy = vi
.spyOn(ZipArtifactReader.prototype, "open")
.mockImplementation(async function (
this: ZipArtifactReader,
entry,
signal,
) {
const source = await originalOpen.call(this, entry, signal);
if (entry.path !== "lib/arm64-v8a/libapp.so") return source;
return Readable.from(
(async function* () {
for await (const chunk of source) {
const bytes = chunk as Buffer;
yield bytes.subarray(0, 47);
if (bytes.length > 47) yield bytes.subarray(47);
}
})(),
);
});
onTestFinished(() => spy.mockRestore());
const execution = await analyzeImage(aotImage(), "identify_flutter_build");
const value = flutterResultSchemas.identify_flutter_build.parse(
execution.result,
);
expect(value.coverage).toBe("complete");
expect(value.abis[0]?.libapp).toMatchObject({
elf_header_valid: true,
snapshot_hash: REFERENCE_HASH,
snapshot_hash_sources: 2,
});
});
it("retains byte-scanned hashes from an ELF with a truncated section table", async () => {
const image = aotImage();
image.writeBigUInt64LE(BigInt(image.length - 1), 0x28);
const execution = await analyzeImage(image, "identify_flutter_build");
const value = flutterResultSchemas.identify_flutter_build.parse(
execution.result,
);
expect(value.coverage).toBe("partial");
expect(value.abis[0]?.libapp).toMatchObject({
elf_header_valid: false,
snapshot_hash: REFERENCE_HASH,
snapshot_hash_sources: 2,
});
expect(execution.limitations.join("\n")).toContain(
"ELF header or table extent",
);
});
it("reports a present but empty libapp as partial", async () => {
const execution = await analyzeImage(
Buffer.alloc(0),
"identify_flutter_build",
);
const value = flutterResultSchemas.identify_flutter_build.parse(
execution.result,
);
expect(value).toMatchObject({
coverage: "partial",
flutter_detected: true,
abis: [
{
libapp: {
present: true,
bytes: 0,
elf_header_valid: false,
snapshot_hash: null,
snapshot_hash_sources: 0,
},
},
],
});
});
it("accepts a section-stripped ELF32 header and bounds its program table", async () => {
const image = Buffer.alloc(52 + 32 + 104);
image.writeUInt32LE(0x464c457f);
image.set([1, 1, 1], 4); // ELF32, little-endian, version 1
image.writeUInt16LE(3, 0x10); // ET_DYN
image.writeUInt32LE(1, 0x14);
image.writeUInt32LE(52, 0x1c);
image.writeUInt16LE(52, 0x28);
image.writeUInt16LE(32, 0x2a);
image.writeUInt16LE(1, 0x2c);
image.writeUInt32LE(1, 52); // PT_LOAD
image.writeUInt32LE(image.length, 52 + 16);
snapshotSection(REFERENCE_HASH).copy(image, 84);
snapshotSection(REFERENCE_HASH).copy(image, 136);
const good = await analyzeImage(image, "identify_flutter_build");
expect(
flutterResultSchemas.identify_flutter_build.parse(good.result).coverage,
).toBe("complete");
image.writeUInt32LE(image.length - 31, 0x1c); // One byte short of the table.
const bad = await analyzeImage(image, "identify_flutter_build");
expect(
flutterResultSchemas.identify_flutter_build.parse(bad.result).coverage,
).toBe("partial");
});
it("retains a hash and the raw declared length when the Dart snapshot exceeds its symbol", async () => {
const image = aotImage();
image.writeBigUInt64LE(49n, 0x1004);
const execution = await analyzeImage(image, "inspect_dart_aot");
const value = flutterResultSchemas.inspect_dart_aot.parse(execution.result);
expect(value.coverage).toBe("partial");
expect(value.libapp.snapshot_hash).toBe(REFERENCE_HASH);
expect(value.libapp.sections[0]).toMatchObject({
magic_valid: true,
header_length: 49,
});
expect(execution.limitations.join("\n")).toContain(
"_kDartVmSnapshotData has an incomplete or invalid snapshot header",
);
});
it("does not let two VM records replace a missing isolate header hash", async () => {
const image = aotImage();
const elf = buildElf64Image([
{ name: "_kDartVmSnapshotData", value: 0x1000, size: 104 },
{ name: "_kDartVmSnapshotInstructions", value: 0x1800, size: 16 },
{ name: "_kDartIsolateSnapshotData", value: 0x2000, size: 0x600 },
{ name: "_kDartIsolateSnapshotInstructions", value: 0x2600, size: 16 },
]);
elf.copy(image);
snapshotSection(REFERENCE_HASH).copy(image, 0x1000 + 52);
image[0x2000 + 20] = 0x78; // Keep the isolate magic but invalidate its hash.
const execution = await analyzeImage(image, "inspect_dart_aot");
const value = flutterResultSchemas.inspect_dart_aot.parse(execution.result);
expect(value.libapp.sections).toHaveLength(4);
expect(value.coverage).toBe("partial");
expect(value.libapp.snapshot_hash_sources).toBe(1);
expect(value.libapp.snapshot_hash_candidates).toEqual([REFERENCE_HASH]);
expect(execution.limitations.join("\n")).toContain(
"_kDartIsolateSnapshotData has an incomplete or invalid snapshot header",
);
});
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s preserves an oversized APK refusal as a resource constraint",
async (operation) => {
const root = await createTestTempDirectory("rea-flutter-size-");
const path = join(root, "oversized.apk");
const file = await open(path, "wx");
try {
await file.truncate(2 * 1024 ** 3 + 1); // Sparse file: no multi-GiB allocation.
} finally {
await file.close();
}
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("expected a size refusal");
expect(result.error).toBeInstanceOf(AnalysisResourceConstraintError);
const projected = projectAnalysisError(result.error);
expect(projected.code).toBe("resource_constraint");
expect(projected.retryable).toBe(false);
expect(projected.details?.refusal).toMatchObject({
kind: "fixed_ceiling",
allowance_kind: "input",
limit: 2 * 1024 ** 3,
unit: "bytes",
});
expect(projected.remediation.action).not.toMatch(/evidence reply/u);
expect(result.error).toMatchObject({
operation,
reportedLimits: {
max_apk_bytes: 2 * 1024 ** 3,
observed_bytes: 2 * 1024 ** 3 + 1,
},
});
},
);
it.each([
{ operation: "identify_flutter_build" },
{ operation: "inspect_dart_aot" },
] as const)(
"preserves native stream aborts during $operation digest",
async ({ operation }) => {
const path = await buildApk("abort.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const controller = new AbortController();
const nativeFs = await vi.importActual<typeof import("node:fs")>("node:fs");
const streamMock = vi.mocked(createReadStream);
let abortCode: string | undefined;
streamMock.mockImplementation((streamPath, options) => {
const stream = nativeFs.createReadStream(streamPath, options);
if (streamPath !== path) {
stream.once("error", (cause: NodeJS.ErrnoException) => {
abortCode = cause.code;
});
stream.once("readable", () =>
controller.abort(new Error("caller requested stop")),
);
}
return stream;
});
onTestFinished(() => {
streamMock.mockReset();
});
const removedRoots: string[] = [];
interceptFlutterRootRemoval(async (root) => {
await realRm(root, { recursive: true, force: true });
removedRoots.push(root);
});
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute(
{ operation, input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("expected cancellation");
expect(abortCode).toBe("ABORT_ERR");
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
expect(result.error).toMatchObject({ operation });
expect(projectAnalysisError(result.error).code).toBe("cancelled");
expect(removedRoots).toHaveLength(1);
},
);
it("preserves an independent abort-shaped digest stream failure when the caller also aborts", async () => {
const path = await buildApk("digest-physical-abort.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const controller = new AbortController();
const reason = { caller: "stop after physical stream failure" };
const physical = Object.assign(new Error("digest read failed"), {
name: "AbortError",
code: "EIO",
});
const nativeFs = await vi.importActual<typeof import("node:fs")>("node:fs");
const streamMock = vi.mocked(createReadStream);
streamMock.mockImplementation((streamPath, options) => {
const stream = nativeFs.createReadStream(streamPath, options);
if (streamPath !== path) {
stream.once("readable", () => {
stream.destroy(physical);
controller.abort(reason);
});
}
return stream;
});
onTestFinished(() => {
streamMock.mockReset();
});
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute(
{ operation: "inspect_dart_aot", input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("expected physical stream failure");
expect(result.error).not.toBeInstanceOf(AnalysisCancelledError);
expect(result.error.cause).toBe(physical);
});
it.each([undefined, "arm64-v8a", "x86_64", "mips"])(
"applies the library cap only to selected ABI %s",
async (abi) => {
const path = await buildApk("mixed-sizes.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
{ path: "lib/x86_64/libapp.so", bytes: aotImage() },
]);
const originalEntries = ZipArtifactReader.prototype.entries;
const inventory = vi
.spyOn(ZipArtifactReader.prototype, "entries")
.mockImplementation(async function* (this: ZipArtifactReader, signal) {
for await (const entry of originalEntries.call(this, signal))
yield entry.path.includes("x86_64")
? { ...entry, declaredSize: 256 * 1024 ** 2 + 1 }
: entry;
});
const reads = vi.spyOn(ZipArtifactReader.prototype, "open");
onTestFinished(() => {
inventory.mockRestore();
reads.mockRestore();
});
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({
operation: "inspect_dart_aot",
input: { path, ...(abi === undefined ? {} : { abi }) },
});
if (abi === "x86_64" || abi === "mips") {
if (result.ok) throw new Error("expected selected-ABI refusal");
expect(projectAnalysisError(result.error).code).toBe(
abi === "x86_64" ? "resource_constraint" : "unsupported_target",
);
expect(reads).not.toHaveBeenCalled();
} else {
if (!result.ok) throw result.error;
expect(
flutterResultSchemas.inspect_dart_aot.parse(result.value.result).abi,
).toBe("arm64-v8a");
expect(reads.mock.calls.map(([entry]) => entry.path)).toEqual([
"lib/arm64-v8a/libapp.so",
]);
}
},
);
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s preserves a real ZIP metadata budget refusal",
async (operation) => {
const root = await createTestTempDirectory("rea-flutter-metadata-");
const path = join(root, "large-directory.apk");
const writer = new ZipWriter(new Uint8ArrayWriter(), {
useWebWorkers: false,
});
const comment = "x".repeat(65535);
for (let index = 0; index < 129; index += 1)
await writer.add(
`assets/empty-${String(index)}`,
new Uint8ArrayReader(new Uint8Array()),
{ comment, level: 0 },
);
await writeFile(path, await writer.close());
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("expected metadata capacity refusal");
expect(projectAnalysisError(result.error).code).toBe("resource_constraint");
expect(result.error).toMatchObject({
operation,
reportedLimits: { max_metadata_read_bytes: 8 * 1024 ** 2 },
});
expect(result.error.message).toContain("ZIP metadata read requires");
expect(projectAnalysisError(result.error).details?.refusal).toMatchObject({
kind: "fixed_ceiling",
limit: 8 * 1024 ** 2,
unit: "bytes",
});
},
);
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s names the APK expansion allowance, not the metadata read allowance, for oversized declared members",
async (operation) => {
const root = await createTestTempDirectory("rea-flutter-expansion-");
const path = join(root, "declared.apk");
// A small archive declaring a 300 MiB member exceeds the 256 MiB floor
// of its size-derived expansion budget while its metadata fits.
await writeFile(
path,
zstandardZipFixture(Buffer.from("x"), {
method: 0,
compressed: Buffer.from("x"),
declaredSize: 300 * 1024 ** 2,
name: "assets/declared.bin",
}),
);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("expected an expansion refusal");
const projected = projectAnalysisError(result.error);
expect(projected.code).toBe("resource_constraint");
expect(projected.details?.refusal).toMatchObject({
kind: "fixed_ceiling",
allowance_kind: "parser",
limit: 256 * 1024 ** 2,
unit: "bytes",
});
expect(projected.details?.reported_limits).toEqual({
max_expanded_bytes: 256 * 1024 ** 2,
});
},
);
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s names the ZIP decoder window, not the metadata read allowance, for a large-window library",
async (operation) => {
const root = await createTestTempDirectory("rea-flutter-zstd-");
const path = join(root, "zstd-window.apk");
// Window descriptor 0x90 declares 256 MiB, above the ZIP reader's window.
const { content, compressed } = zstandardWindowFrameFixture(0x90);
await writeFile(
path,
zstandardZipFixture(content, {
compressed,
name: "lib/arm64-v8a/libapp.so",
}),
);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("expected a decoder window refusal");
const projected = projectAnalysisError(result.error);
expect(projected.code).toBe("resource_constraint");
expect(projected.details?.refusal).toMatchObject({
kind: "fixed_ceiling",
allowance: "The ZIP Zstandard decoder window allowance",
limit: 128 * 1024 ** 2,
unit: "bytes",
});
// The recovery is the reader's ceiling through the refusal formatter.
expect(projected.remediation.action).toBe(
analysisRefusalRemediation({
kind: "fixed_ceiling",
operation,
ceiling: ZIP_ZSTANDARD_WINDOW_CEILING,
}),
);
},
);
it("refuses snapshot names without exported definitions", async () => {
const image = aotImage();
const table = Number(
image.readBigUInt64LE(Number(image.readBigUInt64LE(0x28)) + 64 + 0x18),
);
image[table + 5] = 2; // Hidden VM data.
image.writeUInt16LE(0, table + 24 + 6); // Undefined VM instructions.
image[table + 2 * 24 + 4] = 0; // Local isolate data.
image[table + 3 * 24 + 5] = 1; // Internal isolate instructions.
await expect(analyzeImage(image, "inspect_dart_aot")).rejects.toMatchObject({
_tag: "AnalysisTargetFailureError",
});
});
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s retains a reader whose close failed before removing its root",
async (operation) => {
const path = await buildApk("reader-close.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const readers = new Set<ZipArtifactReader>();
const originalClose = ZipArtifactReader.prototype.close;
let failClose = true;
const close = vi
.spyOn(ZipArtifactReader.prototype, "close")
.mockImplementation(async function (this: ZipArtifactReader) {
readers.add(this);
if (failClose) throw new Error("file handle close refused");
await originalClose.call(this);
});
const removeRoot = vi.fn((root: string) =>
realRm(root, { recursive: true, force: true }),
);
interceptFlutterRootRemoval(removeRoot);
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
failClose = false;
await provider.close();
for (const reader of readers) await originalClose.call(reader);
close.mockRestore();
});
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("Expected retained reader cleanup");
expect(result.error.cleanupIncomplete).toBe(true);
expect(result.error.partialObservation).toMatchObject({ operation });
expect(removeRoot).not.toHaveBeenCalled();
let cleanupFailure: unknown;
try {
await provider.close();
} catch (cause) {
cleanupFailure = cause;
}
expect(cleanupFailure).toBeInstanceOf(ProviderCleanupError);
expect(cleanupFailure).toMatchObject({
operation: "close_flutter_build",
cleanupResources: [expect.stringContaining("rea-flutter-")],
diagnostics: {
reason: expect.stringContaining("file handle close refused"),
retained_roots: [
{
path: expect.stringContaining("rea-flutter-"),
failures: [
{ message: expect.stringContaining("file handle close refused") },
],
},
],
},
});
expect(removeRoot).not.toHaveBeenCalled();
failClose = false;
await provider.close();
expect(close).toHaveBeenCalledTimes(3);
expect(removeRoot).toHaveBeenCalledTimes(1);
},
);
it("qualifies unlabeled Dart-shaped strings as candidates", async () => {
const candidate = "1.2.3 (stable) (unrelated string)";
const path = await buildApk("dart-pattern.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
{
path: "lib/arm64-v8a/libflutter.so",
bytes: Buffer.concat([libflutter(), Buffer.from(`\0${candidate}\0`)]),
},
]);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
if (!result.ok) throw result.error;
const value = flutterResultSchemas.identify_flutter_build.parse(
result.value.result,
);
expect(value.abis[0]?.libflutter.dart_version).toBe(candidate);
expect(result.value.limitations.join(" ")).toContain(
"a matching unrelated string may be reported",
);
});
it("reports omitted Dart labels independently of toolchain labels", async () => {
const path = await buildApk("dart-label.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
{
path: "lib/arm64-v8a/libflutter.so",
bytes: Buffer.concat([
libflutter(),
Buffer.from(`Dart VM version:${"x".repeat(201)}\0`),
]),
},
]);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
if (!result.ok) throw result.error;
const value = flutterResultSchemas.identify_flutter_build.parse(
result.value.result,
);
expect(value.coverage).toBe("partial");
expect(value.abis[0]?.libflutter).toMatchObject({
dart_version: null,
dart_version_capped: true,
toolchain_capped: false,
});
});
it.each([0, 1, 2, 3])(
"retains failed local file-handle closure at acquisition stage %i",
async (stage) => {
const path = await buildApk("file-close.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
// Close order: snapshot writer, admitted source, extracted-image writer, image reader.
const owners: OwnedFileHandle[] = [];
const originalClose = OwnedFileHandle.prototype.close;
let failing = true;
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockImplementation(async function (this: OwnedFileHandle) {
if (!owners.includes(this)) owners.push(this);
if (failing && owners[stage] === this)
throw new Error("handle close refused");
await originalClose.call(this);
});
const removeRoot = vi.fn((root: string) =>
realRm(root, { recursive: true, force: true }),
);
interceptFlutterRootRemoval(removeRoot);
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
failing = false;
await provider.close();
for (const owner of owners) await originalClose.call(owner);
close.mockRestore();
});
const result = await provider.execute({
operation: "inspect_dart_aot",
input: { path },
});
expect(result.ok).toBe(false);
expect(removeRoot).not.toHaveBeenCalled();
await expect(provider.close()).rejects.toBeInstanceOf(ProviderCleanupError);
failing = false;
await provider.close();
expect(removeRoot).toHaveBeenCalledTimes(1);
},
);
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s rejects duplicate real ZIP members before extracting either occurrence",
async (operation) => {
const path = await createDuplicateFlutterApk();
const extract = vi.spyOn(ZipArtifactReader.prototype, "open");
onTestFinished(() => extract.mockRestore());
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("Duplicate libraries must be refused");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
message: expect.stringContaining(
`duplicate Flutter library members: ${path}:`,
),
},
],
},
});
expect(extract).not.toHaveBeenCalled();
},
);
it.each([
["identify_flutter_build", "ABI", 32, "ABIs"],
["inspect_dart_aot", "ABI", 32, "ABIs"],
["identify_flutter_build", "path", 1024, "bytes"],
] as const)(
"%s refuses %s metadata expansion before opening empty libraries",
async (operation, shape, limit, unit) => {
const path = await buildApk(
"many-abis.apk",
shape === "ABI"
? Array.from({ length: 33 }, (_, index) => ({
path: `lib/abi_${String(index)}/libapp.so`,
bytes: Buffer.alloc(0),
}))
: [
{
path: `lib/${"a".repeat(1024)}/libapp.so`,
bytes: Buffer.alloc(0),
},
],
);
const extract = vi.spyOn(ZipArtifactReader.prototype, "open");
onTestFinished(() => extract.mockRestore());
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("ABI expansion must be refused");
const projected = projectAnalysisError(result.error);
expect(projected.code).toBe("resource_constraint");
expect(projected.details?.refusal).toMatchObject({
kind: "fixed_ceiling",
allowance_kind: "parser",
limit,
unit,
});
expect(extract).not.toHaveBeenCalled();
},
);
it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
"%s uses the selected path in malformed-APK diagnostics",
async (operation) => {
const root = await createTestTempDirectory("rea-flutter-malformed-");
const path = join(root, "selected.apk");
await writeFile(path, "not a ZIP");
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("Malformed APK must be refused");
const error = projectAnalysisError(result.error);
expect(error).toMatchObject({
code: "invalid_request",
details: { issues: [{ message: expect.stringContaining(path) }] },
});
expect(JSON.stringify(error)).not.toContain("input.apk");
},
);
const expectSelectionIssue = async (
provider: FlutterBuildProvider,
path: string,
message: string,
): Promise<void> => {
for (const operation of [
"identify_flutter_build",
"inspect_dart_aot",
] as const) {
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("A non-file APK selection must fail");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
path: ["path"],
reason: "invalid_format",
message: `${message}: ${path}`,
},
],
},
});
}
};
it("names a missing or non-file APK selection instead of blaming package bytes", async () => {
const root = await createTestTempDirectory("rea-flutter-selection-");
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
await expectSelectionIssue(
provider,
join(root, "absent.apk"),
"Selected APK does not exist",
);
await expectSelectionIssue(
provider,
root,
"Selected APK is not a regular file",
);
});
// Windows listens on named pipes rather than filesystem sockets.
it.skipIf(process.platform === "win32")(
"names an unopenable Unix socket APK selection as a non-file",
async () => {
const root = await createTestTempDirectory("rea-flutter-socket-");
const socketPath = join(root, "s.sock");
const socket = createServer();
await new Promise<void>((resolve) => socket.listen(socketPath, resolve));
onTestFinished(
() => new Promise<void>((resolve) => socket.close(() => resolve())),
);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
await expectSelectionIssue(
provider,
socketPath,
"Selected APK is not a regular file",
);
},
);
it.each(["EACCES", "EPERM"] as const)(
"preserves %s at the shared source admission boundary",
async (code) => {
const admitted = vi
.spyOn(regularFile, "openRegularFile")
.mockRejectedValue(Object.assign(new Error("denied"), { code }));
onTestFinished(() => admitted.mockRestore());
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
for (const operation of [
"identify_flutter_build",
"inspect_dart_aot",
] as const) {
const result = await provider.execute({
operation,
input: { path: "/selected.apk" },
});
if (result.ok) throw new Error("Denied read must fail");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "access_denied",
});
}
},
);
it.each(["EACCES", "EPERM"] as const)(
"preserves %s during opened-source reads and path revalidation",
async (code) => {
const path = await buildApk("source-denied.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const original = regularFile.openRegularFile;
const cause = Object.assign(new Error("source denied"), { code });
for (const stage of ["read", "revalidate"] as const) {
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const admitted = vi
.spyOn(regularFile, "openRegularFile")
.mockImplementation(async (...args) => {
const descriptor = await original(...args);
const { handle } = descriptor;
if (stage === "read")
vi.spyOn(handle, "read").mockRejectedValueOnce(cause);
return descriptor;
});
const pathStat =
stage === "revalidate"
? vi.spyOn(fsPromises, "stat").mockRejectedValueOnce(cause)
: undefined;
try {
const result = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
if (result.ok) throw new Error("Denied source must fail");
expect(projectAnalysisError(result.error).code).toBe("access_denied");
expect(result.error.cause).toBe(cause);
} finally {
admitted.mockRestore();
pathStat?.mockRestore();
}
}
},
);
it("detects same-size source rewrites with restored mtime through the opened descriptor", async () => {
const path = await buildApk("changing.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const before = await stat(path);
const content = await readFile(path);
const nativeAdmission = regularFile.openRegularFile;
const admitted = vi
.spyOn(regularFile, "openRegularFile")
.mockImplementation(async (...args) => {
const descriptor = await nativeAdmission(...args);
const { handle } = descriptor;
// Rewrite after a real descriptor read. The retained admission snapshot
// remains the baseline for final verification, including ctime.
const nativeRead = handle.read.bind(handle);
let rewritten = false;
vi.spyOn(handle, "read").mockImplementation(async (...readArgs) => {
const result = await nativeRead(...readArgs);
if (!rewritten) {
rewritten = true;
await new Promise((resolve) => setTimeout(resolve, 10));
await writeFile(path, content);
await utimes(path, before.atime, before.mtime);
}
return result;
});
return descriptor;
});
onTestFinished(() => admitted.mockRestore());
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
if (result.ok) throw new Error("Concurrent source rewrite must fail");
expect(projectAnalysisError(result.error).code).toBe("artifact_changed");
});
it("refuses truncated hash-candidate evidence and includes five-character tokens", async () => {
const image = aotImage();
Buffer.from("Login\0").copy(image, 0x2300);
const aot = await analyzeImage(image, "inspect_dart_aot");
expect(
flutterResultSchemas.inspect_dart_aot.parse(aot.result).string_pool
.class_like_tokens,
).toContain("Login");
const bytes = Buffer.concat(
Array.from({ length: 65 }, (_, index) =>
snapshotSection(index.toString(16).padStart(32, "0")),
),
);
const path = await buildApk("many-hashes.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes },
]);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
if (result.ok) throw new Error("Incomplete candidate set must be explicit");
const projected = projectAnalysisError(result.error);
expect(projected.code).toBe("resource_constraint");
expect(projected.details?.refusal).toMatchObject({
kind: "fixed_ceiling",
limit: 64,
unit: "candidates",
});
});
it.each([
{ outcome: "fulfilled", reason: { caller: "stop during source copy" } },
{ outcome: "rejected", reason: { caller: "stop during source copy" } },
{ outcome: "fulfilled", reason: Number.NaN },
{ outcome: "rejected", reason: Number.NaN },
{ outcome: "rejected", reason: 0 },
] as const)(
"preserves a $outcome source copy outcome when cancellation arrives in flight (reason=$reason)",
async ({ outcome, reason }) => {
const path = await buildApk("copy-cancel.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const controller = new AbortController();
const physical = Object.assign(new Error("source read failed"), {
code: "EIO",
});
const nativeAdmission = regularFile.openRegularFile;
const admitted = vi
.spyOn(regularFile, "openRegularFile")
.mockImplementation(async (...args) => {
const descriptor = await nativeAdmission(...args);
const { handle } = descriptor;
const nativeRead = handle.read.bind(handle);
vi.spyOn(handle, "read").mockImplementation(async (...readArgs) => {
const result = await nativeRead(...readArgs);
controller.abort(reason);
if (outcome === "rejected") throw physical;
return result;
});
return descriptor;
});
onTestFinished(() => admitted.mockRestore());
const removeRoot = vi.fn((root: string) =>
realRm(root, { recursive: true, force: true }),
);
interceptFlutterRootRemoval(removeRoot);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute(
{ operation: "identify_flutter_build", input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("Cancelled copying must fail");
if (outcome === "rejected") {
expect(result.error).not.toBeInstanceOf(AnalysisCancelledError);
expect(result.error.cause).toBe(physical);
const primaryCode = projectAnalysisError(result.error).code;
const finalize = (signal: AbortSignal) =>
finishAnalysisCommand(
async () => result,
{ close: () => provider.close() },
"flutter",
"identify_flutter_build",
signal,
);
for (let attempt = 0; attempt < 2; attempt++) {
const finalized = await finalize(controller.signal);
expect(finalized).toMatchObject({
ok: false,
error: {
code: primaryCode,
details: { secondary_failures: [{ code: "cancelled" }] },
},
});
expect(result.error.cause).toBe(physical);
expect(result.error.secondaryFailures).toHaveLength(1);
expect(result.error.secondaryFailures[0]?.cause).toBe(reason);
}
if (Object.is(reason, 0)) {
const otherCaller = new AbortController();
otherCaller.abort(-0);
await finalize(otherCaller.signal);
expect(result.error.secondaryFailures).toHaveLength(2);
expect(Object.is(result.error.secondaryFailures[0]?.cause, 0)).toBe(
true,
);
expect(Object.is(result.error.secondaryFailures[1]?.cause, -0)).toBe(
true,
);
expect(result.error.cause).toBe(physical);
}
} else {
expect(projectAnalysisError(result.error).code).toBe("cancelled");
expect(result.error.cause).toBe(reason);
}
expect(removeRoot).toHaveBeenCalledTimes(1);
},
);
it.each([
["source", "Error", "EIO"],
["image", "Error", "EIO"],
["source", "AbortError", "EIO"],
["image", "AbortError", "EIO"],
["source", "Error", "ABORT_ERR"],
["image", "Error", "ABORT_ERR"],
] as const)(
"preserves a rejected %s read with %s/%s when the exact physical error is also the abort reason",
async (stage, name, code) => {
const path = await buildApk("same-physical-reason.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const originalBytes = await readFile(path);
const controller = new AbortController();
const physical = Object.assign(new Error("physical read failed"), {
name,
code,
});
const rejectRead = (
handle: Awaited<ReturnType<typeof fsPromises.open>>,
) => {
const read = handle.read.bind(handle);
vi.spyOn(handle, "read").mockImplementation(async (...args) => {
await read(...args);
controller.abort(physical);
throw physical;
});
};
if (stage === "source") {
const original = regularFile.openRegularFile;
vi.spyOn(regularFile, "openRegularFile").mockImplementation(
async (...args) => {
const admitted = await original(...args);
rejectRead(admitted.handle);
return admitted;
},
);
} else {
const original = fsPromises.open;
vi.spyOn(fsPromises, "open").mockImplementation(async (...args) => {
const handle = await original(...args);
if (
typeof args[0] === "string" &&
args[0].endsWith(`${sep}libapp.so`) &&
args[1] === "r"
)
rejectRead(handle);
return handle;
});
}
onTestFinished(() => {
vi.restoreAllMocks();
});
const removeRoot = vi.fn((root: string) =>
realRm(root, { recursive: true, force: true }),
);
interceptFlutterRootRemoval(removeRoot);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute(
{ operation: "inspect_dart_aot", input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected a physical read failure");
expect(controller.signal.reason).toBe(physical);
expect(result.error).not.toBeInstanceOf(AnalysisCancelledError);
expect(result.error.cause).toBe(physical);
expect(result.error.cleanupIncomplete).toBe(false);
expect(removeRoot).toHaveBeenCalledOnce();
expect((await readFile(path)).equals(originalBytes)).toBe(true);
},
);
it.each([false, true])(
"reports actual nonempty-root removal failure and resolves its exact error after retry (cancel=%s)",
async (cancel) => {
const path = await buildApk("retained-root.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
let refuse = true;
let retainedRoot = "";
const controller = new AbortController();
const reason = { caller: "cancel during refused cleanup" };
const controlProvider = new FlutterBuildProvider();
const control = await createFlutterBuildAnalysisService(
controlProvider,
).execute("identify_flutter_build", { path });
await controlProvider.close();
if (!control.ok) throw control.error;
interceptFlutterRootRemoval(async (root) => {
retainedRoot = root;
if (refuse) {
if (cancel) controller.abort(reason);
await rmdir(root); // Actual OS refusal: the owned snapshot still occupies the directory.
} else await realRm(root, { recursive: true, force: true });
});
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
refuse = false;
await provider.close();
});
const result = await provider.execute(
{ operation: "identify_flutter_build", input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected owned cleanup failure");
expect(result.error.cleanupIncomplete).toBe(true);
expect(result.error.cleanupResources).toEqual([retainedRoot]);
expect(result.error.partialObservation).toEqual(control.value);
if (cancel) expect(result.error.cleanupPrimary?.cause).toBe(reason);
await expect(stat(join(retainedRoot, "input.apk"))).resolves.toBeDefined();
let retryFailure: unknown;
try {
await provider.close();
} catch (cause) {
retryFailure = cause;
}
expect(retryFailure).toBeInstanceOf(ProviderCleanupError);
refuse = false;
await provider.close();
expect(result.error.cleanupIncomplete).toBe(false);
expect(result.error.cleanupResources).toEqual([]);
expect(retryFailure).toMatchObject({
cleanupIncomplete: false,
cleanupResources: [],
});
await expect(stat(retainedRoot)).rejects.toMatchObject({ code: "ENOENT" });
},
);
it("drains admitted snapshot work before close and fences later execution", async () => {
const path = await buildApk("drain-root.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
let reached!: () => void;
let proceed!: () => void;
const boundary = new Promise<void>((resolve) => {
reached = resolve;
});
const barrier = new Promise<void>((resolve) => {
proceed = resolve;
});
const original = ZipArtifactReader.prototype.open;
const list = vi
.spyOn(ZipArtifactReader.prototype, "open")
.mockImplementation(async function (
this: ZipArtifactReader,
...args: Parameters<typeof original>
) {
reached();
await barrier;
return original.apply(this, args);
});
const removeRoot = vi.fn((root: string) =>
realRm(root, { recursive: true, force: true }),
);
interceptFlutterRootRemoval(removeRoot);
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
proceed();
list.mockRestore();
await provider.close();
});
const running = provider.execute({
operation: "identify_flutter_build",
input: { path },
});
await boundary;
let closed = false;
const closing = provider.close().then(() => {
closed = true;
});
await Promise.resolve();
expect(closed).toBe(false);
expect(removeRoot).not.toHaveBeenCalled();
proceed();
expect((await running).ok).toBe(true);
await closing;
expect(removeRoot).toHaveBeenCalledTimes(1);
expect(
(
await provider.execute({
operation: "identify_flutter_build",
input: { path },
})
).ok,
).toBe(false);
});
it("preserves source admission failure when cancellation arrives during its root cleanup", async () => {
const root = await createTestTempDirectory("flutter-primary-");
const controller = new AbortController();
interceptFlutterRootRemoval(async (path) => {
await realRm(path, { recursive: true, force: true });
controller.abort({ caller: "late cleanup cancellation" });
});
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const result = await provider.execute(
{
operation: "identify_flutter_build",
input: { path: join(root, "absent.apk") },
},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected missing selected source");
expect(result.error).not.toBeInstanceOf(AnalysisCancelledError);
expect(result.error).toMatchObject({ _tag: "AnalysisInputError" });
expect(result.error.cleanupIncomplete).toBe(false);
});
it("retains mixed-root cleanup diagnostics and releases aggregate errors only after every root recovers", async () => {
const path = await buildApk("mixed-roots.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const roots: string[] = [];
const allowed = new Set<string>();
interceptFlutterRootRemoval(async (root) => {
if (!roots.includes(root)) roots.push(root);
if (!allowed.has(root)) await rmdir(root);
else await realRm(root, { recursive: true, force: true });
});
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
roots.forEach((root) => allowed.add(root));
await provider.close();
});
const [first, second] = await Promise.all([
provider.execute({ operation: "identify_flutter_build", input: { path } }),
provider.execute({ operation: "identify_flutter_build", input: { path } }),
]);
expect(first.ok || second.ok).toBe(false);
if (first.ok || second.ok) throw new Error("Expected both cleanup owners");
let aggregate: unknown;
try {
await provider.close();
} catch (cause) {
aggregate = cause;
}
expect(aggregate).toMatchObject({
operation: "close_flutter_build",
cause: expect.objectContaining({
errors: expect.arrayContaining(
roots.map((root) =>
expect.objectContaining({ cleanupResources: [root] }),
),
),
}),
cleanupIncomplete: true,
cleanupResources: expect.arrayContaining(roots),
diagnostics: {
retained_roots: expect.arrayContaining(
roots.map((root) => ({
path: root,
failures: [expect.objectContaining({ code: "ENOTEMPTY" })],
})),
),
},
});
allowed.add(first.error.cleanupResources[0]!);
await expect(provider.close()).rejects.toBeInstanceOf(ProviderCleanupError);
expect(first.error.cleanupIncomplete).toBe(false);
expect(second.error.cleanupIncomplete).toBe(true);
expect(aggregate).toMatchObject({
cleanupIncomplete: true,
cleanupResources: second.error.cleanupResources,
});
allowed.add(second.error.cleanupResources[0]!);
await provider.close();
expect(second.error.cleanupIncomplete).toBe(false);
expect(aggregate).toMatchObject({
cleanupIncomplete: false,
cleanupResources: [],
});
});
it("retries retained cleanup before admitting replacement snapshots", async () => {
const path = await buildApk("blocked-roots.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const roots = new Set<string>();
let refuse = true;
interceptFlutterRootRemoval(async (root) => {
roots.add(root);
if (refuse) await rmdir(root);
else await realRm(root, { recursive: true, force: true });
});
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
refuse = false;
await provider.close();
});
const first = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
const blocked = await provider.execute({
operation: "identify_flutter_build",
input: { path },
});
expect(roots.size).toBe(1);
expect(blocked.ok).toBe(false);
if (first.ok || blocked.ok) throw new Error("Expected retained cleanup");
expect(blocked.error.cleanupResources).toEqual(first.error.cleanupResources);
refuse = false;
expect(
(
await provider.execute({
operation: "identify_flutter_build",
input: { path },
})
).ok,
).toBe(true);
expect(first.error.cleanupIncomplete).toBe(false);
expect(blocked.error.cleanupIncomplete).toBe(false);
expect(roots.size).toBe(2);
});
it("does not create a replacement snapshot when cancelled during cleanup retry", async () => {
const path = await buildApk("cancel-cleanup-retry.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const roots = new Set<string>();
const controller = new AbortController();
const reason = { caller: "cancel during cleanup retry" };
let attempts = 0;
interceptFlutterRootRemoval(async (root) => {
roots.add(root);
attempts++;
if (attempts === 1) await rmdir(root);
else {
controller.abort(reason);
await realRm(root, { recursive: true, force: true });
}
});
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const request = {
operation: "identify_flutter_build" as const,
input: { path },
};
const first = await provider.execute(request);
if (first.ok) throw new Error("Expected retained cleanup");
const cancelled = await provider.execute(request, {
signal: controller.signal,
});
if (cancelled.ok) throw new Error("Expected cancellation");
expect(projectAnalysisError(cancelled.error).code).toBe("cancelled");
expect(cancelled.error.cause).toBe(reason);
expect(first.error.cleanupIncomplete).toBe(false);
expect(roots.size).toBe(1);
});
it.each([false, true])(
"retains the exact late cancellation alongside a completed failure (cleanup fails: %s)",
async (cleanupFails) => {
const path = await buildApk("unsupported-late-cancel.apk", []);
const controller = new AbortController();
const reason = { caller: "cancel during snapshot cleanup" };
let refuse = cleanupFails;
interceptFlutterRootRemoval(async (root) => {
controller.abort(reason);
if (refuse) await rmdir(root);
else await realRm(root, { recursive: true, force: true });
});
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
refuse = false;
await provider.close();
});
const result = await provider.execute(
{ operation: "inspect_dart_aot", input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected unsupported package failure");
const primary = result.error.cleanupPrimary ?? result.error;
expect(primary).not.toBeInstanceOf(AnalysisCancelledError);
expect(
primary.secondaryFailures.some((failure) => failure.cause === reason),
).toBe(true);
expect(result.error.cleanupIncomplete).toBe(cleanupFails);
refuse = false;
await provider.close();
expect(result.error.cleanupIncomplete).toBe(false);
},
);
it("retains cancellation when a prior failed root cannot be retired on admission", async () => {
const path = await buildApk("blocked-cleanup-abort.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: aotImage() },
]);
const controller = new AbortController();
const reason = { caller: "abort during failed retry" };
let refuse = true;
let attempts = 0;
interceptFlutterRootRemoval(async (root) => {
attempts += 1;
if (refuse) {
if (attempts === 2) controller.abort(reason);
await rmdir(root);
} else await realRm(root, { recursive: true, force: true });
});
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
refuse = false;
await provider.close();
});
const request = {
operation: "identify_flutter_build" as const,
input: { path },
};
const first = await provider.execute(request);
if (first.ok) throw new Error("Expected retained root");
const retry = await provider.execute(request, { signal: controller.signal });
if (retry.ok) throw new Error("Expected failed retry");
expect(retry.error.cleanupIncomplete).toBe(true);
expect(
retry.error.secondaryFailures.some((failure) => failure.cause === reason),
).toBe(true);
refuse = false;
await provider.close();
expect(retry.error.cleanupIncomplete).toBe(false);
});
it("preserves a completed analysis failure through refused cleanup and late cancellation", async () => {
const path = await buildApk("unsupported-cleanup.apk", []);
const controlProvider = new FlutterBuildProvider();
const control = await controlProvider.execute({
operation: "inspect_dart_aot",
input: { path },
});
await controlProvider.close();
if (control.ok) throw new Error("Expected unsupported package control");
const controller = new AbortController();
let refuse = true;
interceptFlutterRootRemoval(async (root) => {
if (refuse) {
controller.abort({ caller: "late failed cleanup" });
await rmdir(root);
} else await realRm(root, { recursive: true, force: true });
});
const provider = new FlutterBuildProvider();
onTestFinished(async () => {
refuse = false;
await provider.close();
});
const result = await provider.execute(
{ operation: "inspect_dart_aot", input: { path } },
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected primary and cleanup failure");
expect(result.error.cleanupIncomplete).toBe(true);
expect(projectAnalysisError(result.error.cleanupPrimary!)).toMatchObject(
projectAnalysisError(control.error),
);
expect(result.error.cleanupPrimary).not.toBeInstanceOf(
AnalysisCancelledError,
);
refuse = false;
await provider.close();
expect(result.error.cleanupIncomplete).toBe(false);
expect(projectAnalysisError(result.error)).toMatchObject(
projectAnalysisError(control.error),
);
});
it("recovers omitted Dart pool strings through selection and per-call admission without changing source identity", async () => {
const packages = Array.from(
{ length: 1001 },
(_, index) => `package:many/file${String(index).padStart(4, "0")}.dart`,
);
const tail = "package:tail/omitted.dart";
const longUri = `package:long/${"x".repeat(1100)}.dart`;
const longToken = `LongToken${"A".repeat(130)}`;
const tokens = Array.from(
{ length: 2001 },
(_, index) => `Class${String(index).padStart(4, "0")}`,
);
const isolate = Buffer.concat([
snapshotSection(REFERENCE_HASH),
Buffer.from(
`\0${[...packages, tail, longUri, ...tokens, longToken].join("\0")}\0`,
),
]);
const image = Buffer.alloc(0x2000 + isolate.length + 16);
buildElf64Image([
{ name: "_kDartVmSnapshotData", value: 0x1000, size: 52 },
{ name: "_kDartVmSnapshotInstructions", value: 0x1800, size: 16 },
{ name: "_kDartIsolateSnapshotData", value: 0x2000, size: isolate.length },
{
name: "_kDartIsolateSnapshotInstructions",
value: 0x2000 + isolate.length,
size: 16,
},
]).copy(image);
snapshotSection(REFERENCE_HASH).copy(image, 0x1000);
isolate.copy(image, 0x2000);
const path = await buildApk("pool-headroom.apk", [
{ path: "lib/arm64-v8a/libapp.so", bytes: image },
]);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
const service = createFlutterBuildAnalysisService(provider);
const inspect = async (pool_policy?: Record<string, unknown>) => {
const result = await service.execute("inspect_dart_aot", {
path,
...(pool_policy === undefined ? {} : { pool_policy }),
});
if (!result.ok) throw result.error;
return {
evidence: result.value,
report: flutterResultSchemas.inspect_dart_aot.parse(
result.value.normalized_result,
),
};
};
const limited = await inspect();
expect(limited.report.string_pool.package_uris).toHaveLength(500);
expect(limited.report.string_pool.class_like_tokens).toHaveLength(2000);
expect(limited.report.string_pool.package_uris).not.toContain(tail);
expect(limited.report.string_pool.package_uris).not.toContain(longUri);
expect(limited.report.string_pool.class_like_tokens).not.toContain(longToken);
expect(limited.report.string_pool.coverage).toBe("partial");
expect(
limited.evidence.limitations.some((text) =>
text.includes("pool_policy.run_bytes=1024"),
),
).toBe(true);
// The result records each budget that left the projection partial by
// contract field; adapters name their own option.
// Every budget this fixture exceeds is recorded, and nothing else.
const control = (budget: string, unit: "bytes" | "count", value: number) => ({
control_kind: "parameter",
parameter: `pool_policy.${budget}`,
effective_value: value,
unit,
minimum: 1,
maximum: Number.MAX_SAFE_INTEGER,
default_value: value,
applies_at: "next_call",
});
expect(
[...(limited.report.string_pool.limits ?? [])].sort((left, right) =>
left.parameter.localeCompare(right.parameter),
),
).toEqual([
control("run_bytes", "bytes", 1024),
control("token_bytes", "bytes", 128),
control("token_limit", "count", 2000),
control("uri_limit", "count", 500),
]);
expect(limited.evidence.limitations.join(" ")).not.toContain("--pool-policy");
const selected = await inspect({ contains: "package:tail/" });
expect(selected.report.string_pool.package_uris).toEqual([tail]);
expect(selected.report.string_pool.package_uri_count_at_least).toBe(1);
const recovered = await inspect({
uri_limit: 1003,
token_limit: 2002,
run_bytes: 2048,
token_bytes: 256,
});
expect(recovered.report.string_pool.package_uris).toHaveLength(1003);
expect(recovered.report.string_pool.package_uris).toContain(tail);
expect(recovered.report.string_pool.package_uris).toContain(longUri);
expect(recovered.report.string_pool.class_like_tokens).toContain(longToken);
expect(recovered.report.string_pool.coverage).toBe("complete");
expect(recovered.report.string_pool).not.toHaveProperty("limits");
expect(selected.report.target).toEqual(limited.report.target);
expect(recovered.report.libapp.sha256).toEqual(limited.report.libapp.sha256);
expect(recovered.evidence.parameters.pool_policy).toEqual({
uri_limit: 1003,
token_limit: 2002,
run_bytes: 2048,
token_bytes: 256,
});
const rejected = await service.execute("inspect_dart_aot", {
path,
pool_policy: { uri_limit: 0 },
});
expect(rejected.ok).toBe(false);
});