bpo-40887: Don't use finalized free lists (GH-20700)

In debug mode, ensure that free lists are no longer used after being
finalized. Set numfree to -1 in finalization functions
(eg. _PyList_Fini()), and then check that numfree is not equal to -1
before using a free list (e.g list_dealloc()).
This commit is contained in:
Victor Stinner 2020-06-08 02:14:47 +02:00 committed by GitHub
parent c96a61e816
commit bcb198385d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 3 deletions

View file

@ -335,6 +335,10 @@ _context_alloc(void)
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _Py_context_state *state = &interp->context;
PyContext *ctx;
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
assert(state->numfree != -1);
#endif
if (state->numfree) {
state->numfree--;
ctx = state->freelist;
@ -460,6 +464,10 @@ context_tp_dealloc(PyContext *self)
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _Py_context_state *state = &interp->context;
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
assert(state->numfree != -1);
#endif
if (state->numfree < CONTEXT_FREELIST_MAXLEN) {
state->numfree++;
self->ctx_weakreflist = (PyObject *)state->freelist;
@ -1290,6 +1298,10 @@ _PyContext_Fini(PyThreadState *tstate)
{
Py_CLEAR(_token_missing);
_PyContext_ClearFreeList(tstate);
#ifdef Py_DEBUG
struct _Py_context_state *state = &tstate->interp->context;
state->numfree = -1;
#endif
_PyHamt_Fini();
}