Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | /* * Copyright (c) 2019 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #ifndef _libSystem_h #define _libSystem_h #include <TargetConditionals.h> #include "Defines.h" //FIXME: Hack to avoid <sys/commpage.h> being included by <System/machine/cpu_capabilities.h> #if TARGET_OS_EXCLAVEKIT #include <threads.h> typedef tss_t dyld_thread_key_t; typedef mtx_t dyld_mutex; typedef mtx_t* dyld_mutex_t; typedef mtx_t dyld_recursive_mutex; typedef mtx_t* dyld_recursive_mutex_t; inline int dyld_thread_key_create(dyld_thread_key_t* key, void (*destructor)(void*)) { return ::tss_create(key, destructor); } inline int dyld_thread_key_init_np(dyld_thread_key_t key, void (*destructor)(void*)) { return 0; } inline int dyld_thread_setspecific(dyld_thread_key_t key, const void* value) { return ::tss_set(key, (void*)value); } inline void* dyld_thread_getspecific(dyld_thread_key_t key) { return ::tss_get(key); } typedef unsigned int vm_map_t; typedef uintptr_t vm_address_t; typedef uintptr_t vm_size_t; typedef uintptr_t vm_offset_t; typedef int vm_prot_t; typedef uint64_t user_addr_t; typedef int kern_return_t; typedef struct os_unfair_lock_options_s { uint32_t foo; } *os_unfair_lock_options_t; #define OS_UNFAIR_LOCK_NONE 0 #else #include <pthread.h> #include <pthread/tsd_private.h> #include <unistd.h> #include <malloc/malloc.h> #include <os/lock_private.h> #include <mach/mach.h> #include <mach/vm_types.h> typedef pthread_key_t dyld_thread_key_t; typedef os_unfair_recursive_lock dyld_recursive_mutex; typedef os_unfair_recursive_lock_t dyld_recursive_mutex_t; typedef os_unfair_lock dyld_mutex; typedef os_unfair_lock_t dyld_mutex_t; inline int dyld_thread_key_create(dyld_thread_key_t* key, void (*destructor)(void*)) { return ::pthread_key_create(key, destructor); } inline int dyld_thread_key_init_np(dyld_thread_key_t key, void (*destructor)(void*)) { return ::pthread_key_init_np((int)key, destructor); } inline int dyld_thread_setspecific(dyld_thread_key_t key, const void* value) { return ::pthread_setspecific(key, value); } inline void* dyld_thread_getspecific(dyld_thread_key_t key) { return ::pthread_getspecific(key); } #endif // !TARGET_OS_EXCLAVEKIT #include "DyldSharedCache.h" // mach_o #include "Error.h" #include "Header.h" using mach_o::Header; namespace dyld4 { struct ProgramVars; typedef bool (*FuncLookup)(const char* name, void** addr); // // Helper for performing "up calls" from dyld into libSystem.dylib. // // Note: driverkit and base OS use the same dyld, but different libdyld.dylibs. We use the clang attribute // to ensure both libdyld implementations use the same vtable pointer authentication. Similarly, we cannot use // the generic pthread_key_create() because it takes a clean function pointer parameter and the authentication // for that may differ in the two libdyld.dylibs. // struct VIS_HIDDEN [[clang::ptrauth_vtable_pointer(process_independent, address_discrimination, type_discrimination)]] LibSystemHelpers { typedef void (*ThreadExitFunc)(void* storage); virtual uintptr_t version() const; virtual void* malloc(size_t size) const; virtual void free(void* p) const; virtual size_t malloc_size(const void* p) const; virtual kern_return_t vm_allocate(vm_map_t target_task, vm_address_t* address, vm_size_t size, int flags) const; virtual kern_return_t vm_deallocate(vm_map_t target_task, vm_address_t address, vm_size_t size) const; virtual int pthread_key_create_free(dyld_thread_key_t* key) const; virtual void* pthread_getspecific(dyld_thread_key_t key) const; virtual int pthread_setspecific(dyld_thread_key_t key, const void* value) const; virtual void __cxa_atexit(void (*func)(void*), void* arg, void* dso) const; virtual void __cxa_finalize_ranges(const struct __cxa_range_t ranges[], unsigned int count) const; virtual bool isLaunchdOwned() const; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunguarded-availability-new" virtual void os_unfair_recursive_lock_lock_with_options(dyld_recursive_mutex_t lock, os_unfair_lock_options_t options) const; virtual void os_unfair_recursive_lock_unlock(dyld_recursive_mutex_t lock) const; #pragma clang diagnostic pop virtual void exit(int result) const __attribute__((__noreturn__)); virtual const char* getenv(const char* key) const; virtual int mkstemp(char* templatePath) const; // Added in version 2 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunguarded-availability-new" virtual void os_unfair_recursive_lock_unlock_forked_child(dyld_recursive_mutex_t lock) const; #pragma clang diagnostic pop // Added in version 3 virtual void setDyldPatchedObjCClasses() const; // Added in version 5 virtual void run_async(void* (*func)(void*), void* context) const; // Added in version 6 virtual void os_unfair_lock_lock_with_options(dyld_mutex_t lock, os_unfair_lock_options_t options) const; virtual void os_unfair_lock_unlock(dyld_mutex_t lock) const; // Added in version 7 virtual void setDefaultProgramVars(ProgramVars& vars) const; virtual FuncLookup legacyDyldFuncLookup() const; // only works on x86_64 macOS virtual mach_o::Error setUpThreadLocals(const DyldSharedCache* cache, const Header* hdr) const; }; // __DATA_CONST,__helper section in libdyld.dylib struct LibdyldHelperSection { const LibSystemHelpers helper; }; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunguarded-availability-new" // Wrapper for LibSystemHelpers to force it to take a read only context before calling methods struct LibSystemHelpersWrapper { LibSystemHelpersWrapper() = default; LibSystemHelpersWrapper(const LibSystemHelpersWrapper&) = default; LibSystemHelpersWrapper(LibSystemHelpersWrapper&&) = default; LibSystemHelpersWrapper& operator=(const LibSystemHelpersWrapper&) = default; LibSystemHelpersWrapper& operator=(LibSystemHelpersWrapper&&) = default; LibSystemHelpersWrapper(std::nullptr_t) : helpers(nullptr) { } LibSystemHelpersWrapper(const LibSystemHelpers* helpers, lsl::MemoryManager* memoryManager) : helpers(helpers), memoryManager(memoryManager) { } bool operator!=(std::nullptr_t) const { return helpers != nullptr; } bool operator==(std::nullptr_t) const { return helpers == nullptr; } // Provide access to some methods directly, without the use of read only memory // These are either safe by inspection/convention, or required due to current behaviour (see the locks below) uintptr_t version() const { return helpers->version(); } void setDefaultProgramVars(ProgramVars& vars) const { // This is writing in to TPRO_CONST in dyld, so needs to stay mutable this->helpers->setDefaultProgramVars(vars); } // These methods can't be called from a read-only context. // FIXME: Instead we should obsolete these and use their implementations in dyld itself void os_unfair_recursive_lock_lock_with_options(dyld_recursive_mutex_t lock, os_unfair_lock_options_t options) const { return helpers->os_unfair_recursive_lock_lock_with_options(lock, options); } void os_unfair_recursive_lock_unlock(dyld_recursive_mutex_t lock) const { return helpers->os_unfair_recursive_lock_unlock(lock); } void os_unfair_recursive_lock_unlock_forked_child(dyld_recursive_mutex_t lock) const { return helpers->os_unfair_recursive_lock_unlock_forked_child(lock); } void os_unfair_lock_lock_with_options(dyld_mutex_t lock, os_unfair_lock_options_t options) const { return helpers->os_unfair_lock_lock_with_options(lock, options); } void os_unfair_lock_unlock(dyld_mutex_t lock) const { return helpers->os_unfair_lock_unlock(lock); } // Normal helpers, all of which need to be read-only during calls void* malloc(size_t size) const { void* result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->malloc(size); }); return result; } void free(void* p) const { bool unused = this->memoryManager->withReadOnlyTPROMemory([&] { this->helpers->free(p); return false; }); (void)unused; } size_t malloc_size(const void* p) const { size_t result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->malloc_size(p); }); return result; } kern_return_t vm_allocate(vm_map_t target_task, vm_address_t* address, vm_size_t size, int flags) const { kern_return_t result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->vm_allocate(target_task, address, size, flags); }); return result; } kern_return_t vm_deallocate(vm_map_t target_task, vm_address_t address, vm_size_t size) const { kern_return_t result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->vm_deallocate(target_task, address, size); }); return result; } int pthread_key_create_free(dyld_thread_key_t* key) const { int result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->pthread_key_create_free(key); }); return result; } void* pthread_getspecific(dyld_thread_key_t key) const { void* result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->pthread_getspecific(key); }); return result; } int pthread_setspecific(dyld_thread_key_t key, const void* value) const { int result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->pthread_setspecific(key, value); }); return result; } void __cxa_atexit(void (*func)(void*), void* arg, void* dso) const { bool unused = false; unused = this->memoryManager->withReadOnlyTPROMemory([&] { this->helpers->__cxa_atexit(func, arg, dso); return false; }); } void __cxa_finalize_ranges(const struct __cxa_range_t ranges[], unsigned int count) const { bool unused = false; unused = this->memoryManager->withReadOnlyTPROMemory([&] { this->helpers->__cxa_finalize_ranges(ranges, count); return false; }); } bool isLaunchdOwned() const { bool result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->isLaunchdOwned(); }); return result; } void exit(int result) const __attribute__((__noreturn__)) { bool unused = false; unused = this->memoryManager->withReadOnlyTPROMemory([&] { this->helpers->exit(result); return false; }); (void)unused; __builtin_unreachable(); } const char* getenv(const char* key) const { const char* result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->getenv(key); }); return result; } int mkstemp(char* templatePath) const { int result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->mkstemp(templatePath); }); return result; } void setDyldPatchedObjCClasses() const { bool unused = false; unused = this->memoryManager->withReadOnlyTPROMemory([&] { this->helpers->setDyldPatchedObjCClasses(); return false; }); (void)unused; } void run_async(void* (*func)(void*), void* context) const { bool unused = false; unused = this->memoryManager->withReadOnlyTPROMemory([&] { this->helpers->run_async(func, context); return false; }); (void)unused; } FuncLookup legacyDyldFuncLookup() const { FuncLookup result = this->memoryManager->withReadOnlyTPROMemory([&] { return this->helpers->legacyDyldFuncLookup(); }); return result; } // Note, an error result here was strdup()ed and should be free()d mach_o::Error setUpThreadLocals(const DyldSharedCache* cache, const Header* hdr) const { char* result = this->memoryManager->withReadOnlyTPROMemory([&] { mach_o::Error error = this->helpers->setUpThreadLocals(cache, hdr); // withReadOnlyTPROMemory can't return a struct, so get the error out if we need it if ( error ) { size_t size = strlen(error.message()) + 1; char* buffer = (char*)this->helpers->malloc(size); memcpy(buffer, error.message(), size); return buffer; } else { return (char*)nullptr; } }); if ( result ) { mach_o::Error error("%s", (const char*)result); this->free(result); return error; } return mach_o::Error::none(); } private: const LibSystemHelpers* helpers = nullptr; lsl::MemoryManager* memoryManager = nullptr; }; #pragma clang diagnostic pop } // namespace #endif /* _libSystem_h */ |