Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-135106: [crash repro] Randomly deposit objects in _Py_Dealloc #135589

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ struct _ts {
*/
PyObject *threading_local_sentinel;
_PyRemoteDebuggerSupport remote_debugger_support;

uint64_t prng;
Py_ssize_t ob_dealloc_depth;
};

/* other API */
Expand Down
14 changes: 13 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3155,6 +3155,16 @@ _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Py_FatalError("_PyObject_AssertFailed");
}

static int
should_randomly_deposit_object(PyThreadState *tstate)
{
// splitmix64 from https://prng.di.unimi.it/splitmix64.c
uint64_t z = (tstate->prng += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
uint64_t r = z ^ (z >> 31);
return (r & 0xFF) < 2;
}

/*
When deallocating a container object, it's possible to trigger an unbounded
Expand All @@ -3170,7 +3180,7 @@ _Py_Dealloc(PyObject *op)
destructor dealloc = type->tp_dealloc;
PyThreadState *tstate = _PyThreadState_GET();
intptr_t margin = _Py_RecursionLimit_GetMargin(tstate);
if (margin < 2) {
if (margin < 2 || (tstate->ob_dealloc_depth > 0 && should_randomly_deposit_object(tstate))) {
_PyTrash_thread_deposit_object(tstate, (PyObject *)op);
return;
}
Expand All @@ -3192,7 +3202,9 @@ _Py_Dealloc(PyObject *op)
_Py_ForgetReference(op);
#endif
_PyReftracerTrack(op, PyRefTracer_DESTROY);
tstate->ob_dealloc_depth++;
(*dealloc)(op);
tstate->ob_dealloc_depth--;

#ifdef Py_DEBUG
// gh-89373: The tp_dealloc function must leave the current exception
Expand Down
4 changes: 4 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,10 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->state = _Py_THREAD_SUSPENDED;
}

PyTime_t now;
PyTime_MonotonicRaw(&now);
tstate->prng = (uint64_t)now;

tstate->_status.initialized = 1;
}

Expand Down
Loading