@@ -440,8 +440,8 @@ int pthread_attr_destroy(pthread_attr_t *a)
440440
441441
442442void
443- PyUnstable_ThreadState_SetStack (PyThreadState * tstate ,
444- void * stack_start_addr , size_t stack_size )
443+ tstate_set_stack (PyThreadState * tstate ,
444+ void * stack_start_addr , size_t stack_size )
445445{
446446 assert (stack_size > 0 );
447447
@@ -473,6 +473,21 @@ PyUnstable_ThreadState_SetStack(PyThreadState *tstate,
473473}
474474
475475
476+ int
477+ PyUnstable_ThreadState_SetStack (PyThreadState * tstate ,
478+ void * stack_start_addr , size_t stack_size )
479+ {
480+ if (stack_size == 0 ) {
481+ PyErr_SetString (PyExc_ValueError , "stack_size must be greater than 0" );
482+ return -1 ;
483+ }
484+
485+ tstate_set_stack (tstate , stack_start_addr , stack_size );
486+ return 0 ;
487+ }
488+
489+
490+
476491void
477492PyUnstable_ThreadState_ResetStack (PyThreadState * tstate )
478493{
@@ -485,13 +500,13 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
485500
486501 uintptr_t start = (uintptr_t )low + guarantee + _PyOS_STACK_MARGIN_BYTES ;
487502 size_t size = (uintptr_t )high - start ;
488- PyUnstable_ThreadState_SetStack (tstate , (void * )start , size );
503+ tstate_set_stack (tstate , (void * )start , size );
489504
490505#elif defined(__APPLE__ )
491506 pthread_t this_thread = pthread_self ();
492507 void * top = pthread_get_stackaddr_np (this_thread ); // top of the stack
493508 size_t size = pthread_get_stacksize_np (this_thread );
494- PyUnstable_ThreadState_SetStack (tstate , (char * )top - size , size );
509+ tstate_set_stack (tstate , (char * )top - size , size );
495510
496511#else
497512 // XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size
@@ -512,7 +527,7 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
512527 uintptr_t base = ((uintptr_t )stack_addr ) + guard_size ;
513528 uintptr_t start = base + _PyOS_STACK_MARGIN_BYTES ;
514529 size_t pystack_size = (base + stack_size ) - start ;
515- PyUnstable_ThreadState_SetStack (tstate , (void * )start , pystack_size );
530+ tstate_set_stack (tstate , (void * )start , pystack_size );
516531 }
517532 else
518533# endif
@@ -521,7 +536,7 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
521536 uintptr_t top = _Py_SIZE_ROUND_UP (here_addr , 4096 );
522537 uintptr_t start = top - (Py_C_STACK_SIZE + _PyOS_STACK_MARGIN_BYTES );
523538 size_t pystack_size = top - start ;
524- PyUnstable_ThreadState_SetStack (tstate , (void * )start , pystack_size );
539+ tstate_set_stack (tstate , (void * )start , pystack_size );
525540 }
526541#endif
527542}
0 commit comments