| 1 |
from rpython.conftest import option |
| 2 |
from rpython.translator.translator import TranslationContext, graphof |
| 3 |
from rpython.translator.backendopt.all import backend_optimizations |
| 4 |
from rpython.translator.transform import insert_ll_stackcheck |
| 5 |
from rpython.memory.gctransform import shadowstack |
| 6 |
|
| 7 |
def _follow_path_naive(block, cur_path, accum): |
| 8 |
cur_path = (cur_path, block) |
| 9 |
if not block.exits: |
| 10 |
ops = [] |
| 11 |
while cur_path: |
| 12 |
block = cur_path[1] |
| 13 |
ops.extend(reversed(block.operations)) |
| 14 |
cur_path = cur_path[0] |
| 15 |
accum.append(list(reversed(ops))) |
| 16 |
return |
| 17 |
for link in block.exits: |
| 18 |
_follow_path_naive(link.target, cur_path, accum) |
| 19 |
|
| 20 |
|
| 21 |
def paths_naive(g): |
| 22 |
accum = [] |
| 23 |
_follow_path_naive(g.startblock, None, accum) |
| 24 |
return accum |
| 25 |
|
| 26 |
def direct_target(spaceop): |
| 27 |
return spaceop.args[0].value._obj.graph.name |
| 28 |
|
| 29 |
def direct_calls(p): |
| 30 |
names = [] |
| 31 |
for spaceop in p: |
| 32 |
if spaceop.opname == 'direct_call': |
| 33 |
names.append(direct_target(spaceop)) |
| 34 |
return names |
| 35 |
|
| 36 |
|
| 37 |
def check(g, funcname, ignore=None): |
| 38 |
paths = paths_naive(g) |
| 39 |
relevant = [] |
| 40 |
for p in paths: |
| 41 |
funcs_called = direct_calls(p) |
| 42 |
if funcname in funcs_called and ignore not in funcs_called: |
| 43 |
assert 'stack_check___' in funcs_called |
| 44 |
assert (funcs_called.index(funcname) > |
| 45 |
funcs_called.index('stack_check___')) |
| 46 |
relevant.append(p) |
| 47 |
return relevant |
| 48 |
|
| 49 |
|
| 50 |
class A(object): |
| 51 |
def __init__(self, n): |
| 52 |
self.n = n |
| 53 |
|
| 54 |
def f(a): |
| 55 |
x = A(a.n+1) |
| 56 |
if x.n == 10: |
| 57 |
return |
| 58 |
f(x) |
| 59 |
|
| 60 |
def g(n): |
| 61 |
f(A(n)) |
| 62 |
return 0 |
| 63 |
|
| 64 |
def test_simple(): |
| 65 |
t = TranslationContext() |
| 66 |
a = t.buildannotator() |
| 67 |
a.build_types(g, [int]) |
| 68 |
a.simplify() |
| 69 |
t.buildrtyper().specialize() |
| 70 |
backend_optimizations(t) |
| 71 |
t.checkgraphs() |
| 72 |
n = insert_ll_stackcheck(t) |
| 73 |
t.checkgraphs() |
| 74 |
assert n == 1 |
| 75 |
if option.view: |
| 76 |
t.view() |
| 77 |
check(graphof(t, f), 'f') |
| 78 |
|
| 79 |
def test_gctransformed(): |
| 80 |
t = TranslationContext() |
| 81 |
a = t.buildannotator() |
| 82 |
a.build_types(g, [int]) |
| 83 |
a.simplify() |
| 84 |
t.buildrtyper().specialize() |
| 85 |
backend_optimizations(t) |
| 86 |
t.checkgraphs() |
| 87 |
n = insert_ll_stackcheck(t) |
| 88 |
t.checkgraphs() |
| 89 |
assert n == 1 |
| 90 |
exctransf = t.getexceptiontransformer() |
| 91 |
f_graph = graphof(t, f) |
| 92 |
exctransf.create_exception_handling(f_graph) |
| 93 |
if option.view: |
| 94 |
f_graph.show() |
| 95 |
check(f_graph, 'f') |
| 96 |
|
| 97 |
class GCTransform(shadowstack.ShadowStackFrameworkGCTransformer): |
| 98 |
from rpython.memory.gc.generation import GenerationGC as \ |
| 99 |
GCClass |
| 100 |
GC_PARAMS = {} |
| 101 |
|
| 102 |
gctransf = GCTransform(t) |
| 103 |
gctransf.transform_graph(f_graph) |
| 104 |
if option.view: |
| 105 |
f_graph.show() |
| 106 |
relevant = check(f_graph, 'f') |
| 107 |
for p in relevant: |
| 108 |
in_between = False |
| 109 |
reload = 0 |
| 110 |
for spaceop in p: |
| 111 |
if spaceop.opname == 'direct_call': |
| 112 |
target = direct_target(spaceop) |
| 113 |
if target == 'f': |
| 114 |
in_between = False |
| 115 |
elif target == 'stack_check___': |
| 116 |
in_between = True |
| 117 |
if in_between and spaceop.opname == 'gc_reload_possibly_moved': |
| 118 |
reload += 1 |
| 119 |
|
| 120 |
assert reload == 0 |