Loading...
common/ProcessAtlas.cpp dyld-1245.1 dyld-1066.8
--- dyld/dyld-1245.1/common/ProcessAtlas.cpp
+++ dyld/dyld-1066.8/common/ProcessAtlas.cpp
@@ -24,8 +24,6 @@
 
 #include <TargetConditionals.h>
 
-#if !TARGET_OS_EXCLAVEKIT
-
 #include <atomic>
 #include <cstring>
 #include <Block.h>
@@ -41,8 +39,8 @@
 #include <sys/stat.h>
 #include <sys/fsgetpath.h>
 
+#include <mach/mach_vm.h>
 #include <mach/mach_time.h> // mach_absolute_time()
-#include <mach/mach_vm.h>
 #include <mach-o/dyld_priv.h> // FIXME: We can remove this once we fully integrate into dyld4
 #include "dyld_cache_format.h"
 //FIXME: We should remove this header
@@ -78,8 +76,6 @@
 #else
 #define _transactionalAllocator Allocator::defaultAllocator()
 #endif
-
-#define BLEND_KERN_RETURN_LOCATION(kr, loc) (kr) = ((kr & 0x00ffffff) | loc<<24);
 
 namespace {
 static const size_t kCachePeekSize = 0x4000;
@@ -423,7 +419,7 @@
     }
 
     // mmap whole file temporarily
-    void* tempMapping = ::mmap(nullptr, (size_t)sb.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE | MAP_RESILIENT_CODESIGN, fd, 0);
+    void* tempMapping = ::mmap(nullptr, (size_t)sb.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
     if ( tempMapping == MAP_FAILED ) {
         ::close(fd);
         return nullptr;
@@ -526,14 +522,14 @@
             off_t roundedOffset = offset & (-1*PAGE_SIZE);
             extraBytes = (size_t)offset - (size_t)roundedOffset;
             newMapping = mmap(nullptr, (size_t)size+extraBytes, PROT_READ, MAP_FILE | MAP_PRIVATE, mapping.fd, roundedOffset);
-            if (newMapping == MAP_FAILED) {
+            if (newMapping == (void*)-1) {
 //                fprintf(stderr, "mmap failed: %s (%d)\n", strerror(errno), errno);
-                return {nullptr, false};
+                return {(void*)1, false};
             }
             return {(void*)((uintptr_t)newMapping+extraBytes),true};
         }
     }
-    return {nullptr, false};
+    return {(void*)-1, false};
 }
 
 void Mapper::unmap(const void* addr, uint64_t size) const {
@@ -592,14 +588,14 @@
 #if BUILDING_DYLD
 Image::Image(RuntimeState* state, Allocator& ephemeralAllocator, SharedPtr<Mapper>& mapper, const Loader* ldr)
     :   _ephemeralAllocator(ephemeralAllocator), _mapper(mapper), _rebasedAddress((void*)ldr->loadAddress(*state)) {
-        auto fileID = ldr->fileID(*state);
+        auto fileID = ldr->fileID(state->fileManager);
         if (fileID.inode() &&  fileID.device()) {
-            _file = state->fileManager.fileRecordForFileID(ldr->fileID(*state));
+            _file = state->fileManager.fileRecordForFileID(ldr->fileID(state->fileManager));
             if ( _file.volume().empty() ) {
-                _file = state->fileManager.fileRecordForPath(ephemeralAllocator, ldr->path(*state));
+                _file = state->fileManager.fileRecordForPath(ephemeralAllocator, ldr->path());
             }
         } else {
-            _file = state->fileManager.fileRecordForPath(ephemeralAllocator, ldr->path(*state));
+            _file = state->fileManager.fileRecordForPath(ephemeralAllocator, ldr->path());
         }
     }
 #endif
@@ -656,10 +652,6 @@
             return nullptr;
         }
         _ml = _mapper->map<MachOLoaded>(slidML, 4096);
-        if (!_ml) {
-           _mapperFailed = true;
-           return nullptr;
-        }
         size_t size = _ml->sizeofcmds;
         if ( _ml->magic == MH_MAGIC_64 ) {
             size += sizeof(mach_header_64);
@@ -668,10 +660,6 @@
         }
         if (size > 4096) {
             _ml = _mapper->map<MachOLoaded>(slidML, size);
-            if (!_ml) {
-               _mapperFailed = true;
-               return nullptr;
-            }
         }
     }
     // This is a bit of a mess. With compact info this will be unified, but for now we use a lot of hacky abstactions here to deal with
@@ -1239,8 +1227,6 @@
     pid_t   pid;
     *kr = pid_for_task(_task, &pid);
     if ( *kr != KERN_SUCCESS ) {
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xea);
-        *kr |= 0xeb000000UL;
         return nullptr;
     }
 
@@ -1248,7 +1234,6 @@
     mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
     *kr = task_info(_task, MACH_TASK_BASIC_INFO, (task_info_t)&ti, &count);
     if ( *kr != KERN_SUCCESS ) {
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xe9);
         return nullptr;
     }
 
@@ -1272,7 +1257,6 @@
             mach_vm_size_t readSize = 0;
             *kr = mach_vm_read_overwrite(_task, address, size, (mach_vm_address_t)&unsafeBytes[0], &readSize);
             if ( *kr != KERN_SUCCESS ) {
-                BLEND_KERN_RETURN_LOCATION(*kr, 0xe8);
                 return;
             }
             auto mf = MachOFile::isMachO((const void*)unsafeBytes);
