-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-122527: Fix a crash on deallocation of PyStructSequence
#122577
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Core_and_Builtins/2024-08-01-19-13-58.gh-issue-122527.eztso6.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Fix a crash that occurred when a ``PyStructSequence`` was deallocated after | ||
its type's dictionary was cleared by the GC. The type's | ||
:c:member:`~PyTypeObject.tp_basicsize` now accounts for non-sequence fields | ||
that aren't included in the :c:macro:`Py_SIZE` of the sequence. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,12 +41,20 @@ get_type_attr_as_size(PyTypeObject *tp, PyObject *name) | |
get_type_attr_as_size(tp, &_Py_ID(n_sequence_fields)) | ||
#define REAL_SIZE_TP(tp) \ | ||
get_type_attr_as_size(tp, &_Py_ID(n_fields)) | ||
#define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op)) | ||
#define REAL_SIZE(op) get_real_size((PyObject *)op) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not change also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new calculation involves |
||
|
||
#define UNNAMED_FIELDS_TP(tp) \ | ||
get_type_attr_as_size(tp, &_Py_ID(n_unnamed_fields)) | ||
#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op)) | ||
|
||
static Py_ssize_t | ||
get_real_size(PyObject *op) | ||
{ | ||
// Compute the real size from the visible size (i.e., Py_SIZE()) and the | ||
// number of non-sequence fields accounted for in tp_basicsize. | ||
Py_ssize_t hidden = Py_TYPE(op)->tp_basicsize - offsetof(PyStructSequence, ob_item); | ||
return Py_SIZE(op) + hidden / sizeof(PyObject *); | ||
} | ||
|
||
PyObject * | ||
PyStructSequence_New(PyTypeObject *type) | ||
|
@@ -120,6 +128,9 @@ structseq_dealloc(PyStructSequence *obj) | |
PyObject_GC_UnTrack(obj); | ||
|
||
PyTypeObject *tp = Py_TYPE(obj); | ||
// gh-122527: We can't use REAL_SIZE_TP() or any macros that access the | ||
// type's dictionary here, because the dictionary may have already been | ||
// cleared by the garbage collector. | ||
size = REAL_SIZE(obj); | ||
for (i = 0; i < size; ++i) { | ||
Py_XDECREF(obj->ob_item[i]); | ||
|
@@ -565,10 +576,14 @@ initialize_members(PyStructSequence_Desc *desc, | |
|
||
static void | ||
initialize_static_fields(PyTypeObject *type, PyStructSequence_Desc *desc, | ||
PyMemberDef *tp_members, unsigned long tp_flags) | ||
PyMemberDef *tp_members, Py_ssize_t n_members, | ||
unsigned long tp_flags) | ||
{ | ||
type->tp_name = desc->name; | ||
type->tp_basicsize = sizeof(PyStructSequence) - sizeof(PyObject *); | ||
// Account for hidden members in tp_basicsize because they are not | ||
// included in the variable size. | ||
Py_ssize_t n_hidden = n_members - desc->n_in_sequence; | ||
type->tp_basicsize = sizeof(PyStructSequence) + (n_hidden - 1) * sizeof(PyObject *); | ||
type->tp_itemsize = sizeof(PyObject *); | ||
type->tp_dealloc = (destructor)structseq_dealloc; | ||
type->tp_repr = (reprfunc)structseq_repr; | ||
|
@@ -621,7 +636,7 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp, | |
if (members == NULL) { | ||
goto error; | ||
} | ||
initialize_static_fields(type, desc, members, tp_flags); | ||
initialize_static_fields(type, desc, members, n_members, tp_flags); | ||
|
||
_Py_SetImmortal((PyObject *)type); | ||
} | ||
|
@@ -684,7 +699,7 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) | |
if (members == NULL) { | ||
return -1; | ||
} | ||
initialize_static_fields(type, desc, members, 0); | ||
initialize_static_fields(type, desc, members, n_members, 0); | ||
if (initialize_static_type(type, desc, n_members, n_unnamed_members) < 0) { | ||
PyMem_Free(members); | ||
return -1; | ||
|
@@ -760,7 +775,8 @@ _PyStructSequence_NewType(PyStructSequence_Desc *desc, unsigned long tp_flags) | |
/* The name in this PyType_Spec is statically allocated so it is */ | ||
/* expected that it'll outlive the PyType_Spec */ | ||
spec.name = desc->name; | ||
spec.basicsize = sizeof(PyStructSequence) - sizeof(PyObject *); | ||
Py_ssize_t hidden = n_members - desc->n_in_sequence; | ||
spec.basicsize = (int)(sizeof(PyStructSequence) + (hidden - 1) * sizeof(PyObject *)); | ||
spec.itemsize = sizeof(PyObject *); | ||
spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | tp_flags; | ||
spec.slots = slots; | ||
|
Oops, something went wrong.
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.
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.
Typo:
refcyle
.