Title: [292389] trunk/Source
Revision
292389
Author
zandober...@gmail.com
Date
2022-04-05 03:48:43 -0700 (Tue, 05 Apr 2022)

Log Message

[Unix] Add UnixFileDescriptor, use it in IPC::Semaphore
https://bugs.webkit.org/show_bug.cgi?id=238726

Reviewed by Carlos Garcia Campos.

Source/WebKit:

Use WTF::UnixFileDescriptor in IPC::Semaphore in place of a raw integer
value. This also simplifies other things, e.g. the move constructor and
assignment operator can now both be defaulted.

The IPC encoding and decoding of the semaphore object is also simpler,
leaning into the custom ArgumentCoder specialization for objects of the
WTF::UnixFileDescriptor type. The encoding and decoding is done by
chaperoning the file descriptor through IPC::Attachment.

* Platform/IPC/IPCSemaphore.h:
(IPC::Semaphore::operator bool const):
* Platform/IPC/unix/IPCSemaphoreUnix.cpp:
(IPC::Semaphore::Semaphore):
(IPC::Semaphore::signal):
(IPC::Semaphore::wait):
(IPC::Semaphore::waitFor):
(IPC::Semaphore::encode const):
(IPC::Semaphore::decode):
(IPC::Semaphore::destroy):
(IPC::Semaphore::operator=): Deleted.
* Shared/WebCoreArgumentCoders.cpp:
(IPC::ArgumentCoder<WTF::UnixFileDescriptor>::encode):
(IPC::ArgumentCoder<WTF::UnixFileDescriptor>::decode):
* Shared/WebCoreArgumentCoders.h:

Source/WTF:

Add the UnixFileDescriptor class, intended as a wrapper for a file
descriptor integer. Construction is a matter of adopting or duplicating
the passed-in file descriptor value. UnixFileDescriptor objects can be
moved around and explicitly duplicated, and the contained fd value can
be released.

The wrapper class makes it easier to handle file descriptor values when
embedded into other types as member variables. It also allows to specify
custom encoding and decoding in the WebKit IPC infrastructure.

* wtf/PlatformGTK.cmake:
* wtf/PlatformWPE.cmake:
* wtf/unix/UnixFileDescriptor.h: Added.
(WTF::UnixFileDescriptor::UnixFileDescriptor):
(WTF::UnixFileDescriptor::operator=):
(WTF::UnixFileDescriptor::~UnixFileDescriptor):
(WTF::UnixFileDescriptor::operator bool const):
(WTF::UnixFileDescriptor::value const):
(WTF::UnixFileDescriptor::duplicate const):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (292388 => 292389)


--- trunk/Source/WTF/ChangeLog	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WTF/ChangeLog	2022-04-05 10:48:43 UTC (rev 292389)
@@ -1,3 +1,30 @@
+2022-04-05  Zan Dobersek  <zdober...@igalia.com>
+
+        [Unix] Add UnixFileDescriptor, use it in IPC::Semaphore
+        https://bugs.webkit.org/show_bug.cgi?id=238726
+
+        Reviewed by Carlos Garcia Campos.
+
+        Add the UnixFileDescriptor class, intended as a wrapper for a file
+        descriptor integer. Construction is a matter of adopting or duplicating
+        the passed-in file descriptor value. UnixFileDescriptor objects can be
+        moved around and explicitly duplicated, and the contained fd value can
+        be released.
+
+        The wrapper class makes it easier to handle file descriptor values when
+        embedded into other types as member variables. It also allows to specify
+        custom encoding and decoding in the WebKit IPC infrastructure.
+
+        * wtf/PlatformGTK.cmake:
+        * wtf/PlatformWPE.cmake:
+        * wtf/unix/UnixFileDescriptor.h: Added.
+        (WTF::UnixFileDescriptor::UnixFileDescriptor):
+        (WTF::UnixFileDescriptor::operator=):
+        (WTF::UnixFileDescriptor::~UnixFileDescriptor):
+        (WTF::UnixFileDescriptor::operator bool const):
+        (WTF::UnixFileDescriptor::value const):
+        (WTF::UnixFileDescriptor::duplicate const):
+
 2022-04-04  Tim Horton  <timothy_hor...@apple.com>
 
         Remove GPU process system feature flags

Modified: trunk/Source/WTF/wtf/PlatformGTK.cmake (292388 => 292389)


--- trunk/Source/WTF/wtf/PlatformGTK.cmake	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WTF/wtf/PlatformGTK.cmake	2022-04-05 10:48:43 UTC (rev 292389)
@@ -13,6 +13,8 @@
     glib/WTFGType.h
 
     linux/RealTimeThreads.h
