cwinapiopensslsegmentation-faultwinsock2

How to FIX printf resulted SIGSEGV when compiled with openssl?


I am trying to compile my code with openssl library. My code is printf then the program received SIGSEGV signal.

Code of test.c:

#include <winsock2.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

int main(int argc, char const *argv[]) {
    SSL_load_error_strings();
    OpenSSL_add_ssl_algorithms();
    printf("console\n");
    return 0;
}

Then I am using command prompt to compile my code

gcc test.c -Ipath/to/openssl/include -L path/to/openssl -l:libssl.a -l:libcrypto.a -lws2_32 -lcrypt32 -lucrt -o app

When debugging with gdb resulting backtrace:

#0  0x00007ffa2dc5b057 in msvcrt!_lock () from C:\Windows\System32\msvcrt.dll
#1  0x00007ff65bb46759 in _lock_file ()
#2  0x00007ff65bb40d73 in __mingw_vfprintf ()
#3  0x00007ff65b761717 in printf ()
#4  0x00007ff65b761765 in main ()

The result that I want is:

console

More information:

UPDATE INFORMATION
For some reason I need to give ldd information
The executable result

$ objdump -p app_name | findstr "DLL Name:"
        DLL Name: ADVAPI32.dll
        DLL Name: CRYPT32.dll
        DLL Name: KERNEL32.dll
        DLL Name: msvcrt.dll
        DLL Name: api-ms-win-crt-convert-l1-1-0.dll
        DLL Name: api-ms-win-crt-environment-l1-1-0.dll
        DLL Name: api-ms-win-crt-filesystem-l1-1-0.dll
        DLL Name: api-ms-win-crt-heap-l1-1-0.dll
        DLL Name: api-ms-win-crt-private-l1-1-0.dll
        DLL Name: api-ms-win-crt-runtime-l1-1-0.dll
        DLL Name: api-ms-win-crt-stdio-l1-1-0.dll
        DLL Name: api-ms-win-crt-string-l1-1-0.dll
        DLL Name: api-ms-win-crt-time-l1-1-0.dll
        DLL Name: api-ms-win-crt-utility-l1-1-0.dll
        DLL Name: USER32.dll
        DLL Name: WS2_32.dll

The libssl-3-x64.dll result

$ objdump -p libssl-3-x64.dll | findstr "DLL Name:"
        DLL Name: libcrypto-3-x64.dll
        DLL Name: KERNEL32.dll
        DLL Name: api-ms-win-crt-convert-l1-1-0.dll
        DLL Name: api-ms-win-crt-environment-l1-1-0.dll
        DLL Name: api-ms-win-crt-filesystem-l1-1-0.dll
        DLL Name: api-ms-win-crt-heap-l1-1-0.dll
        DLL Name: api-ms-win-crt-private-l1-1-0.dll
        DLL Name: api-ms-win-crt-runtime-l1-1-0.dll
        DLL Name: api-ms-win-crt-stdio-l1-1-0.dll
        DLL Name: api-ms-win-crt-string-l1-1-0.dll
        DLL Name: api-ms-win-crt-time-l1-1-0.dll
        DLL Name: api-ms-win-crt-utility-l1-1-0.dll
        DLL Name: WS2_32.dll

The libcrypto-3-x64.dll result

$ objdump -p libcrypto-3-x64.dll | findstr "DLL Name:"
        DLL Name: ADVAPI32.dll
        DLL Name: CRYPT32.dll
        DLL Name: KERNEL32.dll
        DLL Name: api-ms-win-crt-convert-l1-1-0.dll
        DLL Name: api-ms-win-crt-environment-l1-1-0.dll
        DLL Name: api-ms-win-crt-filesystem-l1-1-0.dll
        DLL Name: api-ms-win-crt-heap-l1-1-0.dll
        DLL Name: api-ms-win-crt-private-l1-1-0.dll
        DLL Name: api-ms-win-crt-runtime-l1-1-0.dll
        DLL Name: api-ms-win-crt-stdio-l1-1-0.dll
        DLL Name: api-ms-win-crt-string-l1-1-0.dll
        DLL Name: api-ms-win-crt-time-l1-1-0.dll
        DLL Name: api-ms-win-crt-utility-l1-1-0.dll
        DLL Name: USER32.dll
        DLL Name: WS2_32.dll

Solution

  • As Andrew already wrote in the comments, the crash is likely caused by an ABI incompatibility due to linking two different UCRTs.

    "I am using ucrt of msys for make-ing the openssl library".

    This probably isn't the same UCRT as the one your app is linked against which would explain the crash. In general, you want to use the same compiler and the same dependencies for everything you link together to ensure it's all compatible.

    If you just want OpenSSL working with your code, you can choose a binary distribution from here.
    Do note that, while they are listed on the official OpenSSL GitHub repository, they "have not been evaluated or tested by the OpenSSL project". Use at your own risk!