[Python-Dev] stack check on Unix: any suggestions?

Skip Montanaro skip@mojam.com (Skip Montanaro)
Tue, 29 Aug 2000 15:12:57 -0500 (CDT)


    Jeremy> Does anyone have suggestions for how to detect unbounded
    Jeremy> recursion in the Python core on Unix platforms?

On most (all?) processors in common usage, the stack grows down toward the
heap and the heap brows upward, so what you really want to do is detect that
collision.  brk and sbrk are used to manipulate the end of the heap.  A
local variable in the current scope should be able to tell you roughly where
the top of stack is.

Of course, you really can't call brk or sbrk safely.  You have to leave that
to malloc.  You might get some ideas of how to estimate the current end of
the heap by peering at the GNU malloc code.

This might be a good reason to experiment with Vladimir's obmalloc package.
It could easily be modified to remember the largest machine address it
returns via malloc or realloc calls.  That value could be compared with the
current top of stack.  If obmalloc brks() memory back to the system (I've
never looked at it - I'm just guessing) it could lower the saved value to
the last address in the block below the just recycled block.

(I agree this probably won't be done very well before 2.0 release.)

Skip