|
| 1 | +# It's most useful to run these tests with ThreadSanitizer enabled. |
| 2 | +import sys |
| 3 | +import functools |
| 4 | +import threading |
| 5 | +import time |
| 6 | +import unittest |
| 7 | + |
| 8 | +from test.support import threading_helper |
| 9 | + |
| 10 | + |
| 11 | +class TestBase(unittest.TestCase): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +def do_race(func1, func2): |
| 16 | + """Run func1() and func2() repeatedly in separate threads.""" |
| 17 | + n = 1000 |
| 18 | + |
| 19 | + barrier = threading.Barrier(2) |
| 20 | + |
| 21 | + def repeat(func): |
| 22 | + barrier.wait() |
| 23 | + for _i in range(n): |
| 24 | + func() |
| 25 | + |
| 26 | + threads = [ |
| 27 | + threading.Thread(target=functools.partial(repeat, func1)), |
| 28 | + threading.Thread(target=functools.partial(repeat, func2)), |
| 29 | + ] |
| 30 | + for thread in threads: |
| 31 | + thread.start() |
| 32 | + for thread in threads: |
| 33 | + thread.join() |
| 34 | + |
| 35 | + |
| 36 | +@threading_helper.requires_working_threading() |
| 37 | +class TestRaces(TestBase): |
| 38 | + def test_racing_cell_set(self): |
| 39 | + """Test cell object gettr/settr properties.""" |
| 40 | + |
| 41 | + def nested_func(): |
| 42 | + x = 0 |
| 43 | + |
| 44 | + def inner(): |
| 45 | + nonlocal x |
| 46 | + x += 1 |
| 47 | + |
| 48 | + # This doesn't race because LOAD_DEREF and STORE_DEREF on the |
| 49 | + # cell object use critical sections. |
| 50 | + do_race(nested_func, nested_func) |
| 51 | + |
| 52 | + def nested_func2(): |
| 53 | + x = 0 |
| 54 | + |
| 55 | + def inner(): |
| 56 | + y = x |
| 57 | + frame = sys._getframe(1) |
| 58 | + frame.f_locals["x"] = 2 |
| 59 | + |
| 60 | + return inner |
| 61 | + |
| 62 | + def mutate_func2(): |
| 63 | + inner = nested_func2() |
| 64 | + cell = inner.__closure__[0] |
| 65 | + old_value = cell.cell_contents |
| 66 | + cell.cell_contents = 1000 |
| 67 | + time.sleep(0) |
| 68 | + cell.cell_contents = old_value |
| 69 | + time.sleep(0) |
| 70 | + |
| 71 | + # This revealed a race with cell_set_contents() since it was missing |
| 72 | + # the critical section. |
| 73 | + do_race(nested_func2, mutate_func2) |
| 74 | + |
| 75 | + def test_racing_cell_cmp_repr(self): |
| 76 | + """Test cell object compare and repr methods.""" |
| 77 | + |
| 78 | + def nested_func(): |
| 79 | + x = 0 |
| 80 | + y = 0 |
| 81 | + |
| 82 | + def inner(): |
| 83 | + return x + y |
| 84 | + |
| 85 | + return inner.__closure__ |
| 86 | + |
| 87 | + cell_a, cell_b = nested_func() |
| 88 | + |
| 89 | + def mutate(): |
| 90 | + cell_a.cell_contents += 1 |
| 91 | + |
| 92 | + def access(): |
| 93 | + cell_a == cell_b |
| 94 | + s = repr(cell_a) |
| 95 | + |
| 96 | + # cell_richcompare() and cell_repr used to have data races |
| 97 | + do_race(mutate, access) |
| 98 | + |
| 99 | + def test_racing_load_super_attr(self): |
| 100 | + """Test (un)specialization of LOAD_SUPER_ATTR opcode.""" |
| 101 | + |
| 102 | + class C: |
| 103 | + def __init__(self): |
| 104 | + try: |
| 105 | + super().__init__ |
| 106 | + super().__init__() |
| 107 | + except RuntimeError: |
| 108 | + pass # happens if __class__ is replaced with non-type |
| 109 | + |
| 110 | + def access(): |
| 111 | + C() |
| 112 | + |
| 113 | + def mutate(): |
| 114 | + # Swap out the super() global with a different one |
| 115 | + real_super = super |
| 116 | + globals()["super"] = lambda s=1: s |
| 117 | + time.sleep(0) |
| 118 | + globals()["super"] = real_super |
| 119 | + time.sleep(0) |
| 120 | + # Swap out the __class__ closure value with a non-type |
| 121 | + cell = C.__init__.__closure__[0] |
| 122 | + real_class = cell.cell_contents |
| 123 | + cell.cell_contents = 99 |
| 124 | + time.sleep(0) |
| 125 | + cell.cell_contents = real_class |
| 126 | + |
| 127 | + # The initial PR adding specialized opcodes for LOAD_SUPER_ATTR |
| 128 | + # had some races (one with the super() global changing and one |
| 129 | + # with the cell binding being changed). |
| 130 | + do_race(access, mutate) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + unittest.main() |
0 commit comments