perf(native): pre-screen IL2CPP slots and reuse ObjC/PE indices (#13314)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 03:51:45 +00:00
1 parent ed234b2016
commit ac4f655858
8 files changed
+379 -33

No files matched your search

+7 -5
View File
@@ -10,7 +10,6 @@ import {
BinaryReader,
BinaryResourceFailure,
createFileMappingIndex,
mappedFileOffset,
isZeroFilledSpan,
type BinaryContainer,
type FileMappingIndex,
@@ -79,6 +78,8 @@ export interface NativeImage {
readonly container: BinaryContainer;
/** One image's immutable load mappings, shared by stored and loader-aware reads. */
readonly fileMappings: FileMappingIndex;
/** Indexed executable load mappings for constant/log-time code ownership checks. */
readonly executableMappings: FileMappingIndex;
readonly imageBase: bigint;
/** ELF loader writes retain virtual identity; source bytes stay unchanged. */
readonly elfPointerOverlay?: ElfVirtualPointerWrites;
@@ -234,6 +235,10 @@ export function* openImageSteps(
container.mappings,
container.zero_fills,
),
executableMappings: createFileMappingIndex(
container.executable_mappings ?? [],
[],
),
imageBase,
...(elfPointerOverlay === undefined ? {} : { elfPointerOverlay }),
...(machoPointerFixups === undefined ? {} : { machoPointerFixups }),
@@ -342,10 +347,7 @@ export const hasPointerArrayCoverage = (
export const isExecutable = (image: NativeImage, address: bigint): boolean => {
try {
const offset = image.fileMappings.offset(address, 1);
return (
offset ===
mappedFileOffset(image.container.executable_mappings ?? [], address, 1)
);
return offset === image.executableMappings.offset(address, 1);
} catch (cause) {
if (cause instanceof BinaryFormatFailure) return false;
throw cause;
+183 -1
View File
@@ -20,6 +20,75 @@ import type { Il2cppCodeGenModule } from "./Il2cppBinaryFile.js";
const TOTAL_NAME_OCCURRENCES = 65536;
const SCAN_BYTES = 65536;
const hasLegacyMachoFixups = (image: NativeImage): boolean =>
image.machoPointerFixups !== undefined &&
image.machoPointerFixups.kind !== "none";
const canFastRawMapping = (
image: NativeImage,
mapping: {
readonly address: bigint;
readonly offset: number;
readonly size: number;
},
legacyFixups: boolean,
rawBytesLength: number,
): boolean => {
const loaded =
image.elfPointerOverlay?.hasWrites(mapping.address, mapping.size) === true;
return (
!loaded &&
!legacyFixups &&
image.stored.little &&
mapping.offset >= 0 &&
mapping.offset + mapping.size <= rawBytesLength
);
};
/** Unsigned 32-bit little-endian lane without Buffer.readUInt32LE or BigInt allocation. */
const rawUInt32LE = (bytes: Buffer, offset: number): number =>
(bytes[offset]! |
(bytes[offset + 1]! << 8) |
(bytes[offset + 2]! << 16) |
(bytes[offset + 3]! << 24)) >>>
0;
interface AddressLaneFilter {
readonly lo32: ReadonlySet<number>;
readonly singleHi32: number | null;
}
const createAddressLaneFilter = (
addresses: Iterable<bigint>,
): AddressLaneFilter => {
const lo32 = new Set<number>();
let singleHi32: number | null = null;
let multipleHi = false;
for (const address of addresses) {
if (address < 0n || address > 0xffffffff_ffffffffn) continue;
lo32.add(Number(address & 0xffffffffn));
if (!multipleHi) {
const hi = Number((address >> 32n) & 0xffffffffn);
if (singleHi32 === null) singleHi32 = hi;
else if (singleHi32 !== hi) multipleHi = true;
}
}
return { lo32, singleHi32: multipleHi ? null : singleHi32 };
};
const matchesAddressLane = (
filter: AddressLaneFilter,
bytes: Buffer,
offset: number,
): boolean => {
if (
filter.singleHi32 !== null &&
rawUInt32LE(bytes, offset + 4) !== filter.singleHi32
)
return false;
return filter.lo32.has(rawUInt32LE(bytes, offset));
};
/** Index candidates by untouched bytes, retaining bounded representation space. */
const unresolvedPointerMatcher = (
image: NativeImage,
@@ -214,11 +283,22 @@ export function* findModules(
const unresolvedName = unresolvedPointerMatcher(image, names.keys(), (name) =>
(names.get(name) ?? []).map((index) => counts[index]!),
);
const rawBytes = image.stored.bytes;
const legacyFixups = hasLegacyMachoFixups(image);
const nameFilter = createAddressLaneFilter(names.keys());
for (const mapping of image.container.mappings) {
const start = Number((8n - (mapping.address % 8n)) % 8n);
const fastRaw = canFastRawMapping(
image,
mapping,
legacyFixups,
rawBytes.length,
);
for (let at = start; at + 24 <= mapping.size; at += 8) {
if ((at & 0xffff) < 8) yield;
const offset = mapping.offset + at;
if (fastRaw && !matchesAddressLane(nameFilter, rawBytes, offset))
continue;
const address = mapping.address + BigInt(at);
const raw = image.stored.word(offset, 64);
const elfSlot =
@@ -351,10 +431,24 @@ export function* findCodeRegistration(
const expected = new Set(modules.map((module) => module.address));
const matches: { field: bigint; array: bigint }[] = [];
const matchedFields = new Set<bigint>();
const rawBytes = image.stored.bytes;
const legacyFixups = hasLegacyMachoFixups(image);
const expectedCount = modules.length;
for (const mapping of image.container.mappings) {
const start = Number((8n - (mapping.address % 8n)) % 8n);
const fastRaw = canFastRawMapping(
image,
mapping,
legacyFixups,
rawBytes.length,
);
for (let at = start; at + 16 <= mapping.size; at += 8) {
if ((at & 0xffff) < 8) yield;
if (
fastRaw &&
rawUInt32LE(rawBytes, mapping.offset + at) !== expectedCount
)
continue;
// The count owns four bytes; the following four only align its pointer.
const field = mapping.address + BigInt(at);
const count = readUInt32(image, field);
@@ -438,10 +532,19 @@ function* findModuleArrayByCode(
): Generator<void, bigint | null> {
const arrays = new Set<bigint>();
const unresolvedModule = unresolvedPointerMatcher(image, expected);
const rawBytes = image.stored.bytes;
const legacyFixups = hasLegacyMachoFixups(image);
const expectedFilter = createAddressLaneFilter(expected);
let expectedXor = 0n;
for (const candidate of expected) expectedXor ^= candidate;
for (const mapping of image.container.mappings) {
const start = Number((8n - (mapping.address % 8n)) % 8n);
const fastRaw = canFastRawMapping(
image,
mapping,
legacyFixups,
rawBytes.length,
);
let runStart = start;
const lastPosition = new Map<bigint, number>();
const possible = new Map<number, bigint | null>();
@@ -451,7 +554,20 @@ function* findModuleArrayByCode(
let duplicateValues = 0;
for (let at = start; at + 8 <= mapping.size; at += 8) {
if ((at & 0xffff) < 8) yield;
const raw = image.stored.word(mapping.offset + at, 64);
const offset = mapping.offset + at;
if (fastRaw && !matchesAddressLane(expectedFilter, rawBytes, offset)) {
if (possible.size !== 0) {
possible.clear();
occurrences.clear();
unknownPositions.clear();
possibleXor = 0n;
duplicateValues = 0;
}
if (lastPosition.size !== 0) lastPosition.clear();
runStart = at + 8;
continue;
}
const raw = image.stored.word(offset, 64);
const address = mapping.address + BigInt(at);
const elfSlot =
image.elfPointerOverlay === undefined
@@ -694,12 +810,28 @@ export function* findMetadataRegistration(
// A record whose two counts match is static evidence even when its other
// fields are unusable; code is then not asked to stand in for it.
let countsMatched = false;
const rawBytes = image.stored.bytes;
const legacyFixups = hasLegacyMachoFixups(image);
for (const mapping of image.container.mappings) {
const start = Number((8n - (mapping.address % 8n)) % 8n);
const fastRaw = canFastRawMapping(
image,
mapping,
legacyFixups,
rawBytes.length,
);
// The fields used below end at byte 112. The full 128-byte registration
// must still have loader coverage, including any declared zero-fill tail.
for (let at = start; at + 112 <= mapping.size; at += 8) {
if ((at & 0xffff) < 8) yield;
if (fastRaw) {
const off = mapping.offset + at;
if (
rawUInt32LE(rawBytes, off + 80) !== typeDefinitions ||
rawUInt32LE(rawBytes, off + 96) !== typeDefinitions
)
continue;
}
const address = mapping.address + BigInt(at);
const fieldCount = readUInt32(image, address + 80n);
if (fieldCount !== "unknown" && fieldCount !== typeDefinitions) continue;
@@ -875,14 +1007,64 @@ function* findTypeTableByCode(
(typeof slot === "bigint" && recordState(slot) === "unknown")
);
};
const rawBytes = image.stored.bytes;
const legacyFixups = hasLegacyMachoFixups(image);
const firstStart = spans[0]?.start ?? 0n;
const lastReach = reach.at(-1) ?? -1n;
const has12ByteSpan = spans.length > 0 && lastReach >= firstStart + 12n;
const bounded64 =
has12ByteSpan &&
firstStart >= 0n &&
lastReach - 12n <= 0xffffffff_ffffffffn;
const minSpanLo = bounded64 ? Number(firstStart & 0xffffffffn) : 0;
const minSpanHi = bounded64 ? Number(firstStart >> 32n) : 0;
const maxSpanRecordStart = bounded64 ? lastReach - 12n : 0n;
const maxSpanLo = bounded64 ? Number(maxSpanRecordStart & 0xffffffffn) : 0;
const maxSpanHi = bounded64 ? Number(maxSpanRecordStart >> 32n) : 0;
const isPossibleTypePointer = (offset: number): boolean => {
if (!has12ByteSpan || (rawBytes[offset]! & 7) !== 0) return false;
const lo = rawUInt32LE(rawBytes, offset);
const hi = rawUInt32LE(rawBytes, offset + 4);
if (lo === 0 && hi === 0) return false;
if (!bounded64) return true;
if (hi < minSpanHi || hi > maxSpanHi) return false;
if (hi === minSpanHi && lo < minSpanLo) return false;
if (hi === maxSpanHi && lo > maxSpanLo) return false;
return true;
};
for (const mapping of container.mappings) {
const start = Number((8n - (mapping.address % 8n)) % 8n);
const fastRaw = canFastRawMapping(
image,
mapping,
legacyFixups,
rawBytes.length,
);
let runStart = -1;
let startUnknown = false;
let possibleLength = 0;
let possibleUnknown = false;
for (let at = start; at <= mapping.size; at += 8) {
if ((at & 0xffff) < 8) yield;
if (
fastRaw &&
at + 8 <= mapping.size &&
!isPossibleTypePointer(mapping.offset + at)
) {
possibleLength = 0;
possibleUnknown = false;
if (runStart >= 0) {
if ((at - runStart) / 8 >= requiredTypes) {
runs.set(
mapping.address + BigInt(runStart),
startUnknown ? null : (at - runStart) / 8,
);
if (runs.size > 1) return null;
}
runStart = -1;
}
continue;
}
const address = mapping.address + BigInt(at);
let endUnknown = at + 8 > mapping.size;
if (!endUnknown) {