diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index ee39fdd731..b4e9e0b53d 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -25,7 +25,6 @@ "src/domain/elfSectionHeader.ts": 6, "src/domain/elfSymbol.ts": 8, "src/domain/gamecubeDol.ts": 6, - "src/domain/il2cpp/il2cppMetadataHeaderPrefix.ts": 3, "src/domain/inesContainer.ts": 5, "src/domain/managed/managedCliHeaderPrefix.ts": 5, "src/domain/managed/managedMemberPointers.ts": 2, @@ -54,10 +53,9 @@ "src/hopper/HopperAnalysisProfile.ts": 1, "src/il2cpp/Il2cppBinaryImage.ts": 2, "src/il2cpp/Il2cppBinaryRegistration.ts": 8, - "src/il2cpp/Il2cppMetadataFile.ts": 24, + "src/il2cpp/Il2cppMetadataFile.ts": 21, "src/il2cpp/Il2cppMetadataFormat.ts": 3, "src/il2cpp/Il2cppMetadataMembers.ts": 5, - "src/il2cpp/Il2cppMetadataV108.ts": 3, "src/native/AppleDispatchMetadata.ts": 4, "src/native/AppleObjcClassImportBindings.ts": 1, "src/native/AppleObjcDispatchFacets.ts": 1, diff --git a/src/domain/il2cpp/il2cppMetadataHeaderPrefix.ts b/src/domain/il2cpp/il2cppMetadataHeaderPrefix.ts index 6bc7f28ffb..37a634fd17 100644 --- a/src/domain/il2cpp/il2cppMetadataHeaderPrefix.ts +++ b/src/domain/il2cpp/il2cppMetadataHeaderPrefix.ts @@ -1,5 +1,8 @@ +import { BinaryReader } from "../binaryContainer.js"; import { IL2CPP_METADATA_MAGIC } from "./il2cppMetadata.js"; +export const IL2CPP_METADATA_PREFIX_BYTES = 8; + /** Parsed identity facts available before the complete IL2CPP header exists. */ export type Il2cppMetadataHeaderPrefix = | { @@ -34,8 +37,14 @@ export const observeIl2cppMetadataHeaderPrefix = ( rawVersion: null, version: null, }; - const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); - const magic = view.getUint32(0, true); + const reader = new BinaryReader( + Buffer.isBuffer(bytes) + ? bytes + : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength), + true, + "il2cpp-metadata", + ); + const magic = reader.u32(0); if (magic !== IL2CPP_METADATA_MAGIC) return { kind: "other-magic", @@ -43,17 +52,18 @@ export const observeIl2cppMetadataHeaderPrefix = ( rawVersion: null, version: null, }; - if (bytes.byteLength < 8) + if (bytes.byteLength < IL2CPP_METADATA_PREFIX_BYTES) return { kind: "il2cpp", magic: IL2CPP_METADATA_MAGIC, rawVersion: null, version: null, }; + const rawVersion = reader.u32(4); return { kind: "il2cpp", magic: IL2CPP_METADATA_MAGIC, - rawVersion: view.getUint32(4, true), - version: view.getInt32(4, true), + rawVersion, + version: rawVersion | 0, }; }; diff --git a/src/il2cpp/Il2cppMetadataFile.ts b/src/il2cpp/Il2cppMetadataFile.ts index 060dc1cb31..ba48f55279 100644 --- a/src/il2cpp/Il2cppMetadataFile.ts +++ b/src/il2cpp/Il2cppMetadataFile.ts @@ -12,7 +12,14 @@ import { type Il2cppMetadataSection, type IL2cppMetadataSectionName, } from "../domain/il2cpp/il2cppMetadata.js"; -import { observeIl2cppMetadataHeaderPrefix } from "../domain/il2cpp/il2cppMetadataHeaderPrefix.js"; +import { + IL2CPP_METADATA_PREFIX_BYTES, + observeIl2cppMetadataHeaderPrefix, +} from "../domain/il2cpp/il2cppMetadataHeaderPrefix.js"; +import { + firstIl2cppSectionIssue, + readIl2cppSectionTable, +} from "./Il2cppMetadataSectionTable.js"; import { firstDeclaringTypeCycle } from "../domain/il2cpp/il2cppDeclaringTypes.js"; import { readIl2cppMembers, @@ -86,7 +93,7 @@ const sectionEntryBytes = (layout: VersionLayout) => /** Magic, version and one entry per section. */ export const il2cppMetadataHeaderBytes = (version: Il2cppMetadataVersion) => - 8 + + IL2CPP_METADATA_PREFIX_BYTES + il2cppMetadataSectionNames(version).length * sectionEntryBytes(VERSION_LAYOUTS[version]); @@ -158,17 +165,11 @@ const readHeader = (bytes: Buffer) => { `the file has ${bytes.length} bytes, fewer than the ${headerBytes}-byte version ${version} header`, ); const entryBytes = sectionEntryBytes(VERSION_LAYOUTS[version]); - const sections: Il2cppMetadataSection[] = il2cppMetadataSectionNames( - version, - ).map((name, index) => { - const at = 8 + index * entryBytes; - return { - name, - offset: bytes.readInt32LE(at), - size: bytes.readInt32LE(at + 4), - count: entryBytes === 12 ? bytes.readInt32LE(at + 8) : null, - }; - }); + const sections = readIl2cppSectionTable( + bytes, + il2cppMetadataSectionNames(version), + entryBytes, + ); return { magic, version, headerBytes, sections } as const; }; @@ -183,35 +184,22 @@ const checkSections = ( "unexpected-header-size", `stringLiterals starts at ${String(first?.offset)}, not immediately after the ${headerBytes}-byte header`, ); - for (const section of sections) - if ( - section.offset < 0 || - section.size < 0 || - (section.count ?? 0) < 0 || - section.offset + section.size > bytes.length - ) - throw new Il2cppMetadataFormatFailure( - "section-out-of-bounds", - `section ${section.name} (offset ${section.offset}, size ${section.size}, count ${String(section.count)}) is negative or extends past the ${bytes.length}-byte file`, - ); - else if (section.size > 0 && section.offset < headerBytes) - throw new Il2cppMetadataFormatFailure( - "section-out-of-bounds", - `section ${section.name} starts at ${section.offset}, inside the ${headerBytes}-byte header`, - ); - // Empty sections may share an offset with any neighbor; gaps are padding. - const occupied = sections - .filter((section) => section.size > 0) - .sort((left, right) => left.offset - right.offset); - for (let index = 1; index < occupied.length; index += 1) { - const previous = occupied[index - 1]!; - const current = occupied[index]!; - if (previous.offset + previous.size > current.offset) - throw new Il2cppMetadataFormatFailure( - "section-overlap", - `section ${previous.name} [${previous.offset}, ${previous.offset + previous.size}) overlaps ${current.name} starting at ${current.offset}`, - ); + const issue = firstIl2cppSectionIssue(sections, headerBytes, bytes.length); + if (issue === undefined) return; + if (issue.kind === "overlap") { + const { previous, current } = issue; + throw new Il2cppMetadataFormatFailure( + "section-overlap", + `section ${previous.name} [${previous.offset}, ${previous.offset + previous.size}) overlaps ${current.name} starting at ${current.offset}`, + ); } + const { section } = issue; + throw new Il2cppMetadataFormatFailure( + "section-out-of-bounds", + issue.kind === "header" + ? `section ${section.name} starts at ${section.offset}, inside the ${headerBytes}-byte header` + : `section ${section.name} (offset ${section.offset}, size ${section.size}, count ${String(section.count)}) is negative or extends past the ${bytes.length}-byte file`, + ); }; /** A counted header's entry count; fixed headers record none. */ diff --git a/src/il2cpp/Il2cppMetadataSectionTable.ts b/src/il2cpp/Il2cppMetadataSectionTable.ts new file mode 100644 index 0000000000..c83cc0b3eb --- /dev/null +++ b/src/il2cpp/Il2cppMetadataSectionTable.ts @@ -0,0 +1,76 @@ +import { BinaryReader } from "../domain/binaryContainer.js"; +import { IL2CPP_METADATA_PREFIX_BYTES } from "../domain/il2cpp/il2cppMetadataHeaderPrefix.js"; +import { IL2CPP_METADATA_FORMAT } from "./Il2cppMetadataFormat.js"; + +interface SectionDeclaration { + readonly name: Name; + readonly offset: number; + readonly size: number; + readonly count: number | null; +} + +/** Section table framing, after the owner of the header layout admits its extent. */ +export function readIl2cppSectionTable( + bytes: Buffer, + names: readonly Name[], + entryBytes: 12, +): Array & { readonly count: number }>; +export function readIl2cppSectionTable( + bytes: Buffer, + names: readonly Name[], + entryBytes: 8 | 12, +): Array>; +export function readIl2cppSectionTable( + bytes: Buffer, + names: readonly Name[], + entryBytes: 8 | 12, +): Array> { + const reader = new BinaryReader(bytes, true, IL2CPP_METADATA_FORMAT); + return names.map((name, index) => { + const at = IL2CPP_METADATA_PREFIX_BYTES + index * entryBytes; + return { + name, + offset: reader.i32(at), + size: reader.i32(at + 4), + count: entryBytes === 12 ? reader.i32(at + 8) : null, + }; + }); +} + +type SectionIssue = + | { readonly kind: "range" | "header"; readonly section: SectionDeclaration } + | { + readonly kind: "overlap"; + readonly previous: SectionDeclaration; + readonly current: SectionDeclaration; + }; + +/** Shared geometry for counted and fixed tables. Layout-specific rules stay with their callers. */ +export const firstIl2cppSectionIssue = ( + sections: readonly SectionDeclaration[], + headerBytes: number, + fileBytes: number, +): SectionIssue | undefined => { + for (const section of sections) { + if ( + section.offset < 0 || + section.size < 0 || + (section.count ?? 0) < 0 || + section.offset + section.size > fileBytes + ) + return { kind: "range", section }; + if (section.size > 0 && section.offset < headerBytes) + return { kind: "header", section }; + } + // Empty sections may share offsets with their neighbors. + const occupied = sections + .filter((section) => section.size > 0) + .sort((a, b) => a.offset - b.offset); + for (let index = 1; index < occupied.length; index++) { + const previous = occupied[index - 1]!; + const current = occupied[index]!; + if (previous.offset + previous.size > current.offset) + return { kind: "overlap", previous, current }; + } + return undefined; +}; diff --git a/src/il2cpp/Il2cppMetadataV108.ts b/src/il2cpp/Il2cppMetadataV108.ts index 7e8d5eeaaf..2b245d14e8 100644 --- a/src/il2cpp/Il2cppMetadataV108.ts +++ b/src/il2cpp/Il2cppMetadataV108.ts @@ -1,6 +1,13 @@ import { strictUtf8Decoder } from "../domain/strictUtf8.js"; import { IL2CPP_METADATA_SECTION_NAMES } from "../domain/il2cpp/il2cppMetadata.js"; -import { observeIl2cppMetadataHeaderPrefix } from "../domain/il2cpp/il2cppMetadataHeaderPrefix.js"; +import { + IL2CPP_METADATA_PREFIX_BYTES, + observeIl2cppMetadataHeaderPrefix, +} from "../domain/il2cpp/il2cppMetadataHeaderPrefix.js"; +import { + firstIl2cppSectionIssue, + readIl2cppSectionTable, +} from "./Il2cppMetadataSectionTable.js"; import { Il2cppMetadataFormatFailure } from "./Il2cppMetadataFormat.js"; // Header order follows LibCpp2IL's Il2CppGlobalMetadataHeader (v108): @@ -20,7 +27,7 @@ const SECTION_NAMES = [ "rgctxValues", "staticConstructorTypeIndices", ] as const; -const HEADER_BYTES = 8 + SECTION_NAMES.length * 12; +const HEADER_BYTES = IL2CPP_METADATA_PREFIX_BYTES + SECTION_NAMES.length * 12; const SAMPLE_STRINGS = 16; const SAMPLE_BYTES = 128; const SCAN_BATCH_BYTES = 64 * 1024; @@ -38,39 +45,17 @@ export const inspectIl2cppMetadataV108 = async ( "unsupported-version", `version 108 layout is unverified; the file has ${bytes.length} bytes, fewer than the assumed ${HEADER_BYTES}-byte header`, ); - const sections = SECTION_NAMES.map((name, index) => { - const at = 8 + index * 12; - return { - name, - offset: bytes.readInt32LE(at), - size: bytes.readInt32LE(at + 4), - count: bytes.readInt32LE(at + 8), - }; - }); - for (const section of sections) { - if ( - section.offset < 0 || - section.size < 0 || - section.count < 0 || - section.offset + section.size > bytes.length || - (section.size > 0 && section.offset < HEADER_BYTES) - ) - throw new Il2cppMetadataFormatFailure( - "unsupported-version", - `version 108 layout is unverified; assumed section ${section.name} (offset ${section.offset}, size ${section.size}, count ${section.count}) is invalid for the ${bytes.length}-byte file`, - ); - } - const occupied = sections - .filter((section) => section.size > 0) - .sort((a, b) => a.offset - b.offset); - for (let index = 1; index < occupied.length; index++) { - const previous = occupied[index - 1]!; - const current = occupied[index]!; - if (previous.offset + previous.size > current.offset) - throw new Il2cppMetadataFormatFailure( - "unsupported-version", - `version 108 layout is unverified; assumed sections ${previous.name} and ${current.name} overlap at ${current.offset}`, - ); + const sections = readIl2cppSectionTable(bytes, SECTION_NAMES, 12); + const issue = firstIl2cppSectionIssue(sections, HEADER_BYTES, bytes.length); + if (issue !== undefined) { + const detail = + issue.kind === "overlap" + ? `assumed sections ${issue.previous.name} and ${issue.current.name} overlap at ${issue.current.offset}` + : `assumed section ${issue.section.name} (offset ${issue.section.offset}, size ${issue.section.size}, count ${String(issue.section.count)}) is invalid for the ${bytes.length}-byte file`; + throw new Il2cppMetadataFormatFailure( + "unsupported-version", + `version 108 layout is unverified; ${detail}`, + ); } const strings = sections[2]!; const end = strings.offset + strings.size; diff --git a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts index f4c8046b5a..1a36c77c53 100644 --- a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts +++ b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts @@ -149,23 +149,53 @@ it("accepts padding after the assumed v108 header", async () => { }); }); -it("retains source evidence when a v108 section conflicts with the unverified layout", async () => { - const root = await createTestTempDirectory("rea-il2cpp-v108-invalid-"); - const path = join(root, "global-metadata.dat"); - const bytes = version108Header(Buffer.from("Name\0")); - bytes.writeInt32LE(bytes.length + 1, 12 + 2 * 12); - await writeFile(path, bytes); - const result = await inspectMetadata(path); - expect(result.ok).toBe(false); - if (result.ok) return; - expect(result.error).toBeInstanceOf(AnalysisUnsupportedTargetError); - expect(result.error.partialObservation).toMatchObject({ - normalized_result: { artifact: { path, bytes: bytes.length } }, - }); - expect(JSON.stringify(result.error.partialObservation)).not.toContain( - "version_108_partial_inventory", - ); -}); +it.each([ + [ + "extends past the file", + (bytes: Buffer) => { + bytes.writeInt32LE(bytes.length + 1, 12 + 2 * 12); + }, + /assumed section strings .* is invalid/, + ], + [ + "starts inside the header", + (bytes: Buffer) => { + bytes.writeInt32LE(499, 8 + 2 * 12); + }, + /assumed section strings .* is invalid/, + ], + [ + "overlaps another section", + (bytes: Buffer) => { + bytes.writeInt32LE(501, 8 + 3 * 12); + bytes.writeInt32LE(1, 12 + 3 * 12); + }, + /assumed sections strings and events overlap at 501/, + ], +])( + "retains source evidence when a v108 section %s", + async (_name, mutate, reason) => { + const root = await createTestTempDirectory("rea-il2cpp-v108-invalid-"); + const path = join(root, "global-metadata.dat"); + const bytes = version108Header(Buffer.from("Name\0")); + mutate(bytes); + await expect(inspectIl2cppMetadataV108(bytes)).rejects.toMatchObject({ + reason: "unsupported-version", + message: expect.stringMatching(reason), + }); + await writeFile(path, bytes); + const result = await inspectMetadata(path); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.error).toBeInstanceOf(AnalysisUnsupportedTargetError); + expect(result.error.partialObservation).toMatchObject({ + normalized_result: { artifact: { path, bytes: bytes.length } }, + }); + expect(JSON.stringify(result.error.partialObservation)).not.toContain( + "version_108_partial_inventory", + ); + }, +); it("bounds v108 string samples and preserves undecodable bytes and leading BOMs", async () => { const repeated = await inspectIl2cppMetadataV108(