Title: [291428] trunk
Revision
291428
Author
[email protected]
Date
2022-03-17 12:56:14 -0700 (Thu, 17 Mar 2022)

Log Message

Source/WebKit:
Add read and write methods to JSIPCStreamConnectionBuffer
https://bugs.webkit.org/show_bug.cgi?id=237730
rdar://problem/89676460

Patch by Simon Lewis <[email protected]> on 2022-03-17
Reviewed by Kimmo Kinnunen.

When using IPCTestingAPI, this change enables the underlying bytes
of a StreamClientConnection buffer to be modified.

Test: ipc/stream-buffer-read-write.html

* Platform/IPC/StreamClientConnection.cpp:
(IPC::StreamClientConnection::bufferForTesting):
* Platform/IPC/StreamClientConnection.h:
* Platform/IPC/StreamConnectionBuffer.cpp:
(IPC::StreamConnectionBuffer::headerForTesting):
(IPC::StreamConnectionBuffer::dataForTesting):
* Platform/IPC/StreamConnectionBuffer.h:
* WebProcess/WebPage/IPCTestingAPI.cpp:
(WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::staticFunctions):
(WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::readHeaderBytes):
(WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::readDataBytes):
(WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::readBytes):
(WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::writeHeaderBytes):
(WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::writeDataBytes):
(WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::writeBytes):

LayoutTests:
Functional tests for read and write methods of JSIPCStreamConnectionBuffer
https://bugs.webkit.org/show_bug.cgi?id=237730
rdar://problem/89676460

Patch by Simon Lewis <[email protected]> on 2022-03-17
Reviewed by Kimmo Kinnunen.

* ipc/stream-buffer-read-write-expected.txt: Added.
* ipc/stream-buffer-read-write.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (291427 => 291428)


--- trunk/LayoutTests/ChangeLog	2022-03-17 19:04:53 UTC (rev 291427)
+++ trunk/LayoutTests/ChangeLog	2022-03-17 19:56:14 UTC (rev 291428)
@@ -1,3 +1,14 @@
+2022-03-17  Simon Lewis  <[email protected]>
+
+        Functional tests for read and write methods of JSIPCStreamConnectionBuffer
+        https://bugs.webkit.org/show_bug.cgi?id=237730
+        rdar://problem/89676460
+
+        Reviewed by Kimmo Kinnunen.
+
+        * ipc/stream-buffer-read-write-expected.txt: Added.
+        * ipc/stream-buffer-read-write.html: Added.
+
 2022-03-17  Ellie Epskamp-Hunt  <[email protected]>
 
         When a “autofilled and obscured” field becomes empty, turn “autofilled and obscured” off

Added: trunk/LayoutTests/ipc/stream-buffer-read-write-expected.txt (0 => 291428)


--- trunk/LayoutTests/ipc/stream-buffer-read-write-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/ipc/stream-buffer-read-write-expected.txt	2022-03-17 19:56:14 UTC (rev 291428)
@@ -0,0 +1,2 @@
+PASS
+Test passes if it does not crash and has no 'FAIL' output

Added: trunk/LayoutTests/ipc/stream-buffer-read-write.html (0 => 291428)


