Commit 55ddbaa0 authored by Manos Koukoutos's avatar Manos Koukoutos Committed by Commit Bot

[wasm][refactor] Rework immediate-argument abstractions

Motivation:
The immediate-argument classes defined in function-body-decoder.h were
often adding an offset to the provided pc. This was inconsistent,
bug-prone, and counterintuitive. This CL imposes that all immediates
are passed as pc the start of the immediate argument they are parsing.
Some other smaller inconsistencies are fixed as well.

Changes:

src/wasm/:
- Enforce that all Immediates are passed the pc at the start of the
  argument they are parsing. Adapt all call sites.
- Remove unneeded offset arguments from two SIMD related immediates.
- Add a pc argument to all Validate functions for immediates instead
  of using the Decoder's current pc.
- Remove the (unused) pc argument from all Complete functions for
  immediates.
- Introduce Validate() for BranchOnExceptionImmediate.
- In WasmDecoder::Decode(), make sure len is updated before breaking out
  of the loop in case of a Validate() failure.
- Change the default prefix_len of DecodeLoadMem/DecodeStoreMem to 1.

wasm-interpreter.cc:
- Change the default prefix_len of ExecuteLoad/Store to 1.
- Adapt offsets in calls to Immediates.
- Remove redundant opcode_length argument from ExecuteSimdOp, use len
  in its place.

function-body-decoder-unittest.cc
- Adapt offsets in calls to Immediates.
- Introduce and use EXPECT_OK, as is done in other tests.

