From 8a1feb3c701596f4a8570ab7f04fa5525550cbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Sun, 13 Sep 2026 00:37:18 +0200 Subject: [PATCH] compat/atomics: store pointers through the exchange on MSVC for ARM before 19.44 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The alias analysis of MSVC for ARM and ARM64 before 19.44 does not track a pointer that is converted to an integer for the __iso_volatile_store and __stlr intrinsics, so the stores initializing the pointed-to object are eliminated as dead. Storing the address of an automatic array into an atomic pointer and reading through it afterwards then reads uninitialized stack. The stdatomic.h of /experimental:c11atomics generates the same code, 19.44 is the first version without the bug. The interlocked exchange is not affected, and with its result unused it compiles to the same store as a plain store would without the bug. Signed-off-by: Kacper Michajłow --- compat/atomics/stdatomic.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compat/atomics/stdatomic.h b/compat/atomics/stdatomic.h index 71bc52c430..12481dcde4 100644 --- a/compat/atomics/stdatomic.h +++ b/compat/atomics/stdatomic.h @@ -154,7 +154,17 @@ static av_always_inline void ff_atomic_store_ptr(volatile void *object, const volatile void *desired, memory_order order) { +/* The alias analysis of MSVC for ARM before 19.44 does not track a pointer + * converted to an integer for the store intrinsics, so the stores initializing + * the pointed-to object are eliminated as dead. The interlocked exchange is not + * affected, and with its result unused it compiles to the same store as a plain + * store would without the bug. */ +#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1944 && \ + (defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC)) + FF_ATOMIC_SIZE(exchange, sizeof(void *))(object, (uintptr_t)desired, order); +#else FF_ATOMIC_SIZE(store, sizeof(void *))(object, (uintptr_t)desired, order); +#endif } static av_always_inline uintptr_t