summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorNAKAMURA Usaku <usa@ruby-lang.org>2022-03-05 21:38:48 +0900
committerNAKAMURA Usaku <usa@ruby-lang.org>2022-03-05 21:38:48 +0900
commitf740ffb81f3ef11526add528583b0cbfce28af67 (patch)
tree18931c618c80b9ac334ca2faf1f7d8211a12ea61 /ext
parent1034b6e7ba9442320e16c260d634d4c64d5e62bf (diff)
merge revision(s) b3d62a77d928eff01268ca7fa1c1c0966702926d [Backport #17803]
[ruby/zlib] Synchronize access to zstream to prevent segfault in multithreaded use I'm not sure whether this handles all multithreaded use cases, but this handles the example that crashes almost immediately and does 10,000,000 total deflates using 100 separate threads. To prevent the tests from taking forever, the committed test for this uses only 10,000 deflates across 10 separate threads, which still causes a segfault in the previous implementation almost immediately. Fixes [Bug #17803] https://github.com/ruby/zlib/commit/4b1023b3f2 --- ext/zlib/zlib.c | 33 ++++++++++++++++++++++++++- test/zlib/test_zlib.rb | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-)
Diffstat (limited to 'ext')
-rw-r--r--ext/zlib/zlib.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
index 3b8e097da9..bfd55ffe34 100644
--- a/ext/zlib/zlib.c
+++ b/ext/zlib/zlib.c
@@ -530,6 +530,7 @@ struct zstream {
unsigned long flags;
VALUE buf;
VALUE input;
+ VALUE mutex;
z_stream stream;
const struct zstream_funcs {
int (*reset)(z_streamp);
@@ -602,6 +603,7 @@ zstream_init(struct zstream *z, const struct zstream_funcs *func)
z->flags = 0;
z->buf = Qnil;
z->input = Qnil;
+ z->mutex = rb_mutex_new();
z->stream.zalloc = zlib_mem_alloc;
z->stream.zfree = zlib_mem_free;
z->stream.opaque = Z_NULL;
@@ -631,7 +633,9 @@ zstream_expand_buffer(struct zstream *z)
rb_obj_reveal(z->buf, rb_cString);
+ rb_mutex_unlock(z->mutex);
rb_protect(rb_yield, z->buf, &state);
+ rb_mutex_lock(z->mutex);
z->buf = Qnil;
zstream_expand_buffer_into(z, ZSTREAM_AVAIL_OUT_STEP_MAX);
@@ -1025,7 +1029,7 @@ zstream_unblock_func(void *ptr)
}
static void
-zstream_run(struct zstream *z, Bytef *src, long len, int flush)
+zstream_run0(struct zstream *z, Bytef *src, long len, int flush)
{
struct zstream_run_args args;
int err;
@@ -1109,6 +1113,31 @@ loop:
rb_jump_tag(args.jump_state);
}
+struct zstream_run_synchronized_args {
+ struct zstream *z;
+ Bytef *src;
+ long len;
+ int flush;
+};
+
+static VALUE
+zstream_run_synchronized(VALUE value_arg)
+{
+ struct zstream_run_synchronized_args *run_args = (struct zstream_run_synchronized_args *)value_arg;
+ zstream_run0(run_args->z, run_args->src, run_args->len, run_args->flush);
+ return Qnil;
+}
+
+static void
+zstream_run(struct zstream *z, Bytef *src, long len, int flush)
+{
+ struct zstream_run_synchronized_args run_args;
+ run_args.z = z;
+ run_args.src = src;
+ run_args.len = len;
+ run_args.flush = flush;
+ rb_mutex_synchronize(z->mutex, zstream_run_synchronized, (VALUE)&run_args);
+}
static VALUE
zstream_sync(struct zstream *z, Bytef *src, long len)
{
@@ -1154,6 +1183,7 @@ zstream_mark(void *p)
struct zstream *z = p;
rb_gc_mark(z->buf);
rb_gc_mark(z->input);
+ rb_gc_mark(z->mutex);
}
static void