Skip to content

gh-128883: Store ELF perf trampoline templates in read-only data - #160366

Open
zanieb wants to merge 2 commits into
python:mainfrom
zanieb:pbs/perf-trampoline-upstream
Open

zanieb wants to merge 2 commits into
python:mainfrom
zanieb:pbs/perf-trampoline-upstream

Conversation

@zanieb

@zanieb zanieb commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

CPython copies the perf trampoline templates into executable memory at runtime. Store the ELF templates in .rodata so BOLT treats them as data and preserves their bytes and boundaries.

Fixes #128883.

@zanieb

zanieb commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Frankly this is a little beyond my understanding and I'm going to try to gather some clearer evidence of how this manifests downstream.

cc @pablogsal I assume you are the best reviewer for this though

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is the right fix. The byte gives the end marker a separate address from the next function, but it only addresses that specific layout. BOLT can still move the start marker independently or instrument the trampoline itself. In the latter case, it can introduce PC-relative references that become invalid when CPython copies the code to a different address.

CPython uses these bytes as a template for copies in executable memory. The ELF template therefore belongs in .rodata instead of .text. BOLT then treats it as data instead of a native function, which protects both the bytes and the range that CPython copies.

I suggest we change the template section and remove the BOLT-specific test skips as part of the fix. That avoids the extra byte and a trampoline-specific exclusion from BOLT.

@bedevere-app

bedevere-app Bot commented Oct 23, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@zanieb

zanieb commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Appreciate the feedback! Thanks!

CPython copies the trampoline templates into executable memory. Keep the
ELF source in .rodata so BOLT preserves its bytes and boundaries.
@pablogsal pablogsal changed the title gh-128883: Keep the perf trampoline end marker inside its text section gh-128883: Store ELF perf trampoline templates in read-only data Oct 23, 2026
@pablogsal

Copy link
Copy Markdown
Member

I pushed a fix :)

@pablogsal pablogsal added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 23, 2026
@pablogsal

Copy link
Copy Markdown
Member

Tested locally with:

$ ../configure --with-pydebug --without-ensurepip --enable-bolt \
    CC="gcc -no-pie" \
    CFLAGS="-O1 -g -fno-pie -fno-omit-frame-pointer"
...
configure: creating Makefile

$ make -j16 PROFILE_TASK="-m test test_perf_profiler -v"
...
# Run instrumented binaries to collect data.
./python -m test test_perf_profiler -v
...
Ran 16 tests in 34.414s

OK (skipped=1)
...
# Run bolt against the merged data to produce an optimized binary.
...
touch profile-bolt-stamp

$ ./python -m test test_perf_profiler -v
...
Ran 16 tests in 10.048s

OK (skipped=1)
...
Total tests: run=16 skipped=1
Total test files: run=1/1
Result: SUCCESS

@pablogsal
pablogsal enabled auto-merge (squash) October 23, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf trampoline tests fail after BOLT optimizations

2 participants