summaryrefslogtreecommitdiff
path: root/deps/v8/src/wasm/streaming-decoder.cc
blob: 2772cd5945791d6ee726d813ac1474366b2c1b1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// 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.

#include "src/wasm/streaming-decoder.h"

#include "src/objects-inl.h"

#include "src/handles.h"
#include "src/wasm/decoder.h"
#include "src/wasm/leb-helper.h"
#include "src/wasm/module-decoder.h"
#include "src/wasm/wasm-objects.h"
#include "src/wasm/wasm-result.h"

#include "src/objects/descriptor-array.h"
#include "src/objects/dictionary.h"

using namespace v8::internal;
using namespace v8::internal::wasm;

void StreamingDecoder::OnBytesReceived(Vector<const uint8_t> bytes) {
  size_t current = 0;
  while (decoder()->ok() && current < bytes.size()) {
    size_t num_bytes =
        state_->ReadBytes(this, bytes.SubVector(current, bytes.size()));
    current += num_bytes;
    if (state_->is_finished()) {
      state_ = state_->Next(this);
    }
  }
  total_size_ += bytes.size();
}

size_t StreamingDecoder::DecodingState::ReadBytes(StreamingDecoder* streaming,
                                                  Vector<const uint8_t> bytes) {
  size_t num_bytes = std::min(bytes.size(), remaining());
  memcpy(buffer() + offset(), &bytes.first(), num_bytes);
  set_offset(offset() + num_bytes);
  return num_bytes;
}

MaybeHandle<WasmModuleObject> StreamingDecoder::Finish() {
  UNIMPLEMENTED();
  return Handle<WasmModuleObject>::null();
}

bool StreamingDecoder::FinishForTesting() {
  return decoder_.ok() && state_->is_finishing_allowed();
}

// An abstract class to share code among the states which decode VarInts. This
// class takes over the decoding of the VarInt and then calls the actual decode
// code with the decoded value.
class StreamingDecoder::DecodeVarInt32 : public DecodingState {
 public:
  explicit DecodeVarInt32(size_t max_value) : max_value_(max_value) {}
  uint8_t* buffer() override { return byte_buffer_; }
  size_t size() const override { return kMaxVarInt32Size; }

  size_t ReadBytes(StreamingDecoder* streaming,
                   Vector<const uint8_t> bytes) override;

  std::unique_ptr<DecodingState> Next(StreamingDecoder* streaming) override;

  virtual std::unique_ptr<DecodingState> NextWithValue(
      StreamingDecoder* streaming) = 0;

  size_t value() const { return value_; }
  size_t bytes_needed() const { return bytes_needed_; }

 private:
  uint8_t byte_buffer_[kMaxVarInt32Size];
  // The maximum valid value decoded in this state. {Next} returns an error if
  // this value is exceeded.
  size_t max_value_;
  size_t value_ = 0;
  size_t bytes_needed_ = 0;
};

class StreamingDecoder::DecodeModuleHeader : public DecodingState {
 public:
  size_t size() const override { return kModuleHeaderSize; }
  uint8_t* buffer() override { return byte_buffer_; }

  std::unique_ptr<DecodingState> Next(StreamingDecoder* streaming) override;

 private:
  // Checks if the magic bytes of the module header are correct.
  void CheckHeader(Decoder* decoder);

  // The size of the module header.
  static constexpr size_t kModuleHeaderSize = 8;
  uint8_t byte_buffer_[kModuleHeaderSize];
};

class StreamingDecoder::DecodeSectionID : public DecodingState {
 public:
  size_t size() const override { return 1; }
  uint8_t* buffer() override { return &id_; }
  bool is_finishing_allowed() const override { return true; }

  uint8_t id() const { return id_; }

  std::unique_ptr<DecodingState> Next(StreamingDecoder* streaming) override;

 private:
  uint8_t id_ = 0;
};

class StreamingDecoder::DecodeSectionLength : public DecodeVarInt32 {
 public:
  explicit DecodeSectionLength(uint8_t id)
      : DecodeVarInt32(kV8MaxWasmModuleSize), section_id_(id) {}

  uint8_t section_id() const { return section_id_; }

  std::unique_ptr<DecodingState> NextWithValue(
      StreamingDecoder* streaming) override;

 private:
  uint8_t section_id_;
};

class StreamingDecoder::DecodeSectionPayload : public DecodingState {
 public:
  explicit DecodeSectionPayload(SectionBuffer* section_buffer)
      : section_buffer_(section_buffer) {}

  size_t size() const override { return section_buffer_->payload_length(); }
  uint8_t* buffer() override {
    return section_buffer_->bytes() + section_buffer_->payload_offset();
  }

  std::unique_ptr<DecodingState> Next(StreamingDecoder* streaming) override;

