Loading...
--- dyld/dyld-519.2.1/src/dyld_process_info.cpp
+++ dyld/dyld-421.2/src/dyld_process_info.cpp
@@ -38,62 +38,6 @@
#include "dyld_images.h"
#include "dyld_priv.h"
-// this was in dyld_priv.h but it is no longer exported
-extern "C" {
- const struct dyld_all_image_infos* _dyld_get_all_image_infos();
-}
-
-namespace {
-
-void withRemoteBuffer(task_t task, vm_address_t remote_address, size_t remote_size, bool allow_truncation, kern_return_t *kr, void (^block)(void *buffer)) {
- kern_return_t r = KERN_SUCCESS;
- mach_vm_address_t local_address = 0;
- mach_vm_address_t local_size = remote_size;
- while (1) {
- vm_prot_t cur_protection, max_protection;
- r = mach_vm_remap(mach_task_self(),
- &local_address,
- local_size,
- 0, // mask
- VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR | VM_FLAGS_RESILIENT_CODESIGN,
- task,
- remote_address,
- TRUE, // Copy semantics: changes to this memory by the target process will not be visible in this process
- &cur_protection,
- &max_protection,
- VM_INHERIT_DEFAULT);
- //Do this here to allow chaining of multiple embedded blocks with a single error out;
- if (kr != NULL) {
- *kr = r;
- }
- if (r == KERN_SUCCESS) {
- // We got someting, call the block and then exit
- block(reinterpret_cast<void *>(local_address));
- vm_deallocate(mach_task_self(), local_address, local_size);
- break;
- }
- if (!allow_truncation) {
- break;
- }
- // We did not get something, but we are allowed to truncate and try again
- uint64_t trunc_size = ((remote_address + local_size - 1) & PAGE_MASK) + 1;
- if (local_size <= trunc_size) {
- //Even if we truncate it will be in the same page, time to accept defeat
- break;
- } else {
- local_size -= trunc_size;
- }
- }
-}
-
-template<typename T>
-void withRemoteObject(task_t task, vm_address_t remote_address, kern_return_t *kr, void (^block)(T t))
-{
- withRemoteBuffer(task, remote_address, sizeof(T), false, kr, ^(void *buffer) {
- block(*reinterpret_cast<T *>(buffer));
- });
-}
-};
//
// Opaque object returned by _dyld_process_info_create()
@@ -179,10 +123,7 @@
{
// figure out how many path strings will need to be copied and their size
const dyld_all_image_infos* myInfo = _dyld_get_all_image_infos();
- bool sameCacheAsThisProcess = !allImageInfo.processDetachedFromSharedRegion
- && !myInfo->processDetachedFromSharedRegion
- && ((memcmp(myInfo->sharedCacheUUID, allImageInfo.sharedCacheUUID, 16) == 0)
- && (myInfo->sharedCacheSlide == allImageInfo.sharedCacheSlide));
+ bool sameCacheAsThisProcess = ((memcmp(myInfo->sharedCacheUUID, allImageInfo.sharedCacheUUID, 16) == 0) && (myInfo->sharedCacheSlide == allImageInfo.sharedCacheSlide));
unsigned countOfPathsNeedingCopying = 0;
if ( sameCacheAsThisProcess ) {
for (int i=0; i < allImageInfo.infoArrayCount; ++i) {
@@ -240,7 +181,7 @@
goto fail;
}
}
-
+
// fill in info for each image
for (uint32_t i=0; i < allImageInfo.infoArrayCount; ++i) {
if ( kern_return_t r = obj->addImage(task, sameCacheAsThisProcess, imageArray[i].imageLoadAddress, imageArray[i].imageFilePath, NULL) ) {
@@ -271,13 +212,11 @@
return NULL;
}
- __block unsigned imageCount = 0; // main executable and dyld
- __block uint64_t mainExecutableAddress = 0;
- __block uint64_t dyldAddress = 0;
+ unsigned imageCount = 0; // main executable and dyld
+ uint64_t mainExecutableAddress = 0;
+ uint64_t dyldAddress = 0;
char dyldPathBuffer[PATH_MAX+1];
char mainExecutablePathBuffer[PATH_MAX+1];
- __block char * dyldPath = &dyldPathBuffer[0];
- __block char * mainExecutablePath = &mainExecutablePathBuffer[0];
mach_vm_size_t size;
for (mach_vm_address_t address = 0; ; address += size) {
vm_region_basic_info_data_64_t info;
@@ -287,31 +226,23 @@
(vm_region_info_t)&info, &infoCount, &objectName);
if ( result != KERN_SUCCESS )
break;
- 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){
- if ( (mhBuffer.magic != MH_MAGIC) && (mhBuffer.magic != MH_MAGIC_64) )
- return;
- // now know the region is the start of a mach-o file
- if ( mhBuffer.filetype == MH_EXECUTE ) {
- mainExecutableAddress = address;
- int len = proc_regionfilename(pid, mainExecutableAddress, mainExecutablePath, 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);
- if ( len != 0 ) {
- dyldPath[len] = '\0';
- }
- ++imageCount;
- }
- });
+ if ( info.protection == (VM_PROT_READ|VM_PROT_EXECUTE) ) {
+ if ( mainExecutableAddress == 0 ) {
+ mainExecutableAddress = address;
+ int len = proc_regionfilename(pid, mainExecutableAddress, mainExecutablePathBuffer, PATH_MAX);
+ if ( len != 0 )
+ mainExecutablePathBuffer[len] = '\0';
+ ++imageCount;
+ }
+ else if ( dyldAddress == 0 ) {
+ dyldAddress = address;
+ int len = proc_regionfilename(pid, dyldAddress, dyldPathBuffer, PATH_MAX);
+ if ( len != 0 )
+ dyldPathBuffer[len] = '\0';
+ ++imageCount;
+ }
//fprintf(stderr, "vm region: addr=0x%llX, size=0x%llX, prot=0x%X\n", (uint64_t)address, (uint64_t)size, info.protection);
+ }
}
//fprintf(stderr, "dyld: addr=0x%llX, path=%s\n", dyldAddress, dyldPathBuffer);
//fprintf(stderr, "app: addr=0x%llX, path=%s\n", mainExecutableAddress, mainExecutablePathBuffer);
@@ -341,7 +272,7 @@
// fill in info for dyld
if ( dyldAddress != 0 ) {
- if ( kern_return_t r = obj->addDyldImage(task, dyldAddress, 0, dyldPath) ) {
+ if ( kern_return_t r = obj->addDyldImage(task, dyldAddress, 0, dyldPathBuffer) ) {
if ( kr != NULL )
*kr = r;
free(obj);
@@ -351,7 +282,7 @@
// fill in info for each image
if ( mainExecutableAddress != 0 ) {
- if ( kern_return_t r = obj->addImage(task, false, mainExecutableAddress, 0, mainExecutablePath) ) {
+ if ( kern_return_t r = obj->addImage(task, false, mainExecutableAddress, 0, mainExecutablePathBuffer) ) {
if ( kr != NULL )
*kr = r;
free(obj);
@@ -374,12 +305,41 @@
const char* dyld_process_info_base::copyPath(task_t task, uint64_t stringAddressInTask, kern_return_t* kr)
{
- __block const char* retval = NULL;
- withRemoteBuffer(task, stringAddressInTask, PATH_MAX+8, true, kr, ^(void *buffer) {
- retval = addString(static_cast<const char *>(buffer));
- });
- return retval;
-}
+ char temp[PATH_MAX+8]; // +8 is to allow '\0' at temp[PATH_MAX]
+ mach_vm_size_t readSize = PATH_MAX;
+ if ( ((stringAddressInTask & 0xFFF) + PATH_MAX) < 4096 ) {
+ // string fits within page, only one vm_read needed
+ if ( kern_return_t r = mach_vm_read_overwrite(task, stringAddressInTask, PATH_MAX, (vm_address_t)&temp, &readSize) ) {
+ if ( kr != NULL )
+ *kr = r;
+ return NULL;
+ }
+ }
+ else {
+ // string may cross page boundary, split into two reads
+ size_t firstLen = 4096 - (stringAddressInTask & 0xFFF);
+ readSize = firstLen;
+ if ( kern_return_t r = mach_vm_read_overwrite(task, stringAddressInTask, firstLen, (vm_address_t)&temp, &readSize) ) {
+ if ( kr != NULL )
+ *kr = r;
+ return NULL;
+ }
+ temp[firstLen] = '\0';
+ if ( strlen(temp) >= firstLen ) {
+ readSize = PATH_MAX-firstLen;
+ if ( kern_return_t r = mach_vm_read_overwrite(task, stringAddressInTask+firstLen, PATH_MAX-firstLen, (vm_address_t)&temp+firstLen, &readSize) ) {
+ if ( kr != NULL )
+ *kr = r;
+ return NULL;
+ temp[PATH_MAX] = '\0'; // truncate any string that is too long
+ }
+ }
+ }
+ if ( kr != NULL )
+ *kr = KERN_SUCCESS;
+ return addString(temp);
+}
+
kern_return_t dyld_process_info_base::addImage(task_t task, bool sameCacheAsThisProcess, uint64_t imageAddress, uint64_t imagePath, const char* imagePathLocal)
{
@@ -401,16 +361,19 @@
addInfoFromLoadCommands((mach_header*)imageAddress, imageAddress, 32*1024);
}
else {
- __block kern_return_t kr = KERN_SUCCESS;
- withRemoteObject(task, imageAddress, &kr, ^(mach_header_64 mhBuffer) {
- size_t headerPagesSize = sizeof(mach_header_64) + mhBuffer.sizeofcmds;
- withRemoteBuffer(task, imageAddress, headerPagesSize, false, &kr, ^(void * buffer) {
- addInfoFromLoadCommands((mach_header*)buffer, imageAddress, headerPagesSize);
- });
- });
- if (kr != KERN_SUCCESS) {
- return kr;
- }
+ mach_vm_size_t readSize = sizeof(mach_header_64);
+ mach_header_64 mhBuffer;
+ if ( kern_return_t r = mach_vm_read_overwrite(task, imageAddress, sizeof(mach_header_64), (vm_address_t)&mhBuffer, &readSize) ) {
+ return r;
+ }
+ size_t headerPagesSize = (sizeof(mach_header_64) + mhBuffer.sizeofcmds + 4095) & (-4096);
+ vm_address_t localCopyBuffer;
+ unsigned int localCopyBufferSize;
+ if ( kern_return_t r = mach_vm_read(task, imageAddress, headerPagesSize, &localCopyBuffer, &localCopyBufferSize) ) {
+ return r;
+ }
+ addInfoFromLoadCommands((mach_header*)localCopyBuffer, imageAddress, localCopyBufferSize);
+ vm_deallocate(mach_task_self(), localCopyBuffer, localCopyBufferSize);
}
_curImage->segmentsCount = _curSegmentIndex - _curImage->segmentStartIndex;
_curImage++;
@@ -420,7 +383,7 @@
kern_return_t dyld_process_info_base::addDyldImage(task_t task, uint64_t dyldAddress, uint64_t dyldPathAddress, const char* localPath)
{
- __block kern_return_t kr = KERN_SUCCESS;
+ kern_return_t kr;
_curImage->loadAddress = dyldAddress;
_curImage->segmentStartIndex = _curSegmentIndex;
if ( localPath != NULL ) {
@@ -432,16 +395,19 @@
return kr;
}
- withRemoteObject(task, dyldAddress, &kr, ^(mach_header_64 mhBuffer) {
- size_t headerPagesSize = sizeof(mach_header_64) + mhBuffer.sizeofcmds;
- withRemoteBuffer(task, dyldAddress, headerPagesSize, false, &kr, ^(void * buffer) {
- addInfoFromLoadCommands((mach_header*)buffer, dyldAddress, headerPagesSize);
- });
- });
- if (kr != KERN_SUCCESS) {
- return kr;
- }
-
+ mach_vm_size_t readSize = sizeof(mach_header_64);
+ mach_header_64 mhBuffer;
+ if ( kern_return_t r = mach_vm_read_overwrite(task, dyldAddress, sizeof(mach_header_64), (vm_address_t)&mhBuffer, &readSize) ) {
+ return r;
+ }
+ size_t headerPagesSize = (sizeof(mach_header_64) + mhBuffer.sizeofcmds + 4095) & (-4096);
+ vm_address_t localCopyBuffer;
+ unsigned int localCopyBufferSize;
+ if ( kern_return_t r = mach_vm_read(task, dyldAddress, headerPagesSize, &localCopyBuffer, &localCopyBufferSize) ) {
+ return r;
+ }
+ addInfoFromLoadCommands((mach_header*)localCopyBuffer, dyldAddress, localCopyBufferSize);
+ vm_deallocate(mach_task_self(), localCopyBuffer, localCopyBufferSize);
_curImage->segmentsCount = _curSegmentIndex - _curImage->segmentStartIndex;
_curImage++;
return KERN_SUCCESS;
@@ -457,7 +423,7 @@
startCmds = (load_command*)((char *)mh + sizeof(mach_header));
else
return; // not a mach-o file, or wrong endianness
-
+
const load_command* const cmdsEnd = (load_command*)((char*)startCmds + mh->sizeofcmds);
const load_command* cmd = startCmds;
for(uint32_t i = 0; i < mh->ncmds; ++i) {
@@ -534,7 +500,7 @@
// Implementation that works with existing dyld data structures
-static dyld_process_info _dyld_process_info_create_inner(task_t task, uint64_t timestamp, kern_return_t* kr)
+dyld_process_info _dyld_process_info_create(task_t task, uint64_t timestamp, kern_return_t* kr)
{
if ( kr != NULL )
*kr = KERN_SUCCESS;
@@ -622,16 +588,10 @@
imageCount = MIN(imageCount, 8192);
// read image array
- if ( allImageInfo64.infoArray == 0 ) {
- // dyld is in middle of updating image list, try again
- return NULL;
- }
dyld_image_info_64 imageArray64[imageCount];
if ( kern_return_t r = mach_vm_read_overwrite(task, allImageInfo64.infoArray, imageArraySize, (vm_address_t)&imageArray64, &readSize) ) {
- // if image array moved, try whole thing again
- if ( kr != NULL ) {
+ if ( kr != NULL )
*kr = r;
- }
return NULL;
}
// normalize by expanding 32-bit image_infos into 64-bit ones
@@ -647,46 +607,9 @@
}
// create object based on local copy of all image infos and image array
- dyld_process_info result = dyld_process_info_base::make(task, allImageInfo64, imageArray64, kr);
-
- // verify nothing has changed by re-reading all_image_infos struct and checking timestamp
- if ( result != NULL ) {
- dyld_all_image_infos_64 allImageInfo64again;
- readSize = task_dyld_info.all_image_info_size;
- if ( kern_return_t r = mach_vm_read_overwrite(task, task_dyld_info.all_image_info_addr, task_dyld_info.all_image_info_size, (vm_address_t)&allImageInfo64again, &readSize) ) {
- if ( kr != NULL )
- *kr = r;
- free((void*)result);
- return NULL;
- }
- uint64_t doneTimeStamp = allImageInfo64again.infoArrayChangeTimestamp;
- if ( task_dyld_info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32 ) {
- const dyld_all_image_infos_32* allImageInfo32 = (dyld_all_image_infos_32*)&allImageInfo64again;
- doneTimeStamp = allImageInfo32->infoArrayChangeTimestamp;
- }
- if ( allImageInfo64.infoArrayChangeTimestamp != doneTimeStamp ) {
- // image list has changed since we started reading it
- // throw out what we have and start over
- free((void*)result);
- result = nullptr;
- }
- }
-
- return result;
-}
-
-
-dyld_process_info _dyld_process_info_create(task_t task, uint64_t timestamp, kern_return_t* kr)
-{
- // Other process may be loading and unloading as we read its memory, which can cause a read failure
- // <rdar://problem30067343&29567679> Retry if something fails
- for (int i=0; i < 100; ++i) {
- if ( dyld_process_info result = _dyld_process_info_create_inner( task, timestamp, kr) )
- return result;
-
- }
- return NULL;
-}
+ return dyld_process_info_base::make(task, allImageInfo64, imageArray64, kr);
+}
+
void _dyld_process_info_get_state(dyld_process_info info, dyld_process_state_info* stateInfo)
{