@@ -351,21 +351,33 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count)
351351{
352352 uintptr_t here_addr = _Py_get_machine_stack_pointer ();
353353 _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
354+ #if _Py_STACK_GROWS_DOWN
354355 if (here_addr > _tstate -> c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES ) {
356+ #else
357+ if (here_addr <= _tstate -> c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES ) {
358+ #endif
355359 return 0 ;
356360 }
357361 if (_tstate -> c_stack_hard_limit == 0 ) {
358362 _Py_InitializeRecursionLimits (tstate );
359363 }
364+ #if _Py_STACK_GROWS_DOWN
360365 return here_addr <= _tstate -> c_stack_soft_limit + margin_count * _PyOS_STACK_MARGIN_BYTES ;
366+ #else
367+ return here_addr > _tstate -> c_stack_soft_limit - margin_count * _PyOS_STACK_MARGIN_BYTES ;
368+ #endif
361369}
362370
363371void
364372_Py_EnterRecursiveCallUnchecked (PyThreadState * tstate )
365373{
366374 uintptr_t here_addr = _Py_get_machine_stack_pointer ();
367375 _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
376+ #if _Py_STACK_GROWS_DOWN
368377 if (here_addr < _tstate -> c_stack_hard_limit ) {
378+ #else
379+ if (here_addr > _tstate -> c_stack_hard_limit ) {
380+ #endif
369381 Py_FatalError ("Unchecked stack overflow." );
370382 }
371383}
@@ -496,18 +508,33 @@ tstate_set_stack(PyThreadState *tstate,
496508#ifdef _Py_THREAD_SANITIZER
497509 // Thread sanitizer crashes if we use more than half the stack.
498510 uintptr_t stacksize = top - base ;
499- base += stacksize / 2 ;
511+ # if _Py_STACK_GROWS_DOWN
512+ base += stacksize /2 ;
513+ # else
514+ top -= stacksize /2 ;
515+ # endif
500516#endif
501517 _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
518+ #if _Py_STACK_GROWS_DOWN
502519 _tstate -> c_stack_top = top ;
503520 _tstate -> c_stack_hard_limit = base + _PyOS_STACK_MARGIN_BYTES ;
504521 _tstate -> c_stack_soft_limit = base + _PyOS_STACK_MARGIN_BYTES * 2 ;
505-
506- #ifndef NDEBUG
522+ # ifndef NDEBUG
507523 // Sanity checks
508524 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )tstate ;
509525 assert (ts -> c_stack_hard_limit <= ts -> c_stack_soft_limit );
510526 assert (ts -> c_stack_soft_limit < ts -> c_stack_top );
527+ # endif
528+ #else
529+ _tstate -> c_stack_top = base ;
530+ _tstate -> c_stack_hard_limit = top - _PyOS_STACK_MARGIN_BYTES ;
531+ _tstate -> c_stack_soft_limit = top - _PyOS_STACK_MARGIN_BYTES * 2 ;
532+ # ifndef NDEBUG
533+ // Sanity checks
534+ _PyThreadStateImpl * ts = (_PyThreadStateImpl * )tstate ;
535+ assert (ts -> c_stack_hard_limit >= ts -> c_stack_soft_limit );
536+ assert (ts -> c_stack_soft_limit > ts -> c_stack_top );
537+ # endif
511538#endif
512539}
513540
@@ -568,9 +595,15 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
568595 uintptr_t here_addr = _Py_get_machine_stack_pointer ();
569596 assert (_tstate -> c_stack_soft_limit != 0 );
570597 assert (_tstate -> c_stack_hard_limit != 0 );
598+ #if _Py_STACK_GROWS_DOWN
571599 if (here_addr < _tstate -> c_stack_hard_limit ) {
572600 /* Overflowing while handling an overflow. Give up. */
573601 int kbytes_used = (int )(_tstate -> c_stack_top - here_addr )/1024 ;
602+ #else
603+ if (here_addr > _tstate -> c_stack_hard_limit ) {
604+ /* Overflowing while handling an overflow. Give up. */
605+ int kbytes_used = (int )(here_addr - _tstate -> c_stack_top )/1024 ;
606+ #endif
574607 char buffer [80 ];
575608 snprintf (buffer , 80 , "Unrecoverable stack overflow (used %d kB)%s" , kbytes_used , where );
576609 Py_FatalError (buffer );
@@ -579,7 +612,11 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
579612 return 0 ;
580613 }
581614 else {
615+ #if _Py_STACK_GROWS_DOWN
582616 int kbytes_used = (int )(_tstate -> c_stack_top - here_addr )/1024 ;
617+ #else
618+ int kbytes_used = (int )(here_addr - _tstate -> c_stack_top )/1024 ;
619+ #endif
583620 tstate -> recursion_headroom ++ ;
584621 _PyErr_Format (tstate , PyExc_RecursionError ,
585622 "Stack overflow (used %d kB)%s" ,
0 commit comments