faster-cpython / ideas

1.67k stars 49 forks source link

Merge checks for Python recursion limit and stack buffer overflow. #620

Open markshannon opened 10 months ago

markshannon commented 10 months ago

On making a call to a Python function we need to check if there is room for the frame in the frame buffer (_PyThreadState_HasStackSpace) and check the Python recursion limit(_Py_EnterRecursivePy).

The first checks tstate->datastack_top != NULL && size < tstate->datastack_limit - tstate->datastack_top and the second that tstate->py_recursion_remaining > 1.

Given that frames have a minimum size, we can set create a tstate->slots_remaining value, such that slots_remaining <= tstate->datastack_limit - tstate->datastack_top and slots_remaining <= py_recursion_remaining * FRAME_SPECIALS_SIZE. We set slots_remaining to -1 if the datastack is NULL.

Then the check size < tstate->slots_remaining would suffice to check that the data stack is not NULL, for data stack overflow and for the recursion limit all in a single check.

This is unlikely to make much difference overall, but it streamlines calls a bit.

markshannon commented 9 months ago

(Closed by mistake)