Skip to content

Commit 6e22b53

Browse files
authored
[libc++] Fix std::atomic::wait ulock wait UL_COMPARE_AND_WAIT64 (#92783)
in `atomic::wait`, when we call the platform wait ulock_wait , we are using UL_COMPARE_AND_WAIT. But we should use UL_COMPARE_AND_WAIT64 instead as the address we are waiting for is a 64 bit integer. fixes #85107 It is rather hard to test directly because in `atomic::wait`, before calling into the platform wait, our c++ code has some poll logic which checks the value not changing. Thus in this patch, the test is using the internal function.
1 parent 2b2ce50 commit 6e22b53

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

libcxx/src/atomic.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,20 @@ extern "C" int __ulock_wait(
6969
uint32_t operation, void* addr, uint64_t value, uint32_t timeout); /* timeout is specified in microseconds */
7070
extern "C" int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value);
7171

72-
# define UL_COMPARE_AND_WAIT 1
72+
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/ulock.h#L82
73+
# define UL_COMPARE_AND_WAIT64 5
7374
# define ULF_WAKE_ALL 0x00000100
7475

7576
static void
7677
__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
77-
__ulock_wait(UL_COMPARE_AND_WAIT, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
78+
static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waiting on 8 bytes value");
79+
__ulock_wait(UL_COMPARE_AND_WAIT64, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
7880
}
7981

8082
static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
83+
static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waking up on 8 bytes value");
8184
__ulock_wake(
82-
UL_COMPARE_AND_WAIT | (__notify_one ? 0 : ULF_WAKE_ALL), const_cast<__cxx_atomic_contention_t*>(__ptr), 0);
85+
UL_COMPARE_AND_WAIT64 | (__notify_one ? 0 : ULF_WAKE_ALL), const_cast<__cxx_atomic_contention_t*>(__ptr), 0);
8386
}
8487

8588
#elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: no-threads
10+
// This bug was first fixed in LLVM 19
11+
// UNSUPPORTED: using-built-library-before-llvm-19
12+
// XFAIL: availability-synchronization_library-missing
13+
14+
#include <atomic>
15+
#include <cassert>
16+
17+
void test_85107() {
18+
if constexpr (sizeof(std::__cxx_contention_t) == 8 && sizeof(long) > 4) {
19+
// https://github.com/llvm/llvm-project/issues/85107
20+
// [libc++] atomic_wait uses UL_COMPARE_AND_WAIT when it should use UL_COMPARE_AND_WAIT64 on Darwin
21+
constexpr std::__cxx_contention_t old_val = 0;
22+
constexpr std::__cxx_contention_t new_val = old_val + (1ll << 32);
23+
std::__cxx_atomic_contention_t ct(new_val);
24+
std::__libcpp_atomic_wait(&ct, old_val); // this will hang forever if the bug is present
25+
}
26+
}
27+
28+
int main(int, char**) {
29+
test_85107();
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)