Skip to content

Commit 5394683

Browse files
committed
Fix bug in atomic_ref's calculation of lock_free-ness.
The builtin __atomic_always_lock_free takes into account the type of the pointer provided as the second argument. Because we were passing void*, rather than T*, the calculation failed. This meant that atomic_ref<T>::is_always_lock_free was only true for char & bool. This bug exists elsewhere in the atomic library (when using GCC, we fail to pass a pointer at all, and we fail to correctly align the atomic like _Atomic would). This bug was not initially caught because we don't ever actually expect a given value for `is_always_lock_free`. This problem is common throughout atomic, where the tests have been written to assert that _the value under test_ IS _the value under test_. Which leads to the admission of bugs like this. Further work is needed to clean up: (A) Our detection of has-64-bit-atomics, which uses std::atomic to determine if std::atomic is supported... (the type `LargeType` may be 64 bits in size, but it's required alignment is only 1 byte). This configuration test was never intended to provide that information. (B) The use of __atomic_is_always_lock_free in the GCC atomic implementation, where we lie about wether a type is always lock free, when the alignment for the std::atomic<T> is much smaller than required. For example, struct Counter {int x; int y; };, which _Atomic Counter aligns to 8 bytes, but our std::atomic<Counter> under GCC only aligns to 4, but still reports that the type is always lock free. (C) std::atomic_ref<T>::required_alignment should often times be larger than the natural alignment of the type if the sizeof(T) > alignof(T) and sizeof(T) 2, 4, 8, or 16. (See the Counter example). In failing to do so we make many types (Again, see Counter), non-lock free even when there are atomic instructions on the host that support types of that size. (D) We need to actually test against hard coded values throughout our atomic tests to avoid these sorts of bugs in the future. This probably means auditing the entire atomic test suite. This change attempts to start sorting out the testing difficulties by using the __GCC_ATOMIC_(CHAR|SHORT|INT|LONG|LLONG|POINTER)_IS_LOCK_FREE predefined macros to establish an expected value for `is_always_lock_free` and `is_lock_free` for the respective types, as well as types with matching sizes and compatible alignment values (Where compatible alignment meants alignof(T) >= alignof(char|short|int|long|long long) for the matching sized type). Using these compiler pre-defines we can actually validate that certain types, like char and int, are actually always lock free like they are on every platform in the wild(*). (*) At least for every platform we care about. Fixing (B) reqires an ABI break where we bump the alignment on the type std::atomic<T> to match that of _Atomic T (were we under clang). Fixing (C) also requires an ABI break, but atomic_ref is new enough that we should consider it ASAP. (Though fixing (C) is arguably more of a QoI detail, but it's a big one, since we don't want the runtime alignment of memory to determine the locking behavior of the atomic).
1 parent 972f297 commit 5394683

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

