1 /* Test for user-after-free bug in pthread_getattr_default_np (bug 25999).
2 Copyright (C) 2020-2022 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/>. */
21 #include <support/check.h>
22 #include <support/xthread.h>
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
);
35 /* Apply the affinity mask to the default attribute. */
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
);
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
);
78 #include <support/test-driver.c>