Repository: bitcoin/bitcoin (language: cpp)

Issue:
Compilation failure with `-O0` + `-fsanitize=address`  due to inline asm
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current behaviour

Configure with
```
./configure --enable-fuzz --enable-debug --with-sanitizers=address,fuzzer,undefined,integer CC=clang CXX=clang++
```

Compile fails:
```
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:1882: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:2591: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:3284: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:4686: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:5395: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:6088: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:7490: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:8199: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:8892: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:10312: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:11021: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:11714: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:12859: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:13356: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:13853: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:14900: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:15397: note: instantiated into assembly here
crypto/sha256_sse4.cpp:44:9: error: expected relocatable expression
   44 |         "shl    $0x6,%2;"
      |         ^
<inline asm>:1:15894: note: instantiated into assembly here
18 errors generated.
make[2]: *** [Makefile:14531: crypto/libbitcoin_crypto_base_la-sha256_sse4.lo] Error 1
```

### Expected behaviour

It should successfully compile with the debug symbols so that fuzz crashes can be debugged.

Previously, it was possible to work around this issue by using `--disable-asm` however that was removed in #29407

### Steps to reproduce

See above configure .

`--enable-debug` and `--enable-fuzz` individually do not result in compilation failure. I am only seeing this when using them together.

### Relevant log output

[config.log](https://github.com/bitcoin/bitcoin/files/14854906/config.log)


### How did you obtain Bitcoin Core

Compiled from source

### What version of Bitcoin Core are you using?

master@0d509bab45d292caeaf34600e57b5928757c6005

### Operating system and version

Arch w/ linux kernel 6.8.2

### Machine specifications

_No response_


Discussion / hints:
Recalling the IRC discussion, I presume this happens since `-O0` in https://github.com/bitcoin/bitcoin/pull/16435 ?
Ref:

```
1802024-04-01T18:09:22  <cfields> clang++ -std=c++20 -O0 -fsanitize=address -c crypto/sha256_sse4.cpp -o out.o
```

https://www.erisian.com.au/bitcoin-core-dev/log-2024-04-02.html
The broken combination is `clang++ -std=c++20 -O0 -fsanitize=address -c crypto/sha256_sse4.cpp -o out.o`, pointed out by Cory. Happens for me with Clang-18.
I guess the next step is to followup / file an issue with LLVM. I've got #29796 open which may "fix" this by just dropping `-O0`, depending on the outcome of discussion.
FWIW, unable to reproduce on arm64 macOS 14.4.1 with Homebrew clang 17.0.6.

It only happens on x86-64, because the asm is in that format.
In addition to the discussion in #29781, I'll PR a change to make this work with `DISABLE_OPTIMIZED_SHA256` as a fallback. I'm doing that as part of a larger refactor of the crypto defines, though.
How about adding something like:

```diff
--- a/configure.ac
+++ b/configure.ac
@@ -349,7 +349,14 @@ esac
 if test "$enable_debug" = "yes"; then
 
   dnl Disable all optimizations
-  AX_CHECK_COMPILE_FLAG([-O0], [DEBUG_CXXFLAGS="$DEBUG_CXXFLAGS -O0"], [], [$CXXFLAG_WERROR])
+  case "$use_sanitizers" in
+    *address*)
+      AX_CHECK_COMPILE_FLAG([-Og], [DEBUG_CXXFLAGS="$DEBUG_CXXFLAGS -Og"], [], [$CXXFLAG_WERROR])
+      ;;
+    *)
+      AX_CHECK_COMPILE_FLAG([-O0], [DEBUG_CXXFLAGS="$DEBUG_CXXFLAGS -O0"], [], [$CXXFLAG_WERROR])
+      ;;
+  esac
```

in the meantime?

Fix patch:
diff --git a/src/crypto/sha256_sse4.cpp b/src/crypto/sha256_sse4.cpp
index f4557291cefe5..f0e255a23cb56 100644
--- a/src/crypto/sha256_sse4.cpp
+++ b/src/crypto/sha256_sse4.cpp
@@ -13,6 +13,13 @@
 namespace sha256_sse4
 {
 void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
+#if defined(__clang__) && !defined(__OPTIMIZE__)
+  /*
+  clang is unable to compile this with -O0 and -fsanitize=address.
+  See upstream bug: https://github.com/llvm/llvm-project/issues/92182
+  */
+  __attribute__((no_sanitize("address")))
+#endif
 {
     static const uint32_t K256 alignas(16) [] = {
         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,


Test patch: