Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(flutter): find snapshot headers that a read chunk boundary splits (#2804)

Dragolone5278835b534a
diff --git a/src/flutter/FlutterPayloadScan.test.ts b/src/flutter/FlutterPayloadScan.test.ts
index 681a373a1..13bb4f743 100644
--- a/src/flutter/FlutterPayloadScan.test.ts
+++ b/src/flutter/FlutterPayloadScan.test.ts
@@ -70,6 +70,37 @@ describe("SnapshotHashScanner", () => {
});
});
+ it.each([49, 50, 51])(
+ "finds a section %i bytes after a rejected record at any chunking",
+ (start) => {
+ // A stray magic whose hash field is not hex, then a real section that
+ // starts inside the stray record's magic-to-hash span.
+ const payload = new Uint8Array(start + 52);
+ payload.set([0xf5, 0xf5, 0xdc, 0xdc], 0);
+ payload.set(
+ realSnapshotSection("65817c30a78bb44c3dc3771876b6010a"),
+ start,
+ );
+ const whole = new SnapshotHashScanner();
+ whole.push(payload);
+ expect(whole.result()).toEqual({
+ capped: false,
+ sources: 1,
+ candidates: ["65817c30a78bb44c3dc3771876b6010a"],
+ });
+ for (let split = 1; split < payload.length; split += 1) {
+ const scanner = new SnapshotHashScanner();
+ scanner.push(payload.subarray(0, split));
+ scanner.push(payload.subarray(split));
+ expect(scanner.result(), `split at ${split}`).toEqual(whole.result());
+ }
+ const bytewise = new SnapshotHashScanner();
+ for (let offset = 0; offset < payload.length; offset += 1)
+ bytewise.push(payload.subarray(offset, offset + 1));
+ expect(bytewise.result()).toEqual(whole.result());
+ },
+ );
+
it("ignores a magic without a following hash", () => {
const scanner = new SnapshotHashScanner();
scanner.push(Uint8Array.from([0xf5, 0xf5, 0xdc, 0xdc, 0xff, 0xff, 0xff]));
diff --git a/src/flutter/FlutterPayloadScan.ts b/src/flutter/FlutterPayloadScan.ts
index e1a69d594..a58ee5cc4 100644
--- a/src/flutter/FlutterPayloadScan.ts
+++ b/src/flutter/FlutterPayloadScan.ts
@@ -58,17 +58,16 @@ export class SnapshotHashScanner {
private readonly candidates: string[] = [];
private readonly candidateSet = new Set<string>();
#buffer = new Uint8Array(0);
- /** Absolute stream offset from which unprocessed bytes begin. */
- #base = 0;
- #absoluteScanFrom = 0;
push(chunk: Uint8Array): void {
const joined = new Uint8Array(this.#buffer.length + chunk.length);
joined.set(this.#buffer, 0);
joined.set(chunk, this.#buffer.length);
- // Resume after the last fully processed hash so carried bytes are not
- // counted twice across chunk boundaries.
- let offset = Math.max(0, this.#absoluteScanFrom - this.#base);
+ // Carried bytes are an unprocessed pending magic or too short to hold a
+ // magic, so rescanning them never counts a record twice. Within a chunk
+ // the scan resumes after each magic, so a magic inside a previous
+ // record's span is found wherever the chunk boundaries fall.
+ let offset = 0;
let pendingMagic: number | null = null;
for (;;) {
const magic = findBytes(joined, DART_SNAPSHOT_MAGIC, offset);
@@ -96,21 +95,14 @@ export class SnapshotHashScanner {
// magic record whose field is not lowercase hex is not a source.
if (isValidHash) this.#sources += 1;
offset = magic + DART_SNAPSHOT_MAGIC.length;
- this.#absoluteScanFrom =
- this.#base + magic + SNAPSHOT_HASH_OFFSET + SNAPSHOT_HASH_LENGTH;
}
// Keep a pending magic for the next chunk; otherwise keep only the
// trailing bytes where a magic could still begin, so magic-free
// streams never accumulate.
const keepFrom =
pendingMagic ??
- Math.max(
- this.#absoluteScanFrom - this.#base,
- joined.length - (DART_SNAPSHOT_MAGIC.length - 1),
- );
+ Math.max(0, joined.length - (DART_SNAPSHOT_MAGIC.length - 1));
this.#buffer = joined.slice(keepFrom);
- this.#base += keepFrom;
- if (pendingMagic !== null) this.#absoluteScanFrom = this.#base;
}
result(): SnapshotHashScan {