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

Igor Kudrin via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 14 00:16:28 PST 2021


ikudrin created this revision.
ikudrin added reviewers: ldionne, davezarzycki, libc++.
ikudrin added a project: libc++.
ikudrin requested review of this revision.
Herald added 1 blocking reviewer(s): libc++.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94656

Files:
  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


Index: libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
===================================================================
--- libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
+++ libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp
@@ -21,6 +21,7 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "make_test_thread.h"
 #include "test_macros.h"
 
 std::mutex m;
@@ -28,7 +29,10 @@
 int main(int, char**) {
   {
     std::lock_guard<std::mutex> lg(m);
-    assert(m.try_lock() == false);
+    std::thread t = support::make_test_thread([](){
+      assert(m.try_lock() == false);
+    });
+    t.join();
   }
 
   m.lock();
Index: 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/adopt_lock.pass.cpp
+++ libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/adopt_lock.pass.cpp
@@ -18,6 +18,7 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "make_test_thread.h"
 #include "test_macros.h"
 
 std::mutex m;
@@ -26,7 +27,10 @@
   {
     m.lock();
     std::lock_guard<std::mutex> lg(m, std::adopt_lock);
-    assert(m.try_lock() == false);
+    std::thread t = support::make_test_thread([](){
+      assert(m.try_lock() == false);
+    });
+    t.join();
   }
 
   m.lock();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94656.316580.patch
Type: text/x-patch
Size: 1462 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210114/cad40ee0/attachment.bin>


More information about the libcxx-commits mailing list