Loading...
libdyld/dyld_process_info.cpp dyld-1340 dyld-1122.1
--- dyld/dyld-1340/libdyld/dyld_process_info.cpp
+++ dyld/dyld-1122.1/libdyld/dyld_process_info.cpp
@@ -41,17 +41,9 @@
 #include <mach-o/dyld_priv.h>
 
 #include "MachOFile.h"
-#include "DyldSharedCache.h"
 #include "dyld_process_info_internal.h"
 #include "Tracing.h"
 #include "DyldProcessConfig.h"
-#include "StringUtils.h"
-#include "DyldLegacyInterfaceGlue.h"
-#include "SafeVMPrimitives.h"
-
-extern "C" const dyld_all_image_infos* getProcessDyldInfo(); 
-
-#define BLEND_KERN_RETURN_LOCATION(kr, loc) (kr) = ((kr & 0x00ffffff) | loc<<24);
 
 #define IMAGE_COUNT_MAX 8192
 RemoteBuffer& RemoteBuffer::operator=(RemoteBuffer&& other) {
@@ -70,17 +62,29 @@
 
 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;
     vm_prot_t cur_protection = VM_PROT_NONE;
     vm_prot_t max_protection = VM_PROT_READ;
     if (size == 0) {
         return std::make_pair(MACH_VM_MIN_ADDRESS, KERN_INVALID_ARGUMENT);
     }
     mach_vm_address_t localAddress = 0;
-#if __arm64e__
-    // Mask out TBI bits
-    remote_address &= 0x00ff'ffff'ffff'ffffUL;
+#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 = mach_vm_remap_new(mach_task_self(),
+    auto kr = mvrn(mach_task_self(),
                         &localAddress,
                         size,
                         0,  // mask
@@ -93,7 +97,6 @@
                         VM_INHERIT_NONE);
     // The call is not succesfull return
     if (kr != KERN_SUCCESS) {
-        BLEND_KERN_RETURN_LOCATION(kr, 0xfd)
         return std::make_pair(MACH_VM_MIN_ADDRESS, kr);
     }
     // If it is not a shared buffer then copy it into a local buffer so our results are coherent in the event
@@ -107,9 +110,7 @@
         (void)vm_deallocate(mach_task_self(), (vm_address_t)localAddress, size);
         return std::make_pair(MACH_VM_MIN_ADDRESS, KERN_NO_SPACE);
     }
-    remote_memory_audit_start();
     memcpy(buffer, (void *)localAddress, size);
-    remote_memory_audit_end();
     ((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);
@@ -231,15 +232,15 @@
                                 dyld_process_info_base(dyld_platform_t platform, unsigned imageCount, unsigned aotImageCount, size_t totalSize);
     void*                       operator new (size_t, void* buf) { return buf; }
 
-    kern_return_t               addImage(task_t task, bool sameCacheAsThisProcess, uint64_t sharedCacheStart, uint64_t sharedCacheEnd, uint64_t imageAddress, uint64_t imagePath, const char* imagePathLocal,
-                                         uint32_t imageIndex);
+    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);
 
     kern_return_t               addDyldImage(task_t task, uint64_t dyldAddress, uint64_t dyldPathAddress, const char* localPath);
 
     bool                        invalid() { return ((char*)_stringRevBumpPtr < (char*)_curSegment); }
-    const char*                 copyPath(task_t task, kern_return_t* kr, uint64_t pathAddr);
+    const char*                 copyPath(task_t task, uint64_t pathAddr);
     const char*                 addString(const char*, size_t);
     const char*                 copySegmentName(const char*);
 
@@ -369,34 +370,19 @@
     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 = getProcessDyldInfo(); // FIXME: should not need dyld_all_image_info
-        const bool sameCacheAsThisProcess = true
-            && (myInfo != nullptr)
-            && (allImageInfo.sharedCacheBaseAddress != 0)
-            && (myInfo->sharedCacheBaseAddress != 0)
-            && !allImageInfo.processDetachedFromSharedRegion
+        const dyld_all_image_infos* myInfo = (const dyld_all_image_infos*)dyld4::gDyld.allImageInfos;   // FIXME: should not need dyld_all_image_info
+        bool sameCacheAsThisProcess = !allImageInfo.processDetachedFromSharedRegion
             && !myInfo->processDetachedFromSharedRegion
             && ((memcmp(myInfo->sharedCacheUUID, &allImageInfo.sharedCacheUUID[0], 16) == 0)
             && (myInfo->sharedCacheSlide == allImageInfo.sharedCacheSlide));
         unsigned countOfPathsNeedingCopying = 0;
