FLEXRuntimeClient.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //
  2. // FLEXRuntimeClient.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/22/17.
  6. // Copyright © 2017 Tanner Bennett. All rights reserved.
  7. //
  8. #import "FLEXRuntimeClient.h"
  9. #import "NSObject+FLEX_Reflection.h"
  10. #import "FLEXMethod.h"
  11. #import "NSArray+FLEX.h"
  12. #import "FLEXRuntimeSafety.h"
  13. #include <dlfcn.h>
  14. #define Equals(a, b) ([a compare:b options:NSCaseInsensitiveSearch] == NSOrderedSame)
  15. #define Contains(a, b) ([a rangeOfString:b options:NSCaseInsensitiveSearch].location != NSNotFound)
  16. #define HasPrefix(a, b) ([a rangeOfString:b options:NSCaseInsensitiveSearch].location == 0)
  17. #define HasSuffix(a, b) ([a rangeOfString:b options:NSCaseInsensitiveSearch].location == (a.length - b.length))
  18. @interface FLEXRuntimeClient () {
  19. NSMutableArray<NSString *> *_imageDisplayNames;
  20. }
  21. @property (nonatomic) NSMutableDictionary *bundles_pathToShort;
  22. @property (nonatomic) NSMutableDictionary *bundles_shortToPath;
  23. @property (nonatomic) NSCache *bundles_pathToClassNames;
  24. @property (nonatomic) NSMutableArray<NSString *> *imagePaths;
  25. @end
  26. /// @return success if the map passes.
  27. static inline NSString * TBWildcardMap_(NSString *token, NSString *candidate, NSString *success, TBWildcardOptions options) {
  28. switch (options) {
  29. case TBWildcardOptionsNone:
  30. // Only "if equals"
  31. if (Equals(candidate, token)) {
  32. return success;
  33. }
  34. default: {
  35. // Only "if contains"
  36. if (options & TBWildcardOptionsPrefix &&
  37. options & TBWildcardOptionsSuffix) {
  38. if (Contains(candidate, token)) {
  39. return success;
  40. }
  41. }
  42. // Only "if candidate ends with with token"
  43. else if (options & TBWildcardOptionsPrefix) {
  44. if (HasSuffix(candidate, token)) {
  45. return success;
  46. }
  47. }
  48. // Only "if candidate starts with with token"
  49. else if (options & TBWildcardOptionsSuffix) {
  50. // Case like "Bundle." where we want "" to match anything
  51. if (!token.length) {
  52. return success;
  53. }
  54. if (HasPrefix(candidate, token)) {
  55. return success;
  56. }
  57. }
  58. }
  59. }
  60. return nil;
  61. }
  62. /// @return candidate if the map passes.
  63. static inline NSString * TBWildcardMap(NSString *token, NSString *candidate, TBWildcardOptions options) {
  64. return TBWildcardMap_(token, candidate, candidate, options);
  65. }
  66. @implementation FLEXRuntimeClient
  67. #pragma mark - Initialization
  68. + (instancetype)runtime {
  69. static FLEXRuntimeClient *runtime;
  70. static dispatch_once_t onceToken;
  71. dispatch_once(&onceToken, ^{
  72. runtime = [self new];
  73. [runtime reloadLibrariesList];
  74. });
  75. return runtime;
  76. }
  77. - (id)init {
  78. self = [super init];
  79. if (self) {
  80. _imagePaths = [NSMutableArray new];
  81. _bundles_pathToShort = [NSMutableDictionary new];
  82. _bundles_shortToPath = [NSMutableDictionary new];
  83. _bundles_pathToClassNames = [NSCache new];
  84. }
  85. return self;
  86. }
  87. #pragma mark - Private
  88. - (void)reloadLibrariesList {
  89. unsigned int imageCount = 0;
  90. const char **imageNames = objc_copyImageNames(&imageCount);
  91. if (imageNames) {
  92. NSMutableArray *imageNameStrings = [NSMutableArray flex_forEachUpTo:imageCount map:^NSString *(NSUInteger i) {
  93. return @(imageNames[i]);
  94. }];
  95. self.imagePaths = imageNameStrings;
  96. free(imageNames);
  97. // Sort alphabetically
  98. [imageNameStrings sortUsingComparator:^NSComparisonResult(NSString *name1, NSString *name2) {
  99. NSString *shortName1 = [self shortNameForImageName:name1];
  100. NSString *shortName2 = [self shortNameForImageName:name2];
  101. return [shortName1 caseInsensitiveCompare:shortName2];
  102. }];
  103. // Cache image display names
  104. _imageDisplayNames = [imageNameStrings flex_mapped:^id(NSString *path, NSUInteger idx) {
  105. return [self shortNameForImageName:path];
  106. }];
  107. }
  108. }
  109. - (NSString *)shortNameForImageName:(NSString *)imageName {
  110. // Cache
  111. NSString *shortName = _bundles_pathToShort[imageName];
  112. if (shortName) {
  113. return shortName;
  114. }
  115. NSArray *components = [imageName componentsSeparatedByString:@"/"];
  116. if (components.count >= 2) {
  117. NSString *parentDir = components[components.count - 2];
  118. if ([parentDir hasSuffix:@".framework"] || [parentDir hasSuffix:@".axbundle"]) {
  119. if ([imageName hasSuffix:@".dylib"]) {
  120. shortName = imageName.lastPathComponent;
  121. } else {
  122. shortName = parentDir;
  123. }
  124. }
  125. }
  126. if (!shortName) {
  127. shortName = imageName.lastPathComponent;
  128. }
  129. _bundles_pathToShort[imageName] = shortName;
  130. _bundles_shortToPath[shortName] = imageName;
  131. return shortName;
  132. }
  133. - (NSString *)imageNameForShortName:(NSString *)imageName {
  134. return _bundles_shortToPath[imageName];
  135. }
  136. - (NSMutableArray<NSString *> *)classNamesInImageAtPath:(NSString *)path {
  137. // Check cache
  138. NSMutableArray *classNameStrings = [_bundles_pathToClassNames objectForKey:path];
  139. if (classNameStrings) {
  140. return classNameStrings.mutableCopy;
  141. }
  142. unsigned int classCount = 0;
  143. const char **classNames = objc_copyClassNamesForImage(path.UTF8String, &classCount);
  144. if (classNames) {
  145. classNameStrings = [NSMutableArray flex_forEachUpTo:classCount map:^id(NSUInteger i) {
  146. return @(classNames[i]);
  147. }];
  148. free(classNames);
  149. [classNameStrings sortUsingSelector:@selector(caseInsensitiveCompare:)];
  150. [_bundles_pathToClassNames setObject:classNameStrings forKey:path];
  151. return classNameStrings.mutableCopy;
  152. }
  153. return [NSMutableArray new];
  154. }
  155. #pragma mark - Public
  156. + (void)initializeWebKitLegacy {
  157. static dispatch_once_t onceToken;
  158. dispatch_once(&onceToken, ^{
  159. void *handle = dlopen(
  160. "/System/Library/PrivateFrameworks/WebKitLegacy.framework/WebKitLegacy",
  161. RTLD_LAZY
  162. );
  163. void (*WebKitInitialize)() = dlsym(handle, "WebKitInitialize");
  164. if (WebKitInitialize) {
  165. NSAssert(NSThread.isMainThread,
  166. @"WebKitInitialize can only be called on the main thread"
  167. );
  168. WebKitInitialize();
  169. }
  170. });
  171. }
  172. - (NSArray<Class> *)copySafeClassList {
  173. unsigned int count = 0;
  174. Class *classes = objc_copyClassList(&count);
  175. return [NSArray flex_forEachUpTo:count map:^id(NSUInteger i) {
  176. Class cls = classes[i];
  177. return FLEXClassIsSafe(cls) ? cls : nil;
  178. }];
  179. }
  180. - (NSArray<Protocol *> *)copyProtocolList {
  181. unsigned int count = 0;
  182. Protocol *__unsafe_unretained *protocols = objc_copyProtocolList(&count);
  183. return [NSArray arrayWithObjects:protocols count:count];
  184. }
  185. - (NSMutableArray<NSString *> *)bundleNamesForToken:(FLEXSearchToken *)token {
  186. if (self.imagePaths.count) {
  187. TBWildcardOptions options = token.options;
  188. NSString *query = token.string;
  189. // Optimization, avoid a loop
  190. if (options == TBWildcardOptionsAny) {
  191. return _imageDisplayNames;
  192. }
  193. // No dot syntax because imageDisplayNames is only mutable internally
  194. return [_imageDisplayNames flex_mapped:^id(NSString *binary, NSUInteger idx) {
  195. // NSString *UIName = [self shortNameForImageName:binary];
  196. return TBWildcardMap(query, binary, options);
  197. }];
  198. }
  199. return [NSMutableArray new];
  200. }
  201. - (NSMutableArray<NSString *> *)bundlePathsForToken:(FLEXSearchToken *)token {
  202. if (self.imagePaths.count) {
  203. TBWildcardOptions options = token.options;
  204. NSString *query = token.string;
  205. // Optimization, avoid a loop
  206. if (options == TBWildcardOptionsAny) {
  207. return self.imagePaths;
  208. }
  209. return [self.imagePaths flex_mapped:^id(NSString *binary, NSUInteger idx) {
  210. NSString *UIName = [self shortNameForImageName:binary];
  211. // If query == UIName, -> binary
  212. return TBWildcardMap_(query, UIName, binary, options);
  213. }];
  214. }
  215. return [NSMutableArray new];
  216. }
  217. - (NSMutableArray<NSString *> *)classesForToken:(FLEXSearchToken *)token inBundles:(NSMutableArray<NSString *> *)bundles {
  218. // Edge case where token is the class we want already; return superclasses
  219. if (token.isAbsolute) {
  220. if (FLEXClassIsSafe(NSClassFromString(token.string))) {
  221. return [NSMutableArray arrayWithObject:token.string];
  222. }
  223. return [NSMutableArray new];
  224. }
  225. if (bundles.count) {
  226. // Get class names, remove unsafe classes
  227. NSMutableArray<NSString *> *names = [self _classesForToken:token inBundles:bundles];
  228. return [names flex_mapped:^NSString *(NSString *name, NSUInteger idx) {
  229. Class cls = NSClassFromString(name);
  230. BOOL safe = FLEXClassIsSafe(cls);
  231. return safe ? name : nil;
  232. }];
  233. }
  234. return [NSMutableArray new];
  235. }
  236. - (NSMutableArray<NSString *> *)_classesForToken:(FLEXSearchToken *)token inBundles:(NSMutableArray<NSString *> *)bundles {
  237. TBWildcardOptions options = token.options;
  238. NSString *query = token.string;
  239. // Optimization, avoid unnecessary sorting
  240. if (bundles.count == 1) {
  241. // Optimization, avoid a loop
  242. if (options == TBWildcardOptionsAny) {
  243. return [self classNamesInImageAtPath:bundles.firstObject];
  244. }
  245. return [[self classNamesInImageAtPath:bundles.firstObject] flex_mapped:^id(NSString *className, NSUInteger idx) {
  246. return TBWildcardMap(query, className, options);
  247. }];
  248. }
  249. else {
  250. // Optimization, avoid a loop
  251. if (options == TBWildcardOptionsAny) {
  252. return [[bundles flex_flatmapped:^NSArray *(NSString *bundlePath, NSUInteger idx) {
  253. return [self classNamesInImageAtPath:bundlePath];
  254. }] flex_sortedUsingSelector:@selector(caseInsensitiveCompare:)];
  255. }
  256. return [[bundles flex_flatmapped:^NSArray *(NSString *bundlePath, NSUInteger idx) {
  257. return [[self classNamesInImageAtPath:bundlePath] flex_mapped:^id(NSString *className, NSUInteger idx) {
  258. return TBWildcardMap(query, className, options);
  259. }];
  260. }] flex_sortedUsingSelector:@selector(caseInsensitiveCompare:)];
  261. }
  262. }
  263. - (NSArray<NSMutableArray<FLEXMethod *> *> *)methodsForToken:(FLEXSearchToken *)token
  264. instance:(NSNumber *)checkInstance
  265. inClasses:(NSArray<NSString *> *)classes {
  266. if (classes.count) {
  267. TBWildcardOptions options = token.options;
  268. BOOL instance = checkInstance.boolValue;
  269. NSString *selector = token.string;
  270. switch (options) {
  271. // In practice I don't think this case is ever used with methods,
  272. // since they will always have a suffix wildcard at the end
  273. case TBWildcardOptionsNone: {
  274. SEL sel = (SEL)selector.UTF8String;
  275. return @[[classes flex_mapped:^id(NSString *name, NSUInteger idx) {
  276. Class cls = NSClassFromString(name);
  277. // Use metaclass if not instance
  278. if (!instance) {
  279. cls = object_getClass(cls);
  280. }
  281. // Method is absolute
  282. return [FLEXMethod selector:sel class:cls];
  283. }]];
  284. }
  285. case TBWildcardOptionsAny: {
  286. return [classes flex_mapped:^NSArray *(NSString *name, NSUInteger idx) {
  287. // Any means `instance` was not specified
  288. Class cls = NSClassFromString(name);
  289. return [cls flex_allMethods];
  290. }];
  291. }
  292. default: {
  293. // Only "if contains"
  294. if (options & TBWildcardOptionsPrefix &&
  295. options & TBWildcardOptionsSuffix) {
  296. return [classes flex_mapped:^NSArray *(NSString *name, NSUInteger idx) {
  297. Class cls = NSClassFromString(name);
  298. return [[cls flex_allMethods] flex_mapped:^id(FLEXMethod *method, NSUInteger idx) {
  299. // Method is a prefix-suffix wildcard
  300. if (Contains(method.selectorString, selector)) {
  301. return method;
  302. }
  303. return nil;
  304. }];
  305. }];
  306. }
  307. // Only "if method ends with with selector"
  308. else if (options & TBWildcardOptionsPrefix) {
  309. return [classes flex_mapped:^NSArray *(NSString *name, NSUInteger idx) {
  310. Class cls = NSClassFromString(name);
  311. return [[cls flex_allMethods] flex_mapped:^id(FLEXMethod *method, NSUInteger idx) {
  312. // Method is a prefix wildcard
  313. if (HasSuffix(method.selectorString, selector)) {
  314. return method;
  315. }
  316. return nil;
  317. }];
  318. }];
  319. }
  320. // Only "if method starts with with selector"
  321. else if (options & TBWildcardOptionsSuffix) {
  322. assert(checkInstance);
  323. return [classes flex_mapped:^NSArray *(NSString *name, NSUInteger idx) {
  324. Class cls = NSClassFromString(name);
  325. // Case like "Bundle.class.-" where we want "-" to match anything
  326. if (!selector.length) {
  327. if (instance) {
  328. return [cls flex_allInstanceMethods];
  329. } else {
  330. return [cls flex_allClassMethods];
  331. }
  332. }
  333. id mapping = ^id(FLEXMethod *method) {
  334. // Method is a suffix wildcard
  335. if (HasPrefix(method.selectorString, selector)) {
  336. return method;
  337. }
  338. return nil;
  339. };
  340. if (instance) {
  341. return [[cls flex_allInstanceMethods] flex_mapped:mapping];
  342. } else {
  343. return [[cls flex_allClassMethods] flex_mapped:mapping];
  344. }
  345. }];
  346. }
  347. }
  348. }
  349. }
  350. return [NSMutableArray new];
  351. }
  352. @end