1 /* Test allocation function behavior on allocation failure.
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
19 /* This test case attempts to trigger various unusual conditions
20 related to allocation failures, notably switching to a different
21 arena, and falling back to mmap (via sysmalloc). */
30 #include <sys/resource.h>
34 /* Wrapper for calloc with an optimization barrier. */
36 __attribute__ ((noinline
, noclone
))
37 allocate_zeroed (size_t a
, size_t b
)
42 /* System page size, as determined by sysconf (_SC_PAGE_SIZE). */
43 static unsigned long page_size
;
45 /* Test parameters. */
46 static size_t allocation_size
;
47 static size_t alignment
;
57 last_allocation_function
= with_calloc
58 } allocation_function
;
60 /* True if an allocation function uses the alignment test
62 const static bool alignment_sensitive
[last_allocation_function
+ 1] =
64 [with_aligned_alloc
] = true,
65 [with_memalign
] = true,
66 [with_posix_memalign
] = true,
69 /* Combined pointer/expected alignment result of an allocation
71 struct allocate_result
{
76 /* Call the allocation function specified by allocation_function, with
77 allocation_size and alignment (if applicable) as arguments. No
79 static struct allocate_result
82 switch (allocation_function
)
85 return (struct allocate_result
)
86 {malloc (allocation_size
), _Alignof (max_align_t
)};
89 void *p
= realloc (NULL
, 16);
95 q
= realloc (p
, allocation_size
);
99 return (struct allocate_result
) {q
, _Alignof (max_align_t
)};
101 case with_aligned_alloc
:
103 void *p
= aligned_alloc (alignment
, allocation_size
);
104 return (struct allocate_result
) {p
, alignment
};
108 void *p
= memalign (alignment
, allocation_size
);
109 return (struct allocate_result
) {p
, alignment
};
111 case with_posix_memalign
:
114 if (posix_memalign (&p
, alignment
, allocation_size
))
120 printf ("error: posix_memalign (p, %zu, %zu): %m\n",
121 alignment
, allocation_size
);
125 return (struct allocate_result
) {p
, alignment
};
129 void *p
= valloc (allocation_size
);
130 return (struct allocate_result
) {p
, page_size
};
134 void *p
= pvalloc (allocation_size
);
135 return (struct allocate_result
) {p
, page_size
};
139 char *p
= allocate_zeroed (1, allocation_size
);
140 /* Check for non-zero bytes. */
142 for (size_t i
= 0; i
< allocation_size
; ++i
)
145 printf ("error: non-zero byte at offset %zu\n", i
);
148 return (struct allocate_result
) {p
, _Alignof (max_align_t
)};
154 /* Call allocate_1 and perform the alignment check on the result. */
158 struct allocate_result r
= allocate_1 ();
159 if ((((uintptr_t) r
.pointer
) & (r
.alignment
- 1)) != 0)
161 printf ("error: allocation function %d, size %zu not aligned to %zu\n",
162 (int) allocation_function
, allocation_size
, r
.alignment
);
168 /* Barriers to synchronize thread creation and termination. */
169 static pthread_barrier_t start_barrier
;
170 static pthread_barrier_t end_barrier
;
172 /* Thread function which performs the allocation test. Called by
173 pthread_create and from the main thread. */
175 allocate_thread (void *closure
)
177 /* Wait for the creation of all threads. */
179 int ret
= pthread_barrier_wait (&start_barrier
);
180 if (ret
!= 0 && ret
!= PTHREAD_BARRIER_SERIAL_THREAD
)
183 printf ("error: pthread_barrier_wait: %m\n");
188 /* Allocate until we run out of memory, creating a single-linked
193 struct list
*head
= NULL
;
196 struct list
*e
= allocate ();
204 /* Wait for the allocation of all available memory. */
206 int ret
= pthread_barrier_wait (&end_barrier
);
207 if (ret
!= 0 && ret
!= PTHREAD_BARRIER_SERIAL_THREAD
)
210 printf ("error: pthread_barrier_wait: %m\n");
215 /* Free the allocated memory. */
218 struct list
*next
= head
->next
;
226 /* Number of threads (plus the main thread. */
227 enum { thread_count
= 8 };
229 /* Thread attribute to request creation of threads with a non-default
230 stack size which is rather small. This avoids interfering with the
231 configured address space limit. */
232 static pthread_attr_t small_stack
;
234 /* Runs one test in multiple threads, all in a subprocess so that
235 subsequent tests do not interfere with each other. */
239 /* Isolate the tests in a subprocess, so that we can start over
244 /* In the child process. Create the allocation threads. */
245 pthread_t threads
[thread_count
];
247 for (unsigned i
= 0; i
< thread_count
; ++i
)
249 int ret
= pthread_create (threads
+ i
, &small_stack
, allocate_thread
, NULL
);
253 printf ("error: pthread_create: %m\n");
258 /* Also run the test on the main thread. */
259 allocate_thread (NULL
);
261 for (unsigned i
= 0; i
< thread_count
; ++i
)
263 int ret
= pthread_join (threads
[i
], NULL
);
267 printf ("error: pthread_join: %m\n");
275 printf ("error: fork: %m\n");
279 /* In the parent process. Wait for the child process to exit. */
281 if (waitpid (pid
, &status
, 0) < 0)
283 printf ("error: waitpid: %m\n");
288 printf ("error: exit status %d from child process\n", status
);
293 /* Run all applicable allocation functions for the current test
296 run_allocation_functions (void)
298 for (int af
= 0; af
<= last_allocation_function
; ++af
)
300 /* Run alignment-sensitive functions for non-default
302 if (alignment_sensitive
[af
] != (alignment
!= 0))
304 allocation_function
= af
;
312 /* Limit the number of malloc arenas. We use a very low number so
313 that despute the address space limit configured below, all
314 requested arenas a can be created. */
315 if (mallopt (M_ARENA_MAX
, 2) == 0)
317 printf ("error: mallopt (M_ARENA_MAX) failed\n");
321 /* Determine the page size. */
323 long ret
= sysconf (_SC_PAGE_SIZE
);
326 printf ("error: sysconf (_SC_PAGE_SIZE): %m\n");
332 /* Limit the size of the process, so that memory allocation in
333 allocate_thread will eventually fail, without impacting the
337 if (getrlimit (RLIMIT_AS
, &limit
) != 0)
339 printf ("getrlimit (RLIMIT_AS) failed: %m\n");
342 long target
= 200 * 1024 * 1024;
343 if (limit
.rlim_cur
== RLIM_INFINITY
|| limit
.rlim_cur
> target
)
345 limit
.rlim_cur
= target
;
346 if (setrlimit (RLIMIT_AS
, &limit
) != 0)
348 printf ("setrlimit (RLIMIT_AS) failed: %m\n");
354 /* Initialize thread attribute with a reduced stack size. */
356 int ret
= pthread_attr_init (&small_stack
);
360 printf ("error: pthread_attr_init: %m\n");
363 unsigned long stack_size
= ((256 * 1024) / page_size
) * page_size
;
364 if (stack_size
< 4 * page_size
)
365 stack_size
= 8 * page_size
;
366 ret
= pthread_attr_setstacksize (&small_stack
, stack_size
);
370 printf ("error: pthread_attr_setstacksize: %m\n");
375 /* Initialize the barriers. We run thread_count threads, plus 1 for
378 int ret
= pthread_barrier_init (&start_barrier
, NULL
, thread_count
+ 1);
382 printf ("error: pthread_barrier_init: %m\n");
386 ret
= pthread_barrier_init (&end_barrier
, NULL
, thread_count
+ 1);
390 printf ("error: pthread_barrier_init: %m\n");
395 allocation_size
= 144;
396 run_allocation_functions ();
397 allocation_size
= page_size
;
398 run_allocation_functions ();
401 allocation_size
= 512;
402 run_allocation_functions ();
404 allocation_size
= page_size
;
405 run_allocation_functions ();
407 allocation_size
= 17 * page_size
;
408 run_allocation_functions ();
410 /* Deallocation the barriers and the thread attribute. */
412 int ret
= pthread_barrier_destroy (&end_barrier
);
416 printf ("error: pthread_barrier_destroy: %m\n");
419 ret
= pthread_barrier_destroy (&start_barrier
);
423 printf ("error: pthread_barrier_destroy: %m\n");
426 ret
= pthread_attr_destroy (&small_stack
);
430 printf ("error: pthread_attr_destroy: %m\n");
438 /* The repeated allocations take some time on slow machines. */
441 #define TEST_FUNCTION do_test ()
442 #include "../test-skeleton.c"