Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_WASM_WASM_ENGINE_H_
6 : #define V8_WASM_WASM_ENGINE_H_
7 :
8 : #include <memory>
9 : #include <unordered_set>
10 :
11 : #include "src/wasm/wasm-code-manager.h"
12 : #include "src/wasm/wasm-memory.h"
13 : #include "src/wasm/wasm-tier.h"
14 : #include "src/zone/accounting-allocator.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : class AsmWasmData;
20 : class CodeTracer;
21 : class CompilationStatistics;
22 : class HeapNumber;
23 : class WasmInstanceObject;
24 : class WasmModuleObject;
25 :
26 : namespace wasm {
27 :
28 : class AsyncCompileJob;
29 : class ErrorThrower;
30 : struct ModuleWireBytes;
31 : struct WasmFeatures;
32 :
33 2973 : class V8_EXPORT_PRIVATE CompilationResultResolver {
34 : public:
35 : virtual void OnCompilationSucceeded(Handle<WasmModuleObject> result) = 0;
36 : virtual void OnCompilationFailed(Handle<Object> error_reason) = 0;
37 2973 : virtual ~CompilationResultResolver() = default;
38 : };
39 :
40 9154 : class V8_EXPORT_PRIVATE InstantiationResultResolver {
41 : public:
42 : virtual void OnInstantiationSucceeded(Handle<WasmInstanceObject> result) = 0;
43 : virtual void OnInstantiationFailed(Handle<Object> error_reason) = 0;
44 9154 : virtual ~InstantiationResultResolver() = default;
45 : };
46 :
47 : // The central data structure that represents an engine instance capable of
48 : // loading, instantiating, and executing WASM code.
49 : class V8_EXPORT_PRIVATE WasmEngine {
50 : public:
51 : WasmEngine();
52 : ~WasmEngine();
53 :
54 : // Synchronously validates the given bytes that represent an encoded WASM
55 : // module.
56 : bool SyncValidate(Isolate* isolate, const WasmFeatures& enabled,
57 : const ModuleWireBytes& bytes);
58 :
59 : // Synchronously compiles the given bytes that represent a translated
60 : // asm.js module.
61 : MaybeHandle<AsmWasmData> SyncCompileTranslatedAsmJs(
62 : Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
63 : Vector<const byte> asm_js_offset_table_bytes,
64 : Handle<HeapNumber> uses_bitset);
65 : Handle<WasmModuleObject> FinalizeTranslatedAsmJs(
66 : Isolate* isolate, Handle<AsmWasmData> asm_wasm_data,
67 : Handle<Script> script);
68 :
69 : // Synchronously compiles the given bytes that represent an encoded WASM
70 : // module.
71 : MaybeHandle<WasmModuleObject> SyncCompile(Isolate* isolate,
72 : const WasmFeatures& enabled,
73 : ErrorThrower* thrower,
74 : const ModuleWireBytes& bytes);
75 :
76 : // Synchronously instantiate the given WASM module with the given imports.
77 : // If the module represents an asm.js module, then the supplied {memory}
78 : // should be used as the memory of the instance.
79 : MaybeHandle<WasmInstanceObject> SyncInstantiate(
80 : Isolate* isolate, ErrorThrower* thrower,
81 : Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
82 : MaybeHandle<JSArrayBuffer> memory);
83 :
84 : // Begin an asynchronous compilation of the given bytes that represent an
85 : // encoded WASM module.
86 : // The {is_shared} flag indicates if the bytes backing the module could
87 : // be shared across threads, i.e. could be concurrently modified.
88 : void AsyncCompile(Isolate* isolate, const WasmFeatures& enabled,
89 : std::shared_ptr<CompilationResultResolver> resolver,
90 : const ModuleWireBytes& bytes, bool is_shared);
91 :
92 : // Begin an asynchronous instantiation of the given WASM module.
93 : void AsyncInstantiate(Isolate* isolate,
94 : std::unique_ptr<InstantiationResultResolver> resolver,
95 : Handle<WasmModuleObject> module_object,
96 : MaybeHandle<JSReceiver> imports);
97 :
98 : std::shared_ptr<StreamingDecoder> StartStreamingCompilation(
99 : Isolate* isolate, const WasmFeatures& enabled, Handle<Context> context,
100 : std::shared_ptr<CompilationResultResolver> resolver);
101 :
102 : // Compiles the function with the given index at a specific compilation tier.
103 : // Errors are stored internally in the CompilationState.
104 : // This is mostly used for testing to force a function into a specific tier.
105 : void CompileFunction(Isolate* isolate, NativeModule* native_module,
106 : uint32_t function_index, ExecutionTier tier);
107 :
108 : // Exports the sharable parts of the given module object so that they can be
109 : // transferred to a different Context/Isolate using the same engine.
110 : std::shared_ptr<NativeModule> ExportNativeModule(
111 : Handle<WasmModuleObject> module_object);
112 :
113 : // Imports the shared part of a module from a different Context/Isolate using
114 : // the the same engine, recreating a full module object in the given Isolate.
115 : Handle<WasmModuleObject> ImportNativeModule(
116 : Isolate* isolate, std::shared_ptr<NativeModule> shared_module);
117 :
118 : WasmCodeManager* code_manager() { return &code_manager_; }
119 :
120 : WasmMemoryTracker* memory_tracker() { return &memory_tracker_; }
121 :
122 : AccountingAllocator* allocator() { return &allocator_; }
123 :
124 : // Compilation statistics for TurboFan compilations.
125 : CompilationStatistics* GetOrCreateTurboStatistics();
126 :
127 : // Prints the gathered compilation statistics, then resets them.
128 : void DumpAndResetTurboStatistics();
129 :
130 : // Used to redirect tracing output from {stdout} to a file.
131 : CodeTracer* GetCodeTracer();
132 :
133 : // Remove {job} from the list of active compile jobs.
134 : std::unique_ptr<AsyncCompileJob> RemoveCompileJob(AsyncCompileJob* job);
135 :
136 : // Returns true if at least one AsyncCompileJob that belongs to the given
137 : // Isolate is currently running.
138 : bool HasRunningCompileJob(Isolate* isolate);
139 :
140 : // Deletes all AsyncCompileJobs that belong to the given Isolate. All
141 : // compilation is aborted, no more callbacks will be triggered. This is used
142 : // for tearing down an isolate, or to clean it up to be reused.
143 : void DeleteCompileJobsOnIsolate(Isolate* isolate);
144 :
145 : // Manage the set of Isolates that use this WasmEngine.
146 : void AddIsolate(Isolate* isolate);
147 : void RemoveIsolate(Isolate* isolate);
148 :
149 : // Call on process start and exit.
150 : static void InitializeOncePerProcess();
151 : static void GlobalTearDown();
152 :
153 : // Constructs a WasmEngine instance. Depending on whether we are sharing
154 : // engines this might be a pointer to a new instance or to a shared one.
155 : static std::shared_ptr<WasmEngine> GetWasmEngine();
156 :
157 : private:
158 : AsyncCompileJob* CreateAsyncCompileJob(
159 : Isolate* isolate, const WasmFeatures& enabled,
160 : std::unique_ptr<byte[]> bytes_copy, size_t length,
161 : Handle<Context> context,
162 : std::shared_ptr<CompilationResultResolver> resolver);
163 :
164 : WasmMemoryTracker memory_tracker_;
165 : WasmCodeManager code_manager_;
166 : AccountingAllocator allocator_;
167 :
168 : // This mutex protects all information which is mutated concurrently or
169 : // fields that are initialized lazily on the first access.
170 : base::Mutex mutex_;
171 :
172 : //////////////////////////////////////////////////////////////////////////////
173 : // Protected by {mutex_}:
174 :
175 : // We use an AsyncCompileJob as the key for itself so that we can delete the
176 : // job from the map when it is finished.
177 : std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>> jobs_;
178 :
179 : std::unique_ptr<CompilationStatistics> compilation_stats_;
180 : std::unique_ptr<CodeTracer> code_tracer_;
181 :
182 : // Set of isolates which use this WasmEngine. Used for cross-isolate GCs.
183 : std::unordered_set<Isolate*> isolates_;
184 :
185 : // End of fields protected by {mutex_}.
186 : //////////////////////////////////////////////////////////////////////////////
187 :
188 : DISALLOW_COPY_AND_ASSIGN(WasmEngine);
189 : };
190 :
191 : } // namespace wasm
192 : } // namespace internal
193 : } // namespace v8
194 :
195 : #endif // V8_WASM_WASM_ENGINE_H_
|