blob: df58944d884f53d06131c9807ad3482aadd2755d [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test.test_support import verbose, TESTFN
Tim Peters95bf9392001-05-10 08:32:44 +00002import random
Tim Peters23cf6be2001-06-02 08:02:56 +00003import os
Tim Peters95bf9392001-05-10 08:32:44 +00004
5# From SF bug #422121: Insecurities in dict comparison.
6
Tim Peters8c3e91e2001-05-10 19:40:30 +00007# Safety of code doing comparisons has been an historical Python weak spot.
8# The problem is that comparison of structures written in C *naturally*
Tim Peters95bf9392001-05-10 08:32:44 +00009# wants to hold on to things like the size of the container, or "the
10# biggest" containee so far, across a traversal of the container; but
11# code to do containee comparisons can call back into Python and mutate
12# the container in arbitrary ways while the C loop is in midstream. If the
13# C code isn't extremely paranoid about digging things out of memory on
14# each trip, and artificially boosting refcounts for the duration, anything
15# from infinite loops to OS crashes can result (yes, I use Windows <wink>).
16#
17# The other problem is that code designed to provoke a weakness is usually
18# white-box code, and so catches only the particular vulnerabilities the
19# author knew to protect against. For example, Python's list.sort() code
20# went thru many iterations as one "new" vulnerability after another was
21# discovered.
22#
23# So the dict comparison test here uses a black-box approach instead,
24# generating dicts of various sizes at random, and performing random
25# mutations on them at random times. This proved very effective,
26# triggering at least six distinct failure modes the first 20 times I
27# ran it. Indeed, at the start, the driver never got beyond 6 iterations
28# before the test died.
29
30# The dicts are global to make it easy to mutate tham from within functions.
31dict1 = {}
32dict2 = {}
33
34# The current set of keys in dict1 and dict2. These are materialized as
35# lists to make it easy to pick a dict key at random.
36dict1keys = []
37dict2keys = []
38
Neal Norwitz2fcf2062005-11-24 23:28:37 +000039# Global flag telling maybe_mutate() whether to *consider* mutating.
Tim Peters95bf9392001-05-10 08:32:44 +000040mutate = 0
41
42# If global mutate is true, consider mutating a dict. May or may not
43# mutate a dict even if mutate is true. If it does decide to mutate a
44# dict, it picks one of {dict1, dict2} at random, and deletes a random
Tim Peters4c02fec2001-05-10 20:18:30 +000045# entry from it; or, more rarely, adds a random element.
Tim Peters95bf9392001-05-10 08:32:44 +000046
47def maybe_mutate():
Tim Peters4c02fec2001-05-10 20:18:30 +000048 global mutate
Tim Peters95bf9392001-05-10 08:32:44 +000049 if not mutate:
50 return
51 if random.random() < 0.5:
52 return
Tim Peters4c02fec2001-05-10 20:18:30 +000053
Tim Peters95bf9392001-05-10 08:32:44 +000054 if random.random() < 0.5:
55 target, keys = dict1, dict1keys
56 else:
57 target, keys = dict2, dict2keys
Tim Peters4c02fec2001-05-10 20:18:30 +000058
59 if random.random() < 0.2:
60 # Insert a new key.
61 mutate = 0 # disable mutation until key inserted
62 while 1:
63 newkey = Horrid(random.randrange(100))
64 if newkey not in target:
65 break
66 target[newkey] = Horrid(random.randrange(100))
67 keys.append(newkey)
68 mutate = 1
69
70 elif keys:
71 # Delete a key at random.
Armin Rigo57179fe2005-05-15 13:29:26 +000072 mutate = 0 # disable mutation until key deleted
Tim Peters95bf9392001-05-10 08:32:44 +000073 i = random.randrange(len(keys))
74 key = keys[i]
75 del target[key]
Tim Peters95bf9392001-05-10 08:32:44 +000076 del keys[i]
Armin Rigo57179fe2005-05-15 13:29:26 +000077 mutate = 1
Tim Peters95bf9392001-05-10 08:32:44 +000078
79# A horrid class that triggers random mutations of dict1 and dict2 when
80# instances are compared.
81
82class Horrid:
83 def __init__(self, i):
84 # Comparison outcomes are determined by the value of i.
85 self.i = i
86
87 # An artificial hashcode is selected at random so that we don't
Tim Peters8c3e91e2001-05-10 19:40:30 +000088 # have any systematic relationship between comparison outcomes
Tim Peters95bf9392001-05-10 08:32:44 +000089 # (based on self.i and other.i) and relative position within the
Tim Peters8c3e91e2001-05-10 19:40:30 +000090 # hash vector (based on hashcode).
Tim Peters95bf9392001-05-10 08:32:44 +000091 self.hashcode = random.randrange(1000000000)
92
93 def __hash__(self):
94 return self.hashcode
95
96 def __cmp__(self, other):
97 maybe_mutate() # The point of the test.
98 return cmp(self.i, other.i)
99
100 def __repr__(self):
101 return "Horrid(%d)" % self.i
102
103# Fill dict d with numentries (Horrid(i), Horrid(j)) key-value pairs,
104# where i and j are selected at random from the candidates list.
105# Return d.keys() after filling.
106
107def fill_dict(d, candidates, numentries):
108 d.clear()
109 for i in xrange(numentries):
110 d[Horrid(random.choice(candidates))] = \
111 Horrid(random.choice(candidates))
112 return d.keys()
113
114# Test one pair of randomly generated dicts, each with n entries.
115# Note that dict comparison is trivial if they don't have the same number
116# of entires (then the "shorter" dict is instantly considered to be the
117# smaller one, without even looking at the entries).
118
119def test_one(n):
120 global mutate, dict1, dict2, dict1keys, dict2keys
121
122 # Fill the dicts without mutating them.
123 mutate = 0
124 dict1keys = fill_dict(dict1, range(n), n)
125 dict2keys = fill_dict(dict2, range(n), n)
126
127 # Enable mutation, then compare the dicts so long as they have the
128 # same size.
129 mutate = 1
130 if verbose:
131 print "trying w/ lengths", len(dict1), len(dict2),
132 while dict1 and len(dict1) == len(dict2):
133 if verbose:
134 print ".",
135 c = cmp(dict1, dict2)
136 if verbose:
137 print
138
139# Run test_one n times. At the start (before the bugs were fixed), 20
140# consecutive runs of this test each blew up on or before the sixth time
141# test_one was run. So n doesn't have to be large to get an interesting
142# test.
143# OTOH, calling with large n is also interesting, to ensure that the fixed
144# code doesn't hold on to refcounts *too* long (in which case memory would
145# leak).
146
147def test(n):
148 for i in xrange(n):
149 test_one(random.randrange(1, 100))
150
151# See last comment block for clues about good values for n.
152test(100)
Tim Peters23cf6be2001-06-02 08:02:56 +0000153
154##########################################################################
Tim Petersfa517b22001-06-02 08:18:58 +0000155# Another segfault bug, distilled by Michael Hudson from a c.l.py post.
Tim Peters23cf6be2001-06-02 08:02:56 +0000156
157class Child:
158 def __init__(self, parent):
159 self.__dict__['parent'] = parent
160 def __getattr__(self, attr):
161 self.parent.a = 1
162 self.parent.b = 1
163 self.parent.c = 1
164 self.parent.d = 1
165 self.parent.e = 1
166 self.parent.f = 1
167 self.parent.g = 1
168 self.parent.h = 1
169 self.parent.i = 1
170 return getattr(self.parent, attr)
171
172class Parent:
173 def __init__(self):
174 self.a = Child(self)
175
176# Hard to say what this will print! May vary from time to time. But
177# we're specifically trying to test the tp_print slot here, and this is
178# the clearest way to do it. We print the result to a temp file so that
179# the expected-output file doesn't need to change.
180
181f = open(TESTFN, "w")
182print >> f, Parent().__dict__
183f.close()
184os.unlink(TESTFN)
185
186##########################################################################
187# And another core-dumper from Michael Hudson.
188
189dict = {}
190
191# Force dict to malloc its table.
192for i in range(1, 10):
193 dict[i] = i
194
195f = open(TESTFN, "w")
196
197class Machiavelli:
198 def __repr__(self):
199 dict.clear()
200
201 # Michael sez: "doesn't crash without this. don't know why."
202 # Tim sez: "luck of the draw; crashes with or without for me."
203 print >> f
204
205 return `"machiavelli"`
206
207 def __hash__(self):
208 return 0
209
210dict[Machiavelli()] = Machiavelli()
211
212print >> f, str(dict)
213f.close()
214os.unlink(TESTFN)
215del f, dict
Tim Peters453163d2001-06-03 04:54:32 +0000216
217
218##########################################################################
219# And another core-dumper from Michael Hudson.
220
221dict = {}
222
223# let's force dict to malloc its table
224for i in range(1, 10):
225 dict[i] = i
226
227class Machiavelli2:
228 def __eq__(self, other):
229 dict.clear()
230 return 1
231
232 def __hash__(self):
233 return 0
234
235dict[Machiavelli2()] = Machiavelli2()
236
237try:
238 dict[Machiavelli2()]
239except KeyError:
240 pass
241
242del dict
243
244##########################################################################
245# And another core-dumper from Michael Hudson.
246
247dict = {}
248
249# let's force dict to malloc its table
250for i in range(1, 10):
251 dict[i] = i
252
253class Machiavelli3:
254 def __init__(self, id):
255 self.id = id
256
257 def __eq__(self, other):
258 if self.id == other.id:
259 dict.clear()
260 return 1
261 else:
262 return 0
263
264 def __repr__(self):
265 return "%s(%s)"%(self.__class__.__name__, self.id)
266
267 def __hash__(self):
268 return 0
269
270dict[Machiavelli3(1)] = Machiavelli3(0)
271dict[Machiavelli3(2)] = Machiavelli3(0)
272
273f = open(TESTFN, "w")
274try:
275 try:
276 print >> f, dict[Machiavelli3(2)]
277 except KeyError:
278 pass
279finally:
280 f.close()
281 os.unlink(TESTFN)
282
283del dict
Neal Norwitz2fcf2062005-11-24 23:28:37 +0000284del dict1, dict2, dict1keys, dict2keys