about summary refs log tree commit diff
path: root/stdlib/tst-tls-atexit.c
blob: c03a3e5ed37baf1e14722fd561a0567c38ce8328 (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
/* Verify that DSO is unloaded only if its TLS objects are destroyed.
   Copyright (C) 2013-2014 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/>.  */

/* There are two tests in this test case.  The first is implicit where it is
   assumed that the destructor call on exit of the LOAD function does not
   segfault.  The other is a verification that after the thread has exited, a
   dlclose will unload the DSO.  */

#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

void *handle;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;

void *
load (void *u)
{
  pthread_mutex_lock (&m);
  handle = dlopen ("$ORIGIN/tst-tls-atexit-lib.so", RTLD_LAZY);
  if (!handle)
    {
      printf ("Unable to load DSO: %s\n", dlerror ());
      return (void *) (uintptr_t) 1;
    }

  void (*foo) (void) = (void (*) (void)) dlsym(handle, "do_foo");

  if (!foo)
    {
      printf ("Unable to find symbol: %s\n", dlerror ());
      exit (1);
    }

  foo ();

  /* This should not unload the DSO.  If it does, then the thread exit will
     result in a segfault.  */
  dlclose (handle);
  pthread_mutex_unlock (&m);

  return NULL;
}

static int
do_test (void)
{
  pthread_t t;
  int ret;
  void *thr_ret;

  if ((ret = pthread_create (&t, NULL, load, NULL)) != 0)
    {
      printf ("pthread_create failed: %s\n", strerror (ret));
      return 1;
    }

  if ((ret = pthread_join (t, &thr_ret)) != 0)
    {
      printf ("pthread_create failed: %s\n", strerror (ret));
      return 1;
    }

  if (thr_ret != NULL)
    return 1;

  /* Now this should unload the DSO.  */
  dlclose (handle);

  /* Run through our maps and ensure that the DSO is unloaded.  */
  FILE *f = fopen ("/proc/self/maps", "r");

  if (f == NULL)
    {
      perror ("Failed to open /proc/self/maps");
      fprintf (stderr, "Skipping verification of DSO unload\n");
      return 0;
    }

  char *line = NULL;
  size_t s = 0;
  while (getline (&line, &s, f) > 0)
    {
      if (strstr (line, "tst-tls-atexit-lib.so"))
        {
	  printf ("DSO not unloaded yet:\n%s", line);
	  return 1;
	}
    }
  free (line);

  return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"