libcxx/include/__atomic/atomic_ref.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct __atomic_ref_base {
105105
// that the pointer is going to be aligned properly at runtime because that is a (checked) precondition
106106
// of atomic_ref's constructor.
107107
static constexpr bool is_always_lock_free =
108-
__atomic_always_lock_free(sizeof(_Tp), reinterpret_cast<void*>(-required_alignment));
108+
__atomic_always_lock_free(sizeof(_Tp), reinterpret_cast<_Tp*>(-required_alignment));
109109

110110
_LIBCPP_HIDE_FROM_ABI bool is_lock_free() const noexcept { return __atomic_is_lock_free(sizeof(_Tp), __ptr_); }
111111

libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,25 @@
1818
#include <concepts>
1919

2020
#include "test_macros.h"
21+
#include "atomic_helpers.h"
22+
2123

2224
template <typename T>
23-
void check_always_lock_free(std::atomic_ref<T> const a) {
25+
void check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<T> const a) {
26+
if (is_lock_free_status_known<T>()) {
27+
constexpr LockFreeStatus known_status = get_known_atomic_lock_free_status<T>();
28+
29+
static_assert(std::atomic_ref<T>::is_always_lock_free == (known_status == LockFreeStatus::always),
30+
"is_always_lock_free is inconsistent with known lock-free status");
31+
if (known_status == LockFreeStatus::always) {
32+
assert(a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
33+
} else if (known_status == LockFreeStatus::never) {
34+
assert(!a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
35+
} else {
36+
assert(a.is_lock_free() || !a.is_lock_free()); // This is kinda dumb, but we might as well call the function once.
37+
}
38+
39+
}
2440
std::same_as<const bool> decltype(auto) is_always_lock_free = std::atomic_ref<T>::is_always_lock_free;
2541
if (is_always_lock_free) {
2642
std::same_as<bool> decltype(auto) is_lock_free = a.is_lock_free();
@@ -33,18 +49,29 @@ void check_always_lock_free(std::atomic_ref<T> const a) {
3349
do { \
3450
typedef T type; \
3551
type obj{}; \
36-
check_always_lock_free(std::atomic_ref<type>(obj)); \
52+
check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<type>(obj)); \
3753
} while (0)
3854

55+
void check_always_lock_free_types() {
56+
57+
static_assert(std::atomic_ref<int>::is_always_lock_free);
58+
static_assert(std::atomic_ref<char>::is_always_lock_free);
59+
}
60+
3961
void test() {
62+
// While it's hard to portably test the value of is_always_lock_free, since different platforms have different support
63+
// for atomic operations, it's still very important to do so. Specifically, it's important to have at least
64+
// a few tests that have expected values.
65+
check_always_lock_free_types();
66+
4067
int i = 0;
41-
check_always_lock_free(std::atomic_ref<int>(i));
68+
check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<int>(i));
4269

4370
float f = 0.f;
44-
check_always_lock_free(std::atomic_ref<float>(f));
71+
check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<float>(f));
4572

4673
int* p = &i;
47-
check_always_lock_free(std::atomic_ref<int*>(p));
74+
check_always_lock_free_subsumes_is_lock_free(std::atomic_ref<int*>(p));
4875

4976
CHECK_ALWAYS_LOCK_FREE(struct Empty{});
5077
CHECK_ALWAYS_LOCK_FREE(struct OneInt { int i; });

libcxx/test/support/atomic_helpers.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,86 @@
1111

1212
#include <cassert>
1313
#include <cstdint>
14+
#include <cstddef>
15+
#include <type_traits>
1416

1517
#include "test_macros.h"
1618

19+
#if defined(TEST_COMPILER_CLANG)
20+
# define TEST_ATOMIC_CHAR_LOCK_FREE __CLANG_ATOMIC_CHAR_LOCK_FREE
21+
# define TEST_ATOMIC_SHORT_LOCK_FREE __CLANG_ATOMIC_SHORT_LOCK_FREE
22+
# define TEST_ATOMIC_INT_LOCK_FREE __CLANG_ATOMIC_INT_LOCK_FREE
23+
# define TEST_ATOMIC_LONG_LOCK_FREE __CLANG_ATOMIC_LONG_LOCK_FREE
24+
# define TEST_ATOMIC_LLONG_LOCK_FREE __CLANG_ATOMIC_LLONG_LOCK_FREE
25+
# define TEST_ATOMIC_POINTER_LOCK_FREE __CLANG_ATOMIC_POINTER_LOCK_FREE
26+
#elif defined(TEST_COMPILER_GCC)
27+
# define TEST_ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
28+
# define TEST_ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
29+
# define TEST_ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
30+
# define TEST_ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
31+
# define TEST_ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
32+
# define TEST_ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
33+
#elif TEST_COMPILER_MSVC
34+
// This is lifted from STL/stl/inc/atomic on github for the purposes of
35+
// keeping the tests compiling for MSVC's STL. It's not a perfect solution
36+
// but at least the tests will keep running.
37+
//
38+
// Note MSVC's STL never produces a type that is sometimes lock free, but not always lock free.
39+
template <class T, size_t Size = sizeof(T)>
40+
constexpr bool msvc_is_lock_free_macro_value() {
41+
return (Size <= 8 && (Size & Size - 1) == 0) ? 2 : 0;
42+
}
43+
# define TEST_ATOMIC_CHAR_LOCK_FREE ::msvc_is_lock_free_macro_value<char>()
44+
# define TEST_ATOMIC_SHORT_LOCK_FREE ::msvc_is_lock_free_macro_value<short>()
45+
# define TEST_ATOMIC_INT_LOCK_FREE ::msvc_is_lock_free_macro_value<int>()
46+
# define TEST_ATOMIC_LONG_LOCK_FREE ::msvc_is_lock_free_macro_value<long>()
47+
# define TEST_ATOMIC_LLONG_LOCK_FREE ::msvc_is_lock_free_macro_value<long long>()
48+
# define TEST_ATOMIC_POINTER_LOCK_FREE ::msvc_is_lock_free_macro_value<void*>()
49+
#else
50+
# error "Unknown compiler"
51+
#endif
52+
enum class LockFreeStatus { unknown = -1, never = 0, sometimes = 1, always = 2 };
53+
#define COMPARE_TYPES(T1, T2) \
54+
(sizeof(T1) == sizeof(T2) && alignof(T1) >= alignof(T2))
55+
56+
template <class T>
57+
constexpr inline LockFreeStatus get_known_atomic_lock_free_status() {
58+
return LockFreeStatus{COMPARE_TYPES(T, char)
59+
? TEST_ATOMIC_CHAR_LOCK_FREE
60+
: (COMPARE_TYPES(T, short)
61+
? TEST_ATOMIC_SHORT_LOCK_FREE
62+
: (COMPARE_TYPES(T, int)
63+
? TEST_ATOMIC_INT_LOCK_FREE
64+
: (COMPARE_TYPES(T, long)
65+
? TEST_ATOMIC_LONG_LOCK_FREE
66+
: (COMPARE_TYPES(T, long long)
67+
? TEST_ATOMIC_LLONG_LOCK_FREE
68+
: (COMPARE_TYPES(T, void*) ? TEST_ATOMIC_POINTER_LOCK_FREE
69+
: -1)))))};
70+
}
71+
72+
template <class T>
73+
constexpr bool is_lock_free_status_known() {
74+
return get_known_atomic_lock_free_status<T>() != LockFreeStatus::unknown;
75+
}
76+
77+
static_assert(is_lock_free_status_known<char>(), "");
78+
static_assert(is_lock_free_status_known<short>(), "");
79+
static_assert(is_lock_free_status_known<int>(), "");
80+
static_assert(is_lock_free_status_known<long>(), "");
81+
static_assert(is_lock_free_status_known<long long>(), "");
82+
static_assert(is_lock_free_status_known<void*>(), "");
83+
84+
85+
// These macros are somewhat suprising to use, since they take the values 0, 1, or 2.
86+
// To make the tests clearer, get rid of them in preference of AtomicInfo.
87+
#undef TEST_ATOMIC_CHAR_LOCK_FREE
88+
#undef TEST_ATOMIC_SHORT_LOCK_FREE
89+
#undef TEST_ATOMIC_INT_LOCK_FREE
90+
#undef TEST_ATOMIC_LONG_LOCK_FREE
91+
#undef TEST_ATOMIC_LLONG_LOCK_FREE
92+
#undef TEST_ATOMIC_POINTER_LOCK_FREE
93+
1794
struct UserAtomicType {
1895
int i;
1996

@@ -64,6 +141,17 @@ struct LargeUserAtomicType {
64141
}
65142
};
66143

144+
template <template <class TestArg> class TestFunctor>
145+
struct TestEachLockFreeKnownIntegralType {
146+
void operator()() const {
147+
TestFunctor<char>()();
148+
TestFunctor<short>()();
149+
TestFunctor<int>()();
150+
TestFunctor<long long>()();
151+
TestFunctor<void*>()();
152+
}
153+
};
154+
67155
template <template <class TestArg> class TestFunctor>
68156
struct TestEachIntegralType {
69157
void operator()() const {

0 commit comments

Comments
 (0)