[4/4] nptl: Load the unwinder during the first pthread_create (BZ 34689)
Checks
| Context |
Check |
Description |
| redhat-pt-bot/TryBot-apply_patch |
success
|
Patch applied to master at the time it was sent
|
| redhat-pt-bot/TryBot-32bit |
success
|
Build for i686
|
| linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_glibc_build--master-arm |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_glibc_check--master-arm |
success
|
Test passed
|
| linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 |
success
|
Test passed
|
Commit Message
pthread_exit and pthread_cancel load libgcc_s lazily, on first use, and
abort the process with "libgcc_s.so.1 must be installed for ... to
work" if the dlopen fails. The first use might happen long after the
process started, when loading is more likely to fail:
* The process might have lowered its file descriptor limit or
installed a seccomp filter after initialization.
* The process might have exhausted its address space or file
descriptors (BZ 13119).
* The system administrator might have installed a new libgcc_s that
depends on symbols of a newer glibc than the one the process is
running (for instance, libgcc_s built against glibc 2.35 or newer
requires _dl_find_object@GLIBC_2.35).
The first pthread_create now tries to load the unwinder, while the
process is still single-threaded. This also avoids the dlopen racing
with fork in the common case, since fork does not synchronize with a
concurrent dlopen.
The load is best effort, where a failure is ignored and pthread_create
does not fail (and the load is retried on first use as before). Failing
pthread_create for transient errors would require telling them apart
from a missing libgcc_s (BZ 22040), and it would add a new failure mode
for the majority of programs that never cancel a thread. This is a
bigger change than it sounds and can be done as subsequent patch.
The extra cost is one dlopen per process (about 45us on x86_64 if
libgcc_s is not already loaded, similar to the first pthread_create
itself).
On arm, libsupport references the EHABI personality routines, so every
test depends on libgcc_s and the new test would be UNSUPPORTED. The
test is linked with the libgcc stubs (gnulib-arch) to avoid it.
Checked on x86_64-linux-gnu. I also ran the new test on aarch64, alpha,
armv7-a, hppa, i686, loongarch64, mips64el (n64 and n32), mipsel,
powerpc, powerpc64, powerpc64le, riscv64, s390x, sparc64, and sparcv9,
using qemu system for all but i686. Without this change, it fails on
all of them.
---
nptl/Makefile | 7 ++
nptl/pthread_create.c | 6 ++
nptl/tst-pthread-unwind-preload.c | 161 ++++++++++++++++++++++++++++++
3 files changed, 174 insertions(+)
create mode 100644 nptl/tst-pthread-unwind-preload.c
@@ -477,6 +477,7 @@ ifeq ($(build-shared),yes)
tests += \
tst-audit-threads \
tst-compat-forwarder \
+ tst-pthread-unwind-preload \
# tests
tests-internal += \
tst-stackguard1 \
@@ -699,6 +700,12 @@ endif
LDLIBS-tst-cancel24 = -Wl,--no-as-needed -lstdc++
LDLIBS-tst-cancel24-static = $(LDLIBS-tst-cancel24)
+# libsupport is built with unwind tables, which on some ABIs (arm) reference
+# the EHABI personality routines and make every test depend on libgcc_s.
+# Resolve them with the libgcc stubs, so the test starts without
+# libgcc_s loaded (the unwinder uses its own personality routines).
+LDLIBS-tst-pthread-unwind-preload = $(gnulib-arch)
+
ifeq ($(build-shared),yes)
generated += \
@@ -39,6 +39,7 @@
#include <clone_internal.h>
#include <futex-internal.h>
#include <getrandom-internal.h>
+#include <unwind-link.h>
#include <shlib-compat.h>
@@ -670,6 +671,11 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr,
/* __libc_single_threaded can be accessed through copy relocations, so
it requires to update the external copy. */
__libc_single_threaded = 0;
+
+ /* Load the unwinder used by pthread_exit and pthread_cancel while the
+ process is still single-threaded. Loading it on first use may fail
+ so a failure here is ignored and the load is retried on first use. */
+ __libc_unwind_link_get ();
}
const struct pthread_attr *iattr = (struct pthread_attr *) attr;
new file mode 100644
@@ -0,0 +1,161 @@
+/* Check that the first pthread_create loads the unwinder (BZ 34689).
+ Copyright (C) 2026 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
+ <https://www.gnu.org/licenses/>. */
+
+#include <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/resource.h>
+#include <unistd.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/xdlfcn.h>
+#include <support/xthread.h>
+#include <support/xunistd.h>
+
+#ifndef HAVE_CC_WITH_LIBUNWIND
+# define UNWIND_SONAME LIBGCC_S_SO
+#else
+# define UNWIND_SONAME LIBUNWIND_SO
+#endif
+
+static bool
+unwinder_loaded (void)
+{
+ void *handle = dlopen (UNWIND_SONAME, RTLD_LAZY | RTLD_NOLOAD);
+ if (handle == NULL)
+ return false;
+ xdlclose (handle);
+ return true;
+}
+
+static struct rlimit saved_nofile;
+
+static void
+restrict_nofile (void)
+{
+ if (getrlimit (RLIMIT_NOFILE, &saved_nofile) != 0)
+ FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
+ struct rlimit rl = saved_nofile;
+ rl.rlim_cur = 0;
+ if (setrlimit (RLIMIT_NOFILE, &rl) != 0)
+ FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
+}
+
+static void
+restore_nofile (void)
+{
+ if (setrlimit (RLIMIT_NOFILE, &saved_nofile) != 0)
+ FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
+}
+
+static void *
+thread_return (void *closure)
+{
+ return closure;
+}
+
+static void *
+thread_exit (void *closure)
+{
+ pthread_exit (closure);
+}
+
+static void *
+thread_block (void *closure)
+{
+ while (true)
+ pause ();
+ return NULL;
+}
+
+/* The first pthread_create loads the unwinder, so pthread_exit and
+ pthread_cancel keep working after the process can no longer open files. */
+static void
+test_preload (void *closure)
+{
+ TEST_VERIFY (!unwinder_loaded ());
+ xpthread_join (xpthread_create (NULL, thread_return, NULL));
+ TEST_VERIFY (unwinder_loaded ());
+
+ restrict_nofile ();
+
+ void *r = xpthread_join (xpthread_create (NULL, thread_exit,
+ (void *) (uintptr_t) 42));
+ TEST_VERIFY (r == (void *) (uintptr_t) 42);
+
+ pthread_t thr = xpthread_create (NULL, thread_block, NULL);
+ xpthread_cancel (thr);
+ TEST_VERIFY (xpthread_join (thr) == PTHREAD_CANCELED);
+
+ restore_nofile ();
+}
+
+/* A failure to load the unwinder does not make pthread_create fail, and the
+ unwinder is still loaded on first use. This also passes without the early
+ load; it checks that a failed load is ignored and retried. */
+static void
+test_preload_failure (void *closure)
+{
+ TEST_VERIFY (!unwinder_loaded ());
+
+ restrict_nofile ();
+ pthread_t thr;
+ TEST_COMPARE (pthread_create (&thr, NULL, thread_return, NULL), 0);
+ xpthread_join (thr);
+ restore_nofile ();
+
+ TEST_VERIFY (!unwinder_loaded ());
+
+ void *r = xpthread_join (xpthread_create (NULL, thread_exit,
+ (void *) (uintptr_t) 42));
+ TEST_VERIFY (r == (void *) (uintptr_t) 42);
+ TEST_VERIFY (unwinder_loaded ());
+
+ thr = xpthread_create (NULL, thread_block, NULL);
+ xpthread_cancel (thr);
+ TEST_VERIFY (xpthread_join (thr) == PTHREAD_CANCELED);
+}
+
+static int
+do_test (void)
+{
+ /* The tests below require a single-threaded process that has not loaded
+ the unwinder yet. */
+ if (unwinder_loaded ())
+ FAIL_UNSUPPORTED (UNWIND_SONAME " is already loaded");
+
+ pid_t pid = xfork ();
+ if (pid == 0)
+ _exit (dlopen (UNWIND_SONAME, RTLD_LAZY) == NULL);
+
+ {
+ int status;
+ xwaitpid (pid, &status, 0);
+ if (status != 0)
+ FAIL_UNSUPPORTED (UNWIND_SONAME " cannot be loaded");
+ }
+
+ support_isolate_in_subprocess (test_preload, NULL);
+ support_isolate_in_subprocess (test_preload_failure, NULL);
+
+ return 0;
+}
+
+#include <support/test-driver.c>