aboutsummaryrefslogtreecommitdiff
path: root/elf/tst-tls-ie.c
blob: 6b624793a2222519f9c172f53c0c9d83f2751b81 (plain)
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
/* Test dlopen of modules with initial-exec TLS.
   Copyright (C) 2016-2024 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/>.  */

/* This test tries to check that surplus static TLS is not used up for
   dynamic TLS optimizations and 3*192 + 4*144 = 1152 bytes of static
   TLS is available for dlopening modules with initial-exec TLS.  It
   depends on rtld.nns=4 and rtld.optional_static_tls=512 tunable setting.  */

#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int do_test (void);
#include <support/xthread.h>
#include <support/xdlfcn.h>
#include <support/check.h>
#include <support/test-driver.c>

/* Have some big TLS in the main exe: should not use surplus TLS.  */
__thread char maintls[1000];

static pthread_barrier_t barrier;

/* Forces multi-threaded behaviour.  */
static void *
blocked_thread_func (void *closure)
{
  xpthread_barrier_wait (&barrier);
  /* TLS load and access tests run here in the main thread.  */
  xpthread_barrier_wait (&barrier);
  return NULL;
}

static void *
load_and_access (const char *mod, const char *func)
{
  /* Load module with TLS.  */
  void *p = xdlopen (mod, RTLD_NOW);
  /* Access the TLS variable to ensure it is allocated.  */
  void (*f) (void) = (void (*) (void))xdlsym (p, func);
  f ();
  return p;
}

static int
do_test (void)
{
  void *mods[6];

  {
    int ret = pthread_barrier_init (&barrier, NULL, 2);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_barrier_init: %m\n");
        exit (1);
      }
  }

  pthread_t blocked_thread = xpthread_create (NULL, blocked_thread_func, NULL);
  xpthread_barrier_wait (&barrier);

  printf ("maintls[%zu]:\t %p .. %p\n",
	   sizeof maintls, maintls, maintls + sizeof maintls);
  memset (maintls, 1, sizeof maintls);

  /* Load modules with dynamic TLS (may use surplus static TLS
     opportunistically).  */
  mods[0] = load_and_access ("tst-tls-ie-mod0.so", "access0");
  mods[1] = load_and_access ("tst-tls-ie-mod1.so", "access1");
  mods[2] = load_and_access ("tst-tls-ie-mod2.so", "access2");
  mods[3] = load_and_access ("tst-tls-ie-mod3.so", "access3");
  /* Load modules with initial-exec TLS (can only use surplus static TLS).  */
  mods[4] = load_and_access ("tst-tls-ie-mod4.so", "access4");
  mods[5] = load_and_access ("tst-tls-ie-mod5.so", "access5");

  /* Here 1152 bytes of surplus static TLS is in use and at most 512 bytes
     are available (depending on TLS optimizations).  */
  printf ("The next dlopen should fail...\n");
  void *p = dlopen ("tst-tls-ie-mod6.so", RTLD_NOW);
  if (p != NULL)
    FAIL_EXIT1 ("error: expected dlopen to fail because there is "
		"not enough surplus static TLS.\n");
  printf ("...OK failed with: %s.\n", dlerror ());

  xpthread_barrier_wait (&barrier);
  xpthread_join (blocked_thread);

  /* Close the modules.  */
  for (int i = 0; i < 6; ++i)
    xdlclose (mods[i]);

  return 0;
}