Skip to content

Commit 19c72d2

Browse files
authored
gh-132657: Use stronger memory ordering for so->mask. (gh-142735)
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`.
1 parent d844d22 commit 19c72d2

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Objects/setobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ set_lookkey_threadsafe(PySetObject *so, PyObject *key, Py_hash_t hash)
445445
}
446446
ensure_shared_on_read(so);
447447
setentry *table = FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table);
448-
size_t mask = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->mask);
448+
size_t mask = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(so->mask);
449449
if (table == NULL || table != FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table)) {
450450
return set_lookkey(so, key, hash, &entry);
451451
}
@@ -532,7 +532,7 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
532532
assert(newtable != oldtable);
533533
set_zero_table(newtable, newsize);
534534
FT_ATOMIC_STORE_PTR_RELEASE(so->table, NULL);
535-
FT_ATOMIC_STORE_SSIZE_RELAXED(so->mask, newsize - 1);
535+
FT_ATOMIC_STORE_SSIZE_RELEASE(so->mask, newsize - 1);
536536

537537
/* Copy the data over; this is refcount-neutral for active entries;
538538
dummy entries aren't copied over, of course */
@@ -634,7 +634,7 @@ set_empty_to_minsize(PySetObject *so)
634634
set_zero_table(so->smalltable, PySet_MINSIZE);
635635
so->fill = 0;
636636
FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, 0);
637-
FT_ATOMIC_STORE_SSIZE_RELAXED(so->mask, PySet_MINSIZE - 1);
637+
FT_ATOMIC_STORE_SSIZE_RELEASE(so->mask, PySet_MINSIZE - 1);
638638
FT_ATOMIC_STORE_SSIZE_RELAXED(so->hash, -1);
639639
FT_ATOMIC_STORE_PTR_RELEASE(so->table, so->smalltable);
640640
}
@@ -1449,8 +1449,8 @@ set_swap_bodies(PySetObject *a, PySetObject *b)
14491449
FT_ATOMIC_STORE_SSIZE_RELAXED(a->used, b->used);
14501450
FT_ATOMIC_STORE_SSIZE_RELAXED(b->used, t);
14511451
t = a->mask;
1452-
FT_ATOMIC_STORE_SSIZE_RELAXED(a->mask, b->mask);
1453-
FT_ATOMIC_STORE_SSIZE_RELAXED(b->mask, t);
1452+
FT_ATOMIC_STORE_SSIZE_RELEASE(a->mask, b->mask);
1453+
FT_ATOMIC_STORE_SSIZE_RELEASE(b->mask, t);
14541454

14551455
u = a_table;
14561456
if (a_table == a->smalltable)

0 commit comments

Comments
 (0)