FLEXRuntimeSafety.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // FLEXRuntimeSafety.h
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/25/17.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import <objc/runtime.h>
  9. #pragma mark - Classes
  10. extern NSUInteger const kFLEXKnownUnsafeClassCount;
  11. extern const Class * FLEXKnownUnsafeClassList(void);
  12. extern NSSet * FLEXKnownUnsafeClassNames(void);
  13. extern CFSetRef FLEXKnownUnsafeClasses;
  14. static Class cNSObject = nil, cNSProxy = nil;
  15. __attribute__((constructor))
  16. static void FLEXInitKnownRootClasses() {
  17. cNSObject = [NSObject class];
  18. cNSProxy = [NSProxy class];
  19. }
  20. static inline BOOL FLEXClassIsSafe(Class cls) {
  21. // Is it nil or known to be unsafe?
  22. if (!cls || CFSetContainsValue(FLEXKnownUnsafeClasses, (__bridge void *)cls)) {
  23. return NO;
  24. }
  25. // Is it a known root class?
  26. if (!class_getSuperclass(cls)) {
  27. return cls == cNSObject || cls == cNSProxy;
  28. }
  29. // Probably safe
  30. return YES;
  31. }
  32. static inline BOOL FLEXClassNameIsSafe(NSString *cls) {
  33. if (!cls) return NO;
  34. NSSet *ignored = FLEXKnownUnsafeClassNames();
  35. return ![ignored containsObject:cls];
  36. }
  37. #pragma mark - Ivars
  38. extern CFSetRef FLEXKnownUnsafeIvars;
  39. static inline BOOL FLEXIvarIsSafe(Ivar ivar) {
  40. if (!ivar) return NO;
  41. return !CFSetContainsValue(FLEXKnownUnsafeIvars, ivar);
  42. }