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

Conversation

huixie90
Copy link
Member

@huixie90 huixie90 commented May 20, 2024

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.

@huixie90 huixie90 requested a review from a team as a code owner May 20, 2024 16:44
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 20, 2024
@llvmbot
Copy link
Member

llvmbot commented May 20, 2024

@llvm/pr-subscribers-libcxx

Author: Hui (huixie90)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/92783.diff

2 Files Affected:

  • (modified) libcxx/src/atomic.cpp (+3-3)
  • (added) libcxx/test/libcxx/atomics/atomics.syn/wait.pass.cpp (+30)
diff --git a/libcxx/src/atomic.cpp b/libcxx/src/atomic.cpp
index 44100d4414e0d..031f247f88c2f 100644
--- a/libcxx/src/atomic.cpp
+++ b/libcxx/src/atomic.cpp
@@ -69,17 +69,17 @@ 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
+#  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);
+  __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) {
   __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
diff --git a/libcxx/test/libcxx/atomics/atomics.syn/wait.pass.cpp b/libcxx/test/libcxx/atomics/atomics.syn/wait.pass.cpp
new file mode 100644
index 0000000000000..f59db4109bfd5
--- /dev/null
+++ b/libcxx/test/libcxx/atomics/atomics.syn/wait.pass.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// XFAIL: availability-synchronization_library-missing
+// XFAIL: !has-64-bit-atomics
+
+#include <atomic>
+#include <cassert>
+
+#include "test_macros.h"
+
+void test_85107() {
+  // 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 + (1l << 32);
+  std::__cxx_atomic_contention_t ct(new_val);
+  std::__libcpp_atomic_wait(&ct, old_val);
+}
+
+int main(int, char**) {
+  test_85107();
+
+  return 0;
+}

// UNSUPPORTED: no-threads
// XFAIL: availability-synchronization_library-missing
// XFAIL: !has-64-bit-atomics

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++

Copy link
Member

@EricWF EricWF left a comment

Choose a reason for hiding this comment

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

LGTM, once the bots come back.

// UNSUPPORTED: no-threads
// XFAIL: availability-synchronization_library-missing
// XFAIL: !has-64-bit-atomics

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"?

// 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 + (1l << 32);
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately the if constexpr doesn't really help us here, since it's not a template, all of the semantic checking is still done it seems. Which is what's causing the windows builds to fail (it appears).

Changing the 1l to 1ll might help stop the complaining, and since we're basically asserting that sizeof(long) > 4, it seems like a change that shouldn't change the meaning?

@EricWF
Copy link
Member

EricWF commented May 29, 2024

LGTM once the bots are passing.

// UNSUPPORTED: no-threads
// XFAIL: availability-synchronization_library-missing
// 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.

About the CI failures:

The apple-system CI job should be renamed to something like apple-flavor or something like that. Then,

  • It defines stdlib=apple-libc++ because it's the apple-configured flavor
  • It does NOT define stdlib=system, because we are NOT running against the system-provided library

The back-deployment CI jobs running on Apple would then define stdlib=system and target=<arch>-apple-macosx<version>.

We also need something that implicitly defines stdlib=apple-libc++ whenever we have stdlib=system and a target of macosx (or we could set the feature manually too).

I'll work on that patch, this is just self notes.

// UNSUPPORTED: no-threads
// XFAIL: availability-synchronization_library-missing
// 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.

Maybe just turn this into UNSUPPORTED since the apple-backdeployment-hardening looks like it would require very tedious checking of the software versions and maybe even hardware.

@huixie90 huixie90 merged commit 6e22b53 into llvm:main Jun 1, 2024
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++] atomic_wait uses UL_COMPARE_AND_WAIT when it should use UL_COMPARE_AND_WAIT64 on Darwin
4 participants