]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/test-atexit-race-common.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / stdlib / test-atexit-race-common.c
1 /* Bug 14333: Support file for atexit/exit, etc. race tests.
2 Copyright (C) 2017-2019 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
39 const size_t kNumThreads = 1024;
40 const size_t kNumHandlers = 1024;
41 const size_t kStacksize =
42 #ifdef PTHREAD_STACK_MIN
43 0x20000 < PTHREAD_STACK_MIN ? PTHREAD_STACK_MIN :
44 #endif
45 0x20000;
46
47 static void *
48 threadfunc (void *unused)
49 {
50 size_t i;
51 for (i = 0; i < kNumHandlers; ++i) {
52 CALL_ATEXIT;
53 }
54 return NULL;
55 }
56
57 static int
58 do_test (void)
59 {
60 size_t i;
61 pthread_attr_t attr;
62
63 xpthread_attr_init (&attr);
64 xpthread_attr_setdetachstate (&attr, 1);
65
66 /* With default 8MiB Linux stack size, creating 1024 threads can cause
67 VM exhausiton on 32-bit machines. Reduce stack size of each thread to
68 128KiB for a maximum required VM size of 128MiB. */
69 xpthread_attr_setstacksize (&attr, kStacksize);
70
71 for (i = 0; i < kNumThreads; ++i) {
72 xpthread_create (&attr, threadfunc, NULL);
73 }
74 xpthread_attr_destroy (&attr);
75
76 CALL_EXIT;
77 }
78
79 #define TEST_FUNCTION do_test
80 #include <support/test-driver.c>