1/* Bug 14333: Support file for atexit/exit, etc. race tests.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19/* This file must be run from within a directory called "stdlib". */
20
21/* The atexit/exit, at_quick_exit/quick_exit, __cxa_atexit/exit, etc.
22 exhibited data race while accessing destructor function list (Bug 14333).
23
24 This test spawns large number of threads, which all race to register
25 large number of destructors.
26
27 Before the fix, running this test resulted in a SIGSEGV.
28 After the fix, we expect clean process termination. */
29
30#if !defined(CALL_EXIT) || !defined(CALL_ATEXIT)
31#error Must define CALL_EXIT and CALL_ATEXIT before using this file.
32#endif
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <support/xthread.h>
37#include <limits.h>
38
39const size_t kNumThreads = 1024;
40const size_t kNumHandlers = 1024;
41
42static void *
43threadfunc (void *unused)
44{
45 size_t i;
46 for (i = 0; i < kNumHandlers; ++i) {
47 CALL_ATEXIT;
48 }
49 return NULL;
50}
51
52static int
53do_test (void)
54{
55 size_t i;
56 pthread_attr_t attr;
57
58 xpthread_attr_init (attr: &attr);
59 xpthread_attr_setdetachstate (attr: &attr, detachstate: 1);
60
61 /* With default 8MiB Linux stack size, creating 1024 threads can cause
62 VM exhausiton on 32-bit machines. Reduce stack size of each thread to
63 128KiB for a maximum required VM size of 128MiB. */
64 size_t kStacksize =
65#ifdef PTHREAD_STACK_MIN
66 0x20000 < PTHREAD_STACK_MIN ? PTHREAD_STACK_MIN :
67#endif
68 0x20000;
69
70 xpthread_attr_setstacksize (attr: &attr, stacksize: kStacksize);
71
72 for (i = 0; i < kNumThreads; ++i) {
73 xpthread_create (attr: &attr, thread_func: threadfunc, NULL);
74 }
75 xpthread_attr_destroy (attr: &attr);
76
77 CALL_EXIT;
78}
79
80#define TEST_FUNCTION do_test
81#include <support/test-driver.c>
82

source code of glibc/stdlib/test-atexit-race-common.c