Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-99293: Fix stale method caches and assertion errors in SWIG-generated modules#99294

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Yhg1s wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromYhg1s:fix_assertion_errors

Conversation

Yhg1s
Copy link
Member

@Yhg1sYhg1s commentedNov 9, 2022
edited by bedevere-bot
Loading

Extension modules generated by SWIG up to version 4.1.0 clear the Py_TPFLAGS_VALID_VERSION_TAG bit from tp_flags when modifying the type, as they should, but do not update tp_version_tag as typeobject.c expects. (For example, merely one of many instances I've found:https://github.com/OSGeo/gdal/blob/6bd07b20b3e55c2fc94da611244a615a4fd2991f/swig/python/extensions/osr_wrap.cpp#L2296)

In release builds this means a potentially stale cached entry is used, and in debug builds (or release builds that happen to enable assertions, which as it turns out is actually a good idea) it triggers an assertion error.

SWIG 4.1.0 avoids this problem by using PyType_Modified(), but there are a lot of older versions of SWIG and of checked-in generated extension modules around.

modules.Extension modules generated by SWIG up to version 4.1.0 clear thePy_TPFLAGS_VALID_VERSION_TAG bit from tp_flags when modifying the type, asthey should, but do not update tp_version_tag as typeobject.c expects. (Forexample, merely one of many instances I've found:https://github.com/OSGeo/gdal/blob/6bd07b20b3e55c2fc94da611244a615a4fd2991f/swig/python/extensions/osr_wrap.cpp#L2296)In release builds this means a potentially stale cached entry is used, andin debug builds (or release builds that happen to enable assertions, whichas it turns out is actually a good idea) it triggers an assertion error.SWIG 4.1.0 avoids this problem by using PyType_Modified(), but there are alot of older versions of SWIG and of checked-in generated extension modulesaround.
Copy link
Member

@Fidget-SpinnerFidget-Spinner left a comment

Choose a reason for hiding this comment

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

LGTM. However, let's wait a bit for others to review.

#if MCACHE_STATS
cache->hits++;
#endif
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
return entry->value;
}

Copy link
Member

@Fidget-SpinnerFidget-SpinnerNov 9, 2022
edited
Loading

Choose a reason for hiding this comment

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

For other reviewers wondering what happens down this code path ifPy_TPFLAGS_VALID_VERSION_TAG is not set:

  1. A normal lookup happens where we walk the MRO.
  2. We assign a new version tag to the type. This setsPy_TPFLAGS_VALID_VERSION_TAG.

So this should be safe.

@@ -0,0 +1 @@
Fix an issue where extension modules could inadvertently trigger an assertion error in typeobject.c by clearing the Py_TPFLAGS_VALID_VERSION_TAG tp_flag without clearing the tp_version_tag field. (Extension modules should use PyType_Modified() instead, however.)

Choose a reason for hiding this comment

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

I just noticedPy_TPFLAGS_VALID_VERSION_TAG isn't actually documented.

Suggested change
Fix an issue where extension modules could inadvertently trigger an assertion error in typeobject.c by clearing the Py_TPFLAGS_VALID_VERSION_TAG tp_flag without clearing the tp_version_tag field. (Extension modules should use PyType_Modified() instead, however.)
Fix an issue where extension modules could inadvertently trigger an assertion error in``typeobject.c`` by clearing the``Py_TPFLAGS_VALID_VERSION_TAG``:c:member:`~PyTypeObject.tp_flags` without clearing the:c:member:`~PyTypeObject.tp_version_tag` field. (Extension modules should use:c:func:`PyType_Modified` instead, however.)

@Fidget-Spinner
Copy link
Member

Fidget-Spinner commentedNov 9, 2022
edited
Loading

Copying a comment I made elsewhere:

The code willcrash return incorrect cached methods and values on 3.11 when running specialised instructions. Those only use tp_version_tag as a guard and don't check Py_TPFLAGS_VALID_VERSION_TAG for speed reasons.

One fix is to add a guard to all specialisedLOAD_METHOD/ATTR instructions to check for the flag (this will slow down all those instructions a little). Another fix is to ask everyone to regenerate their code with a newer SWIG for Python 3.11.0.

@hugovkhugovk removed the needs backport to 3.10only security fixes labelApr 7, 2023
@serhiy-storchakaserhiy-storchaka added needs backport to 3.12only security fixes needs backport to 3.13bugs and security fixes and removed needs backport to 3.11only security fixes labelsMay 9, 2024
@hugovkhugovk removed the needs backport to 3.12only security fixes labelApr 25, 2025
@serhiy-storchakaserhiy-storchaka added the needs backport to 3.14bugs and security fixes labelMay 8, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@Fidget-SpinnerFidget-SpinnerFidget-Spinner approved these changes

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

Assignees
No one assigned
Labels
DO-NOT-MERGEneeds backport to 3.13bugs and security fixesneeds backport to 3.14bugs and security fixes
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@Yhg1s@Fidget-Spinner@iritkatriel@hugovk@serhiy-storchaka@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp