gh-99377: Add audit events for thread creation and clear (GH-99378)

This commit is contained in:
Steve Dower 2022-11-16 17:15:52 +00:00 committed by GitHub
parent 01fa907aa8
commit 19c1462e8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 7 deletions

View file

@ -873,14 +873,29 @@ PyThreadState *
PyThreadState_New(PyInterpreterState *interp)
{
PyThreadState *tstate = new_threadstate(interp);
_PyThreadState_SetCurrent(tstate);
if (tstate) {
_PyThreadState_SetCurrent(tstate);
if (PySys_Audit("cpython.PyThreadState_New", "K", tstate->id) < 0) {
PyThreadState_Clear(tstate);
_PyThreadState_DeleteCurrent(tstate);
return NULL;
}
}
return tstate;
}
PyThreadState *
_PyThreadState_Prealloc(PyInterpreterState *interp)
{
return new_threadstate(interp);
PyThreadState *tstate = new_threadstate(interp);
if (tstate) {
if (PySys_Audit("cpython.PyThreadState_New", "K", tstate->id) < 0) {
PyThreadState_Clear(tstate);
_PyThreadState_Delete(tstate, 0);
return NULL;
}
}
return tstate;
}
// We keep this around for (accidental) stable ABI compatibility.
@ -1028,6 +1043,10 @@ _PyInterpreterState_ClearModules(PyInterpreterState *interp)
void
PyThreadState_Clear(PyThreadState *tstate)
{
if (PySys_Audit("cpython.PyThreadState_Clear", "K", tstate->id) < 0) {
PyErr_WriteUnraisable(NULL);
}
int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
if (verbose && tstate->cframe->current_frame != NULL) {
@ -1545,16 +1564,16 @@ _PyGILState_Init(_PyRuntimeState *runtime)
PyStatus
_PyGILState_SetTstate(PyThreadState *tstate)
{
/* must init with valid states */
assert(tstate != NULL);
assert(tstate->interp != NULL);
if (!_Py_IsMainInterpreter(tstate->interp)) {
/* Currently, PyGILState is shared by all interpreters. The main
* interpreter is responsible to initialize it. */
return _PyStatus_OK();
}
/* must init with valid states */
assert(tstate != NULL);
assert(tstate->interp != NULL);
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
gilstate->autoInterpreterState = tstate->interp;