Commit ce5efe90 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[v8][wasm] Implement the origin trial flag for WebAssembly threads

The flag is passed to V8 via a callback which has already been
implemented in V8 in https://crrev.com/c/1163621.

Bug: 868844
Change-Id: Ic10c6330ba23a37c5dedd1073a0ea55c6cd86f7c
Reviewed-on: https://chromium-review.googlesource.com/1167187Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarJason Chase <chasej@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584685}
parent f2d1684d
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Build a WebAssembly module which uses thread-features.
function createBuilderWithThreads() {
const builder = new WasmModuleBuilder();
const shared = true;
builder.addMemory(2, 10, false, shared);
builder.addFunction('main', kSig_i_ii)
.addBody([
kExprGetLocal, 0, kExprGetLocal, 1, kAtomicPrefix, kExprI32AtomicAdd, 2,
0
])
.exportFunc();
return builder;
}
// Starts the function provided by blobURL on a worker and checks that the data
// the worker sends with postMessage is 'true'.
function testModuleCompilationOnWorker(blobURL) {
let resolve;
// Create a promise which fulfills when the worker finishes.
let promise = new Promise(function(res, rej) {
resolve = res;
});
let worker = new Worker(blobURL);
worker.addEventListener('message', e => resolve(e.data));
// Send the module bytes to the worker, because the worker does not have
// access to the WasmModuleBuilder.
worker.postMessage(createBuilderWithThreads().toBuffer());
return promise.then(result => assert_true(result));
}
// Test that WebAssembly threads are disabled and a WebAssembly module which
// uses shared memory and atomics cannot be compiled.
function testWasmThreadsDisabled() {
// If WebAssembly threads are enabled even without origin trial token, then we
// do not run this test.
if (window.internals.runtimeFlags.webAssemblyThreadsEnabled)
return;
try {
// Compiling the WebAssembly bytes should throw a CompileError.
createBuilderWithThreads().toModule();
assert_unreachable();
} catch (e) {
assert_true(e instanceof WebAssembly.CompileError);
}
}
// Test that WebAssembly threads are disabled and a WebAssembly module which
// uses shared memory and atomics cannot be compiled on a worker.
function testWasmThreadsDisabledOnWorker() {
// If WebAssembly threads are enabled even without origin trial token, then
// we do not run this test.
if (window.internals.runtimeFlags.webAssemblyThreadsEnabled) {
return Promise.resolve();
}
const blobURL = URL.createObjectURL(new Blob(
[
'(',
function() {
self.addEventListener('message', data => {
try {
// Compiling the WebAssembly bytes should throw a CompileError.
new WebAssembly.Module(data.data);
self.postMessage(false);
} catch (e) {
self.postMessage(e instanceof WebAssembly.CompileError);
}
});
}.toString(),
')()'
],
{type: 'application/javascript'}));
return testModuleCompilationOnWorker(blobURL);
}
// Test that WebAssembly threads are enabled and a WebAssembly module which
// uses shared memory and atomics can be compiled.
function testWasmThreadsEnabled() {
const module = createBuilderWithThreads().toModule();
assert_true(module !== undefined);
assert_true(module instanceof WebAssembly.Module);
}
// Test that WebAssembly threads are enabled and a WebAssembly module which
// uses shared memory and atomics can be compiled on a worker.
function testWasmThreadsEnabledOnWorker() {
let blobURL = URL.createObjectURL(new Blob(
[
'(',
function() {
self.addEventListener('message', data => {
try {
const module = new WebAssembly.Module(data.data);
self.postMessage(module !== undefined);
} catch (e) {
console.log(e);
self.postMessage(false);
}
});
}.toString(),
')()'
],
{type: 'application/javascript'}));
return testModuleCompilationOnWorker(blobURL);
}
<!DOCTYPE html>
<meta charset="utf-8">
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../wasm/resources/wasm-constants.js"></script>
<script src="../../wasm/resources/wasm-module-builder.js"></script>
<script src="resources/wasm-threads-origin-trial.js"></script>
<script>
test(testWasmThreadsDisabled,
"Test that WebAssembly threads are disabled without origin trial token");
promise_test(testWasmThreadsDisabledOnWorker,
"Test that WebAssembly threads are disabled in worker without origin trial token");
</script>
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Generate this token with the command:
generate_token.py http://127.0.0.1:8000 WebAssemblyThreads --expire-timestamp=2000000000
-->
<meta http-equiv="origin-trial" content="AvA8hsoHU+5qiE3oR2wNRZKcI7MOkHl4Vdu3IX8IKv90k4tpNFIaC3KblOaOh9ND63YD/539iVGJ930KCYwotgoAAABaeyJvcmlnaW4iOiAiaHR0cDovLzEyNy4wLjAuMTo4MDAwIiwgImZlYXR1cmUiOiAiV2ViQXNzZW1ibHlUaHJlYWRzIiwgImV4cGlyeSI6IDIwMDAwMDAwMDB9" />
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../wasm/resources/wasm-constants.js"></script>
<script src="../../wasm/resources/wasm-module-builder.js"></script>
<script src="resources/wasm-threads-origin-trial.js"></script>
<script>
test(testWasmThreadsEnabled,
"Test that WebAssembly threads are enabled with origin trial token");
promise_test(testWasmThreadsEnabledOnWorker,
"Test that WebAssembly threads are enabled in worker with origin trial token");
</script>
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
#include "third_party/blink/renderer/core/frame/local_dom_window.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/inspector/console_message.h" #include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/inspector/main_thread_debugger.h" #include "third_party/blink/renderer/core/inspector/main_thread_debugger.h"
#include "third_party/blink/renderer/core/origin_trials/origin_trials.h"
#include "third_party/blink/renderer/core/page/page.h" #include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/script/modulator.h" #include "third_party/blink/renderer/core/script/modulator.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h" #include "third_party/blink/renderer/core/workers/worker_global_scope.h"
...@@ -505,6 +506,14 @@ static bool WasmCodeGenerationCheckCallbackInMainThread( ...@@ -505,6 +506,14 @@ static bool WasmCodeGenerationCheckCallbackInMainThread(
return false; return false;
} }
static bool WasmThreadsEnabledCallback(v8::Local<v8::Context> context) {
ExecutionContext* execution_context = ToExecutionContext(context);
if (!execution_context)
return false;
return OriginTrials::WebAssemblyThreadsEnabled(execution_context);
}
v8::Local<v8::Value> NewRangeException(v8::Isolate* isolate, v8::Local<v8::Value> NewRangeException(v8::Isolate* isolate,
const char* message) { const char* message) {
return v8::Exception::RangeError( return v8::Exception::RangeError(
...@@ -634,6 +643,7 @@ static void InitializeV8Common(v8::Isolate* isolate) { ...@@ -634,6 +643,7 @@ static void InitializeV8Common(v8::Isolate* isolate) {
isolate->SetUseCounterCallback(&UseCounterCallback); isolate->SetUseCounterCallback(&UseCounterCallback);
isolate->SetWasmModuleCallback(WasmModuleOverride); isolate->SetWasmModuleCallback(WasmModuleOverride);
isolate->SetWasmInstanceCallback(WasmInstanceOverride); isolate->SetWasmInstanceCallback(WasmInstanceOverride);
isolate->SetWasmThreadsEnabledCallback(WasmThreadsEnabledCallback);
if (RuntimeEnabledFeatures::ModuleScriptsDynamicImportEnabled()) { if (RuntimeEnabledFeatures::ModuleScriptsDynamicImportEnabled()) {
isolate->SetHostImportModuleDynamicallyCallback( isolate->SetHostImportModuleDynamicallyCallback(
HostImportModuleDynamically); HostImportModuleDynamically);
......
...@@ -1254,6 +1254,11 @@ ...@@ -1254,6 +1254,11 @@
name: "WebAssemblyStreaming", name: "WebAssemblyStreaming",
status: "experimental", status: "experimental",
}, },
{
name: "WebAssemblyThreads",
origin_trial_feature_name: "WebAssemblyThreads",
status: "experimental",
},
{ {
name: "WebAuth", name: "WebAuth",
status: "experimental", status: "experimental",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment