diff --git a/src/wasm/streaming-decoder.cc b/src/wasm/streaming-decoder.cc index 2387edba34..0bdeb1e39d 100644 --- a/src/wasm/streaming-decoder.cc +++ b/src/wasm/streaming-decoder.cc @@ -92,7 +92,9 @@ class StreamingDecoder::DecodeVarInt32 : public DecodingState { public: explicit DecodeVarInt32(size_t max_value, const char* field_name) : max_value_(max_value), field_name_(field_name) {} + uint8_t* buffer() override { return byte_buffer_; } + size_t size() const override { return kMaxVarInt32Size; } size_t ReadBytes(StreamingDecoder* streaming, @@ -103,10 +105,7 @@ class StreamingDecoder::DecodeVarInt32 : public DecodingState { virtual std::unique_ptr NextWithValue( StreamingDecoder* streaming) = 0; - size_t value() const { return value_; } - size_t bytes_consumed() const { return bytes_consumed_; } - - private: + protected: uint8_t byte_buffer_[kMaxVarInt32Size]; // The maximum valid value decoded in this state. {Next} returns an error if // this value is exceeded. @@ -141,10 +140,6 @@ class StreamingDecoder::DecodeSectionID : public DecodingState { uint8_t* buffer() override { return &id_; } bool is_finishing_allowed() const override { return true; } - uint8_t id() const { return id_; } - - uint32_t module_offset() const { return module_offset_; } - std::unique_ptr Next(StreamingDecoder* streaming) override; private: @@ -160,10 +155,6 @@ class StreamingDecoder::DecodeSectionLength : public DecodeVarInt32 { section_id_(id), module_offset_(module_offset) {} - uint8_t section_id() const { return section_id_; } - - uint32_t module_offset() const { return module_offset_; } - std::unique_ptr NextWithValue( StreamingDecoder* streaming) override; @@ -179,14 +170,13 @@ class StreamingDecoder::DecodeSectionPayload : public DecodingState { : 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 Next(StreamingDecoder* streaming) override; - SectionBuffer* section_buffer() const { return section_buffer_; } - private: SectionBuffer* section_buffer_; }; @@ -197,8 +187,6 @@ class StreamingDecoder::DecodeNumberOfFunctions : public DecodeVarInt32 { : DecodeVarInt32(kV8MaxWasmFunctions, "functions count"), section_buffer_(section_buffer) {} - SectionBuffer* section_buffer() const { return section_buffer_; } - std::unique_ptr NextWithValue( StreamingDecoder* streaming) override; @@ -219,10 +207,6 @@ class StreamingDecoder::DecodeFunctionLength : public DecodeVarInt32 { 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 NextWithValue( StreamingDecoder* streaming) override; @@ -244,14 +228,11 @@ class StreamingDecoder::DecodeFunctionBody : public DecodingState { num_remaining_functions_(num_remaining_functions), module_offset_(module_offset) {} - size_t buffer_offset() const { return buffer_offset_; } 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_; } - uint32_t module_offset() const { return module_offset_; } - SectionBuffer* section_buffer() const { return section_buffer_; } std::unique_ptr Next(StreamingDecoder* streaming) override; @@ -297,9 +278,9 @@ StreamingDecoder::DecodeVarInt32::Next(StreamingDecoder* streaming) { if (!streaming->ok()) { return nullptr; } - if (value() > max_value_) { + if (value_ > max_value_) { std::ostringstream oss; - oss << "function size > maximum function size: " << value() << " < " + oss << "function size > maximum function size: " << value_ << " < " << max_value_; return streaming->Error(oss.str()); } @@ -320,32 +301,32 @@ StreamingDecoder::DecodeModuleHeader::Next(StreamingDecoder* streaming) { std::unique_ptr StreamingDecoder::DecodeSectionID::Next(StreamingDecoder* streaming) { TRACE_STREAMING("DecodeSectionID: %s section\n", - SectionName(static_cast(id()))); - return base::make_unique(id(), module_offset()); + SectionName(static_cast(id_))); + return base::make_unique(id_, module_offset_); } std::unique_ptr StreamingDecoder::DecodeSectionLength::NextWithValue( StreamingDecoder* streaming) { - TRACE_STREAMING("DecodeSectionLength(%zu)\n", value()); + TRACE_STREAMING("DecodeSectionLength(%zu)\n", value_); SectionBuffer* buf = streaming->CreateNewBuffer( - module_offset(), section_id(), value(), - Vector(buffer(), static_cast(bytes_consumed()))); + module_offset_, section_id_, value_, + Vector(buffer(), static_cast(bytes_consumed_))); if (!buf) return nullptr; - if (value() == 0) { - if (section_id() == SectionCode::kCodeSectionCode) { + if (value_ == 0) { + if (section_id_ == SectionCode::kCodeSectionCode) { return streaming->Error("Code section cannot have size 0"); } else { streaming->ProcessSection(buf); if (streaming->ok()) { // There is no payload, we go to the next section immediately. - return base::make_unique(streaming->module_offset()); + return base::make_unique(streaming->module_offset_); } else { return nullptr; } } } else { - if (section_id() == SectionCode::kCodeSectionCode) { + if (section_id_ == SectionCode::kCodeSectionCode) { // We reached the code section. All functions of the code section are put // into the same SectionBuffer. return base::make_unique(buf); @@ -358,7 +339,7 @@ StreamingDecoder::DecodeSectionLength::NextWithValue( std::unique_ptr StreamingDecoder::DecodeSectionPayload::Next(StreamingDecoder* streaming) { TRACE_STREAMING("DecodeSectionPayload\n"); - streaming->ProcessSection(section_buffer()); + streaming->ProcessSection(section_buffer_); if (streaming->ok()) { return base::make_unique(streaming->module_offset()); } @@ -368,24 +349,24 @@ StreamingDecoder::DecodeSectionPayload::Next(StreamingDecoder* streaming) { std::unique_ptr StreamingDecoder::DecodeNumberOfFunctions::NextWithValue( StreamingDecoder* streaming) { - TRACE_STREAMING("DecodeNumberOfFunctions(%zu)\n", value()); + TRACE_STREAMING("DecodeNumberOfFunctions(%zu)\n", value_); // Copy the bytes we read into the section buffer. - if (section_buffer()->payload_length() >= bytes_consumed()) { - memcpy(section_buffer()->bytes() + section_buffer()->payload_offset(), - buffer(), bytes_consumed()); + 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"); } // {value} is the number of functions. - if (value() > 0) { - streaming->StartCodeSection(value()); + if (value_ > 0) { + streaming->StartCodeSection(value_); if (!streaming->ok()) return nullptr; return base::make_unique( - section_buffer(), section_buffer()->payload_offset() + bytes_consumed(), - value()); + section_buffer_, section_buffer_->payload_offset() + bytes_consumed_, + value_); } else { - if (section_buffer()->payload_length() != bytes_consumed()) { + if (section_buffer_->payload_length() != bytes_consumed_) { return streaming->Error("not all code section bytes were consumed"); } return base::make_unique(streaming->module_offset()); @@ -395,27 +376,27 @@ StreamingDecoder::DecodeNumberOfFunctions::NextWithValue( std::unique_ptr StreamingDecoder::DecodeFunctionLength::NextWithValue( StreamingDecoder* streaming) { - TRACE_STREAMING("DecodeFunctionLength(%zu)\n", value()); + TRACE_STREAMING("DecodeFunctionLength(%zu)\n", value_); // Copy the bytes we consumed into the section buffer. - if (section_buffer_->length() >= buffer_offset_ + bytes_consumed()) { + if (section_buffer_->length() >= buffer_offset_ + bytes_consumed_) { memcpy(section_buffer_->bytes() + buffer_offset_, buffer(), - bytes_consumed()); + bytes_consumed_); } else { return streaming->Error("Invalid code section length"); } // {value} is the length of the function. - if (value() == 0) { + if (value_ == 0) { return streaming->Error("Invalid function length (0)"); - } else if (buffer_offset() + bytes_consumed() + value() > - section_buffer()->length()) { + } else if (buffer_offset_ + bytes_consumed_ + value_ > + section_buffer_->length()) { streaming->Error("not enough code section bytes"); return nullptr; } return base::make_unique( - section_buffer(), buffer_offset() + bytes_consumed(), value(), - num_remaining_functions(), streaming->module_offset()); + section_buffer_, buffer_offset_ + bytes_consumed_, value_, + num_remaining_functions_, streaming->module_offset()); } std::unique_ptr @@ -423,15 +404,15 @@ StreamingDecoder::DecodeFunctionBody::Next(StreamingDecoder* streaming) { TRACE_STREAMING("DecodeFunctionBody\n"); streaming->ProcessFunctionBody( Vector(buffer(), static_cast(size())), - module_offset()); + module_offset_); if (!streaming->ok()) { return nullptr; } - if (num_remaining_functions() != 0) { + if (num_remaining_functions_ != 0) { return base::make_unique( - section_buffer(), buffer_offset() + size(), num_remaining_functions()); + section_buffer_, buffer_offset_ + size(), num_remaining_functions_); } else { - if (buffer_offset() + size() != section_buffer()->length()) { + if (buffer_offset_ + size() != section_buffer_->length()) { return streaming->Error("not all code section bytes were used"); } return base::make_unique(streaming->module_offset());