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