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/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;