clangllvmfreebsdsanitize

Openssl stucks on SSL_library_init() when compiled on FreeBSD (clang9 and clang11) when using -fsanitize=address


I am getting my software stuck when starting Openssl on FreeBSD 13 (OpenSSL 1.1.1k-freebsd 25 Mar 2021) when using -fsanitize=address in clang11, same problem happens with clang9.

The problem happens indistinctly with:

To reproduce the problem:

#include <openssl/ssl.h>

int main(int argc, char *argv[]) {
    SSL_library_init();
    return EXIT_SUCCESS;
}

Then compile and execute:

clang  -lssl -lcrypto -fsanitize=address -o test test.c && ./test

Compilation happens fine but the program stuck on SSL_library_init() using 100% of CPU. Once the -fsanitize=address is removed everything works as expected.


Solution

  • Moved the solution by the OP to the Answer area.

    I backtraced the bug and this is not an OpenSSL bug, I have confirmed this is a LLVM bug as pointed in the comments:

    (gdb) bt
    #0  0x000000000027ffa7 in wrapped_qsort_compar (a=0x80087ed60, b=0x80087eef0)
        at /usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc:9741
    #1  0x0000000800d6e974 in qsort () from /lib/libc.so.7
    #2  0x0000000000280083 in __interceptor_qsort (base=<optimized out>, nmemb=44, size=80, 
        compar=<optimized out>)
        at /usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc:9761
    #3  0x0000000800d6f846 in qsort () from /lib/libc.so.7
    #4  0x0000000000280083 in __interceptor_qsort (base=<optimized out>, nmemb=164, size=80, 
        compar=<optimized out>)
        at /usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc:9761
    #5  0x000000080082b6af in ?? () from /usr/lib/libssl.so.111
    #6  0x000000080083120e in ?? () from /usr/lib/libssl.so.111
    #7  0x0000000800835f39 in ?? () from /usr/lib/libssl.so.111
    #8  0x0000000800b8fe23 in pthread_once () from /lib/libthr.so.3
    #9  0x00000008009a1019 in CRYPTO_THREAD_run_once () from /lib/libcrypto.so.111
    #10 0x0000000800835ce2 in OPENSSL_init_ssl () from /usr/lib/libssl.so.111
    #11 0x00000000002b37a9 in main ()
    (gdb) 
    ```