Skip to content

Commit

Permalink
* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
Browse files Browse the repository at this point in the history
  entry with VM_METHOD_TYPE_REFINED holds only the original method
  definition, so ci->me is set to a method entry allocated in the
  stack, and it causes SEGV/ILL.  In this commit, a method entry
  with VM_METHOD_TYPE_REFINED holds the whole original method entry.
  Furthermore, rb_thread_mark() is changed to mark cfp->klass to
  avoid GC for iclasses created by copy_refinement_iclass().

* vm_method.c (rb_method_entry_make): add a method entry with
  VM_METHOD_TYPE_REFINED to the class refined by the refinement if
  the target module is a refinement.  When a method entry with
  VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
  the same name is searched in refinements.  If such a method is
  found, the method is invoked.  Otherwise, the original method in
  the refined class (rb_method_definition_t::body.orig_me) is
  invoked.  This change is made to simplify the normal method lookup
  and to improve the performance of normal method calls.

* vm_method.c (EXPR1, search_method, rb_method_entry),
  vm_eval.c (rb_call0, rb_search_method_entry): do not use
  refinements for method lookup.

* vm_insnhelper.c (vm_call_method): search methods in refinements if
  ci->me is VM_METHOD_TYPE_REFINED.  If the method is called by
  super (i.e., ci->call == vm_call_super_method), skip the same
  method entry as the current method to avoid infinite call of the
  same method.

* class.c (include_modules_at): add a refined method entry for each
  method defined in a module included in a refinement.

* class.c (rb_prepend_module): set an empty table to
  RCLASS_M_TBL(klass) to add refined method entries, because
  refinements should have priority over prepended modules.

* proc.c (mnew): use rb_method_entry_with_refinements() to get
  a refined method.

* vm.c (rb_thread_mark): mark cfp->klass for iclasses created by
  copy_refinement_iclass().

* vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass.

* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
  the test because it should pass successfully.

* test/ruby/test_refinement.rb (test_redefine_refined_method): new
  test for the case a refined method is redefined.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
shugo committed Dec 6, 2012
1 parent bd0c636 commit 60d6038
Show file tree
Hide file tree
Showing 14 changed files with 445 additions and 181 deletions.
51 changes: 51 additions & 0 deletions ChangeLog
@@ -1,3 +1,54 @@
Thu Dec 6 18:23:05 2012 Shugo Maeda <shugo@ruby-lang.org>

* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
entry with VM_METHOD_TYPE_REFINED holds only the original method
definition, so ci->me is set to a method entry allocated in the
stack, and it causes SEGV/ILL. In this commit, a method entry
with VM_METHOD_TYPE_REFINED holds the whole original method entry.
Furthermore, rb_thread_mark() is changed to mark cfp->klass to
avoid GC for iclasses created by copy_refinement_iclass().

* vm_method.c (rb_method_entry_make): add a method entry with
VM_METHOD_TYPE_REFINED to the class refined by the refinement if
the target module is a refinement. When a method entry with
VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
the same name is searched in refinements. If such a method is
found, the method is invoked. Otherwise, the original method in
the refined class (rb_method_definition_t::body.orig_me) is
invoked. This change is made to simplify the normal method lookup
and to improve the performance of normal method calls.

* vm_method.c (EXPR1, search_method, rb_method_entry),
vm_eval.c (rb_call0, rb_search_method_entry): do not use
refinements for method lookup.

* vm_insnhelper.c (vm_call_method): search methods in refinements if
ci->me is VM_METHOD_TYPE_REFINED. If the method is called by
super (i.e., ci->call == vm_call_super_method), skip the same
method entry as the current method to avoid infinite call of the
same method.

* class.c (include_modules_at): add a refined method entry for each
method defined in a module included in a refinement.

* class.c (rb_prepend_module): set an empty table to
RCLASS_M_TBL(klass) to add refined method entries, because
refinements should have priority over prepended modules.

* proc.c (mnew): use rb_method_entry_with_refinements() to get
a refined method.

* vm.c (rb_thread_mark): mark cfp->klass for iclasses created by
copy_refinement_iclass().

* vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass.

* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
the test because it should pass successfully.

* test/ruby/test_refinement.rb (test_redefine_refined_method): new
test for the case a refined method is redefined.

Thu Dec 6 17:29:03 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>

* parse.y (parser_here_document): flush string content between new
Expand Down
16 changes: 15 additions & 1 deletion class.c
Expand Up @@ -677,6 +677,13 @@ rb_include_module(VALUE klass, VALUE module)
if (changed) rb_clear_cache();
}

static int
add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data)
{
rb_add_refined_method_entry((VALUE) data, (ID) key);
return ST_CONTINUE;
}

static int
include_modules_at(VALUE klass, VALUE c, VALUE module)
{
Expand Down Expand Up @@ -707,6 +714,13 @@ include_modules_at(VALUE klass, VALUE c, VALUE module)
}
}
c = RCLASS_SUPER(c) = rb_include_class_new(module, RCLASS_SUPER(c));
if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
VALUE refined_class =
rb_refinement_module_get_refined_class(klass);

