Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-108976: Make sure instrumentation line returns the correct opcode when instruction instrumentation is stripped #109043

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,15 @@ def func1():
('instruction', 'func1', 14),
('line', 'get_events', 11)])

def test_gh108976(self):
sys.monitoring.use_tool_id(0, "test")
sys.monitoring.set_events(0, 0)
sys.monitoring.register_callback(0, E.LINE, lambda *args: sys.monitoring.set_events(0, 0))
sys.monitoring.register_callback(0, E.INSTRUCTION, lambda *args: 0)
sys.monitoring.set_events(0, E.LINE | E.INSTRUCTION)
sys.monitoring.set_events(0, 0)


class TestInstallIncrementallly(MonitoringTestBase, unittest.TestCase):

def check_events(self, func, must_include, tool=TEST_TOOL, recorders=(ExceptionRecorder,)):
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,27 @@ def test_pdb_issue_gh_101517():
(Pdb) continue
"""

def test_pdb_issue_gh_108976():
"""See GH-108976

Make sure setting f_trace_opcodes = True won't crash pdb

>>> def test_function():
... import sys
... sys._getframe().f_trace_opcodes = True
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... a = 1

>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'continue'
... ]):
... test_function()
bdb.Bdb.dispatch: unknown debugging event: 'opcode'
> <doctest test.test_pdb.test_pdb_issue_gh_108976[0]>(5)test_function()
-> a = 1
(Pdb) continue
"""

def test_pdb_ambiguous_statements():
"""See GH-104301

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make sure instrumentation line returns the correct opcode when instruction instrumentation is stripped
11 changes: 11 additions & 0 deletions Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,19 @@ _Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame,
remove_line_tools(code, i, 1 << tool);
}
} while (tools);

Py_DECREF(line_obj);
done:
// original_opcode is acquired before trace function and the callbacks. It's
// possible that the trace function or the callbacks turn off the instruction
// monitoring. If the instruction instrumentation is stripped, using the
// opcode could crash the interpreter. We should use the original opcode
// instead.
if (monitoring->active_monitors.tools[PY_MONITORING_EVENT_INSTRUCTION] == 0 &&
original_opcode == INSTRUMENTED_INSTRUCTION) {
original_opcode = instr->op.code;
}

assert(original_opcode != 0);
assert(original_opcode != INSTRUMENTED_LINE);
assert(_PyOpcode_Deopt[original_opcode] == original_opcode);
Expand Down