diff --git a/bsdkm/bsdkm_wc_port.h b/bsdkm/bsdkm_wc_port.h index ee67d4d4a0..2d295d6fab 100644 --- a/bsdkm/bsdkm_wc_port.h +++ b/bsdkm/bsdkm_wc_port.h @@ -207,9 +207,33 @@ extern struct malloc_type M_WOLFSSL[1]; typedef volatile int wolfSSL_Atomic_Int; typedef volatile unsigned int wolfSSL_Atomic_Uint; #define WOLFSSL_ATOMIC_INITIALIZER(x) (x) - #define WOLFSSL_ATOMIC_LOAD(x) (int)atomic_load_acq_int(&(x)) - #define WOLFSSL_ATOMIC_LOAD_UINT(x) atomic_load_acq_int(&(x)) - #define WOLFSSL_ATOMIC_STORE(x, v) atomic_store_rel_int(&(x), (v)) + + /* _Generic selectors dispatch on (x) + 0 so that qualifier handling is + * identical across pre- and post-DR 481 compilers (C17 settled that + * lvalue conversion strips qualifiers from the controlling expression; + * earlier GCC/clang did not) -- the typedefs are volatile-qualified, so + * bare (x) would match no arm on one compiler generation or the other. + * LOAD's result cast uses the same expression to yield the unqualified + * promoted type. The LOAD and STORE tables must stay member-congruent: + * extend both, in pairs, with each new width's own _acq/_rel primitive; + * no default arm -- an unlisted type is a deliberate compile error. + */ + #define WOLFSSL_ATOMIC_LOAD(x) ((__typeof__((x) + 0)) \ + _Generic((__typeof__((x) + 0))0, \ + int: atomic_load_acq_int((volatile u_int *)&(x)), \ + unsigned int: atomic_load_acq_int((volatile u_int *)&(x)), \ + long: atomic_load_acq_long((volatile u_long *)&(x)),\ + unsigned long: atomic_load_acq_long((volatile u_long *)&(x)))) + #define WOLFSSL_ATOMIC_STORE(x, v) _Generic((__typeof__((x) + 0))0, \ + int: atomic_store_rel_int((volatile u_int *)&(x), \ + (u_int)(v)), \ + unsigned int: atomic_store_rel_int((volatile u_int *)&(x), \ + (u_int)(v)), \ + long: atomic_store_rel_long((volatile u_long *)&(x),\ + (u_long)(v)), \ + unsigned long: atomic_store_rel_long((volatile u_long *)&(x),\ + (u_long)(v)) \ + ) #define WOLFSSL_ATOMIC_OPS #if defined(HAVE_FIPS) diff --git a/wolfcrypt/src/cpuid.c b/wolfcrypt/src/cpuid.c index d7ece10d89..39b45818b3 100644 --- a/wolfcrypt/src/cpuid.c +++ b/wolfcrypt/src/cpuid.c @@ -171,11 +171,7 @@ static WC_INLINE void cpuid_set_flags(void) { - #ifdef WOLFSSL_BSDKM - if (WOLFSSL_ATOMIC_LOAD_UINT(cpuid_flags) == WC_CPUID_INITIALIZER) { - #else if (WOLFSSL_ATOMIC_LOAD(cpuid_flags) == WC_CPUID_INITIALIZER) { - #endif cpuid_flags_t new_cpuid_flags = 0, old_cpuid_flags = WC_CPUID_INITIALIZER; int os_avx = cpuid_os_state_enabled(WC_XCR0_AVX); diff --git a/wolfssl/wolfcrypt/cpuid.h b/wolfssl/wolfcrypt/cpuid.h index 192e5cc84c..a4ea3391bf 100644 --- a/wolfssl/wolfcrypt/cpuid.h +++ b/wolfssl/wolfcrypt/cpuid.h @@ -233,11 +233,7 @@ typedef word32 cpuid_flags_t; * accurate. */ static WC_INLINE int cpuid_get_flags_atomic(cpuid_flags_atomic_t *flags) { - #ifdef WOLFSSL_BSDKM - if (WOLFSSL_ATOMIC_LOAD_UINT(*flags) == WC_CPUID_INITIALIZER) { - #else if (WOLFSSL_ATOMIC_LOAD(*flags) == WC_CPUID_INITIALIZER) { - #endif /* WOLFSSL_BSDKM */ cpuid_flags_t old_cpuid_flags = WC_CPUID_INITIALIZER; return wolfSSL_Atomic_Uint_CompareExchange (flags, &old_cpuid_flags, cpuid_get_flags());