123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- #include "objc-private.h"
- #include "runtime.h"
- #include <Block.h>
- #include <Block_private.h>
- #include <mach/mach.h>
- #include <objc/objc-block-trampolines.h>
- #define MEMORY_ORDER_CONSUME std::memory_order_relaxed
- #define SLOT_SIZE 8
- #ifdef __PTRAUTH_INTRINSICS__
- # define TrampolinePtrauth __ptrauth(ptrauth_key_function_pointer, 1, 0x3af1)
- #else
- # define TrampolinePtrauth
- #endif
- class TrampolinePointerWrapper {
- struct TrampolinePointers {
- class TrampolineAddress {
- const void * TrampolinePtrauth storage;
- public:
- TrampolineAddress(void *dylib, const char *name) {
- #define PREFIX "_objc_blockTrampoline"
- char symbol[strlen(PREFIX) + strlen(name) + 1];
- strcpy(symbol, PREFIX);
- strcat(symbol, name);
-
-
- storage = ptrauth_auth_data(dlsym(dylib, symbol),
- ptrauth_key_function_pointer, 0);
- if (!storage) {
- _objc_fatal("couldn't dlsym %s", symbol);
- }
- }
- uintptr_t address() {
- return (uintptr_t)(void*)storage;
- }
- };
- TrampolineAddress impl;
- TrampolineAddress start;
- #if DEBUG
-
-
- TrampolineAddress last;
-
-
-
- # if SUPPORT_STRET
- TrampolineAddress impl_stret;
- TrampolineAddress start_stret;
- TrampolineAddress last_stret;
- # endif
- #endif
- uintptr_t textSegment;
- uintptr_t textSegmentSize;
- void check() {
- #if DEBUG
- ASSERT(impl.address() == textSegment + PAGE_MAX_SIZE);
- ASSERT(impl.address() % PAGE_SIZE == 0);
- assert(impl.address() + PAGE_MAX_SIZE ==
- last.address() + SLOT_SIZE);
- ASSERT(last.address()+8 < textSegment + textSegmentSize);
- ASSERT((last.address() - start.address()) % SLOT_SIZE == 0);
- # if SUPPORT_STRET
- ASSERT(impl_stret.address() == textSegment + 2*PAGE_MAX_SIZE);
- ASSERT(impl_stret.address() % PAGE_SIZE == 0);
- assert(impl_stret.address() + PAGE_MAX_SIZE ==
- last_stret.address() + SLOT_SIZE);
- assert(start.address() - impl.address() ==
- start_stret.address() - impl_stret.address());
- assert(last_stret.address() + SLOT_SIZE <
- textSegment + textSegmentSize);
- assert((last_stret.address() - start_stret.address())
- % SLOT_SIZE == 0);
- # endif
- #endif
- }
- TrampolinePointers(void *dylib)
- : impl(dylib, "Impl")
- , start(dylib, "Start")
- #if DEBUG
- , last(dylib, "Last")
- # if SUPPORT_STRET
- , impl_stret(dylib, "Impl_stret")
- , start_stret(dylib, "Start_stret")
- , last_stret(dylib, "Last_stret")
- # endif
- #endif
- {
- const auto *mh =
- dyld_image_header_containing_address((void *)impl.address());
- unsigned long size = 0;
- textSegment = (uintptr_t)
- getsegmentdata((headerType *)mh, "__TEXT", &size);
- textSegmentSize = size;
- check();
- }
- };
- std::atomic<TrampolinePointers *> trampolines{nil};
- TrampolinePointers *get() {
- return trampolines.load(MEMORY_ORDER_CONSUME);
- }
- public:
- void Initialize() {
- if (get()) return;
-
-
- void *dylib = dlopen("/usr/lib/libobjc-trampolines.dylib",
- RTLD_NOW | RTLD_LOCAL | RTLD_FIRST);
- if (!dylib) {
- _objc_fatal("couldn't dlopen libobjc-trampolines.dylib: %s",
- dlerror());
- }
- auto t = new TrampolinePointers(dylib);
- TrampolinePointers *old = nil;
- if (! trampolines.compare_exchange_strong(old, t, memory_order_release))
- {
- delete t;
- }
- }
- uintptr_t textSegment() { return get()->textSegment; }
- uintptr_t textSegmentSize() { return get()->textSegmentSize; }
-
- uintptr_t dataSize() { return PAGE_MAX_SIZE; }
- uintptr_t impl() { return get()->impl.address(); }
- uintptr_t start() { return get()->start.address(); }
- };
- static TrampolinePointerWrapper Trampolines;
- typedef enum {
- ReturnValueInRegisterArgumentMode,
- #if SUPPORT_STRET
- ReturnValueOnStackArgumentMode,
- #endif
-
- ArgumentModeCount
- } ArgumentMode;
- struct TrampolineBlockPageGroup
- {
- TrampolineBlockPageGroup *nextPageGroup;
- TrampolineBlockPageGroup *nextAvailablePage;
- uintptr_t nextAvailable;
- const void * TrampolinePtrauth const text;
- TrampolineBlockPageGroup()
- : nextPageGroup(nil)
- , nextAvailablePage(nil)
- , nextAvailable(startIndex())
- , text((const void *)((uintptr_t)this + Trampolines.dataSize()))
- { }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- union Payload {
- id block;
- uintptr_t nextAvailable;
- };
-
- static uintptr_t headerSize() {
- return (uintptr_t) (Trampolines.start() - Trampolines.impl());
- }
-
- static uintptr_t slotSize() {
- return SLOT_SIZE;
- }
- static uintptr_t startIndex() {
-
- return headerSize() / slotSize();
- }
- static uintptr_t endIndex() {
- return (uintptr_t)Trampolines.dataSize() / slotSize();
- }
- static bool validIndex(uintptr_t index) {
- return (index >= startIndex() && index < endIndex());
- }
- Payload *payload(uintptr_t index) {
- ASSERT(validIndex(index));
- return (Payload *)((char *)this + index*slotSize());
- }
- uintptr_t trampolinesForMode(int aMode) {
-
-
- return (uintptr_t)this + Trampolines.dataSize() +
- PAGE_MAX_SIZE * (1 + aMode);
- }
-
- IMP trampoline(int aMode, uintptr_t index) {
- ASSERT(validIndex(index));
- char *base = (char *)trampolinesForMode(aMode);
- char *imp = base + index*slotSize();
- #if __arm__
- imp++;
- #endif
- #if __has_feature(ptrauth_calls)
- imp = ptrauth_sign_unauthenticated(imp,
- ptrauth_key_function_pointer, 0);
- #endif
- return (IMP)imp;
- }
- uintptr_t indexForTrampoline(uintptr_t tramp) {
- for (int aMode = 0; aMode < ArgumentModeCount; aMode++) {
- uintptr_t base = trampolinesForMode(aMode);
- uintptr_t start = base + startIndex() * slotSize();
- uintptr_t end = base + endIndex() * slotSize();
- if (tramp >= start && tramp < end) {
- return (uintptr_t)(tramp - base) / slotSize();
- }
- }
- return 0;
- }
- static void check() {
- ASSERT(TrampolineBlockPageGroup::headerSize() >= sizeof(TrampolineBlockPageGroup));
- ASSERT(TrampolineBlockPageGroup::headerSize() % TrampolineBlockPageGroup::slotSize() == 0);
- }
- };
- static TrampolineBlockPageGroup *HeadPageGroup;
- #pragma mark Utility Functions
- #if !__OBJC2__
- #define runtimeLock classLock
- #endif
- #pragma mark Trampoline Management Functions
- static TrampolineBlockPageGroup *_allocateTrampolinesAndData()
- {
- runtimeLock.assertLocked();
- vm_address_t dataAddress;
-
- TrampolineBlockPageGroup::check();
-
-
-
-
-
-
-
-
-
-
-
-
-
- ASSERT(HeadPageGroup == nil || HeadPageGroup->nextAvailablePage == nil);
- auto textSource = Trampolines.textSegment();
- auto textSourceSize = Trampolines.textSegmentSize();
- auto dataSize = Trampolines.dataSize();
-
- kern_return_t result;
- result = vm_allocate(mach_task_self(), &dataAddress,
- dataSize + textSourceSize,
- VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_FOUNDATION));
- if (result != KERN_SUCCESS) {
- _objc_fatal("vm_allocate trampolines failed (%d)", result);
- }
-
-
- vm_address_t textDest = dataAddress + dataSize;
- vm_prot_t currentProtection, maxProtection;
- result = vm_remap(mach_task_self(), &textDest,
- textSourceSize,
- 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
- mach_task_self(), textSource, TRUE,
- ¤tProtection, &maxProtection, VM_INHERIT_SHARE);
- if (result != KERN_SUCCESS) {
- _objc_fatal("vm_remap trampolines failed (%d)", result);
- }
- auto *pageGroup = new ((void*)dataAddress) TrampolineBlockPageGroup;
-
- if (HeadPageGroup) {
- TrampolineBlockPageGroup *lastPageGroup = HeadPageGroup;
- while(lastPageGroup->nextPageGroup) {
- lastPageGroup = lastPageGroup->nextPageGroup;
- }
- lastPageGroup->nextPageGroup = pageGroup;
- HeadPageGroup->nextAvailablePage = pageGroup;
- } else {
- HeadPageGroup = pageGroup;
- }
-
- return pageGroup;
- }
- static TrampolineBlockPageGroup *
- getOrAllocatePageGroupWithNextAvailable()
- {
- runtimeLock.assertLocked();
-
- if (!HeadPageGroup)
- return _allocateTrampolinesAndData();
-
-
- if (HeadPageGroup->nextAvailable != HeadPageGroup->endIndex())
- return HeadPageGroup;
-
- if (HeadPageGroup->nextAvailablePage)
- return HeadPageGroup->nextAvailablePage;
-
- return _allocateTrampolinesAndData();
- }
- static TrampolineBlockPageGroup *
- pageAndIndexContainingIMP(IMP anImp, uintptr_t *outIndex)
- {
- runtimeLock.assertLocked();
-
- uintptr_t trampAddress =
- (uintptr_t)ptrauth_auth_data((const char *)anImp,
- ptrauth_key_function_pointer, 0);
- for (TrampolineBlockPageGroup *pageGroup = HeadPageGroup;
- pageGroup;
- pageGroup = pageGroup->nextPageGroup)
- {
- uintptr_t index = pageGroup->indexForTrampoline(trampAddress);
- if (index) {
- if (outIndex) *outIndex = index;
- return pageGroup;
- }
- }
-
- return nil;
- }
- static ArgumentMode
- argumentModeForBlock(id block)
- {
- ArgumentMode aMode = ReturnValueInRegisterArgumentMode;
- #if SUPPORT_STRET
- if (_Block_has_signature(block) && _Block_use_stret(block))
- aMode = ReturnValueOnStackArgumentMode;
- #else
- ASSERT(! (_Block_has_signature(block) && _Block_use_stret(block)));
- #endif
-
- return aMode;
- }
- void
- _imp_implementationWithBlock_init(void)
- {
- #if TARGET_OS_OSX
-
-
-
-
-
-
-
-
-
-
- if (__progname &&
- (strcmp(__progname, "QtWebEngineProcess") == 0 ||
- strcmp(__progname, "Steam Helper") == 0)) {
- Trampolines.Initialize();
- }
- #endif
- }
- IMP
- _imp_implementationWithBlockNoCopy(id block)
- {
- runtimeLock.assertLocked();
- TrampolineBlockPageGroup *pageGroup =
- getOrAllocatePageGroupWithNextAvailable();
- uintptr_t index = pageGroup->nextAvailable;
- ASSERT(index >= pageGroup->startIndex() && index < pageGroup->endIndex());
- TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index);
-
- uintptr_t nextAvailableIndex = payload->nextAvailable;
- if (nextAvailableIndex == 0) {
-
-
- nextAvailableIndex = index + 1;
- }
- pageGroup->nextAvailable = nextAvailableIndex;
- if (nextAvailableIndex == pageGroup->endIndex()) {
-
-
- TrampolineBlockPageGroup *iterator = HeadPageGroup;
- while(iterator && (iterator->nextAvailablePage != pageGroup)) {
- iterator = iterator->nextAvailablePage;
- }
- if (iterator) {
- iterator->nextAvailablePage = pageGroup->nextAvailablePage;
- pageGroup->nextAvailablePage = nil;
- }
- }
-
- payload->block = block;
- return pageGroup->trampoline(argumentModeForBlock(block), index);
- }
- #pragma mark Public API
- IMP imp_implementationWithBlock(id block)
- {
-
-
- block = Block_copy(block);
-
-
- Trampolines.Initialize();
-
- mutex_locker_t lock(runtimeLock);
- return _imp_implementationWithBlockNoCopy(block);
- }
- id imp_getBlock(IMP anImp) {
- uintptr_t index;
- TrampolineBlockPageGroup *pageGroup;
-
- if (!anImp) return nil;
-
- mutex_locker_t lock(runtimeLock);
-
- pageGroup = pageAndIndexContainingIMP(anImp, &index);
-
- if (!pageGroup) {
- return nil;
- }
- TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index);
-
- if (payload->nextAvailable <= TrampolineBlockPageGroup::endIndex()) {
-
- return nil;
- }
-
- return payload->block;
- }
- BOOL imp_removeBlock(IMP anImp) {
-
- if (!anImp) return NO;
- id block;
-
- {
- mutex_locker_t lock(runtimeLock);
-
- uintptr_t index;
- TrampolineBlockPageGroup *pageGroup =
- pageAndIndexContainingIMP(anImp, &index);
-
- if (!pageGroup) {
- return NO;
- }
-
- TrampolineBlockPageGroup::Payload *payload = pageGroup->payload(index);
- block = payload->block;
-
-
- payload->nextAvailable = pageGroup->nextAvailable;
- pageGroup->nextAvailable = index;
-
-
- TrampolineBlockPageGroup *pageGroupIterator = HeadPageGroup;
-
-
- while (pageGroupIterator->nextAvailablePage &&
- pageGroupIterator->nextAvailablePage != pageGroup)
- {
- pageGroupIterator = pageGroupIterator->nextAvailablePage;
- }
-
- if (! pageGroupIterator->nextAvailablePage) {
-
-
- pageGroupIterator->nextAvailablePage = pageGroup;
- pageGroup->nextAvailablePage = nil;
- }
- }
-
- Block_release(block);
- return YES;
- }
|