https://github.com/python/cpython/commit/1f7071572e49fc8576c72902135a4ddecc6f81c3
commit: 1f7071572e49fc8576c72902135a4ddecc6f81c3
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: encukou <[email protected]>
date: 2024-08-16T12:01:40+02:00
summary:

[3.13] gh-122247: Move instruction instrumentation sanity check after tracing 
check (GH-122251) (GH-122812)

(cherry picked from commit 57d7c3e78fb635a0c6ccce38ec3e2f4284d5fac7)

Co-authored-by: Tian Gao <[email protected]>

files:
M Lib/test/test_monitoring.py
M Python/instrumentation.c

diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py
index b7c6abed1016dc..094d25b88c6e61 100644
--- a/Lib/test/test_monitoring.py
+++ b/Lib/test/test_monitoring.py
@@ -1841,6 +1841,21 @@ def f(a=1, b=2):
         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):
 
diff --git a/Python/instrumentation.c b/Python/instrumentation.c
index ae790a1441b933..3481b5df14288b 100644
--- a/Python/instrumentation.c
+++ b/Python/instrumentation.c
@@ -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] :

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to