FLEXHeapEnumerator.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // FLEXHeapEnumerator.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/28/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXHeapEnumerator.h"
  9. #import "FLEXObjcInternal.h"
  10. #import <malloc/malloc.h>
  11. #import <mach/mach.h>
  12. #import <objc/runtime.h>
  13. static CFMutableSetRef registeredClasses;
  14. // Mimics the objective-c object structure for checking if a range of memory is an object.
  15. typedef struct {
  16. Class isa;
  17. } flex_maybe_object_t;
  18. @implementation FLEXHeapEnumerator
  19. static void range_callback(task_t task, void *context, unsigned type, vm_range_t *ranges, unsigned rangeCount) {
  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. *local_memory = (void *)remote_address;
  42. return KERN_SUCCESS;
  43. }
  44. + (void)enumerateLiveObjectsUsingBlock:(flex_object_enumeration_block_t)block {
  45. if (!block) {
  46. return;
  47. }
  48. // Refresh the class list on every call in case classes are added to the runtime.
  49. [self updateRegisteredClasses];
  50. // Inspired by:
  51. // https://llvm.org/svn/llvm-project/lldb/tags/RELEASE_34/final/examples/darwin/heap_find/heap/heap_find.cpp
  52. // https://gist.github.com/samdmarshall/17f4e66b5e2e579fd396
  53. vm_address_t *zones = NULL;
  54. unsigned int zoneCount = 0;
  55. kern_return_t result = malloc_get_all_zones(TASK_NULL, reader, &zones, &zoneCount);
  56. if (result == KERN_SUCCESS) {
  57. for (unsigned int i = 0; i < zoneCount; i++) {
  58. malloc_zone_t *zone = (malloc_zone_t *)zones[i];
  59. malloc_introspection_t *introspection = zone->introspect;
  60. // This may explain why some zone functions are
  61. // sometimes invalid; perhaps not all zones support them?
  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. BOOL lockZoneValid = FLEXPointerIsReadable(lock_zone);
  74. BOOL unlockZoneValid = FLEXPointerIsReadable(unlock_zone);
  75. // There is little documentation on when and why
  76. // any of these function pointers might be NULL
  77. // or garbage, so we resort to checking for NULL
  78. // and whether the pointer is readable
  79. if (introspection->enumerator && lockZoneValid && unlockZoneValid) {
  80. lock_zone(zone);
  81. introspection->enumerator(TASK_NULL, (void *)&callback, MALLOC_PTR_IN_USE_RANGE_TYPE, (vm_address_t)zone, reader, &range_callback);
  82. unlock_zone(zone);
  83. }
  84. }
  85. }
  86. }
  87. + (void)updateRegisteredClasses {
  88. if (!registeredClasses) {
  89. registeredClasses = CFSetCreateMutable(NULL, 0, NULL);
  90. } else {
  91. CFSetRemoveAllValues(registeredClasses);
  92. }
  93. unsigned int count = 0;
  94. Class *classes = objc_copyClassList(&count);
  95. for (unsigned int i = 0; i < count; i++) {
  96. CFSetAddValue(registeredClasses, (__bridge const void *)(classes[i]));
  97. }
  98. free(classes);
  99. }
  100. @end