Bug #16784
closedCompiling with --enable-load-relative and "musl-gcc -static" yields "negative string size (or size too big) (ArgumentError)"
Description
$ uname -ar
Linux localhost 5.4.27-0-lts #1-Alpine SMP Mon, 23 Mar 2020 18:15:20 UTC ppc64le Linux
$ gcc --version
gcc (Alpine 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ apk info musl
musl-1.1.24-r2 description:
the musl c library (libc) implementation
ruby-2.6.6> CC="gcc -static" ./configure --enable-load-relative
Configuration summary for ruby version 2.6.6
* Installation prefix: /usr/local
* exec prefix: ${prefix}
* arch: powerpc64le-linux-musl
* site arch: ${arch}
* RUBY_BASE_NAME: ruby
* ruby lib prefix: ${libdir}/${RUBY_BASE_NAME}
* site libraries path: ${rubylibprefix}/${sitearch}
* vendor path: ${rubylibprefix}/vendor_ruby
* target OS: linux-musl
* compiler: gcc -static
* with pthread: yes
* enable shared libs: no
* dynamic library ext: so
* CFLAGS: ${optflags} ${debugflags} ${warnflags}
* LDFLAGS: -L. -fstack-protector-strong -rdynamic \
-Wl,-export-dynamic
* DLDFLAGS: -Wl,--compress-debug-sections=zlib
* optflags: -O3
* debugflags: -ggdb3
* warnflags: -Wall -Wextra -Wdeclaration-after-statement \
-Wdeprecated-declarations -Wduplicated-cond \
-Wimplicit-function-declaration -Wimplicit-int \
-Wmisleading-indentation -Wpointer-arith \
-Wrestrict -Wwrite-strings \
-Wimplicit-fallthrough=0 -Wmissing-noreturn \
-Wno-cast-function-type \
-Wno-constant-logical-operand -Wno-long-long \
-Wno-missing-field-initializers \
-Wno-overlength-strings \
-Wno-packed-bitfield-compat \
-Wno-parentheses-equality -Wno-self-assign \
-Wno-tautological-compare -Wno-unused-parameter \
-Wno-unused-value -Wsuggest-attribute=format \
-Wsuggest-attribute=noreturn -Wunused-variable
* strip command: strip -S -x
* install doc: yes
* JIT support: yes
* man page type: man
ruby-2.6.6> make
...
building rb_mjit_header.h
linking miniruby
rb_mjit_header.h updated
generating encdb.h
building .ext/include/powerpc64le-linux-musl/rb_mjit_min_header-2.6.6.h
./miniruby -I./lib -I. -I.ext/common ./tool/transform_mjit_header.rb "gcc -static " rb_mjit_header.h .ext/include/powerpc64le-linux-musl/rb_mjit_min_header-2.6.6.h
Traceback (most recent call last):
Traceback (most recent call last):
./miniruby: negative string size (or size too big) (ArgumentError)
./miniruby: negative string size (or size too big) (ArgumentError)
make: *** [uncommon.mk:781: .rbconfig.time] Error 1
make: *** Waiting for unfinished jobs....
make: *** [uncommon.mk:233: .ext/include/powerpc64le-linux-musl/rb_mjit_min_header-2.6.6.h] Error 1
Traceback (most recent call last):
./miniruby: negative string size (or size too big) (ArgumentError)
make: *** [uncommon.mk:1033: encdb.h] Error 1
ruby-2.6.6> ./miniruby
./miniruby: negative string size (or size too big) (ArgumentError)
Seems to me the miniruby binary is not correctly compiled if CC is musl-gcc -static
(static linking) and --enable-load-relative
option is enabled.
Updated by xtkoba (Tee KOBAYASHI) about 3 years ago
I have confirmed this issue with a recent 3.1.0dev version (33dc0a070a):
$ ./miniruby -e ""
./miniruby: negative string size (or size too big) (ArgumentError)
An observation using GDB:
Breakpoint 1, ruby_init_loadpath () at ../ruby.c:614
614 VALUE load_path, archlibdir = 0;
(gdb) next
616 const char *paths = ruby_initial_load_paths;
(gdb)
630 sopath = runtime_libruby_path();
(gdb)
631 libpath = RSTRING_PTR(sopath);
(gdb)
633 p = strrchr(libpath, '/');
(gdb)
634 if (p) {
(gdb)
675 baselen = p - libpath;
(gdb)
676 rb_str_resize(sopath, baselen);
(gdb) p baselen
$1 = 18446743798821772880
(gdb) p p
$2 = 0x0
(gdb) p libpath
$3 = 0x400096a1b0 ""
It seems that in line 675 baselen
is set to a wrong value when p
is equal to NULL
.
A proposed workaround:
--- a/ruby.c
+++ b/ruby.c
@@ -671,8 +671,11 @@
p = p2;
}
#endif
+ baselen = p - libpath;
+ }
+ else {
+ baselen = 0;
}
- baselen = p - libpath;
rb_str_resize(sopath, baselen);
libpath = RSTRING_PTR(sopath);
#define PREFIX_PATH() sopath
Updated by mame (Yusuke Endoh) about 3 years ago
I don't know powerpc64le-linux-musl at all, but the patch looks reasonable to me. At least, it should be benign on other environments that now ruby works on. I will merge it.
Updated by mame (Yusuke Endoh) about 3 years ago
- Status changed from Open to Closed
Applied in changeset git|8ccc12118ea5257f846476088eb9c64944560892.
Keep libpath length non-negative [Bug #16784]
When runtime_libruby_path does not include '/', it attempts to call
rb_str_resize with negative length. This change makes sure that the
length non-negative.
Co-Authored-By: xtkoba (Tee KOBAYASHI) xtkoba+ruby@gmail.com