st_foreach(RMODULE_M_TBL(module), add_refined_method_entry_i,
(st_data_t) refined_class);
}
if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
changed = 1;
skip:
Expand Down Expand Up @@ -738,7 +752,7 @@ rb_prepend_module(VALUE klass, VALUE module)
RCLASS_SUPER(klass) = origin;
RCLASS_ORIGIN(klass) = origin;
RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
RCLASS_M_TBL(klass) = 0;
RCLASS_M_TBL(klass) = st_init_numtable();
}
changed = include_modules_at(klass, klass, module);
if (changed < 0)
Expand Down
1 change: 1 addition & 0 deletions cont.c
Expand Up @@ -1067,6 +1067,7 @@ fiber_init(VALUE fibval, VALUE proc)
th->cfp->ep = th->stack;
*th->cfp->ep = VM_ENVVAL_BLOCK_PTR(0);
th->cfp->self = Qnil;
th->cfp->klass = Qnil;
th->cfp->flag = 0;
th->cfp->iseq = 0;
th->cfp->proc = 0;
Expand Down
12 changes: 9 additions & 3 deletions eval.c
Expand Up @@ -1187,6 +1187,14 @@ rb_mod_using(VALUE self, VALUE module)
return self;
}

VALUE rb_refinement_module_get_refined_class(VALUE module)
{
ID id_refined_class;

CONST_ID(id_refined_class, "__refined_class__");
return rb_attr_get(module, id_refined_class);
}

static VALUE
refinement_module_include(int argc, VALUE *argv, VALUE module)
{
Expand All @@ -1195,11 +1203,9 @@ refinement_module_include(int argc, VALUE *argv, VALUE module)
rb_control_frame_t *end_cfp = RUBY_VM_END_CONTROL_FRAME(th);
VALUE result = rb_mod_include(argc, argv, module);
NODE *cref;
ID id_refined_class;
VALUE klass, c;

CONST_ID(id_refined_class, "__refined_class__");
klass = rb_attr_get(module, id_refined_class);
klass = rb_refinement_module_get_refined_class(module);
while (RUBY_VM_VALID_CONTROL_FRAME_P(cfp, end_cfp)) {
if (RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) &&
(cref = rb_vm_get_cref(cfp->iseq, cfp->ep)) &&
Expand Down
7 changes: 7 additions & 0 deletions gc.c
Expand Up @@ -2410,6 +2410,7 @@ mark_method_entry(rb_objspace_t *objspace, const rb_method_entry_t *me)
const rb_method_definition_t *def = me->def;

gc_mark(objspace, me->klass);
again:
if (!def) return;
switch (def->type) {
case VM_METHOD_TYPE_ISEQ:
Expand All @@ -2422,6 +2423,12 @@ mark_method_entry(rb_objspace_t *objspace, const rb_method_entry_t *me)
case VM_METHOD_TYPE_IVAR:
gc_mark(objspace, def->body.attr.location);
break;
case VM_METHOD_TYPE_REFINED:
if (def->body.orig_me) {
def = def->body.orig_me->def;
goto again;
}
break;
default:
break; /* ignore */
}
Expand Down
3 changes: 3 additions & 0 deletions internal.h
Expand Up @@ -99,6 +99,9 @@ NORETURN(void rb_async_bug_errno(const char *,int));
const char *rb_builtin_type_name(int t);
const char *rb_builtin_class_name(VALUE x);

/* eval.c */
VALUE rb_refinement_module_get_refined_class(VALUE module);

/* eval_error.c */
void ruby_error_print(void);
VALUE rb_get_backtrace(VALUE info);
Expand Down
15 changes: 11 additions & 4 deletions method.h
Expand Up @@ -42,7 +42,8 @@ typedef enum {
VM_METHOD_TYPE_NOTIMPLEMENTED,
VM_METHOD_TYPE_OPTIMIZED, /* Kernel#send, Proc#call, etc */
VM_METHOD_TYPE_MISSING, /* wrapper for method_missing(id) */
VM_METHOD_TYPE_CFUNC_FRAMELESS
VM_METHOD_TYPE_CFUNC_FRAMELESS,
VM_METHOD_TYPE_REFINED,
} rb_method_type_t;

struct rb_call_info_struct;
Expand Down Expand Up @@ -72,6 +73,7 @@ typedef struct rb_method_definition_struct {
OPTIMIZED_METHOD_TYPE_SEND,
OPTIMIZED_METHOD_TYPE_CALL
} optimize_type;
struct rb_method_entry_struct *orig_me;
} body;
int alias_count;
} rb_method_definition_t;
Expand All @@ -94,9 +96,14 @@ struct unlinked_method_entry_list_entry {
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_flag_t noex);
rb_method_entry_t *rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_flag_t noex);
rb_method_entry_t *rb_method_entry(VALUE klass, ID id, VALUE *define_class_ptr);

