Title: [285138] trunk/Source/WebKit
Revision
285138
Author
[email protected]
Date
2021-11-01 15:27:58 -0700 (Mon, 01 Nov 2021)

Log Message

Avoid heap-allocating ImportanceAssertion
https://bugs.webkit.org/show_bug.cgi?id=232579

Reviewed by Alex Christensen.

Avoid heap-allocating ImportanceAssertion since this is just a wrapper around a
mach_port.

Also update the ImportanceAssertion destructor to null out the mach port as
hardening given <rdar://75139294>.

* Platform/IPC/Decoder.cpp:
(IPC::Decoder::setImportanceAssertion):
* Platform/IPC/Decoder.h:
* Platform/IPC/cocoa/ConnectionCocoa.mm:
(IPC::Connection::receiveSourceEventHandler):
* Platform/IPC/cocoa/ImportanceAssertion.h:
(IPC::ImportanceAssertion::ImportanceAssertion):
(IPC::ImportanceAssertion::operator=):
(IPC::ImportanceAssertion::~ImportanceAssertion):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (285137 => 285138)


--- trunk/Source/WebKit/ChangeLog	2021-11-01 22:15:44 UTC (rev 285137)
+++ trunk/Source/WebKit/ChangeLog	2021-11-01 22:27:58 UTC (rev 285138)
@@ -1,3 +1,26 @@
+2021-11-01  Chris Dumez  <[email protected]>
+
+        Avoid heap-allocating ImportanceAssertion
+        https://bugs.webkit.org/show_bug.cgi?id=232579
+
+        Reviewed by Alex Christensen.
+
+        Avoid heap-allocating ImportanceAssertion since this is just a wrapper around a
+        mach_port.
+
+        Also update the ImportanceAssertion destructor to null out the mach port as
+        hardening given <rdar://75139294>.
+
+        * Platform/IPC/Decoder.cpp:
+        (IPC::Decoder::setImportanceAssertion):
+        * Platform/IPC/Decoder.h:
+        * Platform/IPC/cocoa/ConnectionCocoa.mm:
+        (IPC::Connection::receiveSourceEventHandler):
+        * Platform/IPC/cocoa/ImportanceAssertion.h:
+        (IPC::ImportanceAssertion::ImportanceAssertion):
+        (IPC::ImportanceAssertion::operator=):
+        (IPC::ImportanceAssertion::~ImportanceAssertion):
+
 2021-11-01  Per Arne  <[email protected]>
 
         [macOS] Opening local html files is failing

Modified: trunk/Source/WebKit/Platform/IPC/Decoder.cpp (285137 => 285138)


--- trunk/Source/WebKit/Platform/IPC/Decoder.cpp	2021-11-01 22:15:44 UTC (rev 285137)
+++ trunk/Source/WebKit/Platform/IPC/Decoder.cpp	2021-11-01 22:27:58 UTC (rev 285138)
@@ -147,7 +147,7 @@
 }
 
 #if PLATFORM(MAC)
-void Decoder::setImportanceAssertion(std::unique_ptr<ImportanceAssertion> assertion)
+void Decoder::setImportanceAssertion(ImportanceAssertion&& assertion)
 {
     m_importanceAssertion = WTFMove(assertion);
 }

Modified: trunk/Source/WebKit/Platform/IPC/Decoder.h (285137 => 285138)


--- trunk/Source/WebKit/Platform/IPC/Decoder.h	2021-11-01 22:15:44 UTC (rev 285137)
+++ trunk/Source/WebKit/Platform/IPC/Decoder.h	2021-11-01 22:27:58 UTC (rev 285138)
@@ -32,6 +32,10 @@
 #include <wtf/OptionSet.h>
 #include <wtf/Vector.h>
 
+#if PLATFORM(MAC)
+#include "ImportanceAssertion.h"
+#endif
+
 #if HAVE(QOS_CLASSES)
 #include <pthread/qos.h>
 #endif
