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

ruby-changes:70269

From: Koichi <ko1@a...>
Date: Fri, 17 Dec 2021 15:47:10 +0900 (JST)
Subject: [ruby-changes:70269] 37bd795cf8 (master): `ENV` ivars should not be accessible from ractors

https://git.ruby-lang.org/ruby.git/commit/?id=37bd795cf8

From 37bd795cf8bc9681fccaf9b2d42292b14610a310 Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Fri, 17 Dec 2021 14:22:14 +0900
Subject: `ENV` ivars should not be accessible from ractors

The `ENV` object can have instance variables like other objects,
but they should be accessed only on the main ractor.

fix https://github.com/ruby/ruby/pull/5263#issuecomment-995585766
---
 hash.c                | 13 ++++++++++++-
 test/ruby/test_env.rb | 30 ++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/hash.c b/hash.c
index deb5e77872f..da9af425c14 100644
--- a/hash.c
+++ b/hash.c
@@ -6653,6 +6653,17 @@ env_dup(VALUE obj) https://github.com/ruby/ruby/blob/trunk/hash.c#L6653
     rb_raise(rb_eTypeError, "Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
 }
 
+const rb_data_type_t env_data_type = {
+    "ENV",
+    {
+        NULL,
+        RUBY_DEFAULT_FREE,
+        NULL,
+        NULL,
+    },
+    0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
+};
+
 /*
  *  A \Hash maps each of its unique keys to a specific value.
  *
@@ -7308,7 +7319,7 @@ Init_Hash(void) https://github.com/ruby/ruby/blob/trunk/hash.c#L7319
      * envtbl = rb_define_class("ENV", rb_cObject);
      */
     origenviron = environ;
-    envtbl = rb_obj_alloc(rb_cObject);
+    envtbl = TypedData_Wrap_Struct(rb_cObject, &env_data_type, NULL);
     rb_extend_object(envtbl, rb_mEnumerable);
     FL_SET_RAW(envtbl, RUBY_FL_SHAREABLE);
 
diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb
index 442fc60f7ee..87ccd5102b7 100644
--- a/test/ruby/test_env.rb
+++ b/test/ruby/test_env.rb
@@ -1434,6 +1434,36 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L1434
     end;
   end
 
+  def test_ivar_in_env_should_not_be_access_from_non_main_ractors
+    assert_ractor <<~RUBY
+    ENV.instance_eval{ @a = "hello" }
+    assert_equal "hello", ENV.instance_variable_get(:@a)
+
+    r_get =  Ractor.new do
+      ENV.instance_variable_get(:@a)
+    rescue Ractor::IsolationError => e
+      e
+    end
+    assert_equal Ractor::IsolationError, r_get.take.class
+
+    r_get =  Ractor.new do
+      ENV.instance_eval{ @a }
+    rescue Ractor::IsolationError => e
+      e
+    end
+
+    assert_equal Ractor::IsolationError, r_get.take.class
+
+    r_set = Ractor.new do
+      ENV.instance_eval{ @b = "hello" }
+    rescue Ractor::IsolationError => e
+      e
+    end
+
+    assert_equal Ractor::IsolationError, r_set.take.class
+    RUBY
+  end
+
   if RUBY_PLATFORM =~ /bccwin|mswin|mingw/
     def test_memory_leak_aset
       bug9977 = '[ruby-dev:48323] [Bug #9977]'
-- 
cgit v1.2.1


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

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