randombit / botan

Cryptography Toolkit
https://botan.randombit.net
BSD 2-Clause "Simplified" License
2.58k stars 568 forks source link

Segmentation fault in botan-test.exe #289

Closed nazar554 closed 9 years ago

nazar554 commented 9 years ago

I am trying to package botan for MSYS2 (MinGW-Packages). Currently it builds OK but the tests fail. The problem is somewhere in ffi.cpp. Looks like a double delete in destructor. I see this in both debug and release builds (64-bit).

Backtrace

(gdb) Starting program: D:\MINGW-packages\mingw-w64-libbotan\src\Botan-1.11.20\botan-test.exe 
[New Thread 7156.0x1988]
[New Thread 7156.0x754]
[New Thread 7156.0x19b4]
[New Thread 7156.0x3f8]

Program received signal SIGSEGV, Segmentation fault.
0x0000000000620e7b in std::default_delete<Botan::PK_Ops::Verification>::operator() (this=0x250000, __ptr=0x2078746341)
    at G:/msys64/mingw64/include/c++/5.2.0/bits/unique_ptr.h:76
76      delete __ptr;
(gdb) #0  0x0000000000620e7b in std::default_delete<Botan::PK_Ops::Verification>::operator() (this=0x250000, __ptr=0x2078746341)
    at G:/msys64/mingw64/include/c++/5.2.0/bits/unique_ptr.h:76
#1  0x000000000064b709 in std::unique_ptr<Botan::PK_Ops::Verification, std::default_delete<Botan::PK_Ops::Verification> >::~unique_ptr (this=0x250000, 
    __in_chrg=<optimized out>)
    at G:/msys64/mingw64/include/c++/5.2.0/bits/unique_ptr.h:236
#2  0x00000000005a1ec8 in Botan::PK_Verifier::~PK_Verifier (this=0x250000, 
    __in_chrg=<optimized out>) at build/include/botan/pubkey.h:204
#3  0x0000000000620507 in std::default_delete<Botan::PK_Verifier>::operator()
    (this=0x7ff5ffffc008, __ptr=0x250000)
    at G:/msys64/mingw64/include/c++/5.2.0/bits/unique_ptr.h:76
#4  0x0000000000648d52 in std::unique_ptr<Botan::PK_Verifier, std::default_delete<Botan::PK_Verifier> >::reset (this=0x7ff5ffffc008, __p=0x250000)
    at G:/msys64/mingw64/include/c++/5.2.0/bits/unique_ptr.h:344
#5  0x00000000004d3dd1 in (anonymous namespace)::botan_struct<Botan::PK_Verifier, 730986806u>::~botan_struct (this=0x7ff5ffffc000, __in_chrg=<optimized out>)
    at src/lib/ffi/ffi.cpp:55
#6  0x000000000059eae8 in botan_pk_op_verify_struct::~botan_pk_op_verify_struct (this=0x7ff5ffffc000, __in_chrg=<optimized out>) at src/lib/ffi/ffi.cpp:162
#7  0x00000000004d338e in botan_pk_op_verify_destroy (op=0x7ff5ffffc000)
    at src/lib/ffi/ffi.cpp:1059
#8  0x000000000042c36e in ____C_A_T_C_H____T_E_S_T____254 ()
    at src/tests/test_ffi.cpp:337
#9  0x0000000000610d36 in Catch::FreeFunctionTestCase::invoke (this=0x69041a0)
    at src/tests/catchy/catch.hpp:5810
#10 0x000000000040484d in Catch::TestCase::invoke (this=0x6921a08)
    at src/tests/catchy/catch.hpp:6715
#11 0x00000000005c425c in Catch::RunContext::invokeActiveTestCase (
    this=0x24eab0) at src/tests/catchy/catch.hpp:5412
#12 0x00000000005c3a13 in Catch::RunContext::runCurrentTest (this=0x24eab0, 
    redirectedCout="", redirectedCerr="") at src/tests/catchy/catch.hpp:5384
#13 0x00000000005c49ca in Catch::RunContext::runTest (this=0x24eab0, 
    testCase=...) at src/tests/catchy/catch.hpp:5223
#14 0x00000000005daee7 in Catch::Runner::runTests (this=0x24edc0)
    at src/tests/catchy/catch.hpp:5543
#15 0x00000000005db9b8 in Catch::Session::run (this=0x24f040)
    at src/tests/catchy/catch.hpp:5670
#16 0x000000000040ab0b in (anonymous namespace)::test_catchy ()
    at src/tests/tests.cpp:239
#17 0x000000000067b88a in std::_Function_handler<unsigned long long (), int (*)()>::_M_invoke(std::_Any_data const&) (__functor=...)
    at G:/msys64/mingw64/include/c++/5.2.0/functional:1856
