Skip to content

Commit be95cb0

Browse files
committed
Reimplement executor management so that invalidating an executor does not cause arbitrary code to run
1 parent 16a305f commit be95cb0

File tree

5 files changed

+90
-106
lines changed

5 files changed

+90
-106
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,6 @@ struct _is {
947947
struct _PyExecutorObject *executor_deletion_list_head;
948948
struct _PyExecutorObject *cold_executor;
949949
struct _PyExecutorObject *cold_dynamic_executor;
950-
int executor_deletion_list_remaining_capacity;
951950
size_t executor_creation_counter;
952951
_rare_events rare_events;
953952
PyDict_WatchCallback builtins_dict_watcher;

Include/internal/pycore_optimizer.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ typedef struct {
2525
uint8_t opcode;
2626
uint8_t oparg;
2727
uint8_t valid;
28-
uint8_t linked;
2928
uint8_t chain_depth; // Must be big enough for MAX_CHAIN_DEPTH - 1.
3029
bool warm;
3130
int32_t index; // Index of ENTER_EXECUTOR (if code isn't NULL, below).
@@ -55,11 +54,6 @@ typedef struct _PyExecutorObject {
5554
_PyExitData exits[1];
5655
} _PyExecutorObject;
5756

58-
/* If pending deletion list gets large enough, then scan,
59-
* and free any executors that aren't executing
60-
* i.e. any that aren't a thread's current_executor. */
61-
#define EXECUTOR_DELETE_LIST_MAX 100
62-
6357
// Export for '_opcode' shared extension (JIT compiler).
6458
PyAPI_FUNC(_PyExecutorObject*) _Py_GetExecutor(PyCodeObject *code, int offset);
6559

@@ -80,7 +74,6 @@ PyAPI_FUNC(void) _Py_Executors_InvalidateCold(PyInterpreterState *interp);
8074
#else
8175
# define _Py_Executors_InvalidateDependency(A, B, C) ((void)0)
8276
# define _Py_Executors_InvalidateAll(A, B) ((void)0)
83-
# define _Py_Executors_InvalidateCold(A) ((void)0)
8477

8578
#endif
8679

Python/ceval_gil.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,13 +1397,19 @@ _Py_HandlePending(PyThreadState *tstate)
13971397
if ((breaker & _PY_GC_SCHEDULED_BIT) != 0) {
13981398
_Py_unset_eval_breaker_bit(tstate, _PY_GC_SCHEDULED_BIT);
13991399
_Py_RunGC(tstate);
1400+
#ifdef _Py_TIER2
1401+
_Py_ClearExecutorDeletionList(tstate->interp);
1402+
#endif
14001403
}
14011404

1405+
#ifdef _Py_TIER2
14021406
if ((breaker & _PY_EVAL_JIT_INVALIDATE_COLD_BIT) != 0) {
14031407
_Py_unset_eval_breaker_bit(tstate, _PY_EVAL_JIT_INVALIDATE_COLD_BIT);
14041408
_Py_Executors_InvalidateCold(tstate->interp);
14051409
tstate->interp->executor_creation_counter = JIT_CLEANUP_THRESHOLD;
1410+
_Py_ClearExecutorDeletionList(tstate->interp);
14061411
}
1412+
#endif
14071413

14081414
/* GIL drop request */
14091415
if ((breaker & _PY_GIL_DROP_REQUEST_BIT) != 0) {

Python/optimizer.c

Lines changed: 84 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ get_oparg(PyObject *self, PyObject *Py_UNUSED(ignored))
246246
///////////////////// Experimental UOp Optimizer /////////////////////
247247

248248
static int executor_clear(PyObject *executor);
249-
static void unlink_executor(_PyExecutorObject *executor);
250-
251249

252250
void
253251
_PyExecutor_Free(_PyExecutorObject *self)
@@ -258,63 +256,76 @@ _PyExecutor_Free(_PyExecutorObject *self)
258256
PyObject_GC_Del(self);
259257
}
260258

259+
static void executor_invalidate(PyObject *op);
260+
261+
static void
262+
executor_clear_exits(_PyExecutorObject *executor)
263+
{
264+
_PyExecutorObject *cold = _PyExecutor_GetColdExecutor();
265+
_PyExecutorObject *cold_dynamic = _PyExecutor_GetColdDynamicExecutor();
266+
for (uint32_t i = 0; i < executor->exit_count; i++) {
267+
_PyExitData *exit = &executor->exits[i];
268+
exit->temperature = initial_unreachable_backoff_counter();
269+
_PyExecutorObject *old = executor->exits[i].executor;
270+
exit->executor = exit->is_dynamic ? cold_dynamic : cold;
271+
Py_DECREF(old);
272+
}
273+
}
274+
275+
261276
void
262277
_Py_ClearExecutorDeletionList(PyInterpreterState *interp)
263278
{
279+
if (interp->executor_deletion_list_head == NULL) {
280+
return;
281+
}
264282
_PyRuntimeState *runtime = &_PyRuntime;
265283
HEAD_LOCK(runtime);
266284
PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
285+
while (ts) {
286+
_PyExecutorObject *current = (_PyExecutorObject *)ts->current_executor;
287+
Py_XINCREF(current);
288+
ts = ts->next;
289+
}
267290
HEAD_UNLOCK(runtime);
291+
_PyExecutorObject *keep_list = NULL;
292+
do {
293+
_PyExecutorObject *exec = interp->executor_deletion_list_head;
294+
interp->executor_deletion_list_head = exec->vm_data.links.next;
295+
if (Py_REFCNT(exec) == 0) {
296+
_PyExecutor_Free(exec);
297+
} else {
298+
exec->vm_data.links.next = keep_list;
299+
keep_list = exec;
300+
}
301+
} while (interp->executor_deletion_list_head != NULL);
302+
interp->executor_deletion_list_head = keep_list;
303+
HEAD_LOCK(runtime);
304+
ts = PyInterpreterState_ThreadHead(interp);
268305
while (ts) {
269306
_PyExecutorObject *current = (_PyExecutorObject *)ts->current_executor;
270307
if (current != NULL) {
271-
/* Anything in this list will be unlinked, so we can reuse the
272-
* linked field as a reachability marker. */
273-
current->vm_data.linked = 1;
308+
_Py_DECREF_NO_DEALLOC((PyObject *)current);
274309
}
275-
HEAD_LOCK(runtime);
276-
ts = PyThreadState_Next(ts);
277-
HEAD_UNLOCK(runtime);
278-
}
279-
_PyExecutorObject **prev_to_next_ptr = &interp->executor_deletion_list_head;
280-
_PyExecutorObject *exec = *prev_to_next_ptr;
281-
while (exec != NULL) {
282-
if (exec->vm_data.linked) {
283-
// This executor is currently executing
284-
exec->vm_data.linked = 0;
285-
prev_to_next_ptr = &exec->vm_data.links.next;
286-
}
287-
else {
288-
*prev_to_next_ptr = exec->vm_data.links.next;
289-
_PyExecutor_Free(exec);
290-
}
291-
exec = *prev_to_next_ptr;
310+
ts = ts->next;
292311
}
293-
interp->executor_deletion_list_remaining_capacity = EXECUTOR_DELETE_LIST_MAX;
312+
HEAD_UNLOCK(runtime);
294313
}
295314

296315
static void
297316
add_to_pending_deletion_list(_PyExecutorObject *self)
298317
{
299318
PyInterpreterState *interp = PyInterpreterState_Get();
319+
self->vm_data.links.previous = NULL;
300320
self->vm_data.links.next = interp->executor_deletion_list_head;
301321
interp->executor_deletion_list_head = self;
302-
if (interp->executor_deletion_list_remaining_capacity > 0) {
303-
interp->executor_deletion_list_remaining_capacity--;
304-
}
305-
else {
306-
_Py_ClearExecutorDeletionList(interp);
307-
}
308322
}
309323

310324
static void
311325
uop_dealloc(PyObject *op) {
312326
_PyExecutorObject *self = _PyExecutorObject_CAST(op);
313-
_PyObject_GC_UNTRACK(self);
327+
executor_invalidate(op);
314328
assert(self->vm_data.code == NULL);
315-
unlink_executor(self);
316-
// Once unlinked it becomes impossible to invalidate an executor, so do it here.
317-
self->vm_data.valid = 0;
318329
add_to_pending_deletion_list(self);
319330
}
320331

@@ -1619,19 +1630,14 @@ link_executor(_PyExecutorObject *executor)
16191630
head->vm_data.links.previous = executor;
16201631
interp->executor_list_head = executor;
16211632
}
1622-
executor->vm_data.linked = true;
16231633
/* executor_list_head must be first in list */
16241634
assert(interp->executor_list_head->vm_data.links.previous == NULL);
16251635
}
16261636

