From f5f1275602d7d7c891383036ec08c0bfc904be22 Mon Sep 17 00:00:00 2001 From: Apple Date: Mon, 19 Apr 2021 17:14:25 +0000 Subject: [PATCH] libpthread-454.100.8.tar.gz --- kern/kern_support.c | 32 +++++++++++++++++++++++----- libpthread.xcodeproj/project.pbxproj | 2 ++ src/prototypes_internal.h | 2 +- src/pthread.c | 29 +++++++++++++++++++++---- xcodescripts/install-sys-headers.sh | 9 ++++++++ 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/kern/kern_support.c b/kern/kern_support.c index 4c7848f..d33d6e3 100644 --- a/kern/kern_support.c +++ b/kern/kern_support.c @@ -269,16 +269,34 @@ _bsdthread_create(struct proc *p, } PTHREAD_TRACE(pthread_thread_create | DBG_FUNC_START, flags, 0, 0, 0); - - kret = pthread_kern->thread_create(ctask, &th); + + /* Create thread and make it immovable, do not pin control port yet */ + if (pthread_kern->thread_create_immovable) { + kret = pthread_kern->thread_create_immovable(ctask, &th); + } else { + kret = pthread_kern->thread_create(ctask, &th); + } + if (kret != KERN_SUCCESS) return(ENOMEM); thread_reference(th); pthread_kern->thread_set_tag(th, THREAD_TAG_PTHREAD); - sright = (void *)pthread_kern->convert_thread_to_port(th); - th_thport = pthread_kern->ipc_port_copyout_send(sright, pthread_kern->task_get_ipcspace(ctask)); + if (pthread_kern->convert_thread_to_port_pinned) { + /* Convert to immovable/pinned thread port, but port is not pinned yet */ + sright = (void *)pthread_kern->convert_thread_to_port_pinned(th); + } else { + sright = (void *)pthread_kern->convert_thread_to_port(th); + } + + if (pthread_kern->ipc_port_copyout_send_pinned) { + /* Atomically, pin and copy out the port */ + th_thport = pthread_kern->ipc_port_copyout_send_pinned(sright, pthread_kern->task_get_ipcspace(ctask)); + } else { + th_thport = pthread_kern->ipc_port_copyout_send(sright, pthread_kern->task_get_ipcspace(ctask)); + } + if (!MACH_PORT_VALID(th_thport)) { error = EMFILE; // userland will convert this into a crash goto out; @@ -494,7 +512,11 @@ _bsdthread_terminate(__unused struct proc *p, if (pthread_kern->thread_will_park_or_terminate) { pthread_kern->thread_will_park_or_terminate(th); } - (void)thread_terminate(th); + if (pthread_kern->thread_terminate_pinned) { + (void)pthread_kern->thread_terminate_pinned(th); + } else { + (void)thread_terminate(th); + } if (sem != MACH_PORT_NULL) { kret = pthread_kern->semaphore_signal_internal_trap(sem); if (kret != KERN_SUCCESS) { diff --git a/libpthread.xcodeproj/project.pbxproj b/libpthread.xcodeproj/project.pbxproj index 0c86611..d9b5c19 100644 --- a/libpthread.xcodeproj/project.pbxproj +++ b/libpthread.xcodeproj/project.pbxproj @@ -379,6 +379,7 @@ 6ED6E66C22BFFC3200CE82C2 /* imports_internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = imports_internal.h; sourceTree = ""; }; 6ED6E67322C0033B00CE82C2 /* prototypes_internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = prototypes_internal.h; sourceTree = ""; }; 74E594A41613AAF4006C417B /* libpthread_eOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libpthread_eOS.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 84D2815025314CEB0097326C /* sys_pthread_types.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = sys_pthread_types.modulemap; sourceTree = ""; }; 9202B2301D1A5B3F00945880 /* introspection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = introspection.h; sourceTree = ""; }; 9235CA551CA48D010015C92B /* kext_development.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = kext_development.xcconfig; sourceTree = ""; }; 9240BF321AA669C4003C99B4 /* wqtrace.lua */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = wqtrace.lua; path = tools/wqtrace.lua; sourceTree = SOURCE_ROOT; }; @@ -580,6 +581,7 @@ children = ( C9A325FD15B7513700270056 /* pthread */, FC5A372217CEB3D6008C323E /* sys */, + 84D2815025314CEB0097326C /* sys_pthread_types.modulemap */, ); path = include; sourceTree = ""; diff --git a/src/prototypes_internal.h b/src/prototypes_internal.h index 8fc4d4c..9694bee 100644 --- a/src/prototypes_internal.h +++ b/src/prototypes_internal.h @@ -77,7 +77,7 @@ extern uint64_t _pthread_debugstart; /* pthread.c */ void _pthread_deallocate(pthread_t t, bool from_mach_thread); -void _pthread_main_thread_init(pthread_t p); +void _pthread_main_thread_init(pthread_t p, mach_port_name_t main_th); void _pthread_main_thread_postfork_init(pthread_t p); void _pthread_bsdthread_init(struct _pthread_registration_data *data); void *_pthread_atomic_xchg_ptr(void **p, void *v); diff --git a/src/pthread.c b/src/pthread.c index 39f3131..6882fa2 100644 --- a/src/pthread.c +++ b/src/pthread.c @@ -1732,6 +1732,17 @@ parse_ptr_munge_params(const char *envp[], const char *apple[]) _pthread_init_signature(_main_thread_ptr); } +static void +parse_main_thread_port(const char *apple[], mach_port_name_t *main_th) +{ + const char *p, *s; + p = _simple_getenv(apple, "th_port"); + if (p) { + *main_th = (mach_port_name_t)_pthread_strtoul(p, &s, 16); + bzero((char *)p, strlen(p)); + } +} + int __pthread_init(const struct _libpthread_functions *pthread_funcs, const char *envp[], const char *apple[], @@ -1812,13 +1823,17 @@ __pthread_init(const struct _libpthread_functions *pthread_funcs, stackaddr, stacksize, allocaddr, allocsize); thread->tl_joinable = true; + // Get main thread port name from the kernel. + mach_port_name_t main_th_port = MACH_PORT_NULL; + parse_main_thread_port(apple, &main_th_port); + // Finish initialization with common code that is reinvoked on the // child side of a fork. // Finishes initialization of main thread attributes. // Initializes the thread list and add the main thread. // Calls _pthread_set_self() to prepare the main thread for execution. - _pthread_main_thread_init(thread); + _pthread_main_thread_init(thread, main_th_port); struct _pthread_registration_data registration_data; // Set up kernel entry points with __bsdthread_register. @@ -1842,7 +1857,7 @@ __pthread_init(const struct _libpthread_functions *pthread_funcs, #endif // !VARIANT_DYLD void -_pthread_main_thread_init(pthread_t p) +_pthread_main_thread_init(pthread_t p, mach_port_name_t main_thread_port) { TAILQ_INIT(&__pthread_head); _pthread_lock_init(&_pthread_list_lock); @@ -1850,7 +1865,13 @@ _pthread_main_thread_init(pthread_t p) p->__cleanup_stack = NULL; p->tl_join_ctx = NULL; p->tl_exit_gate = MACH_PORT_NULL; - _pthread_tsd_slot(p, MACH_THREAD_SELF) = mach_thread_self(); + + if (main_thread_port != MACH_PORT_NULL) { + _pthread_tsd_slot(p, MACH_THREAD_SELF) = main_thread_port; + } else { + // Can't get thread port from kernel or we are forking, fallback to mach_thread_self + _pthread_tsd_slot(p, MACH_THREAD_SELF) = mach_thread_self(); + } _pthread_tsd_slot(p, MIG_REPLY) = mach_reply_port(); _pthread_tsd_slot(p, MACH_SPECIAL_REPLY) = MACH_PORT_NULL; _pthread_tsd_slot(p, SEMAPHORE_CACHE) = SEMAPHORE_NULL; @@ -1865,7 +1886,7 @@ _pthread_main_thread_init(pthread_t p) void _pthread_main_thread_postfork_init(pthread_t p) { - _pthread_main_thread_init(p); + _pthread_main_thread_init(p, MACH_PORT_NULL); _pthread_set_self_internal(p); } diff --git a/xcodescripts/install-sys-headers.sh b/xcodescripts/install-sys-headers.sh index 7b391fc..647bfae 100644 --- a/xcodescripts/install-sys-headers.sh +++ b/xcodescripts/install-sys-headers.sh @@ -32,9 +32,18 @@ install_headers() find "${DSTROOT}/${SDK_INSTALL_HEADERS_ROOT}$2" -type f -name *.h -print0 | \ xargs -0I % unifdef -t ${COPY_HEADERS_UNIFDEF_FLAGS} -o "%" "%" +} + +install_modulemaps() +{ + mkdir -p "${DSTROOT}/${SDK_INSTALL_HEADERS_ROOT}$2" + cp "${SRCROOT}/$1/"*.modulemap "${DSTROOT}/${SDK_INSTALL_HEADERS_ROOT}$2" + find "${DSTROOT}/${SDK_INSTALL_HEADERS_ROOT}$2" -type f -name *.modulemap -print0 | \ xargs -0I % unifdef -t ${COPY_HEADERS_UNIFDEF_FLAGS} -o "%" "%" } install_headers "include/sys" "/usr/include" install_headers "private/sys" "/usr/local/include" + +install_modulemaps "include" "/usr/include" -- 2.45.2