 private:
  SectionBuffer* section_buffer_;
};

class StreamingDecoder::DecodeNumberOfFunctions : public DecodeVarInt32 {
 public:
  explicit DecodeNumberOfFunctions(SectionBuffer* section_buffer)
      : DecodeVarInt32(kV8MaxWasmFunctions), section_buffer_(section_buffer) {}

  SectionBuffer* section_buffer() const { return section_buffer_; }

  std::unique_ptr<DecodingState> NextWithValue(
      StreamingDecoder* streaming) override;

 private:
  SectionBuffer* section_buffer_;
};

class StreamingDecoder::DecodeFunctionLength : public DecodeVarInt32 {
 public:
  explicit DecodeFunctionLength(SectionBuffer* section_buffer,
                                size_t buffer_offset,
                                size_t num_remaining_functions)
      : DecodeVarInt32(kV8MaxWasmFunctionSize),
        section_buffer_(section_buffer),
        buffer_offset_(buffer_offset),
        // We are reading a new function, so one function less is remaining.
        num_remaining_functions_(num_remaining_functions - 1) {
    DCHECK_GT(num_remaining_functions, 0);
  }

  size_t num_remaining_functions() const { return num_remaining_functions_; }
  size_t buffer_offset() const { return buffer_offset_; }
  SectionBuffer* section_buffer() const { return section_buffer_; }

  std::unique_ptr<DecodingState> NextWithValue(
      StreamingDecoder* streaming) override;

 private:
  SectionBuffer* section_buffer_;
  size_t buffer_offset_;
  size_t num_remaining_functions_;
};

class StreamingDecoder::DecodeFunctionBody : public DecodingState {
 public:
  explicit DecodeFunctionBody(SectionBuffer* section_buffer,
                              size_t buffer_offset, size_t function_length,
                              size_t num_remaining_functions)
      : section_buffer_(section_buffer),
        buffer_offset_(buffer_offset),
        size_(function_length),
        num_remaining_functions_(num_remaining_functions) {}

  size_t size() const override { return size_; }
  uint8_t* buffer() override {
    return section_buffer_->bytes() + buffer_offset_;
  }
  size_t num_remaining_functions() const { return num_remaining_functions_; }
  size_t buffer_offset() const { return buffer_offset_; }
  SectionBuffer* section_buffer() const { return section_buffer_; }

  std::unique_ptr<DecodingState> Next(StreamingDecoder* streaming) override;

 private:
  SectionBuffer* section_buffer_;
  size_t buffer_offset_;
  size_t size_;
  size_t num_remaining_functions_;
};

size_t StreamingDecoder::DecodeVarInt32::ReadBytes(
    StreamingDecoder* streaming, Vector<const uint8_t> bytes) {
  size_t bytes_read = std::min(bytes.size(), remaining());
  memcpy(buffer() + offset(), &bytes.first(), bytes_read);
  streaming->decoder()->Reset(buffer(), buffer() + offset() + bytes_read);
  value_ = streaming->decoder()->consume_i32v();
  // The number of bytes we actually needed to read.
  DCHECK_GT(streaming->decoder()->pc(), buffer());
  bytes_needed_ = static_cast<size_t>(streaming->decoder()->pc() - buffer());

  if (streaming->decoder()->failed()) {
    if (offset() + bytes_read < size()) {
      // We did not decode a full buffer, so we ignore errors. Maybe the
      // decoding will succeed when we have more bytes.
      streaming->decoder()->Reset(nullptr, nullptr);
    }
    set_offset(offset() + bytes_read);
    return bytes_read;
  } else {
    DCHECK_GT(bytes_needed_, offset());
    size_t result = bytes_needed_ - offset();
    // We read all the bytes we needed.
    set_offset(size());
    return result;
  }
}

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeVarInt32::Next(StreamingDecoder* streaming) {
  if (streaming->decoder()->failed()) {
    return std::unique_ptr<DecodingState>(nullptr);
  }
  if (value() > max_value_) {
    streaming->decoder()->errorf(buffer(), "size > maximum function size: %zu",
                                 value());
    return std::unique_ptr<DecodingState>(nullptr);
  }

  return NextWithValue(streaming);
}

