https://github.com/python/cpython/commit/2aed76380bb3bb46aa9bd6abafa7ecd2a2062d74
commit: 2aed76380bb3bb46aa9bd6abafa7ecd2a2062d74
branch: 3.13
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-09-18T21:27:16Z
summary:

[3.13] gh-156114: Fix crash in perf trampoline with unencodable code names 
(GH-156300) (#157774)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-08-23-12-00-00.gh-issue-156114.Kq7fXz.rst
M Lib/test/test_perf_profiler.py
M Python/perf_jit_trampoline.c
M Python/perf_trampoline.c

diff --git a/Lib/test/test_perf_profiler.py b/Lib/test/test_perf_profiler.py
index 9fd0de94960172d..7b4d566f375803e 100644
--- a/Lib/test/test_perf_profiler.py
+++ b/Lib/test/test_perf_profiler.py
@@ -115,6 +115,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")
+
     def test_trampoline_works_with_forks(self):
         code = """if 1:
                 import os, sys
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 000000000000000..096216f7e6c7b3c
--- /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 3901df7f17d6a5d..1e9693e6d5f5899 100644
--- a/Python/perf_jit_trampoline.c
+++ b/Python/perf_jit_trampoline.c
@@ -1165,11 +1165,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 e296605c16ac326..94728e8fec102b3 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);

_______________________________________________
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