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>
|
2018-09-25 11:24:09 +00:00
|
|
|
#include <unordered_set>
|
2017-12-05 00:28:35 +00:00
|
|
|
|
2019-01-23 11:49:36 +00:00
|
|
|
#include "src/cancelable-task.h"
|
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-11-21 21:19:18 +00:00
|
|
|
class AsmWasmData;
|
2018-07-16 11:52:11 +00:00
|
|
|
class CodeTracer;
|
2018-07-10 13:15:29 +00:00
|
|
|
class CompilationStatistics;
|
2018-12-25 00:19:47 +00:00
|
|
|
class HeapNumber;
|
2018-01-18 10:52:52 +00:00
|
|
|
class WasmInstanceObject;
|
2018-10-23 11:37:58 +00:00
|
|
|
class WasmModuleObject;
|
2018-01-18 10:52:52 +00:00
|
|
|
|
2017-12-05 00:28:35 +00:00
|
|
|
namespace wasm {
|
|
|
|
|
2018-10-23 11:37:58 +00:00
|
|
|
class AsyncCompileJob;
|
2018-01-18 10:52:52 +00:00
|
|
|
class ErrorThrower;
|
|
|
|
struct ModuleWireBytes;
|
2018-10-23 11:37:58 +00:00
|
|
|
struct WasmFeatures;
|
2018-01-18 10:52:52 +00:00
|
|
|
|
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;
|
2018-09-13 09:27:26 +00:00
|
|
|
virtual ~CompilationResultResolver() = default;
|
2018-05-24 21:22:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class V8_EXPORT_PRIVATE InstantiationResultResolver {
|
|
|
|
public:
|
|
|
|
virtual void OnInstantiationSucceeded(Handle<WasmInstanceObject> result) = 0;
|
|
|
|
virtual void OnInstantiationFailed(Handle<Object> error_reason) = 0;
|
2018-09-13 09:27:26 +00:00
|
|
|
virtual ~InstantiationResultResolver() = default;
|
2018-05-24 21:22:27 +00:00
|
|
|
};
|
|
|
|
|
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-09-18 13:05:45 +00:00
|
|
|
WasmEngine();
|
2018-07-10 13:15:29 +00:00
|
|
|
~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.
|
2018-11-21 21:19:18 +00:00
|
|
|
MaybeHandle<AsmWasmData> SyncCompileTranslatedAsmJs(
|
2018-01-18 10:52:52 +00:00
|
|
|
Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
|
2018-11-21 21:19:18 +00:00
|
|
|
Vector<const byte> asm_js_offset_table_bytes,
|
|
|
|
Handle<HeapNumber> uses_bitset);
|
|
|
|
Handle<WasmModuleObject> FinalizeTranslatedAsmJs(
|
|
|
|
Isolate* isolate, Handle<AsmWasmData> asm_wasm_data,
|
|
|
|
Handle<Script> script);
|
2018-01-18 10:52:52 +00:00
|
|
|
|
|
|
|
// 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,
|
2019-04-30 14:04:56 +00:00
|
|
|
const char* api_method_name,
|
2018-08-13 13:35:54 +00:00
|
|
|
std::shared_ptr<CompilationResultResolver> resolver);
|
2017-12-05 00:28:35 +00:00
|
|
|
|
2018-12-12 15:18:38 +00:00
|
|
|
// Compiles the function with the given index at a specific compilation tier.
|
|
|
|
// Errors are stored internally in the CompilationState.
|
|
|
|
// This is mostly used for testing to force a function into a specific tier.
|
|
|
|
void 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-07 11:04:24 +00:00
|
|
|
Isolate* isolate, std::shared_ptr<NativeModule> shared_module);
|
2018-07-31 08:16:22 +00:00
|
|
|
|
2018-09-18 13:05:45 +00:00
|
|
|
WasmCodeManager* code_manager() { return &code_manager_; }
|
2017-12-05 00:28:35 +00:00
|
|
|
|
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.
|
2019-01-08 13:58:01 +00:00
|
|
|
std::unique_ptr<AsyncCompileJob> RemoveCompileJob(AsyncCompileJob* job);
|
2018-05-09 13:48:47 +00:00
|
|
|
|
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
|
|
|
|
2019-01-08 13:58:01 +00:00
|
|
|
// Deletes all AsyncCompileJobs that belong to the given Isolate. All
|
2018-08-13 09:01:48 +00:00
|
|
|
// 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.
|
2019-01-08 13:58:01 +00:00
|
|
|
void DeleteCompileJobsOnIsolate(Isolate* isolate);
|
2018-03-19 09:22:23 +00:00
|
|
|
|
2018-09-25 11:24:09 +00:00
|
|
|
// Manage the set of Isolates that use this WasmEngine.
|
|
|
|
void AddIsolate(Isolate* isolate);
|
|
|
|
void RemoveIsolate(Isolate* isolate);
|
|
|
|
|
2019-01-25 10:35:37 +00:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
std::unique_ptr<T> NewBackgroundCompileTask(Args&&... args) {
|
|
|
|
return base::make_unique<T>(&background_compile_task_manager_,
|
|
|
|
std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2019-01-25 10:44:42 +00:00
|
|
|
// Trigger code logging for this WasmCode in all Isolates which have access to
|
|
|
|
// the NativeModule containing this code. This method can be called from
|
|
|
|
// background threads.
|
|
|
|
void LogCode(WasmCode*);
|
|
|
|
|
2019-02-06 11:19:03 +00:00
|
|
|
// Enable code logging for the given Isolate. Initially, code logging is
|
|
|
|
// enabled if {WasmCode::ShouldBeLogged(Isolate*)} returns true during
|
|
|
|
// {AddIsolate}.
|
|
|
|
void EnableCodeLogging(Isolate*);
|
|
|
|
|
2019-04-16 11:48:09 +00:00
|
|
|
// This is called from the foreground thread of the Isolate to log all
|
|
|
|
// outstanding code objects (added via {LogCode}).
|
|
|
|
void LogOutstandingCodesForIsolate(Isolate*);
|
|
|
|
|
2019-01-25 10:35:37 +00:00
|
|
|
// Create a new NativeModule. The caller is responsible for its
|
|
|
|
// lifetime. The native module will be given some memory for code,
|
|
|
|
// which will be page size aligned. The size of the initial memory
|
|
|
|
// is determined with a heuristic based on the total size of wasm
|
|
|
|
// code. The native module may later request more memory.
|
|
|
|
// TODO(titzer): isolate is only required here for CompilationState.
|
2019-03-12 16:10:58 +00:00
|
|
|
std::shared_ptr<NativeModule> NewNativeModule(
|
2019-01-25 10:35:37 +00:00
|
|
|
Isolate* isolate, const WasmFeatures& enabled_features,
|
|
|
|
size_t code_size_estimate, bool can_request_more,
|
|
|
|
std::shared_ptr<const WasmModule> module);
|
|
|
|
|
|
|
|
void FreeNativeModule(NativeModule*);
|
|
|
|
|
2019-03-11 14:51:56 +00:00
|
|
|
// Sample the code size of the given {NativeModule} in all isolates that have
|
|
|
|
// access to it. Call this after top-tier compilation finished.
|
|
|
|
// This will spawn foreground tasks that do *not* keep the NativeModule alive.
|
|
|
|
void SampleTopTierCodeSizeInAllIsolates(const std::shared_ptr<NativeModule>&);
|
|
|
|
|
2019-05-07 09:21:49 +00:00
|
|
|
// Called by each Isolate to report its live code for a GC cycle. First
|
|
|
|
// version reports an externally determined set of live code (might be empty),
|
|
|
|
// second version gets live code from the execution stack of that isolate.
|
|
|
|
void ReportLiveCodeForGC(Isolate*, Vector<WasmCode*>);
|
|
|
|
void ReportLiveCodeFromStackForGC(Isolate*);
|
2019-04-09 13:05:37 +00:00
|
|
|
|
2019-04-09 14:00:09 +00:00
|
|
|
// Add potentially dead code. The occurrence in the set of potentially dead
|
|
|
|
// code counts as a reference, and is decremented on the next GC.
|
|
|
|
// Returns {true} if the code was added to the set of potentially dead code,
|
|
|
|
// {false} if an entry already exists. The ref count is *unchanged* in any
|
|
|
|
// case.
|
|
|
|
V8_WARN_UNUSED_RESULT bool AddPotentiallyDeadCode(WasmCode*);
|
|
|
|
|
2019-05-03 08:50:51 +00:00
|
|
|
// Free dead code.
|
|
|
|
using DeadCodeMap = std::unordered_map<NativeModule*, std::vector<WasmCode*>>;
|
|
|
|
void FreeDeadCode(const DeadCodeMap&);
|
|
|
|
void FreeDeadCodeLocked(const DeadCodeMap&);
|
2019-04-29 08:24:11 +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:
|
2019-04-09 13:05:37 +00:00
|
|
|
struct CurrentGCInfo;
|
2019-01-25 10:35:37 +00:00
|
|
|
struct IsolateInfo;
|
2019-04-09 14:00:09 +00:00
|
|
|
struct NativeModuleInfo;
|
2019-01-25 10:35:37 +00:00
|
|
|
|
2019-01-08 13:58:01 +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,
|
2019-04-30 14:04:56 +00:00
|
|
|
Handle<Context> context, const char* api_method_name,
|
2018-08-13 13:35:54 +00:00
|
|
|
std::shared_ptr<CompilationResultResolver> resolver);
|
2018-07-31 16:30:57 +00:00
|
|
|
|
2019-04-09 13:05:37 +00:00
|
|
|
void TriggerGC();
|
|
|
|
|
2018-03-20 17:23:01 +00:00
|
|
|
WasmMemoryTracker memory_tracker_;
|
2018-09-18 13:05:45 +00:00
|
|
|
WasmCodeManager code_manager_;
|
2018-07-09 08:02:54 +00:00
|
|
|
AccountingAllocator allocator_;
|
2018-01-06 01:33:31 +00:00
|
|
|
|
2019-01-23 11:49:36 +00:00
|
|
|
// Task manager managing all background compile jobs. Before shut down of the
|
|
|
|
// engine, they must all be finished because they access the allocator.
|
|
|
|
CancelableTaskManager background_compile_task_manager_;
|
|
|
|
|
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_}:
|
|
|
|
|
2019-01-08 13:58:01 +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.
|
2019-02-01 14:57:59 +00:00
|
|
|
std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>>
|
|
|
|
async_compile_jobs_;
|
2018-07-31 09:41:11 +00:00
|
|
|
|
2018-07-16 11:52:11 +00:00
|
|
|
std::unique_ptr<CompilationStatistics> compilation_stats_;
|
|
|
|
std::unique_ptr<CodeTracer> code_tracer_;
|
|
|
|
|
2019-01-25 10:35:37 +00:00
|
|
|
// Set of isolates which use this WasmEngine.
|
|
|
|
std::unordered_map<Isolate*, std::unique_ptr<IsolateInfo>> isolates_;
|
|
|
|
|
2019-04-09 14:00:09 +00:00
|
|
|
// Set of native modules managed by this engine.
|
|
|
|
std::unordered_map<NativeModule*, std::unique_ptr<NativeModuleInfo>>
|
|
|
|
native_modules_;
|
|
|
|
|
|
|
|
// Size of code that became dead since the last GC. If this exceeds a certain
|
|
|
|
// threshold, a new GC is triggered.
|
|
|
|
size_t new_potentially_dead_code_size_ = 0;
|
2018-09-25 11:24:09 +00:00
|
|
|
|
2019-04-09 13:05:37 +00:00
|
|
|
// If an engine-wide GC is currently running, this pointer stores information
|
|
|
|
// about that.
|
|
|
|
std::unique_ptr<CurrentGCInfo> current_gc_info_;
|
|
|
|
|
2018-07-16 11:52:11 +00:00
|
|
|
// 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_
|