Changeset 183189 in webkit


Ignore:
Timestamp:
Apr 23, 2015 9:53:05 AM (9 years ago)
Author:
Carlos Garcia Campos
Message:

[UNIX] Do not allow copies of IPC::Attachment
https://bugs.webkit.org/show_bug.cgi?id=144096

Reviewed by Darin Adler.

It ensures that the file descriptor ownership is always correctly
transferred. This way we can remove the dispose() method to
explicitly close the file descriptor and always close it in the
Attachment destructor (unless explicitly transferred to
IPC::Connection or SharedMemory). It simplifies the code and
ensure we don't leak file descriptors.

  • Platform/IPC/ArgumentDecoder.cpp:

(IPC::ArgumentDecoder::~ArgumentDecoder): Remove the code to
explicitly dispose attachments.
(IPC::ArgumentDecoder::removeAttachment): Use WTF::move().

  • Platform/IPC/ArgumentEncoder.cpp:

(IPC::ArgumentEncoder::~ArgumentEncoder): Remove the code to
explicitly dispose attachments.
(IPC::ArgumentEncoder::addAttachment): Use WTF::move().
(IPC::ArgumentEncoder::releaseAttachments): Simplify by using WTF::move().

  • Platform/IPC/ArgumentEncoder.h:
  • Platform/IPC/Attachment.cpp:

(IPC::Attachment::encode): Move a copy of the attachment, and
reset the file descriptor, since the ownership is passed to the encoder.

  • Platform/IPC/Attachment.h: Make copy constructor and assignment

private to not allow public copies. The only copy allowed is done
by Attachment::encode(). Make m_fileDescriptor mutable so that we
can reset it in Attachment::encode() after passing the ownership
to the encoder.

  • Platform/IPC/unix/AttachmentUnix.cpp:

(IPC::Attachment::~Attachment): Close the file descriptor if it
hasn't been released explicitly.
(IPC::Attachment::dispose): Deleted.

  • Platform/IPC/unix/ConnectionUnix.cpp:

(IPC::Connection::processMessage): Do not use AttachmentResourceGuard.
(IPC::Connection::sendOutgoingMessage): Ditto.
(IPC::AttachmentResourceGuard::AttachmentResourceGuard): Deleted.
(IPC::AttachmentResourceGuard::~AttachmentResourceGuard): Deleted.

  • Platform/unix/SharedMemoryUnix.cpp:

(WebKit::SharedMemory::Handle::~Handle): Do not call clear().
(WebKit::SharedMemory::Handle::clear): Reset the attachment.

  • UIProcess/WebInspectorProxy.cpp:

(WebKit::WebInspectorProxy::createInspectorPage): Use WTF::move().

  • WebProcess/Plugins/PluginProcessConnectionManager.cpp:

(WebKit::PluginProcessConnectionManager::getPluginProcessConnection):
Call releaseFileDescriptor() instead of fileDescritpro() since the
ownership is passed to the connection.

