ยปCore Development>Code coverage>Lib/test/crashers/loosing_mro_ref.py

Python code coverage for Lib/test/crashers/loosing_mro_ref.py

#countcontent
1n/a"""
2n/aThere is a way to put keys of any type in a type's dictionary.
3n/aI think this allows various kinds of crashes, but so far I have only
4n/afound a convoluted attack of _PyType_Lookup(), which uses the mro of the
5n/atype without holding a strong reference to it. Probably works with
6n/asuper.__getattribute__() too, which uses the same kind of code.
7n/a"""
8n/a
9n/aclass MyKey(object):
10n/a def __hash__(self):
11n/a return hash('mykey')
12n/a
13n/a def __eq__(self, other):
14n/a # the following line decrefs the previous X.__mro__
15n/a X.__bases__ = (Base2,)
16n/a # trash all tuples of length 3, to make sure that the items of
17n/a # the previous X.__mro__ are really garbage
18n/a z = []
19n/a for i in range(1000):
20n/a z.append((i, None, None))
21n/a return 0
22n/a
23n/a
24n/aclass Base(object):
25n/a mykey = 'from Base'
26n/a
27n/aclass Base2(object):
28n/a mykey = 'from Base2'
29n/a
30n/a# you can't add a non-string key to X.__dict__, but it can be
31n/a# there from the beginning :-)
32n/aX = type('X', (Base,), {MyKey(): 5})
33n/a
34n/aprint(X.mykey)
35n/a# I get a segfault, or a slightly wrong assertion error in a debug build.