streaming-decoder.h 10.8 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_WASM_STREAMING_DECODER_H_
#define V8_WASM_STREAMING_DECODER_H_

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 15
#include "src/wasm/wasm-constants.h"
#include "src/wasm/wasm-result.h"
16 17 18 19

namespace v8 {
namespace internal {
namespace wasm {
20
class NativeModule;
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// This class is an interface for the StreamingDecoder to start the processing
// of the incoming module bytes.
class V8_EXPORT_PRIVATE StreamingProcessor {
 public:
  virtual ~StreamingProcessor() = default;
  // Process the first 8 bytes of a WebAssembly module. Returns true if the
  // processing finished successfully and the decoding should continue.
  virtual bool ProcessModuleHeader(Vector<const uint8_t> bytes,
                                   uint32_t offset) = 0;

  // Process all sections but the code section. Returns true if the processing
  // finished successfully and the decoding should continue.
  virtual bool ProcessSection(SectionCode section_code,
                              Vector<const uint8_t> bytes, uint32_t offset) = 0;

  // Process the start of the code section. Returns true if the processing
  // finished successfully and the decoding should continue.
39
  virtual bool ProcessCodeSectionHeader(int num_functions, uint32_t offset,
40 41
                                        std::shared_ptr<WireBytesStorage>,
                                        int code_section_length) = 0;
42 43 44 45 46 47 48 49 50 51 52

  // Process a function body. Returns true if the processing finished
  // successfully and the decoding should continue.
  virtual bool ProcessFunctionBody(Vector<const uint8_t> bytes,
                                   uint32_t offset) = 0;

  // Report the end of a chunk.
  virtual void OnFinishedChunk() = 0;
  // Report the end of the stream. If the stream was successful, all
  // received bytes are passed by parameter. If there has been an error, an
  // empty array is passed.
53
  virtual void OnFinishedStream(OwnedVector<uint8_t> bytes) = 0;
54
  // Report an error detected in the StreamingDecoder.
55
  virtual void OnError(const WasmError&) = 0;
56 57
  // Report the abortion of the stream.
  virtual void OnAbort() = 0;
58 59 60 61

  // Attempt to deserialize the module. Supports embedder caching.
  virtual bool Deserialize(Vector<const uint8_t> module_bytes,
                           Vector<const uint8_t> wire_bytes) = 0;
62 63
};

64 65 66 67 68
// The StreamingDecoder takes a sequence of byte arrays, each received by a call
// of {OnBytesReceived}, and extracts the bytes which belong to section payloads
// and function bodies.
class V8_EXPORT_PRIVATE StreamingDecoder {
 public:
69
  explicit StreamingDecoder(std::unique_ptr<StreamingProcessor> processor);
70 71 72 73

  // The buffer passed into OnBytesReceived is owned by the caller.
  void OnBytesReceived(Vector<const uint8_t> bytes);

74
  void Finish();
75

76 77
  void Abort();

78 79
  // Notify the StreamingDecoder that compilation ended and the
  // StreamingProcessor should not be called anymore.
80
  void NotifyCompilationEnded() { Fail(); }
81

82 83
  // Caching support.
  // Sets the callback that is called after the module is fully compiled.
84 85
  using ModuleCompiledCallback =
      std::function<void(const std::shared_ptr<NativeModule>&)>;
86 87 88
  void SetModuleCompiledCallback(ModuleCompiledCallback callback);
  // Passes previously compiled module bytes from the embedder's cache.
  bool SetCompiledModuleBytes(Vector<const uint8_t> compiled_module_bytes);
89

90 91
  void NotifyNativeModuleCreated(
      const std::shared_ptr<NativeModule>& native_module);
92

93 94 95 96 97
  Vector<const char> url() { return VectorOf(url_); }
  void SetUrl(Vector<const char> url) {
    url_.assign(url.begin(), url.length());
  }

98
 private:
99 100 101
  // TODO(ahaas): Put the whole private state of the StreamingDecoder into the
  // cc file (PIMPL design pattern).

102 103 104
  // The SectionBuffer is the data object for the content of a single section.
  // It stores all bytes of the section (including section id and section
  // length), and the offset where the actual payload starts.
105
  class SectionBuffer : public WireBytesStorage {
106 107 108 109
   public:
    // id: The section id.
    // payload_length: The length of the payload.
    // length_bytes: The section length, as it is encoded in the module bytes.
110
    SectionBuffer(uint32_t module_offset, uint8_t id, size_t payload_length,
111 112
                  Vector<const uint8_t> length_bytes)
        :  // ID + length + payload
113
          module_offset_(module_offset),
114 115
          bytes_(OwnedVector<uint8_t>::New(1 + length_bytes.length() +
                                           payload_length)),
116
          payload_offset_(1 + length_bytes.length()) {
117 118
      bytes_.start()[0] = id;
      memcpy(bytes_.start() + 1, &length_bytes.first(), length_bytes.length());
119
    }
120 121

    SectionCode section_code() const {
122
      return static_cast<SectionCode>(bytes_.start()[0]);
123 124
    }

125 126 127 128 129 130 131
    Vector<const uint8_t> GetCode(WireBytesRef ref) const final {
      DCHECK_LE(module_offset_, ref.offset());
      uint32_t offset_in_code_buffer = ref.offset() - module_offset_;
      return bytes().SubVector(offset_in_code_buffer,
                               offset_in_code_buffer + ref.length());
    }

132
    uint32_t module_offset() const { return module_offset_; }
133 134 135
    Vector<uint8_t> bytes() const { return bytes_.as_vector(); }
    Vector<uint8_t> payload() const { return bytes() + payload_offset_; }
    size_t length() const { return bytes_.size(); }
136 137 138
    size_t payload_offset() const { return payload_offset_; }

