svarshavchik / courier

Courier Mail Server
http://www.courier-mta.org
76 stars 15 forks source link

Cone segfault on musl libc #28

Closed g4jc closed 2 years ago

g4jc commented 3 years ago

I've been trying to get Cone working on my Gentoo-musl system, but there are a number of issues. I currently have the following patches in my local tree and I got it compiled and linking, but then it crashes.

To get it compiling:

Fix adwt and waitpid undefined:

Subject: [PATCH 1/3] Fix undefined errors musl

---
 curses/cursesscreen.C | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/curses/cursesscreen.C b/curses/cursesscreen.C
index 7130b71..91a983d 100644
--- a/curses/cursesscreen.C
+++ b/curses/cursesscreen.C
@@ -5,6 +5,7 @@
 */

 #include "curses_config.h"
+#define _XOPEN_SOURCE_EXTENDED
 #include <signal.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -15,7 +16,7 @@
 #include <cstring>
 #include <cerrno>

-#if HAVE_SYS_WAIT_H
+#if defined HAVE_SYS_WAIT_H || !defined waitpid
 #include <sys/wait.h>
 #endif
 #ifndef WEXITSTATUS
-- 
2.26.2

Hardcode -ltinfo as Gentoo ships terminal info in a separate binary than ncurses (better would be to check for this using AC_SEARCH_LIBS([keypad],[tinfow]) or similar, but I couldn't figure out how to this smoothly using the existing autoconf config):

diff --git a/curses/configure.ac b/curses/configure.ac
index a15ea77..1f834b4 100644
--- a/curses/configure.ac
+++ b/curses/configure.ac
@@ -40,6 +40,8 @@ do
                                     ])
 done

+CURSESLIBRARY="ncurses -lncursesw -ltinfo"
+
 if test "$CURSESLIBRARY" = ""
 then
        AC_MSG_ERROR([curses library not found.])

I also attempted to fix the build without libidn, but gave up and installed the dependency after seeing a bunch of linker errors to idna_to_unicode_8z8z and friends in libs/tcpd/libcouriertls.c.

Once I got it compiled, it crashes immediately upon execution:

[102941.355971] cone[20318]: segfault at 10 ip 00007e35cf57164a sp 00007fe662722400 error 4 in libc.so[7e35cf55d000+60000]
[102941.355979] Code: 85 c9 74 01 f4 8b 4f f8 81 f9 ff ff 00 00 7f 01 f4 89 ca 48 89 fb c1 e2 04 48 63 d2 48 29 d3 48 8b 6b f0 48 89 da 48 83 ea 10 <48> 3b 55 10 74 01 f4 0f b6 55 20 83 e2 1f 39 d0 7e 01 f4 8b 55 18

This appears to be stack corruption:

#0  0x00007ffff7f7764a in free () from /lib/ld-musl-x86_64.so.1
No symbol table info available.
#1  0x00007fffffffc608 in ?? ()
No symbol table info available.
#2  0x00000000000000ad in ?? ()
No symbol table info available.
#3  0x00007fffffffc620 in ?? ()
No symbol table info available.
#4  0x00007ffff77ee550 in ?? ()
No symbol table info available.
#5  0x0000000000000001 in ?? ()
No symbol table info available.
#6  0x000000010004f7b0 in __gnu_cxx::new_allocator<char32_t>::deallocate (this=<error reading variable: Cannot access memory at address 0xfffffffffffffff8>, 
    __p=<error reading variable: Cannot access memory at address 0xfffffffffffffff0>) at /usr/lib/gcc/x86_64-gentoo-linux-musl/9.3.0/include/g++-v9/ext/new_allocator.h:128
No locals.
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

Possibly caused by the fact default stack size for musl-libc is 128kb, however I'm unsure how to fix that.

svarshavchik commented 3 years ago

I'm not sure how to fix this either, or what the problem is. If you ever figure it out, I'll review the patch.

mckaygerhard commented 3 years ago

this patch is also need for alpine linux but i dont tested cos i still make builds test with courier-mta

kaniini commented 3 years ago

Use -Wl,-z,stack-size=1024768 to increase thread stack size to 1MiB.

svarshavchik commented 2 years ago

If someone gets around to submitting a concrete patch or a PR, I'll review it.