rb_method_entry_t *rb_method_entry_get_with_refinements(VALUE refinements, VALUE klass, ID id, VALUE *define_class_ptr);
rb_method_entry_t *rb_method_entry_get_without_cache(VALUE klass, VALUE refinements, ID id, VALUE *define_class_ptr);
void rb_add_refined_method_entry(VALUE refined_class, ID mid);
rb_method_entry_t *rb_resolve_refined_method(VALUE refinements,
rb_method_entry_t *me,
VALUE *defined_class_ptr);
rb_method_entry_t *rb_method_entry_with_refinements(VALUE klass, ID id,
VALUE *defined_class_ptr);

rb_method_entry_t *rb_method_entry_get_without_cache(VALUE klass, ID id, VALUE *define_class_ptr);
rb_method_entry_t *rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_flag_t noex);

int rb_method_entry_arity(const rb_method_entry_t *me);
Expand Down
5 changes: 2 additions & 3 deletions object.c
Expand Up @@ -1338,7 +1338,7 @@ rb_obj_cmp(VALUE obj1, VALUE obj2)
static VALUE
rb_mod_to_s(VALUE klass)
{
ID id_refined_class, id_defined_at;
ID id_defined_at;
VALUE refined_class, defined_at;

if (FL_TEST(klass, FL_SINGLETON)) {
Expand All @@ -1358,8 +1358,7 @@ rb_mod_to_s(VALUE klass)

return s;
}
CONST_ID(id_refined_class, "__refined_class__");
refined_class = rb_attr_get(klass, id_refined_class);
refined_class = rb_refinement_module_get_refined_class(klass);
if (!NIL_P(refined_class)) {
VALUE s = rb_usascii_str_new2("#<refinement:");

Expand Down
4 changes: 3 additions & 1 deletion proc.c
Expand Up @@ -917,7 +917,7 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
rb_method_flag_t flag = NOEX_UNDEF;

again:
me = rb_method_entry(klass, id, &defined_class);
me = rb_method_entry_with_refinements(klass, id, &defined_class);
if (UNDEFINED_METHOD_ENTRY_P(me)) {
ID rmiss = rb_intern("respond_to_missing?");
VALUE sym = ID2SYM(id);
Expand Down Expand Up @@ -1683,6 +1683,8 @@ rb_method_entry_arity(const rb_method_entry_t *me)
default:
break;
}
case VM_METHOD_TYPE_REFINED:
return -1;
}
}
rb_bug("rb_method_entry_arity: invalid method entry type (%d)", def->type);
Expand Down
39 changes: 35 additions & 4 deletions test/ruby/test_refinement.rb
Expand Up @@ -379,10 +379,15 @@ def foo
def test_refine_module_with_overriding
m1 = Module.new {
def foo
[:m1]
super << :m1
end
}
c = Class.new {
c0 = Class.new {
def foo
[:c0]
end
}
c = Class.new(c0) {
include m1
}
m2 = Module.new {
Expand All @@ -393,7 +398,7 @@ def foo
end
}
obj = c.new
assert_equal([:m1, :m2], m2.module_eval { obj.foo })
assert_equal([:c0, :m1, :m2], m2.module_eval { obj.foo })
end

def test_refine_module_with_double_overriding
Expand Down Expand Up @@ -726,7 +731,6 @@ def foo
end

def test_inline_method_cache
skip "can't implement efficiently with the current implementation of refinements"
c = InlineMethodCache::C.new
f = Proc.new { c.foo }
assert_equal("original", f.call)
Expand Down Expand Up @@ -822,4 +826,31 @@ def test_circular_using_is_not_allowed
end
end
end

module RedifineRefinedMethod
class C
def foo
"original"
end
end

module M
refine C do
def foo
"refined"
end
end
end

class C
def foo
"redefined"
end
end
end

def test_redefine_refined_method
c = RedifineRefinedMethod::C.new
assert_equal("refined", RedifineRefinedMethod::M.module_eval { c.foo })
end
end
2 changes: 2 additions & 0 deletions vm.c
Expand Up @@ -1686,6 +1686,7 @@ rb_thread_mark(void *ptr)
rb_iseq_t *iseq = cfp->iseq;
rb_gc_mark(cfp->proc);
rb_gc_mark(cfp->self);
rb_gc_mark(cfp->klass);
if (iseq) {
rb_gc_mark(RUBY_VM_NORMAL_ISEQ_P(iseq) ? iseq->self : (VALUE)iseq);
}
Expand Down Expand Up @@ -2223,6 +2224,7 @@ Init_VM(void)
th->cfp->iseq = iseq;
th->cfp->pc = iseq->iseq_encoded;
th->cfp->self = th->top_self;
th->cfp->klass = Qnil;

/*
* The Binding of the top level scope
Expand Down

0 comments on commit 60d6038

Please sign in to comment.