Skip to content

Instantly share code, notes, and snippets.

@mbj
Last active September 28, 2017 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbj/31163a8e712573877268 to your computer and use it in GitHub Desktop.
Save mbj/31163a8e712573877268 to your computer and use it in GitHub Desktop.
segfault.rb
class Foo
include Enumerable
def each(&block)
[].__send__(:each, &block)
rescue Exception
end
def more
to_a # any method from enumerable
[self]
end
end
def call(resource)
resource.more.each(&method(__method__))
end
Thread.new do
Kernel.catch(Exception.new) do
call(Foo.new)
end
end.join
# It is expected to:
# a) Raise a system stack error (expected)
# b) Segfault (Bug in MRI I'm hunting)
# c) Hang (Serve bug in MRI I'm hunting) please also check via strace / gdb if a segfault happened and the signal handler is trapped in a futex
# Context: https://github.com/mbj/mutant/issues/265
@mbj
Copy link
Author

mbj commented Oct 30, 2014

@ALL When it hangs, can you post an strace of the hanging process?

@skade
Copy link

skade commented Oct 30, 2014

@mbj: I used dtruss (OS X) and added a link to the gist with the output.

@mbj
Copy link
Author

mbj commented Oct 30, 2014

@skade, thx.

@plexus
Copy link

plexus commented Nov 4, 2014

Some more info

On trunk from a week ago, compiled with -O0, -O1, -O2, it would work correctly (SystemStackError). This is for this commit

commit 98ea62756142b6c9cc030e99b6d936d986ce463c
Author: nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date:   Wed Oct 29 12:13:26 2014 +0000

    parse.y: warn circular argument reference

    * parse.y (gettable_gen): warn circular argument reference, for
      transition from 2.1 and earlier.  [ruby-core:65990] [Bug #10314]

    git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48188 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

With -O3 it would segfault in rb_const_get (variable.c:2658) on this line

    if (tbl && st_lookup(tbl, (st_data_t)id, &val)) {

On current trunk

commit 2f00182634eaa0389b46322301795de475f46cec
Author: nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date:   Tue Nov 4 07:25:41 2014 +0000

    suppress warnings

    * vm.c (rb_vm_make_proc): cast to suppress warning by VC6.

    * ext/-test-/win32/console/attribute.c (console_set_attribute):
      ditto.

    git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48266 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

It works correctly with -O0 and -O2, gives a segfault with -O1, and hangs with -O3.

Straces of the two problematic runs:

@mbj
Copy link
Author

mbj commented Nov 13, 2014

I just tried with freshly released 2.1.5 and it still segfaults. :(

@mbj
Copy link
Author

mbj commented Jan 31, 2015

This problem was probably solved with: ruby/ruby@8fe95fe

@kbrock
Copy link

kbrock commented Sep 22, 2015

2.2.3 and 2.2.2 give me the same results.

ruby segfault.rb 
segfault.rb:10:in `to_a': stack level too deep (SystemStackError)
    from segfault.rb:10:in `more'
    from segfault.rb:16:in `call'
    from segfault.rb:16:in `each'
    from segfault.rb:16:in `call'
    from segfault.rb:16:in `each'
    from segfault.rb:16:in `call'
    from segfault.rb:16:in `each'
    from segfault.rb:16:in `call'
     ... 1189 levels...
    from segfault.rb:16:in `call'
    from segfault.rb:21:in `block (2 levels) in <main>'
    from segfault.rb:20:in `catch'
    from segfault.rb:20:in `block in <main>'
ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]

of note, 2.3dev just hangs for me.

@mathieujobin
Copy link

mathieujobin commented Sep 28, 2017

[mathieu@hz550 loyalty]$ rvm use 2.2.8
Using /home/mathieu/.rvm/gems/ruby-2.2.8
[mathieu@hz550 loyalty]$ ruby  segfault.rb
segfault.rb:10:in `to_a': stack level too deep (SystemStackError)
        from segfault.rb:10:in `more'
        from segfault.rb:16:in `call'
        from segfault.rb:16:in `each'
        from segfault.rb:16:in `call'
        from segfault.rb:16:in `each'
        from segfault.rb:16:in `call'
        from segfault.rb:16:in `each'
        from segfault.rb:16:in `call'
         ... 1103 levels...
        from segfault.rb:16:in `call'
        from segfault.rb:21:in `block (2 levels) in <main>'
        from segfault.rb:20:in `catch'
        from segfault.rb:20:in `block in <main>'
[mathieu@hz550 loyalty]$ ruby -v 
ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-linux]
[mathieu@hz550 loyalty]$ rvm use 2.3.5
Using /home/mathieu/.rvm/gems/ruby-2.3.5
[mathieu@hz550 loyalty]$ ruby  segfault.rb
segfault.rb:10:in `to_a': stack level too deep (SystemStackError)
        from segfault.rb:10:in `more'
        from segfault.rb:16:in `call'
        from segfault.rb:16:in `each'                                                                                                                                                
        from segfault.rb:16:in `call'                                                                                                                                                
        from segfault.rb:16:in `each'                                                                                                                                                
        from segfault.rb:16:in `call'                                                                                                                                                
        from segfault.rb:16:in `each'                                                                                                                                                
        from segfault.rb:16:in `call'                                                                                                                                                
         ... 809 levels...                                                                                                                                                           
        from segfault.rb:16:in `call'                                                                                                                                                
        from segfault.rb:21:in `block (2 levels) in <main>'                                                                                                                          
        from segfault.rb:20:in `catch'                                                                                                                                               
        from segfault.rb:20:in `block in <main>'                                                                                                                                     
[mathieu@hz550 loyalty]$ rvm use 2.4.2
Using /home/mathieu/.rvm/gems/ruby-2.4.2
[mathieu@hz550 loyalty]$ ruby  segfault.rb
segfault.rb:10: [BUG] vm_call_cfunc - cfp consistency error
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]

-- Control frame information -----------------------------------------------
Segmentation fault (core dumped)
[mathieu@hz550 loyalty]$ 

ArchLinux 64bit
Linux hz550 4.12.12-1-ARCH #1 SMP PREEMPT Sun Sep 10 09:41:14 CEST 2017 x86_64 GNU/Linux

on debian 14.04 I am getting segfault.rb:10: [BUG] vm_call_cfunc - cfp consistency error on 2.2.8

@mathieujobin
Copy link

mathieujobin commented Sep 28, 2017

mathieu@ubuntu-1404 (i-123):~$ ruby segfault.rb 
Segmentation fault (core dumped)                                                                                                                                                     
mathieu@ubuntu-1404 (i-123):~$    

with brighbox compiled packages

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