Skip to content

Commit 022b7ce

Browse files
committed
ssl: explicitly call rb_gc_mark() against SSLContext/SSLSocket objects
We store the reverse reference to the Ruby object in the OpenSSL struct for use from OpenSSL callback functions. To prevent the Ruby object from being relocated by GC.compact, we must "pin" it by calling rb_gc_mark().
1 parent 6880dba commit 022b7ce

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

ext/openssl/ossl_ssl.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ static int ossl_sslctx_ex_ptr_idx;
5959
static int ossl_sslctx_ex_store_p;
6060
#endif
6161

62+
static void
63+
ossl_sslctx_mark(void *ptr)
64+
{
65+
SSL_CTX *ctx = ptr;
66+
rb_gc_mark((VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx));
67+
}
68+
6269
static void
6370
ossl_sslctx_free(void *ptr)
6471
{
@@ -73,7 +80,7 @@ ossl_sslctx_free(void *ptr)
7380
static const rb_data_type_t ossl_sslctx_type = {
7481
"OpenSSL/SSL/CTX",
7582
{
76-
0, ossl_sslctx_free,
83+
ossl_sslctx_mark, ossl_sslctx_free,
7784
},
7885
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
7986
};
@@ -1528,6 +1535,14 @@ ssl_started(SSL *ssl)
15281535
return SSL_get_fd(ssl) >= 0;
15291536
}
15301537

1538+
static void
1539+
ossl_ssl_mark(void *ptr)
1540+
{
1541+
SSL *ssl = ptr;
1542+
rb_gc_mark((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx));
1543+
rb_gc_mark((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_vcb_idx));
1544+
}
1545+
15311546
static void
15321547
ossl_ssl_free(void *ssl)
15331548
{
@@ -1537,7 +1552,7 @@ ossl_ssl_free(void *ssl)
15371552
const rb_data_type_t ossl_ssl_type = {
15381553
"OpenSSL/SSL",
15391554
{
1540-
0, ossl_ssl_free,
1555+
ossl_ssl_mark, ossl_ssl_free,
15411556
},
15421557
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
15431558
};

0 commit comments

Comments
 (0)