Commit 1408e127 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Check the isolate if WebAssembly threads are enabled.

With the origin trial for WebAssembly threads, threads can be turned on
and off by the embedder depending on the context we are currently in.
With this CL we call the embedder callback stored on the isolate to
determine whether threads are enabled in the current context or not.

Design decision:
I decided to extend the {WasmFeaturesFromIsolate} function to ask the
embedder if WebAssembly threads are enabled. This is the function which
defines dynamically which features are turned on. It would be awkward
to have two such functions, one which calls the embedder and one which
does not.
A downside is that in WasmJs::Install the embedder does not seem to be
ready to be called. That's why I changed the code there to call
{WasmFeaturesFromFlags} instead.

R=titzer@chromium.org, mstarzinger@chromium.org

Bug: chromium:868844
Change-Id: I6bfa89960a54cec71992756e3717bbb3a9fe195e
Reviewed-on: https://chromium-review.googlesource.com/1169180
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55076}
parent 91bd6a5d
......@@ -847,6 +847,23 @@ RUNTIME_FUNCTION(Runtime_GetWasmRecoveredTrapCount) {
return *isolate->factory()->NewNumberFromSize(trap_count);
}
namespace {
bool EnableWasmThreads(v8::Local<v8::Context> context) { return true; }
bool DisableWasmThreads(v8::Local<v8::Context> context) { return false; }
} // namespace
// This runtime function enables WebAssembly threads through an embedder
// callback and thereby bypasses the value in FLAG_experimental_wasm_threads.
RUNTIME_FUNCTION(Runtime_SetWasmThreadsEnabled) {
DCHECK_EQ(1, args.length());
CONVERT_BOOLEAN_ARG_CHECKED(flag, 0);
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
v8_isolate->SetWasmThreadsEnabledCallback(flag ? EnableWasmThreads
: DisableWasmThreads);
return ReadOnlyRoots(isolate).undefined_value();
}
#define ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(Name) \
RUNTIME_FUNCTION(Runtime_Has##Name) { \
CONVERT_ARG_CHECKED(JSObject, obj, 0); \
......
......@@ -547,7 +547,8 @@ namespace internal {
F(WasmGetNumberOfInstances, 1, 1) \
F(WasmNumInterpretedCalls, 1, 1) \
F(WasmTraceMemory, 1, 1) \
F(WasmMemoryHasFullGuardRegion, 1, 1)
F(WasmMemoryHasFullGuardRegion, 1, 1) \
F(SetWasmThreadsEnabled, 1, 1)
#define FOR_EACH_INTRINSIC_TYPEDARRAY(F) \
F(ArrayBufferNeuter, 1, 1) \
......
......@@ -4,6 +4,7 @@
#include "src/wasm/wasm-features.h"
#include "src/flags.h"
#include "src/handles-inl.h"
#include "src/isolate.h"
namespace v8 {
......@@ -19,14 +20,15 @@ void UnionFeaturesInto(WasmFeatures* dst, WasmFeatures* src) {
FOREACH_WASM_FEATURE(DO_UNION, SPACE)
}
namespace {
inline WasmFeatures WasmFeaturesFromFlags() {
WasmFeatures WasmFeaturesFromFlags() {
return WasmFeatures{FOREACH_WASM_FEATURE(FLAG_REF, COMMA)};
}
}; // namespace
WasmFeatures WasmFeaturesFromIsolate(Isolate* isolate) {
return WasmFeaturesFromFlags();
WasmFeatures features = WasmFeaturesFromFlags();
features.threads =
isolate->AreWasmThreadsEnabled(handle(isolate->context(), isolate));
return features;
}
#undef DO_UNION
......
......@@ -51,7 +51,12 @@ static constexpr WasmFeatures kNoWasmFeatures{
static constexpr WasmFeatures kAsmjsWasmFeatures = kNoWasmFeatures;
V8_EXPORT_PRIVATE WasmFeatures WasmFeaturesFromFlags();
// Enables features based on both commandline flags and the isolate.
// Precondition: A valid context must be set in {isolate->context()}.
V8_EXPORT_PRIVATE WasmFeatures WasmFeaturesFromIsolate(Isolate* isolate);
V8_EXPORT_PRIVATE void UnionFeaturesInto(WasmFeatures* dst, WasmFeatures* src);
} // namespace wasm
......
......@@ -1496,7 +1496,10 @@ void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
v8_str(isolate, "WebAssembly.Memory"), ro_attributes);
// Setup Global
auto enabled_features = i::wasm::WasmFeaturesFromIsolate(isolate);
// The context is not set up completely yet. That's why we cannot use
// {WasmFeaturesFromIsolate} and have to use {WasmFeaturesFromFlags} instead.
auto enabled_features = i::wasm::WasmFeaturesFromFlags();
if (enabled_features.mut_global) {
Handle<JSFunction> global_constructor =
InstallFunc(isolate, webassembly, "Global", WebAssemblyGlobal, 1);
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --noexperimental-wasm-threads --allow-natives-syntax
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function instantiateModuleWithThreads() {
// Build a WebAssembly module which uses threads-features.
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.instantiate();
}
// Disable WebAssembly threads initially.
%SetWasmThreadsEnabled(false);
assertThrows(instantiateModuleWithThreads, WebAssembly.CompileError);
// Enable WebAssembly threads.
%SetWasmThreadsEnabled(true);
assertInstanceof(instantiateModuleWithThreads(), WebAssembly.Instance);
// Disable WebAssembly threads.
%SetWasmThreadsEnabled(false);
assertThrows(instantiateModuleWithThreads, WebAssembly.CompileError);
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