From 61e1ca8bad73a87324c967eb2b69c5d811ce37a6 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 23:24:34 -0500 Subject: [PATCH] Drop a zstd stream's slot by its stream state under one hold of the lock, not by a pointer found earlier. Between the lookup and the drop another thread could take the spent slot for its own stream; the drop then cleared that thread's slot, and a third thread could take it as free and reset the decoder context still in use. Co-Authored-By: Claude Fable 5.2 --- runtime/ov_zstd.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/runtime/ov_zstd.c b/runtime/ov_zstd.c index 554c51fd..db64c8fa 100644 --- a/runtime/ov_zstd.c +++ b/runtime/ov_zstd.c @@ -348,6 +348,20 @@ static void stream_drop(Stream *st) { plat_mutex_unlock(&stream_lock); } +// Drops the slot of stream state s, if it has one. Found and cleared under one hold of the lock: between a +// stream_find and a stream_drop another thread can take a spent slot for its own stream (stream_take), and the drop +// then cleared that thread's slot, which a third thread took as free and reset the decoder context in use. +static void stream_drop_of(uint64_t s) { + plat_mutex_lock(&stream_lock); + for (int i = 0; i < NSTREAM; i++) + if (streams[i].s == s) { + free(streams[i].shadow); + streams[i].shadow = NULL; + streams[i].s = 0, streams[i].spent = 0; + } + plat_mutex_unlock(&stream_lock); +} + static void stream_spent(Stream *st) { plat_mutex_lock(&stream_lock); st->spent = 1; @@ -560,7 +574,7 @@ static void host_stream(Cpu *c) { if (trusted() && s && s < RT_ADDRESS_LIMIT - 0x38) { uint8_t ph = *(const uint8_t *)gp(s); if (ph == 1) { - stream_drop(stream_find(s)); // a stream the game gave up without ending it + stream_drop_of(s); // a stream the game gave up without ending it st = stream_begin(s, src, size, ddict); if (!st) __atomic_add_fetch(&SS.guest, 1, __ATOMIC_RELAXED); } else if (ph == 2) {