gh-133931: Introduce _PyObject_XSetRefDelayed to replace Py_XSETREF (gh-134377)
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 / 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 / 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

This commit is contained in:
Donghee Na 2025-06-18 08:36:02 +09:00 committed by GitHub
parent cb39410111
commit 52be7f445e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 104 additions and 21 deletions

View file

@ -704,7 +704,8 @@ static PyObject *
gen_get_name(PyObject *self, void *Py_UNUSED(ignored))
{
PyGenObject *op = _PyGen_CAST(self);
return Py_NewRef(op->gi_name);
PyObject *name = FT_ATOMIC_LOAD_PTR_ACQUIRE(op->gi_name);
return Py_NewRef(name);
}
static int
@ -718,7 +719,11 @@ gen_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
"__name__ must be set to a string object");
return -1;
}
Py_XSETREF(op->gi_name, Py_NewRef(value));
Py_BEGIN_CRITICAL_SECTION(self);
// gh-133931: To prevent use-after-free from other threads that reference
// the gi_name.
_PyObject_XSetRefDelayed(&op->gi_name, Py_NewRef(value));
Py_END_CRITICAL_SECTION();
return 0;
}
@ -726,7 +731,8 @@ static PyObject *
gen_get_qualname(PyObject *self, void *Py_UNUSED(ignored))
{
PyGenObject *op = _PyGen_CAST(self);
return Py_NewRef(op->gi_qualname);
PyObject *qualname = FT_ATOMIC_LOAD_PTR_ACQUIRE(op->gi_qualname);
return Py_NewRef(qualname);
}
static int
@ -740,7 +746,11 @@ gen_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
"__qualname__ must be set to a string object");
return -1;
}
Py_XSETREF(op->gi_qualname, Py_NewRef(value));
Py_BEGIN_CRITICAL_SECTION(self);
// gh-133931: To prevent use-after-free from other threads that reference
// the gi_qualname.
_PyObject_XSetRefDelayed(&op->gi_qualname, Py_NewRef(value));
Py_END_CRITICAL_SECTION();
return 0;
}

View file

@ -1231,6 +1231,21 @@ _PyObject_XDecRefDelayed(PyObject *ptr)
}
#endif
#ifdef Py_GIL_DISABLED
void
_PyObject_XSetRefDelayed(PyObject **ptr, PyObject *value)
{
PyObject *old = *ptr;
FT_ATOMIC_STORE_PTR_RELEASE(*ptr, value);
if (old == NULL) {
return;
}
if (!_Py_IsImmortal(old)) {
_PyObject_XDecRefDelayed(old);
}
}
#endif
static struct _mem_work_chunk *
work_queue_first(struct llist_node *head)
{

View file

@ -3967,13 +3967,9 @@ _PyObject_SetDict(PyObject *obj, PyObject *value)
return -1;
}
Py_BEGIN_CRITICAL_SECTION(obj);
PyObject *olddict = *dictptr;
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
#ifdef Py_GIL_DISABLED
_PyObject_XDecRefDelayed(olddict);
#else
Py_XDECREF(olddict);
#endif
// gh-133980: To prevent use-after-free from other threads that reference
// the __dict__
_PyObject_XSetRefDelayed(dictptr, Py_NewRef(value));
Py_END_CRITICAL_SECTION();
return 0;
}