about summary refs log tree commit diff
path: root/elf/tst-dlopen-tlsreinitmod3.c
blob: ef769c51313a234ffa8c572668fff0eb1d4aeab1 (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
/* Test that dlopen preserves already accessed TLS (bug 31717), module 3.
   Copyright (C) 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/>.  */

#include <dlfcn.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

/* Used to verify from the main program that the test ran.  */
bool tlsreinitmod3_tested;

/* This TLS variable must not revert back to the initial state after
   dlopen.  */
static __thread int tlsreinitmod3_state = 1;

/* Set from the ELF constructor during dlopen.  */
static bool tlsreinitmod3_constructed;

/* Second half of test, behind a compiler barrier.  The compiler
   barrier is necessary to prevent carrying over TLS address
   information from call_tlsreinitmod3 to call_tlsreinitmod3_tail.  */
void call_tlsreinitmod3_tail (void *self) __attribute__ ((weak));

/* Called from tst-dlopen-tlsreinitmod2.so.  */
void
call_tlsreinitmod3 (void)
{
  printf ("info: call_tlsreinitmod3 invoked (state=%d)\n",
          tlsreinitmod3_state);

  if (tlsreinitmod3_constructed)
    {
      puts ("error: call_tlsreinitmod3 called after ELF constructor");
      fflush (stdout);
      /* Cannot rely on test harness due to dynamic linking.  */
      _exit (1);
    }

  tlsreinitmod3_state = 2;

  /* Self-dlopen.  This will run the ELF constructor.   */
  void *self = dlopen ("tst-dlopen-tlsreinitmod3.so", RTLD_NOW);
  if (self == NULL)
    {
      printf ("error: dlopen: %s\n", dlerror ());
      fflush (stdout);
      /* Cannot rely on test harness due to dynamic linking.  */
      _exit (1);
    }

  call_tlsreinitmod3_tail (self);
}

void
call_tlsreinitmod3_tail (void *self)
{
  printf ("info: dlopen returned in tlsreinitmod3 (state=%d)\n",
          tlsreinitmod3_state);

  if (!tlsreinitmod3_constructed)
    {
      puts ("error: dlopen did not call tlsreinitmod3 ELF constructor");
      fflush (stdout);
      /* Cannot rely on test harness due to dynamic linking.  */
      _exit (1);
    }

  if (tlsreinitmod3_state != 2)
    {
      puts ("error: TLS state reverted in tlsreinitmod3");
      fflush (stdout);
      /* Cannot rely on test harness due to dynamic linking.  */
      _exit (1);
    }

  dlclose (self);

  /* Signal test completion to the main program.  */
  tlsreinitmod3_tested = true;
}

static void __attribute__ ((constructor))
tlsreinitmod3_init (void)
{
  puts ("info: constructor of tst-dlopen-tlsreinitmod3.so invoked");
  tlsreinitmod3_constructed = true;
}