wasm-engine.h 7.38 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_WASM_WASM_ENGINE_H_
#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 17 18

namespace v8 {
namespace internal {

19
class AsmWasmData;
20
class CodeTracer;
21
class CompilationStatistics;
22
class WasmInstanceObject;
23
class WasmModuleObject;
24

25 26
namespace wasm {

27
class AsyncCompileJob;
28 29
class ErrorThrower;
struct ModuleWireBytes;
30
struct WasmFeatures;
31

32 33 34 35
class V8_EXPORT_PRIVATE CompilationResultResolver {
 public:
  virtual void OnCompilationSucceeded(Handle<WasmModuleObject> result) = 0;
  virtual void OnCompilationFailed(Handle<Object> error_reason) = 0;
36
  virtual ~CompilationResultResolver() = default;
37 38 39 40 41 42
};

class V8_EXPORT_PRIVATE InstantiationResultResolver {
 public:
  virtual void OnInstantiationSucceeded(Handle<WasmInstanceObject> result) = 0;
  virtual void OnInstantiationFailed(Handle<Object> error_reason) = 0;
43
  virtual ~InstantiationResultResolver() = default;
44 45
};

46 47
// The central data structure that represents an engine instance capable of
// loading, instantiating, and executing WASM code.
48
class V8_EXPORT_PRIVATE WasmEngine {
49
 public:
50
  WasmEngine();
51
  ~WasmEngine();
52

53 54
  // Synchronously validates the given bytes that represent an encoded WASM
  // module.
55 56
  bool SyncValidate(Isolate* isolate, const WasmFeatures& enabled,
                    const ModuleWireBytes& bytes);
57

58 59
  // Synchronously compiles the given bytes that represent a translated
  // asm.js module.
60
  MaybeHandle<AsmWasmData> SyncCompileTranslatedAsmJs(
61
      Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes,
62 63 64 65 66
      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);
67 68 69 70

  // Synchronously compiles the given bytes that represent an encoded WASM
  // module.
  MaybeHandle<WasmModuleObject> SyncCompile(Isolate* isolate,
71
                                            const WasmFeatures& enabled,
72 73 74 75 76 77 78 79 80 81 82 83
                                            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
84
  // encoded WASM module.
85 86
  // The {is_shared} flag indicates if the bytes backing the module could
  // be shared across threads, i.e. could be concurrently modified.
87
  void AsyncCompile(Isolate* isolate, const WasmFeatures& enabled,
88
                    std::shared_ptr<CompilationResultResolver> resolver,
89 90
                    const ModuleWireBytes& bytes, bool is_shared);

91 92 93
  // Begin an asynchronous instantiation of the given WASM module.
  void AsyncInstantiate(Isolate* isolate,
                        std::unique_ptr<InstantiationResultResolver> resolver,
94 95 96
                        Handle<WasmModuleObject> module_object,
                        MaybeHandle<JSReceiver> imports);

97
  std::shared_ptr<StreamingDecoder> StartStreamingCompilation(
98
      Isolate* isolate, const WasmFeatures& enabled, Handle<Context> context,
99
      std::shared_ptr<CompilationResultResolver> resolver);
100

101
  // Compiles the function with the given index at a specific compilation tier
102 103
  // and returns true on success, false otherwise. This is mostly used for
  // testing to force a function into a specific tier.
104
  bool CompileFunction(Isolate* isolate, NativeModule* native_module,
105
                       uint32_t function_index, ExecutionTier tier);
106

107 108 109 110 111 112 113 114
  // 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(
115
      Isolate* isolate, std::shared_ptr<NativeModule> shared_module);
116

117
  WasmCodeManager* code_manager() { return &code_manager_; }
118

119
  WasmMemoryTracker* memory_tracker() { return &memory_tracker_; }
120

121 122
  AccountingAllocator* allocator() { return &allocator_; }

123 124 125 126 127 128
  // Compilation statistics for TurboFan compilations.
  CompilationStatistics* GetOrCreateTurboStatistics();

  // Prints the gathered compilation statistics, then resets them.
  void DumpAndResetTurboStatistics();

129 130 131
  // Used to redirect tracing output from {stdout} to a file.
  CodeTracer* GetCodeTracer();

132 133 134
  // Remove {job} from the list of active compile jobs.
  std::unique_ptr<AsyncCompileJob> RemoveCompileJob(AsyncCompileJob* job);

135 136 137
  // Returns true if at least one AsyncCompileJob that belongs to the given
  // Isolate is currently running.
  bool HasRunningCompileJob(Isolate* isolate);
138

139 140 141
  // 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.
142
  void DeleteCompileJobsOnIsolate(Isolate* isolate);
143

144 145 146 147
  // Manage the set of Isolates that use this WasmEngine.
  void AddIsolate(Isolate* isolate);
  void RemoveIsolate(Isolate* isolate);

148 149 150 151 152 153 154 155
  // 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();

156
 private:
157
  AsyncCompileJob* CreateAsyncCompileJob(
158 159
      Isolate* isolate, const WasmFeatures& enabled,
      std::unique_ptr<byte[]> bytes_copy, size_t length,
160
      Handle<Context> context,
161
      std::shared_ptr<CompilationResultResolver> resolver);
162

163
  WasmMemoryTracker memory_tracker_;
164
  WasmCodeManager code_manager_;
165
  AccountingAllocator allocator_;
166

167 168 169 170 171 172 173
  // 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_}:

174 175 176 177
  // 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_;

178 179 180
  std::unique_ptr<CompilationStatistics> compilation_stats_;
  std::unique_ptr<CodeTracer> code_tracer_;

181 182 183
  // Set of isolates which use this WasmEngine. Used for cross-isolate GCs.
  std::unordered_set<Isolate*> isolates_;

184 185 186
  // End of fields protected by {mutex_}.
  //////////////////////////////////////////////////////////////////////////////

187
  DISALLOW_COPY_AND_ASSIGN(WasmEngine);
188 189 190 191 192 193
};

}  // namespace wasm
}  // namespace internal
}  // namespace v8

194
#endif  // V8_WASM_WASM_ENGINE_H_