Title: [209418] trunk/Source/WebKit2
Revision
209418
Author
ander...@apple.com
Date
2016-12-06 13:59:51 -0800 (Tue, 06 Dec 2016)

Log Message

Add a new MachMessage class and use it for sending outgoing messages
https://bugs.webkit.org/show_bug.cgi?id=165484

Reviewed by Sam Weinig.

* Platform/IPC/mac/ConnectionMac.mm:
(IPC::Connection::sendOutgoingMessage):
Instead of trying to randomly use memory from the stack as well as using mmap for the message data for out of line messages
(which makes no sense at all, since the body data is already mmapped), just always create a MachMessage object and use it.

(IPC::machMessageSize): Deleted.
This has been moved to MachMessage::messageSize().

* Platform/IPC/mac/MachMessage.cpp: Added.
(IPC::MachMessage::create):
Allocate extra memory to store the actual buffer.

(IPC::MachMessage::MachMessage):
Initialize variables. m_size isn't really used right now, but will be in a subsequent patch.

(IPC::MachMessage::header):
Return the header.

* Platform/IPC/mac/MachMessage.h: Added.
(IPC::MachMessage::length):

* WebKit2.xcodeproj/project.pbxproj:
Add new files.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (209417 => 209418)


--- trunk/Source/WebKit2/ChangeLog	2016-12-06 21:52:18 UTC (rev 209417)
+++ trunk/Source/WebKit2/ChangeLog	2016-12-06 21:59:51 UTC (rev 209418)
@@ -1,3 +1,34 @@
+2016-12-06  Anders Carlsson  <ander...@apple.com>
+
+        Add a new MachMessage class and use it for sending outgoing messages
+        https://bugs.webkit.org/show_bug.cgi?id=165484
+
+        Reviewed by Sam Weinig.
+
+        * Platform/IPC/mac/ConnectionMac.mm:
+        (IPC::Connection::sendOutgoingMessage):
+        Instead of trying to randomly use memory from the stack as well as using mmap for the message data for out of line messages
+        (which makes no sense at all, since the body data is already mmapped), just always create a MachMessage object and use it.
+        
+        (IPC::machMessageSize): Deleted.
+        This has been moved to MachMessage::messageSize().
+        
+        * Platform/IPC/mac/MachMessage.cpp: Added.
+        (IPC::MachMessage::create):
+        Allocate extra memory to store the actual buffer.
+
+        (IPC::MachMessage::MachMessage):
+        Initialize variables. m_size isn't really used right now, but will be in a subsequent patch.
+
+        (IPC::MachMessage::header):
+        Return the header.
+        
+        * Platform/IPC/mac/MachMessage.h: Added.
+        (IPC::MachMessage::length):
+
+        * WebKit2.xcodeproj/project.pbxproj:
+        Add new files.
+
 2016-12-06  Simon Fraser  <simon.fra...@apple.com>
 
         Enable visual viewports by default on Mac, and iOS Wk2

Modified: trunk/Source/WebKit2/Platform/IPC/mac/ConnectionMac.mm (209417 => 209418)


--- trunk/Source/WebKit2/Platform/IPC/mac/ConnectionMac.mm	2016-12-06 21:52:18 UTC (rev 209417)
+++ trunk/Source/WebKit2/Platform/IPC/mac/ConnectionMac.mm	2016-12-06 21:59:51 UTC (rev 209418)
@@ -28,6 +28,7 @@
 
 #include "DataReference.h"
 #include "ImportanceAssertion.h"
+#include "MachMessage.h"
 #include "MachPort.h"
 #include "MachUtilities.h"
 #include <WebCore/AXObjectCache.h>
@@ -258,19 +259,6 @@
     return true;
 }
 
-static inline size_t machMessageSize(size_t bodySize, size_t numberOfPortDescriptors = 0, size_t numberOfOOLMemoryDescriptors = 0)
-{
-    size_t size = sizeof(mach_msg_header_t) + bodySize;
-    if (numberOfPortDescriptors || numberOfOOLMemoryDescriptors) {
-        size += sizeof(mach_msg_body_t);
-        if (numberOfPortDescriptors)
-            size += (numberOfPortDescriptors * sizeof(mach_msg_port_descriptor_t));
-        if (numberOfOOLMemoryDescriptors)
-            size += (numberOfOOLMemoryDescriptors * sizeof(mach_msg_ool_descriptor_t));
-    }
-    return round_msg(size);
-}
-
 bool Connection::platformCanSendOutgoingMessages() const
 {
     return true;
@@ -288,7 +276,7 @@
             numberOfPortDescriptors++;
     }
     
