]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122247: Move instruction instrumentation sanity check after tracing check (#122251)
authorTian Gao <gaogaotiantian@hotmail.com>
Thu, 8 Aug 2024 04:30:14 +0000 (21:30 -0700)
committerGitHub <noreply@github.com>
Thu, 8 Aug 2024 04:30:14 +0000 (21:30 -0700)
Lib/test/test_monitoring.py
Python/instrumentation.c

index d7043cd4866a1c4a0cba46fcc2dcdbce6dbdc3ac..8ef8c374cd6cc56ef5997d5a198b87a514db0ac8 100644 (file)
@@ -1863,6 +1863,21 @@ class TestRegressions(MonitoringTestBase, unittest.TestCase):
         self.assertEqual(call_data[0], (f, 1))
         self.assertEqual(call_data[1], (f, sys.monitoring.MISSING))
 
+    def test_instruction_explicit_callback(self):
+        # gh-122247
+        # Calling the instruction event callback explicitly should not
+        # crash CPython
+        def callback(code, instruction_offset):
+            pass
+
+        sys.monitoring.use_tool_id(0, "test")
+        self.addCleanup(sys.monitoring.free_tool_id, 0)
+        sys.monitoring.register_callback(0, sys.monitoring.events.INSTRUCTION, callback)
+        sys.monitoring.set_events(0, sys.monitoring.events.INSTRUCTION)
+        callback(None, 0)  # call the *same* handler while it is registered
+        sys.monitoring.restart_events()
+        sys.monitoring.set_events(0, 0)
+
 
 class TestOptimizer(MonitoringTestBase, unittest.TestCase):
 
index ae790a1441b9339ad77e96893e7334e2db86bef9..3481b5df14288be2aff32c6d087dc7e32000eb59 100644 (file)
@@ -1344,7 +1344,6 @@ int
 _Py_call_instrumentation_instruction(PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr)
 {
     PyCodeObject *code = _PyFrame_GetCode(frame);
-    assert(debug_check_sanity(tstate->interp, code));
     int offset = (int)(instr - _PyCode_CODE(code));
     _PyCoMonitoringData *instrumentation_data = code->_co_monitoring;
     assert(instrumentation_data->per_instruction_opcodes);
@@ -1352,6 +1351,7 @@ _Py_call_instrumentation_instruction(PyThreadState *tstate, _PyInterpreterFrame*
     if (tstate->tracing) {
         return next_opcode;
     }
+    assert(debug_check_sanity(tstate->interp, code));
     PyInterpreterState *interp = tstate->interp;
     uint8_t tools = instrumentation_data->per_instruction_tools != NULL ?
         instrumentation_data->per_instruction_tools[offset] :