objc-block-trampolines.mm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (c) 2010 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. * objc-block-trampolines.m
  25. * Author: b.bum
  26. *
  27. **********************************************************************/
  28. /***********************************************************************
  29. * Imports.
  30. **********************************************************************/
  31. #include "objc-private.h"
  32. #include "runtime.h"
  33. #include <Block.h>
  34. #include <Block_private.h>
  35. #include <mach/mach.h>
  36. #include <objc/objc-block-trampolines.h>
  37. // fixme C++ compilers don't implemement memory_order_consume efficiently.
  38. // Use memory_order_relaxed and cross our fingers.
  39. #define MEMORY_ORDER_CONSUME std::memory_order_relaxed
  40. // 8 bytes of text and data per trampoline on all architectures.
  41. #define SLOT_SIZE 8
  42. // The trampolines are defined in assembly files in libobjc-trampolines.dylib.
  43. // We can't link to libobjc-trampolines.dylib directly because
  44. // for security reasons it isn't in the dyld shared cache.
  45. // Trampoline addresses are lazily looked up.
  46. // All of them are hidden behind a single atomic pointer for lock-free init.
  47. #ifdef __PTRAUTH_INTRINSICS__
  48. # define TrampolinePtrauth __ptrauth(ptrauth_key_function_pointer, 1, 0x3af1)
  49. #else
  50. # define TrampolinePtrauth
  51. #endif
  52. class TrampolinePointerWrapper {
  53. struct TrampolinePointers {
  54. class TrampolineAddress {
  55. const void * TrampolinePtrauth storage;
  56. public:
  57. TrampolineAddress(void *dylib, const char *name) {
  58. #define PREFIX "_objc_blockTrampoline"
  59. char symbol[strlen(PREFIX) + strlen(name) + 1];
  60. strcpy(symbol, PREFIX);
  61. strcat(symbol, name);
  62. // dlsym() from a text segment returns a signed pointer
  63. // Authenticate it manually and let the compiler re-sign it.
  64. storage = ptrauth_auth_data(dlsym(dylib, symbol),
  65. ptrauth_key_function_pointer, 0);
  66. if (!storage) {
  67. _objc_fatal("couldn't dlsym %s", symbol);
  68. }
  69. }
  70. uintptr_t address() {
  71. return (uintptr_t)(void*)storage;
  72. }
  73. };
  74. TrampolineAddress impl; // trampoline header code
  75. TrampolineAddress start; // first trampoline
  76. #if DEBUG
  77. // These symbols are only used in assertions.
  78. // fixme might be able to move the assertions to libobjc-trampolines itself
  79. TrampolineAddress last; // start of the last trampoline
  80. // We don't use the address after the last trampoline because that
  81. // address might be in a different section, and then dlsym() would not
  82. // sign it as a function pointer.
  83. # if SUPPORT_STRET
  84. TrampolineAddress impl_stret;
  85. TrampolineAddress start_stret;
  86. TrampolineAddress last_stret;
  87. # endif
  88. #endif
  89. uintptr_t textSegment;
  90. uintptr_t textSegmentSize;
  91. void check() {
  92. #if DEBUG
  93. assert(impl.address() == textSegment + PAGE_MAX_SIZE);
  94. assert(impl.address() % PAGE_SIZE == 0); // not PAGE_MAX_SIZE
  95. assert(impl.address() + PAGE_MAX_SIZE ==
  96. last.address() + SLOT_SIZE);
  97. assert(last.address()+8 < textSegment + textSegmentSize);
  98. assert((last.address() - start.address()) % SLOT_SIZE == 0);
  99. # if SUPPORT_STRET
  100. assert(impl_stret.address() == textSegment + 2*PAGE_MAX_SIZE);
  101. assert(impl_stret.address() % PAGE_SIZE == 0); // not PAGE_MAX_SIZE
  102. assert(impl_stret.address() + PAGE_MAX_SIZE ==
  103. last_stret.address() + SLOT_SIZE);
  104. assert(start.address() - impl.address() ==
  105. start_stret.address() - impl_stret.address());
  106. assert(last_stret.address() + SLOT_SIZE <
  107. textSegment + textSegmentSize);
  108. assert((last_stret.address() - start_stret.address())
  109. % SLOT_SIZE == 0);
  110. # endif
  111. #endif
  112. }
  113. TrampolinePointers(void *dylib)
  114. : impl(dylib, "Impl")
  115. , start(dylib, "Start")
  116. #if DEBUG
  117. , last(dylib, "Last")
  118. # if SUPPORT_STRET
  119. , impl_stret(dylib, "Impl_stret")
  120. , start_stret(dylib, "Start_stret")
  121. , last_stret(dylib, "Last_stret")
  122. # endif
  123. #endif
  124. {
  125. const auto *mh =
  126. dyld_image_header_containing_address((void *)impl.address());
  127. unsigned long size = 0;
  128. textSegment = (uintptr_t)
  129. getsegmentdata((headerType *)mh, "__TEXT", &size);
  130. textSegmentSize = size;
  131. check();
  132. }
  133. };
  134. std::atomic<TrampolinePointers *> trampolines{nil};
  135. TrampolinePointers *get() {
  136. return trampolines.load(MEMORY_ORDER_CONSUME);
  137. }
  138. public:
  139. void Initialize() {
  140. if (get()) return;
  141. // This code may be called concurrently.
  142. // In the worst case we perform extra dyld operations.
  143. void *dylib = dlopen("/usr/lib/libobjc-trampolines.dylib",
  144. RTLD_NOW | RTLD_LOCAL | RTLD_FIRST);
  145. if (!dylib) {
  146. _objc_fatal("couldn't dlopen libobjc-trampolines.dylib");
  147. }
  148. auto t = new TrampolinePointers(dylib);
  149. TrampolinePointers *old = nil;
  150. if (! trampolines.compare_exchange_strong(old, t, memory_order_release))
  151. {
  152. delete t; // Lost an initialization race.
  153. }
  154. }
  155. uintptr_t textSegment() { return get()->textSegment; }
  156. uintptr_t textSegmentSize() { return get()->textSegmentSize; }
  157. uintptr_t impl() { return get()->impl.address(); }
  158. uintptr_t start() { return get()->start.address(); }
  159. };
  160. static TrampolinePointerWrapper Trampolines;
  161. // argument mode identifier
  162. typedef enum {
  163. ReturnValueInRegisterArgumentMode,
  164. #if SUPPORT_STRET
  165. ReturnValueOnStackArgumentMode,
  166. #endif
  167. ArgumentModeCount
  168. } ArgumentMode;
  169. // We must take care with our data layout on architectures that support
  170. // multiple page sizes.
  171. //
  172. // The trampoline template in __TEXT is sized and aligned with PAGE_MAX_SIZE.
  173. // On some platforms this requires additional linker flags.
  174. //
  175. // When we allocate a page group, we use PAGE_MAX_SIZE size.
  176. // This allows trampoline code to find its data by subtracting PAGE_MAX_SIZE.
  177. //
  178. // When we allocate a page group, we use the process's page alignment.
  179. // This simplifies allocation because we don't need to force greater than
  180. // default alignment when running with small pages, but it also means
  181. // the trampoline code MUST NOT look for its data by masking with PAGE_MAX_MASK.
  182. struct TrampolineBlockPageGroup
  183. {
  184. TrampolineBlockPageGroup *nextPageGroup; // linked list of all pages
  185. TrampolineBlockPageGroup *nextAvailablePage; // linked list of pages with available slots
  186. uintptr_t nextAvailable; // index of next available slot, endIndex() if no more available
  187. // Payload data: block pointers and free list.
  188. // Bytes parallel with trampoline header code are the fields above or unused
  189. // uint8_t payloads[PAGE_MAX_SIZE - sizeof(TrampolineBlockPageGroup)]
  190. // Code: Mach-O header, then trampoline header followed by trampolines.
  191. // On platforms with struct return we have non-stret trampolines and
  192. // stret trampolines. The stret and non-stret trampolines at a given
  193. // index share the same data page.
  194. // uint8_t macho[PAGE_MAX_SIZE];
  195. // uint8_t trampolines[ArgumentModeCount][PAGE_MAX_SIZE];
  196. // Per-trampoline block data format:
  197. // initial value is 0 while page data is filled sequentially
  198. // when filled, value is reference to Block_copy()d block
  199. // when empty, value is index of next available slot OR 0 if never used yet
  200. union Payload {
  201. id block;
  202. uintptr_t nextAvailable; // free list
  203. };
  204. static uintptr_t headerSize() {
  205. return (uintptr_t) (Trampolines.start() - Trampolines.impl());
  206. }
  207. static uintptr_t slotSize() {
  208. return SLOT_SIZE;
  209. }
  210. static uintptr_t startIndex() {
  211. // headerSize is assumed to be slot-aligned
  212. return headerSize() / slotSize();
  213. }
  214. static uintptr_t endIndex() {
  215. return (uintptr_t)PAGE_MAX_SIZE / slotSize();
  216. }
  217. static bool validIndex(uintptr_t index) {
  218. return (index >= startIndex() && index < endIndex());
  219. }
  220. Payload *payload(uintptr_t index) {
  221. assert(validIndex(index));
  222. return (Payload *)((char *)this + index*slotSize());
  223. }
  224. uintptr_t trampolinesForMode(int aMode) {
  225. // Skip over data page and Mach-O page.
  226. return (uintptr_t)this + PAGE_MAX_SIZE * (2 + aMode);
  227. }
  228. IMP trampoline(int aMode, uintptr_t index) {
  229. assert(validIndex(index));
  230. char *base = (char *)trampolinesForMode(aMode);
  231. char *imp = base + index*slotSize();
  232. #if __arm__
  233. imp++; // trampoline is Thumb instructions
  234. #endif
  235. #if __has_feature(ptrauth_calls)
  236. imp = ptrauth_sign_unauthenticated(imp,
  237. ptrauth_key_function_pointer, 0);
  238. #endif
  239. return (IMP)imp;
  240. }
  241. uintptr_t indexForTrampoline(uintptr_t tramp) {
  242. for (int aMode = 0; aMode < ArgumentModeCount; aMode++) {
  243. uintptr_t base = trampolinesForMode(aMode);
  244. uintptr_t start = base + startIndex() * slotSize();
  245. uintptr_t end = base + endIndex() * slotSize();
  246. if (tramp >= start && tramp < end) {
  247. return (uintptr_t)(tramp - base) / slotSize();
  248. }
  249. }
  250. return 0;
  251. }
  252. static void check() {
  253. assert(TrampolineBlockPageGroup::headerSize() >= sizeof(TrampolineBlockPageGroup));
  254. assert(TrampolineBlockPageGroup::headerSize() % TrampolineBlockPageGroup::slotSize() == 0);
  255. }
  256. };
  257. static TrampolineBlockPageGroup *HeadPageGroup;
  258. #pragma mark Utility Functions
  259. #if !__OBJC2__
  260. #define runtimeLock classLock
  261. #endif
  262. #pragma mark Trampoline Management Functions
  263. static TrampolineBlockPageGroup *_allocateTrampolinesAndData()
  264. {
  265. runtimeLock.assertLocked();
  266. vm_address_t dataAddress;
  267. TrampolineBlockPageGroup::check();
  268. // Our final mapping will look roughly like this:
  269. // r/w data
  270. // r/o text mapped from libobjc-trampolines.dylib
  271. // with fixed offsets from the text to the data embedded in the text.
  272. //
  273. // More precisely it will look like this:
  274. // 1 page r/w data
  275. // 1 page libobjc-trampolines.dylib Mach-O header
  276. // N pages trampoline code, one for each ArgumentMode
  277. // M pages for the rest of libobjc-trampolines' TEXT segment.
  278. // The kernel requires that we remap the entire TEXT segment every time.
  279. // We assume that our code begins on the second TEXT page, but are robust
  280. // against other additions to the end of the TEXT segment.
  281. assert(HeadPageGroup == nil || HeadPageGroup->nextAvailablePage == nil);
  282. auto textSource = Trampolines.textSegment();
  283. auto textSourceSize = Trampolines.textSegmentSize();
  284. auto dataSize = PAGE_MAX_SIZE;
  285. // Allocate a single contiguous region big enough to hold data+text.
  286. kern_return_t result;
  287. result = vm_allocate(mach_task_self(), &dataAddress,
  288. dataSize + textSourceSize,
  289. VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_FOUNDATION));
  290. if (result != KERN_SUCCESS) {
  291. _objc_fatal("vm_allocate trampolines failed (%d)", result);
  292. }
  293. // Remap libobjc-trampolines' TEXT segment atop all
  294. // but the first of the pages we just allocated:
  295. vm_address_t textDest = dataAddress + dataSize;
  296. vm_prot_t currentProtection, maxProtection;
  297. result = vm_remap(mach_task_self(), &textDest,
  298. textSourceSize,
  299. 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
  300. mach_task_self(), textSource, TRUE,
  301. &currentProtection, &maxProtection, VM_INHERIT_SHARE);
  302. if (result != KERN_SUCCESS) {
  303. _objc_fatal("vm_remap trampolines failed (%d)", result);
  304. }
  305. TrampolineBlockPageGroup *pageGroup = (TrampolineBlockPageGroup *) dataAddress;
  306. pageGroup->nextAvailable = pageGroup->startIndex();
  307. pageGroup->nextPageGroup = nil;
  308. pageGroup->nextAvailablePage = nil;
  309. if (HeadPageGroup) {
  310. TrampolineBlockPageGroup *lastPageGroup = HeadPageGroup;
  311. while(lastPageGroup->nextPageGroup) {
  312. lastPageGroup = lastPageGroup->nextPageGroup;
  313. }
  314. lastPageGroup->nextPageGroup = pageGroup;
  315. HeadPageGroup->nextAvailablePage = pageGroup;
  316. } else {
  317. HeadPageGroup = pageGroup;
  318. }
  319. return pageGroup;
  320. }
  321. static TrampolineBlockPageGroup *
  322. getOrAllocatePageGroupWithNextAvailable()
  323. {
  324. runtimeLock.assertLocked();
  325. if (!HeadPageGroup)
  326. return _allocateTrampolinesAndData();
  327. // make sure head page is filled first
  328. if (HeadPageGroup->nextAvailable != HeadPageGroup->endIndex())
  329. return HeadPageGroup;
  330. if (HeadPageGroup->nextAvailablePage) // check if there is a page w/a hole
  331. return HeadPageGroup->nextAvailablePage;
  332. return _allocateTrampolinesAndData(); // tack on a new one
  333. }
  334. static TrampolineBlockPageGroup *
  335. pageAndIndexContainingIMP(IMP anImp, uintptr_t *outIndex)
  336. {
  337. runtimeLock.assertLocked();
  338. // Authenticate as a function pointer, returning an un-signed address.
  339. uintptr_t trampAddress =
  340. (uintptr_t)ptrauth_auth_data((const char *)anImp,
  341. ptrauth_key_function_pointer, 0);
  342. for (TrampolineBlockPageGroup *pageGroup = HeadPageGroup;
  343. pageGroup;
  344. pageGroup = pageGroup->nextPageGroup)
  345. {
  346. uintptr_t index = pageGroup->indexForTrampoline(trampAddress);
  347. if (index) {
  348. if (outIndex) *outIndex = index;
  349. return pageGroup;
  350. }
  351. }
  352. return nil;
  353. }
  354. static ArgumentMode
  355. argumentModeForBlock(id block)
  356. {
  357. ArgumentMode aMode = ReturnValueInRegisterArgumentMode;
  358. #if SUPPORT_STRET
  359. if (_Block_has_signature(block) && _Block_use_stret(block))
  360. aMode = ReturnValueOnStackArgumentMode;
  361. #else
  362. assert(! (_Block_has_signature(block) && _Block_use_stret(block)));
  363. #endif
  364. return aMode;
  365. }
  366. // `block` must already have been copied
  367. IMP
  368. _imp_implementationWithBlockNoCopy(id block)
  369. {
  370. runtimeLock.assertLocked();
  371. TrampolineBlockPageGroup *pageGroup =
  372. getOrAllocatePageGroupWithNextAvailable();
  373. uintptr_t index = pageGroup->nextAvailable;
  374. assert(index >= pageGroup->startIndex() && index < pageGroup->endIndex());
  375. TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index);
  376. uintptr_t nextAvailableIndex = payload->nextAvailable;
  377. if (nextAvailableIndex == 0) {
  378. // First time through (unused slots are zero). Fill sequentially.
  379. // If the page is now full this will now be endIndex(), handled below.
  380. nextAvailableIndex = index + 1;
  381. }
  382. pageGroup->nextAvailable = nextAvailableIndex;
  383. if (nextAvailableIndex == pageGroup->endIndex()) {
  384. // PageGroup is now full (free list or wilderness exhausted)
  385. // Remove from available page linked list
  386. TrampolineBlockPageGroup *iterator = HeadPageGroup;
  387. while(iterator && (iterator->nextAvailablePage != pageGroup)) {
  388. iterator = iterator->nextAvailablePage;
  389. }
  390. if (iterator) {
  391. iterator->nextAvailablePage = pageGroup->nextAvailablePage;
  392. pageGroup->nextAvailablePage = nil;
  393. }
  394. }
  395. payload->block = block;
  396. return pageGroup->trampoline(argumentModeForBlock(block), index);
  397. }
  398. #pragma mark Public API
  399. IMP imp_implementationWithBlock(id block)
  400. {
  401. // Block object must be copied outside runtimeLock
  402. // because it performs arbitrary work.
  403. block = Block_copy(block);
  404. // Trampolines must be initialized outside runtimeLock
  405. // because it calls dlopen().
  406. Trampolines.Initialize();
  407. mutex_locker_t lock(runtimeLock);
  408. return _imp_implementationWithBlockNoCopy(block);
  409. }
  410. id imp_getBlock(IMP anImp) {
  411. uintptr_t index;
  412. TrampolineBlockPageGroup *pageGroup;
  413. if (!anImp) return nil;
  414. mutex_locker_t lock(runtimeLock);
  415. pageGroup = pageAndIndexContainingIMP(anImp, &index);
  416. if (!pageGroup) {
  417. return nil;
  418. }
  419. TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index);
  420. if (payload->nextAvailable <= TrampolineBlockPageGroup::endIndex()) {
  421. // unallocated
  422. return nil;
  423. }
  424. return payload->block;
  425. }
  426. BOOL imp_removeBlock(IMP anImp) {
  427. if (!anImp) return NO;
  428. id block;
  429. {
  430. mutex_locker_t lock(runtimeLock);
  431. uintptr_t index;
  432. TrampolineBlockPageGroup *pageGroup =
  433. pageAndIndexContainingIMP(anImp, &index);
  434. if (!pageGroup) {
  435. return NO;
  436. }
  437. TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index);
  438. block = payload->block;
  439. // block is released below, outside the lock
  440. payload->nextAvailable = pageGroup->nextAvailable;
  441. pageGroup->nextAvailable = index;
  442. // make sure this page is on available linked list
  443. TrampolineBlockPageGroup *pageGroupIterator = HeadPageGroup;
  444. // see if page is the next available page for any existing pages
  445. while (pageGroupIterator->nextAvailablePage &&
  446. pageGroupIterator->nextAvailablePage != pageGroup)
  447. {
  448. pageGroupIterator = pageGroupIterator->nextAvailablePage;
  449. }
  450. if (! pageGroupIterator->nextAvailablePage) {
  451. // if iteration stopped because nextAvail was nil
  452. // add to end of list.
  453. pageGroupIterator->nextAvailablePage = pageGroup;
  454. pageGroup->nextAvailablePage = nil;
  455. }
  456. }
  457. // do this AFTER dropping the lock
  458. Block_release(block);
  459. return YES;
  460. }