Loading...
dyld3/shared-cache/DyldSharedCache.cpp dyld-551.4 dyld-625.13
--- dyld/dyld-551.4/dyld3/shared-cache/DyldSharedCache.cpp
+++ dyld/dyld-625.13/dyld3/shared-cache/DyldSharedCache.cpp
@@ -37,7 +37,7 @@
 #include <CommonCrypto/CommonDigest.h>
 #include <CommonCrypto/CommonDigestSPI.h>
 
-#if !DYLD_IN_PROCESS
+#if BUILDING_CACHE_BUILDER
 #include <set>
 #include <string>
 #include <vector>
@@ -46,59 +46,83 @@
 #endif
 
 #define NO_ULEB
-#include "MachOParser.h"
+#include "MachOLoaded.h"
+#include "ClosureFileSystemPhysical.h"
 #include "CacheBuilder.h"
 #include "DyldSharedCache.h"
-#include "LaunchCache.h"
 #include "Trie.hpp"
 #include "StringUtils.h"
-
-
-
-#if !DYLD_IN_PROCESS
+#include "FileUtils.h"
+
+
+
+#if BUILDING_CACHE_BUILDER
 DyldSharedCache::CreateResults DyldSharedCache::create(const CreateOptions&             options,
                                                        const std::vector<MappedMachO>&  dylibsToCache,
                                                        const std::vector<MappedMachO>&  otherOsDylibs,
                                                        const std::vector<MappedMachO>&  osExecutables)
 {
     CreateResults  results;
-    CacheBuilder   cache(options);
-
-    cache.build(dylibsToCache, otherOsDylibs, osExecutables);
+    const char* prefix = nullptr;
+    if ( (options.pathPrefixes.size() == 1) && !options.pathPrefixes[0].empty() )
+        prefix = options.pathPrefixes[0].c_str();
+    // FIXME: This prefix will be applied to dylib closures and executable closures, even though
+    // the old code didn't have a prefix on cache dylib closures
+    dyld3::closure::FileSystemPhysical fileSystem(prefix);
+    CacheBuilder   cache(options, fileSystem);
+    if (!cache.errorMessage().empty()) {
+        results.errorMessage = cache.errorMessage();
+        return results;
+    }
+
+    std::vector<FileAlias> aliases;
+    switch ( options.platform ) {
+        case dyld3::Platform::iOS:
+        case dyld3::Platform::watchOS:
+        case dyld3::Platform::tvOS:
+            // FIXME: embedded cache builds should be getting aliases from manifest
+            aliases.push_back({"/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit", "/System/Library/Frameworks/IOKit.framework/IOKit"});
+            aliases.push_back({"/usr/lib/libstdc++.6.dylib",                                  "/usr/lib/libstdc++.dylib"});
+            aliases.push_back({"/usr/lib/libstdc++.6.dylib",                                  "/usr/lib/libstdc++.6.0.9.dylib"});
+            aliases.push_back({"/usr/lib/libz.1.dylib",                                       "/usr/lib/libz.dylib"});
+            aliases.push_back({"/usr/lib/libSystem.B.dylib",                                  "/usr/lib/libSystem.dylib"});
+            break;
+        default:
+            break;
+    }
+
+    cache.build(dylibsToCache, otherOsDylibs, osExecutables, aliases);
 
     results.agileSignature = cache.agileSignature();
-    results.cdHashFirst = cache.cdHashFirst();
-    results.cdHashSecond = cache.cdHashSecond();
-    results.warnings = cache.warnings();
-    results.evictions = cache.evictions();
-
+    results.cdHashFirst    = cache.cdHashFirst();
+    results.cdHashSecond   = cache.cdHashSecond();
+    results.warnings       = cache.warnings();
+    results.evictions      = cache.evictions();
     if ( cache.errorMessage().empty() ) {
-        results.cacheContent = cache.buffer();
-        results.cacheLength  = cache.bufferSize();
-    }
-    else {
-        cache.deleteBuffer();
-        results.cacheContent = nullptr;
-        results.cacheLength  = 0;
-        results.errorMessage = cache.errorMessage();
-    }
+        if ( !options.outputFilePath.empty() )  {
+            // write cache file, if path non-empty
+            cache.writeFile(options.outputFilePath);
+        }
+        if ( !options.outputMapFilePath.empty() ) {
+            // write map file, if path non-empty
+            cache.writeMapFile(options.outputMapFilePath);
+        }
+    }
+    results.errorMessage = cache.errorMessage();
+    cache.deleteBuffer();
     return results;
 }
 
 bool DyldSharedCache::verifySelfContained(std::vector<MappedMachO>& dylibsToCache, MappedMachO (^loader)(const std::string& runtimePath), std::vector<std::pair<DyldSharedCache::MappedMachO, std::set<std::string>>>& rejected)
 {
-
     // build map of dylibs
     __block std::map<std::string, std::set<std::string>> badDylibs;
     __block std::set<std::string> knownDylibs;
     for (const DyldSharedCache::MappedMachO& dylib : dylibsToCache) {
         std::set<std::string> reasons;
-        dyld3::MachOParser parser(dylib.mh);
-        if (parser.canBePlacedInDyldCache(dylib.runtimePath, reasons)) {
+        if ( dylib.mh->canBePlacedInDyldCache(dylib.runtimePath.c_str(), ^(const char* msg) { badDylibs[dylib.runtimePath].insert(msg);}) ) {
             knownDylibs.insert(dylib.runtimePath);
-            knownDylibs.insert(parser.installName());
-        } else {
-            badDylibs[dylib.runtimePath] = reasons;
+            knownDylibs.insert(dylib.mh->installName());
         }
     }
 
@@ -111,41 +135,33 @@
         for (const DyldSharedCache::MappedMachO& dylib : dylibsToCache) {
             if ( badDylibs.count(dylib.runtimePath) != 0 )
                 continue;
-            dyld3::MachOParser parser(dylib.mh);
-            parser.forEachDependentDylib(^(const char* loadPath, bool isWeak, bool isReExport, bool isUpward, uint32_t compatVersion, uint32_t curVersion, bool& stop) {
+            dylib.mh->forEachDependentDylib(^(const char* loadPath, bool isWeak, bool isReExport, bool isUpward, uint32_t compatVersion, uint32_t curVersion, bool& stop) {
                 if ( knownDylibs.count(loadPath) == 0 ) {
                     doAgain = true;
                     MappedMachO foundMapping;
                     if ( badDylibs.count(loadPath) == 0 )
                         foundMapping = loader(loadPath);
                     if ( foundMapping.length == 0 ) {
-                        std::string reason = std::string("Could not find dependency '") + loadPath +"'";
-                        auto i = badDylibs.find(dylib.runtimePath);
-                        if (i == badDylibs.end()) {
-                            std::set<std::string> reasons;
-                            reasons.insert(reason);
-                            badDylibs[dylib.runtimePath] = reasons;
-                        } else {
-                            i->second.insert(reason);
-                        }
+                        badDylibs[dylib.runtimePath].insert(std::string("Could not find dependency '") + loadPath +"'");
                         knownDylibs.erase(dylib.runtimePath);
-                        dyld3::MachOParser parserBad(dylib.mh);
-                        knownDylibs.erase(parserBad.installName());
+                        knownDylibs.erase(dylib.mh->installName());
                     }
                     else {
-                        dyld3::MachOParser foundParser(foundMapping.mh);
                         std::set<std::string> reasons;
-                        if (foundParser.canBePlacedInDyldCache(foundParser.installName(), reasons)) {
-                            foundMappings.push_back(foundMapping);
+                        if ( foundMapping.mh->canBePlacedInDyldCache(foundMapping.runtimePath.c_str(), ^(const char* msg) { badDylibs[foundMapping.runtimePath].insert(msg);})) {
+                            // see if existing mapping was returned
+                            bool alreadyInVector = false;
+                            for (const MappedMachO& existing : dylibsToCache) {
+                                if ( existing.mh == foundMapping.mh ) {
+                                    alreadyInVector = true;
+                                    break;
+                                }
+                            }
+                            if ( !alreadyInVector )
+                                foundMappings.push_back(foundMapping);
+                            knownDylibs.insert(loadPath);
                             knownDylibs.insert(foundMapping.runtimePath);
-                            knownDylibs.insert(foundParser.installName());
-                        } else {
-                            auto i = badDylibs.find(dylib.runtimePath);
-                            if (i == badDylibs.end()) {
-                                badDylibs[dylib.runtimePath] = reasons;
-                            } else {
-                                i->second.insert(reasons.begin(), reasons.end());
-                            }
+                            knownDylibs.insert(foundMapping.mh->installName());
                         }
                    }
                 }
@@ -177,6 +193,33 @@
     for (const dyld_cache_mapping_info* m=mappings; m < mappingsEnd; ++m) {
         handler((char*)this + m->fileOffset, m->address, m->size, m->initProt);
     }
+}
+
+bool DyldSharedCache::inCache(const void* addr, size_t length, bool& readOnly) const
+{
+    // quick out if before start of cache
+    if ( addr < this )
+        return false;
+
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    uintptr_t slide = (uintptr_t)this - (uintptr_t)(mappings[0].address);
+    uintptr_t unslidStart = (uintptr_t)addr - slide;
+
+    // quick out if after end of cache
+    if ( unslidStart > (mappings[2].address + mappings[2].size) )
+        return false;
+
+    // walk cache regions
+    const dyld_cache_mapping_info* mappingsEnd = &mappings[header.mappingCount];
+    uintptr_t unslidEnd = unslidStart + length;
+    for (const dyld_cache_mapping_info* m=mappings; m < mappingsEnd; ++m) {
+        if ( (unslidStart >= m->address) && (unslidEnd < (m->address+m->size)) ) {
+            readOnly = ((m->initProt & VM_PROT_WRITE) == 0);
+            return true;
+        }
+    }
+
+    return false;
 }
 
 void DyldSharedCache::forEachImage(void (^handler)(const mach_header* mh, const char* installName)) const
@@ -220,22 +263,49 @@
     }
 }
 
-void DyldSharedCache::forEachImageTextSegment(void (^handler)(uint64_t loadAddressUnslid, uint64_t textSegmentSize, const uuid_t dylibUUID, const char* installName)) const
+const mach_header* DyldSharedCache::getIndexedImageEntry(uint32_t index, uint64_t& mTime, uint64_t& inode) const
+{
+    const dyld_cache_image_info*   dylibs   = (dyld_cache_image_info*)((char*)this + header.imagesOffset);
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    mTime = dylibs[index].modTime;
+    inode = dylibs[index].inode;
+    return (mach_header*)((uint8_t*)this + dylibs[index].address - mappings[0].address);
+}
+
+void DyldSharedCache::forEachImageTextSegment(void (^handler)(uint64_t loadAddressUnslid, uint64_t textSegmentSize, const uuid_t dylibUUID, const char* installName, bool& stop)) const
 {
     // check for old cache without imagesText array
     if ( header.mappingOffset < 123 )
         return;
 
+    // walk imageText table and call callback for each entry
+    const dyld_cache_image_text_info* imagesText = (dyld_cache_image_text_info*)((char*)this + header.imagesTextOffset);
+    const dyld_cache_image_text_info* imagesTextEnd = &imagesText[header.imagesTextCount];
+    bool stop = false;
+    for (const dyld_cache_image_text_info* p=imagesText; p < imagesTextEnd && !stop; ++p) {
+        handler(p->loadAddress, p->textSegmentSize, p->uuid, (char*)this + p->pathOffset, stop);
+    }
+}
+
+bool DyldSharedCache::addressInText(uint32_t cacheOffset, uint32_t* imageIndex) const
+{
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    if ( cacheOffset > mappings[0].size )
+        return false;
+    uint64_t targetAddr = mappings[0].address + cacheOffset;
     // walk imageText table and call callback for each entry
     const dyld_cache_image_text_info* imagesText = (dyld_cache_image_text_info*)((char*)this + header.imagesTextOffset);
     const dyld_cache_image_text_info* imagesTextEnd = &imagesText[header.imagesTextCount];
     for (const dyld_cache_image_text_info* p=imagesText; p < imagesTextEnd; ++p) {
-        handler(p->loadAddress, p->textSegmentSize, p->uuid, (char*)this + p->pathOffset);
-    }
-}
-
-
-std::string DyldSharedCache::archName() const
+        if ( (p->loadAddress <= targetAddr) && (targetAddr < p->loadAddress+p->textSegmentSize) ) {
+            *imageIndex = (uint32_t)(p-imagesText);
+            return true;
+        }
+    }
+    return false;
+}
+
+const char* DyldSharedCache::archName() const
 {
     const char* archSubString = ((char*)this) + 8;
     while (*archSubString == ' ')
@@ -244,12 +314,12 @@
 }
 
 
-uint32_t DyldSharedCache::platform() const
-{
-    return header.platform;
-}
-
-#if !DYLD_IN_PROCESS
+dyld3::Platform DyldSharedCache::platform() const
+{
+    return (dyld3::Platform)header.platform;
+}
+
+#if BUILDING_CACHE_BUILDER
 std::string DyldSharedCache::mapFile() const
 {
     __block std::string             result;
@@ -280,10 +350,10 @@
 
     forEachImage(^(const mach_header* mh, const char* installName) {
         result += std::string(installName) + "\n";
-        dyld3::MachOParser parser(mh);
-        parser.forEachSegment(^(const char* segName, uint32_t fileOffset, uint32_t fileSize, uint64_t vmAddr, uint64_t vmSize, uint8_t protections, bool& stop) {
+        const dyld3::MachOFile* mf = (dyld3::MachOFile*)mh;
+        mf->forEachSegment(^(const dyld3::MachOFile::SegmentInfo& info, bool& stop) {
             char lineBuffer[256];
-            sprintf(lineBuffer, "\t%16s 0x%08llX -> 0x%08llX\n", segName, vmAddr, vmAddr+vmSize);
+            sprintf(lineBuffer, "\t%16s 0x%08llX -> 0x%08llX\n", info.segName, info.vmAddr, info.vmAddr+info.vmSize);
             result += lineBuffer;
         });
         result += "\n";
@@ -319,12 +389,182 @@
     return (endAddr - startAddr);
 }
 
-
-
-
-
-
-
-
-
-
+bool DyldSharedCache::findMachHeaderImageIndex(const mach_header* mh, uint32_t& imageIndex) const
+{
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    uintptr_t slide = (uintptr_t)this - (uintptr_t)(mappings[0].address);
+    uint64_t unslidMh = (uintptr_t)mh - slide;
+    const dyld_cache_image_info* dylibs = (dyld_cache_image_info*)((char*)this + header.imagesOffset);
+    for (uint32_t i=0; i < header.imagesCount; ++i) {
+        if ( dylibs[i].address == unslidMh ) {
+            imageIndex = i;
+            return true;
+        }
+    }
+    return false;
+}
+
+bool DyldSharedCache::hasImagePath(const char* dylibPath, uint32_t& imageIndex) const
+{
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    if ( mappings[0].fileOffset != 0 )
+        return false;
+    if ( header.mappingOffset >= 0x118 ) {
+        uintptr_t      slide           = (uintptr_t)this - (uintptr_t)(mappings[0].address);
+        const uint8_t* dylibTrieStart  = (uint8_t*)(this->header.dylibsTrieAddr + slide);
+        const uint8_t* dylibTrieEnd    = dylibTrieStart + this->header.dylibsTrieSize;
+
+        Diagnostics diag;
+        const uint8_t* imageNode = dyld3::MachOLoaded::trieWalk(diag, dylibTrieStart, dylibTrieEnd, dylibPath);
+        if ( imageNode != NULL ) {
+            imageIndex = (uint32_t)dyld3::MachOFile::read_uleb128(diag, imageNode, dylibTrieEnd);
+            return true;
+        }
+    }
+    else {
+        const dyld_cache_image_info* dylibs = (dyld_cache_image_info*)((char*)this + header.imagesOffset);
+        uint64_t firstImageOffset = 0;
+        uint64_t firstRegionAddress = mappings[0].address;
+        for (uint32_t i=0; i < header.imagesCount; ++i) {
+            const char* aPath  = (char*)this + dylibs[i].pathFileOffset;
+            if ( strcmp(aPath, dylibPath) == 0 ) {
+                imageIndex = i;
+                return true;
+            }
+            uint64_t offset = dylibs[i].address - firstRegionAddress;
+            if ( firstImageOffset == 0 )
+                firstImageOffset = offset;
+            // skip over aliases
+            if ( dylibs[i].pathFileOffset < firstImageOffset)
+                continue;
+        }
+    }
+
+    return false;
+}
+
+const dyld3::closure::Image* DyldSharedCache::findDlopenOtherImage(const char* path) const
+{
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    if ( mappings[0].fileOffset != 0 )
+        return nullptr;
+    if ( header.mappingOffset < sizeof(dyld_cache_header) )
+        return nullptr;
+    if ( header.otherImageArrayAddr == 0 )
+        return nullptr;
+    uintptr_t      slide           = (uintptr_t)this - (uintptr_t)(mappings[0].address);
+    const uint8_t* dylibTrieStart  = (uint8_t*)(this->header.otherTrieAddr + slide);
+    const uint8_t* dylibTrieEnd    = dylibTrieStart + this->header.otherTrieSize;
+
+    Diagnostics diag;
+    const uint8_t* imageNode = dyld3::MachOLoaded::trieWalk(diag, dylibTrieStart, dylibTrieEnd, path);
+    if ( imageNode != NULL ) {
+        dyld3::closure::ImageNum imageNum = (uint32_t)dyld3::MachOFile::read_uleb128(diag, imageNode, dylibTrieEnd);
+        uint64_t arrayAddrOffset = header.otherImageArrayAddr - mappings[0].address;
+        const dyld3::closure::ImageArray* otherImageArray = (dyld3::closure::ImageArray*)((char*)this + arrayAddrOffset);
+        return otherImageArray->imageForNum(imageNum);
+    }
+
+    return nullptr;
+}
+
+
+
+
+const dyld3::closure::LaunchClosure* DyldSharedCache::findClosure(const char* executablePath) const
+{
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    uintptr_t      slide                = (uintptr_t)this - (uintptr_t)(mappings[0].address);
+    const uint8_t* executableTrieStart  = (uint8_t*)(this->header.progClosuresTrieAddr + slide);
+    const uint8_t* executableTrieEnd    = executableTrieStart + this->header.progClosuresTrieSize;
+    const uint8_t* closuresStart        = (uint8_t*)(this->header.progClosuresAddr + slide);
+
+    Diagnostics diag;
+    const uint8_t* imageNode = dyld3::MachOLoaded::trieWalk(diag, executableTrieStart, executableTrieEnd, executablePath);
+    if ( (imageNode == NULL) && (strncmp(executablePath, "/System/", 8) == 0) ) {
+        // anything in /System/ should have a closure.  Perhaps it was launched via symlink path
+        char realPath[PATH_MAX];
+        if ( realpath(executablePath, realPath) != NULL )
+            imageNode = dyld3::MachOLoaded::trieWalk(diag, executableTrieStart, executableTrieEnd, realPath);
+    }
+    if ( imageNode != NULL ) {
+        uint32_t closureOffset = (uint32_t)dyld3::MachOFile::read_uleb128(diag, imageNode, executableTrieEnd);
+        if ( closureOffset < this->header.progClosuresSize )
+            return (dyld3::closure::LaunchClosure*)((uint8_t*)closuresStart + closureOffset);
+    }
+
+    return nullptr;
+}
+
+#if !BUILDING_LIBDYLD && !BUILDING_DYLD
+void DyldSharedCache::forEachLaunchClosure(void (^handler)(const char* executableRuntimePath, const dyld3::closure::LaunchClosure* closure)) const
+{
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    uintptr_t      slide                = (uintptr_t)this - (uintptr_t)(mappings[0].address);
+    const uint8_t* executableTrieStart  = (uint8_t*)(this->header.progClosuresTrieAddr + slide);
+    const uint8_t* executableTrieEnd    = executableTrieStart + this->header.progClosuresTrieSize;
+    const uint8_t* closuresStart        = (uint8_t*)(this->header.progClosuresAddr + slide);
+
+    std::vector<DylibIndexTrie::Entry> closureEntries;
+    if ( Trie<DylibIndex>::parseTrie(executableTrieStart, executableTrieEnd, closureEntries) ) {
+        for (DylibIndexTrie::Entry& entry : closureEntries ) {
+            uint32_t offset = entry.info.index;
+            if ( offset < this->header.progClosuresSize )
+                handler(entry.name.c_str(), (const dyld3::closure::LaunchClosure*)(closuresStart+offset));
+        }
+    }
+}
+
+void DyldSharedCache::forEachDlopenImage(void (^handler)(const char* runtimePath, const dyld3::closure::Image* image)) const
+{
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    uintptr_t      slide           = (uintptr_t)this - (uintptr_t)(mappings[0].address);
+    const uint8_t* otherTrieStart  = (uint8_t*)(this->header.otherTrieAddr + slide);
+    const uint8_t* otherTrieEnd    = otherTrieStart + this->header.otherTrieSize;
+
+    std::vector<DylibIndexTrie::Entry> otherEntries;
+    if ( Trie<DylibIndex>::parseTrie(otherTrieStart, otherTrieEnd, otherEntries) ) {
+        for (const DylibIndexTrie::Entry& entry : otherEntries ) {
+            dyld3::closure::ImageNum imageNum = entry.info.index;
+            uint64_t arrayAddrOffset = header.otherImageArrayAddr - mappings[0].address;
+            const dyld3::closure::ImageArray* otherImageArray = (dyld3::closure::ImageArray*)((char*)this + arrayAddrOffset);
+            handler(entry.name.c_str(), otherImageArray->imageForNum(imageNum));
+        }
+    }
+}
+#endif
+
+const dyld3::closure::ImageArray* DyldSharedCache::cachedDylibsImageArray() const
+{
+    // check for old cache without imagesArray
+    if ( header.mappingOffset < 0x100 )
+        return nullptr;
+
+    if ( header.dylibsImageArrayAddr == 0 )
+        return nullptr;
+        
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    uint64_t arrayAddrOffset = header.dylibsImageArrayAddr - mappings[0].address;
+    return (dyld3::closure::ImageArray*)((char*)this + arrayAddrOffset);
+}
+
+const dyld3::closure::ImageArray* DyldSharedCache::otherOSImageArray() const
+{
+    // check for old cache without imagesArray
+    if ( header.mappingOffset < sizeof(dyld_cache_header) )
+        return nullptr;
+
+    if ( header.otherImageArrayAddr == 0 )
+        return nullptr;
+
+    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
+    uint64_t arrayAddrOffset = header.otherImageArrayAddr - mappings[0].address;
+    return (dyld3::closure::ImageArray*)((char*)this + arrayAddrOffset);
+}
+
+
+
+
+
+
+