File: no-suspend-with-lock.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (279 lines) | stat: -rw-r--r-- 8,346 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// RUN: %check_clang_tidy -std=c++20 %s cppcoreguidelines-no-suspend-with-lock %t -- -- -fno-delayed-template-parsing -fexceptions

// NOLINTBEGIN
namespace std {
template <typename T, typename... Args>
struct coroutine_traits {
  using promise_type = typename T::promise_type;
};
template <typename T = void>
struct coroutine_handle;
template <>
struct coroutine_handle<void> {
  coroutine_handle() noexcept;
  coroutine_handle(decltype(nullptr)) noexcept;
  static constexpr coroutine_handle from_address(void*);
};
template <typename T>
struct coroutine_handle {
  coroutine_handle() noexcept;
  coroutine_handle(decltype(nullptr)) noexcept;
  static constexpr coroutine_handle from_address(void*);
  operator coroutine_handle<>() const noexcept;
};

template <class Mutex>
class unique_lock {
public:
  unique_lock() noexcept;
  explicit unique_lock(Mutex &m);
  unique_lock& operator=(unique_lock&&);
  void unlock();
  Mutex* release() noexcept;
  Mutex* mutex() const noexcept;
  void swap(unique_lock& other) noexcept;
};

class mutex {
public:
  mutex() noexcept;
  ~mutex();
  mutex(const mutex &) = delete;
  mutex &operator=(const mutex &) = delete;

  void lock();
  void unlock();
};
} // namespace std

class my_own_mutex {
public:
  void lock();
  void unlock();
};

struct Awaiter {
  bool await_ready() noexcept;
  void await_suspend(std::coroutine_handle<>) noexcept;
  void await_resume() noexcept;
};

struct Coro {
  struct promise_type {
    Awaiter initial_suspend();
    Awaiter final_suspend() noexcept;
    void return_void();
    Coro get_return_object();
    void unhandled_exception();
    Awaiter yield_value(int);
  };
};
// NOLINTEND

std::mutex mtx;
std::mutex mtx2;

Coro awaits_with_lock() {
  std::unique_lock<std::mutex> lock(mtx);

  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]

  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]

  if (true) {
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  if (true) {
    std::unique_lock<std::mutex> lock2;
    lock2.unlock();
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock2' held [cppcoreguidelines-no-suspend-with-lock]
  }
}

Coro awaits_with_lock_in_try() try {
  std::unique_lock<std::mutex> lock(mtx);
  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
} catch (...) {}

Coro lock_possibly_unlocked() {
  // CppCoreGuideline CP.52's enforcement strictly requires flagging
  // code that suspends while any lock guard is not destructed.

  {
    std::unique_lock<std::mutex> lock(mtx);
    lock.unlock();
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  {
    std::unique_lock<std::mutex> lock(mtx);
    lock.release();
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  {
    std::unique_lock<std::mutex> lock(mtx);
    std::unique_lock<std::mutex> lock2;
    lock.swap(lock2);
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  {
    std::unique_lock<std::mutex> lock(mtx);
    std::unique_lock<std::mutex> lock2{mtx2};
    lock.swap(lock2);
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  {
    std::unique_lock<std::mutex> lock(mtx);
    lock = std::unique_lock<std::mutex>{};
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  {
    std::unique_lock<std::mutex> lock(mtx);
    lock = std::unique_lock<std::mutex>{mtx2};
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }
}

Coro await_with_underlying_mutex_unlocked() {
  std::unique_lock<std::mutex> lock(mtx);

  // Even though we unlock the mutex here, 'lock' is still active unless
  // there is a call to lock.unlock(). This is a bug in the program since
  // it will result in locking the mutex twice. The check does not track
  // unlock calls on the underlying mutex held by a lock guard object.
  mtx.unlock();

  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
}

Coro await_with_empty_lock() {
  std::unique_lock<std::mutex> lock;
  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
}

Coro await_before_lock() {
  co_await Awaiter{};
  std::unique_lock<std::mutex> lock(mtx);
}

Coro await_with_lock_different_scope() {
  {
    std::unique_lock<std::mutex> lock(mtx);
  }
  co_await Awaiter{};
}

Coro await_with_goto() {
first:
  co_await Awaiter{};
  std::unique_lock<std::mutex> lock(mtx);
  goto first;
}

void await_in_lambda() {
  auto f1 = []() -> Coro {
    std::unique_lock<std::mutex> lock(mtx);
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  };

  auto f2 = [](auto& m) -> Coro {
    std::unique_lock<decltype(m)> lock(m);
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  };
}

void await_in_lambda_without_immediate_mutex() {
  std::unique_lock<std::mutex> lock(mtx);

  auto f1 = []() -> Coro {
    co_await Awaiter{};
  };

  // The check only finds suspension points where there is a lock held in the
  // immediate callable.
  f1();
}

Coro yields_with_lock() {
  std::unique_lock<std::mutex> lock(mtx);
  co_yield 0;
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
}

template <class Mutex>
Coro awaits_templated_type(Mutex& m) {
  std::unique_lock<Mutex> lock(m);
  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
}

template <class T>
Coro awaits_in_template_function(T) {
  std::unique_lock<std::mutex> lock(mtx);
  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
}

template <class Mutex>
Coro awaits_in_never_instantiated_template_of_mutex(Mutex& m) {
  // Nothing should instantiate this function
  std::unique_lock<Mutex> lock(m);
  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
}

template <class T>
Coro awaits_in_never_instantiated_templated_function(T) {
  // Nothing should instantiate this function
  std::unique_lock<std::mutex> lock(mtx);
  co_await Awaiter{};
  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
}

template <class T>
struct my_container {

  Coro push_back() {
    std::unique_lock<std::mutex> lock(mtx_);
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  template <class... Args>
  Coro emplace_back(Args&&...) {
    std::unique_lock<std::mutex> lock(mtx_);
    co_await Awaiter{};
    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine suspended with lock 'lock' held [cppcoreguidelines-no-suspend-with-lock]
  }

  std::mutex mtx_;
};

void calls_templated_functions() {
  my_own_mutex m2;
  awaits_templated_type(mtx);
  awaits_templated_type(m2);

  awaits_in_template_function(1);
  awaits_in_template_function(1.0);
}