Skip to content

Commit 1f69787

Browse files
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.
1 parent f715d25 commit 1f69787

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

Include/cpython/object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ _Py_ThreadId(void)
523523
#elif defined(__MINGW32__) && defined(_M_IX86)
524524
tid = __readfsdword(24);
525525
#elif defined(__MINGW32__) && defined(_M_ARM64)
526-
tid = __getReg(18);
526+
// x18 is the Windows ARM64 platform register and points to the TEB.
527+
__asm__ ("mov %0, x18" : "=r" (tid));
527528
#elif defined(__i386__)
528529
__asm__("{movl %%gs:0, %0|mov %0, dword ptr gs:[0]}" : "=r" (tid)); // 32-bit always uses GS
529530
#elif defined(__MACH__) && defined(__x86_64__)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``_Py_ThreadId()`` compilation on Windows ARM64 with MinGW toolchains,
2+
where ``__getReg()`` is unavailable. The TEB address is now read directly
3+
from ``x18``. Patch by Christian Aurich.

0 commit comments

Comments
 (0)