Skip to content

Commit

Permalink
ssl: explicitly call rb_gc_mark() against SSLContext/SSLSocket objects
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
rhenium committed Oct 14, 2021
1 parent 6880dba commit 022b7ce
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions ext/openssl/ossl_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ static int ossl_sslctx_ex_ptr_idx;
static int ossl_sslctx_ex_store_p;
#endif

static void
ossl_sslctx_mark(void *ptr)
{
SSL_CTX *ctx = ptr;
rb_gc_mark((VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx));
}

static void
ossl_sslctx_free(void *ptr)
{
Expand All @@ -73,7 +80,7 @@ ossl_sslctx_free(void *ptr)
static const rb_data_type_t ossl_sslctx_type = {
"OpenSSL/SSL/CTX",
{
0, ossl_sslctx_free,
ossl_sslctx_mark, ossl_sslctx_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
};
Expand Down Expand Up @@ -1528,6 +1535,14 @@ ssl_started(SSL *ssl)
return SSL_get_fd(ssl) >= 0;
}

static void
ossl_ssl_mark(void *ptr)
{
SSL *ssl = ptr;
rb_gc_mark((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx));
rb_gc_mark((VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_vcb_idx));
}

static void
ossl_ssl_free(void *ssl)
{
Expand All @@ -1537,7 +1552,7 @@ ossl_ssl_free(void *ssl)
const rb_data_type_t ossl_ssl_type = {
"OpenSSL/SSL",
{
0, ossl_ssl_free,
ossl_ssl_mark, ossl_ssl_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
};
Expand Down

0 comments on commit 022b7ce

Please sign in to comment.