11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#if !V8_ENABLE_WEBASSEMBLY
61cb0ef41Sopenharmony_ci#error This header should only be included if WebAssembly is enabled.
71cb0ef41Sopenharmony_ci#endif  // !V8_ENABLE_WEBASSEMBLY
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#ifndef V8_WASM_WASM_SERIALIZATION_H_
101cb0ef41Sopenharmony_ci#define V8_WASM_WASM_SERIALIZATION_H_
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "src/wasm/wasm-code-manager.h"
131cb0ef41Sopenharmony_ci#include "src/wasm/wasm-objects.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace v8 {
161cb0ef41Sopenharmony_cinamespace internal {
171cb0ef41Sopenharmony_cinamespace wasm {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// Support for serializing WebAssembly {NativeModule} objects. This class takes
201cb0ef41Sopenharmony_ci// a snapshot of the module state at instantiation, and other code that modifies
211cb0ef41Sopenharmony_ci// the module after that won't affect the serialized result.
221cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE WasmSerializer {
231cb0ef41Sopenharmony_ci public:
241cb0ef41Sopenharmony_ci  explicit WasmSerializer(NativeModule* native_module);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // Measure the required buffer size needed for serialization.
271cb0ef41Sopenharmony_ci  size_t GetSerializedNativeModuleSize() const;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  // Serialize the {NativeModule} into the provided {buffer}. Returns true on
301cb0ef41Sopenharmony_ci  // success and false if the given buffer it too small for serialization.
311cb0ef41Sopenharmony_ci  bool SerializeNativeModule(base::Vector<byte> buffer) const;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // The data header consists of uint32_t-sized entries (see {WriteVersion}):
341cb0ef41Sopenharmony_ci  // [0] magic number
351cb0ef41Sopenharmony_ci  // [1] version hash
361cb0ef41Sopenharmony_ci  // [2] supported CPU features
371cb0ef41Sopenharmony_ci  // [3] flag hash
381cb0ef41Sopenharmony_ci  // ...  number of functions
391cb0ef41Sopenharmony_ci  // ... serialized functions
401cb0ef41Sopenharmony_ci  static constexpr size_t kMagicNumberOffset = 0;
411cb0ef41Sopenharmony_ci  static constexpr size_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size;
421cb0ef41Sopenharmony_ci  static constexpr size_t kSupportedCPUFeaturesOffset =
431cb0ef41Sopenharmony_ci      kVersionHashOffset + kUInt32Size;
441cb0ef41Sopenharmony_ci  static constexpr size_t kFlagHashOffset =
451cb0ef41Sopenharmony_ci      kSupportedCPUFeaturesOffset + kUInt32Size;
461cb0ef41Sopenharmony_ci  static constexpr size_t kHeaderSize = 4 * kUInt32Size;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci private:
491cb0ef41Sopenharmony_ci  NativeModule* native_module_;
501cb0ef41Sopenharmony_ci  // The {WasmCodeRefScope} keeps the pointers in {code_table_} alive.
511cb0ef41Sopenharmony_ci  WasmCodeRefScope code_ref_scope_;
521cb0ef41Sopenharmony_ci  std::vector<WasmCode*> code_table_;
531cb0ef41Sopenharmony_ci};
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// Support for deserializing WebAssembly {NativeModule} objects.
561cb0ef41Sopenharmony_ci// Checks the version header of the data against the current version.
571cb0ef41Sopenharmony_cibool IsSupportedVersion(base::Vector<const byte> data);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci// Deserializes the given data to create a Wasm module object.
601cb0ef41Sopenharmony_ciV8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> DeserializeNativeModule(
611cb0ef41Sopenharmony_ci    Isolate*, base::Vector<const byte> data,
621cb0ef41Sopenharmony_ci    base::Vector<const byte> wire_bytes, base::Vector<const char> source_url);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci}  // namespace wasm
651cb0ef41Sopenharmony_ci}  // namespace internal
661cb0ef41Sopenharmony_ci}  // namespace v8
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci#endif  // V8_WASM_WASM_SERIALIZATION_H_
69