16271637
static void
16281638
unlink_executor(_PyExecutorObject *executor)
16291639
{
1630-
if (!executor->vm_data.linked) {
1631-
return;
1632-
}
16331640
_PyExecutorLinkListNode *links = &executor->vm_data.links;
1634-
assert(executor->vm_data.valid);
16351641
_PyExecutorObject *next = links->next;
16361642
_PyExecutorObject *prev = links->previous;
16371643
if (next != NULL) {
@@ -1646,7 +1652,6 @@ unlink_executor(_PyExecutorObject *executor)
16461652
assert(interp->executor_list_head == executor);
16471653
interp->executor_list_head = next;
16481654
}
1649-
executor->vm_data.linked = false;
16501655
}
16511656

16521657
/* This must be called by optimizers before using the executor */
@@ -1660,61 +1665,47 @@ _Py_ExecutorInit(_PyExecutorObject *executor, const _PyBloomFilter *dependency_s
16601665
link_executor(executor);
16611666
}
16621667

1663-
_PyExecutorObject *
1664-
_PyExecutor_GetColdExecutor(void)
1668+
static _PyExecutorObject *
1669+
make_cold_executor(uint16_t opcode)
16651670
{
1666-
PyInterpreterState *interp = _PyInterpreterState_GET();
1667-
if (interp->cold_executor != NULL) {
1668-
return interp->cold_executor;
1669-
}
16701671
_PyExecutorObject *cold = allocate_executor(0, 1);
16711672
if (cold == NULL) {
16721673
Py_FatalError("Cannot allocate core JIT code");
16731674
}
1674-
((_PyUOpInstruction *)cold->trace)->opcode = _COLD_EXIT_r00;
1675-
#ifdef _Py_JIT
1676-
cold->jit_code = NULL;
1677-
cold->jit_size = 0;
1675+
((_PyUOpInstruction *)cold->trace)->opcode = opcode;
16781676
// This is initialized to true so we can prevent the executor
16791677
// from being immediately detected as cold and invalidated.
16801678
cold->vm_data.warm = true;
1679+
#ifdef _Py_JIT
1680+
cold->jit_code = NULL;
1681+
cold->jit_size = 0;
16811682
if (_PyJIT_Compile(cold, cold->trace, 1)) {
16821683
Py_DECREF(cold);
16831684
Py_FatalError("Cannot allocate core JIT code");
16841685
}
16851686
#endif
16861687
_Py_SetImmortal((PyObject *)cold);
1687-
interp->cold_executor = cold;
16881688
return cold;
16891689
}
16901690

