FLEXRuntimeSafety.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // FLEXRuntimeSafety.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/25/17.
  6. //
  7. #import "FLEXRuntimeSafety.h"
  8. NSUInteger const kFLEXKnownUnsafeClassCount = 19;
  9. Class * _UnsafeClasses = NULL;
  10. CFSetRef FLEXKnownUnsafeClasses = nil;
  11. CFSetRef FLEXKnownUnsafeIvars = nil;
  12. #define FLEXClassPointerOrCFNull(name) \
  13. (NSClassFromString(name) ?: (__bridge id)kCFNull)
  14. #define FLEXIvarOrCFNull(cls, name) \
  15. (class_getInstanceVariable([cls class], name) ?: (void *)kCFNull)
  16. __attribute__((constructor))
  17. static void FLEXRuntimeSafteyInit() {
  18. FLEXKnownUnsafeClasses = CFSetCreate(
  19. kCFAllocatorDefault,
  20. (const void **)(uintptr_t)FLEXKnownUnsafeClassList(),
  21. kFLEXKnownUnsafeClassCount,
  22. nil
  23. );
  24. Ivar unsafeIvars[] = {
  25. FLEXIvarOrCFNull(NSURL, "_urlString"),
  26. FLEXIvarOrCFNull(NSURL, "_baseURL"),
  27. };
  28. FLEXKnownUnsafeIvars = CFSetCreate(
  29. kCFAllocatorDefault,
  30. (const void **)unsafeIvars,
  31. sizeof(unsafeIvars),
  32. nil
  33. );
  34. }
  35. const Class * FLEXKnownUnsafeClassList() {
  36. if (!_UnsafeClasses) {
  37. const Class ignored[] = {
  38. FLEXClassPointerOrCFNull(@"__ARCLite__"),
  39. FLEXClassPointerOrCFNull(@"__NSCFCalendar"),
  40. FLEXClassPointerOrCFNull(@"__NSCFTimer"),
  41. FLEXClassPointerOrCFNull(@"NSCFTimer"),
  42. FLEXClassPointerOrCFNull(@"__NSGenericDeallocHandler"),
  43. FLEXClassPointerOrCFNull(@"NSAutoreleasePool"),
  44. FLEXClassPointerOrCFNull(@"NSPlaceholderNumber"),
  45. FLEXClassPointerOrCFNull(@"NSPlaceholderString"),
  46. FLEXClassPointerOrCFNull(@"NSPlaceholderValue"),
  47. FLEXClassPointerOrCFNull(@"Object"),
  48. FLEXClassPointerOrCFNull(@"VMUArchitecture"),
  49. FLEXClassPointerOrCFNull(@"JSExport"),
  50. FLEXClassPointerOrCFNull(@"__NSAtom"),
  51. FLEXClassPointerOrCFNull(@"_NSZombie_"),
  52. FLEXClassPointerOrCFNull(@"_CNZombie_"),
  53. FLEXClassPointerOrCFNull(@"__NSMessage"),
  54. FLEXClassPointerOrCFNull(@"__NSMessageBuilder"),
  55. FLEXClassPointerOrCFNull(@"FigIrisAutoTrimmerMotionSampleExport"),
  56. // Temporary until we have our own type encoding parser;
  57. // setVectors: has an invalid type encoding and crashes NSMethodSignature
  58. FLEXClassPointerOrCFNull(@"_UIPointVector"),
  59. };
  60. assert((sizeof(ignored) / sizeof(Class)) == kFLEXKnownUnsafeClassCount);
  61. _UnsafeClasses = (Class *)malloc(sizeof(ignored));
  62. memcpy(_UnsafeClasses, ignored, sizeof(ignored));
  63. }
  64. return _UnsafeClasses;
  65. }
  66. NSSet * FLEXKnownUnsafeClassNames() {
  67. static NSSet *set = nil;
  68. if (!set) {
  69. NSArray *ignored = @[
  70. @"__ARCLite__",
  71. @"__NSCFCalendar",
  72. @"__NSCFTimer",
  73. @"NSCFTimer",
  74. @"__NSGenericDeallocHandler",
  75. @"NSAutoreleasePool",
  76. @"NSPlaceholderNumber",
  77. @"NSPlaceholderString",
  78. @"NSPlaceholderValue",
  79. @"Object",
  80. @"VMUArchitecture",
  81. @"JSExport",
  82. @"__NSAtom",
  83. @"_NSZombie_",
  84. @"_CNZombie_",
  85. @"__NSMessage",
  86. @"__NSMessageBuilder",
  87. @"FigIrisAutoTrimmerMotionSampleExport",
  88. @"_UIPointVector",
  89. ];
  90. set = [NSSet setWithArray:ignored];
  91. assert(set.count == kFLEXKnownUnsafeClassCount);
  92. }
  93. return set;
  94. }