[cleanup][wasm] Use local variables more consistently in the streaming decoder

R=clemensh@chromium.org

Bug: v8:7109
Change-Id: I8836663f4c66fd0f8c50f2cb66ab4cb8e2712f6c
Reviewed-on: https://chromium-review.googlesource.com/803274
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49782}
This commit is contained in:
Andreas Haas 2017-12-01 11:20:46 +01:00 committed by Commit Bot
parent a68831281a
commit 01593d381d

View File

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