Skip to content

gh-131998: Fix NULL dereference when using an unbound method descriptor in a specialized code path #132000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 8, 2025

Conversation

ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Apr 2, 2025

Verified

This commit was signed with the committer’s verified signature.
tmshort Todd Short
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

(not a full review)

# GH-131998: The specialized instruction would get tricked into dereferencing
# a bound "self" that didn't exist if subsequently called unbound.
code = """if True:
import glob
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this import?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, the optimizer is finicky and I can't get it to reliably reproduce without the import. I'll add a comment for clarity.

Copy link
Member

Choose a reason for hiding this comment

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

I reproduce the crash in a reliable way without the import glob.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it reliably reproduce in the test case? I think it only happens if you've already got some compiled bytecode to trigger the optimizer, which isn't the case in CI.

Copy link
Member

Choose a reason for hiding this comment

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

You can replace import glob with just this code copied from Lib/types.py (a dependency of glob):

# CellType comes from types.py
def _cell_factory():
    a = 1
    def f():
        nonlocal a
    return f.__closure__[0]
CellType = type(_cell_factory())

Copy link
Member

Choose a reason for hiding this comment

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

I'm ok-ish with keeping import glob, but add a comment explaining the purpose of this unused import.

Copy link
Member

Choose a reason for hiding this comment

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

Does it reliably reproduce in the test case? I think it only happens if you've already got some compiled bytecode to trigger the optimizer, which isn't the case in CI.

Sorry, I mean that I can reproduce the crash without import glob when I run a script: ./python reproducer.py. I confirm that for this test case, import glob (or the code example that I proposed) is needed to trigger the bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a comment.

Copy link
Member

Choose a reason for hiding this comment

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

This import shouldn't be needed with the changes below.

@vstinner vstinner added the needs backport to 3.13 bugs and security fixes label Apr 2, 2025
ZeroIntensity and others added 3 commits April 2, 2025 05:50

Verified

This commit was signed with the committer’s verified signature.
tmshort Todd Short
Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@ZeroIntensity ZeroIntensity marked this pull request as ready for review April 2, 2025 10:12
Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

We need to guard total_args before reading self.

This applies to CALL_METHOD_DESCRIPTOR_FAST as well.

@bedevere-app
Copy link

bedevere-app bot commented Apr 2, 2025

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.

@ZeroIntensity
Copy link
Member Author

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Apr 2, 2025

Thanks for making the requested changes!

@markshannon: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from markshannon April 2, 2025 21:00
@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@hugovk
Copy link
Member

hugovk commented Apr 7, 2025

This PR had a merge conflict.

Next 3.13/3.14 releases are tomorrow.

@ZeroIntensity
Copy link
Member Author

Fixed the conflicts.

Friendly ping @markshannon -- this is a blocker for 3.13.3.

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

I think we can remove the import glob by filling the stack with lists before the call.

# GH-131998: The specialized instruction would get tricked into dereferencing
# a bound "self" that didn't exist if subsequently called unbound.
code = """if True:
import glob
Copy link
Member

Choose a reason for hiding this comment

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

This import shouldn't be needed with the changes below.

@bedevere-app
Copy link

bedevere-app bot commented Apr 8, 2025

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.

And if you don't make the requested changes, you will be poked with soft cushions!

ZeroIntensity and others added 3 commits April 8, 2025 05:40
Co-authored-by: Mark Shannon <[email protected]>
@markshannon
Copy link
Member

FTR, here's the reproducer on main without importing glob.

>>> def call(part):
...     [] + ([] + [])
...     part.pop()
...
>>> for _ in range(3):
...     call(['a'])
...
>>> call(list)
Segmentation fault (core dumped)

@ZeroIntensity
Copy link
Member Author

I'm not going to trigger the noisy bot again, but this should be good to go.

@Yhg1s Yhg1s enabled auto-merge (squash) April 8, 2025 10:08
@Yhg1s Yhg1s merged commit ac3c439 into python:main Apr 8, 2025
64 checks passed
@miss-islington-app
Copy link

Thanks @ZeroIntensity for the PR, and @Yhg1s for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @ZeroIntensity and @Yhg1s, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker ac3c439cdfee8452f2bcceacd67a1f4e423ac3cf 3.13

@ZeroIntensity
Copy link
Member Author

I'll deal with the backport.

ZeroIntensity added a commit to ZeroIntensity/cpython that referenced this pull request Apr 8, 2025
…method descriptor in a specialized code path (pythonGH-132000)

(cherry picked from commit ac3c439)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Mark Shannon <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Apr 8, 2025

GH-132262 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Apr 8, 2025
@ZeroIntensity ZeroIntensity deleted the gh-131998-method-descriptor-crash branch April 8, 2025 10:39
Yhg1s pushed a commit that referenced this pull request Apr 8, 2025
… descriptor in a specialized code path (GH-132000) (#132262)

(cherry picked from commit ac3c439)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Mark Shannon <[email protected]>
seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
…descriptor in a specialized code path (python#132000)

Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Mark Shannon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants