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 #if !V8_ENABLE_WEBASSEMBLY 6 #error This header should only be included if WebAssembly is enabled. 7 #endif // !V8_ENABLE_WEBASSEMBLY 8 9 #ifndef V8_WASM_WASM_SERIALIZATION_H_ 10 #define V8_WASM_WASM_SERIALIZATION_H_ 11 12 #include "src/wasm/wasm-code-manager.h" 13 #include "src/wasm/wasm-objects.h" 14 15 namespace v8 { 16 namespace internal { 17 namespace wasm { 18 19 // Support for serializing WebAssembly {NativeModule} objects. This class takes 20 // a snapshot of the module state at instantiation, and other code that modifies 21 // the module after that won't affect the serialized result. 22 class V8_EXPORT_PRIVATE WasmSerializer { 23 public: 24 explicit WasmSerializer(NativeModule* native_module); 25 26 // Measure the required buffer size needed for serialization. 27 size_t GetSerializedNativeModuleSize() const; 28 29 // Serialize the {NativeModule} into the provided {buffer}. Returns true on 30 // success and false if the given buffer it too small for serialization. 31 bool SerializeNativeModule(base::Vector<byte> buffer) const; 32 33 // The data header consists of uint32_t-sized entries (see {WriteVersion}): 34 // [0] magic number 35 // [1] version hash 36 // [2] supported CPU features 37 // [3] flag hash 38 // ... number of functions 39 // ... serialized functions 40 static constexpr size_t kMagicNumberOffset = 0; 41 static constexpr size_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size; 42 static constexpr size_t kSupportedCPUFeaturesOffset = 43 kVersionHashOffset + kUInt32Size; 44 static constexpr size_t kFlagHashOffset = 45 kSupportedCPUFeaturesOffset + kUInt32Size; 46 static constexpr size_t kHeaderSize = 4 * kUInt32Size; 47 48 private: 49 NativeModule* native_module_; 50 // The {WasmCodeRefScope} keeps the pointers in {code_table_} alive. 51 WasmCodeRefScope code_ref_scope_; 52 std::vector<WasmCode*> code_table_; 53 }; 54 55 // Support for deserializing WebAssembly {NativeModule} objects. 56 // Checks the version header of the data against the current version. 57 bool IsSupportedVersion(base::Vector<const byte> data); 58 59 // Deserializes the given data to create a Wasm module object. 60 V8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> DeserializeNativeModule( 61 Isolate*, base::Vector<const byte> data, 62 base::Vector<const byte> wire_bytes, base::Vector<const char> source_url); 63 64 } // namespace wasm 65 } // namespace internal 66 } // namespace v8 67 68 #endif // V8_WASM_WASM_SERIALIZATION_H_ 69