wasm-serialization.h 2.47 KB
Newer Older
1 2 3 4
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif  // !V8_ENABLE_WEBASSEMBLY

9 10
#ifndef V8_WASM_WASM_SERIALIZATION_H_
#define V8_WASM_WASM_SERIALIZATION_H_
11

12
#include "src/wasm/wasm-code-manager.h"
13 14 15 16 17 18
#include "src/wasm/wasm-objects.h"

namespace v8 {
namespace internal {
namespace wasm {

19 20 21
// Support for serializing WebAssembly {NativeModule} objects. This class takes
// a snapshot of the module state at instantiation, and other code that modifies
// 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 27 28 29 30

  // Measure the required buffer size needed for serialization.
  size_t GetSerializedNativeModuleSize() const;

  // Serialize the {NativeModule} into the provided {buffer}. Returns true on
  // success and false if the given buffer it too small for serialization.
31
  bool SerializeNativeModule(base::Vector<byte> buffer) const;
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  // The data header consists of uint32_t-sized entries (see {WriteVersion}):
  // [0] magic number
  // [1] version hash
  // [2] supported CPU features
  // [3] flag hash
  // ...  number of functions
  // ... serialized functions
  static constexpr size_t kMagicNumberOffset = 0;
  static constexpr size_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size;
  static constexpr size_t kSupportedCPUFeaturesOffset =
      kVersionHashOffset + kUInt32Size;
  static constexpr size_t kFlagHashOffset =
      kSupportedCPUFeaturesOffset + kUInt32Size;
  static constexpr size_t kHeaderSize = 4 * kUInt32Size;

48 49
 private:
  NativeModule* native_module_;
50 51
  // The {WasmCodeRefScope} keeps the pointers in {code_table_} alive.
  WasmCodeRefScope code_ref_scope_;
52 53 54
  std::vector<WasmCode*> code_table_;
};

55 56
// Support for deserializing WebAssembly {NativeModule} objects.
// 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 62
    Isolate*, base::Vector<const byte> data,
    base::Vector<const byte> wire_bytes, base::Vector<const char> source_url);
63 64 65 66 67

}  // namespace wasm
}  // namespace internal
}  // namespace v8

68
#endif  // V8_WASM_WASM_SERIALIZATION_H_