Skip to content

Commit ac6015d

Browse files
committed
gh-128883: Store ELF perf trampoline templates in read-only data
CPython copies the trampoline templates into executable memory. Keep the ELF source in .rodata so BOLT preserves its bytes and boundaries.
1 parent ee02e31 commit ac6015d

5 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎Lib/test/test_perf_profiler.py‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def tearDown(self) -> None:
6262
for file in files_to_delete:
6363
file.unlink()
6464

65-
@unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
6665
def test_trampoline_works(self):
6766
code = """if 1:
6867
def foo():
@@ -113,7 +112,6 @@ def baz():
113112
"Address should contain only hex characters",
114113
)
115114

116-
@unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
117115
def test_trampoline_with_unencodable_name(self):
118116
code = """if 1:
119117
import sys
@@ -123,7 +121,6 @@ def test_trampoline_with_unencodable_name(self):
123121
"""
124122
assert_python_ok("-c", code, PYTHON_JIT="0")
125123

126-
@unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
127124
def test_trampoline_works_with_forks(self):
128125
code = """if 1:
129126
import os, sys
@@ -193,7 +190,6 @@ def baz():
193190
self.assertNotIn(f"py::bar:{script}", child_perf_file_contents)
194191
self.assertNotIn(f"py::baz:{script}", child_perf_file_contents)
195192

196-
@unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
197193
def test_trampoline_works_after_fork_with_many_code_objects(self):
198194
code = """if 1:
199195
import gc, os, sys, signal
@@ -233,7 +229,6 @@ def test_trampoline_works_after_fork_with_many_code_objects(self):
233229
self.assertEqual(process.returncode, 0, stderr)
234230
self.assertEqual(stderr, "")
235231

236-
@unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
237232
def test_sys_api(self):
238233
for define_eval_hook in (False, True):
239234
code = """if 1:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Add a byte after the ELF perf trampoline end marker so it does not share
2-
the following function's address, which BOLT may otherwise move.
1+
Fix crashes with ``-X perf`` and ``-X perf_jit`` in BOLT builds.
2+
The ELF perf trampoline templates now use read-only data sections.

‎Python/asm_trampoline_aarch64.S‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@
3535
#define GNU_PROPERTY_AARCH64_GCS 0
3636
#endif
3737

38+
/* CPython copies this template into executable memory. */
39+
#if defined(__ELF__)
40+
.section .rodata
41+
#else
3842
.text
43+
#endif
3944
#if defined(__aarch64__) && defined(__AARCH64EL__) && !defined(__ILP32__)
4045
#if defined(__APPLE__)
4146
.globl __Py_trampoline_func_start
@@ -57,7 +62,6 @@ __Py_trampoline_func_end:
5762
#else
5863
.globl _Py_trampoline_func_end
5964
_Py_trampoline_func_end:
60-
.byte 0
6165
.section .note.GNU-stack,"",@progbits
6266
#endif
6367

‎Python/asm_trampoline_riscv64.S‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
.text
1+
/* CPython copies this template into executable memory. */
2+
.section .rodata
23
.globl _Py_trampoline_func_start
34
_Py_trampoline_func_start:
45
addi sp,sp,-16
@@ -9,5 +10,4 @@ _Py_trampoline_func_start:
910
jr ra
1011
.globl _Py_trampoline_func_end
1112
_Py_trampoline_func_end:
12-
.byte 0
1313
.section .note.GNU-stack,"",@progbits

‎Python/asm_trampoline_x86_64.S‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
/* CPython copies this template into executable memory. */
2+
#if defined(__ELF__)
3+
.section .rodata
4+
#else
15
.text
6+
#endif
27
#ifdef __x86_64__
38
#if defined(__APPLE__)
49
.globl __Py_trampoline_func_start
@@ -21,7 +26,6 @@ __Py_trampoline_func_end:
2126
#else
2227
.globl _Py_trampoline_func_end
2328
_Py_trampoline_func_end:
24-
.byte 0
2529
.section .note.GNU-stack,"",@progbits
2630
#endif
2731
# Note for indicating the assembly code supports CET

0 commit comments

Comments
 (0)