[libcxx-commits] [libcxx] 7803636 - [libcxx testing] Fix UB in tests for std::lock_guard

Igor Kudrin via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 15 01:12:31 PST 2021


Author: Igor Kudrin
Date: 2021-01-15T16:11:45+07:00
New Revision: 78036360573c35ea9e6a697d2eed92db893b4850

URL: https://github.com/llvm/llvm-project/commit/78036360573c35ea9e6a697d2eed92db893b4850
DIFF: https://github.com/llvm/llvm-project/commit/78036360573c35ea9e6a697d2eed92db893b4850.diff

LOG: [libcxx testing] Fix UB in tests for std::lock_guard

If mutex::try_lock() is called in a thread that already owns the mutex,
the behavior is undefined. The patch fixes the issue by creating another
thread, where the call is allowed.

Differential Revision: https://reviews.llvm.org/D94656

Added: 
    

Modified: 
    libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/adopt_lock.pass.cpp
    libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/adopt_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/adopt_lock.pass.cpp
index 5135dbcef816..db6a2e35f9c5 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/adopt_lock.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/adopt_lock.pass.cpp
@@ -18,15 +18,21 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "make_test_thread.h"
 #include "test_macros.h"
 
 std::mutex m;
 
+void do_try_lock() {
+  assert(m.try_lock() == false);
+}
+
 int main(int, char**) {
   {
     m.lock();
     std::lock_guard<std::mutex> lg(m, std::adopt_lock);
-    assert(m.try_lock() == false);
+    std::thread t = support::make_test_thread(do_try_lock);
+    t.join();
   }
 
   m.lock();

diff  --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
index 0e096eabe4b6..5dcecd344c36 100644
--- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
+++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
@@ -21,14 +21,20 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "make_test_thread.h"
 #include "test_macros.h"
 
 std::mutex m;
 
+void do_try_lock() {
+  assert(m.try_lock() == false);
+}
+
 int main(int, char**) {
   {
     std::lock_guard<std::mutex> lg(m);
-    assert(m.try_lock() == false);
+    std::thread t = support::make_test_thread(do_try_lock);
+    t.join();
   }
 
   m.lock();


        


More information about the libcxx-commits mailing list