1 // Copyright 2018 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_COMPILATION_ENVIRONMENT_H_ 6 #define V8_WASM_COMPILATION_ENVIRONMENT_H_ 7 8 #include <memory> 9 10 #include "src/wasm/wasm-features.h" 11 #include "src/wasm/wasm-limits.h" 12 #include "src/wasm/wasm-module.h" 13 #include "src/wasm/wasm-tier.h" 14 15 namespace v8 { 16 17 class JobHandle; 18 19 namespace internal { 20 21 class Counters; 22 23 namespace wasm { 24 25 class NativeModule; 26 class WasmCode; 27 class WasmError; 28 29 enum RuntimeExceptionSupport : bool { 30 kRuntimeExceptionSupport = true, 31 kNoRuntimeExceptionSupport = false 32 }; 33 34 enum UseTrapHandler : bool { kUseTrapHandler = true, kNoTrapHandler = false }; 35 36 enum LowerSimd : bool { kLowerSimd = true, kNoLowerSimd = false }; 37 38 // The {CompilationEnv} encapsulates the module data that is used during 39 // compilation. CompilationEnvs are shareable across multiple compilations. 40 struct CompilationEnv { 41 // A pointer to the decoded module's static representation. 42 const WasmModule* const module; 43 44 // True if trap handling should be used in compiled code, rather than 45 // compiling in bounds checks for each memory access. 46 const UseTrapHandler use_trap_handler; 47 48 // If the runtime doesn't support exception propagation, 49 // we won't generate stack checks, and trap handling will also 50 // be generated differently. 51 const RuntimeExceptionSupport runtime_exception_support; 52 53 // The smallest size of any memory that could be used with this module, in 54 // bytes. 55 const uintptr_t min_memory_size; 56 57 // The largest size of any memory that could be used with this module, in 58 // bytes. 59 const uintptr_t max_memory_size; 60 61 // Features enabled for this compilation. 62 const WasmFeatures enabled_features; 63 64 const LowerSimd lower_simd; 65 66 static constexpr uint32_t kMaxMemoryPagesAtRuntime = 67 std::min(kV8MaxWasmMemoryPages, 68 std::numeric_limits<uintptr_t>::max() / kWasmPageSize); 69 70 constexpr CompilationEnv(const WasmModule* module, 71 UseTrapHandler use_trap_handler, 72 RuntimeExceptionSupport runtime_exception_support, 73 const WasmFeatures& enabled_features, 74 LowerSimd lower_simd = kNoLowerSimd) moduleCompilationEnv75 : module(module), 76 use_trap_handler(use_trap_handler), 77 runtime_exception_support(runtime_exception_support), 78 // During execution, the memory can never be bigger than what fits in a 79 // uintptr_t. 80 min_memory_size(std::min(kMaxMemoryPagesAtRuntime, 81 module ? module->initial_pages : 0) * 82 uint64_t{kWasmPageSize}), 83 max_memory_size(static_cast<uintptr_t>( 84 std::min(kMaxMemoryPagesAtRuntime, 85 module && module->has_maximum_pages ? module->maximum_pages 86 : max_mem_pages()) * 87 uint64_t{kWasmPageSize})), 88 enabled_features(enabled_features), 89 lower_simd(lower_simd) {} 90 }; 91 92 // The wire bytes are either owned by the StreamingDecoder, or (after streaming) 93 // by the NativeModule. This class abstracts over the storage location. 94 class WireBytesStorage { 95 public: 96 virtual ~WireBytesStorage() = default; 97 virtual Vector<const uint8_t> GetCode(WireBytesRef) const = 0; 98 }; 99 100 // Callbacks will receive either {kFailedCompilation} or both 101 // {kFinishedBaselineCompilation} and {kFinishedTopTierCompilation}, in that 102 // order. If tier up is off, both events are delivered right after each other. 103 enum class CompilationEvent : uint8_t { 104 kFinishedBaselineCompilation, 105 kFinishedExportWrappers, 106 kFinishedTopTierCompilation, 107 kFailedCompilation, 108 kFinishedRecompilation 109 }; 110 111 // The implementation of {CompilationState} lives in module-compiler.cc. 112 // This is the PIMPL interface to that private class. 113 class V8_EXPORT_PRIVATE CompilationState { 114 public: 115 using callback_t = std::function<void(CompilationEvent)>; 116 117 ~CompilationState(); 118 119 void CancelCompilation(); 120 121 void SetError(); 122 123 void SetWireBytesStorage(std::shared_ptr<WireBytesStorage>); 124 125 std::shared_ptr<WireBytesStorage> GetWireBytesStorage() const; 126 127 void AddCallback(callback_t); 128 129 void InitializeAfterDeserialization(); 130 131 // Wait until top tier compilation finished, or compilation failed. 132 void WaitForTopTierFinished(); 133 134 // Set a higher priority for the compilation job. 135 void SetHighPriority(); 136 137 bool failed() const; 138 bool baseline_compilation_finished() const; 139 bool top_tier_compilation_finished() const; 140 bool recompilation_finished() const; 141 142 // Override {operator delete} to avoid implicit instantiation of {operator 143 // delete} with {size_t} argument. The {size_t} argument would be incorrect. delete(void * ptr)144 void operator delete(void* ptr) { ::operator delete(ptr); } 145 146 CompilationState() = delete; 147 148 private: 149 // NativeModule is allowed to call the static {New} method. 150 friend class NativeModule; 151 152 // The CompilationState keeps a {std::weak_ptr} back to the {NativeModule} 153 // such that it can keep it alive (by regaining a {std::shared_ptr}) in 154 // certain scopes. 155 static std::unique_ptr<CompilationState> New( 156 const std::shared_ptr<NativeModule>&, std::shared_ptr<Counters>); 157 }; 158 159 } // namespace wasm 160 } // namespace internal 161 } // namespace v8 162 163 #endif // V8_WASM_COMPILATION_ENVIRONMENT_H_ 164