gh-132657: Use stronger memory ordering for so->mask. (gh-142735)
Some checks are pending
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / iOS (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run

We need to use release/acquire ordering for the 'mask' member of the set
structure. Without this, `set_lookkey_threadsafe()` could be looking at
the old value of `table` but the new value of `mask`.
This commit is contained in:
Neil Schemenauer 2025-12-14 20:27:37 -08:00 committed by GitHub
parent d844d22cb0
commit 19c72d23fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -445,7 +445,7 @@ set_lookkey_threadsafe(PySetObject *so, PyObject *key, Py_hash_t hash)
}
ensure_shared_on_read(so);
setentry *table = FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table);
size_t mask = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->mask);
size_t mask = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(so->mask);
if (table == NULL || table != FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table)) {
return set_lookkey(so, key, hash, &entry);
}
@ -532,7 +532,7 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
assert(newtable != oldtable);
set_zero_table(newtable, newsize);
FT_ATOMIC_STORE_PTR_RELEASE(so->table, NULL);
FT_ATOMIC_STORE_SSIZE_RELAXED(so->mask, newsize - 1);
FT_ATOMIC_STORE_SSIZE_RELEASE(so->mask, newsize - 1);
/* Copy the data over; this is refcount-neutral for active entries;
dummy entries aren't copied over, of course */
@ -634,7 +634,7 @@ set_empty_to_minsize(PySetObject *so)
set_zero_table(so->smalltable, PySet_MINSIZE);
so->fill = 0;
FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, 0);
FT_ATOMIC_STORE_SSIZE_RELAXED(so->mask, PySet_MINSIZE - 1);
FT_ATOMIC_STORE_SSIZE_RELEASE(so->mask, PySet_MINSIZE - 1);
FT_ATOMIC_STORE_SSIZE_RELAXED(so->hash, -1);
FT_ATOMIC_STORE_PTR_RELEASE(so->table, so->smalltable);
}
@ -1449,8 +1449,8 @@ set_swap_bodies(PySetObject *a, PySetObject *b)
FT_ATOMIC_STORE_SSIZE_RELAXED(a->used, b->used);
FT_ATOMIC_STORE_SSIZE_RELAXED(b->used, t);
t = a->mask;
FT_ATOMIC_STORE_SSIZE_RELAXED(a->mask, b->mask);
FT_ATOMIC_STORE_SSIZE_RELAXED(b->mask, t);
FT_ATOMIC_STORE_SSIZE_RELEASE(a->mask, b->mask);
FT_ATOMIC_STORE_SSIZE_RELEASE(b->mask, t);
u = a_table;
if (a_table == a->smalltable)