-        // If constexpr is basically superfluous, we only do it to avoid compiler warnings...
-        // The only time when sizeof(T2().imageFilePath) != sizeof(void*) is when a 64 bit process is introspecting a 32 bit process
-        // or vice versa, and in those cases sameCacheAsThisProcess will be false. The issue is the compiler can't know that which
-        // forces it to build the code, which results in the case from imageArray[i].imageFilePath to a (void*) to generate a warning.
-        // By using the constexpr check here we can elide compiling that case and the warning, which prevents -Werror failues.
-        if constexpr(sizeof(T2().imageFilePath) == sizeof(void*)) {
-            const DyldSharedCache* dyldCacheHeader = (DyldSharedCache*)sharedCacheStart;
-            if ( sameCacheAsThisProcess && dyldCacheHeader != nullptr) {
-                bool readOnly = false;
-                for (uint32_t i=0; i < imageCount; ++i) {
-                    if ( !dyldCacheHeader->inCache((void*)imageArray[i].imageFilePath, 1, readOnly) || !readOnly )
-                        ++countOfPathsNeedingCopying;
-                }
-            }
-            else {
-                countOfPathsNeedingCopying = imageCount+1;
-            }
-        } else {
+        if ( sameCacheAsThisProcess ) {
+            for (uint32_t i=0; i < imageCount; ++i) {
+                if ( !inCache(imageArray[i].imageFilePath, sharedCacheStart, sharedCacheEnd) )
+                    ++countOfPathsNeedingCopying;
+            }
+        }
+        else {
             countOfPathsNeedingCopying = imageCount+1;
         }
         unsigned imageCountWithDyld = imageCount+1;
@@ -464,7 +450,7 @@
         }
         // 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, i);
+            *kr =  info->addImage(task, sameCacheAsThisProcess, sharedCacheStart, sharedCacheEnd, imageArray[i].imageLoadAddress, imageArray[i].imageFilePath, NULL);
             if (*kr != KERN_SUCCESS) {
                 result = nullptr;
                 return;
@@ -513,7 +499,6 @@
     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;
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xfc)
         return  nullptr;
     }
 
@@ -526,7 +511,6 @@
         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) {
-            BLEND_KERN_RETURN_LOCATION(*kr, 0xfb)
             // Not a corpse, so forward progress is possible. Return so that make() can pause and retry
             return nullptr;
         }
@@ -541,10 +525,8 @@
     char                    mainExecutablePathBuffer[PATH_MAX+1];
     __block char *          dyldPath = &dyldPathBuffer[0];
     __block char *          mainExecutablePath = &mainExecutablePathBuffer[0];
