Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: third_party/WebKit/LayoutTests/fast/workers/worker-sharedarraybuffer-transfer.html

Issue 2615803002: Disallow SharedArrayBuffer in postMessage transfer list (Closed)
Patch Set: remove exceptionState Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3 <body> 3 <body>
4 <p>Test sharing SharedArrayBuffers between dedicated workers.</p> 4 <p>Test sharing SharedArrayBuffers between dedicated workers.</p>
5 <div id="result"></div> 5 <div id="result"></div>
6 <script type="text/javascript"> 6 <script type="text/javascript">
7 function log(message) 7 function log(message)
8 { 8 {
9 document.getElementById("result").innerHTML += message + "</br>"; 9 document.getElementById("result").innerHTML += message + "</br>";
10 } 10 }
11 11
12 if (window.testRunner) { 12 var testCases = [
13 testRunner.dumpAsText(); 13 "testSendSharedArrayBuffer",
14 testRunner.waitUntilDone(); 14 "testSendInt8Array",
15 "testSendUint8Array",
16 "testSendUint8ClampedArray",
17 "testSendInt16Array",
18 "testSendUint16Array",
19 "testSendInt32Array",
20 "testSendUint32Array",
21 "testSendFloat32Array",
22 "testSendFloat64Array",
23 "testSendSharedArrayBufferTwice",
24 "testTransferArrayBufferAndSharedArrayBuffer"
25 ];
26 var testIndex = 0;
27
28 function runNextTest()
29 {
30 if (testIndex < testCases.length) {
31 testIndex++;
32 try {
33 window[testCases[testIndex - 1]]();
34 } catch (ex) {
35 log("FAIL: unexpected exception " + ex);
36 runNextTest();
37 }
38 } else {
39 log("DONE");
40 if (window.testRunner)
41 testRunner.notifyDone();
42 }
15 } 43 }
16 44
17 var testCases = [ 45 function testSendSharedArrayBuffer()
18 {name: "SharedArrayBuffer"}, 46 {
19 {name: "Int8Array"}, 47 runSendTest("SharedArrayBuffer");
20 {name: "Uint8Array"}, 48 }
21 {name: "Uint8ClampedArray"},
22 {name: "Int16Array"},
23 {name: "Uint16Array"},
24 {name: "Int32Array"},
25 {name: "Uint32Array"},
26 {name: "Float32Array"},
27 {name: "Float64Array"}
28 ];
29 var currentTestCase = 0;
30 49
31 function runTestCase(testCase) { 50 function testSendInt8Array()
51 {
52 runSendTest("Int8Array");
53 }
54
55 function testSendUint8Array()
56 {
57 runSendTest("Uint8Array");
58 }
59
60 function testSendUint8ClampedArray()
61 {
62 runSendTest("Uint8ClampedArray");
63 }
64
65 function testSendInt16Array()
66 {
67 runSendTest("Int16Array");
68 }
69
70 function testSendUint16Array()
71 {
72 runSendTest("Uint16Array");
73 }
74
75 function testSendInt32Array()
76 {
77 runSendTest("Int32Array");
78 }
79
80 function testSendUint32Array()
81 {
82 runSendTest("Uint32Array");
83 }
84
85 function testSendFloat32Array()
86 {
87 runSendTest("Float32Array");
88 }
89
90 function testSendFloat64Array()
91 {
92 runSendTest("Float64Array");
93 }
94
95 function initializeTypedArray(ta, length) {
96 var i;
97 for (i = 0; i < length; ++i)
98 ta[i] = i;
99 }
100
101 function runSendTest(name)
102 {
32 var length = 8; 103 var length = 8;
33 var name = testCase.name;
34 var type = window[name]; 104 var type = window[name];
35 var sab; 105 var sab;
36 var ta; 106 var ta;
107 var msg;
37 108
38 log("Running " + name + " test case"); 109 log("Running " + name + " test case");
39 110
40 if (testCase.name == 'SharedArrayBuffer') { 111 if (name == 'SharedArrayBuffer') {
41 sab = new SharedArrayBuffer(length); 112 sab = new SharedArrayBuffer(length);
42 ta = new Uint8Array(sab); 113 ta = new Uint8Array(sab);
114 msg = {name: name, data: sab, length: length};
43 } else { 115 } else {
44 sab = new SharedArrayBuffer(length * type.BYTES_PER_ELEMENT); 116 sab = new SharedArrayBuffer(length * type.BYTES_PER_ELEMENT);
45 ta = new type(sab); 117 ta = new type(sab);
118 msg = {name: name, data: ta, length: length};
46 } 119 }
47 120
48 var i; 121 initializeTypedArray(ta, length);
49 for (i = 0; i < length; ++i) 122
50 ta[i] = i; 123 // Don't allow passing a SharedArrayBuffer in the transfer list.
124 try {
125 worker.postMessage(msg, [sab]);
126 log("FAIL: Passing SharedArrayBuffer in the transfer list did not throw.") ;
127 } catch (e) {
128 log("PASS: Passing SharedArrayBuffer in the transfer list threw.");
129 }
51 130
52 // Without Atomics, we can't safely test modifying the contents of the 131 // Without Atomics, we can't safely test modifying the contents of the
53 // SharedArrayBuffer. All we can test for now is that the SharedArrayBuffer 132 // SharedArrayBuffer. All we can test for now is that the SharedArrayBuffer
54 // is not neutered when transferred to a Worker. 133 // is not neutered when transferred to a Worker.
55 if (testCase.name == 'SharedArrayBuffer') 134 worker.postMessage(msg);
56 worker.postMessage({name: name, data: sab, length: length}, [sab]);
57 else
58 worker.postMessage({name: name, data: ta, length: length}, [sab]);
59 135
60 if (sab.length === 0) 136 if (sab.length === 0)
61 log("FAIL: SharedArrayBuffer was neutered during transfer."); 137 log("FAIL: SharedArrayBuffer was neutered during transfer.");
62 else 138 else
63 log("PASS: SharedArrayBuffer not neutered during transfer."); 139 log("PASS: SharedArrayBuffer not neutered during transfer.");
64 140
65 // Ensure the data wasn't changed. 141 // Ensure the data wasn't changed.
66 for (i = 0; i < length; ++i) { 142 for (i = 0; i < length; ++i) {
67 if (ta[i] != i) { 143 if (ta[i] != i) {
68 log("FAIL: Original data changed during transfer. Expected " + i + 144 log("FAIL: Original data changed during transfer. Expected " + i +
69 " got " + ta[i]); 145 " got " + ta[i]);
70 break; 146 break;
71 } 147 }
72 } 148 }
73 if (i == length) 149 if (i == length)
74 log("PASS: Original data not changed during transfer."); 150 log("PASS: Original data not changed during transfer.");
75 } 151 }
76 152
153 function testTransferArrayBufferAndSharedArrayBuffer() {
154 var ab = new ArrayBuffer(4);
155 var sab = new SharedArrayBuffer(16);
156 var msg = {
157 name : 'ArrayBufferAndSharedArrayBuffer',
158 ab: ab,
159 abLength: ab.byteLength,
160 sab: sab,
161 sabLength: sab.byteLength,
162 };
163
164 log("Running TransferArrayBufferAndSharedArrayBuffer test case");
165
166 initializeTypedArray(new Uint8Array(ab), ab.byteLength);
167 initializeTypedArray(new Uint8Array(sab), sab.byteLength);
168
169 worker.postMessage(msg, [ab]);
170
171 if (ab.byteLength === 0)
172 log("PASS: ArrayBuffer was neutered during transfer.");
173 else
174 log("FAIL: ArrayBuffer was not neutered during transfer.");
175 }
176
177 function testSendSharedArrayBufferTwice() {
178 var sab = new SharedArrayBuffer(16);
179 var msg = {
180 name : 'SharedArrayBufferTwice',
181 sab: sab,
182 sabLength: sab.byteLength,
183 sab2: sab,
184 sab2Length: sab.byteLength,
185 };
186
187 log("Running SendSharedArrayBufferTwice test case");
188
189 initializeTypedArray(new Uint8Array(sab), sab.byteLength);
190
191 worker.postMessage(msg);
192 }
193
194 if (window.testRunner) {
195 testRunner.dumpAsText();
196 testRunner.waitUntilDone();
197 }
198
77 if (window.internals && internals.runtimeFlags.sharedArrayBufferEnabled && windo w.SharedArrayBuffer) { 199 if (window.internals && internals.runtimeFlags.sharedArrayBufferEnabled && windo w.SharedArrayBuffer) {
78 var worker = new Worker('resources/worker-sharedarraybuffer-transfer.js'); 200 var worker = new Worker('resources/worker-sharedarraybuffer-transfer.js');
79 201
80 runTestCase(testCases[currentTestCase]); 202 runNextTest();
81 203
82 worker.onmessage = function(e) { 204 worker.onmessage = function(e) {
83 if (e.data == 'DONE') { 205 if (e.data != 'DONE') {
84 // The current test case is finished.
85 if (++currentTestCase == testCases.length) {
86 log("DONE");
87 testRunner.notifyDone();
88 } else {
89 runTestCase(testCases[currentTestCase]);
90 }
91 } else {
92 // The worker sent a pass/fail message. 206 // The worker sent a pass/fail message.
93 log(e.data); 207 log(e.data);
208 } else {
209 runNextTest();
94 } 210 }
95
96 }; 211 };
97 } else { 212 } else {
98 log("SharedArrayBuffers are not enabled -- skipping test."); 213 log("SharedArrayBuffers are not enabled -- skipping test.");
99 testRunner.notifyDone(); 214 testRunner.notifyDone();
100 } 215 }
101 216
102 </script> 217 </script>
103 </body> 218 </body>
104 </html> 219 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698