8000 gh-130019: Fix data race in _PyType_AllocNoTrack by colesbury · Pull Request #130058 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-130019: Fix data race in _PyType_AllocNoTrack #130058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-130019: Fix data race in _PyType_AllocNoTrack
The reference count fields, such as `ob_tid` and `ob_ref_shared`, may be
accessed concurrently in the free threading build by a `_Py_TryXGetRef`
or similar operation. The PyObject header fields will be initialized by
`_PyObject_Init`, so only call `memset()` to zero-initialize the remainder
of the allocation.
  • Loading branch information
colesbury committed Feb 12, 2025
commit 78e40c86a1ae8da39766cfe25423ccc753183750
4 changes: 3 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,9 @@ _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems)
if (PyType_IS_GC(type)) {
_PyObject_GC_Link(obj);
}
memset(obj, '\0', size);
// Zero out the object after the PyObject header. The header fields are
// initialized by _PyObject_Init[Var]().
memset((char *)obj + sizeof(PyObject), '\0', size - sizeof(PyObject));

if (type->tp_itemsize == 0) {
_PyObject_Init(obj, type);
Expand Down
5 changes: 3 additions & 2 deletions Python/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2309,11 +2309,12 @@ PyObject *
PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
{
size_t presize = _PyType_PreHeaderSize(tp);
PyObject *op = gc_alloc(tp, _PyObject_SIZE(tp) + extra_size, presize);
size_t size = _PyObject_SIZE(tp) + extra_size;
PyObject *op = gc_alloc(tp, size, presize);
if (op == NULL) {
return NULL;
}
memset(op, 0, _PyObject_SIZE(tp) + extra_size);
memset((char *)op + sizeof(PyObject), 0, size - sizeof(PyObject));
_PyObject_Init(op, tp);
return op;
}
Expand Down
5 changes: 3 additions & 2 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -2593,11 +2593,12 @@ PyObject *
PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *tp, size_t extra_size)
{
size_t presize = _PyType_PreHeaderSize(tp);
PyObject *op = gc_alloc(tp, _PyObject_SIZE(tp) + extra_size, presize);
size_t size = _PyObject_SIZE(tp) + extra_size;
PyObject *op = gc_alloc(tp, size, presize);
if (op == NULL) {
return NULL;
}
memset(op, 0, _PyObject_SIZE(tp) + extra_size);
memset((char *)op + sizeof(PyObject), 0, size - sizeof(PyObject));
_PyObject_Init(op, tp);
return op;
}
Expand Down
Loading
0