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