Addition review followup for:
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 6 May 2015 23:32:59 +0000 (23:32 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 6 May 2015 23:32:59 +0000 (23:32 +0000)
Crash executing null AsyncRequest in IDB code.
<rdar://problem/18854856> and https://bugs.webkit.org/show_bug.cgi?id=144715

Reviewed by Darin Adler.

This patch adds a layer on top of the UniqueIDBDatabase request map to catch this coding mistake in the future.

 * DatabaseProcess/IndexedDB/UniqueIDBDatabase.cpp:
(WebKit::UniqueIDBDatabase::didCompleteBoolRequest): Get rid of the ASSERT and the temporary variable,
  make it a single line which reads better.
(WebKit::UniqueIDBDatabase::didPutRecordInBackingStore): Ditto.
(WebKit::UniqueIDBDatabase::didGetRecordFromBackingStore): Ditto.
(WebKit::UniqueIDBDatabase::didOpenCursorInBackingStore): Ditto.
(WebKit::UniqueIDBDatabase::didAdvanceCursorInBackingStore): Ditto.
(WebKit::UniqueIDBDatabase::didIterateCursorInBackingStore): Ditto.
(WebKit::UniqueIDBDatabase::didCountInBackingStore): Ditto.
(WebKit::UniqueIDBDatabase::didDeleteRangeInBackingStore): Ditto.
* DatabaseProcess/IndexedDB/UniqueIDBDatabase.h: Ditto.

Add the class AsyncRequestMap which shadows a HashMap but does aggressive runtime checks
in ::take() to make sure the same request was not dispatched twice in a row, and makes sure we
never try to execute a null AsyncRequest by way of a RELEASE_ASSERT.
* Shared/AsyncRequest.h:
(WebKit::AsyncRequestMap::AsyncRequestMap):
(WebKit::AsyncRequestMap::take):
(WebKit::AsyncRequestMap::add):
(WebKit::AsyncRequestMap::clear):
(WebKit::AsyncRequestMap::values):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@183898 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/DatabaseProcess/IndexedDB/UniqueIDBDatabase.cpp
Source/WebKit2/DatabaseProcess/IndexedDB/UniqueIDBDatabase.h
Source/WebKit2/Shared/AsyncRequest.h

index 01f1709..f98186b 100644 (file)
@@ -1,5 +1,37 @@
 2015-05-06  Brady Eidson  <beidson@apple.com>
 
+        Addition review followup for:
+        Crash executing null AsyncRequest in IDB code.
+        <rdar://problem/18854856> and https://bugs.webkit.org/show_bug.cgi?id=144715
+
+        Reviewed by Darin Adler.
+
+        This patch adds a layer on top of the UniqueIDBDatabase request map to catch this coding mistake in the future. 
+         * DatabaseProcess/IndexedDB/UniqueIDBDatabase.cpp:
+        (WebKit::UniqueIDBDatabase::didCompleteBoolRequest): Get rid of the ASSERT and the temporary variable,
+          make it a single line which reads better.
+        (WebKit::UniqueIDBDatabase::didPutRecordInBackingStore): Ditto.
+        (WebKit::UniqueIDBDatabase::didGetRecordFromBackingStore): Ditto.
+        (WebKit::UniqueIDBDatabase::didOpenCursorInBackingStore): Ditto.
+        (WebKit::UniqueIDBDatabase::didAdvanceCursorInBackingStore): Ditto.
+        (WebKit::UniqueIDBDatabase::didIterateCursorInBackingStore): Ditto.
+        (WebKit::UniqueIDBDatabase::didCountInBackingStore): Ditto.
+        (WebKit::UniqueIDBDatabase::didDeleteRangeInBackingStore): Ditto.
+        * DatabaseProcess/IndexedDB/UniqueIDBDatabase.h: Ditto.
+
+        Add the class AsyncRequestMap which shadows a HashMap but does aggressive runtime checks
+        in ::take() to make sure the same request was not dispatched twice in a row, and makes sure we
+        never try to execute a null AsyncRequest by way of a RELEASE_ASSERT.
+        * Shared/AsyncRequest.h:
+        (WebKit::AsyncRequestMap::AsyncRequestMap):
+        (WebKit::AsyncRequestMap::take):
+        (WebKit::AsyncRequestMap::add):
+        (WebKit::AsyncRequestMap::clear):
+        (WebKit::AsyncRequestMap::values):
+
+2015-05-06  Brady Eidson  <beidson@apple.com>
+
         Crash executing null AsyncRequest in IDB code.
         <rdar://problem/18854856> and https://bugs.webkit.org/show_bug.cgi?id=144715
 
index 22b0a47..f7681e7 100644 (file)
@@ -405,10 +405,7 @@ void UniqueIDBDatabase::didDeleteIndex(uint64_t requestID, bool success)
 
 void UniqueIDBDatabase::didCompleteBoolRequest(uint64_t requestID, bool success)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(success);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(success);
 }
 
 void UniqueIDBDatabase::createObjectStore(const IDBIdentifier& transactionIdentifier, const IDBObjectStoreMetadata& metadata, std::function<void (bool)> successCallback)
@@ -889,10 +886,7 @@ void UniqueIDBDatabase::putRecordInBackingStore(uint64_t requestID, const IDBIde
 
 void UniqueIDBDatabase::didPutRecordInBackingStore(uint64_t requestID, const IDBKeyData& keyData, uint32_t errorCode, const String& errorMessage)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(keyData, errorCode, errorMessage);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(keyData, errorCode, errorMessage);
 }
 
 void UniqueIDBDatabase::getRecordFromBackingStore(uint64_t requestID, const IDBIdentifier& transaction, const IDBObjectStoreMetadata& objectStoreMetadata, int64_t indexID, const IDBKeyRangeData& keyRangeData, IndexedDB::CursorType cursorType)
@@ -950,10 +944,7 @@ void UniqueIDBDatabase::getRecordFromBackingStore(uint64_t requestID, const IDBI
 
 void UniqueIDBDatabase::didGetRecordFromBackingStore(uint64_t requestID, const IDBGetResult& result, uint32_t errorCode, const String& errorMessage)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(result, errorCode, errorMessage);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(result, errorCode, errorMessage);
 }
 
 void UniqueIDBDatabase::openCursorInBackingStore(uint64_t requestID, const IDBIdentifier& transactionIdentifier, int64_t objectStoreID, int64_t indexID, IndexedDB::CursorDirection cursorDirection, IndexedDB::CursorType cursorType, IDBDatabaseBackend::TaskType taskType, const IDBKeyRangeData& keyRange)
@@ -979,10 +970,7 @@ void UniqueIDBDatabase::openCursorInBackingStore(uint64_t requestID, const IDBId
 
 void UniqueIDBDatabase::didOpenCursorInBackingStore(uint64_t requestID, int64_t cursorID, const IDBKeyData& key, const IDBKeyData& primaryKey, const Vector<uint8_t>& valueBuffer, uint32_t errorCode, const String& errorMessage)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(cursorID, key, primaryKey, SharedBuffer::create(valueBuffer.data(), valueBuffer.size()), errorCode, errorMessage);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(cursorID, key, primaryKey, SharedBuffer::create(valueBuffer.data(), valueBuffer.size()), errorCode, errorMessage);
 }
 
 void UniqueIDBDatabase::advanceCursorInBackingStore(uint64_t requestID, const IDBIdentifier& cursorIdentifier, uint64_t count)
@@ -1004,10 +992,7 @@ void UniqueIDBDatabase::advanceCursorInBackingStore(uint64_t requestID, const ID
 
 void UniqueIDBDatabase::didAdvanceCursorInBackingStore(uint64_t requestID, const IDBKeyData& key, const IDBKeyData& primaryKey, const Vector<uint8_t>& valueBuffer, uint32_t errorCode, const String& errorMessage)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(key, primaryKey, SharedBuffer::create(valueBuffer.data(), valueBuffer.size()), errorCode, errorMessage);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(key, primaryKey, SharedBuffer::create(valueBuffer.data(), valueBuffer.size()), errorCode, errorMessage);
 }
 
 void UniqueIDBDatabase::iterateCursorInBackingStore(uint64_t requestID, const IDBIdentifier& cursorIdentifier, const IDBKeyData& iterateKey)
@@ -1029,10 +1014,7 @@ void UniqueIDBDatabase::iterateCursorInBackingStore(uint64_t requestID, const ID
 
 void UniqueIDBDatabase::didIterateCursorInBackingStore(uint64_t requestID, const IDBKeyData& key, const IDBKeyData& primaryKey, const Vector<uint8_t>& valueBuffer, uint32_t errorCode, const String& errorMessage)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(key, primaryKey, SharedBuffer::create(valueBuffer.data(), valueBuffer.size()), errorCode, errorMessage);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(key, primaryKey, SharedBuffer::create(valueBuffer.data(), valueBuffer.size()), errorCode, errorMessage);
 }
 
 void UniqueIDBDatabase::countInBackingStore(uint64_t requestID, const IDBIdentifier& transactionIdentifier, int64_t objectStoreID, int64_t indexID, const IDBKeyRangeData& keyRangeData)
