Skip to content

gh-122712: Test CALL_ALLOC_AND_ENTER_INIT handles reassignment of __code__ #122713

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 18 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
21 changes: 21 additions & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,27 @@ extern void _Py_Specialize_Send(_PyStackRef receiver, _Py_CODEUNIT *instr);
extern void _Py_Specialize_ToBool(_PyStackRef value, _Py_CODEUNIT *instr);
extern void _Py_Specialize_ContainsOp(_PyStackRef value, _Py_CODEUNIT *instr);

/* Helpers shared by the specializer and instructions */

typedef enum {
_Py_Specialize_Ok = 0,
_Py_Specialize_ErrComplexParams = 1,
_Py_Specialize_ErrCodeUnoptimized = 2,
} _Py_Specialize_CheckCodeResult;

static inline _Py_Specialize_CheckCodeResult
_Py_Specialize_CheckCode(PyCodeObject *code)
{
int flags = code->co_flags;
if ((flags & (CO_VARKEYWORDS | CO_VARARGS)) || code->co_kwonlyargcount) {
return _Py_Specialize_ErrComplexParams;
}
if ((flags & CO_OPTIMIZED) == 0) {
return _Py_Specialize_ErrCodeUnoptimized;
}
return _Py_Specialize_Ok;
}

#ifdef Py_STATS

#include "pycore_bitutils.h" // _Py_bit_length
Expand Down
39 changes: 32 additions & 7 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def wrapper(*args, **kwargs):
return wrapper


class TestBase(unittest.TestCase):
def assert_specialized(self, f, opname):
instructions = dis.get_instructions(f, adaptive=True)
opnames = {instruction.opname for instruction in instructions}
self.assertIn(opname, opnames)


class TestLoadSuperAttrCache(unittest.TestCase):
def test_descriptor_not_double_executed_on_spec_fail(self):
calls = []
Expand Down Expand Up @@ -479,7 +486,7 @@ def f():
self.assertFalse(f())


class TestCallCache(unittest.TestCase):
class TestCallCache(TestBase):
def test_too_many_defaults_0(self):
def f():
pass
Expand Down Expand Up @@ -507,22 +514,40 @@ def f(x, y):
f(None)
f()

@disabling_optimizer
@requires_specialization
def test_assign_init_code(self):
class MyClass:
def __init__(self):
pass

def instantiate():
return MyClass()

# Trigger specialization
for _ in range(1025):
instantiate()
self.assert_specialized(instantiate, "CALL_ALLOC_AND_ENTER_INIT")

def count_args(self, *args):
self.num_args = len(args)

# Set MyClass.__init__.__code__ to a code object that is incompatible
# (uses varargs) with the current specialization
MyClass.__init__.__code__ = count_args.__code__
instantiate()


@threading_helper.requires_working_threading()
@requires_specialization
class TestRacesDoNotCrash(unittest.TestCase):
class TestRacesDoNotCrash(TestBase):
# Careful with these. Bigger numbers have a higher chance of catching bugs,
# but you can also burn through a *ton* of type/dict/function versions:
ITEMS = 1000
LOOPS = 4
WARMUPS = 2
WRITERS = 2

def assert_specialized(self, f, opname):
instructions = dis.get_instructions(f, adaptive=True)
opnames = {instruction.opname for instruction in instructions}
self.assertIn(opname, opnames)

@disabling_optimizer
def assert_races_do_not_crash(
self, opname, get_items, read, write, *, check_items=False
Expand Down
1 change: 1 addition & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3563,6 +3563,7 @@ dummy_func(
PyFunctionObject *init = (PyFunctionObject *)cls->_spec_cache.init;
PyCodeObject *code = (PyCodeObject *)init->func_code;
DEOPT_IF(code->co_argcount != oparg+1);
DEOPT_IF(_Py_Specialize_CheckCode(code) != _Py_Specialize_Ok);
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize + _Py_InitCleanup.co_framesize));
STAT_INC(CALL, hit);
PyObject *self = _PyType_NewManagedObject(tp);
Expand Down
1 change: 1 addition & 0 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ _PyCode_Quicken(PyCodeObject *code)
#define SPEC_FAIL_CODE_COMPLEX_PARAMETERS 7
#define SPEC_FAIL_CODE_NOT_OPTIMIZED 8


#define SPEC_FAIL_LOAD_GLOBAL_NON_DICT 17
#define SPEC_FAIL_LOAD_GLOBAL_NON_STRING_OR_SPLIT 18

Expand Down Expand Up @@ -1469,15 +1468,20 @@ binary_subscr_fail_kind(PyTypeObject *container_type, PyObject *sub)
#endif // Py_STATS

static int
function_kind(PyCodeObject *code) {
int flags = code->co_flags;
if ((flags & (CO_VARKEYWORDS | CO_VARARGS)) || code->co_kwonlyargcount) {
return SPEC_FAIL_CODE_COMPLEX_PARAMETERS;
}
if ((flags & CO_OPTIMIZED) == 0) {
return SPEC_FAIL_CODE_NOT_OPTIMIZED;
function_kind(PyCodeObject *code)
{
switch (_Py_Specialize_CheckCode(code)) {
case _Py_Specialize_ErrComplexParams: {
return SPEC_FAIL_CODE_COMPLEX_PARAMETERS;
}
case _Py_Specialize_ErrCodeUnoptimized: {
return SPEC_FAIL_CODE_NOT_OPTIMIZED;
}
case _Py_Specialize_Ok: {
return SIMPLE_FUNCTION;
}
}
return SIMPLE_FUNCTION;
Py_UNREACHABLE();
}

/* Returning false indicates a failure. */
Expand Down
Loading