diff options
Diffstat (limited to 'deps/v8/src/wasm/streaming-decoder.cc')
-rw-r--r-- | deps/v8/src/wasm/streaming-decoder.cc | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/deps/v8/src/wasm/streaming-decoder.cc b/deps/v8/src/wasm/streaming-decoder.cc index b48d11c902..2387edba34 100644 --- a/deps/v8/src/wasm/streaming-decoder.cc +++ b/deps/v8/src/wasm/streaming-decoder.cc @@ -15,11 +15,17 @@ #include "src/wasm/wasm-objects.h" #include "src/wasm/wasm-result.h" +#define TRACE_STREAMING(...) \ + do { \ + if (FLAG_trace_wasm_streaming) PrintF(__VA_ARGS__); \ + } while (false) + namespace v8 { namespace internal { namespace wasm { void StreamingDecoder::OnBytesReceived(Vector<const uint8_t> bytes) { + TRACE_STREAMING("OnBytesReceived(%zu bytes)\n", bytes.size()); size_t current = 0; while (ok() && current < bytes.size()) { size_t num_bytes = @@ -39,12 +45,14 @@ void StreamingDecoder::OnBytesReceived(Vector<const uint8_t> bytes) { size_t StreamingDecoder::DecodingState::ReadBytes(StreamingDecoder* streaming, Vector<const uint8_t> bytes) { size_t num_bytes = std::min(bytes.size(), remaining()); + TRACE_STREAMING("ReadBytes(%zu bytes)\n", num_bytes); memcpy(buffer() + offset(), &bytes.first(), num_bytes); set_offset(offset() + num_bytes); return num_bytes; } void StreamingDecoder::Finish() { + TRACE_STREAMING("Finish\n"); if (!ok()) { return; } @@ -73,6 +81,7 @@ void StreamingDecoder::Finish() { } void StreamingDecoder::Abort() { + TRACE_STREAMING("Abort\n"); if (ok()) processor_->OnAbort(); } @@ -95,7 +104,7 @@ class StreamingDecoder::DecodeVarInt32 : public DecodingState { StreamingDecoder* streaming) = 0; size_t value() const { return value_; } - size_t bytes_needed() const { return bytes_needed_; } + size_t bytes_consumed() const { return bytes_consumed_; } private: uint8_t byte_buffer_[kMaxVarInt32Size]; @@ -104,7 +113,7 @@ class StreamingDecoder::DecodeVarInt32 : public DecodingState { size_t max_value_; const char* field_name_; size_t value_ = 0; - size_t bytes_needed_ = 0; + size_t bytes_consumed_ = 0; }; class StreamingDecoder::DecodeModuleHeader : public DecodingState { @@ -257,13 +266,15 @@ class StreamingDecoder::DecodeFunctionBody : public DecodingState { size_t StreamingDecoder::DecodeVarInt32::ReadBytes( StreamingDecoder* streaming, Vector<const uint8_t> bytes) { size_t bytes_read = std::min(bytes.size(), remaining()); + TRACE_STREAMING("ReadBytes of a VarInt\n"); memcpy(buffer() + offset(), &bytes.first(), bytes_read); Decoder decoder(buffer(), buffer() + offset() + bytes_read, streaming->module_offset()); value_ = decoder.consume_u32v(field_name_); // The number of bytes we actually needed to read. DCHECK_GT(decoder.pc(), buffer()); - bytes_needed_ = static_cast<size_t>(decoder.pc() - buffer()); + bytes_consumed_ = static_cast<size_t>(decoder.pc() - buffer()); + TRACE_STREAMING(" ==> %zu bytes consumed\n", bytes_consumed_); if (decoder.failed()) { if (offset() + bytes_read == size()) { @@ -273,8 +284,8 @@ size_t StreamingDecoder::DecodeVarInt32::ReadBytes( set_offset(offset() + bytes_read); return bytes_read; } else { - DCHECK_GT(bytes_needed_, offset()); - size_t result = bytes_needed_ - offset(); + DCHECK_GT(bytes_consumed_, offset()); + size_t result = bytes_consumed_ - offset(); // We read all the bytes we needed. set_offset(size()); return result; @@ -298,6 +309,7 @@ StreamingDecoder::DecodeVarInt32::Next(StreamingDecoder* streaming) { std::unique_ptr<StreamingDecoder::DecodingState> StreamingDecoder::DecodeModuleHeader::Next(StreamingDecoder* streaming) { + TRACE_STREAMING("DecodeModuleHeader\n"); streaming->ProcessModuleHeader(); if (streaming->ok()) { return base::make_unique<DecodeSectionID>(streaming->module_offset()); @@ -307,15 +319,18 @@ StreamingDecoder::DecodeModuleHeader::Next(StreamingDecoder* streaming) { std::unique_ptr<StreamingDecoder::DecodingState> StreamingDecoder::DecodeSectionID::Next(StreamingDecoder* streaming) { + TRACE_STREAMING("DecodeSectionID: %s section\n", + SectionName(static_cast<SectionCode>(id()))); return base::make_unique<DecodeSectionLength>(id(), module_offset()); } std::unique_ptr<StreamingDecoder::DecodingState> StreamingDecoder::DecodeSectionLength::NextWithValue( StreamingDecoder* streaming) { + TRACE_STREAMING("DecodeSectionLength(%zu)\n", value()); SectionBuffer* buf = streaming->CreateNewBuffer( module_offset(), section_id(), value(), - Vector<const uint8_t>(buffer(), static_cast<int>(bytes_needed()))); + Vector<const uint8_t>(buffer(), static_cast<int>(bytes_consumed()))); if (!buf) return nullptr; if (value() == 0) { if (section_id() == SectionCode::kCodeSectionCode) { @@ -342,6 +357,7 @@ StreamingDecoder::DecodeSectionLength::NextWithValue( std::unique_ptr<StreamingDecoder::DecodingState> StreamingDecoder::DecodeSectionPayload::Next(StreamingDecoder* streaming) { + TRACE_STREAMING("DecodeSectionPayload\n"); streaming->ProcessSection(section_buffer()); if (streaming->ok()) { return base::make_unique<DecodeSectionID>(streaming->module_offset()); @@ -352,10 +368,11 @@ StreamingDecoder::DecodeSectionPayload::Next(StreamingDecoder* streaming) { std::unique_ptr<StreamingDecoder::DecodingState> StreamingDecoder::DecodeNumberOfFunctions::NextWithValue( StreamingDecoder* streaming) { + TRACE_STREAMING("DecodeNumberOfFunctions(%zu)\n", value()); // 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()); + if (section_buffer()->payload_length() >= bytes_consumed()) { + memcpy(section_buffer()->bytes() + section_buffer()->payload_offset(), + buffer(), bytes_consumed()); } else { return streaming->Error("Invalid code section length"); } @@ -365,9 +382,12 @@ StreamingDecoder::DecodeNumberOfFunctions::NextWithValue( streaming->StartCodeSection(value()); if (!streaming->ok()) return nullptr; return base::make_unique<DecodeFunctionLength>( - section_buffer(), section_buffer()->payload_offset() + bytes_needed(), + section_buffer(), section_buffer()->payload_offset() + bytes_consumed(), value()); } else { + if (section_buffer()->payload_length() != bytes_consumed()) { + return streaming->Error("not all code section bytes were consumed"); + } return base::make_unique<DecodeSectionID>(streaming->module_offset()); } } @@ -375,9 +395,11 @@ StreamingDecoder::DecodeNumberOfFunctions::NextWithValue( 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()); + TRACE_STREAMING("DecodeFunctionLength(%zu)\n", value()); + // Copy the bytes we consumed into the section buffer. + if (section_buffer_->length() >= buffer_offset_ + bytes_consumed()) { + memcpy(section_buffer_->bytes() + buffer_offset_, buffer(), + bytes_consumed()); } else { return streaming->Error("Invalid code section length"); } @@ -385,19 +407,20 @@ StreamingDecoder::DecodeFunctionLength::NextWithValue( // {value} is the length of the function. if (value() == 0) { return streaming->Error("Invalid function length (0)"); - } else if (buffer_offset() + bytes_needed() + value() > + } else if (buffer_offset() + bytes_consumed() + value() > section_buffer()->length()) { streaming->Error("not enough code section bytes"); return nullptr; } return base::make_unique<DecodeFunctionBody>( - section_buffer(), buffer_offset() + bytes_needed(), value(), + section_buffer(), buffer_offset() + bytes_consumed(), value(), num_remaining_functions(), streaming->module_offset()); } std::unique_ptr<StreamingDecoder::DecodingState> StreamingDecoder::DecodeFunctionBody::Next(StreamingDecoder* streaming) { + TRACE_STREAMING("DecodeFunctionBody\n"); streaming->ProcessFunctionBody( Vector<const uint8_t>(buffer(), static_cast<int>(size())), module_offset()); @@ -423,3 +446,5 @@ StreamingDecoder::StreamingDecoder( } // namespace wasm } // namespace internal } // namespace v8 + +#undef TRACE_STREAMING |