-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
gh-118331: Fix a couple of issues when list allocation fails #130811
Conversation
Set the items pointer in the list object to NULL after the items array is freed during list deallocation. Otherwise, we can end up with a list object added to the free list that contains a pointer to an already-freed items array.
I think technically it's not escaping, because the only object that can be decrefed if allocation fails is an exact list, which cannot execute arbitrary code when it is destroyed. However, this seems less intrusive than trying to special cases objects in the assert in `_Py_Dealloc` that checks for non-null stackpointers and shouldn't matter for performance.
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.
I think going with (3), treating _PyList_FromStackRefStealOnSuccess
as possibly escaping, makes sense for now. I don't expect it to impact performance given that we started treating PyStackRef_CLOSE
as escaping without problems, and STORE_FAST
is executed ~35x more frequently than BUILD_LIST
.
This commit introduced a failure in
|
This fixes a couple of bugs that are triggered when list allocation fails. Specifically, when the list object is allocated
successfully, but allocation of the items array fails.
Use-after-free on the items array
We didn't set the items array pointer in the list object to
NULL
after freeing the items array. As a result, we could end up with a list object added to the free list that contained a pointer to an already-freed items array. A subsequent list allocation that successfully retrieved a list object from the free list but failed to allocate a new items would deallocate thelist object:
cpython/Objects/listobject.c
Lines 251 to 255 in bbf1979
list_dealloc
would then try to use the previously freed items array:cpython/Objects/listobject.c
Lines 526 to 536 in bbf1979
Incorrect stackpointer assertion
We check that either there is no Python code executing (frame is
NULL
) or the stack pointer for the current frame is set when executing_Py_Dealloc
:cpython/Objects/object.c
Lines 2987 to 2991 in bbf1979
I think the intent here is to catch places in the interpreter loop that escape due to decrefs where we aren't setting / clearing the stack pointer correctly (e.g. due to shortcomings in our analysis or escaping calls that are incorrectly marked as non-escaping). The assertion is overly conservative. It will catch all potentially escaping decrefs, but it will also catch decrefs that can never escape. In this case,
_PyList_FromStackRefStealOnSuccess
is, correctly, I think, marked as non-escaping. The only decref it can perform is on an exact list (not a subtype). However, it triggers this assertion.There are a few options I can see for fixing this:
_PyList_FromStackRefStealOnSuccess
as escaping.I went with (3) because it's correct, if pessimistic, and I don't think it'll impact performance too much. I think the assertion is worth keeping around and I'm not sure of a good way to do (2) generically. Happy to do something else if reviewers feel strongly otherwise.