Loading...
src/dyld_process_info.cpp dyld-852 dyld-832.7.1
--- dyld/dyld-852/src/dyld_process_info.cpp
+++ dyld/dyld-832.7.1/src/dyld_process_info.cpp
@@ -22,7 +22,6 @@
  * @APPLE_LICENSE_HEADER_END@
  */
 
-#include <dlfcn.h>
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>
@@ -49,48 +48,46 @@
     std::swap(_localAddress, other._localAddress);
     std::swap(_size, other._size);
     std::swap(_kr, other._kr);
+    std::swap(_shared, other._shared);
     return *this;
 }
 
-RemoteBuffer::RemoteBuffer() : _localAddress(0), _size(0), _kr(KERN_SUCCESS) {}
-RemoteBuffer::RemoteBuffer(std::tuple<mach_vm_address_t,vm_size_t,kern_return_t> T)
-    : _localAddress(std::get<0>(T)), _size(std::get<1>(T)), _kr(std::get<2>(T)) {}
-
-RemoteBuffer::RemoteBuffer(task_t task, mach_vm_address_t remote_address, size_t remote_size, bool allow_truncation)
-: RemoteBuffer(RemoteBuffer::create(task, remote_address, remote_size, allow_truncation)) {};
+RemoteBuffer::RemoteBuffer() : _localAddress(0), _size(0), _kr(KERN_SUCCESS), _shared(false) {}
+RemoteBuffer::RemoteBuffer(std::tuple<mach_vm_address_t,vm_size_t,kern_return_t,bool> T)
+    : _localAddress(std::get<0>(T)), _size(std::get<1>(T)), _kr(std::get<2>(T)), _shared(std::get<3>(T)) {}
+
+RemoteBuffer::RemoteBuffer(task_t task, mach_vm_address_t remote_address, size_t remote_size, bool shared, bool allow_truncation)
+: RemoteBuffer(RemoteBuffer::create(task, remote_address, remote_size, shared, allow_truncation)) {};
 
 std::pair<mach_vm_address_t, kern_return_t>
