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_STREAMING_DECODER_H_ 6 #define V8_WASM_STREAMING_DECODER_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "src/base/macros.h" 12 #include "src/utils/vector.h" 13 #include "src/wasm/compilation-environment.h" 14 #include "src/wasm/wasm-constants.h" 15 #include "src/wasm/wasm-engine.h" 16 #include "src/wasm/wasm-result.h" 17 18 namespace v8 { 19 namespace internal { 20 namespace wasm { 21 class NativeModule; 22 23 // This class is an interface for the StreamingDecoder to start the processing 24 // of the incoming module bytes. 25 class V8_EXPORT_PRIVATE StreamingProcessor { 26 public: 27 virtual ~StreamingProcessor() = default; 28 // Process the first 8 bytes of a WebAssembly module. Returns true if the 29 // processing finished successfully and the decoding should continue. 30 virtual bool ProcessModuleHeader(Vector<const uint8_t> bytes, 31 uint32_t offset) = 0; 32 33 // Process all sections but the code section. Returns true if the processing 34 // finished successfully and the decoding should continue. 35 virtual bool ProcessSection(SectionCode section_code, 36 Vector<const uint8_t> bytes, uint32_t offset) = 0; 37 38 // Process the start of the code section. Returns true if the processing 39 // finished successfully and the decoding should continue. 40 virtual bool ProcessCodeSectionHeader(int num_functions, uint32_t offset, 41 std::shared_ptr<WireBytesStorage>, 42 int code_section_length) = 0; 43 44 // Process a function body. Returns true if the processing finished 45 // successfully and the decoding should continue. 46 virtual bool ProcessFunctionBody(Vector<const uint8_t> bytes, 47 uint32_t offset) = 0; 48 49 // Report the end of a chunk. 50 virtual void OnFinishedChunk() = 0; 51 // Report the end of the stream. If the stream was successful, all 52 // received bytes are passed by parameter. If there has been an error, an 53 // empty array is passed. 54 virtual void OnFinishedStream(OwnedVector<uint8_t> bytes) = 0; 55 // Report an error detected in the StreamingDecoder. 56 virtual void OnError(const WasmError&) = 0; 57 // Report the abortion of the stream. 58 virtual void OnAbort() = 0; 59 60 // Attempt to deserialize the module. Supports embedder caching. 61 virtual bool Deserialize(Vector<const uint8_t> module_bytes, 62 Vector<const uint8_t> wire_bytes) = 0; 63 }; 64 65 // The StreamingDecoder takes a sequence of byte arrays, each received by a call 66 // of {OnBytesReceived}, and extracts the bytes which belong to section payloads 67 // and function bodies. 68 class V8_EXPORT_PRIVATE StreamingDecoder { 69 public: 70 virtual ~StreamingDecoder() = default; 71 72 // The buffer passed into OnBytesReceived is owned by the caller. 73 virtual void OnBytesReceived(Vector<const uint8_t> bytes) = 0; 74 75 virtual void Finish() = 0; 76 77 virtual void Abort() = 0; 78 79 // Notify the StreamingDecoder that compilation ended and the 80 // StreamingProcessor should not be called anymore. 81 virtual void NotifyCompilationEnded() = 0; 82 83 // Caching support. 84 // Sets the callback that is called after the module is fully compiled. 85 using ModuleCompiledCallback = 86 std::function<void(const std::shared_ptr<NativeModule>&)>; 87 SetModuleCompiledCallback(ModuleCompiledCallback callback)88 void SetModuleCompiledCallback(ModuleCompiledCallback callback) { 89 module_compiled_callback_ = callback; 90 } 91 92 // Passes previously compiled module bytes from the embedder's cache. SetCompiledModuleBytes(Vector<const uint8_t> compiled_module_bytes)93 bool SetCompiledModuleBytes(Vector<const uint8_t> compiled_module_bytes) { 94 compiled_module_bytes_ = compiled_module_bytes; 95 return true; 96 } 97 98 virtual void NotifyNativeModuleCreated( 99 const std::shared_ptr<NativeModule>& native_module) = 0; 100 url()101 Vector<const char> url() { return VectorOf(url_); } 102 SetUrl(Vector<const char> url)103 void SetUrl(Vector<const char> url) { 104 url_.assign(url.begin(), url.length()); 105 } 106 107 static std::unique_ptr<StreamingDecoder> CreateAsyncStreamingDecoder( 108 std::unique_ptr<StreamingProcessor> processor); 109 110 static std::unique_ptr<StreamingDecoder> CreateSyncStreamingDecoder( 111 Isolate* isolate, const WasmFeatures& enabled, Handle<Context> context, 112 const char* api_method_name_for_errors, 113 std::shared_ptr<CompilationResultResolver> resolver); 114 115 protected: deserializing()116 bool deserializing() const { return !compiled_module_bytes_.empty(); } 117 118 std::string url_; 119 ModuleCompiledCallback module_compiled_callback_; 120 Vector<const uint8_t> compiled_module_bytes_; 121 }; 122 123 } // namespace wasm 124 } // namespace internal 125 } // namespace v8 126 127 #endif // V8_WASM_STREAMING_DECODER_H_ 128