Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f2d6b9

Browse files
committed
Don't potentially leak executor
1 parent 59b8c76 commit 9f2d6b9

File tree

7 files changed

+37
-14
lines changed

7 files changed

+37
-14
lines changed

Include/cpython/pyatomic_msc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ _Py_atomic_load_uintptr_acquire(const uintptr_t *obj)
922922
#elif defined(_M_ARM64)
923923
return (uintptr_t)__ldar64((unsigned __int64 volatile *)obj);
924924
#else
925-
# error "no implementation of _Py_atomic_load_ptr_acquire"
925+
# error "no implementation of _Py_atomic_load_uintptr_acquire"
926926
#endif
927927
}
928928

@@ -947,7 +947,7 @@ _Py_atomic_store_uintptr_release(uintptr_t *obj, uintptr_t value)
947947
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int64);
948948
__stlr64((unsigned __int64 volatile *)obj, (unsigned __int64)value);
949949
#else
950-
# error "no implementation of _Py_atomic_store_int_release"
950+
# error "no implementation of _Py_atomic_store_uintptr_release"
951951
#endif
952952
}
953953

Include/internal/pycore_pyatomic_ft_wrappers.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ extern "C" {
3737
_Py_atomic_store_ssize_relaxed(&value, new_value)
3838
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) \
3939
_Py_atomic_store_uint8_relaxed(&value, new_value)
40+
#define FT_ATOMIC_EXCHANGE_PYOBJECT(value, new_value) \
41+
_Py_atomic_exchange_ptr(&value, new_value)
42+
4043
#else
4144
#define FT_ATOMIC_LOAD_SSIZE(value) value
4245
#define FT_ATOMIC_LOAD_SSIZE_RELAXED(value) value
@@ -47,6 +50,16 @@ extern "C" {
4750
#define FT_ATOMIC_STORE_UINTPTR_RELEASE(value, new_value) value = new_value
4851
#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
4952
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value
53+
#define FT_ATOMIC_EXCHANGE_PYOBJECT(value, new_value) \
54+
_atomic_exchange_pyobject_withgil(&value, new_value)
55+
56+
static inline PyObject *
57+
_atomic_exchange_pyobject_withgil(PyObject **src, PyObject *new_value)
58+
{
59+
PyObject *res = *src;
60+
*src = new_value;
61+
return res;
62+
}
5063

5164
#endif
5265

Python/bytecodes.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,14 @@ dummy_func(
23922392
};
23932393

23942394
tier1 inst(ENTER_EXECUTOR, (--)) {
2395+
int prevoparg = oparg;
2396+
CHECK_EVAL_BREAKER();
2397+
if (this_instr->op.code != ENTER_EXECUTOR ||
2398+
this_instr->op.arg != prevoparg) {
2399+
next_instr = this_instr;
2400+
DISPATCH();
2401+
}
2402+
23952403
PyCodeObject *code = _PyFrame_GetCode(frame);
23962404
_PyExecutorObject *executor = code->co_executors->executors[oparg & 255];
23972405
assert(executor->vm_data.index == INSTR_OFFSET() - 1);
@@ -2400,7 +2408,6 @@ dummy_func(
24002408
assert(tstate->previous_executor == NULL);
24012409
tstate->previous_executor = Py_None;
24022410
Py_INCREF(executor);
2403-
CHECK_EVAL_BREAKER();
24042411
GOTO_TIER_TWO(executor);
24052412
}
24062413

Python/generated_cases.c.h

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/instrumentation.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
assert(!_PyInterpreterState_GET()->stoptheworld.world_stopped); \
4343
Py_BEGIN_CRITICAL_SECTION(code)
4444

45-
#define UNLOCK_CODE(code) Py_END_CRITICAL_SECTION()
45+
#define UNLOCK_CODE() Py_END_CRITICAL_SECTION()
4646

4747
#else
4848

@@ -1374,15 +1374,10 @@ _PyMonitoring_RegisterCallback(int tool_id, int event_id, PyObject *obj)
13741374
PyInterpreterState *is = _PyInterpreterState_GET();
13751375
assert(0 <= tool_id && tool_id < PY_MONITORING_TOOL_IDS);
13761376
assert(0 <= event_id && event_id < _PY_MONITORING_EVENTS);
1377-
#ifdef Py_GIL_DISABLED
1378-
PyObject *callback = _Py_atomic_exchange_ptr(
1379-
&is->monitoring_callables[tool_id][event_id],
1377+
PyObject *callback = FT_ATOMIC_EXCHANGE_PYOBJECT(is->monitoring_callables[tool_id][event_id],
13801378
Py_XNewRef(obj)
13811379
);
1382-
#else
1383-
PyObject *callback = is->monitoring_callables[tool_id][event_id];
1384-
is->monitoring_callables[tool_id][event_id] = Py_XNewRef(obj);
1385-
#endif
1380+
13861381
return callback;
13871382
}
13881383

Python/legacy_tracing.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
586586
// needs to be decref'd outside of the lock
587587
PyObject *old_traceobj;
588588
LOCK_SETUP();
589-
int tracing_threads = setup_tracing(tstate, func, arg, &old_traceobj);
589+
Py_ssize_t tracing_threads = setup_tracing(tstate, func, arg, &old_traceobj);
590590
UNLOCK_SETUP();
591591
Py_XDECREF(old_traceobj);
592592
if (tracing_threads < 0) {

Tools/jit/template.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_opcode_metadata.h"
1212
#include "pycore_opcode_utils.h"
1313
#include "pycore_optimizer.h"
14+
#include "pycore_pyatomic_ft_wrappers.h"
1415
#include "pycore_range.h"
1516
#include "pycore_setobject.h"
1617
#include "pycore_sliceobject.h"

0 commit comments

Comments
 (0)