-RemoteBuffer::map(task_t task, mach_vm_address_t remote_address, vm_size_t size) {
-    static kern_return_t (*mvrn)(vm_map_t, mach_vm_address_t*, mach_vm_size_t, mach_vm_offset_t, int, vm_map_read_t, mach_vm_address_t,
-                                 boolean_t, vm_prot_t*, vm_prot_t*, vm_inherit_t) = nullptr;
+RemoteBuffer::map(task_t task, mach_vm_address_t remote_address, vm_size_t size, bool shared) {
     vm_prot_t cur_protection = VM_PROT_NONE;
-    vm_prot_t max_protection = VM_PROT_READ;
+    vm_prot_t max_protection = VM_PROT_NONE;
+    int flags;
     if (size == 0) {
         return std::make_pair(MACH_VM_MIN_ADDRESS, KERN_INVALID_ARGUMENT);
     }
+    if (shared) {
+        flags = VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR;
+    } else {
+    // <rdar://55343677>
+    // Since we are getting rid of the flag probing we have to make sure that simulator libdyld's do not use VM_FLAGS_RESILIENT_MEDIA
+    // FIXME: Remove this when simulator builds do not support back deployment to 10.14
+#if TARGET_OS_SIMULATOR
+        flags = VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR | VM_FLAGS_RESILIENT_CODESIGN;
+#else
+        flags = VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR | VM_FLAGS_RESILIENT_CODESIGN | VM_FLAGS_RESILIENT_MEDIA;
+#endif
+    }
     mach_vm_address_t localAddress = 0;
-#if TARGET_OS_SIMULATOR
-    static dispatch_once_t onceToken;
-    dispatch_once(&onceToken, ^{
-        mvrn = (kern_return_t (*)(vm_map_t, mach_vm_address_t*, mach_vm_size_t, mach_vm_offset_t, int, vm_map_read_t, mach_vm_address_t,
-                                  boolean_t, vm_prot_t*, vm_prot_t*, vm_inherit_t))dlsym(RTLD_DEFAULT, "mach_vm_remap_new");
-        if (mvrn == nullptr) {
-            // We are running on a system that does not support task_read ports, use the old call
-            mvrn = (kern_return_t (*)(vm_map_t, mach_vm_address_t*, mach_vm_size_t, mach_vm_offset_t, int, vm_map_read_t, mach_vm_address_t,
-                                      boolean_t, vm_prot_t*, vm_prot_t*, vm_inherit_t))dlsym(RTLD_DEFAULT, "mach_vm_remap");
-        }
-    });
-#else
-    mvrn = &mach_vm_remap_new;
-#endif
-    auto kr = mvrn(mach_task_self(),
+    auto kr = mach_vm_remap(mach_task_self(),
                         &localAddress,
                         size,
                         0,  // mask
-                        VM_FLAGS_ANYWHERE | VM_FLAGS_RESILIENT_CODESIGN | VM_FLAGS_RESILIENT_MEDIA,
+                        flags,
                         task,
                         remote_address,
-                        true,
+                        !shared,
                         &cur_protection,
                         &max_protection,
                         VM_INHERIT_NONE);
@@ -104,51 +101,68 @@
     // 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);
-    if (buffer == nullptr) {
+    if (!shared) {
+        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);
         (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);
-    (void)vm_deallocate(mach_task_self(), (vm_address_t)localAddress, size);
-    return std::make_pair((vm_address_t)buffer, KERN_SUCCESS);
-}
-
-std::tuple<mach_vm_address_t,vm_size_t,kern_return_t> RemoteBuffer::create(task_t task,
+        return std::make_pair((vm_address_t)buffer, KERN_SUCCESS);
+    }
+    // A shared buffer was requested, if the permissions are not correct deallocate the region and return failure
+    if (cur_protection != (VM_PROT_READ|VM_PROT_WRITE)) {
+        if (localAddress != 0) {
+            (void)vm_deallocate(mach_task_self(), (size_t)localAddress, size);
+        }
+        return std::make_pair(MACH_VM_MIN_ADDRESS, KERN_PROTECTION_FAILURE);
+    }
+    // We have a successfully created shared buffer with the correct permissions, return it
+    return std::make_pair(localAddress, KERN_SUCCESS);
+}
+
+std::tuple<mach_vm_address_t,vm_size_t,kern_return_t,bool> RemoteBuffer::create(task_t task,
                                                                                 mach_vm_address_t remote_address,
                                                                                 size_t size,
+                                                                                bool shared,
                                                                                 bool allow_truncation) {
     mach_vm_address_t localAddress;
     kern_return_t kr;
     // Try the initial map
-    std::tie(localAddress, kr) = map(task, remote_address, size);
-    if (kr == KERN_SUCCESS) return std::make_tuple(localAddress, size, kr);
+    std::tie(localAddress, kr) = map(task, remote_address, size, shared);
+    if (kr == KERN_SUCCESS) return std::make_tuple(localAddress, size, kr, shared);
     // The first attempt failed, truncate if possible and try again. We only need to try once since the largest
     // 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) {
         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);
+        std::tie(localAddress, kr) = map(task, remote_address, size, shared);
+        if (kr == KERN_SUCCESS) return std::make_tuple(localAddress, size, kr, shared);
     }
     // If we reach this then the mapping completely failed
-    return std::make_tuple(MACH_VM_MIN_ADDRESS, 0, kr);
+    return std::make_tuple(MACH_VM_MIN_ADDRESS, 0, kr, shared);
 }
 
 RemoteBuffer::~RemoteBuffer() {
     if (!_localAddress) { return; }
-    free((void*)_localAddress);
+
+    if (_shared) {
+        (void)vm_deallocate(mach_task_self(), (vm_address_t)_localAddress, _size);
+    } else {
+        free((void*)_localAddress);
+    }
 }
 void *RemoteBuffer::getLocalAddress() const { return (void *)_localAddress; }
 size_t RemoteBuffer::getSize() const { return _size; }
 kern_return_t RemoteBuffer::getKernelReturn() const { return _kr; }
 
