gh-156118: Fix DTrace probes with names that cannot be encoded - #158127
Open
Danishk2445 wants to merge 1 commit into
Open
Danishk2445 wants to merge 1 commit into
Danishk2445 wants to merge 1 commit into
Conversation
The import and function entry/return probes passed the result of PyUnicode_AsUTF8() without checking it, leaving a UnicodeEncodeError set for names with lone surrogates. When unwinding, it also replaced the exception being raised. Now the error is discarded and the current exception is kept; the probes still receive NULL for such names.
Danishk2445
requested review from
FFY00,
brettcannon,
ericsnowcurrently,
kumaraditya303,
markshannon,
ncoghlan and
warsaw
as code owners
September 24, 2026 17:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a DTrace or SystemTap probe is attached, the import and function entry/return probes get the module name, function name and filename from
PyUnicode_AsUTF8()without checking the result. For a name with a lone surrogate the conversion fails and leaves aUnicodeEncodeErrorset:assert(!_PyErr_Occurred(tstate)), as in the issue.SystemError("returned a result with an exception set") or aUnicodeEncodeErrorsomewhere unrelated.DTRACE_FUNCTION_RETURN()also runs inexit_unwind, so when such a function raises, the exception being raised gets replaced.This PR sets aside the current exception during the conversion, discards any encoding error and then restores the saved exception.
get_importtime_name()does the same for-X importtime(gh-156126). The probes still receive NULL for such names, as before. They could get abackslashreplaced name instead, but that would allocate a bytes object on every function entry and return while tracing.The new
test_unencodable_namescase intest_dtraceonly prints probe firings with a NULL name, so the scripts never read the names. I tested it on Linux with bpftrace and SystemTap 5.6, in debug and release builds. It fails without the fix (the assertion from the issue) and passes with it. I couldn't run the DTrace script (unencodable_names.d); it follows the existing.dscripts.SystemTap*Tests.test_function_entry_returnalso fails for me on unmodifiedmainwith SystemTap 5.6 (thestartlines are missing), so that failure is unrelated to this PR.