mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
refactor(il2cpp): share metadata section table framing (#12364)
This commit is contained in:
1 parent
370602cfbb
commit
7e7516fbe9
6 files changed
+188
-101
No files matched your search
@@ -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. */
|
||||
|
||||
@@ -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<Name extends string = string> {
|
||||
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<Name extends string>(
|
||||
bytes: Buffer,
|
||||
names: readonly Name[],
|
||||
entryBytes: 12,
|
||||
): Array<SectionDeclaration<Name> & { readonly count: number }>;
|
||||
export function readIl2cppSectionTable<Name extends string>(
|
||||
bytes: Buffer,
|
||||
names: readonly Name[],
|
||||
entryBytes: 8 | 12,
|
||||
): Array<SectionDeclaration<Name>>;
|
||||
export function readIl2cppSectionTable<Name extends string>(
|
||||
bytes: Buffer,
|
||||
names: readonly Name[],
|
||||
entryBytes: 8 | 12,
|
||||
): Array<SectionDeclaration<Name>> {
|
||||
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;
|
||||
};
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user