debug: Reformat Makefile.
[glibc.git] / nptl / tst-pthread-defaultattr-free.c
blob9338ec6a4c2aad63b23f9b93b374647d135761d8
1 /* Test for user-after-free bug in pthread_getattr_default_np (bug 25999).
2 Copyright (C) 2020-2023 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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <pthread.h>
20 #include <sched.h>
21 #include <support/check.h>
22 #include <support/xthread.h>
24 static int
25 do_test (void)
27 /* This is a typical affinity size. */
28 enum { cpu_count = 128 };
29 cpu_set_t *set = CPU_ALLOC (cpu_count);
30 size_t set_size = CPU_ALLOC_SIZE (cpu_count);
31 CPU_ZERO_S (set_size, set);
32 CPU_SET (1, set);
33 CPU_SET (3, set);
35 /* Apply the affinity mask to the default attribute. */
36 pthread_attr_t attr;
37 xpthread_attr_init (&attr);
38 TEST_COMPARE (pthread_attr_setaffinity_np (&attr, set_size, set), 0);
39 TEST_COMPARE (pthread_setattr_default_np (&attr), 0);
40 xpthread_attr_destroy (&attr);
42 /* Read back the default attribute and check affinity mask. */
43 pthread_getattr_default_np (&attr);
44 CPU_ZERO_S (set_size, set);
45 TEST_COMPARE (pthread_attr_getaffinity_np (&attr, set_size, set), 0);
46 for (int i = 0; i < cpu_count; ++i)
47 TEST_COMPARE (!!CPU_ISSET (i, set), i == 1 || i == 3);
50 /* Apply a larger CPU affinity mask to the default attribute, to
51 trigger reallocation. */
53 cpu_set_t *large_set = CPU_ALLOC (4 * cpu_count);
54 size_t large_set_size = CPU_ALLOC_SIZE (4 * cpu_count);
55 CPU_ZERO_S (large_set_size, large_set);
56 pthread_attr_t large_attr;
57 xpthread_attr_init (&large_attr);
58 TEST_COMPARE (pthread_attr_setaffinity_np (&large_attr,
59 large_set_size, large_set), 0);
60 TEST_COMPARE (pthread_setattr_default_np (&large_attr), 0);
61 xpthread_attr_destroy (&large_attr);
62 CPU_FREE (large_set);
65 /* Read back the default attribute and check affinity mask. */
66 CPU_ZERO_S (set_size, set);
67 TEST_COMPARE (pthread_attr_getaffinity_np (&attr, set_size, set), 0);
68 for (int i = 0; i < cpu_count; ++i)
69 TEST_COMPARE (!!CPU_ISSET (i, set), i == 1 || i == 3);
70 /* Due to bug 25999, the following caused a double-free abort. */
71 xpthread_attr_destroy (&attr);
73 CPU_FREE (set);
75 return 0;
78 #include <support/test-driver.c>