#define BYTES(x) (x & 0xff), (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff
// Decode the module header. The error state of the decoder stores the result.
void StreamingDecoder::DecodeModuleHeader::CheckHeader(Decoder* decoder) {
  // TODO(ahaas): Share code with the module-decoder.
  decoder->Reset(buffer(), buffer() + size());
  uint32_t magic_word = decoder->consume_u32("wasm magic");
  if (magic_word != kWasmMagic) {
    decoder->errorf(buffer(),
                    "expected magic word %02x %02x %02x %02x, "
                    "found %02x %02x %02x %02x",
                    BYTES(kWasmMagic), BYTES(magic_word));
  }
  uint32_t magic_version = decoder->consume_u32("wasm version");
  if (magic_version != kWasmVersion) {
    decoder->errorf(buffer(),
                    "expected version %02x %02x %02x %02x, "
                    "found %02x %02x %02x %02x",
                    BYTES(kWasmVersion), BYTES(magic_version));
  }
}
#undef BYTES

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeModuleHeader::Next(StreamingDecoder* streaming) {
  CheckHeader(streaming->decoder());
  return std::unique_ptr<DecodingState>(new DecodeSectionID());
}

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeSectionID::Next(StreamingDecoder* streaming) {
  return std::unique_ptr<DecodingState>(new DecodeSectionLength(id()));
}

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeSectionLength::NextWithValue(
    StreamingDecoder* streaming) {
  SectionBuffer* buf = streaming->CreateNewBuffer(
      section_id(), value(),
      Vector<const uint8_t>(buffer(), static_cast<int>(bytes_needed())));
  if (value() == 0) {
    // There is no payload, we go to the next section immediately.
    return std::unique_ptr<DecodingState>(new DecodeSectionID());
  } else if (section_id() == SectionCode::kCodeSectionCode) {
    // We reached the code section. All functions of the code section are put
    // into the same SectionBuffer.
    return std::unique_ptr<DecodingState>(new DecodeNumberOfFunctions(buf));
  } else {
    return std::unique_ptr<DecodingState>(new DecodeSectionPayload(buf));
  }
}

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeSectionPayload::Next(StreamingDecoder* streaming) {
  return std::unique_ptr<DecodingState>(new DecodeSectionID());
}

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeNumberOfFunctions::NextWithValue(
    StreamingDecoder* streaming) {
  // Copy the bytes we read into the section buffer.
  if (section_buffer_->payload_length() >= bytes_needed()) {
    memcpy(section_buffer_->bytes() + section_buffer_->payload_offset(),
           buffer(), bytes_needed());
  } else {
    streaming->decoder()->error("Invalid code section length");
    return std::unique_ptr<DecodingState>(new DecodeSectionID());
  }

  // {value} is the number of functions.
  if (value() > 0) {
    return std::unique_ptr<DecodingState>(new DecodeFunctionLength(
        section_buffer(), section_buffer()->payload_offset() + bytes_needed(),
        value()));
  } else {
    return std::unique_ptr<DecodingState>(new DecodeSectionID());
  }
}

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeFunctionLength::NextWithValue(
    StreamingDecoder* streaming) {
  // Copy the bytes we read into the section buffer.
  if (section_buffer_->length() >= buffer_offset_ + bytes_needed()) {
    memcpy(section_buffer_->bytes() + buffer_offset_, buffer(), bytes_needed());
  } else {
    streaming->decoder()->error("Invalid code section length");
    return std::unique_ptr<DecodingState>(new DecodeSectionID());
  }

  // {value} is the length of the function.
  if (value() == 0) {
    streaming->decoder()->errorf(buffer(), "Invalid function length (0)");
    return std::unique_ptr<DecodingState>(nullptr);
  } else if (buffer_offset() + bytes_needed() + value() >
             section_buffer()->length()) {
    streaming->decoder()->errorf(buffer(), "not enough code section bytes");
    return std::unique_ptr<DecodingState>(nullptr);
  }

  return std::unique_ptr<DecodingState>(
      new DecodeFunctionBody(section_buffer(), buffer_offset() + bytes_needed(),
                             value(), num_remaining_functions()));
}

std::unique_ptr<StreamingDecoder::DecodingState>
StreamingDecoder::DecodeFunctionBody::Next(StreamingDecoder* streaming) {
  // TODO(ahaas): Start compilation of the function here.
  if (num_remaining_functions() != 0) {
    return std::unique_ptr<DecodingState>(new DecodeFunctionLength(
        section_buffer(), buffer_offset() + size(), num_remaining_functions()));
  } else {
    if (buffer_offset() + size() != section_buffer()->length()) {
      streaming->decoder()->Reset(
          section_buffer()->bytes(),
          section_buffer()->bytes() + section_buffer()->length());
      streaming->decoder()->errorf(
          section_buffer()->bytes() + buffer_offset() + size(),
          "not all code section bytes were used");
      return std::unique_ptr<DecodingState>(nullptr);
    }
    return std::unique_ptr<DecodingState>(new DecodeSectionID());
  }
}

StreamingDecoder::StreamingDecoder(Isolate* isolate)
    : isolate_(isolate),
      // A module always starts with a module header.
      state_(new DecodeModuleHeader()),
      decoder_(nullptr, nullptr) {
  USE(isolate_);
}