From bdcff0aa290faad6adb3f5be1c6a4552e3b3b74b Mon Sep 17 00:00:00 2001 From: EgorBo Date: Tue, 23 Jun 2026 15:43:40 +0200 Subject: [PATCH] Fix alignment of 8-byte thread statics on direct TLS path GetTLSIndexForThreadStatic computes the alignment for a thread static placed on the direct-on-thread-local-data bump allocator. The chain of size checks was missing an `else` before the `>= 4` case, so an 8-byte field (long/double) first set alignment = 8 and then immediately overwrote it with alignment = 4. The resulting offset could land on a 4-mod-8 boundary, producing a misaligned long/double. On arm64 this causes Interlocked.Increment(ref threadStaticLong) to throw DataMisalignedException, since the atomic lowers to ldaxr/stlxr which fault on a misaligned address. Fixes #129733 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/coreclr/vm/threadstatics.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/vm/threadstatics.cpp b/src/coreclr/vm/threadstatics.cpp index 5847cc5c13bb47..e7246ab1db414f 100644 --- a/src/coreclr/vm/threadstatics.cpp +++ b/src/coreclr/vm/threadstatics.cpp @@ -731,7 +731,7 @@ void GetTLSIndexForThreadStatic(MethodTable* pMT, bool gcStatic, TLSIndex* pInde uint32_t alignment; if (bytesNeeded >= 8) alignment = 8; - if (bytesNeeded >= 4) + else if (bytesNeeded >= 4) alignment = 4; else if (bytesNeeded >= 2) alignment = 2;