[08/15] nptl: Use exit_lock when accessing TID on pthread_getcpuclockid

Message ID 20210930200051.1017457-9-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • Fix various NPTL synchronization issues
Related show

Commit Message

Carlos O'Donell via Libc-alpha Sept. 30, 2021, 8 p.m.
Checked on x86_64-linux-gnu.
---
 nptl/pthread_getcpuclockid.c         | 26 ++++++++++++++------------
 sysdeps/pthread/tst-pthread-exited.c |  7 +++++++
 2 files changed, 21 insertions(+), 12 deletions(-)

-- 
2.30.2

Patch

diff --git a/nptl/pthread_getcpuclockid.c b/nptl/pthread_getcpuclockid.c
index 0a6656ea4c..63f1978665 100644
--- a/nptl/pthread_getcpuclockid.c
+++ b/nptl/pthread_getcpuclockid.c
@@ -16,11 +16,9 @@ 
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <pthreadP.h>
-#include <sys/time.h>
-#include <tls.h>
+#include <libc-lock.h>
 #include <kernel-posix-cpu-timers.h>
+#include <pthreadP.h>
 #include <shlib-compat.h>
 
 int
@@ -28,17 +26,21 @@  __pthread_getcpuclockid (pthread_t threadid, clockid_t *clockid)
 {
   struct pthread *pd = (struct pthread *) threadid;
 
-  /* Make sure the descriptor is valid.  */
-  if (INVALID_TD_P (pd))
-    /* Not a valid thread handle.  */
-    return ESRCH;
+  /* Block all signals, as required by pd->exit_lock.  */
+  sigset_t old_mask;
+  __libc_signal_block_all (&old_mask);
+  __libc_lock_lock (pd->exit_lock);
 
-  /* The clockid_t value is a simple computation from the TID.  */
+  int res = 0;
+  if (pd->tid != 0)
+    *clockid = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  else
+    res = ESRCH;
 
-  const clockid_t tidclock = MAKE_THREAD_CPUCLOCK (pd->tid, CPUCLOCK_SCHED);
+  __libc_lock_unlock (pd->exit_lock);
+  __libc_signal_restore_set (&old_mask);
 
-  *clockid = tidclock;
-  return 0;
+  return res;
 }
 versioned_symbol (libc, __pthread_getcpuclockid, pthread_getcpuclockid,
                   GLIBC_2_34);
diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c
index 567d6b9b49..d33e6b28fe 100644
--- a/sysdeps/pthread/tst-pthread-exited.c
+++ b/sysdeps/pthread/tst-pthread-exited.c
@@ -23,6 +23,7 @@ 
 #include <support/check.h>
 #include <support/support.h>
 #include <support/xthread.h>
+#include <time.h>
 
 static void *
 noop_thread (void *closure)
@@ -50,6 +51,12 @@  do_test (void)
     TEST_COMPARE (r, ESRCH);
   }
 
+  {
+    clockid_t clkid;
+    int r = pthread_getcpuclockid (thr, &clkid);
+    TEST_COMPARE (r, ESRCH);
+  }
+
   xpthread_join (thr);
 
   return 0;