#18 0x0000000000632b62 in std::function<unsigned long long ()>::operator()() const (this=0x24f2a0) at G:/msys64/mingw64/include/c++/5.2.0/functional:2271
#19 0x0000000000409c40 in run_tests(std::vector<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::function<unsigned long long ()> >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::function<unsigned long long ()> > > > const&) (tests=std::vector of length 43, capacity 64 = {...})
    at src/tests/tests.cpp:66
#20 0x000000000040c48e in main (argc=1, argv=0x371b90)
    at src/tests/tests.cpp:324
(gdb) A debugging session is active.

    Inferior 1 [process 7156] will be killed.

Quit anyway? (y or n) [answered Y; input not from terminal]

MSYS2 uses ported pacman, so here is my debug PKGBUILD build()

build() {
  cd "${srcdir}/Botan-${pkgver}"

  local BUILD_TYPE_FLAGS="--build-mode=release"
  if check_option "debug" "y"; then
    BUILD_TYPE_FLAGS="--build-mode=debug --debug-mode"
  fi

  # Set prefix to mingw{32,64} not full path. Remove starting slash
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/python3 configure.py \
                              --os=mingw \
                              --cc=gcc \
                              --prefix="${MINGW_PREFIX:1}" \
                              --destdir="${pkgdir}" \
                              ${BUILD_TYPE_FLAGS} \
                              --makefile-style=gmake \
                              --link-method=copy \
                              --with-doxygen \
                              --with-boost \
                              --with-bzip2 \
                              --with-lzma \
                              --with-sqlite3 \
                              --with-zlib

    make
}
nazar554 commented 9 years ago

Is it caused by this?

INFO: Disabling shared lib on mingw

I am currently trying to build with --os=cygwin(disable non-mingw modules) to try shared libs, but I think it is the wrong way(I get build errors about missing functions).

webmaster128 commented 9 years ago

The problem is caused by building a static library without amalgamation. This will probably work in the upcoming release 1.11.21 early in October. For now you can try the amalgamation by adding --via-amalgamation

nazar554 commented 9 years ago

So shared build on mingw is not supported? I tried --disable-shared -via-amalgamation and --with-boost but my build is failing:

   INFO: Platform: OS="Windows" machine="AMD64" proc="Intel64 Family 6 Model 60 Stepping 3, GenuineIntel"
   INFO: Guessing target processor is a x86_64/x86_64 (use --cpu to set)
   INFO: Target is gcc-mingw-x86_64-x86_64
   INFO: Skipping, by request only - cvc
   INFO: Skipping, incompatible CPU - mp_x86_32 simd_altivec
   INFO: Skipping, incompatible OS - beos_stats dev_random dyn_load egd locking_allocator proc_walk unix_procs
   INFO: Skipping, incompatible compiler - mp_x86_32_msvc
   INFO: Skipping, loaded only if needed by dependency - mp_generic simd_scalar
   INFO: Skipping, requires external dependency - openssl
   INFO: Using MP module mp_x86_64
   INFO: Using SIMD module simd_sse2
   INFO: Loading modules adler32 aead aes aes_ni aes_ssse3 aont asn1 auto_rng base base64 bcrypt benchmark bigint block blowfish boost bzip2 camellia cascade cast cbc cbc_mac ccm cfb chacha chacha20poly1305 clmul cmac codec_filt comb4p compression crc24 crc32 cryptoapi_rng cryptobox ctr curve25519 datastor des dh dl_algo dl_group dlies dsa eax ec_gfp ec_group ecb ecc_key ecdh ecdsa elgamal eme_oaep eme_pkcs1 eme_raw emsa1 emsa1_bsi emsa_pkcs1 emsa_pssr emsa_raw emsa_x931 entropy fd_unix ffi filters fpe_fe1 gcm gost_28147 gost_3410 gost_3411 has160 hash hash_id hex hkdf hmac hmac_drbg hmac_rng hres_timer http_util idea idea_sse2 if_algo kasumi kdf kdf1 kdf2 keccak keypair lion lzma mac mars mce mceies md2 md4 md5 mdx_hash mgf1 misty1 mode_pad modes mp mp_x86_64 noekeon noekeon_simd nr numbertheory ocb ofb oid_lookup openpgp par_hash passhash9 pbes2 pbkdf pbkdf1 pbkdf2 pem pk_pad poly1305 prf_tls prf_x942 pubkey rc2 rc4 rc5 rc6 rdrand rfc3394 rfc6979 rmd128 rmd160 rng rsa rw safer salsa20 seed serpent serpent_simd sessions_sql sessions_sqlite3 sha1 sha1_sse2 sha2_32 sha2_64 simd simd_sse2 siphash siv skein sqlite3 srp6 stream system_rng tea threefish threefish_avx2 tiger tls tss twofish utils whirlpool win32_stats x509 x919_mac x931_rng xtea xtea_simd xts zlib
   INFO: Enabling use of external dependency boost
   INFO: Enabling use of external dependency bzip2
   INFO: Enabling use of external dependency lzma
   INFO: Enabling use of external dependency sqlite3
   INFO: Enabling use of external dependency zlib
   INFO: Assuming CPU is little endian
   INFO: Assuming unaligned memory access works
   INFO: Using hardlink to link files into build dir (use --link-method to change)
   INFO: Writing amalgamation header to botan_all.h
   INFO: Writing amalgamation source to botan_all.cpp
   INFO: Writing amalgamation source to botan_all_aesni.cpp
   INFO: Writing amalgamation source to botan_all_ssse3.cpp
   INFO: Writing amalgamation source to botan_all_rdrand.cpp
   INFO: Writing amalgamation source to botan_all_avx2.cpp
   INFO: Botan 1.11.20 (released dated 20150907) build setup is complete
