Commit 15a621a2 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm][test] Check streaming decoder error messages

In the wasm streaming decoder error position test, do also check the
error messages generated. This revealed messages that were not quite
fitting and some that were formatted differently than the majority.

R=ahaas@chromium.org

Bug: v8:8814
Change-Id: If157f1083a104413bf14797ac56e756baac98c17
Reviewed-on: https://chromium-review.googlesource.com/c/1463780
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59604}
parent 2fa546d6
......@@ -383,7 +383,7 @@ StreamingDecoder::DecodeSectionLength::NextWithValue(
if (!buf) return nullptr;
if (value_ == 0) {
if (section_id_ == SectionCode::kCodeSectionCode) {
return streaming->Error("Code section cannot have size 0");
return streaming->Error("code section cannot have size 0");
}
streaming->ProcessSection(buf);
if (!streaming->ok()) return nullptr;
......@@ -414,14 +414,14 @@ StreamingDecoder::DecodeNumberOfFunctions::NextWithValue(
// Copy the bytes we read into the section buffer.
Vector<uint8_t> payload_buf = section_buffer_->payload();
if (payload_buf.size() < bytes_consumed_) {
return streaming->Error("Invalid code section length");
return streaming->Error("invalid code section length");
}
memcpy(payload_buf.start(), buffer().start(), bytes_consumed_);
// {value} is the number of functions.
if (value_ == 0) {
if (payload_buf.size() != bytes_consumed_) {
return streaming->Error("not all code section bytes were consumed");
return streaming->Error("not all code section bytes were used");
}
return base::make_unique<DecodeSectionID>(streaming->module_offset());
}
......@@ -440,12 +440,12 @@ StreamingDecoder::DecodeFunctionLength::NextWithValue(
// Copy the bytes we consumed into the section buffer.
Vector<uint8_t> fun_length_buffer = section_buffer_->bytes() + buffer_offset_;
if (fun_length_buffer.size() < bytes_consumed_) {
return streaming->Error("Invalid code section length");
return streaming->Error("read past code section end");
}
memcpy(fun_length_buffer.start(), buffer().start(), bytes_consumed_);
// {value} is the length of the function.
if (value_ == 0) return streaming->Error("Invalid function length (0)");
if (value_ == 0) return streaming->Error("invalid function length (0)");
if (buffer_offset_ + bytes_consumed_ + value_ > section_buffer_->length()) {
return streaming->Error("not enough code section bytes");
......@@ -486,7 +486,7 @@ StreamingDecoder::SectionBuffer* StreamingDecoder::CreateNewBuffer(
// Check the order of sections. Unknown sections can appear at any position.
if (section_id != kUnknownSectionCode) {
if (section_id < next_section_id_) {
Error("unexpected section");
Error("section out of order");
return nullptr;
}
next_section_id_ = section_id + 1;
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --wasm-test-streaming --expose-wasm --allow-natives-syntax
// Flags: --wasm-test-streaming --expose-wasm
'use strict';
......
......@@ -20,9 +20,11 @@ namespace wasm {
struct MockStreamingResult {
size_t num_sections = 0;
size_t num_functions = 0;
bool ok = true;
WasmError error;
OwnedVector<uint8_t> received_bytes;
bool ok() const { return !error.has_error(); }
MockStreamingResult() = default;
};
......@@ -37,12 +39,12 @@ class MockStreamingProcessor : public StreamingProcessor {
Decoder decoder(bytes.begin(), bytes.end());
uint32_t magic_word = decoder.consume_u32("wasm magic");
if (decoder.failed() || magic_word != kWasmMagic) {
result_->ok = false;
result_->error = WasmError(0, "expected wasm magic");
return false;
}
uint32_t magic_version = decoder.consume_u32("wasm version");
if (decoder.failed() || magic_version != kWasmVersion) {
result_->ok = false;
result_->error = WasmError(4, "expected wasm version");
return false;
}
return true;
......@@ -74,7 +76,10 @@ class MockStreamingProcessor : public StreamingProcessor {
}
// Report an error detected in the StreamingDecoder.
void OnError(const WasmError&) override { result_->ok = false; }
void OnError(const WasmError& error) override {
result_->error = error;
CHECK(!result_->ok());
}
void OnAbort() override {}
......@@ -98,14 +103,14 @@ class WasmStreamingDecoderTest : public ::testing::Test {
stream.OnBytesReceived(data.SubVector(0, split));
stream.OnBytesReceived(data.SubVector(split, data.length()));
stream.Finish();
EXPECT_TRUE(result.ok);
EXPECT_TRUE(result.ok());
EXPECT_EQ(expected_sections, result.num_sections);
EXPECT_EQ(expected_functions, result.num_functions);
EXPECT_EQ(data, result.received_bytes.as_vector());
}
}
void ExpectFailure(Vector<const uint8_t> data) {
void ExpectFailure(Vector<const uint8_t> data, const char* message) {
for (int split = 0; split <= data.length(); ++split) {
MockStreamingResult result;
StreamingDecoder stream(
......@@ -113,7 +118,8 @@ class WasmStreamingDecoderTest : public ::testing::Test {
stream.OnBytesReceived(data.SubVector(0, split));
stream.OnBytesReceived(data.SubVector(split, data.length()));
stream.Finish();
EXPECT_FALSE(result.ok);
EXPECT_FALSE(result.ok());
EXPECT_EQ(message, result.error.message());
}
}
......@@ -124,7 +130,7 @@ TEST_F(WasmStreamingDecoderTest, EmptyStream) {
MockStreamingResult result;
StreamingDecoder stream(base::make_unique<MockStreamingProcessor>(&result));
stream.Finish();
EXPECT_FALSE(result.ok);
EXPECT_FALSE(result.ok());
}
TEST_F(WasmStreamingDecoderTest, IncompleteModuleHeader) {
......@@ -134,10 +140,11 @@ TEST_F(WasmStreamingDecoderTest, IncompleteModuleHeader) {
StreamingDecoder stream(base::make_unique<MockStreamingProcessor>(&result));
stream.OnBytesReceived(Vector<const uint8_t>(data, 1));
stream.Finish();
EXPECT_FALSE(result.ok);
EXPECT_FALSE(result.ok());
}
for (int length = 1; length < static_cast<int>(arraysize(data)); ++length) {
ExpectFailure(Vector<const uint8_t>(data, length));
ExpectFailure(Vector<const uint8_t>(data, length),
"unexpected end of stream");
}
}
......@@ -149,14 +156,14 @@ TEST_F(WasmStreamingDecoderTest, MagicAndVersion) {
TEST_F(WasmStreamingDecoderTest, BadMagic) {
for (uint32_t x = 1; x; x <<= 1) {
const uint8_t data[] = {U32_LE(kWasmMagic ^ x), U32_LE(kWasmVersion)};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "expected wasm magic");
}
}
TEST_F(WasmStreamingDecoderTest, BadVersion) {
for (uint32_t x = 1; x; x <<= 1) {
const uint8_t data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion ^ x)};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "expected wasm version");
}
}
......@@ -243,7 +250,7 @@ TEST_F(WasmStreamingDecoderTest, OneSectionNotEnoughPayload1) {
0x0, // 4
0x0 // 5
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "unexpected end of stream");
}
TEST_F(WasmStreamingDecoderTest, OneSectionNotEnoughPayload2) {
......@@ -254,7 +261,7 @@ TEST_F(WasmStreamingDecoderTest, OneSectionNotEnoughPayload2) {
0x6, // Section Length
0x0 // Payload
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "unexpected end of stream");
}
TEST_F(WasmStreamingDecoderTest, OneSectionInvalidLength) {
......@@ -262,13 +269,13 @@ TEST_F(WasmStreamingDecoderTest, OneSectionInvalidLength) {
U32_LE(kWasmMagic), // --
U32_LE(kWasmVersion), // --
0x1, // Section ID
0x80, // Section Length (0 in LEB)
0x80, // Section Length (invalid LEB)
0x80, // --
0x80, // --
0x80, // --
0x80, // --
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "expected section length");
}
TEST_F(WasmStreamingDecoderTest, TwoLongSections) {
......@@ -383,7 +390,7 @@ TEST_F(WasmStreamingDecoderTest, EmptyFunction) {
0x1, // Number of Functions
0x0, // Function Length
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "invalid function length (0)");
}
TEST_F(WasmStreamingDecoderTest, TwoFunctions) {
......@@ -440,7 +447,7 @@ TEST_F(WasmStreamingDecoderTest, CodeSectionLengthZero) {
kCodeSectionCode, // Section ID
0x0, // Section Length
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "code section cannot have size 0");
}
TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooHigh) {
......@@ -461,7 +468,7 @@ TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooHigh) {
0x1, // Function Length
0x0, // Function
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "not all code section bytes were used");
}
TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooHighZeroFunctions) {
......@@ -472,7 +479,7 @@ TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooHighZeroFunctions) {
0xD, // Section Length
0x0, // Number of Functions
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "not all code section bytes were used");
}
TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLow) {
......@@ -493,7 +500,7 @@ TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLow) {
0x1, // Function Length
0x0, // Function
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "read past code section end");
}
TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLowEndsInNumFunctions) {
......@@ -516,7 +523,7 @@ TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLowEndsInNumFunctions) {
0x1, // Function Length
0x0, // Function
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "invalid code section length");
}
TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLowEndsInFunctionLength) {
......@@ -541,7 +548,7 @@ TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLowEndsInFunctionLength) {
0x1, // Function Length
0x0, // Function
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "read past code section end");
}
TEST_F(WasmStreamingDecoderTest, NumberOfFunctionsTooHigh) {
......@@ -562,7 +569,7 @@ TEST_F(WasmStreamingDecoderTest, NumberOfFunctionsTooHigh) {
0x1, // Function Length
0x0, // Function
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "unexpected end of stream");
}
TEST_F(WasmStreamingDecoderTest, NumberOfFunctionsTooLow) {
......@@ -586,7 +593,7 @@ TEST_F(WasmStreamingDecoderTest, NumberOfFunctionsTooLow) {
0x0, // 6
0x0, // 7
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "not all code section bytes were used");
}
TEST_F(WasmStreamingDecoderTest, TwoCodeSections) {
......@@ -604,7 +611,7 @@ TEST_F(WasmStreamingDecoderTest, TwoCodeSections) {
0x1, // Function Length
0x0, // Function
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "section out of order");
}
TEST_F(WasmStreamingDecoderTest, UnknownSection) {
......@@ -645,7 +652,7 @@ TEST_F(WasmStreamingDecoderTest, UnknownSectionSandwich) {
0x1, // Function Length
0x0, // Function
};
ExpectFailure(ArrayVector(data));
ExpectFailure(ArrayVector(data), "section out of order");
}
} // namespace wasm
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment