about summary refs log tree commit diff
path: root/elf/tst-tls20.c
blob: 907dfe2ca2b3c235f5c47b2f667d14ebaffe4bd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Test dtv setup if entries don't have monotone increasing generation.
   Copyright (C) 2021-2023 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
   <http://www.gnu.org/licenses/>.  */

#include <array_length.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <support/check.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <support/xdlfcn.h>
#include <support/xthread.h>

#define NMOD 100
static void *mod[NMOD];

static void
load_fail (void)
{
  /* Expected to fail because of a missing symbol.  */
  void *m = dlopen ("tst-tls20mod-bad.so", RTLD_NOW);
  if (m != NULL)
    FAIL_EXIT1 ("dlopen of tst-tls20mod-bad.so succeeded\n");
}

static void
load_mod (int i)
{
  char *buf = xasprintf ("tst-tls-manydynamic%02dmod.so", i);
  mod[i] = xdlopen (buf, RTLD_LAZY);
  free (buf);
}

static void
unload_mod (int i)
{
  if (mod[i] != NULL)
    xdlclose (mod[i]);
  mod[i] = NULL;
}

static void
access (int i)
{
  char *buf = xasprintf ("tls_global_%02d", i);
  dlerror ();
  int *p = dlsym (mod[i], buf);
  if (test_verbose)
    printf ("mod[%d]: &tls = %p\n", i, p);
  if (p == NULL)
    FAIL_EXIT1 ("dlsym failed: %s\n", dlerror ());
  TEST_COMPARE (*p, 0);
  ++*p;
  free (buf);
}

static void
access_mod (const char *modname, void *mod, int i)
{
  char *modsym = xasprintf ("tls_global_%d", i);
  dlerror ();
  int *p = dlsym (mod, modsym);
  if (test_verbose)
    printf ("%s: &tls = %p\n", modname, p);
  if (p == NULL)
    FAIL_EXIT1 ("dlsym failed: %s\n", dlerror ());
  TEST_COMPARE (*p, 0);
  ++*p;
  free (modsym);
}

static void
access_dep (int i)
{
  char *modname = xasprintf ("tst-tls-manydynamic%dmod-dep.so", i);
  void *moddep = xdlopen (modname, RTLD_LAZY);
  access_mod (modname, moddep, i);
  free (modname);
  xdlclose (moddep);
}

struct start_args
{
  const char *modname;
  void *mod;
  int modi;
  int ndeps;
  const int *deps;
};

static void *
start (void *a)
{
  struct start_args *args = a;

  for (int i = 0; i < NMOD; i++)
    if (mod[i] != NULL)
      access (i);

  if (args != NULL)
    {
      access_mod (args->modname, args->mod, args->modi);
      for (int n = 0; n < args->ndeps; n++)
	access_dep (args->deps[n]);
    }

  return 0;
}

/* This test gaps with shared libraries with dynamic TLS that has no
   dependencies.  The DTV gap is set with by trying to load an invalid
   module, the entry should be used on the dlopen.  */
static void
do_test_no_depedency (void)
{
  for (int i = 0; i < NMOD; i++)
    {
      load_mod (i);
      /* Bump the generation of mod[0] without using new dtv slot.  */
      unload_mod (0);
      load_fail (); /* Ensure GL(dl_tls_dtv_gaps) is true: see bug 27135.  */
      load_mod (0);
      /* Access TLS in all loaded modules.  */
      pthread_t t = xpthread_create (0, start, 0);
      xpthread_join (t);
    }
  for (int i = 0; i < NMOD; i++)
    unload_mod (i);
}

/* The following test check DTV gaps handling with shared libraries that has
   dependencies.  It defines 5 different sets:

   1. Single dependency:
      mod0 -> mod1
   2. Double dependency:
      mod2 -> [mod3,mod4]
   3. Double dependency with each dependency dependent of another module:
      mod5 -> [mod6,mod7] -> mod8
   4. Long chain with one double dependency in the middle:
      mod9 -> [mod10, mod11] -> mod12 -> mod13
   5. Long chain with two double dependencies in the middle:
      mod14 -> mod15 -> [mod16, mod17]
      mod15 -> [mod18, mod19]

   This does not cover all the possible gaps and configuration, but it
   should check if different dynamic shared sets are placed correctly in
   different gaps configurations.  */

static int
nmodules (uint32_t v)
{
  unsigned int r = 0;
  while (v >>= 1)
    r++;
  return r + 1;
}

static inline bool
is_mod_set (uint32_t g, uint32_t n)
{
  return (1U << (n - 1)) & g;
}

static void
print_gap (uint32_t g)
{
  if (!test_verbose)
    return;
  printf ("gap: ");
  int nmods = nmodules (g);
  for (int n = 1; n <= nmods; n++)
    printf ("%c", ((1 << (n - 1)) & g) == 0 ? 'G' : 'M');
  printf ("\n");
}

static void
do_test_dependency (void)
{
  /* Maps the module and its dependencies, use thread to access the TLS on
     each loaded module.  */
  static const int tlsmanydeps0[] = { 1 };
  static const int tlsmanydeps1[] = { 3, 4 };
  static const int tlsmanydeps2[] = { 6, 7, 8 };
  static const int tlsmanydeps3[] = { 10, 11, 12 };
  static const int tlsmanydeps4[] = { 15, 16, 17, 18, 19 };
  static const struct tlsmanydeps_t
  {
    int modi;
    int ndeps;
    const int *deps;
  } tlsmanydeps[] =
  {
    {  0, array_length (tlsmanydeps0), tlsmanydeps0 },
    {  2, array_length (tlsmanydeps1), tlsmanydeps1 },
    {  5, array_length (tlsmanydeps2), tlsmanydeps2 },
    {  9, array_length (tlsmanydeps3), tlsmanydeps3 },
    { 14, array_length (tlsmanydeps4), tlsmanydeps4 },
  };

  /* The gap configuration is defined as a bitmap: the bit set represents a
     loaded module prior the tests execution, while a bit unset is a module
     unloaded.  Not all permtation will show gaps, but it is simpler than
     define each one independently.  */
  for (uint32_t g = 0; g < 64; g++)
    {
      print_gap (g);
      int nmods = nmodules (g);

      int mods[nmods];
      /* We use '0' as indication for a gap, to avoid the dlclose on iteration
	 cleanup.  */
      for (int n = 1; n < nmods; n++)
	{
	  load_mod (n);
	   mods[n] = n;
	}
      for (int n = 1; n < nmods; n++)
	{
	  if (!is_mod_set (g, n))
	    {
	      unload_mod (n);
	      mods[n] = 0;
	    }
	}

      for (int t = 0; t < array_length (tlsmanydeps); t++)
	{
	  char *moddepname = xasprintf ("tst-tls-manydynamic%dmod-dep.so",
					tlsmanydeps[t].modi);
	  void *moddep = xdlopen (moddepname, RTLD_LAZY);

	  /* Access TLS in all loaded modules.  */
	  struct start_args args =
	    {
	      moddepname,
	      moddep,
	      tlsmanydeps[t].modi,
	      tlsmanydeps[t].ndeps,
	      tlsmanydeps[t].deps
	    };
	  pthread_t t = xpthread_create (0, start, &args);
	  xpthread_join (t);

	  free (moddepname);
	  xdlclose (moddep);
	}

      for (int n = 1; n < nmods; n++)
	if (mods[n] != 0)
	  unload_mod (n);
    }
}

/* The following test check DTV gaps handling with shared libraries that has
   invalid dependencies.  It defines 5 different sets:

   1. Single dependency:
      mod0 -> invalid
   2. Double dependency:
      mod1 -> [mod2,invalid]
   3. Double dependency with each dependency dependent of another module:
      mod3 -> [mod4,mod5] -> invalid
   4. Long chain with one double dependency in the middle:
      mod6 -> [mod7, mod8] -> mod12 -> invalid
   5. Long chain with two double dependencies in the middle:
      mod10 -> mod11 -> [mod12, mod13]
      mod12 -> [mod14, invalid]

   This does not cover all the possible gaps and configuration, but it
   should check if different dynamic shared sets are placed correctly in
   different gaps configurations.  */

static void
do_test_invalid_dependency (bool bind_now)
{
  static const int tlsmanydeps[] = { 0, 1, 3, 6, 10 };

  /* The gap configuration is defined as a bitmap: the bit set represents a
     loaded module prior the tests execution, while a bit unset is a module
     unloaded.  Not all permtation will show gaps, but it is simpler than
     define each one independently.  */
  for (uint32_t g = 0; g < 64; g++)
    {
      print_gap (g);
      int nmods = nmodules (g);

      int mods[nmods];
      /* We use '0' as indication for a gap, to avoid the dlclose on iteration
	 cleanup.  */
      for (int n = 1; n < nmods; n++)
	{
	  load_mod (n);
	   mods[n] = n;
	}
      for (int n = 1; n < nmods; n++)
	{
	  if (!is_mod_set (g, n))
	    {
	      unload_mod (n);
	      mods[n] = 0;
	    }
	}

      for (int t = 0; t < array_length (tlsmanydeps); t++)
	{
	  char *moddepname = xasprintf ("tst-tls-manydynamic%dmod-dep-bad.so",
					tlsmanydeps[t]);
	  void *moddep;
	  if (bind_now)
	    {
	      moddep = dlopen (moddepname, RTLD_NOW);
	      TEST_VERIFY (moddep == 0);
	    }
	  else
	    moddep = dlopen (moddepname, RTLD_LAZY);

	  /* Access TLS in all loaded modules.  */
	  pthread_t t = xpthread_create (0, start, NULL);
	  xpthread_join (t);

	  free (moddepname);
	  if (!bind_now)
	    xdlclose (moddep);
	}

      for (int n = 1; n < nmods; n++)
	if (mods[n] != 0)
	  unload_mod (n);
    }
}

static int
do_test (void)
{
  do_test_no_depedency ();
  do_test_dependency ();
  do_test_invalid_dependency (true);
  do_test_invalid_dependency (false);

  return 0;
}

#include <support/test-driver.c>