-    size_t messageSize = machMessageSize(encoder->bufferSize(), numberOfPortDescriptors, numberOfOOLMemoryDescriptors);
+    size_t messageSize = MachMessage::messageSize(encoder->bufferSize(), numberOfPortDescriptors, numberOfOOLMemoryDescriptors);
 
     bool messageBodyIsOOL = false;
     if (messageSize > inlineMessageMaxSize) {
@@ -295,20 +283,14 @@
         messageBodyIsOOL = true;
 
         numberOfOOLMemoryDescriptors++;
-        messageSize = machMessageSize(0, numberOfPortDescriptors, numberOfOOLMemoryDescriptors);
+        messageSize = MachMessage::messageSize(0, numberOfPortDescriptors, numberOfOOLMemoryDescriptors);
     }
 
-    char stackBuffer[inlineMessageMaxSize];
-    char* buffer = &stackBuffer[0];
-    if (messageSize > inlineMessageMaxSize) {
-        buffer = (char*)mmap(0, messageSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
-        if (buffer == MAP_FAILED)
-            return false;
-    }
+    auto message = MachMessage::create(messageSize);
 
-    bool isComplex = (numberOfPortDescriptors + numberOfOOLMemoryDescriptors > 0);
+    bool isComplex = (numberOfPortDescriptors + numberOfOOLMemoryDescriptors) > 0;
 
-    mach_msg_header_t* header = reinterpret_cast<mach_msg_header_t*>(buffer);
+    mach_msg_header_t* header = message->header();
     header->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
     header->msgh_size = messageSize;
     header->msgh_remote_port = m_sendPort;
@@ -371,9 +353,6 @@
         // FIXME: What should we do here?
     }
 
-    if (buffer != &stackBuffer[0])
-        munmap(buffer, messageSize);
-
     return true;
 }
 

Added: trunk/Source/WebKit2/Platform/IPC/mac/MachMessage.cpp (0 => 209418)


--- trunk/Source/WebKit2/Platform/IPC/mac/MachMessage.cpp	                        (rev 0)
+++ trunk/Source/WebKit2/Platform/IPC/mac/MachMessage.cpp	2016-12-06 21:59:51 UTC (rev 209418)
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "MachMessage.h"
+
+namespace IPC {
+
+std::unique_ptr<MachMessage> MachMessage::create(size_t length)
+{
+    void* memory = WTF::fastMalloc(sizeof(MachMessage) + length);
+    return std::unique_ptr<MachMessage> { new (NotNull, memory) MachMessage { length } };
+}
+
+MachMessage::MachMessage(size_t length)
+    : m_length { length }
+{
+}
+
+MachMessage::~MachMessage()
+{
+}
+
+size_t MachMessage::messageSize(size_t bodySize, size_t portDescriptorCount, size_t memoryDescriptorCount)
+{
+    size_t messageSize = sizeof(mach_msg_header_t) + bodySize;
+
+    if (portDescriptorCount || memoryDescriptorCount) {
+        messageSize += sizeof(mach_msg_body_t);
+
+        if (portDescriptorCount)
+            messageSize += (portDescriptorCount * sizeof(mach_msg_port_descriptor_t));
+        if (memoryDescriptorCount)
+            messageSize += (memoryDescriptorCount * sizeof(mach_msg_ool_descriptor_t));
+    }
+
+    return round_msg(messageSize);
+}
+
+mach_msg_header_t* MachMessage::header()
+{
+    return reinterpret_cast<mach_msg_header_t*>(m_buffer);
+}
+
+}

Added: trunk/Source/WebKit2/Platform/IPC/mac/MachMessage.h (0 => 209418)


--- trunk/Source/WebKit2/Platform/IPC/mac/MachMessage.h	                        (rev 0)
+++ trunk/Source/WebKit2/Platform/IPC/mac/MachMessage.h	2016-12-06 21:59:51 UTC (rev 209418)
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2016 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <memory>
+
+namespace IPC {
+
+class MachMessage {
+    WTF_MAKE_FAST_ALLOCATED;
+public:
+    static std::unique_ptr<MachMessage> create(size_t length);
+    ~MachMessage();
+
+    static size_t messageSize(size_t bodySize, size_t portDescriptorCount, size_t memoryDescriptorCount);
+
+    size_t length() const { return m_length; }
+    mach_msg_header_t* header();
+
+private:
+    MachMessage(size_t length);
+
+    size_t m_length;
+    uint8_t m_buffer[0];
+};
+
+}

Modified: trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj (209417 => 209418)


--- trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj	2016-12-06 21:52:18 UTC (rev 209417)
+++ trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj	2016-12-06 21:59:51 UTC (rev 209418)
@@ -284,6 +284,8 @@
 		1A6563E51B7A8C50009CF787 /* APIWindowFeatures.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6563E31B7A8C50009CF787 /* APIWindowFeatures.h */; };
 		1A66BF8F18A052ED002071B4 /* WKWebViewInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A66BF8E18A052ED002071B4 /* WKWebViewInternal.h */; };
 		1A67CD2E1CBC513F00BFE3EA /* WKOpenPanelParametersInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A67CD2D1CBC513F00BFE3EA /* WKOpenPanelParametersInternal.h */; };
+		1A6D86C11DF75265007745E8 /* MachMessage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A6D86BF1DF75265007745E8 /* MachMessage.cpp */; };
+		1A6D86C21DF75265007745E8 /* MachMessage.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6D86C01DF75265007745E8 /* MachMessage.h */; };
 		1A6FA21E1BD0435B00AAA650 /* WKFrameInfoPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6FA21D1BD0435B00AAA650 /* WKFrameInfoPrivate.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		1A6FB7AE11E64B6800DB1371 /* PluginView.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A6FB7AC11E64B6800DB1371 /* PluginView.cpp */; };
 		1A6FB7AF11E64B6800DB1371 /* PluginView.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6FB7AD11E64B6800DB1371 /* PluginView.h */; };
@@ -2312,6 +2314,8 @@
 		1A67CD2D1CBC513F00BFE3EA /* WKOpenPanelParametersInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKOpenPanelParametersInternal.h; sourceTree = "<group>"; };
 		1A6D141F1B0167D500785FF0 /* Info-OSX-10.9-10.10.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-OSX-10.9-10.10.plist"; sourceTree = "<group>"; };
 		1A6D14211B01681600785FF0 /* PluginService.32-64-10.9-10.10.Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "PluginService.32-64-10.9-10.10.Info.plist"; path = "PluginProcess/EntryPoint/mac/XPCService/PluginService.32-64-10.9-10.10.Info.plist"; sourceTree = SOURCE_ROOT; };
+		1A6D86BF1DF75265007745E8 /* MachMessage.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MachMessage.cpp; sourceTree = "<group>"; };
+		1A6D86C01DF75265007745E8 /* MachMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MachMessage.h; sourceTree = "<group>"; };
 		1A6FA21D1BD0435B00AAA650 /* WKFrameInfoPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKFrameInfoPrivate.h; sourceTree = "<group>"; };
 		1A6FB7AC11E64B6800DB1371 /* PluginView.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PluginView.cpp; sourceTree = "<group>"; };
 		1A6FB7AD11E64B6800DB1371 /* PluginView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PluginView.h; sourceTree = "<group>"; };
@@ -6978,6 +6982,8 @@
 			children = (
 				1A30EAC5115D7DA30053E937 /* ConnectionMac.mm */,
 				1A1EC69D1872092100B951F0 /* ImportanceAssertion.h */,
+				1A6D86BF1DF75265007745E8 /* MachMessage.cpp */,
+				1A6D86C01DF75265007745E8 /* MachMessage.h */,
 				BCC56F771159957D001CCAF9 /* MachPort.h */,
 			);
 			path = mac;
@@ -7633,6 +7639,7 @@
 				2DF9EEEC1A7836EE00B6CFBE /* APINavigationAction.h in Headers */,
 				2DD9EB2D1A6F012500BB1267 /* APINavigationClient.h in Headers */,
 				BCF69FA21176D01400471A52 /* APINavigationData.h in Headers */,
+				1A6D86C21DF75265007745E8 /* MachMessage.h in Headers */,
 				2DF9EEEE1A786EAD00B6CFBE /* APINavigationResponse.h in Headers */,
 				BC33DD681238464600360F3F /* APINumber.h in Headers */,
 				BC857FB512B830E600EDEB2E /* APIOpenPanelParameters.h in Headers */,
@@ -9522,6 +9529,7 @@
 				F6A90813133C20510082C3F4 /* WebCookieManagerMac.mm in Sources */,
 				330934471315B9220097A7BC /* WebCookieManagerMessageReceiver.cpp in Sources */,
 				330934551315B9750097A7BC /* WebCookieManagerProxy.cpp in Sources */,
+				1A6D86C11DF75265007745E8 /* MachMessage.cpp in Sources */,
 				33AA1066131F060000D4A575 /* WebCookieManagerProxyClient.cpp in Sources */,
 				F6D632BC133D198200743D77 /* WebCookieManagerProxyMac.mm in Sources */,
 				330934491315B9220097A7BC /* WebCookieManagerProxyMessageReceiver.cpp in Sources */,
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to