- Revision
- 269327
- Author
- wenson_hs...@apple.com
- Date
- 2020-11-03 12:20:37 -0800 (Tue, 03 Nov 2020)
Log Message
Add helper methods to encode and decode IPC arguments as raw data
https://bugs.webkit.org/show_bug.cgi?id=218516
Reviewed by Geoff Garen.
Add new helper methods to `IPC::Encoder` and `IPC::Decoder` that can be used to convert anything that can be
sent as an IPC argument into raw bytes (i.e. using `WebCore::SharedBuffer`), as long as there are no IPC
attachments in the encoded data.
For more details, see <webkit.org/b/218406>.
* Platform/IPC/Decoder.cpp:
(IPC::Decoder::Decoder):
Add private versions of the Encoder and Decoder constructors that avoid IPC header data. These constructors are
called only from within `encodeSingleObject` and `decodeSingleObject`, respectively.
(IPC::m_bufferDeallocator):
* Platform/IPC/Decoder.h:
(IPC::Decoder::decodeSingleObject):
* Platform/IPC/Encoder.cpp:
(IPC::Encoder::Encoder):
(IPC::Encoder::releaseAttachments):
(IPC::Encoder::hasAttachments const):
* Platform/IPC/Encoder.h:
Modified Paths
Diff
Modified: trunk/Source/WebKit/ChangeLog (269326 => 269327)
--- trunk/Source/WebKit/ChangeLog 2020-11-03 20:16:14 UTC (rev 269326)
+++ trunk/Source/WebKit/ChangeLog 2020-11-03 20:20:37 UTC (rev 269327)
@@ -1,3 +1,31 @@
+2020-11-03 Wenson Hsieh <wenson_hs...@apple.com>
+
+ Add helper methods to encode and decode IPC arguments as raw data
+ https://bugs.webkit.org/show_bug.cgi?id=218516
+
+ Reviewed by Geoff Garen.
+
+ Add new helper methods to `IPC::Encoder` and `IPC::Decoder` that can be used to convert anything that can be
+ sent as an IPC argument into raw bytes (i.e. using `WebCore::SharedBuffer`), as long as there are no IPC
+ attachments in the encoded data.
+
+ For more details, see <webkit.org/b/218406>.
+
+ * Platform/IPC/Decoder.cpp:
+ (IPC::Decoder::Decoder):
+
+ Add private versions of the Encoder and Decoder constructors that avoid IPC header data. These constructors are
+ called only from within `encodeSingleObject` and `decodeSingleObject`, respectively.
+
+ (IPC::m_bufferDeallocator):
+ * Platform/IPC/Decoder.h:
+ (IPC::Decoder::decodeSingleObject):
+ * Platform/IPC/Encoder.cpp:
+ (IPC::Encoder::Encoder):
+ (IPC::Encoder::releaseAttachments):
+ (IPC::Encoder::hasAttachments const):
+ * Platform/IPC/Encoder.h:
+
2020-11-03 Stephan Szabo <stephan.sz...@sony.com>
[WinCairo/PlayStation] ICU 68.1 no longer exposes FALSE and TRUE macros by default
Modified: trunk/Source/WebKit/Platform/IPC/Decoder.cpp (269326 => 269327)
--- trunk/Source/WebKit/Platform/IPC/Decoder.cpp 2020-11-03 20:16:14 UTC (rev 269326)
+++ trunk/Source/WebKit/Platform/IPC/Decoder.cpp 2020-11-03 20:20:37 UTC (rev 269327)
@@ -74,6 +74,16 @@
return;
}
+Decoder::Decoder(const uint8_t* buffer, size_t bufferSize, ConstructWithoutHeaderTag)
+ : m_buffer { buffer }
+ , m_bufferPos { m_buffer }
+ , m_bufferEnd { m_buffer + bufferSize }
+ , m_bufferDeallocator([] (const uint8_t*, size_t) { })
+{
+ if (reinterpret_cast<uintptr_t>(m_buffer) % alignof(uint64_t))
+ markInvalid();
+}
+
Decoder::~Decoder()
{
ASSERT(m_buffer);
Modified: trunk/Source/WebKit/Platform/IPC/Decoder.h (269326 => 269327)
--- trunk/Source/WebKit/Platform/IPC/Decoder.h 2020-11-03 20:16:14 UTC (rev 269326)
+++ trunk/Source/WebKit/Platform/IPC/Decoder.h 2020-11-03 20:20:37 UTC (rev 269327)
@@ -30,6 +30,7 @@
#include "MessageNames.h"
#include "StringReference.h"
#include <WebCore/ContextMenuItem.h>
+#include <WebCore/SharedBuffer.h>
#include <wtf/OptionSet.h>
#include <wtf/Vector.h>
@@ -173,7 +174,22 @@
static const bool isIPCDecoder = true;
+ template <typename T>
+ static Optional<T> decodeSingleObject(const uint8_t* source, size_t numberOfBytes)
+ {
+ Optional<T> result;
+ Decoder decoder(source, numberOfBytes, ConstructWithoutHeader);
+ if (!decoder.isValid())
+ return WTF::nullopt;
+
+ decoder >> result;
+ return result;
+ }
+
private:
+ enum ConstructWithoutHeaderTag { ConstructWithoutHeader };
+ Decoder(const uint8_t* buffer, size_t bufferSize, ConstructWithoutHeaderTag);
+
bool alignBufferPosition(size_t alignment, size_t);
bool bufferIsLargeEnoughToContain(size_t alignment, size_t) const;
Modified: trunk/Source/WebKit/Platform/IPC/Encoder.cpp (269326 => 269327)
--- trunk/Source/WebKit/Platform/IPC/Encoder.cpp 2020-11-03 20:16:14 UTC (rev 269326)
+++ trunk/Source/WebKit/Platform/IPC/Encoder.cpp 2020-11-03 20:20:37 UTC (rev 269327)
@@ -72,6 +72,16 @@
encodeHeader();
}
+Encoder::Encoder(ConstructWithoutHeaderTag)
+ : m_messageName()
+ , m_destinationID(0)
+ , m_buffer(m_inlineBuffer)
+ , m_bufferPointer(m_inlineBuffer)
+ , m_bufferSize(0)
+ , m_bufferCapacity(sizeof(m_inlineBuffer))
+{
+}
+
Encoder::~Encoder()
{
if (m_buffer != m_inlineBuffer)
@@ -219,7 +229,12 @@
Vector<Attachment> Encoder::releaseAttachments()
{
- return WTFMove(m_attachments);
+ return std::exchange(m_attachments, { });
}
+bool Encoder::hasAttachments() const
+{
+ return !m_attachments.isEmpty();
+}
+
} // namespace IPC
Modified: trunk/Source/WebKit/Platform/IPC/Encoder.h (269326 => 269327)
--- trunk/Source/WebKit/Platform/IPC/Encoder.h 2020-11-03 20:16:14 UTC (rev 269326)
+++ trunk/Source/WebKit/Platform/IPC/Encoder.h 2020-11-03 20:20:37 UTC (rev 269327)
@@ -30,6 +30,7 @@
#include "MessageNames.h"
#include "StringReference.h"
#include <WebCore/ContextMenuItem.h>
+#include <WebCore/SharedBuffer.h>
#include <wtf/OptionSet.h>
#include <wtf/Vector.h>
@@ -99,7 +100,24 @@
static const bool isIPCEncoder = true;
+ template<typename T>
+ static RefPtr<WebCore::SharedBuffer> encodeSingleObject(const T& object)
+ {
+ Encoder encoder(ConstructWithoutHeader);
+ encoder << object;
+
+ if (encoder.hasAttachments()) {
+ ASSERT_NOT_REACHED();
+ return nullptr;
+ }
+
+ return WebCore::SharedBuffer::create(encoder.buffer(), encoder.bufferSize());
+ }
+
private:
+ enum ConstructWithoutHeaderTag { ConstructWithoutHeader };
+ Encoder(ConstructWithoutHeaderTag);
+
uint8_t* grow(size_t alignment, size_t);
template<typename E, std::enable_if_t<std::is_enum<E>::value>* = nullptr>
@@ -109,6 +127,8 @@
encode(WTF::enumToUnderlyingType<E>(enumValue));
}
+ bool hasAttachments() const;
+
void encodeHeader();
const OptionSet<MessageFlags>& messageFlags() const;
OptionSet<MessageFlags>& messageFlags();