Loading...
libdyld/dyld_process_info.cpp dyld-1122.1 dyld-955
--- dyld/dyld-1122.1/libdyld/dyld_process_info.cpp
+++ dyld/dyld-955/libdyld/dyld_process_info.cpp
@@ -22,9 +22,6 @@
  * @APPLE_LICENSE_HEADER_END@
  */
 
-#include <TargetConditionals.h>
-#if !TARGET_OS_EXCLAVEKIT
-
 #include <dlfcn.h>
 #include <stdlib.h>
 #include <string.h>
@@ -38,14 +35,13 @@
 #include <libkern/OSAtomic.h>
 #include <mach-o/dyld_process_info.h>
 #include <mach-o/dyld_images.h>
-#include <mach-o/dyld_priv.h>
 
 #include "MachOFile.h"
 #include "dyld_process_info_internal.h"
 #include "Tracing.h"
+#include "DebuggerSupport.h"
 #include "DyldProcessConfig.h"
 
-#define IMAGE_COUNT_MAX 8192
 RemoteBuffer& RemoteBuffer::operator=(RemoteBuffer&& other) {
     std::swap(_localAddress, other._localAddress);
     std::swap(_size, other._size);
@@ -105,13 +101,12 @@
     // we are copying some memory in the middle of a mach-o that is on a USB drive that is disconnected after we perform
     // the mapping). Once we copy them into a local buffer the memory will be handled by the default pager instead of
     // potentially being backed by the mmap pager, and thus will be guaranteed not to mutate out from under us.
-    void* buffer = malloc(size + 1);
+    void* buffer = malloc(size);
     if (buffer == nullptr) {
         (void)vm_deallocate(mach_task_self(), (vm_address_t)localAddress, size);
         return std::make_pair(MACH_VM_MIN_ADDRESS, KERN_NO_SPACE);
     }
     memcpy(buffer, (void *)localAddress, size);
-    ((char*)buffer)[size] = 0; // Add a null terminator so strlcpy does not read base the end of the buffer.
     (void)vm_deallocate(mach_task_self(), (vm_address_t)localAddress, size);
     return std::make_pair((vm_address_t)buffer, KERN_SUCCESS);
 }
@@ -129,8 +124,7 @@
     // truncatable buffer we map is less than a single page. To be more general we would need to try repeatedly in a
     // loop.
     if (allow_truncation) {
-        // Manually set to 4096 instead of page size to deal with wierd issues involving 4k page arm64 binaries
-        size = 4096 - remote_address%4096;
+        size = PAGE_SIZE - remote_address%PAGE_SIZE;
         std::tie(localAddress, kr) = map(task, remote_address, size);
         if (kr == KERN_SUCCESS) return std::make_tuple(localAddress, size, kr);
     }
@@ -182,7 +176,7 @@
     template<typename T1, typename T2>
     static dyld_process_info_ptr make(task_t task, const T1& allImageInfo, uint64_t timestamp, kern_return_t* kr);
     template<typename T>
-    static dyld_process_info_ptr makeSuspended(task_t task, const T& allImageInfo, uint64_t timestamp, kern_return_t* kr);
+    static dyld_process_info_ptr makeSuspended(task_t task, const T& allImageInfo, kern_return_t* kr);
 
     std::atomic<uint32_t>&       retainCount() const { return _retainCount; }
     dyld_process_cache_info*     cacheInfo() const { return (dyld_process_cache_info*)(((char*)this) + _cacheInfoOffset); }
@@ -232,10 +226,10 @@
                                 dyld_process_info_base(dyld_platform_t platform, unsigned imageCount, unsigned aotImageCount, size_t totalSize);
     void*                       operator new (size_t, void* buf) { return buf; }
 
-    static bool                 inCache(uint64_t addr, uint64_t sharedCacheStart, uint64_t sharedCacheEnd) { return (addr > sharedCacheStart) && (addr < sharedCacheEnd); }
-    kern_return_t               addImage(task_t task, bool sameCacheAsThisProcess, uint64_t sharedCacheStart, uint64_t sharedCacheEnd, uint64_t imageAddress, uint64_t imagePath, const char* imagePathLocal);
-
-    kern_return_t               addAotImage(dyld_aot_image_info_64 aotImageInfo);
+    static bool                 inCache(uint64_t addr) { return (addr > SHARED_REGION_BASE) && (addr < SHARED_REGION_BASE+SHARED_REGION_SIZE); }
+    bool                        addImage(task_t task, bool sameCacheAsThisProcess, uint64_t imageAddress, uint64_t imagePath, const char* imagePathLocal);
+
+    bool                        addAotImage(dyld_aot_image_info_64 aotImageInfo);
 
     kern_return_t               addDyldImage(task_t task, uint64_t dyldAddress, uint64_t dyldPathAddress, const char* localPath);
 
