danielpclark/rutie

SEGV in the example

Closed this issue · 2 comments

Hi. I would like some help running test in rutie_ruby_example on Mac. (full output on gist)

% bundle exec rake test

... 

/Users/atsushi/.rbenv/versions/2.6.3/lib/libruby.2.6.dylib(rb_print_backtrace+0x19) [0x1014eda19]
/Users/atsushi/.rbenv/versions/2.6.3/lib/libruby.2.6.dylib(rb_vm_bugreport+0x88) [0x1014edac8]
/Users/atsushi/.rbenv/versions/2.6.3/lib/libruby.2.6.dylib(rb_bug_context+0x1c2) [0x101299da2]
/Users/atsushi/.rbenv/versions/2.6.3/lib/libruby.2.6.dylib(sigsegv+0x58) [0x10141be68]
/usr/lib/system/libsystem_platform.dylib(_sigtramp+0x1d) [0x7fff76cd1b5d]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(ruby_xmalloc0+0x11) [0x103b5c521]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(ruby_xmalloc_body+0x2c) [0x103b5c45c]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(ruby_xmalloc+0x15) [0x103b58de5]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(st_init_table_with_size+0x28) [0x103cc2368]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(st_init_table+0x19) [0x103cc2589]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(st_init_strcasetable+0x10) [0x103cc2640]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(rb_enc_init+0x28) [0x103b11b38]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(rb_usascii_encoding+0x17) [0x103b13b27]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(rb_intern2+0x25) [0x103d02525]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(rb_intern+0x2c) [0x103d0256c]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(rb_define_class+0x19) [0x103ac9359]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(_ZN5rutie7binding5class12define_class17hbe122892dfb23322E+0x87) [0x103a143f7]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(_ZN5rutie5class5class5Class3new17h910a3e69208f47ceE+0x42) [0x103a0b8c2]
/Users/atsushi/prog/rutie/examples/rutie_ruby_example/target/debug/librutie_ruby_example.dylib(Init_rutie_ruby_example+0x28) [0x103a02ca8]

I've investigated with lldb and found that rb_enc_init was entered twice with different pointer to the static struct enc_table, whereas it looks like the function should only be run only once.

% bundle exec lldb -- ruby -I test test/rutie_ruby_example_test.rb
(lldb) target create "ruby"
Current executable set to 'ruby' (x86_64).
(lldb) settings set -- target.run-args  "-I" "test" "test/rutie_ruby_example_test.rb"
(lldb) breakpoint set --name rb_enc_init
Breakpoint 1: where = libruby.2.6.dylib`rb_enc_init + 8 at encoding.c:587:5, address = 0x0000000000085088
(lldb) run
Process 86782 launched: '/Users/atsushi/.rbenv/versions/2.6.3/bin/ruby' (x86_64)
Process 86782 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100153088 libruby.2.6.dylib`rb_enc_init at encoding.c:587:5
   584 	void
   585 	rb_enc_init(void)
   586 	{
-> 587 	    enc_table_expand(ENCODING_COUNT + 1);
   588 	    if (!enc_table.names) {
   589 		enc_table.names = st_init_strcasetable();
   590 	    }
Target 0: (ruby) stopped.
(lldb) print &enc_table
((anonymous struct) *) $0 = 0x000000010047a1c8
(lldb) continue
Process 86782 resuming
1 location added to breakpoint 1
Process 86782 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.2
    frame #0: 0x0000000105310b18 librutie_ruby_example.dylib`rb_enc_init at encoding.c:587:5
   584 	void
   585 	rb_enc_init(void)
   586 	{
-> 587 	    enc_table_expand(ENCODING_COUNT + 1);
   588 	    if (!enc_table.names) {
   589 		enc_table.names = st_init_strcasetable();
   590 	    }
Target 0: (ruby) stopped.
(lldb) print &enc_table
((anonymous struct) *) $1 = 0x000000010567b8a8
(lldb) continue
Process 86782 resuming
Process 86782 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x3e0)
    frame #0: 0x000000010535b521 librutie_ruby_example.dylib`ruby_xmalloc0(size=56) at gc.c:8318:31
   8315	static void *
   8316	ruby_xmalloc0(size_t size)
   8317	{
-> 8318	    return objspace_xmalloc0(&rb_objspace, size);
   8319	}
   8320
   8321	void *
Target 0: (ruby) stopped.

My Ruby was installed this way. Compiling with or without CONFIGURE_OPTS=--enable-shared did not matter.

% CONFIGURE_OPTS=--enable-shared CFLAGS='-g -O0' rbenv install 2.6.3 -k
rbenv: /Users/atsushi/.rbenv/versions/2.6.3 already exists
continue with installation? (y/N) y
ruby-build: use openssl from homebrew
Downloading ruby-2.6.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.bz2
Installing ruby-2.6.3...
ruby-build: use readline from homebrew
Installed ruby-2.6.3 to /Users/atsushi/.rbenv/versions/2.6.3
% rbenv global 2.6.3
% ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
% export LIBRARY_PATH="$(rbenv root)/versions/2.6.3/lib"
% ruby -e "require 'rbconfig'; puts RbConfig::CONFIG['ENABLE_SHARED']"
yes

I'm using macOS 10.14.5 (Mojave) and Rust 1.35.0 (2019-05-20).

If old build artifacts are left behind from before (the original Ruby that was installed as static which was linked against) it may be causing these errors. You can delete the target directory or run cargo clean. Then try it again.

Wow, cargo clean worked! I thought I'd tried deleting the target directory, but anyway, thank you.