g++ -m64 -pthread -fstack-protector  -std=c++11 -D_REENTRANT -c -O2 -Wall -Wextra -Wstrict-aliasing -Wstrict-overflow=5 -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wzero-as-null-pointer-constant -Ibuild/include botan_all.cpp -o build/obj/lib/botan_all.o
g++ -m64 -pthread -fstack-protector -maes -mpclmul -mssse3  -std=c++11 -D_REENTRANT -c -O2 -Wall -Wextra -Wstrict-aliasing -Wstrict-overflow=5 -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wzero-as-null-pointer-constant -Ibuild/include botan_all_aesni.cpp -o build/obj/lib/botan_all_aesni.o
g++ -m64 -pthread -fstack-protector -mssse3  -std=c++11 -D_REENTRANT -c -O2 -Wall -Wextra -Wstrict-aliasing -Wstrict-overflow=5 -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wzero-as-null-pointer-constant -Ibuild/include botan_all_ssse3.cpp -o build/obj/lib/botan_all_ssse3.o
g++ -m64 -pthread -fstack-protector -mrdrnd  -std=c++11 -D_REENTRANT -c -O2 -Wall -Wextra -Wstrict-aliasing -Wstrict-overflow=5 -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wzero-as-null-pointer-constant -Ibuild/include botan_all_rdrand.cpp -o build/obj/lib/botan_all_rdrand.o
g++ -m64 -pthread -fstack-protector -mavx2  -std=c++11 -D_REENTRANT -c -O2 -Wall -Wextra -Wstrict-aliasing -Wstrict-overflow=5 -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wzero-as-null-pointer-constant -Ibuild/include botan_all_avx2.cpp -o build/obj/lib/botan_all_avx2.o
In file included from G:/msys64/mingw64/include/boost/asio/detail/win_tss_ptr.hpp:23:0,
                 from G:/msys64/mingw64/include/boost/asio/detail/tss_ptr.hpp:25,
                 from G:/msys64/mingw64/include/boost/asio/detail/call_stack.hpp:20,
                 from G:/msys64/mingw64/include/boost/asio/impl/handler_alloc_hook.ipp:19,
                 from G:/msys64/mingw64/include/boost/asio/handler_alloc_hook.hpp:80,
                 from G:/msys64/mingw64/include/boost/asio/detail/handler_alloc_helpers.hpp:21,
                 from G:/msys64/mingw64/include/boost/asio/detail/bind_handler.hpp:19,
                 from G:/msys64/mingw64/include/boost/asio/detail/wrapped_handler.hpp:18,
                 from G:/msys64/mingw64/include/boost/asio/io_service.hpp:24,
                 from G:/msys64/mingw64/include/boost/asio/basic_io_object.hpp:19,
                 from G:/msys64/mingw64/include/boost/asio/basic_socket.hpp:20,
                 from G:/msys64/mingw64/include/boost/asio/basic_datagram_socket.hpp:20,
                 from G:/msys64/mingw64/include/boost/asio.hpp:21,
                 from botan_all.cpp:21525:
G:/msys64/mingw64/include/boost/asio/detail/socket_types.hpp:24:4: error: #error WinSock.h has already been included
 #  error WinSock.h has already been included
    ^
