summaryrefslogtreecommitdiff
path: root/malloc/tst-malloc-thread-exit.c
blob: fa6ebf98bfc6792dbe95ae48bdf954e618301ec7 (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
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
/* Test malloc with concurrent thread termination.
   Copyright (C) 2015-2016 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/>.  */

/* This thread spawns a number of outer threads, equal to the arena
   limit.  The outer threads run a loop which start and join two
   different kinds of threads: the first kind allocates (attaching an
   arena to the thread; malloc_first_thread) and waits, the second
   kind waits and allocates (wait_first_threads).  Both kinds of
   threads exit immediately after waiting.  The hope is that this will
   exhibit races in thread termination and arena management,
   particularly related to the arena free list.  */

#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static int do_test (void);

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

static bool termination_requested;
static int inner_thread_count = 4;
static size_t malloc_size = 32;

static void
__attribute__ ((noinline, noclone))
unoptimized_free (void *ptr)
{
  free (ptr);
}

static void *
malloc_first_thread (void * closure)
{
  pthread_barrier_t *barrier = closure;
  void *ptr = malloc (malloc_size);
  if (ptr == NULL)
    {
      printf ("error: malloc: %m\n");
      abort ();
    }
  int ret = pthread_barrier_wait (barrier);
  if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      errno = ret;
      printf ("error: pthread_barrier_wait: %m\n");
      abort ();
    }
  unoptimized_free (ptr);
  return NULL;
}

static void *
wait_first_thread (void * closure)
{
  pthread_barrier_t *barrier = closure;
  int ret = pthread_barrier_wait (barrier);
  if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      errno = ret;
      printf ("error: pthread_barrier_wait: %m\n");
      abort ();
    }
  void *ptr = malloc (malloc_size);
  if (ptr == NULL)
    {
      printf ("error: malloc: %m\n");
      abort ();
    }
  unoptimized_free (ptr);
  return NULL;
}

static void *
outer_thread (void *closure)
{
  pthread_t *threads = calloc (sizeof (*threads), inner_thread_count);
  if (threads == NULL)
    {
      printf ("error: calloc: %m\n");
      abort ();
    }

  while (!__atomic_load_n (&termination_requested, __ATOMIC_RELAXED))
    {
      pthread_barrier_t barrier;
      int ret = pthread_barrier_init (&barrier, NULL, inner_thread_count + 1);
      if (ret != 0)
        {
          errno = ret;
          printf ("pthread_barrier_init: %m\n");
          abort ();
        }
      for (int i = 0; i < inner_thread_count; ++i)
        {
          void *(*func) (void *);
          if ((i  % 2) == 0)
            func = malloc_first_thread;
          else
            func = wait_first_thread;
          ret = pthread_create (threads + i, NULL, func, &barrier);
          if (ret != 0)
            {
              errno = ret;
              printf ("error: pthread_create: %m\n");
              abort ();
            }
        }
      ret = pthread_barrier_wait (&barrier);
      if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
        {
          errno = ret;
          printf ("pthread_wait: %m\n");
          abort ();
        }
      for (int i = 0; i < inner_thread_count; ++i)
        {
          ret = pthread_join (threads[i], NULL);
          if (ret != 0)
            {
              ret = errno;
              printf ("error: pthread_join: %m\n");
              abort ();
            }
        }
      ret = pthread_barrier_destroy (&barrier);
      if (ret != 0)
        {
          ret = errno;
          printf ("pthread_barrier_destroy: %m\n");
          abort ();
        }
    }

  free (threads);

  return NULL;
}

static int
do_test (void)
{
  /* The number of threads should be smaller than the number of
     arenas, so that there will be some free arenas to add to the
     arena free list.  */
  enum { outer_thread_count = 2 };
  if (mallopt (M_ARENA_MAX, 8) == 0)
    {
      printf ("error: mallopt (M_ARENA_MAX) failed\n");
      return 1;
    }

  /* Leave some room for shutting down all threads gracefully.  */
  int timeout = 3;
  if (timeout > TIMEOUT)
    timeout = TIMEOUT - 1;

  pthread_t *threads = calloc (sizeof (*threads), outer_thread_count);
  if (threads == NULL)
    {
      printf ("error: calloc: %m\n");
      abort ();
    }

  for (long i = 0; i < outer_thread_count; ++i)
    {
      int ret = pthread_create (threads + i, NULL, outer_thread, NULL);
      if (ret != 0)
        {
          errno = ret;
          printf ("error: pthread_create: %m\n");
          abort ();
        }
    }

  struct timespec ts = {timeout, 0};
  if (nanosleep (&ts, NULL))
    {
      printf ("error: error: nanosleep: %m\n");
      abort ();
    }

  __atomic_store_n (&termination_requested, true, __ATOMIC_RELAXED);

  for (long i = 0; i < outer_thread_count; ++i)
    {
      int ret = pthread_join (threads[i], NULL);
      if (ret != 0)
        {
          errno = ret;
          printf ("error: pthread_join: %m\n");
          abort ();
        }
    }
  free (threads);

  return 0;
}