-void withRemoteBuffer(task_t task, mach_vm_address_t remote_address, size_t remote_size, bool allow_truncation, kern_return_t *kr, void (^block)(void *buffer, size_t size)) {
+void withRemoteBuffer(task_t task, mach_vm_address_t remote_address, size_t remote_size, bool shared, bool allow_truncation, kern_return_t *kr, void (^block)(void *buffer, size_t size)) {
     kern_return_t krSink = KERN_SUCCESS;
     if (kr == nullptr) {
         kr = &krSink;
     }
-    RemoteBuffer buffer(task, remote_address, remote_size, allow_truncation);
+    RemoteBuffer buffer(task, remote_address, remote_size, shared, allow_truncation);
     *kr = buffer.getKernelReturn();
     if (*kr == KERN_SUCCESS) {
         block(buffer.getLocalAddress(), buffer.getSize());
@@ -309,11 +323,13 @@
         if (result) {
             // If it returned the process is suspended and there is nothing more to do
             return std::move(result);
-        }
-        usleep(1000 * 50); // 50ms
-        // Not exactly correct, but conveys that operation may succeed in the future
-        *kr = KERN_RESOURCE_SHORTAGE;
-        return  nullptr;
+        } else {
+            // Check to see if the process change timestamp is greater than 0, if not then sleep to let the process
+            // finish initializing
+            if (allImageInfo.infoArrayChangeTimestamp == 0) {
+                usleep(1000 * 50); // 50ms
+            }
+        }
     }
 
     // Test to see if there are no changes and we can exit early
@@ -322,138 +338,151 @@
         return nullptr;
     }
 