In file included from G:/msys64/mingw64/include/boost/asio/detail/socket_types.hpp:38:0,
                 from G:/msys64/mingw64/include/boost/asio/detail/win_tss_ptr.hpp:23,
                 from G:/msys64/mingw64/include/boost/asio/detail/tss_ptr.hpp:25,
                 from G:/msys64/mingw64/include/boost/asio/detail/call_stack.hpp:20,
                 from G:/msys64/mingw64/include/boost/asio/impl/handler_alloc_hook.ipp:19,
                 from G:/msys64/mingw64/include/boost/asio/handler_alloc_hook.hpp:80,
                 from G:/msys64/mingw64/include/boost/asio/detail/handler_alloc_helpers.hpp:21,
                 from G:/msys64/mingw64/include/boost/asio/detail/bind_handler.hpp:19,
                 from G:/msys64/mingw64/include/boost/asio/detail/wrapped_handler.hpp:18,
                 from G:/msys64/mingw64/include/boost/asio/io_service.hpp:24,
                 from G:/msys64/mingw64/include/boost/asio/basic_io_object.hpp:19,
                 from G:/msys64/mingw64/include/boost/asio/basic_socket.hpp:20,
                 from G:/msys64/mingw64/include/boost/asio/basic_datagram_socket.hpp:20,
                 from G:/msys64/mingw64/include/boost/asio.hpp:21,
                 from botan_all.cpp:21525:
G:/msys64/mingw64/x86_64-w64-mingw32/include/winsock2.h:15:2: warning: #warning Please include winsock2.h before windows.h [-Wcpp]
 #warning Please include winsock2.h before windows.h
  ^

botan_all.cpp:21525


#if defined(BOTAN_HAS_BOOST_ASIO)
#include <boost/asio.hpp>
#endif

It looks like windows.h was included before boost/asio.hpp (by Win32 CryptoAPI EntropySource), Boost ASIO in turn includes winsock.h. Any ideas how to pull out #include <windows.h> from es_capi.cpp without disabling the module? Or at least put it lower in the source file?

Also build without Boost fails in botan-test.exe(i get non-zero return in PKGBUILD check()):

fuzzer: Exception escaped test: No filesystem access enabled.
===============
Tests 1 FAILs
webmaster128 commented 9 years ago

Yes, shared libs are not supported on MinGW due to the strange mix of Unix and Windows technologies and the lack of development resources and interest. I am sure it can be done but I also think it's not an easy pick.

Header order: could you try moving the <boost/asio.hpp> include up, e.g. below #include "botan_all_internal.h"?

The test fail is fixed in the upcoming release 1.11.21, see #276.

webmaster128 commented 9 years ago

Could you please try static build (--disable-shared) without amalgamation using the new 1.11.21 release?

nazar554 commented 9 years ago

Through some patching I managed to compile a working x86_64 MinGW build. I had to:

However, i686 build is failing with this error: ./libbotan-1.11.a(utils_calendar.o):calendar.cpp:(.text+0x713): undefined reference to _mkgmtime32 It is a MSVC C library function, so linking to some library won't help. I will investigate if a workaround is possible.

nazar554 commented 9 years ago

For now I forced calendar.cpp to use boost_timegm, and it fixed the i686 build.

webmaster128 commented 9 years ago

Nice work!

Link to winsock2

Any idea why we don't need ws2_32 and mswsock on Windows using MSVC? Those libraries could be added to the info.txt file for the proper module by just adding

<libs>
mingw -> ws2_32,mswsock
</libs>

Do you know where those are used?

Through some clever hack install everything to correct directory because configure.py script didn't work with --prefix and --destdir correctly in my case

The hack is indeed really fancy. --prefix kind of the same as --destdir, so I'll try to get rid of one. We need to know the installation dir at configure time for a number of generated files, so it is not easy to change it. The other option you have would be to call install.py manually if your current hack starts to cause problems.

However, i686 build is failing with this error: ./libbotan-1.11.a(utils_calendar.o):calendar.cpp:(.text+0x713): undefined reference to _mkgmtime32

Is this a MinGW bug? We can easily drop the mkgmtime OS feature from src/build-data/os/mingw.txt. In fact your patch should do exactly that if you know that boost is available.

webmaster128 commented 9 years ago

@mouse07410 could you please stop posting your own problems under unrelated issues? This is a MinGW threat. Get a fucking Github client, or a browser, open Botan issues page and you'll find the right ticket there. If not, open one. I remove your comment as non-constructive and annoying. Next time, I'll remove without comment.

And please stop replying to Github conversations via email. Nobody wants to see your signature and full quotes. And get the fucking context before writing anything! You're wasting people's time.

webmaster128 commented 9 years ago

I am closing this due to inactivity. Feel free to open more specific follow-up issues if those occur when packaging new versions of Botan or you want to refactor your packaging process.