[libcxx] r291976 - Diagnose invalid memory orderings in <atomic>
Eric Fiselier via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 13 15:45:39 PST 2017
Author: ericwf
Date: Fri Jan 13 17:45:39 2017
New Revision: 291976
URL: http://llvm.org/viewvc/llvm-project?rev=291976&view=rev
Log:
Diagnose invalid memory orderings in <atomic>
Added:
libcxx/trunk/test/libcxx/atomics/diagnose_invalid_memory_order.fail.cpp
Modified:
libcxx/trunk/include/__config
libcxx/trunk/include/atomic
Modified: libcxx/trunk/include/__config
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__config?rev=291976&r1=291975&r2=291976&view=diff
==============================================================================
--- libcxx/trunk/include/__config (original)
+++ libcxx/trunk/include/__config Fri Jan 13 17:45:39 2017
@@ -1008,9 +1008,9 @@ _LIBCPP_FUNC_VIS extern "C" void __sanit
#if __has_attribute(diagnose_if) && !defined(_LIBCPP_DISABLE_ADDITIONAL_DIAGNOSTICS)
# define _LIBCPP_DIAGNOSE_WARNING(...) \
- __attribute__((__diagnose_if__(__VA_ARGS__, "warning")))
+ __attribute__((diagnose_if(__VA_ARGS__, "warning")))
# define _LIBCPP_DIAGNOSE_ERROR(...) \
- __attribute__((__diagnose_if__(__VA_ARGS__, "error")))
+ __attribute__((diagnose_if(__VA_ARGS__, "error")))
#else
# define _LIBCPP_DIAGNOSE_WARNING(...)
# define _LIBCPP_DIAGNOSE_ERROR(...)
Modified: libcxx/trunk/include/atomic
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/atomic?rev=291976&r1=291975&r2=291976&view=diff
==============================================================================
--- libcxx/trunk/include/atomic (original)
+++ libcxx/trunk/include/atomic Fri Jan 13 17:45:39 2017
@@ -560,6 +560,22 @@ void atomic_signal_fence(memory_order m)
# define __cpp_lib_atomic_is_always_lock_free 201603L
#endif
+#define _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) \
+ _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_consume || \
+ __m == memory_order_acquire || \
+ __m == memory_order_acq_rel, \
+ "memory order argument to atomic operation is invalid")
+
+#define _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) \
+ _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_release || \
+ __m == memory_order_acq_rel, \
+ "memory order argument to atomic operation is invalid")
+
+#define _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__m, __f) \
+ _LIBCPP_DIAGNOSE_WARNING(__f == memory_order_release || \
+ __f == memory_order_acq_rel, \
+ "memory order argument to atomic operation is invalid")
+
_LIBCPP_BEGIN_NAMESPACE_STD
typedef enum memory_order
@@ -881,15 +897,19 @@ struct __atomic_base // false
{return static_cast<__atomic_base const volatile*>(this)->is_lock_free();}
_LIBCPP_INLINE_VISIBILITY
void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
+ _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
{__c11_atomic_store(&__a_, __d, __m);}
_LIBCPP_INLINE_VISIBILITY
void store(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT
+ _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
{__c11_atomic_store(&__a_, __d, __m);}
_LIBCPP_INLINE_VISIBILITY
_Tp load(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
+ _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
{return __c11_atomic_load(&__a_, __m);}
_LIBCPP_INLINE_VISIBILITY
_Tp load(memory_order __m = memory_order_seq_cst) const _NOEXCEPT
+ _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
{return __c11_atomic_load(&__a_, __m);}
_LIBCPP_INLINE_VISIBILITY
operator _Tp() const volatile _NOEXCEPT {return load();}
@@ -904,18 +924,22 @@ struct __atomic_base // false
_LIBCPP_INLINE_VISIBILITY
bool compare_exchange_weak(_Tp& __e, _Tp __d,
memory_order __s, memory_order __f) volatile _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{return __c11_atomic_compare_exchange_weak(&__a_, &__e, __d, __s, __f);}
_LIBCPP_INLINE_VISIBILITY
bool compare_exchange_weak(_Tp& __e, _Tp __d,
memory_order __s, memory_order __f) _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{return __c11_atomic_compare_exchange_weak(&__a_, &__e, __d, __s, __f);}
_LIBCPP_INLINE_VISIBILITY
bool compare_exchange_strong(_Tp& __e, _Tp __d,
memory_order __s, memory_order __f) volatile _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{return __c11_atomic_compare_exchange_strong(&__a_, &__e, __d, __s, __f);}
_LIBCPP_INLINE_VISIBILITY
bool compare_exchange_strong(_Tp& __e, _Tp __d,
memory_order __s, memory_order __f) _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{return __c11_atomic_compare_exchange_strong(&__a_, &__e, __d, __s, __f);}
_LIBCPP_INLINE_VISIBILITY
bool compare_exchange_weak(_Tp& __e, _Tp __d,
@@ -1181,6 +1205,7 @@ template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
void
atomic_store_explicit(volatile atomic<_Tp>* __o, _Tp __d, memory_order __m) _NOEXCEPT
+ _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
{
__o->store(__d, __m);
}
@@ -1189,6 +1214,7 @@ template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
void
atomic_store_explicit(atomic<_Tp>* __o, _Tp __d, memory_order __m) _NOEXCEPT
+ _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
{
__o->store(__d, __m);
}
@@ -1217,6 +1243,7 @@ template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_Tp
atomic_load_explicit(const volatile atomic<_Tp>* __o, memory_order __m) _NOEXCEPT
+ _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
{
return __o->load(__m);
}
@@ -1225,6 +1252,7 @@ template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_Tp
atomic_load_explicit(const atomic<_Tp>* __o, memory_order __m) _NOEXCEPT
+ _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
{
return __o->load(__m);
}
@@ -1309,6 +1337,7 @@ bool
atomic_compare_exchange_weak_explicit(volatile atomic<_Tp>* __o, _Tp* __e,
_Tp __d,
memory_order __s, memory_order __f) _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{
return __o->compare_exchange_weak(*__e, __d, __s, __f);
}
@@ -1318,6 +1347,7 @@ inline _LIBCPP_INLINE_VISIBILITY
bool
atomic_compare_exchange_weak_explicit(atomic<_Tp>* __o, _Tp* __e, _Tp __d,
memory_order __s, memory_order __f) _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{
return __o->compare_exchange_weak(*__e, __d, __s, __f);
}
@@ -1330,6 +1360,7 @@ bool
atomic_compare_exchange_strong_explicit(volatile atomic<_Tp>* __o,
_Tp* __e, _Tp __d,
memory_order __s, memory_order __f) _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{
return __o->compare_exchange_strong(*__e, __d, __s, __f);
}
@@ -1340,6 +1371,7 @@ bool
atomic_compare_exchange_strong_explicit(atomic<_Tp>* __o, _Tp* __e,
_Tp __d,
memory_order __s, memory_order __f) _NOEXCEPT
+ _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
{
return __o->compare_exchange_strong(*__e, __d, __s, __f);
}
Added: libcxx/trunk/test/libcxx/atomics/diagnose_invalid_memory_order.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/atomics/diagnose_invalid_memory_order.fail.cpp?rev=291976&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/atomics/diagnose_invalid_memory_order.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/atomics/diagnose_invalid_memory_order.fail.cpp Fri Jan 13 17:45:39 2017
@@ -0,0 +1,124 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: verify-support, diagnose-if-support
+// UNSUPPORTED: libcpp-has-no-threads
+
+// <atomic>
+
+// Test that invalid memory order arguments are diagnosed where possible.
+
+#include <atomic>
+
+int main() {
+ std::atomic<int> x(42);
+ volatile std::atomic<int>& vx = x;
+ int val1 = 1; ((void)val1);
+ int val2 = 2; ((void)val2);
+ // load operations
+ {
+ x.load(std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ x.load(std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.load(std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.load(std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ x.load(std::memory_order_relaxed);
+ x.load(std::memory_order_consume);
+ x.load(std::memory_order_acquire);
+ x.load(std::memory_order_seq_cst);
+ }
+ {
+ std::atomic_load_explicit(&x, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_load_explicit(&x, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_load_explicit(&vx, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_load_explicit(&vx, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ std::atomic_load_explicit(&x, std::memory_order_relaxed);
+ std::atomic_load_explicit(&x, std::memory_order_consume);
+ std::atomic_load_explicit(&x, std::memory_order_acquire);
+ std::atomic_load_explicit(&x, std::memory_order_seq_cst);
+ }
+ // store operations
+ {
+ x.store(42, std::memory_order_consume); // expected-warning {{memory order argument to atomic operation is invalid}}
+ x.store(42, std::memory_order_acquire); // expected-warning {{memory order argument to atomic operation is invalid}}
+ x.store(42, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.store(42, std::memory_order_consume); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.store(42, std::memory_order_acquire); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.store(42, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ x.store(42, std::memory_order_relaxed);
+ x.store(42, std::memory_order_release);
+ x.store(42, std::memory_order_seq_cst);
+ }
+ {
+ std::atomic_store_explicit(&x, 42, std::memory_order_consume); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_store_explicit(&x, 42, std::memory_order_acquire); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_store_explicit(&x, 42, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_store_explicit(&vx, 42, std::memory_order_consume); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_store_explicit(&vx, 42, std::memory_order_acquire); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_store_explicit(&vx, 42, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ std::atomic_store_explicit(&x, 42, std::memory_order_relaxed);
+ std::atomic_store_explicit(&x, 42, std::memory_order_release);
+ std::atomic_store_explicit(&x, 42, std::memory_order_seq_cst);
+ }
+ // compare exchange weak
+ {
+ x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_relaxed);
+ x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_consume);
+ x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_acquire);
+ x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_seq_cst);
+ // Test that the cmpxchg overload with only one memory order argument
+ // does not generate any diagnostics.
+ x.compare_exchange_weak(val1, val2, std::memory_order_release);
+ }
+ {
+ std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_compare_exchange_weak_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_compare_exchange_weak_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_relaxed);
+ std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_consume);
+ std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_acquire);
+ std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_seq_cst);
+ }
+ // compare exchange strong
+ {
+ x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ vx.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_relaxed);
+ x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_consume);
+ x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_acquire);
+ x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_seq_cst);
+ // Test that the cmpxchg overload with only one memory order argument
+ // does not generate any diagnostics.
+ x.compare_exchange_strong(val1, val2, std::memory_order_release);
+ }
+ {
+ std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_compare_exchange_strong_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-warning {{memory order argument to atomic operation is invalid}}
+ std::atomic_compare_exchange_strong_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-warning {{memory order argument to atomic operation is invalid}}
+ // valid memory orders
+ std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_relaxed);
+ std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_consume);
+ std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_acquire);
+ std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_seq_cst);
+ }
+}
More information about the cfe-commits
mailing list