summary refs log tree commit diff
path: root/malloc/tst-malloc-thread-fail.c
blob: 2ffe848107a62d6aaa66ecb08be07d46f8b98574 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* Test allocation function behavior on allocation failure.
   Copyright (C) 2015-2019 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; see the file COPYING.LIB.  If
   not, see <http://www.gnu.org/licenses/>.  */

/* This test case attempts to trigger various unusual conditions
   related to allocation failures, notably switching to a different
   arena, and falling back to mmap (via sysmalloc).  */

#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>

/* Wrapper for calloc with an optimization barrier.  */
static void *
__attribute__ ((noinline, noclone))
allocate_zeroed (size_t a, size_t b)
{
  return calloc (a, b);
}

/* System page size, as determined by sysconf (_SC_PAGE_SIZE).  */
static unsigned long page_size;

/* Test parameters. */
static size_t allocation_size;
static size_t alignment;
static enum {
  with_malloc,
  with_realloc,
  with_aligned_alloc,
  with_memalign,
  with_posix_memalign,
  with_valloc,
  with_pvalloc,
  with_calloc,
  last_allocation_function = with_calloc
} allocation_function;

/* True if an allocation function uses the alignment test
   parameter.  */
const static bool alignment_sensitive[last_allocation_function + 1] =
  {
    [with_aligned_alloc] = true,
    [with_memalign] = true,
    [with_posix_memalign] = true,
  };

/* Combined pointer/expected alignment result of an allocation
   function.  */
struct allocate_result {
  void *pointer;
  size_t alignment;
};

/* Call the allocation function specified by allocation_function, with
   allocation_size and alignment (if applicable) as arguments.  No
   alignment check.  */
static struct allocate_result
allocate_1 (void)
{
  switch (allocation_function)
    {
    case with_malloc:
      return (struct allocate_result)
        {malloc (allocation_size), _Alignof (max_align_t)};
    case with_realloc:
      {
        void *p = realloc (NULL, 16);
        void *q;
        if (p == NULL)
          q = NULL;
        else
          {
            q = realloc (p, allocation_size);
            if (q == NULL)
              free (p);
          }
        return (struct allocate_result) {q, _Alignof (max_align_t)};
      }
    case with_aligned_alloc:
      {
        void *p = aligned_alloc (alignment, allocation_size);
        return (struct allocate_result) {p, alignment};
      }
    case with_memalign:
      {
        void *p = memalign (alignment, allocation_size);
        return (struct allocate_result) {p, alignment};
      }
    case with_posix_memalign:
      {
        void *p;
        if (posix_memalign (&p, alignment, allocation_size))
          {
            if (errno == ENOMEM)
              p = NULL;
            else
              {
                printf ("error: posix_memalign (p, %zu, %zu): %m\n",
                        alignment, allocation_size);
                abort ();
              }
          }
        return (struct allocate_result) {p, alignment};
      }
    case with_valloc:
      {
        void *p = valloc (allocation_size);
        return (struct allocate_result) {p, page_size};
      }
    case with_pvalloc:
      {
        void *p = pvalloc (allocation_size);
        return (struct allocate_result) {p, page_size};
      }
    case with_calloc:
      {
        char *p = allocate_zeroed (1, allocation_size);
        /* Check for non-zero bytes.  */
        if (p != NULL)
          for (size_t i = 0; i < allocation_size; ++i)
            if (p[i] != 0)
              {
                printf ("error: non-zero byte at offset %zu\n", i);
                abort ();
              }
        return (struct allocate_result) {p, _Alignof (max_align_t)};
      }
    }
  abort ();
}

/* Call allocate_1 and perform the alignment check on the result.  */
static void *
allocate (void)
{
  struct allocate_result r = allocate_1 ();
  if ((((uintptr_t) r.pointer) & (r.alignment - 1)) != 0)
    {
      printf ("error: allocation function %d, size %zu not aligned to %zu\n",
              (int) allocation_function, allocation_size, r.alignment);
      abort ();
    }
  return r.pointer;
}

/* Barriers to synchronize thread creation and termination.  */
static pthread_barrier_t start_barrier;
static pthread_barrier_t end_barrier;

/* Thread function which performs the allocation test.  Called by
   pthread_create and from the main thread.  */
static void *
allocate_thread (void *closure)
{
  /* Wait for the creation of all threads.  */
  {
    int ret = pthread_barrier_wait (&start_barrier);
    if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
      {
        errno = ret;
        printf ("error: pthread_barrier_wait: %m\n");
        abort ();
      }
  }

  /* Allocate until we run out of memory, creating a single-linked
     list.  */
  struct list {
    struct list *next;
  };
  struct list *head = NULL;
  while (true)
    {
      struct list *e = allocate ();
      if (e == NULL)
        break;

      e->next = head;
      head = e;
    }

  /* Wait for the allocation of all available memory.  */
  {
    int ret = pthread_barrier_wait (&end_barrier);
    if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
      {
        errno = ret;
        printf ("error: pthread_barrier_wait: %m\n");
        abort ();
      }
  }

  /* Free the allocated memory.  */
  while (head != NULL)
    {
      struct list *next = head->next;
      free (head);
      head = next;
    }

  return NULL;
}

