-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDyldCachePrintTests.swift
More file actions
472 lines (429 loc) · 15.9 KB
/
DyldCachePrintTests.swift
File metadata and controls
472 lines (429 loc) · 15.9 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//
// DyldCachePrintTests.swift
//
//
// Created by p-x9 on 2024/01/13.
//
//
import XCTest
@testable import MachOKit
final class DyldCachePrintTests: XCTestCase {
private var cache: DyldCache!
private var cache1: DyldCache!
override func setUp() {
print("----------------------------------------------------")
let arch = "arm64e"
let path = "/System/Volumes/Preboot/Cryptexes/OS/System/Library/dyld/dyld_shared_cache_\(arch)"
// let path = "/System/Volumes/Preboot/Cryptexes/OS/System/Library/dyld/dyld_shared_cache_\(arch).01"
// let path = "/System/Volumes/Preboot/Cryptexes/OS/System/DriverKit/System/Library/dyld/dyld_shared_cache_\(arch)"
// let path = "/System/Volumes/Preboot/Cryptexes/OS/System/DriverKit/System/Library/dyld/dyld_shared_cache_\(arch).symbols"
let url = URL(fileURLWithPath: path)
self.cache = try! DyldCache(url: url)
self.cache1 = try! DyldCache(
subcacheUrl: URL(fileURLWithPath: path + ".01"),
mainCacheHeader: cache.header
)
}
func testHeader() throws {
let header = cache.header
print("Magic:", header.magic)
print("UUID:", header.uuid)
print("CacheType:", header.cacheType!)
print("Mappings:", header.mappingCount)
print("Images:", header.imagesCount)
print("Platform:", header.platform)
print("OS Version:", header.osVersion)
print("isSimulator:", header.isSimulator)
print("Alt Platform:", header.altPlatform)
print("Alt OS Version:", header.altOsVersion)
print("CPU:", cache.cpu)
// dump(header.layout)
}
func testMappingInfos() throws {
guard let infos = cache.mappingInfos else {
return
}
for info in infos {
print("----")
print("Address:", String(info.address, radix: 16))
print("File Offset::", String(info.fileOffset, radix: 16))
print("Size:", String(info.size, radix: 16))
print("MaxProtection:", info.maxProtection.bits)
print("InitProtection:", info.initialProtection.bits)
}
}
func testMappingAndSlideInfos() throws {
guard let infos = cache.mappingAndSlideInfos else {
return
}
for info in infos {
print("----")
print("Address:", String(info.address, radix: 16))
print("Name:", info.mappingName ?? "unknown")
print("File Offset::", String(info.fileOffset, radix: 16))
print("Size:", String(info.size, radix: 16))
print("Flags:", info.flags.bits)
print("MaxProtection:", info.maxProtection.bits)
print("InitProtection:", info.initialProtection.bits)
if let slideInfo = info.slideInfo(in: cache) {
print("SlideInfo")
print(" Version:", slideInfo.version.rawValue)
print(" Info:", slideInfo)
}
}
}
func testImageInfos() throws {
guard let infos = cache.imageInfos else {
return
}
print("Images:", cache.header.imagesCount)
for info in infos {
print("----")
print("Address:", String(info.address, radix: 16))
print("Path:", info.path(in: cache) ?? "unknown")
}
}
func testImageTextInfos() throws {
guard let infos = cache.imageTextInfos else {
return
}
print("ImageTexts:", cache.header.imagesTextCount)
for info in infos {
print("----")
print("Address:", String(info.loadAddress, radix: 16))
print("Path:", info.path(in: cache) ?? "unknown")
print("UUID:", info.uuid)
}
}
func testSubCaches() throws {
guard let subCaches = cache.subCaches else { return }
for subCache in subCaches {
print("----")
print("UUID:", subCache.uuid)
print("VM Offset:", String(subCache.cacheVMOffset, radix: 16))
print("File Suffix:", subCache.fileSuffix)
print(
"Path:",
cache.url.path + subCache.fileSuffix
)
}
}
func testLocalSymbolsInfo() throws {
guard let symbolsInfo = cache.localSymbolsInfo else {
return
}
let symbols = symbolsInfo.symbols(in: cache)
for symbol in symbols {
print(
String(symbol.offset, radix: 16),
symbol.demangledName
)
}
}
func testLocalSymbolsInfoEntries() throws {
guard let symbolsInfo = cache.localSymbolsInfo else {
return
}
let entries = symbolsInfo.entries(in: cache)
let symbols = Array(symbolsInfo.symbols(in: cache))
for entry in entries {
let start = entry.nlistStartIndex
let end = entry.nlistStartIndex + entry.nlistCount
print(
"Offset:", String(entry.dylibOffset, radix: 16),
"Symbols:", symbols[start ..< end].count
)
}
}
func testLocalSymbolsInSymbolCache() throws {
guard let symbolCache = try cache.symbolCache else {
return
}
guard let info = symbolCache.localSymbolsInfo else {
return
}
let machO = cache.machOFiles().first(
where: {
$0.imagePath.contains("/SwiftUICore")
}
)!
guard let entry = info.entry(for: machO, in: symbolCache) else {
XCTFail("No entry found")
return
}
let symbols = Array(info.symbols(in: symbolCache))[entry.nlistRange]
for symbol in symbols.prefix(100) {
print(symbol.name)
}
}
func testMachOFiles() throws {
let machOs = cache.machOFiles()
for machO in machOs {
print(
String(machO.headerStartOffsetInCache, radix: 16),
machO.imagePath,
machO.header.ncmds
)
}
}
func testDyld() throws {
guard let dyld = cache.dyld else { return }
let sourceVersion = dyld.loadCommands.info(of: LoadCommand.sourceVersion)!
print("Dyld:", dyld.imagePath, "dyld-\(sourceVersion.version)")
for cmd in dyld.loadCommands {
print(" -", cmd.type)
}
}
func testDylibIndices() {
let cache = self.cache1!
let indices = cache.dylibIndices
.sorted(by: { lhs, rhs in
lhs.index < rhs.index
})
let trie = cache.dylibsTrie
for index in indices {
let found = trie?.search(by: index.name)
XCTAssertNotNil(found)
XCTAssertEqual(found?.index, index.index)
print(index.index, index.name)
}
}
func testProgramOffsets() {
let cache = self.cache1!
let programOffsets = cache.programOffsets
let trie = cache.programsTrie
for programOffset in programOffsets {
let found = trie?.search(by: programOffset.name)
XCTAssertNotNil(found)
XCTAssertEqual(found?.offset, programOffset.offset)
print(programOffset.offset, programOffset.name)
}
}
func testProgramPreBuildLoaderSet() {
let cache = self.cache1!
let programOffsets = cache.programOffsets
for programOffset in programOffsets {
guard !programOffset.name.starts(with: "/cdhash") else {
continue
}
guard let loaderSet = cache.prebuiltLoaderSet(for: programOffset) else {
continue
}
print("Name:", programOffset.name)
print("Loaders:")
for loader in loaderSet.loaders(in: cache) ?? [] {
print(" \(loader.path(in: cache) ?? "unknown")")
}
for loader in loaderSet.loaders_pre1165_3(in: cache) ?? [] {
print(" \(loader.path(in: cache) ?? "unknown")")
}
let dyldCacheUUID = loaderSet.dyldCacheUUID(in: cache)
print("dyldCacheUUID:", dyldCacheUUID?.uuidString ?? "None")
let mustBeMissingPaths = loaderSet.mustBeMissingPaths(in: cache) ?? []
print("mustBeMissingPaths:")
for path in mustBeMissingPaths {
print("", path)
}
}
}
func testDylibsPreBuildLoaderSet() {
let cache = self.cache1!
guard let loaderSet = cache.dylibsPrebuiltLoaderSet else {
return
}
XCTAssertNotNil(loaderSet.version)
print("Loaders:")
for loader in loaderSet.loaders(in: cache) ?? [] {
print(" \(loader.path(in: cache) ?? "unknown")")
}
for loader in loaderSet.loaders_pre1165_3(in: cache) ?? [] {
print(" \(loader.path(in: cache) ?? "unknown")")
}
}
func testObjCOptimization() throws {
guard let objcOptimization = cache.objcOptimization else { return }
print("Version:", objcOptimization.version)
print("Flags:", objcOptimization.flags)
print("Header Info RO Cache Offset:", objcOptimization.headerInfoROCacheOffset)
print("Header Info RW Cache Offset:", objcOptimization.headerInfoRWCacheOffset)
print("Selector Hash Table Cache Offset:", objcOptimization.selectorHashTableCacheOffset)
print("Class Hash Table Cache Offset:", objcOptimization.classHashTableCacheOffset)
print("Protocol Hash Table Cache Offset:", objcOptimization.protocolHashTableCacheOffset)
print("Relative Method Selector Base Address Offset:", objcOptimization.relativeMethodSelectorBaseAddressOffset)
}
func testObjCHeaderOptimizationRW() throws {
guard let objcOptimization = cache.objcOptimization else { return }
let rw = objcOptimization.headerOptimizationRW64(in: cache)!
let rwHeaders = rw.headerInfos(in: cache)!
print("Count:", rw.count)
print("EntrySize:", rw.entrySize)
for info in rwHeaders {
print(" isLoaded: \(info.isLoaded), isAllClassesRelized: \(info.isAllClassesRelized)")
}
}
func testObjCHeaderOptimizationRO() throws {
guard let objcOptimization = cache.objcOptimization else { return }
let ro = objcOptimization.headerOptimizationRO64(in: cache)!
let roHeaders = ro.headerInfos(in: cache)!
print("Count:", ro.count)
print("EntrySize:", ro.entrySize)
print("Image Info:")
for info in roHeaders {
guard let imageInfo = info.imageInfo(in: cache) else {
print(" nil")
continue
}
print(" Flags: \(imageInfo.flags.bits), Version: \(imageInfo.version)")
}
print("Image:")
for info in roHeaders {
guard let machO = info.machO(
in: cache
) else {
print(" nil")
continue
}
let _info = ro.headerInfo(in: cache, for: machO)!
XCTAssertEqual(info.mhdr_offset, _info.mhdr_offset)
XCTAssertEqual(info.info_offset, _info.info_offset)
print(" \(machO.imagePath), offset: \(machO.headerStartOffsetInCache)")
}
}
func testSwiftOptimization() throws {
guard let swiftOptimization = cache.swiftOptimization else { return }
print("Version:", swiftOptimization.version)
print("Padding:", swiftOptimization.padding)
print("Type Conformance Hash Table Cache Offset:", swiftOptimization.typeConformanceHashTableCacheOffset)
print("Metadata Conformance Hash Table Cache Offset:", swiftOptimization.metadataConformanceHashTableCacheOffset)
print("Foreign Type Conformance Hash Table Cache Offset:", swiftOptimization.foreignTypeConformanceHashTableCacheOffset)
print("Prespecialized Data Cache Offset:", swiftOptimization.prespecializationDataCacheOffset)
print("Prespecialized Metadata Hash Table Cache Offset:", swiftOptimization.prespecializedMetadataHashTableCacheOffsets)
}
func testTproMappings() throws {
guard let mappings = cache.tproMappings else { return }
for mapping in mappings {
print("- 0x\(String(mapping.unslidAddress, radix: 16)), \(mapping.size)")
}
}
func testFunctionVariantInfo() throws {
guard let variantInfo = cache1.functionVariantInfo else { return }
print("Version:", variantInfo.layout.version)
print("Count:", variantInfo.layout.count)
guard let entries = variantInfo.entries(in: cache) else {
if variantInfo.layout.count > 0 {
XCTFail()
}
return
}
for entry in entries {
print(" ", entry.layout)
}
}
func testPrewarmingData() throws {
let caches = [cache!] + cache.subCaches!
.compactMap {
try! $0.subcache(for: cache)
}
for cache in caches {
guard let prewarmingData = cache.prewarmingData else { continue }
print("Version:", prewarmingData.layout.version)
print("Count:", prewarmingData.layout.count)
guard let entries = prewarmingData.entries(in: cache) else {
if prewarmingData.layout.count > 0 {
XCTFail()
}
return
}
for entry in entries {
print(
" ",
"cacheVMOffset:", entry.layout.cacheVMOffset,
"pages:", entry.layout.numPages
)
}
}
}
}
extension DyldCachePrintTests {
func testCodeSign() {
guard let codeSign = cache.codeSign else {
return
}
guard let superBlob = codeSign.superBlob else {
return
}
let indices = superBlob.blobIndices(in: codeSign)
print(
indices.compactMap(\.type)
)
}
func testCodeSignCodeDirectories() {
guard let codeSign = cache.codeSign else {
return
}
let directories = codeSign.codeDirectories
/* Identifier */
let identifiers = directories
.compactMap {
$0.identifier(in: codeSign)
}
print(
"identifier:",
identifiers
)
/* CD Hash */
let cdHashes = directories
.compactMap {
$0.hash(in: codeSign)
}.map {
$0.map { String(format: "%02x", $0) }.joined()
}
print(
"CDHash:",
cdHashes
)
/* Page Hashes*/
// let pageHashes = directories
// .map { directory in
// (-Int(directory.nSpecialSlots)..<Int(directory.nCodeSlots))
// .map {
// if let hash = directory.hash(forSlot: $0, in: codeSign) {
// return "\($0) " + hash.map { String(format: "%02x", $0) }.joined()
// } else {
// return "\($0) unknown"
// }
// }
// }
// print(
// "PageHashes:",
// pageHashes
// )
/* Team IDs */
let teamIDs = directories
.compactMap {
$0.teamId(in: codeSign)
}
print(
"TeamID:",
teamIDs
)
/* Exec Segment */
let execSeg = directories
.compactMap {
$0.executableSegment(in: codeSign)
}
print(
"ExecSeg:",
execSeg
)
/* Runtime */
let runtime = directories
.compactMap {
$0.runtime(in: codeSign)
}
print(
"Runtime:",
runtime
)
}
}