Debian Codegraph @

rpython/memory/gc/test/test_object_pinning.py

raw symbols SCIP
1 import py
2 from rpython.rtyper.lltypesystem import lltype, llmemory, llarena
3 from rpython.memory.gc.incminimark import IncrementalMiniMarkGC, WORD
4 from rpython.memory.gc.incminimark import GCFLAG_VISITED
5 from test_direct import BaseDirectGCTest
6
7 T = lltype.GcForwardReference()
8 T.become(lltype.GcStruct('pinning_test_struct2',
9 ('someInt', lltype.Signed)))
10
11 S = lltype.GcForwardReference()
12 S.become(lltype.GcStruct('pinning_test_struct1',
13 ('someInt', lltype.Signed),
14 ('next', lltype.Ptr(T)),
15 ('data', lltype.Ptr(T))))
16
17 class PinningGCTest(BaseDirectGCTest):
18
19 def setup_method(self, meth):
20 BaseDirectGCTest.setup_method(self, meth)
21 max = getattr(meth, 'max_number_of_pinned_objects', 20)
22 self.gc.max_number_of_pinned_objects = max
23 if not hasattr(self.gc, 'minor_collection'):
24 self.gc.minor_collection = self.gc._minor_collection
25
26 def test_pin_can_move(self):
27 # even a pinned object is considered to be movable. Only the caller
28 # of pin() knows if it is currently movable or not.
29 ptr = self.malloc(T)
30 adr = llmemory.cast_ptr_to_adr(ptr)
31 assert self.gc.can_move(adr)
32 assert self.gc.pin(adr)
33 assert self.gc.can_move(adr)
34
35 def test_pin_twice(self):
36 ptr = self.malloc(T)
37 adr = llmemory.cast_ptr_to_adr(ptr)
38 assert self.gc.pin(adr)
39 assert not self.gc.pin(adr)
40
41 def test_unpin_not_pinned(self):
42 # this test checks a requirement of the unpin() interface
43 ptr = self.malloc(S)
44 py.test.raises(Exception,
45 self.gc.unpin, llmemory.cast_ptr_to_adr(ptr))
46
47 def test__is_pinned(self):
48 ptr = self.malloc(T)
49 adr = llmemory.cast_ptr_to_adr(ptr)
50 assert not self.gc._is_pinned(adr)
51 assert self.gc.pin(adr)
52 assert self.gc._is_pinned(adr)
53 self.gc.unpin(adr)
54 assert not self.gc._is_pinned(adr)
55
56 def test_prebuilt_not_pinnable(self):
57 ptr = lltype.malloc(T, immortal=True)
58 self.consider_constant(ptr)
59 assert not self.gc.pin(llmemory.cast_ptr_to_adr(ptr))
60 self.gc.collect()
61 assert not self.gc.pin(llmemory.cast_ptr_to_adr(ptr))
62
63 # XXX test with multiple mallocs, and only part of them is pinned
64
65 def test_random(self):
66 # scenario: create bunch of objects. randomly pin, unpin, add to
67 # stackroots and remove from stackroots.
68 import random
69
70 for i in xrange(10**3):
71 obj = self.malloc(T)
72 obj.someInt = 100
73 #
74 if random.random() < 0.5:
75 self.stackroots.append(obj)
76 print("+stack")
77 if random.random() < 0.5:
78 self.gc.pin(llmemory.cast_ptr_to_adr(obj))
79 print("+pin")
80 self.gc.debug_gc_step(random.randint(1, 4))
81 for o in self.stackroots[:]:
82 assert o.someInt == 100
83 o_adr = llmemory.cast_ptr_to_adr(o)
84 if random.random() < 0.1 and self.gc._is_pinned(o_adr):
85 print("-pin")
86 self.gc.unpin(o_adr)
87 if random.random() < 0.1:
88 print("-stack")
89 self.stackroots.remove(o)
90
91
92 class TestIncminimark(PinningGCTest):
93 from rpython.memory.gc.incminimark import IncrementalMiniMarkGC as GCClass
94 from rpython.memory.gc.incminimark import STATE_SCANNING, STATE_MARKING
95
96 def test_try_pin_gcref_containing_type(self):
97 # scenario: incminimark's object pinning can't pin objects that may
98 # contain GC pointers
99 obj = self.malloc(S)
100 assert not self.gc.pin(llmemory.cast_ptr_to_adr(obj))
101
102
103 def test_pin_old(self):
104 # scenario: try pinning an old object. This should be not possible and
105 # we want to make sure everything stays as it is.
106 old_ptr = self.malloc(S)
107 old_ptr.someInt = 900
108 self.stackroots.append(old_ptr)
109 assert self.stackroots[0] == old_ptr # test assumption
110 self.gc.collect()
111 old_ptr = self.stackroots[0]
112 # now we try to pin it
113 old_adr = llmemory.cast_ptr_to_adr(old_ptr)
114 assert not self.gc.is_in_nursery(old_adr)
115 assert not self.gc.pin(old_adr)
116 assert self.gc.pinned_objects_in_nursery == 0
117
118
119 def pin_pin_pinned_object_count(self, collect_func):
120 # scenario: pin two objects that are referenced from stackroots. Check
121 # if the pinned objects count is correct, even after an other collection
122 pinned1_ptr = self.malloc(T)
123 pinned1_ptr.someInt = 100
124 self.stackroots.append(pinned1_ptr)
125 #
126 pinned2_ptr = self.malloc(T)
127 pinned2_ptr.someInt = 200
128 self.stackroots.append(pinned2_ptr)
129 #
130 assert self.gc.pin(llmemory.cast_ptr_to_adr(pinned1_ptr))
131 assert self.gc.pinned_objects_in_nursery == 1
132 assert self.gc.pin(llmemory.cast_ptr_to_adr(pinned2_ptr))
133 assert self.gc.pinned_objects_in_nursery == 2
134 #
135 collect_func()
136 #
137 assert self.gc.pinned_objects_in_nursery == 2
138
139 def test_pin_pin_pinned_object_count_minor_collection(self):
140 self.pin_pin_pinned_object_count(self.gc.minor_collection)
141
142 def test_pin_pin_pinned_object_count_major_collection(self):
143 self.pin_pin_pinned_object_count(self.gc.collect)
144
145
146 def pin_unpin_pinned_object_count(self, collect_func):
147 # scenario: pin an object and check the pinned object count. Unpin it
148 # and check the count again.
149 pinned_ptr = self.malloc(T)
150 pinned_ptr.someInt = 100
151 self.stackroots.append(pinned_ptr)
152 pinned_adr = llmemory.cast_ptr_to_adr(pinned_ptr)
153 #
154 assert self.gc.pinned_objects_in_nursery == 0
155 assert self.gc.pin(pinned_adr)
156 assert self.gc.pinned_objects_in_nursery == 1
157 collect_func()
158 assert self.gc.pinned_objects_in_nursery == 1
159 self.gc.unpin(pinned_adr)
160 assert self.gc.pinned_objects_in_nursery == 0
161 collect_func()
162 assert self.gc.pinned_objects_in_nursery == 0
163
164 def test_pin_unpin_pinned_object_count_minor_collection(self):
165 self.pin_unpin_pinned_object_count(self.gc.minor_collection)
166
167 def test_pin_unpin_pinned_object_count_major_collection(self):
168 self.pin_unpin_pinned_object_count(self.gc.collect)
169
170
171 def pinned_obj_in_stackroot(self, collect_func):
172 # scenario: a pinned object that is part of the stack roots. Check if
173 # it is not moved
174 #
175 ptr = self.malloc(T)
176 ptr.someInt = 100
177 self.stackroots.append(ptr)
178 assert self.stackroots[0] == ptr # validate our assumption
179
180 adr = llmemory.cast_ptr_to_adr(ptr)
181 assert self.gc.is_in_nursery(adr) # to be sure
182 assert self.gc.pin(adr)
183 #
184 # the object shouldn't move from now on
185 collect_func()
186 #
187 # check if it is still at the same location as expected
188 adr_after_collect = llmemory.cast_ptr_to_adr(self.stackroots[0])
189 assert self.gc.is_in_nursery(adr_after_collect)
190 assert adr == adr_after_collect
191 assert self.gc._is_pinned(adr)
192 assert ptr.someInt == 100
193 assert self.gc.pinned_objects_in_nursery == 1
194
195 def test_pinned_obj_in_stackroot_minor_collection(self):
196 self.pinned_obj_in_stackroot(self.gc.minor_collection)
197
198 def test_pinned_obj_in_stackroot_full_major_collection(self):
199 self.pinned_obj_in_stackroot(self.gc.collect)
200
201 def test_pinned_obj_in_stackroots_stepwise_major_collection(self):
202 # scenario: same as for 'pinned_obj_in_stackroot' with minor change
203 # that we do stepwise major collection and check in each step for
204 # a correct state
205 #
206 ptr = self.malloc(T)
207 ptr.someInt = 100
208 self.stackroots.append(ptr)
209 assert self.stackroots[0] == ptr # validate our assumption
210
211 adr = llmemory.cast_ptr_to_adr(ptr)
212 assert self.gc.is_in_nursery(adr)
213 assert self.gc.pin(adr)
214 #
215 # the object shouldn't move from now on. Do a full round of major
216 # steps and check each time for correct state
217 #
218 # check that we start at the expected point
219 assert self.gc.gc_state == self.STATE_SCANNING
220 done = False
221 while not done:
222 self.gc.debug_gc_step()
223 # check that the pinned object didn't move
224 ptr_after_collection = self.stackroots[0]
225 adr_after_collection = llmemory.cast_ptr_to_adr(ptr_after_collection)
226 assert self.gc.is_in_nursery(adr_after_collection)
227 assert adr == adr_after_collection
228 assert self.gc._is_pinned(adr)
229 assert ptr.someInt == 100
230 assert self.gc.pinned_objects_in_nursery == 1
231 # as the object is referenced from the stackroots, the gc internal
232 # 'old_objects_pointing_to_pinned' should be empty
233 assert not self.gc.old_objects_pointing_to_pinned.non_empty()
234 #
235 # break condition
236 done = self.gc.gc_state == self.STATE_SCANNING
237
238
239 def pin_unpin_moved_stackroot(self, collect_func):
240 # scenario: test if the pinned object is moved after being unpinned.
241 # the second part of the scenario is the tested one. The first part
242 # is already tests by other tests.
243 ptr = self.malloc(T)
244 ptr.someInt = 100
245 self.stackroots.append(ptr)
246 assert self.stackroots[0] == ptr # validate our assumption
247
248 adr = llmemory.cast_ptr_to_adr(ptr)
249 assert self.gc.pin(adr)
250
251 collect_func()
252 #
253 # from here on the test really starts. previouse logic is already tested
254 #
255 self.gc.unpin(adr)
256 assert not self.gc._is_pinned(adr)
257 assert self.gc.is_in_nursery(adr)
258 #
259 # now we do another collection and the object should be moved out of
260 # the nursery.
261 collect_func()
262 new_adr = llmemory.cast_ptr_to_adr(self.stackroots[0])
263 assert not self.gc.is_in_nursery(new_adr)
264 assert self.stackroots[0].someInt == 100
265 with py.test.raises(RuntimeError) as exinfo:
266 ptr.someInt = 200
267 assert "freed" in str(exinfo.value)
268
269 def test_pin_unpin_moved_stackroot_minor_collection(self):
270 self.pin_unpin_moved_stackroot(self.gc.minor_collection)
271
272 def test_pin_unpin_moved_stackroot_major_collection(self):
273 self.pin_unpin_moved_stackroot(self.gc.collect)
274
275
276 def pin_referenced_from_old(self, collect_func):
277 # scenario: an old object points to a pinned one. Check if the pinned
278 # object is correctly kept in the nursery and not moved.
279 #
280 # create old object
281 old_ptr = self.malloc(S)
282 old_ptr.someInt = 900
283 self.stackroots.append(old_ptr)
284 assert self.stackroots[0] == old_ptr # validate our assumption
285 collect_func() # make it old: move it out of the nursery
286 old_ptr = self.stackroots[0]
287 assert not self.gc.is_in_nursery(llmemory.cast_ptr_to_adr(old_ptr))
288 #
289 # create young pinned one and let the old one reference the young one
290 pinned_ptr = self.malloc(T)
291 pinned_ptr.someInt = 100
292 self.write(old_ptr, 'next', pinned_ptr)
293 pinned_adr = llmemory.cast_ptr_to_adr(pinned_ptr)
294 assert self.gc.pin(pinned_adr)
295 assert self.gc.is_in_nursery(pinned_adr)
296 assert old_ptr.next.someInt == 100
297 assert self.gc.pinned_objects_in_nursery == 1
298 #
299 # do a collection run and make sure the pinned one didn't move
300 collect_func()
301 assert old_ptr.next.someInt == pinned_ptr.someInt == 100
302 assert llmemory.cast_ptr_to_adr(old_ptr.next) == pinned_adr
303 assert self.gc.is_in_nursery(pinned_adr)
304
305 def test_pin_referenced_from_old_minor_collection(self):
306 self.pin_referenced_from_old(self.gc.minor_collection)
307
308 def test_pin_referenced_from_old_major_collection(self):
309 self.pin_referenced_from_old(self.gc.collect)
310
311 def test_pin_referenced_from_old_stepwise_major_collection(self):
312 # scenario: same as in 'pin_referenced_from_old'. However,
313 # this time we do a major collection step by step and check
314 # between steps that the states are as expected.
315 #
316 # create old object
317 old_ptr = self.malloc(S)
318 old_ptr.someInt = 900
319 self.stackroots.append(old_ptr)
320 assert self.stackroots[0] == old_ptr # validate our assumption
321 self.gc.minor_collection() # make it old: move it out of the nursery
322 old_ptr = self.stackroots[0]
323 old_adr = llmemory.cast_ptr_to_adr(old_ptr)
324 assert not self.gc.is_in_nursery(old_adr)
325 #
326 # create young pinned one and let the old one reference the young one
327 pinned_ptr = self.malloc(T)
328 pinned_ptr.someInt = 100
329 self.write(old_ptr, 'next', pinned_ptr)
330 pinned_adr = llmemory.cast_ptr_to_adr(pinned_ptr)
331 assert self.gc.pin(pinned_adr)
332 assert self.gc.is_in_nursery(pinned_adr)
333 assert old_ptr.next.someInt == 100
334 assert self.gc.pinned_objects_in_nursery == 1
335 #
336 # stepwise major collection with validation between steps
337 # check that we start at the expected point
338 assert self.gc.gc_state == self.STATE_SCANNING
339 done = False
340 while not done:
341 self.gc.debug_gc_step()
342 #
343 # make sure pinned object didn't move
344 assert old_ptr.next.someInt == pinned_ptr.someInt == 100
345 assert llmemory.cast_ptr_to_adr(old_ptr.next) == pinned_adr
346 assert self.gc.is_in_nursery(pinned_adr)
347 assert self.gc.pinned_objects_in_nursery == 1
348 #
349 # validate that the old object is part of the internal list
350 # 'old_objects_pointing_to_pinned' as expected.
351 should_be_old_adr = self.gc.old_objects_pointing_to_pinned.pop()
352 assert should_be_old_adr == old_adr
353 self.gc.old_objects_pointing_to_pinned.append(should_be_old_adr)
354 #
355 # break condition
356 done = self.gc.gc_state == self.STATE_SCANNING
357
358
359 def pin_referenced_from_old_remove_ref(self, collect_func):
360 # scenario: an old object points to a pinned one. We remove the
361 # reference from the old one. So nothing points to the pinned object.
362 # After this the pinned object should be collected (it's dead).
363 #
364 # Create the objects and get them to our initial state (this is not
365 # tested here, should be already tested by other tests)
366 old_ptr = self.malloc(S)
367 old_ptr.someInt = 900
368 self.stackroots.append(old_ptr)
369 assert self.stackroots[0] == old_ptr # check assumption
370 collect_func() # make it old
371 old_ptr = self.stackroots[0]
372 #
373 pinned_ptr = self.malloc(T)
374 pinned_ptr.someInt = 100
375 self.write(old_ptr, 'next', pinned_ptr)
376 pinned_adr = llmemory.cast_ptr_to_adr(pinned_ptr)
377 assert self.gc.pin(pinned_adr)
378 #
379 collect_func()
380 # from here on we have our initial state for this test.
381 #
382 # first check some basic assumptions.
383 assert self.gc.is_in_nursery(pinned_adr)
384 assert self.gc._is_pinned(pinned_adr)
385 # remove the reference
386 self.write(old_ptr, 'next', lltype.nullptr(T))
387 # from now on the pinned object is dead. Do a collection and make sure
388 # old object still there and the pinned one is gone.
389 collect_func()
390 assert self.stackroots[0].someInt == 900
391 assert not self.gc.old_objects_pointing_to_pinned.non_empty()
392 with py.test.raises(RuntimeError) as exinfo:
393 pinned_ptr.someInt = 200
394 assert "freed" in str(exinfo.value)
395
396 def test_pin_referenced_from_old_remove_ref_minor_collection(self):
397 self.pin_referenced_from_old_remove_ref(self.gc.minor_collection)
398
399 def test_pin_referenced_from_old_remove_ref_major_collection(self):
400 self.pin_referenced_from_old_remove_ref(self.gc.collect)
401
402
403 def pin_referenced_from_old_remove_old(self, collect_func):
404 # scenario: an old object referenced a pinned object. After removing
405 # the stackroot reference to the old object, bot objects (old and pinned)
406 # must be collected.
407 # This test is important as we expect not reachable pinned objects to
408 # be collected. At the same time we have an internal list of objects
409 # pointing to pinned ones and we must make sure that because of it the
410 # old/pinned object survive.
411 #
412 # create the objects and get them to the initial state for this test.
413 # Everything on the way to the initial state should be covered by
414 # other tests.
415 old_ptr = self.malloc(S)
416 old_ptr.someInt = 900
417 self.stackroots.append(old_ptr)
418 collect_func()
419 old_ptr = self.stackroots[0]
420 #
421 pinned_ptr = self.malloc(T)
422 pinned_ptr.someInt = 100
423 self.write(old_ptr, 'next', pinned_ptr)
424 assert self.gc.pin(llmemory.cast_ptr_to_adr(pinned_ptr))
425 #
426 collect_func()
427 #
428 # now we have our initial state: old object referenced from stackroots.
429 # Old object referencing a young pinned one. Next step is to make some
430 # basic checks that we got the expected state.
431 assert not self.gc.is_in_nursery(llmemory.cast_ptr_to_adr(old_ptr))
432 assert self.gc.is_in_nursery(llmemory.cast_ptr_to_adr(pinned_ptr))
433 assert pinned_ptr == old_ptr.next
434 #
435 # now we remove the old object from the stackroots...
436 self.stackroots.remove(old_ptr)
437 # ... and do a major collection (otherwise the old object wouldn't be
438 # gone).
439 self.gc.collect()
440 # check that both objects are gone
441 assert not self.gc.old_objects_pointing_to_pinned.non_empty()
442 with py.test.raises(RuntimeError) as exinfo_old:
443 old_ptr.someInt = 800
444 assert "freed" in str(exinfo_old.value)
445 #
446 with py.test.raises(RuntimeError) as exinfo_pinned:
447 pinned_ptr.someInt = 200
448 assert "freed" in str(exinfo_pinned.value)
449
450 def test_pin_referenced_from_old_remove_old_minor_collection(self):
451 self.pin_referenced_from_old_remove_old(self.gc.minor_collection)
452
453 def test_pin_referenced_from_old_remove_old_major_collection(self):
454 self.pin_referenced_from_old_remove_old(self.gc.collect)
455
456
457 def pin_referenced_from_young_in_stackroots(self, collect_func):
458 # scenario: a young object is referenced from the stackroots. This
459 # young object points to a young pinned object. We check if everything
460 # behaves as expected after a collection: the young object is moved out
461 # of the nursery while the pinned one stays where it is.
462 #
463 root_ptr = self.malloc(S)
464 root_ptr.someInt = 900
465 self.stackroots.append(root_ptr)
466 assert self.stackroots[0] == root_ptr # validate assumption
467 #
468 pinned_ptr = self.malloc(T)
469 pinned_ptr.someInt = 100
470 self.write(root_ptr, 'next', pinned_ptr)
471 pinned_adr = llmemory.cast_ptr_to_adr(pinned_ptr)
472 assert self.gc.pin(pinned_adr)
473 # check both are in nursery
474 assert self.gc.is_in_nursery(llmemory.cast_ptr_to_adr(root_ptr))
475 assert self.gc.is_in_nursery(pinned_adr)
476 #
477 # no old object yet pointing to a pinned one
478 assert not self.gc.old_objects_pointing_to_pinned.non_empty()
479 #
480 # now we do a collection and check if the result is as expected
481 collect_func()
482 #
483 # check if objects are where we expect them
484 root_ptr = self.stackroots[0]
485 assert not self.gc.is_in_nursery(llmemory.cast_ptr_to_adr(root_ptr))
486 assert self.gc.is_in_nursery(pinned_adr)
487 # and as 'root_ptr' object is now old, it should be tracked specially
488 should_be_root_adr = self.gc.old_objects_pointing_to_pinned.pop()
489 assert should_be_root_adr == llmemory.cast_ptr_to_adr(root_ptr)
490 self.gc.old_objects_pointing_to_pinned.append(should_be_root_adr)
491 # check that old object still points to the pinned one as expected
492 assert root_ptr.next == pinned_ptr
493
494 def test_pin_referenced_from_young_in_stackroots_minor_collection(self):
495 self.pin_referenced_from_young_in_stackroots(self.gc.minor_collection)
496
497 def test_pin_referenced_from_young_in_stackroots_major_collection(self):
498 self.pin_referenced_from_young_in_stackroots(self.gc.collect)
499
500
501 def pin_referenced_from_prebuilt(self, collect_func):
502 # scenario: a prebuilt object points to a pinned object. Check if the
503 # pinned object doesn't move and is still accessible.
504 #
505 prebuilt_ptr = lltype.malloc(S, immortal=True)
506 prebuilt_ptr.someInt = 900
507 self.consider_constant(prebuilt_ptr)
508 prebuilt_adr = llmemory.cast_ptr_to_adr(prebuilt_ptr)
509 collect_func()
510 #
511 pinned_ptr = self.malloc(T)
512 pinned_ptr.someInt = 100
513 self.write(prebuilt_ptr, 'next', pinned_ptr)
514 pinned_adr = llmemory.cast_ptr_to_adr(pinned_ptr)
515 assert self.gc.pin(pinned_adr)
516 #
517 # check if everything is as expected
518 assert not self.gc.is_in_nursery(prebuilt_adr)
519 assert self.gc.is_in_nursery(pinned_adr)
520 assert pinned_ptr == prebuilt_ptr.next
521 assert pinned_ptr.someInt == 100
522 #
523 # do a collection and check again
524 collect_func()
525 assert self.gc.is_in_nursery(pinned_adr)
526 assert pinned_ptr == prebuilt_ptr.next
527 assert pinned_ptr.someInt == 100
528
529 def test_pin_referenced_from_prebuilt_minor_collection(self):
530 self.pin_referenced_from_prebuilt(self.gc.minor_collection)
531
532 def test_pin_referenced_from_prebuilt_major_collection(self):
533 self.pin_referenced_from_prebuilt(self.gc.collect)
534
535
536 def test_old_objects_pointing_to_pinned_not_exploading(self):
537 # scenario: two old object, each pointing twice to a pinned object.
538 # The internal 'old_objects_pointing_to_pinned' should contain
539 # always two objects.
540 # In previous implementation the list exploded (grew with every minor
541 # collection), hence this test.
542 old1_ptr = self.malloc(S)
543 old1_ptr.someInt = 900
544 self.stackroots.append(old1_ptr)
545
546 old2_ptr = self.malloc(S)
547 old2_ptr.someInt = 800
548 self.stackroots.append(old2_ptr)
549
550 pinned_ptr = self.malloc(T)
551 pinned_ptr.someInt = 100
552 assert self.gc.pin(llmemory.cast_ptr_to_adr(pinned_ptr))
553
554 self.write(old1_ptr, 'next', pinned_ptr)
555 self.write(old1_ptr, 'data', pinned_ptr)
556 self.write(old2_ptr, 'next', pinned_ptr)
557 self.write(old2_ptr, 'data', pinned_ptr)
558
559 self.gc.collect()
560 old1_ptr = self.stackroots[0]
561 old2_ptr = self.stackroots[1]
562 assert not self.gc.is_in_nursery(llmemory.cast_ptr_to_adr(old1_ptr))
563 assert not self.gc.is_in_nursery(llmemory.cast_ptr_to_adr(old2_ptr))
564
565 # do multiple rounds to make sure
566 for _ in range(10):
567 assert self.gc.old_objects_pointing_to_pinned.length() == 2
568 self.gc.debug_gc_step()
569
570
571 def pin_shadow_1(self, collect_func):
572 ptr = self.malloc(T)
573 adr = llmemory.cast_ptr_to_adr(ptr)
574 self.stackroots.append(ptr)
575 ptr.someInt = 100
576 assert self.gc.pin(adr)
577 self.gc.id(ptr) # allocate shadow
578 collect_func()
579 assert self.gc.is_in_nursery(adr)
580 assert ptr.someInt == 100
581 self.gc.unpin(adr)
582 collect_func() # move to shadow
583 adr = llmemory.cast_ptr_to_adr(self.stackroots[0])
584 assert not self.gc.is_in_nursery(adr)
585
586 def test_pin_shadow_1_minor_collection(self):
587 self.pin_shadow_1(self.gc.minor_collection)
588
589 def test_pin_shadow_1_major_collection(self):
590 self.pin_shadow_1(self.gc.collect)
591
592
593 def test_malloc_different_types(self):
594 # scenario: malloc two objects of different type and pin them. Do a
595 # minor and major collection in between. This test showed a bug that was
596 # present in a previous implementation of pinning.
597 obj1 = self.malloc(T)
598 self.stackroots.append(obj1)
599 assert self.gc.pin(llmemory.cast_ptr_to_adr(obj1))
600 #
601 self.gc.collect()
602 #
603 obj2 = self.malloc(T)
604 self.stackroots.append(obj2)
605 assert self.gc.pin(llmemory.cast_ptr_to_adr(obj2))
606
607
608 def test_objects_to_trace_bug(self):
609 # scenario: In a previous implementation there was a bug because of a
610 # dead pointer inside 'objects_to_trace'. This was caused by the first
611 # major collection step that added the pointer to the list and right
612 # after the collection step the object is unpinned and freed by the minor
613 # collection, leaving a dead pointer in the list.
614 pinned_ptr = self.malloc(T)
615 pinned_ptr.someInt = 101
616 self.stackroots.append(pinned_ptr)
617 pinned_adr = llmemory.cast_ptr_to_adr(pinned_ptr)
618 assert self.gc.pin(pinned_adr)
619 self.gc.debug_gc_step()
620 self.gc.unpin(pinned_adr)
621 self.gc.debug_gc_step()
622
623
624 def pin_shadow_2(self, collect_func):
625 ptr = self.malloc(T)
626 adr = llmemory.cast_ptr_to_adr(ptr)
627 self.stackroots.append(ptr)
628 ptr.someInt = 100
629 assert self.gc.pin(adr)
630 self.gc.identityhash(ptr) # allocate shadow
631 collect_func()
632 assert self.gc.is_in_nursery(adr)
633 assert ptr.someInt == 100
634 self.gc.unpin(adr)
635 collect_func() # move to shadow
636 adr = llmemory.cast_ptr_to_adr(self.stackroots[0])
637 assert not self.gc.is_in_nursery(adr)
638
639 def test_pin_shadow_2_minor_collection(self):
640 self.pin_shadow_2(self.gc.minor_collection)
641
642 def test_pin_shadow_2_major_collection(self):
643 self.pin_shadow_2(self.gc.collect)
644
645
646 def test_pin_nursery_top_scenario1(self):
647 ptr1 = self.malloc(T)
648 adr1 = llmemory.cast_ptr_to_adr(ptr1)
649 ptr1.someInt = 101
650 self.stackroots.append(ptr1)
651 assert self.gc.pin(adr1)
652
653 ptr2 = self.malloc(T)
654 adr2 = llmemory.cast_ptr_to_adr(ptr2)
655 ptr2.someInt = 102
656 self.stackroots.append(ptr2)
657 assert self.gc.pin(adr2)
658
659 ptr3 = self.malloc(T)
660 adr3 = llmemory.cast_ptr_to_adr(ptr3)
661 ptr3.someInt = 103
662 self.stackroots.append(ptr3)
663 assert self.gc.pin(adr3)
664
665 # scenario: no minor collection happened, only three mallocs
666 # and pins
667 #
668 # +- nursery
669 # |
670 # v
671 # +--------+--------+--------+---------------------...---+
672 # | pinned | pinned | pinned | empty |
673 # +--------+--------+--------+---------------------...---+
674 # ^ ^
675 # | |
676 # nursery_free -+ |
677 # nursery_top -+
678 #
679 assert adr3 < self.gc.nursery_free
680 assert self.gc.nursery_free < self.gc.nursery_top
681
682
683 def test_pin_nursery_top_scenario2(self):
684 ptr1 = self.malloc(T)
685 adr1 = llmemory.cast_ptr_to_adr(ptr1)
686 ptr1.someInt = 101
687 self.stackroots.append(ptr1)
688 assert self.gc.pin(adr1)
689
690 ptr2 = self.malloc(T)
691 adr2 = llmemory.cast_ptr_to_adr(ptr2)
692 ptr2.someInt = 102
693 self.stackroots.append(ptr2)
694 assert self.gc.pin(adr2)
695
696 ptr3 = self.malloc(T)
697 adr3 = llmemory.cast_ptr_to_adr(ptr3)
698 ptr3.someInt = 103
699 self.stackroots.append(ptr3)
700 assert self.gc.pin(adr3)
701
702 # scenario: after first GC minor collection
703 #
704 # +- nursery
705 # |
706 # v
707 # +--------+--------+--------+---------------------...---+
708 # | pinned | pinned | pinned | empty |
709 # +--------+--------+--------+---------------------...---+
710 # ^
711 # |
712 # +- nursery_free
713 # +- nursery_top
714 #
715 self.gc.collect()
716
717 assert self.gc.nursery_free == self.gc.nursery_top
718 assert self.gc.nursery_top == self.gc.nursery
719 assert self.gc.nursery_top < adr3
720
721
722 def test_pin_nursery_top_scenario3(self):
723 ptr1 = self.malloc(T)
724 adr1 = llmemory.cast_ptr_to_adr(ptr1)
725 ptr1.someInt = 101
726 self.stackroots.append(ptr1)
727 assert self.gc.pin(adr1)
728
729 ptr2 = self.malloc(T)
730 adr2 = llmemory.cast_ptr_to_adr(ptr2)
731 ptr2.someInt = 102
732 self.stackroots.append(ptr2)
733 assert self.gc.pin(adr2)
734
735 ptr3 = self.malloc(T)
736 adr3 = llmemory.cast_ptr_to_adr(ptr3)
737 ptr3.someInt = 103
738 self.stackroots.append(ptr3)
739 assert self.gc.pin(adr3)
740
741 # scenario: after unpinning first object and a minor
742 # collection
743 #
744 # +- nursery
745 # |
746 # v
747 # +--------+--------+--------+---------------------...---+
748 # | empty | pinned | pinned | empty |
749 # +--------+--------+--------+---------------------...---+
750 # ^ ^
751 # | |
752 # | +- nursery_top
753 # +- nursery_free
754 #
755 self.gc.unpin(adr1)
756 self.gc.collect()
757
758 assert self.gc.nursery_free == self.gc.nursery
759 assert self.gc.nursery_top > self.gc.nursery_free
760 assert self.gc.nursery_top < adr2
761
762
763 def test_pin_nursery_top_scenario4(self):
764 ptr1 = self.malloc(T)
765 adr1 = llmemory.cast_ptr_to_adr(ptr1)
766 ptr1.someInt = 101
767 self.stackroots.append(ptr1)
768 assert self.gc.pin(adr1)
769
770 ptr2 = self.malloc(T)
771 adr2 = llmemory.cast_ptr_to_adr(ptr2)
772 ptr2.someInt = 102
773 self.stackroots.append(ptr2)
774 assert self.gc.pin(adr2)
775
776 ptr3 = self.malloc(T)
777 adr3 = llmemory.cast_ptr_to_adr(ptr3)
778 ptr3.someInt = 103
779 self.stackroots.append(ptr3)
780 assert self.gc.pin(adr3)
781
782 # scenario: after unpinning first & second object and a minor
783 # collection
784 #
785 # +- nursery
786 # |
787 # v
788 # +-----------------+--------+---------------------...---+
789 # | empty | pinned | empty |
790 # +-----------------+--------+---------------------...---+
791 # ^ ^
792 # | |
793 # | +- nursery_top
794 # +- nursery_free
795 #
796 self.gc.unpin(adr1)
797 self.gc.unpin(adr2)
798 self.gc.collect()
799
800 assert self.gc.nursery_free == self.gc.nursery
801 assert self.gc.nursery_free < self.gc.nursery_top
802 assert self.gc.nursery_top < adr3
803
804
805 def test_pin_nursery_top_scenario5(self):
806 ptr1 = self.malloc(T)
807 adr1 = llmemory.cast_ptr_to_adr(ptr1)
808 ptr1.someInt = 101
809 self.stackroots.append(ptr1)
810 assert self.gc.pin(adr1)
811
812 ptr2 = self.malloc(T)
813 adr2 = llmemory.cast_ptr_to_adr(ptr2)
814 ptr2.someInt = 102
815 self.stackroots.append(ptr2)
816 assert self.gc.pin(adr2)
817
818 ptr3 = self.malloc(T)
819 adr3 = llmemory.cast_ptr_to_adr(ptr3)
820 ptr3.someInt = 103
821 self.stackroots.append(ptr3)
822 assert self.gc.pin(adr3)
823
824 # scenario: no minor collection happened, only three mallocs
825 # and pins
826 #
827 # +- nursery
828 # |
829 # v
830 # +--------+--------+--------+---------------------...---+
831 # | pinned | pinned | pinned | empty |
832 # +--------+--------+--------+---------------------...---+
833 # ^ ^
834 # | |
835 # nursery_free -+ |
836 # nursery_top -+
837 #
838 assert adr3 < self.gc.nursery_free
839 assert self.gc.nursery_free < self.gc.nursery_top
840
841 # scenario: unpin everything and minor collection
842 #
843 # +- nursery
844 # |
845 # v
846 # +----------------------------------+-------------...---+
847 # | reset arena | empty (not reset) |
848 # +----------------------------------+-------------...---+
849 # ^ ^
850 # | |
851 # +- nursery_free |
852 # nursery_top -+
853 #
854 self.gc.unpin(adr1)
855 self.gc.unpin(adr2)
856 self.gc.unpin(adr3)
857 self.gc.collect()
858
859 assert self.gc.nursery_free == self.gc.nursery
860 assert self.gc.nursery_top > self.gc.nursery_free
861
862
863 def fill_nursery_with_pinned_objects(self):
864 typeid = self.get_type_id(T)
865 size = self.gc.fixed_size(typeid) + self.gc.gcheaderbuilder.size_gc_header
866 raw_size = llmemory.raw_malloc_usage(size)
867 object_mallocs = self.gc.nursery_size // raw_size
868 for instance_nr in xrange(object_mallocs):
869 ptr = self.malloc(T)
870 adr = llmemory.cast_ptr_to_adr(ptr)
871 ptr.someInt = 100 + instance_nr
872 self.stackroots.append(ptr)
873 self.gc.pin(adr)
874
875 def test_full_pinned_nursery_pin_fail(self):
876 self.fill_nursery_with_pinned_objects()
877 # nursery should be full now, at least no space for another `T`.
878 # Next malloc should fail.
879 py.test.raises(Exception, self.malloc, T)
880
881 def test_full_pinned_nursery_arena_reset(self):
882 # there were some bugs regarding the 'arena_reset()' calls at
883 # the end of the minor collection. This test brought them to light.
884 self.fill_nursery_with_pinned_objects()
885 self.gc.collect()
886
887 def test_pinning_limit(self):
888 assert self.gc.max_number_of_pinned_objects == 5
889 for instance_nr in xrange(self.gc.max_number_of_pinned_objects):
890 ptr = self.malloc(T)
891 adr = llmemory.cast_ptr_to_adr(ptr)
892 ptr.someInt = 100 + instance_nr
893 self.stackroots.append(ptr)
894 assert self.gc.pin(adr)
895 #
896 # now we reached the maximum amount of pinned objects
897 ptr = self.malloc(T)
898 adr = llmemory.cast_ptr_to_adr(ptr)
899 self.stackroots.append(ptr)
900 assert not self.gc.pin(adr)
901 test_pinning_limit.max_number_of_pinned_objects = 5
902
903 def test_full_pinned_nursery_pin_fail(self):
904 typeid = self.get_type_id(T)
905 size = self.gc.fixed_size(typeid) + self.gc.gcheaderbuilder.size_gc_header
906 raw_size = llmemory.raw_malloc_usage(size)
907 object_mallocs = self.gc.nursery_size // raw_size
908 # just to be sure we do not run into the limit as we test not the limiter
909 # but rather the case of a nursery full with pinned objects.
910 assert object_mallocs < self.gc.max_number_of_pinned_objects
911 for instance_nr in xrange(object_mallocs):
912 ptr = self.malloc(T)
913 adr = llmemory.cast_ptr_to_adr(ptr)
914 ptr.someInt = 100 + instance_nr
915 self.stackroots.append(ptr)
916 self.gc.pin(adr)
917 #
918 # nursery should be full now, at least no space for another `T`.
919 # Next malloc should fail.
920 py.test.raises(Exception, self.malloc, T)
921 test_full_pinned_nursery_pin_fail.max_number_of_pinned_objects = 50
922
923
924 def test_pin_bug1(self):
925 #
926 # * the nursery contains a pinned object 'ptr1'
927 #
928 # * outside the nursery is another object 'ptr2' pointing to 'ptr1'
929 #
930 # * during one incremental tracing step, we see 'ptr2' but don't
931 # trace 'ptr1' right now: it is left behind on the trace-me-later
932 # list
933 #
934 # * then we run the program, unpin 'ptr1', and remove it from 'ptr2'
935 #
936 # * at the next minor collection, we free 'ptr1' because we don't
937 # find anything pointing to it (it is removed from 'ptr2'),
938 # but 'ptr1' is still in the trace-me-later list
939 #
940 # * the trace-me-later list is deep enough that 'ptr1' is not
941 # seen right now! it is only seen at some later minor collection
942 #
943 # * at that later point, crash, because 'ptr1' in the nursery was
944 # overwritten
945 #
946 ptr2 = self.malloc(S)
947 ptr2.someInt = 102
948 self.stackroots.append(ptr2)
949
950 self.gc.collect()
951 ptr2 = self.stackroots[-1] # now outside the nursery
952 adr2 = llmemory.cast_ptr_to_adr(ptr2)
953
954 ptr1 = self.malloc(T)
955 adr1 = llmemory.cast_ptr_to_adr(ptr1)
956 ptr1.someInt = 101
957 self.write(ptr2, 'data', ptr1)
958 res = self.gc.pin(adr1)
959 assert res
960
961 self.gc.minor_collection()
962 assert self.gc.gc_state == self.STATE_SCANNING
963 self.gc.major_collection_step()
964 assert self.gc.objects_to_trace.tolist() == [adr2]
965 assert self.gc.more_objects_to_trace.tolist() == []
966
967 self.gc.TEST_VISIT_SINGLE_STEP = True
968
969 self.gc.minor_collection()
970 assert self.gc.gc_state == self.STATE_MARKING
971 self.gc.major_collection_step()
972 assert self.gc.objects_to_trace.tolist() == []
973 assert self.gc.more_objects_to_trace.tolist() == [adr2]
974
975 self.write(ptr2, 'data', lltype.nullptr(T))
976 self.gc.unpin(adr1)
977
978 assert ptr1.someInt == 101
979 self.gc.minor_collection() # should free 'ptr1'
980 py.test.raises(RuntimeError, "ptr1.someInt")
981 assert self.gc.gc_state == self.STATE_MARKING
982 self.gc.major_collection_step() # should not crash reading 'ptr1'!
983
984 del self.gc.TEST_VISIT_SINGLE_STEP
985
986
987 def test_pin_bug2(self):
988 #
989 # * we have an old object A that points to a pinned object B
990 #
991 # * we unpin B
992 #
993 # * the next minor_collection() is done in STATE_MARKING==1
994 # when the object A is already black
995 #
996 # * _minor_collection() => _visit_old_objects_pointing_to_pinned()
997 # which will move the now-unpinned B out of the nursery, to B'
998 #
999 # At that point we need to take care of colors, otherwise we
1000 # get a black object (A) pointing to a white object (B'),
1001 # which must never occur.
1002 #
1003 ptrA = self.malloc(T)
1004 ptrA.someInt = 42
1005 adrA = llmemory.cast_ptr_to_adr(ptrA)
1006 res = self.gc.pin(adrA)
1007 assert res
1008
1009 ptrC = self.malloc(S)
1010 self.stackroots.append(ptrC)
1011
1012 ptrB = self.malloc(S)
1013 ptrB.data = ptrA
1014 self.stackroots.append(ptrB)
1015
1016 self.gc.collect()
1017 ptrB = self.stackroots[-1] # now old and outside the nursery
1018 ptrC = self.stackroots[-2] # another random old object, traced later
1019 adrB = llmemory.cast_ptr_to_adr(ptrB)
1020
1021 self.gc.minor_collection()
1022 assert self.gc.gc_state == self.STATE_SCANNING
1023 self.gc.major_collection_step()
1024 assert self.gc.gc_state == self.STATE_MARKING
1025 assert not (self.gc.header(adrB).tid & GCFLAG_VISITED) # not black yet
1026
1027 self.gc.TEST_VISIT_SINGLE_STEP = True
1028 self.gc.major_collection_step()
1029 assert self.gc.gc_state == self.STATE_MARKING
1030 assert self.gc.header(adrB).tid & GCFLAG_VISITED # now black
1031 # but ptrC is not traced yet, which is why we're still in STATE_MARKING
1032 assert self.gc.old_objects_pointing_to_pinned.tolist() == [adrB]
1033
1034 self.gc.unpin(adrA)
1035
1036 self.gc.DEBUG = 2
1037 self.gc.minor_collection()