Skip to content

gh-117657: Fix some races in tracebacks #121748

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 4 additions & 0 deletions Include/cpython/pyatomic_gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ static inline int
_Py_atomic_load_int_acquire(const int *obj)
{ return __atomic_load_n(obj, __ATOMIC_ACQUIRE); }

static inline void
_Py_atomic_store_uint8_release(uint8_t *obj, uint8_t value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_uint32_release(uint32_t *obj, uint32_t value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }
Expand Down
13 changes: 13 additions & 0 deletions Include/cpython/pyatomic_msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,19 @@ _Py_atomic_load_int_acquire(const int *obj)
#endif
}

static inline void
_Py_atomic_store_uint8_release(uint8_t *obj, uint8_t value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(uint8_t volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int8);
__stlr8((unsigned __int8 volatile *)obj, (unsigned __int8)value);
#else
# error "no implementation of _Py_atomic_store_uint32_release"
#endif
}

static inline void
_Py_atomic_store_uint32_release(uint32_t *obj, uint32_t value)
{
Expand Down
8 changes: 8 additions & 0 deletions Include/cpython/pyatomic_std.h
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,14 @@ _Py_atomic_load_int_acquire(const int *obj)
memory_order_acquire);
}

static inline void
_Py_atomic_store_uint8_release(uint8_t *obj, uint8_t value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(uint8_t)*)obj, value,
memory_order_release);
}

static inline void
_Py_atomic_store_uint32_release(uint32_t *obj, uint32_t value)
{
Expand Down
24 changes: 22 additions & 2 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,26 @@ BaseException_set_args(PyBaseExceptionObject *self, PyObject *val, void *Py_UNUS
}

static PyObject *
BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored))
BaseException_get_tb_lock_held(PyBaseExceptionObject *self, void *Py_UNUSED(ignored))
{
if (self->traceback == NULL) {
Py_RETURN_NONE;
}
return Py_NewRef(self->traceback);
}

static PyObject *
BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored))
{
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(self);
res = BaseException_get_tb_lock_held(self, NULL);
Py_END_CRITICAL_SECTION();
return res;
}

static int
BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(ignored))
BaseException_set_tb_lock_held(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(ignored))
{
if (tb == NULL) {
PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
Expand All @@ -337,6 +347,16 @@ BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(
return 0;
}

static int
BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(ignored))
{
int res;
Py_BEGIN_CRITICAL_SECTION(self);
res = BaseException_set_tb_lock_held(self, tb, NULL);
Py_END_CRITICAL_SECTION();
return res;
}

static PyObject *
BaseException_get_context(PyObject *self, void *Py_UNUSED(ignored))
{
Expand Down
8 changes: 4 additions & 4 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2360,12 +2360,12 @@ new_reference(PyObject *op)
#if !defined(Py_GIL_DISABLED)
op->ob_refcnt = 1;
#else
op->ob_tid = _Py_ThreadId();
_Py_atomic_store_uintptr_release(&op->ob_tid, _Py_ThreadId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to do this, at least not for now. I want to wait until we fix all the other races before we address new_reference because there are perf implications and the race is in practice benign.

The "release" memory order is also stronger (and more expensive on arm64) than necessary.

op->_padding = 0;
op->ob_mutex = (PyMutex){ 0 };
op->ob_gc_bits = 0;
op->ob_ref_local = 1;
op->ob_ref_shared = 0;
_Py_atomic_store_uint8_release(&op->ob_gc_bits, 0);
_Py_atomic_store_uint32_release(&op->ob_ref_local, 1);
_Py_atomic_store_ssize_release(&op->ob_ref_shared, 0);
#endif
#ifdef Py_TRACE_REFS
_Py_AddToAllObjects(op);
Expand Down
Loading