Loading...
common/DyldSharedCache.cpp dyld-1162 dyld-1042.1
--- dyld/dyld-1162/common/DyldSharedCache.cpp
+++ dyld/dyld-1042.1/common/DyldSharedCache.cpp
@@ -22,9 +22,6 @@
  * @APPLE_LICENSE_HEADER_END@
  */
 
-#include <TargetConditionals.h>
-
-#if !TARGET_OS_EXCLAVEKIT
 
 #include <dirent.h>
 #include <sys/errno.h>
@@ -282,40 +279,6 @@
     }
 }
 
-const char* DyldSharedCache::mappingName(uint32_t maxProt, uint64_t flags)
-{
-    const char* mappingName = "";
-    if ( maxProt & VM_PROT_EXECUTE ) {
-        if ( flags & DYLD_CACHE_MAPPING_TEXT_STUBS ) {
-            mappingName = "__TEXT_STUBS";
-        } else {
-            mappingName = "__TEXT";
-        }
-    } else if ( maxProt & VM_PROT_WRITE ) {
-        if ( flags & DYLD_CACHE_MAPPING_AUTH_DATA ) {
-            if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
-                mappingName = "__AUTH_DIRTY";
-            else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
-                mappingName = "__AUTH_CONST";
-            else
-                mappingName = "__AUTH";
-        } else {
-            if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
-                mappingName = "__DATA_DIRTY";
-            else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
-                mappingName = "__DATA_CONST";
-            else
-                mappingName = "__DATA";
-        }
-    }
-    else if ( maxProt & VM_PROT_READ ) {
-        mappingName = "__LINKEDIT";
-    } else {
-        mappingName = "*unknown*";
-    }
-    return mappingName;
-}
-
 void DyldSharedCache::forEachRange(void (^handler)(const char* mappingName,
                                                    uint64_t unslidVMAddr, uint64_t vmSize,
                                                    uint32_t cacheFileIndex, uint64_t fileOffset,
@@ -327,7 +290,35 @@
     forEachCache(^(const DyldSharedCache *cache, bool& stopCache) {
         cache->forEachRegion(^(const void *content, uint64_t unslidVMAddr, uint64_t size,
                         uint32_t initProt, uint32_t maxProt, uint64_t flags, bool& stopRegion) {
-            const char* mappingName = DyldSharedCache::mappingName(maxProt, flags);
+            const char* mappingName = "";
+            if ( maxProt & VM_PROT_EXECUTE ) {
+                if ( flags & DYLD_CACHE_MAPPING_TEXT_STUBS ) {
+                    mappingName = "__TEXT_STUBS";
+                } else {
+                    mappingName = "__TEXT";
+                }
+            } else if ( maxProt & VM_PROT_WRITE ) {
+                if ( flags & DYLD_CACHE_MAPPING_AUTH_DATA ) {
+                    if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
+                        mappingName = "__AUTH_DIRTY";
+                    else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
+                        mappingName = "__AUTH_CONST";
+                    else
+                        mappingName = "__AUTH";
+                } else {
+                    if ( flags & DYLD_CACHE_MAPPING_DIRTY_DATA )
+                        mappingName = "__DATA_DIRTY";
+                    else if ( flags & DYLD_CACHE_MAPPING_CONST_DATA )
+                        mappingName = "__DATA_CONST";
+                    else
+                        mappingName = "__DATA";
+                }
+            }
+            else if ( maxProt & VM_PROT_READ ) {
+                mappingName = "__LINKEDIT";
+            } else {
+                mappingName = "*unknown*";
+            }
             uint64_t fileOffset = (uint8_t*)content - (uint8_t*)cache;
             bool stop = false;
             handler(mappingName, unslidVMAddr, size, cacheFileIndex, fileOffset, initProt, maxProt, stop);
@@ -361,7 +352,7 @@
         return;
 
     for (uint32_t i = 0; i != header.subCacheArrayCount; ++i) {
-        const DyldSharedCache* cache = (const DyldSharedCache*)((uintptr_t)this + this->getSubCacheVmOffset(i));
+        const DyldSharedCache* cache = (const DyldSharedCache*)((uint8_t*)this + this->getSubCacheVmOffset(i));
         handler(cache, stop);
         if ( stop )
             return;
@@ -395,25 +386,25 @@
 
 void DyldSharedCache::getSubCacheUuid(uint8_t index, uint8_t uuid[]) const {
     if (header.mappingOffset <= __offsetof(dyld_cache_header, cacheSubType) ) {
-        const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uintptr_t)this + header.subCacheArrayOffset);
+        const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uint8_t*)this + header.subCacheArrayOffset);
         memcpy(uuid, subCacheEntries[index].uuid, 16);
     } else {
-        const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uintptr_t)this + header.subCacheArrayOffset);
+        const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uint8_t*)this + header.subCacheArrayOffset);
         memcpy(uuid, subCacheEntries[index].uuid, 16);
     }
 }
 
 uint64_t DyldSharedCache::getSubCacheVmOffset(uint8_t index) const {
     if (header.mappingOffset <= __offsetof(dyld_cache_header, cacheSubType) ) {
-        const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uintptr_t)this + header.subCacheArrayOffset);
+        const dyld_subcache_entry_v1* subCacheEntries = (dyld_subcache_entry_v1*)((uint8_t*)this + header.subCacheArrayOffset);
         return subCacheEntries[index].cacheVMOffset;
     } else {
-        const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uintptr_t)this + header.subCacheArrayOffset);
+        const dyld_subcache_entry* subCacheEntries = (dyld_subcache_entry*)((uint8_t*)this + header.subCacheArrayOffset);
         return subCacheEntries[index].cacheVMOffset;
     }
 }
 
-bool DyldSharedCache::inCache(const void* addr, size_t length, bool& immutable) const
+bool DyldSharedCache::inCache(const void* addr, size_t length, bool& readOnly) const
 {
     // quick out if before start of cache
     if ( addr < this )
@@ -423,19 +414,22 @@
     uintptr_t slide = (uintptr_t)this - (uintptr_t)(mappings[0].address);
     uintptr_t unslidStart = (uintptr_t)addr - slide;
 
-    // walk cache ranges
-    __block bool found = false;
-    auto inRange = ^(const char* mappingName, uint64_t unslidVMAddr, uint64_t vmSize, uint32_t cacheFileIndex,
-                     uint64_t fileOffset, uint32_t initProt, uint32_t maxProt, bool& stopRange) {
-        if ( (unslidVMAddr <= unslidStart) && ((unslidStart+length) < (unslidVMAddr+vmSize)) ) {
-            found     = true;
-            immutable = ((maxProt & VM_PROT_WRITE) == 0);
-            stopRange = true;
-        }
-    };
-    this->forEachRange(inRange, nullptr);
-
-    return found;
+    // quick out if after end of cache
+    const dyld_cache_mapping_info* lastMapping = &mappings[header.mappingCount - 1];
+    if ( unslidStart > (lastMapping->address + lastMapping->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;
 }
 
 bool DyldSharedCache::isAlias(const char* path) const {
@@ -548,7 +542,7 @@
     // check for cache without local symbols info
     if (!this->hasLocalSymbolsInfo())
         return nullptr;
-    const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+    const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
     return getLocalNlistEntries(localInfo);
 }
 
@@ -557,7 +551,7 @@
     // check for cache without local symbols info
      if (!this->hasLocalSymbolsInfo())
         return 0;
-    const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+    const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
     return localInfo->nlistCount;
 }
 
@@ -571,7 +565,7 @@
     // check for cache without local symbols info
      if (!this->hasLocalSymbolsInfo())
         return nullptr;
-    const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+    const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
     return getLocalStrings(localInfo);
 }
 
@@ -580,7 +574,7 @@
     // check for cache without local symbols info
      if (!this->hasLocalSymbolsInfo())
         return 0;
-    const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+    const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
     return localInfo->stringsSize;
 }
 
@@ -590,7 +584,7 @@
     const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((char*)this + header.mappingOffset);
     uint32_t dyldCacheImageIndex;
     if ( hasImagePath(dylibPath, dyldCacheImageIndex) )
-        return (dyld3::MachOFile*)((uintptr_t)this + dylibs[dyldCacheImageIndex].address - mappings[0].address);
+        return (dyld3::MachOFile*)((uint8_t*)this + dylibs[dyldCacheImageIndex].address - mappings[0].address);
     return nullptr;
 }
 
@@ -599,7 +593,7 @@
     // check for cache without local symbols info
     if (!this->hasLocalSymbolsInfo())
         return;
