Title: [290531] trunk
Revision
290531
Author
[email protected]
Date
2022-02-25 15:29:21 -0800 (Fri, 25 Feb 2022)

Log Message

SharedArrayBuffer posted to AudioWorkletProcessor is not actually shared with the main thread
https://bugs.webkit.org/show_bug.cgi?id=237144
<rdar://problem/89479641>

Reviewed by Darin Adler.

Source/WebCore:

SerializedScriptValue only maintains SharedArrayBuffers when constructed with SerializationContext::WorkerPostMessage.
With other SerializationContext values, such as the default one that AudioWorkletNode was using, SharedArrayBuffers
are serialized as simple arrays and are no longer shared.

This patch passes SerializationContext::WorkerPostMessage when constructing the SerializedScriptValue since we want
SharedArrayBuffer sharing and since a worklet is essentially a worker.

Note that this patch allows passing a SharedArrayBuffer to an AudioWorklet upon construction via processorOptions.
However, SharedArrayBuffer still won't be shared if sent later on via a MessagePort.postMessage() call. This is
because MessagePort.postMessage() routes all messages via the network process, even if the destination ends up
being within the same process as the sender (See Bug 220038).

Test: http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html

* Modules/webaudio/AudioWorkletNode.cpp:
(WebCore::AudioWorkletNode::create):

LayoutTests:

Add layout test coverage.

* http/wpt/webaudio/the-audio-api/the-audioworklet-interface/processors/shared-array-buffer-processor.js: Added.
(SharedArrayBufferTestProcessor):
(SharedArrayBufferTestProcessor.prototype.process):
* http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https-expected.txt: Added.
* http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html: Added.
* http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html.headers: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (290530 => 290531)


--- trunk/LayoutTests/ChangeLog	2022-02-25 23:19:46 UTC (rev 290530)
+++ trunk/LayoutTests/ChangeLog	2022-02-25 23:29:21 UTC (rev 290531)
@@ -1,3 +1,20 @@
+2022-02-25  Chris Dumez  <[email protected]>
+
+        SharedArrayBuffer posted to AudioWorkletProcessor is not actually shared with the main thread
+        https://bugs.webkit.org/show_bug.cgi?id=237144
+        <rdar://problem/89479641>
+
+        Reviewed by Darin Adler.
+
+        Add layout test coverage.
+
+        * http/wpt/webaudio/the-audio-api/the-audioworklet-interface/processors/shared-array-buffer-processor.js: Added.
+        (SharedArrayBufferTestProcessor):
+        (SharedArrayBufferTestProcessor.prototype.process):
+        * http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https-expected.txt: Added.
+        * http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html: Added.
+        * http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html.headers: Added.
+
 2022-02-25  Robert Jenner  <[email protected]>
 
         [ Mac wk1 ] 7X imported/w3c/web-platform-tests/pointerevents/pointerevent (layout-tests) are flaky text failures

Added: trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/processors/shared-array-buffer-processor.js (0 => 290531)


--- trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/processors/shared-array-buffer-processor.js	                        (rev 0)
+++ trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/processors/shared-array-buffer-processor.js	2022-02-25 23:29:21 UTC (rev 290531)
@@ -0,0 +1,16 @@
+class SharedArrayBufferTestProcessor extends AudioWorkletProcessor {
+  constructor(options) {
+    super();
+    this.sab = options.processorOptions['buffer'];
+    this.port._onmessage_ = (e) => {
+        this.port.postMessage({ 'sab_value': new Int16Array(this.sab)[0] });
+    };
+    this.port.postMessage({ 'sab_value': new Int16Array(this.sab)[0] });
+  }
+
+  process(inputs, outputs) {
+    return true;
+  }
+}
+
+registerProcessor('shared-array-buffer-test', SharedArrayBufferTestProcessor);

Added: trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https-expected.txt (0 => 290531)


--- trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https-expected.txt	2022-02-25 23:29:21 UTC (rev 290531)
@@ -0,0 +1,3 @@
+
+PASS SharedArrayBuffer sharing with AudioWorklet
+

Added: trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html (0 => 290531)


--- trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html	                        (rev 0)
+++ trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html	2022-02-25 23:29:21 UTC (rev 290531)
@@ -0,0 +1,40 @@
+<!doctype html>
+<title>Tests that SharedArrayBuffer can actually be shared with an AudioWorklet</title>
+<script src=""
+<script src=""
+<script>
+var context;
+promise_setup(async (t) => {
+  context = new AudioContext();
+  const filePath = 'processors/shared-array-buffer-processor.js';
+  await context.audioWorklet.addModule(filePath);
+});
+
+const get_sab_value_in_worklet = async (node) => {
+  const event = await new Promise((resolve) => {
+    node.port._onmessage_ = resolve;
+  });
+  return event.data.sab_value;
+};
+
+promise_test(async (t) => {
+  let sab = new SharedArrayBuffer(1024);
+  let array = new Int32Array(sab);
+  array[0] = 1;
+  const options = {
+    numberOfInputs: 0,
+    numberOfOutputs: 1,
+    processorOptions: { buffer: sab }
+  };
+
+  const node = new AudioWorkletNode(context, 'shared-array-buffer-test', options);
+
+  let sab_value = await get_sab_value_in_worklet(node);
+  assert_equals(sab_value, 1, "SharedArrayBuffer initial value");
+  array[0] = 2;
+  node.port.postMessage("foo");
+  
+  sab_value = await get_sab_value_in_worklet(node);
+  assert_equals(sab_value, 2, "SharedArrayBuffer value after update");
+}, 'SharedArrayBuffer sharing with AudioWorklet');
+</script>

Added: trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html.headers (0 => 290531)


--- trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html.headers	                        (rev 0)
+++ trunk/LayoutTests/http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html.headers	2022-02-25 23:29:21 UTC (rev 290531)
@@ -0,0 +1,2 @@
+Cross-Origin-Opener-Policy: same-origin
+Cross-Origin-Embedder-Policy: require-corp

Modified: trunk/Source/WebCore/ChangeLog (290530 => 290531)


--- trunk/Source/WebCore/ChangeLog	2022-02-25 23:19:46 UTC (rev 290530)
+++ trunk/Source/WebCore/ChangeLog	2022-02-25 23:29:21 UTC (rev 290531)
@@ -1,3 +1,28 @@
+2022-02-25  Chris Dumez  <[email protected]>
+
+        SharedArrayBuffer posted to AudioWorkletProcessor is not actually shared with the main thread
+        https://bugs.webkit.org/show_bug.cgi?id=237144
+        <rdar://problem/89479641>
+
+        Reviewed by Darin Adler.
+
+        SerializedScriptValue only maintains SharedArrayBuffers when constructed with SerializationContext::WorkerPostMessage.
+        With other SerializationContext values, such as the default one that AudioWorkletNode was using, SharedArrayBuffers
+        are serialized as simple arrays and are no longer shared.
+
+        This patch passes SerializationContext::WorkerPostMessage when constructing the SerializedScriptValue since we want
+        SharedArrayBuffer sharing and since a worklet is essentially a worker.
+
+        Note that this patch allows passing a SharedArrayBuffer to an AudioWorklet upon construction via processorOptions.
+        However, SharedArrayBuffer still won't be shared if sent later on via a MessagePort.postMessage() call. This is
+        because MessagePort.postMessage() routes all messages via the network process, even if the destination ends up
+        being within the same process as the sender (See Bug 220038).
+
+        Test: http/wpt/webaudio/the-audio-api/the-audioworklet-interface/shared-array-buffer.https.html
+
+        * Modules/webaudio/AudioWorkletNode.cpp:
+        (WebCore::AudioWorkletNode::create):
+
 2022-02-25  Jer Noble  <[email protected]>
 
         [Cocoa] HLS + Modern EME key rotation causes playback to stall indefinitely

Modified: trunk/Source/WebCore/Modules/webaudio/AudioWorkletNode.cpp (290530 => 290531)


--- trunk/Source/WebCore/Modules/webaudio/AudioWorkletNode.cpp	2022-02-25 23:19:46 UTC (rev 290530)
+++ trunk/Source/WebCore/Modules/webaudio/AudioWorkletNode.cpp	2022-02-25 23:29:21 UTC (rev 290531)
@@ -90,9 +90,12 @@
     {
         auto lock = JSC::JSLockHolder { &globalObject };
         auto* jsOptions = convertDictionaryToJS(globalObject, *JSC::jsCast<JSDOMGlobalObject*>(&globalObject), options);
-        serializedOptions = SerializedScriptValue::create(globalObject, jsOptions, SerializationErrorMode::NonThrowing);
-        if (!serializedOptions)
+        Vector<RefPtr<MessagePort>> ports;
+        auto serializedValue = SerializedScriptValue::create(globalObject, jsOptions, { }, ports, SerializationContext::WorkerPostMessage);
+        if (serializedValue.hasException())
             serializedOptions = SerializedScriptValue::nullValue();
+        else
+            serializedOptions = serializedValue.releaseReturnValue();
     }
 
     auto parameterData = WTFMove(options.parameterData);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to