Applied, thanks!

Samuel

ajrojas, le dim. 29 nov. 2026 11:01:08 -0300, a ecrit:
> htl's pthread_once registers its cleanup handler with
> pthread_cleanup_push around the init routine, and qsort with
> pthread_cleanup_combined_push, which on htl is only
> __pthread_cleanup_push.  htl runs those handlers only from
> pthread_exit, without unwinding, so if the callback exits with an
> exception the handler is never run.  pthread_once leaves the once
> control locked, so the next call on it hangs (std::call_once with a
> callable that throws, for example), and qsort leaks its buffer.  The
> handler also stays on the thread's cleanup stack in a dead frame, and a
> later pthread_exit in that thread calls it.
> 
> Implement pthread_cleanup_combined_push and pthread_cleanup_combined_pop
> for htl, as nptl did in commit f0419e6a107 (BZ #18435).  The handler is
> still pushed on the cleanup stack for pthread_exit, and a variable with
> the cleanup attribute pops it and runs it when the frame is unwound.
> Use them in pthread_once, and build pt-once.c with $(uses-callbacks)
> like qsort.c.
> 
> Move tst-once5 from nptl to sysdeps/pthread so that htl runs it too.
> sysd-rules only has rules for C and assembly sources, so add a vpath
> for the C++ sources of sysdeps/pthread.
> 
> Reported by nigko and Yelninei in
> https://codeberg.org/guix/guix/issues/11303.
> 
> Signed-off-by: ajrojas <[email protected]>
> ---
> I have no FSF copyright assignment for glibc; this is under the DCO.
> 
> nigko reduced the hang of Cython's
> cpp_mutex.test_py_safe_once_object_fail in Guix to std::call_once with a
> callable that throws.  Yelninei pointed at htl's pthread_once and
> guessed that its lock is never released, and nigko found that
> BZ #18435 had fixed the same thing in nptl only, see
> https://codeberg.org/guix/guix/issues/11303.  nigko also reproduced it
> on Debian GNU/Hurd i386.  The combined macros are the TODO that
> c8e73a1492b left in sysdeps/htl/pthreadP.h.  Florian Weimer suggested
> in his review of that patch that the Hurd side implement them
> (<[email protected]>).
> 
> Tested on x86_64-gnu (Guix in QEMU) with master b69a70dc23 cross-built
> twice, once with the patch and once with only its tst-once5 move,
> running each test 20 times through the built ld.so.  With the patch
> tst-once5 passes, and so do five local programs (not in the patch, I can
> send them): std::call_once with a throwing callable, a standalone copy
> of tst-once5, pthread_exit after catching an exception thrown by the
> init routine, cancelling a thread in the init routine, and qsort with a
> comparison function that throws, followed by pthread_exit (mtrace shows
> the buffer freed by qsort's landing pad).  With only the test move,
> tst-once5 and the first two programs hang every time, the two
> pthread_exit programs get SIGSEGV, and only the cancel one passes.
> htl's C once tests (tst-once1 to 4, tst-oncex3/4, tst-oncey3/4, 5 runs
> each) pass in both builds.
> 
> tst-qsort7 and tst-qsortx7 exit 0 in both builds, and qsort's buffer is
> freed from pthread_exit in both.  Their -mem checks fail in both
> anyway, because mtrace also reports the thread's own structure and
> TLS, which htl keeps for reuse.  So I left their xfails in
> sysdeps/mach/hurd/Makefile.  Their comment does not give that reason,
> I can update it.
> 
> I have not run the full testsuite on the Hurd (the htl and stdlib tests
> were cross-built and these ones run by hand), nor any static test, and
> I have not tried i686-gnu.  On x86_64-linux nptl still builds and
> passes tst-once5.
> 
> Guix's libstdc++ and the local programs were built against Guix's
> glibc 2.41, which, like Debian's, has pthread_once@GLIBC_2.41, so I
> preloaded a stub that provides that version as a jump to
> __pthread_once.  tst-once5 is linked against the built libc and calls
> pthread_once@GLIBC_2.42 itself.
> 
> The vpath for .cc in sysdeps/pthread/Makefile is the smallest thing
> that works; tell me if you would rather have a .cc rule in sysd-rules.
> 
>  htl/Makefile                           |  2 +
>  nptl/Makefile                          |  3 --
>  sysdeps/htl/pt-once.c                  |  5 ++-
>  sysdeps/htl/pthreadP.h                 | 52 +++++++++++++++++++-------
>  sysdeps/pthread/Makefile               | 11 ++++++
>  {nptl => sysdeps/pthread}/tst-once5.cc |  0
>  6 files changed, 55 insertions(+), 18 deletions(-)
>  rename {nptl => sysdeps/pthread}/tst-once5.cc (100%)
> 
> diff --git a/htl/Makefile b/htl/Makefile
> index 8078a7ade1b..9eb42c5b277 100644
> --- a/htl/Makefile
> +++ b/htl/Makefile
> @@ -211,6 +211,8 @@ install-lib-ldscripts := libpthread_syms.a
>  
>  include ../Makeconfig
>  
> +CFLAGS-pt-once.c += $(uses-callbacks)
> +
>  all: # Make this the default target; it will be defined in Rules.
>  
>  subdir_install: $(inst_libdir)/libpthread2.a $(inst_libdir)/libpthread_syms.a
> diff --git a/nptl/Makefile b/nptl/Makefile
> index bbab398e800..1c8c5c81c33 100644
> --- a/nptl/Makefile
> +++ b/nptl/Makefile
> @@ -259,7 +259,6 @@ CFLAGS-sem_clockwait.c = -fexceptions 
> -fasynchronous-unwind-tables
>  
>  CFLAGS-futex-internal.c += -fexceptions -fasynchronous-unwind-tables
>  
> -LDLIBS-tst-once5 = -lstdc++
>  CFLAGS-tst-thread_local1.o = -std=gnu++11
>  LDLIBS-tst-thread_local1 = -lstdc++
>  CFLAGS-tst-thread-exit-clobber.o = -std=gnu++11
> @@ -313,7 +312,6 @@ tests = \
>    tst-mutexpi11 \
>    tst-mutexpi12 \
>    tst-mutexpp11 \
> -  tst-once5 \
>    tst-pthread-affinity-inheritance \
>    tst-pthread-attr-affinity \
>    tst-pthread-attr-affinity-fail \
> @@ -597,7 +595,6 @@ tests-unsupported += \
>    tst-cancel24 \
>    tst-cancel24-static \
>    tst-minstack-throw \
> -  tst-once5 \
>    tst-thread-exit-clobber \
>    # tests-unsupported
>  endif
> diff --git a/sysdeps/htl/pt-once.c b/sysdeps/htl/pt-once.c
> index 965ab8894c9..32eb213f658 100644
> --- a/sysdeps/htl/pt-once.c
> +++ b/sysdeps/htl/pt-once.c
> @@ -20,6 +20,7 @@
>  #include <atomic.h>
>  
>  #include <pt-internal.h>
> +#include <pthreadP.h>
>  #include <shlib-compat.h>
>  
>  static void
> @@ -40,9 +41,9 @@ __pthread_once (pthread_once_t *once_control, void 
> (*init_routine) (void))
>  
>        if (once_control->__run == 0)
>       {
> -       pthread_cleanup_push (clear_once_control, once_control);
> +       pthread_cleanup_combined_push (clear_once_control, once_control);
>         init_routine ();
> -       pthread_cleanup_pop (0);
> +       pthread_cleanup_combined_pop (0);
>  
>         atomic_store_release (&once_control->__run, 1);
>       }
> diff --git a/sysdeps/htl/pthreadP.h b/sysdeps/htl/pthreadP.h
> index a8058dfad3c..885cfe4424b 100644
> --- a/sysdeps/htl/pthreadP.h
> +++ b/sysdeps/htl/pthreadP.h
> @@ -257,18 +257,44 @@ libc_hidden_proto (__pthread_create)
>    _Static_assert (sizeof (type) == size,                             \
>                 "sizeof (" #type ") != " #size)
>  
> - /* Special cleanup macros which register cleanup both using
> -    __pthread_cleanup_{push,pop} and using cleanup attribute.  This is needed
> -    for qsort, so that it supports both throwing exceptions from the caller
> -    sort function callback (only cleanup  attribute works there) and
> -    cancellation of the thread running the callback if the callback or some
> -    routines it calls don't have unwind information.
> -    TODO: add support for cleanup routines.  */
> -#ifndef pthread_cleanup_combined_push
> -# define pthread_cleanup_combined_push  __pthread_cleanup_push
> -#endif
> -#ifndef pthread_cleanup_combined_pop
> -# define pthread_cleanup_combined_pop   __pthread_cleanup_pop
> -#endif
> +#if defined __EXCEPTIONS && !defined __cplusplus
> +/* Special cleanup macros which register cleanup both using
> +   __pthread_cleanup_{push,pop} and using cleanup attribute.  This is needed
> +   for pthread_once and qsort, so that they support both throwing exceptions
> +   from the callback (only the cleanup attribute works there) and
> +   cancellation of the thread running the callback (pthread_exit runs the
> +   handlers registered with __pthread_cleanup_push, without unwinding).  */
> +
> +static __always_inline void
> +__pthread_cleanup_combined_routine (struct __pthread_cancelation_handler
> +                                 *__handler)
> +{
> +  /* The frame is being unwound and pthread_cleanup_combined_pop did not
> +     run.  Unregister the handler, and any handler registered after it,
> +     before the frame is gone, and call it.  */
> +  if (__handler->__handler != NULL)
> +    {
> +      *__pthread_get_cleanup_stack () = __handler->__next;
> +      __handler->__handler (__handler->__arg);
> +    }
> +}
> +
> +# define pthread_cleanup_combined_push(routine, arg) \
> +  do {                                                                       
>       \
> +    struct __pthread_cancelation_handler **__handlers                        
>       \
> +      = __pthread_get_cleanup_stack ();                                      
>       \
> +    struct __pthread_cancelation_handler __handler                         \
> +      __attribute__ ((__cleanup__ (__pthread_cleanup_combined_routine)))     
>  \
> +      = { (routine), (arg), *__handlers };                                 \
> +    *__handlers = &__handler;
> +
> +# define pthread_cleanup_combined_pop(execute) \
> +    *__handlers = __handler.__next;                                        \
> +    void (*__routine) (void *) = __handler.__handler;                        
>       \
> +    __handler.__handler = NULL;                                              
>       \
> +    if (execute)                                                           \
> +      __routine (__handler.__arg);                                         \
> +  } while (0)
> +#endif /* __EXCEPTIONS && !defined __cplusplus */
>  
>  #endif       /* pthreadP.h */
> diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
> index d0f3cd59ac6..11114202b40 100644
> --- a/sysdeps/pthread/Makefile
> +++ b/sysdeps/pthread/Makefile
> @@ -204,6 +204,7 @@ tests += \
>    tst-once2 \
>    tst-once3 \
>    tst-once4 \
> +  tst-once5 \
>    tst-pt-align \
>    tst-pt-align3 \
>    tst-pt-popen1 \
> @@ -524,6 +525,16 @@ tests += \
>    # tests
>  endif
>  
> +LDLIBS-tst-once5 = -lstdc++
> +ifeq (,$(CXX))
> +# This test requires a C++ compiler and runtime.
> +tests-unsupported += tst-once5
> +endif
> +
> +# sysd-rules only has rules for C and assembly sources, so find the C++
> +# test sources in this directory through vpath.
> +vpath %.cc $(..)sysdeps/pthread
> +
>  CFLAGS-tst-oncex3.c += -fexceptions
>  CFLAGS-tst-oncex4.c += -fexceptions
>  CFLAGS-tst-oncey3.c += -fno-exceptions -fno-asynchronous-unwind-tables
> diff --git a/nptl/tst-once5.cc b/sysdeps/pthread/tst-once5.cc
> similarity index 100%
> rename from nptl/tst-once5.cc
> rename to sysdeps/pthread/tst-once5.cc
> -- 
> 2.56.0
> 

-- 
Samuel
<k> faut en profiter, aujourd'hui, les blagues bidon sont à 100 dollars
 -+- #sos-bourse -+-

Reply via email to