Skip to content

Commit bd9f5c3

Browse files
authored
Merge pull request #416 from ruby/sync
Sync from ruby master
2 parents f2d0046 + f2db943 commit bd9f5c3

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

ext/openssl/ossl.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,11 @@ print_mem_leaks(VALUE self)
497497
int ret;
498498
#endif
499499

500-
BN_CTX_free(ossl_bn_ctx);
501-
ossl_bn_ctx = NULL;
500+
#ifndef HAVE_RB_EXT_RACTOR_SAFE
501+
// for Ruby 2.x
502+
void ossl_bn_ctx_free(void); // ossl_bn.c
503+
ossl_bn_ctx_free();
504+
#endif
502505

503506
#if OPENSSL_VERSION_NUMBER >= 0x10100000
504507
ret = CRYPTO_mem_leaks_fp(stderr);
@@ -1126,6 +1129,10 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
11261129
void
11271130
Init_openssl(void)
11281131
{
1132+
#if HAVE_RB_EXT_RACTOR_SAFE
1133+
rb_ext_ractor_safe(true);
1134+
#endif
1135+
11291136
#undef rb_intern
11301137
/*
11311138
* Init timezone info

ext/openssl/ossl_bn.c

+61-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
/* modified by Michal Rokos <[email protected]> */
1111
#include "ossl.h"
1212

13+
#if HAVE_RB_EXT_RACTOR_SAFE
14+
#include <ruby/ractor.h>
15+
#endif
16+
1317
#define NewBN(klass) \
1418
TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
1519
#define SetBN(obj, bn) do { \
@@ -150,12 +154,58 @@ ossl_bn_value_ptr(volatile VALUE *ptr)
150154
/*
151155
* Private
152156
*/
153-
/*
154-
* BN_CTX - is used in more difficult math. ops
155-
* (Why just 1? Because Ruby itself isn't thread safe,
156-
* we don't need to care about threads)
157-
*/
158-
BN_CTX *ossl_bn_ctx;
157+
158+
#if HAVE_RB_EXT_RACTOR_SAFE
159+
void
160+
ossl_bn_ctx_free(void *ptr)
161+
{
162+
BN_CTX *ctx = (BN_CTX *)ptr;
163+
BN_CTX_free(ctx);
164+
}
165+
166+
struct rb_ractor_local_storage_type ossl_bn_ctx_key_type = {
167+
NULL, // mark
168+
ossl_bn_ctx_free,
169+
};
170+
171+
rb_ractor_local_key_t ossl_bn_ctx_key;
172+
173+
BN_CTX *
174+
ossl_bn_ctx_get(void)
175+
{
176+
// stored in ractor local storage
177+
178+
BN_CTX *ctx = rb_ractor_local_storage_ptr(ossl_bn_ctx_key);
179+
if (!ctx) {
180+
if (!(ctx = BN_CTX_new())) {
181+
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
182+
}
183+
rb_ractor_local_storage_ptr_set(ossl_bn_ctx_key, ctx);
184+
}
185+
return ctx;
186+
}
187+
#else
188+
// for ruby 2.x
189+
static BN_CTX *gv_ossl_bn_ctx;
190+
191+
BN_CTX *
192+
ossl_bn_ctx_get(void)
193+
{
194+
if (gv_ossl_bn_ctx == NULL) {
195+
if (!(gv_ossl_bn_ctx = BN_CTX_new())) {
196+
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
197+
}
198+
}
199+
return gv_ossl_bn_ctx;
200+
}
201+
202+
void
203+
ossl_bn_ctx_free(void)
204+
{
205+
BN_CTX_free(gv_ossl_bn_ctx);
206+
gv_ossl_bn_ctx = NULL;
207+
}
208+
#endif
159209

160210
static VALUE
161211
ossl_bn_alloc(VALUE klass)
@@ -1092,9 +1142,11 @@ Init_ossl_bn(void)
10921142
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
10931143
#endif
10941144

1095-
if (!(ossl_bn_ctx = BN_CTX_new())) {
1096-
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
1097-
}
1145+
#ifdef HAVE_RB_EXT_RACTOR_SAFE
1146+
ossl_bn_ctx_key = rb_ractor_local_storage_ptr_newkey(&ossl_bn_ctx_key_type);
1147+
#else
1148+
ossl_bn_ctx_get();
1149+
#endif
10981150

10991151
eBNError = rb_define_class_under(mOSSL, "BNError", eOSSLError);
11001152

ext/openssl/ossl_bn.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
extern VALUE cBN;
1414
extern VALUE eBNError;
1515

16-
extern BN_CTX *ossl_bn_ctx;
16+
BN_CTX *ossl_bn_ctx_get(void);
17+
#define ossl_bn_ctx ossl_bn_ctx_get()
1718

1819
#define GetBNPtr(obj) ossl_bn_value_ptr(&(obj))
1920

0 commit comments

Comments
 (0)