Skip to content

Conversation

@DanBlackwell
Copy link
Contributor

@DanBlackwell DanBlackwell commented Oct 31, 2025

It is possible for "Enter potentially blocking region" to come before "Enter constructor" in this test - if the thread that acquires the guard fails to reach its printf before the other thread that enters the blocking region reaches its own printf. Note that for the exit logs this inversion is not possible.

This patch addresses this by allowing those two log lines to come in either order.

rdar://163375661

@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Dan Blackwell (DanBlackwell)

Changes

It is possible for "Enter potentially blocking region" to come before "Enter constructor" in this test - if the thread that acquires the guard fails to reach its printf before the other thread that enters the blocking region reaches its own printf. Note that for the exit logs this inversion is not possible.

This patch addresses this by allowing those two log lines to come in either order.

rdar://163375661


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

1 Files Affected:

  • (modified) compiler-rt/test/tsan/cxa_guard_acquire.cpp (+10-3)
diff --git a/compiler-rt/test/tsan/cxa_guard_acquire.cpp b/compiler-rt/test/tsan/cxa_guard_acquire.cpp
index fc407259e8968..2e7fb102dbea8 100644
--- a/compiler-rt/test/tsan/cxa_guard_acquire.cpp
+++ b/compiler-rt/test/tsan/cxa_guard_acquire.cpp
@@ -66,10 +66,17 @@ int main(int argc, char **argv) {
   printf("Enter main\n");
 
   // If initialization is contended, the blocked thread should enter a
-  // potentially blocking region.
+  // potentially blocking region. Note that we use a DAG check because it is
+  // possible for Thread 1 to acquire the guard, then Thread 2 fail to acquire
+  // the guard then call `OnPotentiallyBlockingRegionBegin` and print "Enter
+  // potentially blocking region\n", before Thread 1 manages to reach "Enter
+  // constructor\n". This is exceptionally rare, but can be replicated by
+  // inserting a `sleep(1)` between `LazyInit() {` and `printf("Enter 
+  // constructor\n");`. Due to the barrier it is not possible for the exit logs
+  // to be inverted.
   //
-  // CHECK-NEXT: Enter constructor
-  // CHECK-NEXT: Enter potentially blocking region
+  // CHECK-DAG: Enter constructor
+  // CHECK-DAG: Enter potentially blocking region
   // CHECK-NEXT: Exit constructor
   // CHECK-NEXT: Exit potentially blocking region
   barrier_init(&barrier, 2);

@github-actions
Copy link

github-actions bot commented Oct 31, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

It is possible for "Enter potentially blocking region" to come before "Enter constructor" in this test - if the thread that acquires the guard fails to reach its printf before the other thread that enters the blocking region reaches its own printf. Note that for the exit logs this inversion is not possible.

This patch addresses this by allowing those two log lines to come in either order.
@DanBlackwell DanBlackwell force-pushed the guard-acquire-tsan-test-fix branch from 4f15a60 to 0d579bf Compare October 31, 2025 12:06
Copy link
Contributor

@wrotki wrotki left a comment

Choose a reason for hiding this comment

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

Makes sense. Thanks for the detailed description, and especially for the replication hint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants