FLEXHeapEnumerator.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // FLEXHeapEnumerator.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/28/14.
  6. // Copyright (c) 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXHeapEnumerator.h"
  9. #import <malloc/malloc.h>
  10. #import <mach/mach.h>
  11. #import <objc/runtime.h>
  12. static CFMutableSetRef registeredClasses;
  13. // Mimics the objective-c object structure for checking if a range of memory is an object.
  14. typedef struct {
  15. Class isa;
  16. } flex_maybe_object_t;
  17. @implementation FLEXHeapEnumerator
  18. static void range_callback(task_t task, void *context, unsigned type, vm_range_t *ranges, unsigned rangeCount) {
  19. if (!context) {
  20. return;
  21. }
  22. for (unsigned int i = 0; i < rangeCount; i++) {
  23. vm_range_t range = ranges[i];
  24. flex_maybe_object_t *tryObject = (flex_maybe_object_t *)range.address;
  25. Class tryClass = NULL;
  26. #ifdef __arm64__
  27. // See http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
  28. extern uint64_t objc_debug_isa_class_mask WEAK_IMPORT_ATTRIBUTE;
  29. tryClass = (__bridge Class)((void *)((uint64_t)tryObject->isa & objc_debug_isa_class_mask));
  30. #else
  31. tryClass = tryObject->isa;
  32. #endif
  33. // If the class pointer matches one in our set of class pointers from the runtime, then we should have an object.
  34. if (CFSetContainsValue(registeredClasses, (__bridge const void *)(tryClass))) {
  35. (*(flex_object_enumeration_block_t __unsafe_unretained *)context)((__bridge id)tryObject, tryClass);
  36. }
  37. }
  38. }
  39. static kern_return_t reader(__unused task_t remote_task, vm_address_t remote_address, __unused vm_size_t size, void **local_memory) {
  40. *local_memory = (void *)remote_address;
  41. return KERN_SUCCESS;
  42. }
  43. + (void)enumerateLiveObjectsUsingBlock:(flex_object_enumeration_block_t)block {
  44. if (!block) {
  45. return;
  46. }
  47. // Refresh the class list on every call in case classes are added to the runtime.
  48. [self updateRegisteredClasses];
  49. // Inspired by:
  50. // https://llvm.org/svn/llvm-project/lldb/tags/RELEASE_34/final/examples/darwin/heap_find/heap/heap_find.cpp
  51. // https://gist.github.com/samdmarshall/17f4e66b5e2e579fd396
  52. vm_address_t *zones = NULL;
  53. unsigned int zoneCount = 0;
  54. kern_return_t result = malloc_get_all_zones(TASK_NULL, reader, &zones, &zoneCount);
  55. if (result == KERN_SUCCESS) {
  56. for (unsigned int i = 0; i < zoneCount; i++) {
  57. malloc_zone_t *zone = (malloc_zone_t *)zones[i];
  58. malloc_introspection_t *introspection = zone->introspect;
  59. // This may explain why some zone functions are
  60. // sometimes invalid; perhaps not all zones support them?
  61. if (!introspection) {
  62. continue;
  63. }
  64. void (*lock_zone)(malloc_zone_t *zone) = introspection->force_lock;
  65. void (*unlock_zone)(malloc_zone_t *zone) = introspection->force_unlock;
  66. // Callback has to unlock the zone so we freely allocate memory inside the given block
  67. flex_object_enumeration_block_t callback = ^(__unsafe_unretained id object, __unsafe_unretained Class actualClass) {
  68. unlock_zone(zone);
  69. block(object, actualClass);
  70. lock_zone(zone);
  71. };
  72. // The largest realistic memory address varies by platform.
  73. // Only 48 bits are used by 64 bit machines while
  74. // 32 bit machines use all bits.
  75. //
  76. // __LP64__ is defined as 1 for both arm64 and x86_64
  77. // via: clang -dM -arch [arm64|x86_64] -E -x c /dev/null | grep LP
  78. #if __LP64__
  79. static uintptr_t MAX_REALISTIC_ADDRESS = 0x0000FFFFFFFFFFFF;
  80. BOOL lockZoneValid = lock_zone != nil && (uintptr_t)lock_zone < MAX_REALISTIC_ADDRESS;
  81. BOOL unlockZoneValid = unlock_zone != nil && (uintptr_t)unlock_zone < MAX_REALISTIC_ADDRESS;
  82. #else
  83. BOOL lockZoneValid = lock_zone != nil;
  84. BOOL unlockZoneValid = unlock_zone != nil;
  85. #endif
  86. // There is little documentation on when and why
  87. // any of these function pointers might be NULL
  88. // or garbage, so we resort to checking for NULL
  89. // and impossible memory addresses at least
  90. if (introspection->enumerator && lockZoneValid && unlockZoneValid) {
  91. lock_zone(zone);
  92. introspection->enumerator(TASK_NULL, (void *)&callback, MALLOC_PTR_IN_USE_RANGE_TYPE, (vm_address_t)zone, reader, &range_callback);
  93. unlock_zone(zone);
  94. }
  95. }
  96. }
  97. }
  98. + (void)updateRegisteredClasses {
  99. if (!registeredClasses) {
  100. registeredClasses = CFSetCreateMutable(NULL, 0, NULL);
  101. } else {
  102. CFSetRemoveAllValues(registeredClasses);
  103. }
  104. unsigned int count = 0;
  105. Class *classes = objc_copyClassList(&count);
  106. for (unsigned int i = 0; i < count; i++) {
  107. CFSetAddValue(registeredClasses, (__bridge const void *)(classes[i]));
  108. }
  109. free(classes);
  110. }
  111. @end