-    mach_vm_size_t          size;
-    dyldPathBuffer[0]           = '\0';
-    mainExecutablePathBuffer[0] = '\0';
-
+    __block dyld3::Platform platformID = dyld3::Platform::unknown;
+    mach_vm_size_t      size;
     for (mach_vm_address_t address = 0; ; address += size) {
         vm_region_basic_info_data_64_t  info;
         mach_port_t                     objectName;
@@ -563,18 +545,16 @@
                 if ( mhBuffer.filetype == MH_EXECUTE ) {
                     mainExecutableAddress = address;
                     int len = proc_regionfilename(pid, mainExecutableAddress, mainExecutablePath, PATH_MAX);
-                    // explicitly clear the path if call failed
-                    if ( len <= 0 ) {
-                        strlcpy(mainExecutablePath, "/main_executable_path_missing", PATH_MAX);
+                    if ( len != 0 ) {
+                        mainExecutablePath[len] = '\0';
                     }
                     ++imageCount;
                 }
                 else if ( mhBuffer.filetype == MH_DYLINKER ) {
                     dyldAddress = address;
                     int len = proc_regionfilename(pid, dyldAddress, dyldPath, PATH_MAX);
-                    // explicitly clear the path if call failed
-                    if ( len <= 0 ) {
-                        strlcpy(dyldPath, "/dyld_path_missing", PATH_MAX);
+                    if ( len != 0 ) {
+                        dyldPath[len] = '\0';
                     }
                     ++imageCount;
                 }
@@ -608,10 +588,9 @@
     void* storage = malloc(allocationSize);
     if (storage == nullptr) {
         *kr = KERN_NO_SPACE;
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xfa)
         return  nullptr;
     }
-    auto obj = dyld_process_info_ptr(new (storage) dyld_process_info_base(PLATFORM_UNKNOWN, imageCount, aotImageCount, allocationSize), deleter);
+    auto obj = dyld_process_info_ptr(new (storage) dyld_process_info_base((dyld_platform_t)platformID, imageCount, aotImageCount, allocationSize), deleter);
     (void)obj->reserveSpace(sizeof(dyld_process_info_base)+sizeof(dyld_process_cache_info)+sizeof(dyld_process_aot_cache_info)+sizeof(dyld_process_state_info));
     // fill in base info
     dyld_process_cache_info* cacheInfo = obj->cacheInfo();
@@ -639,7 +618,7 @@
 
     // fill in info for each image
     if ( mainExecutableAddress != 0 ) {
-        if ((*kr = obj->addImage(task, false, 0, 0, mainExecutableAddress, 0, mainExecutablePath, 0))) {
+        if ((*kr = obj->addImage(task, false, 0, 0, mainExecutableAddress, 0, mainExecutablePath))) {
             return nullptr;
         }
     }
@@ -664,60 +643,30 @@
     return _stringRevBumpPtr;
 }
 
-const char* dyld_process_info_base::copyPath(task_t task, kern_return_t *kr, uint64_t stringAddressInTask)
+const char* dyld_process_info_base::copyPath(task_t task, uint64_t stringAddressInTask)
 {
     __block const char* retval = "";
-    withRemoteBuffer(task, stringAddressInTask, PATH_MAX, true, kr, ^(void *buffer, size_t size) {
+    withRemoteBuffer(task, stringAddressInTask, PATH_MAX, true, nullptr, ^(void *buffer, size_t size) {
         retval = addString(static_cast<const char *>(buffer), size);
     });
     return retval;
 }
 
-static char sCrashReporterInfo[4096];
-
-static void checkPath(const char* path, uint64_t pathAddress, uint32_t imageIndex)
-{
-    if ( path[0] != '/' ) {
-        // Set the crash reporter info to help work out why we pass non-UTF8 strings to CS
-        snprintf(sCrashReporterInfo, sizeof(sCrashReporterInfo), "dyld: found non-UTF8 string on image[%d]: 0x%llx [ ", imageIndex, pathAddress);
-        for ( char c : std::string_view(path, strnlen(path, 8)) ) {
-            char buffer[8];
-            bytesToHex((uint8_t*)&c, 1, buffer);
-            strlcat(sCrashReporterInfo, buffer, sizeof(sCrashReporterInfo));
-            strlcat(sCrashReporterInfo, " ", sizeof(sCrashReporterInfo));
-        }
-
-        strlcat(sCrashReporterInfo, " ]\n", sizeof(sCrashReporterInfo));
-
-        CRSetCrashLogMessage2(sCrashReporterInfo);
-    }
-}
-
-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,
-                                               uint32_t imageIndex)
-{
-    const DyldSharedCache* dyldCacheHeader = (DyldSharedCache*)sharedCacheStart;
-    bool readOnly = false;
+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)
+{
     _curImage->loadAddress = imageAddress;
     _curImage->segmentStartIndex = _curSegmentIndex;
     if ( imagePathLocal != NULL ) {
         _curImage->path = addString(imagePathLocal, PATH_MAX);
-    } else if ( sameCacheAsThisProcess && dyldCacheHeader != nullptr && dyldCacheHeader->inCache((void*)imagePath, 1, readOnly) && readOnly ) {
+    } else if ( sameCacheAsThisProcess && inCache(imagePath, sharedCacheStart, sharedCacheEnd) ) {
         _curImage->path = (const char*)imagePath;
     } else if (imagePath) {
-        kern_return_t kr = KERN_SUCCESS;
-        _curImage->path = copyPath(task, &kr, imagePath);
-        if ( kr != KERN_SUCCESS ) {
-            return kr;
-        }
+        _curImage->path = copyPath(task, imagePath);
     } else {
-        _curImage->path = addString("/<unknown>", PATH_MAX);
-    }
-
-    checkPath(_curImage->path, imagePath, imageIndex);
-
-    if ( sameCacheAsThisProcess && dyldCacheHeader != nullptr && dyldCacheHeader->inCache((void*)imageAddress, 32*1024, readOnly) ) {
+        _curImage->path = "";
+    }
+
+    if ( sameCacheAsThisProcess && inCache(imageAddress, sharedCacheStart, sharedCacheEnd) ) {
         addInfoFromLoadCommands((mach_header*)imageAddress, imageAddress, 32*1024);
     } else {
         auto kr = addInfoFromRemoteLoadCommands(task, imageAddress);
@@ -783,12 +732,10 @@
         _curImage->path = addString(localPath, PATH_MAX);
     }
     else {
-        _curImage->path = copyPath(task, &kr, dyldPathAddress);
+        _curImage->path = copyPath(task, dyldPathAddress);
         if ( kr != KERN_SUCCESS)
             return kr;
     }
-
-    checkPath(_curImage->path, dyldPathAddress, ~0U);
 
     kr = addInfoFromRemoteLoadCommands(task, dyldAddress);
     if ( kr != KERN_SUCCESS)
@@ -864,18 +811,7 @@
 void dyld_process_info_base::forEachImage(void (^callback)(uint64_t machHeaderAddress, const uuid_t uuid, const char* path)) const
 {
     for (const ImageInfo* p = _firstImage; p < _curImage; ++p) {
-        // Set a temporary to hold a log message. We will always switch back to the original value before the scope ends, so it can live on the stack
-        char asiBuffer[1024];
-        const char* oldLogMessage = CRGetCrashLogMessage();
-        if (p->loadAddress == 0 || p->path == nullptr) {
-            // Set an ASI for debugging. Unfortunately we don't have much state here, so let's grab what we have that is meaingful which will hopefully
-            // let us figure out why this is sometimes set incorrectly.
-            snprintf(&asiBuffer[0], 1024, "Bad dyld_proces_info image info for\n\tplatform: %u\n\taddress = 0x%llx\n\tpath = %s\n",
-                     _platform, p->loadAddress, p->path ? p->path : "(null)");
-            CRSetCrashLogMessage(asiBuffer);
-        }
         callback(p->loadAddress, p->uuid, p->path);
-        CRSetCrashLogMessage(oldLogMessage);
     }
 }
 
@@ -914,9 +850,6 @@
 
 dyld_process_info _dyld_process_info_create(task_t task, uint64_t timestamp, kern_return_t* kr)
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_create(task, timestamp, kr);
-    }
     __block dyld_process_info result = nullptr;
     kern_return_t krSink = KERN_SUCCESS;
     if (kr == nullptr) {
@@ -928,14 +861,12 @@
     mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
     if ( kern_return_t r = task_info(task, TASK_DYLD_INFO, (task_info_t)&task_dyld_info, &count) ) {
         *kr = r;
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xff);
         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) {
         *kr = KERN_FAILURE;
-        BLEND_KERN_RETURN_LOCATION(*kr, 0xfe);
         return nullptr;
     }
 
@@ -969,74 +900,47 @@
 
 void _dyld_process_info_get_state(dyld_process_info info, dyld_process_state_info* stateInfo)
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_get_state(info, stateInfo);
-    }
     *stateInfo = *info->stateInfo();
 }
 
 void _dyld_process_info_get_cache(dyld_process_info info, dyld_process_cache_info* cacheInfo)
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_get_cache(info, cacheInfo);
-    }
     *cacheInfo = *info->cacheInfo();
 }
 
 void _dyld_process_info_get_aot_cache(dyld_process_info info, dyld_process_aot_cache_info* aotCacheInfo)
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_get_aot_cache(info, aotCacheInfo);
-    }
     *aotCacheInfo = *info->aotCacheInfo();
 }
 
 void _dyld_process_info_retain(dyld_process_info object)
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_retain(object);
-    }
     const_cast<dyld_process_info_base*>(object)->retain();
 }
 
 dyld_platform_t _dyld_process_info_get_platform(dyld_process_info object) {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_get_platform(object);
-    }
     return  const_cast<dyld_process_info_base*>(object)->platform();
 }
 
 void _dyld_process_info_release(dyld_process_info object)
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_release(object);
-    }
     const_cast<dyld_process_info_base*>(object)->release();
 }
 
 void _dyld_process_info_for_each_image(dyld_process_info info, void (^callback)(uint64_t machHeaderAddress, const uuid_t uuid, const char* path))
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_for_each_image(info, callback);
-    }
     info->forEachImage(callback);
 }
 
 #if TARGET_OS_OSX
 void _dyld_process_info_for_each_aot_image(dyld_process_info info, bool (^callback)(uint64_t x86Address, uint64_t aotAddress, uint64_t aotSize, uint8_t* aotImageKey, size_t aotImageKeySize))
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_for_each_aot_image(info, callback);
-    }
     info->forEachAotImage(callback);
 }
 #endif
 
 void _dyld_process_info_for_each_segment(dyld_process_info info, uint64_t machHeaderAddress, void (^callback)(uint64_t segmentAddress, uint64_t segmentSize, const char* segmentName))
 {
-    if ( const IntrospectionVtable* vtable = dyldFrameworkIntrospectionVtable() ) {
-        return vtable->_dyld_process_info_for_each_segment(info, machHeaderAddress, callback);
-    }
     info->forEachSegment(machHeaderAddress, callback);
 }