@@ -1333,12 +1317,10 @@
     task_dyld_info_data_t task_dyld_info;
     *kr = task_info(_task, TASK_DYLD_INFO, (task_info_t)&task_dyld_info, &count);
     if ( *kr != KERN_SUCCESS ) {
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xef);
         return nullptr;
     }
     //The kernel will return MACH_VM_MIN_ADDRESS for an executable that has not had dyld loaded
     if (task_dyld_info.all_image_info_addr == MACH_VM_MIN_ADDRESS) {
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xee);
         return nullptr;
     }
     uint8_t remoteBuffer[16*1024];
@@ -1349,7 +1331,6 @@
         *kr = mach_vm_read_overwrite(_task, task_dyld_info.all_image_info_addr, task_dyld_info.all_image_info_size,
                                      (mach_vm_address_t)&remoteBuffer[0], &readSize);
         if (*kr != KERN_SUCCESS) {
-            BLEND_KERN_RETURN_LOCATION(*kr, 0xed);
             // If we cannot read the all image info this is game over
             return nullptr;
         }
@@ -1370,7 +1351,6 @@
         auto compactInfo = UniquePtr<std::byte>((std::byte*)_transactionalAllocator.malloc((size_t)compactInfoSize));
         *kr = mach_vm_read_overwrite(_task, compactInfoAddress, compactInfoSize, (mach_vm_address_t)&*compactInfo, &readSize);
         if (*kr != KERN_SUCCESS) {
-            BLEND_KERN_RETURN_LOCATION(*kr, 0xec);
             // The read failed, chances are the process mutated the compact info, retry
             continue;
         }
@@ -1379,7 +1359,6 @@
         if (!result->valid()) {
             // Something blew up we don't know what
             *kr = KERN_FAILURE;
-            BLEND_KERN_RETURN_LOCATION(*kr, 0xeb);
             return nullptr;
         }
         return result;
@@ -1738,8 +1717,8 @@
     _bitmap = _transactionalAllocator.makeUnique<Bitmap>(_transactionalAllocator, _sharedCache->imageCount());
 }
 
-/// Assumes the mach_header parameter is in the range of the shared cache. Otherwise asserts
 void ProcessSnapshot::addSharedCacheImage(const struct mach_header* mh) {
+    assert(mh->flags & MH_DYLIB_IN_CACHE);
     auto header = (dyld_cache_header*)_sharedCache->rebasedAddress();
     auto headerBytes = (uint8_t*)header;
     auto slide = (uint64_t)header - header->sharedRegionStart;
@@ -1907,7 +1886,7 @@
     }
 }
 
-bool ProcessSnapshot::Serializer::readMappedFileInfo(std::span<std::byte>& data, uint64_t& rebasedAddress, UUID& uuid, FileRecord& file) {
+void ProcessSnapshot::Serializer::readMappedFileInfo(std::span<std::byte>& data, uint64_t& rebasedAddress, UUID& uuid, FileRecord& file) {
     uint64_t flags = readPVLEUInt64(data);
     rebasedAddress = readPVLEUInt64(data);
     if (flags & kMappedFileFlagsHasUUID) {
@@ -1917,17 +1896,12 @@
     if (flags & kMappedFileFlagsHasFileID) {
         uint64_t volumeIndex = readPVLEUInt64(data);
         uint64_t objectID = readPVLEUInt64(data);
-        if (volumeIndex >= _volumeUUIDs.size() )
-            return false;
         file = _fileManager.fileRecordForVolumeUUIDAndObjID(_volumeUUIDs[(size_t)volumeIndex], objectID);
     }
     if (flags & kMappedFileFlagsHasFilePath) {
         uint64_t pathOffset = readPVLEUInt64(data);
-        if ( pathOffset >= _stringTableBuffer.size() )
-            return false;
         file = _fileManager.fileRecordForPath(_ephemeralAllocator, &_stringTableBuffer[(size_t)pathOffset]);
     }
-    return true;
 }
 
 Vector<std::byte> ProcessSnapshot::Serializer::serialize() {
@@ -2031,17 +2005,17 @@
     _timestamp          = read<uint64_t>(i);
     _crc32c             = read<uint32_t>(i);
     if (_magic != kMagic) {
-        return false;
+        assert(0);
     }
     if (_version != 0) {
-        return false;
+        assert(0);
     }
     CRC32c checksumer;
     checksumer(std::span(&data[0], 32));
     checksumer((uint32_t)0); // Zero out the actual checksum
     checksumer(std::span(&data[36], data.size() - 36));
     if (_crc32c != checksumer) {
-        return false;
+        assert(0);
     }
     _processFlags           = readPVLEUInt64(i);
     _platform               = readPVLEUInt64(i);
@@ -2062,8 +2036,7 @@
         uint64_t rebasedAddress;
         UUID uuid;
         FileRecord file;
-        if ( !readMappedFileInfo(i, rebasedAddress, uuid, file) )
-            return false;
+        readMappedFileInfo(i, rebasedAddress, uuid, file);
         rebasedAddress = rebasedAddress * ((_processFlags & kProcessFlagsHas16kPages) ? 16384 : 4096);
         SharedPtr<Mapper> mapper = nullptr;
         if (_processSnapshot._useIdentityMapper) {
@@ -2089,8 +2062,7 @@
         uint64_t rebasedAddress;
         UUID uuid;
         FileRecord file;
-        if ( !readMappedFileInfo(i, rebasedAddress, uuid, file) )
-            return false;
+        readMappedFileInfo(i, rebasedAddress, uuid, file);
         rebasedAddress = (rebasedAddress * ((_processFlags & kProcessFlagsHas16kPages) ? 16384 : 4096)) + lastAddress;
         lastAddress = rebasedAddress;
         SharedPtr<Mapper> mapper = nullptr;
@@ -2110,4 +2082,3 @@
 
 };
 };
-#endif // !TARGET_OS_EXCLAVEKIT