1 | /* Test dlopen of modules with initial-exec TLS. |
2 | Copyright (C) 2016-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
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. */ |
23 | |
24 | #include <errno.h> |
25 | #include <pthread.h> |
26 | #include <stdio.h> |
27 | #include <stdlib.h> |
28 | #include <string.h> |
29 | |
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> |
35 | |
36 | /* Have some big TLS in the main exe: should not use surplus TLS. */ |
37 | __thread char maintls[1000]; |
38 | |
39 | static pthread_barrier_t barrier; |
40 | |
41 | /* Forces multi-threaded behaviour. */ |
42 | static void * |
43 | blocked_thread_func (void *closure) |
44 | { |
45 | xpthread_barrier_wait (barrier: &barrier); |
46 | /* TLS load and access tests run here in the main thread. */ |
47 | xpthread_barrier_wait (barrier: &barrier); |
48 | return NULL; |
49 | } |
50 | |
51 | static void * |
52 | load_and_access (const char *mod, const char *func) |
53 | { |
54 | /* Load module with TLS. */ |
55 | void *p = xdlopen (filename: mod, RTLD_NOW); |
56 | /* Access the TLS variable to ensure it is allocated. */ |
57 | void (*f) (void) = (void (*) (void))xdlsym (handle: p, symbol: func); |
58 | f (); |
59 | return p; |
60 | } |
61 | |
62 | static int |
63 | do_test (void) |
64 | { |
65 | void *mods[6]; |
66 | |
67 | { |
68 | int ret = pthread_barrier_init (barrier: &barrier, NULL, count: 2); |
69 | if (ret != 0) |
70 | { |
71 | errno = ret; |
72 | printf (format: "error: pthread_barrier_init: %m\n" ); |
73 | exit (status: 1); |
74 | } |
75 | } |
76 | |
77 | pthread_t blocked_thread = xpthread_create (NULL, thread_func: blocked_thread_func, NULL); |
78 | xpthread_barrier_wait (barrier: &barrier); |
79 | |
80 | printf (format: "maintls[%zu]:\t %p .. %p\n" , |
81 | sizeof maintls, maintls, maintls + sizeof maintls); |
82 | memset (s: maintls, c: 1, n: sizeof maintls); |
83 | |
84 | /* Load modules with dynamic TLS (may use surplus static TLS |
85 | opportunistically). */ |
86 | mods[0] = load_and_access (mod: "tst-tls-ie-mod0.so" , func: "access0" ); |
87 | mods[1] = load_and_access (mod: "tst-tls-ie-mod1.so" , func: "access1" ); |
88 | mods[2] = load_and_access (mod: "tst-tls-ie-mod2.so" , func: "access2" ); |
89 | mods[3] = load_and_access (mod: "tst-tls-ie-mod3.so" , func: "access3" ); |
90 | /* Load modules with initial-exec TLS (can only use surplus static TLS). */ |
91 | mods[4] = load_and_access (mod: "tst-tls-ie-mod4.so" , func: "access4" ); |
92 | mods[5] = load_and_access (mod: "tst-tls-ie-mod5.so" , func: "access5" ); |
93 | |
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 (format: "The next dlopen should fail...\n" ); |
97 | void *p = dlopen (file: "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 (format: "...OK failed with: %s.\n" , dlerror ()); |
102 | |
103 | xpthread_barrier_wait (barrier: &barrier); |
104 | xpthread_join (thr: blocked_thread); |
105 | |
106 | /* Close the modules. */ |
107 | for (int i = 0; i < 6; ++i) |
108 | xdlclose (handle: mods[i]); |
109 | |
110 | return 0; |
111 | } |
112 | |