From 1f697876aff14a65061d7cd21639db69cd5da99e Mon Sep 17 00:00:00 2001 From: Christian Aurich Date: Thu, 10 Sep 2026 02:39:22 -0300 Subject: [PATCH] gh-157212: Fix _Py_ThreadId() on Windows ARM64 with MinGW The __MINGW32__ && _M_ARM64 branch calls __getReg(18), mirroring the MSVC branch above it. The intrinsic is not available for MinGW ARM64: mingw-w64 declares __getReg only for Itanium, and GCC does not provide a corresponding builtin. The branch is reached by AArch64 MinGW toolchains because mingw-w64 defines _M_ARM64 when __aarch64__ is defined. Falling through to the generic __aarch64__ branch would not provide the Windows thread pointer. The Windows ARM64 ABI reserves x18 as the platform register pointing to the TEB, so read x18 directly. --- Include/cpython/object.h | 3 ++- .../Windows/2026-09-10-02-00-00.gh-issue-157212.x18TEB.rst | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Windows/2026-09-10-02-00-00.gh-issue-157212.x18TEB.rst diff --git a/Include/cpython/object.h b/Include/cpython/object.h index 4c5a677e5543ece..a77ab09f6fa8f0a 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -523,7 +523,8 @@ _Py_ThreadId(void) #elif defined(__MINGW32__) && defined(_M_IX86) tid = __readfsdword(24); #elif defined(__MINGW32__) && defined(_M_ARM64) - tid = __getReg(18); + // x18 is the Windows ARM64 platform register and points to the TEB. + __asm__ ("mov %0, x18" : "=r" (tid)); #elif defined(__i386__) __asm__("{movl %%gs:0, %0|mov %0, dword ptr gs:[0]}" : "=r" (tid)); // 32-bit always uses GS #elif defined(__MACH__) && defined(__x86_64__) diff --git a/Misc/NEWS.d/next/Windows/2026-09-10-02-00-00.gh-issue-157212.x18TEB.rst b/Misc/NEWS.d/next/Windows/2026-09-10-02-00-00.gh-issue-157212.x18TEB.rst new file mode 100644 index 000000000000000..8d0970516242f93 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-09-10-02-00-00.gh-issue-157212.x18TEB.rst @@ -0,0 +1,3 @@ +Fix ``_Py_ThreadId()`` compilation on Windows ARM64 with MinGW toolchains, +where ``__getReg()`` is unavailable. The TEB address is now read directly +from ``x18``. Patch by Christian Aurich.