Bug #20072
closedfree(): invalid pointer when compiled with --enable-shared --with-jemalloc
Description
When ruby is built with --enable-shared --with-jemalloc
on Linux (current Gentoo, ubuntu22 in docker),
running a rails app yields:
free(): invalid pointer
Aborted
The issue started appearing after 5bb946228550c7f171c27725860b153a675404f3 https://github.com/ruby/ruby/commit/5bb946228550c7f171c27725860b153a675404f3
Related to https://bugs.ruby-lang.org/issues/18409 (workaround to LD_PRELOAD jemalloc from that issue works)
Updated by nobu (Nobuyoshi Nakada) 5 months ago
Could you share your config.log and crash report?
Updated by misdoro (Mikhail Doronin) 5 months ago
- File config.log added
Hi Nobu, you will find the config.log attached.
Debugged it a bit deeper, it boils down to:
- install ruby with --enable-shared --with-jemalloc
gem install sassc
- running
irb
andrequire 'sassc'
that is immediately crashing:
$ irb
irb(main):001> require 'sassc'
free(): invalid pointer
Aborted
Updated by kjtsanaktsidis (KJ Tsanaktsidis) 5 months ago
I wasn't able to reproduce your crash, but there is definitely a problem - when using --enable-shared
and --with-jemalloc
together, the Ruby that gets built still uses libc's malloc and ignores jemalloc. This is because we pass -ljemalloc
to the link line for libruby.so
, but we don't pass it to ruby
. This means that the built Ruby isn't marked as needing libjemalloc.so
:
root@jammy-189dc9d584290f1a:/var/ruby# readelf --dynamic ruby | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [libruby.so.3.3]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
And because the dynamic linker (at least the glibc one) links libraries in breadth-first order, that means that libc.so.6
is linked before libjemalloc.so.2
:
root@jammy-189dc9d584290f1a:/var/ruby# ldd ruby
linux-vdso.so.1 (0x00007ffe873fb000)
libruby.so.3.3 => /usr/local/lib/libruby.so.3.3 (0x00007f8870000000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f886fc00000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f887054c000)
libjemalloc.so.2 => /lib/x86_64-linux-gnu/libjemalloc.so.2 (0x00007f886f800000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f8870512000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f886ff19000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8870572000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f886f400000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f886fef9000)
We need to pass -ljemalloc
to the linker command line for the final Ruby executable. I'm playing around trying to find the right Autoconf magic spells for this now.
Updated by hsbt (Hiroshi SHIBATA) 5 months ago
- Related to Bug #19831: warning message of linker with macOS Sonoma beta added
Updated by hsbt (Hiroshi SHIBATA) 5 months ago
- Status changed from Open to Closed
https://github.com/ruby/ruby/pull/9284 has been merged.
#19831 is already solved. There are no warnings with the latest Xcode.
Updated by shyouhei (Shyouhei Urabe) 5 months ago
This issue reminds me of https://github.com/ruby/ruby/pull/4627