fix(il2cpp): validate record extents before width inference (#12429)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 17:28:17 +00:00
1 parent 7cc7bda6e3
commit 0ea671abf5
4 files changed
+135 -68

No files matched your search

+39 -39
View File
@@ -220,6 +220,11 @@ const recordBytes = (section: Il2cppMetadataSection): number | null => {
);
return null;
}
if (section.size === 0)
throw new Il2cppMetadataFormatFailure(
"record-size-mismatch",
`section ${section.name} declares ${count} records with no bytes`,
);
if (section.size % count !== 0)
throw new Il2cppMetadataFormatFailure(
"record-size-mismatch",
@@ -238,15 +243,14 @@ const FIXED_WIDTHS: Il2cppMetadataDecoded["index_widths"] = {
const GENERIC_CONTAINER_RECORD_BYTES = 16;
const resolveWidths = (
const resolveCountedTables = (
section: (name: SectionName) => Il2cppMetadataSection,
): Il2cppMetadataDecoded["index_widths"] => {
const genericContainers = section("genericContainers");
genericContainerCount: number,
) => {
const type_definition = il2cppIndexWidth(
headerCount(section("typeDefinitions")),
);
const generic_container = il2cppIndexWidth(headerCount(genericContainers));
const parameter = il2cppIndexWidth(headerCount(section("parameters")));
const generic_container = il2cppIndexWidth(genericContainerCount);
// TypeIndex counts native types, so its width is recovered from records.
const interfaceOffset = recordBytes(section("interfaceOffsets"));
let type: number;
@@ -267,13 +271,19 @@ const resolveWidths = (
"type-index-width",
`the ${type_width_source} does not imply a 1-, 2- or 4-byte TypeIndex`,
);
return {
const members = readIl2cppMemberTables({
typeIndexWidth: type,
indexing: "counted",
section,
});
const widths: Il2cppMetadataDecoded["index_widths"] = {
type_definition,
generic_container,
parameter,
parameter: il2cppIndexWidth(members.counts.parameters),
type,
type_width_source,
};
return { widths, members };
};
/** Record counts checked against the record sizes the widths imply. */
@@ -282,47 +292,28 @@ const recordCounts = (
section: (name: SectionName) => Il2cppMetadataSection,
expected: Readonly<Record<RecordSection, number>>,
widths: Il2cppMetadataDecoded["index_widths"],
genericContainers: number,
): Readonly<
Record<
RecordSection | "nestedTypes" | "genericContainers" | "genericParameters",
number
>
> => {
const genericContainers = checkedSectionCount(
section("genericContainers"),
GENERIC_CONTAINER_RECORD_BYTES,
);
const genericParameters = checkedSectionCount(
section("genericParameters"),
widths.generic_container + 12,
);
const names = ["typeDefinitions", "images", "methods"] as const;
if (VERSION_LAYOUTS[version].indexing === "fixed") {
if (VERSION_LAYOUTS[version].indexing === "fixed")
checkedSectionCount(section("interfaceOffsets"), widths.type + 4);
const sized = Object.fromEntries(
names.map((name) => [
name,
checkedSectionCount(section(name), expected[name]),
]),
) as Record<RecordSection, number>;
return {
...sized,
nestedTypes: checkedSectionCount(
section("nestedTypes"),
NESTED_TYPE_ENTRY_BYTES,
),
genericContainers,
genericParameters,
};
}
const counted = Object.fromEntries(
const names = ["typeDefinitions", "images", "methods"] as const;
const sized = Object.fromEntries(
names.map((name) => [
name,
checkedSectionCount(section(name), expected[name]),
]),
) as Record<RecordSection, number>;
return {
...counted,
...sized,
nestedTypes: checkedSectionCount(
section("nestedTypes"),
NESTED_TYPE_ENTRY_BYTES,
@@ -867,15 +858,22 @@ function* readMetadataCoreSteps(
const byName = new Map(sections.map((entry) => [entry.name, entry]));
const section = (name: SectionName) => byName.get(name)!;
const layout = VERSION_LAYOUTS[version];
const widths =
layout.indexing === "counted" ? resolveWidths(section) : FIXED_WIDTHS;
const genericContainers = checkedSectionCount(
section("genericContainers"),
GENERIC_CONTAINER_RECORD_BYTES,
);
const counted =
layout.indexing === "counted"
? resolveCountedTables(section, genericContainers)
: undefined;
const widths = counted?.widths ?? FIXED_WIDTHS;
const expected = il2cppMetadataRecordBytes(widths, version);
let tables: MetadataTables = {
bytes,
section,
widths,
recordBytes: expected,
counts: recordCounts(version, section, expected, widths),
counts: recordCounts(version, section, expected, widths, genericContainers),
typeDefinitionTypeIndices: layout.typeDefinitionTypeIndices,
indexing: layout.indexing,
methodReturnParameterToken: layout.methodReturnParameterToken,
@@ -884,11 +882,13 @@ function* readMetadataCoreSteps(
section("strings"),
options.beforeText,
),
members: readIl2cppMemberTables({
widths,
indexing: layout.indexing,
section,
}),
members:
counted?.members ??
readIl2cppMemberTables({
typeIndexWidth: widths.type,
indexing: layout.indexing,
section,
}),
beforeText: options.beforeText,
onRow: options.onRow,
};
+7 -5
View File
@@ -25,15 +25,17 @@ export interface MetadataMemberOwners {
/** Own member record widths and validate all counts before any row allocation. */
export const readIl2cppMemberTables = ({
widths,
typeIndexWidth,
indexing,
section,
}: Pick<MetadataTables, "widths" | "indexing" | "section">) => {
}: Pick<MetadataTables, "indexing" | "section"> & {
readonly typeIndexWidth: MetadataTables["widths"]["type"];
}) => {
const recordBytes = {
fields: 8 + widths.type,
fields: 8 + typeIndexWidth,
properties: 20,
events: 20 + widths.type,
parameters: 8 + widths.type,
events: 20 + typeIndexWidth,
parameters: 8 + typeIndexWidth,
stringLiterals: indexing === "counted" ? 4 : 8,
};
const literalRecords = checkedSectionCount(
@@ -8,7 +8,7 @@ import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
cliTest(
"rejects partial IL2CPP table extents through CLI and MCP without a binary",
"reports inconsistent IL2CPP table extents through CLI and MCP without a binary",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-il2cpp-extent-");
const { call } = await connectLocalToolsMcp();
@@ -16,11 +16,15 @@ cliTest(
[31, "genericContainers"],
[31, "genericParameters"],
[31, "interfaceOffsets"],
[39, "genericContainers"],
[39, "genericParameters"],
[39, "parameters"],
] as const) {
const isCountedContainer = version === 39 && name === "genericContainers";
const fixture = createIl2cppMetadataFixture({
version,
genericOwners: true,
...(isCountedContainer && { interfaceOffsets: false }),
});
const path = join(root, `${version}-${name}.dat`);
await writeFile(path, fixture.bytes);
@@ -30,10 +34,14 @@ cliTest(
expect(intact.exitCode, intact.stderr).toBe(0);
const damaged = Buffer.from(fixture.bytes);
damaged.writeInt32LE(
fixture.section(name).size - 1,
fixture.header(name, "size"),
);
if (isCountedContainer || name === "parameters") {
damaged.writeInt32LE(300, fixture.header(name, "count"));
} else {
damaged.writeInt32LE(
fixture.section(name).size - 1,
fixture.header(name, "size"),
);
}
await writeFile(path, damaged);
const invalid = await cli.run({
arguments: ["inspect-il2cpp-metadata", path, "--json"],
@@ -45,7 +53,9 @@ cliTest(
{
path: ["path"],
reason: "invalid_format",
message: expect.stringContaining("record-size-mismatch"),
message: expect.stringContaining(
`record-size-mismatch: section ${name}`,
),
},
],
},
@@ -132,18 +132,28 @@ describe("IL2CPP global-metadata version 39", () => {
},
);
it("recovers the TypeIndex width from type records without interfaceOffsets", () => {
const fixture = createIl2cppMetadataFixture({
typeIndexWidth: 2,
interfaceOffsets: false,
});
const decoded = readIl2cppMetadata(fixture.bytes);
expect(decoded.index_widths.type).toBe(2);
expect(decoded.index_widths.type_width_source).toBe(
"type-definition-record",
);
expect(decoded.methods).toEqual(FIXTURE_METHODS);
});
it.each([
[0, 1],
[300, 2],
] as const)(
"recovers the TypeIndex width from type records with %i generic containers and no interface offsets",
(genericContainers, genericContainerWidth) => {
const fixture = createIl2cppMetadataFixture({
typeIndexWidth: 2,
interfaceOffsets: false,
wide: { fillerTypes: 0, genericContainers, parameters: 0 },
});
const decoded = readIl2cppMetadata(fixture.bytes);
expect(decoded.index_widths.generic_container).toBe(
genericContainerWidth,
);
expect(decoded.index_widths.type).toBe(2);
expect(decoded.index_widths.type_width_source).toBe(
"type-definition-record",
);
expect(decoded.methods).toEqual(FIXTURE_METHODS);
},
);
it("accepts empty sections that share an offset with occupied sections and padding gaps", () => {
const bytes = mutated((fixture, target) => {
@@ -323,17 +333,25 @@ describe("IL2CPP generic-container linking facts", () => {
describe("IL2CPP generic-container extents", () => {
it("checks generic-container count against its fixed 16-byte records before resolving widths", () => {
// A positive count with no record bytes.
for (const count of [1, 300])
rejects(
mutated((fixture, bytes) =>
for (const [interfaceOffsets, count] of [
[true, 1],
[true, 300],
[false, 300],
] as const) {
const bytes = mutated(
(fixture, bytes) =>
bytes.writeInt32LE(
count,
fixture.header("genericContainers", "count"),
),
),
"record-size-mismatch",
createIl2cppMetadataFixture({ interfaceOffsets }),
);
for (const reader of [readIl2cppMetadata, readIl2cppMetadataForLinking]) {
const rejected = failure(bytes, reader);
expect(rejected.reason).toBe("record-size-mismatch");
expect(rejected.message).toContain("genericContainers");
}
}
const oneRecord = createIl2cppMetadataFixture({
wide: { fillerTypes: 0, genericContainers: 1, parameters: 0 },
});
@@ -377,6 +395,43 @@ describe("IL2CPP generic-container extents", () => {
});
});
it("checks backing parameter records before widening method parameter indices", () => {
const count = 300;
const unbacked = mutated((fixture, bytes) =>
bytes.writeInt32LE(count, fixture.header("parameters", "count")),
);
for (const reader of [readIl2cppMetadata, readIl2cppMetadataForLinking]) {
const rejected = failure(unbacked, reader);
expect(rejected.reason).toBe("record-size-mismatch");
expect(rejected.message).toContain("section parameters");
}
const backed = createIl2cppMetadataFixture({
wide: { fillerTypes: 0, genericContainers: 0, parameters: count },
});
const decoded = readIl2cppMetadata(backed.bytes);
expect(decoded.index_widths.parameter).toBe(2);
expect(decoded.parameters).toHaveLength(count);
expect(decoded.methods).toHaveLength(FIXTURE_METHODS.length);
});
it.each(["interfaceOffsets", "typeDefinitions"] as const)(
"rejects %s as width evidence when its declared records have no bytes",
(name) => {
const bytes = mutated(
(fixture, bytes) => bytes.writeInt32LE(0, fixture.header(name, "size")),
createIl2cppMetadataFixture({
interfaceOffsets: name === "interfaceOffsets",
}),
);
for (const reader of [readIl2cppMetadata, readIl2cppMetadataForLinking]) {
const rejected = failure(bytes, reader);
expect(rejected.reason).toBe("record-size-mismatch");
expect(rejected.message).toContain(name);
}
},
);
describe("rejected IL2CPP metadata references", () => {
it("rejects image, type, method and nested-type indices outside their tables", () => {
rejects(