From c09506eb0b86693ca3f05aa5300ad59b13a19bc3 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Mon, 25 Nov 2024 08:51:37 +0000 Subject: [PATCH 1/2] make config frozen on initialize --- ext/openssl/ossl_config.c | 5 ++++- test/openssl/test_config.rb | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/openssl/ossl_config.c b/ext/openssl/ossl_config.c index 203d7ef29..3b387239a 100644 --- a/ext/openssl/ossl_config.c +++ b/ext/openssl/ossl_config.c @@ -87,6 +87,7 @@ config_s_parse(VALUE klass, VALUE str) bio = ossl_obj2bio(&str); config_load_bio(conf, bio); /* Consumes BIO */ + rb_obj_freeze(obj); return obj; } @@ -144,6 +145,7 @@ config_initialize(int argc, VALUE *argv, VALUE self) ossl_raise(eConfigError, "BIO_new_file"); config_load_bio(conf, bio); /* Consumes BIO */ } + rb_obj_freeze(self); return self; } @@ -158,6 +160,7 @@ config_initialize_copy(VALUE self, VALUE other) rb_check_frozen(self); bio = ossl_obj2bio(&str); config_load_bio(conf, bio); /* Consumes BIO */ + rb_obj_freeze(self); return self; } @@ -453,6 +456,6 @@ Init_ossl_config(void) * The default system configuration file for OpenSSL. */ path = CONF_get1_default_config_file(); - path_str = ossl_buf2str(path, rb_long2int(strlen(path))); + path_str = rb_obj_freeze(ossl_buf2str(path, rb_long2int(strlen(path)))); rb_define_const(cConfig, "DEFAULT_CONFIG_FILE", path_str); } diff --git a/test/openssl/test_config.rb b/test/openssl/test_config.rb index 6dbb9c613..298e99f41 100644 --- a/test/openssl/test_config.rb +++ b/test/openssl/test_config.rb @@ -188,6 +188,8 @@ def test_initialize c = OpenSSL::Config.new assert_equal("", c.to_s) assert_equal([], c.sections) + assert c.frozen? + assert c.dup.frozen? end def test_initialize_with_empty_file From bc680a9814b39164a74a6a942f9f8f6e40e0a408 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Mon, 25 Nov 2024 08:53:16 +0000 Subject: [PATCH 2/2] make configs shareable when frozen --- ext/openssl/ossl_config.c | 2 +- test/openssl/test_config.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ext/openssl/ossl_config.c b/ext/openssl/ossl_config.c index 3b387239a..ee2ff6786 100644 --- a/ext/openssl/ossl_config.c +++ b/ext/openssl/ossl_config.c @@ -22,7 +22,7 @@ static const rb_data_type_t ossl_config_type = { { 0, nconf_free, }, - 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE, }; CONF * diff --git a/test/openssl/test_config.rb b/test/openssl/test_config.rb index 298e99f41..6da308d8e 100644 --- a/test/openssl/test_config.rb +++ b/test/openssl/test_config.rb @@ -275,6 +275,15 @@ def test_dup assert_equal(@it.sections.sort, c2.sections.sort) end + if respond_to?(:ractor) + ractor + def test_ractor + assert Ractor.shareable?(@it) + assert Ractor.shareable?(OpenSSL::Config.parse("[empty]\n")) + assert Ractor.shareable?(OpenSSL::Config::DEFAULT_CONFIG_FILE) + end + end + private def in_tmpdir(*args)