]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-opt.mm
objc4-787.1.tar.gz
[apple/objc4.git] / runtime / objc-opt.mm
1 /*
2 * Copyright (c) 2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 objc-opt.mm
26 Management of optimizations in the dyld shared cache
27 */
28
29 #include "objc-private.h"
30 #include "objc-os.h"
31 #include "objc-file.h"
32
33
34 #if !SUPPORT_PREOPT
35 // Preoptimization not supported on this platform.
36
37 bool isPreoptimized(void)
38 {
39 return false;
40 }
41
42 bool noMissingWeakSuperclasses(void)
43 {
44 return false;
45 }
46
47 bool header_info::isPreoptimized() const
48 {
49 return false;
50 }
51
52 bool header_info::hasPreoptimizedSelectors() const
53 {
54 return false;
55 }
56
57 bool header_info::hasPreoptimizedClasses() const
58 {
59 return false;
60 }
61
62 bool header_info::hasPreoptimizedProtocols() const
63 {
64 return false;
65 }
66
67 Protocol *getPreoptimizedProtocol(const char *name)
68 {
69 return nil;
70 }
71
72 unsigned int getPreoptimizedClassUnreasonableCount()
73 {
74 return 0;
75 }
76
77 Class getPreoptimizedClass(const char *name)
78 {
79 return nil;
80 }
81
82 Class* copyPreoptimizedClasses(const char *name, int *outCount)
83 {
84 *outCount = 0;
85 return nil;
86 }
87
88 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
89 {
90 return nil;
91 }
92
93 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
94 {
95 return nil;
96 }
97
98 void preopt_init(void)
99 {
100 disableSharedCacheOptimizations();
101
102 if (PrintPreopt) {
103 _objc_inform("PREOPTIMIZATION: is DISABLED "
104 "(not supported on ths platform)");
105 }
106 }
107
108
109 // !SUPPORT_PREOPT
110 #else
111 // SUPPORT_PREOPT
112
113 #include <objc-shared-cache.h>
114
115 using objc_opt::objc_stringhash_offset_t;
116 using objc_opt::objc_protocolopt2_t;
117 using objc_opt::objc_clsopt_t;
118 using objc_opt::objc_headeropt_ro_t;
119 using objc_opt::objc_headeropt_rw_t;
120 using objc_opt::objc_opt_t;
121
122 __BEGIN_DECLS
123
124 // preopt: the actual opt used at runtime (nil or &_objc_opt_data)
125 // _objc_opt_data: opt data possibly written by dyld
126 // opt is initialized to ~0 to detect incorrect use before preopt_init()
127
128 static const objc_opt_t *opt = (objc_opt_t *)~0;
129 static bool preoptimized;
130
131 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_opt_ro
132
133 namespace objc_opt {
134 struct objc_headeropt_ro_t {
135 uint32_t count;
136 uint32_t entsize;
137 header_info headers[0]; // sorted by mhdr address
138
139 header_info& getOrEnd(uint32_t i) const {
140 ASSERT(i <= count);
141 return *(header_info *)((uint8_t *)&headers + (i * entsize));
142 }
143
144 header_info& get(uint32_t i) const {
145 ASSERT(i < count);
146 return *(header_info *)((uint8_t *)&headers + (i * entsize));
147 }
148
149 uint32_t index(const header_info* hi) const {
150 const header_info* begin = &get(0);
151 const header_info* end = &getOrEnd(count);
152 ASSERT(hi >= begin && hi < end);
153 return (uint32_t)(((uintptr_t)hi - (uintptr_t)begin) / entsize);
154 }
155
156 header_info *get(const headerType *mhdr)
157 {
158 int32_t start = 0;
159 int32_t end = count;
160 while (start <= end) {
161 int32_t i = (start+end)/2;
162 header_info &hi = get(i);
163 if (mhdr == hi.mhdr()) return &hi;
164 else if (mhdr < hi.mhdr()) end = i-1;
165 else start = i+1;
166 }
167
168 #if DEBUG
169 for (uint32_t i = 0; i < count; i++) {
170 header_info &hi = get(i);
171 if (mhdr == hi.mhdr()) {
172 _objc_fatal("failed to find header %p (%d/%d)",
173 mhdr, i, count);
174 }
175 }
176 #endif
177
178 return nil;
179 }
180 };
181
182 struct objc_headeropt_rw_t {
183 uint32_t count;
184 uint32_t entsize;
185 header_info_rw headers[0]; // sorted by mhdr address
186 };
187 };
188
189 /***********************************************************************
190 * Return YES if we have a valid optimized shared cache.
191 **********************************************************************/
192 bool isPreoptimized(void)
193 {
194 return preoptimized;
195 }
196
197
198 /***********************************************************************
199 * Return YES if the shared cache does not have any classes with
200 * missing weak superclasses.
201 **********************************************************************/
202 bool noMissingWeakSuperclasses(void)
203 {
204 if (!preoptimized) return NO; // might have missing weak superclasses
205 return opt->flags & objc_opt::NoMissingWeakSuperclasses;
206 }
207
208
209 /***********************************************************************
210 * Return YES if this image's dyld shared cache optimizations are valid.
211 **********************************************************************/
212 bool header_info::isPreoptimized() const
213 {
214 // preoptimization disabled for some reason
215 if (!preoptimized) return NO;
216
217 // image not from shared cache, or not fixed inside shared cache
218 if (!info()->optimizedByDyld()) return NO;
219
220 return YES;
221 }
222
223 bool header_info::hasPreoptimizedSelectors() const
224 {
225 // preoptimization disabled for some reason
226 if (!preoptimized) return NO;
227
228 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
229 }
230
231 bool header_info::hasPreoptimizedClasses() const
232 {
233 // preoptimization disabled for some reason
234 if (!preoptimized) return NO;
235
236 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
237 }
238
239 bool header_info::hasPreoptimizedProtocols() const
240 {
241 // preoptimization disabled for some reason
242 if (!preoptimized) return NO;
243
244 return info()->optimizedByDyld() || info()->optimizedByDyldClosure();
245 }
246
247 bool header_info::hasPreoptimizedSectionLookups() const
248 {
249 objc_opt::objc_headeropt_ro_t *hinfoRO = opt->headeropt_ro();
250 if (hinfoRO->entsize == (2 * sizeof(intptr_t)))
251 return NO;
252
253 return YES;
254 }
255
256 const classref_t *header_info::nlclslist(size_t *outCount) const
257 {
258 #if __OBJC2__
259 // This field is new, so temporarily be resilient to the shared cache
260 // not generating it
261 if (isPreoptimized() && hasPreoptimizedSectionLookups()) {
262 *outCount = nlclslist_count;
263 const classref_t *list = (const classref_t *)(((intptr_t)&nlclslist_offset) + nlclslist_offset);
264 #if DEBUG
265 size_t debugCount;
266 assert((list == _getObjc2NonlazyClassList(mhdr(), &debugCount)) && (*outCount == debugCount));
267 #endif
268 return list;
269 }
270 return _getObjc2NonlazyClassList(mhdr(), outCount);
271 #else
272 return NULL;
273 #endif
274 }
275
276 category_t * const *header_info::nlcatlist(size_t *outCount) const
277 {
278 #if __OBJC2__
279 // This field is new, so temporarily be resilient to the shared cache
280 // not generating it
281 if (isPreoptimized() && hasPreoptimizedSectionLookups()) {
282 *outCount = nlcatlist_count;
283 category_t * const *list = (category_t * const *)(((intptr_t)&nlcatlist_offset) + nlcatlist_offset);
284 #if DEBUG
285 size_t debugCount;
286 assert((list == _getObjc2NonlazyCategoryList(mhdr(), &debugCount)) && (*outCount == debugCount));
287 #endif
288 return list;
289 }
290 return _getObjc2NonlazyCategoryList(mhdr(), outCount);
291 #else
292 return NULL;
293 #endif
294 }
295
296 category_t * const *header_info::catlist(size_t *outCount) const
297 {
298 #if __OBJC2__
299 // This field is new, so temporarily be resilient to the shared cache
300 // not generating it
301 if (isPreoptimized() && hasPreoptimizedSectionLookups()) {
302 *outCount = catlist_count;
303 category_t * const *list = (category_t * const *)(((intptr_t)&catlist_offset) + catlist_offset);
304 #if DEBUG
305 size_t debugCount;
306 assert((list == _getObjc2CategoryList(mhdr(), &debugCount)) && (*outCount == debugCount));
307 #endif
308 return list;
309 }
310 return _getObjc2CategoryList(mhdr(), outCount);
311 #else
312 return NULL;
313 #endif
314 }
315
316 category_t * const *header_info::catlist2(size_t *outCount) const
317 {
318 #if __OBJC2__
319 // This field is new, so temporarily be resilient to the shared cache
320 // not generating it
321 if (isPreoptimized() && hasPreoptimizedSectionLookups()) {
322 *outCount = catlist2_count;
323 category_t * const *list = (category_t * const *)(((intptr_t)&catlist2_offset) + catlist2_offset);
324 #if DEBUG
325 size_t debugCount;
326 assert((list == _getObjc2CategoryList2(mhdr(), &debugCount)) && (*outCount == debugCount));
327 #endif
328 return list;
329 }
330 return _getObjc2CategoryList2(mhdr(), outCount);
331 #else
332 return NULL;
333 #endif
334 }
335
336
337 Protocol *getSharedCachePreoptimizedProtocol(const char *name)
338 {
339 objc_protocolopt2_t *protocols = opt ? opt->protocolopt2() : nil;
340 if (!protocols) return nil;
341
342 // Note, we have to pass the lambda directly here as otherwise we would try
343 // message copy and autorelease.
344 return (Protocol *)protocols->getProtocol(name, [](const void* hi) -> bool {
345 return ((header_info *)hi)->isLoaded();
346 });
347 }
348
349
350 Protocol *getPreoptimizedProtocol(const char *name)
351 {
352 objc_protocolopt2_t *protocols = opt ? opt->protocolopt2() : nil;
353 if (!protocols) return nil;
354
355 // Try table from dyld closure first. It was built to ignore the dupes it
356 // knows will come from the cache, so anything left in here was there when
357 // we launched
358 Protocol *result = nil;
359 // Note, we have to pass the lambda directly here as otherwise we would try
360 // message copy and autorelease.
361 _dyld_for_each_objc_protocol(name, [&result](void* protocolPtr, bool isLoaded, bool* stop) {
362 // Skip images which aren't loaded. This supports the case where dyld
363 // might soft link an image from the main binary so its possibly not
364 // loaded yet.
365 if (!isLoaded)
366 return;
367
368 // Found a loaded image with this class name, so stop the search
369 result = (Protocol *)protocolPtr;
370 *stop = true;
371 });
372 if (result) return result;
373
374 return getSharedCachePreoptimizedProtocol(name);
375 }
376
377
378 unsigned int getPreoptimizedClassUnreasonableCount()
379 {
380 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
381 if (!classes) return 0;
382
383 // This is an overestimate: each set of duplicates
384 // gets double-counted in `capacity` as well.
385 return classes->capacity + classes->duplicateCount();
386 }
387
388
389 Class getPreoptimizedClass(const char *name)
390 {
391 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
392 if (!classes) return nil;
393
394 // Try table from dyld closure first. It was built to ignore the dupes it
395 // knows will come from the cache, so anything left in here was there when
396 // we launched
397 Class result = nil;
398 // Note, we have to pass the lambda directly here as otherwise we would try
399 // message copy and autorelease.
400 _dyld_for_each_objc_class(name, [&result](void* classPtr, bool isLoaded, bool* stop) {
401 // Skip images which aren't loaded. This supports the case where dyld
402 // might soft link an image from the main binary so its possibly not
403 // loaded yet.
404 if (!isLoaded)
405 return;
406
407 // Found a loaded image with this class name, so stop the search
408 result = (Class)classPtr;
409 *stop = true;
410 });
411 if (result) return result;
412
413 void *cls;
414 void *hi;
415 uint32_t count = classes->getClassAndHeader(name, cls, hi);
416 if (count == 1 && ((header_info *)hi)->isLoaded()) {
417 // exactly one matching class, and its image is loaded
418 return (Class)cls;
419 }
420 else if (count > 1) {
421 // more than one matching class - find one that is loaded
422 void *clslist[count];
423 void *hilist[count];
424 classes->getClassesAndHeaders(name, clslist, hilist);
425 for (uint32_t i = 0; i < count; i++) {
426 if (((header_info *)hilist[i])->isLoaded()) {
427 return (Class)clslist[i];
428 }
429 }
430 }
431
432 // no match that is loaded
433 return nil;
434 }
435
436
437 Class* copyPreoptimizedClasses(const char *name, int *outCount)
438 {
439 *outCount = 0;
440
441 objc_clsopt_t *classes = opt ? opt->clsopt() : nil;
442 if (!classes) return nil;
443
444 void *cls;
445 void *hi;
446 uint32_t count = classes->getClassAndHeader(name, cls, hi);
447 if (count == 0) return nil;
448
449 Class *result = (Class *)calloc(count, sizeof(Class));
450 if (count == 1 && ((header_info *)hi)->isLoaded()) {
451 // exactly one matching class, and its image is loaded
452 result[(*outCount)++] = (Class)cls;
453 return result;
454 }
455 else if (count > 1) {
456 // more than one matching class - find those that are loaded
457 void *clslist[count];
458 void *hilist[count];
459 classes->getClassesAndHeaders(name, clslist, hilist);
460 for (uint32_t i = 0; i < count; i++) {
461 if (((header_info *)hilist[i])->isLoaded()) {
462 result[(*outCount)++] = (Class)clslist[i];
463 }
464 }
465
466 if (*outCount == 0) {
467 // found multiple classes with that name, but none are loaded
468 free(result);
469 result = nil;
470 }
471 return result;
472 }
473
474 // no match that is loaded
475 return nil;
476 }
477
478
479 header_info *preoptimizedHinfoForHeader(const headerType *mhdr)
480 {
481 #if !__OBJC2__
482 // fixme old ABI shared cache doesn't prepare these properly
483 return nil;
484 #endif
485
486 objc_headeropt_ro_t *hinfos = opt ? opt->headeropt_ro() : nil;
487 if (hinfos) return hinfos->get(mhdr);
488 else return nil;
489 }
490
491
492 header_info_rw *getPreoptimizedHeaderRW(const struct header_info *const hdr)
493 {
494 #if !__OBJC2__
495 // fixme old ABI shared cache doesn't prepare these properly
496 return nil;
497 #endif
498
499 objc_headeropt_ro_t *hinfoRO = opt ? opt->headeropt_ro() : nil;
500 objc_headeropt_rw_t *hinfoRW = opt ? opt->headeropt_rw() : nil;
501 if (!hinfoRO || !hinfoRW) {
502 _objc_fatal("preoptimized header_info missing for %s (%p %p %p)",
503 hdr->fname(), hdr, hinfoRO, hinfoRW);
504 }
505 int32_t index = hinfoRO->index(hdr);
506 ASSERT(hinfoRW->entsize == sizeof(header_info_rw));
507 return &hinfoRW->headers[index];
508 }
509
510
511 void preopt_init(void)
512 {
513 // Get the memory region occupied by the shared cache.
514 size_t length;
515 const uintptr_t start = (uintptr_t)_dyld_get_shared_cache_range(&length);
516
517 if (start) {
518 objc::dataSegmentsRanges.add(start, start + length);
519 }
520
521 // `opt` not set at compile time in order to detect too-early usage
522 const char *failure = nil;
523 opt = &_objc_opt_data;
524
525 if (DisablePreopt) {
526 // OBJC_DISABLE_PREOPTIMIZATION is set
527 // If opt->version != VERSION then you continue at your own risk.
528 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
529 }
530 else if (opt->version != objc_opt::VERSION) {
531 // This shouldn't happen. You probably forgot to edit objc-sel-table.s.
532 // If dyld really did write the wrong optimization version,
533 // then we must halt because we don't know what bits dyld twiddled.
534 _objc_fatal("bad objc preopt version (want %d, got %d)",
535 objc_opt::VERSION, opt->version);
536 }
537 else if (!opt->selopt() || !opt->headeropt_ro()) {
538 // One of the tables is missing.
539 failure = "(dyld shared cache is absent or out of date)";
540 }
541
542 if (failure) {
543 // All preoptimized selector references are invalid.
544 preoptimized = NO;
545 opt = nil;
546 disableSharedCacheOptimizations();
547
548 if (PrintPreopt) {
549 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
550 }
551 }
552 else {
553 // Valid optimization data written by dyld shared cache
554 preoptimized = YES;
555
556 if (PrintPreopt) {
557 _objc_inform("PREOPTIMIZATION: is ENABLED "
558 "(version %d)", opt->version);
559 }
560 }
561 }
562
563
564 __END_DECLS
565
566 // SUPPORT_PREOPT
567 #endif