[前][次][番号順一覧][スレッド一覧]

ruby-changes:70505

From: Koichi <ko1@a...>
Date: Fri, 24 Dec 2021 13:52:17 +0900 (JST)
Subject: [ruby-changes:70505] 6050e3e2a6 (master): @@cv is not accessible from non-main ractors

https://git.ruby-lang.org/ruby.git/commit/?id=6050e3e2a6

From 6050e3e2a6ce2269c56fa74bc5b75a94d064b61f Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Fri, 24 Dec 2021 12:26:21 +0900
Subject: @@cv is not accessible from non-main ractors

Class variables (@@cv) is not accessible from non-main ractors.
But without this patch cached @@cv can be read.

fix [Bug #18128]
---
 bootstraptest/test_ractor.rb | 22 ++++++++++++++++++++++
 vm_insnhelper.c              |  4 +++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb
index 5d9edb26d6d..b29db7ab0eb 100644
--- a/bootstraptest/test_ractor.rb
+++ b/bootstraptest/test_ractor.rb
@@ -1101,6 +1101,28 @@ assert_equal 'can not access class variables from non-main Ractors', %q{ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_ractor.rb#L1101
   end
 }
 
+# also cached cvar in shareable-objects are not allowed to access from non-main Ractor
+assert_equal 'can not access class variables from non-main Ractors', %q{
+  class C
+    @@cv = 'str'
+    def self.cv
+      @@cv
+    end
+  end
+
+  C.cv # cache
+
+  r = Ractor.new do
+    C.cv
+  end
+
+  begin
+    r.take
+  rescue Ractor::RemoteError => e
+    e.cause.message
+  end
+}
+
 # Getting non-shareable objects via constants by other Ractors is not allowed
 assert_equal 'can not access non-shareable objects in constant C::CONST by non-main Ractor.', %q{
   class C
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 37229b5dc06..e01d39de77a 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -1320,7 +1320,9 @@ vm_getclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *reg_cfp, ID https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1320
         VALUE v = Qundef;
         RB_DEBUG_COUNTER_INC(cvar_read_inline_hit);
 
-        if (st_lookup(RCLASS_IV_TBL(ic->entry->class_value), (st_data_t)id, &v)) {
+        if (st_lookup(RCLASS_IV_TBL(ic->entry->class_value), (st_data_t)id, &v) &&
+            LIKELY(rb_ractor_main_p())) {
+
             return v;
         }
     }
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]