1 From cd84c46ce780242879e8aaa7d698b9cd87996dbd Mon Sep 17 00:00:00 2001
2 From: Colin Ian King <colin.i.king@gmail.com>
3 Date: Sun, 15 Oct 2023 15:50:07 +0100
4 Subject: [PATCH] core-*, stress-*: Add musl-gcc detection and
7 Detect for musl-gcc and define HAVE_COMPILER_MUSL and also define
8 HAVE_COMPILER_GCC_OR_MUSL for GCC or MUSL compilers. Allows one
9 to differentiate between gcc tool chains with and without glibc/musl
12 Fixes https://github.com/ColinIanKing/stress-ng/issues/325
14 Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
16 core-attribute.h | 56 ++++++++++++++++++++++----------------------
17 core-helper.c | 4 ++--
18 core-pragma.h | 18 +++++++-------
20 core-target-clones.h | 2 +-
21 core-vecmath.h | 4 ++--
22 stress-atomic.c | 4 ++--
23 stress-flushcache.c | 2 +-
24 stress-lockbus.c | 4 ++--
25 stress-malloc.c | 4 ++--
26 stress-memthrash.c | 12 +++++-----
27 stress-ng.h | 32 ++++++++++++++++---------
28 stress-regs.c | 8 +++----
29 stress-rseq.c | 14 +++++------
30 stress-vnni.c | 4 ++++
31 15 files changed, 92 insertions(+), 78 deletions(-)
33 --- a/core-attribute.h
34 +++ b/core-attribute.h
36 #define CORE_ATTRIBUTE_H
38 /* warn unused attribute */
39 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(4, 2, 0)) || \
40 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(4, 2, 0)) || \
41 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0))
42 #define WARN_UNUSED __attribute__((warn_unused_result))
46 #if defined(HAVE_ATTRIBUTE_FAST_MATH) && \
47 !defined(HAVE_COMPILER_ICC) && \
48 - defined(HAVE_COMPILER_GCC) && \
49 + defined(HAVE_COMPILER_GCC_OR_MUSL) && \
51 #define OPTIMIZE_FAST_MATH __attribute__((optimize("fast-math")))
57 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(2, 5, 0)) || \
58 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(2, 5, 0)) || \
59 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0))
60 #define NORETURN __attribute__((noreturn))
66 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(4, 0, 0)) || \
67 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(4, 0, 0)) || \
68 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 4, 0))
69 #define WEAK __attribute__((weak))
70 #define HAVE_WEAK_ATTRIBUTE
74 /* force inlining hint */
75 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(3, 4, 0) \
76 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(3, 4, 0) \
77 && ((!defined(__s390__) && !defined(__s390x__)) || NEED_GNUC(6, 0, 1))) || \
78 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0))
79 #define ALWAYS_INLINE __attribute__((always_inline))
83 /* force no inlining hint */
84 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(3, 4, 0)) || \
85 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(3, 4, 0)) || \
86 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0))
87 #define NOINLINE __attribute__((noinline))
92 /* -O3 attribute support */
93 -#if defined(HAVE_COMPILER_GCC) && \
94 - !defined(HAVE_COMPILER_CLANG) && \
95 - !defined(HAVE_COMPILER_ICC) && \
96 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
97 + !defined(HAVE_COMPILER_CLANG) && \
98 + !defined(HAVE_COMPILER_ICC) && \
100 #define OPTIMIZE3 __attribute__((optimize("-O3")))
105 /* -O2 attribute support */
106 -#if defined(HAVE_COMPILER_GCC) && \
107 - !defined(HAVE_COMPILER_CLANG) && \
108 - !defined(HAVE_COMPILER_ICC) && \
109 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
110 + !defined(HAVE_COMPILER_CLANG) && \
111 + !defined(HAVE_COMPILER_ICC) && \
113 #define OPTIMIZE2 __attribute__((optimize("-O2")))
118 /* -O1 attribute support */
119 -#if defined(HAVE_COMPILER_GCC) && \
120 - !defined(HAVE_COMPILER_CLANG) && \
121 - !defined(HAVE_COMPILER_ICC) && \
122 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
123 + !defined(HAVE_COMPILER_CLANG) && \
124 + !defined(HAVE_COMPILER_ICC) && \
126 #define OPTIMIZE1 __attribute__((optimize("-O1")))
131 /* -O0 attribute support */
132 -#if defined(HAVE_COMPILER_GCC) && \
133 - !defined(HAVE_COMPILER_ICC) && \
134 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
135 + !defined(HAVE_COMPILER_ICC) && \
137 #define OPTIMIZE0 __attribute__((optimize("-O0")))
138 #elif (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(10, 0, 0))
139 @@ -121,10 +121,10 @@
143 -#if ((defined(HAVE_COMPILER_GCC) && NEED_GNUC(3, 3, 0)) || \
144 - (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0)) || \
145 - (defined(HAVE_COMPILER_ICC) && NEED_ICC(2021, 0, 0))) && \
146 - !defined(HAVE_COMPILER_PCC) && \
147 +#if ((defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(3, 3, 0)) || \
148 + (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0)) || \
149 + (defined(HAVE_COMPILER_ICC) && NEED_ICC(2021, 0, 0))) && \
150 + !defined(HAVE_COMPILER_PCC) && \
152 #define ALIGNED(a) __attribute__((aligned(a)))
155 #define ALIGN64 ALIGNED(64)
158 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(4, 6, 0)) || \
159 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(4, 6, 0)) || \
160 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0))
161 #if (defined(__APPLE__) && defined(__MACH__))
162 #define SECTION(s) __attribute__((__section__(# s "," # s)))
166 /* GCC hot attribute */
167 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(4, 6, 0)) || \
168 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(4, 6, 0)) || \
169 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 3, 0))
170 #define HOT __attribute__((hot))
172 @@ -156,10 +156,10 @@
175 /* GCC mlocked data and data section attribute */
176 -#if ((defined(HAVE_COMPILER_GCC) && NEED_GNUC(4, 6, 0) || \
177 - (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0)))) && \
178 - !defined(__sun__) && \
179 - !defined(__APPLE__) && \
180 +#if ((defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(4, 6, 0) || \
181 + (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0)))) && \
182 + !defined(__sun__) && \
183 + !defined(__APPLE__) && \
184 !defined(BUILD_STATIC)
185 #define MLOCKED_TEXT __attribute__((__section__("mlocked_text")))
186 #define MLOCKED_SECTION (1)
190 /* print format attribute */
191 -#if ((defined(HAVE_COMPILER_GCC) && NEED_GNUC(3, 2, 0)) || \
192 +#if ((defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(3, 2, 0)) || \
193 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(3, 0, 0)))
194 #define FORMAT(func, a, b) __attribute__((format(func, a, b)))
198 @@ -3486,8 +3486,8 @@ void NORETURN MLOCKED_TEXT stress_sig_ha
200 * override stack smashing callback
202 -#if defined(HAVE_COMPILER_GCC) && \
203 - !defined(HAVE_COMPILER_CLANG) && \
204 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
205 + !defined(HAVE_COMPILER_CLANG) && \
206 defined(HAVE_WEAK_ATTRIBUTE)
207 extern void __stack_chk_fail(void);
212 #define STRESS_PRAGMA_(x) _Pragma (#x)
213 #define STRESS_PRAGMA(x) STRESS_PRAGMA_(x)
215 -#if defined(HAVE_PRAGMA_NO_HARD_DFP) && \
216 - defined(HAVE_COMPILER_GCC) && \
217 +#if defined(HAVE_PRAGMA_NO_HARD_DFP) && \
218 + defined(HAVE_COMPILER_GCC_OR_MUSL) && \
220 #define STRESS_PRAGMA_NO_HARD_DFP _Pragma("GCC target (\"no-hard-dfp\")")
223 #define STRESS_PRAGMA_PUSH _Pragma("GCC diagnostic push")
224 #define STRESS_PRAGMA_POP _Pragma("GCC diagnostic pop")
225 #define STRESS_PRAGMA_WARN_OFF _Pragma("GCC diagnostic ignored \"-Weverything\"")
226 -#elif defined(HAVE_COMPILER_GCC) && \
227 - defined(HAVE_PRAGMA) && \
228 +#elif defined(HAVE_COMPILER_GCC_OR_MUSL) && \
229 + defined(HAVE_PRAGMA) && \
231 #define STRESS_PRAGMA_PUSH _Pragma("GCC diagnostic push")
232 #define STRESS_PRAGMA_POP _Pragma("GCC diagnostic pop")
234 _Pragma("GCC diagnostic ignored \"-Wcast-qual\"") \
235 _Pragma("GCC diagnostic ignored \"-Wnonnull\"") \
236 _Pragma("GCC diagnostic ignored \"-Wstringop-overflow\"")
237 -#elif defined(HAVE_COMPILER_GCC) && \
238 - defined(HAVE_PRAGMA) && \
239 +#elif defined(HAVE_COMPILER_GCC_OR_MUSL) && \
240 + defined(HAVE_PRAGMA) && \
242 #define STRESS_PRAGMA_PUSH _Pragma("GCC diagnostic push")
243 #define STRESS_PRAGMA_POP _Pragma("GCC diagnostic pop")
245 NEED_CLANG(8, 0, 0) && \
247 #define STRESS_PRAGMA_WARN_CPP_OFF _Pragma("GCC diagnostic ignored \"-Wcpp\"")
248 -#elif defined(HAVE_COMPILER_GCC) && \
249 - defined(HAVE_PRAGMA) && \
250 +#elif defined(HAVE_COMPILER_GCC_OR_MUSL) && \
251 + defined(HAVE_PRAGMA) && \
253 #define STRESS_PRAGMA_WARN_CPP_OFF _Pragma("GCC diagnostic ignored \"-Wcpp\"")
257 #define PRAGMA_UNROLL_N(n) STRESS_PRAGMA(unroll n)
258 #define PRAGMA_UNROLL STRESS_PRAGMA(unroll)
259 -#elif defined(HAVE_COMPILER_GCC) && \
260 +#elif defined(HAVE_COMPILER_GCC_OR_MUSL) && \
262 #define PRAGMA_UNROLL_N(n) STRESS_PRAGMA(GCC unroll n)
263 #define PRAGMA_UNROLL STRESS_PRAGMA(GCC unroll 8)
266 @@ -494,7 +494,7 @@ int shim_getrandom(void *buff, size_t bu
268 void shim_flush_icache(void *begin, void *end)
270 -#if defined(HAVE_COMPILER_GCC) && \
271 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
272 defined(STRESS_ARCH_ARM)
273 __clear_cache(begin, end);
274 #elif defined(STRESS_ARCH_RISCV) && \
275 --- a/core-target-clones.h
276 +++ b/core-target-clones.h
280 #if defined(HAVE_TARGET_CLONES_GRANITERAPIDS) && \
281 - defined(HAVE_COMPILER_GCC)
282 + defined(HAVE_COMPILER_GCC_OR_MUSL)
283 #define TARGET_CLONE_GRANITERAPIDS "arch=graniterapids",
284 #define TARGET_CLONE_USE
289 * PPC64 for some reason with some flavours of the toolchain
290 * so disable this test for now
292 -#if defined(STRESS_ARCH_PPC64) && \
293 - defined(HAVE_COMPILER_GCC) && \
294 +#if defined(STRESS_ARCH_PPC64) && \
295 + defined(HAVE_COMPILER_GCC_OR_MUSL) && \
299 --- a/stress-atomic.c
300 +++ b/stress-atomic.c
301 @@ -71,7 +71,7 @@ typedef int (*atomic_func_t)(const stres
303 #if defined(HAVE_ATOMIC_FETCH_NAND)
304 #define HAVE_ATOMIC_OPS
305 -#if defined(HAVE_COMPILER_GCC) && __GNUC__ != 11
306 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && __GNUC__ != 11
307 #define SHIM_ATOMIC_FETCH_NAND(ptr, val, memorder) \
308 do { __atomic_fetch_nand(ptr, val, memorder); } while (0)
310 @@ -121,7 +121,7 @@ typedef int (*atomic_func_t)(const stres
312 #if defined(HAVE_ATOMIC_NAND_FETCH)
313 #define HAVE_ATOMIC_OPS
314 -#if defined(HAVE_COMPILER_GCC) && __GNUC__ != 11
315 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && __GNUC__ != 11
316 #define SHIM_ATOMIC_NAND_FETCH(ptr, val, memorder) \
317 do { __atomic_nand_fetch(ptr, val, memorder); } while (0)
319 --- a/stress-flushcache.c
320 +++ b/stress-flushcache.c
321 @@ -37,7 +37,7 @@ static const stress_help_t help[] = {
322 defined(STRESS_ARCH_S390) || \
323 defined(STRESS_ARCH_PPC64)) && \
324 defined(HAVE_MPROTECT) && \
325 - ((defined(HAVE_COMPILER_GCC) && NEED_GNUC(4,6,0)) || \
326 + ((defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(4,6,0)) || \
327 (defined(HAVE_COMPILER_CLANG) && NEED_CLANG(9,0,0)) || \
328 (defined(HAVE_COMPILER_ICX) && NEED_ICX(2023,2,0)) || \
329 (defined(HAVE_COMPILER_ICC) && NEED_ICC(2021,0,0)))
330 --- a/stress-lockbus.c
331 +++ b/stress-lockbus.c
332 @@ -37,14 +37,14 @@ static const stress_opt_set_func_t opt_s
336 -#if (((defined(HAVE_COMPILER_GCC) || \
337 +#if (((defined(HAVE_COMPILER_GCC_OR_MUSL) || \
338 defined(HAVE_COMPILER_CLANG) || \
339 defined(HAVE_COMPILER_ICC) || \
340 defined(HAVE_COMPILER_ICX) || \
341 defined(HAVE_COMPILER_TCC) || \
342 defined(HAVE_COMPILER_PCC)) && \
343 defined(STRESS_ARCH_X86)) || \
344 - (defined(HAVE_COMPILER_GCC) && \
345 + (defined(HAVE_COMPILER_GCC_OR_MUSL) && \
346 (defined(HAVE_ATOMIC_ADD_FETCH) || \
347 defined(HAVE_ATOMIC_FETCH_ADD)) && \
348 defined(__ATOMIC_SEQ_CST) && \
349 --- a/stress-malloc.c
350 +++ b/stress-malloc.c
351 @@ -453,8 +453,8 @@ static int stress_malloc(const stress_ar
352 malloc_max = MIN_MALLOC_MAX;
355 -#if defined(HAVE_COMPILER_GCC) && \
356 - defined(HAVE_MALLOPT) && \
357 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
358 + defined(HAVE_MALLOPT) && \
359 defined(M_MMAP_THRESHOLD)
361 size_t malloc_threshold = DEFAULT_MALLOC_THRESHOLD;
362 --- a/stress-memthrash.c
363 +++ b/stress-memthrash.c
364 @@ -94,12 +94,12 @@ static sigset_t set;
366 static stress_memthrash_primes_t stress_memthrash_primes[MEM_SIZE_PRIMES];
368 -#if (((defined(HAVE_COMPILER_GCC) || defined(HAVE_COMPILER_CLANG)) && \
369 - defined(STRESS_ARCH_X86)) || \
370 - (defined(HAVE_COMPILER_GCC) && \
371 - defined(HAVE_ATOMIC_ADD_FETCH) && \
372 - defined(__ATOMIC_SEQ_CST) && \
373 - NEED_GNUC(4,7,0) && \
374 +#if (((defined(HAVE_COMPILER_GCC_OR_MUSL) || defined(HAVE_COMPILER_CLANG)) && \
375 + defined(STRESS_ARCH_X86)) || \
376 + (defined(HAVE_COMPILER_GCC_OR_MUSL) && \
377 + defined(HAVE_ATOMIC_ADD_FETCH) && \
378 + defined(__ATOMIC_SEQ_CST) && \
379 + NEED_GNUC(4,7,0) && \
380 defined(STRESS_ARCH_ARM)))
381 #if defined(HAVE_ATOMIC_ADD_FETCH)
382 #define MEM_LOCK(ptr, inc) __atomic_add_fetch(ptr, inc, __ATOMIC_SEQ_CST)
393 +#if defined(HAVE_FEATURES_H)
394 +#include <features.h>
397 #if defined(__ICC) && \
398 defined(__INTEL_COMPILER)
399 /* Intel ICC compiler */
401 #elif defined(__clang__)
403 #define HAVE_COMPILER_CLANG
404 +#elif defined(__GNUC__) && \
405 + !defined(__USE_GNU)
407 +#define HAVE_COMPILER_MUSL
408 +#define HAVE_COMPILER_GCC_OR_MUSL
409 #elif defined(__GNUC__)
411 #define HAVE_COMPILER_GCC
412 +#define HAVE_COMPILER_GCC_OR_MUSL
418 +#ifndef _ATFILE_SOURCE
419 #define _ATFILE_SOURCE
421 #ifndef _LARGEFILE_SOURCE
422 #define _LARGEFILE_SOURCE
428 -#if defined(HAVE_FEATURES_H)
429 -#include <features.h>
431 #if defined(HAVE_LIB_PTHREAD)
436 #if defined(HAVE_SYS_SYSINFO_H)
437 #include <sys/sysinfo.h>
438 -#if defined(HAVE_COMPILER_GCC) && \
439 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
441 /* Suppress kernel sysinfo to avoid collision with musl */
442 #define _LINUX_SYSINFO_H
443 @@ -237,7 +247,7 @@ typedef struct stress_stressor_info {
445 #if defined(CHECK_UNEXPECTED) && \
446 defined(HAVE_PRAGMA) && \
447 - defined(HAVE_COMPILER_GCC)
448 + defined(HAVE_COMPILER_GCC_OR_MUSL)
449 #define UNEXPECTED_PRAGMA(x) _Pragma (#x)
450 #define UNEXPECTED_XSTR(x) UNEXPECTED_STR(x)
451 #define UNEXPECTED_STR(x) # x
452 @@ -427,7 +437,7 @@ typedef struct stressor_info {
455 /* gcc 4.7 and later support vector ops */
456 -#if defined(HAVE_COMPILER_GCC) && \
457 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
459 #define STRESS_VECTOR (1)
461 @@ -508,7 +518,7 @@ extern const char stress_config[];
462 #define PAGE_MAPPED (0x01)
463 #define PAGE_MAPPED_FAIL (0x02)
465 -#if defined(HAVE_COMPILER_GCC) || defined(HAVE_COMPILER_CLANG)
466 +#if defined(HAVE_COMPILER_GCC_OR_MUSL) || defined(HAVE_COMPILER_CLANG)
467 #define TYPEOF_CAST(a) (typeof(a))
469 #define TYPEOF_CAST(a)
470 @@ -839,7 +849,7 @@ extern void stress_metrics_set_const_che
472 #if !defined(STRESS_CORE_SHIM) && \
473 !defined(HAVE_PEDANTIC) && \
474 - (defined(HAVE_COMPILER_GCC) && defined(HAVE_COMPILER_CLANG))
475 + (defined(HAVE_COMPILER_GCC_OR_MUSL) && defined(HAVE_COMPILER_CLANG))
476 int unlink(const char *pathname) __attribute__((deprecated("use shim_unlink")));
477 int unlinkat(int dirfd, const char *pathname, int flags) __attribute__((deprecated("use shim_unlinkat")));
478 int rmdir(const char *pathname) __attribute__((deprecated("use shim_rmdir")));
481 @@ -33,10 +33,10 @@ static const stress_help_t help[] = {
485 -#if (defined(HAVE_COMPILER_GCC) && NEED_GNUC(8, 0, 0)) && \
486 - !defined(HAVE_COMPILER_CLANG) && \
487 - !defined(HAVE_COMPILER_ICC) && \
488 - !defined(HAVE_COMPILER_PCC) && \
489 +#if (defined(HAVE_COMPILER_GCC_OR_MUSL) && NEED_GNUC(8, 0, 0)) && \
490 + !defined(HAVE_COMPILER_CLANG) && \
491 + !defined(HAVE_COMPILER_ICC) && \
492 + !defined(HAVE_COMPILER_PCC) && \
493 !defined(HAVE_COMPILER_TCC)
495 static volatile uint32_t stash32;
498 @@ -32,13 +32,13 @@ static const stress_help_t help[] = {
502 -#if defined(HAVE_LINUX_RSEQ_H) && \
503 - defined(HAVE_ASM_NOP) && \
504 - defined(__NR_rseq) && \
505 - defined(HAVE_SYSCALL) && \
506 - defined(HAVE_COMPILER_GCC) && \
507 - !defined(HAVE_COMPILER_CLANG) && \
508 - !defined(HAVE_COMPILER_ICC) && \
509 +#if defined(HAVE_LINUX_RSEQ_H) && \
510 + defined(HAVE_ASM_NOP) && \
511 + defined(__NR_rseq) && \
512 + defined(HAVE_SYSCALL) && \
513 + defined(HAVE_COMPILER_GCC_OR_MUSL) && \
514 + !defined(HAVE_COMPILER_CLANG) && \
515 + !defined(HAVE_COMPILER_ICC) && \
516 !defined(HAVE_COMPILER_ICX)
518 #define STRESS_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
522 #include "core-pragma.h"
523 #include "core-target-clones.h"
525 +#if defined(HAVE_COMPILER_MUSL)
526 +#undef HAVE_IMMINTRIN_H
529 #if defined(HAVE_IMMINTRIN_H)
530 #include <immintrin.h>