From 726916532371dfc65643aa1a9d6723b6edc833ed Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 22:31:33 -0500 Subject: [PATCH] perf(il2cpp): fix 8x yields and hoist opcode checks before BigInt math in executable scans (#13272) * perf(il2cpp): fix 8x yields and hoist opcode checks before BigInt math in executable scans * test(il2cpp): type codegen module fixtures without as-never casts --- src/il2cpp/Il2cppBinaryRegistration.ts | 49 ++++++- .../boundary/il2cpp/il2cppBinaryFile.test.ts | 124 +++++++++++++++++- 2 files changed, 170 insertions(+), 3 deletions(-) diff --git a/src/il2cpp/Il2cppBinaryRegistration.ts b/src/il2cpp/Il2cppBinaryRegistration.ts index 4f5b269f36..444585ceff 100644 --- a/src/il2cpp/Il2cppBinaryRegistration.ts +++ b/src/il2cpp/Il2cppBinaryRegistration.ts @@ -549,13 +549,29 @@ function* ripRelativeReferences( arrays: ReadonlySet, ): Generator> { const referenced = new Set(); + const bytes = image.stored.bytes; for (const mapping of image.container.executable_mappings ?? []) { const loaded = image.elfPointerOverlay?.hasWrites(mapping.address, mapping.size) === true; + if (!loaded && image.stored.little) { + const startAt = Math.max(0, mapping.offset); + const end = Math.min(mapping.offset + mapping.size, bytes.length) - 7; + for (let at = startAt; at <= end; at++) { + if ((at & 0xffff) === 0) yield; + if ((bytes[at]! & 0xfb) !== 0x48) continue; // REX.W, optionally REX.R + const opcode = bytes[at + 1]!; + if (opcode !== 0x8d && opcode !== 0x8b) continue; + if ((bytes[at + 2]! & 0xc7) !== 0x05) continue; // mod 00, rm 101: RIP + disp32 + const next = mapping.address + BigInt(at - mapping.offset + 7); + const target = next + BigInt(image.stored.i32(at + 3)); + if (arrays.has(target)) referenced.add(target); + } + continue; + } const end = mapping.offset + mapping.size - 7; for (let at = mapping.offset; at <= end; at++) { - if ((at & 0xffff) < 8) yield; + if ((at & 0xffff) === 0) yield; const address = mapping.address + BigInt(at - mapping.offset); const prefix = loaded ? readUInt32(image, address) : image.stored.u32(at); if (typeof prefix !== "number") continue; @@ -588,11 +604,42 @@ function* arm64PageReferences( arrays: ReadonlySet, ): Generator> { const referenced = new Set(); + const bytes = image.stored.bytes; for (const mapping of image.container.executable_mappings ?? []) { const loaded = image.elfPointerOverlay?.hasWrites(mapping.address, mapping.size) === true; const start = Number((4n - (mapping.address % 4n)) % 4n); + if (!loaded && image.stored.little) { + const baseOffset = mapping.offset; + const limit = Math.min( + mapping.size, + Math.max(0, bytes.length - baseOffset), + ); + for (let at = start; at + 8 <= limit; at += 4) { + if ((at & 0xffff) < 4) yield; + const offset = baseOffset + at; + if ((bytes[offset + 3]! & 0x9f) !== 0x90) continue; + if (bytes[offset + 7]! !== 0x91 || (bytes[offset + 6]! & 0xc0) !== 0) + continue; + const adrp = image.stored.u32(offset); + const register = adrp & 0x1f; + // Register 31 is xzr for ADRP and sp for ADD, so no address is formed. + if (register === 31) continue; + const add = image.stored.u32(offset + 4); + if ((add & 0x1f) !== register || ((add >>> 5) & 0x1f) !== register) + continue; + const pages = + (((((adrp >>> 5) & 0x7ffff) << 2) | ((adrp >>> 29) & 3)) << 11) >> 11; + const pc = mapping.address + BigInt(at); + const target = + (pc & ~0xfffn) + + (BigInt(pages) << 12n) + + BigInt((add >>> 10) & 0xfff); + if (arrays.has(target)) referenced.add(target); + } + continue; + } for (let at = start; at + 8 <= mapping.size; at += 4) { if ((at & 0xffff) < 4) yield; const address = mapping.address + BigInt(at); diff --git a/tests/boundary/il2cpp/il2cppBinaryFile.test.ts b/tests/boundary/il2cpp/il2cppBinaryFile.test.ts index 3624b70752..e5f30c5c07 100644 --- a/tests/boundary/il2cpp/il2cppBinaryFile.test.ts +++ b/tests/boundary/il2cpp/il2cppBinaryFile.test.ts @@ -8,8 +8,12 @@ import { } from "../../../src/il2cpp/Il2cppBinaryImage.js"; import { AnalysisInputError } from "../../../src/domain/analysisErrorCore.js"; import type { Il2cppBinaryArchitecture } from "../../../src/domain/il2cpp/il2cppMetadata.js"; -import { expect, it } from "vitest"; -import { linkIl2cppBinarySteps } from "../../../src/il2cpp/Il2cppBinaryFile.js"; +import { describe, expect, it } from "vitest"; +import { findCodeRegistration } from "../../../src/il2cpp/Il2cppBinaryRegistration.js"; +import { + linkIl2cppBinarySteps, + type Il2cppCodeGenModule, +} from "../../../src/il2cpp/Il2cppBinaryFile.js"; import { drain, linkIl2cppBinary, @@ -2335,3 +2339,119 @@ it("retains an unresolved matching image count when its method array could fit", bytes.writeBigUInt64LE(bytes.readBigUInt64LE(0x410), 0x710); expect(() => link(bytes)).toThrow(Il2cppBinaryFormatFailure); }); + +describe("executable reference scan fast paths", () => { + const buildExecScanFixture = ( + arch: "amd64" | "arm64", + extendExecutableMappingPastEof = false, + ) => { + const execSize = 0x2000; + const totalSize = 0x4000; + const bytes = Buffer.alloc(totalSize); + const base = 0x100000000n; + const container = { + format: "pe" as const, + architecture: arch, + bits: 64 as const, + byte_order: "little" as const, + image_base: base, + mappings: [{ address: base + 0x1000n, size: 0x2000, offset: 0x1000 }], + executable_mappings: [ + { + address: base + 0x1000n, + size: extendExecutableMappingPastEof ? 0x10_000 : execSize, + offset: 0x1000, + }, + ], + sections: [], + zero_fills: [], + symbols: [], + }; + return { bytes, base, image: drain(openImageSteps(bytes, container)) }; + }; + + const createCodeGenModules = ( + addresses: readonly bigint[], + ): Il2cppCodeGenModule[] => + addresses.map((address, image) => ({ + image, + address, + method_pointer_count: 1, + method_pointers: 0n, + })); + + it("matches x86-64 REX.W+R mov/lea with negative displacement and ignores non-matching bytes", () => { + const fix = buildExecScanFixture("amd64"); + fix.bytes.fill(0x48, 0x1000, 0x2fff); // partial REX decoy + fix.bytes.writeBigUInt64LE(fix.base + 0x1100n, 0x1000); + fix.bytes.writeBigUInt64LE(fix.base + 0x1140n, 0x1008); + const insnAt = 0x2000; + fix.bytes.set([0x4c, 0x8b, 0x0d], insnAt); // REX.W|REX.R (0x4c), MOV (0x8b), mod=0 rm=5 reg=1 (0x0d) + fix.bytes.writeInt32LE(0x1000 - (insnAt + 7), insnAt + 3); + expect( + drain( + findCodeRegistration( + fix.image, + createCodeGenModules([fix.base + 0x1100n, fix.base + 0x1140n]), + ), + ), + ).toEqual({ field: null, array: fix.base + 0x1000n }); + }); + + it("rejects arm64 register 31 / mismatched registers and matches negative ADRP+ADD page deltas", () => { + const fix = buildExecScanFixture("arm64"); + fix.bytes.writeBigUInt64LE(fix.base + 0x1200n, 0x1000); + fix.bytes.writeBigUInt64LE(fix.base + 0x1240n, 0x1008); + // At 0x2000: ADRP x31 (xzr) + ADD x31, x31, #0 -> must be rejected + fix.bytes.writeUInt32LE(0x90ffffff >>> 0, 0x2000); + fix.bytes.writeUInt32LE(0x910003ff >>> 0, 0x2004); + // At 0x2010: ADRP x8, #page(0x1000); ADD x9, x8, #0 (mismatched dst register!) -> rejected + const deltaNeg = -1 & 0x1fffff; + fix.bytes.writeUInt32LE( + (0x90000008 | ((deltaNeg & 3) << 29) | ((deltaNeg >> 2) << 5)) >>> 0, + 0x2010, + ); + fix.bytes.writeUInt32LE(0x91000109 >>> 0, 0x2014); + // At 0x2020: valid ADRP x8, #page(0x1000); ADD x8, x8, #0x000 + fix.bytes.writeUInt32LE( + (0x90000008 | ((deltaNeg & 3) << 29) | ((deltaNeg >> 2) << 5)) >>> 0, + 0x2020, + ); + fix.bytes.writeUInt32LE(0x91000108 >>> 0, 0x2024); + expect( + drain( + findCodeRegistration( + fix.image, + createCodeGenModules([fix.base + 0x1200n, fix.base + 0x1240n]), + ), + ), + ).toEqual({ field: null, array: fix.base + 0x1000n }); + }); + + it("clamps scan bounds cleanly when an executable mapping exceeds buffer length", () => { + for (const arch of ["amd64", "arm64"] as const) { + const fix = buildExecScanFixture(arch, true); + fix.bytes.writeBigUInt64LE(fix.base + 0x1100n, 0x1000); + fix.bytes.writeBigUInt64LE(fix.base + 0x1140n, 0x1008); + if (arch === "amd64") { + fix.bytes.set([0x48, 0x8d, 0x05], 0x2000); + fix.bytes.writeInt32LE(0x1000 - (0x2000 + 7), 0x2003); + } else { + const deltaNeg = -1 & 0x1fffff; + fix.bytes.writeUInt32LE( + (0x90000008 | ((deltaNeg & 3) << 29) | ((deltaNeg >> 2) << 5)) >>> 0, + 0x2000, + ); + fix.bytes.writeUInt32LE(0x91000108 >>> 0, 0x2004); + } + expect( + drain( + findCodeRegistration( + fix.image, + createCodeGenModules([fix.base + 0x1100n, fix.base + 0x1140n]), + ), + ), + ).toEqual({ field: null, array: fix.base + 0x1000n }); + } + }); +});