--- trunk/LayoutTests/ipc/stream-buffer-read-write.html	                        (rev 0)
+++ trunk/LayoutTests/ipc/stream-buffer-read-write.html	2022-03-17 19:56:14 UTC (rev 291428)
@@ -0,0 +1,74 @@
+<!DOCTYPE html><!-- webkit-test-runner [ IPCTestingAPIEnabled=true ] -->
+<script>
+function arraysAreEqual(a, b) {
+    if (a.byteLength !== b.byteLength) return false;
+    return a.every((val, i) => val === b[i]);
+}
+
+if (window.testRunner)
+    testRunner.dumpAsText()
+
+if (window.IPC) {
+    let headerReadSuccess = false;
+    let headerWriteSuccess = false;
+    let dataReadSuccess = false;
+    let dataWriteSuccess = false;
+
+    const testBytes = Uint8Array.from({length: 16}, (x, i) => i + 0x41);
+    const bufferSize = 48;
+    const streamConnection = IPC.createStreamClientConnection('GPU', bufferSize);
+
+    let streamBuffer = streamConnection.streamBuffer();
+    let initialData = new Uint8Array(streamBuffer.readHeaderBytes(0, 32));
+
+    streamBuffer.writeHeaderBytes(testBytes, 0, 4);
+    streamBuffer.writeHeaderBytes(testBytes.slice(4,8), 4, 4);
+
+    let readResult = new Uint8Array(streamBuffer.readHeaderBytes(0, 32));
+
+    let expected = initialData.slice();
+    for(let i=0; i<8; i++){
+        expected[i] = testBytes[i];
+    }
+
+    if(arraysAreEqual(readResult, expected)){
+        headerWriteSuccess = true;
+        readResult = new Uint8Array(streamBuffer.readHeaderBytes(4, 28));
+        if(arraysAreEqual(readResult, expected.slice(4, 32))){
+            headerReadSuccess = true;
+        } else {
+            document.write("FAIL: Header offset read");
+        }   
+    } else {
+        document.write("FAIL: Header write");
+    }
+
+    if(headerWriteSuccess && headerReadSuccess){
+        streamBuffer.writeDataBytes(testBytes.slice(0,8), 0, 8);
+        streamBuffer.writeDataBytes(testBytes.slice(8,16), 8, 8);
+        readResult = new Uint8Array(streamBuffer.readDataBytes(0));
+
+        if(arraysAreEqual(readResult, testBytes)){
+            dataWriteSuccess = true;
+            readResult = new Uint8Array(streamBuffer.readDataBytes(3));
+            if(arraysAreEqual(readResult, testBytes.slice(3))){
+                dataReadSuccess = true;
+            }  else {
+                document.write("FAIL: Data offset read");
+            }
+        } else {
+            document.write("FAIL: Data write");
+        }
+
+        if(dataWriteSuccess && dataReadSuccess){
+            document.write("PASS");
+        }
+    }
+} else {
+    // IPC not enabled so this test passes by default.
+    document.write("PASS");    
+}
+</script>
+<p>Test passes if it does not crash and has no 'FAIL' output</p>
+</body>
+</html>
\ No newline at end of file

Modified: trunk/Source/WebKit/ChangeLog (291427 => 291428)


--- trunk/Source/WebKit/ChangeLog	2022-03-17 19:04:53 UTC (rev 291427)
+++ trunk/Source/WebKit/ChangeLog	2022-03-17 19:56:14 UTC (rev 291428)
@@ -1,3 +1,32 @@
+2022-03-17  Simon Lewis  <[email protected]>
+
+        Add read and write methods to JSIPCStreamConnectionBuffer
+        https://bugs.webkit.org/show_bug.cgi?id=237730
+        rdar://problem/89676460
+
+        Reviewed by Kimmo Kinnunen.
+
+        When using IPCTestingAPI, this change enables the underlying bytes
+        of a StreamClientConnection buffer to be modified.
+
+        Test: ipc/stream-buffer-read-write.html
+
+        * Platform/IPC/StreamClientConnection.cpp:
+        (IPC::StreamClientConnection::bufferForTesting):
+        * Platform/IPC/StreamClientConnection.h:
+        * Platform/IPC/StreamConnectionBuffer.cpp:
+        (IPC::StreamConnectionBuffer::headerForTesting):
+        (IPC::StreamConnectionBuffer::dataForTesting):
+        * Platform/IPC/StreamConnectionBuffer.h:
+        * WebProcess/WebPage/IPCTestingAPI.cpp:
+        (WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::staticFunctions):
+        (WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::readHeaderBytes):
+        (WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::readDataBytes):
+        (WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::readBytes):
+        (WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::writeHeaderBytes):
+        (WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::writeDataBytes):
+        (WebKit::IPCTestingAPI::JSIPCStreamConnectionBuffer::writeBytes):
+
 2022-03-17  Simon Fraser  <[email protected]>
 
         Fix the build when LOG_DISABLED is 0 in release builds