Location:
trunk/Source/WebKit2
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r183188 r183189  
     12015-04-23  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        [UNIX] Do not allow copies of IPC::Attachment
     4        https://bugs.webkit.org/show_bug.cgi?id=144096
     5
     6        Reviewed by Darin Adler.
     7
     8        It ensures that the file descriptor ownership is always correctly
     9        transferred. This way we can remove the dispose() method to
     10        explicitly close the file descriptor and always close it in the
     11        Attachment destructor (unless explicitly transferred to
     12        IPC::Connection or SharedMemory). It simplifies the code and
     13        ensure we don't leak file descriptors.
     14
     15        * Platform/IPC/ArgumentDecoder.cpp:
     16        (IPC::ArgumentDecoder::~ArgumentDecoder): Remove the code to
     17        explicitly dispose attachments.
     18        (IPC::ArgumentDecoder::removeAttachment): Use WTF::move().
     19        * Platform/IPC/ArgumentEncoder.cpp:
     20        (IPC::ArgumentEncoder::~ArgumentEncoder): Remove the code to
     21        explicitly dispose attachments.
     22        (IPC::ArgumentEncoder::addAttachment): Use WTF::move().
     23        (IPC::ArgumentEncoder::releaseAttachments): Simplify by using WTF::move().
     24        * Platform/IPC/ArgumentEncoder.h:
     25        * Platform/IPC/Attachment.cpp:
     26        (IPC::Attachment::encode): Move a copy of the attachment, and
     27        reset the file descriptor, since the ownership is passed to the encoder.
     28        * Platform/IPC/Attachment.h: Make copy constructor and assignment
     29        private to not allow public copies. The only copy allowed is done
     30        by Attachment::encode(). Make m_fileDescriptor mutable so that we
     31        can reset it in Attachment::encode() after passing the ownership
     32        to the encoder.
     33        * Platform/IPC/unix/AttachmentUnix.cpp:
     34        (IPC::Attachment::~Attachment): Close the file descriptor if it
     35        hasn't been released explicitly.
     36        (IPC::Attachment::dispose): Deleted.
     37        * Platform/IPC/unix/ConnectionUnix.cpp:
     38        (IPC::Connection::processMessage): Do not use AttachmentResourceGuard.
     39        (IPC::Connection::sendOutgoingMessage): Ditto.
     40        (IPC::AttachmentResourceGuard::AttachmentResourceGuard): Deleted.
     41        (IPC::AttachmentResourceGuard::~AttachmentResourceGuard): Deleted.
     42        * Platform/unix/SharedMemoryUnix.cpp:
     43        (WebKit::SharedMemory::Handle::~Handle): Do not call clear().
     44        (WebKit::SharedMemory::Handle::clear): Reset the attachment.
     45        * UIProcess/WebInspectorProxy.cpp:
     46        (WebKit::WebInspectorProxy::createInspectorPage): Use WTF::move().
     47        * WebProcess/Plugins/PluginProcessConnectionManager.cpp:
     48        (WebKit::PluginProcessConnectionManager::getPluginProcessConnection):
     49        Call releaseFileDescriptor() instead of fileDescritpro() since the
     50        ownership is passed to the connection.
     51
    1522015-04-23  Alexey Proskuryakov  <ap@apple.com>
    253
  • trunk/Source/WebKit2/Platform/IPC/ArgumentDecoder.cpp

    r176762 r183189  
    4848    ASSERT(m_buffer);
    4949    free(m_buffer);
    50 #if !USE(UNIX_DOMAIN_SOCKETS)
    5150    // FIXME: We need to dispose of the mach ports in cases of failure.
    52 #else
    53     Vector<Attachment>::iterator end = m_attachments.end();
    54     for (Vector<Attachment>::iterator it = m_attachments.begin(); it != end; ++it)
    55         it->dispose();
    56 #endif
    5751}
    5852
     
    220214        return false;
    221215
    222     attachment = m_attachments.last();
    223     m_attachments.removeLast();
     216    attachment = m_attachments.takeLast();
    224217    return true;
    225218}
  • trunk/Source/WebKit2/Platform/IPC/ArgumentEncoder.cpp

    r179707 r183189  
    7171    if (m_buffer != m_inlineBuffer)
    7272        freeBuffer(m_buffer, m_bufferCapacity);
    73 
    74 #if !USE(UNIX_DOMAIN_SOCKETS)
    7573    // FIXME: We need to dispose of the attachments in cases of failure.
    76 #else
    77     for (size_t i = 0; i < m_attachments.size(); ++i)
    78         m_attachments[i].dispose();
    79 #endif
    8074}
    8175
     
    192186}
    193187
    194 void ArgumentEncoder::addAttachment(const Attachment& attachment)
     188void ArgumentEncoder::addAttachment(Attachment&& attachment)
    195189{
    196     m_attachments.append(attachment);
     190    m_attachments.append(WTF::move(attachment));
    197191}
    198192
    199193Vector<Attachment> ArgumentEncoder::releaseAttachments()
    200194{
    201     Vector<Attachment> newList;
    202     newList.swap(m_attachments);
    203     return newList;
     195    return WTF::move(m_attachments);
    204196}
    205197
  • trunk/Source/WebKit2/Platform/IPC/ArgumentEncoder.h

    r177543 r183189  
    6666    size_t bufferSize() const { return m_bufferSize; }
    6767
    68     void addAttachment(const Attachment&);
     68    void addAttachment(Attachment&&);
    6969    Vector<Attachment> releaseAttachments();
    7070    void reserve(size_t);
  • trunk/Source/WebKit2/Platform/IPC/Attachment.cpp

    r161148 r183189  
    5353void Attachment::encode(ArgumentEncoder& encoder) const
    5454{
    55     encoder.addAttachment(*this);
     55    encoder.addAttachment(WTF::move(*const_cast<Attachment*>(this)));
    5656}
    5757
  • trunk/Source/WebKit2/Platform/IPC/Attachment.h

    r183176 r183189  
    5656    Attachment(Attachment&&);
    5757    Attachment& operator=(Attachment&&);
    58     Attachment(const Attachment&) = default;
    59     Attachment& operator=(Attachment&) = default;
    6058    Attachment(int fileDescriptor, size_t);
    6159    Attachment(int fileDescriptor);
     60    ~Attachment();
    6261#endif
    6362
     
    7675    int releaseFileDescriptor() { int temp = m_fileDescriptor; m_fileDescriptor = -1; return temp; }
    7776    int fileDescriptor() const { return m_fileDescriptor; }
    78 
    79     void dispose();
    8077#endif
    8178
  • trunk/Source/WebKit2/Platform/IPC/unix/AttachmentUnix.cpp

    r183176 r183189  
    6868}
    6969
    70 void Attachment::dispose()
     70Attachment::~Attachment()
    7171{
    72     if (m_fileDescriptor == -1)
    73         return;
    74 
    75     closeWithRetry(m_fileDescriptor);
    76     m_fileDescriptor = -1;
     72    if (m_fileDescriptor != -1)
     73        closeWithRetry(m_fileDescriptor);
    7774}
    7875
  • trunk/Source/WebKit2/Platform/IPC/unix/ConnectionUnix.cpp

    r183176 r183189  
    156156}
    157157
    158 template<class T, class iterator>
    159 class AttachmentResourceGuard {
    160 public:
    161     AttachmentResourceGuard(T& attachments)
    162         : m_attachments(attachments)
    163     {
    164     }
    165     ~AttachmentResourceGuard()
    166     {
    167         iterator end = m_attachments.end();
    168         for (iterator i = m_attachments.begin(); i != end; ++i)
    169             i->dispose();
    170     }
    171 private:
    172     T& m_attachments;
    173 };
    174 
    175158bool Connection::processMessage()
    176159{
     
    215198
    216199    Vector<Attachment> attachments(attachmentCount);
    217     AttachmentResourceGuard<Vector<Attachment>, Vector<Attachment>::iterator> attachementDisposer(attachments);
    218200    RefPtr<WebKit::SharedMemory> oolMessageBody;
    219201
     
    424406
    425407    Vector<Attachment> attachments = encoder->releaseAttachments();
    426     AttachmentResourceGuard<Vector<Attachment>, Vector<Attachment>::iterator> attachementDisposer(attachments);
    427 
    428408    if (attachments.size() > (attachmentMaxAmount - 1)) {
    429409        ASSERT_NOT_REACHED();
  • trunk/Source/WebKit2/Platform/unix/SharedMemoryUnix.cpp

    r183184 r183189  
    5353SharedMemory::Handle::~Handle()
    5454{
    55     clear();
    5655}
    5756
    5857void SharedMemory::Handle::clear()
    5958{
    60     m_attachment.dispose();
     59    m_attachment = IPC::Attachment();
    6160}
    6261
  • trunk/Source/WebKit2/UIProcess/WebInspectorProxy.cpp

    r182447 r183189  
    487487
    488488    m_underTest = underTest;
    489     m_connectionIdentifier = connectionIdentifier;
     489    m_connectionIdentifier = WTF::move(connectionIdentifier);
    490490
    491491    m_inspectorPage->process().send(Messages::WebInspectorUI::EstablishConnection(m_connectionIdentifier, m_inspectedPage->pageID(), m_underTest), m_inspectorPage->pageID());
  • trunk/Source/WebKit2/WebProcess/Plugins/PluginProcessConnectionManager.cpp

    r180054 r183189  
    7777#if OS(DARWIN)
    7878    IPC::Connection::Identifier connectionIdentifier(encodedConnectionIdentifier.port());
     79#elif USE(UNIX_DOMAIN_SOCKETS)
     80    IPC::Connection::Identifier connectionIdentifier = encodedConnectionIdentifier.releaseFileDescriptor();
     81#endif
    7982    if (IPC::Connection::identifierIsNull(connectionIdentifier))
    80         return 0;
    81 #elif USE(UNIX_DOMAIN_SOCKETS)
    82     IPC::Connection::Identifier connectionIdentifier = encodedConnectionIdentifier.fileDescriptor();
    83     if (connectionIdentifier == -1)
    84         return 0;
    85 #endif
     83        return nullptr;
    8684
    8785    RefPtr<PluginProcessConnection> pluginProcessConnection = PluginProcessConnection::create(this, pluginProcessToken, connectionIdentifier, supportsAsynchronousInitialization);
Note: See TracChangeset for help on using the changeset viewer.