From a9390e36dbb16da12039489c544b410b27cb33bc Mon Sep 17 00:00:00 2001 From: CelloSerenity <195480169+CelloSerenity@users.noreply.github.com> Date: Mon, 6 Jul 2026 15:31:19 -0600 Subject: [PATCH] Batch JIT page bless commands to reduce debugserver round-trips --- Sources/ScriptRunner.swift | 77 +++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/Sources/ScriptRunner.swift b/Sources/ScriptRunner.swift index 687e8e0..96302ce 100644 --- a/Sources/ScriptRunner.swift +++ b/Sources/ScriptRunner.swift @@ -5,6 +5,8 @@ import JavaScriptCore final class ScriptRunner { private static let jitPageSize: UInt64 = 16384 + private static let jitPageCommandLength = 19 + private static let commandsPerBatch = 128 private let targetPID: Int32 private let debugProxy: OpaquePointer @@ -67,17 +69,80 @@ final class ScriptRunner { private func prepareMemoryRegion(_ address: UInt64, length: UInt64) -> String? { guard length > 0 else { return "OK" } - let pageCount = (length - 1) / Self.jitPageSize + 1 - for page in 0.. UnsafeMutablePointer? in + let base = rawBuffer.bindMemory(to: UInt8.self).baseAddress! + return debug_proxy_send_raw(debugProxy, base.advanced(by: byteOffset), UInt(byteCount)) + } + if let sendError { + progress("page bless batch failed @0x\(String(address, radix: 16)): \(IdeviceFFI.consume(sendError, fallback: "debug_proxy_send_raw").localizedDescription)") return nil } + + for _ in 0..? + let readError = debug_proxy_read_response(debugProxy, &response) + if let response { + idevice_string_free(response) + } + if let readError { + progress("page bless batch failed @0x\(String(address, radix: 16)): \(IdeviceFFI.consume(readError, fallback: "debug_proxy_read_response").localizedDescription)") + return nil + } + } } + progress("Blessed \(pageCount) JIT page(s) at 0x\(String(address, radix: 16))") return "OK" } + + private static func makeBlessCommands(startAddress: UInt64, pageCount: Int) -> [UInt8] { + var buffer = [UInt8](repeating: 0, count: pageCount * jitPageCommandLength) + + for page in 0..> shift) & 0xf)) + } + } + + private static func writeChecksum(into buffer: inout [UInt8], bodyStart: Int, hashIndex: Int) { + var checksum: UInt8 = 0 + for index in bodyStart..> 4) + buffer[hashIndex + 2] = hexDigit(checksum & 0x0f) + } + + private static func hexDigit(_ value: UInt8) -> UInt8 { + value < 10 ? value + UInt8(ascii: "0") : value - 10 + UInt8(ascii: "a") + } }