Skip to content

Commit

Permalink
Generate serialization of ShareableResourceHandle
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=261351

Reviewed by Alex Christensen.

Migrate `ShareableResourceHandle` to the serialization format. Remove
manual serialization.

* Source/WebKit/CMakeLists.txt:
* Source/WebKit/DerivedSources-input.xcfilelist:
* Source/WebKit/DerivedSources.make:
* Source/WebKit/Shared/ShareableResource.cpp:
(WebKit::ShareableResourceHandle::ShareableResourceHandle):
(WebKit::ShareableResource::createHandle):
(WebKit::ShareableResourceHandle::encode): Deleted.
(WebKit::ShareableResourceHandle::decode): Deleted.
* Source/WebKit/Shared/ShareableResource.h:
* Source/WebKit/Shared/ShareableResource.serialization.in: Added.

Canonical link: https://commits.webkit.org/267876@main
  • Loading branch information
donny-dont committed Sep 11, 2023
1 parent 780a84b commit a3cca24
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
1 change: 1 addition & 0 deletions Source/WebKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ set(WebKit_SERIALIZATION_IN_FILES
Shared/SameDocumentNavigationType.serialization.in
Shared/SessionState.serialization.in
Shared/ShareableBitmap.serialization.in
Shared/ShareableResource.serialization.in
Shared/TextFlags.serialization.in
Shared/TextRecognitionResult.serialization.in
Shared/UpdateInfo.serialization.in
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources-input.xcfilelist
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ $(PROJECT_DIR)/Shared/RemoteWorkerType.serialization.in
$(PROJECT_DIR)/Shared/SameDocumentNavigationType.serialization.in
$(PROJECT_DIR)/Shared/SessionState.serialization.in
$(PROJECT_DIR)/Shared/ShareableBitmap.serialization.in
$(PROJECT_DIR)/Shared/ShareableResource.serialization.in
$(PROJECT_DIR)/Shared/Shared/EditorState.serialization.in
$(PROJECT_DIR)/Shared/TextFlags.serialization.in
$(PROJECT_DIR)/Shared/TextRecognitionResult.serialization.in
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources.make
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ SERIALIZATION_DESCRIPTION_FILES = \
Shared/SameDocumentNavigationType.serialization.in \
Shared/SessionState.serialization.in \
Shared/ShareableBitmap.serialization.in \
Shared/ShareableResource.serialization.in \
Shared/TextFlags.serialization.in \
Shared/TextRecognitionResult.serialization.in \
Shared/UserInterfaceIdiom.serialization.in \
Expand Down
32 changes: 5 additions & 27 deletions Source/WebKit/Shared/ShareableResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,11 @@ using namespace WebCore;

ShareableResourceHandle::ShareableResourceHandle() = default;

void ShareableResourceHandle::encode(IPC::Encoder& encoder) &&
{
encoder << WTFMove(m_handle);
encoder << m_offset;
encoder << m_size;
}

bool ShareableResourceHandle::decode(IPC::Decoder& decoder, ShareableResourceHandle& handle)
ShareableResourceHandle::ShareableResourceHandle(SharedMemory::Handle&& handle, unsigned offset, unsigned size)
: m_handle(WTFMove(handle))
, m_offset(offset)
, m_size(size)
{
SharedMemory::Handle memoryHandle;
if (UNLIKELY(!decoder.decode(memoryHandle)))
return false;
if (UNLIKELY(!decoder.decode(handle.m_offset)))
return false;
if (UNLIKELY(!decoder.decode(handle.m_size)))
return false;
auto neededSize = Checked<unsigned> { handle.m_offset } + handle.m_size;
if (UNLIKELY(neededSize.hasOverflowed()))
return false;
if (memoryHandle.size() < neededSize)
return false;
handle.m_handle = WTFMove(memoryHandle);
return true;
}

RefPtr<SharedBuffer> ShareableResource::wrapInSharedBuffer()
Expand Down Expand Up @@ -119,11 +101,7 @@ auto ShareableResource::createHandle() -> std::optional<Handle>
if (!memoryHandle)
return std::nullopt;

Handle handle;
handle.m_handle = WTFMove(*memoryHandle);
handle.m_offset = m_offset;
handle.m_size = m_size;
return { WTFMove(handle) };
return { Handle { WTFMove(*memoryHandle), m_offset, m_size } };
}

const uint8_t* ShareableResource::data() const
Expand Down
6 changes: 3 additions & 3 deletions Source/WebKit/Shared/ShareableResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ class ShareableResourceHandle {
public:
ShareableResourceHandle();
ShareableResourceHandle(ShareableResourceHandle&&) = default;
ShareableResourceHandle(SharedMemory::Handle&&, unsigned, unsigned);

ShareableResourceHandle& operator=(ShareableResourceHandle&&) = default;

bool isNull() const { return m_handle.isNull(); }
unsigned size() const { return m_size; }

void encode(IPC::Encoder&) &&;
static WARN_UNUSED_RETURN bool decode(IPC::Decoder&, ShareableResourceHandle&);

RefPtr<WebCore::SharedBuffer> tryWrapInSharedBuffer() &&;

private:
friend struct IPC::ArgumentCoder<ShareableResourceHandle, void>;
friend class ShareableResource;

SharedMemory::Handle m_handle;
Expand Down
29 changes: 29 additions & 0 deletions Source/WebKit/Shared/ShareableResource.serialization.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (C) 2023 Sony Interactive Entertainment Inc.
#
# 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.

header: "ShareableResource.h"

[CustomHeader, RValue] class WebKit::ShareableResourceHandle {
WebKit::SharedMemoryHandle m_handle;
unsigned m_offset;
[Validator='!(Checked<unsigned> { *m_offset } + *m_size).hasOverflowed() && (*m_offset + *m_size <= m_handle->size())'] unsigned m_size;
}

0 comments on commit a3cca24

Please sign in to comment.