@@ -38,7 +42,6 @@
 
 namespace IPC {
 
-class ImportanceAssertion;
 enum class MessageFlags : uint8_t;
 enum class ShouldDispatchWhenWaitingForSyncReply : uint8_t;
 
@@ -67,7 +70,7 @@
     bool shouldMaintainOrderingWithAsyncMessages() const;
 
 #if PLATFORM(MAC)
-    void setImportanceAssertion(std::unique_ptr<ImportanceAssertion>);
+    void setImportanceAssertion(ImportanceAssertion&&);
 #endif
 
     static std::unique_ptr<Decoder> unwrapForTesting(Decoder&);
@@ -173,7 +176,7 @@
     uint64_t m_destinationID;
 
 #if PLATFORM(MAC)
-    std::unique_ptr<ImportanceAssertion> m_importanceAssertion;
+    ImportanceAssertion m_importanceAssertion;
 #endif
 };
 

Modified: trunk/Source/WebKit/Platform/IPC/cocoa/ConnectionCocoa.mm (285137 => 285138)


--- trunk/Source/WebKit/Platform/IPC/cocoa/ConnectionCocoa.mm	2021-11-01 22:15:44 UTC (rev 285137)
+++ trunk/Source/WebKit/Platform/IPC/cocoa/ConnectionCocoa.mm	2021-11-01 22:27:58 UTC (rev 285138)
@@ -542,7 +542,7 @@
         return;
 
 #if PLATFORM(MAC)
-    decoder->setImportanceAssertion(makeUnique<ImportanceAssertion>(header));
+    decoder->setImportanceAssertion(ImportanceAssertion { header });
 #endif
 
     if (decoder->messageName() == MessageName::InitializeConnection) {

Modified: trunk/Source/WebKit/Platform/IPC/cocoa/ImportanceAssertion.h (285137 => 285138)


--- trunk/Source/WebKit/Platform/IPC/cocoa/ImportanceAssertion.h	2021-11-01 22:15:44 UTC (rev 285137)
+++ trunk/Source/WebKit/Platform/IPC/cocoa/ImportanceAssertion.h	2021-11-01 22:27:58 UTC (rev 285138)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -32,30 +32,44 @@
 namespace IPC {
 
 class ImportanceAssertion {
-    WTF_MAKE_FAST_ALLOCATED;
-    WTF_MAKE_NONCOPYABLE(ImportanceAssertion);
+public:
+    ImportanceAssertion() = default;
 
-public:
     explicit ImportanceAssertion(mach_msg_header_t* header)
-        : m_voucher(0)
     {
         if (MACH_MSGH_BITS_HAS_VOUCHER(header->msgh_bits)) {
-            m_voucher = header->msgh_voucher_port;
-            header->msgh_voucher_port = MACH_VOUCHER_NULL;
+            m_voucher = std::exchange(header->msgh_voucher_port, MACH_VOUCHER_NULL);
             header->msgh_bits &= ~(MACH_MSGH_BITS_VOUCHER_MASK | MACH_MSGH_BITS_RAISEIMP);
         }
     }
 
+    ImportanceAssertion(ImportanceAssertion&& other)
+        : m_voucher(std::exchange(other.m_voucher, MACH_VOUCHER_NULL))
+    {
+    }
+
+    ImportanceAssertion& operator=(ImportanceAssertion&& other)
+    {
+        if (&other != this)
+            std::swap(m_voucher, other.m_voucher);
+        return *this;
+    }
+
+    ImportanceAssertion(const ImportanceAssertion&) = delete;
+    ImportanceAssertion& operator=(const ImportanceAssertion&) = delete;
+
     ~ImportanceAssertion()
     {
-        if (m_voucher) {
-            kern_return_t kr = mach_voucher_deallocate(m_voucher);
-            ASSERT_UNUSED(kr, !kr);
-        }
+        if (!m_voucher)
+            return;
+
+        kern_return_t kr = mach_voucher_deallocate(m_voucher);
+        ASSERT_UNUSED(kr, !kr);
+        m_voucher = MACH_VOUCHER_NULL;
     }
 
 private:
-    mach_voucher_t m_voucher;
+    mach_voucher_t m_voucher { MACH_VOUCHER_NULL };
 };
 
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to