@@ -275,20 +269,13 @@
     // char                     stringPool[]
 };
 
-static uint32_t addWithOverflowOrReturnZero(uint32_t a, uint32_t b)
-{
-    uint32_t result = 0;
-    assert( !__builtin_add_overflow(a, b, &result) );
-    return result;
-}
-
 dyld_process_info_base::dyld_process_info_base(dyld_platform_t platform, unsigned imageCount, unsigned aotImageCount, size_t totalSize)
  :  _retainCount(1), _cacheInfoOffset(sizeof(dyld_process_info_base)),
-    _aotCacheInfoOffset(_cacheInfoOffset + sizeof(dyld_process_cache_info)),
-    _stateInfoOffset(_aotCacheInfoOffset + sizeof(dyld_process_aot_cache_info)),
-    _imageInfosOffset(_stateInfoOffset + sizeof(dyld_process_state_info)),
-    _aotImageInfosOffset(addWithOverflowOrReturnZero(_imageInfosOffset, imageCount*sizeof(ImageInfo))),
-    _segmentInfosOffset(addWithOverflowOrReturnZero(_aotImageInfosOffset, aotImageCount*sizeof(dyld_aot_image_info_64))),
+    _aotCacheInfoOffset(sizeof(dyld_process_info_base) + sizeof(dyld_process_cache_info)),
+    _stateInfoOffset(sizeof(dyld_process_info_base) + sizeof(dyld_process_cache_info) + sizeof(dyld_process_aot_cache_info)),
+    _imageInfosOffset(sizeof(dyld_process_info_base) + sizeof(dyld_process_cache_info) + sizeof(dyld_process_aot_cache_info) + sizeof(dyld_process_state_info)),
+    _aotImageInfosOffset(sizeof(dyld_process_info_base) + sizeof(dyld_process_cache_info) + sizeof(dyld_process_aot_cache_info) + sizeof(dyld_process_state_info) + imageCount*sizeof(ImageInfo)),
+    _segmentInfosOffset(sizeof(dyld_process_info_base) + sizeof(dyld_process_cache_info) + sizeof(dyld_process_aot_cache_info) + sizeof(dyld_process_state_info) + imageCount*sizeof(ImageInfo) + aotImageCount*sizeof(dyld_aot_image_info_64)),
     _freeSpace(totalSize), _platform(platform),
     _firstImage((ImageInfo*)(((uint8_t*)this) + _imageInfosOffset)),
     _curImage((ImageInfo*)(((uint8_t*)this) + _imageInfosOffset)),
@@ -299,7 +286,6 @@
     _curSegmentIndex(0),
     _stringRevBumpPtr((char*)(this)+totalSize)
 {
-
 }
 
 template<typename T1, typename T2>
@@ -314,16 +300,8 @@
     }
 
     // Check if the process is suspended
