Bug #2127: Fiber#resume - segfault inside C extension
http://redmine.ruby-lang.org/issues/show/2127

Author: Suraj Kurapati
Status: Open, Priority: Normal
ruby -v: ruby 1.9.2dev (2009-09-14 trunk 24923) [i686-linux]

Hi,

I am using ruby 1.9.2dev (2009-09-14 trunk 24923) [i686-linux].

Both Fiber#resume and rb_fiber_resume() are causing a segfault
when called from inside a very simple C extension (see below).

Thanks for your consideration.

###############################################################
# ls
###############################################################

extconf.rb  main.c

###############################################################
# cat extconf.rb 
###############################################################

require 'mkmf'

have_library('ruby-static', 'ruby_init') ||
have_library('ruby', 'ruby_init')

create_makefile('main')

###############################################################
# cat main.c
###############################################################

#include <stdio.h>
#include <ruby.h>

VALUE body(VALUE arg)
{
    printf("Fiber: Inside fiber body"); 
    fflush(stdout);
    return Qnil;
}

RUBY_GLOBAL_SETUP

int main(int argc, char** argv)
{
    ruby_sysinit(&argc, &argv);
    RUBY_INIT_STACK;
    ruby_init();

    printf("Main: Creating Ruby fiber..."); 
    fflush(stdout);
    VALUE fib = rb_fiber_new(body, Qnil);

    VALUE dump = rb_inspect(fib);
    rb_io_puts(1, &dump, rb_stdout);

    printf("Main: Going to resume fiber...\n"); 
    fflush(stdout);

    rb_funcall(fib, rb_intern("resume"), 0); /* <== SEGFAULT */

    printf("Main: Fiber resumed successfully.\n");
    fflush(stdout);

    return ruby_cleanup(0);
}

###############################################################
# ruby -v extconf.rb 
###############################################################

ruby 1.9.2dev (2009-09-14 trunk 24923) [i686-linux]
checking for ruby_init() in -lruby-static... yes
creating Makefile

###############################################################
# sed -i 's,-shared,,g' Makefile ; make
###############################################################

gcc -I. -I/home/sun/app/ruby-1.9-git/include/ruby-1.9.1/i686-linux -I/home/sun/app/ruby-1.9-git/include/ruby-1.9.1/ruby/backward -I/home/sun/app/ruby-1.9-git/include/ruby-1.9.1 -I. -D_FILE_OFFSET_BITS=64  -fPIC -g  -o main.o -c main.c
gcc  -o main.so main.o -L. -L/home/sun/app/ruby-1.9-git/lib -Wl,-R/home/sun/app/ruby-1.9-git/lib -L.  -rdynamic -Wl,-export-dynamic    -lruby-static  -lpthread -lrt -ldl -lcrypt -lm   -lc

###############################################################
# ./main.so
###############################################################

Main: Creating Ruby fiber...#<Fiber:0x8a5938c>
Main: Going to resume fiber...
[BUG] Segmentation fault
ruby 1.9.2dev (2009-09-14 trunk 24923) [i686-linux]

-- control frame ----------
c:0001 p:---- s:0001 b:-001 l:000000 d:000000 ------
---------------------------

fish: Job 1, ???./main.so??? terminated by signal SIGSEGV (Address boundary error)

####################################################
# gdb main.so core 
####################################################

GNU gdb 6.8
[...]
Core was generated by `./main.so'.
Program terminated with signal 11, Segmentation fault.
[New process 8635]
[New process 8636]
#0  0xb7c539be in uw_frame_state_for () from /usr/lib/libgcc_s.so.1

(gdb) bt full
#0  0xb7c539be in uw_frame_state_for () from /usr/lib/libgcc_s.so.1
No symbol table info available.
#1  0xb7c54011 in _Unwind_Backtrace () from /usr/lib/libgcc_s.so.1
No symbol table info available.
#2  0xb7ddf405 in backtrace () from /lib/libc.so.6
No symbol table info available.
#3  0x08141c33 in rb_vm_bugreport () at vm_dump.c:598
	trace = {0x8141c33, 0x816fb60, 0x816fbba, 0x80e386e, 0xb7ee740c, 0x0 <repeats 1019 times>}
	n = -1209027184
	syms = (char **) 0xb7d4630f
	i = -1209789088
#4  0x0816fb60 in report_bug (file=0x0, line=0, fmt=0x819a2e3 "Segmentation fault", 
    args=0x8aa97d4 "\236é\031k") at error.c:215
	buf = '\0' <repeats 8176 times>, "\230\227ª\bcè\023\b\2102£\b\230~¬\b"
	out = (FILE *) 0xb7e41560
	len = 0
#5  0x0816fbba in rb_bug (fmt=0x819a2e3 "Segmentation fault") at error.c:232
	args = 0x8aa97d4 "\236é\031k"
#6  0x080e386e in sigsegv (sig=11, info=0x8aa980c, ctx=0x8aa988c) at signal.c:616
	th = (rb_thread_t *) 0x8a33288
#7  <signal handler called>
No symbol table info available.
#8  0x6b19e99e in ?? ()
No symbol table info available.


----------------------------------------
http://redmine.ruby-lang.org