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