| 1 |
""" |
| 2 |
jpy_reachability_fence_test.py |
| 3 |
=============================== |
| 4 |
Stress tests for the Reference.reachabilityFence guards in PyObject. |
| 5 |
|
| 6 |
Problem |
| 7 |
------- |
| 8 |
When a PyObject wrapper is used transiently — e.g. the result of getAttribute() |
| 9 |
is immediately passed to callMethod(), getIntValue(), str(), etc. — JLS §12.6.1 |
| 10 |
permits the JIT to treat the wrapper as reachability-dead once getPointer() has |
| 11 |
copied the native pointer into a long. At that point the GC may collect the |
| 12 |
wrapper and the cleanup thread may Py_DECREF the underlying PyObject* while JNI |
| 13 |
is still using it, producing a SIGSEGV (use-after-free). |
| 14 |
|
| 15 |
Approach |
| 16 |
-------- |
| 17 |
Each test method defines a Python object with specific attributes, starts a |
| 18 |
background GC-pressure thread (continuously allocating to trigger young-gen GC), |
| 19 |
then hammers a particular PyObject method path at high iteration count. |
| 20 |
|
| 21 |
Without the Reference.reachabilityFence fix these tests crash the JVM with |
| 22 |
SIGSEGV inside libpython. With the fix they must complete without error. |
| 23 |
""" |
| 24 |
|
| 25 |
import unittest |
| 26 |
|
| 27 |
import jpyutil |
| 28 |
|
| 29 |
|
| 30 |
jpyutil.init_jvm(jvm_maxmem='128M', jvm_classpath=['target/classes', 'target/test-classes']) |
| 31 |
import jpy |
| 32 |
|
| 33 |
ITERATIONS = 500_000 |
| 34 |
|
| 35 |
|
| 36 |
class TestReachabilityFence(unittest.TestCase): |
| 37 |
|
| 38 |
def setUp(self): |
| 39 |
self.Fixture = jpy.get_type( |
| 40 |
'org.jpy.fixtures.ReachabilityFenceTestFixture') |
| 41 |
self.assertIsNotNone(self.Fixture) |
| 42 |
|
| 43 |
def tearDown(self): |
| 44 |
self.Fixture.stopAllocator() |
| 45 |
|
| 46 |
def _run_with_gc_pressure(self, stress_fn): |
| 47 |
"""Helper: start allocator, run the stress function, stop allocator.""" |
| 48 |
self.Fixture.startAllocator() |
| 49 |
try: |
| 50 |
stress_fn() |
| 51 |
finally: |
| 52 |
self.Fixture.stopAllocator() |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
def test_stress_call_method(self): |
| 58 |
"""getAttribute('__call__') -> transient.callMethod('__call__')""" |
| 59 |
|
| 60 |
class _Holder: |
| 61 |
def __call__(self): |
| 62 |
return 42 |
| 63 |
|
| 64 |
holder = _Holder() |
| 65 |
self._run_with_gc_pressure( |
| 66 |
lambda: self.Fixture.stressCallTransient(holder, ITERATIONS)) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
def test_stress_get_int_value(self): |
| 72 |
"""getAttribute('value') -> transient.getIntValue()""" |
| 73 |
|
| 74 |
class _Holder: |
| 75 |
value = 99 |
| 76 |
|
| 77 |
holder = _Holder() |
| 78 |
self._run_with_gc_pressure( |
| 79 |
lambda: self.Fixture.stressGetIntValue(holder, ITERATIONS)) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
def test_stress_get_string_value(self): |
| 85 |
"""getAttribute('name') -> transient.getStringValue()""" |
| 86 |
|
| 87 |
class _Holder: |
| 88 |
name = "hello" |
| 89 |
|
| 90 |
holder = _Holder() |
| 91 |
self._run_with_gc_pressure( |
| 92 |
lambda: self.Fixture.stressGetStringValue(holder, ITERATIONS)) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
def test_stress_str(self): |
| 98 |
"""getAttribute('value') -> transient.str()""" |
| 99 |
|
| 100 |
class _Holder: |
| 101 |
value = 42 |
| 102 |
|
| 103 |
holder = _Holder() |
| 104 |
self._run_with_gc_pressure( |
| 105 |
lambda: self.Fixture.stressStr(holder, ITERATIONS)) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
def test_stress_repr(self): |
| 111 |
"""getAttribute('value') -> transient.repr()""" |
| 112 |
|
| 113 |
class _Holder: |
| 114 |
value = 42 |
| 115 |
|
| 116 |
holder = _Holder() |
| 117 |
self._run_with_gc_pressure( |
| 118 |
lambda: self.Fixture.stressRepr(holder, ITERATIONS)) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
def test_stress_hash(self): |
| 124 |
"""getAttribute('name') -> transient.hash()""" |
| 125 |
|
| 126 |
class _Holder: |
| 127 |
name = "hashme" |
| 128 |
|
| 129 |
holder = _Holder() |
| 130 |
self._run_with_gc_pressure( |
| 131 |
lambda: self.Fixture.stressHash(holder, ITERATIONS)) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
def test_stress_type_checks(self): |
| 137 |
"""getAttribute('value') -> transient.isInt()/isFloat()/isString()/...""" |
| 138 |
|
| 139 |
class _Holder: |
| 140 |
value = 7 |
| 141 |
|
| 142 |
holder = _Holder() |
| 143 |
self._run_with_gc_pressure( |
| 144 |
lambda: self.Fixture.stressTypeChecks(holder, ITERATIONS)) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
def test_stress_has_attribute(self): |
| 150 |
"""getAttribute('nested') -> transient.hasAttribute('value')""" |
| 151 |
|
| 152 |
class _Nested: |
| 153 |
value = 1 |
| 154 |
|
| 155 |
class _Holder: |
| 156 |
nested = _Nested() |
| 157 |
|
| 158 |
holder = _Holder() |
| 159 |
self._run_with_gc_pressure( |
| 160 |
lambda: self.Fixture.stressHasAttribute(holder, ITERATIONS)) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
def test_stress_call(self): |
| 166 |
"""getAttribute('compute') -> transient.call('__call__', arg)""" |
| 167 |
|
| 168 |
class _Holder: |
| 169 |
@staticmethod |
| 170 |
def compute(x): |
| 171 |
return x * 2 |
| 172 |
|
| 173 |
holder = _Holder() |
| 174 |
self._run_with_gc_pressure( |
| 175 |
lambda: self.Fixture.stressCall(holder, ITERATIONS)) |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
def test_stress_get_object_value(self): |
| 181 |
"""getAttribute('value') -> transient.getObjectValue()""" |
| 182 |
|
| 183 |
class _Holder: |
| 184 |
value = 123 |
| 185 |
|
| 186 |
holder = _Holder() |
| 187 |
self._run_with_gc_pressure( |
| 188 |
lambda: self.Fixture.stressGetObjectValue(holder, ITERATIONS)) |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
def test_stress_proxy(self): |
| 194 |
"""createProxy(Computable) -> transient proxy.compute(i)""" |
| 195 |
|
| 196 |
class _Computable: |
| 197 |
def compute(self, x): |
| 198 |
return x * 2 |
| 199 |
|
| 200 |
obj = _Computable() |
| 201 |
self._run_with_gc_pressure( |
| 202 |
lambda: self.Fixture.stressProxy(obj, ITERATIONS)) |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
def test_stress_dict_contains_key(self): |
| 208 |
"""asDict().containsKey('key') on a transient wrapper""" |
| 209 |
|
| 210 |
d = {"key": 42, "other": 99} |
| 211 |
|
| 212 |
py_dict = jpy.get_type('org.jpy.PyObject').executeCode( |
| 213 |
"{'key': 42, 'other': 99}", jpy.get_type('org.jpy.PyInputMode').EXPRESSION) |
| 214 |
self._run_with_gc_pressure( |
| 215 |
lambda: self.Fixture.stressDictContainsKey(py_dict, ITERATIONS)) |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
def test_stress_dict_key_set(self): |
| 221 |
"""asDict().keySet() on a transient wrapper""" |
| 222 |
|
| 223 |
py_dict = jpy.get_type('org.jpy.PyObject').executeCode( |
| 224 |
"{'a': 1, 'b': 2, 'c': 3}", jpy.get_type('org.jpy.PyInputMode').EXPRESSION) |
| 225 |
self._run_with_gc_pressure( |
| 226 |
lambda: self.Fixture.stressDictKeySet(py_dict, ITERATIONS)) |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
def test_stress_dict_copy(self): |
| 232 |
"""asDict().copy() on a transient wrapper""" |
| 233 |
|
| 234 |
py_dict = jpy.get_type('org.jpy.PyObject').executeCode( |
| 235 |
"{'x': 10}", jpy.get_type('org.jpy.PyInputMode').EXPRESSION) |
| 236 |
self._run_with_gc_pressure( |
| 237 |
lambda: self.Fixture.stressDictCopy(py_dict, ITERATIONS)) |
| 238 |
|
| 239 |
|
| 240 |
if __name__ == '__main__': |
| 241 |
print('\nRunning ' + __file__) |
| 242 |
unittest.main() |
| 243 |
|