mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
refactor(il2cpp): decode metadata tables through one shared binary reader (#12554)
This commit is contained in:
5 files changed
+72
-52
No files matched your search
@@ -17,9 +17,6 @@
|
||||
"src/ghidra/extensions/GhidraExtensions.ts": 1,
|
||||
"src/il2cpp/Il2cppBinaryImage.ts": 2,
|
||||
"src/il2cpp/Il2cppBinaryRegistration.ts": 8,
|
||||
"src/il2cpp/Il2cppMetadataFile.ts": 21,
|
||||
"src/il2cpp/Il2cppMetadataFormat.ts": 3,
|
||||
"src/il2cpp/Il2cppMetadataMembers.ts": 5,
|
||||
"src/native/AppleDispatchMetadata.ts": 4,
|
||||
"src/native/AppleObjcDispatchFacets.ts": 1,
|
||||
"src/native/AppleSwiftDispatchFacets.ts": 1,
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { BinaryReader } from "../domain/binaryContainer.js";
|
||||
import { strictUtf8Decoder } from "../domain/strictUtf8.js";
|
||||
import {
|
||||
checkRange,
|
||||
checkedSectionCount,
|
||||
readIndex,
|
||||
Il2cppMetadataFormatFailure,
|
||||
IL2CPP_METADATA_FORMAT,
|
||||
} from "./Il2cppMetadataFormat.js";
|
||||
import {
|
||||
IL2CPP_METADATA_VERSIONS,
|
||||
@@ -364,7 +366,7 @@ const createStringReader = (
|
||||
|
||||
/** Checked header facts shared by the record readers. */
|
||||
export interface MetadataTables {
|
||||
readonly bytes: Buffer;
|
||||
readonly reader: BinaryReader;
|
||||
readonly section: (name: SectionName) => Il2cppMetadataSection;
|
||||
readonly widths: Il2cppMetadataDecoded["index_widths"];
|
||||
readonly recordBytes: ReturnType<typeof il2cppMetadataRecordBytes>;
|
||||
@@ -380,7 +382,7 @@ export interface MetadataTables {
|
||||
}
|
||||
|
||||
function* readImages({
|
||||
bytes,
|
||||
reader,
|
||||
section,
|
||||
widths,
|
||||
recordBytes: sizes,
|
||||
@@ -396,9 +398,9 @@ function* readImages({
|
||||
if (index % 256 === 0) yield;
|
||||
const at = images.offset + index * sizes.images;
|
||||
const image = {
|
||||
name: readString(bytes.readInt32LE(at), `image ${index} name`),
|
||||
type_start: readIndex(bytes, at + 8, width),
|
||||
type_count: bytes.readUInt32LE(at + 8 + width),
|
||||
name: readString(reader.i32(at), `image ${index} name`),
|
||||
type_start: readIndex(reader, at + 8, width),
|
||||
type_count: reader.u32(at + 8 + width),
|
||||
};
|
||||
checkRange(
|
||||
`image ${index} types`,
|
||||
@@ -414,7 +416,7 @@ function* readImages({
|
||||
|
||||
/** Record each type's enclosing type from its declaring type's nestedTypes range. */
|
||||
function* assignNesting(
|
||||
{ bytes, section, counts }: MetadataTables,
|
||||
{ reader, section, counts }: MetadataTables,
|
||||
types: Il2cppMetadataDecoded["types"],
|
||||
) {
|
||||
let work = 0;
|
||||
@@ -429,7 +431,7 @@ function* assignNesting(
|
||||
);
|
||||
for (let offset = 0; offset < type.nested_type_count; offset += 1) {
|
||||
if (work++ % 256 === 0) yield;
|
||||
const nested = bytes.readInt32LE(
|
||||
const nested = reader.i32(
|
||||
nestedTypes.offset +
|
||||
(type.nested_type_start + offset) * NESTED_TYPE_ENTRY_BYTES,
|
||||
);
|
||||
@@ -476,7 +478,7 @@ function* readTypes(tables: MetadataTables): Generator<
|
||||
};
|
||||
}
|
||||
> {
|
||||
const { bytes, section, widths, recordBytes: sizes, counts } = tables;
|
||||
const { reader, section, widths, recordBytes: sizes, counts } = tables;
|
||||
const { readString, typeDefinitionTypeIndices } = tables;
|
||||
const typeDefinitions = section("typeDefinitions");
|
||||
const methodTotal = counts.methods;
|
||||
@@ -523,15 +525,12 @@ function* readTypes(tables: MetadataTables): Generator<
|
||||
}
|
||||
const countsAt = starts + 32;
|
||||
const type = {
|
||||
name: readString(bytes.readInt32LE(at), `type ${index} name`),
|
||||
namespace: readString(
|
||||
bytes.readInt32LE(at + 4),
|
||||
`type ${index} namespace`,
|
||||
),
|
||||
method_start: bytes.readInt32LE(starts + 4),
|
||||
method_count: bytes.readUInt16LE(countsAt),
|
||||
nested_type_start: bytes.readInt32LE(starts + 16),
|
||||
nested_type_count: bytes.readUInt16LE(countsAt + 8),
|
||||
name: readString(reader.i32(at), `type ${index} name`),
|
||||
namespace: readString(reader.i32(at + 4), `type ${index} namespace`),
|
||||
method_start: reader.i32(starts + 4),
|
||||
method_count: reader.u16(countsAt),
|
||||
nested_type_start: reader.i32(starts + 16),
|
||||
nested_type_count: reader.u16(countsAt + 8),
|
||||
declaring_type: -1,
|
||||
};
|
||||
checkRange(
|
||||
@@ -541,16 +540,16 @@ function* readTypes(tables: MetadataTables): Generator<
|
||||
methodTotal,
|
||||
);
|
||||
memberRanges.fields.push({
|
||||
start: bytes.readInt32LE(starts),
|
||||
count: bytes.readUInt16LE(countsAt + 4),
|
||||
start: reader.i32(starts),
|
||||
count: reader.u16(countsAt + 4),
|
||||
});
|
||||
memberRanges.events.push({
|
||||
start: bytes.readInt32LE(starts + 8),
|
||||
count: bytes.readUInt16LE(countsAt + 6),
|
||||
start: reader.i32(starts + 8),
|
||||
count: reader.u16(countsAt + 6),
|
||||
});
|
||||
memberRanges.properties.push({
|
||||
start: bytes.readInt32LE(starts + 12),
|
||||
count: bytes.readUInt16LE(countsAt + 2),
|
||||
start: reader.i32(starts + 12),
|
||||
count: reader.u16(countsAt + 2),
|
||||
});
|
||||
tables.onRow?.(type);
|
||||
types.push(type);
|
||||
@@ -560,7 +559,7 @@ function* readTypes(tables: MetadataTables): Generator<
|
||||
}
|
||||
|
||||
function* readMethods({
|
||||
bytes,
|
||||
reader,
|
||||
section,
|
||||
widths,
|
||||
recordBytes: sizes,
|
||||
@@ -584,8 +583,8 @@ function* readMethods({
|
||||
if (index % 256 === 0) yield;
|
||||
const at = methods.offset + index * sizes.methods;
|
||||
const method = {
|
||||
name: readString(bytes.readInt32LE(at), `method ${index} name`),
|
||||
declaring_type: readIndex(bytes, at + 4, widths.type_definition),
|
||||
name: readString(reader.i32(at), `method ${index} name`),
|
||||
declaring_type: readIndex(reader, at + 4, widths.type_definition),
|
||||
};
|
||||
if (method.declaring_type < -1 || method.declaring_type >= typeTotal)
|
||||
throw new Il2cppMetadataFormatFailure(
|
||||
@@ -594,8 +593,8 @@ function* readMethods({
|
||||
);
|
||||
if (linkFacts !== undefined) {
|
||||
const returnAt = at + 4 + widths.type_definition;
|
||||
linkFacts.returnTypes[index] = readIndex(bytes, returnAt, widths.type);
|
||||
linkFacts.methodTokens[index] = bytes.readUInt32LE(
|
||||
linkFacts.returnTypes[index] = readIndex(reader, returnAt, widths.type);
|
||||
linkFacts.methodTokens[index] = reader.u32(
|
||||
at +
|
||||
methodContainerAt({ widths, methodReturnParameterToken }) +
|
||||
widths.generic_container,
|
||||
@@ -603,7 +602,7 @@ function* readMethods({
|
||||
}
|
||||
parameterRanges.push({
|
||||
start: readIndex(
|
||||
bytes,
|
||||
reader,
|
||||
at +
|
||||
4 +
|
||||
widths.type_definition +
|
||||
@@ -611,7 +610,7 @@ function* readMethods({
|
||||
(methodReturnParameterToken ? 4 : 0),
|
||||
widths.parameter,
|
||||
),
|
||||
count: bytes.readUInt16LE(at + sizes.methods - 2),
|
||||
count: reader.u16(at + sizes.methods - 2),
|
||||
});
|
||||
onRow?.(method);
|
||||
result.push(method);
|
||||
@@ -751,7 +750,7 @@ const methodContainerAt = ({
|
||||
/** Raw row interpretation; callers own index and layout admission. */
|
||||
const readTypeGenericContainer = (tables: MetadataTables, index: number) =>
|
||||
readIndex(
|
||||
tables.bytes,
|
||||
tables.reader,
|
||||
tables.section("typeDefinitions").offset +
|
||||
index * tables.recordBytes.typeDefinitions +
|
||||
8 +
|
||||
@@ -765,8 +764,8 @@ const readGenericContainer = (tables: MetadataTables, index: number) => {
|
||||
tables.section("genericContainers").offset +
|
||||
index * GENERIC_CONTAINER_RECORD_BYTES;
|
||||
return {
|
||||
count: tables.bytes.readInt32LE(at + 4),
|
||||
parameterStart: tables.bytes.readInt32LE(at + 12),
|
||||
count: tables.reader.i32(at + 4),
|
||||
parameterStart: tables.reader.i32(at + 12),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -786,7 +785,7 @@ const createGenericContainerReaders = (tables: MetadataTables) => {
|
||||
!inRange(index, methodCount)
|
||||
? undefined
|
||||
: readIndex(
|
||||
tables.bytes,
|
||||
tables.reader,
|
||||
methods.offset + index * methodBytes + methodAt,
|
||||
tables.widths.generic_container,
|
||||
),
|
||||
@@ -810,7 +809,7 @@ const createGenericParameterReader = (tables: MetadataTables) => {
|
||||
if (!Number.isInteger(index) || index < 0 || index >= count)
|
||||
return undefined;
|
||||
return tables.readString(
|
||||
tables.bytes.readInt32LE(
|
||||
tables.reader.i32(
|
||||
parameters.offset + index * width + tables.widths.generic_container,
|
||||
),
|
||||
`generic parameter ${index} name`,
|
||||
@@ -869,7 +868,7 @@ function* readMetadataCoreSteps(
|
||||
const widths = counted?.widths ?? FIXED_WIDTHS;
|
||||
const expected = il2cppMetadataRecordBytes(widths, version);
|
||||
let tables: MetadataTables = {
|
||||
bytes,
|
||||
reader: new BinaryReader(bytes, true, IL2CPP_METADATA_FORMAT),
|
||||
section,
|
||||
widths,
|
||||
recordBytes: expected,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { BinaryReader } from "../domain/binaryContainer.js";
|
||||
/** Stable machine-readable reason for each rejected input. */
|
||||
export const IL2CPP_METADATA_FAILURE_REASONS = {
|
||||
unsupported: ["unrecognized-magic", "unsupported-version"],
|
||||
@@ -93,16 +94,20 @@ export const checkRange = (
|
||||
};
|
||||
|
||||
/** Read a serialized index; the all-ones value of narrow widths means -1. */
|
||||
export const readIndex = (bytes: Buffer, offset: number, width: 1 | 2 | 4) => {
|
||||
export const readIndex = (
|
||||
reader: BinaryReader,
|
||||
offset: number,
|
||||
width: 1 | 2 | 4,
|
||||
) => {
|
||||
if (width === 1) {
|
||||
const value = bytes.readUInt8(offset);
|
||||
const value = reader.u8(offset);
|
||||
return value === 0xff ? -1 : value;
|
||||
}
|
||||
if (width === 2) {
|
||||
const value = bytes.readUInt16LE(offset);
|
||||
const value = reader.u16(offset);
|
||||
return value === 0xffff ? -1 : value;
|
||||
}
|
||||
return bytes.readInt32LE(offset);
|
||||
return reader.i32(offset);
|
||||
};
|
||||
import type { Il2cppMetadataSection } from "../domain/il2cpp/il2cppMetadata.js";
|
||||
import {
|
||||
|
||||
@@ -109,7 +109,7 @@ function* readNamed<
|
||||
if (index % 256 === 0) yield;
|
||||
const row = makeRow(
|
||||
tables.readString(
|
||||
tables.bytes.readInt32LE(
|
||||
tables.reader.i32(
|
||||
section.offset + index * tables.members.recordBytes[name],
|
||||
),
|
||||
`${name} ${index} name`,
|
||||
@@ -125,7 +125,7 @@ function* readNamed<
|
||||
? tables.linkFacts.fieldTypes
|
||||
: tables.linkFacts.parameterTypes;
|
||||
column[index] = readIndex(
|
||||
tables.bytes,
|
||||
tables.reader,
|
||||
section.offset +
|
||||
index * tables.members.recordBytes[name] +
|
||||
(name === "fields" ? 4 : 8),
|
||||
@@ -141,7 +141,7 @@ function* readNamed<
|
||||
function* readStringLiterals(
|
||||
tables: MetadataTables,
|
||||
): Generator<void, Members["string_literals"]> {
|
||||
const { bytes, section, indexing, members } = tables;
|
||||
const { reader, section, indexing, members } = tables;
|
||||
const literals = section("stringLiterals"),
|
||||
data = section("stringLiteralData");
|
||||
const decoder = strictUtf8Decoder("preserve");
|
||||
@@ -149,7 +149,7 @@ function* readStringLiterals(
|
||||
const decoded = new Map<string, Members["string_literals"][number]>();
|
||||
const rows: Members["string_literals"] = [];
|
||||
if (indexing === "counted" && members.literalRecords === 1) {
|
||||
const sentinel = bytes.readInt32LE(literals.offset);
|
||||
const sentinel = reader.i32(literals.offset);
|
||||
if (sentinel < 0 || sentinel > data.size)
|
||||
throw new Il2cppMetadataFormatFailure(
|
||||
"index-out-of-range",
|
||||
@@ -159,11 +159,9 @@ function* readStringLiterals(
|
||||
for (let index = 0; index < members.counts.string_literals; index++) {
|
||||
if (index % 256 === 0) yield;
|
||||
const at = literals.offset + index * members.recordBytes.stringLiterals;
|
||||
const start = bytes.readInt32LE(at + (indexing === "counted" ? 0 : 4));
|
||||
const start = reader.i32(at + (indexing === "counted" ? 0 : 4));
|
||||
const length =
|
||||
indexing === "counted"
|
||||
? bytes.readInt32LE(at + 4) - start
|
||||
: bytes.readUInt32LE(at);
|
||||
indexing === "counted" ? reader.i32(at + 4) - start : reader.u32(at);
|
||||
if (start < 0 || length < 0 || start + length > data.size)
|
||||
throw new Il2cppMetadataFormatFailure(
|
||||
"index-out-of-range",
|
||||
@@ -175,7 +173,7 @@ function* readStringLiterals(
|
||||
// Strict UTF-8 round trips preserve at least these bytes; base64 is larger.
|
||||
// Refuse impossible output before allocating either representation.
|
||||
tables.beforeText?.(length);
|
||||
const value = bytes.subarray(
|
||||
const value = reader.bytes.subarray(
|
||||
data.offset + start,
|
||||
data.offset + start + length,
|
||||
);
|
||||
|
||||
@@ -890,3 +890,24 @@ describe("IL2CPP global-metadata version 24", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps unsigned legacy literal lengths distinct from signed counted offsets", () => {
|
||||
const legacy = mutated(
|
||||
(fixture, bytes) => {
|
||||
bytes.writeUInt32LE(0xffffffff, fixture.section("stringLiterals").offset);
|
||||
},
|
||||
createIl2cppMetadataFixture({ version: 29 }),
|
||||
);
|
||||
expect(failure(legacy)).toMatchObject({
|
||||
reason: "index-out-of-range",
|
||||
message: expect.stringContaining("4294967295"),
|
||||
});
|
||||
|
||||
const counted = mutated((fixture, bytes) => {
|
||||
bytes.writeInt32LE(-1, fixture.section("stringLiterals").offset);
|
||||
});
|
||||
expect(failure(counted)).toMatchObject({
|
||||
reason: "index-out-of-range",
|
||||
message: expect.stringContaining("[-1,"),
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user