-    bool shouldMakeSuspended = (allImageInfo.infoArrayChangeTimestamp == 0);
-    if ( !shouldMakeSuspended ) {
-        if ( (allImageInfo.infoArray == 0)
-            && (allImageInfo.sharedCacheBaseAddress != 0)
-            && ((uintptr_t)allImageInfo.dyldImageLoadAddress > allImageInfo.sharedCacheBaseAddress) )
-            shouldMakeSuspended = true;
-    }
-    if (shouldMakeSuspended) {
-        result = dyld_process_info_base::makeSuspended<T1>(task, allImageInfo,
-                                                           allImageInfo.infoArrayChangeTimestamp, kr);
+    if (allImageInfo.infoArrayChangeTimestamp == 0) {
+        result = dyld_process_info_base::makeSuspended<T1>(task, allImageInfo, kr);
         // If we have a result return it, otherwise rescan
         if (result) {
             // If it returned the process is suspended and there is nothing more to do
@@ -331,9 +309,7 @@
         }
         usleep(1000 * 50); // 50ms
         // Not exactly correct, but conveys that operation may succeed in the future
-        if (*kr == KERN_SUCCESS) {
-            *kr = KERN_RESOURCE_SHORTAGE;
-        }
+        *kr = KERN_RESOURCE_SHORTAGE;
         return  nullptr;
     }
 
@@ -348,7 +324,7 @@
     if (infoArray == 0) {
         usleep(1000 * 50); // 50ms
         // Not exactly correct, but conveys that operation may succeed in the future
-        *kr = KERN_UREFS_OVERFLOW;
+        *kr = KERN_RESOURCE_SHORTAGE;
         return  nullptr;
     };
 
@@ -356,21 +332,13 @@
     // terrible things that corrupt their own image lists and we need to stop clients from crashing
     // reading them. We can try to do something more advanced in the future. rdar://27446361
     uint32_t imageCount = allImageInfo.infoArrayCount;
-    imageCount = MIN(imageCount, IMAGE_COUNT_MAX);
+    imageCount = MIN(imageCount, 8192);
     size_t imageArraySize = imageCount * sizeof(T2);
-
-    uint64_t sharedCacheStart   = 0;
-    uint64_t sharedCacheEnd     = 0;
-    size_t   sharedCacheLength  = 0;
-    if ( const void* start = _dyld_get_shared_cache_range(&sharedCacheLength) ) {
-        sharedCacheStart = (uint64_t)start;
-        sharedCacheEnd = sharedCacheStart + (uint64_t)sharedCacheLength;
-    }
 
     withRemoteBuffer(task, infoArray, imageArraySize, false, kr, ^(void *buffer, size_t size) {
         // figure out how many path strings will need to be copied and their size
         T2* imageArray = (T2 *)buffer;
-        const dyld_all_image_infos* myInfo = (const dyld_all_image_infos*)dyld4::gDyld.allImageInfos;   // FIXME: should not need dyld_all_image_info
+        const dyld_all_image_infos* myInfo = (const dyld_all_image_infos*)dyld4::gDyld.allImageInfos;
         bool sameCacheAsThisProcess = !allImageInfo.processDetachedFromSharedRegion
             && !myInfo->processDetachedFromSharedRegion
             && ((memcmp(myInfo->sharedCacheUUID, &allImageInfo.sharedCacheUUID[0], 16) == 0)
@@ -378,7 +346,7 @@
         unsigned countOfPathsNeedingCopying = 0;
         if ( sameCacheAsThisProcess ) {
             for (uint32_t i=0; i < imageCount; ++i) {
-                if ( !inCache(imageArray[i].imageFilePath, sharedCacheStart, sharedCacheEnd) )
+                if ( !inCache(imageArray[i].imageFilePath) )
                     ++countOfPathsNeedingCopying;
             }
         }
@@ -386,7 +354,6 @@
             countOfPathsNeedingCopying = imageCount+1;
         }
         unsigned imageCountWithDyld = imageCount+1;
-        unsigned aotImageCount = MIN(allImageInfo.aotInfoCount, IMAGE_COUNT_MAX);
 
         // allocate result object
         size_t allocationSize = sizeof(dyld_process_info_base)
@@ -394,7 +361,7 @@
                                     + sizeof(dyld_process_aot_cache_info)
                                     + sizeof(dyld_process_state_info)
                                     + sizeof(ImageInfo)*(imageCountWithDyld)
-                                    + sizeof(dyld_aot_image_info_64)*(aotImageCount) // add the size necessary for aot info to this buffer
+                                    + sizeof(dyld_aot_image_info_64)*(allImageInfo.aotInfoCount) // add the size necessary for aot info to this buffer
                                     + sizeof(SegmentInfo)*imageCountWithDyld*10
                                     + countOfPathsNeedingCopying*PATH_MAX;
         void* storage = malloc(allocationSize);
@@ -450,8 +417,8 @@
         }
         // fill in info for each image
         for (uint32_t i=0; i < imageCount; ++i) {
-            *kr =  info->addImage(task, sameCacheAsThisProcess, sharedCacheStart, sharedCacheEnd, imageArray[i].imageLoadAddress, imageArray[i].imageFilePath, NULL);
-            if (*kr != KERN_SUCCESS) {
+            if (!info->addImage(task, sameCacheAsThisProcess, imageArray[i].imageLoadAddress, imageArray[i].imageFilePath, NULL)) {
+                *kr = KERN_FAILURE;
                 result = nullptr;
                 return;
             }
@@ -476,7 +443,8 @@
         withRemoteBuffer(task, aotImageArray, aotImageArraySize, false, kr, ^(void *buffer, size_t size) {
             dyld_aot_image_info_64* imageArray = (dyld_aot_image_info_64*)buffer;
             for (uint32_t i = 0; i < aotImageCount; i++) {
-                if ((*kr = result->addAotImage(imageArray[i]))) {
+                if (!result->addAotImage(imageArray[i])) {
+                    *kr = KERN_FAILURE;
                     result = nullptr;
                     return;
                 }
@@ -487,8 +455,7 @@
 }
 
 template<typename T>
-dyld_process_info_ptr dyld_process_info_base::makeSuspended(task_t task, const T& allImageInfo,
-                                                            uint64_t timestamp, kern_return_t* kr)
+dyld_process_info_ptr dyld_process_info_base::makeSuspended(task_t task, const T& allImageInfo, kern_return_t* kr)
 {
     pid_t   pid;
     if ((*kr = pid_for_task(task, &pid))) {
@@ -498,24 +465,12 @@
     mach_task_basic_info ti;
     mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
     if ((*kr = task_info(task, MACH_TASK_BASIC_INFO, (task_info_t)&ti, &count))) {
-        *kr = KERN_NO_SPACE;
         return  nullptr;
     }
 
     // The task is not suspended, exit
     if (ti.suspend_count == 0) {
-        // Even if the process is not suspended it might never make forward progress. This is because it might be a corpse, which
-        // despite not being runnable may not have the suspended flag set. The only way to tell if it is a corpse is to to try to
-        // map the corpse data, even though we don't actually need the corpse data
-        mach_vm_address_t kcd_addr_begin = 0;
-        mach_vm_size_t kcd_size = 0;
-        *kr = task_map_corpse_info_64(mach_task_self(), task, &kcd_addr_begin, &kcd_size);
-        if (*kr != KERN_SUCCESS) {
-            // Not a corpse, so forward progress is possible. Return so that make() can pause and retry
-            return nullptr;
-        }
-        // It is a corpse. Unmap the corpse info and keep scavenging the suspended process
-        vm_deallocate(mach_task_self(), (vm_address_t)kcd_addr_begin, (vm_size_t)kcd_size);
+        return  nullptr;
     }
 
     __block unsigned        imageCount = 0;  // main executable and dyld
@@ -564,15 +519,6 @@
     //fprintf(stderr, "dyld: addr=0x%llX, path=%s\n", dyldAddress, dyldPathBuffer);
     //fprintf(stderr, "app:  addr=0x%llX, path=%s\n", mainExecutableAddress, mainExecutablePathBuffer);
 
-    // fill in info for dyld
-    if ( dyldAddress == 0 ) {
-        // if dyld was not found in vm walk, then we've switched to the dyld in the cache
-        dyldAddress = allImageInfo.dyldImageLoadAddress;
-        strcpy(dyldPathBuffer, "/usr/lib/dyld");
-        ++imageCount;
-    }
-
-    imageCount = MIN(imageCount, IMAGE_COUNT_MAX);
     // explicitly set aot image count to 0 in the suspended case
     unsigned aotImageCount = 0;
 
@@ -605,11 +551,12 @@
     aotCacheInfo->cacheBaseAddress = 0;
 
     dyld_process_state_info* stateInfo = obj->stateInfo();
-    stateInfo->timestamp           = timestamp;
+    stateInfo->timestamp           = 0;
     stateInfo->imageCount          = imageCount;
     stateInfo->initialImageCount   = imageCount;
     stateInfo->dyldState           = dyld_process_state_not_started;
 
+    // fill in info for dyld
     if ( dyldAddress != 0 ) {
         if ((*kr = obj->addDyldImage(task, dyldAddress, 0, dyldPath))) {
             return nullptr;
@@ -618,13 +565,22 @@
 
     // fill in info for each image
     if ( mainExecutableAddress != 0 ) {
-        if ((*kr = obj->addImage(task, false, 0, 0, mainExecutableAddress, 0, mainExecutablePath))) {
+        if (!obj->addImage(task, false, mainExecutableAddress, 0, mainExecutablePath)) {
             return nullptr;
         }
     }
 
-    if (allImageInfo.infoArrayChangeTimestamp != timestamp) {
-        *kr = KERN_INVALID_VALUE;
+    if (allImageInfo.infoArrayChangeTimestamp != 0) {
+        return  nullptr;
+    }
+
+    count = MACH_TASK_BASIC_INFO_COUNT;
+    if ((*kr = task_info(task, MACH_TASK_BASIC_INFO, (task_info_t)&ti, &count))) {
+        return  nullptr;
+    }
+
+    // The task is not suspended, exit
+    if (ti.suspend_count == 0) {
         return  nullptr;
     }
 
@@ -652,13 +608,13 @@
     return retval;
 }
 
-kern_return_t dyld_process_info_base::addImage(task_t task, bool sameCacheAsThisProcess, uint64_t sharedCacheStart, uint64_t sharedCacheEnd, uint64_t imageAddress, uint64_t imagePath, const char* imagePathLocal)
+bool dyld_process_info_base::addImage(task_t task, bool sameCacheAsThisProcess, uint64_t imageAddress, uint64_t imagePath, const char* imagePathLocal)
 {
     _curImage->loadAddress = imageAddress;
     _curImage->segmentStartIndex = _curSegmentIndex;
     if ( imagePathLocal != NULL ) {
         _curImage->path = addString(imagePathLocal, PATH_MAX);
-    } else if ( sameCacheAsThisProcess && inCache(imagePath, sharedCacheStart, sharedCacheEnd) ) {
+    } else if ( sameCacheAsThisProcess && inCache(imagePath) ) {
         _curImage->path = (const char*)imagePath;
     } else if (imagePath) {
         _curImage->path = copyPath(task, imagePath);
@@ -666,23 +622,20 @@
         _curImage->path = "";
     }
 
-    if ( sameCacheAsThisProcess && inCache(imageAddress, sharedCacheStart, sharedCacheEnd) ) {
+    if ( sameCacheAsThisProcess && inCache(imageAddress) ) {
         addInfoFromLoadCommands((mach_header*)imageAddress, imageAddress, 32*1024);
-    } else {
-        auto kr = addInfoFromRemoteLoadCommands(task, imageAddress);
-        if (kr != KERN_SUCCESS) {
-            // The image is not here, return early
-            return kr;
-        }
+    } else if (addInfoFromRemoteLoadCommands(task, imageAddress) != KERN_SUCCESS) {
+        // The image is not here, return early
+        return false;
     }
     _curImage->segmentsCount = _curSegmentIndex - _curImage->segmentStartIndex;
     _curImage++;
-    return KERN_SUCCESS;
-}
-
-kern_return_t dyld_process_info_base::addAotImage(dyld_aot_image_info_64 aotImageInfo) {
+    return true;
+}
+
+bool dyld_process_info_base::addAotImage(dyld_aot_image_info_64 aotImageInfo) {
     if (!reserveSpace(sizeof(dyld_aot_image_info_64))) {
-        return KERN_NO_SPACE;
+        return false;
     }
     _curAotImage->x86LoadAddress = aotImageInfo.x86LoadAddress;
     _curAotImage->aotLoadAddress = aotImageInfo.aotLoadAddress;
@@ -690,7 +643,7 @@
     memcpy(_curAotImage->aotImageKey, aotImageInfo.aotImageKey, sizeof(aotImageInfo.aotImageKey));
 
     _curAotImage++;
-    return KERN_SUCCESS;
+    return true;
 }
 
 kern_return_t dyld_process_info_base::addInfoFromRemoteLoadCommands(task_t task, uint64_t remoteMH) {
@@ -699,7 +652,7 @@
     __block bool done = false;
 
     //Since the minimum we can reasonably map is a page, map that.
-    withRemoteBuffer(task, remoteMH, 4096, true, &kr, ^(void * buffer, size_t size) {
+    withRemoteBuffer(task, remoteMH, PAGE_SIZE, true, &kr, ^(void * buffer, size_t size) {
         if (size > sizeof(mach_header)) {
             const mach_header* mh = (const mach_header*)buffer;
             headerPagesSize = sizeof(mach_header) + mh->sizeofcmds;
@@ -888,12 +841,6 @@
             }
         });
         if (*kr == KERN_SUCCESS) { break; }
-        // possible that dyld moved, causing imageinfo reads to fail, so get TASK_DYLD_INFO again
-        task_dyld_info_data_t task_dyld_info2;
-        mach_msg_type_number_t count2 = TASK_DYLD_INFO_COUNT;
-        if ( task_info(task, TASK_DYLD_INFO, (task_info_t)&task_dyld_info2, &count2) == KERN_SUCCESS ) {
-            task_dyld_info = task_dyld_info2;
-        }
     }
     return  result;
 }
@@ -944,6 +891,5 @@
     info->forEachSegment(machHeaderAddress, callback);
 }
 
-#endif // !TARGET_OS_EXCLAVEKIT
-
-
+
+