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/cancelable-task.h"
12 : #include "src/wasm/wasm-code-manager.h"
13 : #include "src/wasm/wasm-memory.h"
14 : #include "src/wasm/wasm-tier.h"
15 : #include "src/zone/accounting-allocator.h"
16 :
17 : namespace v8 {
18 : namespace internal {
19 :
20 : class AsmWasmData;
21 : class CodeTracer;
22 : class CompilationStatistics;
23 : class HeapNumber;
24 : class WasmInstanceObject;
25 : class WasmModuleObject;
26 :
27 : namespace wasm {
28 :
29 : class AsyncCompileJob;
30 : class ErrorThrower;
31 : struct ModuleWireBytes;
32 : struct WasmFeatures;
33 :
34 2811 : class V8_EXPORT_PRIVATE CompilationResultResolver {
35 : public:
36 : virtual void OnCompilationSucceeded(Handle<WasmModuleObject> result) = 0;
37 : virtual void OnCompilationFailed(Handle<Object> error_reason) = 0;
38 2811 : virtual ~CompilationResultResolver() = default;
39 : };
40 :
41 9139 : class V8_EXPORT_PRIVATE InstantiationResultResolver {
42 : public:
43 : virtual void OnInstantiationSucceeded(Handle<WasmInstanceObject> result) = 0;
44 : virtual void OnInstantiationFailed(Handle<Object> error_reason) = 0;
45 9139 : virtual ~InstantiationResultResolver() = default;
46 : };
47 :
48 : // The central data structure that represents an engine instance capable of
49 : // loading, instantiating, and executing WASM code.
50 : class V8_EXPORT_PRIVATE WasmEngine {
51 : public:
52 : WasmEngine();
53 : ~WasmEngine();
54 :
55 : // Synchronously validates the given bytes that represent an encoded WASM
56 : // module.
57 : bool SyncValidate(Isolate* isolate, const WasmFeatures& enabled,
58 : const ModuleWireBytes& bytes);
59 :
60 : // Synchronously compiles the given bytes that represent a translated
61 : // asm.js module.
62 : MaybeHandle<AsmWasmData> SyncCompileTranslatedAsmJs(
63 : Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
64 : Vector<const byte> asm_js_offset_table_bytes,
65 : Handle<HeapNumber> uses_bitset);
66 : Handle<WasmModuleObject> FinalizeTranslatedAsmJs(
67 : Isolate* isolate, Handle<AsmWasmData> asm_wasm_data,
68 : Handle<Script> script);
69 :
70 : // Synchronously compiles the given bytes that represent an encoded WASM
71 : // module.
72 : MaybeHandle<WasmModuleObject> SyncCompile(Isolate* isolate,
73 : const WasmFeatures& enabled,
74 : ErrorThrower* thrower,
75 : const ModuleWireBytes& bytes);
76 :
77 : // Synchronously instantiate the given WASM module with the given imports.
78 : // If the module represents an asm.js module, then the supplied {memory}
79 : // should be used as the memory of the instance.
80 : MaybeHandle<WasmInstanceObject> SyncInstantiate(
81 : Isolate* isolate, ErrorThrower* thrower,
82 : Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
83 : MaybeHandle<JSArrayBuffer> memory);
84 :
85 : // Begin an asynchronous compilation of the given bytes that represent an
86 : // encoded WASM module.
87 : // The {is_shared} flag indicates if the bytes backing the module could
88 : // be shared across threads, i.e. could be concurrently modified.
89 : void AsyncCompile(Isolate* isolate, const WasmFeatures& enabled,
90 : std::shared_ptr<CompilationResultResolver> resolver,
91 : const ModuleWireBytes& bytes, bool is_shared);
92 :
93 : // Begin an asynchronous instantiation of the given WASM module.
94 : void AsyncInstantiate(Isolate* isolate,
95 : std::unique_ptr<InstantiationResultResolver> resolver,
96 : Handle<WasmModuleObject> module_object,
97 : MaybeHandle<JSReceiver> imports);
98 :
99 : std::shared_ptr<StreamingDecoder> StartStreamingCompilation(
100 : Isolate* isolate, const WasmFeatures& enabled, Handle<Context> context,
101 : std::shared_ptr<CompilationResultResolver> resolver);
102 :
103 : // Compiles the function with the given index at a specific compilation tier.
104 : // Errors are stored internally in the CompilationState.
105 : // This is mostly used for testing to force a function into a specific tier.
106 : void CompileFunction(Isolate* isolate, NativeModule* native_module,
107 : uint32_t function_index, ExecutionTier tier);
108 :
109 : // Exports the sharable parts of the given module object so that they can be
110 : // transferred to a different Context/Isolate using the same engine.
111 : std::shared_ptr<NativeModule> ExportNativeModule(
112 : Handle<WasmModuleObject> module_object);
113 :
114 : // Imports the shared part of a module from a different Context/Isolate using
115 : // the the same engine, recreating a full module object in the given Isolate.
116 : Handle<WasmModuleObject> ImportNativeModule(
117 : Isolate* isolate, std::shared_ptr<NativeModule> shared_module);
118 :
119 54367898 : WasmCodeManager* code_manager() { return &code_manager_; }
120 :
121 554232 : WasmMemoryTracker* memory_tracker() { return &memory_tracker_; }
122 :
123 4003428 : AccountingAllocator* allocator() { return &allocator_; }
124 :
125 : // Compilation statistics for TurboFan compilations.
126 : CompilationStatistics* GetOrCreateTurboStatistics();
127 :
128 : // Prints the gathered compilation statistics, then resets them.
129 : void DumpAndResetTurboStatistics();
130 :
131 : // Used to redirect tracing output from {stdout} to a file.
132 : CodeTracer* GetCodeTracer();
133 :
134 : // Remove {job} from the list of active compile jobs.
135 : std::unique_ptr<AsyncCompileJob> RemoveCompileJob(AsyncCompileJob* job);
136 :
137 : // Returns true if at least one AsyncCompileJob that belongs to the given
138 : // Isolate is currently running.
139 : bool HasRunningCompileJob(Isolate* isolate);
140 :
141 : // Deletes all AsyncCompileJobs that belong to the given Isolate. All
142 : // compilation is aborted, no more callbacks will be triggered. This is used
143 : // for tearing down an isolate, or to clean it up to be reused.
144 : void DeleteCompileJobsOnIsolate(Isolate* isolate);
145 :
146 : // Manage the set of Isolates that use this WasmEngine.
147 : void AddIsolate(Isolate* isolate);
148 : void RemoveIsolate(Isolate* isolate);
149 :
150 : template <typename T, typename... Args>
151 : std::unique_ptr<T> NewBackgroundCompileTask(Args&&... args) {
152 : return base::make_unique<T>(&background_compile_task_manager_,
153 32898 : std::forward<Args>(args)...);
154 : }
155 :
156 : // Trigger code logging for this WasmCode in all Isolates which have access to
157 : // the NativeModule containing this code. This method can be called from
158 : // background threads.
159 : void LogCode(WasmCode*);
160 :
161 : // Enable code logging for the given Isolate. Initially, code logging is
162 : // enabled if {WasmCode::ShouldBeLogged(Isolate*)} returns true during
163 : // {AddIsolate}.
164 : void EnableCodeLogging(Isolate*);
165 :
166 : // Create a new NativeModule. The caller is responsible for its
167 : // lifetime. The native module will be given some memory for code,
168 : // which will be page size aligned. The size of the initial memory
169 : // is determined with a heuristic based on the total size of wasm
170 : // code. The native module may later request more memory.
171 : // TODO(titzer): isolate is only required here for CompilationState.
172 : std::shared_ptr<NativeModule> NewNativeModule(
173 : Isolate* isolate, const WasmFeatures& enabled_features,
174 : size_t code_size_estimate, bool can_request_more,
175 : std::shared_ptr<const WasmModule> module);
176 :
177 : void FreeNativeModule(NativeModule*);
178 :
179 : // Sample the code size of the given {NativeModule} in all isolates that have
180 : // access to it. Call this after top-tier compilation finished.
181 : // This will spawn foreground tasks that do *not* keep the NativeModule alive.
182 : void SampleTopTierCodeSizeInAllIsolates(const std::shared_ptr<NativeModule>&);
183 :
184 : // Call on process start and exit.
185 : static void InitializeOncePerProcess();
186 : static void GlobalTearDown();
187 :
188 : // Constructs a WasmEngine instance. Depending on whether we are sharing
189 : // engines this might be a pointer to a new instance or to a shared one.
190 : static std::shared_ptr<WasmEngine> GetWasmEngine();
191 :
192 : private:
193 : struct IsolateInfo;
194 :
195 : AsyncCompileJob* CreateAsyncCompileJob(
196 : Isolate* isolate, const WasmFeatures& enabled,
197 : std::unique_ptr<byte[]> bytes_copy, size_t length,
198 : Handle<Context> context,
199 : std::shared_ptr<CompilationResultResolver> resolver);
200 :
201 : WasmMemoryTracker memory_tracker_;
202 : WasmCodeManager code_manager_;
203 : AccountingAllocator allocator_;
204 :
205 : // Task manager managing all background compile jobs. Before shut down of the
206 : // engine, they must all be finished because they access the allocator.
207 : CancelableTaskManager background_compile_task_manager_;
208 :
209 : // This mutex protects all information which is mutated concurrently or
210 : // fields that are initialized lazily on the first access.
211 : base::Mutex mutex_;
212 :
213 : //////////////////////////////////////////////////////////////////////////////
214 : // Protected by {mutex_}:
215 :
216 : // We use an AsyncCompileJob as the key for itself so that we can delete the
217 : // job from the map when it is finished.
218 : std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>>
219 : async_compile_jobs_;
220 :
221 : std::unique_ptr<CompilationStatistics> compilation_stats_;
222 : std::unique_ptr<CodeTracer> code_tracer_;
223 :
224 : // Set of isolates which use this WasmEngine.
225 : std::unordered_map<Isolate*, std::unique_ptr<IsolateInfo>> isolates_;
226 :
227 : // Maps each NativeModule to the set of Isolates that have access to that
228 : // NativeModule. The isolate sets currently only grow, they never shrink.
229 : std::unordered_map<NativeModule*, std::unordered_set<Isolate*>>
230 : isolates_per_native_module_;
231 :
232 : // End of fields protected by {mutex_}.
233 : //////////////////////////////////////////////////////////////////////////////
234 :
235 : DISALLOW_COPY_AND_ASSIGN(WasmEngine);
236 : };
237 :
238 : } // namespace wasm
239 : } // namespace internal
240 : } // namespace v8
241 :
242 : #endif // V8_WASM_WASM_ENGINE_H_
|