commit a7ec6363a3a8fd7a2014fd7398bcdcab42919ec1 Author: Adhemerval Zanella Date: Tue May 31 17:13:35 2022 -0300 nptl: Fix __libc_cleanup_pop_restore asynchronous restore (BZ#29214) This was due a wrong revert done on 404656009b459658. Checked on x86_64-linux-gnu. (cherry picked from commit c7d36dcecc08a29825175f65c4ee873ff3177a23) diff --git a/nptl/libc-cleanup.c b/nptl/libc-cleanup.c index fccb1abe69aa693c..a37c48ff876d613a 100644 --- a/nptl/libc-cleanup.c +++ b/nptl/libc-cleanup.c @@ -58,7 +58,8 @@ __libc_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer) THREAD_SETMEM (self, cleanup, buffer->__prev); int cancelhandling = atomic_load_relaxed (&self->cancelhandling); - if (cancelhandling & CANCELTYPE_BITMASK) + if (buffer->__canceltype != PTHREAD_CANCEL_DEFERRED + && (cancelhandling & CANCELTYPE_BITMASK) == 0) { int newval; do diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 5147588c130c9415..d99c161c827ef4b8 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -126,6 +126,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \ tst-pthread-raise-blocked-self \ tst-pthread_kill-exited \ tst-pthread_kill-exiting \ + tst-cancel30 \ # tests tests-time64 := \ diff --git a/sysdeps/pthread/tst-cancel30.c b/sysdeps/pthread/tst-cancel30.c new file mode 100644 index 0000000000000000..e08392f96874de5f --- /dev/null +++ b/sysdeps/pthread/tst-cancel30.c @@ -0,0 +1,82 @@ +/* Check if printf like functions does not disable asynchronous cancellation + mode (BZ#29214). + + Copyright (C) 2022 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 +#include +#include +#include + +static pthread_barrier_t b; + +static void * +tf (void *arg) +{ + int old; + + TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL), 0); + + TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0); + TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS); + + /* Check if internal lock cleanup routines restore the cancellation type + correctly. */ + printf ("...\n"); + TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0); + TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS); + + xpthread_barrier_wait (&b); + + /* Wait indefinitely for cancellation, which only works if asynchronous + cancellation is enabled. */ +#ifdef SYS_pause + syscall (SYS_pause); +#elif defined SYS_ppoll || defined SYS_ppoll_time64 +# ifndef SYS_ppoll_time64 +# define SYS_ppoll_time64 SYS_ppoll +# endif + syscall (SYS_ppoll_time64, NULL, 0, NULL, NULL); +#else + for (;;); +#endif + + return 0; +} + +static int +do_test (void) +{ + xpthread_barrier_init (&b, NULL, 2); + + pthread_t th = xpthread_create (NULL, tf, NULL); + + xpthread_barrier_wait (&b); + + xpthread_cancel (th); + + void *status = xpthread_join (th); + TEST_VERIFY (status == PTHREAD_CANCELED); + + return 0; +} + +/* There is no need to wait full TIMEOUT if asynchronous is not working. */ +#define TIMEOUT 3 +#include