| 1 |
from rpython.annotator.listdef import s_list_of_strings |
| 2 |
from rpython.annotator.model import SomeInteger |
| 3 |
from rpython.flowspace.model import Constant, SpaceOperation, mkentrymap |
| 4 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 5 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 6 |
from rpython.memory.gc.semispace import SemiSpaceGC |
| 7 |
from rpython.memory.gctransform.framework import (CollectAnalyzer, |
| 8 |
find_initializing_stores, find_clean_setarrayitems) |
| 9 |
from rpython.memory.gctransform.shadowstack import ( |
| 10 |
ShadowStackFrameworkGCTransformer) |
| 11 |
from rpython.memory.gctransform.test.test_transform import rtype |
| 12 |
from rpython.memory.gctransform.transform import GcHighLevelOp |
| 13 |
from rpython.rtyper.rtyper import LowLevelOpList |
| 14 |
from rpython.translator.backendopt.all import backend_optimizations |
| 15 |
from rpython.translator.c.gc import BasicFrameworkGcPolicy |
| 16 |
from rpython.translator.exceptiontransform import ExceptionTransformer |
| 17 |
from rpython.translator.translator import TranslationContext, graphof |
| 18 |
from rpython.translator.unsimplify import varoftype |
| 19 |
|
| 20 |
import py |
| 21 |
|
| 22 |
class FrameworkGcPolicy2(BasicFrameworkGcPolicy): |
| 23 |
class transformerclass(ShadowStackFrameworkGCTransformer): |
| 24 |
root_stack_depth = 100 |
| 25 |
|
| 26 |
def test_framework_simple(): |
| 27 |
def g(x): |
| 28 |
return x + 1 |
| 29 |
class A(object): |
| 30 |
pass |
| 31 |
def entrypoint(argv): |
| 32 |
a = A() |
| 33 |
a.b = g(1) |
| 34 |
return str(a.b) |
| 35 |
|
| 36 |
from rpython.rtyper.llinterp import LLInterpreter |
| 37 |
from rpython.translator.c.genc import CStandaloneBuilder |
| 38 |
|
| 39 |
t = rtype(entrypoint, [s_list_of_strings]) |
| 40 |
t.config.translation.gc = "minimark" |
| 41 |
cbuild = CStandaloneBuilder(t, entrypoint, t.config, |
| 42 |
gcpolicy=FrameworkGcPolicy2) |
| 43 |
cbuild.make_entrypoint_wrapper = False |
| 44 |
db = cbuild.build_database() |
| 45 |
entrypointptr = cbuild.getentrypointptr() |
| 46 |
entrygraph = entrypointptr._obj.graph |
| 47 |
|
| 48 |
r_list_of_strings = t.rtyper.getrepr(s_list_of_strings) |
| 49 |
ll_argv = r_list_of_strings.convert_const([]) |
| 50 |
|
| 51 |
llinterp = LLInterpreter(t.rtyper) |
| 52 |
|
| 53 |
|
| 54 |
setupgraph = db.gctransformer.frameworkgc_setup_ptr.value._obj.graph |
| 55 |
llinterp.eval_graph(setupgraph, []) |
| 56 |
|
| 57 |
res = llinterp.eval_graph(entrygraph, [ll_argv]) |
| 58 |
|
| 59 |
assert ''.join(res.chars) == "2" |
| 60 |
|
| 61 |
def test_cancollect(): |
| 62 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 63 |
def g(): |
| 64 |
lltype.malloc(S, zero=True) |
| 65 |
t = rtype(g, []) |
| 66 |
gg = graphof(t, g) |
| 67 |
assert CollectAnalyzer(t).analyze_direct_call(gg) |
| 68 |
|
| 69 |
def g(x): |
| 70 |
return -x |
| 71 |
t = rtype(g, [int]) |
| 72 |
gg = graphof(t, g) |
| 73 |
assert not CollectAnalyzer(t).analyze_direct_call(gg) |
| 74 |
|
| 75 |
def test_cancollect_external(): |
| 76 |
fext1 = rffi.llexternal('fext1', [], lltype.Void, releasegil=False) |
| 77 |
def g(): |
| 78 |
fext1() |
| 79 |
t = rtype(g, []) |
| 80 |
gg = graphof(t, g) |
| 81 |
assert not CollectAnalyzer(t).analyze_direct_call(gg) |
| 82 |
|
| 83 |
fext2 = rffi.llexternal('fext2', [], lltype.Void, releasegil=True) |
| 84 |
def g(): |
| 85 |
fext2() |
| 86 |
t = rtype(g, []) |
| 87 |
gg = graphof(t, g) |
| 88 |
assert CollectAnalyzer(t).analyze_direct_call(gg) |
| 89 |
|
| 90 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 91 |
FUNC = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Void)) |
| 92 |
fext3 = rffi.llexternal('fext3', [FUNC], lltype.Void, releasegil=False) |
| 93 |
def h(x): |
| 94 |
lltype.malloc(S, zero=True) |
| 95 |
def g(): |
| 96 |
fext3(h) |
| 97 |
t = rtype(g, []) |
| 98 |
gg = graphof(t, g) |
| 99 |
assert CollectAnalyzer(t).analyze_direct_call(gg) |
| 100 |
|
| 101 |
def test_no_collect(): |
| 102 |
from rpython.rlib import rgc |
| 103 |
from rpython.translator.c.genc import CStandaloneBuilder |
| 104 |
|
| 105 |
@rgc.no_collect |
| 106 |
def g(): |
| 107 |
return 1 |
| 108 |
|
| 109 |
assert g._dont_inline_ |
| 110 |
assert g._gc_no_collect_ |
| 111 |
|
| 112 |
def entrypoint(argv): |
| 113 |
return g() + 2 |
| 114 |
|
| 115 |
t = rtype(entrypoint, [s_list_of_strings]) |
| 116 |
t.config.translation.gc = "minimark" |
| 117 |
cbuild = CStandaloneBuilder(t, entrypoint, t.config, |
| 118 |
gcpolicy=FrameworkGcPolicy2) |
| 119 |
cbuild.make_entrypoint_wrapper = False |
| 120 |
db = cbuild.build_database() |
| 121 |
|
| 122 |
def test_no_collect_detection(): |
| 123 |
from rpython.rlib import rgc |
| 124 |
from rpython.translator.c.genc import CStandaloneBuilder |
| 125 |
|
| 126 |
class A(object): |
| 127 |
def __init__(self, x): |
| 128 |
self.x = x |
| 129 |
|
| 130 |
@rgc.no_collect |
| 131 |
def g(): |
| 132 |
return A(1).x |
| 133 |
|
| 134 |
assert g._dont_inline_ |
| 135 |
assert g._gc_no_collect_ |
| 136 |
|
| 137 |
def entrypoint(argv): |
| 138 |
return g() + 2 |
| 139 |
|
| 140 |
t = rtype(entrypoint, [s_list_of_strings]) |
| 141 |
t.config.translation.gc = "minimark" |
| 142 |
cbuild = CStandaloneBuilder(t, entrypoint, t.config, |
| 143 |
gcpolicy=FrameworkGcPolicy2) |
| 144 |
cbuild.make_entrypoint_wrapper = False |
| 145 |
with py.test.raises(Exception) as f: |
| 146 |
cbuild.build_database() |
| 147 |
expected = "'no_collect' function can trigger collection: <function g at " |
| 148 |
assert str(f.value).startswith(expected) |
| 149 |
|
| 150 |
def test_custom_trace_function_no_collect(): |
| 151 |
from rpython.rlib import rgc |
| 152 |
from rpython.translator.c.genc import CStandaloneBuilder |
| 153 |
|
| 154 |
S = lltype.GcStruct("MyStructure") |
| 155 |
class Glob: |
| 156 |
pass |
| 157 |
glob = Glob() |
| 158 |
def trace_func(gc, obj, callback, arg1, arg2): |
| 159 |
glob.foo = (gc, obj) |
| 160 |
lambda_trace_func = lambda: trace_func |
| 161 |
def entrypoint(argv): |
| 162 |
lltype.malloc(S) |
| 163 |
rgc.register_custom_trace_hook(S, lambda_trace_func) |
| 164 |
return 0 |
| 165 |
|
| 166 |
def run_rtyper(fn): |
| 167 |
t = rtype(fn, [s_list_of_strings]) |
| 168 |
t.config.translation.gc = "minimark" |
| 169 |
cbuild = CStandaloneBuilder(t, fn, t.config, |
| 170 |
gcpolicy=FrameworkGcPolicy2) |
| 171 |
cbuild.make_entrypoint_wrapper = False |
| 172 |
cbuild.build_database() |
| 173 |
return True |
| 174 |
|
| 175 |
with py.test.raises(Exception) as f: |
| 176 |
run_rtyper(entrypoint) |
| 177 |
assert 'can cause the GC to be called' in str(f.value) |
| 178 |
assert 'trace_func' in str(f.value) |
| 179 |
assert 'MyStructure' in str(f.value) |
| 180 |
|
| 181 |
|
| 182 |
trace_func._skip_collect_analyzer_ = True |
| 183 |
assert run_rtyper(entrypoint) |
| 184 |
|
| 185 |
|
| 186 |
class WriteBarrierTransformer(ShadowStackFrameworkGCTransformer): |
| 187 |
clean_sets = {} |
| 188 |
GC_PARAMS = {} |
| 189 |
class GCClass(SemiSpaceGC): |
| 190 |
needs_write_barrier = True |
| 191 |
def writebarrier_before_copy(self, source, dest, |
| 192 |
source_start, dest_start, length): |
| 193 |
return True |
| 194 |
def writebarrier_before_move(self, *args): |
| 195 |
raise NotImplementedError |
| 196 |
|
| 197 |
def write_barrier_check(spaceop, needs_write_barrier=True): |
| 198 |
t = TranslationContext() |
| 199 |
t.buildannotator().build_types(lambda x:x, [SomeInteger()]) |
| 200 |
t.buildrtyper().specialize() |
| 201 |
transformer = WriteBarrierTransformer(t) |
| 202 |
llops = LowLevelOpList() |
| 203 |
hop = GcHighLevelOp(transformer, spaceop, 0, llops) |
| 204 |
hop.dispatch() |
| 205 |
found = False |
| 206 |
print spaceop, '======>' |
| 207 |
for op in llops: |
| 208 |
print '\t', op |
| 209 |
if op.opname == 'direct_call': |
| 210 |
found = True |
| 211 |
assert found == needs_write_barrier |
| 212 |
|
| 213 |
def test_write_barrier_support_setfield(): |
| 214 |
PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) |
| 215 |
PTR_TYPE = lltype.Ptr(lltype.GcStruct('S', ('x', PTR_TYPE2))) |
| 216 |
write_barrier_check(SpaceOperation( |
| 217 |
"setfield", |
| 218 |
[varoftype(PTR_TYPE), Constant('x', lltype.Void), |
| 219 |
varoftype(PTR_TYPE2)], |
| 220 |
varoftype(lltype.Void))) |
| 221 |
|
| 222 |
|
| 223 |
def test_dont_add_write_barrier_for_constant_new_value(): |
| 224 |
PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) |
| 225 |
PTR_TYPE = lltype.Ptr(lltype.GcStruct('S', ('x', PTR_TYPE2))) |
| 226 |
write_barrier_check(SpaceOperation( |
| 227 |
"setfield", |
| 228 |
[varoftype(PTR_TYPE), Constant('x', lltype.Void), |
| 229 |
Constant('foo', varoftype(PTR_TYPE2))], |
| 230 |
varoftype(lltype.Void)), needs_write_barrier=False) |
| 231 |
|
| 232 |
def test_write_barrier_support_setarrayitem(): |
| 233 |
PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) |
| 234 |
ARRAYPTR = lltype.Ptr(lltype.GcArray(PTR_TYPE2)) |
| 235 |
write_barrier_check(SpaceOperation( |
| 236 |
"setarrayitem", |
| 237 |
[varoftype(ARRAYPTR), varoftype(lltype.Signed), |
| 238 |
varoftype(PTR_TYPE2)], |
| 239 |
varoftype(lltype.Void))) |
| 240 |
|
| 241 |
def test_write_barrier_support_setinteriorfield(): |
| 242 |
PTR_TYPE2 = lltype.Ptr(lltype.GcStruct('T', ('y', lltype.Signed))) |
| 243 |
ARRAYPTR2 = lltype.Ptr(lltype.GcArray(('a', lltype.Signed), |
| 244 |
('b', PTR_TYPE2))) |
| 245 |
write_barrier_check(SpaceOperation( |
| 246 |
"setinteriorfield", |
| 247 |
[varoftype(ARRAYPTR2), varoftype(lltype.Signed), |
| 248 |
Constant('b', lltype.Void), varoftype(PTR_TYPE2)], |
| 249 |
varoftype(lltype.Void))) |
| 250 |
|
| 251 |
def test_remove_duplicate_write_barrier(): |
| 252 |
from rpython.translator.c.genc import CStandaloneBuilder |
| 253 |
from rpython.flowspace.model import summary |
| 254 |
|
| 255 |
class A(object): |
| 256 |
pass |
| 257 |
glob_a_1 = A() |
| 258 |
glob_a_2 = A() |
| 259 |
|
| 260 |
def f(a, cond): |
| 261 |
a.x = a |
| 262 |
a.z = a |
| 263 |
if cond: |
| 264 |
a.y = a |
| 265 |
def g(): |
| 266 |
f(glob_a_1, 5) |
| 267 |
f(glob_a_2, 0) |
| 268 |
t = rtype(g, []) |
| 269 |
t.config.translation.gc = "minimark" |
| 270 |
cbuild = CStandaloneBuilder(t, g, t.config, |
| 271 |
gcpolicy=FrameworkGcPolicy2) |
| 272 |
cbuild.make_entrypoint_wrapper = False |
| 273 |
db = cbuild.build_database() |
| 274 |
|
| 275 |
ff = graphof(t, f) |
| 276 |
|
| 277 |
assert summary(ff)['direct_call'] == 1 |
| 278 |
|
| 279 |
def test_find_initializing_stores(): |
| 280 |
|
| 281 |
class A(object): |
| 282 |
pass |
| 283 |
class B(object): |
| 284 |
pass |
| 285 |
def f(): |
| 286 |
a = A() |
| 287 |
b = B() |
| 288 |
b.a = a |
| 289 |
b.b = 1 |
| 290 |
t = rtype(f, []) |
| 291 |
etrafo = ExceptionTransformer(t) |
| 292 |
graphs = etrafo.transform_completely() |
| 293 |
collect_analyzer = CollectAnalyzer(t) |
| 294 |
init_stores = find_initializing_stores(collect_analyzer, t.graphs[0], |
| 295 |
mkentrymap(t.graphs[0])) |
| 296 |
assert len(init_stores) == 1 |
| 297 |
|
| 298 |
def test_find_initializing_stores_across_blocks(): |
| 299 |
|
| 300 |
class A(object): |
| 301 |
pass |
| 302 |
class B(object): |
| 303 |
pass |
| 304 |
def f(x): |
| 305 |
a1 = A() |
| 306 |
a2 = A() |
| 307 |
a = A() |
| 308 |
b = B() |
| 309 |
b.a = a |
| 310 |
if x: |
| 311 |
b.b = a1 |
| 312 |
b.c = a2 |
| 313 |
else: |
| 314 |
b.c = a1 |
| 315 |
b.b = a2 |
| 316 |
t = rtype(f, [int]) |
| 317 |
etrafo = ExceptionTransformer(t) |
| 318 |
graphs = etrafo.transform_completely() |
| 319 |
collect_analyzer = CollectAnalyzer(t) |
| 320 |
init_stores = find_initializing_stores(collect_analyzer, t.graphs[0], |
| 321 |
mkentrymap(t.graphs[0])) |
| 322 |
assert len(init_stores) == 5 |
| 323 |
|
| 324 |
def test_find_clean_setarrayitems(): |
| 325 |
S = lltype.GcStruct('S') |
| 326 |
A = lltype.GcArray(lltype.Ptr(S)) |
| 327 |
|
| 328 |
def f(): |
| 329 |
l = lltype.malloc(A, 3) |
| 330 |
l[0] = lltype.malloc(S) |
| 331 |
l[1] = lltype.malloc(S) |
| 332 |
l[2] = lltype.malloc(S) |
| 333 |
x = l[1] |
| 334 |
l[0] = x |
| 335 |
return len(l) |
| 336 |
|
| 337 |
t = rtype(f, []) |
| 338 |
etrafo = ExceptionTransformer(t) |
| 339 |
graph = etrafo.transform_completely() |
| 340 |
collect_analyzer = CollectAnalyzer(t) |
| 341 |
clean_setarrayitems = find_clean_setarrayitems(collect_analyzer, |
| 342 |
t.graphs[0]) |
| 343 |
assert len(clean_setarrayitems) == 1 |
| 344 |
|
| 345 |
def test_find_clean_setarrayitems_2(): |
| 346 |
S = lltype.GcStruct('S') |
| 347 |
A = lltype.GcArray(lltype.Ptr(S)) |
| 348 |
|
| 349 |
def f(): |
| 350 |
l = lltype.malloc(A, 3) |
| 351 |
l[0] = lltype.malloc(S) |
| 352 |
l[1] = lltype.malloc(S) |
| 353 |
l[2] = lltype.malloc(S) |
| 354 |
x = l[1] |
| 355 |
l[2] = lltype.malloc(S) |
| 356 |
l[0] = x |
| 357 |
return len(l) |
| 358 |
|
| 359 |
t = rtype(f, []) |
| 360 |
etrafo = ExceptionTransformer(t) |
| 361 |
graph = etrafo.transform_completely() |
| 362 |
collect_analyzer = CollectAnalyzer(t) |
| 363 |
clean_setarrayitems = find_clean_setarrayitems(collect_analyzer, |
| 364 |
t.graphs[0]) |
| 365 |
assert len(clean_setarrayitems) == 0 |
| 366 |
|
| 367 |
def test_find_clean_setarrayitems_3(): |
| 368 |
S = lltype.GcStruct('S') |
| 369 |
A = lltype.GcArray(lltype.Ptr(S)) |
| 370 |
|
| 371 |
def f(): |
| 372 |
l = lltype.malloc(A, 3) |
| 373 |
l[0] = lltype.malloc(S) |
| 374 |
l[1] = lltype.malloc(S) |
| 375 |
l[2] = lltype.malloc(S) |
| 376 |
l2 = lltype.malloc(A, 4) |
| 377 |
x = l[1] |
| 378 |
l2[0] = x |
| 379 |
return len(l) |
| 380 |
|
| 381 |
t = rtype(f, []) |
| 382 |
etrafo = ExceptionTransformer(t) |
| 383 |
graph = etrafo.transform_completely() |
| 384 |
collect_analyzer = CollectAnalyzer(t) |
| 385 |
clean_setarrayitems = find_clean_setarrayitems(collect_analyzer, |
| 386 |
t.graphs[0]) |
| 387 |
assert len(clean_setarrayitems) == 0 |
| 388 |
|
| 389 |
def test_list_operations(): |
| 390 |
|
| 391 |
class A(object): |
| 392 |
pass |
| 393 |
|
| 394 |
def f(): |
| 395 |
l = [A(), A()] |
| 396 |
l.append(A()) |
| 397 |
l[1] = l[0] |
| 398 |
return len(l) |
| 399 |
|
| 400 |
t = rtype(f, []) |
| 401 |
backend_optimizations(t, storesink=True) |
| 402 |
etrafo = ExceptionTransformer(t) |
| 403 |
graph = etrafo.transform_completely() |
| 404 |
collect_analyzer = CollectAnalyzer(t) |
| 405 |
clean_setarrayitems = find_clean_setarrayitems(collect_analyzer, |
| 406 |
t.graphs[0]) |
| 407 |
assert len(clean_setarrayitems) == 1 |