FLEXHeapEnumerator.m 4.5 KB

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