@@ -440,8 +440,8 @@ int pthread_attr_destroy(pthread_attr_t *a)
440
440
441
441
442
442
void
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 )
445
445
{
446
446
assert (stack_size > 0 );
447
447
@@ -473,6 +473,21 @@ PyUnstable_ThreadState_SetStack(PyThreadState *tstate,
473
473
}
474
474
475
475
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
+
476
491
void
477
492
PyUnstable_ThreadState_ResetStack (PyThreadState * tstate )
478
493
{
@@ -485,13 +500,13 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
485
500
486
501
uintptr_t start = (uintptr_t )low + guarantee + _PyOS_STACK_MARGIN_BYTES ;
487
502
size_t size = (uintptr_t )high - start ;
488
- PyUnstable_ThreadState_SetStack (tstate , (void * )start , size );
503
+ tstate_set_stack (tstate , (void * )start , size );
489
504
490
505
#elif defined(__APPLE__ )
491
506
pthread_t this_thread = pthread_self ();
492
507
void * top = pthread_get_stackaddr_np (this_thread ); // top of the stack
493
508
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 );
495
510
496
511
#else
497
512
// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size
@@ -512,7 +527,7 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
512
527
uintptr_t base = ((uintptr_t )stack_addr ) + guard_size ;
513
528
uintptr_t start = base + _PyOS_STACK_MARGIN_BYTES ;
514
529
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 );
516
531
}
517
532
else
518
533
# endif
@@ -521,7 +536,7 @@ PyUnstable_ThreadState_ResetStack(PyThreadState *tstate)
521
536
uintptr_t top = _Py_SIZE_ROUND_UP (here_addr , 4096 );
522
537
uintptr_t start = top - (Py_C_STACK_SIZE + _PyOS_STACK_MARGIN_BYTES );
523
538
size_t pystack_size = top - start ;
524
- PyUnstable_ThreadState_SetStack (tstate , (void * )start , pystack_size );
539
+ tstate_set_stack (tstate , (void * )start , pystack_size );
525
540
}
526
541
#endif
527
542
}
0 commit comments