Skip to content

Commit 89f9618

Browse files
committed
Use std::atomic_ref in Atomics.h's free standing functions
rdar://190462472 Reviewed by Yusuke Suzuki. The current code is technically UB, std::atomic_ref covers the same use case without any UB. It seems like memcpyAtomic didn't verify the destination was aligned, which, in general, can make the store non-atomic. None of the users were doing it in that way, except where atomicity didn't actually matter. i.e. on the same thread. No new tests, no behavior change. Covered by existing tests. Canonical link: https://commits.webkit.org/324276@main
1 parent 2adcfb1 commit 89f9618

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

‎Source/JavaScriptCore/assembler/AssemblerCommon.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,10 @@ WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
664664

665665
ALWAYS_INLINE void* memcpyAtomic(void* dst, const void* src, size_t n)
666666
{
667-
// This produces a much nicer error message for unaligned accesses.
667+
// Note: The `opaque` here matters because the compiler can backwards propagate the alignment from the
668+
// cast when n is constant. e.g. if `n == 4`, we cast `dst` to `uint32_t*`, so Clang can assume it's
669+
// already 4 byte aligned and remove the check.
670+
ASSERT(isPowerOfTwo(n) && WTF::isAligned(WTF::opaque(dst), n));
668671
switch (n) {
669672
case 1:
670673
WTF::atomicStore(std::bit_cast<uint8_t*>(dst), *std::bit_cast<const uint8_t*>(src), std::memory_order_relaxed);
@@ -701,7 +704,7 @@ ALWAYS_INLINE void* memcpyTearing(void* dst, const void* src, size_t n)
701704

702705
static ALWAYS_INLINE void* memcpyAtomicIfPossible(void* dst, const void* src, size_t n)
703706
{
704-
if (isPowerOfTwo(n) && n <= sizeof(CPURegister))
707+
if (isPowerOfTwo(n) && n <= sizeof(CPURegister) && WTF::isAligned(dst, n))
705708
return memcpyAtomic(dst, src, n);
706709
return memcpyTearing(dst, src, n);
707710
}

‎Source/WTF/wtf/Atomics.h‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,79 +159,82 @@ struct Atomic {
159159
template<typename T>
160160
inline T atomicLoad(T* location, std::memory_order order = std::memory_order_seq_cst)
161161
{
162-
return std::bit_cast<Atomic<T>*>(location)->load(order);
162+
return std::atomic_ref<T>(*location).load(order);
163163
}
164164

165165
template<typename T>
166166
inline T atomicLoadFullyFenced(T* location)
167167
{
168-
return std::bit_cast<Atomic<T>*>(location)->loadFullyFenced();
168+
return std::atomic_ref<T>(*location).fetch_add(T());
169169
}
170170

171171
template<typename T>
172172
inline void atomicStore(T* location, T newValue, std::memory_order order = std::memory_order_seq_cst)
173173
{
174-
std::bit_cast<Atomic<T>*>(location)->store(newValue, order);
174+
std::atomic_ref<T>(*location).store(newValue, order);
175175
}
176176

177177
template<typename T>
178178
inline void atomicStoreFullyFenced(T* location, T newValue)
179179
{
180-
std::bit_cast<Atomic<T>*>(location)->storeFullyFenced(newValue);
180+
std::atomic_ref<T>(*location).exchange(newValue);
181181
}
182182

183183
template<typename T>
184184
inline bool atomicCompareExchangeWeak(T* location, T expected, T newValue, std::memory_order order = std::memory_order_seq_cst)
185185
{
186-
return std::bit_cast<Atomic<T>*>(location)->compareExchangeWeak(expected, newValue, order);
186+
T expectedOrActual = expected;
187+
return std::atomic_ref<T>(*location).compare_exchange_weak(expectedOrActual, newValue, order);
187188
}
188189

189190
template<typename T>
190191
inline bool atomicCompareExchangeWeakRelaxed(T* location, T expected, T newValue)
191192
{
192-
return std::bit_cast<Atomic<T>*>(location)->compareExchangeWeakRelaxed(expected, newValue);
193+
return atomicCompareExchangeWeak(location, expected, newValue, std::memory_order_relaxed);
193194
}
194195

195196
template<typename T>
196197
inline T atomicCompareExchangeStrong(T* location, T expected, T newValue, std::memory_order order = std::memory_order_seq_cst)
197198
{
198-
return std::bit_cast<Atomic<T>*>(location)->compareExchangeStrong(expected, newValue, order);
199+
T expectedOrActual = expected;
200+
std::atomic_ref<T>(*location).compare_exchange_strong(expectedOrActual, newValue, order);
201+
return expectedOrActual;
199202
}
200203

201204
template<typename T, typename U>
202205
inline T atomicExchangeAdd(T* location, U operand, std::memory_order order = std::memory_order_seq_cst)
203206
{
204-
return std::bit_cast<Atomic<T>*>(location)->exchangeAdd(operand, order);
207+
return std::atomic_ref<T>(*location).fetch_add(operand, order);
205208
}
206209

207210
template<typename T, typename U>
208211
inline T atomicExchangeAnd(T* location, U operand, std::memory_order order = std::memory_order_seq_cst)
209212
{
210-
return std::bit_cast<Atomic<T>*>(location)->exchangeAnd(operand, order);
213+
return std::atomic_ref<T>(*location).fetch_and(operand, order);
211214
}
212215

213216
template<typename T, typename U>
214217
inline T atomicExchangeOr(T* location, U operand, std::memory_order order = std::memory_order_seq_cst)
215218
{
216-
return std::bit_cast<Atomic<T>*>(location)->exchangeOr(operand, order);
219+
return std::atomic_ref<T>(*location).fetch_or(operand, order);
217220
}
218221

219222
template<typename T, typename U>
220223
inline T atomicExchangeSub(T* location, U operand, std::memory_order order = std::memory_order_seq_cst)
221224
{
222-
return std::bit_cast<Atomic<T>*>(location)->exchangeSub(operand, order);
225+
return std::atomic_ref<T>(*location).fetch_sub(operand, order);
223226
}
224227

225228
template<typename T, typename U>
226229
inline T atomicExchangeXor(T* location, U operand, std::memory_order order = std::memory_order_seq_cst)
227230
{
228-
return std::bit_cast<Atomic<T>*>(location)->exchangeXor(operand, order);
231+
return std::atomic_ref<T>(*location).fetch_xor(operand, order);
229232
}
230233

231234
template<typename T>
232235
inline T atomicExchange(T* location, T newValue, std::memory_order order = std::memory_order_seq_cst)
233236
{
234-
return std::bit_cast<Atomic<T>*>(location)->exchange(newValue, order);
237+
return std::atomic_ref<T>(*location).exchange(newValue, order);
235238
}
236239

237240
// Just a compiler fence. Has no effect on the hardware, but tells the compiler

0 commit comments

Comments
 (0)