+
+    unix/UnixFileDescriptor.h
 )
 
 if (CMAKE_SYSTEM_NAME MATCHES "Linux")

Modified: trunk/Source/WTF/wtf/PlatformWPE.cmake (292388 => 292389)


--- trunk/Source/WTF/wtf/PlatformWPE.cmake	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WTF/wtf/PlatformWPE.cmake	2022-04-05 10:48:43 UTC (rev 292389)
@@ -13,6 +13,8 @@
     linux/ProcessMemoryFootprint.h
     linux/CurrentProcessMemoryStatus.h
     linux/RealTimeThreads.h
+
+    unix/UnixFileDescriptor.h
 )
 
 list(APPEND WTF_SOURCES

Added: trunk/Source/WTF/wtf/unix/UnixFileDescriptor.h (0 => 292389)


--- trunk/Source/WTF/wtf/unix/UnixFileDescriptor.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/unix/UnixFileDescriptor.h	2022-04-05 10:48:43 UTC (rev 292389)
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2022 Igalia S.L.
+ *
+ * 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. ``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
+ * 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 <utility>
+#include <wtf/Compiler.h>
+#include <wtf/Noncopyable.h>
+#include <wtf/StdLibExtras.h>
+#include <wtf/UniStdExtras.h>
+
+namespace WTF {
+
+class UnixFileDescriptor {
+    WTF_MAKE_NONCOPYABLE(UnixFileDescriptor);
+public:
+    UnixFileDescriptor() = default;
+
+    enum AdoptionTag { Adopt };
+    UnixFileDescriptor(int fd, AdoptionTag)
+        : m_value(fd)
+    { }
+
+    enum DuplicationTag { Duplicate };
+    UnixFileDescriptor(int fd, DuplicationTag)
+    {
+        if (fd >= 0)
+            m_value = dupCloseOnExec(fd);
+    }
+
+    UnixFileDescriptor(UnixFileDescriptor&& o)
+    {
+        m_value = o.release();
+    }
+
+    UnixFileDescriptor& operator=(UnixFileDescriptor&& o)
+    {
+        if (&o == this)
+            return *this;
+
+        this->~UnixFileDescriptor();
+        new (this) UnixFileDescriptor(WTFMove(o));
+        return *this;
+    }
+
+    ~UnixFileDescriptor()
+    {
+        if (m_value >= 0)
+            closeWithRetry(m_value);
+    }
+
+    explicit operator bool() const { return m_value >= 0; }
+
+    int value() const { return m_value; }
+
+    UnixFileDescriptor duplicate() const
+    {
+        return UnixFileDescriptor { m_value, Duplicate };
+    }
+
+    int release() WARN_UNUSED_RETURN { return std::exchange(m_value, -1); }
+
+private:
+    int m_value { -1 };
+};
+
+} // namespace WTF
+
+using WTF::UnixFileDescriptor;

Modified: trunk/Source/WebKit/ChangeLog (292388 => 292389)


--- trunk/Source/WebKit/ChangeLog	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WebKit/ChangeLog	2022-04-05 10:48:43 UTC (rev 292389)
@@ -1,3 +1,35 @@
+2022-04-05  Zan Dobersek  <zdober...@igalia.com>
+
+        [Unix] Add UnixFileDescriptor, use it in IPC::Semaphore
+        https://bugs.webkit.org/show_bug.cgi?id=238726
+
+        Reviewed by Carlos Garcia Campos.
+
+        Use WTF::UnixFileDescriptor in IPC::Semaphore in place of a raw integer
+        value. This also simplifies other things, e.g. the move constructor and
+        assignment operator can now both be defaulted.
+
+        The IPC encoding and decoding of the semaphore object is also simpler,
+        leaning into the custom ArgumentCoder specialization for objects of the
+        WTF::UnixFileDescriptor type. The encoding and decoding is done by
+        chaperoning the file descriptor through IPC::Attachment.
+
+        * Platform/IPC/IPCSemaphore.h:
+        (IPC::Semaphore::operator bool const):
+        * Platform/IPC/unix/IPCSemaphoreUnix.cpp:
+        (IPC::Semaphore::Semaphore):
+        (IPC::Semaphore::signal):
+        (IPC::Semaphore::wait):
+        (IPC::Semaphore::waitFor):
+        (IPC::Semaphore::encode const):
+        (IPC::Semaphore::decode):
+        (IPC::Semaphore::destroy):
+        (IPC::Semaphore::operator=): Deleted.
+        * Shared/WebCoreArgumentCoders.cpp:
+        (IPC::ArgumentCoder<WTF::UnixFileDescriptor>::encode):
+        (IPC::ArgumentCoder<WTF::UnixFileDescriptor>::decode):
+        * Shared/WebCoreArgumentCoders.h:
+
 2022-04-05  Megan Gardner  <megan_gard...@apple.com>
 
         Expand autocorrect context for more accurate results.

Modified: trunk/Source/WebKit/Platform/IPC/IPCSemaphore.h (292388 => 292389)


--- trunk/Source/WebKit/Platform/IPC/IPCSemaphore.h	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WebKit/Platform/IPC/IPCSemaphore.h	2022-04-05 10:48:43 UTC (rev 292389)
@@ -34,6 +34,8 @@
 #include <wtf/MachSendRight.h>
 #elif OS(WINDOWS)
 #include <windows.h>
+#elif USE(UNIX_DOMAIN_SOCKETS)
+#include <wtf/unix/UnixFileDescriptor.h>
 #endif
 
 namespace IPC {
@@ -65,9 +67,8 @@
 #elif OS(WINDOWS)
     explicit Semaphore(HANDLE);
 #elif USE(UNIX_DOMAIN_SOCKETS)
-    explicit Semaphore(int fd);
-
-    explicit operator bool() const { return m_fd != -1; }
+    explicit Semaphore(UnixFileDescriptor&&);
+    explicit operator bool() const { return !!m_fd; }
 #else
     explicit operator bool() const { return true; }
 #endif
@@ -80,7 +81,7 @@
 #elif OS(WINDOWS)
     HANDLE m_semaphoreHandle { nullptr };
 #elif USE(UNIX_DOMAIN_SOCKETS)
-    int m_fd { -1 };
+    UnixFileDescriptor m_fd;
 #endif
 };
 

Modified: trunk/Source/WebKit/Platform/IPC/unix/IPCSemaphoreUnix.cpp (292388 => 292389)


--- trunk/Source/WebKit/Platform/IPC/unix/IPCSemaphoreUnix.cpp	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WebKit/Platform/IPC/unix/IPCSemaphoreUnix.cpp	2022-04-05 10:48:43 UTC (rev 292389)
@@ -24,10 +24,11 @@
  */
 
 #include "config.h"
+#include "IPCSemaphore.h"
+
 #include "Decoder.h"
 #include "Encoder.h"
-#include "IPCSemaphore.h"
-
+#include "WebCoreArgumentCoders.h"
 #include <wtf/UniStdExtras.h>
 
 #if OS(LINUX)
@@ -40,19 +41,16 @@
 Semaphore::Semaphore()
 {
 #if OS(LINUX)
-    m_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE);
+    m_fd = { eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE), UnixFileDescriptor::Adopt };
 #endif
 }
 
