ScopedLambda should have a lifetime story that makes sense to the compiler
authorfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 26 May 2016 21:58:42 +0000 (21:58 +0000)
committerfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 26 May 2016 21:58:42 +0000 (21:58 +0000)
commit34a171cc1a8534de35dab0bcc4d50136360686cd
tree6bc6892378735f36b1bf8b655b631f23c5ee984c
parentb90de19514bf89be6cd5473a86ebe289e20fa5d5
ScopedLambda should have a lifetime story that makes sense to the compiler
https://bugs.webkit.org/show_bug.cgi?id=158118

Reviewed by Mark Lam.
Source/WTF:

Prior to this change, there were two lifetime bugs in ScopedLambda:

- scopedLambda(Functor&&) would bind Functor to const lambda&, so the resulting ScopedLambdaFunctor
  would hold a reference to the original lambda. This would have surprising behavior; for example
  it meant that this code was wrong:

  auto l = scopedLambda<things>([&] ...);

  The solution is to have explicit copy/move versions of scopedLambda() rather than rely on perfect
  forwarding.

- ScopedLambdaFunctor did not override its copy or move operations, so if the compiler did not RVO
  scopedLambda(), it would return a ScopedLambdaFunctor whose m_arg points to a dead temporary
  ScopedLambdaFunctor instance. The solution is to have explicit copy/move constructors and
  operators, which preserve the invariant that ScopedLambda::m_arg points to this.

One nice side-effect of all of these constructors and operators being explicit is that we can rely
on WTFMove's excellent assertions, which helped catch the first issue.

This reverts ParkingLot to use ScopedLambda again.

* wtf/ParkingLot.cpp:
(WTF::ParkingLot::parkConditionallyImpl):
(WTF::ParkingLot::unparkOne):
(WTF::ParkingLot::unparkOneImpl):
* wtf/ParkingLot.h:
(WTF::ParkingLot::parkConditionally):
(WTF::ParkingLot::unparkOne):
* wtf/ScopedLambda.h:
(WTF::scopedLambda):

Tools:

Added a test case. This test crashes before the fix and now it passes.

* TestWebKitAPI/CMakeLists.txt:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/ScopedLambda.cpp: Added.
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@201433 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Source/WTF/ChangeLog
Source/WTF/wtf/ParkingLot.cpp
Source/WTF/wtf/ParkingLot.h
Source/WTF/wtf/ScopedLambda.h
Tools/ChangeLog
Tools/TestWebKitAPI/CMakeLists.txt
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/ScopedLambda.cpp [new file with mode: 0644]