Debian Codegraph @

rpython/memory/gc/test/test_rawrefcount.py

raw symbols SCIP
1 import py
2 from rpython.rtyper.lltypesystem import lltype, llmemory
3 from rpython.memory.gc.incminimark import IncrementalMiniMarkGC
4 from rpython.memory.gc.test.test_direct import BaseDirectGCTest
5 from rpython.rlib.rawrefcount import REFCNT_FROM_PYPY
6 from rpython.rlib.rawrefcount import REFCNT_FROM_PYPY_LIGHT
7
8 PYOBJ_HDR = IncrementalMiniMarkGC.PYOBJ_HDR
9 PYOBJ_HDR_PTR = IncrementalMiniMarkGC.PYOBJ_HDR_PTR
10
11 S = lltype.GcForwardReference()
12 S.become(lltype.GcStruct('S',
13 ('x', lltype.Signed),
14 ('prev', lltype.Ptr(S)),
15 ('next', lltype.Ptr(S))))
16
17
18 class TestRawRefCount(BaseDirectGCTest):
19 GCClass = IncrementalMiniMarkGC
20
21 def _collect(self, major, expected_trigger=0):
22 if major:
23 self.gc.collect()
24 else:
25 self.gc._minor_collection()
26 count1 = len(self.trigger)
27 self.gc.rrc_invoke_callback()
28 count2 = len(self.trigger)
29 assert count2 - count1 == expected_trigger
30
31 def _rawrefcount_pair(self, intval, is_light=False, is_pyobj=False,
32 create_old=False, create_immortal=False,
33 force_external=False):
34 if is_light:
35 rc = REFCNT_FROM_PYPY_LIGHT
36 else:
37 rc = REFCNT_FROM_PYPY
38 self.trigger = []
39 self.gc.rawrefcount_init(lambda: self.trigger.append(1))
40 #
41 if create_immortal:
42 p1 = lltype.malloc(S, immortal=True)
43 else:
44 saved = self.gc.nonlarge_max
45 try:
46 if force_external:
47 self.gc.nonlarge_max = 1
48 p1 = self.malloc(S)
49 finally:
50 self.gc.nonlarge_max = saved
51 p1.x = intval
52 if create_immortal:
53 self.consider_constant(p1)
54 elif create_old:
55 self.stackroots.append(p1)
56 self._collect(major=False)
57 p1 = self.stackroots.pop()
58 p1ref = lltype.cast_opaque_ptr(llmemory.GCREF, p1)
59 r1 = lltype.malloc(PYOBJ_HDR, flavor='raw', immortal=create_immortal)
60 r1.ob_refcnt = rc
61 r1.ob_pypy_link = 0
62 r1addr = llmemory.cast_ptr_to_adr(r1)
63 if is_pyobj:
64 assert not is_light
65 self.gc.rawrefcount_create_link_pyobj(p1ref, r1addr)
66 else:
67 self.gc.rawrefcount_create_link_pypy(p1ref, r1addr)
68 assert r1.ob_refcnt == rc
69 assert r1.ob_pypy_link != 0
70
71 def check_alive(extra_refcount):
72 assert r1.ob_refcnt == rc + extra_refcount
73 assert r1.ob_pypy_link != 0
74 p1ref = self.gc.rawrefcount_to_obj(r1addr)
75 p1 = lltype.cast_opaque_ptr(lltype.Ptr(S), p1ref)
76 assert p1.x == intval
77 if not is_pyobj:
78 assert self.gc.rawrefcount_from_obj(p1ref) == r1addr
79 else:
80 assert self.gc.rawrefcount_from_obj(p1ref) == llmemory.NULL
81 return p1
82 return p1, p1ref, r1, r1addr, check_alive
83
84 def test_rawrefcount_objects_basic(self, old=False):
85 p1, p1ref, r1, r1addr, check_alive = (
86 self._rawrefcount_pair(42, is_light=True, create_old=old))
87 p2 = self.malloc(S)
88 p2.x = 84
89 p2ref = lltype.cast_opaque_ptr(llmemory.GCREF, p2)
90 r2 = lltype.malloc(PYOBJ_HDR, flavor='raw')
91 r2.ob_refcnt = 1
92 r2.ob_pypy_link = 0
93 r2addr = llmemory.cast_ptr_to_adr(r2)
94 # p2 and r2 are not linked
95 assert r1.ob_pypy_link != 0
96 assert r2.ob_pypy_link == 0
97 assert self.gc.rawrefcount_from_obj(p1ref) == r1addr
98 assert self.gc.rawrefcount_from_obj(p2ref) == llmemory.NULL
99 assert self.gc.rawrefcount_to_obj(r1addr) == p1ref
100 assert self.gc.rawrefcount_to_obj(r2addr) == lltype.nullptr(
101 llmemory.GCREF.TO)
102 lltype.free(r1, flavor='raw')
103 lltype.free(r2, flavor='raw')
104
105 def test_rawrefcount_objects_collection_survives_from_raw(self, old=False):
106 p1, p1ref, r1, r1addr, check_alive = (
107 self._rawrefcount_pair(42, is_light=True, create_old=old))
108 check_alive(0)
109 r1.ob_refcnt += 1
110 self._collect(major=False)
111 check_alive(+1)
112 self._collect(major=True)
113 check_alive(+1)
114 r1.ob_refcnt -= 1
115 self._collect(major=False)
116 p1 = check_alive(0)
117 self._collect(major=True)
118 py.test.raises(RuntimeError, "r1.ob_refcnt") # dead
119 py.test.raises(RuntimeError, "p1.x") # dead
120 self.gc.check_no_more_rawrefcount_state()
121 assert self.trigger == []
122 assert self.gc.rawrefcount_next_dead() == llmemory.NULL
123
124 def test_rawrefcount_dies_quickly(self, old=False):
125 p1, p1ref, r1, r1addr, check_alive = (
126 self._rawrefcount_pair(42, is_light=True, create_old=old))
127 check_alive(0)
128 self._collect(major=False)
129 if old:
130 check_alive(0)
131 self._collect(major=True)
132 py.test.raises(RuntimeError, "r1.ob_refcnt") # dead
133 py.test.raises(RuntimeError, "p1.x") # dead
134 self.gc.check_no_more_rawrefcount_state()
135
136 def test_rawrefcount_objects_collection_survives_from_obj(self, old=False):
137 p1, p1ref, r1, r1addr, check_alive = (
138 self._rawrefcount_pair(42, is_light=True, create_old=old))
139 check_alive(0)
140 self.stackroots.append(p1)
141 self._collect(major=False)
142 check_alive(0)
143 self._collect(major=True)
144 check_alive(0)
145 p1 = self.stackroots.pop()
146 self._collect(major=False)
147 check_alive(0)
148 assert p1.x == 42
149 self._collect(major=True)
150 py.test.raises(RuntimeError, "r1.ob_refcnt") # dead
151 py.test.raises(RuntimeError, "p1.x") # dead
152 self.gc.check_no_more_rawrefcount_state()
153
154 def test_rawrefcount_objects_basic_old(self):
155 self.test_rawrefcount_objects_basic(old=True)
156 def test_rawrefcount_objects_collection_survives_from_raw_old(self):
157 self.test_rawrefcount_objects_collection_survives_from_raw(old=True)
158 def test_rawrefcount_dies_quickly_old(self):
159 self.test_rawrefcount_dies_quickly(old=True)
160 def test_rawrefcount_objects_collection_survives_from_obj_old(self):
161 self.test_rawrefcount_objects_collection_survives_from_obj(old=True)
162
163 def test_pypy_nonlight_survives_from_raw(self, old=False):
164 p1, p1ref, r1, r1addr, check_alive = (
165 self._rawrefcount_pair(42, is_light=False, create_old=old))
166 check_alive(0)
167 r1.ob_refcnt += 1
168 self._collect(major=False)
169 check_alive(+1)
170 self._collect(major=True)
171 check_alive(+1)
172 r1.ob_refcnt -= 1
173 self._collect(major=False)
174 p1 = check_alive(0)
175 self._collect(major=True, expected_trigger=1)
176 py.test.raises(RuntimeError, "p1.x") # dead
177 assert r1.ob_refcnt == 1 # in the pending list
178 assert r1.ob_pypy_link == 0
179 assert self.gc.rawrefcount_next_dead() == r1addr
180 assert self.gc.rawrefcount_next_dead() == llmemory.NULL
181 assert self.gc.rawrefcount_next_dead() == llmemory.NULL
182 self.gc.check_no_more_rawrefcount_state()
183 lltype.free(r1, flavor='raw')
184
185 def test_pypy_nonlight_survives_from_obj(self, old=False):
186 p1, p1ref, r1, r1addr, check_alive = (
187 self._rawrefcount_pair(42, is_light=False, create_old=old))
188 check_alive(0)
189 self.stackroots.append(p1)
190 self._collect(major=False)
191 check_alive(0)
192 self._collect(major=True)
193 check_alive(0)
194 p1 = self.stackroots.pop()
195 self._collect(major=False)
196 check_alive(0)
197 assert p1.x == 42
198 self._collect(major=True, expected_trigger=1)
199 py.test.raises(RuntimeError, "p1.x") # dead
200 assert r1.ob_refcnt == 1
201 assert r1.ob_pypy_link == 0
202 assert self.gc.rawrefcount_next_dead() == r1addr
203 self.gc.check_no_more_rawrefcount_state()
204 lltype.free(r1, flavor='raw')
205
206 def test_pypy_nonlight_dies_quickly(self, old=False):
207 p1, p1ref, r1, r1addr, check_alive = (
208 self._rawrefcount_pair(42, is_light=False, create_old=old))
209 check_alive(0)
210 if old:
211 self._collect(major=False)
212 check_alive(0)
213 self._collect(major=True, expected_trigger=1)
214 else:
215 self._collect(major=False, expected_trigger=1)
216 py.test.raises(RuntimeError, "p1.x") # dead
217 assert r1.ob_refcnt == 1
218 assert r1.ob_pypy_link == 0
219 assert self.gc.rawrefcount_next_dead() == r1addr
220 self.gc.check_no_more_rawrefcount_state()
221 lltype.free(r1, flavor='raw')
222
223 def test_pypy_nonlight_survives_from_raw_old(self):
224 self.test_pypy_nonlight_survives_from_raw(old=True)
225 def test_pypy_nonlight_survives_from_obj_old(self):
226 self.test_pypy_nonlight_survives_from_obj(old=True)
227 def test_pypy_nonlight_dies_quickly_old(self):
228 self.test_pypy_nonlight_dies_quickly(old=True)
229
230 @py.test.mark.parametrize('external', [False, True])
231 def test_pyobject_pypy_link_dies_on_minor_collection(self, external):
232 p1, p1ref, r1, r1addr, check_alive = (
233 self._rawrefcount_pair(42, is_pyobj=True, force_external=external))
234 check_alive(0)
235 r1.ob_refcnt += 1 # the pyobject is kept alive
236 self._collect(major=False)
237 assert r1.ob_refcnt == 1 # refcnt dropped to 1
238 assert r1.ob_pypy_link == 0 # detached
239 self.gc.check_no_more_rawrefcount_state()
240 lltype.free(r1, flavor='raw')
241
242 @py.test.mark.parametrize('old,external', [
243 (False, False), (True, False), (False, True)])
244 def test_pyobject_dies(self, old, external):
245 p1, p1ref, r1, r1addr, check_alive = (
246 self._rawrefcount_pair(42, is_pyobj=True, create_old=old,
247 force_external=external))
248 check_alive(0)
249 if old:
250 self._collect(major=False)
251 check_alive(0)
252 self._collect(major=True, expected_trigger=1)
253 else:
254 self._collect(major=False, expected_trigger=1)
255 assert r1.ob_refcnt == 1 # refcnt 1, in the pending list
256 assert r1.ob_pypy_link == 0 # detached
257 assert self.gc.rawrefcount_next_dead() == r1addr
258 self.gc.check_no_more_rawrefcount_state()
259 lltype.free(r1, flavor='raw')
260
261 @py.test.mark.parametrize('old,external', [
262 (False, False), (True, False), (False, True)])
263 def test_pyobject_survives_from_obj(self, old, external):
264 p1, p1ref, r1, r1addr, check_alive = (
265 self._rawrefcount_pair(42, is_pyobj=True, create_old=old,
266 force_external=external))
267 check_alive(0)
268 self.stackroots.append(p1)
269 self._collect(major=False)
270 check_alive(0)
271 self._collect(major=True)
272 check_alive(0)
273 p1 = self.stackroots.pop()
274 self._collect(major=False)
275 check_alive(0)
276 assert p1.x == 42
277 assert self.trigger == []
278 self._collect(major=True, expected_trigger=1)
279 py.test.raises(RuntimeError, "p1.x") # dead
280 assert r1.ob_refcnt == 1
281 assert r1.ob_pypy_link == 0
282 assert self.gc.rawrefcount_next_dead() == r1addr
283 self.gc.check_no_more_rawrefcount_state()
284 lltype.free(r1, flavor='raw')
285
286 def test_pyobject_attached_to_prebuilt_obj(self):
287 p1, p1ref, r1, r1addr, check_alive = (
288 self._rawrefcount_pair(42, create_immortal=True))
289 check_alive(0)
290 self._collect(major=True)
291 check_alive(0)
292
293 def test_rawrefcount_next_dead_robust_against_non_init(self):
294 # does not crash despite not calling init
295 assert not self.gc.rawrefcount_next_dead()