Repository navigation
Description
Bug report
Description
Modules/_pickle.c:8544 declares {Py_mod_gil, Py_MOD_GIL_NOT_USED}.
While _pickle_Pickler_dump_impl() (L5130) guards dump() with BEGIN_USING_PICKLER(self), _pickle_PicklerMemoProxy_clear_impl() (L5354-5360), _pickle_PicklerMemoProxy_copy_impl() (L5369-5403), and Pickler_set_memo() (L5505-5566, calling PyMemoTable_Del(self->memo) at L5557) acquire neither BEGIN_USING_PICKLER nor any critical section.
Under free-threaded CPython (--disable-gil), concurrently replacing or clearing p.memo while p.dump() is running frees self->memo out from under memo_put() / PyMemoTable_Set() / _PyMemoTable_Lookup(), causing a data race and heap use-after-free.
Reproducer
import io
import threading
import _pickle
items = [[i, f"item_{i}"] for i in range(60)]
for _ in range(20):
buf = io.BytesIO()
p = _pickle.Pickler(buf, protocol=4)
proxy = p.memo
bar = threading.Barrier(4)
def worker(tid, _p=p, _proxy=proxy):
bar.wait()
for _ in range(25):
try:
if tid == 0:
_p.dump(items)
elif tid == 1:
_ = _proxy.copy()
elif tid == 2:
_proxy.clear()
else:
_p.memo = {0: (0, items[0])}
except RuntimeError:
pass
ts = [threading.Thread(target=worker, args=(i,)) for i in range(4)]
for t in ts:
t.start()
for t in ts:
t.join()./configure --disable-gil --with-thread-sanitizer --with-pydebug
make -j
TSAN_OPTIONS="suppressions=Tools/tsan/suppressions_free_threading.txt:symbolize=1:halt_on_error=1" \
./python -X faulthandler reproduce.pyCaught by Stock Free-Threaded ThreadSanitizer (exit=66, WARNING: ThreadSanitizer: data race, full capture in output.txt), crashes Stock FT ASan (exit=1)
==================
WARNING: ThreadSanitizer: data race (pid=172)
Read of size 8 at 0x7fa747438b50 by thread T1:
#0 save_object Modules/_pickle.c:4790:52
#1 save Modules/_pickle.c:4975:12
#2 dump Modules/_pickle.c:5042:9
#3 _pickle_Pickler_dump_impl Modules/_pickle.c:5132:9
#4 _pickle_Pickler_dump Modules/clinic/_pickle.c.h:61:20
Previous write of size 8 at 0x7fa747438b50 by thread T4:
#0 Pickler_set_memo Modules/_pickle.c:5558:16
#1 getset_set Objects/descrobject.c:252:16
#2 _PyObject_GenericSetAttrWithDict Objects/object.c:2069:19
#3 PyObject_GenericSetAttr Objects/object.c:2140:12
#4 Pickler_setattr Modules/_pickle.c:5594:12
SUMMARY: ThreadSanitizer: data race Modules/_pickle.c:4790:52 in save_object
==================
Under Stock Free-Threaded AddressSanitizer (exit=1, full capture in output.txt):
=================================================================
==157==ERROR: AddressSanitizer: SEGV on unknown address 0x03e80000009d
#5 _PyMemoTable_Lookup Modules/_pickle.c:872:16
#6 PyMemoTable_Get Modules/_pickle.c:942:26
#7 save_object Modules/_pickle.c:4790:30
#8 save Modules/_pickle.c:4975:12
#13 dump Modules/_pickle.c:5042:9
#14 _pickle_Pickler_dump_impl Modules/_pickle.c:5132:9
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity