Title: [286097] trunk/Source/WebKit
Revision
286097
Author
[email protected]
Date
2021-11-20 15:21:55 -0800 (Sat, 20 Nov 2021)

Log Message

Remove unnecessary flattening of SharedBuffer when sending them over IPC
https://bugs.webkit.org/show_bug.cgi?id=233363
rdar://85600684

Reviewed by Sam Weinig.

In several places, a SharedMemory was allocated followed by a copy. To perform
this copy, the SharedBuffer was flattened which could involve a memory allocation
followed by a copy of all segments into the final vector.
We instead make use of SharedBuffer::copyBuffer convenience method which will simply
copy the individual segment into the newly allocated SharedMemory.

No change in user observable behaviour.

Fly-by fix: SharedMemory::allocate is fallible; few instances didn't check the
returned value. So test that it succeeded and prevent a crash (null deref)

* Shared/WebHitTestResultData.cpp:
(WebKit::WebHitTestResultData::WebHitTestResultData):
* UIProcess/Cocoa/WebPasteboardProxyCocoa.mm:
(WebKit::WebPasteboardProxy::getPasteboardBufferForType):
(WebKit::WebPasteboardProxy::readBufferFromPasteboard):
* WebProcess/Plugins/PDF/PDFPlugin.mm:
(WebKit::PDFPlugin::writeItemsToPasteboard):
* WebProcess/WebCoreSupport/WebPlatformStrategies.cpp:
(WebKit::WebPlatformStrategies::setBufferForType):
* WebProcess/WebCoreSupport/mac/WebDragClientMac.mm:
(WebKit::WebDragClient::declareAndWriteDragImage):
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::performActionOnElement):
* WebProcess/WebPage/mac/WebPageMac.mm:
(WebKit::WebPage::getDataSelectionForPasteboard):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (286096 => 286097)


--- trunk/Source/WebKit/ChangeLog	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/ChangeLog	2021-11-20 23:21:55 UTC (rev 286097)
@@ -1,3 +1,38 @@
+2021-11-20  Jean-Yves Avenard  <[email protected]>
+
+        Remove unnecessary flattening of SharedBuffer when sending them over IPC
+        https://bugs.webkit.org/show_bug.cgi?id=233363
+        rdar://85600684
+
+        Reviewed by Sam Weinig.
+
+        In several places, a SharedMemory was allocated followed by a copy. To perform
+        this copy, the SharedBuffer was flattened which could involve a memory allocation
+        followed by a copy of all segments into the final vector.
+        We instead make use of SharedBuffer::copyBuffer convenience method which will simply
+        copy the individual segment into the newly allocated SharedMemory.
+
+        No change in user observable behaviour.
+
+        Fly-by fix: SharedMemory::allocate is fallible; few instances didn't check the
+        returned value. So test that it succeeded and prevent a crash (null deref)
+
+        * Shared/WebHitTestResultData.cpp:
+        (WebKit::WebHitTestResultData::WebHitTestResultData):
+        * UIProcess/Cocoa/WebPasteboardProxyCocoa.mm:
+        (WebKit::WebPasteboardProxy::getPasteboardBufferForType):
+        (WebKit::WebPasteboardProxy::readBufferFromPasteboard):
+        * WebProcess/Plugins/PDF/PDFPlugin.mm:
+        (WebKit::PDFPlugin::writeItemsToPasteboard):
+        * WebProcess/WebCoreSupport/WebPlatformStrategies.cpp:
+        (WebKit::WebPlatformStrategies::setBufferForType):
+        * WebProcess/WebCoreSupport/mac/WebDragClientMac.mm:
+        (WebKit::WebDragClient::declareAndWriteDragImage):
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::performActionOnElement):
+        * WebProcess/WebPage/mac/WebPageMac.mm:
+        (WebKit::WebPage::getDataSelectionForPasteboard):
+
 2021-11-20  Carlos Garcia Campos  <[email protected]>
 
         Report the initiating url instead of the redirected one

Modified: trunk/Source/WebKit/Shared/WebHitTestResultData.cpp (286096 => 286097)


--- trunk/Source/WebKit/Shared/WebHitTestResultData.cpp	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/Shared/WebHitTestResultData.cpp	2021-11-20 23:21:55 UTC (rev 286097)
@@ -87,8 +87,7 @@
     if (Image* image = hitTestResult.image()) {
         RefPtr<SharedBuffer> buffer = image->data();
         if (buffer) {
-            imageSharedMemory = WebKit::SharedMemory::allocate(buffer->size());
-            memcpy(imageSharedMemory->data(), buffer->data(), buffer->size());
+            imageSharedMemory = WebKit::SharedMemory::copyBuffer(*buffer);
             imageSize = buffer->size();
         }
     }

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebPasteboardProxyCocoa.mm (286096 => 286097)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebPasteboardProxyCocoa.mm	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebPasteboardProxyCocoa.mm	2021-11-20 23:21:55 UTC (rev 286097)
@@ -233,10 +233,9 @@
         uint64_t size = buffer->size();
         if (!size)
             return completionHandler({ });
-        RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(size);
+        auto sharedMemoryBuffer = SharedMemory::copyBuffer(*buffer);
         if (!sharedMemoryBuffer)
             return completionHandler({ });
-        memcpy(sharedMemoryBuffer->data(), buffer->data(), size);
         SharedMemory::Handle handle;
         if (!sharedMemoryBuffer->createHandle(handle, SharedMemory::Protection::ReadOnly))
             return completionHandler({ });
@@ -556,10 +555,9 @@
         uint64_t size = buffer->size();
         if (!size)
             return completionHandler({ });
-        RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(size);
+        auto sharedMemoryBuffer = SharedMemory::copyBuffer(*buffer);
         if (!sharedMemoryBuffer)
             return completionHandler({ });
-        memcpy(sharedMemoryBuffer->data(), buffer->data(), size);
         SharedMemory::Handle handle;
         if (!sharedMemoryBuffer->createHandle(handle, SharedMemory::Protection::ReadOnly))
             return completionHandler({ });

Modified: trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm (286096 => 286097)


--- trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm	2021-11-20 23:21:55 UTC (rev 286097)
@@ -2663,9 +2663,10 @@
             webProcess.parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardStringForType(pasteboardName, type, plainTextString.get(), pageIdentifier), Messages::WebPasteboardProxy::SetPasteboardStringForType::Reply(newChangeCount), 0);
         } else {
             auto buffer = SharedBuffer::create(data);
+            auto sharedMemory = SharedMemory::copyBuffer(buffer.get());
+            if (!sharedMemory)
+                continue;
             SharedMemory::Handle handle;
-            auto sharedMemory = SharedMemory::allocate(buffer->size());
-            memcpy(sharedMemory->data(), buffer->data(), buffer->size());
             sharedMemory->createHandle(handle, SharedMemory::Protection::ReadOnly);
             webProcess.parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardBufferForType(pasteboardName, type, SharedMemory::IPCHandle { WTFMove(handle), buffer->size() }, pageIdentifier), Messages::WebPasteboardProxy::SetPasteboardBufferForType::Reply(newChangeCount), 0);
         }

Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/WebPlatformStrategies.cpp (286096 => 286097)


--- trunk/Source/WebKit/WebProcess/WebCoreSupport/WebPlatformStrategies.cpp	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/WebPlatformStrategies.cpp	2021-11-20 23:21:55 UTC (rev 286097)
@@ -211,13 +211,11 @@
 {
     SharedMemory::Handle handle;
     if (buffer && buffer->size()) {
-        RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(buffer->size());
+        auto sharedMemoryBuffer = SharedMemory::copyBuffer(*buffer);
         // FIXME: Null check prevents crashing, but it is not great that we will have empty pasteboard content for this type,
         // because we've already set the types.
-        if (sharedMemoryBuffer) {
-            memcpy(sharedMemoryBuffer->data(), buffer->data(), buffer->size());
+        if (sharedMemoryBuffer)
             sharedMemoryBuffer->createHandle(handle, SharedMemory::Protection::ReadOnly);
-        }
     }
     int64_t newChangeCount { 0 };
     WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardBufferForType(pasteboardName, pasteboardType, SharedMemory::IPCHandle { WTFMove(handle), buffer ? buffer->size() : 0 }, pageIdentifier(context)), Messages::WebPasteboardProxy::SetPasteboardBufferForType::Reply(newChangeCount), 0);

Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/mac/WebDragClientMac.mm (286096 => 286097)


--- trunk/Source/WebKit/WebProcess/WebCoreSupport/mac/WebDragClientMac.mm	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/mac/WebDragClientMac.mm	2021-11-20 23:21:55 UTC (rev 286097)
@@ -152,12 +152,11 @@
     
     RefPtr<SharedBuffer> imageBuffer = image->image()->data();
     size_t imageSize = imageBuffer->size();
-    SharedMemory::Handle imageHandle;
-    
-    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(imageBuffer->size());
+
+    auto sharedMemoryBuffer = SharedMemory::copyBuffer(*imageBuffer);
     if (!sharedMemoryBuffer)
         return;
-    memcpy(sharedMemoryBuffer->data(), imageBuffer->data(), imageSize);
+    SharedMemory::Handle imageHandle;
     sharedMemoryBuffer->createHandle(imageHandle, SharedMemory::Protection::ReadOnly);
     
     RetainPtr<CFDataRef> data = "" ? archive->rawDataRepresentation() : 0;
@@ -165,11 +164,10 @@
     size_t archiveSize = 0;
     if (data) {
         auto archiveBuffer = SharedBuffer::create((__bridge NSData *)data.get());
-        RefPtr<SharedMemory> archiveSharedMemoryBuffer = SharedMemory::allocate(archiveBuffer->size());
+        auto archiveSharedMemoryBuffer = SharedMemory::copyBuffer(archiveBuffer.get());
         if (!archiveSharedMemoryBuffer)
             return;
         archiveSize = archiveBuffer->size();
-        memcpy(archiveSharedMemoryBuffer->data(), archiveBuffer->data(), archiveSize);
         archiveSharedMemoryBuffer->createHandle(archiveHandle, SharedMemory::Protection::ReadOnly);
     }
 

Modified: trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm (286096 => 286097)


--- trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2021-11-20 23:21:55 UTC (rev 286097)
@@ -3220,12 +3220,12 @@
         RefPtr<SharedBuffer> buffer = cachedImage->resourceBuffer();
         if (!buffer)
             return;
-        uint64_t bufferSize = buffer->size();
-        RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(bufferSize);
-        memcpy(sharedMemoryBuffer->data(), buffer->data(), bufferSize);
+        auto sharedMemoryBuffer = SharedMemory::copyBuffer(*buffer);
+        if (!sharedMemoryBuffer)
+            return;
         SharedMemory::Handle handle;
         sharedMemoryBuffer->createHandle(handle, SharedMemory::Protection::ReadOnly);
-        send(Messages::WebPageProxy::SaveImageToLibrary(SharedMemory::IPCHandle { WTFMove(handle), bufferSize }));
+        send(Messages::WebPageProxy::SaveImageToLibrary(SharedMemory::IPCHandle { WTFMove(handle), buffer->size() }));
     }
 }
 

Modified: trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm (286096 => 286097)


--- trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm	2021-11-20 14:43:28 UTC (rev 286096)
+++ trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm	2021-11-20 23:21:55 UTC (rev 286097)
@@ -569,12 +569,12 @@
     RefPtr<SharedBuffer> buffer = frame.editor().dataSelectionForPasteboard(pasteboardType);
     if (!buffer)
         return completionHandler({ });
-    uint64_t size = buffer->size();
-    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(size);
-    memcpy(sharedMemoryBuffer->data(), buffer->data(), size);
+    auto sharedMemoryBuffer = SharedMemory::copyBuffer(*buffer);
+    if (!sharedMemoryBuffer)
+        return completionHandler({ });
     SharedMemory::Handle handle;
     sharedMemoryBuffer->createHandle(handle, SharedMemory::Protection::ReadOnly);
-    completionHandler(SharedMemory::IPCHandle { WTFMove(handle), size });
+    completionHandler(SharedMemory::IPCHandle { WTFMove(handle), buffer->size() });
 }
 
 WKAccessibilityWebPageObject* WebPage::accessibilityRemoteObject()
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to