| 1 |
import py |
| 2 |
from rpython.memory.gctransform.test.test_transform import rtype, rtype_and_transform, getops |
| 3 |
from rpython.memory.gctransform.test.test_transform import LLInterpedTranformerTests |
| 4 |
from rpython.memory.gctransform.refcounting import RefcountingGCTransformer |
| 5 |
from rpython.rtyper.lltypesystem import lltype |
| 6 |
from rpython.translator.translator import TranslationContext, graphof |
| 7 |
from rpython.translator.c.gc import RefcountingGcPolicy |
| 8 |
from rpython.conftest import option |
| 9 |
|
| 10 |
class TestLLInterpedRefcounting(LLInterpedTranformerTests): |
| 11 |
gcpolicy = RefcountingGcPolicy |
| 12 |
|
| 13 |
def test_llinterp_refcounted_graph_with_del(self): |
| 14 |
from rpython.annotator.model import SomeInteger |
| 15 |
|
| 16 |
class D: |
| 17 |
pass |
| 18 |
|
| 19 |
delcounter = D() |
| 20 |
delcounter.dels = 0 |
| 21 |
|
| 22 |
class C: |
| 23 |
def __del__(self): |
| 24 |
delcounter.dels += 1 |
| 25 |
c = C() |
| 26 |
c.x = 1 |
| 27 |
def h(x): |
| 28 |
if x: |
| 29 |
return c |
| 30 |
else: |
| 31 |
d = C() |
| 32 |
d.x = 2 |
| 33 |
return d |
| 34 |
def g(x): |
| 35 |
return h(x).x |
| 36 |
def f(x): |
| 37 |
r = g(x) |
| 38 |
return r + delcounter.dels |
| 39 |
|
| 40 |
llinterp, graph = self.llinterpreter_for_transformed_graph(f, [SomeInteger()]) |
| 41 |
|
| 42 |
res = llinterp.eval_graph(graph, [1]) |
| 43 |
assert res == 1 |
| 44 |
res = llinterp.eval_graph(graph, [0]) |
| 45 |
assert res == 3 |
| 46 |
|
| 47 |
def test_raw_instance_flavor(self): |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
py.test.skip("a probably-illegal test") |
| 52 |
class State: |
| 53 |
pass |
| 54 |
state = State() |
| 55 |
class Y: |
| 56 |
def __del__(self): |
| 57 |
state.freed_counter += 1 |
| 58 |
class X: |
| 59 |
_alloc_flavor_ = 'raw' |
| 60 |
def g(): |
| 61 |
x = X() |
| 62 |
x.y = Y() |
| 63 |
return x |
| 64 |
def f(): |
| 65 |
from rpython.rlib.objectmodel import free_non_gc_object |
| 66 |
state.freed_counter = 0 |
| 67 |
x = g() |
| 68 |
assert state.freed_counter == 0 |
| 69 |
x.y = None |
| 70 |
assert state.freed_counter == 1 |
| 71 |
free_non_gc_object(x) |
| 72 |
|
| 73 |
|
| 74 |
llinterp, graph = self.llinterpreter_for_transformed_graph(f, []) |
| 75 |
llinterp.eval_graph(graph, []) |
| 76 |
|
| 77 |
def test_simple_barrier(): |
| 78 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 79 |
T = lltype.GcStruct("T", ('s', lltype.Ptr(S))) |
| 80 |
def f(): |
| 81 |
s1 = lltype.malloc(S) |
| 82 |
s1.x = 1 |
| 83 |
s2 = lltype.malloc(S) |
| 84 |
s2.x = 2 |
| 85 |
t = lltype.malloc(T) |
| 86 |
t.s = s1 |
| 87 |
t.s = s2 |
| 88 |
return t |
| 89 |
t, transformer = rtype_and_transform(f, [], RefcountingGCTransformer, |
| 90 |
check=False) |
| 91 |
graph = graphof(t, f) |
| 92 |
ops = getops(graph) |
| 93 |
assert len(ops['getfield']) == 2 |
| 94 |
assert len(ops['bare_setfield']) == 4 |
| 95 |
|
| 96 |
def test_arraybarrier(): |
| 97 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 98 |
A = lltype.GcArray(lltype.Ptr(S)) |
| 99 |
def f(): |
| 100 |
s1 = lltype.malloc(S) |
| 101 |
s1.x = 1 |
| 102 |
s2 = lltype.malloc(S) |
| 103 |
s2.x = 2 |
| 104 |
a = lltype.malloc(A, 1) |
| 105 |
a[0] = s1 |
| 106 |
a[0] = s2 |
| 107 |
t, transformer = rtype_and_transform(f, [], RefcountingGCTransformer, |
| 108 |
check=False) |
| 109 |
graph = graphof(t, f) |
| 110 |
ops = getops(graph) |
| 111 |
assert len(ops['getarrayitem']) == 2 |
| 112 |
assert len(ops['bare_setarrayitem']) == 2 |
| 113 |
assert len(ops['bare_setfield']) == 2 |
| 114 |
|
| 115 |
def make_deallocator(TYPE, |
| 116 |
attr="static_deallocation_funcptr_for_type", |
| 117 |
cls=RefcountingGCTransformer): |
| 118 |
if TYPE._is_varsize(): |
| 119 |
def f(): |
| 120 |
return lltype.malloc(TYPE, 1) |
| 121 |
else: |
| 122 |
def f(): |
| 123 |
return lltype.malloc(TYPE) |
| 124 |
t = TranslationContext() |
| 125 |
t.buildannotator().build_types(f, []) |
| 126 |
t.buildrtyper().specialize() |
| 127 |
transformer = cls(t) |
| 128 |
fptr = getattr(transformer, attr)(TYPE) |
| 129 |
transformer.transform_graph(graphof(t, f)) |
| 130 |
transformer.finish(backendopt=False) |
| 131 |
if option.view: |
| 132 |
t.view() |
| 133 |
if fptr: |
| 134 |
return fptr._obj.graph, t |
| 135 |
else: |
| 136 |
return None, t |
| 137 |
|
| 138 |
def test_deallocator_simple(): |
| 139 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 140 |
dgraph, t = make_deallocator(S) |
| 141 |
ops = [] |
| 142 |
for block in dgraph.iterblocks(): |
| 143 |
ops.extend([op for op in block.operations if op.opname != 'same_as']) |
| 144 |
assert len(ops) == 1 |
| 145 |
op = ops[0] |
| 146 |
assert op.opname == 'gc_free' |
| 147 |
|
| 148 |
def test_deallocator_less_simple(): |
| 149 |
TPtr = lltype.Ptr(lltype.GcStruct("T", ('a', lltype.Signed))) |
| 150 |
S = lltype.GcStruct( |
| 151 |
"S", |
| 152 |
('x', lltype.Signed), |
| 153 |
('y', TPtr), |
| 154 |
('z', TPtr), |
| 155 |
) |
| 156 |
dgraph, t = make_deallocator(S) |
| 157 |
ops = getops(dgraph) |
| 158 |
assert len(ops['direct_call']) == 2 |
| 159 |
assert len(ops['getfield']) == 2 |
| 160 |
assert len(ops['gc_free']) == 1 |
| 161 |
|
| 162 |
def test_deallocator_array(): |
| 163 |
TPtr = lltype.Ptr(lltype.GcStruct("T", ('a', lltype.Signed))) |
| 164 |
GcA = lltype.GcArray(('x', TPtr), ('y', TPtr)) |
| 165 |
A = lltype.Array(('x', TPtr), ('y', TPtr)) |
| 166 |
APtr = lltype.Ptr(GcA) |
| 167 |
S = lltype.GcStruct('S', ('t', TPtr), ('x', lltype.Signed), ('aptr', APtr), |
| 168 |
('rest', A)) |
| 169 |
dgraph, t = make_deallocator(S) |
| 170 |
ops = getops(dgraph) |
| 171 |
assert len(ops['direct_call']) == 4 |
| 172 |
assert len(ops['getfield']) == 2 |
| 173 |
assert len(ops['getinteriorfield']) == 2 |
| 174 |
assert len(ops['getinteriorarraysize']) == 1 |
| 175 |
assert len(ops['gc_free']) == 1 |
| 176 |
|
| 177 |
def test_deallocator_with_destructor(): |
| 178 |
S = lltype.GcStruct("S", ('x', lltype.Signed), rtti=True) |
| 179 |
def f(s): |
| 180 |
s.x = 1 |
| 181 |
def type_info_S(p): |
| 182 |
return lltype.getRuntimeTypeInfo(S) |
| 183 |
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], |
| 184 |
lltype.Ptr(lltype.RuntimeTypeInfo)), |
| 185 |
"type_info_S", |
| 186 |
_callable=type_info_S) |
| 187 |
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], |
| 188 |
lltype.Void), |
| 189 |
"destructor_funcptr", |
| 190 |
_callable=f) |
| 191 |
pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp) |
| 192 |
graph, t = make_deallocator(S) |
| 193 |
|
| 194 |
def test_caching_dynamic_deallocator(): |
| 195 |
S = lltype.GcStruct("S", ('x', lltype.Signed), rtti=True) |
| 196 |
S1 = lltype.GcStruct("S1", ('s', S), ('y', lltype.Signed), rtti=True) |
| 197 |
T = lltype.GcStruct("T", ('x', lltype.Signed), rtti=True) |
| 198 |
def f_S(s): |
| 199 |
s.x = 1 |
| 200 |
def f_S1(s1): |
| 201 |
s1.s.x = 1 |
| 202 |
s1.y = 2 |
| 203 |
def f_T(s): |
| 204 |
s.x = 1 |
| 205 |
def type_info_S(p): |
| 206 |
return lltype.getRuntimeTypeInfo(S) |
| 207 |
def type_info_T(p): |
| 208 |
return lltype.getRuntimeTypeInfo(T) |
| 209 |
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], |
| 210 |
lltype.Ptr(lltype.RuntimeTypeInfo)), |
| 211 |
"type_info_S", |
| 212 |
_callable=type_info_S) |
| 213 |
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], |
| 214 |
lltype.Void), |
| 215 |
"destructor_funcptr", |
| 216 |
_callable=f_S) |
| 217 |
pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp) |
| 218 |
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)], |
| 219 |
lltype.Void), |
| 220 |
"destructor_funcptr", |
| 221 |
_callable=f_S1) |
| 222 |
pinf = lltype.attachRuntimeTypeInfo(S1, qp, destrptr=dp) |
| 223 |
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(T)], |
| 224 |
lltype.Ptr(lltype.RuntimeTypeInfo)), |
| 225 |
"type_info_S", |
| 226 |
_callable=type_info_T) |
| 227 |
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(T)], |
| 228 |
lltype.Void), |
| 229 |
"destructor_funcptr", |
| 230 |
_callable=f_T) |
| 231 |
pinf = lltype.attachRuntimeTypeInfo(T, qp, destrptr=dp) |
| 232 |
def f(): |
| 233 |
pass |
| 234 |
t = TranslationContext() |
| 235 |
t.buildannotator().build_types(f, []) |
| 236 |
t.buildrtyper().specialize() |
| 237 |
transformer = RefcountingGCTransformer(t) |
| 238 |
p_S = transformer.dynamic_deallocation_funcptr_for_type(S) |
| 239 |
p_S1 = transformer.dynamic_deallocation_funcptr_for_type(S1) |
| 240 |
p_T = transformer.dynamic_deallocation_funcptr_for_type(T) |
| 241 |
assert p_S is not p_T |
| 242 |
assert p_S is p_S1 |
| 243 |
|
| 244 |
def test_dynamic_deallocator(): |
| 245 |
class A(object): |
| 246 |
pass |
| 247 |
class B(A): |
| 248 |
pass |
| 249 |
def f(x): |
| 250 |
a = A() |
| 251 |
a.x = 1 |
| 252 |
b = B() |
| 253 |
b.x = 2 |
| 254 |
b.y = 3 |
| 255 |
if x: |
| 256 |
c = a |
| 257 |
else: |
| 258 |
c = b |
| 259 |
return c.x |
| 260 |
t, transformer = rtype_and_transform( |
| 261 |
f, [int], RefcountingGCTransformer, check=False) |
| 262 |
fgraph = graphof(t, f) |
| 263 |
s_instance = t.annotator.bookkeeper.valueoftype(A) |
| 264 |
TYPE = t.rtyper.getrepr(s_instance).lowleveltype.TO |
| 265 |
p = transformer.dynamic_deallocation_funcptr_for_type(TYPE) |
| 266 |
t.rtyper.specialize_more_blocks() |
| 267 |
|
| 268 |
def test_recursive_structure(): |
| 269 |
F = lltype.GcForwardReference() |
| 270 |
S = lltype.GcStruct('abc', ('x', lltype.Ptr(F))) |
| 271 |
F.become(S) |
| 272 |
def f(): |
| 273 |
s1 = lltype.malloc(S) |
| 274 |
s2 = lltype.malloc(S) |
| 275 |
s1.x = s2 |
| 276 |
t, transformer = rtype_and_transform( |
| 277 |
f, [], RefcountingGCTransformer, check=False) |
| 278 |
|
| 279 |
def test_dont_decref_nongc_pointers(): |
| 280 |
S = lltype.GcStruct('S', |
| 281 |
('x', lltype.Ptr(lltype.Struct('T', ('x', lltype.Signed)))), |
| 282 |
('y', lltype.Ptr(lltype.GcStruct('Y', ('x', lltype.Signed)))) |
| 283 |
) |
| 284 |
def f(): |
| 285 |
pass |
| 286 |
graph, t = make_deallocator(S) |
| 287 |
ops = getops(graph) |
| 288 |
assert len(ops['direct_call']) == 1 |