2018-07-05 09:17:35 +00:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "src/objects-inl.h"
|
2018-08-01 10:13:03 +00:00
|
|
|
#include "src/wasm/function-compiler.h"
|
2018-07-05 09:17:35 +00:00
|
|
|
#include "src/wasm/wasm-engine.h"
|
|
|
|
#include "src/wasm/wasm-module-builder.h"
|
|
|
|
#include "src/wasm/wasm-module.h"
|
|
|
|
#include "src/wasm/wasm-objects-inl.h"
|
|
|
|
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/common/wasm/test-signatures.h"
|
|
|
|
#include "test/common/wasm/wasm-macro-gen.h"
|
|
|
|
#include "test/common/wasm/wasm-module-runner.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
namespace test_wasm_shared_engine {
|
|
|
|
|
|
|
|
// Helper class representing a WebAssembly engine that is capable of being
|
|
|
|
// shared between multiple Isolates, sharing the underlying generated code.
|
|
|
|
class SharedEngine {
|
|
|
|
public:
|
|
|
|
explicit SharedEngine(size_t max_committed = kMaxWasmCodeMemory)
|
2018-09-18 13:05:45 +00:00
|
|
|
: wasm_engine_(base::make_unique<WasmEngine>()) {}
|
2018-07-05 09:17:35 +00:00
|
|
|
~SharedEngine() {
|
|
|
|
// Ensure no remaining uses exist.
|
|
|
|
CHECK(wasm_engine_.unique());
|
|
|
|
}
|
|
|
|
|
|
|
|
WasmEngine* engine() const { return wasm_engine_.get(); }
|
|
|
|
WasmCodeManager* code_manager() const { return engine()->code_manager(); }
|
|
|
|
|
|
|
|
int NumberOfExportedEngineUses() const {
|
|
|
|
// This class holds one implicit use itself, which we discount.
|
|
|
|
return static_cast<int>(wasm_engine_.use_count()) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<WasmEngine> ExportEngineForSharing() { return wasm_engine_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<WasmEngine> wasm_engine_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper type definition representing a WebAssembly module shared between
|
|
|
|
// multiple Isolates with implicit reference counting.
|
|
|
|
using SharedModule = std::shared_ptr<NativeModule>;
|
|
|
|
|
|
|
|
// Helper class representing an Isolate based on a given shared WebAssembly
|
|
|
|
// engine available at construction time.
|
|
|
|
class SharedEngineIsolate {
|
|
|
|
public:
|
|
|
|
explicit SharedEngineIsolate(SharedEngine* engine)
|
|
|
|
: isolate_(v8::Isolate::Allocate()) {
|
2018-09-25 11:24:09 +00:00
|
|
|
isolate()->SetWasmEngine(engine->ExportEngineForSharing());
|
2018-07-05 09:17:35 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
|
|
|
|
v8::Isolate::Initialize(isolate_, create_params);
|
|
|
|
v8::HandleScope handle_scope(v8_isolate());
|
|
|
|
v8::Context::New(v8_isolate())->Enter();
|
|
|
|
testing::SetupIsolateForWasmModule(isolate());
|
|
|
|
zone_.reset(new Zone(isolate()->allocator(), ZONE_NAME));
|
|
|
|
}
|
|
|
|
~SharedEngineIsolate() {
|
|
|
|
zone_.reset();
|
|
|
|
isolate_->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Zone* zone() const { return zone_.get(); }
|
|
|
|
v8::Isolate* v8_isolate() { return isolate_; }
|
|
|
|
Isolate* isolate() { return reinterpret_cast<Isolate*>(isolate_); }
|
|
|
|
|
|
|
|
Handle<WasmInstanceObject> CompileAndInstantiate(ZoneBuffer* buffer) {
|
|
|
|
ErrorThrower thrower(isolate(), "CompileAndInstantiate");
|
|
|
|
MaybeHandle<WasmInstanceObject> instance =
|
|
|
|
testing::CompileAndInstantiateForTesting(
|
|
|
|
isolate(), &thrower,
|
|
|
|
ModuleWireBytes(buffer->begin(), buffer->end()));
|
|
|
|
return instance.ToHandleChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<WasmInstanceObject> ImportInstance(SharedModule shared_module) {
|
2018-07-05 14:31:15 +00:00
|
|
|
Handle<WasmModuleObject> module_object =
|
2018-07-31 08:16:22 +00:00
|
|
|
isolate()->wasm_engine()->ImportNativeModule(isolate(), shared_module);
|
2018-07-05 09:17:35 +00:00
|
|
|
ErrorThrower thrower(isolate(), "ImportInstance");
|
|
|
|
MaybeHandle<WasmInstanceObject> instance =
|
|
|
|
isolate()->wasm_engine()->SyncInstantiate(isolate(), &thrower,
|
|
|
|
module_object, {}, {});
|
|
|
|
return instance.ToHandleChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedModule ExportInstance(Handle<WasmInstanceObject> instance) {
|
|
|
|
return instance->module_object()->managed_native_module()->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Run(Handle<WasmInstanceObject> instance) {
|
|
|
|
return testing::RunWasmModuleForTesting(isolate(), instance, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Isolate* isolate_;
|
|
|
|
std::unique_ptr<Zone> zone_;
|
|
|
|
};
|
|
|
|
|
2018-07-26 13:08:04 +00:00
|
|
|
// Helper class representing a Thread running its own instance of an Isolate
|
|
|
|
// with a shared WebAssembly engine available at construction time.
|
|
|
|
class SharedEngineThread : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
SharedEngineThread(SharedEngine* engine,
|
|
|
|
std::function<void(SharedEngineIsolate&)> callback)
|
|
|
|
: Thread(Options("SharedEngineThread")),
|
|
|
|
engine_(engine),
|
|
|
|
callback_(callback) {}
|
|
|
|
|
2018-09-14 14:54:08 +00:00
|
|
|
void Run() override {
|
2018-07-26 13:08:04 +00:00
|
|
|
SharedEngineIsolate isolate(engine_);
|
|
|
|
callback_(isolate);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SharedEngine* engine_;
|
|
|
|
std::function<void(SharedEngineIsolate&)> callback_;
|
|
|
|
};
|
|
|
|
|
2018-07-05 09:17:35 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
ZoneBuffer* BuildReturnConstantModule(Zone* zone, int constant) {
|
|
|
|
TestSignatures sigs;
|
|
|
|
ZoneBuffer* buffer = new (zone) ZoneBuffer(zone);
|
|
|
|
WasmModuleBuilder* builder = new (zone) WasmModuleBuilder(zone);
|
|
|
|
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_v());
|
|
|
|
f->builder()->AddExport(CStrVector("main"), f);
|
|
|
|
byte code[] = {WASM_I32V_2(constant)};
|
|
|
|
f->EmitCode(code, sizeof(code));
|
|
|
|
f->Emit(kExprEnd);
|
|
|
|
builder->WriteTo(*buffer);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2018-07-31 09:41:11 +00:00
|
|
|
class MockInstantiationResolver : public InstantiationResultResolver {
|
|
|
|
public:
|
|
|
|
explicit MockInstantiationResolver(Handle<Object>* out_instance)
|
|
|
|
: out_instance_(out_instance) {}
|
2018-09-14 14:54:08 +00:00
|
|
|
void OnInstantiationSucceeded(Handle<WasmInstanceObject> result) override {
|
2018-10-31 13:42:16 +00:00
|
|
|
*out_instance_->location() = *result;
|
2018-07-31 09:41:11 +00:00
|
|
|
}
|
2018-09-14 14:54:08 +00:00
|
|
|
void OnInstantiationFailed(Handle<Object> error_reason) override {
|
2018-07-31 09:41:11 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Handle<Object>* out_instance_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockCompilationResolver : public CompilationResultResolver {
|
|
|
|
public:
|
|
|
|
MockCompilationResolver(SharedEngineIsolate& isolate,
|
|
|
|
Handle<Object>* out_instance)
|
|
|
|
: isolate_(isolate), out_instance_(out_instance) {}
|
2018-09-14 14:54:08 +00:00
|
|
|
void OnCompilationSucceeded(Handle<WasmModuleObject> result) override {
|
2018-07-31 09:41:11 +00:00
|
|
|
isolate_.isolate()->wasm_engine()->AsyncInstantiate(
|
|
|
|
isolate_.isolate(),
|
|
|
|
base::make_unique<MockInstantiationResolver>(out_instance_), result,
|
|
|
|
{});
|
|
|
|
}
|
2018-09-14 14:54:08 +00:00
|
|
|
void OnCompilationFailed(Handle<Object> error_reason) override {
|
2018-07-31 09:41:11 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SharedEngineIsolate& isolate_;
|
|
|
|
Handle<Object>* out_instance_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void PumpMessageLoop(SharedEngineIsolate& isolate) {
|
|
|
|
v8::platform::PumpMessageLoop(i::V8::GetCurrentPlatform(),
|
|
|
|
isolate.v8_isolate(),
|
|
|
|
platform::MessageLoopBehavior::kWaitForWork);
|
|
|
|
isolate.isolate()->RunMicrotasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<WasmInstanceObject> CompileAndInstantiateAsync(
|
|
|
|
SharedEngineIsolate& isolate, ZoneBuffer* buffer) {
|
|
|
|
Handle<Object> maybe_instance = handle(Smi::kZero, isolate.isolate());
|
2018-08-08 14:54:44 +00:00
|
|
|
auto enabled_features = WasmFeaturesFromIsolate(isolate.isolate());
|
2018-07-31 09:41:11 +00:00
|
|
|
isolate.isolate()->wasm_engine()->AsyncCompile(
|
2018-08-08 14:54:44 +00:00
|
|
|
isolate.isolate(), enabled_features,
|
2018-07-31 09:41:11 +00:00
|
|
|
base::make_unique<MockCompilationResolver>(isolate, &maybe_instance),
|
|
|
|
ModuleWireBytes(buffer->begin(), buffer->end()), true);
|
|
|
|
while (!maybe_instance->IsWasmInstanceObject()) PumpMessageLoop(isolate);
|
|
|
|
Handle<WasmInstanceObject> instance =
|
|
|
|
Handle<WasmInstanceObject>::cast(maybe_instance);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-07-05 09:17:35 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(SharedEngineUseCount) {
|
|
|
|
SharedEngine engine;
|
|
|
|
CHECK_EQ(0, engine.NumberOfExportedEngineUses());
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate(&engine);
|
|
|
|
CHECK_EQ(1, engine.NumberOfExportedEngineUses());
|
|
|
|
}
|
|
|
|
CHECK_EQ(0, engine.NumberOfExportedEngineUses());
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate1(&engine);
|
|
|
|
CHECK_EQ(1, engine.NumberOfExportedEngineUses());
|
|
|
|
SharedEngineIsolate isolate2(&engine);
|
|
|
|
CHECK_EQ(2, engine.NumberOfExportedEngineUses());
|
|
|
|
}
|
|
|
|
CHECK_EQ(0, engine.NumberOfExportedEngineUses());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SharedEngineRunSeparated) {
|
|
|
|
SharedEngine engine;
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate(&engine);
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 23);
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
|
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate(&engine);
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 42);
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
|
|
|
|
CHECK_EQ(42, isolate.Run(instance));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SharedEngineRunImported) {
|
|
|
|
SharedEngine engine;
|
|
|
|
SharedModule module;
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate(&engine);
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 23);
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
|
2018-07-09 08:02:54 +00:00
|
|
|
module = isolate.ExportInstance(instance);
|
2018-07-05 09:17:35 +00:00
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
2018-07-09 08:02:54 +00:00
|
|
|
CHECK_EQ(2, module.use_count());
|
2018-07-05 09:17:35 +00:00
|
|
|
}
|
2018-07-09 08:02:54 +00:00
|
|
|
CHECK_EQ(1, module.use_count());
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate(&engine);
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.ImportInstance(module);
|
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
CHECK_EQ(2, module.use_count());
|
|
|
|
}
|
|
|
|
CHECK_EQ(1, module.use_count());
|
2018-07-05 09:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 09:41:11 +00:00
|
|
|
TEST(SharedEngineRunThreadedBuildingSync) {
|
2018-07-26 13:08:04 +00:00
|
|
|
SharedEngine engine;
|
|
|
|
SharedEngineThread thread1(&engine, [](SharedEngineIsolate& isolate) {
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 23);
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
|
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
});
|
|
|
|
SharedEngineThread thread2(&engine, [](SharedEngineIsolate& isolate) {
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 42);
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
|
|
|
|
CHECK_EQ(42, isolate.Run(instance));
|
|
|
|
});
|
|
|
|
thread1.Start();
|
|
|
|
thread2.Start();
|
|
|
|
thread1.Join();
|
|
|
|
thread2.Join();
|
|
|
|
}
|
|
|
|
|
2018-07-31 09:41:11 +00:00
|
|
|
TEST(SharedEngineRunThreadedBuildingAsync) {
|
|
|
|
SharedEngine engine;
|
|
|
|
SharedEngineThread thread1(&engine, [](SharedEngineIsolate& isolate) {
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 23);
|
|
|
|
Handle<WasmInstanceObject> instance =
|
|
|
|
CompileAndInstantiateAsync(isolate, buffer);
|
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
});
|
|
|
|
SharedEngineThread thread2(&engine, [](SharedEngineIsolate& isolate) {
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 42);
|
|
|
|
Handle<WasmInstanceObject> instance =
|
|
|
|
CompileAndInstantiateAsync(isolate, buffer);
|
|
|
|
CHECK_EQ(42, isolate.Run(instance));
|
|
|
|
});
|
|
|
|
thread1.Start();
|
|
|
|
thread2.Start();
|
|
|
|
thread1.Join();
|
|
|
|
thread2.Join();
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:08:04 +00:00
|
|
|
TEST(SharedEngineRunThreadedExecution) {
|
|
|
|
SharedEngine engine;
|
|
|
|
SharedModule module;
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate(&engine);
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 23);
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
|
|
|
|
module = isolate.ExportInstance(instance);
|
|
|
|
}
|
|
|
|
SharedEngineThread thread1(&engine, [module](SharedEngineIsolate& isolate) {
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.ImportInstance(module);
|
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
});
|
|
|
|
SharedEngineThread thread2(&engine, [module](SharedEngineIsolate& isolate) {
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.ImportInstance(module);
|
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
});
|
|
|
|
thread1.Start();
|
|
|
|
thread2.Start();
|
|
|
|
thread1.Join();
|
|
|
|
thread2.Join();
|
|
|
|
}
|
|
|
|
|
2018-08-01 10:13:03 +00:00
|
|
|
TEST(SharedEngineRunThreadedTierUp) {
|
|
|
|
SharedEngine engine;
|
|
|
|
SharedModule module;
|
|
|
|
{
|
|
|
|
SharedEngineIsolate isolate(&engine);
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 23);
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
|
|
|
|
module = isolate.ExportInstance(instance);
|
|
|
|
}
|
|
|
|
constexpr int kNumberOfThreads = 5;
|
|
|
|
std::list<SharedEngineThread> threads;
|
|
|
|
for (int i = 0; i < kNumberOfThreads; ++i) {
|
|
|
|
threads.emplace_back(&engine, [module](SharedEngineIsolate& isolate) {
|
|
|
|
constexpr int kNumberOfIterations = 100;
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.ImportInstance(module);
|
|
|
|
for (int j = 0; j < kNumberOfIterations; ++j) {
|
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
threads.emplace_back(&engine, [module](SharedEngineIsolate& isolate) {
|
|
|
|
HandleScope scope(isolate.isolate());
|
|
|
|
Handle<WasmInstanceObject> instance = isolate.ImportInstance(module);
|
2018-08-23 14:44:28 +00:00
|
|
|
WasmFeatures detected = kNoWasmFeatures;
|
2018-08-01 10:13:03 +00:00
|
|
|
WasmCompilationUnit::CompileWasmFunction(
|
2018-10-30 12:46:04 +00:00
|
|
|
isolate.isolate(), module.get(), &detected,
|
2018-08-21 15:01:31 +00:00
|
|
|
&module->module()->functions[0], ExecutionTier::kOptimized);
|
2018-08-01 10:13:03 +00:00
|
|
|
CHECK_EQ(23, isolate.Run(instance));
|
|
|
|
});
|
|
|
|
for (auto& thread : threads) thread.Start();
|
|
|
|
for (auto& thread : threads) thread.Join();
|
|
|
|
}
|
|
|
|
|
2018-07-05 09:17:35 +00:00
|
|
|
} // namespace test_wasm_shared_engine
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|