Skip to content

Commit 3e811ec

Browse files
committed
pythongh-89811: Check for valid tp_version_tag in specializer (pythonGH-113558)
1 parent e197639 commit 3e811ec

File tree

4 files changed

+260
-1
lines changed

4 files changed

+260
-1
lines changed

Lib/test/test_type_cache.py

Lines changed: 195 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Tests for the internal type cache in CPython. """
22
import unittest
3+
import dis
34
from test import support
45
from test.support import import_helper
56
try:
@@ -8,7 +9,11 @@
89
_clear_type_cache = None
910

1011
# Skip this test if the _testcapi module isn't available.
11-
type_get_version = import_helper.import_module('_testcapi').type_get_version
12+
_testcapi = import_helper.import_module("_testcapi")
13+
type_get_version = _testcapi.type_get_version
14+
type_assign_specific_version_unsafe = _testcapi.type_assign_specific_version_unsafe
15+
type_assign_version = _testcapi.type_assign_version
16+
type_modified = _testcapi.type_modified
1217

1318

1419
@support.cpython_only
@@ -42,6 +47,195 @@ def test_tp_version_tag_unique(self):
4247
self.assertEqual(len(set(all_version_tags)), 30,
4348
msg=f"{all_version_tags} contains non-unique versions")
4449

50+
def test_type_assign_version(self):
51+
class C:
52+
x = 5
53+
54+
self.assertEqual(type_assign_version(C), 1)
55+
c_ver = type_get_version(C)
56+
57+
C.x = 6
58+
self.assertEqual(type_get_version(C), 0)
59+
self.assertEqual(type_assign_version(C), 1)
60+
self.assertNotEqual(type_get_version(C), 0)
61+
self.assertNotEqual(type_get_version(C), c_ver)
62+
63+
def test_type_assign_specific_version(self):
64+
"""meta-test for type_assign_specific_version_unsafe"""
65+
class C:
66+
pass
67+
68+
type_assign_version(C)
69+
orig_version = type_get_version(C)
70+
self.assertNotEqual(orig_version, 0)
71+
72+
type_modified(C)
73+
type_assign_specific_version_unsafe(C, orig_version + 5)
74+
type_assign_version(C) # this should do nothing
75+
76+
new_version = type_get_version(C)
77+
self.assertEqual(new_version, orig_version + 5)
78+
79+
_clear_type_cache()
80+
81+
82+
@support.cpython_only
83+
class TypeCacheWithSpecializationTests(unittest.TestCase):
84+
def tearDown(self):
85+
_clear_type_cache()
86+
87+
def _assign_and_check_valid_version(self, user_type):
88+
type_modified(user_type)
89+
type_assign_version(user_type)
90+
self.assertNotEqual(type_get_version(user_type), 0)
91+
92+
def _assign_and_check_version_0(self, user_type):
93+
type_modified(user_type)
94+
type_assign_specific_version_unsafe(user_type, 0)
95+
self.assertEqual(type_get_version(user_type), 0)
96+
97+
def _all_opnames(self, func):
98+
return set(instr.opname for instr in dis.Bytecode(func, adaptive=True))
99+
100+
def _check_specialization(self, func, arg, opname, *, should_specialize):
101+
self.assertIn(opname, self._all_opnames(func))
102+
103+
for _ in range(100):
104+
func(arg)
105+
106+
if should_specialize:
107+
self.assertNotIn(opname, self._all_opnames(func))
108+
else:
109+
self.assertIn(opname, self._all_opnames(func))
110+
111+
def test_class_load_attr_specialization_user_type(self):
112+
class A:
113+
def foo(self):
114+
pass
115+
116+
self._assign_and_check_valid_version(A)
117+
118+
def load_foo_1(type_):
119+
type_.foo
120+
121+
self._check_specialization(load_foo_1, A, "LOAD_ATTR", should_specialize=True)
122+
del load_foo_1
123+
124+
self._assign_and_check_version_0(A)
125+
126+
def load_foo_2(type_):
127+
return type_.foo
128+
129+
self._check_specialization(load_foo_2, A, "LOAD_ATTR", should_specialize=False)
130+
131+
def test_class_load_attr_specialization_static_type(self):
132+
self._assign_and_check_valid_version(str)
133+
self._assign_and_check_valid_version(bytes)
134+
135+
def get_capitalize_1(type_):
136+
return type_.capitalize
137+
138+
self._check_specialization(get_capitalize_1, str, "LOAD_ATTR", should_specialize=True)
139+
self.assertEqual(get_capitalize_1(str)('hello'), 'Hello')
140+
self.assertEqual(get_capitalize_1(bytes)(b'hello'), b'Hello')
141+
del get_capitalize_1
142+
143+
# Permanently overflow the static type version counter, and force str and bytes
144+
# to have tp_version_tag == 0
145+
for _ in range(2**16):
146+
type_modified(str)
147+
type_assign_version(str)
148+
type_modified(bytes)
149+
type_assign_version(bytes)
150+
151+
self.assertEqual(type_get_version(str), 0)
152+
self.assertEqual(type_get_version(bytes), 0)
153+
154+
def get_capitalize_2(type_):
155+
return type_.capitalize
156+
157+
self._check_specialization(get_capitalize_2, str, "LOAD_ATTR", should_specialize=False)
158+
self.assertEqual(get_capitalize_2(str)('hello'), 'Hello')
159+
self.assertEqual(get_capitalize_2(bytes)(b'hello'), b'Hello')
160+
161+
def test_property_load_attr_specialization_user_type(self):
162+
class G:
163+
@property
164+
def x(self):
165+
return 9
166+
167+
self._assign_and_check_valid_version(G)
168+
169+
def load_x_1(instance):
170+
instance.x
171+
172+
self._check_specialization(load_x_1, G(), "LOAD_ATTR", should_specialize=True)
173+
del load_x_1
174+
175+
self._assign_and_check_version_0(G)
176+
177+
def load_x_2(instance):
178+
instance.x
179+
180+
self._check_specialization(load_x_2, G(), "LOAD_ATTR", should_specialize=False)
181+
182+
def test_store_attr_specialization_user_type(self):
183+
class B:
184+
__slots__ = ("bar",)
185+
186+
self._assign_and_check_valid_version(B)
187+
188+
def store_bar_1(type_):
189+
type_.bar = 10
190+
191+
self._check_specialization(store_bar_1, B(), "STORE_ATTR", should_specialize=True)
192+
del store_bar_1
193+
194+
self._assign_and_check_version_0(B)
195+
196+
def store_bar_2(type_):
197+
type_.bar = 10
198+
199+
self._check_specialization(store_bar_2, B(), "STORE_ATTR", should_specialize=False)
200+
201+
def test_class_call_specialization_user_type(self):
202+
class F:
203+
def __init__(self):
204+
pass
205+
206+
self._assign_and_check_valid_version(F)
207+
208+
def call_class_1(type_):
209+
type_()
210+
211+
self._check_specialization(call_class_1, F, "CALL", should_specialize=True)
212+
del call_class_1
213+
214+
self._assign_and_check_version_0(F)
215+
216+
def call_class_2(type_):
217+
type_()
218+
219+
self._check_specialization(call_class_2, F, "CALL", should_specialize=False)
220+
221+
def test_to_bool_specialization_user_type(self):
222+
class H:
223+
pass
224+
225+
self._assign_and_check_valid_version(H)
226+
227+
def to_bool_1(instance):
228+
not instance
229+
230+
self._check_specialization(to_bool_1, H(), "TO_BOOL", should_specialize=True)
231+
del to_bool_1
232+
233+
self._assign_and_check_version_0(H)
234+
235+
def to_bool_2(instance):
236+
not instance
237+
238+
self._check_specialization(to_bool_2, H(), "TO_BOOL", should_specialize=False)
45239

46240
if __name__ == "__main__":
47241
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Check for a valid ``tp_version_tag`` before performing bytecode specializations that
2+
rely on this value being usable.

Modules/_testcapimodule.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5899,6 +5899,43 @@ type_get_version(PyObject *self, PyObject *type)
58995899
return res;
59005900
}
59015901

5902+
static PyObject *
5903+
type_modified(PyObject *self, PyObject *type)
5904+
{
5905+
if (!PyType_Check(type)) {
5906+
PyErr_SetString(PyExc_TypeError, "argument must be a type");
5907+
return NULL;
5908+
}
5909+
PyType_Modified((PyTypeObject *)type);
5910+
Py_RETURN_NONE;
5911+
}
5912+
5913+
// Circumvents standard version assignment machinery - use with caution and only on
5914+
// short-lived heap types
5915+
static PyObject *
5916+
type_assign_specific_version_unsafe(PyObject *self, PyObject *args)
5917+
{
5918+
PyTypeObject *type;
5919+
unsigned int version;
5920+
if (!PyArg_ParseTuple(args, "Oi:type_assign_specific_version_unsafe", &type, &version)) {
5921+
return NULL;
5922+
}
5923+
assert(!PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE));
5924+
type->tp_version_tag = version;
5925+
type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
5926+
Py_RETURN_NONE;
5927+
}
5928+
5929+
static PyObject *
5930+
type_assign_version(PyObject *self, PyObject *type)
5931+
{
5932+
if (!PyType_Check(type)) {
5933+
PyErr_SetString(PyExc_TypeError, "argument must be a type");
5934+
return NULL;
5935+
}
5936+
int res = PyUnstable_Type_AssignVersionTag((PyTypeObject *)type);
5937+
return PyLong_FromLong(res);
5938+
}
59025939

59035940
// Test PyThreadState C API
59045941
static PyObject *
@@ -6782,6 +6819,10 @@ static PyMethodDef TestMethods[] = {
67826819
{"fatal_error", test_fatal_error, METH_VARARGS,
67836820
PyDoc_STR("fatal_error(message, release_gil=False): call Py_FatalError(message)")},
67846821
{"type_get_version", type_get_version, METH_O, PyDoc_STR("type->tp_version_tag")},
6822+
{"type_modified", type_modified, METH_O, PyDoc_STR("PyType_Modified")},
6823+
{"type_assign_specific_version_unsafe", type_assign_specific_version_unsafe, METH_VARARGS,
6824+
PyDoc_STR("forcefully assign type->tp_version_tag")},
6825+
{"type_assign_version", type_assign_version, METH_O, PyDoc_STR("PyUnstable_Type_AssignVersionTag")},
67856826
{"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
67866827
{"float_pack", test_float_pack, METH_VARARGS, NULL},
67876828
{"float_unpack", test_float_unpack, METH_VARARGS, NULL},

Python/specialize.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ miss_counter_start(void) {
481481
#define SPEC_FAIL_UNPACK_SEQUENCE_ITERATOR 8
482482
#define SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE 9
483483

484+
static uint32_t type_get_version(PyTypeObject *t, int opcode);
484485

485486
static int
486487
specialize_module_load_attr(PyObject *owner, _Py_CODEUNIT *instr,
@@ -673,6 +674,9 @@ _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
673674
}
674675
PyObject *descr;
675676
DescriptorClassification kind = analyze_descriptor(type, name, &descr, 0);
677+
if (type_get_version(type, LOAD_ATTR) == 0) {
678+
goto fail;
679+
}
676680
switch(kind) {
677681
case OVERRIDING:
678682
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR);
@@ -766,6 +770,9 @@ _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
766770
}
767771
PyObject *descr;
768772
DescriptorClassification kind = analyze_descriptor(type, name, &descr, 1);
773+
if (type_get_version(type, STORE_ATTR) == 0) {
774+
goto fail;
775+
}
769776
switch(kind) {
770777
case OVERRIDING:
771778
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_ATTR_OVERRIDING_DESCRIPTOR);
@@ -889,6 +896,9 @@ specialize_class_load_method(PyObject *owner, _Py_CODEUNIT *instr,
889896
PyObject *descr = NULL;
890897
DescriptorClassification kind = 0;
891898
kind = analyze_descriptor((PyTypeObject *)owner, name, &descr, 0);
899+
if (type_get_version((PyTypeObject *)owner, LOAD_ATTR) == 0) {
900+
return -1;
901+
}
892902
switch (kind) {
893903
case METHOD:
894904
case NON_DESCRIPTOR:
@@ -1183,6 +1193,18 @@ function_kind(PyCodeObject *code) {
11831193
return SIMPLE_FUNCTION;
11841194
}
11851195

1196+
/* Returning 0 indicates a failure. */
1197+
static uint32_t
1198+
type_get_version(PyTypeObject *t, int opcode)
1199+
{
1200+
uint32_t version = t->tp_version_tag;
1201+
if (version == 0) {
1202+
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_VERSIONS);
1203+
return 0;
1204+
}
1205+
return version;
1206+
}
1207+
11861208
int
11871209
_Py_Specialize_BinarySubscr(
11881210
PyObject *container, PyObject *sub, _Py_CODEUNIT *instr)

0 commit comments

Comments
 (0)