mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 06:55:55 +00:00
test(flutter): consolidate ELF and snapshot header checks (#13595)
This commit is contained in:
1 parent
a63c373a74
commit
2d65688ac7
2 files changed
+212
-217
No files matched your search
+105
-63
@@ -1,7 +1,11 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { withElf64DirectSectionCount } from "../native/elf/ElfNotes.fixture.js";
|
||||
import { readElfSymbols, readSnapshotHeader } from "./DartElf.js";
|
||||
import {
|
||||
readElfHeader,
|
||||
readElfSymbols,
|
||||
readSnapshotHeader,
|
||||
} from "./DartElf.js";
|
||||
import {
|
||||
REFERENCE_HASH,
|
||||
buildElf32Image,
|
||||
@@ -25,11 +29,12 @@ const readElfSymbol = async (bytes: Buffer, name: string) => {
|
||||
*/
|
||||
const GALLERY_HASH = REFERENCE_HASH;
|
||||
|
||||
const imageWithSections = (): Buffer => {
|
||||
const elf = buildElf64Image([
|
||||
{ name: "_kDartVmSnapshotData", value: 0x400, size: 52 },
|
||||
{ name: "_kDartIsolateSnapshotData", value: 0x800, size: 52 },
|
||||
]);
|
||||
const snapshotSymbols = [
|
||||
{ name: "_kDartVmSnapshotData", value: 0x400, size: 52 },
|
||||
{ name: "_kDartIsolateSnapshotData", value: 0x800, size: 52 },
|
||||
];
|
||||
|
||||
const withSnapshotSections = (elf: Buffer): Buffer => {
|
||||
const section = snapshotSection(GALLERY_HASH);
|
||||
return Buffer.concat([
|
||||
elf,
|
||||
@@ -40,38 +45,30 @@ const imageWithSections = (): Buffer => {
|
||||
]);
|
||||
};
|
||||
|
||||
/** The same layout without section headers: symbols live in PT_DYNAMIC. */
|
||||
const imageWithSections = (): Buffer =>
|
||||
withSnapshotSections(buildElf64Image(snapshotSymbols));
|
||||
|
||||
const strippedImageWithSections = (
|
||||
extraDynamicEntries: ReadonlyArray<readonly [number, bigint]> = [],
|
||||
): Buffer => {
|
||||
const elf = buildStrippedElf64Image(
|
||||
[
|
||||
{ name: "_kDartVmSnapshotData", value: 0x400, size: 52 },
|
||||
{ name: "_kDartIsolateSnapshotData", value: 0x800, size: 52 },
|
||||
],
|
||||
extraDynamicEntries,
|
||||
): Buffer =>
|
||||
withSnapshotSections(
|
||||
buildStrippedElf64Image(snapshotSymbols, extraDynamicEntries),
|
||||
);
|
||||
const section = snapshotSection(GALLERY_HASH);
|
||||
return Buffer.concat([
|
||||
elf,
|
||||
Buffer.alloc(0x400 - elf.length),
|
||||
section,
|
||||
Buffer.alloc(0x800 - 0x400 - section.length),
|
||||
section,
|
||||
]);
|
||||
};
|
||||
|
||||
describe("readElfSymbol", () => {
|
||||
it("locates snapshot symbols in a synthetic ELF64 image", async () => {
|
||||
const image = imageWithSections();
|
||||
const isolateData = await readElfSymbol(image, "_kDartIsolateSnapshotData");
|
||||
if ("failure" in isolateData) throw new Error(isolateData.failure.reason);
|
||||
expect(isolateData.symbol.offset).toBe(0x800);
|
||||
expect(isolateData.symbol.size).toBe(52);
|
||||
const header = readSnapshotHeader(image, isolateData.symbol.offset);
|
||||
expect(header.magicValid).toBe(true);
|
||||
expect(header.kind).toBe(3);
|
||||
});
|
||||
it.each([
|
||||
["section table", imageWithSections],
|
||||
["dynamic segment", strippedImageWithSections],
|
||||
] as const)(
|
||||
"locates an exact ELF64 snapshot range through a %s",
|
||||
async (_, image) => {
|
||||
const name = "_kDartIsolateSnapshotData";
|
||||
expect(await readElfSymbols(image(), [name])).toEqual({
|
||||
symbols: [{ name, offset: 0x800, size: 52 }],
|
||||
failures: [],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("refuses to read snapshot symbols through a reserved direct section count", async () => {
|
||||
const image = withElf64DirectSectionCount(imageWithSections(), 0xff00);
|
||||
@@ -90,17 +87,6 @@ describe("readElfSymbol", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("locates snapshot symbols in a section-stripped ET_DYN image", async () => {
|
||||
const image = strippedImageWithSections();
|
||||
const isolateData = await readElfSymbol(image, "_kDartIsolateSnapshotData");
|
||||
if ("failure" in isolateData) throw new Error(isolateData.failure.reason);
|
||||
expect(isolateData.symbol.offset).toBe(0x800);
|
||||
expect(isolateData.symbol.size).toBe(52);
|
||||
const header = readSnapshotHeader(image, isolateData.symbol.offset);
|
||||
expect(header.magicValid).toBe(true);
|
||||
expect(header.kind).toBe(3);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[16, "DT_SYMBOLIC"],
|
||||
[22, "DT_TEXTREL"],
|
||||
@@ -504,17 +490,68 @@ describe.each(Object.entries(ELF_CLASSES))(
|
||||
},
|
||||
);
|
||||
|
||||
describe("readSnapshotHeader", () => {
|
||||
it("reads a synthesized data header", async () => {
|
||||
const header = readSnapshotHeader(snapshotSection(GALLERY_HASH), 0);
|
||||
expect(header.magicValid).toBe(true);
|
||||
expect(header.kind).toBe(3);
|
||||
it("checks declared ELF table bounds from a prefix of the real image", () => {
|
||||
const original = buildElf64Image([]);
|
||||
const eof = BigInt(original.length);
|
||||
const admission = (bytes: Buffer) =>
|
||||
readElfHeader(bytes.subarray(0, 64), bytes.length);
|
||||
expect(admission(original).header).toMatchObject({
|
||||
e_phoff: 64,
|
||||
e_shoff: 120,
|
||||
});
|
||||
expect(
|
||||
readElfHeader(original.subarray(0, 63), original.length).header,
|
||||
).toBeUndefined();
|
||||
for (const [at, value, reason] of [
|
||||
[0x28, 0n, /section table is missing/],
|
||||
[0x20, eof + 1n, /program header table/],
|
||||
[0x28, eof - 1n, /section header table/],
|
||||
[0x20, 1n, /program header table/],
|
||||
] as const) {
|
||||
const bytes = Buffer.from(original);
|
||||
bytes.writeBigUInt64LE(value, at);
|
||||
expect(admission(bytes).failure?.reason ?? "", `field at ${at}`).toMatch(
|
||||
reason,
|
||||
);
|
||||
}
|
||||
for (const [at, value, reason] of [
|
||||
[0x36, 0, /program header table/],
|
||||
[0x38, 0, /program header table/],
|
||||
[0x3a, 1, /section header table/],
|
||||
[0x3c, 0, /section header table/],
|
||||
] as const) {
|
||||
const bytes = Buffer.from(original);
|
||||
bytes.writeUInt16LE(value, at);
|
||||
expect(admission(bytes).failure?.reason ?? "", `field at ${at}`).toMatch(
|
||||
reason,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("reports invalid magic for offsets outside a complete byte-aligned prefix", () => {
|
||||
it("rejects an unsupported class and admitted big-endian fixed fields", () => {
|
||||
const bytes = buildElf64Image([]);
|
||||
bytes[4] = 3;
|
||||
expect(readElfHeader(bytes).failure?.reason).toMatch(/unsupported ELF class/);
|
||||
bytes[4] = 2;
|
||||
bytes[5] = 2;
|
||||
bytes.writeUInt16BE(3, 16);
|
||||
bytes.writeUInt32BE(1, 20);
|
||||
bytes.writeUInt16BE(64, 52);
|
||||
expect(readElfHeader(bytes).failure?.reason).toMatch(
|
||||
/unsupported ELF byte order/,
|
||||
);
|
||||
});
|
||||
|
||||
describe("readSnapshotHeader", () => {
|
||||
it("cannot read a snapshot with invalid magic or an unusable offset", () => {
|
||||
const bytes = snapshotSection(GALLERY_HASH);
|
||||
for (const offset of [-1, 0.5, bytes.length - 3])
|
||||
expect(readSnapshotHeader(bytes, offset)).toEqual({
|
||||
for (const [buffer, offset] of [
|
||||
[Buffer.alloc(8), 0],
|
||||
[bytes, -1],
|
||||
[bytes, 0.5],
|
||||
[bytes, bytes.length - 3],
|
||||
] as const)
|
||||
expect(readSnapshotHeader(buffer, offset)).toEqual({
|
||||
magicValid: false,
|
||||
kind: null,
|
||||
headerLength: null,
|
||||
@@ -523,6 +560,22 @@ describe("readSnapshotHeader", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([0n, 15n, 47n, 48n, 49n, 1n << 63n])(
|
||||
"retains the observed hash and checks declared length %s",
|
||||
(length) => {
|
||||
const bytes = snapshotSection(GALLERY_HASH, 0);
|
||||
bytes.writeBigUInt64LE(length, 4);
|
||||
expect(readSnapshotHeader(bytes, 0)).toEqual({
|
||||
magicValid: true,
|
||||
kind: 0,
|
||||
headerLength:
|
||||
length > BigInt(Number.MAX_SAFE_INTEGER) ? null : Number(length),
|
||||
hash: GALLERY_HASH,
|
||||
complete: length === 48n,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("retains a backed length when the kind is truncated or unrepresentable", () => {
|
||||
const bytes = snapshotSection(GALLERY_HASH);
|
||||
expect(readSnapshotHeader(bytes.subarray(0, 19), 0)).toEqual({
|
||||
@@ -543,17 +596,6 @@ describe("readSnapshotHeader", () => {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("reports nulls for sections without the data magic", async () => {
|
||||
const header = readSnapshotHeader(Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]), 0);
|
||||
expect(header).toEqual({
|
||||
magicValid: false,
|
||||
kind: null,
|
||||
headerLength: null,
|
||||
hash: null,
|
||||
complete: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
|
||||
@@ -274,27 +274,26 @@ it("retains earlier build IDs when a later note lacks required alignment padding
|
||||
}
|
||||
});
|
||||
|
||||
it.each([0, 1, 2, 4, 0xffff])(
|
||||
"retains snapshot kind %s with partial decoding coverage",
|
||||
async (kind) => {
|
||||
const image = aotImage();
|
||||
image.writeBigUInt64LE(BigInt(kind), 0x1000 + 12);
|
||||
image.writeBigUInt64LE(BigInt(kind), 0x2000 + 12);
|
||||
const execution = await analyzeImage(image, "inspect_dart_aot");
|
||||
const value = flutterResultSchemas.inspect_dart_aot.parse(execution.result);
|
||||
expect(value).toMatchObject({
|
||||
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",
|
||||
libapp: { coverage: "partial" },
|
||||
string_pool: { coverage: "complete" },
|
||||
});
|
||||
expect(
|
||||
value.libapp.sections
|
||||
.filter((section) => section.name.endsWith("Data"))
|
||||
.map((section) => section.kind),
|
||||
).toEqual([kind, kind]);
|
||||
expect(value.libapp.snapshot_hash).toBe(REFERENCE_HASH);
|
||||
},
|
||||
);
|
||||
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",
|
||||
@@ -780,124 +779,82 @@ it("cancels before creating any snapshot root", async () => {
|
||||
await provider.close();
|
||||
});
|
||||
|
||||
it.each([1, 20, 47, 48, 61])(
|
||||
"identifies a valid ELF when ZIP chunks are only %i bytes",
|
||||
async (chunkBytes) => {
|
||||
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);
|
||||
return Readable.from(
|
||||
(async function* () {
|
||||
for await (const chunk of source) {
|
||||
const bytes = chunk as Buffer;
|
||||
for (let offset = 0; offset < bytes.length; offset += chunkBytes)
|
||||
yield bytes.subarray(offset, offset + chunkBytes);
|
||||
}
|
||||
})(),
|
||||
);
|
||||
});
|
||||
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("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.each([
|
||||
{
|
||||
name: "zeroed offsets",
|
||||
corrupt: (image: Buffer) => image.fill(0, 0x20, 0x30),
|
||||
},
|
||||
{
|
||||
name: "program table past EOF",
|
||||
corrupt: (image: Buffer) =>
|
||||
image.writeBigUInt64LE(BigInt(image.length + 1), 0x20),
|
||||
},
|
||||
{
|
||||
name: "section table past EOF",
|
||||
corrupt: (image: Buffer) =>
|
||||
image.writeBigUInt64LE(BigInt(image.length - 1), 0x28),
|
||||
},
|
||||
{
|
||||
name: "zero program entry width",
|
||||
corrupt: (image: Buffer) => image.writeUInt16LE(0, 0x36),
|
||||
},
|
||||
{
|
||||
name: "short section entry width",
|
||||
corrupt: (image: Buffer) => image.writeUInt16LE(1, 0x3a),
|
||||
},
|
||||
{
|
||||
name: "absent program table",
|
||||
corrupt: (image: Buffer) => image.writeUInt16LE(0, 0x38),
|
||||
},
|
||||
{
|
||||
name: "unsupported class",
|
||||
corrupt: (image: Buffer) => image.writeUInt8(3, 4),
|
||||
},
|
||||
{
|
||||
name: "unsupported byte order",
|
||||
corrupt: (image: Buffer) => image.writeUInt8(2, 5),
|
||||
},
|
||||
{
|
||||
name: "unsafe table offset",
|
||||
corrupt: (image: Buffer) => image.writeBigUInt64LE(1n << 63n, 0x20),
|
||||
},
|
||||
{
|
||||
name: "table inside header",
|
||||
corrupt: (image: Buffer) => image.writeBigUInt64LE(1n, 0x20),
|
||||
},
|
||||
{
|
||||
name: "extended section count",
|
||||
corrupt: (image: Buffer) => image.writeUInt16LE(0, 0x3c),
|
||||
},
|
||||
])(
|
||||
"retains hash observations with partial coverage for $name",
|
||||
async ({ corrupt }) => {
|
||||
const image = aotImage();
|
||||
corrupt(image);
|
||||
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("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.each([0, 4, 20, 47, 61])(
|
||||
"reports a %i-byte ELF prefix as partial without throwing",
|
||||
async (length) => {
|
||||
const execution = await analyzeImage(
|
||||
aotImage().subarray(0, length),
|
||||
"identify_flutter_build",
|
||||
);
|
||||
const value = flutterResultSchemas.identify_flutter_build.parse(
|
||||
execution.result,
|
||||
);
|
||||
expect(value.coverage).toBe("partial");
|
||||
expect(value.abis[0]?.libapp.elf_header_valid).toBe(false);
|
||||
},
|
||||
);
|
||||
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);
|
||||
@@ -924,25 +881,21 @@ it("accepts a section-stripped ELF32 header and bounds its program table", async
|
||||
).toBe("partial");
|
||||
});
|
||||
|
||||
it.each([0n, 15n, 47n, 49n, 1n << 63n])(
|
||||
"retains malformed snapshot length %s as partial evidence",
|
||||
async (length) => {
|
||||
const image = aotImage();
|
||||
image.writeBigUInt64LE(length, 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:
|
||||
length > BigInt(Number.MAX_SAFE_INTEGER) ? null : Number(length),
|
||||
});
|
||||
expect(execution.limitations.join("\n")).toContain(
|
||||
"_kDartVmSnapshotData has an incomplete or invalid snapshot header",
|
||||
);
|
||||
},
|
||||
);
|
||||
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();
|
||||
|
||||
Reference in new issue
Block a user