Bug 238726 - [Unix] Add UnixFileDescriptor, use it in IPC::Semaphore
Summary: [Unix] Add UnixFileDescriptor, use it in IPC::Semaphore
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Zan Dobersek
URL:
Keywords: InRadar
Depends on:
Blocks: 238593 238733
  Show dependency treegraph
 
Reported: 2022-04-04 00:52 PDT by Zan Dobersek
Modified: 2022-04-05 06:09 PDT (History)
12 users (show)

See Also:


Attachments
Patch (13.75 KB, patch)
2022-04-04 00:53 PDT, Zan Dobersek
no flags Details | Formatted Diff | Diff
Patch (13.90 KB, patch)
2022-04-05 01:10 PDT, Zan Dobersek
no flags Details | Formatted Diff | Diff
Patch for landing (13.86 KB, patch)
2022-04-05 03:45 PDT, Zan Dobersek
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Zan Dobersek 2022-04-04 00:52:02 PDT
[Unix] Add UnixFileDescriptor, use it in IPC::Semaphore
Comment 1 Zan Dobersek 2022-04-04 00:53:13 PDT
Created attachment 456544 [details]
Patch
Comment 2 Adrian Perez 2022-04-04 14:18:59 PDT
Comment on attachment 456544 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=456544&action=review

> Source/WTF/wtf/unix/UnixFileDescriptor.h:51
> +    UnixFileDescriptor& operator=(const UnixFileDescriptor&) = delete;

You could replace these two with WTF_MAKE_NONCOPYABLE(UnixFileDescriptor)

> Source/WTF/wtf/unix/UnixFileDescriptor.h:56
> +        o.value = -1;

These two lines could be replaced with:

  value = std::exchange(o.value, -1);

> Source/WebKit/Platform/IPC/unix/IPCSemaphoreUnix.cpp:129
> +    encoder << m_fd.duplicate();

I think this is duplicating the UnixFileDescriptor twice unneedlessly,
because the ArgumentCoder<…>::encode() function below will take another
duplicate itself.

> Source/WebKit/Shared/WebCoreArgumentCoders.cpp:3298
> +    auto duplicate = fd.duplicate();

…here is where the second duplicate is made during encoding.
I think this one here should be the only one needed.
Comment 3 Zan Dobersek 2022-04-05 00:24:34 PDT
Comment on attachment 456544 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=456544&action=review

>> Source/WTF/wtf/unix/UnixFileDescriptor.h:51
>> +    UnixFileDescriptor& operator=(const UnixFileDescriptor&) = delete;
> 
> You could replace these two with WTF_MAKE_NONCOPYABLE(UnixFileDescriptor)

They are manually deleted just like the move constructor and assignment operators are manually defined. No need to reach out for additional macros.

>> Source/WebKit/Shared/WebCoreArgumentCoders.cpp:3298
>> +    auto duplicate = fd.duplicate();
> 
> …here is where the second duplicate is made during encoding.
> I think this one here should be the only one needed.

No, cause the `m_fd.duplicate()` in Semaphore::encode() returns an rvalue that's then passed to ArgumentCoder<>::encode(Encoder&, WTF::UnixFileDescriptor&&), which handles the object like the temporary that it is.
Comment 4 Carlos Garcia Campos 2022-04-05 00:45:15 PDT
Comment on attachment 456544 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=456544&action=review

I like the idea, but since the destructor is closing the fd, it's somehow like a smart pointer class, so I would follow the same patterns we are already familiar with. I would make it a class instead of a struct with get method