16911691
_PyExecutorObject *
1692-
_PyExecutor_GetColdDynamicExecutor(void)
1692+
_PyExecutor_GetColdExecutor(void)
16931693
{
16941694
PyInterpreterState *interp = _PyInterpreterState_GET();
1695-
if (interp->cold_dynamic_executor != NULL) {
1696-
assert(interp->cold_dynamic_executor->trace[0].opcode == _COLD_DYNAMIC_EXIT_r00);
1697-
return interp->cold_dynamic_executor;
1698-
}
1699-
_PyExecutorObject *cold = allocate_executor(0, 1);
1700-
if (cold == NULL) {
1701-
Py_FatalError("Cannot allocate core JIT code");
1695+
if (interp->cold_executor == NULL) {
1696+
return interp->cold_executor = make_cold_executor(_COLD_EXIT_r00);;
17021697
}
1703-
((_PyUOpInstruction *)cold->trace)->opcode = _COLD_DYNAMIC_EXIT_r00;
1704-
#ifdef _Py_JIT
1705-
cold->jit_code = NULL;
1706-
cold->jit_size = 0;
1707-
// This is initialized to true so we can prevent the executor
1708-
// from being immediately detected as cold and invalidated.
1709-
cold->vm_data.warm = true;
1710-
if (_PyJIT_Compile(cold, cold->trace, 1)) {
1711-
Py_DECREF(cold);
1712-
Py_FatalError("Cannot allocate core JIT code");
1698+
return interp->cold_executor;
1699+
}
1700+
1701+
_PyExecutorObject *
1702+
_PyExecutor_GetColdDynamicExecutor(void)
1703+
{
1704+
PyInterpreterState *interp = _PyInterpreterState_GET();
1705+
if (interp->cold_dynamic_executor == NULL) {
1706+
interp->cold_dynamic_executor = make_cold_executor(_COLD_DYNAMIC_EXIT_r00);
17131707
}
1714-
#endif
1715-
_Py_SetImmortal((PyObject *)cold);
1716-
interp->cold_dynamic_executor = cold;
1717-
return cold;
1708+
return interp->cold_dynamic_executor;
17181709
}
17191710

17201711
void
@@ -1753,32 +1744,28 @@ _Py_ExecutorDetach(_PyExecutorObject *executor)
17531744
Py_DECREF(executor);
17541745
}
17551746

1756-
static int
1757-
executor_clear(PyObject *op)
1747+
/* Executors can be invalidated at any time,
1748+
even with a stop-the-world lock held.
1749+
Consequently it must not run arbitrary code,
1750+
including Py_DECREF with a non-executor. */
1751+
static void
1752+
executor_invalidate(PyObject *op)
17581753
{
17591754
_PyExecutorObject *executor = _PyExecutorObject_CAST(op);
17601755
if (!executor->vm_data.valid) {
1761-
return 0;
1756+
return;
17621757
}
1763-
assert(executor->vm_data.valid == 1);
1764-
unlink_executor(executor);
17651758
executor->vm_data.valid = 0;
1766-
1767-
/* It is possible for an executor to form a reference
1768-
* cycle with itself, so decref'ing a side exit could
1769-
* free the executor unless we hold a strong reference to it
1770-
*/
1771-
_PyExecutorObject *cold = _PyExecutor_GetColdExecutor();
1772-
Py_INCREF(executor);
1773-
for (uint32_t i = 0; i < executor->exit_count; i++) {
1774-
executor->exits[i].temperature = initial_unreachable_backoff_counter();
1775-
_PyExecutorObject *e = executor->exits[i].executor;
1776-
executor->exits[i].executor = cold;
1777-
Py_DECREF(e);
1778-
}
1759+
unlink_executor(executor);
1760+
executor_clear_exits(executor);
17791761
_Py_ExecutorDetach(executor);
1780-
Py_DECREF(executor);
1781-
return 0;
1762+
_PyObject_GC_UNTRACK(op);
1763+
}
1764+
1765+
static int
1766+
executor_clear(PyObject *op)
1767+
{
1768+
executor_invalidate(op);
17821769
}
17831770

17841771
void
@@ -1803,7 +1790,7 @@ _Py_Executors_InvalidateDependency(PyInterpreterState *interp, void *obj, int is
18031790
if (invalidate == NULL) {
18041791
goto error;
18051792
}
1806-
/* Clearing an executor can deallocate others, so we need to make a list of
1793+
/* Clearing an executor can clear others, so we need to make a list of
18071794
* executors to invalidate first */
18081795
for (_PyExecutorObject *exec = interp->executor_list_head; exec != NULL;) {
18091796
assert(exec->vm_data.valid);
@@ -1817,7 +1804,7 @@ _Py_Executors_InvalidateDependency(PyInterpreterState *interp, void *obj, int is
18171804
}
18181805
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(invalidate); i++) {
18191806
PyObject *exec = PyList_GET_ITEM(invalidate, i);
1820-
executor_clear(exec);
1807+
executor_invalidate(exec);
18211808
if (is_invalidation) {
18221809
OPT_STAT_INC(executors_invalidated);
18231810
}
@@ -1849,13 +1836,13 @@ _Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_invalidation)
18491836
{
18501837
while (interp->executor_list_head) {
18511838
_PyExecutorObject *executor = interp->executor_list_head;
1852-
assert(executor->vm_data.valid == 1 && executor->vm_data.linked == 1);
1839+
assert(executor->vm_data.valid);
18531840
if (executor->vm_data.code) {
18541841
// Clear the entire code object so its co_executors array be freed:
18551842
_PyCode_Clear_Executors(executor->vm_data.code);
18561843
}
18571844
else {
1858-
executor_clear((PyObject *)executor);
1845+
executor_invalidate((PyObject *)executor);
18591846
}
18601847
if (is_invalidation) {
18611848
OPT_STAT_INC(executors_invalidated);
@@ -1890,7 +1877,7 @@ _Py_Executors_InvalidateCold(PyInterpreterState *interp)
18901877
}
18911878
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(invalidate); i++) {
18921879
PyObject *exec = PyList_GET_ITEM(invalidate, i);
1893-
executor_clear(exec);
1880+
executor_invalidate(exec);
18941881
}
18951882
Py_DECREF(invalidate);
18961883
return;

Python/pystate.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ init_interpreter(PyInterpreterState *interp,
570570
interp->compiling = false;
571571
interp->executor_list_head = NULL;
572572
interp->executor_deletion_list_head = NULL;
573-
interp->executor_deletion_list_remaining_capacity = 0;
574573
interp->executor_creation_counter = JIT_CLEANUP_THRESHOLD;
575574
if (interp != &runtime->_main_interpreter) {
576575
/* Fix the self-referential, statically initialized fields. */

0 commit comments

Comments
 (0)