-    const auto localInfo = (dyld_cache_local_symbols_info*)((uintptr_t)this + header.localSymbolsOffset);
+    const auto localInfo = (dyld_cache_local_symbols_info*)((uint8_t*)this + header.localSymbolsOffset);
 
     if ( header.mappingOffset >= __offsetof(dyld_cache_header, symbolFileUUID) ) {
         // On new caches, the dylibOffset is 64-bits, and is a VM offset
@@ -624,10 +618,10 @@
 const mach_header* DyldSharedCache::getIndexedImageEntry(uint32_t index, uint64_t& mTime, uint64_t& inode) const
 {
     const dyld_cache_image_info*   dylibs   = images();
-    const dyld_cache_mapping_info* mappings = (dyld_cache_mapping_info*)((uintptr_t)this + header.mappingOffset);
+    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*)((uintptr_t)this + dylibs[index].address - mappings[0].address);
+    return (mach_header*)((uint8_t*)this + dylibs[index].address - mappings[0].address);
 }
 
 const mach_header* DyldSharedCache::getIndexedImageEntry(uint32_t index) const
@@ -1141,8 +1135,7 @@
 #if BUILDING_SHARED_CACHE_UTIL
 void DyldSharedCache::forEachPatchableUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl,
                                                   void (^handler)(uint32_t userImageIndex, uint32_t userVMOffset,
-                                                                  MachOLoaded::PointerMetaData pmd, uint64_t addend,
-                                                                  bool isWeakImport)) const {
+                                                                  MachOLoaded::PointerMetaData pmd, uint64_t addend)) const {
     if ( header.patchInfoAddr == 0 )
         return;
 
@@ -1248,7 +1241,7 @@
                 pmd.key               = patchLocation.key;
                 pmd.usesAddrDiversity = patchLocation.usesAddressDiversity;
 
-                handler(userImageIndex, userVMOffset, pmd, patchLocation.getAddend(), false);
+                handler(userImageIndex, userVMOffset, pmd, patchLocation.getAddend());
             }
         }
         return;
@@ -1278,8 +1271,7 @@
 }
 
 void DyldSharedCache::forEachPatchableUseOfExportInImage(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl, uint32_t userImageIndex,
-                                                         void (^handler)(uint32_t userVMOffset, MachOLoaded::PointerMetaData pmd, uint64_t addend,
-                                                                         bool isWeakImport)) const {
+                                                         void (^handler)(uint32_t userVMOffset, MachOLoaded::PointerMetaData pmd, uint64_t addend)) const {
     if ( header.patchInfoAddr == 0 )
         return;
 
@@ -1387,7 +1379,7 @@
                     pmd.key               = patchLocation.key;
                     pmd.usesAddrDiversity = patchLocation.usesAddressDiversity;
 
-                    handler(userVMOffset, pmd, patchLocation.getAddend(), false);
+                    handler(userVMOffset, pmd, patchLocation.getAddend());
                 }
             }
         }
@@ -1402,8 +1394,7 @@
 
 void DyldSharedCache::forEachPatchableUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl,
                                                   void (^handler)(uint64_t cacheVMOffset,
-                                                                  MachOLoaded::PointerMetaData pmd, uint64_t addend,
-                                                                  bool isWeakImport)) const {
+                                                                  MachOLoaded::PointerMetaData pmd, uint64_t addend)) const {
     if ( header.patchInfoAddr == 0 )
         return;
 
@@ -1451,7 +1442,7 @@
                 pmd.key               = patchLocation.key;
                 pmd.usesAddrDiversity = patchLocation.usesAddressDiversity;
 
-                handler(patchLocation.cacheOffset, pmd, patchLocation.getAddend(), false);
+                handler(patchLocation.cacheOffset, pmd, patchLocation.getAddend());
             }
         }
         return;
@@ -1473,8 +1464,7 @@
 void DyldSharedCache::forEachPatchableGOTUseOfExport(uint32_t imageIndex, uint32_t dylibVMOffsetOfImpl,
                                                      void (^handler)(uint64_t cacheVMOffset,
                                                                      MachOFile::PointerMetaData pmd,
-                                                                     uint64_t addend,
-                                                                     bool isWeakImport)) const {
+                                                                     uint64_t addend)) const {
     if ( header.patchInfoAddr == 0 )
         return;
 
@@ -2210,5 +2200,3 @@
     });
 }
 #endif
-
-#endif // !TARGET_OS_EXCLAVEKIT