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 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 23:24:34 -05:00
parent 9be72ae9c0
commit 61e1ca8bad

View File

@@ -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) {