GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-91051: fix segfault when using all 8 type watchers by carljm · Pull Request #107853 · python/cpython

Conversation

@carljm

@carljm carljm commented Aug 11, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.)

  • Issue: Allow extensions to set a callback to be invoked when a type is modified #91051

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

itamaro reacted with eyes emoji
All reactions
  • 👀 1 reaction
@bedevere-bot bedevere-bot mentioned this pull request Aug 11, 2023
Allow extensions to set a callback to be invoked when a type is modified #91051
Closed
@bedevere-bot bedevere-bot added the awaiting core review label Aug 11, 2023
@carljm
carljm requested review from itamaro and markshannon August 11, 2023 00:39
@carljm

carljm commented Aug 11, 2023

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@carljm carljm added the release-blocker label Aug 11, 2023
@carljm
pythongh-91051: fix segfault when using all 8 type watchers
e149aed
@carljm
carljm force-pushed the typewatchersign branch from 7c249fa to e149aed Compare August 11, 2023 00:47
itamaro
itamaro approved these changes Aug 11, 2023
View reviewed changes

@itamaro itamaro left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_capi/test_watchers.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ambv

ambv commented Aug 11, 2023

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ambv ambv closed this Aug 11, 2023
@ambv ambv reopened this Aug 11, 2023
@carljm
add comment
8b92d50
@carljm carljm added the needs backport to 3.12 only security fixes label Aug 11, 2023
@carljm
carljm merged commit 66e4edd into python:main Aug 11, 2023
@miss-islington

miss-islington commented 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!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the awaiting core review label Aug 11, 2023
@carljm
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
@carljm @miss-islington
pythongh-91051: fix segfault when using all 8 type watchers (pythonGH…
4f05fc0
…-107853)

(cherry picked from commit 66e4edd)

Co-authored-by: Carl Meyer <[email protected]>
@bedevere-bot

bedevere-bot commented Aug 11, 2023

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot 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
@miss-islington @carljm
[3.12] gh-91051: fix segfault when using all 8 type watchers (GH-107853…
00bfed7
…) (#107876)

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

Co-authored-by: Carl Meyer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@itamaro itamaro itamaro approved these changes
@markshannon markshannon Awaiting requested review from markshannon

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

release-blocker

Projects

Release and Deferred blockers 🚫
Status: Done

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@carljm @ambv @miss-islington @bedevere-bot @itamaro
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.