Modified: trunk/Source/WebKit/Platform/IPC/StreamClientConnection.cpp (291427 => 291428)


--- trunk/Source/WebKit/Platform/IPC/StreamClientConnection.cpp	2022-03-17 19:04:53 UTC (rev 291427)
+++ trunk/Source/WebKit/Platform/IPC/StreamClientConnection.cpp	2022-03-17 19:56:14 UTC (rev 291428)
@@ -78,4 +78,9 @@
     wakeUpServer();
 }
 
+StreamConnectionBuffer& StreamClientConnection::bufferForTesting()
+{
+    return m_buffer;
 }
+
+}

Modified: trunk/Source/WebKit/Platform/IPC/StreamClientConnection.h (291427 => 291428)


--- trunk/Source/WebKit/Platform/IPC/StreamClientConnection.h	2022-03-17 19:04:53 UTC (rev 291427)
+++ trunk/Source/WebKit/Platform/IPC/StreamClientConnection.h	2022-03-17 19:56:14 UTC (rev 291428)
@@ -74,6 +74,7 @@
     using SendSyncResult = Connection::SendSyncResult;
     template<typename T, typename U>
     SendSyncResult sendSync(T&& message, typename T::Reply&&, ObjectIdentifier<U> destinationID, Timeout);
+    StreamConnectionBuffer& bufferForTesting();
 
 private:
     friend class WebKit::IPCTestingAPI::JSIPCStreamClientConnection;

Modified: trunk/Source/WebKit/Platform/IPC/StreamConnectionBuffer.cpp (291427 => 291428)


--- trunk/Source/WebKit/Platform/IPC/StreamConnectionBuffer.cpp	2022-03-17 19:04:53 UTC (rev 291427)
+++ trunk/Source/WebKit/Platform/IPC/StreamConnectionBuffer.cpp	2022-03-17 19:56:14 UTC (rev 291428)
@@ -100,4 +100,14 @@
     return StreamConnectionBuffer { sharedMemory.releaseNonNull(), dataSize,  WTFMove(*semaphore) };
 }
 
+Span<uint8_t> StreamConnectionBuffer::headerForTesting()
+{
+    return { static_cast<uint8_t*>(m_sharedMemory->data()), headerSize() };
 }
+
+Span<uint8_t> StreamConnectionBuffer::dataForTesting()
+{
+    return { data(), m_sharedMemory->size() - headerSize() };
+}
+
+}

Modified: trunk/Source/WebKit/Platform/IPC/StreamConnectionBuffer.h (291427 => 291428)


--- trunk/Source/WebKit/Platform/IPC/StreamConnectionBuffer.h	2022-03-17 19:04:53 UTC (rev 291427)
+++ trunk/Source/WebKit/Platform/IPC/StreamConnectionBuffer.h	2022-03-17 19:56:14 UTC (rev 291428)
@@ -115,6 +115,9 @@
     void encode(Encoder&) const;
     static std::optional<StreamConnectionBuffer> decode(Decoder&);
 
+    Span<uint8_t> headerForTesting();
+    Span<uint8_t> dataForTesting();
+
 private:
     StreamConnectionBuffer(Ref<WebKit::SharedMemory>&&, size_t memorySize, Semaphore&& clientWaitSemaphore);
 

Modified: trunk/Source/WebKit/WebProcess/WebPage/IPCTestingAPI.cpp (291427 => 291428)


--- trunk/Source/WebKit/WebProcess/WebPage/IPCTestingAPI.cpp	2022-03-17 19:04:53 UTC (rev 291427)
+++ trunk/Source/WebKit/WebProcess/WebPage/IPCTestingAPI.cpp	2022-03-17 19:56:14 UTC (rev 291428)
@@ -119,6 +119,8 @@
     IPC::StreamClientConnection& connection() { return m_streamConnection; }
 
 private:
+    friend class JSIPCStreamConnectionBuffer;
+
     JSIPCStreamClientConnection(JSIPC& jsIPC, IPC::Connection& connection, size_t bufferSize)
         : m_jsIPC(jsIPC)
         , m_streamConnection { connection, bufferSize }
@@ -167,6 +169,13 @@
 
     static const JSStaticFunction* staticFunctions();
 
+    static JSValueRef readHeaderBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
+    static JSValueRef readDataBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
+    static JSValueRef readBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception, Span<uint8_t>);
+    static JSValueRef writeHeaderBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
+    static JSValueRef writeDataBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
+    static JSValueRef writeBytes(JSContextRef, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception, Span<uint8_t>);
+
     WeakPtr<JSIPCStreamClientConnection> m_streamConnection;
 };
 
@@ -629,6 +638,10 @@
 const JSStaticFunction* JSIPCStreamConnectionBuffer::staticFunctions()
 {
     static const JSStaticFunction functions[] = {
+        { "readHeaderBytes", readHeaderBytes, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
+        { "writeHeaderBytes", writeHeaderBytes, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
+        { "readDataBytes", readDataBytes, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
+        { "writeDataBytes", writeDataBytes, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
         { 0, 0, 0 }
     };
     return functions;
@@ -884,6 +897,179 @@
     return JSValueMakeUndefined(context);
 }
 
+JSValueRef JSIPCStreamConnectionBuffer::readHeaderBytes(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+    RefPtr jsStreamBuffer = toWrapped(context, thisObject);
+    if (!jsStreamBuffer) {
+        *exception = createTypeError(context, "Wrong type"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    if (!jsStreamBuffer->m_streamConnection) {
+        *exception = createTypeError(context, "streamConnection not valid"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    IPC::StreamConnectionBuffer& buffer = jsStreamBuffer->m_streamConnection->m_streamConnection.bufferForTesting();
+    Span<uint8_t> span = buffer.headerForTesting();
+
+    return readBytes(context, nullptr, thisObject, argumentCount, arguments, exception, span);
+}
+
+JSValueRef JSIPCStreamConnectionBuffer::readDataBytes(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+    RefPtr jsStreamBuffer = toWrapped(context, thisObject);
+    if (!jsStreamBuffer) {
+        *exception = createTypeError(context, "Wrong type"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    if (!jsStreamBuffer->m_streamConnection) {
+        *exception = createTypeError(context, "streamConnection not valid"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    IPC::StreamConnectionBuffer& buffer = jsStreamBuffer->m_streamConnection->m_streamConnection.bufferForTesting();
+    Span<uint8_t> span = buffer.dataForTesting();
+
+    return readBytes(context, nullptr, thisObject, argumentCount, arguments, exception, span);
+}
+
+JSValueRef JSIPCStreamConnectionBuffer::readBytes(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception, Span<uint8_t> span)
+{
+    size_t offset = 0;
+    size_t length = span.size();
+    uint8_t* data = ""
+    auto* globalObject = toJS(context);
+    auto& vm = globalObject->vm();
+    JSC::JSLockHolder lock(vm);
+    if (argumentCount) {
+        auto offsetValue = convertToUint64(toJS(globalObject, arguments[0]));
+        if (!offsetValue) {
+            *exception = createTypeError(context, "The first argument must be a byte offset to read"_s);
+            return JSValueMakeUndefined(context);
+        }
+        if (*offsetValue > length)
+            offset = length;
+        else
+            offset = *offsetValue;
+        length -= offset;
+    }
+
+    if (argumentCount > 1) {
+        auto lengthValue = convertToUint64(toJS(globalObject, arguments[1]));
+        if (!lengthValue) {
+            *exception = createTypeError(context, "The second argument must be the number of bytes to read"_s);
+            return JSValueMakeUndefined(context);
+        }
+        if (*lengthValue < length)
+            length = *lengthValue;
+    }
+
+    auto arrayBuffer = JSC::ArrayBuffer::create(data + offset, length);
+    JSC::JSArrayBuffer* jsArrayBuffer = nullptr;
+    if (auto* structure = globalObject->arrayBufferStructure(arrayBuffer->sharingMode()))
+        jsArrayBuffer = JSC::JSArrayBuffer::create(vm, structure, WTFMove(arrayBuffer));
+    if (!jsArrayBuffer) {
+        *exception = createTypeError(context, "Failed to create the array buffer for the read bytes"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    return toRef(jsArrayBuffer);
+}
+
+JSValueRef JSIPCStreamConnectionBuffer::writeHeaderBytes(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+    RefPtr jsStreamBuffer = toWrapped(context, thisObject);
+    if (!jsStreamBuffer) {
+        *exception = createTypeError(context, "Wrong type"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    if (!jsStreamBuffer->m_streamConnection) {
+        *exception = createTypeError(context, "streamConnection not valid"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    IPC::StreamConnectionBuffer& buffer = jsStreamBuffer->m_streamConnection->m_streamConnection.bufferForTesting();
+    Span<uint8_t> span = buffer.headerForTesting();
+
+    return writeBytes(context, nullptr, thisObject, argumentCount, arguments, exception, span);
+}
+
+JSValueRef JSIPCStreamConnectionBuffer::writeDataBytes(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
+{
+    RefPtr jsStreamBuffer = toWrapped(context, thisObject);
+    if (!jsStreamBuffer) {
+        *exception = createTypeError(context, "Wrong type"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    if (!jsStreamBuffer->m_streamConnection) {
+        *exception = createTypeError(context, "streamConnection not valid"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    IPC::StreamConnectionBuffer& buffer = jsStreamBuffer->m_streamConnection->m_streamConnection.bufferForTesting();
+    Span<uint8_t> span = buffer.dataForTesting();
+
+    return writeBytes(context, nullptr, thisObject, argumentCount, arguments, exception, span);
+}
+
+JSValueRef JSIPCStreamConnectionBuffer::writeBytes(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception, Span<uint8_t> span)
+{
+    auto type = argumentCount ? JSValueGetTypedArrayType(context, arguments[0], exception) : kJSTypedArrayTypeNone;
+    if (type == kJSTypedArrayTypeNone) {
+        *exception = createTypeError(context, "The first argument must be an array buffer or a typed array"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    auto data = "" type, arguments[0], exception);
+    if (!data.buffer) {
+        *exception = createTypeError(context, "Could not read the buffer"_s);
+        return JSValueMakeUndefined(context);
+    }
+
+    size_t offset = 0;
+    size_t length = data.length;
+
+    size_t sharedMemorySize = span.size();
+    uint8_t* destinationData = span.data();
+
+    auto* globalObject = toJS(context);
+    auto& vm = globalObject->vm();
+    JSC::JSLockHolder lock(vm);
+
+    if (argumentCount > 1) {
+        auto offsetValue = convertToUint64(toJS(globalObject, arguments[1]));
+        if (!offsetValue) {
+            *exception = createTypeError(context, "The second argument must be a byte offset to write"_s);
+            return JSValueMakeUndefined(context);
+        }
+        if (*offsetValue >= sharedMemorySize) {
+            *exception = createTypeError(context, "Offset is too big"_s);
+            return JSValueMakeUndefined(context);
+        }
+        offset = *offsetValue;
+    }
+
+    if (argumentCount > 2) {
+        auto lengthValue = convertToUint64(toJS(globalObject, arguments[2]));
+        if (!lengthValue) {
+            *exception = createTypeError(context, "The third argument must be the number of bytes to write"_s);
+            return JSValueMakeUndefined(context);
+        }
+        if (*lengthValue > sharedMemorySize - offset || *lengthValue > length) {
+            *exception = createTypeError(context, "The number of bytes to write is too big"_s);
+            return JSValueMakeUndefined(context);
+        }
+        length = *lengthValue;
+    }
+
+    memcpy(destinationData + offset, data.buffer, length);
+    return JSValueMakeUndefined(context);
+}
+
 JSClassRef JSIPC::wrapperClass()
 {
     static JSClassRef jsClass;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to