-Semaphore::Semaphore(int fd)
-    : m_fd(fd)
+Semaphore::Semaphore(UnixFileDescriptor&& fd)
+    : m_fd(WTFMove(fd))
 { }
 
-Semaphore::Semaphore(Semaphore&& o)
-{
-    m_fd = o.m_fd;
-    o.m_fd = -1;
-}
+Semaphore::Semaphore(Semaphore&&) = default;
+Semaphore& Semaphore::operator=(Semaphore&&) = default;
 
 Semaphore::~Semaphore()
 {
@@ -59,26 +57,15 @@
     destroy();
 }
 
-Semaphore& Semaphore::operator=(Semaphore&& o)
-{
-    if (&o == this)
-        return *this;
-
-    destroy();
-    m_fd = o.m_fd;
-    o.m_fd = -1;
-    return *this;
-}
-
 void Semaphore::signal()
 {
 #if OS(LINUX)
-    ASSERT_WITH_MESSAGE(m_fd >= 0, "Signalling on an invalid semaphore object");
+    ASSERT_WITH_MESSAGE(m_fd.value() >= 0, "Signalling on an invalid semaphore object");
 
     // Matching waitImpl() and EFD_SEMAPHORE semantics, increment the semaphore value by 1.
     uint64_t value = 1;
     while (true) {
-        int ret = write(m_fd, &value, sizeof(uint64_t));
+        int ret = write(m_fd.value(), &value, sizeof(uint64_t));
         if (LIKELY(ret != -1 || errno != EINTR))
             break;
     }
@@ -115,9 +102,9 @@
 bool Semaphore::wait()
 {
 #if OS(LINUX)
-    ASSERT_WITH_MESSAGE(m_fd >= 0, "Waiting on an invalid semaphore object");
+    ASSERT_WITH_MESSAGE(m_fd.value() >= 0, "Waiting on an invalid semaphore object");
 
-    return waitImpl(m_fd, -1);
+    return waitImpl(m_fd.value(), -1);
 #else
     return false;
 #endif
@@ -126,12 +113,12 @@
 bool Semaphore::waitFor(Timeout timeout)
 {
 #if OS(LINUX)
-    ASSERT_WITH_MESSAGE(m_fd >= 0, "Waiting on an invalid semaphore object");
+    ASSERT_WITH_MESSAGE(m_fd.value() >= 0, "Waiting on an invalid semaphore object");
 
     int timeoutValue = -1;
     if (!timeout.isInfinity())
         timeoutValue = int(timeout.secondsUntilDeadline().milliseconds());
-    return waitImpl(m_fd, timeoutValue);
+    return waitImpl(m_fd.value(), timeoutValue);
 #else
     return false;
 #endif
@@ -139,25 +126,21 @@
 
 void Semaphore::encode(Encoder& encoder) const
 {
-    int duplicate = -1;
-    if (m_fd != -1)
-        duplicate = dupCloseOnExec(m_fd);
-
-    encoder.addAttachment(Attachment(duplicate));
+    encoder << m_fd.duplicate();
 }
 
 std::optional<Semaphore> Semaphore::decode(Decoder& decoder)
 {
-    auto attachment = decoder.takeLastAttachment();
-    if (!attachment)
+    std::optional<UnixFileDescriptor> fd;
+    decoder >> fd;
+    if (!fd)
         return std::nullopt;
-    return std::optional<Semaphore> { std::in_place, attachment->releaseFileDescriptor() };
+    return std::optional<Semaphore> { std::in_place, WTFMove(*fd) };
 }
 
 void Semaphore::destroy()
 {
-    if (m_fd >= 0)
-        closeWithRetry(m_fd);
+    m_fd = { };
 }
 
 } // namespace IPC

Modified: trunk/Source/WebKit/Shared/WebCoreArgumentCoders.cpp (292388 => 292389)


--- trunk/Source/WebKit/Shared/WebCoreArgumentCoders.cpp	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WebKit/Shared/WebCoreArgumentCoders.cpp	2022-04-05 10:48:43 UTC (rev 292389)
@@ -3291,4 +3291,27 @@
 
 #endif // ENABLE(IMAGE_ANALYSIS) && ENABLE(DATA_DETECTION)
 
+#if USE(UNIX_DOMAIN_SOCKETS)
+
+void ArgumentCoder<UnixFileDescriptor>::encode(Encoder& encoder, const UnixFileDescriptor& fd)
+{
+    auto duplicate = fd.duplicate();
+    encoder.addAttachment(Attachment(duplicate.release()));
+}
+
+void ArgumentCoder<UnixFileDescriptor>::encode(Encoder& encoder, UnixFileDescriptor&& fd)
+{
+    encoder.addAttachment(Attachment(fd.release()));
+}
+
+std::optional<UnixFileDescriptor> ArgumentCoder<UnixFileDescriptor>::decode(Decoder& decoder)
+{
+    auto attachment = decoder.takeLastAttachment();
+    if (!attachment)
+        return std::nullopt;
+    return std::optional<UnixFileDescriptor> { std::in_place, attachment->releaseFileDescriptor(), UnixFileDescriptor::Adopt };
+}
+
+#endif
+
 } // namespace IPC

Modified: trunk/Source/WebKit/Shared/WebCoreArgumentCoders.h (292388 => 292389)


--- trunk/Source/WebKit/Shared/WebCoreArgumentCoders.h	2022-04-05 09:40:14 UTC (rev 292388)
+++ trunk/Source/WebKit/Shared/WebCoreArgumentCoders.h	2022-04-05 10:48:43 UTC (rev 292389)
@@ -106,6 +106,10 @@
 }
 #endif
 
+#if USE(UNIX_DOMAIN_SOCKETS)
+#include <wtf/unix/UnixFileDescriptor.h>
+#endif
+
 OBJC_CLASS VKCImageAnalysis;
 
 #if USE(AVFOUNDATION)
@@ -839,6 +843,16 @@
 
 #endif
 
+#if USE(UNIX_DOMAIN_SOCKETS)
+
+template<> struct ArgumentCoder<UnixFileDescriptor> {
+    static void encode(Encoder&, const UnixFileDescriptor&);
+    static void encode(Encoder&, UnixFileDescriptor&&);
+    static std::optional<UnixFileDescriptor> decode(Decoder&);
+};
+
+#endif
+
 } // namespace IPC
 
 namespace WTF {
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to