From patchwork Wed May 20 21:55:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roland McGrath X-Patchwork-Id: 6835 Received: (qmail 54509 invoked by alias); 20 May 2015 21:55:28 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 54420 invoked by uid 89); 20 May 2015 21:55:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY autolearn=no version=3.3.2 X-HELO: topped-with-meat.com MIME-Version: 1.0 From: Roland McGrath To: "GNU C. Library" Subject: [COMMITTED PATCH] NaCl: Set tid field to a unique value. Message-Id: <20150520215524.87CA82C3ABD@topped-with-meat.com> Date: Wed, 20 May 2015 14:55:24 -0700 (PDT) X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=SvUDtp+0 c=1 sm=1 tr=0 a=WkljmVdYkabdwxfqvArNOQ==:117 a=14OXPxybAAAA:8 a=kj9zAlcOel0A:10 a=hOe2yjtxAAAA:8 a=mDV3o1hIAAAA:8 a=9RreDBEwINWdFKEa61AA:9 a=NfWm3E6KTzlVoW92:21 a=ZjpGGZ5BKs4idTMv:21 a=CjuIK1q_8ugA:10 This entailed a trivial refactoring to get a Linuxism out of nptl/nptl-init.c. I've verified on x86_64-linux-gnu and i686-linux-gnu that the compiled code was unchanged by moving the code to an inline function. Thanks, Roland * sysdeps/nacl/pthread-pids.h: New file. * sysdeps/nacl/createthread.c: Include . (create_thread): Use __nacl_get_tid to initialize PD->tid. * nptl/pthread-pids.h: New file. * sysdeps/unix/sysv/linux/pthread-pids.h: New file. * nptl/nptl-init.c: Include . (__pthread_initialize_minimal_internal): Call __pthread_initialize_pids instead of set_tid_address syscall. diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c index 1ad41c5..5b8d931 100644 --- a/nptl/nptl-init.c +++ b/nptl/nptl-init.c @@ -36,6 +36,7 @@ #include #include #include +#include #ifndef TLS_MULTIPLE_THREADS_IN_TCB /* Pointer to the corresponding variable in libc. */ @@ -311,10 +312,7 @@ __pthread_initialize_minimal_internal (void) /* Minimal initialization of the thread descriptor. */ struct pthread *pd = THREAD_SELF; -#ifdef __NR_set_tid_address - INTERNAL_SYSCALL_DECL (err); - pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid); -#endif + __pthread_initialize_pids (pd); THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]); THREAD_SETMEM (pd, user_stack, true); if (LLL_LOCK_INITIALIZER != 0) diff --git a/nptl/pthread-pids.h b/nptl/pthread-pids.h new file mode 100644 index 0000000..33d764c --- /dev/null +++ b/nptl/pthread-pids.h @@ -0,0 +1,29 @@ +/* Initialize pid and tid fields of struct pthread. Stub version. + Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +/* Initialize PD->pid and PD->tid for the initial thread. If there is + setup required to arrange that __exit_thread causes PD->tid to be + cleared and futex-woken, then this function should do that as well. */ +static inline void +__pthread_initialize_pids (struct pthread *pd) +{ +#error "sysdeps pthread-pids.h file required" + pd->pid = pd->tid = -1; +} diff --git a/sysdeps/nacl/createthread.c b/sysdeps/nacl/createthread.c index 9df4eae..9d9284a 100644 --- a/sysdeps/nacl/createthread.c +++ b/sysdeps/nacl/createthread.c @@ -17,6 +17,7 @@ . */ #include +#include /* See the comments in pthread_create.c for the requirements for these two macros and the create_thread function. */ @@ -33,6 +34,8 @@ static int create_thread (struct pthread *pd, const struct pthread_attr *attr, bool stopped_start, STACK_VARIABLES_PARMS, bool *thread_ran) { + pd->tid = __nacl_get_tid (pd); + pd->stopped_start = stopped_start; if (__glibc_unlikely (stopped_start)) /* We make sure the thread does not run far by forcing it to get a diff --git a/sysdeps/nacl/pthread-pids.h b/sysdeps/nacl/pthread-pids.h new file mode 100644 index 0000000..ccb99d6 --- /dev/null +++ b/sysdeps/nacl/pthread-pids.h @@ -0,0 +1,65 @@ +/* Initialize pid and tid fields of struct pthread. NaCl version. + Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + + +/* NaCl has no concept of PID or TID, nor even any notion of an + identifier for a thread within the process. But various places in + the NPTL implementation rely on using the 'tid' field of the TCB + (struct pthread) as an identifier that is unique at least among all + live threads in the process. So we must synthesize some number to + use. Conveniently, the 'pthread_t' value itself is already unique + in exactly this way (because it's the 'struct pthread' pointer). + + The only wrinkle is that 'tid' is a (32-bit) 'int' and its high + (sign) bit is used for special purposes, so we must be absolutely + sure that we never use a pointer value with the high bit set. (It + also cannot be zero, but zero is never a valid pointer anyway.) + The NaCl sandbox models for 32-bit machines limit the address space + to less than 3GB (in fact, to 1GB), so it's already impossible that + a valid pointer will have its high bit set. But the NaCl x86-64 + sandbox model allows a full 4GB of address space, so we cannot + assume that an arbitrary pointer value will not have the high bit + set. Conveniently, there are always unused bits in the pointer + value for a 'struct pthread', because it is always aligned to at + least 32 bits and so the low bits are always zero. Hence, we can + safely avoid the danger of a nonzero high bit just by shifting the + pointer value right. */ + +static inline int +__nacl_get_tid (struct pthread *pd) +{ + uintptr_t id = (uintptr_t) pd; + int tid = id >> 1; + assert ((id & 1) == 0); + assert (sizeof id == sizeof tid); + assert (tid > 0); + return tid; +} + + +/* Initialize PD->pid and PD->tid for the initial thread. If there is + setup required to arrange that __exit_thread causes PD->tid to be + cleared and futex-woken, then this function should do that as well. */ +static inline void +__pthread_initialize_pids (struct pthread *pd) +{ + pd->tid = __nacl_get_tid (pd); + pd->pid = -1; +} diff --git a/sysdeps/unix/sysv/linux/pthread-pids.h b/sysdeps/unix/sysv/linux/pthread-pids.h new file mode 100644 index 0000000..d333648 --- /dev/null +++ b/sysdeps/unix/sysv/linux/pthread-pids.h @@ -0,0 +1,30 @@ +/* Initialize pid and tid fields of struct pthread. Linux version. + Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Initialize PD->pid and PD->tid for the initial thread. If there is + setup required to arrange that __exit_thread causes PD->tid to be + cleared and futex-woken, then this function should do that as well. */ +static inline void +__pthread_initialize_pids (struct pthread *pd) +{ + INTERNAL_SYSCALL_DECL (err); + pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid); +}