08c3a6
commit a7ec6363a3a8fd7a2014fd7398bcdcab42919ec1
08c3a6
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
08c3a6
Date:   Tue May 31 17:13:35 2022 -0300
08c3a6
08c3a6
    nptl: Fix __libc_cleanup_pop_restore asynchronous restore (BZ#29214)
08c3a6
    
08c3a6
    This was due a wrong revert done on 404656009b459658.
08c3a6
    
08c3a6
    Checked on x86_64-linux-gnu.
08c3a6
    
08c3a6
    (cherry picked from commit c7d36dcecc08a29825175f65c4ee873ff3177a23)
08c3a6
08c3a6
diff --git a/nptl/libc-cleanup.c b/nptl/libc-cleanup.c
08c3a6
index fccb1abe69aa693c..a37c48ff876d613a 100644
08c3a6
--- a/nptl/libc-cleanup.c
08c3a6
+++ b/nptl/libc-cleanup.c
08c3a6
@@ -58,7 +58,8 @@ __libc_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer)
08c3a6
   THREAD_SETMEM (self, cleanup, buffer->__prev);
08c3a6
 
08c3a6
   int cancelhandling = atomic_load_relaxed (&self->cancelhandling);
08c3a6
-  if (cancelhandling & CANCELTYPE_BITMASK)
08c3a6
+  if (buffer->__canceltype != PTHREAD_CANCEL_DEFERRED
08c3a6
+      && (cancelhandling & CANCELTYPE_BITMASK) == 0)
08c3a6
     {
08c3a6
       int newval;
08c3a6
       do
08c3a6
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
08c3a6
index 5147588c130c9415..d99c161c827ef4b8 100644
08c3a6
--- a/sysdeps/pthread/Makefile
08c3a6
+++ b/sysdeps/pthread/Makefile
08c3a6
@@ -126,6 +126,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \
08c3a6
 	 tst-pthread-raise-blocked-self \
08c3a6
 	 tst-pthread_kill-exited \
08c3a6
 	 tst-pthread_kill-exiting \
08c3a6
+	 tst-cancel30 \
08c3a6
 	 # tests
08c3a6
 
08c3a6
 tests-time64 := \
08c3a6
diff --git a/sysdeps/pthread/tst-cancel30.c b/sysdeps/pthread/tst-cancel30.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..e08392f96874de5f
08c3a6
--- /dev/null
08c3a6
+++ b/sysdeps/pthread/tst-cancel30.c
08c3a6
@@ -0,0 +1,82 @@
08c3a6
+/* Check if printf like functions does not disable asynchronous cancellation
08c3a6
+   mode (BZ#29214).
08c3a6
+
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <support/check.h>
08c3a6
+#include <support/xstdio.h>
08c3a6
+#include <support/xthread.h>
08c3a6
+#include <sys/syscall.h>
08c3a6
+#include <unistd.h>
08c3a6
+
08c3a6
+static pthread_barrier_t b;
08c3a6
+
08c3a6
+static void *
08c3a6
+tf (void *arg)
08c3a6
+{
08c3a6
+  int old;
08c3a6
+
08c3a6
+  TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL), 0);
08c3a6
+
08c3a6
+  TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0);
08c3a6
+  TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS);
08c3a6
+
08c3a6
+  /* Check if internal lock cleanup routines restore the cancellation type
08c3a6
+     correctly.  */
08c3a6
+  printf ("...\n");
08c3a6
+  TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0);
08c3a6
+  TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS);
08c3a6
+
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  /* Wait indefinitely for cancellation, which only works if asynchronous
08c3a6
+     cancellation is enabled.  */
08c3a6
+#ifdef SYS_pause
08c3a6
+  syscall (SYS_pause);
08c3a6
+#elif defined SYS_ppoll || defined SYS_ppoll_time64
08c3a6
+# ifndef SYS_ppoll_time64
08c3a6
+#  define SYS_ppoll_time64 SYS_ppoll
08c3a6
+# endif
08c3a6
+  syscall (SYS_ppoll_time64, NULL, 0, NULL, NULL);
08c3a6
+#else
08c3a6
+  for (;;);
08c3a6
+#endif
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+static int
08c3a6
+do_test (void)
08c3a6
+{
08c3a6
+  xpthread_barrier_init (&b, NULL, 2);
08c3a6
+
08c3a6
+  pthread_t th = xpthread_create (NULL, tf, NULL);
08c3a6
+
08c3a6
+  xpthread_barrier_wait (&b);
08c3a6
+
08c3a6
+  xpthread_cancel (th);
08c3a6
+
08c3a6
+  void *status = xpthread_join (th);
08c3a6
+  TEST_VERIFY (status == PTHREAD_CANCELED);
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+/* There is no need to wait full TIMEOUT if asynchronous is not working.  */
08c3a6
+#define TIMEOUT 3
08c3a6
+#include <support/test-driver.c>