Debian Codegraph @

rpython/rlib/test/test_rstacklet.py

raw symbols SCIP
1 import gc, sys
2 import py
3 import platform
4 from rpython.rtyper.tool.rffi_platform import CompilationError
5 try:
6 from rpython.rlib import rstacklet
7 except CompilationError as e:
8 py.test.skip("cannot import rstacklet: %s" % e)
9
10 from rpython.config.translationoption import DEFL_ROOTFINDER_WITHJIT
11 from rpython.rlib import rrandom, rgc
12 from rpython.rlib.rarithmetic import intmask
13 from rpython.rlib.nonconst import NonConstant
14 from rpython.rlib import rvmprof
15 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
16 from rpython.translator.c.test.test_standalone import StandaloneTests
17
18
19
20 class Runner:
21 STATUSMAX = 5000
22
23 def init(self, seed):
24 self.sthread = rstacklet.StackletThread()
25 self.random = rrandom.Random(seed)
26
27 def done(self):
28 self.sthread = None
29 gc.collect(); gc.collect(); gc.collect()
30
31 TESTS = []
32 def here_is_a_test(fn, TESTS=TESTS):
33 TESTS.append((fn.__name__, fn))
34 return fn
35
36 @here_is_a_test
37 def test_new(self):
38 print 'start'
39 h = self.sthread.new(empty_callback, rffi.cast(llmemory.Address, 123))
40 print 'end', h
41 assert self.sthread.is_empty_handle(h)
42
43 def nextstatus(self, nextvalue):
44 print 'expected nextvalue to be %d, got %d' % (nextvalue,
45 self.status + 1)
46 assert self.status + 1 == nextvalue
47 self.status = nextvalue
48
49 @here_is_a_test
50 def test_simple_switch(self):
51 self.status = 0
52 h = self.sthread.new(switchbackonce_callback,
53 rffi.cast(llmemory.Address, 321))
54 assert not self.sthread.is_empty_handle(h)
55 self.nextstatus(2)
56 h = self.sthread.switch(h)
57 self.nextstatus(4)
58 print 'end', h
59 assert self.sthread.is_empty_handle(h)
60
61 @here_is_a_test
62 def test_various_depths(self):
63 self.tasks = [Task(i) for i in range(10)]
64 self.nextstep = -1
65 self.comefrom = -1
66 self.status = 0
67 while self.status < self.STATUSMAX or self.any_alive():
68 self.tasks[0].withdepth(self.random.genrand32() % 50)
69 assert len(self.tasks[0].lst) == 0
70
71 @here_is_a_test
72 def test_destroy(self):
73 # this used to give MemoryError in shadowstack tests
74 for i in range(100000):
75 self.status = 0
76 h = self.sthread.new(switchbackonce_callback,
77 rffi.cast(llmemory.Address, 321))
78 # 'h' ignored
79 if (i % 2000) == 1000:
80 rgc.collect() # This should run in < 1.5GB virtual memory
81
82 def any_alive(self):
83 for task in self.tasks:
84 if task.h:
85 return True
86 return False
87
88 @here_is_a_test
89 def test_c_callback(self):
90 #
91 self.steps = [0]
92 self.main_h = self.sthread.new(cb_stacklet_callback, llmemory.NULL)
93 self.steps.append(2)
94 call_qsort_rec(10)
95 self.steps.append(9)
96 assert not self.sthread.is_empty_handle(self.main_h)
97 self.main_h = self.sthread.switch(self.main_h)
98 assert self.sthread.is_empty_handle(self.main_h)
99 #
100 # check that self.steps == [0,1,2, 3,4,5,6, 3,4,5,6, 3,4,5,6,..., 9]
101 print self.steps
102 expected = 0
103 assert self.steps[-1] == 9
104 for i in range(len(self.steps)-1):
105 if expected == 7:
106 expected = 3
107 assert self.steps[i] == expected
108 expected += 1
109 assert expected == 7
110
111
112 class FooObj:
113 def __init__(self, n, d, next=None):
114 self.n = n
115 self.d = d
116 self.next = next
117
118
119 class Task:
120 def __init__(self, n):
121 self.n = n
122 self.h = runner.sthread.get_null_handle()
123 self.lst = []
124
125 def withdepth(self, d):
126 if d > 0:
127 foo = FooObj(self.n, d)
128 foo2 = FooObj(self.n + 100, d, foo)
129 self.lst.append(foo)
130 res = self.withdepth(d-1)
131 foo = self.lst.pop()
132 assert foo2.n == self.n + 100
133 assert foo2.d == d
134 assert foo2.next is foo
135 assert foo.n == self.n
136 assert foo.d == d
137 assert foo.next is None
138 else:
139 res = 0
140 n = intmask(runner.random.genrand32() % 10)
141 if n == self.n or (runner.status >= runner.STATUSMAX and
142 not runner.tasks[n].h):
143 return 1
144
145 print "status == %d, self.n = %d" % (runner.status, self.n)
146 assert not self.h
147 assert runner.nextstep == -1
148 runner.status += 1
149 runner.nextstep = runner.status
150 runner.comefrom = self.n
151 runner.gointo = n
152 task = runner.tasks[n]
153 if not task.h:
154 # start a new stacklet
155 print "NEW", n
156 h = runner.sthread.new(variousstackdepths_callback,
157 rffi.cast(llmemory.Address, n))
158 else:
159 # switch to this stacklet
160 print "switch to", n
161 h = task.h
162 task.h = runner.sthread.get_null_handle()
163 h = runner.sthread.switch(h)
164
165 print "back in self.n = %d, coming from %d" % (self.n,
166 runner.comefrom)
167 assert runner.nextstep == runner.status
168 runner.nextstep = -1
169 assert runner.gointo == self.n
170 assert runner.comefrom != self.n
171 assert not self.h
172 if runner.comefrom != -42:
173 assert 0 <= runner.comefrom < 10
174 task = runner.tasks[runner.comefrom]
175 assert not task.h
176 task.h = h
177 else:
178 assert runner.sthread.is_empty_handle(h)
179 runner.comefrom = -1
180 runner.gointo = -1
181 assert (res & (res-1)) == 0 # to prevent a tail-call to withdepth()
182 return res
183
184
185 runner = Runner()
186
187
188 def empty_callback(h, arg):
189 print 'in empty_callback:', h, arg
190 assert rffi.cast(lltype.Signed, arg) == 123
191 return h
192
193 def switchbackonce_callback(h, arg):
194 print 'in switchbackonce_callback:', h, arg
195 assert rffi.cast(lltype.Signed, arg) == 321
196 runner.nextstatus(1)
197 assert not runner.sthread.is_empty_handle(h)
198 h = runner.sthread.switch(h)
199 runner.nextstatus(3)
200 assert not runner.sthread.is_empty_handle(h)
201 return h
202
203 def variousstackdepths_callback(h, arg):
204 assert runner.nextstep == runner.status
205 runner.nextstep = -1
206 arg = rffi.cast(lltype.Signed, arg)
207 assert arg == runner.gointo
208 self = runner.tasks[arg]
209 assert self.n == runner.gointo
210 assert not self.h
211 assert 0 <= runner.comefrom < 10
212 task = runner.tasks[runner.comefrom]
213 assert not task.h
214 assert bool(h) and not runner.sthread.is_empty_handle(h)
215 task.h = h
216 runner.comefrom = -1
217 runner.gointo = -1
218
219 while self.withdepth(runner.random.genrand32() % 20) == 0:
220 assert len(self.lst) == 0
221
222 assert len(self.lst) == 0
223 assert not self.h
224 while 1:
225 n = intmask(runner.random.genrand32() % 10)
226 h = runner.tasks[n].h
227 if h:
228 break
229
230 assert not runner.sthread.is_empty_handle(h)
231 runner.tasks[n].h = runner.sthread.get_null_handle()
232 runner.comefrom = -42
233 runner.gointo = n
234 assert runner.nextstep == -1
235 runner.status += 1
236 runner.nextstep = runner.status
237 print "LEAVING %d to go to %d" % (self.n, n)
238 return h
239
240 QSORT_CALLBACK_PTR = lltype.Ptr(lltype.FuncType(
241 [rffi.CONST_VOIDP, rffi.CONST_VOIDP], rffi.INT))
242 qsort = rffi.llexternal('qsort',
243 [llmemory.Address, rffi.SIZE_T, rffi.SIZE_T,
244 QSORT_CALLBACK_PTR],
245 lltype.Void)
246 def cb_compare_callback(a, b):
247 runner.steps.append(3)
248 assert not runner.sthread.is_empty_handle(runner.main_h)
249 runner.main_h = runner.sthread.switch(runner.main_h)
250 assert not runner.sthread.is_empty_handle(runner.main_h)
251 runner.steps.append(6)
252 return rffi.cast(rffi.INT, 1)
253 def cb_stacklet_callback(h, arg):
254 runner.steps.append(1)
255 while True:
256 assert not runner.sthread.is_empty_handle(h)
257 h = runner.sthread.switch(h)
258 assert not runner.sthread.is_empty_handle(h)
259 if runner.steps[-1] == 9:
260 return h
261 runner.steps.append(4)
262 rgc.collect()
263 runner.steps.append(5)
264 class GcObject(object):
265 num = 1234
266 def call_qsort_rec(r):
267 if r > 0:
268 g = GcObject()
269 g.num += r
270 call_qsort_rec(r - 1)
271 assert g.num == 1234 + r
272 else:
273 raw = llmemory.raw_malloc(5)
274 qsort(raw, 5, 1, cb_compare_callback)
275 llmemory.raw_free(raw)
276
277
278 # <vmprof-hack>
279 # bah, we need to make sure that vmprof_execute_code is annotated, else
280 # rvmprof.c does not compile correctly
281 class FakeVMProfCode(object):
282 pass
283 rvmprof.register_code_object_class(FakeVMProfCode, lambda code: 'name')
284 @rvmprof.vmprof_execute_code("xcode1", lambda code, num: code)
285 def fake_vmprof_main(code, num):
286 return 42
287 # </vmprof-hack>
288
289 def entry_point(argv):
290 # <vmprof-hack>
291 if NonConstant(False):
292 fake_vmprof_main(FakeVMProfCode(), 42)
293 # </vmprof-hack>
294 #
295 seed = 0
296 if len(argv) > 1:
297 seed = int(argv[1])
298 runner.init(seed)
299 for name, meth in Runner.TESTS:
300 print '-----', name, '-----'
301 meth(runner)
302 print '----- all done -----'
303 runner.done()
304 return 0
305
306
307 class BaseTestStacklet(StandaloneTests):
308
309 def setup_class(cls):
310 from rpython.config.translationoption import get_combined_translation_config
311 config = get_combined_translation_config(translating=True)
312 config.translation.gc = cls.gc
313 if cls.gcrootfinder is not None:
314 config.translation.continuation = True
315 config.translation.gcrootfinder = cls.gcrootfinder
316 GCROOTFINDER = cls.gcrootfinder
317 cls.config = config
318 cls.old_status_max = Runner.STATUSMAX
319 Runner.STATUSMAX = 25000
320
321 def teardown_class(cls):
322 Runner.STATUSMAX = cls.old_status_max
323
324 def test_demo1(self):
325 t, cbuilder = self.compile(entry_point)
326
327 for i in range(15):
328 if (i & 1) == 0:
329 env = {}
330 else:
331 env = {'PYPY_GC_NURSERY': '2k'}
332 print 'running %s/%s with arg=%d and env=%r' % (
333 self.gc, self.gcrootfinder, i, env)
334 data = cbuilder.cmdexec('%d' % i, env=env)
335 assert data.endswith("----- all done -----\n")
336 for name, meth in Runner.TESTS:
337 assert ('----- %s -----\n' % name) in data
338
339
340 class DONTTestStackletBoehm(BaseTestStacklet):
341 # Boehm does not work well with stacklets, probably because the
342 # moved-away copies of the stack are parsed using a different
343 # selection logic than the real stack
344 gc = 'boehm'
345 gcrootfinder = None
346
347
348 class TestStackletShadowStack(BaseTestStacklet):
349 gc = 'minimark'
350 gcrootfinder = 'shadowstack'
351
352
353 def test_dont_keep_debug_to_true():
354 assert not rstacklet.DEBUG
355
356
357 def target(*args):
358 return entry_point, None
359
360 if __name__ == '__main__':
361 import sys
362 sys.exit(entry_point(sys.argv))