@@ -314,24 +314,28 @@ auto remote_load_library = [](std::size_t* instructions_size) -> std::uint8_t* {
314
314
auto find_library = [](mach_port_t task, const char * library) -> std::intptr_t {
315
315
task_dyld_info_data_t info;
316
316
mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
317
- task_info (task, TASK_DYLD_INFO, reinterpret_cast <task_info_t >(&info), &count);
317
+
318
+ kern_return_t err = task_info (task, TASK_DYLD_INFO, reinterpret_cast <task_info_t >(&info), &count);
319
+ if (err != KERN_SUCCESS)
320
+ {
321
+ // print mach_error_string(err);
322
+ return NULL ;
323
+ }
318
324
319
325
// Get the loaded dylibs/images
320
326
dyld_all_image_infos infos = {0 };
321
- mach_vm_size_t size = info.all_image_info_size ;
322
- kern_return_t err = mach_vm_read_overwrite (task, info.all_image_info_addr , info.all_image_info_size , reinterpret_cast <mach_vm_address_t >(&infos), &size);
323
-
324
- if (size <= 0 || err != KERN_SUCCESS)
327
+ mach_vm_size_t size = info.all_image_info_size ; // or sizeof(infos)
328
+ err = mach_vm_read_overwrite (task, info.all_image_info_addr , info.all_image_info_size , reinterpret_cast <mach_vm_address_t >(&infos), &size);
329
+ if (err != KERN_SUCCESS || size != info.all_image_info_size )
325
330
{
326
331
return NULL ;
327
332
}
328
333
329
334
// Get the info for each dylib/image
330
- size = sizeof (dyld_all_image_infos ) * infos.infoArrayCount ;
331
- std::unique_ptr<dyld_image_info[]> image_infos = std::make_unique<dyld_image_info[]>(size );
335
+ size = sizeof (dyld_image_info ) * infos.infoArrayCount ;
336
+ std::unique_ptr<dyld_image_info[]> image_infos = std::make_unique<dyld_image_info[]>(infos. infoArrayCount );
332
337
err = mach_vm_read_overwrite (task, reinterpret_cast <mach_vm_address_t >(infos.infoArray ), size, reinterpret_cast <mach_vm_address_t >(image_infos.get ()), &size);
333
-
334
- if (size <= 0 || err != KERN_SUCCESS)
338
+ if (err != KERN_SUCCESS || size != sizeof (dyld_image_info) * infos.infoArrayCount )
335
339
{
336
340
return NULL ;
337
341
}
@@ -342,11 +346,11 @@ auto find_library = [](mach_port_t task, const char* library) -> std::intptr_t {
342
346
char buffer[512 ] = {0 };
343
347
mach_vm_size_t size = sizeof (buffer);
344
348
345
- kern_return_t err = mach_vm_read_overwrite (task, reinterpret_cast <mach_vm_address_t >(image_infos[i].imageFilePath ), size, reinterpret_cast <mach_vm_address_t >(&buffer[0 ]), &size);
346
- if (err == KERN_SUCCESS && size > 0 )
349
+ mach_vm_size_t err = mach_vm_read_overwrite (task, reinterpret_cast <mach_vm_address_t >(image_infos[i].imageFilePath ), size, reinterpret_cast <mach_vm_address_t >(&buffer[0 ]), &size);
350
+ if (err == KERN_SUCCESS && size > 0 )
347
351
{
348
352
std::string path = strip_path (buffer);
349
- if (!strcasecmp (path.c_str (), library))
353
+ if (!strcasecmp (path.c_str (), library))
350
354
{
351
355
return reinterpret_cast <std::uintptr_t >(image_infos[i].imageLoadAddress );
352
356
}
0 commit comments