/* Number of threads (plus the main thread.  */
enum { thread_count = 8 };

/* Thread attribute to request creation of threads with a non-default
   stack size which is rather small.  This avoids interfering with the
   configured address space limit.  */
static pthread_attr_t small_stack;

/* Runs one test in multiple threads, all in a subprocess so that
   subsequent tests do not interfere with each other.  */
static void
run_one (void)
{
  /* Isolate the tests in a subprocess, so that we can start over
     from scratch.  */
  pid_t pid = fork ();
  if (pid == 0)
    {
      /* In the child process.  Create the allocation threads.  */
      pthread_t threads[thread_count];

      for (unsigned i = 0; i < thread_count; ++i)
        {
          int ret = pthread_create (threads + i, &small_stack, allocate_thread, NULL);
          if (ret != 0)
            {
              errno = ret;
              printf ("error: pthread_create: %m\n");
              abort ();
            }
        }

      /* Also run the test on the main thread.  */
      allocate_thread (NULL);

      for (unsigned i = 0; i < thread_count; ++i)
        {
          int ret = pthread_join (threads[i], NULL);
          if (ret != 0)
            {
              errno = ret;
              printf ("error: pthread_join: %m\n");
              abort ();
            }
        }
      _exit (0);
    }
  else if (pid < 0)
    {
      printf ("error: fork: %m\n");
      abort ();
    }

  /* In the parent process.  Wait for the child process to exit.  */
  int status;
  if (waitpid (pid, &status, 0) < 0)
    {
      printf ("error: waitpid: %m\n");
      abort ();
    }
  if (status != 0)
    {
      printf ("error: exit status %d from child process\n", status);
      exit (1);
    }
}

/* Run all applicable allocation functions for the current test
   parameters.  */
static void
run_allocation_functions (void)
{
  for (int af = 0; af <= last_allocation_function; ++af)
    {
      /* Run alignment-sensitive functions for non-default
         alignments.  */
      if (alignment_sensitive[af] != (alignment != 0))
        continue;
      allocation_function = af;
      run_one ();
    }
}

int
do_test (void)
{
  /* Limit the number of malloc arenas.  We use a very low number so
     that despute the address space limit configured below, all
     requested arenas a can be created.  */
  if (mallopt (M_ARENA_MAX, 2) == 0)
    {
      printf ("error: mallopt (M_ARENA_MAX) failed\n");
      return 1;
    }

  /* Determine the page size.  */
  {
    long ret = sysconf (_SC_PAGE_SIZE);
    if (ret < 0)
      {
        printf ("error: sysconf (_SC_PAGE_SIZE): %m\n");
        return 1;
      }
    page_size = ret;
  }

  /* Limit the size of the process, so that memory allocation in
     allocate_thread will eventually fail, without impacting the
     entire system.  */
  {
    struct rlimit limit;
    if (getrlimit (RLIMIT_AS, &limit) != 0)
      {
        printf ("getrlimit (RLIMIT_AS) failed: %m\n");
        return 1;
      }
    long target = 200 * 1024 * 1024;
    if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
      {
        limit.rlim_cur = target;
        if (setrlimit (RLIMIT_AS, &limit) != 0)
          {
            printf ("setrlimit (RLIMIT_AS) failed: %m\n");
            return 1;
          }
      }
  }

  /* Initialize thread attribute with a reduced stack size.  */
  {
    int ret = pthread_attr_init (&small_stack);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_attr_init: %m\n");
        abort ();
      }
    unsigned long stack_size = ((256 * 1024) / page_size) * page_size;
    if (stack_size < 4 * page_size)
      stack_size = 8 * page_size;
    ret = pthread_attr_setstacksize (&small_stack, stack_size);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_attr_setstacksize: %m\n");
        abort ();
      }
  }

  /* Initialize the barriers.  We run thread_count threads, plus 1 for
     the main thread.  */
  {
    int ret = pthread_barrier_init (&start_barrier, NULL, thread_count + 1);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_barrier_init: %m\n");
        abort ();
      }

    ret = pthread_barrier_init (&end_barrier, NULL, thread_count + 1);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_barrier_init: %m\n");
        abort ();
      }
  }

  allocation_size = 144;
  run_allocation_functions ();
  allocation_size = page_size;
  run_allocation_functions ();

  alignment = 128;
  allocation_size = 512;
  run_allocation_functions ();

  allocation_size = page_size;
  run_allocation_functions ();

  allocation_size = 17 * page_size;
  run_allocation_functions ();

  /* Deallocation the barriers and the thread attribute.  */
  {
    int ret = pthread_barrier_destroy (&end_barrier);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_barrier_destroy: %m\n");
        return 1;
      }
    ret = pthread_barrier_destroy (&start_barrier);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_barrier_destroy: %m\n");
        return 1;
      }
    ret = pthread_attr_destroy (&small_stack);
    if (ret != 0)
      {
        errno = ret;
        printf ("error: pthread_attr_destroy: %m\n");
        return 1;
      }
  }

  return 0;
}

/* The repeated allocations take some time on slow machines.  */
#define TIMEOUT 100

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