From 2d3569d6d843a483c3c04e70e3019cdcab081151 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:10:07 -0500 Subject: [PATCH] perf(il2cpp): keep fastRaw scans active on relocated ELF segments via sorted overlay merge cursor (#13482) * perf(il2cpp): keep fastRaw scans active on relocated ELF segments via sorted overlay merge cursor (#13480) * fix(il2cpp): expose ELF write intervals from the value-only overlay --- .../native/elfPointerRelocations.test.ts | 6 ++ src/domain/native/elfPointerRelocations.ts | 37 ++++++++++ src/il2cpp/Il2cppBinaryRegistration.ts | 70 +++++++++++++------ 3 files changed, 92 insertions(+), 21 deletions(-) diff --git a/src/domain/native/elfPointerRelocations.test.ts b/src/domain/native/elfPointerRelocations.test.ts index fee9932007..6a509c502c 100644 --- a/src/domain/native/elfPointerRelocations.test.ts +++ b/src/domain/native/elfPointerRelocations.test.ts @@ -752,6 +752,12 @@ it("admits fitting pointer workspace and preserves its complete values", () => { ).toEqual([0x1234n, 0x1334n, 0x1434n]); }); +it("exposes clipped, coalesced loader-write intervals from the value-only overlay", () => { + const facts = pointerValues(relativeValues(), 6144); + expect(facts.writeIntervalsIn(0x304n, 16)).toEqual([{ start: 0, end: 16 }]); + expect(facts.writeIntervalsIn(0x318n, 8)).toEqual([]); +}); + it("charges repeated virtual destinations once within the pointer workspace", () => { const facts = pointerValues(relativeValues(true), 2048); expect(facts.unsignedAt(0x300n, 8)).toBe(0x1234n); diff --git a/src/domain/native/elfPointerRelocations.ts b/src/domain/native/elfPointerRelocations.ts index 21f53ca571..d89b8597c9 100644 --- a/src/domain/native/elfPointerRelocations.ts +++ b/src/domain/native/elfPointerRelocations.ts @@ -1027,6 +1027,11 @@ export function* readElfRelocationWritesSteps( /** Loader values and overlap ownership, independent of retained relocation provenance. */ export interface ElfVirtualPointerWrites { readonly hasWrites: (address: bigint, size: number) => boolean; + /** Sorted, coalesced mapping-relative `[start, end)` byte intervals overlapping loader writes. */ + readonly writeIntervalsIn: ( + address: bigint, + size: number, + ) => readonly { readonly start: number; readonly end: number }[]; /** Undefined when a write leaves the selected field unresolved or partly overwritten. */ readonly unsignedAt: (address: bigint, width: 4 | 8) => bigint | undefined; } @@ -1182,6 +1187,37 @@ class ElfVirtualWriteIndex { const low = before(address + BigInt(size)); return low > 0 && (reach[low - 1] ?? 0n) > address; }, + writeIntervalsIn: ( + address: bigint, + size: number, + ): readonly { readonly start: number; readonly end: number }[] => { + if (size <= 0) return []; + const limit = address + BigInt(size); + const last = before(limit) - 1; + if (last < 0 || (reach[last] ?? 0n) <= address) return []; + const reversed: { start: number; end: number }[] = []; + for (let at = last; at >= 0 && (reach[at] ?? 0n) > address; at--) { + const range = ranges[at]!; + if (range.end <= address) continue; + const start = + range.address <= address ? 0 : Number(range.address - address); + const end = range.end >= limit ? size : Number(range.end - address); + reversed.push({ start, end }); + } + if (reversed.length <= 1) return reversed; + reversed.reverse(); + const coalesced: { start: number; end: number }[] = [reversed[0]!]; + for (let i = 1; i < reversed.length; i += 1) { + const next = reversed[i]!; + const tail = coalesced[coalesced.length - 1]!; + if (next.start <= tail.end) { + if (next.end > tail.end) tail.end = next.end; + } else { + coalesced.push(next); + } + } + return coalesced; + }, }; } } @@ -1289,6 +1325,7 @@ export function* readElfPointerValuesSteps( const { originalValueAt } = next.value; return { hasWrites: facts.hasWrites, + writeIntervalsIn: facts.writeIntervalsIn, unsignedAt: (address, width) => { if (!facts.hasWrites(address, width)) return undefined; return facts.unsignedAt( diff --git a/src/il2cpp/Il2cppBinaryRegistration.ts b/src/il2cpp/Il2cppBinaryRegistration.ts index 0b22b57125..cc4114ca42 100644 --- a/src/il2cpp/Il2cppBinaryRegistration.ts +++ b/src/il2cpp/Il2cppBinaryRegistration.ts @@ -24,7 +24,14 @@ const hasLegacyMachoFixups = (image: NativeImage): boolean => image.machoPointerFixups !== undefined && image.machoPointerFixups.kind !== "none"; -const canFastRawMapping = ( +interface FastRawOverlay { + /** Monotonic O(1)-amortized check whether `[relStart, relStart + width)` overlaps an ELF write. */ + readonly hasWrite: (relStart: number, width: number) => boolean; +} + +const NO_OVERLAY_WRITES: FastRawOverlay = { hasWrite: () => false }; + +const createFastRawOverlay = ( image: NativeImage, mapping: { readonly address: bigint; @@ -33,16 +40,27 @@ const canFastRawMapping = ( }, 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 - ); +): FastRawOverlay | null => { + if ( + legacyFixups || + !image.stored.little || + mapping.offset < 0 || + mapping.offset + mapping.size > rawBytesLength + ) + return null; + const overlay = image.elfPointerOverlay; + if (overlay === undefined) return NO_OVERLAY_WRITES; + const intervals = overlay.writeIntervalsIn(mapping.address, mapping.size); + if (intervals.length === 0) return NO_OVERLAY_WRITES; + let cursor = 0; + return { + hasWrite(relStart: number, width: number): boolean { + const relEnd = relStart + width; + while (cursor < intervals.length && intervals[cursor]!.end <= relStart) + cursor += 1; + return cursor < intervals.length && intervals[cursor]!.start < relEnd; + }, + }; }; /** Unsigned 32-bit little-endian lane without Buffer.readUInt32LE or BigInt allocation. */ @@ -288,7 +306,7 @@ export function* findModules( const nameFilter = createAddressLaneFilter(names.keys()); for (const mapping of image.container.mappings) { const start = Number((8n - (mapping.address % 8n)) % 8n); - const fastRaw = canFastRawMapping( + const fastRaw = createFastRawOverlay( image, mapping, legacyFixups, @@ -297,7 +315,11 @@ export function* findModules( 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)) + if ( + fastRaw !== null && + !fastRaw.hasWrite(at, 8) && + !matchesAddressLane(nameFilter, rawBytes, offset) + ) continue; const address = mapping.address + BigInt(at); const raw = image.stored.word(offset, 64); @@ -436,7 +458,7 @@ export function* findCodeRegistration( const expectedCount = modules.length; for (const mapping of image.container.mappings) { const start = Number((8n - (mapping.address % 8n)) % 8n); - const fastRaw = canFastRawMapping( + const fastRaw = createFastRawOverlay( image, mapping, legacyFixups, @@ -445,7 +467,8 @@ export function* findCodeRegistration( for (let at = start; at + 16 <= mapping.size; at += 8) { if ((at & 0xffff) < 8) yield; if ( - fastRaw && + fastRaw !== null && + !fastRaw.hasWrite(at, 4) && rawUInt32LE(rawBytes, mapping.offset + at) !== expectedCount ) continue; @@ -539,7 +562,7 @@ function* findModuleArrayByCode( 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( + const fastRaw = createFastRawOverlay( image, mapping, legacyFixups, @@ -555,7 +578,11 @@ function* findModuleArrayByCode( for (let at = start; at + 8 <= mapping.size; at += 8) { if ((at & 0xffff) < 8) yield; const offset = mapping.offset + at; - if (fastRaw && !matchesAddressLane(expectedFilter, rawBytes, offset)) { + if ( + fastRaw !== null && + !fastRaw.hasWrite(at, 8) && + !matchesAddressLane(expectedFilter, rawBytes, offset) + ) { if (possible.size !== 0) { possible.clear(); occurrences.clear(); @@ -814,7 +841,7 @@ export function* findMetadataRegistration( const legacyFixups = hasLegacyMachoFixups(image); for (const mapping of image.container.mappings) { const start = Number((8n - (mapping.address % 8n)) % 8n); - const fastRaw = canFastRawMapping( + const fastRaw = createFastRawOverlay( image, mapping, legacyFixups, @@ -824,7 +851,7 @@ export function* findMetadataRegistration( // 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) { + if (fastRaw !== null && !fastRaw.hasWrite(at + 80, 20)) { const off = mapping.offset + at; if ( rawUInt32LE(rawBytes, off + 80) !== typeDefinitions || @@ -1034,7 +1061,7 @@ function* findTypeTableByCode( }; for (const mapping of container.mappings) { const start = Number((8n - (mapping.address % 8n)) % 8n); - const fastRaw = canFastRawMapping( + const fastRaw = createFastRawOverlay( image, mapping, legacyFixups, @@ -1047,8 +1074,9 @@ function* findTypeTableByCode( for (let at = start; at <= mapping.size; at += 8) { if ((at & 0xffff) < 8) yield; if ( - fastRaw && + fastRaw !== null && at + 8 <= mapping.size && + !fastRaw.hasWrite(at, 8) && !isPossibleTypePointer(mapping.offset + at) ) { possibleLength = 0;