> Source/WTF/wtf/unix/UnixFileDescriptor.h:35
> +struct UnixFileDescriptor {

WTF_MAKE_NONCOPYABLE

> Source/WTF/wtf/unix/UnixFileDescriptor.h:44
> +    enum AdoptionTag { Adopt };
> +    UnixFileDescriptor(int fd, AdoptionTag)
> +        : value(fd)
> +    { }
> +
> +    enum DuplicationTag { Duplicate };
> +    UnixFileDescriptor(int fd, DuplicationTag)

Why not using a single enum and the same constructor that adopts or duplicates depending on the enum value. We could also make them private and add adoptFileDescriptor/duplicateFileDescritor.

>> Source/WTF/wtf/unix/UnixFileDescriptor.h:56
>> +        o.value = -1;
> 
> These two lines could be replaced with:
> 
>   value = std::exchange(o.value, -1);

that's release()

value = o.release();

> Source/WTF/wtf/unix/UnixFileDescriptor.h:82
> +    int value { -1 };

Being this a public member of the struct you can do foo.value = fd and mess it up.

> Source/WebKit/Platform/IPC/IPCSemaphore.h:84
> +    WTF::UnixFileDescriptor m_fd { };

We don't need the initialization now.

>> Source/WebKit/Shared/WebCoreArgumentCoders.cpp:3298
>> +    auto duplicate = fd.duplicate();
> 
> …here is where the second duplicate is made during encoding.
> I think this one here should be the only one needed.

I think it would be easier of IPC::Attachment used UnixFileDescriptor instead of int.
Comment 5 Zan Dobersek 2022-04-05 00:56:35 PDT
Comment on attachment 456544 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=456544&action=review

>> Source/WTF/wtf/unix/UnixFileDescriptor.h:44
>> +    UnixFileDescriptor(int fd, DuplicationTag)
> 
> Why not using a single enum and the same constructor that adopts or duplicates depending on the enum value. We could also make them private and add adoptFileDescriptor/duplicateFileDescritor.

Because that means generated branches from testing the enum value.

>>>> Source/WebKit/Shared/WebCoreArgumentCoders.cpp:3298
>>>> +    auto duplicate = fd.duplicate();
>>> 
>>> …here is where the second duplicate is made during encoding.
>>> I think this one here should be the only one needed.
>> 
>> No, cause the `m_fd.duplicate()` in Semaphore::encode() returns an rvalue that's then passed to ArgumentCoder<>::encode(Encoder&, WTF::UnixFileDescriptor&&), which handles the object like the temporary that it is.
> 
> I think it would be easier of IPC::Attachment used UnixFileDescriptor instead of int.

In a different patch, cause it will require additional changes in ConnectionUnix.cpp.
Comment 6 Zan Dobersek 2022-04-05 01:10:47 PDT
Created attachment 456681 [details]
Patch
Comment 7 Carlos Garcia Campos 2022-04-05 02:03:29 PDT
Comment on attachment 456681 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=456681&action=review

> Source/WTF/wtf/unix/UnixFileDescriptor.h:51
> +    enum DuplicationTag { Duplicate };
> +    UnixFileDescriptor(int fd, DuplicationTag)
> +    {
> +        if (fd >= 0)
> +            m_value = dupCloseOnExec(fd);
> +    }

I think we can keep this private for now, since it's always used from ::duplicate().

> Source/WTF/wtf/unix/UnixFileDescriptor.h:88
> +

using WTF::UnixFileDescriptor

> Source/WebKit/Platform/IPC/unix/IPCSemaphoreUnix.cpp:44
> +    m_fd = { eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE), WTF::UnixFileDescriptor::Adopt };

I still prefer m_fd = adoptFileDescriptor(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE)); but no strong opinion if you really want to expose WTF::UnixFileDescriptor::Adopt and WTF::UnixFileDescriptor::Duplicate

> Source/WebKit/Shared/WebCoreArgumentCoders.h:854
> +#if USE(UNIX_DOMAIN_SOCKETS)
> +
> +template<> struct ArgumentCoder<WTF::UnixFileDescriptor> {
> +    static void encode(Encoder&, const WTF::UnixFileDescriptor&);
> +    static void encode(Encoder&, WTF::UnixFileDescriptor&&);
> +    static std::optional<WTF::UnixFileDescriptor> decode(Decoder&);
> +};
> +
> +#endif

This doesn't really belong here, since UnixFileDescriptor is not a WebCore class. Why don't you add the encode/decode methods to UnixFileDescriptor class?
Comment 8 Zan Dobersek 2022-04-05 03:21:25 PDT
Comment on attachment 456681 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=456681&action=review

>> Source/WTF/wtf/unix/UnixFileDescriptor.h:51
>> +    }
> 
> I think we can keep this private for now, since it's always used from ::duplicate().

I need it in the next patches.

>> Source/WebKit/Shared/WebCoreArgumentCoders.h:854
>> +#endif
> 
> This doesn't really belong here, since UnixFileDescriptor is not a WebCore class. Why don't you add the encode/decode methods to UnixFileDescriptor class?

I don't have IPC::Attachment in WTF.
Comment 9 Zan Dobersek 2022-04-05 03:23:22 PDT
Comment on attachment 456681 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=456681&action=review

>>> Source/WebKit/Shared/WebCoreArgumentCoders.h:854
>>> +#endif
>> 
>> This doesn't really belong here, since UnixFileDescriptor is not a WebCore class. Why don't you add the encode/decode methods to UnixFileDescriptor class?
> 
> I don't have IPC::Attachment in WTF.

There's also WTF::MachSendRight in this file, even when the definitions are kept somewhere else.
Comment 10 Zan Dobersek 2022-04-05 03:45:15 PDT
Created attachment 456688 [details]
Patch for landing
Comment 11 Zan Dobersek (Reviews) 2022-04-05 03:48:57 PDT
Comment on attachment 456688 [details]
Patch for landing

Clearing flags on attachment: 456688

Committed r292389 (249254@trunk): <https://commits.webkit.org/249254@trunk>
Comment 12 Zan Dobersek (Reviews) 2022-04-05 03:49:04 PDT
All reviewed patches have been landed.  Closing bug.
Comment 13 Radar WebKit Bug Importer 2022-04-05 03:50:19 PDT
<rdar://problem/91286018>