Skip to content

Commit 7803636

Browse files
committed
[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
1 parent 89e84de commit 7803636

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@
1818
#include <cstdlib>
1919
#include <cassert>
2020

21+
#include "make_test_thread.h"
2122
#include "test_macros.h"
2223

2324
std::mutex m;
2425

26+
void do_try_lock() {
27+
assert(m.try_lock() == false);
28+
}
29+
2530
int main(int, char**) {
2631
{
2732
m.lock();
2833
std::lock_guard<std::mutex> lg(m, std::adopt_lock);
29-
assert(m.try_lock() == false);
34+
std::thread t = support::make_test_thread(do_try_lock);
35+
t.join();
3036
}
3137

3238
m.lock();

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.guard/mutex.pass.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
#include <cstdlib>
2222
#include <cassert>
2323

24+
#include "make_test_thread.h"
2425
#include "test_macros.h"
2526

2627
std::mutex m;
2728

29+
void do_try_lock() {
30+
assert(m.try_lock() == false);
31+
}
32+
2833
int main(int, char**) {
2934
{
3035
std::lock_guard<std::mutex> lg(m);
31-
assert(m.try_lock() == false);
36+
std::thread t = support::make_test_thread(do_try_lock);
37+
t.join();
3238
}
3339

3440
m.lock();

0 commit comments

Comments
 (0)