Skip to content

bpo-46465: Check eval breaker in specialized CALL opcodes #30826

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

Closed
Closed
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
25 changes: 25 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"run_with_tz", "PGO", "missing_compiler_executable",
"ALWAYS_EQ", "NEVER_EQ", "LARGEST", "SMALLEST",
"LOOPBACK_TIMEOUT", "INTERNET_TIMEOUT", "SHORT_TIMEOUT", "LONG_TIMEOUT",
"repeat_cpython_adaptative",
]


Expand Down Expand Up @@ -2118,3 +2119,27 @@ def clear_ignored_deprecations(*tokens: object) -> None:
if warnings.filters != new_filters:
warnings.filters[:] = new_filters
warnings._filters_mutated()


def repeat_cpython_adaptative(f):
"""Runs a test multiple times. First with PEP 659 adaptive opcodes, then
with specialized opcodes once the opcodes turn "hot". This catches bugs
related to specialized opcodes.

See bpo-46465 for an example bug affecting only specialized opcodes,
due to a missing CHECK_EVAL_BREAKER.
"""
# _co_warmedup is only available in CPython
if check_impl_detail(cpython=True) and hasattr(f.__code__, "_co_warmedup"):
@functools.wraps(f)
def wrapper(self):
done = False
while not done:
is_warmedup = f.__code__._co_warmedup
if is_warmedup:
done = True
with self.subTest(cpython_is_warmedup=is_warmedup):
f(self)
return wrapper
else:
return f
9 changes: 7 additions & 2 deletions Lib/unittest/test/test_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import weakref

import unittest
from test.support import repeat_cpython_adaptative


@unittest.skipUnless(hasattr(os, 'kill'), "Test requires os.kill")
Expand All @@ -23,7 +24,9 @@ def tearDown(self):
unittest.signals._results = weakref.WeakKeyDictionary()
unittest.signals._interrupt_handler = None


# Tests both adaptive and specialized opcodes for proper
# CHECK_EVAL_BREAKER(). See bpo-46465 for an example bug.
@repeat_cpython_adaptative
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've tested in https://bugs.python.org/issue46709 there several more test cases that require this decorator:

  • testInterruptCaught
  • testSecondInterrupt
  • testTwoResults

def testInstallHandler(self):
default_handler = signal.getsignal(signal.SIGINT)
unittest.installHandler()
Expand Down Expand Up @@ -121,7 +124,9 @@ def test(result):
self.assertTrue(result2.shouldStop)
self.assertFalse(result3.shouldStop)


# Tests both adaptive and specialized opcodes for proper
# CHECK_EVAL_BREAKER(). See bpo-46465 for an example bug.
@repeat_cpython_adaptative
def testHandlerReplacedButCalled(self):
# Can't use skipIf decorator because the signal handler may have
# been changed after defining this method.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Properly check for eval loop breaker in specialized ``CALL`` opcodes. Patch
by Victor Stinner and Ken Jin.
12 changes: 12 additions & 0 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1542,13 +1542,25 @@ code_getfreevars(PyCodeObject *code, void *closure)
return _PyCode_GetFreevars(code);
}

static PyObject *
code_iswarmedup(PyCodeObject *code, void *closure)
{
if (code->co_quickened != NULL) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}

static PyGetSetDef code_getsetlist[] = {
{"co_lnotab", (getter)code_getlnotab, NULL, NULL},
// The following old names are kept for backward compatibility.
{"co_nlocals", (getter)code_getnlocals, NULL, NULL},
{"co_varnames", (getter)code_getvarnames, NULL, NULL},
{"co_cellvars", (getter)code_getcellvars, NULL, NULL},
{"co_freevars", (getter)code_getfreevars, NULL, NULL},
{"_co_warmedup", (getter)code_iswarmedup, NULL, NULL},
{0}
};

Expand Down
6 changes: 6 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4752,6 +4752,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
if (res == NULL) {
goto error;
}
CHECK_EVAL_BREAKER();
DISPATCH();
}

Expand Down Expand Up @@ -4783,6 +4784,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
if (res == NULL) {
goto error;
}
CHECK_EVAL_BREAKER();
DISPATCH();
}

Expand Down Expand Up @@ -4822,6 +4824,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
*/
goto error;
}
CHECK_EVAL_BREAKER();
DISPATCH();
}

Expand Down Expand Up @@ -4881,6 +4884,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
if (res == NULL) {
goto error;
}
CHECK_EVAL_BREAKER();
DISPATCH();
}

Expand Down Expand Up @@ -4939,6 +4943,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
if (res == NULL) {
goto error;
}
CHECK_EVAL_BREAKER();
DISPATCH();
}

Expand Down Expand Up @@ -4972,6 +4977,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
if (res == NULL) {
goto error;
}
CHECK_EVAL_BREAKER();
DISPATCH();
}

Expand Down