@@ -333,21 +333,33 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count)
333333{
334334 uintptr_t here_addr = _Py_get_machine_stack_pointer ();
335335 _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
336+ #if _Py_STACK_GROWS_DOWN
336337 if (here_addr > _tstate -> c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES ) {
338+ #else
339+ if (here_addr <= _tstate -> c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES ) {
340+ #endif
337341 return 0 ;
338342 }
339343 if (_tstate -> c_stack_hard_limit == 0 ) {
340344 _Py_InitializeRecursionLimits (tstate );
341345 }
346+ #if _Py_STACK_GROWS_DOWN
342347 return here_addr <= _tstate -> c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES ;
348+ #else
349+ return here_addr > _tstate -> c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES ;
350+ #endif
343351}
344352
345353void
346354_Py_EnterRecursiveCallUnchecked (PyThreadState * tstate )
347355{
348356 uintptr_t here_addr = _Py_get_machine_stack_pointer ();
349357 _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
358+ #if _Py_STACK_GROWS_DOWN
350359 if (here_addr < _tstate -> c_stack_hard_limit ) {
360+ #else
361+ if (here_addr > _tstate -> c_stack_hard_limit ) {
362+ #endif
351363 Py_FatalError ("Unchecked stack overflow." );
352364 }
353365}
@@ -455,16 +467,28 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
455467 }
456468 if (err == 0 ) {
457469 uintptr_t base = ((uintptr_t )stack_addr ) + guard_size ;
458- _tstate -> c_stack_top = base + stack_size ;
459- #ifdef _Py_THREAD_SANITIZER
470+ uintptr_t top = base + stack_size ;
471+ # ifdef _Py_THREAD_SANITIZER
460472 // Thread sanitizer crashes if we use a bit more than half the stack.
461- _tstate -> c_stack_soft_limit = base + (stack_size / 2 );
462- #else
463- _tstate -> c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2 ;
464- #endif
473+ # if _Py_STACK_GROWS_DOWN
474+ base += stack_size / 2 ;
475+ # else
476+ top -= stack_size / 2 ;
477+ # endif
478+ # endif
479+ # if _Py_STACK_GROWS_DOWN
480+ _tstate -> c_stack_top = top ;
465481 _tstate -> c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES ;
482+ _tstate -> c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2 ;
466483 assert (_tstate -> c_stack_soft_limit < here_addr );
467484 assert (here_addr < _tstate -> c_stack_top );
485+ # else
486+ _tstate -> c_stack_top = base ;
487+ _tstate -> c_stack_hard_limit = top - _PyOS_STACK_MARGIN_BYTES ;
488+ _tstate -> c_stack_soft_limit = top - _PyOS_STACK_MARGIN_BYTES * 2 ;
489+ assert (here_addr > base );
490+ assert (here_addr < _tstate -> c_stack_soft_limit );
491+ # endif
468492 return ;
469493 }
470494# endif
@@ -483,9 +507,15 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
483507 uintptr_t here_addr = _Py_get_machine_stack_pointer ();
484508 assert (_tstate -> c_stack_soft_limit != 0 );
485509 assert (_tstate -> c_stack_hard_limit != 0 );
510+ #if _Py_STACK_GROWS_DOWN
486511 if (here_addr < _tstate -> c_stack_hard_limit ) {
487512 /* Overflowing while handling an overflow. Give up. */
488513 int kbytes_used = (int )(_tstate -> c_stack_top - here_addr )/1024 ;
514+ #else
515+ if (here_addr > _tstate -> c_stack_hard_limit ) {
516+ /* Overflowing while handling an overflow. Give up. */
517+ int kbytes_used = (int )(here_addr - _tstate -> c_stack_top )/1024 ;
518+ #endif
489519 char buffer [80 ];
490520 snprintf (buffer , 80 , "Unrecoverable stack overflow (used %d kB)%s" , kbytes_used , where );
491521 Py_FatalError (buffer );
@@ -494,7 +524,11 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
494524 return 0 ;
495525 }
496526 else {
527+ #if _Py_STACK_GROWS_DOWN
497528 int kbytes_used = (int )(_tstate -> c_stack_top - here_addr )/1024 ;
529+ #else
530+ int kbytes_used = (int )(here_addr - _tstate -> c_stack_top )/1024 ;
531+ #endif
498532 tstate -> recursion_headroom ++ ;
499533 _PyErr_Format (tstate , PyExc_RecursionError ,
500534 "Stack overflow (used %d kB)%s" ,
0 commit comments