From ea20fcef5dab525b55ba9b264d0fed8505ac9f58 Mon Sep 17 00:00:00 2001 From: Shamil Date: Sat, 19 Sep 2026 00:04:21 +0300 Subject: [PATCH] gh-156114: Fix crash in perf trampoline with unencodable code names (#156300) (cherry picked from commit 6dad8b88cc39d8f9f41c22502a0b52304b326dd9) --- Lib/test/test_perf_profiler.py | 10 ++++++++++ .../2026-08-23-12-00-00.gh-issue-156114.Kq7fXz.rst | 2 ++ Python/perf_jit_trampoline.c | 8 ++++++++ Python/perf_trampoline.c | 8 ++++++++ 4 files changed, 28 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-12-00-00.gh-issue-156114.Kq7fXz.rst diff --git a/Lib/test/test_perf_profiler.py b/Lib/test/test_perf_profiler.py index f46331ced2abb0..c90e1b8e056b92 100644 --- a/Lib/test/test_perf_profiler.py +++ b/Lib/test/test_perf_profiler.py @@ -113,6 +113,16 @@ def baz(): "Address should contain only hex characters", ) + @unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries") + def test_trampoline_with_unencodable_name(self): + code = """if 1: + import sys + + sys.activate_stack_trampoline("perf") + eval(compile("pass", "bad\\ud800file", "exec")) + """ + assert_python_ok("-c", code, PYTHON_JIT="0") + @unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries") def test_trampoline_works_with_forks(self): code = """if 1: diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-12-00-00.gh-issue-156114.Kq7fXz.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-12-00-00.gh-issue-156114.Kq7fXz.rst new file mode 100644 index 00000000000000..096216f7e6c7b3 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-12-00-00.gh-issue-156114.Kq7fXz.rst @@ -0,0 +1,2 @@ +Fix a crash in the perf trampoline when a code object has a name or filename +that cannot be encoded to UTF-8. Patched by Shamil Abdulaev. diff --git a/Python/perf_jit_trampoline.c b/Python/perf_jit_trampoline.c index 33c37e2f0ddb41..1fca943b5d02bd 100644 --- a/Python/perf_jit_trampoline.c +++ b/Python/perf_jit_trampoline.c @@ -1166,11 +1166,19 @@ static void perf_map_jit_write_entry(void *state, const void *code_addr, const char *entry = ""; if (co->co_qualname != NULL) { entry = PyUnicode_AsUTF8(co->co_qualname); + if (entry == NULL) { + PyErr_Clear(); + entry = ""; + } } const char *filename = ""; if (co->co_filename != NULL) { filename = PyUnicode_AsUTF8(co->co_filename); + if (filename == NULL) { + PyErr_Clear(); + filename = ""; + } } /* diff --git a/Python/perf_trampoline.c b/Python/perf_trampoline.c index 8e7cfff33ebfcd..40f202360e80d6 100644 --- a/Python/perf_trampoline.c +++ b/Python/perf_trampoline.c @@ -255,10 +255,18 @@ perf_map_write_entry(void *state, const void *code_addr, const char *entry = ""; if (co->co_qualname != NULL) { entry = PyUnicode_AsUTF8(co->co_qualname); + if (entry == NULL) { + PyErr_Clear(); + entry = ""; + } } const char *filename = ""; if (co->co_filename != NULL) { filename = PyUnicode_AsUTF8(co->co_filename); + if (filename == NULL) { + PyErr_Clear(); + filename = ""; + } } size_t perf_map_entry_size = snprintf(NULL, 0, "py::%s:%s", entry, filename) + 1; char* perf_map_entry = (char*) PyMem_RawMalloc(perf_map_entry_size);