Skip to content

[libc++] Fix std::atomic::wait ulock wait UL_COMPARE_AND_WAIT64 #92783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions libcxx/src/atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,20 @@ extern "C" int __ulock_wait(
uint32_t operation, void* addr, uint64_t value, uint32_t timeout); /* timeout is specified in microseconds */
extern "C" int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value);

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

static void
__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
__ulock_wait(UL_COMPARE_AND_WAIT, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waiting on 8 bytes value");
__ulock_wait(UL_COMPARE_AND_WAIT64, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
}

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

#elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
Expand Down
32 changes: 32 additions & 0 deletions libcxx/test/libcxx/atomics/atomics.syn/wait.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: no-threads
// This bug was first fixed in LLVM 19
// UNSUPPORTED: using-built-library-before-llvm-19
// XFAIL: availability-synchronization_library-missing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// This bug was first fixed in LLVM 19
// XFAIL: using-built-library-before-llvm-19

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming that implies "Darwin"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://buildkite.com/llvm-project/libcxx-ci/builds/35617#018fc382-8cf3-45dc-a8c8-c3c9d9ccd245

Unfortunately we have "unexpected passes" because of this line. It seems that the CI "Apple system" does have this feature

using-built-library-before-llvm-19

and from what I can see, it is linking against the just built libc++ which has the fix in it

-L /Users/libcxx-buildkite-agent/libcxx.buildkite-agent/builds/f1-1-macminivault-com/llvm-project/libcxx-ci/build/apple-system/arm64/lib -lc++

#include <atomic>
#include <cassert>

void test_85107() {
if constexpr (sizeof(std::__cxx_contention_t) == 8 && sizeof(long) > 4) {
// https://github.com/llvm/llvm-project/issues/85107
// [libc++] atomic_wait uses UL_COMPARE_AND_WAIT when it should use UL_COMPARE_AND_WAIT64 on Darwin
constexpr std::__cxx_contention_t old_val = 0;
constexpr std::__cxx_contention_t new_val = old_val + (1ll << 32);
std::__cxx_atomic_contention_t ct(new_val);
std::__libcpp_atomic_wait(&ct, old_val); // this will hang forever if the bug is present
}
}

int main(int, char**) {
test_85107();

return 0;
}
Loading