wcsmbs: Reformat Makefile.
[glibc.git] / elf / tst-tls-ie-dlmopen.c
blob73f68e2454a6f4a3d124b02eea60097d541f4a3e
1 /* Test dlopen of modules with initial-exec TLS after dlmopen.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* This test tries to check that surplus static TLS is not used up for
20 dynamic TLS optimizations and 4*144 = 576 bytes of static TLS is
21 still available for dlopening modules with initial-exec TLS after 3
22 new dlmopen namespaces are created. It depends on rtld.nns=4 and
23 rtld.optional_static_tls=512 tunable settings. */
25 #include <errno.h>
26 #include <pthread.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 static int do_test (void);
32 #include <support/xthread.h>
33 #include <support/xdlfcn.h>
34 #include <support/check.h>
35 #include <support/test-driver.c>
37 /* Have some big TLS in the main exe: should not use surplus TLS. */
38 __thread char maintls[1000];
40 static pthread_barrier_t barrier;
42 /* Forces multi-threaded behaviour. */
43 static void *
44 blocked_thread_func (void *closure)
46 xpthread_barrier_wait (&barrier);
47 /* TLS load and access tests run here in the main thread. */
48 xpthread_barrier_wait (&barrier);
49 return NULL;
52 static void *
53 load_and_access (Lmid_t lmid, const char *mod, const char *func)
55 /* Load module with TLS. */
56 void *p = xdlmopen (lmid, mod, RTLD_NOW);
57 /* Access the TLS variable to ensure it is allocated. */
58 void (*f) (void) = (void (*) (void))xdlsym (p, func);
59 f ();
60 return p;
63 static int
64 do_test (void)
66 void *mods[5];
69 int ret = pthread_barrier_init (&barrier, NULL, 2);
70 if (ret != 0)
72 errno = ret;
73 printf ("error: pthread_barrier_init: %m\n");
74 exit (1);
78 pthread_t blocked_thread = xpthread_create (NULL, blocked_thread_func, NULL);
79 xpthread_barrier_wait (&barrier);
81 printf ("maintls[%zu]:\t %p .. %p\n",
82 sizeof maintls, maintls, maintls + sizeof maintls);
83 memset (maintls, 1, sizeof maintls);
85 /* Load modules with dynamic TLS (use surplus static TLS for libc
86 in new namespaces and may be for TLS optimizations too). */
87 mods[0] = load_and_access (LM_ID_BASE, "tst-tls-ie-mod0.so", "access0");
88 mods[1] = load_and_access (LM_ID_NEWLM, "tst-tls-ie-mod1.so", "access1");
89 mods[2] = load_and_access (LM_ID_NEWLM, "tst-tls-ie-mod2.so", "access2");
90 mods[3] = load_and_access (LM_ID_NEWLM, "tst-tls-ie-mod3.so", "access3");
91 /* Load modules with initial-exec TLS (can only use surplus static TLS). */
92 mods[4] = load_and_access (LM_ID_BASE, "tst-tls-ie-mod6.so", "access6");
94 /* Here 576 bytes + 3 * libc use of surplus static TLS is in use so less
95 than 1024 bytes are available (exact number depends on TLS optimizations
96 and the libc TLS use). */
97 printf ("The next dlmopen should fail...\n");
98 void *p = dlmopen (LM_ID_BASE, "tst-tls-ie-mod4.so", RTLD_NOW);
99 if (p != NULL)
100 FAIL_EXIT1 ("error: expected dlmopen to fail because there is "
101 "not enough surplus static TLS.\n");
102 printf ("...OK failed with: %s.\n", dlerror ());
104 xpthread_barrier_wait (&barrier);
105 xpthread_join (blocked_thread);
107 /* Close the modules. */
108 for (int i = 0; i < 5; ++i)
109 xdlclose (mods[i]);
111 return 0;