2017-12-05 00:28:35 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-02-02 12:05:19 +00:00
|
|
|
#ifndef V8_WASM_WASM_ENGINE_H_
|
|
|
|
#define V8_WASM_WASM_ENGINE_H_
|
2017-12-05 00:28:35 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2017-12-05 01:47:59 +00:00
|
|
|
#include "src/wasm/wasm-code-manager.h"
|
2018-01-06 01:33:31 +00:00
|
|
|
#include "src/wasm/wasm-memory.h"
|
2018-08-21 15:01:31 +00:00
|
|
|
#include "src/wasm/wasm-tier.h"
|
2018-07-09 08:02:54 +00:00
|
|
|
#include "src/zone/accounting-allocator.h"
|
2017-12-05 00:28:35 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2018-07-16 11:52:11 +00:00
|
|
|
class CodeTracer;
|
2018-07-10 13:15:29 +00:00
|
|
|
class CompilationStatistics;
|
2018-01-18 10:52:52 +00:00
|
|
|
class WasmModuleObject;
|
|
|
|
class WasmInstanceObject;
|
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
namespace wasm {
|
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
class ErrorThrower;
|
2018-08-08 14:54:44 +00:00
|
|
|
struct WasmFeatures;
|
2018-01-18 10:52:52 +00:00
|
|
|
struct ModuleWireBytes;
|
|
|
|
|
2018-05-24 21:22:27 +00:00
|
|
|
class V8_EXPORT_PRIVATE CompilationResultResolver {
|
|
|
|
public:
|
|
|
|
virtual void OnCompilationSucceeded(Handle<WasmModuleObject> result) = 0;
|
|
|
|
virtual void OnCompilationFailed(Handle<Object> error_reason) = 0;
|
|
|
|
virtual ~CompilationResultResolver() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class V8_EXPORT_PRIVATE InstantiationResultResolver {
|
|
|
|
public:
|
|
|
|
virtual void OnInstantiationSucceeded(Handle<WasmInstanceObject> result) = 0;
|
|
|
|
virtual void OnInstantiationFailed(Handle<Object> error_reason) = 0;
|
|
|
|
virtual ~InstantiationResultResolver() {}
|
|
|
|
};
|
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
// The central data structure that represents an engine instance capable of
|
|
|
|
// loading, instantiating, and executing WASM code.
|
2018-01-17 14:46:27 +00:00
|
|
|
class V8_EXPORT_PRIVATE WasmEngine {
|
2017-12-05 00:28:35 +00:00
|
|
|
public:
|
2018-07-10 13:15:29 +00:00
|
|
|
explicit WasmEngine(std::unique_ptr<WasmCodeManager> code_manager);
|
|
|
|
~WasmEngine();
|
2017-12-05 00:28:35 +00:00
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
// Synchronously validates the given bytes that represent an encoded WASM
|
|
|
|
// module.
|
2018-08-08 14:54:44 +00:00
|
|
|
bool SyncValidate(Isolate* isolate, const WasmFeatures& enabled,
|
|
|
|
const ModuleWireBytes& bytes);
|
2018-01-17 14:46:27 +00:00
|
|
|
|
2018-01-18 10:52:52 +00:00
|
|
|
// Synchronously compiles the given bytes that represent a translated
|
|
|
|
// asm.js module.
|
|
|
|
MaybeHandle<WasmModuleObject> SyncCompileTranslatedAsmJs(
|
|
|
|
Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
|
|
|
|
Handle<Script> asm_js_script,
|
|
|
|
Vector<const byte> asm_js_offset_table_bytes);
|
|
|
|
|
|
|
|
// Synchronously compiles the given bytes that represent an encoded WASM
|
|
|
|
// module.
|
|
|
|
MaybeHandle<WasmModuleObject> SyncCompile(Isolate* isolate,
|
2018-08-08 14:54:44 +00:00
|
|
|
const WasmFeatures& enabled,
|
2018-01-18 10:52:52 +00:00
|
|
|
ErrorThrower* thrower,
|
|
|
|
const ModuleWireBytes& bytes);
|
|
|
|
|
|
|
|
// Synchronously instantiate the given WASM module with the given imports.
|
|
|
|
// If the module represents an asm.js module, then the supplied {memory}
|
|
|
|
// should be used as the memory of the instance.
|
|
|
|
MaybeHandle<WasmInstanceObject> SyncInstantiate(
|
|
|
|
Isolate* isolate, ErrorThrower* thrower,
|
|
|
|
Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
|
|
|
|
MaybeHandle<JSArrayBuffer> memory);
|
|
|
|
|
|
|
|
// Begin an asynchronous compilation of the given bytes that represent an
|
2018-05-24 21:22:27 +00:00
|
|
|
// encoded WASM module.
|
2018-01-18 10:52:52 +00:00
|
|
|
// The {is_shared} flag indicates if the bytes backing the module could
|
|
|
|
// be shared across threads, i.e. could be concurrently modified.
|
2018-08-08 14:54:44 +00:00
|
|
|
void AsyncCompile(Isolate* isolate, const WasmFeatures& enabled,
|
2018-08-13 13:35:54 +00:00
|
|
|
std::shared_ptr<CompilationResultResolver> resolver,
|
2018-01-18 10:52:52 +00:00
|
|
|
const ModuleWireBytes& bytes, bool is_shared);
|
|
|
|
|
2018-05-24 21:22:27 +00:00
|
|
|
// Begin an asynchronous instantiation of the given WASM module.
|
|
|
|
void AsyncInstantiate(Isolate* isolate,
|
|
|
|
std::unique_ptr<InstantiationResultResolver> resolver,
|
2018-01-18 10:52:52 +00:00
|
|
|
Handle<WasmModuleObject> module_object,
|
|
|
|
MaybeHandle<JSReceiver> imports);
|
|
|
|
|
2018-05-09 13:48:47 +00:00
|
|
|
std::shared_ptr<StreamingDecoder> StartStreamingCompilation(
|
2018-08-08 14:54:44 +00:00
|
|
|
Isolate* isolate, const WasmFeatures& enabled, Handle<Context> context,
|
2018-08-13 13:35:54 +00:00
|
|
|
std::shared_ptr<CompilationResultResolver> resolver);
|
2017-12-05 00:28:35 +00:00
|
|
|
|
2018-08-17 12:35:29 +00:00
|
|
|
// Compiles the function with the given index at a specific compilation tier
|
|
|
|
// and returns true on success, false (and pending exception) otherwise. This
|
|
|
|
// is mostly used for testing to force a function into a specific tier.
|
|
|
|
bool CompileFunction(Isolate* isolate, NativeModule* native_module,
|
2018-08-21 15:01:31 +00:00
|
|
|
uint32_t function_index, ExecutionTier tier);
|
2018-08-17 12:35:29 +00:00
|
|
|
|
2018-07-31 08:16:22 +00:00
|
|
|
// Exports the sharable parts of the given module object so that they can be
|
|
|
|
// transferred to a different Context/Isolate using the same engine.
|
|
|
|
std::shared_ptr<NativeModule> ExportNativeModule(
|
|
|
|
Handle<WasmModuleObject> module_object);
|
|
|
|
|
|
|
|
// Imports the shared part of a module from a different Context/Isolate using
|
|
|
|
// the the same engine, recreating a full module object in the given Isolate.
|
|
|
|
Handle<WasmModuleObject> ImportNativeModule(
|
2018-09-06 15:51:40 +00:00
|
|
|
Isolate* isolate, const std::shared_ptr<NativeModule>& shared_module);
|
2018-07-31 08:16:22 +00:00
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
WasmCodeManager* code_manager() const { return code_manager_.get(); }
|
|
|
|
|
2018-03-20 17:23:01 +00:00
|
|
|
WasmMemoryTracker* memory_tracker() { return &memory_tracker_; }
|
2018-01-06 01:33:31 +00:00
|
|
|
|
2018-07-09 08:02:54 +00:00
|
|
|
AccountingAllocator* allocator() { return &allocator_; }
|
|
|
|
|
2018-07-10 13:15:29 +00:00
|
|
|
// Compilation statistics for TurboFan compilations.
|
|
|
|
CompilationStatistics* GetOrCreateTurboStatistics();
|
|
|
|
|
|
|
|
// Prints the gathered compilation statistics, then resets them.
|
|
|
|
void DumpAndResetTurboStatistics();
|
|
|
|
|
2018-07-16 11:52:11 +00:00
|
|
|
// Used to redirect tracing output from {stdout} to a file.
|
|
|
|
CodeTracer* GetCodeTracer();
|
|
|
|
|
2018-05-09 13:48:47 +00:00
|
|
|
// Remove {job} from the list of active compile jobs.
|
|
|
|
std::unique_ptr<AsyncCompileJob> RemoveCompileJob(AsyncCompileJob* job);
|
|
|
|
|
2018-08-01 12:36:38 +00:00
|
|
|
// Returns true if at least one AsyncCompileJob that belongs to the given
|
|
|
|
// Isolate is currently running.
|
|
|
|
bool HasRunningCompileJob(Isolate* isolate);
|
2018-05-09 13:48:47 +00:00
|
|
|
|
2018-08-13 09:01:48 +00:00
|
|
|
// Deletes all AsyncCompileJobs that belong to the given Isolate. All
|
|
|
|
// compilation is aborted, no more callbacks will be triggered. This is used
|
|
|
|
// for tearing down an isolate, or to clean it up to be reused.
|
2018-07-26 13:08:04 +00:00
|
|
|
void DeleteCompileJobsOnIsolate(Isolate* isolate);
|
2018-03-19 09:22:23 +00:00
|
|
|
|
2018-07-24 15:58:31 +00:00
|
|
|
// Call on process start and exit.
|
|
|
|
static void InitializeOncePerProcess();
|
|
|
|
static void GlobalTearDown();
|
|
|
|
|
|
|
|
// Constructs a WasmEngine instance. Depending on whether we are sharing
|
|
|
|
// engines this might be a pointer to a new instance or to a shared one.
|
|
|
|
static std::shared_ptr<WasmEngine> GetWasmEngine();
|
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
private:
|
2018-05-24 21:22:27 +00:00
|
|
|
AsyncCompileJob* CreateAsyncCompileJob(
|
2018-08-08 14:54:44 +00:00
|
|
|
Isolate* isolate, const WasmFeatures& enabled,
|
|
|
|
std::unique_ptr<byte[]> bytes_copy, size_t length,
|
2018-05-24 21:22:27 +00:00
|
|
|
Handle<Context> context,
|
2018-08-13 13:35:54 +00:00
|
|
|
std::shared_ptr<CompilationResultResolver> resolver);
|
2018-07-31 16:30:57 +00:00
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
std::unique_ptr<WasmCodeManager> code_manager_;
|
2018-03-20 17:23:01 +00:00
|
|
|
WasmMemoryTracker memory_tracker_;
|
2018-07-09 08:02:54 +00:00
|
|
|
AccountingAllocator allocator_;
|
2018-01-06 01:33:31 +00:00
|
|
|
|
2018-07-16 11:52:11 +00:00
|
|
|
// This mutex protects all information which is mutated concurrently or
|
|
|
|
// fields that are initialized lazily on the first access.
|
|
|
|
base::Mutex mutex_;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Protected by {mutex_}:
|
|
|
|
|
2018-07-31 09:41:11 +00:00
|
|
|
// We use an AsyncCompileJob as the key for itself so that we can delete the
|
|
|
|
// job from the map when it is finished.
|
|
|
|
std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>> jobs_;
|
|
|
|
|
2018-07-16 11:52:11 +00:00
|
|
|
std::unique_ptr<CompilationStatistics> compilation_stats_;
|
|
|
|
std::unique_ptr<CodeTracer> code_tracer_;
|
|
|
|
|
|
|
|
// End of fields protected by {mutex_}.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-06 01:33:31 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WasmEngine);
|
2017-12-05 00:28:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
2018-02-02 12:05:19 +00:00
|
|
|
#endif // V8_WASM_WASM_ENGINE_H_
|