Bug #2781

crash when gc_mark()ing already free'd locals of cloned scope

Added by coderrr . almost 2 years ago. Updated 10 months ago.

[ruby-core:28300]
Status:Open Start date:02/23/2010
Priority:High Due date:
Assignee:- % Done:

0%

Category:-
Target version:-
ruby -v:>= 1.8.7-p248

Description

This causes a segfault on >= 1.8.7-p248

---
def def_x(arg)
  Object.send :define_method, :x do
    def_x lambda{}
  end
end

GC.stress = true  # unnecessary but makes it occur faster
def_x nil
n = 3 # minimum for crash, increase if needed
n.times { x 0 }
---

This bug was caused by the fix i suggested for #1322,  http://github.com/rubyspec/matzruby/commit/7c646cbba0815b3c9c7dc76f80fae58b30ec66b4.

The previous fix is flawed in that it added the SCOPE_MALLOC flag to the scope just so scope_dup() didn't process it.  This had the side-effect that gc_mark_children() now processes the scope whereas it would not have before.   A better fix is the following, which instead of adding the SCOPE_MALLOC flag, we add a check for the SCOPE_CLONE flag to scope_dup().  This fixes bug #1322 as well as the segfault: http://github.com/coderrr/matzruby/commit/249c7d9912b961a9350f300ed148857100a659f8

Please check the patch for other unforseen side effects.  I didn't see any changes in rubyspec failures from p174 to a patched p248.

History

Updated by coderrr . almost 2 years ago

just realized the check for SCOPE_CLONE is also no longer needed before freeing locals: http://github.com/coderrr/matzruby/commit/9c80aae67002e443314033b04ceb9c6e5b886c57

Updated by coderrr . almost 2 years ago

By the way, this causes the popular web framework sinatra to segfault due to http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L687-702 

Updated by Aman Gupta over 1 year ago

I can confirm that this is still an issue in 1.8.7-p302 (I had to increase n=3000 to reproduce on linux).

It is also causing segfaults when using Sinatra <= 0.9.5. The segfaults in Sinatra are fixed as of >= 0.9.6 with this patch: http://github.com/sinatra/sinatra/commit/ae34a6fde5e15e9ba3ca40cf800d0366e44eec1f

Also available in: Atom PDF