Skip to content

gh-99377: Add audit events for thread creation and clear #99378

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 6 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1239,12 +1239,25 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
The global interpreter lock need not be held, but may be held if it is
necessary to serialize calls to this function.

.. audit-event:: cpython.PyThreadState_New id c.PyThreadState_New

Raise an auditing event ``cpython.PyThreadState_New`` with Python's thread
id as the argument. The event will be raised from the thread creating the new
``PyThreadState``, which may not be the new thread.


.. c:function:: void PyThreadState_Clear(PyThreadState *tstate)

Reset all information in a thread state object. The global interpreter lock
must be held.

.. audit-event:: cpython.PyThreadState_Clear id c.PyThreadState_Clear

Raise an auditing event ``cpython.PyThreadState_Clear`` with Python's
thread id as the argument. The event may be raised from a different thread
than the one being cleared. Exceptions raised from a hook will be treated
as unraisable and will not abort the operation.

.. versionchanged:: 3.9
This function now calls the :c:member:`PyThreadState.on_delete` callback.
Previously, that happened in :c:func:`PyThreadState_Delete`.
Expand Down
2 changes: 2 additions & 0 deletions Doc/library/_thread.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ This module defines the following constants and functions:
When the function raises a :exc:`SystemExit` exception, it is silently
ignored.

.. audit-event:: _thread.start_new_thread function,args,kwargs start_new_thread

.. versionchanged:: 3.8
:func:`sys.unraisablehook` is now used to handle unhandled exceptions.

Expand Down
42 changes: 42 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,48 @@ def hook(event, args):
sys._getframe()


def test_threading():
import _thread

def hook(event, args):
if event.startswith(("_thread.", "cpython.PyThreadState", "test.")):
print(event, args)

sys.addaudithook(hook)

lock = _thread.allocate_lock()
lock.acquire()

class test_func:
def __repr__(self): return "<test_func>"
def __call__(self):
sys.audit("test.test_func")
lock.release()

i = _thread.start_new_thread(test_func(), ())
lock.acquire()


def test_threading_abort():
# Ensures that aborting PyThreadState_New raises the correct exception
import _thread

class ThreadNewAbortError(Exception):
pass

def hook(event, args):
if event == "cpython.PyThreadState_New":
raise ThreadNewAbortError()

sys.addaudithook(hook)

try:
_thread.start_new_thread(lambda: None, ())
except ThreadNewAbortError:
# Other exceptions are raised and the test will fail
pass


def test_wmi_exec_query():
import _wmi

Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ def test_sys_getframe(self):

self.assertEqual(actual, expected)


def test_threading(self):
returncode, events, stderr = self.run_python("test_threading")
if returncode:
self.fail(stderr)

if support.verbose:
print(*events, sep='\n')
actual = [(ev[0], ev[2]) for ev in events]
expected = [
("_thread.start_new_thread", "(<test_func>, (), None)"),
("cpython.PyThreadState_New", "(2,)"),
("test.test_func", "()"),
("cpython.PyThreadState_Clear", "(2,)"),
]

self.assertEqual(actual, expected)

def test_threading_abort(self):
# Ensures that aborting PyThreadState_New raises the correct exception
returncode, events, stderr = self.run_python("test_threading_abort")
if returncode:
self.fail(stderr)


def test_wmi_exec_query(self):
import_helper.import_module("_wmi")
returncode, events, stderr = self.run_python("test_wmi_exec_query")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add audit events for thread creation and clear operations.
10 changes: 9 additions & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
return NULL;
}

if (PySys_Audit("_thread.start_new_thread", "OOO",
func, args, kwargs ? kwargs : Py_None) < 0) {
return NULL;
}

PyInterpreterState *interp = _PyInterpreterState_GET();
if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_THREADS)) {
PyErr_SetString(PyExc_RuntimeError,
Expand All @@ -1160,7 +1165,10 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
boot->tstate = _PyThreadState_Prealloc(boot->interp);
if (boot->tstate == NULL) {
PyMem_Free(boot);
return PyErr_NoMemory();
if (!PyErr_Occurred()) {
return PyErr_NoMemory();
}
return NULL;
}
boot->runtime = runtime;
boot->func = Py_NewRef(func);
Expand Down
31 changes: 25 additions & 6 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,14 +874,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.
Expand Down Expand Up @@ -1029,6 +1044,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) {
Expand Down Expand Up @@ -1546,16 +1565,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;
Expand Down