FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-91051: fix segfault when using all 8 type watchers by carljm · Pull Request #107853 · python/cpython · GitHub

/ cpython Public

gh-91051: fix segfault when using all 8 type watchers - #107853

Merged
carljm merged 2 commits into
python:mainfrom
carljm:typewatchersign
Aug 11, 2023
Merged

gh-91051: fix segfault when using all 8 type watchers#107853
carljm merged 2 commits into
python:mainfrom
carljm:typewatchersign

Conversation

carljm commented Aug 11, 2023
edited
Loading

Copy link
Copy Markdown
Member

The type watcher implementation adds char tp_watched; to the PyTypeObject struct, and then in PyType_Modified it assigns int bits = type->tp_watched; and then treats bits as a bitset, expecting that only the low 8 bits should ever be set.

On platforms where char is signed (e.g. x86), if the top bit in tp_watched is set, the cast to int bits will sign-extend with 1s, and we will thus have a lot of high bits set in bits that are not supposed to ever be set.

On x86 in a debug build, without the fix in this PR, the new test hits the assertion in PyType_Modified that only bit positions < TYPE_MAX_WATCHERS should be set, and aborts.

The fix is simple: tp_watched should be defined as unsigned char so that it is zero-extended, not sign-extended.

Note that the test is carefully designed to not assume there are no type watchers already active. It doesn't attempt to register a fixed number of type watchers, instead it just registers type watchers until it reaches TYPE_MAX_WATCHERS - 1, the last available slot and the one that can trigger this bug (since it will set the highest bit in tp_watched when watching a type.)

carljm commented Aug 11, 2023

Copy link
Copy Markdown
Member Author

@Yhg1s I think this fix should be backported to 3.12.

itamaro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

lgtm! we're going to want to backport to 3.12!

ambv commented Aug 11, 2023

Copy link
Copy Markdown
Contributor

Closing and re-opening to retrigger CLA checks. Sorry for the noise.

ambv closed this Aug 11, 2023
ambv reopened this Aug 11, 2023
carljm added the needs backport to 3.12 only security fixes label Aug 11, 2023
carljm merged commit 66e4edd into python:main Aug 11, 2023

Copy link
Copy Markdown
Contributor

Thanks @carljm for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

carljm deleted the typewatchersign branch August 11, 2023 18:42
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 11, 2023
…-107853)

(cherry picked from commit 66e4edd)

Co-authored-by: Carl Meyer <carl@oddbird.net>

Copy link
Copy Markdown

GH-107876 is a backport of this pull request to the 3.12 branch.

bedevere-bot removed the needs backport to 3.12 only security fixes label Aug 11, 2023
Yhg1s pushed a commit that referenced this pull request Aug 16, 2023
…) (#107876)

* gh-91051: fix segfault when using all 8 type watchers (GH-107853)
(cherry picked from commit 66e4edd)

Co-authored-by: Carl Meyer <carl@oddbird.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL