Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | /* * Copyright (c) 2023 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ import System import Foundation @_implementationOnly import Dyld_Internal @objc @implementation extension _DYSegment { @objc var name: String { return impl.name } @objc var permissions: UInt64 { return impl.permissions } @objc var vmsize: UInt64 { return impl.vmSize } @objc var address: UInt64 { return impl.address.value } @objc var preferredLoadAddress: UInt64 { return impl.preferredLoadAddress.value } @objc func getFastPathData(_ data: UnsafeMutablePointer<_DYSegmentFastPathData>) { let info = impl.info switch info.name { case .asciiBuffer(let buffer): data.pointee.segmentNamePtr = buffer.baseAddress! data.pointee.segmentNameSize = UInt64(buffer.count) default: break } data.pointee.fileSize = info.fileSize data.pointee.vmSize = info.vmSize data.pointee.address = info.address data.pointee.preferredAddress = info.preferredAddress data.pointee.permissions = info.permissions } func withSegmentData(_ block:(@escaping (Data) -> Void)) -> Bool { do { try impl.withSegmentData(block) return true } catch { return false } } @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @nonobjc convenience fileprivate init(_ impl: Segment) { self.init(internal:true) self.impl = impl } @nonobjc private var impl: Segment! } @objc @implementation extension _DYImage { @objc var installname: String? { return impl.installname } @objc var filePath: String? { return impl.filePath } @objc var uuid: UUID? { return impl.uuid } @objc var address: UInt64 { return impl.address.value } @objc var pointerSize: UInt64 { return impl.pointerSize } @objc var preferredLoadAddress: UInt64 { return impl.preferredLoadAddress.value } @objc var sharedCache: _DYSharedCache? { guard let sharedCache = impl.sharedCache else { return nil } return _DYSharedCache(sharedCache) } @objc func getFastPathData(_ data: UnsafeMutablePointer<_DYImageFastPathData>) { switch impl.info.installname { case .asciiBuffer(let buffer): data.pointee.installNamePtr = buffer.baseAddress! data.pointee.installNameSize = UInt64(buffer.count) case .bplist: data.pointee.unicodeInstallname = true case .none: break } switch impl.info.filePath { case .asciiBuffer(let buffer): data.pointee.filePathPtr = buffer.baseAddress! data.pointee.filePathSize = UInt64(buffer.count) case .bplist: data.pointee.unicodeFilePath = true case .none: break } if let uuid = impl.uuid { data.pointee.uuid = uuid.uuid } data.pointee.address = impl.address.value if impl.sharedCache != nil { data.pointee.sharedCacheImage = true } } @objc lazy var segments: [_DYSegment] = { return impl.segments.map { _DYSegment($0) } }() @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @nonobjc convenience fileprivate init(_ impl: Image) { self.init(internal:true) self.impl = impl } @nonobjc private var impl: Image! } @objc @implementation extension _DYEnvironment { @objc var rootPath: String? { return impl.rootPath } @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @nonobjc convenience fileprivate init?(_ impl: Environment?) { guard let impl else { return nil } self.init(internal:true) self.impl = impl } @nonobjc private var impl: Environment! } @objc @implementation extension _DYAOTImage { @objc var x86Address: UInt64 { return impl.x86Address.value } @objc var aotAddress: UInt64 { return impl.aotAddress.value } @objc var aotSize: UInt64 { return impl.aotSize } @objc var aotImageKey: Data { return impl.aotImageKey } @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @nonobjc convenience fileprivate init(_ impl: AOTImage) { self.init(internal:true) self.impl = impl } @nonobjc private var impl: AOTImage! } @objc @implementation extension _DYSubCache { func withVMLayoutData(_ block:(@escaping (Data) -> Void)) -> Bool { return impl.withVMLayoutData(block) } @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @nonobjc convenience fileprivate init(_ impl: SubCache) { self.init(internal:true) self.impl = impl } @nonobjc private var impl: SubCache! } @objc @implementation extension _DYSharedCache { @objc var uuid: UUID { return impl.uuid } @objc var address: UInt64 { return impl.address.value } @objc var vmsize: UInt64 { return impl.vmSize } @objc var preferredLoadAddress: UInt64 { return impl.preferredLoadAddress.value } @objc var mappedPrivate: Bool { return impl.mappedPrivate } @objc var filePaths: [String] { return impl.filePaths } @objc var aotUuid: UUID? { return impl.aotUuid } @objc var aotAddress: UInt64 { return impl.aotAddress?.value ?? 0 } @objc var localSymbolPath: String? { return impl.localSymbolPath } @objc var localSymbolData: Data? { return impl.localSymbolData } @objc lazy var images: [_DYImage] = { return impl.images.map { _DYImage($0) } }() @objc func pinMappings() -> Bool { return impl.pinMappings() } @objc func unpinMappings() { impl.unpinMappings() } @objc lazy var subCaches: [_DYSubCache] = { return impl.subCaches.map { _DYSubCache($0) } }() @objc class func installedSharedCaches() -> [_DYSharedCache] { return SharedCache.installedSharedCaches().map { _DYSharedCache($0) } } @objc(installedSharedCachesForSystemPath:) class func installedSharedCaches(systemPath: String) -> [_DYSharedCache] { return SharedCache.installedSharedCaches(systemPath:FilePath(systemPath)).map { _DYSharedCache($0) } } @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @objc convenience init(path:String) throws { self.init(internal:true) impl = try SharedCache(path:FilePath(path)) } @nonobjc convenience fileprivate init(_ impl: SharedCache) { self.init(internal:true) self.impl = impl } @nonobjc private var impl: SharedCache! } @objc @implementation extension _DYSnapshot { @objc var pid: pid_t { return impl.pid } @objc var pageSize: size_t { return impl.pageSize } @objc var images: [_DYImage]! { return impl.images.map { _DYImage($0) } } @objc var aotImages: [_DYAOTImage]? { return impl.aotImages?.map { _DYAOTImage($0) } } @objc var timestamp: UInt64 { return impl.timestamp } @objc var initialImageCount: UInt64 { return impl.initialImageCount } @objc var state: UInt8 { return impl.state } @objc var platform: UInt64 { return impl.platform } @objc var environment: _DYEnvironment? { return _DYEnvironment(impl.environment) } @objc lazy var sharedCache: _DYSharedCache? = { guard let sharedCache = impl.sharedCache else { return nil } return _DYSharedCache(sharedCache) }() @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @objc convenience init(data: Data) throws { self.init(internal:true) self.impl = try Snapshot(data:data) } @nonobjc convenience fileprivate init(_ impl: Snapshot) { self.init(internal:true) self.impl = impl } @nonobjc private var impl: Snapshot! } //MARK: - //MARK: Process @objc @implementation extension _DYEventHandlerToken { @nonobjc private var _value: UInt32! @objc var value: UInt32 { return _value } @objc private override init() { self._value = nil super.init() } @objc public init(value: UInt32) { self._value = value } } @objc @implementation extension _DYProcess { @objc var queue: DispatchQueue? { get { return impl.queue } set { impl.queue = newValue } } @objc(initWithTask:queue:error:) init(task: task_read_t, queue: DispatchQueue? = nil) throws { self.impl = try Process(task:task, queue:queue) } @objc(processForCurrentTask) class func forCurrentTask() -> Self { let process = Process.forCurrentTask() return _DYProcess(process) as! Self } @objc(registerChangeNotificationsWithError:handler:) func registerChangeNotifications(handler: @escaping (_ image: _DYImage, _ load: Bool) -> Void) throws -> _DYEventHandlerToken { let tokenValue = try impl.registerForChangeNotifications() { handler(_DYImage($0), $1) } return _DYEventHandlerToken(value:tokenValue) } @objc(registerForEvent:error:handler:) func register(event:UInt32, handler: @escaping () -> Void) throws -> _DYEventHandlerToken { let tokenValue = try impl.register(event:event, handler) return _DYEventHandlerToken(value:tokenValue) } @objc(unregisterForEvent:) func unregister(event: _DYEventHandlerToken) { impl.unregister(event:event.value); } @objc(getCurrentSnapshotAndReturnError:) func getCurrentSnapshot() throws -> _DYSnapshot { return _DYSnapshot(impl.getCurrentSnapshot()) } @objc private override init() { fatalError("init() not supported") } @objc private init(internal: Bool) { super.init() } @nonobjc convenience private init(_ impl: Process) { self.init(internal:true) self.impl = impl } @nonobjc private var impl: Process! } |