Skip to content

Memcached -o modern segfaults when using Musl/Alpine Linux #286

Closed

Description

@zoidyzoidzoid

More info here: docker-library/memcached#20

Not too sure if this is a Musl bug or Memcached bug?

Activity

  1. dormando commented on Jul 11, 2017

    @dormando
    Member

    any way of someone getting a backtrace (or at least the segfault line out of dmesg + addr2line?) If I have to set that all up to get it to crash it might take a while to figure out :(

  2. dormando commented on Jul 11, 2017

    @dormando
    Member

    Also: I know there're a bunch of options, but if you look at -h it'll list everything that goes into -o modern. it would be super if someone knew which specific option was causing it!

  3. zoidyzoidzoid commented on Jul 11, 2017

    @zoidyzoidzoid
    Author
  4. dormando commented on Jul 12, 2017

    @dormando
    Member

    https://github.com/memcached/memcached/wiki/ReleaseNotes1437 largeish number of small changes in .37. would still save a lot of time to narrow it down, but good to know .36 is fine. If you have an easy repro you can even do a git bisect :) but a hint from a backtrace is probably good enough.

  5. zoidyzoidzoid commented on Jul 12, 2017

    @zoidyzoidzoid
    Author

    So it seems like memcached -o lru_maintainer is the problem:

    / $ memcached  -o lru_maintainer
    Segmentation fault (core dumped)
    

    slab_chunk_max also raises a different strange error, though maybe that's me setting it in the wrong place.

    / $ memcached  -o slab_chunk_max:512k
    Illegal suboption "(null)"
    
    / $ memcached -h
    memcached 1.4.39
    ...
    -o            Comma separated list of extended or experimental options
                    ...
                  - modern: Enables 'modern' defaults. Options that will be default in future.
                    enables: slab_chunk_max:512k,slab_reassign,slab_automove=1,maxconns_fast,
                             hash_algorithm=murmur3,lru_crawler,lru_maintainer,no_inline_ascii_resp
    

    Option by option:

    Details
    / $ memcached  -o no_inline_ascii_resp
    ^CSignal handled: Interrupt.
    / $ memcached  -o no_inline_ascii_resp,lru_maintainer
    Segmentation fault (core dumped)
    / $ memcached  -o no_inline_ascii_resp,lru_crawler
    ^CSignal handled: Interrupt.
    / $ memcached  -o no_inline_ascii_resp,lru_crawler,hash_algorithm=murmur3
    ^CSignal handled: Interrupt.
    / $ memcached  -o no_inline_ascii_resp,lru_crawler,hash_algorithm=murmur3,maxconns_fast
    ^CSignal handled: Interrupt.
    / $ memcached  -o no_inline_ascii_resp,lru_crawler,hash_algorithm=murmur3,maxconns_fast,slab_automove=1
    ^CSignal handled: Interrupt.
    / $ memcached  -o no_inline_ascii_resp,lru_crawler,hash_algorithm=murmur3,maxconns_fast,slab_automove=1,slab_reassign
    ^CSignal handled: Interrupt.
    / $ memcached  -o no_inline_ascii_resp,lru_crawler,hash_algorithm=murmur3,maxconns_fast,slab_automove=1,slab_reassign,slab_chunk_max:512k
    Illegal suboption "(null)"
    / $ memcached  -o no_inline_ascii_resp,lru_crawler,hash_algorithm=murmur3,maxconns_fast,slab_automove=1,slab_reassign,lru_maintainer
    Segmentation fault (core dumped)
    

    I'm not sure if this helps.

    dmesg output from memcached -o modern:

    Details
    [  315.958995] memcached[4579]: segfault at 7f1e9bd0b000 ip 00007f1e9bc0c162 sp 00007f1e9bcffe68 error 6 in ld-musl-x86_64.so.1[7f1e9bbbc000+88000]
    [  315.959039] audit: type=1701 audit(1499867799.088:84): auid=4294967295 uid=1000 gid=1000 ses=4294967295 subj=kernel pid=4571 comm="memcached" exe="/usr/local/bin/memcached" sig=11
    [  316.084659] audit: type=1325 audit(1499867799.213:85): table=nat family=2 entries=63
    [  316.084702] audit: type=1300 audit(1499867799.213:85): arch=c000003e syscall=54 success=yes exit=0 a0=4 a1=0 a2=40 a3=1962930 items=0 ppid=3348 pid=4583 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="iptables" exe="/usr/sbin/xtables-multi" subj=kernel key=(null)
    [  316.084710] audit: type=1327 audit(1499867799.213:85): proctitle=69707461626C6573002D7732002D43004B5542452D4D41524B2D44524F50002D74006E6174002D6A004D41524B002D2D7365742D786D61726B00307830303030383030302F30783030303038303030
    [  316.151142] audit: type=1325 audit(1499867799.280:86): table=nat family=2 entries=63
    [  316.151376] audit: type=1300 audit(1499867799.280:86): arch=c000003e syscall=54 success=yes exit=0 a0=4 a1=0 a2=40 a3=1f5f930 items=0 ppid=3348 pid=4590 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="iptables" exe="/usr/sbin/xtables-multi" subj=kernel key=(null)
    [  316.151473] audit: type=1327 audit(1499867799.280:86): proctitle=69707461626C6573002D7732002D43004B5542452D4D41524B2D4D415351002D74006E6174002D6A004D41524B002D2D7365742D786D61726B00307830303030343030302F30783030303034303030
    [  316.171704] audit: type=1325 audit(1499867799.297:87): table=nat family=2 entries=63
    [  316.171748] audit: type=1300 audit(1499867799.297:87): arch=c000003e syscall=54 success=yes exit=0 a0=4 a1=0 a2=40 a3=266f690 items=0 ppid=3348 pid=4592 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="iptables" exe="/usr/sbin/xtables-multi" subj=kernel key=(null)
    
  6. dormando commented on Jul 12, 2017

    @dormando
    Member

    can you addr2line -e ./memcached 00007f1e9bc0c162 ?
    also, if you're building from source, starting with memcached-debug might do something more useful now that I think about it.

    if you can start it so easily, the best thing to do is just: gdb memcached-debug (if using source, else just memcached), then r -o modern and bt

  7. zoidyzoidzoid commented on Jul 13, 2017

    @zoidyzoidzoid
    Author
    /usr/src/memcached # gdb memcached-debug
    This GDB was configured as "x86_64-alpine-linux-musl".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from memcached-debug...done.
    (gdb) r -u root -o lru_maintainer
    Starting program: /usr/src/memcached/memcached-debug -u root -o lru_maintainer
    warning: Error disabling address space randomization: Operation not permitted
    During startup program terminated with signal SIGSEGV, Segmentation fault.
    (gdb) bt
    No stack.
    
  8. zoidyzoidzoid commented on Jul 13, 2017

    @zoidyzoidzoid
    Author

    Trying to get addr2line and gdb to give me info, but I don't have too much experience with them.

    /usr/src/memcached # ./memcached-debug -u root -o modern
    Segmentation fault (core dumped)
    /usr/src/memcached # dmesg | grep segfault
    [ 4985.771359] memcached-debug[14203]: segfault at 7f9c2491d000 ip 00007f9c2481e162 sp 00007f9c24911e38 error 6 in ld-musl-x86_64.so.1[7f9c247ce000+88000]
    /usr/src/memcached # addr2line -e ./memcached-debug 00007f9c2481e162
    ??:0
    
  9. dormando commented on Jul 16, 2017

    @dormando
    Member

    sorry it took so long, I finally had some time to figure out wtf musl libc was.

    a quick bisect showed me I was creating something large on the stack, and musl must have a much smaller thread stack by default or something. It didn't need to be there so I switched it to a calloc.

    could you give the next branch a try? https://github.com/memcached/memcached/tree/next - just threw a commit on top that makes it not segfault for me. However can't build the debug binary without surgery I don't really have time for right now, so the tests don't run. It's impressive/scary that someone's been packging memcached without working tests.

  10. tianon commented on Jul 17, 2017

    @tianon
    Contributor

    It's impressive/scary that someone's been packging memcached without working tests.

    😅 that's my fault -- I'll go see what I can do to get make test running during our build right away. 😇 ❤️

    (Edit: hopefully there's a test that will exhibit this failure so I can try applying that patch! 👍)

  11. tianon commented on Jul 17, 2017

    @tianon
    Contributor

    Good news and bad news. The good news is that as expected, the tests pass 100% in our Debian variant (whew). The bad news is that Alpine has four tests failures:

    Test Summary Report
    -------------------
    t/dyn-maxbytes.t          (Wstat: 28416 Tests: 0 Failed: 0)
      Non-zero exit status: 111
      Parse errors: Bad plan.  You planned 309 tests but ran 0.
    t/lru-maintainer.t        (Wstat: 28416 Tests: 0 Failed: 0)
      Non-zero exit status: 111
      Parse errors: Bad plan.  You planned 226 tests but ran 0.
    t/slabs-reassign-chunked.t (Wstat: 28416 Tests: 0 Failed: 0)
      Non-zero exit status: 111
      Parse errors: Bad plan.  You planned 8 tests but ran 0.
    t/slabs-reassign2.t       (Wstat: 28416 Tests: 0 Failed: 0)
      Non-zero exit status: 111
      Parse errors: Bad plan.  You planned 12 tests but ran 0.
    Files=59, Tests=25154, 171 wallclock secs ( 7.77 usr  0.53 sys + 15.84 cusr  6.51 csys = 30.65 CPU)
    Result: FAIL
    

    (Edit: full output of make test: https://gist.github.com/anonymous/5cf98b4f58a0add4f8de34ddfc5ac303)

    Going to try applying 1831634 now to see if that gets us any further. 👍

  12. tianon commented on Jul 17, 2017

    @tianon
    Contributor

    Still the same 4 failures failing with 1831634 -- anything more I can do to help debug? Would a Dockerfile for reproducing within Docker be helpful? Can I upload binaries somewhere? 😄

    Edit: I messed up applying the patch, in my excitement. 🤦‍♂️ New post incoming with the test results following a proper patching.

  13. tianon commented on Jul 17, 2017

    @tianon
    Contributor

    (Should I file a new issue to discuss the test failures instead?)

  14. tianon commented on Jul 17, 2017

    @tianon
    Contributor

    Ok, scratch all that -- I messed up applying the patch. 😇

    After applying the patch, all tests pass!!!

  15. dormando commented on Jul 17, 2017

    @dormando
    Member

    excellent! If I knew how to make the -debug binary build (fails looking for gcov stuff) I could add musl to the buildbots.

  16. tianon commented on Jul 17, 2017

    @tianon
    Contributor

    Oooh, intriguing. Do they run on some version of Debian/Ubuntu? I'm happy to take a look at what it'll take to make that work!

  17. dormando commented on Jul 17, 2017

    @dormando
    Member

    I have a bunch of them. some are debian, some are ubuntu, from a few different versions.

    on my local machine I just quickly installed the latest musl into /usr/local/musl, compiled latest libevent into it, then built memcached against musl-gcc. that was enough to run the bisect so I stopped there. make would fail at linking memcached-debug together though.

    I can either clone one of the debian/ubuntu builders and do something similar with fetching latest musl/building/etc or etc.

  18. tianon commented on Jul 18, 2017

    @tianon
    Contributor

    Ah nice, I think we could get clever with the musl-tools package (https://packages.debian.org/stretch/musl-tools, https://packages.ubuntu.com/xenial/musl-tools; which includes musl-gcc). Then you've only got to compile libevent and memcached itself (rather than also building musl). I'll play around a little bit and see what I can come up with.

  19. tianon commented on Jul 18, 2017

    @tianon
    Contributor

    Alright, good news -- with the following, I've successfully reproduced the four musl-related test failures from Debian Stretch amd64:

    FROM debian:stretch-slim
    
    RUN set -ex; \
    	apt-get update; \
    	apt-get install -y --no-install-recommends \
    		ca-certificates \
    		gnupg2 dirmngr \
    		wget \
    		\
    		make \
    		musl-tools \
    	; \
    	rm -rf /var/lib/apt/lists/*
    
    ENV CC="musl-gcc -static -fPIC"
    
    ENV LIBEVENT_VERSION 2.1.8
    ENV LIBEVENT_GPG_KEY 9E3AC83A27974B84D1B3401DB86086848EF8686D
    # gpg: key B86086848EF8686D: public key "Azat Khuzhin <[email protected]>" imported
    
    RUN set -ex; \
    	wget -O libevent.tar.gz "https://github.com/libevent/libevent/releases/download/release-${LIBEVENT_VERSION}-stable/libevent-${LIBEVENT_VERSION}-stable.tar.gz"; \
    	wget -O libevent.tar.gz.asc "https://github.com/libevent/libevent/releases/download/release-${LIBEVENT_VERSION}-stable/libevent-${LIBEVENT_VERSION}-stable.tar.gz.asc"; \
    	export GNUPGHOME="$(mktemp -d)"; \
    	gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$LIBEVENT_GPG_KEY"; \
    	gpg --batch --verify libevent.tar.gz.asc libevent.tar.gz; \
    	rm -rf "$GNUPGHOME" libevent.tar.gz.asc; \
    	mkdir -p /usr/src/libevent; \
    	tar -xf libevent.tar.gz -C /usr/src/libevent --strip-components=1; \
    	rm libevent.tar.gz
    
    RUN set -ex; \
    	cd /usr/src/libevent; \
    	./configure; \
    	make -j "$(nproc)"; \
    # TODO figure out why this has "getaddrinfo" related failures
    #	make verify; \
    	make install
    
    ENV MEMCACHED_VERSION 1.4.39
    ENV MEMCACHED_SHA1 9d6f77f4f9f1b50289882fd88851dece7699c74b
    
    RUN set -ex; \
    	wget -O memcached.tar.gz "https://memcached.org/files/memcached-$MEMCACHED_VERSION.tar.gz"; \
    	echo "$MEMCACHED_SHA1 *memcached.tar.gz" | sha1sum -c -; \
    	mkdir -p /usr/src/memcached; \
    	tar -xf memcached.tar.gz -C /usr/src/memcached --strip-components=1; \
    	rm memcached.tar.gz
    
    RUN set -ex; \
    	cd /usr/src/memcached; \
    	./configure; \
    	make -j "$(nproc)"; \
    	make test; \
    	make install
    
    RUN memcached -V
    
    USER nobody
    CMD ["memcached"]

    Once I apply the following patch, the image builds (and runs!) successfully and all the tests pass: 👍 🤘

    @@ -47,6 +47,13 @@ RUN set -ex; \
            tar -xf memcached.tar.gz -C /usr/src/memcached --strip-components=1; \
            rm memcached.tar.gz
     
    +# https://github.com/memcached/memcached/issues/286
    +RUN set -ex; \
    +       cd /usr/src/memcached; \
    +       wget -O musl.patch 'https://github.com/memcached/memcached/commit/18316347865978868d70cc70dad54df2e3a8357e.patch'; \
    +       apt-get update && apt-get install -y --no-install-recommends patch && rm -rf /var/lib/apt/lists/*; \
    +       patch -p1 < musl.patch
    +
     RUN set -ex; \
            cd /usr/src/memcached; \
            ./configure; \
  20. dormando commented on Jul 18, 2017

    @dormando
    Member

    I initially tried with the self-compile because the included version of debian/ubuntu of musl was much older than what alpine had packaged. I haven't tried with the built-ins to see if that fixes the memcached-debug compile issue.

    edit: swapped some words around..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions