ruby-changes:64068
From: Koichi <ko1@a...>
Date: Thu, 10 Dec 2020 18:28:01 +0900 (JST)
Subject: [ruby-changes:64068] 72f1c43584 (master): ObjectSpace._id2ref should not support unshareable
https://git.ruby-lang.org/ruby.git/commit/?id=72f1c43584 From 72f1c43584df714a011864ca9fafc6e15ace392c Mon Sep 17 00:00:00 2001 From: Koichi Sasada <ko1@a...> Date: Thu, 10 Dec 2020 13:27:08 +0900 Subject: ObjectSpace._id2ref should not support unshareable ObjectSpace._id2ref(id) can return any objects even if they are unshareable, so this patch raises RangeError if it runs on multi-ractor mode and the found object is unshareable. diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 125b51b..2bbf823 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -919,6 +919,19 @@ assert_equal '0', %q{ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_ractor.rb#L919 }.take } +# ObjectSpace._id2ref can not handle unshareable objects with Ractors +assert_equal 'ok', %q{ + s = 'hello' + + Ractor.new s.object_id do |id ;s| + begin + s = ObjectSpace._id2ref(id) + rescue => e + :ok + end + end.take +} + # Ractor.make_shareable(obj) assert_equal 'true', %q{ class C diff --git a/gc.c b/gc.c index c583ed2..75255bd 100644 --- a/gc.c +++ b/gc.c @@ -3987,6 +3987,8 @@ id2ref_obj_tbl(rb_objspace_t *objspace, VALUE objid) https://github.com/ruby/ruby/blob/trunk/gc.c#L3987 * r = ObjectSpace._id2ref(s.object_id) #=> "I am a string" * r == s #=> true * + * On multi-ractor mode, if the object is not sharable, it raises + * RangeError. */ static VALUE @@ -4023,7 +4025,13 @@ id2ref(VALUE objid) https://github.com/ruby/ruby/blob/trunk/gc.c#L4025 if ((orig = id2ref_obj_tbl(objspace, objid)) != Qundef && is_live_object(objspace, orig)) { - return orig; + + if (!rb_multi_ractor_p() || rb_ractor_shareable_p(orig)) { + return orig; + } + else { + rb_raise(rb_eRangeError, "%+"PRIsVALUE" is id of the unshareable object on multi-ractor", rb_int2str(objid, 10)); + } } if (rb_int_ge(objid, objspace->next_object_id)) { -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/