Skip to content

gh-157212: Fix _Py_ThreadId() on Windows ARM64 with MinGW - #157253

Merged
corona10 merged 1 commit into
python:mainfrom
christianaurichzm:fix-threadid-mingw-arm64
Sep 25, 2026
Merged

corona10 merged 1 commit into
python:mainfrom
christianaurichzm:fix-threadid-mingw-arm64

Conversation

@christianaurichzm

Copy link
Copy Markdown
Contributor

Fixes #157212

_Py_ThreadId() currently uses __getReg(18) for the MinGW ARM64 branch:

#elif defined(__MINGW32__) && defined(_M_ARM64)
    tid = __getReg(18);

That intrinsic is not available for MinGW ARM64. For a *-windows-gnu target, Clang's intrin.h defers to mingw-w64's header, which declares __getReg only for Itanium. GCC does not provide a corresponding builtin either.

The branch is reached by AArch64 MinGW toolchains because mingw-w64 defines _M_ARM64 when __aarch64__ is defined.

Falling through to the generic __aarch64__ implementation is not a valid alternative. The Windows ARM64 ABI reserves x18 as the platform register pointing to the Thread Environment Block (TEB). In the system tested in gh-157212, x18 also matched NtCurrentTeb() for each tested thread, while tpidr_el0 was observed as zero.

This change therefore reads the TEB address directly from x18 for MinGW
ARM64:

__asm__ ("mov %0, x18" : "=r" (tid));

The MSVC branch is unchanged.

Verification

I reproduced the original compile failure with zig cc/Clang targeting
aarch64-windows-gnu:

error: call to undeclared function '__getReg'; ISO C99 and later do not
support implicit function declarations [-Wimplicit-function-declaration]

With this change, the same target compiles successfully and at -O2 emits the expected register read:

mov     x0, x18
ret

The change is limited to the MinGW ARM64 branch.

AI tools were used to help organize and edit the wording of this PR description.

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.
@kumaraditya303

Copy link
Copy Markdown
Contributor

I think _Py_get_machine_stack_pointer has the same issue on ARM64 Windows MinGW.

@corona10 corona10 self-assigned this Sep 25, 2026

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

https://godbolt.org/z/sj97f6TEr

I've double checked with godbolt code gen :)

@corona10 corona10 added awaiting review needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes and removed awaiting merge labels Sep 25, 2026
@corona10
corona10 enabled auto-merge (squash) September 25, 2026 13:03
@corona10
corona10 merged commit c1af94a into python:main Sep 25, 2026
70 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @christianaurichzm for the PR, and @corona10 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @christianaurichzm and @corona10, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker c1af94ae348da826ac530c8d5097fa887e19579c 3.14

@miss-islington-app

Copy link
Copy Markdown

Sorry, @christianaurichzm and @corona10, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker c1af94ae348da826ac530c8d5097fa887e19579c 3.13

@bedevere-app

bedevere-app Bot commented Sep 25, 2026

Copy link
Copy Markdown

GH-158164 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Sep 25, 2026
hugovk pushed a commit that referenced this pull request Sep 26, 2026
…157253) (#158164)

Co-authored-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@StanFromIreland StanFromIreland removed the needs backport to 3.13 only security fixes label Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs backport to 3.14 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_Py_ThreadId() fails to compile with clang on Windows ARM64 (MSYS2 CLANGARM64)

4 participants