Commit a4d295ad authored by Manos Koukoutos's avatar Manos Koukoutos Committed by Commit Bot

[wasm] Refactor/cleanup DecodeLocals, read_value_type

Changes:
Cleanup:
- Make sure read_value_type has the same interface as other
  read_* functions, i.e., returns the decoded value and writes
  the consumed length into a pointer.
- DecodeLocals is now an instance method.
- DecodeLocals should fail when given a wrong number of locals.
  Add tests to catch that.
- Fix a buggy test.

Refactoring in preparation of introducing the 'let'
instruction as per [wasm-gc]:
- DecodeLocals does not consume any input and can start from any pc.
- DecodeLocals gives the option of not appending the decoded
  locals to local_types_.
- Separate locals initialization from signature.

Bug: v8:7748
Change-Id: Iaaff87fdb9abe0ddd716484ea3fa87779d2d1a2f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2202992
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67845}
parent ed7f102e
This diff is collapsed.
......@@ -21,10 +21,15 @@ namespace wasm {
bool DecodeLocalDecls(const WasmFeatures& enabled, BodyLocalDecls* decls,
const byte* start, const byte* end) {
Decoder decoder(start, end);
if (WasmDecoder<Decoder::kValidate>::DecodeLocals(enabled, &decoder, nullptr,
&decls->type_list)) {
WasmFeatures no_features = WasmFeatures::None();
WasmDecoder<Decoder::kValidate> decoder(nullptr, enabled, &no_features,
nullptr, start, end, 0);
decoder.local_types_ = &decls->type_list;
uint32_t length;
if (decoder.DecodeLocals(decoder.pc(), &length,
decoder.DecodeLocalsMode::kUpdateLocals)) {
DCHECK(decoder.ok());
decoder.consume_bytes(length);
decls->encoded_size = decoder.pc_offset();
return true;
}
......
......@@ -1723,11 +1723,11 @@ class ModuleDecoderImpl : public Decoder {
}
ValueType consume_value_type() {
ValueType result;
uint32_t type_length = value_type_reader::read_value_type<kValidate>(
this, this->pc(), &result,
uint32_t type_length;
ValueType result = value_type_reader::read_value_type<kValidate>(
this, this->pc(), &type_length,
origin_ == kWasmOrigin ? enabled_features_ : WasmFeatures::None());
if (type_length == 0) error(pc_, "invalid value type");
if (result == kWasmBottom) error(pc_, "invalid value type");
consume_bytes(type_length);
return result;
}
......
......@@ -1174,9 +1174,10 @@ STREAM_TEST(TestCompileErrorFunctionName) {
U32V_1(1), // functions count
0, // signature index
kCodeSectionCode, // section code
U32V_1(3), // section size
U32V_1(4), // section size
U32V_1(1), // functions count
1, // body size
2, // body size
0, // local definitions count
kExprNop, // body
};
......@@ -1210,7 +1211,7 @@ STREAM_TEST(TestCompileErrorFunctionName) {
CHECK(tester.IsPromiseRejected());
CHECK_EQ(
"CompileError: WebAssembly.compileStreaming(): Compiling function "
"#0:\"f\" failed: function body must end with \"end\" opcode @+25",
"#0:\"f\" failed: function body must end with \"end\" opcode @+26",
tester.error_message());
}
}
......
......@@ -3820,6 +3820,21 @@ TEST_F(LocalDeclDecoderTest, NoLocals) {
EXPECT_TRUE(decls.type_list.empty());
}
TEST_F(LocalDeclDecoderTest, WrongLocalDeclsCount1) {
static const byte data[] = {1};
BodyLocalDecls decls(zone());
bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
EXPECT_FALSE(result);
}
TEST_F(LocalDeclDecoderTest, WrongLocalDeclsCount2) {
static const byte data[] = {2, 1,
static_cast<byte>(kWasmI32.value_type_code())};
BodyLocalDecls decls(zone());
bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
EXPECT_FALSE(result);
}
TEST_F(LocalDeclDecoderTest, OneLocal) {
WASM_FEATURE_SCOPE(anyref);
for (size_t i = 0; i < arraysize(kValueTypes); i++) {
......
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