From fedeff7f201df0206eefb744ad125e44a63a3ea0 Mon Sep 17 00:00:00 2001 From: deadmanoz Date: Fri, 13 Mar 2026 14:50:41 +0800 Subject: [PATCH] crypto: disable ASan instrumentation of SSE4 SHA256 for GCC The existing Clang-only no_sanitize("address") guard is extended to also cover GCC. When GCC compiles this file with -fsanitize=address in debug builds, the instrumented inline assembly causes a SEGV during SHA256AutoDetect()'s self-test on CPUs that use the SSE4 code path (i.e. those without SHA-NI support), regardless of optimization level. The original Clang code placed the attribute between the function declarator and the opening brace. GCC's Attribute Syntax documentation notes that this position in a function definition "may, in future, be permitted," so it is not currently supported. The attribute is moved to the start of the function definition, which is valid form for both GCC and Clang. The preprocessor guards are restructured so each compiler branch is explicit: __clang__ with __has_feature, and __GNUC__ with __SANITIZE_ADDRESS__. --- src/crypto/sha256_sse4.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/crypto/sha256_sse4.cpp b/src/crypto/sha256_sse4.cpp index 2d37d124d1e..4464ec92439 100644 --- a/src/crypto/sha256_sse4.cpp +++ b/src/crypto/sha256_sse4.cpp @@ -12,18 +12,23 @@ namespace sha256_sse4 { -void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks) -#if defined(__clang__) - /* - clang is unable to compile this with -O0 and -fsanitize=address. - See upstream bug: https://github.com/llvm/llvm-project/issues/92182. - This also fails to compile with -O2, -fcf-protection & -fsanitize=address. - See https://github.com/bitcoin/bitcoin/issues/31913. - */ -#if __has_feature(address_sanitizer) +/* +Both Clang and GCC fail with ASan on this inline assembly: +- Clang: compile failure with -O0 or -O2 + -fcf-protection under ASan. + See https://github.com/llvm/llvm-project/issues/92182 + and https://github.com/bitcoin/bitcoin/issues/31913. +- GCC: runtime SEGV during SHA256AutoDetect()'s self-test under ASan, + regardless of optimization level. + See https://github.com/bitcoin/bitcoin/issues/34881. +*/ +#if defined(__SANITIZE_ADDRESS__) + __attribute__((no_sanitize("address"))) +#elif defined(__clang__) +#if __has_feature(address_sanitizer) // fallback can be removed once support for Clang 21 is dropped __attribute__((no_sanitize("address"))) #endif #endif +void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks) { static const uint32_t K256 alignas(16) [] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,