I have a network and multithreaded application that was originally developed for Linux. It uses libev, mbedtls, protobuf-c and other libraries. My goal is to run it on Windows. I tried to port it with msys2 and cygwin. I decided to do it with cygwin, because seems there were more packages in the repository. Finally, I started it on Windows, but it works extremely unstable. In gdb it almost always crashes with damaged stack. After recovering SP the backtrace usually shows:
Thread 1 "App" received signal SIGABRT, Aborted.
0x0000000100b86619 in operator new (sz=0) at /usr/src/debug/gcc-9.3.0-2/libstdc++-v3/libsupc++/new_op.cc:50
50 /usr/src/debug/gcc-9.3.0-2/libstdc++-v3/libsupc++/new_op.cc: No such file or directory.
(gdb) bt
#0 0x0000000100b86619 in operator new (sz=0) at /usr/src/debug/gcc-9.3.0-2/libstdc++-v3/libsupc++/new_op.cc:50
#1 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) set $pc = *(void **)$rsp
(gdb) set $rsp = $rsp + 8
(gdb) info stack
#0 0x00007ff85c4e415f in WaitForSingleObjectEx () from /cygdrive/c/Windows/system32/KERNELBASE.dll
#1 0x000000018013efde in sig_send (p=<optimized out>, p@entry=0x0, si=..., tls=<optimized out>) at /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/sigproc.cc:719
#2 0x00000001801656d4 in pthread_kill (thread=<optimized out>, sig=6) at /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/thread.cc:3314
#3 0x000000018013b61b in raise (sig=<optimized out>) at /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/signal.cc:306
#4 0x000000018013b94f in abort () at /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/signal.cc:402
#5 0x0000000180176905 in dlmalloc (bytes=bytes@entry=32) at /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/malloc.cc:4587
#6 0x00000001800d4369 in malloc (size=32) at /usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/malloc_wrapper.cc:56
#7 0x00000001801367ab in _sigfe () at sigfe.s:35
#8 0x0000000100b86619 in operator new (sz=32) at /usr/src/debug/gcc-9.3.0-2/libstdc++-v3/libsupc++/new_op.cc:50
#9 0x00000001008bd229 in __gnu_cxx::new_allocator<char>::allocate (this=<optimized out>, __n=<optimized out>)
at /usr/src/debug/gcc-9.3.0-2/x86_64-pc-cygwin/libstdc++-v3/include/ext/new_allocator.h:102
#10 std::string::_Rep::_S_create (__capacity=__capacity@entry=7, __old_capacity=__old_capacity@entry=0, __alloc=...)
at /usr/src/debug/gcc-9.3.0-2/x86_64-pc-cygwin/libstdc++-v3/include/bits/basic_string.tcc:1058
#11 0x00000001008bc97b in std::string::_S_construct<char*> (__beg=0x100c40ae5 "0.0.0.0", __end=<optimized out>, __a=...)
at /usr/src/debug/gcc-9.3.0-2/x86_64-pc-cygwin/libstdc++-v3/include/bits/stl_iterator_base_funcs.h:138
#12 0x00000001008be8d7 in std::string::_S_construct_aux<char const*> (__a=..., __end=<optimized out>, __beg=0x100c40ae5 "0.0.0.0")
at /usr/src/debug/gcc-9.3.0-2/x86_64-pc-cygwin/libstdc++-v3/include/bits/basic_string.h:5137
#13 std::string::_S_construct<char const*> (__a=..., __end=<optimized out>, __beg=0x100c40ae5 "0.0.0.0")
at /usr/src/debug/gcc-9.3.0-2/x86_64-pc-cygwin/libstdc++-v3/include/bits/basic_string.h:5140
#14 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (this=0xffffc548, __s=0x100c40ae5 "0.0.0.0", __a=...)
at /usr/src/debug/gcc-9.3.0-2/x86_64-pc-cygwin/libstdc++-v3/include/bits/basic_string.tcc:665
. . .
#21 0x00000003f50310f2 in ev_invoke_pending () from /usr/bin/cygev-4.dll
#22 0x00000003f5033f95 in ev_run () from /usr/bin/cygev-4.dll
#23 0x00000001005c4471 in ev::loop_ref::run (this=0x100be7a28 <app+8>, flags=0) at /usr/include/ev++.h:211
Sometimes it crashes in malloc(). Obviously it has memory corruption. I have some questions:
- are there any memory tools in cygwin? (sanitizer etc)
- is it real to get a stable application on cygwin?
- does it make sense to use mingw/msys2 instead of cygwin?
UPD: Tested it on msys2 with the same results. So I think there's not much difference between cygwin and msys2. The problem is with the code, not the environments.