Debian Codegraph @

clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=webkit.UncountedLambdaCapturesChecker -verify %s
2
3 #include "mock-types.h"
4
5 namespace std {
6
7 template <typename T>
8 T&& move(T& t) {
9 return static_cast<T&&>(t);
10 }
11
12 namespace ranges {
13
14 template<typename IteratorType, typename CallbackType>
15 void for_each(IteratorType first, IteratorType last, CallbackType callback) {
16 for (auto it = first; !(it == last); ++it)
17 callback(*it);
18 }
19
20 struct all_of_impl {
21 template <typename Collection, typename Predicate>
22 constexpr bool operator()(const Collection& collection, Predicate predicate) const {
23 for (auto it = collection.begin(); it != collection.end(); ++it) {
24 if (!predicate(*it))
25 return false;
26 }
27 return true;
28 }
29 };
30 inline constexpr auto all_of = all_of_impl {};
31
32 }
33
34 }
35
36 namespace WTF {
37
38 namespace Detail {
39
40 template<typename Out, typename... In>
41 class CallableWrapperBase {
42 public:
43 virtual ~CallableWrapperBase() { }
44 virtual Out call(In...) = 0;
45 };
46
47 template<typename, typename, typename...> class CallableWrapper;
48
49 template<typename CallableType, typename Out, typename... In>
50 class CallableWrapper : public CallableWrapperBase<Out, In...> {
51 public:
52 explicit CallableWrapper(CallableType& callable)
53 : m_callable(callable) { }
54 Out call(In... in) final { return m_callable(in...); }
55
56 private:
57 CallableType m_callable;
58 };
59
60 } // namespace Detail
61
62 template<typename> class Function;
63
64 template<typename Out, typename... In> Function<Out(In...)> adopt(Detail::CallableWrapperBase<Out, In...>*);
65
66 template <typename Out, typename... In>
67 class Function<Out(In...)> {
68 public:
69 using Impl = Detail::CallableWrapperBase<Out, In...>;
70
71 Function() = default;
72
73 template<typename FunctionType>
74 Function(FunctionType f)
75 : m_callableWrapper(new Detail::CallableWrapper<FunctionType, Out, In...>(f)) { }
76
77 Out operator()(In... in) const { return m_callableWrapper->call(in...); }
78 explicit operator bool() const { return !!m_callableWrapper; }
79
80 private:
81 enum AdoptTag { Adopt };
82 Function(Impl* impl, AdoptTag)
83 : m_callableWrapper(impl)
84 {
85 }
86
87 friend Function adopt<Out, In...>(Impl*);
88
89 std::unique_ptr<Impl> m_callableWrapper;
90 };
91
92 template<typename Out, typename... In> Function<Out(In...)> adopt(Detail::CallableWrapperBase<Out, In...>* impl)
93 {
94 return Function<Out(In...)>(impl, Function<Out(In...)>::Adopt);
95 }
96
97 template <typename KeyType, typename ValueType>
98 class HashMap {
99 public:
100 HashMap();
101 HashMap([[clang::noescape]] const Function<ValueType()>&);
102 void ensure(const KeyType&, [[clang::noescape]] const Function<ValueType()>&);
103 bool operator+([[clang::noescape]] const Function<ValueType()>&) const;
104 static void ifAny(HashMap, [[clang::noescape]] const Function<bool(ValueType)>&);
105
106 private:
107 ValueType* m_table { nullptr };
108 };
109
110 class ScopeExit final {
111 public:
112 template<typename ExitFunctionParameter>
113 explicit ScopeExit(ExitFunctionParameter&& exitFunction)
114 : m_exitFunction(std::move(exitFunction)) {
115 }
116
117 ScopeExit(ScopeExit&& other)
118 : m_exitFunction(std::move(other.m_exitFunction))
119 , m_execute(std::move(other.m_execute)) {
120 }
121
122 ~ScopeExit() {
123 if (m_execute)
124 m_exitFunction();
125 }
126
127 WTF::Function<void()> take() {
128 m_execute = false;
129 return std::move(m_exitFunction);
130 }
131
132 void release() { m_execute = false; }
133
134 ScopeExit(const ScopeExit&) = delete;
135 ScopeExit& operator=(const ScopeExit&) = delete;
136 ScopeExit& operator=(ScopeExit&&) = delete;
137
138 private:
139 WTF::Function<void()> m_exitFunction;
140 bool m_execute { true };
141 };
142
143 template<typename ExitFunction> ScopeExit makeScopeExit(ExitFunction&&);
144 template<typename ExitFunction>
145 ScopeExit makeScopeExit(ExitFunction&& exitFunction)
146 {
147 return ScopeExit(std::move(exitFunction));
148 }
149
150 // Visitor adapted from http://stackoverflow.com/questions/25338795/is-there-a-name-for-this-tuple-creation-idiom
151
152 template<class A, class... B> struct Visitor : Visitor<A>, Visitor<B...> {
153 Visitor(A a, B... b)
154 : Visitor<A>(a)
155 , Visitor<B...>(b...)
156 {
157 }
158
159 using Visitor<A>::operator ();
160 using Visitor<B...>::operator ();
161 };
162
163 template<class A> struct Visitor<A> : A {
164 Visitor(A a)
165 : A(a)
166 {
167 }
168
169 using A::operator();
170 };
171
172 template<class... F> Visitor<F...> makeVisitor(F... f)
173 {
174 return Visitor<F...>(f...);
175 }
176
177 void opaqueFunction();
178 template <typename Visitor, typename... Variants> void visit(Visitor&& v, Variants&&... values)
179 {
180 opaqueFunction();
181 }
182
183 } // namespace WTF
184
185 struct A {
186 static void b();
187 };
188
189 RefCountable* make_obj();
190
191 void someFunction();
192 template <typename Callback> void call(Callback callback) {
193 someFunction();
194 callback();
195 }
196 void callAsync(const WTF::Function<void()>&);
197
198 void raw_ptr() {
199 RefCountable* ref_countable = make_obj();
200 auto foo1 = [ref_countable](){
201 // expected-warning@-1{{Captured raw-pointer 'ref_countable' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
202 ref_countable->method();
203 };
204 auto foo2 = [&ref_countable](){
205 // expected-warning@-1{{Captured raw-pointer 'ref_countable' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
206 ref_countable->method();
207 };
208 auto foo3 = [&](){
209 ref_countable->method();
210 // expected-warning@-1{{Implicitly captured raw-pointer 'ref_countable' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
211 ref_countable = nullptr;
212 };
213
214 auto foo4 = [=](){
215 ref_countable->method();
216 // expected-warning@-1{{Implicitly captured raw-pointer 'ref_countable' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
217 };
218
219 call(foo1);
220 call(foo2);
221 call(foo3);
222 call(foo4);
223
224 // Confirm that the checker respects [[clang::suppress]].
225 RefCountable* suppressed_ref_countable = nullptr;
226 [[clang::suppress]] auto foo5 = [suppressed_ref_countable](){};
227 // no warning.
228 call(foo5);
229 }
230
231 void references() {
232 RefCountable automatic;
233 RefCountable& ref_countable_ref = automatic;
234 auto foo1 = [ref_countable_ref](){ ref_countable_ref.constMethod(); };
235 auto foo2 = [&ref_countable_ref](){ ref_countable_ref.method(); };
236 // expected-warning@-1{{Captured reference 'ref_countable_ref' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
237 auto foo3 = [&](){ ref_countable_ref.method(); };
238 // expected-warning@-1{{Implicitly captured reference 'ref_countable_ref' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
239 auto foo4 = [=](){ ref_countable_ref.constMethod(); };
240
241 call(foo1);
242 call(foo2);
243 call(foo3);
244 call(foo4);
245 }
246
247 void quiet() {
248 // This code is not expected to trigger any warnings.
249 {
250 RefCountable automatic;
251 RefCountable &ref_countable_ref = automatic;
252 }
253
254 auto foo3 = [&]() {};
255 auto foo4 = [=]() {};
256
257 call(foo3);
258 call(foo4);
259
260 RefCountable *ref_countable = nullptr;
261 }
262
263 template <typename Callback>
264 void map(RefCountable* start, [[clang::noescape]] Callback&& callback)
265 {
266 while (start) {
267 callback(*start);
268 start = start->next();
269 }
270 }
271
272 template <typename Callback1, typename Callback2>
273 void doubleMap(RefCountable* start, [[clang::noescape]] Callback1&& callback1, Callback2&& callback2)
274 {
275 while (start) {
276 callback1(*start);
277 callback2(*start);
278 start = start->next();
279 }
280 }
281
282 void noescape_lambda() {
283 RefCountable* someObj = make_obj();
284 RefCountable* otherObj = make_obj();
285 map(make_obj(), [&](RefCountable& obj) {
286 otherObj->method();
287 });
288 doubleMap(make_obj(), [&](RefCountable& obj) {
289 otherObj->method();
290 }, [&](RefCountable& obj) {
291 otherObj->method();
292 // expected-warning@-1{{Implicitly captured raw-pointer 'otherObj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
293 });
294 ([&] {
295 someObj->method();
296 })();
297 }
298
299 void lambda_capture_param(RefCountable* obj) {
300 auto someLambda = [&]() {
301 obj->method();
302 };
303 someLambda();
304 someLambda();
305 }
306
307 struct RefCountableWithLambdaCapturingThis {
308 void ref() const;
309 void deref() const;
310 void nonTrivial();
311
312 void method_captures_this_safe() {
313 auto lambda = [&]() {
314 nonTrivial();
315 };
316 lambda();
317 }
318
319 void method_captures_this_unsafe() {
320 auto lambda = [&]() {
321 nonTrivial();
322 // expected-warning@-1{{Implicitly captured raw-pointer 'this' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
323 };
324 call(lambda);
325 }
326
327 void method_captures_this_unsafe_capture_local_var_explicitly() {
328 RefCountable* x = make_obj();
329 call([this, protectedThis = RefPtr { this }, x]() {
330 // expected-warning@-1{{Captured raw-pointer 'x' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
331 nonTrivial();
332 x->method();
333 });
334 }
335
336 void method_captures_this_with_other_protected_var() {
337 RefCountable* x = make_obj();
338 call([this, protectedX = RefPtr { x }]() {
339 // expected-warning@-1{{Captured raw-pointer 'this' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
340 nonTrivial();
341 protectedX->method();
342 });
343 }
344
345 void method_captures_this_unsafe_capture_local_var_explicitly_with_deref() {
346 RefCountable* x = make_obj();
347 call([this, protectedThis = Ref { *this }, x]() {
348 // expected-warning@-1{{Captured raw-pointer 'x' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
349 nonTrivial();
350 x->method();
351 });
352 }
353
354 void method_captures_this_unsafe_local_var_via_vardecl() {
355 RefCountable* x = make_obj();
356 auto lambda = [this, protectedThis = Ref { *this }, x]() {
357 // expected-warning@-1{{Captured raw-pointer 'x' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
358 nonTrivial();
359 x->method();
360 };
361 call(lambda);
362 }
363
364 void method_captures_this_with_guardian() {
365 auto lambda = [this, protectedThis = Ref { *this }]() {
366 nonTrivial();
367 };
368 call(lambda);
369 }
370
371 void method_captures_this_with_guardian_refptr() {
372 auto lambda = [this, protectedThis = RefPtr { &*this }]() {
373 nonTrivial();
374 };
375 call(lambda);
376 }
377
378 void forEach(const WTF::Function<void(RefCountable&)>&);
379 void method_captures_this_with_lambda_with_no_escape() {
380 auto run = [&]([[clang::noescape]] const WTF::Function<void(RefCountable&)>& func) {
381 forEach(func);
382 };
383 run([&](RefCountable&) {
384 nonTrivial();
385 });
386 }
387
388 static void callLambda([[clang::noescape]] const WTF::Function<RefPtr<RefCountable>()>&);
389 void method_captures_this_in_template_method() {
390 RefCountable* obj = make_obj();
391 WTF::HashMap<int, RefPtr<RefCountable>> nextMap;
392 nextMap.ensure(3, [&] {
393 return obj->next();
394 });
395 nextMap+[&] {
396 return obj->next();
397 };
398 WTF::HashMap<int, RefPtr<RefCountable>>::ifAny(nextMap, [&](auto& item) -> bool {
399 return item->next() && obj->next();
400 });
401 callLambda([&]() -> RefPtr<RefCountable> {
402 return obj->next();
403 });
404 WTF::HashMap<int, RefPtr<RefCountable>> anotherMap([&] {
405 return obj->next();
406 });
407 }
408
409 void callAsyncNoescape([[clang::noescape]] WTF::Function<bool(RefCountable&)>&&);
410 void method_temp_lambda(RefCountable* obj) {
411 callAsyncNoescape([this, otherObj = RefPtr { obj }](auto& obj) {
412 return otherObj == &obj;
413 });
414 }
415
416 void method_nested_lambda() {
417 callAsync([this, protectedThis = Ref { *this }] {
418 callAsync([this, protectedThis = static_cast<const Ref<RefCountableWithLambdaCapturingThis>&&>(protectedThis)] {
419 nonTrivial();
420 });
421 });
422 }
423
424 void method_nested_lambda2() {
425 callAsync([this, protectedThis = RefPtr { this }] {
426 callAsync([this, protectedThis = std::move(*protectedThis)] {
427 nonTrivial();
428 });
429 });
430 }
431
432 void method_nested_lambda3() {
433 callAsync([this, protectedThis = RefPtr { this }] {
434 callAsync([this] {
435 // expected-warning@-1{{Captured raw-pointer 'this' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
436 nonTrivial();
437 });
438 });
439 }
440
441 void method_nested_lambda4() {
442 callAsync([this, protectedThis = RefPtr { this }] {
443 callAsync([this, protectedThis = WTF::move(*protectedThis)] {
444 nonTrivial();
445 });
446 });
447 }
448 };
449
450 struct NonRefCountableWithLambdaCapturingThis {
451 void nonTrivial();
452
453 void method_captures_this_safe() {
454 auto lambda = [&]() {
455 nonTrivial();
456 };
457 lambda();
458 }
459
460 void method_captures_this_unsafe() {
461 auto lambda = [&]() {
462 nonTrivial();
463 };
464 call(lambda);
465 }
466 };
467
468 void trivial_lambda() {
469 RefCountable* ref_countable = make_obj();
470 auto trivial_lambda = [&]() {
471 return ref_countable->trivial();
472 };
473 trivial_lambda();
474 }
475
476 bool call_lambda_var_decl() {
477 RefCountable* ref_countable = make_obj();
478 auto lambda1 = [&]() -> bool {
479 return ref_countable->next();
480 };
481 auto lambda2 = [=]() -> bool {
482 return ref_countable->next();
483 };
484 return lambda1() && lambda2();
485 }
486
487 void lambda_with_args(RefCountable* obj) {
488 auto trivial_lambda = [&](int v) {
489 obj->method();
490 };
491 trivial_lambda(1);
492 }
493
494 void callFunctionOpaque(WTF::Function<void()>&&);
495 void callFunction(WTF::Function<void()>&& function) {
496 someFunction();
497 function();
498 }
499
500 void lambda_converted_to_function(RefCountable* obj)
501 {
502 callFunction([&]() {
503 obj->method();
504 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
505 });
506 callFunctionOpaque([&]() {
507 obj->method();
508 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
509 });
510 }
511
512 void capture_copy_in_lambda(CheckedObj& checked) {
513 callFunctionOpaque([checked]() mutable {
514 checked.method();
515 });
516 auto* ptr = &checked;
517 callFunctionOpaque([ptr]() mutable {
518 // expected-warning@-1{{Captured raw-pointer 'ptr' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
519 ptr->method();
520 });
521 }
522
523 struct TemplateFunctionCallsLambda {
524 void ref() const;
525 void deref() const;
526
527 RefCountable* obj();
528
529 template <typename T>
530 RefPtr<T> method(T* t) {
531 auto ret = ([&]() -> RefPtr<T> {
532 if constexpr (T::isEncodable)
533 return t;
534 return obj() ? t : nullptr;
535 })();
536 return ret;
537 }
538 };
539
540 class Iterator {
541 public:
542 Iterator(void* array, unsigned long sizeOfElement, unsigned int index);
543 Iterator(const Iterator&);
544 Iterator& operator=(const Iterator&);
545 bool operator==(const Iterator&);
546
547 Iterator& operator++();
548 int& operator*();
549
550 private:
551 void* current { nullptr };
552 unsigned long sizeOfElement { 0 };
553 };
554
555 void ranges_for_each(RefCountable* obj) {
556 int array[] = { 1, 2, 3, 4, 5 };
557 std::ranges::for_each(Iterator(array, sizeof(*array), 0), Iterator(array, sizeof(*array), 5), [&](int& item) {
558 obj->method();
559 ++item;
560 });
561 }
562
563 class IntCollection {
564 public:
565 int* begin();
566 int* end();
567 const int* begin() const;
568 const int* end() const;
569 };
570
571 class RefCountedObj {
572 public:
573 void ref();
574 void deref();
575
576 bool allOf(const IntCollection&);
577 bool isMatch(int);
578
579 void call() const;
580 void callLambda([[clang::noescape]] const WTF::Function<void ()>& callback) const;
581 void doSomeWork() const;
582 };
583
584 bool RefCountedObj::allOf(const IntCollection& collection) {
585 return std::ranges::all_of(collection, [&](auto& number) {
586 return isMatch(number);
587 });
588 }
589
590 void RefCountedObj::callLambda([[clang::noescape]] const WTF::Function<void ()>& callback) const
591 {
592 callback();
593 }
594
595 void RefCountedObj::call() const
596 {
597 auto lambda = [&] {
598 doSomeWork();
599 };
600 callLambda(lambda);
601 }
602
603 void scope_exit(RefCountable* obj) {
604 auto scope = WTF::makeScopeExit([&] {
605 obj->method();
606 });
607 someFunction();
608 WTF::ScopeExit scope2([&] {
609 obj->method();
610 });
611 someFunction();
612 }
613
614 void doWhateverWith(WTF::ScopeExit& obj);
615
616 void scope_exit_with_side_effect(RefCountable* obj) {
617 auto scope = WTF::makeScopeExit([&] {
618 obj->method();
619 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
620 });
621 doWhateverWith(scope);
622 }
623
624 void scope_exit_static(RefCountable* obj) {
625 static auto scope = WTF::makeScopeExit([&] {
626 obj->method();
627 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
628 });
629 }
630
631 WTF::Function<void()> scope_exit_take_lambda(RefCountable* obj) {
632 auto scope = WTF::makeScopeExit([&] {
633 obj->method();
634 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
635 });
636 return scope.take();
637 }
638
639 // FIXME: Ideally, we treat release() as a trivial function.
640 void scope_exit_release(RefCountable* obj) {
641 auto scope = WTF::makeScopeExit([&] {
642 obj->method();
643 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
644 });
645 scope.release();
646 }
647
648 void make_visitor(RefCountable* obj) {
649 auto visitor = WTF::makeVisitor([&] {
650 obj->method();
651 });
652 }
653
654 void use_visitor(RefCountable* obj) {
655 auto visitor = WTF::makeVisitor([&] {
656 obj->method();
657 });
658 WTF::visit(visitor, obj);
659 }
660
661 template <typename Visitor, typename ObjectType>
662 void bad_visit(Visitor&, ObjectType*) {
663 someFunction();
664 }
665
666 void static_visitor(RefCountable* obj) {
667 static auto visitor = WTF::makeVisitor([&] {
668 obj->method();
669 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
670 });
671 }
672
673 void bad_use_visitor(RefCountable* obj) {
674 auto visitor = WTF::makeVisitor([&] {
675 obj->method();
676 // expected-warning@-1{{Implicitly captured raw-pointer 'obj' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]}}
677 });
678 bad_visit(visitor, obj);
679 }