string: Fix tester build with fortify enable with gcc 6
[glibc.git] / elf / tst-tls-ie.c
blobd71b349f5dfb812ff2b5fc9ed8d8eef38dcda922
1 /* Test dlopen of modules with initial-exec TLS.
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 3*192 + 4*144 = 1152 bytes of static
21 TLS is available for dlopening modules with initial-exec TLS. It
22 depends on rtld.nns=4 and rtld.optional_static_tls=512 tunable setting. */
24 #include <errno.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 static int do_test (void);
31 #include <support/xthread.h>
32 #include <support/xdlfcn.h>
33 #include <support/check.h>
34 #include <support/test-driver.c>
36 /* Have some big TLS in the main exe: should not use surplus TLS. */
37 __thread char maintls[1000];
39 static pthread_barrier_t barrier;
41 /* Forces multi-threaded behaviour. */
42 static void *
43 blocked_thread_func (void *closure)
45 xpthread_barrier_wait (&barrier);
46 /* TLS load and access tests run here in the main thread. */
47 xpthread_barrier_wait (&barrier);
48 return NULL;
51 static void *
52 load_and_access (const char *mod, const char *func)
54 /* Load module with TLS. */
55 void *p = xdlopen (mod, RTLD_NOW);
56 /* Access the TLS variable to ensure it is allocated. */
57 void (*f) (void) = (void (*) (void))xdlsym (p, func);
58 f ();
59 return p;
62 static int
63 do_test (void)
65 void *mods[6];
68 int ret = pthread_barrier_init (&barrier, NULL, 2);
69 if (ret != 0)
71 errno = ret;
72 printf ("error: pthread_barrier_init: %m\n");
73 exit (1);
77 pthread_t blocked_thread = xpthread_create (NULL, blocked_thread_func, NULL);
78 xpthread_barrier_wait (&barrier);
80 printf ("maintls[%zu]:\t %p .. %p\n",
81 sizeof maintls, maintls, maintls + sizeof maintls);
82 memset (maintls, 1, sizeof maintls);
84 /* Load modules with dynamic TLS (may use surplus static TLS
85 opportunistically). */
86 mods[0] = load_and_access ("tst-tls-ie-mod0.so", "access0");
87 mods[1] = load_and_access ("tst-tls-ie-mod1.so", "access1");
88 mods[2] = load_and_access ("tst-tls-ie-mod2.so", "access2");
89 mods[3] = load_and_access ("tst-tls-ie-mod3.so", "access3");
90 /* Load modules with initial-exec TLS (can only use surplus static TLS). */
91 mods[4] = load_and_access ("tst-tls-ie-mod4.so", "access4");
92 mods[5] = load_and_access ("tst-tls-ie-mod5.so", "access5");
94 /* Here 1152 bytes of surplus static TLS is in use and at most 512 bytes
95 are available (depending on TLS optimizations). */
96 printf ("The next dlopen should fail...\n");
97 void *p = dlopen ("tst-tls-ie-mod6.so", RTLD_NOW);
98 if (p != NULL)
99 FAIL_EXIT1 ("error: expected dlopen to fail because there is "
100 "not enough surplus static TLS.\n");
101 printf ("...OK failed with: %s.\n", dlerror ());
103 xpthread_barrier_wait (&barrier);
104 xpthread_join (blocked_thread);
106 /* Close the modules. */
107 for (int i = 0; i < 6; ++i)
108 xdlclose (mods[i]);
110 return 0;