@@ -1051,10 +1033,7 @@ void UniqueIDBDatabase::countInBackingStore(uint64_t requestID, const IDBIdentif
 
 void UniqueIDBDatabase::didCountInBackingStore(uint64_t requestID, int64_t count, uint32_t errorCode, const String& errorMessage)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(count, errorCode, errorMessage);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(count, errorCode, errorMessage);
 }
 
 void UniqueIDBDatabase::deleteRangeInBackingStore(uint64_t requestID, const IDBIdentifier& transactionIdentifier, int64_t objectStoreID, const IDBKeyRangeData& keyRangeData)
@@ -1073,10 +1052,7 @@ void UniqueIDBDatabase::deleteRangeInBackingStore(uint64_t requestID, const IDBI
 
 void UniqueIDBDatabase::didDeleteRangeInBackingStore(uint64_t requestID, uint32_t errorCode, const String& errorMessage)
 {
-    RefPtr<AsyncRequest> request = m_pendingDatabaseTasks.take(requestID);
-    ASSERT(request);
-
-    request->completeRequest(errorCode, errorMessage);
+    m_pendingDatabaseTasks.take(requestID).get().completeRequest(errorCode, errorMessage);
 }
 
 void UniqueIDBDatabase::didEstablishTransaction(const IDBIdentifier& transactionIdentifier, bool success)
index 029aa8c..dc6b4f0 100644 (file)
@@ -28,6 +28,7 @@
 
 #if ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
 
+#include "AsyncRequest.h"
 #include "IDBIdentifier.h"
 #include "UniqueIDBDatabaseIdentifier.h"
 #include <WebCore/IDBDatabaseBackend.h>
@@ -48,7 +49,6 @@ struct IDBKeyData;
 
 namespace WebKit {
 
-class AsyncRequest;
 class AsyncTask;
 class DatabaseProcessIDBConnection;
 class UniqueIDBDatabaseBackingStore;
@@ -195,7 +195,7 @@ private:
     Deque<RefPtr<AsyncRequest>> m_pendingMetadataRequests;
     HashMap<IDBIdentifier, RefPtr<AsyncRequest>> m_pendingTransactionRequests;
     HashSet<IDBIdentifier> m_pendingTransactionRollbacks;
-    HashMap<uint64_t, RefPtr<AsyncRequest>> m_pendingDatabaseTasks;
+    AsyncRequestMap m_pendingDatabaseTasks;
     RefPtr<AsyncRequest> m_pendingShutdownTask;
 
     std::unique_ptr<WebCore::IDBDatabaseMetadata> m_metadata;
index 08fb84e..7bd9d02 100644 (file)
@@ -28,6 +28,7 @@
 #define AsyncRequest_h
 
 #include <functional>
+#include <wtf/HashMap.h>
 #include <wtf/PassRefPtr.h>
 #include <wtf/RefCounted.h>
 
@@ -104,6 +105,49 @@ template<typename... Arguments> void AsyncRequest::completeRequest(Arguments&&..
     m_abortHandler = nullptr;
 }
 
+class AsyncRequestMap {
+public:
+    AsyncRequestMap()
+#ifndef NDEBUG
+        : m_lastRequestIDTaken(std::numeric_limits<uint64_t>::max())
+#endif
+    { }
+
+    Ref<AsyncRequest> take(uint64_t requestID)
+    {
+#ifndef NDEBUG
+        ASSERT_WITH_MESSAGE(requestID != m_lastRequestIDTaken, "Attempt to take the same AsyncRequest twice in a row. A background queue might have dispatched both an error callback and a success callback?");
+        m_lastRequestIDTaken = requestID;
+#endif
+
+        RefPtr<AsyncRequest> request = m_requestMap.take(requestID);
+        RELEASE_ASSERT(request);
+
+        return adoptRef(*request.leakRef());
+    }
+
+    void add(uint64_t requestID, PassRefPtr<AsyncRequest> request)
+    {
+        m_requestMap.add(requestID, request);
+    }
+
+    void clear()
+    {
+        m_requestMap.clear();
+    }
+
+    WTF::IteratorRange<HashMap<uint64_t, RefPtr<AsyncRequest>>::iterator::Values> values()
+    {
+        return m_requestMap.values();
+    }
+
+private:
+    HashMap<uint64_t, RefPtr<AsyncRequest>> m_requestMap;
+#ifndef NDEBUG
+    uint64_t m_lastRequestIDTaken;
+#endif
+};
+
 } // namespace WebKit
 
 #endif // AsyncRequest_h