Change-Id: I534606c0e238af309804d4a7c8cec75b1e49c6ad
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2267381
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@{#68559}
parent 1f80b36c
...@@ -255,15 +255,13 @@ ValueType read_value_type(Decoder* decoder, const byte* pc, ...@@ -255,15 +255,13 @@ ValueType read_value_type(Decoder* decoder, const byte* pc,
} // namespace value_type_reader } // namespace value_type_reader
// Helpers for decoding different kinds of immediates which follow bytecodes. // Helpers for decoding different kinds of immediates which follow bytecodes.
// TODO(manoskouk): Some Immediates look at current pc and some introduce an
// offset. Clear up this situation.
template <Decoder::ValidateFlag validate> template <Decoder::ValidateFlag validate>
struct LocalIndexImmediate { struct LocalIndexImmediate {
uint32_t index; uint32_t index;
uint32_t length; uint32_t length;
inline LocalIndexImmediate(Decoder* decoder, const byte* pc) { inline LocalIndexImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 1, &length, "local index"); index = decoder->read_u32v<validate>(pc, &length, "local index");
} }
}; };
...@@ -274,7 +272,7 @@ struct ExceptionIndexImmediate { ...@@ -274,7 +272,7 @@ struct ExceptionIndexImmediate {
uint32_t length; uint32_t length;
inline ExceptionIndexImmediate(Decoder* decoder, const byte* pc) { inline ExceptionIndexImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 1, &length, "exception index"); index = decoder->read_u32v<validate>(pc, &length, "exception index");
} }
}; };
...@@ -283,7 +281,7 @@ struct ImmI32Immediate { ...@@ -283,7 +281,7 @@ struct ImmI32Immediate {
int32_t value; int32_t value;
uint32_t length; uint32_t length;
inline ImmI32Immediate(Decoder* decoder, const byte* pc) { inline ImmI32Immediate(Decoder* decoder, const byte* pc) {
value = decoder->read_i32v<validate>(pc + 1, &length, "immi32"); value = decoder->read_i32v<validate>(pc, &length, "immi32");
} }
}; };
...@@ -292,7 +290,7 @@ struct ImmI64Immediate { ...@@ -292,7 +290,7 @@ struct ImmI64Immediate {
int64_t value; int64_t value;
uint32_t length; uint32_t length;
inline ImmI64Immediate(Decoder* decoder, const byte* pc) { inline ImmI64Immediate(Decoder* decoder, const byte* pc) {
value = decoder->read_i64v<validate>(pc + 1, &length, "immi64"); value = decoder->read_i64v<validate>(pc, &length, "immi64");
} }
}; };
...@@ -304,7 +302,7 @@ struct ImmF32Immediate { ...@@ -304,7 +302,7 @@ struct ImmF32Immediate {
// We can't use bit_cast here because calling any helper function that // We can't use bit_cast here because calling any helper function that
// returns a float would potentially flip NaN bits per C++ semantics, so we // returns a float would potentially flip NaN bits per C++ semantics, so we
// have to inline the memcpy call directly. // have to inline the memcpy call directly.
uint32_t tmp = decoder->read_u32<validate>(pc + 1, "immf32"); uint32_t tmp = decoder->read_u32<validate>(pc, "immf32");
memcpy(&value, &tmp, sizeof(value)); memcpy(&value, &tmp, sizeof(value));
} }
}; };
...@@ -315,7 +313,7 @@ struct ImmF64Immediate { ...@@ -315,7 +313,7 @@ struct ImmF64Immediate {
uint32_t length = 8; uint32_t length = 8;
inline ImmF64Immediate(Decoder* decoder, const byte* pc) { inline ImmF64Immediate(Decoder* decoder, const byte* pc) {
// Avoid bit_cast because it might not preserve the signalling bit of a NaN. // Avoid bit_cast because it might not preserve the signalling bit of a NaN.
uint64_t tmp = decoder->read_u64<validate>(pc + 1, "immf64"); uint64_t tmp = decoder->read_u64<validate>(pc, "immf64");
memcpy(&value, &tmp, sizeof(value)); memcpy(&value, &tmp, sizeof(value));
} }
}; };
...@@ -328,7 +326,7 @@ struct GlobalIndexImmediate { ...@@ -328,7 +326,7 @@ struct GlobalIndexImmediate {
uint32_t length; uint32_t length;
inline GlobalIndexImmediate(Decoder* decoder, const byte* pc) { inline GlobalIndexImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 1, &length, "global index"); index = decoder->read_u32v<validate>(pc, &length, "global index");
} }
}; };
...@@ -340,15 +338,15 @@ struct SelectTypeImmediate { ...@@ -340,15 +338,15 @@ struct SelectTypeImmediate {
inline SelectTypeImmediate(const WasmFeatures& enabled, Decoder* decoder, inline SelectTypeImmediate(const WasmFeatures& enabled, Decoder* decoder,
const byte* pc) { const byte* pc) {
uint8_t num_types = uint8_t num_types =
decoder->read_u32v<validate>(pc + 1, &length, "number of select types"); decoder->read_u32v<validate>(pc, &length, "number of select types");
if (!VALIDATE(num_types == 1)) { if (!VALIDATE(num_types == 1)) {
decoder->error( decoder->error(
pc + 1, "Invalid number of types. Select accepts exactly one type"); pc + 1, "Invalid number of types. Select accepts exactly one type");
return; return;
} }
uint32_t type_length; uint32_t type_length;
type = value_type_reader::read_value_type<validate>( type = value_type_reader::read_value_type<validate>(decoder, pc + length,
decoder, pc + length + 1, &type_length, enabled); &type_length, enabled);
length += type_length; length += type_length;
if (!VALIDATE(type != kWasmBottom)) { if (!VALIDATE(type != kWasmBottom)) {
decoder->error(pc + 1, "invalid select type"); decoder->error(pc + 1, "invalid select type");
...@@ -365,12 +363,12 @@ struct BlockTypeImmediate { ...@@ -365,12 +363,12 @@ struct BlockTypeImmediate {
inline BlockTypeImmediate(const WasmFeatures& enabled, Decoder* decoder, inline BlockTypeImmediate(const WasmFeatures& enabled, Decoder* decoder,
const byte* pc) { const byte* pc) {
if (decoder->read_u8<validate>(pc + 1, "block type") == kLocalVoid) { if (decoder->read_u8<validate>(pc, "block type") == kLocalVoid) {
// 1st case: void block. Struct fields stay at default values. // 1st case: void block. Struct fields stay at default values.
return; return;
} }
type = value_type_reader::read_value_type<validate>(decoder, pc + 1, type = value_type_reader::read_value_type<validate>(decoder, pc, &length,
&length, enabled); enabled);
if (type != kWasmBottom) { if (type != kWasmBottom) {
// 2nd case: block with val type immediate. // 2nd case: block with val type immediate.
return; return;
...@@ -378,14 +376,14 @@ struct BlockTypeImmediate { ...@@ -378,14 +376,14 @@ struct BlockTypeImmediate {
// It has to be the 3rd case: multi-value block, // It has to be the 3rd case: multi-value block,
// which is represented by a type index. // which is represented by a type index.
if (!VALIDATE(enabled.has_mv())) { if (!VALIDATE(enabled.has_mv())) {
decoder->error(pc + 1, "invalid block type"); decoder->error(pc, "invalid block type");
return; return;
} }
if (!VALIDATE(decoder->ok())) return; if (!VALIDATE(decoder->ok())) return;
int32_t index = int32_t index =
decoder->read_i32v<validate>(pc + 1, &length, "block type index"); decoder->read_i32v<validate>(pc, &length, "block type index");
if (!VALIDATE(length > 0 && index >= 0)) { if (!VALIDATE(length > 0 && index >= 0)) {
decoder->error(pc + 1, "invalid block type index"); decoder->error(pc, "invalid block type index");
return; return;
} }
sig_index = static_cast<uint32_t>(index); sig_index = static_cast<uint32_t>(index);
...@@ -417,7 +415,7 @@ struct BranchDepthImmediate { ...@@ -417,7 +415,7 @@ struct BranchDepthImmediate {
uint32_t depth; uint32_t depth;
uint32_t length; uint32_t length;
inline BranchDepthImmediate(Decoder* decoder, const byte* pc) { inline BranchDepthImmediate(Decoder* decoder, const byte* pc) {
depth = decoder->read_u32v<validate>(pc + 1, &length, "branch depth"); depth = decoder->read_u32v<validate>(pc, &length, "branch depth");
} }
}; };
...@@ -438,7 +436,7 @@ struct FunctionIndexImmediate { ...@@ -438,7 +436,7 @@ struct FunctionIndexImmediate {
uint32_t index = 0; uint32_t index = 0;
uint32_t length = 1; uint32_t length = 1;
inline FunctionIndexImmediate(Decoder* decoder, const byte* pc) { inline FunctionIndexImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 1, &length, "function index"); index = decoder->read_u32v<validate>(pc, &length, "function index");
} }
}; };
...@@ -448,9 +446,9 @@ struct MemoryIndexImmediate { ...@@ -448,9 +446,9 @@ struct MemoryIndexImmediate {
uint32_t length = 1; uint32_t length = 1;
inline MemoryIndexImmediate() = default; inline MemoryIndexImmediate() = default;
inline MemoryIndexImmediate(Decoder* decoder, const byte* pc) { inline MemoryIndexImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u8<validate>(pc + 1, "memory index"); index = decoder->read_u8<validate>(pc, "memory index");
if (!VALIDATE(index == 0)) { if (!VALIDATE(index == 0)) {
decoder->errorf(pc + 1, "expected memory index 0, found %u", index); decoder->errorf(pc, "expected memory index 0, found %u", index);
} }
} }
}; };
...@@ -461,7 +459,7 @@ struct TableIndexImmediate { ...@@ -461,7 +459,7 @@ struct TableIndexImmediate {
unsigned length = 1; unsigned length = 1;
inline TableIndexImmediate() = default; inline TableIndexImmediate() = default;
inline TableIndexImmediate(Decoder* decoder, const byte* pc) { inline TableIndexImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 1, &length, "table index"); index = decoder->read_u32v<validate>(pc, &length, "table index");
} }
}; };
...@@ -509,11 +507,11 @@ struct CallIndirectImmediate { ...@@ -509,11 +507,11 @@ struct CallIndirectImmediate {
inline CallIndirectImmediate(const WasmFeatures enabled, Decoder* decoder, inline CallIndirectImmediate(const WasmFeatures enabled, Decoder* decoder,
const byte* pc) { const byte* pc) {
uint32_t len = 0; uint32_t len = 0;
sig_index = decoder->read_u32v<validate>(pc + 1, &len, "signature index"); sig_index = decoder->read_u32v<validate>(pc, &len, "signature index");
TableIndexImmediate<validate> table(decoder, pc + len); TableIndexImmediate<validate> table(decoder, pc + len);
if (!VALIDATE((table.index == 0 && table.length == 1) || if (!VALIDATE((table.index == 0 && table.length == 1) ||
enabled.has_reftypes())) { enabled.has_reftypes())) {
decoder->errorf(pc + 1 + len, "expected table index 0, found %u", decoder->errorf(pc + len, "expected table index 0, found %u",
table.index); table.index);
} }
table_index = table.index; table_index = table.index;
...@@ -527,7 +525,7 @@ struct CallFunctionImmediate { ...@@ -527,7 +525,7 @@ struct CallFunctionImmediate {
const FunctionSig* sig = nullptr; const FunctionSig* sig = nullptr;
uint32_t length; uint32_t length;
inline CallFunctionImmediate(Decoder* decoder, const byte* pc) { inline CallFunctionImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 1, &length, "function index"); index = decoder->read_u32v<validate>(pc, &length, "function index");
} }
}; };
...@@ -537,11 +535,10 @@ struct BranchTableImmediate { ...@@ -537,11 +535,10 @@ struct BranchTableImmediate {
const byte* start; const byte* start;
const byte* table; const byte* table;
inline BranchTableImmediate(Decoder* decoder, const byte* pc) { inline BranchTableImmediate(Decoder* decoder, const byte* pc) {
DCHECK_EQ(kExprBrTable, decoder->read_u8<validate>(pc, "opcode")); start = pc;
start = pc + 1;
uint32_t len = 0; uint32_t len = 0;
table_count = decoder->read_u32v<validate>(pc + 1, &len, "table count"); table_count = decoder->read_u32v<validate>(pc, &len, "table count");
table = pc + 1 + len; table = pc + len;
} }
}; };
...@@ -576,11 +573,11 @@ class BranchTableIterator { ...@@ -576,11 +573,11 @@ class BranchTableIterator {
table_count_(imm.table_count) {} table_count_(imm.table_count) {}
private: private:
Decoder* decoder_; Decoder* const decoder_;
const byte* start_; const byte* start_;
const byte* pc_; const byte* pc_;
uint32_t index_ = 0; // the current index. uint32_t index_ = 0; // the current index.
uint32_t table_count_; // the count of entries, not including default. const uint32_t table_count_; // the count of entries, not including default.
}; };
template <Decoder::ValidateFlag validate> template <Decoder::ValidateFlag validate>
...@@ -592,16 +589,16 @@ struct MemoryAccessImmediate { ...@@ -592,16 +589,16 @@ struct MemoryAccessImmediate {
uint32_t max_alignment) { uint32_t max_alignment) {
uint32_t alignment_length; uint32_t alignment_length;
alignment = alignment =
decoder->read_u32v<validate>(pc + 1, &alignment_length, "alignment"); decoder->read_u32v<validate>(pc, &alignment_length, "alignment");
if (!VALIDATE(alignment <= max_alignment)) { if (!VALIDATE(alignment <= max_alignment)) {
decoder->errorf(pc + 1, decoder->errorf(pc,
"invalid alignment; expected maximum alignment is %u, " "invalid alignment; expected maximum alignment is %u, "
"actual alignment is %u", "actual alignment is %u",
max_alignment, alignment); max_alignment, alignment);
} }
uint32_t offset_length; uint32_t offset_length;
offset = decoder->read_u32v<validate>(pc + 1 + alignment_length, offset = decoder->read_u32v<validate>(pc + alignment_length, &offset_length,
&offset_length, "offset"); "offset");
length = alignment_length + offset_length; length = alignment_length + offset_length;
} }
}; };
...@@ -612,12 +609,8 @@ struct SimdLaneImmediate { ...@@ -612,12 +609,8 @@ struct SimdLaneImmediate {
uint8_t lane; uint8_t lane;
uint32_t length = 1; uint32_t length = 1;
inline SimdLaneImmediate(Decoder* decoder, const byte* pc, inline SimdLaneImmediate(Decoder* decoder, const byte* pc) {
uint32_t opcode_length) { lane = decoder->read_u8<validate>(pc, "lane");
// Callers should pass in pc unchanged from where the decoding happens. 1 is
// added to account for the SIMD prefix byte, and opcode_length is the
// number of bytes the LEB encoding of the SIMD opcode takes.
lane = decoder->read_u8<validate>(pc + 1 + opcode_length, "lane");
} }
}; };
...@@ -626,14 +619,9 @@ template <Decoder::ValidateFlag validate> ...@@ -626,14 +619,9 @@ template <Decoder::ValidateFlag validate>
struct Simd8x16ShuffleImmediate { struct Simd8x16ShuffleImmediate {
uint8_t shuffle[kSimd128Size] = {0}; uint8_t shuffle[kSimd128Size] = {0};
inline Simd8x16ShuffleImmediate(Decoder* decoder, const byte* pc, inline Simd8x16ShuffleImmediate(Decoder* decoder, const byte* pc) {
uint32_t opcode_length) {
// Callers should pass in pc unchanged from where the decoding happens. 1 is
// added to account for the SIMD prefix byte, and opcode_length is the
// number of bytes the LEB encoding of the SIMD opcode takes.
for (uint32_t i = 0; i < kSimd128Size; ++i) { for (uint32_t i = 0; i < kSimd128Size; ++i) {
shuffle[i] = shuffle[i] = decoder->read_u8<validate>(pc + i, "shuffle");
decoder->read_u8<validate>(pc + 1 + opcode_length + i, "shuffle");
} }
} }
}; };
...@@ -647,8 +635,8 @@ struct MemoryInitImmediate { ...@@ -647,8 +635,8 @@ struct MemoryInitImmediate {
inline MemoryInitImmediate(Decoder* decoder, const byte* pc) { inline MemoryInitImmediate(Decoder* decoder, const byte* pc) {
uint32_t len = 0; uint32_t len = 0;
data_segment_index = data_segment_index =
decoder->read_u32v<validate>(pc + 2, &len, "data segment index"); decoder->read_u32v<validate>(pc, &len, "data segment index");
memory = MemoryIndexImmediate<validate>(decoder, pc + 1 + len); memory = MemoryIndexImmediate<validate>(decoder, pc + len);
length = len + memory.length; length = len + memory.length;
} }
}; };
...@@ -659,7 +647,7 @@ struct DataDropImmediate { ...@@ -659,7 +647,7 @@ struct DataDropImmediate {
unsigned length; unsigned length;
inline DataDropImmediate(Decoder* decoder, const byte* pc) { inline DataDropImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 2, &length, "data segment index"); index = decoder->read_u32v<validate>(pc, &length, "data segment index");
} }
}; };
...@@ -670,9 +658,9 @@ struct MemoryCopyImmediate { ...@@ -670,9 +658,9 @@ struct MemoryCopyImmediate {
unsigned length = 0; unsigned length = 0;
inline MemoryCopyImmediate(Decoder* decoder, const byte* pc) { inline MemoryCopyImmediate(Decoder* decoder, const byte* pc) {
memory_src = MemoryIndexImmediate<validate>(decoder, pc + 1); memory_src = MemoryIndexImmediate<validate>(decoder, pc);
memory_dst = memory_dst =
MemoryIndexImmediate<validate>(decoder, pc + 1 + memory_src.length); MemoryIndexImmediate<validate>(decoder, pc + memory_src.length);
length = memory_src.length + memory_dst.length; length = memory_src.length + memory_dst.length;
} }
}; };
...@@ -686,8 +674,8 @@ struct TableInitImmediate { ...@@ -686,8 +674,8 @@ struct TableInitImmediate {
inline TableInitImmediate(Decoder* decoder, const byte* pc) { inline TableInitImmediate(Decoder* decoder, const byte* pc) {
uint32_t len = 0; uint32_t len = 0;
elem_segment_index = elem_segment_index =
decoder->read_u32v<validate>(pc + 2, &len, "elem segment index"); decoder->read_u32v<validate>(pc, &len, "elem segment index");
table = TableIndexImmediate<validate>(decoder, pc + 1 + len); table = TableIndexImmediate<validate>(decoder, pc + len);
length = len + table.length; length = len + table.length;
} }
}; };
...@@ -698,7 +686,7 @@ struct ElemDropImmediate { ...@@ -698,7 +686,7 @@ struct ElemDropImmediate {
unsigned length; unsigned length;
inline ElemDropImmediate(Decoder* decoder, const byte* pc) { inline ElemDropImmediate(Decoder* decoder, const byte* pc) {
index = decoder->read_u32v<validate>(pc + 2, &length, "elem segment index"); index = decoder->read_u32v<validate>(pc, &length, "elem segment index");
} }
}; };
...@@ -709,9 +697,8 @@ struct TableCopyImmediate { ...@@ -709,9 +697,8 @@ struct TableCopyImmediate {
unsigned length = 0; unsigned length = 0;
inline TableCopyImmediate(Decoder* decoder, const byte* pc) { inline TableCopyImmediate(Decoder* decoder, const byte* pc) {
table_dst = TableIndexImmediate<validate>(decoder, pc + 1); table_dst = TableIndexImmediate<validate>(decoder, pc);
table_src = table_src = TableIndexImmediate<validate>(decoder, pc + table_dst.length);
TableIndexImmediate<validate>(decoder, pc + 1 + table_dst.length);
length = table_src.length + table_dst.length; length = table_src.length + table_dst.length;
} }
}; };
...@@ -724,8 +711,7 @@ struct HeapTypeImmediate { ...@@ -724,8 +711,7 @@ struct HeapTypeImmediate {
const byte* pc) { const byte* pc) {
// Check for negative 1-byte value first, e.g. -0x10 == funcref. If there's // Check for negative 1-byte value first, e.g. -0x10 == funcref. If there's
// no match, we'll read a uint32 from the same offset later. // no match, we'll read a uint32 from the same offset later.
uint8_t heap_index = uint8_t heap_index = decoder->read_u8<validate>(pc, "heap type immediate");
decoder->read_u8<validate>(pc + 1, "heap type immediate");
switch (static_cast<ValueTypeCode>(heap_index)) { switch (static_cast<ValueTypeCode>(heap_index)) {
#define REF_TYPE_CASE(heap_type, feature) \ #define REF_TYPE_CASE(heap_type, feature) \
case kLocal##heap_type##Ref: { \ case kLocal##heap_type##Ref: { \
...@@ -743,8 +729,8 @@ struct HeapTypeImmediate { ...@@ -743,8 +729,8 @@ struct HeapTypeImmediate {
REF_TYPE_CASE(Exn, eh) REF_TYPE_CASE(Exn, eh)
#undef REF_TYPE_CASE #undef REF_TYPE_CASE
default: default:
uint32_t type_index = decoder->read_u32v<validate>( uint32_t type_index =
pc + 1, &length, "heap type immediate"); decoder->read_u32v<validate>(pc, &length, "heap type immediate");
if (!VALIDATE(type_index < kV8MaxWasmTypes)) { if (!VALIDATE(type_index < kV8MaxWasmTypes)) {
decoder->errorf(pc + 1, decoder->errorf(pc + 1,
"Type index %u is greater than the maximum number " "Type index %u is greater than the maximum number "
...@@ -1099,7 +1085,7 @@ class WasmDecoder : public Decoder { ...@@ -1099,7 +1085,7 @@ class WasmDecoder : public Decoder {
break; break;
case kExprLocalSet: // fallthru case kExprLocalSet: // fallthru
case kExprLocalTee: { case kExprLocalTee: {
LocalIndexImmediate<validate> imm(decoder, pc); LocalIndexImmediate<validate> imm(decoder, pc + 1);
if (assigned->length() > 0 && if (assigned->length() > 0 &&
imm.index < static_cast<uint32_t>(assigned->length())) { imm.index < static_cast<uint32_t>(assigned->length())) {
// Unverified code might have an out-of-bounds index. // Unverified code might have an out-of-bounds index.
...@@ -1133,21 +1119,21 @@ class WasmDecoder : public Decoder { ...@@ -1133,21 +1119,21 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, LocalIndexImmediate<validate>& imm) { inline bool Validate(const byte* pc, LocalIndexImmediate<validate>& imm) {
if (!VALIDATE(imm.index < num_locals())) { if (!VALIDATE(imm.index < num_locals())) {
errorf(pc + 1, "invalid local index: %u", imm.index); errorf(pc, "invalid local index: %u", imm.index);
return false; return false;
} }
return true; return true;
} }
inline bool Complete(const byte* pc, ExceptionIndexImmediate<validate>& imm) { inline bool Complete(ExceptionIndexImmediate<validate>& imm) {
if (!VALIDATE(imm.index < module_->exceptions.size())) return false; if (!VALIDATE(imm.index < module_->exceptions.size())) return false;
imm.exception = &module_->exceptions[imm.index]; imm.exception = &module_->exceptions[imm.index];
return true; return true;
} }
inline bool Validate(const byte* pc, ExceptionIndexImmediate<validate>& imm) { inline bool Validate(const byte* pc, ExceptionIndexImmediate<validate>& imm) {
if (!Complete(pc, imm)) { if (!Complete(imm)) {
errorf(pc + 1, "Invalid exception index: %u", imm.index); errorf(pc, "Invalid exception index: %u", imm.index);
return false; return false;
} }
return true; return true;
...@@ -1155,7 +1141,7 @@ class WasmDecoder : public Decoder { ...@@ -1155,7 +1141,7 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, GlobalIndexImmediate<validate>& imm) { inline bool Validate(const byte* pc, GlobalIndexImmediate<validate>& imm) {
if (!VALIDATE(imm.index < module_->globals.size())) { if (!VALIDATE(imm.index < module_->globals.size())) {
errorf(pc + 1, "invalid global index: %u", imm.index); errorf(pc, "invalid global index: %u", imm.index);
return false; return false;
} }
imm.global = &module_->globals[imm.index]; imm.global = &module_->globals[imm.index];
...@@ -1163,14 +1149,14 @@ class WasmDecoder : public Decoder { ...@@ -1163,14 +1149,14 @@ class WasmDecoder : public Decoder {
return true; return true;
} }
inline bool Complete(const byte* pc, StructIndexImmediate<validate>& imm) { inline bool Complete(StructIndexImmediate<validate>& imm) {
if (!VALIDATE(module_->has_struct(imm.index))) return false; if (!VALIDATE(module_->has_struct(imm.index))) return false;
imm.struct_type = module_->struct_type(imm.index); imm.struct_type = module_->struct_type(imm.index);
return true; return true;
} }
inline bool Validate(const byte* pc, StructIndexImmediate<validate>& imm) { inline bool Validate(const byte* pc, StructIndexImmediate<validate>& imm) {
if (Complete(pc, imm)) return true; if (Complete(imm)) return true;
errorf(pc, "invalid struct index: %u", imm.index); errorf(pc, "invalid struct index: %u", imm.index);
return false; return false;
} }
...@@ -1185,14 +1171,14 @@ class WasmDecoder : public Decoder { ...@@ -1185,14 +1171,14 @@ class WasmDecoder : public Decoder {
return true; return true;
} }
inline bool Complete(const byte* pc, ArrayIndexImmediate<validate>& imm) { inline bool Complete(ArrayIndexImmediate<validate>& imm) {
if (!VALIDATE(module_->has_array(imm.index))) return false; if (!VALIDATE(module_->has_array(imm.index))) return false;
imm.array_type = module_->array_type(imm.index); imm.array_type = module_->array_type(imm.index);
return true; return true;
} }
inline bool Validate(const byte* pc, ArrayIndexImmediate<validate>& imm) { inline bool Validate(const byte* pc, ArrayIndexImmediate<validate>& imm) {
if (!Complete(pc, imm)) { if (!Complete(imm)) {
errorf(pc, "invalid array index: %u", imm.index); errorf(pc, "invalid array index: %u", imm.index);
return false; return false;
} }
...@@ -1209,7 +1195,7 @@ class WasmDecoder : public Decoder { ...@@ -1209,7 +1195,7 @@ class WasmDecoder : public Decoder {
return true; return true;
} }
inline bool Complete(const byte* pc, CallFunctionImmediate<validate>& imm) { inline bool Complete(CallFunctionImmediate<validate>& imm) {
if (!VALIDATE(imm.index < module_->functions.size())) return false; if (!VALIDATE(imm.index < module_->functions.size())) return false;
imm.sig = module_->functions[imm.index].sig; imm.sig = module_->functions[imm.index].sig;
if (imm.sig->return_count() > 1) { if (imm.sig->return_count() > 1) {
...@@ -1219,14 +1205,14 @@ class WasmDecoder : public Decoder { ...@@ -1219,14 +1205,14 @@ class WasmDecoder : public Decoder {
} }
inline bool Validate(const byte* pc, CallFunctionImmediate<validate>& imm) { inline bool Validate(const byte* pc, CallFunctionImmediate<validate>& imm) {
if (!Complete(pc, imm)) { if (!Complete(imm)) {
errorf(pc + 1, "invalid function index: %u", imm.index); errorf(pc, "invalid function index: %u", imm.index);
return false; return false;
} }
return true; return true;
} }
inline bool Complete(const byte* pc, CallIndirectImmediate<validate>& imm) { inline bool Complete(CallIndirectImmediate<validate>& imm) {
if (!VALIDATE(module_->has_signature(imm.sig_index))) return false; if (!VALIDATE(module_->has_signature(imm.sig_index))) return false;
imm.sig = module_->signature(imm.sig_index); imm.sig = module_->signature(imm.sig_index);
if (imm.sig->return_count() > 1) { if (imm.sig->return_count() > 1) {
...@@ -1244,8 +1230,8 @@ class WasmDecoder : public Decoder { ...@@ -1244,8 +1230,8 @@ class WasmDecoder : public Decoder {
error("table of call_indirect must be of type funcref"); error("table of call_indirect must be of type funcref");
return false; return false;
} }
if (!Complete(pc, imm)) { if (!Complete(imm)) {
errorf(pc + 1, "invalid signature index: #%u", imm.sig_index); errorf(pc, "invalid signature index: #%u", imm.sig_index);
return false; return false;
} }
return true; return true;
...@@ -1254,22 +1240,29 @@ class WasmDecoder : public Decoder { ...@@ -1254,22 +1240,29 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, BranchDepthImmediate<validate>& imm, inline bool Validate(const byte* pc, BranchDepthImmediate<validate>& imm,
size_t control_depth) { size_t control_depth) {
if (!VALIDATE(imm.depth < control_depth)) { if (!VALIDATE(imm.depth < control_depth)) {
errorf(pc + 1, "invalid branch depth: %u", imm.depth); errorf(pc, "invalid branch depth: %u", imm.depth);
return false; return false;
} }
return true; return true;
} }
bool Validate(const byte* pc, BranchTableImmediate<validate>& imm, inline bool Validate(const byte* pc, BranchTableImmediate<validate>& imm,
size_t block_depth) { size_t block_depth) {
if (!VALIDATE(imm.table_count <= kV8MaxWasmFunctionBrTableSize)) { if (!VALIDATE(imm.table_count <= kV8MaxWasmFunctionBrTableSize)) {
errorf(pc + 1, "invalid table count (> max br_table size): %u", errorf(pc, "invalid table count (> max br_table size): %u",
imm.table_count); imm.table_count);
return false; return false;
} }
return checkAvailable(imm.table_count); return checkAvailable(imm.table_count);
} }
inline bool Validate(const byte* pc,
BranchOnExceptionImmediate<validate>& imm,
size_t control_size) {
return Validate(pc, imm.depth, control_size) &&
Validate(pc + imm.depth.length, imm.index);
}
inline bool Validate(const byte* pc, WasmOpcode opcode, inline bool Validate(const byte* pc, WasmOpcode opcode,
SimdLaneImmediate<validate>& imm) { SimdLaneImmediate<validate>& imm) {
uint8_t num_lanes = 0; uint8_t num_lanes = 0;
...@@ -1301,7 +1294,7 @@ class WasmDecoder : public Decoder { ...@@ -1301,7 +1294,7 @@ class WasmDecoder : public Decoder {
break; break;
} }
if (!VALIDATE(imm.lane >= 0 && imm.lane < num_lanes)) { if (!VALIDATE(imm.lane >= 0 && imm.lane < num_lanes)) {
error(pc_ + 2, "invalid lane index"); error(pc, "invalid lane index");
return false; return false;
} else { } else {
return true; return true;
...@@ -1316,7 +1309,7 @@ class WasmDecoder : public Decoder { ...@@ -1316,7 +1309,7 @@ class WasmDecoder : public Decoder {
} }
// Shuffle indices must be in [0..31] for a 16 lane shuffle. // Shuffle indices must be in [0..31] for a 16 lane shuffle.
if (!VALIDATE(max_lane < 2 * kSimd128Size)) { if (!VALIDATE(max_lane < 2 * kSimd128Size)) {
error(pc_ + 2, "invalid shuffle mask"); error(pc, "invalid shuffle mask");
return false; return false;
} }
return true; return true;
...@@ -1332,10 +1325,10 @@ class WasmDecoder : public Decoder { ...@@ -1332,10 +1325,10 @@ class WasmDecoder : public Decoder {
return true; return true;
} }
inline bool Validate(BlockTypeImmediate<validate>& imm) { inline bool Validate(const byte* pc, BlockTypeImmediate<validate>& imm) {
if (!Complete(imm)) { if (!Complete(imm)) {
errorf(pc_, "block type index %u out of bounds (%zu types)", errorf(pc, "block type index %u out of bounds (%zu types)", imm.sig_index,
imm.sig_index, module_->types.size()); module_->types.size());
return false; return false;
} }
return true; return true;
...@@ -1355,35 +1348,34 @@ class WasmDecoder : public Decoder { ...@@ -1355,35 +1348,34 @@ class WasmDecoder : public Decoder {
inline bool Validate(const byte* pc, MemoryIndexImmediate<validate>& imm) { inline bool Validate(const byte* pc, MemoryIndexImmediate<validate>& imm) {
if (!VALIDATE(module_->has_memory)) { if (!VALIDATE(module_->has_memory)) {
errorf(pc + 1, "memory instruction with no memory"); errorf(pc, "memory instruction with no memory");
return false; return false;
} }
return true; return true;
} }
inline bool Validate(MemoryInitImmediate<validate>& imm) { inline bool Validate(const byte* pc, MemoryInitImmediate<validate>& imm) {
if (!VALIDATE(imm.data_segment_index < if (!VALIDATE(imm.data_segment_index <
module_->num_declared_data_segments)) { module_->num_declared_data_segments)) {
errorf(pc_ + 2, "invalid data segment index: %u", imm.data_segment_index); errorf(pc, "invalid data segment index: %u", imm.data_segment_index);
return false; return false;
} }
if (!Validate(pc_ + imm.length - imm.memory.length - 1, imm.memory)) if (!Validate(pc + imm.length - imm.memory.length, imm.memory))
return false; return false;
return true; return true;
} }
inline bool Validate(DataDropImmediate<validate>& imm) { inline bool Validate(const byte* pc, DataDropImmediate<validate>& imm) {
if (!VALIDATE(imm.index < module_->num_declared_data_segments)) { if (!VALIDATE(imm.index < module_->num_declared_data_segments)) {
errorf(pc_ + 2, "invalid data segment index: %u", imm.index); errorf(pc, "invalid data segment index: %u", imm.index);
return false; return false;
} }
return true; return true;
} }
inline bool Validate(MemoryCopyImmediate<validate>& imm) { inline bool Validate(const byte* pc, MemoryCopyImmediate<validate>& imm) {
if (!Validate(pc_ + 1, imm.memory_src)) return false; return Validate(pc, imm.memory_src) &&
if (!Validate(pc_ + 2, imm.memory_dst)) return false; Validate(pc + imm.memory_src.length, imm.memory_dst);
return true;
} }
inline bool Validate(const byte* pc, TableIndexImmediate<validate>& imm) { inline bool Validate(const byte* pc, TableIndexImmediate<validate>& imm) {
...@@ -1394,40 +1386,39 @@ class WasmDecoder : public Decoder { ...@@ -1394,40 +1386,39 @@ class WasmDecoder : public Decoder {
return true; return true;
} }
inline bool Validate(TableInitImmediate<validate>& imm) { inline bool Validate(const byte* pc, TableInitImmediate<validate>& imm) {
if (!VALIDATE(imm.elem_segment_index < module_->elem_segments.size())) { if (!VALIDATE(imm.elem_segment_index < module_->elem_segments.size())) {
errorf(pc_ + 2, "invalid element segment index: %u", errorf(pc, "invalid element segment index: %u", imm.elem_segment_index);
imm.elem_segment_index);
return false; return false;
} }
if (!Validate(pc_ + imm.length - imm.table.length - 1, imm.table)) { if (!Validate(pc + imm.length - imm.table.length, imm.table)) {
return false; return false;
} }
ValueType elem_type = module_->elem_segments[imm.elem_segment_index].type; ValueType elem_type = module_->elem_segments[imm.elem_segment_index].type;
if (!VALIDATE(IsSubtypeOf(elem_type, module_->tables[imm.table.index].type, if (!VALIDATE(IsSubtypeOf(elem_type, module_->tables[imm.table.index].type,
module_))) { module_))) {
errorf(pc_ + 2, "table %u is not a super-type of %s", imm.table.index, errorf(pc, "table %u is not a super-type of %s", imm.table.index,
elem_type.type_name().c_str()); elem_type.type_name().c_str());
return false; return false;
} }
return true; return true;
} }
inline bool Validate(ElemDropImmediate<validate>& imm) { inline bool Validate(const byte* pc, ElemDropImmediate<validate>& imm) {
if (!VALIDATE(imm.index < module_->elem_segments.size())) { if (!VALIDATE(imm.index < module_->elem_segments.size())) {
errorf(pc_ + 2, "invalid element segment index: %u", imm.index); errorf(pc, "invalid element segment index: %u", imm.index);
return false; return false;
} }
return true; return true;
} }
inline bool Validate(TableCopyImmediate<validate>& imm) { inline bool Validate(const byte* pc, TableCopyImmediate<validate>& imm) {
if (!Validate(pc_ + 1, imm.table_src)) return false; if (!Validate(pc, imm.table_src)) return false;
if (!Validate(pc_ + 2, imm.table_dst)) return false; if (!Validate(pc + imm.table_src.length, imm.table_dst)) return false;
ValueType src_type = module_->tables[imm.table_src.index].type; ValueType src_type = module_->tables[imm.table_src.index].type;
if (!VALIDATE(IsSubtypeOf( if (!VALIDATE(IsSubtypeOf(
src_type, module_->tables[imm.table_dst.index].type, module_))) { src_type, module_->tables[imm.table_dst.index].type, module_))) {
errorf(pc_ + 2, "table %u is not a super-type of %s", imm.table_dst.index, errorf(pc, "table %u is not a super-type of %s", imm.table_dst.index,
src_type.type_name().c_str()); src_type.type_name().c_str());
return false; return false;
} }
...@@ -1455,32 +1446,33 @@ class WasmDecoder : public Decoder { ...@@ -1455,32 +1446,33 @@ class WasmDecoder : public Decoder {
FOREACH_STORE_MEM_OPCODE(DECLARE_OPCODE_CASE) FOREACH_STORE_MEM_OPCODE(DECLARE_OPCODE_CASE)
#undef DECLARE_OPCODE_CASE #undef DECLARE_OPCODE_CASE
{ {
MemoryAccessImmediate<validate> imm(decoder, pc, UINT32_MAX); MemoryAccessImmediate<validate> imm(decoder, pc + 1, UINT32_MAX);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprBr: case kExprBr:
case kExprBrIf: { case kExprBrIf: {
BranchDepthImmediate<validate> imm(decoder, pc); BranchDepthImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprGlobalGet: case kExprGlobalGet:
case kExprGlobalSet: { case kExprGlobalSet: {
GlobalIndexImmediate<validate> imm(decoder, pc); GlobalIndexImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprTableGet: case kExprTableGet:
case kExprTableSet: { case kExprTableSet: {
TableIndexImmediate<validate> imm(decoder, pc); TableIndexImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprCallFunction: case kExprCallFunction:
case kExprReturnCall: { case kExprReturnCall: {
CallFunctionImmediate<validate> imm(decoder, pc); CallFunctionImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprCallIndirect: case kExprCallIndirect:
case kExprReturnCallIndirect: { case kExprReturnCallIndirect: {
CallIndirectImmediate<validate> imm(WasmFeatures::All(), decoder, pc); CallIndirectImmediate<validate> imm(WasmFeatures::All(), decoder,
pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
...@@ -1488,12 +1480,12 @@ class WasmDecoder : public Decoder { ...@@ -1488,12 +1480,12 @@ class WasmDecoder : public Decoder {
case kExprIf: // fall through case kExprIf: // fall through
case kExprLoop: case kExprLoop:
case kExprBlock: { case kExprBlock: {
BlockTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc); BlockTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprLet: { case kExprLet: {
BlockTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc); BlockTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc + 1);
uint32_t locals_length; uint32_t locals_length;
bool locals_result = bool locals_result =
decoder->DecodeLocals(decoder->pc() + 1 + imm.length, decoder->DecodeLocals(decoder->pc() + 1 + imm.length,
...@@ -1502,57 +1494,57 @@ class WasmDecoder : public Decoder { ...@@ -1502,57 +1494,57 @@ class WasmDecoder : public Decoder {
} }
case kExprThrow: { case kExprThrow: {
ExceptionIndexImmediate<validate> imm(decoder, pc); ExceptionIndexImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprBrOnExn: { case kExprBrOnExn: {
BranchOnExceptionImmediate<validate> imm(decoder, pc); BranchOnExceptionImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprBrOnNull: { case kExprBrOnNull: {
BranchDepthImmediate<validate> imm(decoder, pc); BranchDepthImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprLocalGet: case kExprLocalGet:
case kExprLocalSet: case kExprLocalSet:
case kExprLocalTee: { case kExprLocalTee: {
LocalIndexImmediate<validate> imm(decoder, pc); LocalIndexImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprSelectWithType: { case kExprSelectWithType: {
SelectTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc); SelectTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprBrTable: { case kExprBrTable: {
BranchTableImmediate<validate> imm(decoder, pc); BranchTableImmediate<validate> imm(decoder, pc + 1);
BranchTableIterator<validate> iterator(decoder, imm); BranchTableIterator<validate> iterator(decoder, imm);
return 1 + iterator.length(); return 1 + iterator.length();
} }
case kExprI32Const: { case kExprI32Const: {
ImmI32Immediate<validate> imm(decoder, pc); ImmI32Immediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprI64Const: { case kExprI64Const: {
ImmI64Immediate<validate> imm(decoder, pc); ImmI64Immediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprRefNull: { case kExprRefNull: {
HeapTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc); HeapTypeImmediate<validate> imm(WasmFeatures::All(), decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprRefIsNull: { case kExprRefIsNull: {
return 1; return 1;
} }
case kExprRefFunc: { case kExprRefFunc: {
FunctionIndexImmediate<validate> imm(decoder, pc); FunctionIndexImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprMemoryGrow: case kExprMemoryGrow:
case kExprMemorySize: { case kExprMemorySize: {
MemoryIndexImmediate<validate> imm(decoder, pc); MemoryIndexImmediate<validate> imm(decoder, pc + 1);
return 1 + imm.length; return 1 + imm.length;
} }
case kExprF32Const: case kExprF32Const:
...@@ -1575,37 +1567,37 @@ class WasmDecoder : public Decoder { ...@@ -1575,37 +1567,37 @@ class WasmDecoder : public Decoder {
case kExprI64UConvertSatF64: case kExprI64UConvertSatF64:
return 2; return 2;
case kExprMemoryInit: { case kExprMemoryInit: {
MemoryInitImmediate<validate> imm(decoder, pc); MemoryInitImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
case kExprDataDrop: { case kExprDataDrop: {
DataDropImmediate<validate> imm(decoder, pc); DataDropImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
case kExprMemoryCopy: { case kExprMemoryCopy: {
MemoryCopyImmediate<validate> imm(decoder, pc); MemoryCopyImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
case kExprMemoryFill: { case kExprMemoryFill: {
MemoryIndexImmediate<validate> imm(decoder, pc + 1); MemoryIndexImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
case kExprTableInit: { case kExprTableInit: {
TableInitImmediate<validate> imm(decoder, pc); TableInitImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
case kExprElemDrop: { case kExprElemDrop: {
ElemDropImmediate<validate> imm(decoder, pc); ElemDropImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
case kExprTableCopy: { case kExprTableCopy: {
TableCopyImmediate<validate> imm(decoder, pc); TableCopyImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
case kExprTableGrow: case kExprTableGrow:
case kExprTableSize: case kExprTableSize:
case kExprTableFill: { case kExprTableFill: {
TableIndexImmediate<validate> imm(decoder, pc); TableIndexImmediate<validate> imm(decoder, pc + 2);
return 2 + imm.length; return 2 + imm.length;
} }
default: default:
...@@ -1629,7 +1621,7 @@ class WasmDecoder : public Decoder { ...@@ -1629,7 +1621,7 @@ class WasmDecoder : public Decoder {
FOREACH_SIMD_MEM_OPCODE(DECLARE_OPCODE_CASE) FOREACH_SIMD_MEM_OPCODE(DECLARE_OPCODE_CASE)
#undef DECLARE_OPCODE_CASE #undef DECLARE_OPCODE_CASE
{ {
MemoryAccessImmediate<validate> imm(decoder, pc + length, MemoryAccessImmediate<validate> imm(decoder, pc + length + 1,
UINT32_MAX); UINT32_MAX);
return 1 + length + imm.length; return 1 + length + imm.length;
} }
...@@ -1650,7 +1642,7 @@ class WasmDecoder : public Decoder { ...@@ -1650,7 +1642,7 @@ class WasmDecoder : public Decoder {
FOREACH_ATOMIC_OPCODE(DECLARE_OPCODE_CASE) FOREACH_ATOMIC_OPCODE(DECLARE_OPCODE_CASE)
#undef DECLARE_OPCODE_CASE #undef DECLARE_OPCODE_CASE
{ {
MemoryAccessImmediate<validate> imm(decoder, pc + 1, UINT32_MAX); MemoryAccessImmediate<validate> imm(decoder, pc + 2, UINT32_MAX);
return 2 + imm.length; return 2 + imm.length;
} }
#define DECLARE_OPCODE_CASE(name, opcode, sig) case kExpr##name: #define DECLARE_OPCODE_CASE(name, opcode, sig) case kExpr##name:
...@@ -1768,20 +1760,20 @@ class WasmDecoder : public Decoder { ...@@ -1768,20 +1760,20 @@ class WasmDecoder : public Decoder {
case kExprMemorySize: case kExprMemorySize:
return {0, 1}; return {0, 1};
case kExprCallFunction: { case kExprCallFunction: {
CallFunctionImmediate<validate> imm(this, pc); CallFunctionImmediate<validate> imm(this, pc + 1);
CHECK(Complete(pc, imm)); CHECK(Complete(imm));
return {imm.sig->parameter_count(), imm.sig->return_count()}; return {imm.sig->parameter_count(), imm.sig->return_count()};
} }
case kExprCallIndirect: { case kExprCallIndirect: {
CallIndirectImmediate<validate> imm(this->enabled_, this, pc); CallIndirectImmediate<validate> imm(this->enabled_, this, pc + 1);
CHECK(Complete(pc, imm)); CHECK(Complete(imm));
// Indirect calls pop an additional argument for the table index. // Indirect calls pop an additional argument for the table index.
return {imm.sig->parameter_count() + 1, return {imm.sig->parameter_count() + 1,
imm.sig->return_count()}; imm.sig->return_count()};
} }
case kExprThrow: { case kExprThrow: {
ExceptionIndexImmediate<validate> imm(this, pc); ExceptionIndexImmediate<validate> imm(this, pc + 1);
CHECK(Complete(pc, imm)); CHECK(Complete(imm));
DCHECK_EQ(0, imm.exception->sig->return_count()); DCHECK_EQ(0, imm.exception->sig->return_count());
return {imm.exception->sig->parameter_count(), 0}; return {imm.exception->sig->parameter_count(), 0};
} }
...@@ -2064,20 +2056,21 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2064,20 +2056,21 @@ class WasmFullDecoder : public WasmDecoder<validate> {
if (decoder_->failed()) continue; if (decoder_->failed()) continue;
switch (val_opcode) { switch (val_opcode) {
case kExprI32Const: { case kExprI32Const: {
ImmI32Immediate<Decoder::kNoValidate> imm(decoder_, val.pc); ImmI32Immediate<Decoder::kNoValidate> imm(decoder_, val.pc + 1);
Append("[%d]", imm.value); Append("[%d]", imm.value);
break; break;
} }
case kExprLocalGet: case kExprLocalGet:
case kExprLocalSet: case kExprLocalSet:
case kExprLocalTee: { case kExprLocalTee: {
LocalIndexImmediate<Decoder::kNoValidate> imm(decoder_, val.pc); LocalIndexImmediate<Decoder::kNoValidate> imm(decoder_, val.pc + 1);
Append("[%u]", imm.index); Append("[%u]", imm.index);
break; break;
} }
case kExprGlobalGet: case kExprGlobalGet:
case kExprGlobalSet: { case kExprGlobalSet: {
GlobalIndexImmediate<Decoder::kNoValidate> imm(decoder_, val.pc); GlobalIndexImmediate<Decoder::kNoValidate> imm(decoder_,
val.pc + 1);
Append("[%u]", imm.index); Append("[%u]", imm.index);
break; break;
} }
...@@ -2131,14 +2124,14 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2131,14 +2124,14 @@ class WasmFullDecoder : public WasmDecoder<validate> {
case kExprNop: case kExprNop:
break; break;
case kExprBlock: { case kExprBlock: {
BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_); BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 1);
if (!this->Validate(imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
ArgVector args = PopArgs(imm.sig); ArgVector args = PopArgs(imm.sig);
Control* block = PushControl(kControlBlock); Control* block = PushControl(kControlBlock);
SetBlockType(block, imm, args.begin()); SetBlockType(block, imm, args.begin());
CALL_INTERFACE_IF_REACHABLE(Block, block); CALL_INTERFACE_IF_REACHABLE(Block, block);
PushMergeValues(block, &block->start_merge); PushMergeValues(block, &block->start_merge);
len = 1 + imm.length;
break; break;
} }
case kExprRethrow: { case kExprRethrow: {
...@@ -2150,9 +2143,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2150,9 +2143,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprThrow: { case kExprThrow: {
CHECK_PROTOTYPE_OPCODE(eh); CHECK_PROTOTYPE_OPCODE(eh);
ExceptionIndexImmediate<validate> imm(this, this->pc_); ExceptionIndexImmediate<validate> imm(this, this->pc_ + 1);
len = 1 + imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 1, imm)) break;
ArgVector args = PopArgs(imm.exception->ToFunctionSig()); ArgVector args = PopArgs(imm.exception->ToFunctionSig());
CALL_INTERFACE_IF_REACHABLE(Throw, imm, VectorOf(args)); CALL_INTERFACE_IF_REACHABLE(Throw, imm, VectorOf(args));
EndControl(); EndControl();
...@@ -2160,12 +2153,12 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2160,12 +2153,12 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprTry: { case kExprTry: {
CHECK_PROTOTYPE_OPCODE(eh); CHECK_PROTOTYPE_OPCODE(eh);
BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_); BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 1);
if (!this->Validate(imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
ArgVector args = PopArgs(imm.sig); ArgVector args = PopArgs(imm.sig);
Control* try_block = PushControl(kControlTry); Control* try_block = PushControl(kControlTry);
SetBlockType(try_block, imm, args.begin()); SetBlockType(try_block, imm, args.begin());
len = 1 + imm.length;
CALL_INTERFACE_IF_REACHABLE(Try, try_block); CALL_INTERFACE_IF_REACHABLE(Try, try_block);
PushMergeValues(try_block, &try_block->start_merge); PushMergeValues(try_block, &try_block->start_merge);
break; break;
...@@ -2196,9 +2189,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2196,9 +2189,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprBrOnExn: { case kExprBrOnExn: {
CHECK_PROTOTYPE_OPCODE(eh); CHECK_PROTOTYPE_OPCODE(eh);
BranchOnExceptionImmediate<validate> imm(this, this->pc_); BranchOnExceptionImmediate<validate> imm(this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm.depth, control_.size())) break; len += imm.length;
if (!this->Validate(this->pc_ + imm.depth.length, imm.index)) break; if (!this->Validate(this->pc() + 1, imm, control_.size())) break;
Control* c = control_at(imm.depth.depth); Control* c = control_at(imm.depth.depth);
Value exception = Pop(0, kWasmExnRef); Value exception = Pop(0, kWasmExnRef);
const WasmExceptionSig* sig = imm.index.exception->sig; const WasmExceptionSig* sig = imm.index.exception->sig;
...@@ -2218,7 +2211,6 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2218,7 +2211,6 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} else if (check_result == kInvalidStack) { } else if (check_result == kInvalidStack) {
break; break;
} }
len = 1 + imm.length;
for (size_t i = 0; i < value_count; ++i) Pop(); for (size_t i = 0; i < value_count; ++i) Pop();
Value* pexception = Push(kWasmExnRef); Value* pexception = Push(kWasmExnRef);
*pexception = exception; *pexception = exception;
...@@ -2226,9 +2218,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2226,9 +2218,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprBrOnNull: { case kExprBrOnNull: {
CHECK_PROTOTYPE_OPCODE(typed_funcref); CHECK_PROTOTYPE_OPCODE(typed_funcref);
BranchDepthImmediate<validate> imm(this, this->pc_); BranchDepthImmediate<validate> imm(this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm, control_.size())) break; len += imm.length;
len = 1 + imm.length; if (!this->Validate(this->pc_ + len, imm, control_.size())) break;
Value ref_object = Pop(); Value ref_object = Pop();
if (this->failed()) break; if (this->failed()) break;
Control* c = control_at(imm.depth); Control* c = control_at(imm.depth);
...@@ -2261,17 +2253,17 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2261,17 +2253,17 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprLet: { case kExprLet: {
CHECK_PROTOTYPE_OPCODE(typed_funcref); CHECK_PROTOTYPE_OPCODE(typed_funcref);
BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_); BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + len);
if (!this->Validate(imm)) break; if (!this->Validate(this->pc_ + len, imm)) break;
uint32_t old_local_count = this->num_locals(); uint32_t old_local_count = this->num_locals();
// Temporarily add the let-defined values // Temporarily add the let-defined values
// to the beginning of the function locals. // to the beginning of the function locals.
uint32_t locals_length; uint32_t locals_length;
if (!this->DecodeLocals(this->pc() + 1 + imm.length, &locals_length, if (!this->DecodeLocals(this->pc() + len + imm.length, &locals_length,
0)) { 0)) {
break; break;
} }
len = 1 + imm.length + locals_length; len += imm.length + locals_length;
uint32_t num_added_locals = this->num_locals() - old_local_count; uint32_t num_added_locals = this->num_locals() - old_local_count;
ArgVector let_local_values = ArgVector let_local_values =
PopArgs(static_cast<uint32_t>(imm.in_arity()), PopArgs(static_cast<uint32_t>(imm.in_arity()),
...@@ -2285,26 +2277,26 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2285,26 +2277,26 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprLoop: { case kExprLoop: {
BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_); BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 1);
if (!this->Validate(imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
ArgVector args = PopArgs(imm.sig); ArgVector args = PopArgs(imm.sig);
Control* block = PushControl(kControlLoop); Control* block = PushControl(kControlLoop);
SetBlockType(&control_.back(), imm, args.begin()); SetBlockType(&control_.back(), imm, args.begin());
len = 1 + imm.length;
CALL_INTERFACE_IF_REACHABLE(Loop, block); CALL_INTERFACE_IF_REACHABLE(Loop, block);
PushMergeValues(block, &block->start_merge); PushMergeValues(block, &block->start_merge);
break; break;
} }
case kExprIf: { case kExprIf: {
BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_); BlockTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 1);
if (!this->Validate(imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
Value cond = Pop(0, kWasmI32); Value cond = Pop(0, kWasmI32);
ArgVector args = PopArgs(imm.sig); ArgVector args = PopArgs(imm.sig);
if (!VALIDATE(this->ok())) break; if (!VALIDATE(this->ok())) break;
Control* if_block = PushControl(kControlIf); Control* if_block = PushControl(kControlIf);
SetBlockType(if_block, imm, args.begin()); SetBlockType(if_block, imm, args.begin());
CALL_INTERFACE_IF_REACHABLE(If, cond, if_block); CALL_INTERFACE_IF_REACHABLE(If, cond, if_block);
len = 1 + imm.length;
PushMergeValues(if_block, &if_block->start_merge); PushMergeValues(if_block, &if_block->start_merge);
break; break;
} }
...@@ -2389,19 +2381,20 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2389,19 +2381,20 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprSelectWithType: { case kExprSelectWithType: {
CHECK_PROTOTYPE_OPCODE(reftypes); CHECK_PROTOTYPE_OPCODE(reftypes);
SelectTypeImmediate<validate> imm(this->enabled_, this, this->pc_); SelectTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 1);
len += imm.length;
if (this->failed()) break; if (this->failed()) break;
Value cond = Pop(2, kWasmI32); Value cond = Pop(2, kWasmI32);
Value fval = Pop(1, imm.type); Value fval = Pop(1, imm.type);
Value tval = Pop(0, imm.type); Value tval = Pop(0, imm.type);
Value* result = Push(imm.type); Value* result = Push(imm.type);
CALL_INTERFACE_IF_REACHABLE(Select, cond, fval, tval, result); CALL_INTERFACE_IF_REACHABLE(Select, cond, fval, tval, result);
len = 1 + imm.length;
break; break;
} }
case kExprBr: { case kExprBr: {
BranchDepthImmediate<validate> imm(this, this->pc_); BranchDepthImmediate<validate> imm(this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm, control_.size())) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm, control_.size())) break;
Control* c = control_at(imm.depth); Control* c = control_at(imm.depth);
TypeCheckBranchResult check_result = TypeCheckBranch(c, false); TypeCheckBranchResult check_result = TypeCheckBranch(c, false);
if (V8_LIKELY(check_result == kReachableBranch)) { if (V8_LIKELY(check_result == kReachableBranch)) {
...@@ -2414,15 +2407,15 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2414,15 +2407,15 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} else if (check_result == kInvalidStack) { } else if (check_result == kInvalidStack) {
break; break;
} }
len = 1 + imm.length;
EndControl(); EndControl();
break; break;
} }
case kExprBrIf: { case kExprBrIf: {
BranchDepthImmediate<validate> imm(this, this->pc_); BranchDepthImmediate<validate> imm(this, this->pc_ + 1);
len += imm.length;
if (!this->Validate(this->pc_ + 1, imm, control_.size())) break;
Value cond = Pop(0, kWasmI32); Value cond = Pop(0, kWasmI32);
if (this->failed()) break; if (this->failed()) break;
if (!this->Validate(this->pc_, imm, control_.size())) break;
Control* c = control_at(imm.depth); Control* c = control_at(imm.depth);
TypeCheckBranchResult check_result = TypeCheckBranch(c, true); TypeCheckBranchResult check_result = TypeCheckBranch(c, true);
if (V8_LIKELY(check_result == kReachableBranch)) { if (V8_LIKELY(check_result == kReachableBranch)) {
...@@ -2431,15 +2424,14 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2431,15 +2424,14 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} else if (check_result == kInvalidStack) { } else if (check_result == kInvalidStack) {
break; break;
} }
len = 1 + imm.length;
break; break;
} }
case kExprBrTable: { case kExprBrTable: {
BranchTableImmediate<validate> imm(this, this->pc_); BranchTableImmediate<validate> imm(this, this->pc_ + 1);
BranchTableIterator<validate> iterator(this, imm); BranchTableIterator<validate> iterator(this, imm);
Value key = Pop(0, kWasmI32); Value key = Pop(0, kWasmI32);
if (this->failed()) break; if (this->failed()) break;
if (!this->Validate(this->pc_, imm, control_.size())) break; if (!this->Validate(this->pc_ + 1, imm, control_.size())) break;
// Cache the branch targets during the iteration, so that we can set // Cache the branch targets during the iteration, so that we can set
// all branch targets as reachable after the {CALL_INTERFACE} call. // all branch targets as reachable after the {CALL_INTERFACE} call.
...@@ -2482,7 +2474,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2482,7 +2474,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
} }
len = 1 + iterator.length(); len += iterator.length();
EndControl(); EndControl();
break; break;
} }
...@@ -2509,47 +2501,46 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2509,47 +2501,46 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprI32Const: { case kExprI32Const: {
ImmI32Immediate<validate> imm(this, this->pc_); ImmI32Immediate<validate> imm(this, this->pc_ + 1);
Value* value = Push(kWasmI32); Value* value = Push(kWasmI32);
CALL_INTERFACE_IF_REACHABLE(I32Const, value, imm.value); CALL_INTERFACE_IF_REACHABLE(I32Const, value, imm.value);
len = 1 + imm.length; len += imm.length;
break; break;
} }
case kExprI64Const: { case kExprI64Const: {
ImmI64Immediate<validate> imm(this, this->pc_); ImmI64Immediate<validate> imm(this, this->pc_ + 1);
Value* value = Push(kWasmI64); Value* value = Push(kWasmI64);
CALL_INTERFACE_IF_REACHABLE(I64Const, value, imm.value); CALL_INTERFACE_IF_REACHABLE(I64Const, value, imm.value);
len = 1 + imm.length; len += imm.length;
break; break;
} }
case kExprF32Const: { case kExprF32Const: {
ImmF32Immediate<validate> imm(this, this->pc_); ImmF32Immediate<validate> imm(this, this->pc_ + 1);
Value* value = Push(kWasmF32); Value* value = Push(kWasmF32);
CALL_INTERFACE_IF_REACHABLE(F32Const, value, imm.value); CALL_INTERFACE_IF_REACHABLE(F32Const, value, imm.value);
len = 1 + imm.length; len += imm.length;
break; break;
} }
case kExprF64Const: { case kExprF64Const: {
ImmF64Immediate<validate> imm(this, this->pc_); ImmF64Immediate<validate> imm(this, this->pc_ + 1);
Value* value = Push(kWasmF64); Value* value = Push(kWasmF64);
CALL_INTERFACE_IF_REACHABLE(F64Const, value, imm.value); CALL_INTERFACE_IF_REACHABLE(F64Const, value, imm.value);
len = 1 + imm.length; len += imm.length;
break; break;
} }
case kExprRefNull: { case kExprRefNull: {
CHECK_PROTOTYPE_OPCODE(reftypes); CHECK_PROTOTYPE_OPCODE(reftypes);
HeapTypeImmediate<validate> imm(this->enabled_, this, this->pc_); HeapTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
Value* value = Push(ValueType::Ref(imm.type, kNullable)); Value* value = Push(ValueType::Ref(imm.type, kNullable));
CALL_INTERFACE_IF_REACHABLE(RefNull, value); CALL_INTERFACE_IF_REACHABLE(RefNull, value);
len = 1 + imm.length;
break; break;
} }
case kExprRefIsNull: { case kExprRefIsNull: {
CHECK_PROTOTYPE_OPCODE(reftypes); CHECK_PROTOTYPE_OPCODE(reftypes);
Value value = Pop(); Value value = Pop();
Value* result = Push(kWasmI32); Value* result = Push(kWasmI32);
len = 1;
if (value.type.is_nullable()) { if (value.type.is_nullable()) {
CALL_INTERFACE_IF_REACHABLE(UnOp, opcode, value, result); CALL_INTERFACE_IF_REACHABLE(UnOp, opcode, value, result);
break; break;
...@@ -2560,18 +2551,18 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2560,18 +2551,18 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
this->errorf(this->pc_, this->errorf(this->pc_,
"invalid argument type to ref.is_null. Expected " "invalid argument type to ref.is_null. Expected reference "
"reference type, got %s", "type, got %s",
value.type.type_name().c_str()); value.type.type_name().c_str());
break; break;
} }
case kExprRefFunc: { case kExprRefFunc: {
CHECK_PROTOTYPE_OPCODE(reftypes); CHECK_PROTOTYPE_OPCODE(reftypes);
FunctionIndexImmediate<validate> imm(this, this->pc_); FunctionIndexImmediate<validate> imm(this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
Value* value = Push(ValueType::Ref(kHeapFunc, kNonNullable)); Value* value = Push(ValueType::Ref(kHeapFunc, kNonNullable));
CALL_INTERFACE_IF_REACHABLE(RefFunc, imm.index, value); CALL_INTERFACE_IF_REACHABLE(RefFunc, imm.index, value);
len = 1 + imm.length;
break; break;
} }
case kExprRefAsNonNull: { case kExprRefAsNonNull: {
...@@ -2590,35 +2581,37 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2590,35 +2581,37 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
default: default:
this->error(this->pc_ + 1, this->errorf(this->pc_,
"invalid agrument type to ref.as_non_null"); "invalid agrument type to ref.as_non_null: Expected "
"reference type, got %s",
value.type.type_name().c_str());
break; break;
} }
break; break;
} }
case kExprLocalGet: { case kExprLocalGet: {
LocalIndexImmediate<validate> imm(this, this->pc_); LocalIndexImmediate<validate> imm(this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
Value* value = Push(this->local_type(imm.index)); Value* value = Push(this->local_type(imm.index));
CALL_INTERFACE_IF_REACHABLE(LocalGet, value, imm); CALL_INTERFACE_IF_REACHABLE(LocalGet, value, imm);
len = 1 + imm.length;
break; break;
} }
case kExprLocalSet: { case kExprLocalSet: {
LocalIndexImmediate<validate> imm(this, this->pc_); LocalIndexImmediate<validate> imm(this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
Value value = Pop(0, this->local_type(imm.index)); Value value = Pop(0, this->local_type(imm.index));
CALL_INTERFACE_IF_REACHABLE(LocalSet, value, imm); CALL_INTERFACE_IF_REACHABLE(LocalSet, value, imm);
len = 1 + imm.length;
break; break;
} }
case kExprLocalTee: { case kExprLocalTee: {
LocalIndexImmediate<validate> imm(this, this->pc_); LocalIndexImmediate<validate> imm(this, this->pc_ + 1);
if (!this->Validate(this->pc_, imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
Value value = Pop(0, this->local_type(imm.index)); Value value = Pop(0, this->local_type(imm.index));
Value* result = Push(value.type); Value* result = Push(value.type);
CALL_INTERFACE_IF_REACHABLE(LocalTee, value, result, imm); CALL_INTERFACE_IF_REACHABLE(LocalTee, value, result, imm);
len = 1 + imm.length;
break; break;
} }
case kExprDrop: { case kExprDrop: {
...@@ -2627,17 +2620,17 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2627,17 +2620,17 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprGlobalGet: { case kExprGlobalGet: {
GlobalIndexImmediate<validate> imm(this, this->pc_); GlobalIndexImmediate<validate> imm(this, this->pc_ + 1);
len = 1 + imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 1, imm)) break;
Value* result = Push(imm.type); Value* result = Push(imm.type);
CALL_INTERFACE_IF_REACHABLE(GlobalGet, result, imm); CALL_INTERFACE_IF_REACHABLE(GlobalGet, result, imm);
break; break;
} }
case kExprGlobalSet: { case kExprGlobalSet: {
GlobalIndexImmediate<validate> imm(this, this->pc_); GlobalIndexImmediate<validate> imm(this, this->pc_ + 1);
len = 1 + imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 1, imm)) break;
if (!VALIDATE(imm.global->mutability)) { if (!VALIDATE(imm.global->mutability)) {
this->errorf(this->pc_, "immutable global #%u cannot be assigned", this->errorf(this->pc_, "immutable global #%u cannot be assigned",
imm.index); imm.index);
...@@ -2649,9 +2642,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2649,9 +2642,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprTableGet: { case kExprTableGet: {
CHECK_PROTOTYPE_OPCODE(reftypes); CHECK_PROTOTYPE_OPCODE(reftypes);
TableIndexImmediate<validate> imm(this, this->pc_); TableIndexImmediate<validate> imm(this, this->pc_ + 1);
len = 1 + imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 1, imm)) break;
Value index = Pop(0, kWasmI32); Value index = Pop(0, kWasmI32);
Value* result = Push(this->module_->tables[imm.index].type); Value* result = Push(this->module_->tables[imm.index].type);
CALL_INTERFACE_IF_REACHABLE(TableGet, index, result, imm); CALL_INTERFACE_IF_REACHABLE(TableGet, index, result, imm);
...@@ -2659,9 +2652,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2659,9 +2652,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprTableSet: { case kExprTableSet: {
CHECK_PROTOTYPE_OPCODE(reftypes); CHECK_PROTOTYPE_OPCODE(reftypes);
TableIndexImmediate<validate> imm(this, this->pc_); TableIndexImmediate<validate> imm(this, this->pc_ + 1);
len = 1 + imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 1, imm)) break;
Value value = Pop(1, this->module_->tables[imm.index].type); Value value = Pop(1, this->module_->tables[imm.index].type);
Value index = Pop(0, kWasmI32); Value index = Pop(0, kWasmI32);
CALL_INTERFACE_IF_REACHABLE(TableSet, index, value, imm); CALL_INTERFACE_IF_REACHABLE(TableSet, index, value, imm);
...@@ -2669,78 +2662,78 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2669,78 +2662,78 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprI32LoadMem8S: case kExprI32LoadMem8S:
len = 1 + DecodeLoadMem(LoadType::kI32Load8S); len += DecodeLoadMem(LoadType::kI32Load8S);
break; break;
case kExprI32LoadMem8U: case kExprI32LoadMem8U:
len = 1 + DecodeLoadMem(LoadType::kI32Load8U); len += DecodeLoadMem(LoadType::kI32Load8U);
break; break;
case kExprI32LoadMem16S: case kExprI32LoadMem16S:
len = 1 + DecodeLoadMem(LoadType::kI32Load16S); len += DecodeLoadMem(LoadType::kI32Load16S);
break; break;
case kExprI32LoadMem16U: case kExprI32LoadMem16U:
len = 1 + DecodeLoadMem(LoadType::kI32Load16U); len += DecodeLoadMem(LoadType::kI32Load16U);
break; break;
case kExprI32LoadMem: case kExprI32LoadMem:
len = 1 + DecodeLoadMem(LoadType::kI32Load); len += DecodeLoadMem(LoadType::kI32Load);
break; break;
case kExprI64LoadMem8S: case kExprI64LoadMem8S:
len = 1 + DecodeLoadMem(LoadType::kI64Load8S); len += DecodeLoadMem(LoadType::kI64Load8S);
break; break;
case kExprI64LoadMem8U: case kExprI64LoadMem8U:
len = 1 + DecodeLoadMem(LoadType::kI64Load8U); len += DecodeLoadMem(LoadType::kI64Load8U);
break; break;
case kExprI64LoadMem16S: case kExprI64LoadMem16S:
len = 1 + DecodeLoadMem(LoadType::kI64Load16S); len += DecodeLoadMem(LoadType::kI64Load16S);
break; break;
case kExprI64LoadMem16U: case kExprI64LoadMem16U:
len = 1 + DecodeLoadMem(LoadType::kI64Load16U); len += DecodeLoadMem(LoadType::kI64Load16U);
break; break;
case kExprI64LoadMem32S: case kExprI64LoadMem32S:
len = 1 + DecodeLoadMem(LoadType::kI64Load32S); len += DecodeLoadMem(LoadType::kI64Load32S);
break; break;
case kExprI64LoadMem32U: case kExprI64LoadMem32U:
len = 1 + DecodeLoadMem(LoadType::kI64Load32U); len += DecodeLoadMem(LoadType::kI64Load32U);
break; break;
case kExprI64LoadMem: case kExprI64LoadMem:
len = 1 + DecodeLoadMem(LoadType::kI64Load); len += DecodeLoadMem(LoadType::kI64Load);
break; break;
case kExprF32LoadMem: case kExprF32LoadMem:
len = 1 + DecodeLoadMem(LoadType::kF32Load); len += DecodeLoadMem(LoadType::kF32Load);
break; break;
case kExprF64LoadMem: case kExprF64LoadMem:
len = 1 + DecodeLoadMem(LoadType::kF64Load); len += DecodeLoadMem(LoadType::kF64Load);
break; break;
case kExprI32StoreMem8: case kExprI32StoreMem8:
len = 1 + DecodeStoreMem(StoreType::kI32Store8); len += DecodeStoreMem(StoreType::kI32Store8);
break; break;
case kExprI32StoreMem16: case kExprI32StoreMem16:
len = 1 + DecodeStoreMem(StoreType::kI32Store16); len += DecodeStoreMem(StoreType::kI32Store16);
break; break;
case kExprI32StoreMem: case kExprI32StoreMem:
len = 1 + DecodeStoreMem(StoreType::kI32Store); len += DecodeStoreMem(StoreType::kI32Store);
break; break;
case kExprI64StoreMem8: case kExprI64StoreMem8:
len = 1 + DecodeStoreMem(StoreType::kI64Store8); len += DecodeStoreMem(StoreType::kI64Store8);
break; break;
case kExprI64StoreMem16: case kExprI64StoreMem16:
len = 1 + DecodeStoreMem(StoreType::kI64Store16); len += DecodeStoreMem(StoreType::kI64Store16);
break; break;
case kExprI64StoreMem32: case kExprI64StoreMem32:
len = 1 + DecodeStoreMem(StoreType::kI64Store32); len += DecodeStoreMem(StoreType::kI64Store32);
break; break;
case kExprI64StoreMem: case kExprI64StoreMem:
len = 1 + DecodeStoreMem(StoreType::kI64Store); len += DecodeStoreMem(StoreType::kI64Store);
break; break;
case kExprF32StoreMem: case kExprF32StoreMem:
len = 1 + DecodeStoreMem(StoreType::kF32Store); len += DecodeStoreMem(StoreType::kF32Store);
break; break;
case kExprF64StoreMem: case kExprF64StoreMem:
len = 1 + DecodeStoreMem(StoreType::kF64Store); len += DecodeStoreMem(StoreType::kF64Store);
break; break;
case kExprMemoryGrow: { case kExprMemoryGrow: {
if (!CheckHasMemory()) break; if (!CheckHasMemory()) break;
MemoryIndexImmediate<validate> imm(this, this->pc_); MemoryIndexImmediate<validate> imm(this, this->pc_ + 1);
len = 1 + imm.length; len += imm.length;
if (!VALIDATE(this->module_->origin == kWasmOrigin)) { if (!VALIDATE(this->module_->origin == kWasmOrigin)) {
this->error("grow_memory is not supported for asmjs modules"); this->error("grow_memory is not supported for asmjs modules");
break; break;
...@@ -2752,25 +2745,26 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2752,25 +2745,26 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprMemorySize: { case kExprMemorySize: {
if (!CheckHasMemory()) break; if (!CheckHasMemory()) break;
MemoryIndexImmediate<validate> imm(this, this->pc_); MemoryIndexImmediate<validate> imm(this, this->pc_ + 1);
Value* result = Push(kWasmI32); Value* result = Push(kWasmI32);
len = 1 + imm.length; len += imm.length;
CALL_INTERFACE_IF_REACHABLE(CurrentMemoryPages, result); CALL_INTERFACE_IF_REACHABLE(CurrentMemoryPages, result);
break; break;
} }
case kExprCallFunction: { case kExprCallFunction: {
CallFunctionImmediate<validate> imm(this, this->pc_); CallFunctionImmediate<validate> imm(this, this->pc_ + 1);
len = 1 + imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 1, imm)) break;
ArgVector args = PopArgs(imm.sig); ArgVector args = PopArgs(imm.sig);
Value* returns = PushReturns(imm.sig); Value* returns = PushReturns(imm.sig);
CALL_INTERFACE_IF_REACHABLE(CallDirect, imm, args.begin(), returns); CALL_INTERFACE_IF_REACHABLE(CallDirect, imm, args.begin(), returns);
break; break;
} }
case kExprCallIndirect: { case kExprCallIndirect: {
CallIndirectImmediate<validate> imm(this->enabled_, this, this->pc_); CallIndirectImmediate<validate> imm(this->enabled_, this,
len = 1 + imm.length; this->pc_ + 1);
if (!this->Validate(this->pc_, imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
Value index = Pop(0, kWasmI32); Value index = Pop(0, kWasmI32);
ArgVector args = PopArgs(imm.sig); ArgVector args = PopArgs(imm.sig);
Value* returns = PushReturns(imm.sig); Value* returns = PushReturns(imm.sig);
...@@ -2780,27 +2774,25 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2780,27 +2774,25 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprReturnCall: { case kExprReturnCall: {
CHECK_PROTOTYPE_OPCODE(return_call); CHECK_PROTOTYPE_OPCODE(return_call);
CallFunctionImmediate<validate> imm(this, this->pc_ + 1);
CallFunctionImmediate<validate> imm(this, this->pc_); len += imm.length;
len = 1 + imm.length; if (!this->Validate(this->pc_ + 1, imm)) break;
if (!this->Validate(this->pc_, imm)) break;
if (!VALIDATE(this->CanReturnCall(imm.sig))) { if (!VALIDATE(this->CanReturnCall(imm.sig))) {
this->errorf(this->pc_, "%s: %s", WasmOpcodes::OpcodeName(opcode), this->errorf(this->pc_, "%s: %s", WasmOpcodes::OpcodeName(opcode),
"tail call return types mismatch"); "tail call return types mismatch");
break; break;
} }
ArgVector args = PopArgs(imm.sig); ArgVector args = PopArgs(imm.sig);
CALL_INTERFACE_IF_REACHABLE(ReturnCall, imm, args.begin()); CALL_INTERFACE_IF_REACHABLE(ReturnCall, imm, args.begin());
EndControl(); EndControl();
break; break;
} }
case kExprReturnCallIndirect: { case kExprReturnCallIndirect: {
CHECK_PROTOTYPE_OPCODE(return_call); CHECK_PROTOTYPE_OPCODE(return_call);
CallIndirectImmediate<validate> imm(this->enabled_, this, this->pc_); CallIndirectImmediate<validate> imm(this->enabled_, this,
len = 1 + imm.length; this->pc_ + 1);
if (!this->Validate(this->pc_, imm)) break; len += imm.length;
if (!this->Validate(this->pc_ + 1, imm)) break;
if (!VALIDATE(this->CanReturnCall(imm.sig))) { if (!VALIDATE(this->CanReturnCall(imm.sig))) {
this->errorf(this->pc_, "%s: %s", WasmOpcodes::OpcodeName(opcode), this->errorf(this->pc_, "%s: %s", WasmOpcodes::OpcodeName(opcode),
"tail call return types mismatch"); "tail call return types mismatch");
...@@ -2814,7 +2806,6 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2814,7 +2806,6 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kNumericPrefix: { case kNumericPrefix: {
++len;
byte numeric_index = byte numeric_index =
this->template read_u8<validate>(this->pc_ + 1, "numeric index"); this->template read_u8<validate>(this->pc_ + 1, "numeric index");
WasmOpcode full_opcode = WasmOpcode full_opcode =
...@@ -2827,7 +2818,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2827,7 +2818,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
trace_msg.Append(TRACE_INST_FORMAT, startrel(this->pc_), trace_msg.Append(TRACE_INST_FORMAT, startrel(this->pc_),
WasmOpcodes::OpcodeName(full_opcode)); WasmOpcodes::OpcodeName(full_opcode));
len += DecodeNumericOpcode(full_opcode); len += 1 + DecodeNumericOpcode(full_opcode);
break; break;
} }
case kSimdPrefix: { case kSimdPrefix: {
...@@ -2835,24 +2826,22 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -2835,24 +2826,22 @@ class WasmFullDecoder : public WasmDecoder<validate> {
uint32_t length = 0; uint32_t length = 0;
WasmOpcode full_opcode = WasmOpcode full_opcode =
this->template read_prefixed_opcode<validate>(this->pc_, &length); this->template read_prefixed_opcode<validate>(this->pc_, &length);
if (!VALIDATE(this->ok())) break;
len += length; len += length;
if (!VALIDATE(this->ok())) break;
trace_msg.Append(TRACE_INST_FORMAT, startrel(this->pc_), trace_msg.Append(TRACE_INST_FORMAT, startrel(this->pc_),
WasmOpcodes::OpcodeName(full_opcode)); WasmOpcodes::OpcodeName(full_opcode));
len += DecodeSimdOpcode(full_opcode, length); len += DecodeSimdOpcode(full_opcode, len);
break; break;
} }
case kAtomicPrefix: { case kAtomicPrefix: {
CHECK_PROTOTYPE_OPCODE(threads); CHECK_PROTOTYPE_OPCODE(threads);
len++;
byte atomic_index = byte atomic_index =
this->template read_u8<validate>(this->pc_ + 1, "atomic index"); this->template read_u8<validate>(this->pc_ + 1, "atomic index");
WasmOpcode full_opcode = WasmOpcode full_opcode =
static_cast<WasmOpcode>(opcode << 8 | atomic_index); static_cast<WasmOpcode>(opcode << 8 | atomic_index);
trace_msg.Append(TRACE_INST_FORMAT, startrel(this->pc_), trace_msg.Append(TRACE_INST_FORMAT, startrel(this->pc_),
WasmOpcodes::OpcodeName(full_opcode)); WasmOpcodes::OpcodeName(full_opcode));
len += DecodeAtomicOpcode(full_opcode); len += 1 + DecodeAtomicOpcode(full_opcode);
break; break;
} }
case kGCPrefix: { case kGCPrefix: {
...@@ -3024,7 +3013,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3024,7 +3013,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
current_code_reachable_ = control_.back().reachable(); current_code_reachable_ = control_.back().reachable();
} }
int DecodeLoadMem(LoadType type, int prefix_len = 0) { int DecodeLoadMem(LoadType type, int prefix_len = 1) {
if (!CheckHasMemory()) return 0; if (!CheckHasMemory()) return 0;
MemoryAccessImmediate<validate> imm(this, this->pc_ + prefix_len, MemoryAccessImmediate<validate> imm(this, this->pc_ + prefix_len,
type.size_log_2()); type.size_log_2());
...@@ -3049,7 +3038,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3049,7 +3038,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
return imm.length; return imm.length;
} }
int DecodeStoreMem(StoreType store, int prefix_len = 0) { int DecodeStoreMem(StoreType store, int prefix_len = 1) {
if (!CheckHasMemory()) return 0; if (!CheckHasMemory()) return 0;
MemoryAccessImmediate<validate> imm(this, this->pc_ + prefix_len, MemoryAccessImmediate<validate> imm(this, this->pc_ + prefix_len,
store.size_log_2()); store.size_log_2());
...@@ -3154,8 +3143,8 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3154,8 +3143,8 @@ class WasmFullDecoder : public WasmDecoder<validate> {
uint32_t SimdExtractLane(WasmOpcode opcode, ValueType type, uint32_t SimdExtractLane(WasmOpcode opcode, ValueType type,
uint32_t opcode_length) { uint32_t opcode_length) {
SimdLaneImmediate<validate> imm(this, this->pc_, opcode_length); SimdLaneImmediate<validate> imm(this, this->pc_ + opcode_length);
if (this->Validate(this->pc_, opcode, imm)) { if (this->Validate(this->pc_ + opcode_length, opcode, imm)) {
Value inputs[] = {Pop(0, kWasmS128)}; Value inputs[] = {Pop(0, kWasmS128)};
Value* result = Push(type); Value* result = Push(type);
CALL_INTERFACE_IF_REACHABLE(SimdLaneOp, opcode, imm, ArrayVector(inputs), CALL_INTERFACE_IF_REACHABLE(SimdLaneOp, opcode, imm, ArrayVector(inputs),
...@@ -3166,8 +3155,8 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3166,8 +3155,8 @@ class WasmFullDecoder : public WasmDecoder<validate> {
uint32_t SimdReplaceLane(WasmOpcode opcode, ValueType type, uint32_t SimdReplaceLane(WasmOpcode opcode, ValueType type,
uint32_t opcode_length) { uint32_t opcode_length) {
SimdLaneImmediate<validate> imm(this, this->pc_, opcode_length); SimdLaneImmediate<validate> imm(this, this->pc_ + opcode_length);
if (this->Validate(this->pc_, opcode, imm)) { if (this->Validate(this->pc_ + opcode_length, opcode, imm)) {
Value inputs[2] = {UnreachableValue(this->pc_), Value inputs[2] = {UnreachableValue(this->pc_),
UnreachableValue(this->pc_)}; UnreachableValue(this->pc_)};
inputs[1] = Pop(1, type); inputs[1] = Pop(1, type);
...@@ -3180,8 +3169,8 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3180,8 +3169,8 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
uint32_t Simd8x16ShuffleOp(uint32_t opcode_length) { uint32_t Simd8x16ShuffleOp(uint32_t opcode_length) {
Simd8x16ShuffleImmediate<validate> imm(this, this->pc_, opcode_length); Simd8x16ShuffleImmediate<validate> imm(this, this->pc_ + opcode_length);
if (this->Validate(this->pc_, imm)) { if (this->Validate(this->pc_ + opcode_length, imm)) {
Value input1 = Pop(1, kWasmS128); Value input1 = Pop(1, kWasmS128);
Value input0 = Pop(0, kWasmS128); Value input0 = Pop(0, kWasmS128);
Value* result = Push(kWasmS128); Value* result = Push(kWasmS128);
...@@ -3317,9 +3306,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3317,9 +3306,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
uint32_t len = 2; uint32_t len = 2;
switch (opcode) { switch (opcode) {
case kExprStructNew: { case kExprStructNew: {
StructIndexImmediate<validate> imm(this, this->pc_ + len); StructIndexImmediate<validate> imm(this, this->pc_ + 2);
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 2, imm)) break;
ArgVector args = PopArgs(imm.struct_type); ArgVector args = PopArgs(imm.struct_type);
Value* value = Push( Value* value = Push(
ValueType::Ref(static_cast<HeapType>(imm.index), kNonNullable)); ValueType::Ref(static_cast<HeapType>(imm.index), kNonNullable));
...@@ -3327,8 +3316,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3327,8 +3316,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprStructGet: { case kExprStructGet: {
FieldIndexImmediate<validate> field(this, this->pc_ + len); FieldIndexImmediate<validate> field(this, this->pc_ + 2);
if (!this->Validate(this->pc_ + len, field)) break; len += field.length;
if (!this->Validate(this->pc_ + 2, field)) break;
ValueType field_type = ValueType field_type =
field.struct_index.struct_type->field(field.index); field.struct_index.struct_type->field(field.index);
if (!VALIDATE(!field_type.is_packed())) { if (!VALIDATE(!field_type.is_packed())) {
...@@ -3337,7 +3327,6 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3337,7 +3327,6 @@ class WasmFullDecoder : public WasmDecoder<validate> {
"Use struct.get_s or struct.get_u instead."); "Use struct.get_s or struct.get_u instead.");
break; break;
} }
len += field.length;
Value struct_obj = Pop( Value struct_obj = Pop(
0, ValueType::Ref(static_cast<HeapType>(field.struct_index.index), 0, ValueType::Ref(static_cast<HeapType>(field.struct_index.index),
kNullable)); kNullable));
...@@ -3347,9 +3336,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3347,9 +3336,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprStructGetU: case kExprStructGetU:
case kExprStructGetS: { case kExprStructGetS: {
FieldIndexImmediate<validate> field(this, this->pc_ + len); FieldIndexImmediate<validate> field(this, this->pc_ + 2);
if (!this->Validate(this->pc_ + len, field)) break;
len += field.length; len += field.length;
if (!this->Validate(this->pc_ + 2, field)) break;
ValueType field_type = ValueType field_type =
field.struct_index.struct_type->field(field.index); field.struct_index.struct_type->field(field.index);
if (!VALIDATE(field_type.is_packed())) { if (!VALIDATE(field_type.is_packed())) {
...@@ -3368,9 +3357,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3368,9 +3357,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprStructSet: { case kExprStructSet: {
FieldIndexImmediate<validate> field(this, this->pc_ + len); FieldIndexImmediate<validate> field(this, this->pc_ + 2);
if (!this->Validate(this->pc_ + len, field)) break;
len += field.length; len += field.length;
if (!this->Validate(this->pc_ + 2, field)) break;
const StructType* struct_type = field.struct_index.struct_type; const StructType* struct_type = field.struct_index.struct_type;
if (!VALIDATE(struct_type->mutability(field.index))) { if (!VALIDATE(struct_type->mutability(field.index))) {
this->error(this->pc_, "setting immutable struct field"); this->error(this->pc_, "setting immutable struct field");
...@@ -3384,9 +3373,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3384,9 +3373,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprArrayNew: { case kExprArrayNew: {
ArrayIndexImmediate<validate> imm(this, this->pc_ + len); ArrayIndexImmediate<validate> imm(this, this->pc_ + 2);
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_, imm)) break; if (!this->Validate(this->pc_ + 2, imm)) break;
Value length = Pop(1, kWasmI32); Value length = Pop(1, kWasmI32);
Value initial_value = Pop(0, imm.array_type->element_type().Unpacked()); Value initial_value = Pop(0, imm.array_type->element_type().Unpacked());
Value* value = Push( Value* value = Push(
...@@ -3397,9 +3386,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3397,9 +3386,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
case kExprArrayGetS: case kExprArrayGetS:
case kExprArrayGetU: { case kExprArrayGetU: {
ArrayIndexImmediate<validate> imm(this, this->pc_ + len); ArrayIndexImmediate<validate> imm(this, this->pc_ + 2);
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + len, imm)) break; if (!this->Validate(this->pc_ + 2, imm)) break;
if (!VALIDATE(imm.array_type->element_type().is_packed())) { if (!VALIDATE(imm.array_type->element_type().is_packed())) {
this->errorf(this->pc_, this->errorf(this->pc_,
"%s is only valid for packed arrays. " "%s is only valid for packed arrays. "
...@@ -3417,9 +3406,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3417,9 +3406,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprArrayGet: { case kExprArrayGet: {
ArrayIndexImmediate<validate> imm(this, this->pc_ + len); ArrayIndexImmediate<validate> imm(this, this->pc_ + 2);
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + len, imm)) break; if (!this->Validate(this->pc_ + 2, imm)) break;
if (!VALIDATE(!imm.array_type->element_type().is_packed())) { if (!VALIDATE(!imm.array_type->element_type().is_packed())) {
this->error(this->pc_, this->error(this->pc_,
"array.get used with a field of packed type. " "array.get used with a field of packed type. "
...@@ -3436,9 +3425,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3436,9 +3425,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprArraySet: { case kExprArraySet: {
ArrayIndexImmediate<validate> imm(this, this->pc_ + len); ArrayIndexImmediate<validate> imm(this, this->pc_ + 2);
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + len, imm)) break; if (!this->Validate(this->pc_ + 2, imm)) break;
if (!VALIDATE(imm.array_type->mutability())) { if (!VALIDATE(imm.array_type->mutability())) {
this->error(this->pc_, "setting element of immutable array"); this->error(this->pc_, "setting element of immutable array");
break; break;
...@@ -3452,9 +3441,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3452,9 +3441,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprArrayLen: { case kExprArrayLen: {
ArrayIndexImmediate<validate> imm(this, this->pc_ + len); ArrayIndexImmediate<validate> imm(this, this->pc_ + 2);
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + len, imm)) break; if (!this->Validate(this->pc_ + 2, imm)) break;
Value array_obj = Value array_obj =
Pop(0, ValueType::Ref(static_cast<HeapType>(imm.index), kNullable)); Pop(0, ValueType::Ref(static_cast<HeapType>(imm.index), kNullable));
Value* value = Push(kWasmI32); Value* value = Push(kWasmI32);
...@@ -3462,9 +3451,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3462,9 +3451,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprRttCanon: { case kExprRttCanon: {
HeapTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 1); HeapTypeImmediate<validate> imm(this->enabled_, this, this->pc_ + 2);
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + len, imm)) break; if (!this->Validate(this->pc_ + 2, imm)) break;
Value* value = Push(ValueType::Rtt(imm.type, 1)); Value* value = Push(ValueType::Rtt(imm.type, 1));
CALL_INTERFACE_IF_REACHABLE(RttCanon, imm, value); CALL_INTERFACE_IF_REACHABLE(RttCanon, imm, value);
break; break;
...@@ -3517,7 +3506,7 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3517,7 +3506,7 @@ class WasmFullDecoder : public WasmDecoder<validate> {
} }
if (!CheckHasMemoryForAtomics()) return 0; if (!CheckHasMemoryForAtomics()) return 0;
MemoryAccessImmediate<validate> imm( MemoryAccessImmediate<validate> imm(
this, this->pc_ + 1, ElementSizeLog2Of(memtype.representation())); this, this->pc_ + 2, ElementSizeLog2Of(memtype.representation()));
len += imm.length; len += imm.length;
ArgVector args = PopArgs(sig); ArgVector args = PopArgs(sig);
Value* result = ret_type == kWasmStmt ? nullptr : Push(GetReturnType(sig)); Value* result = ret_type == kWasmStmt ? nullptr : Push(GetReturnType(sig));
...@@ -3544,9 +3533,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3544,9 +3533,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
BuildSimpleOperator(opcode, sig); BuildSimpleOperator(opcode, sig);
break; break;
case kExprMemoryInit: { case kExprMemoryInit: {
MemoryInitImmediate<validate> imm(this, this->pc_); MemoryInitImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
Value size = Pop(2, sig->GetParam(2)); Value size = Pop(2, sig->GetParam(2));
Value src = Pop(1, sig->GetParam(1)); Value src = Pop(1, sig->GetParam(1));
Value dst = Pop(0, sig->GetParam(0)); Value dst = Pop(0, sig->GetParam(0));
...@@ -3554,16 +3543,16 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3554,16 +3543,16 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprDataDrop: { case kExprDataDrop: {
DataDropImmediate<validate> imm(this, this->pc_); DataDropImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
CALL_INTERFACE_IF_REACHABLE(DataDrop, imm); CALL_INTERFACE_IF_REACHABLE(DataDrop, imm);
break; break;
} }
case kExprMemoryCopy: { case kExprMemoryCopy: {
MemoryCopyImmediate<validate> imm(this, this->pc_); MemoryCopyImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
Value size = Pop(2, sig->GetParam(2)); Value size = Pop(2, sig->GetParam(2));
Value src = Pop(1, sig->GetParam(1)); Value src = Pop(1, sig->GetParam(1));
Value dst = Pop(0, sig->GetParam(0)); Value dst = Pop(0, sig->GetParam(0));
...@@ -3571,9 +3560,9 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3571,9 +3560,9 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprMemoryFill: { case kExprMemoryFill: {
MemoryIndexImmediate<validate> imm(this, this->pc_ + 1); MemoryIndexImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(this->pc_ + 1, imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
Value size = Pop(2, sig->GetParam(2)); Value size = Pop(2, sig->GetParam(2));
Value value = Pop(1, sig->GetParam(1)); Value value = Pop(1, sig->GetParam(1));
Value dst = Pop(0, sig->GetParam(0)); Value dst = Pop(0, sig->GetParam(0));
...@@ -3581,32 +3570,32 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3581,32 +3570,32 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprTableInit: { case kExprTableInit: {
TableInitImmediate<validate> imm(this, this->pc_); TableInitImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
ArgVector args = PopArgs(sig); ArgVector args = PopArgs(sig);
CALL_INTERFACE_IF_REACHABLE(TableInit, imm, VectorOf(args)); CALL_INTERFACE_IF_REACHABLE(TableInit, imm, VectorOf(args));
break; break;
} }
case kExprElemDrop: { case kExprElemDrop: {
ElemDropImmediate<validate> imm(this, this->pc_); ElemDropImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
CALL_INTERFACE_IF_REACHABLE(ElemDrop, imm); CALL_INTERFACE_IF_REACHABLE(ElemDrop, imm);
break; break;
} }
case kExprTableCopy: { case kExprTableCopy: {
TableCopyImmediate<validate> imm(this, this->pc_); TableCopyImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
ArgVector args = PopArgs(sig); ArgVector args = PopArgs(sig);
CALL_INTERFACE_IF_REACHABLE(TableCopy, imm, VectorOf(args)); CALL_INTERFACE_IF_REACHABLE(TableCopy, imm, VectorOf(args));
break; break;
} }
case kExprTableGrow: { case kExprTableGrow: {
TableIndexImmediate<validate> imm(this, this->pc_ + 1); TableIndexImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(this->pc_, imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
Value delta = Pop(1, sig->GetParam(1)); Value delta = Pop(1, sig->GetParam(1));
Value value = Pop(0, this->module_->tables[imm.index].type); Value value = Pop(0, this->module_->tables[imm.index].type);
Value* result = Push(kWasmI32); Value* result = Push(kWasmI32);
...@@ -3614,17 +3603,17 @@ class WasmFullDecoder : public WasmDecoder<validate> { ...@@ -3614,17 +3603,17 @@ class WasmFullDecoder : public WasmDecoder<validate> {
break; break;
} }
case kExprTableSize: { case kExprTableSize: {
TableIndexImmediate<validate> imm(this, this->pc_ + 1); TableIndexImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(this->pc_, imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
Value* result = Push(kWasmI32); Value* result = Push(kWasmI32);
CALL_INTERFACE_IF_REACHABLE(TableSize, imm, result); CALL_INTERFACE_IF_REACHABLE(TableSize, imm, result);
break; break;
} }
case kExprTableFill: { case kExprTableFill: {
TableIndexImmediate<validate> imm(this, this->pc_ + 1); TableIndexImmediate<validate> imm(this, this->pc_ + 2);
if (!this->Validate(this->pc_, imm)) break;
len += imm.length; len += imm.length;
if (!this->Validate(this->pc_ + 2, imm)) break;
Value count = Pop(2, sig->GetParam(2)); Value count = Pop(2, sig->GetParam(2));
Value value = Pop(1, this->module_->tables[imm.index].type); Value value = Pop(1, this->module_->tables[imm.index].type);
Value start = Pop(0, sig->GetParam(0)); Value start = Pop(0, sig->GetParam(0));
......
...@@ -244,7 +244,7 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body, ...@@ -244,7 +244,7 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
case kExprBlock: case kExprBlock:
case kExprTry: { case kExprTry: {
BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i, BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i,
i.pc()); i.pc() + 1);
os << " @" << i.pc_offset(); os << " @" << i.pc_offset();
if (decoder.Complete(imm)) { if (decoder.Complete(imm)) {
for (uint32_t i = 0; i < imm.out_arity(); i++) { for (uint32_t i = 0; i < imm.out_arity(); i++) {
...@@ -259,33 +259,33 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body, ...@@ -259,33 +259,33 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
control_depth--; control_depth--;
break; break;
case kExprBr: { case kExprBr: {
BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc()); BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
os << " depth=" << imm.depth; os << " depth=" << imm.depth;
break; break;
} }
case kExprBrIf: { case kExprBrIf: {
BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc()); BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
os << " depth=" << imm.depth; os << " depth=" << imm.depth;
break; break;
} }
case kExprBrTable: { case kExprBrTable: {
BranchTableImmediate<Decoder::kNoValidate> imm(&i, i.pc()); BranchTableImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
os << " entries=" << imm.table_count; os << " entries=" << imm.table_count;
break; break;
} }
case kExprCallIndirect: { case kExprCallIndirect: {
CallIndirectImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i, CallIndirectImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i,
i.pc()); i.pc() + 1);
os << " sig #" << imm.sig_index; os << " sig #" << imm.sig_index;
if (decoder.Complete(i.pc(), imm)) { if (decoder.Complete(imm)) {
os << ": " << *imm.sig; os << ": " << *imm.sig;
} }
break; break;
} }
case kExprCallFunction: { case kExprCallFunction: {
CallFunctionImmediate<Decoder::kNoValidate> imm(&i, i.pc()); CallFunctionImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
os << " function #" << imm.index; os << " function #" << imm.index;
if (decoder.Complete(i.pc(), imm)) { if (decoder.Complete(imm)) {
os << ": " << *imm.sig; os << ": " << *imm.sig;
} }
break; break;
......
...@@ -1610,7 +1610,7 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1610,7 +1610,7 @@ class ModuleDecoderImpl : public Decoder {
uint32_t len = 0; uint32_t len = 0;
switch (opcode) { switch (opcode) {
case kExprGlobalGet: { case kExprGlobalGet: {
GlobalIndexImmediate<Decoder::kValidate> imm(this, pc() - 1); GlobalIndexImmediate<Decoder::kValidate> imm(this, pc());
if (module->globals.size() <= imm.index) { if (module->globals.size() <= imm.index) {
error("global index is out of bounds"); error("global index is out of bounds");
expr.kind = WasmInitExpr::kNone; expr.kind = WasmInitExpr::kNone;
...@@ -1632,28 +1632,28 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1632,28 +1632,28 @@ class ModuleDecoderImpl : public Decoder {
break; break;
} }
case kExprI32Const: { case kExprI32Const: {
ImmI32Immediate<Decoder::kValidate> imm(this, pc() - 1); ImmI32Immediate<Decoder::kValidate> imm(this, pc());
expr.kind = WasmInitExpr::kI32Const; expr.kind = WasmInitExpr::kI32Const;
expr.val.i32_const = imm.value; expr.val.i32_const = imm.value;
len = imm.length; len = imm.length;
break; break;
} }
case kExprF32Const: { case kExprF32Const: {
ImmF32Immediate<Decoder::kValidate> imm(this, pc() - 1); ImmF32Immediate<Decoder::kValidate> imm(this, pc());
expr.kind = WasmInitExpr::kF32Const; expr.kind = WasmInitExpr::kF32Const;
expr.val.f32_const = imm.value; expr.val.f32_const = imm.value;
len = imm.length; len = imm.length;
break; break;
} }
case kExprI64Const: { case kExprI64Const: {
ImmI64Immediate<Decoder::kValidate> imm(this, pc() - 1); ImmI64Immediate<Decoder::kValidate> imm(this, pc());
expr.kind = WasmInitExpr::kI64Const; expr.kind = WasmInitExpr::kI64Const;
expr.val.i64_const = imm.value; expr.val.i64_const = imm.value;
len = imm.length; len = imm.length;
break; break;
} }
case kExprF64Const: { case kExprF64Const: {
ImmF64Immediate<Decoder::kValidate> imm(this, pc() - 1); ImmF64Immediate<Decoder::kValidate> imm(this, pc());
expr.kind = WasmInitExpr::kF64Const; expr.kind = WasmInitExpr::kF64Const;
expr.val.f64_const = imm.value; expr.val.f64_const = imm.value;
len = imm.length; len = imm.length;
...@@ -1662,7 +1662,7 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1662,7 +1662,7 @@ class ModuleDecoderImpl : public Decoder {
case kExprRefNull: { case kExprRefNull: {
if (enabled_features_.has_reftypes() || enabled_features_.has_eh()) { if (enabled_features_.has_reftypes() || enabled_features_.has_eh()) {
HeapTypeImmediate<Decoder::kValidate> imm(WasmFeatures::All(), this, HeapTypeImmediate<Decoder::kValidate> imm(WasmFeatures::All(), this,
pc() - 1); pc());
expr.kind = WasmInitExpr::kRefNullConst; expr.kind = WasmInitExpr::kRefNullConst;
len = imm.length; len = imm.length;
ValueType type = ValueType::Ref(imm.type, kNullable); ValueType type = ValueType::Ref(imm.type, kNullable);
...@@ -1677,7 +1677,7 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1677,7 +1677,7 @@ class ModuleDecoderImpl : public Decoder {
} }
case kExprRefFunc: { case kExprRefFunc: {
if (enabled_features_.has_reftypes()) { if (enabled_features_.has_reftypes()) {
FunctionIndexImmediate<Decoder::kValidate> imm(this, pc() - 1); FunctionIndexImmediate<Decoder::kValidate> imm(this, pc());
if (module->functions.size() <= imm.index) { if (module->functions.size() <= imm.index) {
errorf(pc() - 1, "invalid function index: %u", imm.index); errorf(pc() - 1, "invalid function index: %u", imm.index);
break; break;
...@@ -2023,8 +2023,7 @@ class ModuleDecoderImpl : public Decoder { ...@@ -2023,8 +2023,7 @@ class ModuleDecoderImpl : public Decoder {
if (failed()) return index; if (failed()) return index;
switch (opcode) { switch (opcode) {
case kExprRefNull: { case kExprRefNull: {
HeapTypeImmediate<kValidate> imm(WasmFeatures::All(), this, HeapTypeImmediate<kValidate> imm(WasmFeatures::All(), this, this->pc());
this->pc() - 1);
consume_bytes(imm.length, "ref.null immediate"); consume_bytes(imm.length, "ref.null immediate");
index = WasmElemSegment::kNullIndex; index = WasmElemSegment::kNullIndex;
break; break;
......
...@@ -797,7 +797,7 @@ class SideTable : public ZoneObject { ...@@ -797,7 +797,7 @@ class SideTable : public ZoneObject {
case kExprLoop: { case kExprLoop: {
bool is_loop = opcode == kExprLoop; bool is_loop = opcode == kExprLoop;
BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i, BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i,
i.pc()); i.pc() + 1);
if (imm.type == kWasmBottom) { if (imm.type == kWasmBottom) {
imm.sig = module->signature(imm.sig_index); imm.sig = module->signature(imm.sig_index);
} }
...@@ -819,7 +819,7 @@ class SideTable : public ZoneObject { ...@@ -819,7 +819,7 @@ class SideTable : public ZoneObject {
} }
case kExprIf: { case kExprIf: {
BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i, BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i,
i.pc()); i.pc() + 1);
if (imm.type == kWasmBottom) { if (imm.type == kWasmBottom) {
imm.sig = module->signature(imm.sig_index); imm.sig = module->signature(imm.sig_index);
} }
...@@ -859,7 +859,7 @@ class SideTable : public ZoneObject { ...@@ -859,7 +859,7 @@ class SideTable : public ZoneObject {
} }
case kExprTry: { case kExprTry: {
BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i, BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), &i,
i.pc()); i.pc() + 1);
if (imm.type == kWasmBottom) { if (imm.type == kWasmBottom) {
imm.sig = module->signature(imm.sig_index); imm.sig = module->signature(imm.sig_index);
} }
...@@ -894,7 +894,7 @@ class SideTable : public ZoneObject { ...@@ -894,7 +894,7 @@ class SideTable : public ZoneObject {
break; break;
} }
case kExprBrOnExn: { case kExprBrOnExn: {
BranchOnExceptionImmediate<Decoder::kNoValidate> imm(&i, i.pc()); BranchOnExceptionImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
uint32_t depth = imm.depth.depth; // Extracted for convenience. uint32_t depth = imm.depth.depth; // Extracted for convenience.
imm.index.exception = &module->exceptions[imm.index.index]; imm.index.exception = &module->exceptions[imm.index.index];
DCHECK_EQ(0, imm.index.exception->sig->return_count()); DCHECK_EQ(0, imm.index.exception->sig->return_count());
...@@ -923,21 +923,21 @@ class SideTable : public ZoneObject { ...@@ -923,21 +923,21 @@ class SideTable : public ZoneObject {
break; break;
} }
case kExprBr: { case kExprBr: {
BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc()); BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
TRACE("control @%u: Br[depth=%u]\n", i.pc_offset(), imm.depth); TRACE("control @%u: Br[depth=%u]\n", i.pc_offset(), imm.depth);
Control* c = &control_stack[control_stack.size() - imm.depth - 1]; Control* c = &control_stack[control_stack.size() - imm.depth - 1];
if (!unreachable) c->end_label->Ref(i.pc(), stack_height); if (!unreachable) c->end_label->Ref(i.pc(), stack_height);
break; break;
} }
case kExprBrIf: { case kExprBrIf: {
BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc()); BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
TRACE("control @%u: BrIf[depth=%u]\n", i.pc_offset(), imm.depth); TRACE("control @%u: BrIf[depth=%u]\n", i.pc_offset(), imm.depth);
Control* c = &control_stack[control_stack.size() - imm.depth - 1]; Control* c = &control_stack[control_stack.size() - imm.depth - 1];
if (!unreachable) c->end_label->Ref(i.pc(), stack_height); if (!unreachable) c->end_label->Ref(i.pc(), stack_height);
break; break;
} }
case kExprBrTable: { case kExprBrTable: {
BranchTableImmediate<Decoder::kNoValidate> imm(&i, i.pc()); BranchTableImmediate<Decoder::kNoValidate> imm(&i, i.pc() + 1);
BranchTableIterator<Decoder::kNoValidate> iterator(&i, imm); BranchTableIterator<Decoder::kNoValidate> iterator(&i, imm);
TRACE("control @%u: BrTable[count=%u]\n", i.pc_offset(), TRACE("control @%u: BrTable[count=%u]\n", i.pc_offset(),
imm.table_count); imm.table_count);
...@@ -1379,12 +1379,13 @@ class WasmInterpreterInternals { ...@@ -1379,12 +1379,13 @@ class WasmInterpreterInternals {
pc_t ReturnPc(Decoder* decoder, InterpreterCode* code, pc_t pc) { pc_t ReturnPc(Decoder* decoder, InterpreterCode* code, pc_t pc) {
switch (code->start[pc]) { switch (code->start[pc]) {
case kExprCallFunction: { case kExprCallFunction: {
CallFunctionImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc)); CallFunctionImmediate<Decoder::kNoValidate> imm(decoder,
code->at(pc + 1));
return pc + 1 + imm.length; return pc + 1 + imm.length;
} }
case kExprCallIndirect: { case kExprCallIndirect: {
CallIndirectImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), CallIndirectImmediate<Decoder::kNoValidate> imm(
decoder, code->at(pc)); WasmFeatures::All(), decoder, code->at(pc + 1));
return pc + 1 + imm.length; return pc + 1 + imm.length;
} }
default: default:
...@@ -1521,12 +1522,11 @@ class WasmInterpreterInternals { ...@@ -1521,12 +1522,11 @@ class WasmInterpreterInternals {
template <typename ctype, typename mtype> template <typename ctype, typename mtype>
bool ExecuteLoad(Decoder* decoder, InterpreterCode* code, pc_t pc, bool ExecuteLoad(Decoder* decoder, InterpreterCode* code, pc_t pc,
int* const len, MachineRepresentation rep, int* const len, MachineRepresentation rep,
int prefix_len = 0) { uint32_t prefix_len = 1) {
// Some opcodes have a prefix byte, and MemoryAccessImmediate assumes that // prefix_len is the length of the opcode, before the immediate. We don't
// the memarg is 1 byte from pc. We don't increment pc at the caller, // increment pc at the caller, because we want to keep pc to the start of
// because we want to keep pc to the start of the operation to keep trap // the operation to keep trap reporting and tracing accurate, otherwise
// reporting and tracing accurate, otherwise those will report at the middle // those will report at the middle of an opcode.
// of an opcode.
MemoryAccessImmediate<Decoder::kNoValidate> imm( MemoryAccessImmediate<Decoder::kNoValidate> imm(
decoder, code->at(pc + prefix_len), sizeof(ctype)); decoder, code->at(pc + prefix_len), sizeof(ctype));
uint32_t index = Pop().to<uint32_t>(); uint32_t index = Pop().to<uint32_t>();
...@@ -1554,12 +1554,11 @@ class WasmInterpreterInternals { ...@@ -1554,12 +1554,11 @@ class WasmInterpreterInternals {
template <typename ctype, typename mtype> template <typename ctype, typename mtype>
bool ExecuteStore(Decoder* decoder, InterpreterCode* code, pc_t pc, bool ExecuteStore(Decoder* decoder, InterpreterCode* code, pc_t pc,
int* const len, MachineRepresentation rep, int* const len, MachineRepresentation rep,
int prefix_len = 0) { uint32_t prefix_len = 1) {
// Some opcodes have a prefix byte, and MemoryAccessImmediate assumes that // prefix_len is the length of the opcode, before the immediate. We don't
// the memarg is 1 byte from pc. We don't increment pc at the caller, // increment pc at the caller, because we want to keep pc to the start of
// because we want to keep pc to the start of the operation to keep trap // the operation to keep trap reporting and tracing accurate, otherwise
// reporting and tracing accurate, otherwise those will report at the middle // those will report at the middle of an opcode.
// of an opcode.
MemoryAccessImmediate<Decoder::kNoValidate> imm( MemoryAccessImmediate<Decoder::kNoValidate> imm(
decoder, code->at(pc + prefix_len), sizeof(ctype)); decoder, code->at(pc + prefix_len), sizeof(ctype));
ctype val = Pop().to<ctype>(); ctype val = Pop().to<ctype>();
...@@ -1587,7 +1586,7 @@ class WasmInterpreterInternals { ...@@ -1587,7 +1586,7 @@ class WasmInterpreterInternals {
bool ExtractAtomicOpParams(Decoder* decoder, InterpreterCode* code, bool ExtractAtomicOpParams(Decoder* decoder, InterpreterCode* code,
Address* address, pc_t pc, int* const len, Address* address, pc_t pc, int* const len,
type* val = nullptr, type* val2 = nullptr) { type* val = nullptr, type* val2 = nullptr) {
MemoryAccessImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + 1), MemoryAccessImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + 2),
sizeof(type)); sizeof(type));
if (val2) *val2 = static_cast<type>(Pop().to<op_type>()); if (val2) *val2 = static_cast<type>(Pop().to<op_type>());
if (val) *val = static_cast<type>(Pop().to<op_type>()); if (val) *val = static_cast<type>(Pop().to<op_type>());
...@@ -1610,7 +1609,8 @@ class WasmInterpreterInternals { ...@@ -1610,7 +1609,8 @@ class WasmInterpreterInternals {
pc_t pc, int* const len, pc_t pc, int* const len,
uint32_t* buffer_offset, type* val, uint32_t* buffer_offset, type* val,
int64_t* timeout = nullptr) { int64_t* timeout = nullptr) {
MemoryAccessImmediate<Decoder::kValidate> imm(decoder, code->at(pc + 1), // TODO(manoskouk): Introduce test which exposes wrong pc offset below.
MemoryAccessImmediate<Decoder::kValidate> imm(decoder, code->at(pc + *len),
sizeof(type)); sizeof(type));
if (timeout) { if (timeout) {
*timeout = Pop().to<int64_t>(); *timeout = Pop().to<int64_t>();
...@@ -1662,7 +1662,8 @@ class WasmInterpreterInternals { ...@@ -1662,7 +1662,8 @@ class WasmInterpreterInternals {
Push(WasmValue(ExecuteI64UConvertSatF64(Pop().to<double>()))); Push(WasmValue(ExecuteI64UConvertSatF64(Pop().to<double>())));
return true; return true;
case kExprMemoryInit: { case kExprMemoryInit: {
MemoryInitImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc)); MemoryInitImmediate<Decoder::kNoValidate> imm(decoder,
code->at(pc + 2));
// The data segment index must be in bounds since it is required by // The data segment index must be in bounds since it is required by
// validation. // validation.
DCHECK_LT(imm.data_segment_index, module()->num_declared_data_segments); DCHECK_LT(imm.data_segment_index, module()->num_declared_data_segments);
...@@ -1686,7 +1687,7 @@ class WasmInterpreterInternals { ...@@ -1686,7 +1687,7 @@ class WasmInterpreterInternals {
return true; return true;
} }
case kExprDataDrop: { case kExprDataDrop: {
DataDropImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc)); DataDropImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + 2));
// The data segment index must be in bounds since it is required by // The data segment index must be in bounds since it is required by
// validation. // validation.
DCHECK_LT(imm.index, module()->num_declared_data_segments); DCHECK_LT(imm.index, module()->num_declared_data_segments);
...@@ -1695,7 +1696,8 @@ class WasmInterpreterInternals { ...@@ -1695,7 +1696,8 @@ class WasmInterpreterInternals {
return true; return true;
} }
case kExprMemoryCopy: { case kExprMemoryCopy: {
MemoryCopyImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc)); MemoryCopyImmediate<Decoder::kNoValidate> imm(decoder,
code->at(pc + 2));
*len += imm.length; *len += imm.length;
auto size = Pop().to<uint32_t>(); auto size = Pop().to<uint32_t>();
auto src = Pop().to<uint32_t>(); auto src = Pop().to<uint32_t>();
...@@ -1714,7 +1716,7 @@ class WasmInterpreterInternals { ...@@ -1714,7 +1716,7 @@ class WasmInterpreterInternals {
} }
case kExprMemoryFill: { case kExprMemoryFill: {
MemoryIndexImmediate<Decoder::kNoValidate> imm(decoder, MemoryIndexImmediate<Decoder::kNoValidate> imm(decoder,
code->at(pc + 1)); code->at(pc + 2));
*len += imm.length; *len += imm.length;
auto size = Pop().to<uint32_t>(); auto size = Pop().to<uint32_t>();
auto value = Pop().to<uint32_t>(); auto value = Pop().to<uint32_t>();
...@@ -1729,7 +1731,7 @@ class WasmInterpreterInternals { ...@@ -1729,7 +1731,7 @@ class WasmInterpreterInternals {
return true; return true;
} }
case kExprTableInit: { case kExprTableInit: {
TableInitImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc)); TableInitImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + 2));
*len += imm.length; *len += imm.length;
auto size = Pop().to<uint32_t>(); auto size = Pop().to<uint32_t>();
auto src = Pop().to<uint32_t>(); auto src = Pop().to<uint32_t>();
...@@ -1742,13 +1744,13 @@ class WasmInterpreterInternals { ...@@ -1742,13 +1744,13 @@ class WasmInterpreterInternals {
return ok; return ok;
} }
case kExprElemDrop: { case kExprElemDrop: {
ElemDropImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc)); ElemDropImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + 2));
*len += imm.length; *len += imm.length;
instance_object_->dropped_elem_segments()[imm.index] = 1; instance_object_->dropped_elem_segments()[imm.index] = 1;
return true; return true;
} }
case kExprTableCopy: { case kExprTableCopy: {
TableCopyImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc)); TableCopyImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + 2));
auto size = Pop().to<uint32_t>(); auto size = Pop().to<uint32_t>();
auto src = Pop().to<uint32_t>(); auto src = Pop().to<uint32_t>();
auto dst = Pop().to<uint32_t>(); auto dst = Pop().to<uint32_t>();
...@@ -1762,7 +1764,7 @@ class WasmInterpreterInternals { ...@@ -1762,7 +1764,7 @@ class WasmInterpreterInternals {
} }
case kExprTableGrow: { case kExprTableGrow: {
TableIndexImmediate<Decoder::kNoValidate> imm(decoder, TableIndexImmediate<Decoder::kNoValidate> imm(decoder,
code->at(pc + 1)); code->at(pc + 2));
HandleScope handle_scope(isolate_); HandleScope handle_scope(isolate_);
auto table = handle( auto table = handle(
WasmTableObject::cast(instance_object_->tables().get(imm.index)), WasmTableObject::cast(instance_object_->tables().get(imm.index)),
...@@ -1776,7 +1778,7 @@ class WasmInterpreterInternals { ...@@ -1776,7 +1778,7 @@ class WasmInterpreterInternals {
} }
case kExprTableSize: { case kExprTableSize: {
TableIndexImmediate<Decoder::kNoValidate> imm(decoder, TableIndexImmediate<Decoder::kNoValidate> imm(decoder,
code->at(pc + 1)); code->at(pc + 2));
HandleScope handle_scope(isolate_); HandleScope handle_scope(isolate_);
auto table = handle( auto table = handle(
WasmTableObject::cast(instance_object_->tables().get(imm.index)), WasmTableObject::cast(instance_object_->tables().get(imm.index)),
...@@ -1788,7 +1790,7 @@ class WasmInterpreterInternals { ...@@ -1788,7 +1790,7 @@ class WasmInterpreterInternals {
} }
case kExprTableFill: { case kExprTableFill: {
TableIndexImmediate<Decoder::kNoValidate> imm(decoder, TableIndexImmediate<Decoder::kNoValidate> imm(decoder,
code->at(pc + 1)); code->at(pc + 2));
HandleScope handle_scope(isolate_); HandleScope handle_scope(isolate_);
auto count = Pop().to<uint32_t>(); auto count = Pop().to<uint32_t>();
auto value = Pop().to_externref(); auto value = Pop().to_externref();
...@@ -2077,7 +2079,7 @@ class WasmInterpreterInternals { ...@@ -2077,7 +2079,7 @@ class WasmInterpreterInternals {
} }
bool ExecuteSimdOp(WasmOpcode opcode, Decoder* decoder, InterpreterCode* code, bool ExecuteSimdOp(WasmOpcode opcode, Decoder* decoder, InterpreterCode* code,
pc_t pc, int* const len, uint32_t opcode_length) { pc_t pc, int* const len) {
switch (opcode) { switch (opcode) {
#define SPLAT_CASE(format, sType, valType, num) \ #define SPLAT_CASE(format, sType, valType, num) \
case kExpr##format##Splat: { \ case kExpr##format##Splat: { \
...@@ -2095,16 +2097,15 @@ class WasmInterpreterInternals { ...@@ -2095,16 +2097,15 @@ class WasmInterpreterInternals {
SPLAT_CASE(I16x8, int8, int32_t, 8) SPLAT_CASE(I16x8, int8, int32_t, 8)
SPLAT_CASE(I8x16, int16, int32_t, 16) SPLAT_CASE(I8x16, int16, int32_t, 16)
#undef SPLAT_CASE #undef SPLAT_CASE
#define EXTRACT_LANE_CASE(format, name) \ #define EXTRACT_LANE_CASE(format, name) \
case kExpr##format##ExtractLane: { \ case kExpr##format##ExtractLane: { \
SimdLaneImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc), \ SimdLaneImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + *len)); \
opcode_length); \ *len += 1; \
*len += 1; \ WasmValue val = Pop(); \
WasmValue val = Pop(); \ Simd128 s = val.to_s128(); \
Simd128 s = val.to_s128(); \ auto ss = s.to_##name(); \
auto ss = s.to_##name(); \ Push(WasmValue(ss.val[LANE(imm.lane, ss)])); \
Push(WasmValue(ss.val[LANE(imm.lane, ss)])); \ return true; \
return true; \
} }
EXTRACT_LANE_CASE(F64x2, f64x2) EXTRACT_LANE_CASE(F64x2, f64x2)
EXTRACT_LANE_CASE(F32x4, f32x4) EXTRACT_LANE_CASE(F32x4, f32x4)
...@@ -2118,24 +2119,23 @@ class WasmInterpreterInternals { ...@@ -2118,24 +2119,23 @@ class WasmInterpreterInternals {
// unsigned extracts, we will cast it int8_t -> uint8_t -> uint32_t. We // unsigned extracts, we will cast it int8_t -> uint8_t -> uint32_t. We
// add the DCHECK to ensure that if the array type changes, we know to // add the DCHECK to ensure that if the array type changes, we know to
// change this function. // change this function.
#define EXTRACT_LANE_EXTEND_CASE(format, name, sign, extended_type) \ #define EXTRACT_LANE_EXTEND_CASE(format, name, sign, extended_type) \
case kExpr##format##ExtractLane##sign: { \ case kExpr##format##ExtractLane##sign: { \
SimdLaneImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc), \ SimdLaneImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + *len)); \
opcode_length); \ *len += 1; \
*len += 1; \ WasmValue val = Pop(); \
WasmValue val = Pop(); \ Simd128 s = val.to_s128(); \
Simd128 s = val.to_s128(); \ auto ss = s.to_##name(); \
auto ss = s.to_##name(); \ auto res = ss.val[LANE(imm.lane, ss)]; \
auto res = ss.val[LANE(imm.lane, ss)]; \ DCHECK(std::is_signed<decltype(res)>::value); \
DCHECK(std::is_signed<decltype(res)>::value); \ if (std::is_unsigned<extended_type>::value) { \
if (std::is_unsigned<extended_type>::value) { \ using unsigned_type = std::make_unsigned<decltype(res)>::type; \
using unsigned_type = std::make_unsigned<decltype(res)>::type; \ Push(WasmValue( \
Push(WasmValue( \ static_cast<extended_type>(static_cast<unsigned_type>(res)))); \
static_cast<extended_type>(static_cast<unsigned_type>(res)))); \ } else { \
} else { \ Push(WasmValue(static_cast<extended_type>(res))); \
Push(WasmValue(static_cast<extended_type>(res))); \ } \
} \ return true; \
return true; \
} }
EXTRACT_LANE_EXTEND_CASE(I16x8, i16x8, S, int32_t) EXTRACT_LANE_EXTEND_CASE(I16x8, i16x8, S, int32_t)
EXTRACT_LANE_EXTEND_CASE(I16x8, i16x8, U, uint32_t) EXTRACT_LANE_EXTEND_CASE(I16x8, i16x8, U, uint32_t)
...@@ -2376,17 +2376,16 @@ class WasmInterpreterInternals { ...@@ -2376,17 +2376,16 @@ class WasmInterpreterInternals {
CMPOP_CASE(I8x16LeU, i8x16, int16, int16, 16, CMPOP_CASE(I8x16LeU, i8x16, int16, int16, 16,
static_cast<uint8_t>(a) <= static_cast<uint8_t>(b)) static_cast<uint8_t>(a) <= static_cast<uint8_t>(b))
#undef CMPOP_CASE #undef CMPOP_CASE
#define REPLACE_LANE_CASE(format, name, stype, ctype) \ #define REPLACE_LANE_CASE(format, name, stype, ctype) \
case kExpr##format##ReplaceLane: { \ case kExpr##format##ReplaceLane: { \
SimdLaneImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc), \ SimdLaneImmediate<Decoder::kNoValidate> imm(decoder, code->at(pc + *len)); \
opcode_length); \ *len += 1; \
*len += 1; \ WasmValue new_val = Pop(); \
WasmValue new_val = Pop(); \ WasmValue simd_val = Pop(); \
WasmValue simd_val = Pop(); \ stype s = simd_val.to_s128().to_##name(); \
stype s = simd_val.to_s128().to_##name(); \ s.val[LANE(imm.lane, s)] = new_val.to<ctype>(); \
s.val[LANE(imm.lane, s)] = new_val.to<ctype>(); \ Push(WasmValue(Simd128(s))); \
Push(WasmValue(Simd128(s))); \ return true; \
return true; \
} }
REPLACE_LANE_CASE(F64x2, f64x2, float2, double) REPLACE_LANE_CASE(F64x2, f64x2, float2, double)
REPLACE_LANE_CASE(F32x4, f32x4, float4, float) REPLACE_LANE_CASE(F32x4, f32x4, float4, float)
...@@ -2398,11 +2397,11 @@ class WasmInterpreterInternals { ...@@ -2398,11 +2397,11 @@ class WasmInterpreterInternals {
case kExprS128LoadMem: case kExprS128LoadMem:
return ExecuteLoad<Simd128, Simd128>(decoder, code, pc, len, return ExecuteLoad<Simd128, Simd128>(decoder, code, pc, len,
MachineRepresentation::kSimd128, MachineRepresentation::kSimd128,
/*prefix_len=*/opcode_length); /*prefix_len=*/*len);
case kExprS128StoreMem: case kExprS128StoreMem:
return ExecuteStore<Simd128, Simd128>(decoder, code, pc, len, return ExecuteStore<Simd128, Simd128>(decoder, code, pc, len,
MachineRepresentation::kSimd128, MachineRepresentation::kSimd128,
/*prefix_len=*/opcode_length); /*prefix_len=*/*len);
#define SHIFT_CASE(op, name, stype, count, expr) \ #define SHIFT_CASE(op, name, stype, count, expr) \
case kExpr##op: { \ case kExpr##op: { \
uint32_t shift = Pop().to<uint32_t>(); \ uint32_t shift = Pop().to<uint32_t>(); \
...@@ -2563,8 +2562,8 @@ class WasmInterpreterInternals { ...@@ -2563,8 +2562,8 @@ class WasmInterpreterInternals {
return true; return true;
} }
case kExprS8x16Shuffle: { case kExprS8x16Shuffle: {
Simd8x16ShuffleImmediate<Decoder::kNoValidate> imm( Simd8x16ShuffleImmediate<Decoder::kNoValidate> imm(decoder,
decoder, code->at(pc), opcode_length); code->at(pc + *len));
*len += 16; *len += 16;
int16 v2 = Pop().to_s128().to_i8x16(); int16 v2 = Pop().to_s128().to_i8x16();
int16 v1 = Pop().to_s128().to_i8x16(); int16 v1 = Pop().to_s128().to_i8x16();
...@@ -2671,7 +2670,7 @@ class WasmInterpreterInternals { ...@@ -2671,7 +2670,7 @@ class WasmInterpreterInternals {
// the prefix_len for ExecuteLoad is len, minus the prefix byte itself. // the prefix_len for ExecuteLoad is len, minus the prefix byte itself.
// Think of prefix_len as: number of extra bytes that make up this op. // Think of prefix_len as: number of extra bytes that make up this op.
if (!ExecuteLoad<result_type, load_type>(decoder, code, pc, len, rep, if (!ExecuteLoad<result_type, load_type>(decoder, code, pc, len, rep,
/*prefix_len=*/*len - 1)) { /*prefix_len=*/*len)) {
return false; return false;
} }
result_type v = Pop().to<result_type>(); result_type v = Pop().to<result_type>();
...@@ -2687,7 +2686,7 @@ class WasmInterpreterInternals { ...@@ -2687,7 +2686,7 @@ class WasmInterpreterInternals {
static_assert(sizeof(wide_type) == sizeof(narrow_type) * 2, static_assert(sizeof(wide_type) == sizeof(narrow_type) * 2,
"size mismatch for wide and narrow types"); "size mismatch for wide and narrow types");
if (!ExecuteLoad<uint64_t, uint64_t>(decoder, code, pc, len, rep, if (!ExecuteLoad<uint64_t, uint64_t>(decoder, code, pc, len, rep,
/*prefix_len=*/*len - 1)) { /*prefix_len=*/*len)) {
return false; return false;
} }
constexpr int lanes = kSimd128Size / sizeof(wide_type); constexpr int lanes = kSimd128Size / sizeof(wide_type);
...@@ -2960,15 +2959,16 @@ class WasmInterpreterInternals { ...@@ -2960,15 +2959,16 @@ class WasmInterpreterInternals {
DCHECK_NOT_NULL(code->start); DCHECK_NOT_NULL(code->start);
int len = 1; int len = 1;
// We need to store this, because SIMD opcodes are LEB encoded, and later
// on when executing, we need to know where to read immediates from.
uint32_t simd_opcode_length = 0;
byte orig = code->start[pc]; byte orig = code->start[pc];
WasmOpcode opcode = static_cast<WasmOpcode>(orig); WasmOpcode opcode = static_cast<WasmOpcode>(orig);
// If the opcode is a prefix, read the suffix and add the extra length to
// 'len'.
if (WasmOpcodes::IsPrefixOpcode(opcode)) { if (WasmOpcodes::IsPrefixOpcode(opcode)) {
uint32_t prefixed_opcode_length = 0;
opcode = decoder.read_prefixed_opcode<Decoder::kNoValidate>( opcode = decoder.read_prefixed_opcode<Decoder::kNoValidate>(
&code->start[pc], &simd_opcode_length); code->at(pc), &prefixed_opcode_length);
len += simd_opcode_length; len += prefixed_opcode_length;
} }
// If max is 0, break. If max is positive (a limit is set), decrement it. // If max is 0, break. If max is positive (a limit is set), decrement it.
...@@ -2997,14 +2997,14 @@ class WasmInterpreterInternals { ...@@ -2997,14 +2997,14 @@ class WasmInterpreterInternals {
case kExprBlock: case kExprBlock:
case kExprLoop: case kExprLoop:
case kExprTry: { case kExprTry: {
BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), BlockTypeImmediate<Decoder::kNoValidate> imm(
&decoder, code->at(pc)); WasmFeatures::All(), &decoder, code->at(pc + 1));
len = 1 + imm.length; len = 1 + imm.length;
break; break;
} }
case kExprIf: { case kExprIf: {
BlockTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), BlockTypeImmediate<Decoder::kNoValidate> imm(
&decoder, code->at(pc)); WasmFeatures::All(), &decoder, code->at(pc + 1));
WasmValue cond = Pop(); WasmValue cond = Pop();
bool is_true = cond.to<uint32_t>() != 0; bool is_true = cond.to<uint32_t>() != 0;
if (is_true) { if (is_true) {
...@@ -3025,7 +3025,7 @@ class WasmInterpreterInternals { ...@@ -3025,7 +3025,7 @@ class WasmInterpreterInternals {
} }
case kExprThrow: { case kExprThrow: {
ExceptionIndexImmediate<Decoder::kNoValidate> imm(&decoder, ExceptionIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
CommitPc(pc); // Needed for local unwinding. CommitPc(pc); // Needed for local unwinding.
const WasmException* exception = &module()->exceptions[imm.index]; const WasmException* exception = &module()->exceptions[imm.index];
if (!DoThrowException(exception, imm.index)) return; if (!DoThrowException(exception, imm.index)) return;
...@@ -3044,8 +3044,8 @@ class WasmInterpreterInternals { ...@@ -3044,8 +3044,8 @@ class WasmInterpreterInternals {
continue; // Do not bump pc. continue; // Do not bump pc.
} }
case kExprBrOnExn: { case kExprBrOnExn: {
BranchOnExceptionImmediate<Decoder::kNoValidate> imm(&decoder, BranchOnExceptionImmediate<Decoder::kNoValidate> imm(
code->at(pc)); &decoder, code->at(pc + 1));
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
WasmValue ex = Pop(); WasmValue ex = Pop();
Handle<Object> exception = ex.to_externref(); Handle<Object> exception = ex.to_externref();
...@@ -3063,8 +3063,8 @@ class WasmInterpreterInternals { ...@@ -3063,8 +3063,8 @@ class WasmInterpreterInternals {
break; break;
} }
case kExprSelectWithType: { case kExprSelectWithType: {
SelectTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), SelectTypeImmediate<Decoder::kNoValidate> imm(
&decoder, code->at(pc)); WasmFeatures::All(), &decoder, code->at(pc + 1));
len = 1 + imm.length; len = 1 + imm.length;
V8_FALLTHROUGH; V8_FALLTHROUGH;
} }
...@@ -3078,14 +3078,14 @@ class WasmInterpreterInternals { ...@@ -3078,14 +3078,14 @@ class WasmInterpreterInternals {
} }
case kExprBr: { case kExprBr: {
BranchDepthImmediate<Decoder::kNoValidate> imm(&decoder, BranchDepthImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
len = DoBreak(code, pc, imm.depth); len = DoBreak(code, pc, imm.depth);
TRACE(" br => @%zu\n", pc + len); TRACE(" br => @%zu\n", pc + len);
break; break;
} }
case kExprBrIf: { case kExprBrIf: {
BranchDepthImmediate<Decoder::kNoValidate> imm(&decoder, BranchDepthImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
WasmValue cond = Pop(); WasmValue cond = Pop();
bool is_true = cond.to<uint32_t>() != 0; bool is_true = cond.to<uint32_t>() != 0;
if (is_true) { if (is_true) {
...@@ -3099,7 +3099,7 @@ class WasmInterpreterInternals { ...@@ -3099,7 +3099,7 @@ class WasmInterpreterInternals {
} }
case kExprBrTable: { case kExprBrTable: {
BranchTableImmediate<Decoder::kNoValidate> imm(&decoder, BranchTableImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
BranchTableIterator<Decoder::kNoValidate> iterator(&decoder, imm); BranchTableIterator<Decoder::kNoValidate> iterator(&decoder, imm);
uint32_t key = Pop().to<uint32_t>(); uint32_t key = Pop().to<uint32_t>();
uint32_t depth = 0; uint32_t depth = 0;
...@@ -3124,39 +3124,39 @@ class WasmInterpreterInternals { ...@@ -3124,39 +3124,39 @@ class WasmInterpreterInternals {
break; break;
} }
case kExprI32Const: { case kExprI32Const: {
ImmI32Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); ImmI32Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc + 1));
Push(WasmValue(imm.value)); Push(WasmValue(imm.value));
len = 1 + imm.length; len = 1 + imm.length;
break; break;
} }
case kExprI64Const: { case kExprI64Const: {
ImmI64Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); ImmI64Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc + 1));
Push(WasmValue(imm.value)); Push(WasmValue(imm.value));
len = 1 + imm.length; len = 1 + imm.length;
break; break;
} }
case kExprF32Const: { case kExprF32Const: {
ImmF32Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); ImmF32Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc + 1));
Push(WasmValue(imm.value)); Push(WasmValue(imm.value));
len = 1 + imm.length; len = 1 + imm.length;
break; break;
} }
case kExprF64Const: { case kExprF64Const: {
ImmF64Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); ImmF64Immediate<Decoder::kNoValidate> imm(&decoder, code->at(pc + 1));
Push(WasmValue(imm.value)); Push(WasmValue(imm.value));
len = 1 + imm.length; len = 1 + imm.length;
break; break;
} }
case kExprRefNull: { case kExprRefNull: {
HeapTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(), HeapTypeImmediate<Decoder::kNoValidate> imm(
&decoder, code->at(pc)); WasmFeatures::All(), &decoder, code->at(pc + 1));
len = 1 + imm.length; len = 1 + imm.length;
Push(WasmValue(isolate_->factory()->null_value())); Push(WasmValue(isolate_->factory()->null_value()));
break; break;
} }
case kExprRefFunc: { case kExprRefFunc: {
FunctionIndexImmediate<Decoder::kNoValidate> imm(&decoder, FunctionIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
Handle<WasmExternalFunction> function = Handle<WasmExternalFunction> function =
...@@ -3167,14 +3167,16 @@ class WasmInterpreterInternals { ...@@ -3167,14 +3167,16 @@ class WasmInterpreterInternals {
break; break;
} }
case kExprLocalGet: { case kExprLocalGet: {
LocalIndexImmediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); LocalIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc + 1));
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
Push(GetStackValue(frames_.back().sp + imm.index)); Push(GetStackValue(frames_.back().sp + imm.index));
len = 1 + imm.length; len = 1 + imm.length;
break; break;
} }
case kExprLocalSet: { case kExprLocalSet: {
LocalIndexImmediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); LocalIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc + 1));
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
WasmValue val = Pop(); WasmValue val = Pop();
SetStackValue(frames_.back().sp + imm.index, val); SetStackValue(frames_.back().sp + imm.index, val);
...@@ -3182,7 +3184,8 @@ class WasmInterpreterInternals { ...@@ -3182,7 +3184,8 @@ class WasmInterpreterInternals {
break; break;
} }
case kExprLocalTee: { case kExprLocalTee: {
LocalIndexImmediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); LocalIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc + 1));
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
WasmValue val = Pop(); WasmValue val = Pop();
SetStackValue(frames_.back().sp + imm.index, val); SetStackValue(frames_.back().sp + imm.index, val);
...@@ -3196,7 +3199,7 @@ class WasmInterpreterInternals { ...@@ -3196,7 +3199,7 @@ class WasmInterpreterInternals {
} }
case kExprCallFunction: { case kExprCallFunction: {
CallFunctionImmediate<Decoder::kNoValidate> imm(&decoder, CallFunctionImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
InterpreterCode* target = codemap_.GetCode(imm.index); InterpreterCode* target = codemap_.GetCode(imm.index);
CHECK(!target->function->imported); CHECK(!target->function->imported);
// Execute an internal call. // Execute an internal call.
...@@ -3207,7 +3210,7 @@ class WasmInterpreterInternals { ...@@ -3207,7 +3210,7 @@ class WasmInterpreterInternals {
case kExprCallIndirect: { case kExprCallIndirect: {
CallIndirectImmediate<Decoder::kNoValidate> imm( CallIndirectImmediate<Decoder::kNoValidate> imm(
WasmFeatures::All(), &decoder, code->at(pc)); WasmFeatures::All(), &decoder, code->at(pc + 1));
uint32_t entry_index = Pop().to<uint32_t>(); uint32_t entry_index = Pop().to<uint32_t>();
CommitPc(pc); // TODO(wasm): Be more disciplined about committing PC. CommitPc(pc); // TODO(wasm): Be more disciplined about committing PC.
CallResult result = CallResult result =
...@@ -3228,7 +3231,7 @@ class WasmInterpreterInternals { ...@@ -3228,7 +3231,7 @@ class WasmInterpreterInternals {
case kExprReturnCall: { case kExprReturnCall: {
CallFunctionImmediate<Decoder::kNoValidate> imm(&decoder, CallFunctionImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
InterpreterCode* target = codemap_.GetCode(imm.index); InterpreterCode* target = codemap_.GetCode(imm.index);
CHECK(!target->function->imported); CHECK(!target->function->imported);
...@@ -3240,7 +3243,7 @@ class WasmInterpreterInternals { ...@@ -3240,7 +3243,7 @@ class WasmInterpreterInternals {
case kExprReturnCallIndirect: { case kExprReturnCallIndirect: {
CallIndirectImmediate<Decoder::kNoValidate> imm( CallIndirectImmediate<Decoder::kNoValidate> imm(
WasmFeatures::All(), &decoder, code->at(pc)); WasmFeatures::All(), &decoder, code->at(pc + 1));
uint32_t entry_index = Pop().to<uint32_t>(); uint32_t entry_index = Pop().to<uint32_t>();
CommitPc(pc); // TODO(wasm): Be more disciplined about committing PC. CommitPc(pc); // TODO(wasm): Be more disciplined about committing PC.
...@@ -3268,7 +3271,7 @@ class WasmInterpreterInternals { ...@@ -3268,7 +3271,7 @@ class WasmInterpreterInternals {
case kExprGlobalGet: { case kExprGlobalGet: {
GlobalIndexImmediate<Decoder::kNoValidate> imm(&decoder, GlobalIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
HandleScope handle_scope(isolate_); HandleScope handle_scope(isolate_);
Push(WasmInstanceObject::GetGlobalValue( Push(WasmInstanceObject::GetGlobalValue(
instance_object_, module()->globals[imm.index])); instance_object_, module()->globals[imm.index]));
...@@ -3277,7 +3280,7 @@ class WasmInterpreterInternals { ...@@ -3277,7 +3280,7 @@ class WasmInterpreterInternals {
} }
case kExprGlobalSet: { case kExprGlobalSet: {
GlobalIndexImmediate<Decoder::kNoValidate> imm(&decoder, GlobalIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
auto& global = module()->globals[imm.index]; auto& global = module()->globals[imm.index];
switch (global.type.kind()) { switch (global.type.kind()) {
#define CASE_TYPE(valuetype, ctype) \ #define CASE_TYPE(valuetype, ctype) \
...@@ -3314,7 +3317,8 @@ class WasmInterpreterInternals { ...@@ -3314,7 +3317,8 @@ class WasmInterpreterInternals {
break; break;
} }
case kExprTableGet: { case kExprTableGet: {
TableIndexImmediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); TableIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc + 1));
HandleScope handle_scope(isolate_); HandleScope handle_scope(isolate_);
auto table = handle( auto table = handle(
WasmTableObject::cast(instance_object_->tables().get(imm.index)), WasmTableObject::cast(instance_object_->tables().get(imm.index)),
...@@ -3331,7 +3335,8 @@ class WasmInterpreterInternals { ...@@ -3331,7 +3335,8 @@ class WasmInterpreterInternals {
break; break;
} }
case kExprTableSet: { case kExprTableSet: {
TableIndexImmediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); TableIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc + 1));
HandleScope handle_scope(isolate_); HandleScope handle_scope(isolate_);
auto table = handle( auto table = handle(
WasmTableObject::cast(instance_object_->tables().get(imm.index)), WasmTableObject::cast(instance_object_->tables().get(imm.index)),
...@@ -3434,7 +3439,7 @@ class WasmInterpreterInternals { ...@@ -3434,7 +3439,7 @@ class WasmInterpreterInternals {
#undef ASMJS_STORE_CASE #undef ASMJS_STORE_CASE
case kExprMemoryGrow: { case kExprMemoryGrow: {
MemoryIndexImmediate<Decoder::kNoValidate> imm(&decoder, MemoryIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
uint32_t delta_pages = Pop().to<uint32_t>(); uint32_t delta_pages = Pop().to<uint32_t>();
HandleScope handle_scope(isolate_); // Avoid leaking handles. HandleScope handle_scope(isolate_); // Avoid leaking handles.
Handle<WasmMemoryObject> memory(instance_object_->memory_object(), Handle<WasmMemoryObject> memory(instance_object_->memory_object(),
...@@ -3450,7 +3455,7 @@ class WasmInterpreterInternals { ...@@ -3450,7 +3455,7 @@ class WasmInterpreterInternals {
} }
case kExprMemorySize: { case kExprMemorySize: {
MemoryIndexImmediate<Decoder::kNoValidate> imm(&decoder, MemoryIndexImmediate<Decoder::kNoValidate> imm(&decoder,
code->at(pc)); code->at(pc + 1));
Push(WasmValue(static_cast<uint32_t>(instance_object_->memory_size() / Push(WasmValue(static_cast<uint32_t>(instance_object_->memory_size() /
kWasmPageSize))); kWasmPageSize)));
len = 1 + imm.length; len = 1 + imm.length;
...@@ -3497,9 +3502,7 @@ class WasmInterpreterInternals { ...@@ -3497,9 +3502,7 @@ class WasmInterpreterInternals {
break; break;
} }
case kSimdPrefix: { case kSimdPrefix: {
if (!ExecuteSimdOp(opcode, &decoder, code, pc, &len, if (!ExecuteSimdOp(opcode, &decoder, code, pc, &len)) return;
simd_opcode_length))
return;
break; break;
} }
......
...@@ -33,6 +33,14 @@ namespace function_body_decoder_unittest { ...@@ -33,6 +33,14 @@ namespace function_body_decoder_unittest {
#define WASM_IF_OP kExprIf, kLocalVoid #define WASM_IF_OP kExprIf, kLocalVoid
#define WASM_LOOP_OP kExprLoop, kLocalVoid #define WASM_LOOP_OP kExprLoop, kLocalVoid
#define EXPECT_OK(result) \
do { \
if (!result.ok()) { \
GTEST_NONFATAL_FAILURE_(result.error().message().c_str()); \
return; \
} \
} while (false)
static const byte kCodeGetLocal0[] = {kExprLocalGet, 0}; static const byte kCodeGetLocal0[] = {kExprLocalGet, 0};
static const byte kCodeGetLocal1[] = {kExprLocalGet, 1}; static const byte kCodeGetLocal1[] = {kExprLocalGet, 1};
static const byte kCodeSetLocal0[] = {WASM_SET_LOCAL(0, WASM_ZERO)}; static const byte kCodeSetLocal0[] = {WASM_SET_LOCAL(0, WASM_ZERO)};
...@@ -3398,14 +3406,14 @@ class BranchTableIteratorTest : public TestWithZone { ...@@ -3398,14 +3406,14 @@ class BranchTableIteratorTest : public TestWithZone {
BranchTableIteratorTest() : TestWithZone() {} BranchTableIteratorTest() : TestWithZone() {}
void CheckBrTableSize(const byte* start, const byte* end) { void CheckBrTableSize(const byte* start, const byte* end) {
Decoder decoder(start, end); Decoder decoder(start, end);
BranchTableImmediate<Decoder::kValidate> operand(&decoder, start); BranchTableImmediate<Decoder::kValidate> operand(&decoder, start + 1);
BranchTableIterator<Decoder::kValidate> iterator(&decoder, operand); BranchTableIterator<Decoder::kValidate> iterator(&decoder, operand);
EXPECT_EQ(end - start - 1u, iterator.length()); EXPECT_EQ(end - start - 1u, iterator.length());
EXPECT_TRUE(decoder.ok()); EXPECT_OK(decoder);
} }
void CheckBrTableError(const byte* start, const byte* end) { void CheckBrTableError(const byte* start, const byte* end) {
Decoder decoder(start, end); Decoder decoder(start, end);
BranchTableImmediate<Decoder::kValidate> operand(&decoder, start); BranchTableImmediate<Decoder::kValidate> operand(&decoder, start + 1);
BranchTableIterator<Decoder::kValidate> iterator(&decoder, operand); BranchTableIterator<Decoder::kValidate> iterator(&decoder, operand);
iterator.length(); iterator.length();
EXPECT_FALSE(decoder.ok()); EXPECT_FALSE(decoder.ok());
...@@ -3872,6 +3880,7 @@ TEST_F(BytecodeIteratorTest, WithLocalDecls) { ...@@ -3872,6 +3880,7 @@ TEST_F(BytecodeIteratorTest, WithLocalDecls) {
#undef WASM_IF_OP #undef WASM_IF_OP
#undef WASM_LOOP_OP #undef WASM_LOOP_OP
#undef WASM_BRV_IF_ZERO #undef WASM_BRV_IF_ZERO
#undef EXPECT_OK
} // namespace function_body_decoder_unittest } // namespace function_body_decoder_unittest
} // namespace wasm } // 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