-    uint64_t currentTimestamp = allImageInfo.infoArrayChangeTimestamp;
-    mach_vm_address_t infoArray = allImageInfo.infoArray;
-    if (infoArray == 0) {
-        usleep(1000 * 50); // 50ms
-        // Not exactly correct, but conveys that operation may succeed in the future
-        *kr = KERN_RESOURCE_SHORTAGE;
-        return  nullptr;
-    };
-
-    // For the moment we are going to truncate any image list longer than 8192 because some programs do
-    // 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, 8192);
-    size_t imageArraySize = imageCount * sizeof(T2);
-
-    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 = _dyld_get_all_image_infos();
-        bool sameCacheAsThisProcess = !allImageInfo.processDetachedFromSharedRegion
-            && !myInfo->processDetachedFromSharedRegion
-            && ((memcmp(myInfo->sharedCacheUUID, &allImageInfo.sharedCacheUUID[0], 16) == 0)
-            && (myInfo->sharedCacheSlide == allImageInfo.sharedCacheSlide));
-        unsigned countOfPathsNeedingCopying = 0;
-        if ( sameCacheAsThisProcess ) {
+    for (uint32_t j=0; j < 10; ++j) {
+        uint64_t currentTimestamp = allImageInfo.infoArrayChangeTimestamp;
+        mach_vm_address_t infoArray = allImageInfo.infoArray;
+        if (currentTimestamp == 0) continue;
+        if (infoArray == 0) {
+            // Check if the task is suspended mid dylib load and exit early
+            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))) {
+                continue;
+            }
+
+            // The task is suspended, exit
+            if (ti.suspend_count != 0) {
+                // Not exactly correct, but conveys that operation may succeed in the future
+                *kr = KERN_RESOURCE_SHORTAGE;
+                return  nullptr;
+            }
+            continue;
+        };
+
+        // For the moment we are going to truncate any image list longer than 8192 because some programs do
+        // 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, 8192);
+        size_t imageArraySize = imageCount * sizeof(T2);
+
+        withRemoteBuffer(task, infoArray, imageArraySize, false, 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 = _dyld_get_all_image_infos();
+            bool sameCacheAsThisProcess = !allImageInfo.processDetachedFromSharedRegion
+                && !myInfo->processDetachedFromSharedRegion
+                && ((memcmp(myInfo->sharedCacheUUID, &allImageInfo.sharedCacheUUID[0], 16) == 0)
+                && (myInfo->sharedCacheSlide == allImageInfo.sharedCacheSlide));
+            unsigned countOfPathsNeedingCopying = 0;
+            if ( sameCacheAsThisProcess ) {
+                for (uint32_t i=0; i < imageCount; ++i) {
+                    if ( !inCache(imageArray[i].imageFilePath) )
+                        ++countOfPathsNeedingCopying;
+                }
+            }
+            else {
+                countOfPathsNeedingCopying = imageCount+1;
+            }
+            unsigned imageCountWithDyld = imageCount+1;
+
+            // allocate result object
+            size_t allocationSize = sizeof(dyld_process_info_base)
+                                        + sizeof(dyld_process_cache_info)
+                                        + sizeof(dyld_process_aot_cache_info)
+                                        + sizeof(dyld_process_state_info)
+                                        + sizeof(ImageInfo)*(imageCountWithDyld)
+                                        + 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);
+            if (storage == nullptr) {
+                *kr = KERN_NO_SPACE;
+                result = nullptr;
+                return;
+            }
+            auto info = dyld_process_info_ptr(new (storage) dyld_process_info_base(allImageInfo.platform, imageCountWithDyld, allImageInfo.aotInfoCount, allocationSize), deleter);
+            (void)info->reserveSpace(sizeof(dyld_process_info_base)+sizeof(dyld_process_cache_info)+sizeof(dyld_process_state_info)+sizeof(dyld_process_aot_cache_info));
+            (void)info->reserveSpace(sizeof(ImageInfo)*imageCountWithDyld);
+
+            // fill in base info
+            dyld_process_cache_info* cacheInfo = info->cacheInfo();
+            memcpy(cacheInfo->cacheUUID, &allImageInfo.sharedCacheUUID[0], 16);
+            cacheInfo->cacheBaseAddress    = allImageInfo.sharedCacheBaseAddress;
+            cacheInfo->privateCache        = allImageInfo.processDetachedFromSharedRegion;
+            // if no cache is used, allImageInfo has all zeros for cache UUID
+            cacheInfo->noCache = true;
+            for (int i=0; i < 16; ++i) {
+                if ( cacheInfo->cacheUUID[i] != 0 ) {
+                    cacheInfo->noCache = false;
+                }
+            }
+
+            // fill in aot shared cache info
+            dyld_process_aot_cache_info* aotCacheInfo = info->aotCacheInfo();
+            memcpy(aotCacheInfo->cacheUUID, &allImageInfo.aotSharedCacheUUID[0], 16);
+            aotCacheInfo->cacheBaseAddress = allImageInfo.aotSharedCacheBaseAddress;
+
+            dyld_process_state_info* stateInfo = info->stateInfo();
+            stateInfo->timestamp           = currentTimestamp;
+            stateInfo->imageCount          = imageCountWithDyld;
+            stateInfo->initialImageCount   = (uint32_t)(allImageInfo.initialImageCount+1);
+            stateInfo->dyldState = dyld_process_state_dyld_initialized;
+
+            if ( allImageInfo.libSystemInitialized != 0 ) {
+                stateInfo->dyldState = dyld_process_state_libSystem_initialized;
+                if ( allImageInfo.initialImageCount != imageCount ) {
+                    stateInfo->dyldState = dyld_process_state_program_running;
+                }
+            }
+            if ( allImageInfo.errorMessage != 0 ) {
+                stateInfo->dyldState = allImageInfo.terminationFlags ? dyld_process_state_terminated_before_inits : dyld_process_state_dyld_terminated;
+            }
+            // fill in info for dyld
+            if ( allImageInfo.dyldPath != 0 ) {
+                if ((*kr = info->addDyldImage(task, allImageInfo.dyldImageLoadAddress, allImageInfo.dyldPath, NULL))) {
+                    result = nullptr;
+                    return;
+                }
+            }
+            // fill in info for each image
             for (uint32_t i=0; i < imageCount; ++i) {
-                if ( !inCache(imageArray[i].imageFilePath) )
-                    ++countOfPathsNeedingCopying;
-            }
-        }
-        else {
-            countOfPathsNeedingCopying = imageCount+1;
-        }
-        unsigned imageCountWithDyld = imageCount+1;
-
-        // allocate result object
-        size_t allocationSize = sizeof(dyld_process_info_base)
-                                    + sizeof(dyld_process_cache_info)
-                                    + sizeof(dyld_process_aot_cache_info)
-                                    + sizeof(dyld_process_state_info)
-                                    + sizeof(ImageInfo)*(imageCountWithDyld)
-                                    + 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);
-        if (storage == nullptr) {
-            *kr = KERN_NO_SPACE;
-            result = nullptr;
-            return;
-        }
-        auto info = dyld_process_info_ptr(new (storage) dyld_process_info_base(allImageInfo.platform, imageCountWithDyld, allImageInfo.aotInfoCount, allocationSize), deleter);
-        (void)info->reserveSpace(sizeof(dyld_process_info_base)+sizeof(dyld_process_cache_info)+sizeof(dyld_process_state_info)+sizeof(dyld_process_aot_cache_info));
-        (void)info->reserveSpace(sizeof(ImageInfo)*imageCountWithDyld);
-
-        // fill in base info
-        dyld_process_cache_info* cacheInfo = info->cacheInfo();
-        memcpy(cacheInfo->cacheUUID, &allImageInfo.sharedCacheUUID[0], 16);
-        cacheInfo->cacheBaseAddress    = allImageInfo.sharedCacheBaseAddress;
-        cacheInfo->privateCache        = allImageInfo.processDetachedFromSharedRegion;
-        // if no cache is used, allImageInfo has all zeros for cache UUID
-        cacheInfo->noCache = true;
-        for (int i=0; i < 16; ++i) {
-            if ( cacheInfo->cacheUUID[i] != 0 ) {
-                cacheInfo->noCache = false;
-            }
-        }
-
-        // fill in aot shared cache info
-        dyld_process_aot_cache_info* aotCacheInfo = info->aotCacheInfo();
-        memcpy(aotCacheInfo->cacheUUID, &allImageInfo.aotSharedCacheUUID[0], 16);
-        aotCacheInfo->cacheBaseAddress = allImageInfo.aotSharedCacheBaseAddress;
-
-        dyld_process_state_info* stateInfo = info->stateInfo();
-        stateInfo->timestamp           = currentTimestamp;
-        stateInfo->imageCount          = imageCountWithDyld;
-        stateInfo->initialImageCount   = (uint32_t)(allImageInfo.initialImageCount+1);
-        stateInfo->dyldState = dyld_process_state_dyld_initialized;
-
-        if ( allImageInfo.libSystemInitialized != 0 ) {
-            stateInfo->dyldState = dyld_process_state_libSystem_initialized;
-            if ( allImageInfo.initialImageCount != imageCount ) {
-                stateInfo->dyldState = dyld_process_state_program_running;
-            }
-        }
-        if ( allImageInfo.errorMessage != 0 ) {
-            stateInfo->dyldState = allImageInfo.terminationFlags ? dyld_process_state_terminated_before_inits : dyld_process_state_dyld_terminated;
-        }
-        // fill in info for dyld
-        if ( allImageInfo.dyldPath != 0 ) {
-            if ((*kr = info->addDyldImage(task, allImageInfo.dyldImageLoadAddress, allImageInfo.dyldPath, NULL))) {
+                if (!info->addImage(task, sameCacheAsThisProcess, imageArray[i].imageLoadAddress, imageArray[i].imageFilePath, NULL)) {
+                    result = nullptr;
+                    return;
+                }
+            }
+            // sanity check internal data did not overflow
+            if ( info->invalid() ) {
                 *kr = KERN_FAILURE;
                 result = nullptr;
                 return;
             }
-        }
-        // fill in info for each image
-        for (uint32_t i=0; i < imageCount; ++i) {
-            if (!info->addImage(task, sameCacheAsThisProcess, imageArray[i].imageLoadAddress, imageArray[i].imageFilePath, NULL)) {
-                *kr = KERN_FAILURE;
-                result = nullptr;
-                return;
-            }
-        }
-        // sanity check internal data did not overflow
-        if ( info->invalid() ) {
-            *kr = KERN_FAILURE;
-            result = nullptr;
-            return;
-        }
-
-        result = std::move(info);
-    });
-
-    mach_vm_address_t aotImageArray = allImageInfo.aotInfoArray;
-    // shortcircuit this code path if aotImageArray == 0 (32 vs 64 bit struct difference)
-    // and if result == nullptr, since we need to append aot image infos to the process info struct
-    if (aotImageArray != 0 && result != nullptr) {
-        uint32_t aotImageCount = allImageInfo.aotInfoCount;
-        size_t aotImageArraySize = aotImageCount * sizeof(dyld_aot_image_info_64);
-
-        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 (!result->addAotImage(imageArray[i])) {
-                    *kr = KERN_FAILURE;
-                    result = nullptr;
-                    return;
+
+            result = std::move(info);
+        });
+
+        mach_vm_address_t aotImageArray = allImageInfo.aotInfoArray;
+        // shortcircuit this code path if aotImageArray == 0 (32 vs 64 bit struct difference)
+        // and if result == nullptr, since we need to append aot image infos to the process info struct
+        if (aotImageArray != 0 && result != nullptr) {
+            uint32_t aotImageCount = allImageInfo.aotInfoCount;
+            size_t aotImageArraySize = aotImageCount * sizeof(dyld_aot_image_info_64);
+
+            withRemoteBuffer(task, aotImageArray, aotImageArraySize, false, 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 (!result->addAotImage(imageArray[i])) {
+                        result = nullptr;
+                        return;
+                    }
                 }
-            }
-        });
-    }
+            });
+        }
+
+        if (result) break;
+    }
+
     return std::move(result);
 }
 
