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 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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | /* * Copyright (c) 2017 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@ */ #ifndef DyldSharedCache_h #define DyldSharedCache_h #include <TargetConditionals.h> #include <uuid/uuid.h> #if (BUILDING_LIBDYLD || BUILDING_DYLD) #include <sys/types.h> #endif #if !(BUILDING_LIBDYLD || BUILDING_DYLD) #include <set> #include <string> #include <vector> #include <unordered_map> #include <unordered_set> #endif #include "dyld_cache_format.h" #include "CachePatching.h" #include "Diagnostics.h" #include "MachOAnalyzer.h" #if !(BUILDING_LIBDYLD || BUILDING_DYLD) #include "JSON.h" #endif namespace dyld4 { class PrebuiltLoader; struct PrebuiltLoaderSet; } namespace objc_opt { struct objc_opt_t; } namespace objc { struct HeaderInfoRO; struct HeaderInfoRW; class SelectorHashTable; class ClassHashTable; class ProtocolHashTable; } struct SwiftOptimizationHeader; struct VIS_HIDDEN ObjCOptimizationHeader { uint32_t version; uint32_t flags; uint64_t headerInfoROCacheOffset; uint64_t headerInfoRWCacheOffset; uint64_t selectorHashTableCacheOffset; uint64_t classHashTableCacheOffset; uint64_t protocolHashTableCacheOffset; uint64_t relativeMethodSelectorBaseAddressOffset; }; class VIS_HIDDEN DyldSharedCache { public: #if BUILDING_CACHE_BUILDER // FIXME: Delete this as its no longer used struct FileAlias { std::string realPath; std::string aliasPath; }; enum CodeSigningDigestMode { SHA256only = 0, SHA1only = 1, Agile = 2 }; enum class LocalSymbolsMode { keep, unmap, strip }; struct CreateOptions { std::string outputFilePath; std::string outputMapFilePath; const dyld3::GradedArchs* archs; dyld3::Platform platform; LocalSymbolsMode localSymbolMode; uint64_t cacheConfiguration; bool optimizeDyldDlopens; bool optimizeDyldLaunches; CodeSigningDigestMode codeSigningDigestMode; bool dylibsRemovedDuringMastering; bool inodesAreSameAsRuntime; bool cacheSupportsASLR; bool forSimulator; bool isLocallyBuiltCache; bool verbose; bool evictLeafDylibsOnOverflow; std::unordered_map<std::string, unsigned> dylibOrdering; std::unordered_map<std::string, unsigned> dirtyDataSegmentOrdering; dyld3::json::Node objcOptimizations; std::string loggingPrefix; // Customer and dev caches share a local symbols file. Only one will get this set to emit the file std::string localSymbolsPath; }; struct MappedMachO { MappedMachO() : mh(nullptr), length(0), isSetUID(false), protectedBySIP(false), sliceFileOffset(0), modTime(0), inode(0) { } MappedMachO(const std::string& path, const dyld3::MachOAnalyzer* p, size_t l, bool isu, bool sip, uint64_t o, uint64_t m, uint64_t i) : runtimePath(path), mh(p), length(l), isSetUID(isu), protectedBySIP(sip), sliceFileOffset(o), modTime(m), inode(i) { } std::string runtimePath; const dyld3::MachOAnalyzer* mh; size_t length; uint64_t isSetUID : 1, protectedBySIP : 1, sliceFileOffset : 62; uint64_t modTime; // only recorded if inodesAreSameAsRuntime uint64_t inode; // only recorded if inodesAreSameAsRuntime }; struct CreateResults { std::string errorMessage; std::set<std::string> warnings; std::set<const dyld3::MachOAnalyzer*> evictions; }; // This function verifies the set of dylibs that will go into the cache are self contained. That the depend on no dylibs // outset the set. It will call back the loader function to try to find any mising dylibs. static bool verifySelfContained(std::vector<MappedMachO>& dylibsToCache, std::unordered_set<std::string>& badZippered, MappedMachO (^loader)(const std::string& runtimePath, Diagnostics& diag), std::vector<std::pair<DyldSharedCache::MappedMachO, std::set<std::string>>>& excluded); // // This function is single threaded and creates a shared cache. The cache file is created in-memory. // // Inputs: // options: various per-platform flags // dylibsToCache: a list of dylibs to include in the cache // otherOsDylibs: a list of other OS dylibs and bundle which should have load info added to the cache // osExecutables: a list of main executables which should have closures created in the cache // // Returns: // On success: // cacheContent: start of the allocated cache buffer which must be vm_deallocated after the caller writes out the buffer. // cacheLength: size of the allocated cache buffer // cdHash: hash of the code directory of the code blob of the created cache // warnings: all warning messsages generated during the creation of the cache // // On failure: // cacheContent: nullptr // errorMessage: the string describing why the cache could not be created // warnings: all warning messsages generated before the failure // static CreateResults create(const CreateOptions& options, const dyld3::closure::FileSystem& fileSystem, const std::vector<MappedMachO>& dylibsToCache, const std::vector<MappedMachO>& otherOsDylibs, const std::vector<MappedMachO>& osExecutables); // // Returns a text "map" file as a big string // std::string mapFile() const; #endif // BUILDING_CACHE_BUILDER // // Returns the architecture name of the shared cache, e.g. "arm64" // const char* archName() const; // // Returns the platform the cache is for // dyld3::Platform platform() const; // // Iterates over each dylib in the cache // void forEachImage(void (^handler)(const mach_header* mh, const char* installName)) const; void forEachDylib(void (^handler)(const dyld3::MachOAnalyzer* ma, const char* installName, uint32_t imageIndex, uint64_t inode, uint64_t mtime, bool& stop)) const; // // Searches cache for dylib with specified path // bool hasImagePath(const char* dylibPath, uint32_t& imageIndex) const; // // Path is to a dylib in the cache and this is an optimized cache so that path cannot be overridden // bool hasNonOverridablePath(const char* dylibPath) const; // // Check if this shared cache file contains local symbols info // Note this might be the .symbols file, in which case this returns true // The main cache file in a split cache will return false here. // Use hasLocalSymbolsInfoFile() instead to see if a main cache has a .symbols file // const bool hasLocalSymbolsInfo() const; // // Check if this cache file has a reference to a local symbols file // const bool hasLocalSymbolsInfoFile() const; // // Get string name for a given cache type // static const char* getCacheTypeName(uint64_t cacheType); // // Searches cache for dylib with specified mach_header // bool findMachHeaderImageIndex(const mach_header* mh, uint32_t& imageIndex) const; // // Iterates over each dylib in the cache // void forEachImageEntry(void (^handler)(const char* path, uint64_t mTime, uint64_t inode)) const; // // Get image entry from index // const mach_header* getIndexedImageEntry(uint32_t index, uint64_t& mTime, uint64_t& node) const; const mach_header* getIndexedImageEntry(uint32_t index) const; // iterates over all dylibs and aliases void forEachDylibPath(void (^handler)(const char* dylibPath, uint32_t index)) const; // // If path is a dylib in the cache, return is mach_header // const dyld3::MachOFile* getImageFromPath(const char* dylibPath) const; // // Get image path from index // const char* getIndexedImagePath(uint32_t index) const; // // Get the canonical (dylib) path for a given path, which may be a symlink to something in the cache // const char* getCanonicalPath(const char* path) const; // // Iterates over each text segment in the cache // void forEachImageTextSegment(void (^handler)(uint64_t loadAddressUnslid, uint64_t textSegmentSize, const uuid_t dylibUUID, const char* installName, bool& stop)) const; // // Iterates over each of the three regions in the cache // void forEachRegion(void (^handler)(const void* content, uint64_t vmAddr, uint64_t size, uint32_t initProt, uint32_t maxProt, uint64_t flags, bool& stopRegion)) const; // // Gets a name for the mapping. // static const char* mappingName(uint32_t maxProt, uint64_t flags); // // Iterates over each of the mappings in the cache and all subCaches // After iterating over all mappings, calls the subCache handler if its not-null // void forEachRange(void (^mappingHandler)(const char* mappingName, uint64_t unslidVMAddr, uint64_t vmSize, uint32_t cacheFileIndex, uint64_t fileOffset, uint32_t initProt, uint32_t maxProt, bool& stopRange), void (^subCacheHandler)(const DyldSharedCache* subCache, uint32_t cacheFileIndex) = nullptr) const; // // Iterates over each of the subCaches, including the current cache // void forEachCache(void (^handler)(const DyldSharedCache* cache, bool& stopCache)) const; // // Returns the number of subCache files // uint32_t numSubCaches() const; // // Returns index of subCache containing the address // int32_t getSubCacheIndex(const void* addr) const; // // Gets uuid of the subCache // void getSubCacheUuid(uint8_t index, uint8_t uuid[]) const; // // Returns the vmOffset of the subCache // uint64_t getSubCacheVmOffset(uint8_t index) const; // // Returns the address of the first dyld_cache_image_info in the cache // const dyld_cache_image_info* images() const; // // Returns the number of images in the cache // uint32_t imagesCount() const; // // Get local symbols nlist entries // static const void* getLocalNlistEntries(const dyld_cache_local_symbols_info* localInfo); const void* getLocalNlistEntries() const; // // Get local symbols nlist count // const uint32_t getLocalNlistCount() const; // // Get local symbols strings // static const char* getLocalStrings(const dyld_cache_local_symbols_info* localInfo); const char* getLocalStrings() const; // // Get local symbols strings size // const uint32_t getLocalStringsSize() const; // // Iterates over each local symbol entry in the cache // void forEachLocalSymbolEntry(void (^handler)(uint64_t dylibCacheVMOffset, uint32_t nlistStartIndex, uint32_t nlistCount, bool& stop)) const; // // Returns if an address range is in this cache, and if so if in an immutable area // bool inCache(const void* addr, size_t length, bool& immutable) const; // // Returns true if a path is an alternate path (symlink) // bool isAlias(const char* path) const; // // returns address the cache would load at if unslid // uint64_t unslidLoadAddress() const; // // returns UUID of cache // void getUUID(uuid_t uuid) const; // // returns the vm size required to map cache // uint64_t mappedSize() const; // // Returns the cache PBLS, if one exists // const dyld4::PrebuiltLoaderSet* dylibsLoaderSet() const; // // searches cache for PrebuiltLoader for image // const dyld4::PrebuiltLoader* findPrebuiltLoader(const char* path) const; // // calculate how much cache was slid when loaded // intptr_t slide() const; // // iterates all pre-built closures for program // void forEachLaunchLoaderSet(void (^handler)(const char* executableRuntimePath, const dyld4::PrebuiltLoaderSet* pbls)) const; // // searches cache for PrebuiltLoader for program // const dyld4::PrebuiltLoaderSet* findLaunchLoaderSet(const char* executablePath) const; // // searches cache for PrebuiltLoader for program // const dyld4::PrebuiltLoaderSet* findLaunchLoaderSetWithCDHash(const char* cdHashString) const; // // searches cache for PrebuiltLoader for program by cdHash // bool hasLaunchLoaderSetWithCDHash(const char* cdHashString) const; // // Returns the pointer to the slide info for this cache // const dyld_cache_slide_info* legacyCacheSlideInfo() const; // // Returns a pointer to the __DATA region mapping in the cache // const dyld_cache_mapping_info* legacyCacheDataRegionMapping() const; // // Returns a pointer to the start of the __DATA region in the cache // const uint8_t* legacyCacheDataRegionBuffer() const; // // Returns a pointer to the shared cache optimized Objective-C pointer structures // const void* objcOptPtrs() const; bool hasOptimizedObjC() const; uint32_t objcOptVersion() const; uint32_t objcOptFlags() const; const objc::HeaderInfoRO* objcHeaderInfoRO() const; const objc::HeaderInfoRW* objcHeaderInfoRW() const; const objc::SelectorHashTable* objcSelectorHashTable() const; const objc::ClassHashTable* objcClassHashTable() const; const objc::ProtocolHashTable* objcProtocolHashTable() const; const void* objcRelativeMethodListsBaseAddress() const; #if !(BUILDING_LIBDYLD || BUILDING_DYLD) // // In Large Shared Caches, shared cache relative method lists are offsets from the magic // selector in libobjc. // Returns the VM address of that selector, if it exists // uint64_t sharedCacheRelativeSelectorBaseVMAddress() const; #endif // // Returns a pointer to the shared cache optimized Swift data structures // const SwiftOptimizationHeader* swiftOpt() const; // Returns true if the cache has any slide info, either old style on a single data region // or on each individual data mapping bool hasSlideInfo() const; void forEachSlideInfo(void (^handler)(uint64_t mappingStartAddress, uint64_t mappingSize, const uint8_t* mappingPagesStart, uint64_t slideInfoOffset, uint64_t slideInfoSize, const dyld_cache_slide_info* slideInfoHeader)) const; // // returns true if the offset is in the TEXT of some cached dylib and sets *index to the dylib index // bool addressInText(uint64_t cacheOffset, uint32_t* index) const; const void* patchTable() const; uint32_t patchInfoVersion() const; uint32_t patchableExportCount(uint32_t imageIndex) const; void forEachPatchableExport(uint32_t imageIndex, void (^handler)(uint32_t dylibVMOffsetOfImpl, const char* exportName, PatchKind kind)) const; #if BUILDING_SHARED_CACHE_UTIL void forEachPatchableUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl, void (^handler)(uint32_t userImageIndex, uint32_t userVMOffset, dyld3::MachOLoaded::PointerMetaData pmd, uint64_t addend, bool isWeakImport)) const; #endif // Use this when you have a root of at imageIndex, and are trying to patch a cached dylib at userImageIndex bool shouldPatchClientOfImage(uint32_t imageIndex, uint32_t userImageIndex) const; void forEachPatchableUseOfExportInImage(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl, uint32_t userImageIndex, void (^handler)(uint32_t userVMOffset, dyld3::MachOLoaded::PointerMetaData pmd, uint64_t addend, bool isWeakImport)) const; // Note, use this for weak-defs when you just want all uses of an export, regardless of which dylib they are in. void forEachPatchableUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl, void (^handler)(uint64_t cacheVMOffset, dyld3::MachOLoaded::PointerMetaData pmd, uint64_t addend, bool isWeakImport)) const; // Used to walk just the GOT uses of a given export. The above method will walk both regular and GOT uses void forEachPatchableGOTUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl, void (^handler)(uint64_t cacheVMOffset, dyld3::MachOLoaded::PointerMetaData pmd, uint64_t addend, bool isWeakImport)) const; #if !(BUILDING_LIBDYLD || BUILDING_DYLD) // MRM map file generator std::string generateJSONMap(const char* disposition, uuid_t cache_uuid, bool verbose) const; // This generates a JSON representation of deep reverse dependency information in the cache. // For each dylib, the output will contain the list of all the other dylibs transitively // dependening on that library. (For example, the entry for libsystem will contain almost // all of the dylibs in the cache ; a very high-level framework such as ARKit will have way // fewer dependents). // This is used by the shared cache ordering script to put "deep" dylibs used by everybody // closer to the center of the cache. std::string generateJSONDependents() const; #endif // Note these enum entries are only valid for 64-bit archs. enum class ConstantClasses { cfStringAtomSize = 32 }; // Returns the start and size of the range in the shared cache of the ObjC constants, such as // all of the CFString's which have been moved in to a contiguous range std::pair<const void*, uint64_t> getObjCConstantRange() const; #if !(BUILDING_LIBDYLD || BUILDING_DYLD) dyld3::MachOAnalyzer::VMAddrConverter makeVMAddrConverter(bool contentRebased) const; #endif // Returns true if the given MachO is in the shared cache range. // Returns false if the cache is null. static bool inDyldCache(const DyldSharedCache* cache, const dyld3::MachOFile* mf); // Returns ture if the given path is a subCache filepath. static bool isSubCachePath(const char* leafName); #if !(BUILDING_LIBDYLD || BUILDING_DYLD) // // Apply rebases for manually mapped shared cache // void applyCacheRebases() const; // mmap() an shared cache file read/only but laid out like it would be at runtime static const DyldSharedCache* mapCacheFile(const char* path, uint64_t baseCacheUnslidAddress, uint8_t* buffer); static std::vector<const DyldSharedCache*> mapCacheFiles(const char* path); #endif dyld_cache_header header; // The most mappings we could generate. // For now its __TEXT, __DATA_CONST, __DATA_DIRTY, __DATA, __LINKEDIT, // and optionally also __AUTH, __AUTH_CONST, __AUTH_DIRTY static const uint32_t MaxMappings = 8; private: // Returns a variable of type "const T" which corresponds to the header field with the given unslid address template<typename T> const T getAddrField(uint64_t addr) const; #if !(BUILDING_LIBDYLD || BUILDING_DYLD) void fillMachOAnalyzersMap(std::unordered_map<std::string,dyld3::MachOAnalyzer*> & dylibAnalyzers) const; void computeReverseDependencyMapForDylib(std::unordered_map<std::string, std::set<std::string>> &reverseDependencyMap, const std::unordered_map<std::string,dyld3::MachOAnalyzer*> & dylibAnalyzers, const std::string &loadPath) const; void computeReverseDependencyMap(std::unordered_map<std::string, std::set<std::string>> &reverseDependencyMap) const; void findDependentsRecursively(std::unordered_map<std::string, std::set<std::string>> &transitiveDependents, const std::unordered_map<std::string, std::set<std::string>> &reverseDependencyMap, std::set<std::string> & visited, const std::string &loadPath) const; void computeTransitiveDependents(std::unordered_map<std::string, std::set<std::string>> & transitiveDependents) const; #endif // // Returns a pointer to the old shared cache optimized Objective-C data structures // const objc_opt::objc_opt_t* oldObjcOpt() const; // // Returns a pointer to the new shared cache optimized Objective-C data structures // const ObjCOptimizationHeader* objcOpts() const; }; #endif /* DyldSharedCache_h */ |