From a0e7fee6dd4c400c0e6780bca5b37395c9bc6fd2 Mon Sep 17 00:00:00 2001 From: Diego Russo Date: Fri, 27 Mar 2026 13:05:30 +0000 Subject: [PATCH 1/2] GH-126910: avoid reading the FP for getting the SP --- Include/internal/pycore_pystate.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 189a8dde9f09ed..1a20a794beba8b 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -306,23 +306,15 @@ _Py_AssertHoldsTstateFunc(const char *func) #define _Py_AssertHoldsTstate() #endif -#if !_Py__has_builtin(__builtin_frame_address) && !defined(__GNUC__) && !defined(_MSC_VER) static uintptr_t return_pointer_as_int(char* p) { return (uintptr_t)p; } -#endif static inline uintptr_t _Py_get_machine_stack_pointer(void) { -#if _Py__has_builtin(__builtin_frame_address) || defined(__GNUC__) - return (uintptr_t)__builtin_frame_address(0); -#elif defined(_MSC_VER) - return (uintptr_t)_AddressOfReturnAddress(); -#else char here; /* Avoid compiler warning about returning stack address */ return return_pointer_as_int(&here); -#endif } static inline intptr_t From dcee86740f53044050047fb0dab8028433ce43d9 Mon Sep 17 00:00:00 2001 From: Diego Russo Date: Fri, 27 Mar 2026 17:00:57 +0000 Subject: [PATCH 2/2] Address feedback --- Include/internal/pycore_pystate.h | 9 +++------ Python/pystate.c | 13 +++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 1a20a794beba8b..a66543cf1eb164 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -306,16 +306,13 @@ _Py_AssertHoldsTstateFunc(const char *func) #define _Py_AssertHoldsTstate() #endif +#if !_Py__has_builtin(__builtin_frame_address) && !defined(__GNUC__) && !defined(_MSC_VER) static uintptr_t return_pointer_as_int(char* p) { return (uintptr_t)p; } +#endif -static inline uintptr_t -_Py_get_machine_stack_pointer(void) { - char here; - /* Avoid compiler warning about returning stack address */ - return return_pointer_as_int(&here); -} +PyAPI_DATA(uintptr_t) _Py_get_machine_stack_pointer(void); static inline intptr_t _Py_RecursionLimit_GetMargin(PyThreadState *tstate) diff --git a/Python/pystate.c b/Python/pystate.c index 143175da0f45c7..f974c82c391f6a 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -3286,3 +3286,16 @@ _Py_GetMainConfig(void) } return _PyInterpreterState_GetConfig(interp); } + +uintptr_t +_Py_get_machine_stack_pointer(void) { +#if _Py__has_builtin(__builtin_frame_address) || defined(__GNUC__) + return (uintptr_t)__builtin_frame_address(0); +#elif defined(_MSC_VER) + return (uintptr_t)_AddressOfReturnAddress(); +#else + char here; + /* Avoid compiler warning about returning stack address */ + return return_pointer_as_int(&here); +#endif +}