@@ -496,7 +525,7 @@
         if ( info.protection != (VM_PROT_READ|VM_PROT_EXECUTE) )
             continue;
             // read start of vm region to verify it is a mach header
-            withRemoteObject(task, address, NULL, ^(mach_header_64 mhBuffer){
+            withRemoteObject(task, address, false, NULL, ^(mach_header_64 mhBuffer){
                 if ( (mhBuffer.magic != MH_MAGIC) && (mhBuffer.magic != MH_MAGIC_64) )
                     return;
                 // now know the region is the start of a mach-o file
@@ -605,7 +634,7 @@
 const char* dyld_process_info_base::copyPath(task_t task, uint64_t stringAddressInTask)
 {
     __block const char* retval = "";
-    withRemoteBuffer(task, stringAddressInTask, PATH_MAX, true, nullptr, ^(void *buffer, size_t size) {
+    withRemoteBuffer(task, stringAddressInTask, PATH_MAX, false, true, nullptr, ^(void *buffer, size_t size) {
         retval = addString(static_cast<const char *>(buffer), size);
     });
     return retval;
@@ -655,7 +684,7 @@
     __block bool done = false;
 
     //Since the minimum we can reasonably map is a page, map that.
-    withRemoteBuffer(task, remoteMH, PAGE_SIZE, false, &kr, ^(void * buffer, size_t size) {
+    withRemoteBuffer(task, remoteMH, PAGE_SIZE, false, false, &kr, ^(void * buffer, size_t size) {
         const mach_header* mh = (const mach_header*)buffer;
         headerPagesSize = sizeof(mach_header) + mh->sizeofcmds;
         if (headerPagesSize <= PAGE_SIZE) {
@@ -669,7 +698,7 @@
         if (kr != KERN_SUCCESS) {
             return kr;
         }
-        withRemoteBuffer(task, remoteMH, headerPagesSize, false, &kr, ^(void * buffer, size_t size) {
+        withRemoteBuffer(task, remoteMH, headerPagesSize, false, false, &kr, ^(void * buffer, size_t size) {
             addInfoFromLoadCommands((mach_header*)buffer, remoteMH, size);
         });
     }
@@ -819,30 +848,26 @@
     }
 
     //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) {
-        *kr = KERN_FAILURE;
+    if (task_dyld_info.all_image_info_addr == MACH_VM_MIN_ADDRESS)
         return nullptr;
-    }
-
-    for (auto i = 0; i < 10; ++i) {
-        withRemoteBuffer(task, task_dyld_info.all_image_info_addr, (size_t)task_dyld_info.all_image_info_size, false, kr, ^(void *buffer, size_t size) {
-            dyld_process_info_ptr base;
-            if (task_dyld_info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32 ) {
-                const dyld_all_image_infos_32* info = (const dyld_all_image_infos_32*)buffer;
-                base = dyld_process_info_base::make<dyld_all_image_infos_32, dyld_image_info_32>(task, *info, timestamp, kr);
-            } else {
-                const dyld_all_image_infos_64* info = (const dyld_all_image_infos_64*)buffer;
-                base = dyld_process_info_base::make<dyld_all_image_infos_64, dyld_image_info_64>(task, *info, timestamp, kr);
-            }
-            if (base) {
-                if (result) {
-                    free((void*)result);
-                }
-                result = base.release();
-            }
-        });
-        if (kr == KERN_SUCCESS) { break; }
-    }
+
+    // We use a true shared memory buffer here, that way by making sure that libdyld in both processes
+    // reads and writes the the timestamp atomically we can make sure we get a coherent view of the
+    // remote process.
+    // That also means that we *MUST* directly read the memory, which is why we template the make() call
+    withRemoteBuffer(task, task_dyld_info.all_image_info_addr, (size_t)task_dyld_info.all_image_info_size, true, false, kr, ^(void *buffer, size_t size) {
+        dyld_process_info_ptr base;
+        if (task_dyld_info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32 ) {
+            const dyld_all_image_infos_32* info = (const dyld_all_image_infos_32*)buffer;
+            base = dyld_process_info_base::make<dyld_all_image_infos_32, dyld_image_info_32>(task, *info, timestamp, kr);
+        } else {
+            const dyld_all_image_infos_64* info = (const dyld_all_image_infos_64*)buffer;
+            base = dyld_process_info_base::make<dyld_all_image_infos_64, dyld_image_info_64>(task, *info, timestamp, kr);
+        }
+        if (base) {
+            result = base.release();
+        }
+    });
     return  result;
 }