   private:
139 140 141
    const uint32_t module_offset_;
    const OwnedVector<uint8_t> bytes_;
    const size_t payload_offset_;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  };

  // The decoding of a stream of wasm module bytes is organized in states. Each
  // state provides a buffer to store the bytes required for the current state,
  // information on how many bytes have already been received, how many bytes
  // are needed, and a {Next} function which starts the next state once all
  // bytes of the current state were received.
  //
  // The states change according to the following state diagram:
  //
  //       Start
  //         |
  //         |
  //         v
  // DecodeModuleHeader
  //         |   _________________________________________
  //         |   |                                        |
  //         v   v                                        |
  //  DecodeSectionID --> DecodeSectionLength --> DecodeSectionPayload
  //         A                  |
  //         |                  | (if the section id == code)
  //         |                  v
  //         |      DecodeNumberOfFunctions -- > DecodeFunctionLength
  //         |                                          A    |
  //         |                                          |    |
  //         |  (after all functions were read)         |    v
  //         ------------------------------------- DecodeFunctionBody
  //
  class DecodingState {
   public:
    virtual ~DecodingState() = default;

    // Reads the bytes for the current state and returns the number of read
    // bytes.
    virtual size_t ReadBytes(StreamingDecoder* streaming,
                             Vector<const uint8_t> bytes);

    // Returns the next state of the streaming decoding.
    virtual std::unique_ptr<DecodingState> Next(
        StreamingDecoder* streaming) = 0;
    // The buffer to store the received bytes.
183
    virtual Vector<uint8_t> buffer() = 0;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    // The number of bytes which were already received.
    size_t offset() const { return offset_; }
    void set_offset(size_t value) { offset_ = value; }
    // A flag to indicate if finishing the streaming decoder is allowed without
    // error.
    virtual bool is_finishing_allowed() const { return false; }

   private:
    size_t offset_ = 0;
  };

  // Forward declarations of the concrete states. This is needed so that they
  // can access private members of the StreamingDecoder.
  class DecodeVarInt32;
  class DecodeModuleHeader;
  class DecodeSectionID;
  class DecodeSectionLength;
  class DecodeSectionPayload;
  class DecodeNumberOfFunctions;
  class DecodeFunctionLength;
  class DecodeFunctionBody;

  // Creates a buffer for the next section of the module.
207
  SectionBuffer* CreateNewBuffer(uint32_t module_offset, uint8_t section_id,
208
                                 size_t length,
209
                                 Vector<const uint8_t> length_bytes);
210

211 212
  std::unique_ptr<DecodingState> Error(const WasmError& error) {
    if (ok()) processor_->OnError(error);
213
    Fail();
214 215 216 217
    return std::unique_ptr<DecodingState>(nullptr);
  }

  std::unique_ptr<DecodingState> Error(std::string message) {
218
    return Error(WasmError{module_offset_ - 1, std::move(message)});
219 220 221
  }

  void ProcessModuleHeader() {
222 223
    if (!ok()) return;
    if (!processor_->ProcessModuleHeader(state_->buffer(), 0)) Fail();
224 225 226
  }

  void ProcessSection(SectionBuffer* buffer) {
227
    if (!ok()) return;
228 229 230 231
    if (!processor_->ProcessSection(
            buffer->section_code(), buffer->payload(),
            buffer->module_offset() +
                static_cast<uint32_t>(buffer->payload_offset()))) {
232
      Fail();
233
    }
234 235
  }

236
  void StartCodeSection(int num_functions,
237 238
                        std::shared_ptr<WireBytesStorage> wire_bytes_storage,
                        int code_section_length) {
239
    if (!ok()) return;
240 241
    // The offset passed to {ProcessCodeSectionHeader} is an error offset and
    // not the start offset of a buffer. Therefore we need the -1 here.
242 243 244
    if (!processor_->ProcessCodeSectionHeader(
            num_functions, module_offset() - 1, std::move(wire_bytes_storage),
            code_section_length)) {
245
      Fail();
246
    }
247 248 249 250
  }

  void ProcessFunctionBody(Vector<const uint8_t> bytes,
                           uint32_t module_offset) {
251 252 253 254 255 256 257 258 259
    if (!ok()) return;
    if (!processor_->ProcessFunctionBody(bytes, module_offset)) Fail();
  }

  void Fail() {
    // We reset the {processor_} field to represent failure. This also ensures
    // that we do not accidentally call further methods on the processor after
    // failure.
    processor_.reset();
260 261
  }

262
  bool ok() const { return processor_ != nullptr; }
263 264

  uint32_t module_offset() const { return module_offset_; }
265

266
  bool deserializing() const { return !compiled_module_bytes_.empty(); }
267

268
  std::unique_ptr<StreamingProcessor> processor_;
269
  std::unique_ptr<DecodingState> state_;
270
  std::vector<std::shared_ptr<SectionBuffer>> section_buffers_;
271
  bool code_section_processed_ = false;
272
  uint32_t module_offset_ = 0;
273
  size_t total_size_ = 0;
274
  std::string url_;
275

276 277 278 279 280 281
  // Caching support.
  ModuleCompiledCallback module_compiled_callback_ = nullptr;
  // We need wire bytes in an array for deserializing cached modules.
  std::vector<uint8_t> wire_bytes_for_deserializing_;
  Vector<const uint8_t> compiled_module_bytes_;

282 283 284 285 286 287 288 289
  DISALLOW_COPY_AND_ASSIGN(StreamingDecoder);
};

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

#endif  // V8_WASM_STREAMING_DECODER_H_