function-body-decoder-unittest.cc 181 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/wasm/function-body-decoder.h"
6

7
#include "src/init/v8.h"
8 9
#include "src/objects/objects-inl.h"
#include "src/objects/objects.h"
10
#include "src/utils/ostreams.h"
11
#include "src/wasm/function-body-decoder-impl.h"
12
#include "src/wasm/leb-helper.h"
13
#include "src/wasm/local-decl-encoder.h"
14
#include "src/wasm/signature-map.h"
15
#include "src/wasm/wasm-limits.h"
16
#include "src/wasm/wasm-module.h"
17
#include "src/wasm/wasm-opcodes-inl.h"
18
#include "src/zone/zone.h"
19
#include "test/common/wasm/flag-utils.h"
20 21
#include "test/common/wasm/test-signatures.h"
#include "test/common/wasm/wasm-macro-gen.h"
22
#include "test/unittests/test-utils.h"
23
#include "testing/gmock-support.h"
24

25 26 27
namespace v8 {
namespace internal {
namespace wasm {
28
namespace function_body_decoder_unittest {
29

30 31 32 33
#define B1(a) WASM_BLOCK(a)
#define B2(a, b) WASM_BLOCK(a, b)
#define B3(a, b, c) WASM_BLOCK(a, b, c)

34 35
#define WASM_IF_OP kExprIf, kVoidCode
#define WASM_LOOP_OP kExprLoop, kVoidCode
36

37 38 39 40 41 42 43 44
#define EXPECT_OK(result)                                        \
  do {                                                           \
    if (!result.ok()) {                                          \
      GTEST_NONFATAL_FAILURE_(result.error().message().c_str()); \
      return;                                                    \
    }                                                            \
  } while (false)

45 46
static const byte kCodeGetLocal0[] = {kExprLocalGet, 0};
static const byte kCodeGetLocal1[] = {kExprLocalGet, 1};
47
static const byte kCodeSetLocal0[] = {WASM_SET_LOCAL(0, WASM_ZERO)};
48
static const byte kCodeTeeLocal0[] = {WASM_TEE_LOCAL(0, WASM_ZERO)};
49

50 51
static const ValueType kValueTypes[] = {kWasmI32, kWasmI64, kWasmF32, kWasmF64,
                                        kWasmExternRef};
52 53 54 55 56 57 58 59 60 61 62 63
static const MachineType machineTypes[] = {
    MachineType::Int8(),   MachineType::Uint8(),  MachineType::Int16(),
    MachineType::Uint16(), MachineType::Int32(),  MachineType::Uint32(),
    MachineType::Int64(),  MachineType::Uint64(), MachineType::Float32(),
    MachineType::Float64()};

static const WasmOpcode kInt32BinopOpcodes[] = {
    kExprI32Add,  kExprI32Sub,  kExprI32Mul,  kExprI32DivS, kExprI32DivU,
    kExprI32RemS, kExprI32RemU, kExprI32And,  kExprI32Ior,  kExprI32Xor,
    kExprI32Shl,  kExprI32ShrU, kExprI32ShrS, kExprI32Eq,   kExprI32LtS,
    kExprI32LeS,  kExprI32LtU,  kExprI32LeU};

64
#define WASM_BRV_IF_ZERO(depth, val) \
65 66
  val, WASM_ZERO, kExprBrIf, static_cast<byte>(depth)

67 68
constexpr size_t kMaxByteSizedLeb128 = 127;

69 70
using F = std::pair<ValueType, bool>;

71 72
enum MemoryType { kMemory32, kMemory64 };

73 74 75 76
// A helper for tests that require a module environment for functions,
// globals, or memories.
class TestModuleBuilder {
 public:
77
  explicit TestModuleBuilder(ModuleOrigin origin = kWasmOrigin)
78
      : allocator(), mod(std::make_unique<Zone>(&allocator, ZONE_NAME)) {
79 80 81 82 83 84 85 86 87 88 89 90 91 92
    mod.origin = origin;
  }
  byte AddGlobal(ValueType type, bool mutability = true) {
    mod.globals.push_back(
        {type, mutability, WasmInitExpr(), {0}, false, false});
    CHECK_LE(mod.globals.size(), kMaxByteSizedLeb128);
    return static_cast<byte>(mod.globals.size() - 1);
  }
  byte AddSignature(const FunctionSig* sig) {
    mod.add_signature(sig);
    CHECK_LE(mod.types.size(), kMaxByteSizedLeb128);
    return static_cast<byte>(mod.types.size() - 1);
  }
  byte AddFunction(const FunctionSig* sig, bool declared = true) {
93 94 95 96 97 98 99 100 101
    byte sig_index = AddSignature(sig);
    mod.functions.push_back(
        {sig,                                          // sig
         static_cast<uint32_t>(mod.functions.size()),  // func_index
         sig_index,                                    // sig_index
         {0, 0},                                       // code
         false,                                        // import
         false,                                        // export
         declared});                                   // declared
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    CHECK_LE(mod.functions.size(), kMaxByteSizedLeb128);
    return static_cast<byte>(mod.functions.size() - 1);
  }
  byte AddImport(const FunctionSig* sig) {
    byte result = AddFunction(sig);
    mod.functions[result].imported = true;
    return result;
  }
  byte AddException(WasmExceptionSig* sig) {
    mod.exceptions.emplace_back(sig);
    CHECK_LE(mod.types.size(), kMaxByteSizedLeb128);
    return static_cast<byte>(mod.exceptions.size() - 1);
  }

  byte AddTable(ValueType type, uint32_t initial_size, bool has_maximum_size,
                uint32_t maximum_size) {
118
    CHECK(WasmTable::IsValidTableType(type, &mod));
119 120 121 122 123 124 125 126 127
    mod.tables.emplace_back();
    WasmTable& table = mod.tables.back();
    table.type = type;
    table.initial_size = initial_size;
    table.has_maximum_size = has_maximum_size;
    table.maximum_size = maximum_size;
    return static_cast<byte>(mod.tables.size() - 1);
  }

128 129 130 131 132 133 134 135 136 137 138
  byte AddStruct(std::initializer_list<F> fields) {
    StructType::Builder type_builder(mod.signature_zone.get(),
                                     static_cast<uint32_t>(fields.size()));
    for (F field : fields) {
      type_builder.AddField(field.first, field.second);
    }
    mod.add_struct_type(type_builder.Build());
    return static_cast<byte>(mod.type_kinds.size() - 1);
  }

  byte AddArray(ValueType type, bool mutability) {
139
    ArrayType* array = mod.signature_zone->New<ArrayType>(type, mutability);
140 141 142 143
    mod.add_array_type(array);
    return static_cast<byte>(mod.type_kinds.size() - 1);
  }

144
  void InitializeMemory(MemoryType mem_type = kMemory32) {
145
    mod.has_memory = true;
146
    mod.is_memory64 = mem_type == kMemory64;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    mod.initial_pages = 1;
    mod.maximum_pages = 100;
  }

  byte InitializeTable(wasm::ValueType type) {
    mod.tables.emplace_back();
    mod.tables.back().type = type;
    return static_cast<byte>(mod.tables.size() - 1);
  }

  byte AddPassiveElementSegment(wasm::ValueType type) {
    mod.elem_segments.emplace_back(false);
    auto& init = mod.elem_segments.back();
    init.type = type;
    // Add 5 empty elements.
    for (uint32_t j = 0; j < 5; j++) {
      init.entries.push_back(WasmElemSegment::kNullIndex);
    }
    return static_cast<byte>(mod.elem_segments.size() - 1);
  }

  byte AddDeclarativeElementSegment() {
    mod.elem_segments.emplace_back(true);
    mod.elem_segments.back().entries.push_back(WasmElemSegment::kNullIndex);
    return static_cast<byte>(mod.elem_segments.size() - 1);
  }

  // Set the number of data segments as declared by the DataCount section.
  void SetDataSegmentCount(uint32_t data_segment_count) {
    // The Data section occurs after the Code section, so we don't need to
    // update mod.data_segments, as it is always empty.
    mod.num_declared_data_segments = data_segment_count;
  }

  WasmModule* module() { return &mod; }

 private:
184
  AccountingAllocator allocator;
185 186 187
  WasmModule mod;
};

188
class FunctionBodyDecoderTest : public TestWithZone {
189
 public:
190
  using LocalsDecl = std::pair<uint32_t, ValueType>;
191 192
  // All features are disabled by default and must be activated with
  // a WASM_FEATURE_SCOPE in individual tests.
193
  WasmFeatures enabled_features_ = WasmFeatures::None();
194

195
  FunctionBodyDecoderTest() : local_decls(zone()) {}
196

197
  TestSignatures sigs;
198 199
  TestModuleBuilder builder;
  WasmModule* module = builder.module();
200
  LocalDeclEncoder local_decls;
201

202
  void AddLocals(ValueType type, uint32_t count) {
203
    local_decls.AddLocals(count, type);
204 205
  }

206 207
  enum AppendEnd : bool { kAppendEnd, kOmitEnd };

208 209
  Vector<const byte> PrepareBytecode(Vector<const byte> code,
                                     AppendEnd append_end) {
210
    size_t locals_size = local_decls.Size();
211 212
    size_t total_size =
        code.size() + locals_size + (append_end == kAppendEnd ? 1 : 0);
213
    byte* buffer = zone()->NewArray<byte>(total_size);
214 215 216
    // Prepend the local decls to the code.
    local_decls.Emit(buffer);
    // Emit the code.
217
    if (code.size() > 0) {
218
      memcpy(buffer + locals_size, code.begin(), code.size());
219
    }
220 221 222 223
    if (append_end == kAppendEnd) {
      // Append an extra end opcode.
      buffer[total_size - 1] = kExprEnd;
    }
224

225
    return {buffer, total_size};
226 227
  }

228 229 230 231 232 233 234 235 236 237 238 239
  template <size_t N>
  Vector<const byte> CodeToVector(const byte (&code)[N]) {
    return ArrayVector(code);
  }

  Vector<const byte> CodeToVector(
      const std::initializer_list<const byte>& code) {
    return VectorOf(&*code.begin(), code.size());
  }

  Vector<const byte> CodeToVector(Vector<const byte> vec) { return vec; }

240
  // Prepends local variable declarations and renders nice error messages for
241
  // verification failures.
242
  template <typename Code = std::initializer_list<const byte>>
243
  void Validate(bool expected_success, const FunctionSig* sig, Code&& raw_code,
244 245 246 247 248 249
                AppendEnd append_end = kAppendEnd,
                const char* message = nullptr) {
    Vector<const byte> code =
        PrepareBytecode(CodeToVector(std::forward<Code>(raw_code)), append_end);

    // Validate the code.
250
    FunctionBody body(sig, 0, code.begin(), code.end());
251
    WasmFeatures unused_detected_features = WasmFeatures::None();
252
    DecodeResult result =
253 254
        VerifyWasmCode(zone()->allocator(), enabled_features_, module,
                       &unused_detected_features, body);
255

256
    std::ostringstream str;
257 258 259
    if (result.failed()) {
      str << "Verification failed: pc = +" << result.error().offset()
          << ", msg = " << result.error().message();
260
    } else {
261
      str << "Verification succeeded, expected failure";
262
    }
263
    EXPECT_EQ(result.ok(), expected_success) << str.str();
264 265
    if (result.failed() && message) {
      EXPECT_THAT(result.error().message(), ::testing::HasSubstr(message));
266
    }
267 268
  }

269
  template <typename Code = std::initializer_list<const byte>>
270
  void ExpectValidates(const FunctionSig* sig, Code&& raw_code,
271 272 273 274 275 276
                       AppendEnd append_end = kAppendEnd,
                       const char* message = nullptr) {
    Validate(true, sig, std::forward<Code>(raw_code), append_end, message);
  }

  template <typename Code = std::initializer_list<const byte>>
277
  void ExpectFailure(const FunctionSig* sig, Code&& raw_code,
278 279 280 281 282
                     AppendEnd append_end = kAppendEnd,
                     const char* message = nullptr) {
    Validate(false, sig, std::forward<Code>(raw_code), append_end, message);
  }

283
  void TestBinop(WasmOpcode opcode, const FunctionSig* success) {
284
    // op(local[0], local[1])
285
    byte code[] = {WASM_BINOP(opcode, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))};
286
    ExpectValidates(success, code);
287 288

    // Try all combinations of return and parameter types.
289 290 291 292
    for (size_t i = 0; i < arraysize(kValueTypes); i++) {
      for (size_t j = 0; j < arraysize(kValueTypes); j++) {
        for (size_t k = 0; k < arraysize(kValueTypes); k++) {
          ValueType types[] = {kValueTypes[i], kValueTypes[j], kValueTypes[k]};
293 294 295 296 297
          if (types[0] != success->GetReturn(0) ||
              types[1] != success->GetParam(0) ||
              types[2] != success->GetParam(1)) {
            // Test signature mismatch.
            FunctionSig sig(1, 2, types);
298
            ExpectFailure(&sig, code);
299 300 301 302 303 304
          }
        }
      }
    }
  }

305
  void TestUnop(WasmOpcode opcode, const FunctionSig* success) {
306 307 308
    TestUnop(opcode, success->GetReturn(), success->GetParam(0));
  }

309
  void TestUnop(WasmOpcode opcode, ValueType ret_type, ValueType param_type) {
310
    // Return(op(local[0]))
311
    byte code[] = {WASM_UNOP(opcode, WASM_GET_LOCAL(0))};
312
    {
313
      ValueType types[] = {ret_type, param_type};
314
      FunctionSig sig(1, 1, types);
315
      ExpectValidates(&sig, code);
316 317 318
    }

    // Try all combinations of return and parameter types.
319 320 321
    for (size_t i = 0; i < arraysize(kValueTypes); i++) {
      for (size_t j = 0; j < arraysize(kValueTypes); j++) {
        ValueType types[] = {kValueTypes[i], kValueTypes[j]};
322 323 324
        if (types[0] != ret_type || types[1] != param_type) {
          // Test signature mismatch.
          FunctionSig sig(1, 1, types);
325
          ExpectFailure(&sig, code);
326 327 328 329 330 331
        }
      }
    }
  }
};

332 333 334 335
TEST_F(FunctionBodyDecoderTest, Int32Const1) {
  byte code[] = {kExprI32Const, 0};
  for (int i = -64; i <= 63; i++) {
    code[1] = static_cast<byte>(i & 0x7F);
336
    ExpectValidates(sigs.i_i(), code);
337 338 339
  }
}

340
TEST_F(FunctionBodyDecoderTest, RefFunc) {
341
  WASM_FEATURE_SCOPE(reftypes);
342 343 344 345 346 347

  builder.AddFunction(sigs.v_ii());
  builder.AddFunction(sigs.ii_v());
  ExpectValidates(sigs.a_v(), {kExprRefFunc, 1});
}

348
TEST_F(FunctionBodyDecoderTest, EmptyFunction) {
349 350
  ExpectValidates(sigs.v_v(), {});
  ExpectFailure(sigs.i_i(), {});
351 352
}

353
TEST_F(FunctionBodyDecoderTest, IncompleteIf1) {
354
  byte code[] = {kExprIf};
355 356
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
357 358
}

359
TEST_F(FunctionBodyDecoderTest, Int32Const_fallthru) {
360
  ExpectValidates(sigs.i_i(), {WASM_I32V_1(0)});
361 362
}

363
TEST_F(FunctionBodyDecoderTest, Int32Const_fallthru2) {
364
  ExpectFailure(sigs.i_i(), {WASM_I32V_1(0), WASM_I32V_1(1)});
365 366
}

367
TEST_F(FunctionBodyDecoderTest, Int32Const) {
368 369
  const int kInc = 4498211;
  for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) {
370
    // TODO(binji): expand test for other sized int32s; 1 through 5 bytes.
371
    ExpectValidates(sigs.i_i(), {WASM_I32V(i)});
372 373 374
  }
}

375
TEST_F(FunctionBodyDecoderTest, Int64Const) {
376 377
  const int kInc = 4498211;
  for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) {
378 379
    ExpectValidates(sigs.l_l(),
                    {WASM_I64V((static_cast<uint64_t>(i) << 32) | i)});
380 381 382
  }
}

383
TEST_F(FunctionBodyDecoderTest, Float32Const) {
384
  byte code[] = {kExprF32Const, 0, 0, 0, 0};
385
  Address ptr = reinterpret_cast<Address>(code + 1);
386
  for (int i = 0; i < 30; i++) {
387
    base::WriteLittleEndianValue<float>(ptr, i * -7.75f);
388
    ExpectValidates(sigs.f_ff(), code);
389 390 391
  }
}

392
TEST_F(FunctionBodyDecoderTest, Float64Const) {
393
  byte code[] = {kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0};
394
  Address ptr = reinterpret_cast<Address>(code + 1);
395
  for (int i = 0; i < 30; i++) {
396
    base::WriteLittleEndianValue<double>(ptr, i * 33.45);
397
    ExpectValidates(sigs.d_dd(), code);
398 399 400
  }
}

401
TEST_F(FunctionBodyDecoderTest, Int32Const_off_end) {
402
  byte code[] = {kExprI32Const, 0xAA, 0xBB, 0xCC, 0x44};
403

404
  for (size_t size = 1; size <= 4; ++size) {
405
    ExpectFailure(sigs.i_i(), VectorOf(code, size), kAppendEnd);
406
    // Should also fail without the trailing 'end' opcode.
407
    ExpectFailure(sigs.i_i(), VectorOf(code, size), kOmitEnd);
408 409 410
  }
}

411
TEST_F(FunctionBodyDecoderTest, GetLocal0_param) {
412
  ExpectValidates(sigs.i_i(), kCodeGetLocal0);
413 414
}

415
TEST_F(FunctionBodyDecoderTest, GetLocal0_local) {
416
  AddLocals(kWasmI32, 1);
417
  ExpectValidates(sigs.i_v(), kCodeGetLocal0);
418 419
}

420
TEST_F(FunctionBodyDecoderTest, TooManyLocals) {
421
  AddLocals(kWasmI32, 4034986500);
422
  ExpectFailure(sigs.i_v(), kCodeGetLocal0);
423 424
}

425
TEST_F(FunctionBodyDecoderTest, GetLocal0_param_n) {
426 427
  for (const FunctionSig* sig : {sigs.i_i(), sigs.i_ii(), sigs.i_iii()}) {
    ExpectValidates(sig, kCodeGetLocal0);
428 429 430
  }
}

431
TEST_F(FunctionBodyDecoderTest, GetLocalN_local) {
432
  for (byte i = 1; i < 8; i++) {
433
    AddLocals(kWasmI32, 1);
434
    for (byte j = 0; j < i; j++) {
435
      ExpectValidates(sigs.i_v(), {kExprLocalGet, j});
436 437 438 439
    }
  }
}

440
TEST_F(FunctionBodyDecoderTest, GetLocal0_fail_no_params) {
441
  ExpectFailure(sigs.i_v(), kCodeGetLocal0);
442 443
}

444
TEST_F(FunctionBodyDecoderTest, GetLocal1_fail_no_locals) {
445
  ExpectFailure(sigs.i_i(), kCodeGetLocal1);
446 447
}

448
TEST_F(FunctionBodyDecoderTest, GetLocal_off_end) {
449
  ExpectFailure(sigs.i_i(), {kExprLocalGet});
450 451
}

452
TEST_F(FunctionBodyDecoderTest, NumLocalBelowLimit) {
453
  AddLocals(kWasmI32, kV8MaxWasmFunctionLocals - 1);
454
  ExpectValidates(sigs.v_v(), {WASM_NOP});
455 456
}

457
TEST_F(FunctionBodyDecoderTest, NumLocalAtLimit) {
458
  AddLocals(kWasmI32, kV8MaxWasmFunctionLocals);
459
  ExpectValidates(sigs.v_v(), {WASM_NOP});
460 461
}

462
TEST_F(FunctionBodyDecoderTest, NumLocalAboveLimit) {
463
  AddLocals(kWasmI32, kV8MaxWasmFunctionLocals + 1);
464
  ExpectFailure(sigs.v_v(), {WASM_NOP});
465 466
}

467
TEST_F(FunctionBodyDecoderTest, GetLocal_varint) {
468
  const int kMaxLocals = kV8MaxWasmFunctionLocals - 1;
469
  AddLocals(kWasmI32, kMaxLocals);
470

471 472 473 474
  ExpectValidates(sigs.i_i(), {kExprLocalGet, U32V_1(66)});
  ExpectValidates(sigs.i_i(), {kExprLocalGet, U32V_2(7777)});
  ExpectValidates(sigs.i_i(), {kExprLocalGet, U32V_3(8888)});
  ExpectValidates(sigs.i_i(), {kExprLocalGet, U32V_4(9999)});
475

476
  ExpectValidates(sigs.i_i(), {kExprLocalGet, U32V_5(kMaxLocals - 1)});
477

478
  ExpectFailure(sigs.i_i(), {kExprLocalGet, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
479

480 481 482
  ExpectValidates(sigs.i_i(), {kExprLocalGet, U32V_4(kMaxLocals - 1)});
  ExpectValidates(sigs.i_i(), {kExprLocalGet, U32V_4(kMaxLocals)});
  ExpectFailure(sigs.i_i(), {kExprLocalGet, U32V_4(kMaxLocals + 1)});
483

484 485
  ExpectFailure(sigs.i_v(), {kExprLocalGet, U32V_4(kMaxLocals)});
  ExpectFailure(sigs.i_v(), {kExprLocalGet, U32V_4(kMaxLocals + 1)});
486 487
}

488
TEST_F(FunctionBodyDecoderTest, GetLocal_toomany) {
489
  AddLocals(kWasmI32, kV8MaxWasmFunctionLocals - 100);
490
  AddLocals(kWasmI32, 100);
491

492 493
  ExpectValidates(sigs.i_v(), {kExprLocalGet, U32V_1(66)});
  ExpectFailure(sigs.i_i(), {kExprLocalGet, U32V_1(66)});
494 495
}

496
TEST_F(FunctionBodyDecoderTest, Binops_off_end) {
497 498 499
  byte code1[] = {0};  // [opcode]
  for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) {
    code1[0] = kInt32BinopOpcodes[i];
500
    ExpectFailure(sigs.i_i(), code1);
501 502
  }

503
  byte code3[] = {kExprLocalGet, 0, 0};  // [expr] [opcode]
504
  for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) {
505
    code3[2] = kInt32BinopOpcodes[i];
506
    ExpectFailure(sigs.i_i(), code3);
507 508
  }

509
  byte code4[] = {kExprLocalGet, 0, 0, 0};  // [expr] [opcode] [opcode]
510
  for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) {
511
    code4[2] = kInt32BinopOpcodes[i];
512
    code4[3] = kInt32BinopOpcodes[i];
513
    ExpectFailure(sigs.i_i(), code4);
514 515 516
  }
}

517
TEST_F(FunctionBodyDecoderTest, BinopsAcrossBlock1) {
518
  ExpectFailure(sigs.i_i(), {WASM_ZERO, kExprBlock, kI32Code, WASM_ZERO,
519
                             kExprI32Add, kExprEnd});
520 521
}

522
TEST_F(FunctionBodyDecoderTest, BinopsAcrossBlock2) {
523
  ExpectFailure(sigs.i_i(), {WASM_ZERO, WASM_ZERO, kExprBlock, kI32Code,
524
                             kExprI32Add, kExprEnd});
525 526
}

527
TEST_F(FunctionBodyDecoderTest, BinopsAcrossBlock3) {
528
  ExpectFailure(sigs.i_i(), {WASM_ZERO, WASM_ZERO, kExprIf, kI32Code,
529
                             kExprI32Add, kExprElse, kExprI32Add, kExprEnd});
530
}
531

532
TEST_F(FunctionBodyDecoderTest, Nop) {
533
  ExpectValidates(sigs.v_v(), {kExprNop});
534 535
}

536
TEST_F(FunctionBodyDecoderTest, SetLocal0_void) {
537
  ExpectFailure(sigs.i_i(), {WASM_SET_LOCAL(0, WASM_ZERO)});
538 539
}

540
TEST_F(FunctionBodyDecoderTest, SetLocal0_param) {
541 542 543
  ExpectFailure(sigs.i_i(), kCodeSetLocal0);
  ExpectFailure(sigs.f_ff(), kCodeSetLocal0);
  ExpectFailure(sigs.d_dd(), kCodeSetLocal0);
544 545
}

546
TEST_F(FunctionBodyDecoderTest, TeeLocal0_param) {
547 548 549
  ExpectValidates(sigs.i_i(), kCodeTeeLocal0);
  ExpectFailure(sigs.f_ff(), kCodeTeeLocal0);
  ExpectFailure(sigs.d_dd(), kCodeTeeLocal0);
550 551
}

552
TEST_F(FunctionBodyDecoderTest, SetLocal0_local) {
553 554
  ExpectFailure(sigs.i_v(), kCodeSetLocal0);
  ExpectFailure(sigs.v_v(), kCodeSetLocal0);
555
  AddLocals(kWasmI32, 1);
556 557
  ExpectFailure(sigs.i_v(), kCodeSetLocal0);
  ExpectValidates(sigs.v_v(), kCodeSetLocal0);
558 559
}

560
TEST_F(FunctionBodyDecoderTest, TeeLocal0_local) {
561
  ExpectFailure(sigs.i_v(), kCodeTeeLocal0);
562
  AddLocals(kWasmI32, 1);
563
  ExpectValidates(sigs.i_v(), kCodeTeeLocal0);
564 565
}

566
TEST_F(FunctionBodyDecoderTest, TeeLocalN_local) {
567
  for (byte i = 1; i < 8; i++) {
568
    AddLocals(kWasmI32, 1);
569
    for (byte j = 0; j < i; j++) {
570 571
      ExpectFailure(sigs.v_v(), {WASM_TEE_LOCAL(j, WASM_I32V_1(i))});
      ExpectValidates(sigs.i_i(), {WASM_TEE_LOCAL(j, WASM_I32V_1(i))});
572 573 574 575
    }
  }
}

576
TEST_F(FunctionBodyDecoderTest, BlockN) {
577
  constexpr size_t kMaxSize = 200;
578
  byte buffer[kMaxSize + 3];
579

580
  for (size_t i = 0; i <= kMaxSize; i++) {
581 582
    memset(buffer, kExprNop, sizeof(buffer));
    buffer[0] = kExprBlock;
583
    buffer[1] = kVoidCode;
584
    buffer[i + 2] = kExprEnd;
585
    ExpectValidates(sigs.v_i(), VectorOf(buffer, i + 3), kAppendEnd);
586 587 588
  }
}

589
#define WASM_EMPTY_BLOCK kExprBlock, kVoidCode, kExprEnd
590

591
TEST_F(FunctionBodyDecoderTest, Block0) {
592 593
  ExpectValidates(sigs.v_v(), {WASM_EMPTY_BLOCK});
  ExpectFailure(sigs.i_i(), {WASM_EMPTY_BLOCK});
594 595
}

596
TEST_F(FunctionBodyDecoderTest, Block0_fallthru1) {
597 598
  ExpectValidates(sigs.v_v(), {WASM_BLOCK(WASM_EMPTY_BLOCK)});
  ExpectFailure(sigs.i_i(), {WASM_BLOCK(WASM_EMPTY_BLOCK)});
599 600
}

601
TEST_F(FunctionBodyDecoderTest, Block0Block0) {
602 603
  ExpectValidates(sigs.v_v(), {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK});
  ExpectFailure(sigs.i_i(), {WASM_EMPTY_BLOCK, WASM_EMPTY_BLOCK});
604 605
}

606
TEST_F(FunctionBodyDecoderTest, Block0_end) {
607
  ExpectFailure(sigs.v_v(), {WASM_EMPTY_BLOCK, kExprEnd});
608 609
}

610 611
#undef WASM_EMPTY_BLOCK

612
TEST_F(FunctionBodyDecoderTest, Block1) {
613
  byte code[] = {WASM_BLOCK_I(WASM_GET_LOCAL(0))};
614 615 616 617 618
  ExpectValidates(sigs.i_i(), code);
  ExpectFailure(sigs.v_i(), code);
  ExpectFailure(sigs.d_dd(), code);
  ExpectFailure(sigs.i_f(), code);
  ExpectFailure(sigs.i_d(), code);
619 620
}

621
TEST_F(FunctionBodyDecoderTest, Block1_i) {
622
  byte code[] = {WASM_BLOCK_I(WASM_ZERO)};
623 624 625 626
  ExpectValidates(sigs.i_i(), code);
  ExpectFailure(sigs.f_ff(), code);
  ExpectFailure(sigs.d_dd(), code);
  ExpectFailure(sigs.l_ll(), code);
627 628
}

629
TEST_F(FunctionBodyDecoderTest, Block1_f) {
630
  byte code[] = {WASM_BLOCK_F(WASM_F32(0))};
631 632 633 634
  ExpectFailure(sigs.i_i(), code);
  ExpectValidates(sigs.f_ff(), code);
  ExpectFailure(sigs.d_dd(), code);
  ExpectFailure(sigs.l_ll(), code);
635 636
}

637
TEST_F(FunctionBodyDecoderTest, Block1_continue) {
638
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_BR(0))});
639 640
}

641
TEST_F(FunctionBodyDecoderTest, Block1_br) {
642 643 644
  ExpectValidates(sigs.v_v(), {B1(WASM_BR(0))});
  ExpectValidates(sigs.v_v(), {B1(WASM_BR(1))});
  ExpectFailure(sigs.v_v(), {B1(WASM_BR(2))});
645 646
}

647
TEST_F(FunctionBodyDecoderTest, Block2_br) {
648 649 650
  ExpectValidates(sigs.v_v(), {B2(WASM_NOP, WASM_BR(0))});
  ExpectValidates(sigs.v_v(), {B2(WASM_BR(0), WASM_NOP)});
  ExpectValidates(sigs.v_v(), {B2(WASM_BR(0), WASM_BR(0))});
651 652
}

653
TEST_F(FunctionBodyDecoderTest, Block2) {
654 655 656 657 658
  ExpectFailure(sigs.i_i(), {WASM_BLOCK(WASM_NOP, WASM_NOP)});
  ExpectFailure(sigs.i_i(), {WASM_BLOCK_I(WASM_NOP, WASM_NOP)});
  ExpectValidates(sigs.i_i(), {WASM_BLOCK_I(WASM_NOP, WASM_ZERO)});
  ExpectValidates(sigs.i_i(), {WASM_BLOCK_I(WASM_ZERO, WASM_NOP)});
  ExpectFailure(sigs.i_i(), {WASM_BLOCK_I(WASM_ZERO, WASM_ZERO)});
659 660
}

661
TEST_F(FunctionBodyDecoderTest, Block2b) {
662
  byte code[] = {WASM_BLOCK_I(WASM_SET_LOCAL(0, WASM_ZERO), WASM_ZERO)};
663 664 665
  ExpectValidates(sigs.i_i(), code);
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.f_ff(), code);
666 667
}

668
TEST_F(FunctionBodyDecoderTest, Block2_fallthru) {
669 670 671
  ExpectValidates(sigs.i_i(), {B2(WASM_SET_LOCAL(0, WASM_ZERO),
                                  WASM_SET_LOCAL(0, WASM_ZERO)),
                               WASM_I32V_1(23)});
672
}
673

674
TEST_F(FunctionBodyDecoderTest, Block3) {
675 676 677
  ExpectValidates(sigs.i_i(), {WASM_BLOCK_I(WASM_SET_LOCAL(0, WASM_ZERO),
                                            WASM_SET_LOCAL(0, WASM_ZERO),
                                            WASM_I32V_1(11))});
678 679
}

680
TEST_F(FunctionBodyDecoderTest, Block5) {
681
  ExpectFailure(sigs.v_i(), {WASM_BLOCK(WASM_ZERO)});
682

683
  ExpectFailure(sigs.v_i(), {WASM_BLOCK(WASM_ZERO, WASM_ZERO)});
684

685
  ExpectFailure(sigs.v_i(), {WASM_BLOCK(WASM_ZERO, WASM_ZERO, WASM_ZERO)});
686

687 688
  ExpectFailure(sigs.v_i(),
                {WASM_BLOCK(WASM_ZERO, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
689

690 691
  ExpectFailure(sigs.v_i(), {WASM_BLOCK(WASM_ZERO, WASM_ZERO, WASM_ZERO,
                                        WASM_ZERO, WASM_ZERO)});
692
}
693

694
TEST_F(FunctionBodyDecoderTest, BlockType) {
695 696 697 698
  ExpectValidates(sigs.i_i(), {WASM_BLOCK_I(WASM_GET_LOCAL(0))});
  ExpectValidates(sigs.l_l(), {WASM_BLOCK_L(WASM_GET_LOCAL(0))});
  ExpectValidates(sigs.f_f(), {WASM_BLOCK_F(WASM_GET_LOCAL(0))});
  ExpectValidates(sigs.d_d(), {WASM_BLOCK_D(WASM_GET_LOCAL(0))});
699 700
}

701
TEST_F(FunctionBodyDecoderTest, BlockType_fail) {
702 703 704
  ExpectFailure(sigs.i_i(), {WASM_BLOCK_L(WASM_I64V_1(0))});
  ExpectFailure(sigs.i_i(), {WASM_BLOCK_F(WASM_F32(0.0))});
  ExpectFailure(sigs.i_i(), {WASM_BLOCK_D(WASM_F64(1.1))});
705

706 707 708
  ExpectFailure(sigs.l_l(), {WASM_BLOCK_I(WASM_ZERO)});
  ExpectFailure(sigs.l_l(), {WASM_BLOCK_F(WASM_F32(0.0))});
  ExpectFailure(sigs.l_l(), {WASM_BLOCK_D(WASM_F64(1.1))});
709

710 711 712
  ExpectFailure(sigs.f_ff(), {WASM_BLOCK_I(WASM_ZERO)});
  ExpectFailure(sigs.f_ff(), {WASM_BLOCK_L(WASM_I64V_1(0))});
  ExpectFailure(sigs.f_ff(), {WASM_BLOCK_D(WASM_F64(1.1))});
713

714 715 716
  ExpectFailure(sigs.d_dd(), {WASM_BLOCK_I(WASM_ZERO)});
  ExpectFailure(sigs.d_dd(), {WASM_BLOCK_L(WASM_I64V_1(0))});
  ExpectFailure(sigs.d_dd(), {WASM_BLOCK_F(WASM_F32(0.0))});
717 718
}

719
TEST_F(FunctionBodyDecoderTest, BlockF32) {
720
  static const byte code[] = {WASM_BLOCK_F(kExprF32Const, 0, 0, 0, 0)};
721 722 723
  ExpectValidates(sigs.f_ff(), code);
  ExpectFailure(sigs.i_i(), code);
  ExpectFailure(sigs.d_dd(), code);
724 725
}

726
TEST_F(FunctionBodyDecoderTest, BlockN_off_end) {
727
  byte code[] = {WASM_BLOCK(kExprNop, kExprNop, kExprNop, kExprNop)};
728
  ExpectValidates(sigs.v_v(), code);
729
  for (size_t i = 1; i < arraysize(code); i++) {
730 731
    ExpectFailure(sigs.v_v(), VectorOf(code, i), kAppendEnd);
    ExpectFailure(sigs.v_v(), VectorOf(code, i), kOmitEnd);
732
  }
733 734
}

735
TEST_F(FunctionBodyDecoderTest, Block2_continue) {
736 737 738
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_NOP, WASM_BR(0))});
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_NOP, WASM_BR(1))});
  ExpectFailure(sigs.v_v(), {WASM_LOOP(WASM_NOP, WASM_BR(2))});
739 740
}

741
TEST_F(FunctionBodyDecoderTest, Block3_continue) {
742 743 744 745
  ExpectValidates(sigs.v_v(), {B1(WASM_LOOP(WASM_NOP, WASM_BR(0)))});
  ExpectValidates(sigs.v_v(), {B1(WASM_LOOP(WASM_NOP, WASM_BR(1)))});
  ExpectValidates(sigs.v_v(), {B1(WASM_LOOP(WASM_NOP, WASM_BR(2)))});
  ExpectFailure(sigs.v_v(), {B1(WASM_LOOP(WASM_NOP, WASM_BR(3)))});
746 747
}

748
TEST_F(FunctionBodyDecoderTest, NestedBlock_return) {
749
  ExpectValidates(sigs.i_i(), {B1(B1(WASM_RETURN1(WASM_ZERO))), WASM_ZERO});
750 751
}

752
TEST_F(FunctionBodyDecoderTest, BlockBrBinop) {
753 754 755
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_AND(WASM_BLOCK_I(WASM_BRV(0, WASM_I32V_1(1))),
                                WASM_I32V_1(2))});
756 757
}

758
TEST_F(FunctionBodyDecoderTest, If_empty1) {
759
  ExpectValidates(sigs.v_v(), {WASM_ZERO, WASM_IF_OP, kExprEnd});
760 761
}

762
TEST_F(FunctionBodyDecoderTest, If_empty2) {
763
  ExpectValidates(sigs.v_v(), {WASM_ZERO, WASM_IF_OP, kExprElse, kExprEnd});
764 765
}

766
TEST_F(FunctionBodyDecoderTest, If_empty3) {
767 768 769 770
  ExpectValidates(sigs.v_v(),
                  {WASM_ZERO, WASM_IF_OP, WASM_NOP, kExprElse, kExprEnd});
  ExpectFailure(sigs.v_v(),
                {WASM_ZERO, WASM_IF_OP, WASM_ZERO, kExprElse, kExprEnd});
771 772
}

773
TEST_F(FunctionBodyDecoderTest, If_empty4) {
774 775 776 777
  ExpectValidates(sigs.v_v(),
                  {WASM_ZERO, WASM_IF_OP, kExprElse, WASM_NOP, kExprEnd});
  ExpectFailure(sigs.v_v(),
                {WASM_ZERO, WASM_IF_OP, kExprElse, WASM_ZERO, kExprEnd});
778 779
}

780
TEST_F(FunctionBodyDecoderTest, If_empty_stack) {
781
  byte code[] = {kExprIf};
782 783
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
784 785
}

786
TEST_F(FunctionBodyDecoderTest, If_incomplete1) {
787
  byte code[] = {kExprI32Const, 0, kExprIf};
788 789
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
790 791
}

792
TEST_F(FunctionBodyDecoderTest, If_incomplete2) {
793
  byte code[] = {kExprI32Const, 0, kExprIf, kExprNop};
794 795
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
796 797
}

798
TEST_F(FunctionBodyDecoderTest, If_else_else) {
799
  byte code[] = {kExprI32Const, 0, WASM_IF_OP, kExprElse, kExprElse, kExprEnd};
800 801
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
802 803
}

804
TEST_F(FunctionBodyDecoderTest, IfEmpty) {
805
  ExpectValidates(sigs.v_i(), {kExprLocalGet, 0, WASM_IF_OP, kExprEnd});
806 807
}

808
TEST_F(FunctionBodyDecoderTest, IfSet) {
809 810 811 812 813
  ExpectValidates(sigs.v_i(),
                  {WASM_IF(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO))});
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO),
                                WASM_NOP)});
814 815
}

816
TEST_F(FunctionBodyDecoderTest, IfElseEmpty) {
817 818 819 820
  ExpectValidates(sigs.v_i(),
                  {WASM_GET_LOCAL(0), WASM_IF_OP, kExprElse, kExprEnd});
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)});
821 822
}

823
TEST_F(FunctionBodyDecoderTest, IfElseUnreachable1) {
824 825 826 827 828 829
  ExpectValidates(
      sigs.i_i(),
      {WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_UNREACHABLE, WASM_GET_LOCAL(0))});
  ExpectValidates(
      sigs.i_i(),
      {WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_UNREACHABLE)});
830 831
}

832
TEST_F(FunctionBodyDecoderTest, IfElseUnreachable2) {
833
  static const byte code[] = {
834
      WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_UNREACHABLE, WASM_GET_LOCAL(0))};
835

836 837
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType types[] = {kWasmI32, kValueTypes[i]};
838 839
    FunctionSig sig(1, 1, types);

840
    Validate(kValueTypes[i] == kWasmI32, &sig, code);
841 842 843
  }
}

844
TEST_F(FunctionBodyDecoderTest, OneArmedIfWithArity) {
845
  static const byte code[] = {WASM_ZERO, kExprIf, kI32Code, WASM_ONE, kExprEnd};
846 847
  ExpectFailure(sigs.i_v(), code, kAppendEnd,
                "start-arity and end-arity of one-armed if must match");
848 849
}

850
TEST_F(FunctionBodyDecoderTest, IfBreak) {
851 852 853
  ExpectValidates(sigs.v_i(), {WASM_IF(WASM_GET_LOCAL(0), WASM_BR(0))});
  ExpectValidates(sigs.v_i(), {WASM_IF(WASM_GET_LOCAL(0), WASM_BR(1))});
  ExpectFailure(sigs.v_i(), {WASM_IF(WASM_GET_LOCAL(0), WASM_BR(2))});
854 855
}

856
TEST_F(FunctionBodyDecoderTest, IfElseBreak) {
857 858 859 860 861 862
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_BR(0))});
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_BR(1))});
  ExpectFailure(sigs.v_i(),
                {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_BR(2))});
863 864
}

865
TEST_F(FunctionBodyDecoderTest, Block_else) {
866
  byte code[] = {kExprI32Const, 0, kExprBlock, kExprElse, kExprEnd};
867 868
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
869 870
}

871
TEST_F(FunctionBodyDecoderTest, IfNop) {
872 873 874
  ExpectValidates(sigs.v_i(), {WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)});
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)});
875 876
}

877
TEST_F(FunctionBodyDecoderTest, If_end) {
878 879
  ExpectValidates(sigs.v_i(), {kExprLocalGet, 0, WASM_IF_OP, kExprEnd});
  ExpectFailure(sigs.v_i(), {kExprLocalGet, 0, WASM_IF_OP, kExprEnd, kExprEnd});
880 881
}

882
TEST_F(FunctionBodyDecoderTest, If_falloff1) {
883 884
  ExpectFailure(sigs.v_i(), {kExprLocalGet, 0, kExprIf});
  ExpectFailure(sigs.v_i(), {kExprLocalGet, 0, WASM_IF_OP});
885
  ExpectFailure(sigs.v_i(),
886
                {kExprLocalGet, 0, WASM_IF_OP, kExprNop, kExprElse});
887 888
}

889
TEST_F(FunctionBodyDecoderTest, IfElseNop) {
890 891 892
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO),
                                WASM_NOP)});
893 894
}

895
TEST_F(FunctionBodyDecoderTest, IfBlock1) {
896 897 898
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0),
                                B1(WASM_SET_LOCAL(0, WASM_ZERO)), WASM_NOP)});
899 900
}

901
TEST_F(FunctionBodyDecoderTest, IfBlock1b) {
902 903
  ExpectValidates(sigs.v_i(), {WASM_IF(WASM_GET_LOCAL(0),
                                       B1(WASM_SET_LOCAL(0, WASM_ZERO)))});
904 905
}

906
TEST_F(FunctionBodyDecoderTest, IfBlock2a) {
907 908 909
  ExpectValidates(sigs.v_i(), {WASM_IF(WASM_GET_LOCAL(0),
                                       B2(WASM_SET_LOCAL(0, WASM_ZERO),
                                          WASM_SET_LOCAL(0, WASM_ZERO)))});
910 911
}

912
TEST_F(FunctionBodyDecoderTest, IfBlock2b) {
913 914 915 916
  ExpectValidates(sigs.v_i(), {WASM_IF_ELSE(WASM_GET_LOCAL(0),
                                            B2(WASM_SET_LOCAL(0, WASM_ZERO),
                                               WASM_SET_LOCAL(0, WASM_ZERO)),
                                            WASM_NOP)});
917 918
}

919
TEST_F(FunctionBodyDecoderTest, IfElseSet) {
920 921 922
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_SET_LOCAL(0, WASM_ZERO),
                                WASM_SET_LOCAL(0, WASM_I32V_1(1)))});
923 924
}

925
TEST_F(FunctionBodyDecoderTest, Loop0) {
926
  ExpectValidates(sigs.v_v(), {WASM_LOOP_OP, kExprEnd});
927
}
928

929
TEST_F(FunctionBodyDecoderTest, Loop1) {
930
  static const byte code[] = {WASM_LOOP(WASM_SET_LOCAL(0, WASM_ZERO))};
931 932 933
  ExpectValidates(sigs.v_i(), code);
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.f_ff(), code);
934 935
}

936
TEST_F(FunctionBodyDecoderTest, Loop2) {
937 938
  ExpectValidates(sigs.v_i(), {WASM_LOOP(WASM_SET_LOCAL(0, WASM_ZERO),
                                         WASM_SET_LOCAL(0, WASM_ZERO))});
939 940
}

941
TEST_F(FunctionBodyDecoderTest, Loop1_continue) {
942
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_BR(0))});
943 944
}

945
TEST_F(FunctionBodyDecoderTest, Loop1_break) {
946
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_BR(1))});
947 948
}

949
TEST_F(FunctionBodyDecoderTest, Loop2_continue) {
950 951
  ExpectValidates(sigs.v_i(),
                  {WASM_LOOP(WASM_SET_LOCAL(0, WASM_ZERO), WASM_BR(0))});
952 953
}

954
TEST_F(FunctionBodyDecoderTest, Loop2_break) {
955 956
  ExpectValidates(sigs.v_i(),
                  {WASM_LOOP(WASM_SET_LOCAL(0, WASM_ZERO), WASM_BR(1))});
957 958
}

959
TEST_F(FunctionBodyDecoderTest, InfiniteLoop1) {
960 961 962
  ExpectValidates(sigs.i_i(), {WASM_LOOP(WASM_BR(0)), WASM_ZERO});
  ExpectValidates(sigs.i_i(), {WASM_LOOP(WASM_BR(0)), WASM_ZERO});
  ExpectValidates(sigs.i_i(), {WASM_LOOP_I(WASM_BRV(1, WASM_ZERO))});
963 964 965
}

TEST_F(FunctionBodyDecoderTest, InfiniteLoop2) {
966
  ExpectFailure(sigs.i_i(), {WASM_LOOP(WASM_BR(0), WASM_ZERO), WASM_ZERO});
967 968
}

969
TEST_F(FunctionBodyDecoderTest, Loop2_unreachable) {
970
  ExpectValidates(sigs.i_i(), {WASM_LOOP_I(WASM_BR(0), WASM_NOP)});
971 972
}

973
TEST_F(FunctionBodyDecoderTest, LoopType) {
974 975 976 977
  ExpectValidates(sigs.i_i(), {WASM_LOOP_I(WASM_GET_LOCAL(0))});
  ExpectValidates(sigs.l_l(), {WASM_LOOP_L(WASM_GET_LOCAL(0))});
  ExpectValidates(sigs.f_f(), {WASM_LOOP_F(WASM_GET_LOCAL(0))});
  ExpectValidates(sigs.d_d(), {WASM_LOOP_D(WASM_GET_LOCAL(0))});
978 979
}

980
TEST_F(FunctionBodyDecoderTest, LoopType_void) {
981 982 983 984
  ExpectFailure(sigs.v_v(), {WASM_LOOP_I(WASM_ZERO)});
  ExpectFailure(sigs.v_v(), {WASM_LOOP_L(WASM_I64V_1(0))});
  ExpectFailure(sigs.v_v(), {WASM_LOOP_F(WASM_F32(0.0))});
  ExpectFailure(sigs.v_v(), {WASM_LOOP_D(WASM_F64(1.1))});
985 986
}

987
TEST_F(FunctionBodyDecoderTest, LoopType_fail) {
988 989 990
  ExpectFailure(sigs.i_i(), {WASM_LOOP_L(WASM_I64V_1(0))});
  ExpectFailure(sigs.i_i(), {WASM_LOOP_F(WASM_F32(0.0))});
  ExpectFailure(sigs.i_i(), {WASM_LOOP_D(WASM_F64(1.1))});
991

992 993 994
  ExpectFailure(sigs.l_l(), {WASM_LOOP_I(WASM_ZERO)});
  ExpectFailure(sigs.l_l(), {WASM_LOOP_F(WASM_F32(0.0))});
  ExpectFailure(sigs.l_l(), {WASM_LOOP_D(WASM_F64(1.1))});
995

996 997 998
  ExpectFailure(sigs.f_ff(), {WASM_LOOP_I(WASM_ZERO)});
  ExpectFailure(sigs.f_ff(), {WASM_LOOP_L(WASM_I64V_1(0))});
  ExpectFailure(sigs.f_ff(), {WASM_LOOP_D(WASM_F64(1.1))});
999

1000 1001 1002
  ExpectFailure(sigs.d_dd(), {WASM_LOOP_I(WASM_ZERO)});
  ExpectFailure(sigs.d_dd(), {WASM_LOOP_L(WASM_I64V_1(0))});
  ExpectFailure(sigs.d_dd(), {WASM_LOOP_F(WASM_F32(0.0))});
1003 1004
}

1005
TEST_F(FunctionBodyDecoderTest, ReturnVoid1) {
1006
  static const byte code[] = {kExprNop};
1007 1008 1009
  ExpectValidates(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
  ExpectFailure(sigs.i_f(), code);
1010 1011
}

1012
TEST_F(FunctionBodyDecoderTest, ReturnVoid2) {
1013
  static const byte code[] = {WASM_BLOCK(WASM_BR(0))};
1014 1015 1016
  ExpectValidates(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
  ExpectFailure(sigs.i_f(), code);
1017 1018
}

1019
TEST_F(FunctionBodyDecoderTest, ReturnVoid3) {
1020 1021 1022 1023 1024
  ExpectFailure(sigs.v_v(), {kExprI32Const, 0});
  ExpectFailure(sigs.v_v(), {kExprI64Const, 0});
  ExpectFailure(sigs.v_v(), {kExprF32Const, 0, 0, 0, 0});
  ExpectFailure(sigs.v_v(), {kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0});
  ExpectFailure(sigs.v_v(), {kExprRefNull});
1025
  ExpectFailure(sigs.v_v(), {kExprRefFunc, 0});
1026

1027
  ExpectFailure(sigs.v_i(), {kExprLocalGet, 0});
1028 1029
}

1030
TEST_F(FunctionBodyDecoderTest, Unreachable1) {
1031 1032 1033
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE});
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, WASM_UNREACHABLE});
  ExpectValidates(sigs.i_i(), {WASM_UNREACHABLE, WASM_ZERO});
1034 1035 1036
}

TEST_F(FunctionBodyDecoderTest, Unreachable2) {
1037 1038
  ExpectFailure(sigs.v_v(), {B2(WASM_UNREACHABLE, WASM_ZERO)});
  ExpectFailure(sigs.v_v(), {B2(WASM_BR(0), WASM_ZERO)});
1039 1040
}

1041
TEST_F(FunctionBodyDecoderTest, UnreachableLoop1) {
1042 1043 1044 1045
  ExpectFailure(sigs.v_v(), {WASM_LOOP(WASM_UNREACHABLE, WASM_ZERO)});
  ExpectFailure(sigs.v_v(), {WASM_LOOP(WASM_BR(0), WASM_ZERO)});
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_UNREACHABLE, WASM_NOP)});
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_BR(0), WASM_NOP)});
1046 1047 1048
}

TEST_F(FunctionBodyDecoderTest, Unreachable_binop1) {
1049 1050
  ExpectValidates(sigs.i_i(), {WASM_I32_AND(WASM_ZERO, WASM_UNREACHABLE)});
  ExpectValidates(sigs.i_i(), {WASM_I32_AND(WASM_UNREACHABLE, WASM_ZERO)});
1051 1052
}

1053
TEST_F(FunctionBodyDecoderTest, Unreachable_binop2) {
1054 1055
  ExpectValidates(sigs.i_i(), {WASM_I32_AND(WASM_F32(0.0), WASM_UNREACHABLE)});
  ExpectFailure(sigs.i_i(), {WASM_I32_AND(WASM_UNREACHABLE, WASM_F32(0.0))});
1056 1057 1058
}

TEST_F(FunctionBodyDecoderTest, Unreachable_select1) {
1059 1060 1061 1062 1063 1064
  ExpectValidates(sigs.i_i(),
                  {WASM_SELECT(WASM_UNREACHABLE, WASM_ZERO, WASM_ZERO)});
  ExpectValidates(sigs.i_i(),
                  {WASM_SELECT(WASM_ZERO, WASM_UNREACHABLE, WASM_ZERO)});
  ExpectValidates(sigs.i_i(),
                  {WASM_SELECT(WASM_ZERO, WASM_ZERO, WASM_UNREACHABLE)});
1065 1066
}

1067
TEST_F(FunctionBodyDecoderTest, Unreachable_select2) {
1068 1069 1070 1071 1072 1073
  ExpectValidates(sigs.i_i(),
                  {WASM_SELECT(WASM_F32(0.0), WASM_UNREACHABLE, WASM_ZERO)});
  ExpectFailure(sigs.i_i(),
                {WASM_SELECT(WASM_UNREACHABLE, WASM_F32(0.0), WASM_ZERO)});
  ExpectFailure(sigs.i_i(),
                {WASM_SELECT(WASM_UNREACHABLE, WASM_ZERO, WASM_F32(0.0))});
1074 1075
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
TEST_F(FunctionBodyDecoderTest, UnreachableRefTypes) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);
  WASM_FEATURE_SCOPE(return_call);

  byte function_index = builder.AddFunction(sigs.i_ii());
  byte struct_index = builder.AddStruct({F(kWasmI32, true), F(kWasmI64, true)});
  byte array_index = builder.AddArray(kWasmI32, true);

  ValueType struct_type = ValueType::Ref(struct_index, kNonNullable);
  FunctionSig sig_v_s(0, 1, &struct_type);
  byte struct_consumer = builder.AddFunction(&sig_v_s);

  ExpectValidates(sigs.v_v(), {WASM_BLOCK(WASM_UNREACHABLE, kExprBrOnNull, 0)});
  ExpectValidates(sigs.i_v(), {WASM_UNREACHABLE, kExprRefIsNull});
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, kExprRefAsNonNull, kExprDrop});

  ExpectValidates(sigs.i_v(), {WASM_UNREACHABLE, kExprCallRef, WASM_I32V(1)});
  ExpectValidates(sigs.i_v(), {WASM_UNREACHABLE, WASM_REF_FUNC(function_index),
                               kExprCallRef});
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, kExprReturnCallRef});

  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_GC_OP(kExprStructNewWithRtt),
                   struct_index, kExprCallFunction, struct_consumer});
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, WASM_RTT_CANON(struct_index),
                               WASM_GC_OP(kExprStructNewWithRtt), struct_index,
                               kExprCallFunction, struct_consumer});
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, WASM_I64V(42),
                               WASM_RTT_CANON(struct_index),
                               WASM_GC_OP(kExprStructNewWithRtt), struct_index,
                               kExprCallFunction, struct_consumer});
  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_GC_OP(kExprStructNewDefault),
                   struct_index, kExprDrop});
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, WASM_RTT_CANON(struct_index),
                               WASM_GC_OP(kExprStructNewDefault), struct_index,
                               kExprCallFunction, struct_consumer});

  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_GC_OP(kExprArrayNewWithRtt),
                   array_index, kExprDrop});
  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_RTT_CANON(array_index),
                   WASM_GC_OP(kExprArrayNewWithRtt), array_index, kExprDrop});
  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_I32V(42), WASM_RTT_CANON(array_index),
                   WASM_GC_OP(kExprArrayNewWithRtt), array_index, kExprDrop});
  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_GC_OP(kExprArrayNewDefault),
                   array_index, kExprDrop});
  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_RTT_CANON(array_index),
                   WASM_GC_OP(kExprArrayNewDefault), array_index, kExprDrop});

  ExpectValidates(sigs.i_v(), {WASM_UNREACHABLE, WASM_GC_OP(kExprRefTest),
                               struct_index, struct_index});
  ExpectValidates(sigs.i_v(),
                  {WASM_UNREACHABLE, WASM_RTT_CANON(struct_index),
                   WASM_GC_OP(kExprRefTest), struct_index, struct_index});

  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, WASM_GC_OP(kExprRefCast),
                               struct_index, struct_index, kExprDrop});
  ExpectValidates(sigs.v_v(), {WASM_UNREACHABLE, WASM_RTT_CANON(struct_index),
                               WASM_GC_OP(kExprRefCast), struct_index,
                               struct_index, kExprDrop});

  ExpectValidates(sigs.v_v(),
                  {WASM_UNREACHABLE, WASM_GC_OP(kExprRttSub), array_index,
                   WASM_GC_OP(kExprRttSub), array_index, kExprDrop});
}

1149
TEST_F(FunctionBodyDecoderTest, If1) {
1150 1151 1152 1153 1154 1155 1156
  ExpectValidates(sigs.i_i(), {WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_I32V_1(9),
                                              WASM_I32V_1(8))});
  ExpectValidates(sigs.i_i(), {WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_I32V_1(9),
                                              WASM_GET_LOCAL(0))});
  ExpectValidates(
      sigs.i_i(),
      {WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_I32V_1(8))});
1157 1158
}

1159
TEST_F(FunctionBodyDecoderTest, If_off_end) {
1160 1161 1162
  static const byte kCode[] = {
      WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))};
  for (size_t len = 3; len < arraysize(kCode); len++) {
1163 1164
    ExpectFailure(sigs.i_i(), VectorOf(kCode, len), kAppendEnd);
    ExpectFailure(sigs.i_i(), VectorOf(kCode, len), kOmitEnd);
1165 1166 1167
  }
}

1168
TEST_F(FunctionBodyDecoderTest, If_type1) {
1169 1170
  // float|double ? 1 : 2
  static const byte kCode[] = {
1171
      WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_I32V_1(0), WASM_I32V_1(2))};
1172 1173 1174
  ExpectValidates(sigs.i_i(), kCode);
  ExpectFailure(sigs.i_f(), kCode);
  ExpectFailure(sigs.i_d(), kCode);
1175 1176
}

1177
TEST_F(FunctionBodyDecoderTest, If_type2) {
1178 1179
  // 1 ? float|double : 2
  static const byte kCode[] = {
1180
      WASM_IF_ELSE_I(WASM_I32V_1(1), WASM_GET_LOCAL(0), WASM_I32V_1(1))};
1181 1182 1183
  ExpectValidates(sigs.i_i(), kCode);
  ExpectFailure(sigs.i_f(), kCode);
  ExpectFailure(sigs.i_d(), kCode);
1184 1185
}

1186
TEST_F(FunctionBodyDecoderTest, If_type3) {
1187
  // stmt ? 0 : 1
1188
  static const byte kCode[] = {
1189
      WASM_IF_ELSE_I(WASM_NOP, WASM_I32V_1(0), WASM_I32V_1(1))};
1190 1191 1192
  ExpectFailure(sigs.i_i(), kCode);
  ExpectFailure(sigs.i_f(), kCode);
  ExpectFailure(sigs.i_d(), kCode);
1193 1194
}

1195
TEST_F(FunctionBodyDecoderTest, If_type4) {
1196 1197
  // 0 ? stmt : 1
  static const byte kCode[] = {
1198
      WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_NOP, WASM_I32V_1(1))};
1199 1200 1201
  ExpectFailure(sigs.i_i(), kCode);
  ExpectFailure(sigs.i_f(), kCode);
  ExpectFailure(sigs.i_d(), kCode);
1202 1203
}

1204
TEST_F(FunctionBodyDecoderTest, If_type5) {
1205
  // 0 ? 1 : stmt
1206 1207
  static const byte kCode[] = {
      WASM_IF_ELSE_I(WASM_ZERO, WASM_I32V_1(1), WASM_NOP)};
1208 1209 1210
  ExpectFailure(sigs.i_i(), kCode);
  ExpectFailure(sigs.i_f(), kCode);
  ExpectFailure(sigs.i_d(), kCode);
1211 1212
}

1213
TEST_F(FunctionBodyDecoderTest, Int64Local_param) {
1214
  ExpectValidates(sigs.l_l(), kCodeGetLocal0);
1215 1216
}

1217
TEST_F(FunctionBodyDecoderTest, Int64Locals) {
1218
  for (byte i = 1; i < 8; i++) {
1219
    AddLocals(kWasmI64, 1);
1220
    for (byte j = 0; j < i; j++) {
1221
      ExpectValidates(sigs.l_v(), {WASM_GET_LOCAL(j)});
1222 1223 1224 1225
    }
  }
}

1226
TEST_F(FunctionBodyDecoderTest, Int32Binops) {
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
  TestBinop(kExprI32Add, sigs.i_ii());
  TestBinop(kExprI32Sub, sigs.i_ii());
  TestBinop(kExprI32Mul, sigs.i_ii());
  TestBinop(kExprI32DivS, sigs.i_ii());
  TestBinop(kExprI32DivU, sigs.i_ii());
  TestBinop(kExprI32RemS, sigs.i_ii());
  TestBinop(kExprI32RemU, sigs.i_ii());
  TestBinop(kExprI32And, sigs.i_ii());
  TestBinop(kExprI32Ior, sigs.i_ii());
  TestBinop(kExprI32Xor, sigs.i_ii());
  TestBinop(kExprI32Shl, sigs.i_ii());
  TestBinop(kExprI32ShrU, sigs.i_ii());
  TestBinop(kExprI32ShrS, sigs.i_ii());
  TestBinop(kExprI32Eq, sigs.i_ii());
  TestBinop(kExprI32LtS, sigs.i_ii());
  TestBinop(kExprI32LeS, sigs.i_ii());
  TestBinop(kExprI32LtU, sigs.i_ii());
  TestBinop(kExprI32LeU, sigs.i_ii());
}

1247
TEST_F(FunctionBodyDecoderTest, DoubleBinops) {
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  TestBinop(kExprF64Add, sigs.d_dd());
  TestBinop(kExprF64Sub, sigs.d_dd());
  TestBinop(kExprF64Mul, sigs.d_dd());
  TestBinop(kExprF64Div, sigs.d_dd());

  TestBinop(kExprF64Eq, sigs.i_dd());
  TestBinop(kExprF64Lt, sigs.i_dd());
  TestBinop(kExprF64Le, sigs.i_dd());
}

1258
TEST_F(FunctionBodyDecoderTest, FloatBinops) {
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
  TestBinop(kExprF32Add, sigs.f_ff());
  TestBinop(kExprF32Sub, sigs.f_ff());
  TestBinop(kExprF32Mul, sigs.f_ff());
  TestBinop(kExprF32Div, sigs.f_ff());

  TestBinop(kExprF32Eq, sigs.i_ff());
  TestBinop(kExprF32Lt, sigs.i_ff());
  TestBinop(kExprF32Le, sigs.i_ff());
}

1269
TEST_F(FunctionBodyDecoderTest, TypeConversions) {
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
  TestUnop(kExprI32SConvertF32, kWasmI32, kWasmF32);
  TestUnop(kExprI32SConvertF64, kWasmI32, kWasmF64);
  TestUnop(kExprI32UConvertF32, kWasmI32, kWasmF32);
  TestUnop(kExprI32UConvertF64, kWasmI32, kWasmF64);
  TestUnop(kExprF64SConvertI32, kWasmF64, kWasmI32);
  TestUnop(kExprF64UConvertI32, kWasmF64, kWasmI32);
  TestUnop(kExprF64ConvertF32, kWasmF64, kWasmF32);
  TestUnop(kExprF32SConvertI32, kWasmF32, kWasmI32);
  TestUnop(kExprF32UConvertI32, kWasmF32, kWasmI32);
  TestUnop(kExprF32ConvertF64, kWasmF32, kWasmF64);
1280 1281
}

1282
TEST_F(FunctionBodyDecoderTest, MacrosStmt) {
1283
  builder.InitializeMemory();
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
  ExpectValidates(sigs.v_i(), {WASM_SET_LOCAL(0, WASM_I32V_3(87348))});
  ExpectValidates(
      sigs.v_i(),
      {WASM_STORE_MEM(MachineType::Int32(), WASM_I32V_1(24), WASM_I32V_1(40))});
  ExpectValidates(sigs.v_i(), {WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)});
  ExpectValidates(sigs.v_i(),
                  {WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)});
  ExpectValidates(sigs.v_v(), {WASM_NOP});
  ExpectValidates(sigs.v_v(), {B1(WASM_NOP)});
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_NOP)});
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_BR(0))});
1295 1296
}

1297
TEST_F(FunctionBodyDecoderTest, MacrosContinue) {
1298
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_CONTINUE(0))});
1299 1300
}

1301
TEST_F(FunctionBodyDecoderTest, MacrosVariadic) {
1302 1303 1304 1305
  ExpectValidates(sigs.v_v(), {B2(WASM_NOP, WASM_NOP)});
  ExpectValidates(sigs.v_v(), {B3(WASM_NOP, WASM_NOP, WASM_NOP)});
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_NOP, WASM_NOP)});
  ExpectValidates(sigs.v_v(), {WASM_LOOP(WASM_NOP, WASM_NOP, WASM_NOP)});
1306 1307
}

1308
TEST_F(FunctionBodyDecoderTest, MacrosNestedBlocks) {
1309 1310 1311 1312 1313
  ExpectValidates(sigs.v_v(), {B2(WASM_NOP, B2(WASM_NOP, WASM_NOP))});
  ExpectValidates(sigs.v_v(), {B3(WASM_NOP,                   // --
                                  B2(WASM_NOP, WASM_NOP),     // --
                                  B2(WASM_NOP, WASM_NOP))});  // --
  ExpectValidates(sigs.v_v(), {B1(B1(B2(WASM_NOP, WASM_NOP)))});
1314 1315
}

1316
TEST_F(FunctionBodyDecoderTest, MultipleReturn) {
1317 1318
  static ValueType kIntTypes5[] = {kWasmI32, kWasmI32, kWasmI32, kWasmI32,
                                   kWasmI32};
1319
  FunctionSig sig_ii_v(2, 0, kIntTypes5);
1320 1321
  ExpectValidates(&sig_ii_v, {WASM_RETURNN(2, WASM_ZERO, WASM_ONE)});
  ExpectFailure(&sig_ii_v, {WASM_RETURNN(1, WASM_ZERO)});
1322 1323

  FunctionSig sig_iii_v(3, 0, kIntTypes5);
1324 1325 1326
  ExpectValidates(&sig_iii_v,
                  {WASM_RETURNN(3, WASM_ZERO, WASM_ONE, WASM_I32V_1(44))});
  ExpectFailure(&sig_iii_v, {WASM_RETURNN(2, WASM_ZERO, WASM_ONE)});
1327 1328
}

1329
TEST_F(FunctionBodyDecoderTest, MultipleReturn_fallthru) {
1330 1331
  static ValueType kIntTypes5[] = {kWasmI32, kWasmI32, kWasmI32, kWasmI32,
                                   kWasmI32};
1332 1333
  FunctionSig sig_ii_v(2, 0, kIntTypes5);

1334 1335
  ExpectValidates(&sig_ii_v, {WASM_ZERO, WASM_ONE});
  ExpectFailure(&sig_ii_v, {WASM_ZERO});
1336 1337

  FunctionSig sig_iii_v(3, 0, kIntTypes5);
1338 1339
  ExpectValidates(&sig_iii_v, {WASM_ZERO, WASM_ONE, WASM_I32V_1(44)});
  ExpectFailure(&sig_iii_v, {WASM_ZERO, WASM_ONE});
1340 1341
}

1342
TEST_F(FunctionBodyDecoderTest, MacrosInt32) {
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_I32V_1(12))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I32V_1(13))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_MUL(WASM_GET_LOCAL(0), WASM_I32V_1(14))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I32V_1(15))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_I32V_1(16))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_I32V_1(17))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_REMU(WASM_GET_LOCAL(0), WASM_I32V_1(18))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_AND(WASM_GET_LOCAL(0), WASM_I32V_1(19))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_IOR(WASM_GET_LOCAL(0), WASM_I32V_1(20))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_XOR(WASM_GET_LOCAL(0), WASM_I32V_1(21))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_SHL(WASM_GET_LOCAL(0), WASM_I32V_1(22))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_SHR(WASM_GET_LOCAL(0), WASM_I32V_1(23))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_SAR(WASM_GET_LOCAL(0), WASM_I32V_1(24))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_ROR(WASM_GET_LOCAL(0), WASM_I32V_1(24))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_ROL(WASM_GET_LOCAL(0), WASM_I32V_1(24))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I32V_1(25))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_NE(WASM_GET_LOCAL(0), WASM_I32V_1(25))});

  ExpectValidates(sigs.i_i(),
                  {WASM_I32_LTS(WASM_GET_LOCAL(0), WASM_I32V_1(26))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_LES(WASM_GET_LOCAL(0), WASM_I32V_1(27))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_LTU(WASM_GET_LOCAL(0), WASM_I32V_1(28))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_LEU(WASM_GET_LOCAL(0), WASM_I32V_1(29))});

  ExpectValidates(sigs.i_i(),
                  {WASM_I32_GTS(WASM_GET_LOCAL(0), WASM_I32V_1(26))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_GES(WASM_GET_LOCAL(0), WASM_I32V_1(27))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_GTU(WASM_GET_LOCAL(0), WASM_I32V_1(28))});
  ExpectValidates(sigs.i_i(),
                  {WASM_I32_GEU(WASM_GET_LOCAL(0), WASM_I32V_1(29))});
1395 1396
}

1397
TEST_F(FunctionBodyDecoderTest, MacrosInt64) {
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_I64V_1(12))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_I64V_1(13))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_MUL(WASM_GET_LOCAL(0), WASM_I64V_1(14))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_I64V_1(15))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_I64V_1(16))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_I64V_1(17))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_I64V_1(18))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_AND(WASM_GET_LOCAL(0), WASM_I64V_1(19))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_IOR(WASM_GET_LOCAL(0), WASM_I64V_1(20))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_I64V_1(21))});

  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64V_1(22))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64V_1(23))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64V_1(24))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_ROR(WASM_GET_LOCAL(0), WASM_I64V_1(24))});
  ExpectValidates(sigs.l_ll(),
                  {WASM_I64_ROL(WASM_GET_LOCAL(0), WASM_I64V_1(24))});

  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_LTS(WASM_GET_LOCAL(0), WASM_I64V_1(26))});
  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_LES(WASM_GET_LOCAL(0), WASM_I64V_1(27))});
  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_LTU(WASM_GET_LOCAL(0), WASM_I64V_1(28))});
  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_LEU(WASM_GET_LOCAL(0), WASM_I64V_1(29))});

  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_GTS(WASM_GET_LOCAL(0), WASM_I64V_1(26))});
  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_GES(WASM_GET_LOCAL(0), WASM_I64V_1(27))});
  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_GTU(WASM_GET_LOCAL(0), WASM_I64V_1(28))});
  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_GEU(WASM_GET_LOCAL(0), WASM_I64V_1(29))});

  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_EQ(WASM_GET_LOCAL(0), WASM_I64V_1(25))});
  ExpectValidates(sigs.i_ll(),
                  {WASM_I64_NE(WASM_GET_LOCAL(0), WASM_I64V_1(25))});
1452 1453
}

1454
TEST_F(FunctionBodyDecoderTest, AllSimpleExpressions) {
1455
  WASM_FEATURE_SCOPE(reftypes);
1456
// Test all simple expressions which are described by a signature.
1457 1458 1459 1460 1461 1462 1463 1464
#define DECODE_TEST(name, opcode, sig)                            \
  {                                                               \
    const FunctionSig* sig = WasmOpcodes::Signature(kExpr##name); \
    if (sig->parameter_count() == 1) {                            \
      TestUnop(kExpr##name, sig);                                 \
    } else {                                                      \
      TestBinop(kExpr##name, sig);                                \
    }                                                             \
1465 1466 1467 1468 1469 1470 1471
  }

  FOREACH_SIMPLE_OPCODE(DECODE_TEST);

#undef DECODE_TEST
}

1472
TEST_F(FunctionBodyDecoderTest, MemorySize) {
1473
  builder.InitializeMemory();
1474
  byte code[] = {kExprMemorySize, 0};
1475 1476
  ExpectValidates(sigs.i_i(), code);
  ExpectFailure(sigs.f_ff(), code);
1477 1478
}

1479
TEST_F(FunctionBodyDecoderTest, LoadMemOffset) {
1480
  builder.InitializeMemory();
1481
  for (int offset = 0; offset < 128; offset += 7) {
1482
    byte code[] = {kExprI32Const, 0, kExprI32LoadMem, ZERO_ALIGNMENT,
1483
                   static_cast<byte>(offset)};
1484
    ExpectValidates(sigs.i_i(), code);
1485 1486 1487
  }
}

1488
TEST_F(FunctionBodyDecoderTest, LoadMemAlignment) {
1489
  builder.InitializeMemory();
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
  struct {
    WasmOpcode instruction;
    uint32_t maximum_aligment;
  } values[] = {
      {kExprI32LoadMem8U, 0},   // --
      {kExprI32LoadMem8S, 0},   // --
      {kExprI32LoadMem16U, 1},  // --
      {kExprI32LoadMem16S, 1},  // --
      {kExprI64LoadMem8U, 0},   // --
      {kExprI64LoadMem8S, 0},   // --
      {kExprI64LoadMem16U, 1},  // --
      {kExprI64LoadMem16S, 1},  // --
      {kExprI64LoadMem32U, 2},  // --
      {kExprI64LoadMem32S, 2},  // --
      {kExprI32LoadMem, 2},     // --
      {kExprI64LoadMem, 3},     // --
      {kExprF32LoadMem, 2},     // --
      {kExprF64LoadMem, 3},     // --
  };

1510
  for (size_t i = 0; i < arraysize(values); i++) {
1511
    for (byte alignment = 0; alignment <= 4; alignment++) {
1512 1513
      byte code[] = {WASM_ZERO, static_cast<byte>(values[i].instruction),
                     alignment, ZERO_OFFSET, WASM_DROP};
1514
      Validate(alignment <= values[i].maximum_aligment, sigs.v_i(), code);
1515 1516 1517 1518
    }
  }
}

1519
TEST_F(FunctionBodyDecoderTest, StoreMemOffset) {
1520
  builder.InitializeMemory();
1521
  for (byte offset = 0; offset < 128; offset += 7) {
1522 1523
    byte code[] = {WASM_STORE_MEM_OFFSET(MachineType::Int32(), offset,
                                         WASM_ZERO, WASM_ZERO)};
1524
    ExpectValidates(sigs.v_i(), code);
1525 1526 1527
  }
}

1528
TEST_F(FunctionBodyDecoderTest, StoreMemOffset_void) {
1529
  builder.InitializeMemory();
1530 1531
  ExpectFailure(sigs.i_i(), {WASM_STORE_MEM_OFFSET(MachineType::Int32(), 0,
                                                   WASM_ZERO, WASM_ZERO)});
1532 1533
}

1534
TEST_F(FunctionBodyDecoderTest, LoadMemOffset_varint) {
1535
  builder.InitializeMemory();
1536
  ExpectValidates(sigs.i_i(),
1537
                  {WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT, U32V_1(0x45)});
1538
  ExpectValidates(sigs.i_i(), {WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT,
1539
                               U32V_2(0x3999)});
1540
  ExpectValidates(sigs.i_i(), {WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT,
1541
                               U32V_3(0x344445)});
1542
  ExpectValidates(sigs.i_i(), {WASM_ZERO, kExprI32LoadMem, ZERO_ALIGNMENT,
1543
                               U32V_4(0x36666667)});
1544 1545
}

1546
TEST_F(FunctionBodyDecoderTest, StoreMemOffset_varint) {
1547
  builder.InitializeMemory();
1548
  ExpectValidates(sigs.v_i(), {WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
1549
                               ZERO_ALIGNMENT, U32V_1(0x33)});
1550
  ExpectValidates(sigs.v_i(), {WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
1551
                               ZERO_ALIGNMENT, U32V_2(0x1111)});
1552
  ExpectValidates(sigs.v_i(), {WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
1553
                               ZERO_ALIGNMENT, U32V_3(0x222222)});
1554
  ExpectValidates(sigs.v_i(), {WASM_ZERO, WASM_ZERO, kExprI32StoreMem,
1555
                               ZERO_ALIGNMENT, U32V_4(0x44444444)});
1556 1557
}

1558
TEST_F(FunctionBodyDecoderTest, AllLoadMemCombinations) {
1559
  builder.InitializeMemory();
1560 1561
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType local_type = kValueTypes[i];
1562 1563
    for (size_t j = 0; j < arraysize(machineTypes); j++) {
      MachineType mem_type = machineTypes[j];
1564
      byte code[] = {WASM_LOAD_MEM(mem_type, WASM_ZERO)};
1565
      FunctionSig sig(1, 0, &local_type);
1566
      Validate(local_type == ValueType::For(mem_type), &sig, code);
1567 1568 1569 1570
    }
  }
}

1571
TEST_F(FunctionBodyDecoderTest, AllStoreMemCombinations) {
1572
  builder.InitializeMemory();
1573 1574
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType local_type = kValueTypes[i];
1575 1576
    for (size_t j = 0; j < arraysize(machineTypes); j++) {
      MachineType mem_type = machineTypes[j];
1577
      byte code[] = {WASM_STORE_MEM(mem_type, WASM_ZERO, WASM_GET_LOCAL(0))};
1578
      FunctionSig sig(0, 1, &local_type);
1579
      Validate(local_type == ValueType::For(mem_type), &sig, code);
1580 1581 1582 1583
    }
  }
}

1584
TEST_F(FunctionBodyDecoderTest, SimpleCalls) {
1585
  const FunctionSig* sig = sigs.i_i();
1586

1587 1588 1589
  builder.AddFunction(sigs.i_v());
  builder.AddFunction(sigs.i_i());
  builder.AddFunction(sigs.i_ii());
1590

1591 1592 1593 1594
  ExpectValidates(sig, {WASM_CALL_FUNCTION0(0)});
  ExpectValidates(sig, {WASM_CALL_FUNCTION(1, WASM_I32V_1(27))});
  ExpectValidates(sig,
                  {WASM_CALL_FUNCTION(2, WASM_I32V_1(37), WASM_I32V_2(77))});
1595 1596
}

1597
TEST_F(FunctionBodyDecoderTest, CallsWithTooFewArguments) {
1598
  const FunctionSig* sig = sigs.i_i();
1599

1600 1601 1602
  builder.AddFunction(sigs.i_i());
  builder.AddFunction(sigs.i_ii());
  builder.AddFunction(sigs.f_ff());
1603

1604 1605 1606
  ExpectFailure(sig, {WASM_CALL_FUNCTION0(0)});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(1, WASM_ZERO)});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0))});
1607 1608
}

1609
TEST_F(FunctionBodyDecoderTest, CallsWithMismatchedSigs2) {
1610
  const FunctionSig* sig = sigs.i_i();
1611

1612
  builder.AddFunction(sigs.i_i());
1613

1614 1615 1616
  ExpectFailure(sig, {WASM_CALL_FUNCTION(0, WASM_I64V_1(17))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(0, WASM_F32(17.1))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(0, WASM_F64(17.1))});
1617 1618
}

1619
TEST_F(FunctionBodyDecoderTest, CallsWithMismatchedSigs3) {
1620
  const FunctionSig* sig = sigs.i_i();
1621

1622
  builder.AddFunction(sigs.i_f());
1623

1624 1625 1626
  ExpectFailure(sig, {WASM_CALL_FUNCTION(0, WASM_I32V_1(17))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(0, WASM_I64V_1(27))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(0, WASM_F64(37.2))});
1627

1628
  builder.AddFunction(sigs.i_d());
1629

1630 1631 1632
  ExpectFailure(sig, {WASM_CALL_FUNCTION(1, WASM_I32V_1(16))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(1, WASM_I64V_1(16))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(1, WASM_F32(17.6))});
1633 1634
}

1635 1636 1637
TEST_F(FunctionBodyDecoderTest, SimpleReturnCalls) {
  WASM_FEATURE_SCOPE(return_call);

1638
  const FunctionSig* sig = sigs.i_i();
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652

  builder.AddFunction(sigs.i_v());
  builder.AddFunction(sigs.i_i());
  builder.AddFunction(sigs.i_ii());

  ExpectValidates(sig, {WASM_RETURN_CALL_FUNCTION0(0)});
  ExpectValidates(sig, {WASM_RETURN_CALL_FUNCTION(1, WASM_I32V_1(27))});
  ExpectValidates(
      sig, {WASM_RETURN_CALL_FUNCTION(2, WASM_I32V_1(37), WASM_I32V_2(77))});
}

TEST_F(FunctionBodyDecoderTest, ReturnCallsWithTooFewArguments) {
  WASM_FEATURE_SCOPE(return_call);

1653
  const FunctionSig* sig = sigs.i_i();
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666

  builder.AddFunction(sigs.i_i());
  builder.AddFunction(sigs.i_ii());
  builder.AddFunction(sigs.f_ff());

  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION0(0)});
  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(1, WASM_ZERO)});
  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(2, WASM_GET_LOCAL(0))});
}

TEST_F(FunctionBodyDecoderTest, ReturnCallsWithMismatchedSigs) {
  WASM_FEATURE_SCOPE(return_call);

1667
  const FunctionSig* sig = sigs.i_i();
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683

  builder.AddFunction(sigs.i_f());
  builder.AddFunction(sigs.f_f());

  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(0, WASM_I32V_1(17))});
  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(0, WASM_I64V_1(27))});
  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(0, WASM_F64(37.2))});

  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(1, WASM_F64(37.2))});
  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(1, WASM_F32(37.2))});
  ExpectFailure(sig, {WASM_RETURN_CALL_FUNCTION(1, WASM_I32V_1(17))});
}

TEST_F(FunctionBodyDecoderTest, SimpleIndirectReturnCalls) {
  WASM_FEATURE_SCOPE(return_call);

1684
  const FunctionSig* sig = sigs.i_i();
1685
  builder.AddTable(kWasmFuncRef, 20, true, 30);
1686

1687 1688 1689
  byte sig0 = builder.AddSignature(sigs.i_v());
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
1690

1691
  ExpectValidates(sig, {WASM_RETURN_CALL_INDIRECT(sig0, WASM_ZERO)});
1692
  ExpectValidates(
1693 1694 1695
      sig, {WASM_RETURN_CALL_INDIRECT(sig1, WASM_I32V_1(22), WASM_ZERO)});
  ExpectValidates(sig, {WASM_RETURN_CALL_INDIRECT(sig2, WASM_I32V_1(32),
                                                  WASM_I32V_2(72), WASM_ZERO)});
1696 1697 1698 1699 1700
}

TEST_F(FunctionBodyDecoderTest, IndirectReturnCallsOutOfBounds) {
  WASM_FEATURE_SCOPE(return_call);

1701
  const FunctionSig* sig = sigs.i_i();
1702
  builder.AddTable(kWasmFuncRef, 20, false, 20);
1703

1704
  ExpectFailure(sig, {WASM_RETURN_CALL_INDIRECT(0, WASM_ZERO)});
1705
  builder.AddSignature(sigs.i_v());
1706
  ExpectValidates(sig, {WASM_RETURN_CALL_INDIRECT(0, WASM_ZERO)});
1707 1708

  ExpectFailure(sig,
1709
                {WASM_RETURN_CALL_INDIRECT(1, WASM_I32V_1(22), WASM_ZERO)});
1710 1711
  builder.AddSignature(sigs.i_i());
  ExpectValidates(sig,
1712
                  {WASM_RETURN_CALL_INDIRECT(1, WASM_I32V_1(27), WASM_ZERO)});
1713 1714

  ExpectFailure(sig,
1715
                {WASM_RETURN_CALL_INDIRECT(2, WASM_I32V_1(27), WASM_ZERO)});
1716 1717 1718 1719 1720
}

TEST_F(FunctionBodyDecoderTest, IndirectReturnCallsWithMismatchedSigs3) {
  WASM_FEATURE_SCOPE(return_call);

1721
  const FunctionSig* sig = sigs.i_i();
1722
  builder.InitializeTable(wasm::kWasmStmt);
1723

1724
  byte sig0 = builder.AddSignature(sigs.i_f());
1725 1726

  ExpectFailure(sig,
1727
                {WASM_RETURN_CALL_INDIRECT(sig0, WASM_I32V_1(17), WASM_ZERO)});
1728
  ExpectFailure(sig,
1729
                {WASM_RETURN_CALL_INDIRECT(sig0, WASM_I64V_1(27), WASM_ZERO)});
1730
  ExpectFailure(sig,
1731
                {WASM_RETURN_CALL_INDIRECT(sig0, WASM_F64(37.2), WASM_ZERO)});
1732

1733 1734 1735
  ExpectFailure(sig, {WASM_RETURN_CALL_INDIRECT(sig0, WASM_I32V_1(17))});
  ExpectFailure(sig, {WASM_RETURN_CALL_INDIRECT(sig0, WASM_I64V_1(27))});
  ExpectFailure(sig, {WASM_RETURN_CALL_INDIRECT(sig0, WASM_F64(37.2))});
1736

1737
  byte sig1 = builder.AddFunction(sigs.i_d());
1738 1739

  ExpectFailure(sig,
1740
                {WASM_RETURN_CALL_INDIRECT(sig1, WASM_I32V_1(16), WASM_ZERO)});
1741
  ExpectFailure(sig,
1742
                {WASM_RETURN_CALL_INDIRECT(sig1, WASM_I64V_1(16), WASM_ZERO)});
1743
  ExpectFailure(sig,
1744
                {WASM_RETURN_CALL_INDIRECT(sig1, WASM_F32(17.6), WASM_ZERO)});
1745 1746 1747 1748 1749
}

TEST_F(FunctionBodyDecoderTest, IndirectReturnCallsWithoutTableCrash) {
  WASM_FEATURE_SCOPE(return_call);

1750
  const FunctionSig* sig = sigs.i_i();
1751

1752 1753 1754
  byte sig0 = builder.AddSignature(sigs.i_v());
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
1755

1756
  ExpectFailure(sig, {WASM_RETURN_CALL_INDIRECT(sig0, WASM_ZERO)});
1757
  ExpectFailure(sig,
1758 1759 1760
                {WASM_RETURN_CALL_INDIRECT(sig1, WASM_I32V_1(22), WASM_ZERO)});
  ExpectFailure(sig, {WASM_RETURN_CALL_INDIRECT(sig2, WASM_I32V_1(32),
                                                WASM_I32V_2(72), WASM_ZERO)});
1761 1762 1763
}

TEST_F(FunctionBodyDecoderTest, IncompleteIndirectReturnCall) {
1764
  const FunctionSig* sig = sigs.i_i();
1765
  builder.InitializeTable(wasm::kWasmStmt);
1766 1767 1768 1769 1770

  static byte code[] = {kExprReturnCallIndirect};
  ExpectFailure(sig, ArrayVector(code), kOmitEnd);
}

1771
TEST_F(FunctionBodyDecoderTest, MultiReturn) {
1772
  WASM_FEATURE_SCOPE(mv);
1773
  ValueType storage[] = {kWasmI32, kWasmI32};
1774 1775 1776
  FunctionSig sig_ii_v(2, 0, storage);
  FunctionSig sig_v_ii(0, 2, storage);

1777 1778
  builder.AddFunction(&sig_v_ii);
  builder.AddFunction(&sig_ii_v);
1779

1780 1781 1782
  ExpectValidates(&sig_ii_v, {WASM_CALL_FUNCTION0(1)});
  ExpectValidates(sigs.v_v(), {WASM_CALL_FUNCTION0(1), WASM_DROP, WASM_DROP});
  ExpectValidates(sigs.v_v(), {WASM_CALL_FUNCTION0(1), kExprCallFunction, 0});
1783 1784
}

1785
TEST_F(FunctionBodyDecoderTest, MultiReturnType) {
1786
  WASM_FEATURE_SCOPE(mv);
1787 1788 1789 1790 1791
  for (size_t a = 0; a < arraysize(kValueTypes); a++) {
    for (size_t b = 0; b < arraysize(kValueTypes); b++) {
      for (size_t c = 0; c < arraysize(kValueTypes); c++) {
        for (size_t d = 0; d < arraysize(kValueTypes); d++) {
          ValueType storage_ab[] = {kValueTypes[a], kValueTypes[b]};
1792
          FunctionSig sig_ab_v(2, 0, storage_ab);
1793
          ValueType storage_cd[] = {kValueTypes[c], kValueTypes[d]};
1794 1795
          FunctionSig sig_cd_v(2, 0, storage_cd);

1796 1797 1798
          TestModuleBuilder builder;
          module = builder.module();
          builder.AddFunction(&sig_cd_v);
1799

1800
          ExpectValidates(&sig_cd_v, {WASM_CALL_FUNCTION0(0)});
1801

1802 1803
          if (IsSubtypeOf(kValueTypes[c], kValueTypes[a], module) &&
              IsSubtypeOf(kValueTypes[d], kValueTypes[b], module)) {
1804
            ExpectValidates(&sig_ab_v, {WASM_CALL_FUNCTION0(0)});
1805
          } else {
1806
            ExpectFailure(&sig_ab_v, {WASM_CALL_FUNCTION0(0)});
1807 1808 1809 1810 1811
          }
        }
      }
    }
  }
1812 1813
}

1814
TEST_F(FunctionBodyDecoderTest, SimpleIndirectCalls) {
1815
  const FunctionSig* sig = sigs.i_i();
1816
  builder.AddTable(kWasmFuncRef, 20, false, 20);
1817

1818 1819 1820
  byte sig0 = builder.AddSignature(sigs.i_v());
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
1821

1822 1823 1824 1825
  ExpectValidates(sig, {WASM_CALL_INDIRECT(sig0, WASM_ZERO)});
  ExpectValidates(sig, {WASM_CALL_INDIRECT(sig1, WASM_I32V_1(22), WASM_ZERO)});
  ExpectValidates(sig, {WASM_CALL_INDIRECT(sig2, WASM_I32V_1(32),
                                           WASM_I32V_2(72), WASM_ZERO)});
1826 1827
}

1828
TEST_F(FunctionBodyDecoderTest, IndirectCallsOutOfBounds) {
1829
  const FunctionSig* sig = sigs.i_i();
1830
  builder.AddTable(kWasmFuncRef, 20, false, 20);
1831

1832
  ExpectFailure(sig, {WASM_CALL_INDIRECT(0, WASM_ZERO)});
1833
  builder.AddSignature(sigs.i_v());
1834
  ExpectValidates(sig, {WASM_CALL_INDIRECT(0, WASM_ZERO)});
1835

1836
  ExpectFailure(sig, {WASM_CALL_INDIRECT(1, WASM_I32V_1(22), WASM_ZERO)});
1837
  builder.AddSignature(sigs.i_i());
1838
  ExpectValidates(sig, {WASM_CALL_INDIRECT(1, WASM_I32V_1(27), WASM_ZERO)});
1839

1840
  ExpectFailure(sig, {WASM_CALL_INDIRECT(2, WASM_I32V_1(27), WASM_ZERO)});
1841 1842
}

1843
TEST_F(FunctionBodyDecoderTest, IndirectCallsWithMismatchedSigs1) {
1844
  const FunctionSig* sig = sigs.i_i();
1845
  builder.InitializeTable(wasm::kWasmStmt);
1846

1847
  byte sig0 = builder.AddSignature(sigs.i_f());
1848

1849 1850 1851
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig0, WASM_I32V_1(17), WASM_ZERO)});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig0, WASM_I64V_1(27), WASM_ZERO)});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig0, WASM_F64(37.2), WASM_ZERO)});
1852

1853 1854 1855
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig0, WASM_I32V_1(17))});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig0, WASM_I64V_1(27))});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig0, WASM_F64(37.2))});
1856

1857
  byte sig1 = builder.AddFunction(sigs.i_d());
1858

1859 1860 1861
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig1, WASM_I32V_1(16), WASM_ZERO)});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig1, WASM_I64V_1(16), WASM_ZERO)});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig1, WASM_F32(17.6), WASM_ZERO)});
1862 1863
}

1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
TEST_F(FunctionBodyDecoderTest, IndirectCallsWithMismatchedSigs2) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  byte table_type_index = builder.AddSignature(sigs.i_i());
  byte table_index =
      builder.InitializeTable(ValueType::Ref(table_type_index, kNullable));

  ExpectValidates(sigs.i_v(),
                  {WASM_CALL_INDIRECT_TABLE(table_index, table_type_index,
                                            WASM_I32V_1(42), WASM_ZERO)});

  byte wrong_type_index = builder.AddSignature(sigs.i_ii());
  ExpectFailure(sigs.i_v(),
                {WASM_CALL_INDIRECT_TABLE(table_index, wrong_type_index,
                                          WASM_I32V_1(42), WASM_ZERO)},
                kAppendEnd,
                "call_indirect: Immediate signature #1 is not a subtype of "
                "immediate table #0");

  byte non_function_table_index = builder.InitializeTable(kWasmExternRef);
  ExpectFailure(
      sigs.i_v(),
      {WASM_CALL_INDIRECT_TABLE(non_function_table_index, table_type_index,
                                WASM_I32V_1(42), WASM_ZERO)},
      kAppendEnd,
      "call_indirect: immediate table #1 is not of a function type");
}

1892
TEST_F(FunctionBodyDecoderTest, IndirectCallsWithoutTableCrash) {
1893
  const FunctionSig* sig = sigs.i_i();
1894

1895 1896 1897
  byte sig0 = builder.AddSignature(sigs.i_v());
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
1898

1899 1900 1901 1902
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig0, WASM_ZERO)});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig1, WASM_I32V_1(22), WASM_ZERO)});
  ExpectFailure(sig, {WASM_CALL_INDIRECT(sig2, WASM_I32V_1(32), WASM_I32V_2(72),
                                         WASM_ZERO)});
1903 1904
}

1905
TEST_F(FunctionBodyDecoderTest, IncompleteIndirectCall) {
1906
  const FunctionSig* sig = sigs.i_i();
1907
  builder.InitializeTable(wasm::kWasmStmt);
1908 1909

  static byte code[] = {kExprCallIndirect};
1910
  ExpectFailure(sig, ArrayVector(code), kOmitEnd);
1911 1912 1913
}

TEST_F(FunctionBodyDecoderTest, IncompleteStore) {
1914
  const FunctionSig* sig = sigs.i_i();
1915
  builder.InitializeMemory();
1916
  builder.InitializeTable(wasm::kWasmStmt);
1917 1918

  static byte code[] = {kExprI32StoreMem};
1919
  ExpectFailure(sig, ArrayVector(code), kOmitEnd);
1920 1921
}

1922
TEST_F(FunctionBodyDecoderTest, IncompleteI8x16Shuffle) {
1923
  WASM_FEATURE_SCOPE(simd);
1924
  const FunctionSig* sig = sigs.i_i();
1925
  builder.InitializeMemory();
1926
  builder.InitializeTable(wasm::kWasmStmt);
1927 1928

  static byte code[] = {kSimdPrefix,
1929
                        static_cast<byte>(kExprI8x16Shuffle & 0xff)};
1930
  ExpectFailure(sig, ArrayVector(code), kOmitEnd);
1931 1932
}

1933
TEST_F(FunctionBodyDecoderTest, SimpleImportCalls) {
1934
  const FunctionSig* sig = sigs.i_i();
1935

1936 1937 1938
  byte f0 = builder.AddImport(sigs.i_v());
  byte f1 = builder.AddImport(sigs.i_i());
  byte f2 = builder.AddImport(sigs.i_ii());
1939

1940 1941 1942 1943
  ExpectValidates(sig, {WASM_CALL_FUNCTION0(f0)});
  ExpectValidates(sig, {WASM_CALL_FUNCTION(f1, WASM_I32V_1(22))});
  ExpectValidates(sig,
                  {WASM_CALL_FUNCTION(f2, WASM_I32V_1(32), WASM_I32V_2(72))});
1944 1945
}

1946
TEST_F(FunctionBodyDecoderTest, ImportCallsWithMismatchedSigs3) {
1947
  const FunctionSig* sig = sigs.i_i();
1948

1949
  byte f0 = builder.AddImport(sigs.i_f());
1950

1951 1952 1953 1954
  ExpectFailure(sig, {WASM_CALL_FUNCTION0(f0)});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(f0, WASM_I32V_1(17))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(f0, WASM_I64V_1(27))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(f0, WASM_F64(37.2))});
1955

1956
  byte f1 = builder.AddImport(sigs.i_d());
1957

1958 1959 1960 1961
  ExpectFailure(sig, {WASM_CALL_FUNCTION0(f1)});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(f1, WASM_I32V_1(16))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(f1, WASM_I64V_1(16))});
  ExpectFailure(sig, {WASM_CALL_FUNCTION(f1, WASM_F32(17.6))});
1962
}
1963

1964
TEST_F(FunctionBodyDecoderTest, Int32Globals) {
1965
  const FunctionSig* sig = sigs.i_i();
1966

1967
  builder.AddGlobal(kWasmI32);
1968

1969 1970 1971
  ExpectValidates(sig, {WASM_GET_GLOBAL(0)});
  ExpectFailure(sig, {WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0))});
  ExpectValidates(sig, {WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0)), WASM_ZERO});
1972 1973
}

1974
TEST_F(FunctionBodyDecoderTest, ImmutableGlobal) {
1975
  const FunctionSig* sig = sigs.v_v();
1976

1977 1978
  uint32_t g0 = builder.AddGlobal(kWasmI32, true);
  uint32_t g1 = builder.AddGlobal(kWasmI32, false);
1979

1980 1981
  ExpectValidates(sig, {WASM_SET_GLOBAL(g0, WASM_ZERO)});
  ExpectFailure(sig, {WASM_SET_GLOBAL(g1, WASM_ZERO)});
1982 1983
}

1984
TEST_F(FunctionBodyDecoderTest, Int32Globals_fail) {
1985
  const FunctionSig* sig = sigs.i_i();
1986

1987 1988 1989 1990
  builder.AddGlobal(kWasmI64);
  builder.AddGlobal(kWasmI64);
  builder.AddGlobal(kWasmF32);
  builder.AddGlobal(kWasmF64);
1991

1992 1993 1994 1995
  ExpectFailure(sig, {WASM_GET_GLOBAL(0)});
  ExpectFailure(sig, {WASM_GET_GLOBAL(1)});
  ExpectFailure(sig, {WASM_GET_GLOBAL(2)});
  ExpectFailure(sig, {WASM_GET_GLOBAL(3)});
1996

1997 1998 1999 2000
  ExpectFailure(sig, {WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0)), WASM_ZERO});
  ExpectFailure(sig, {WASM_SET_GLOBAL(1, WASM_GET_LOCAL(0)), WASM_ZERO});
  ExpectFailure(sig, {WASM_SET_GLOBAL(2, WASM_GET_LOCAL(0)), WASM_ZERO});
  ExpectFailure(sig, {WASM_SET_GLOBAL(3, WASM_GET_LOCAL(0)), WASM_ZERO});
2001 2002
}

2003
TEST_F(FunctionBodyDecoderTest, Int64Globals) {
2004
  const FunctionSig* sig = sigs.l_l();
2005

2006 2007
  builder.AddGlobal(kWasmI64);
  builder.AddGlobal(kWasmI64);
2008

2009 2010
  ExpectValidates(sig, {WASM_GET_GLOBAL(0)});
  ExpectValidates(sig, {WASM_GET_GLOBAL(1)});
2011

2012 2013 2014 2015
  ExpectValidates(sig,
                  {WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0)), WASM_GET_LOCAL(0)});
  ExpectValidates(sig,
                  {WASM_SET_GLOBAL(1, WASM_GET_LOCAL(0)), WASM_GET_LOCAL(0)});
2016 2017
}

2018
TEST_F(FunctionBodyDecoderTest, Float32Globals) {
2019
  const FunctionSig* sig = sigs.f_ff();
2020

2021
  builder.AddGlobal(kWasmF32);
2022

2023 2024 2025
  ExpectValidates(sig, {WASM_GET_GLOBAL(0)});
  ExpectValidates(sig,
                  {WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0)), WASM_GET_LOCAL(0)});
2026 2027
}

2028
TEST_F(FunctionBodyDecoderTest, Float64Globals) {
2029
  const FunctionSig* sig = sigs.d_dd();
2030

2031
  builder.AddGlobal(kWasmF64);
2032

2033 2034 2035
  ExpectValidates(sig, {WASM_GET_GLOBAL(0)});
  ExpectValidates(sig,
                  {WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0)), WASM_GET_LOCAL(0)});
2036 2037
}

2038
TEST_F(FunctionBodyDecoderTest, AllGetGlobalCombinations) {
2039 2040 2041 2042
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType local_type = kValueTypes[i];
    for (size_t j = 0; j < arraysize(kValueTypes); j++) {
      ValueType global_type = kValueTypes[j];
2043
      FunctionSig sig(1, 0, &local_type);
2044 2045 2046
      TestModuleBuilder builder;
      module = builder.module();
      builder.AddGlobal(global_type);
2047
      Validate(IsSubtypeOf(global_type, local_type, module), &sig,
2048
               {WASM_GET_GLOBAL(0)});
2049 2050 2051 2052
    }
  }
}

2053
TEST_F(FunctionBodyDecoderTest, AllSetGlobalCombinations) {
2054 2055 2056 2057
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType local_type = kValueTypes[i];
    for (size_t j = 0; j < arraysize(kValueTypes); j++) {
      ValueType global_type = kValueTypes[j];
2058
      FunctionSig sig(0, 1, &local_type);
2059 2060 2061
      TestModuleBuilder builder;
      module = builder.module();
      builder.AddGlobal(global_type);
2062
      Validate(IsSubtypeOf(local_type, global_type, module), &sig,
2063
               {WASM_SET_GLOBAL(0, WASM_GET_LOCAL(0))});
2064 2065 2066 2067
    }
  }
}

2068
TEST_F(FunctionBodyDecoderTest, TableSet) {
2069
  WASM_FEATURE_SCOPE(reftypes);
2070 2071 2072
  WASM_FEATURE_SCOPE(typed_funcref);

  byte tab_type = builder.AddSignature(sigs.i_i());
2073
  byte tab_ref1 = builder.AddTable(kWasmExternRef, 10, true, 20);
2074 2075
  byte tab_func1 = builder.AddTable(kWasmFuncRef, 20, true, 30);
  byte tab_func2 = builder.AddTable(kWasmFuncRef, 10, false, 20);
2076
  byte tab_ref2 = builder.AddTable(kWasmExternRef, 10, false, 20);
2077 2078 2079 2080 2081 2082
  byte tab_typed_func =
      builder.AddTable(ValueType::Ref(tab_type, kNullable), 10, false, 20);

  ValueType sig_types[]{kWasmExternRef, kWasmFuncRef, kWasmI32,
                        ValueType::Ref(tab_type, kNonNullable)};
  FunctionSig sig(0, 4, sig_types);
2083 2084
  byte local_ref = 0;
  byte local_func = 1;
2085
  byte local_int = 2;
2086 2087
  byte local_typed_func = 3;

2088
  ExpectValidates(&sig, {WASM_TABLE_SET(tab_ref1, WASM_I32V(6),
2089
                                        WASM_GET_LOCAL(local_ref))});
2090
  ExpectValidates(&sig, {WASM_TABLE_SET(tab_func1, WASM_I32V(5),
2091
                                        WASM_GET_LOCAL(local_func))});
2092
  ExpectValidates(&sig, {WASM_TABLE_SET(tab_func2, WASM_I32V(7),
2093
                                        WASM_GET_LOCAL(local_func))});
2094
  ExpectValidates(&sig, {WASM_TABLE_SET(tab_ref2, WASM_I32V(8),
2095
                                        WASM_GET_LOCAL(local_ref))});
2096 2097 2098 2099
  ExpectValidates(&sig, {WASM_TABLE_SET(tab_typed_func, WASM_I32V(8),
                                        WASM_GET_LOCAL(local_typed_func))});
  ExpectValidates(&sig, {WASM_TABLE_SET(tab_func1, WASM_I32V(8),
                                        WASM_GET_LOCAL(local_typed_func))});
2100

2101 2102 2103
  // Only values of the correct type can be set to a table.
  ExpectFailure(&sig, {WASM_TABLE_SET(tab_ref1, WASM_I32V(4),
                                      WASM_GET_LOCAL(local_func))});
2104
  ExpectFailure(&sig, {WASM_TABLE_SET(tab_func1, WASM_I32V(9),
2105
                                      WASM_GET_LOCAL(local_ref))});
2106
  ExpectFailure(&sig, {WASM_TABLE_SET(tab_func2, WASM_I32V(3),
2107
                                      WASM_GET_LOCAL(local_ref))});
2108 2109
  ExpectFailure(&sig, {WASM_TABLE_SET(tab_ref2, WASM_I32V(2),
                                      WASM_GET_LOCAL(local_func))});
2110
  ExpectFailure(&sig, {WASM_TABLE_SET(tab_ref1, WASM_I32V(9),
2111
                                      WASM_GET_LOCAL(local_int))});
2112
  ExpectFailure(&sig, {WASM_TABLE_SET(tab_func1, WASM_I32V(3),
2113
                                      WASM_GET_LOCAL(local_int))});
2114 2115
  ExpectFailure(&sig, {WASM_TABLE_SET(tab_typed_func, WASM_I32V(3),
                                      WASM_GET_LOCAL(local_func))});
2116

2117 2118
  // Out-of-bounds table index should fail.
  byte oob_tab = 37;
2119
  ExpectFailure(
2120 2121
      &sig, {WASM_TABLE_SET(oob_tab, WASM_I32V(9), WASM_GET_LOCAL(local_ref))});
  ExpectFailure(&sig, {WASM_TABLE_SET(oob_tab, WASM_I32V(3),
2122
                                      WASM_GET_LOCAL(local_func))});
2123 2124
}

2125
TEST_F(FunctionBodyDecoderTest, TableGet) {
2126
  WASM_FEATURE_SCOPE(reftypes);
2127 2128 2129
  WASM_FEATURE_SCOPE(typed_funcref);

  byte tab_type = builder.AddSignature(sigs.i_i());
2130
  byte tab_ref1 = builder.AddTable(kWasmExternRef, 10, true, 20);
2131 2132
  byte tab_func1 = builder.AddTable(kWasmFuncRef, 20, true, 30);
  byte tab_func2 = builder.AddTable(kWasmFuncRef, 10, false, 20);
2133
  byte tab_ref2 = builder.AddTable(kWasmExternRef, 10, false, 20);
2134 2135 2136 2137 2138 2139
  byte tab_typed_func =
      builder.AddTable(ValueType::Ref(tab_type, kNullable), 10, false, 20);

  ValueType sig_types[]{kWasmExternRef, kWasmFuncRef, kWasmI32,
                        ValueType::Ref(tab_type, kNullable)};
  FunctionSig sig(0, 4, sig_types);
2140 2141 2142
  byte local_ref = 0;
  byte local_func = 1;
  byte local_int = 2;
2143 2144
  byte local_typed_func = 3;

2145
  ExpectValidates(
2146
      &sig,
2147
      {WASM_SET_LOCAL(local_ref, WASM_TABLE_GET(tab_ref1, WASM_I32V(6)))});
2148
  ExpectValidates(
2149
      &sig,
2150
      {WASM_SET_LOCAL(local_ref, WASM_TABLE_GET(tab_ref2, WASM_I32V(8)))});
2151 2152
  ExpectValidates(
      &sig,
2153
      {WASM_SET_LOCAL(local_func, WASM_TABLE_GET(tab_func1, WASM_I32V(5)))});
2154 2155
  ExpectValidates(
      &sig,
2156
      {WASM_SET_LOCAL(local_func, WASM_TABLE_GET(tab_func2, WASM_I32V(7)))});
2157 2158 2159
  ExpectValidates(
      &sig, {WASM_SET_LOCAL(local_ref, WASM_SEQ(WASM_I32V(6), kExprTableGet,
                                                U32V_2(tab_ref1)))});
2160 2161 2162 2163 2164 2165
  ExpectValidates(
      &sig, {WASM_SET_LOCAL(local_func,
                            WASM_TABLE_GET(tab_typed_func, WASM_I32V(7)))});
  ExpectValidates(
      &sig, {WASM_SET_LOCAL(local_typed_func,
                            WASM_TABLE_GET(tab_typed_func, WASM_I32V(7)))});
2166

2167
  // We cannot store references as any other type.
2168
  ExpectFailure(&sig, {WASM_SET_LOCAL(local_func,
2169
                                      WASM_TABLE_GET(tab_ref1, WASM_I32V(4)))});
2170 2171 2172 2173
  ExpectFailure(&sig, {WASM_SET_LOCAL(
                          local_ref, WASM_TABLE_GET(tab_func1, WASM_I32V(9)))});
  ExpectFailure(&sig, {WASM_SET_LOCAL(
                          local_ref, WASM_TABLE_GET(tab_func2, WASM_I32V(3)))});
2174
  ExpectFailure(&sig, {WASM_SET_LOCAL(local_func,
2175
                                      WASM_TABLE_GET(tab_ref2, WASM_I32V(2)))});
2176 2177

  ExpectFailure(&sig, {WASM_SET_LOCAL(local_int,
2178
                                      WASM_TABLE_GET(tab_ref1, WASM_I32V(9)))});
2179
  ExpectFailure(&sig, {WASM_SET_LOCAL(
2180
                          local_int, WASM_TABLE_GET(tab_func1, WASM_I32V(3)))});
2181 2182 2183 2184
  ExpectFailure(&sig,
                {WASM_SET_LOCAL(local_typed_func,
                                WASM_TABLE_GET(tab_func1, WASM_I32V(3)))});

2185 2186
  // Out-of-bounds table index should fail.
  byte oob_tab = 37;
2187
  ExpectFailure(
2188
      &sig, {WASM_SET_LOCAL(local_ref, WASM_TABLE_GET(oob_tab, WASM_I32V(9)))});
2189
  ExpectFailure(&sig, {WASM_SET_LOCAL(local_func,
2190
                                      WASM_TABLE_GET(oob_tab, WASM_I32V(3)))});
2191 2192
}

2193
TEST_F(FunctionBodyDecoderTest, MultiTableCallIndirect) {
2194 2195
  WASM_FEATURE_SCOPE(reftypes);
  byte tab_ref = builder.AddTable(kWasmExternRef, 10, true, 20);
2196
  byte tab_func = builder.AddTable(kWasmFuncRef, 20, true, 30);
2197

2198
  ValueType sig_types[]{kWasmExternRef, kWasmFuncRef, kWasmI32};
2199 2200 2201
  FunctionSig sig(0, 3, sig_types);
  byte sig_index = builder.AddSignature(sigs.i_v());

2202
  // We can store funcref values as externref, but not the other way around.
2203 2204 2205 2206 2207 2208 2209
  ExpectValidates(sigs.i_v(),
                  {kExprI32Const, 0, kExprCallIndirect, sig_index, tab_func});

  ExpectFailure(sigs.i_v(),
                {kExprI32Const, 0, kExprCallIndirect, sig_index, tab_ref});
}

2210
TEST_F(FunctionBodyDecoderTest, WasmMemoryGrow) {
2211
  builder.InitializeMemory();
2212

2213
  byte code[] = {WASM_GET_LOCAL(0), kExprMemoryGrow, 0};
2214 2215
  ExpectValidates(sigs.i_i(), code);
  ExpectFailure(sigs.i_d(), code);
2216 2217
}

2218
TEST_F(FunctionBodyDecoderTest, AsmJsMemoryGrow) {
2219
  module->origin = kAsmJsSloppyOrigin;
2220
  builder.InitializeMemory();
2221

2222
  byte code[] = {WASM_GET_LOCAL(0), kExprMemoryGrow, 0};
2223
  ExpectFailure(sigs.i_i(), code);
2224 2225
}

2226
TEST_F(FunctionBodyDecoderTest, AsmJsBinOpsCheckOrigin) {
2227
  ValueType float32int32float32[] = {kWasmF32, kWasmI32, kWasmF32};
2228
  FunctionSig sig_f_if(1, 2, float32int32float32);
2229
  ValueType float64int32float64[] = {kWasmF64, kWasmI32, kWasmF64};
2230 2231 2232
  FunctionSig sig_d_id(1, 2, float64int32float64);
  struct {
    WasmOpcode op;
2233
    const FunctionSig* sig;
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
  } AsmJsBinOps[] = {
      {kExprF64Atan2, sigs.d_dd()},
      {kExprF64Pow, sigs.d_dd()},
      {kExprF64Mod, sigs.d_dd()},
      {kExprI32AsmjsDivS, sigs.i_ii()},
      {kExprI32AsmjsDivU, sigs.i_ii()},
      {kExprI32AsmjsRemS, sigs.i_ii()},
      {kExprI32AsmjsRemU, sigs.i_ii()},
      {kExprI32AsmjsStoreMem8, sigs.i_ii()},
      {kExprI32AsmjsStoreMem16, sigs.i_ii()},
      {kExprI32AsmjsStoreMem, sigs.i_ii()},
      {kExprF32AsmjsStoreMem, &sig_f_if},
      {kExprF64AsmjsStoreMem, &sig_d_id},
  };

  {
2250
    TestModuleBuilder builder(kAsmJsSloppyOrigin);
2251 2252
    module = builder.module();
    builder.InitializeMemory();
2253
    for (size_t i = 0; i < arraysize(AsmJsBinOps); i++) {
2254 2255 2256 2257 2258
      TestBinop(AsmJsBinOps[i].op, AsmJsBinOps[i].sig);
    }
  }

  {
2259 2260 2261
    TestModuleBuilder builder;
    module = builder.module();
    builder.InitializeMemory();
2262
    for (size_t i = 0; i < arraysize(AsmJsBinOps); i++) {
2263 2264 2265
      ExpectFailure(AsmJsBinOps[i].sig,
                    {WASM_BINOP(AsmJsBinOps[i].op, WASM_GET_LOCAL(0),
                                WASM_GET_LOCAL(1))});
2266 2267 2268 2269
    }
  }
}

2270
TEST_F(FunctionBodyDecoderTest, AsmJsUnOpsCheckOrigin) {
2271
  ValueType float32int32[] = {kWasmF32, kWasmI32};
2272
  FunctionSig sig_f_i(1, 1, float32int32);
2273
  ValueType float64int32[] = {kWasmF64, kWasmI32};
2274
  FunctionSig sig_d_i(1, 1, float64int32);
2275 2276
  struct {
    WasmOpcode op;
2277
    const FunctionSig* sig;
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
  } AsmJsUnOps[] = {{kExprF64Acos, sigs.d_d()},
                    {kExprF64Asin, sigs.d_d()},
                    {kExprF64Atan, sigs.d_d()},
                    {kExprF64Cos, sigs.d_d()},
                    {kExprF64Sin, sigs.d_d()},
                    {kExprF64Tan, sigs.d_d()},
                    {kExprF64Exp, sigs.d_d()},
                    {kExprF64Log, sigs.d_d()},
                    {kExprI32AsmjsLoadMem8S, sigs.i_i()},
                    {kExprI32AsmjsLoadMem8U, sigs.i_i()},
                    {kExprI32AsmjsLoadMem16S, sigs.i_i()},
                    {kExprI32AsmjsLoadMem16U, sigs.i_i()},
                    {kExprI32AsmjsLoadMem, sigs.i_i()},
                    {kExprF32AsmjsLoadMem, &sig_f_i},
                    {kExprF64AsmjsLoadMem, &sig_d_i},
                    {kExprI32AsmjsSConvertF32, sigs.i_f()},
                    {kExprI32AsmjsUConvertF32, sigs.i_f()},
                    {kExprI32AsmjsSConvertF64, sigs.i_d()},
                    {kExprI32AsmjsUConvertF64, sigs.i_d()}};
  {
2298
    TestModuleBuilder builder(kAsmJsSloppyOrigin);
2299 2300
    module = builder.module();
    builder.InitializeMemory();
2301
    for (size_t i = 0; i < arraysize(AsmJsUnOps); i++) {
2302 2303 2304 2305 2306
      TestUnop(AsmJsUnOps[i].op, AsmJsUnOps[i].sig);
    }
  }

  {
2307 2308 2309
    TestModuleBuilder builder;
    module = builder.module();
    builder.InitializeMemory();
2310
    for (size_t i = 0; i < arraysize(AsmJsUnOps); i++) {
2311 2312
      ExpectFailure(AsmJsUnOps[i].sig,
                    {WASM_UNOP(AsmJsUnOps[i].op, WASM_GET_LOCAL(0))});
2313 2314 2315 2316
    }
  }
}

2317
TEST_F(FunctionBodyDecoderTest, BreakEnd) {
2318 2319 2320 2321 2322 2323
  ExpectValidates(
      sigs.i_i(),
      {WASM_BLOCK_I(WASM_I32_ADD(WASM_BRV(0, WASM_ZERO), WASM_ZERO))});
  ExpectValidates(
      sigs.i_i(),
      {WASM_BLOCK_I(WASM_I32_ADD(WASM_ZERO, WASM_BRV(0, WASM_ZERO)))});
2324 2325
}

2326
TEST_F(FunctionBodyDecoderTest, BreakIfBinop) {
2327 2328 2329 2330 2331 2332 2333
  ExpectValidates(sigs.i_i(),
                  {WASM_BLOCK_I(WASM_I32_ADD(
                      WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO), WASM_ZERO))});
  ExpectValidates(sigs.i_i(),
                  {WASM_BLOCK_I(WASM_I32_ADD(
                      WASM_ZERO, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO)))});
  ExpectValidates(
2334
      sigs.f_ff(),
2335
      {WASM_BLOCK_F(WASM_F32_ABS(WASM_BRV_IF(0, WASM_F32(0.0f), WASM_ZERO)))});
2336 2337
}

2338
TEST_F(FunctionBodyDecoderTest, BreakIfBinop_fail) {
2339
  ExpectFailure(
2340
      sigs.f_ff(),
2341 2342
      {WASM_BLOCK_F(WASM_F32_ABS(WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO)))});
  ExpectFailure(
2343
      sigs.i_i(),
2344
      {WASM_BLOCK_I(WASM_F32_ABS(WASM_BRV_IF(0, WASM_F32(0.0f), WASM_ZERO)))});
2345 2346
}

2347
TEST_F(FunctionBodyDecoderTest, BreakIfUnrNarrow) {
2348
  ExpectFailure(
2349
      sigs.f_ff(),
2350 2351 2352
      {WASM_BLOCK_I(WASM_BRV_IF(0, WASM_UNREACHABLE, WASM_UNREACHABLE),
                    WASM_RETURN0),
       WASM_F32(0.0)});
2353 2354
}

2355
TEST_F(FunctionBodyDecoderTest, BreakNesting1) {
2356 2357
  for (int i = 0; i < 5; i++) {
    // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) p)
2358
    byte code[] = {WASM_BLOCK_I(
2359
        WASM_LOOP(WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(i + 1, WASM_ZERO)),
2360
                  WASM_SET_LOCAL(0, WASM_I32V_1(1))),
2361
        WASM_ZERO)};
2362
    Validate(i < 3, sigs.i_i(), code);
2363 2364 2365
  }
}

2366
TEST_F(FunctionBodyDecoderTest, BreakNesting2) {
2367 2368
  for (int i = 0; i < 7; i++) {
    byte code[] = {B1(WASM_LOOP(WASM_IF(WASM_ZERO, WASM_BR(i)), WASM_NOP))};
2369
    Validate(i <= 3, sigs.v_v(), code);
2370 2371 2372
  }
}

2373
TEST_F(FunctionBodyDecoderTest, BreakNesting3) {
2374
  for (int i = 0; i < 7; i++) {
2375
    // (block[1] (loop[1] (block[1] (if 0 break[N])
2376
    byte code[] = {
2377
        WASM_BLOCK(WASM_LOOP(B1(WASM_IF(WASM_ZERO, WASM_BR(i + 1)))))};
2378
    Validate(i < 4, sigs.v_v(), code);
2379 2380 2381
  }
}

2382
TEST_F(FunctionBodyDecoderTest, BreaksWithMultipleTypes) {
2383 2384
  ExpectFailure(sigs.i_i(),
                {B2(WASM_BRV_IF_ZERO(0, WASM_I32V_1(7)), WASM_F32(7.7))});
2385

2386 2387 2388 2389 2390 2391 2392 2393
  ExpectFailure(sigs.i_i(), {B2(WASM_BRV_IF_ZERO(0, WASM_I32V_1(7)),
                                WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))});
  ExpectFailure(sigs.i_i(), {B3(WASM_BRV_IF_ZERO(0, WASM_I32V_1(8)),
                                WASM_BRV_IF_ZERO(0, WASM_I32V_1(0)),
                                WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))});
  ExpectFailure(sigs.i_i(), {B3(WASM_BRV_IF_ZERO(0, WASM_I32V_1(9)),
                                WASM_BRV_IF_ZERO(0, WASM_F32(7.7)),
                                WASM_BRV_IF_ZERO(0, WASM_I32V_1(11)))});
2394 2395
}

2396
TEST_F(FunctionBodyDecoderTest, BreakNesting_6_levels) {
2397 2398
  for (int mask = 0; mask < 64; mask++) {
    for (int i = 0; i < 14; i++) {
2399 2400
      byte code[] = {WASM_BLOCK(WASM_BLOCK(
          WASM_BLOCK(WASM_BLOCK(WASM_BLOCK(WASM_BLOCK(WASM_BR(i)))))))};
2401 2402

      int depth = 6;
2403
      int m = mask;
2404
      for (size_t pos = 0; pos < sizeof(code) - 1; pos++) {
2405 2406 2407
        if (code[pos] != kExprBlock) continue;
        if (m & 1) {
          code[pos] = kExprLoop;
2408
          code[pos + 1] = kVoidCode;
2409
        }
2410
        m >>= 1;
2411 2412
      }

2413
      Validate(i <= depth, sigs.v_v(), code);
2414 2415 2416 2417
    }
  }
}

2418
TEST_F(FunctionBodyDecoderTest, Break_TypeCheck) {
2419 2420
  for (const FunctionSig* sig :
       {sigs.i_i(), sigs.l_l(), sigs.f_ff(), sigs.d_dd()}) {
2421
    // unify X and X => OK
2422 2423 2424
    byte code[] = {WASM_BLOCK_T(
        sig->GetReturn(), WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))),
        WASM_GET_LOCAL(0))};
2425
    ExpectValidates(sig, code);
2426 2427 2428
  }

  // unify i32 and f32 => fail
2429 2430 2431
  ExpectFailure(sigs.i_i(),
                {WASM_BLOCK_I(WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_ZERO)),
                              WASM_F32(1.2))});
2432 2433

  // unify f64 and f64 => OK
2434 2435 2436 2437
  ExpectValidates(
      sigs.d_dd(),
      {WASM_BLOCK_D(WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))),
                    WASM_F64(1.2))});
2438 2439
}

2440
TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll1) {
2441
  WASM_FEATURE_SCOPE(reftypes);
2442 2443 2444
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    for (size_t j = 0; j < arraysize(kValueTypes); j++) {
      ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
2445 2446 2447 2448
      FunctionSig sig(1, 2, storage);
      byte code[] = {WASM_BLOCK_T(
          sig.GetReturn(), WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))),
          WASM_GET_LOCAL(1))};
2449

2450
      Validate(IsSubtypeOf(kValueTypes[j], kValueTypes[i], module), &sig, code);
2451 2452 2453 2454
    }
  }
}

2455
TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll2) {
2456
  WASM_FEATURE_SCOPE(reftypes);
2457 2458 2459
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    for (size_t j = 0; j < arraysize(kValueTypes); j++) {
      ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
2460 2461 2462 2463 2464
      FunctionSig sig(1, 2, storage);
      byte code[] = {WASM_IF_ELSE_T(sig.GetReturn(0), WASM_ZERO,
                                    WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)),
                                    WASM_GET_LOCAL(1))};

2465
      Validate(IsSubtypeOf(kValueTypes[j], kValueTypes[i], module), &sig, code);
2466 2467 2468 2469
    }
  }
}

2470
TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll3) {
2471
  WASM_FEATURE_SCOPE(reftypes);
2472 2473 2474
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    for (size_t j = 0; j < arraysize(kValueTypes); j++) {
      ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
2475
      FunctionSig sig(1, 2, storage);
2476 2477 2478
      byte code[] = {WASM_IF_ELSE_T(sig.GetReturn(), WASM_ZERO,
                                    WASM_GET_LOCAL(1),
                                    WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)))};
2479

2480
      Validate(IsSubtypeOf(kValueTypes[j], kValueTypes[i], module), &sig, code);
2481 2482 2483 2484
    }
  }
}

2485
TEST_F(FunctionBodyDecoderTest, Break_Unify) {
2486
  for (int which = 0; which < 2; which++) {
2487 2488 2489
    for (size_t i = 0; i < arraysize(kValueTypes); i++) {
      ValueType type = kValueTypes[i];
      ValueType storage[] = {kWasmI32, kWasmI32, type};
2490 2491
      FunctionSig sig(1, 2, storage);

2492 2493 2494
      byte code1[] = {WASM_BLOCK_T(
          type, WASM_IF(WASM_ZERO, WASM_BRV(1, WASM_GET_LOCAL(which))),
          WASM_GET_LOCAL(which ^ 1))};
2495

2496
      Validate(type == kWasmI32, &sig, code1);
2497 2498 2499 2500
    }
  }
}

2501
TEST_F(FunctionBodyDecoderTest, BreakIf_cond_type) {
2502
  WASM_FEATURE_SCOPE(reftypes);
2503 2504 2505
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    for (size_t j = 0; j < arraysize(kValueTypes); j++) {
      ValueType types[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
2506 2507 2508
      FunctionSig sig(1, 2, types);
      byte code[] = {WASM_BLOCK_T(
          types[0], WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))};
2509

2510
      Validate(types[2] == kWasmI32, &sig, code);
2511 2512 2513
    }
  }
}
2514

2515
TEST_F(FunctionBodyDecoderTest, BreakIf_val_type) {
2516
  WASM_FEATURE_SCOPE(reftypes);
2517 2518 2519 2520
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    for (size_t j = 0; j < arraysize(kValueTypes); j++) {
      ValueType types[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j],
                           kWasmI32};
2521
      FunctionSig sig(1, 3, types);
2522 2523 2524
      byte code[] = {WASM_BLOCK_T(
          types[1], WASM_BRV_IF(0, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
          WASM_DROP, WASM_GET_LOCAL(0))};
2525

2526
      Validate(IsSubtypeOf(kValueTypes[j], kValueTypes[i], module), &sig, code);
2527 2528 2529 2530
    }
  }
}

2531
TEST_F(FunctionBodyDecoderTest, BreakIf_Unify) {
2532
  for (int which = 0; which < 2; which++) {
2533 2534 2535
    for (size_t i = 0; i < arraysize(kValueTypes); i++) {
      ValueType type = kValueTypes[i];
      ValueType storage[] = {kWasmI32, kWasmI32, type};
2536
      FunctionSig sig(1, 2, storage);
2537 2538
      byte code[] = {WASM_BLOCK_I(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(which)),
                                  WASM_DROP, WASM_GET_LOCAL(which ^ 1))};
2539

2540
      Validate(type == kWasmI32, &sig, code);
2541 2542 2543 2544
    }
  }
}

2545
TEST_F(FunctionBodyDecoderTest, BrTable0) {
2546
  ExpectFailure(sigs.v_v(), {kExprBrTable, 0, BR_TARGET(0)});
2547 2548
}

2549
TEST_F(FunctionBodyDecoderTest, BrTable0b) {
2550
  static byte code[] = {kExprI32Const, 11, kExprBrTable, 0, BR_TARGET(0)};
2551 2552
  ExpectValidates(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
2553 2554
}

2555
TEST_F(FunctionBodyDecoderTest, BrTable0c) {
2556
  static byte code[] = {kExprI32Const, 11, kExprBrTable, 0, BR_TARGET(1)};
2557 2558
  ExpectFailure(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
2559 2560
}

2561
TEST_F(FunctionBodyDecoderTest, BrTable1a) {
2562 2563
  ExpectValidates(sigs.v_v(),
                  {B1(WASM_BR_TABLE(WASM_I32V_2(67), 0, BR_TARGET(0)))});
2564
}
2565

2566
TEST_F(FunctionBodyDecoderTest, BrTable1b) {
2567
  static byte code[] = {B1(WASM_BR_TABLE(WASM_ZERO, 0, BR_TARGET(0)))};
2568 2569 2570 2571
  ExpectValidates(sigs.v_v(), code);
  ExpectFailure(sigs.i_i(), code);
  ExpectFailure(sigs.f_ff(), code);
  ExpectFailure(sigs.d_dd(), code);
2572 2573
}

2574
TEST_F(FunctionBodyDecoderTest, BrTable2a) {
2575 2576 2577
  ExpectValidates(
      sigs.v_v(),
      {B1(WASM_BR_TABLE(WASM_I32V_2(67), 1, BR_TARGET(0), BR_TARGET(0)))});
2578 2579
}

2580
TEST_F(FunctionBodyDecoderTest, BrTable2b) {
2581 2582 2583
  ExpectValidates(sigs.v_v(),
                  {WASM_BLOCK(WASM_BLOCK(WASM_BR_TABLE(
                      WASM_I32V_2(67), 1, BR_TARGET(0), BR_TARGET(1))))});
2584 2585
}

2586
TEST_F(FunctionBodyDecoderTest, BrTable_off_end) {
2587
  static byte code[] = {B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))};
2588
  for (size_t len = 1; len < sizeof(code); len++) {
2589 2590
    ExpectFailure(sigs.i_i(), VectorOf(code, len), kAppendEnd);
    ExpectFailure(sigs.i_i(), VectorOf(code, len), kOmitEnd);
2591 2592 2593
  }
}

2594
TEST_F(FunctionBodyDecoderTest, BrTable_invalid_br1) {
2595
  for (int depth = 0; depth < 4; depth++) {
2596
    byte code[] = {B1(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))};
2597
    Validate(depth <= 1, sigs.v_i(), code);
2598 2599 2600
  }
}

2601
TEST_F(FunctionBodyDecoderTest, BrTable_invalid_br2) {
2602
  for (int depth = 0; depth < 7; depth++) {
2603
    byte code[] = {
2604
        WASM_LOOP(WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))};
2605
    Validate(depth < 2, sigs.v_i(), code);
2606
  }
2607 2608
}

2609
TEST_F(FunctionBodyDecoderTest, BrTable_arity_mismatch1) {
2610 2611 2612 2613
  ExpectFailure(
      sigs.v_v(),
      {WASM_BLOCK(WASM_BLOCK_I(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2614 2615 2616
}

TEST_F(FunctionBodyDecoderTest, BrTable_arity_mismatch2) {
2617 2618 2619 2620
  ExpectFailure(
      sigs.v_v(),
      {WASM_BLOCK_I(WASM_BLOCK(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2621 2622 2623
}

TEST_F(FunctionBodyDecoderTest, BrTable_arity_mismatch_loop1) {
2624 2625 2626 2627
  ExpectFailure(
      sigs.v_v(),
      {WASM_LOOP(WASM_BLOCK_I(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2628 2629 2630
}

TEST_F(FunctionBodyDecoderTest, BrTable_arity_mismatch_loop2) {
2631 2632 2633 2634
  ExpectFailure(
      sigs.v_v(),
      {WASM_BLOCK_I(WASM_LOOP(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2635 2636 2637
}

TEST_F(FunctionBodyDecoderTest, BrTable_loop_block) {
2638 2639 2640 2641
  ExpectValidates(
      sigs.v_v(),
      {WASM_LOOP(WASM_BLOCK(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2642 2643 2644
}

TEST_F(FunctionBodyDecoderTest, BrTable_block_loop) {
2645 2646 2647 2648
  ExpectValidates(
      sigs.v_v(),
      {WASM_LOOP(WASM_BLOCK(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2649 2650 2651
}

TEST_F(FunctionBodyDecoderTest, BrTable_type_mismatch1) {
2652 2653 2654 2655
  ExpectFailure(
      sigs.v_v(),
      {WASM_BLOCK_I(WASM_BLOCK_F(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2656 2657 2658
}

TEST_F(FunctionBodyDecoderTest, BrTable_type_mismatch2) {
2659 2660 2661 2662
  ExpectFailure(
      sigs.v_v(),
      {WASM_BLOCK_F(WASM_BLOCK_I(
          WASM_ONE, WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2663 2664 2665
}

TEST_F(FunctionBodyDecoderTest, BrTable_type_mismatch_unreachable) {
2666 2667 2668 2669
  ExpectFailure(sigs.v_v(),
                {WASM_BLOCK_F(WASM_BLOCK_I(
                    WASM_UNREACHABLE,
                    WASM_BR_TABLE(WASM_ONE, 1, BR_TARGET(0), BR_TARGET(1))))});
2670 2671
}

2672
TEST_F(FunctionBodyDecoderTest, BrUnreachable1) {
2673 2674
  ExpectValidates(sigs.v_i(),
                  {WASM_GET_LOCAL(0), kExprBrTable, 0, BR_TARGET(0)});
2675 2676 2677
}

TEST_F(FunctionBodyDecoderTest, BrUnreachable2) {
2678 2679 2680 2681
  ExpectValidates(sigs.v_i(),
                  {WASM_GET_LOCAL(0), kExprBrTable, 0, BR_TARGET(0), WASM_NOP});
  ExpectFailure(sigs.v_i(),
                {WASM_GET_LOCAL(0), kExprBrTable, 0, BR_TARGET(0), WASM_ZERO});
2682 2683
}

2684
TEST_F(FunctionBodyDecoderTest, Brv1) {
2685 2686 2687
  ExpectValidates(sigs.i_i(), {WASM_BLOCK_I(WASM_BRV(0, WASM_ZERO))});
  ExpectValidates(sigs.i_i(),
                  {WASM_BLOCK_I(WASM_LOOP_I(WASM_BRV(2, WASM_ZERO)))});
2688 2689
}

2690
TEST_F(FunctionBodyDecoderTest, Brv1_type) {
2691 2692 2693 2694
  ExpectValidates(sigs.i_ii(), {WASM_BLOCK_I(WASM_BRV(0, WASM_GET_LOCAL(0)))});
  ExpectValidates(sigs.l_ll(), {WASM_BLOCK_L(WASM_BRV(0, WASM_GET_LOCAL(0)))});
  ExpectValidates(sigs.f_ff(), {WASM_BLOCK_F(WASM_BRV(0, WASM_GET_LOCAL(0)))});
  ExpectValidates(sigs.d_dd(), {WASM_BLOCK_D(WASM_BRV(0, WASM_GET_LOCAL(0)))});
2695
}
2696

2697
TEST_F(FunctionBodyDecoderTest, Brv1_type_n) {
2698 2699
  ExpectFailure(sigs.i_f(), {WASM_BLOCK_I(WASM_BRV(0, WASM_GET_LOCAL(0)))});
  ExpectFailure(sigs.i_d(), {WASM_BLOCK_I(WASM_BRV(0, WASM_GET_LOCAL(0)))});
2700
}
2701

2702
TEST_F(FunctionBodyDecoderTest, BrvIf1) {
2703
  ExpectValidates(sigs.i_v(), {WASM_BLOCK_I(WASM_BRV_IF_ZERO(0, WASM_ZERO))});
2704 2705
}

2706
TEST_F(FunctionBodyDecoderTest, BrvIf1_type) {
2707 2708 2709 2710 2711 2712 2713 2714
  ExpectValidates(sigs.i_i(),
                  {WASM_BLOCK_I(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)))});
  ExpectValidates(sigs.l_l(),
                  {WASM_BLOCK_L(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)))});
  ExpectValidates(sigs.f_ff(),
                  {WASM_BLOCK_F(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)))});
  ExpectValidates(sigs.d_dd(),
                  {WASM_BLOCK_D(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)))});
2715 2716
}

2717
TEST_F(FunctionBodyDecoderTest, BrvIf1_type_n) {
2718 2719 2720 2721
  ExpectFailure(sigs.i_f(),
                {WASM_BLOCK_I(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)))});
  ExpectFailure(sigs.i_d(),
                {WASM_BLOCK_I(WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0)))});
2722 2723
}

2724
TEST_F(FunctionBodyDecoderTest, Select) {
2725 2726 2727 2728 2729 2730 2731 2732
  ExpectValidates(sigs.i_i(), {WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
                                           WASM_ZERO)});
  ExpectValidates(sigs.f_ff(),
                  {WASM_SELECT(WASM_F32(0.0), WASM_F32(0.0), WASM_ZERO)});
  ExpectValidates(sigs.d_dd(),
                  {WASM_SELECT(WASM_F64(0.0), WASM_F64(0.0), WASM_ZERO)});
  ExpectValidates(sigs.l_l(),
                  {WASM_SELECT(WASM_I64V_1(0), WASM_I64V_1(0), WASM_ZERO)});
2733 2734
}

2735
TEST_F(FunctionBodyDecoderTest, Select_needs_value_type) {
2736
  WASM_FEATURE_SCOPE(reftypes);
2737
  ExpectFailure(sigs.e_e(),
2738
                {WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_ZERO)});
2739
  ExpectFailure(sigs.c_c(),
2740 2741 2742
                {WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_ZERO)});
}

2743
TEST_F(FunctionBodyDecoderTest, Select_fail1) {
2744 2745 2746 2747 2748 2749
  ExpectFailure(sigs.i_i(), {WASM_SELECT(WASM_F32(0.0), WASM_GET_LOCAL(0),
                                         WASM_GET_LOCAL(0))});
  ExpectFailure(sigs.i_i(), {WASM_SELECT(WASM_GET_LOCAL(0), WASM_F32(0.0),
                                         WASM_GET_LOCAL(0))});
  ExpectFailure(sigs.i_i(), {WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
                                         WASM_F32(0.0))});
2750 2751
}

2752
TEST_F(FunctionBodyDecoderTest, Select_fail2) {
2753 2754 2755
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType type = kValueTypes[i];
    if (type == kWasmI32) continue;
2756
    // Select without specified type is only allowed for number types.
2757
    if (type == kWasmExternRef) continue;
2758

2759
    ValueType types[] = {type, kWasmI32, type};
2760 2761
    FunctionSig sig(1, 2, types);

2762 2763
    ExpectValidates(&sig, {WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(1),
                                       WASM_GET_LOCAL(0))});
2764

2765 2766
    ExpectFailure(&sig, {WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0),
                                     WASM_GET_LOCAL(0))});
2767

2768 2769
    ExpectFailure(&sig, {WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
                                     WASM_GET_LOCAL(0))});
2770

2771 2772
    ExpectFailure(&sig, {WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
                                     WASM_GET_LOCAL(1))});
2773
  }
2774 2775
}

2776
TEST_F(FunctionBodyDecoderTest, Select_TypeCheck) {
2777 2778
  ExpectFailure(sigs.i_i(), {WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0),
                                         WASM_GET_LOCAL(0))});
2779

2780 2781
  ExpectFailure(sigs.i_i(), {WASM_SELECT(WASM_GET_LOCAL(0), WASM_F64(0.25),
                                         WASM_GET_LOCAL(0))});
2782

2783 2784
  ExpectFailure(sigs.i_i(), {WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0),
                                         WASM_I64V_1(0))});
2785 2786
}

2787
TEST_F(FunctionBodyDecoderTest, SelectWithType) {
2788
  WASM_FEATURE_SCOPE(reftypes);
2789 2790 2791 2792 2793 2794 2795 2796
  ExpectValidates(sigs.i_i(), {WASM_SELECT_I(WASM_GET_LOCAL(0),
                                             WASM_GET_LOCAL(0), WASM_ZERO)});
  ExpectValidates(sigs.f_ff(),
                  {WASM_SELECT_F(WASM_F32(0.0), WASM_F32(0.0), WASM_ZERO)});
  ExpectValidates(sigs.d_dd(),
                  {WASM_SELECT_D(WASM_F64(0.0), WASM_F64(0.0), WASM_ZERO)});
  ExpectValidates(sigs.l_l(),
                  {WASM_SELECT_L(WASM_I64V_1(0), WASM_I64V_1(0), WASM_ZERO)});
2797
  ExpectValidates(sigs.e_e(),
2798 2799
                  {WASM_SELECT_R(WASM_REF_NULL(kExternRefCode),
                                 WASM_REF_NULL(kExternRefCode), WASM_ZERO)});
2800
  ExpectValidates(sigs.c_c(),
2801 2802
                  {WASM_SELECT_A(WASM_REF_NULL(kFuncRefCode),
                                 WASM_REF_NULL(kFuncRefCode), WASM_ZERO)});
2803 2804 2805
}

TEST_F(FunctionBodyDecoderTest, SelectWithType_fail) {
2806
  WASM_FEATURE_SCOPE(reftypes);
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816
  ExpectFailure(sigs.i_i(), {WASM_SELECT_F(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
                                           WASM_ZERO)});
  ExpectFailure(sigs.f_ff(),
                {WASM_SELECT_D(WASM_F32(0.0), WASM_F32(0.0), WASM_ZERO)});
  ExpectFailure(sigs.d_dd(),
                {WASM_SELECT_L(WASM_F64(0.0), WASM_F64(0.0), WASM_ZERO)});
  ExpectFailure(sigs.l_l(),
                {WASM_SELECT_I(WASM_I64V_1(0), WASM_I64V_1(0), WASM_ZERO)});
}

2817
TEST_F(FunctionBodyDecoderTest, Throw) {
2818
  WASM_FEATURE_SCOPE(eh);
2819 2820 2821
  byte ex1 = builder.AddException(sigs.v_v());
  byte ex2 = builder.AddException(sigs.v_i());
  byte ex3 = builder.AddException(sigs.v_ii());
2822 2823 2824 2825 2826 2827
  ExpectValidates(sigs.v_v(), {kExprThrow, ex1});
  ExpectValidates(sigs.v_v(), {WASM_I32V(0), kExprThrow, ex2});
  ExpectFailure(sigs.v_v(), {WASM_F32(0.0), kExprThrow, ex2});
  ExpectValidates(sigs.v_v(), {WASM_I32V(0), WASM_I32V(0), kExprThrow, ex3});
  ExpectFailure(sigs.v_v(), {WASM_F32(0.0), WASM_I32V(0), kExprThrow, ex3});
  ExpectFailure(sigs.v_v(), {kExprThrow, 99});
2828 2829
}

2830
TEST_F(FunctionBodyDecoderTest, ThrowUnreachable) {
2831
  WASM_FEATURE_SCOPE(eh);
2832 2833
  byte ex1 = builder.AddException(sigs.v_v());
  byte ex2 = builder.AddException(sigs.v_i());
2834 2835 2836 2837 2838 2839 2840 2841
  ExpectValidates(sigs.i_i(), {WASM_GET_LOCAL(0), kExprThrow, ex1, WASM_NOP});
  ExpectValidates(sigs.v_i(), {WASM_GET_LOCAL(0), kExprThrow, ex2, WASM_NOP});
  ExpectValidates(sigs.i_i(), {WASM_GET_LOCAL(0), kExprThrow, ex1, WASM_ZERO});
  ExpectFailure(sigs.v_i(), {WASM_GET_LOCAL(0), kExprThrow, ex2, WASM_ZERO});
  ExpectFailure(sigs.i_i(),
                {WASM_GET_LOCAL(0), kExprThrow, ex1, WASM_F32(0.0)});
  ExpectFailure(sigs.v_i(),
                {WASM_GET_LOCAL(0), kExprThrow, ex2, WASM_F32(0.0)});
2842 2843
}

2844
#define WASM_TRY_OP kExprTry, kVoidCode
2845 2846
#define WASM_BR_ON_EXN(depth, index) \
  kExprBrOnExn, static_cast<byte>(depth), static_cast<byte>(index)
2847

2848
TEST_F(FunctionBodyDecoderTest, TryCatch) {
2849
  WASM_FEATURE_SCOPE(eh);
2850 2851 2852 2853 2854
  ExpectValidates(sigs.v_v(), {WASM_TRY_OP, kExprCatch, kExprDrop, kExprEnd});
  ExpectFailure(sigs.v_v(), {WASM_TRY_OP, kExprCatch, kExprCatch, kExprEnd});
  ExpectFailure(sigs.v_v(), {WASM_TRY_OP, kExprEnd});    // Missing catch.
  ExpectFailure(sigs.v_v(), {WASM_TRY_OP, kExprCatch});  // Missing end.
  ExpectFailure(sigs.v_v(), {kExprCatch, kExprEnd});     // Missing try.
2855
}
2856

2857
TEST_F(FunctionBodyDecoderTest, Rethrow) {
2858
  WASM_FEATURE_SCOPE(eh);
2859 2860 2861 2862 2863
  ExpectValidates(sigs.v_v(),
                  {WASM_TRY_OP, kExprCatch, kExprRethrow, kExprEnd});
  ExpectFailure(sigs.v_v(), {WASM_TRY_OP, kExprRethrow, kExprCatch, kExprEnd});
  ExpectFailure(sigs.v_v(), {WASM_BLOCK(kExprRethrow)});
  ExpectFailure(sigs.v_v(), {kExprRethrow});
2864 2865
}

2866
TEST_F(FunctionBodyDecoderTest, BrOnExn) {
2867 2868
  WASM_FEATURE_SCOPE(eh);
  byte ex1 = builder.AddException(sigs.v_v());
2869
  byte ex2 = builder.AddException(sigs.v_i());
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
  ExpectValidates(sigs.v_v(), {WASM_TRY_OP, kExprCatch, WASM_BR_ON_EXN(0, ex1),
                               kExprDrop, kExprEnd});
  ExpectValidates(sigs.v_v(), {WASM_TRY_OP, kExprCatch, WASM_BR_ON_EXN(1, ex1),
                               kExprDrop, kExprEnd});
  ExpectValidates(sigs.v_v(), {WASM_TRY_OP, kExprCatch, WASM_BR_ON_EXN(0, ex1),
                               WASM_BR_ON_EXN(0, ex1), kExprDrop, kExprEnd});
  ExpectValidates(sigs.v_v(),
                  {WASM_BLOCK(WASM_TRY_OP, kExprCatch, WASM_BR_ON_EXN(1, ex1),
                              kExprDrop, kExprEnd)});
  ExpectValidates(sigs.i_v(),
                  {WASM_BLOCK_I(WASM_TRY_OP, kExprCatch, WASM_BR_ON_EXN(1, ex2),
                                kExprDrop, kExprEnd, kExprI32Const, 0)});
  ExpectFailure(sigs.v_v(), {WASM_TRY_OP, kExprCatch, WASM_BR_ON_EXN(2, ex1),
                             kExprDrop, kExprEnd});
  ExpectFailure(sigs.v_v(), {WASM_TRY_OP, kExprCatch, kExprDrop,
                             WASM_BR_ON_EXN(0, ex1), kExprEnd});
  ExpectFailure(sigs.v_v(),
                {WASM_TRY_OP, kExprCatch, WASM_BR_ON_EXN(0, ex1), kExprEnd});
2888 2889
}

2890
#undef WASM_BR_ON_EXN
2891 2892
#undef WASM_TRY_OP

2893
TEST_F(FunctionBodyDecoderTest, MultiValBlock1) {
2894
  WASM_FEATURE_SCOPE(mv);
2895
  byte sig0 = builder.AddSignature(sigs.ii_v());
2896 2897
  ExpectValidates(
      sigs.i_ii(),
2898 2899
      {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), kExprI32Add});
  ExpectFailure(sigs.i_ii(), {WASM_BLOCK_X(sig0, WASM_NOP), kExprI32Add});
2900
  ExpectFailure(sigs.i_ii(),
2901
                {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0)), kExprI32Add});
2902
  ExpectFailure(sigs.i_ii(),
2903
                {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
2904 2905 2906 2907
                              WASM_GET_LOCAL(0)),
                 kExprI32Add});
  ExpectFailure(
      sigs.i_ii(),
2908
      {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), kExprF32Add});
2909 2910
}

2911
TEST_F(FunctionBodyDecoderTest, MultiValBlock2) {
2912
  WASM_FEATURE_SCOPE(mv);
2913
  byte sig0 = builder.AddSignature(sigs.ii_v());
2914
  ExpectValidates(sigs.i_ii(),
2915
                  {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
2916
                   WASM_I32_ADD(WASM_NOP, WASM_NOP)});
2917 2918 2919
  ExpectFailure(sigs.i_ii(), {WASM_BLOCK_X(sig0, WASM_NOP),
                              WASM_I32_ADD(WASM_NOP, WASM_NOP)});
  ExpectFailure(sigs.i_ii(), {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0)),
2920 2921
                              WASM_I32_ADD(WASM_NOP, WASM_NOP)});
  ExpectFailure(sigs.i_ii(),
2922
                {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
2923 2924 2925
                              WASM_GET_LOCAL(0)),
                 WASM_I32_ADD(WASM_NOP, WASM_NOP)});
  ExpectFailure(sigs.i_ii(),
2926
                {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
2927
                 WASM_F32_ADD(WASM_NOP, WASM_NOP)});
2928 2929 2930
}

TEST_F(FunctionBodyDecoderTest, MultiValBlockBr) {
2931
  WASM_FEATURE_SCOPE(mv);
2932 2933 2934 2935
  byte sig0 = builder.AddSignature(sigs.ii_v());
  ExpectFailure(sigs.i_ii(), {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0), WASM_BR(0)),
                              kExprI32Add});
  ExpectValidates(sigs.i_ii(), {WASM_BLOCK_X(sig0, WASM_GET_LOCAL(0),
2936 2937
                                             WASM_GET_LOCAL(1), WASM_BR(0)),
                                kExprI32Add});
2938 2939
}

2940
TEST_F(FunctionBodyDecoderTest, MultiValLoop1) {
2941
  WASM_FEATURE_SCOPE(mv);
2942
  byte sig0 = builder.AddSignature(sigs.ii_v());
2943 2944
  ExpectValidates(
      sigs.i_ii(),
2945 2946 2947 2948 2949
      {WASM_LOOP_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), kExprI32Add});
  ExpectFailure(sigs.i_ii(), {WASM_LOOP_X(sig0, WASM_NOP), kExprI32Add});
  ExpectFailure(sigs.i_ii(),
                {WASM_LOOP_X(sig0, WASM_GET_LOCAL(0)), kExprI32Add});
  ExpectFailure(sigs.i_ii(), {WASM_LOOP_X(sig0, WASM_GET_LOCAL(0),
2950 2951 2952 2953
                                          WASM_GET_LOCAL(1), WASM_GET_LOCAL(0)),
                              kExprI32Add});
  ExpectFailure(
      sigs.i_ii(),
2954
      {WASM_LOOP_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), kExprF32Add});
2955 2956 2957
}

TEST_F(FunctionBodyDecoderTest, MultiValIf) {
2958
  WASM_FEATURE_SCOPE(mv);
2959
  byte sig0 = builder.AddSignature(sigs.ii_v());
2960 2961
  ExpectValidates(
      sigs.i_ii(),
2962
      {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0),
2963 2964 2965 2966
                      WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
                      WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))),
       kExprI32Add});
  ExpectFailure(sigs.i_ii(),
2967 2968 2969 2970
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP),
                 kExprI32Add});
  ExpectFailure(sigs.i_ii(),
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0), WASM_NOP,
2971 2972 2973 2974
                                WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))),
                 kExprI32Add});
  ExpectFailure(
      sigs.i_ii(),
2975
      {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0),
2976 2977 2978
                      WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), WASM_NOP),
       kExprI32Add});
  ExpectFailure(sigs.i_ii(),
2979
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
2980 2981 2982
                                WASM_GET_LOCAL(1)),
                 kExprI32Add});
  ExpectFailure(sigs.i_ii(),
2983
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
2984 2985 2986
                                WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))),
                 kExprI32Add});
  ExpectFailure(sigs.i_ii(),
2987
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0),
2988 2989 2990 2991 2992 2993
                                WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
                                WASM_GET_LOCAL(1)),
                 kExprI32Add});
  ExpectFailure(
      sigs.i_ii(),
      {WASM_IF_ELSE_X(
2994
           sig0, WASM_GET_LOCAL(0),
2995 2996 2997 2998
           WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)),
           WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))),
       kExprI32Add});
  ExpectFailure(sigs.i_ii(),
2999
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0),
3000 3001 3002 3003 3004
                                WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0),
                                         WASM_GET_LOCAL(0)),
                                WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))),
                 kExprI32Add});
  ExpectFailure(sigs.i_ii(),
3005
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0),
3006 3007 3008 3009 3010
                                WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
                                WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(1),
                                         WASM_GET_LOCAL(1))),
                 kExprI32Add});
  ExpectFailure(sigs.i_ii(),
3011
                {WASM_IF_ELSE_X(sig0, WASM_GET_LOCAL(0),
3012 3013 3014
                                WASM_SEQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)),
                                WASM_SEQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))),
                 kExprF32Add});
3015 3016
}

3017
TEST_F(FunctionBodyDecoderTest, BlockParam) {
3018
  WASM_FEATURE_SCOPE(mv);
3019 3020
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
3021 3022
  ExpectValidates(
      sigs.i_ii(),
3023 3024
      {WASM_GET_LOCAL(0), WASM_BLOCK_X(sig1, WASM_GET_LOCAL(1),
                                       WASM_I32_ADD(WASM_NOP, WASM_NOP))});
3025 3026
  ExpectValidates(sigs.i_ii(),
                  {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3027
                   WASM_BLOCK_X(sig2, WASM_I32_ADD(WASM_NOP, WASM_NOP))});
3028
  ExpectValidates(sigs.i_ii(), {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3029
                                WASM_BLOCK_X(sig1, WASM_NOP),
3030
                                WASM_I32_ADD(WASM_NOP, WASM_NOP)});
3031 3032 3033
  ExpectFailure(sigs.i_ii(), {WASM_BLOCK_X(sig1, WASM_NOP),
                              WASM_RETURN1(WASM_GET_LOCAL(0))});
  ExpectFailure(sigs.i_ii(), {WASM_BLOCK_X(sig1, WASM_GET_LOCAL(0)),
3034 3035 3036
                              WASM_RETURN1(WASM_GET_LOCAL(0))});
  ExpectFailure(
      sigs.i_ii(),
3037
      {WASM_GET_LOCAL(0), WASM_BLOCK_X(sig2, WASM_I32_ADD(WASM_NOP, WASM_NOP)),
3038 3039
       WASM_RETURN1(WASM_GET_LOCAL(0))});
  ExpectFailure(sigs.i_ii(),
3040
                {WASM_GET_LOCAL(0), WASM_BLOCK_X(sig1, WASM_F32_NEG(WASM_NOP)),
3041
                 WASM_RETURN1(WASM_GET_LOCAL(0))});
3042 3043 3044
}

TEST_F(FunctionBodyDecoderTest, LoopParam) {
3045
  WASM_FEATURE_SCOPE(mv);
3046 3047
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
3048
  ExpectValidates(sigs.i_ii(), {WASM_GET_LOCAL(0),
3049
                                WASM_LOOP_X(sig1, WASM_GET_LOCAL(1),
3050 3051 3052
                                            WASM_I32_ADD(WASM_NOP, WASM_NOP))});
  ExpectValidates(sigs.i_ii(),
                  {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3053
                   WASM_LOOP_X(sig2, WASM_I32_ADD(WASM_NOP, WASM_NOP))});
3054
  ExpectValidates(sigs.i_ii(), {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3055
                                WASM_LOOP_X(sig1, WASM_NOP),
3056 3057
                                WASM_I32_ADD(WASM_NOP, WASM_NOP)});
  ExpectFailure(sigs.i_ii(),
3058 3059
                {WASM_LOOP_X(sig1, WASM_NOP), WASM_RETURN1(WASM_GET_LOCAL(0))});
  ExpectFailure(sigs.i_ii(), {WASM_LOOP_X(sig1, WASM_GET_LOCAL(0)),
3060
                              WASM_RETURN1(WASM_GET_LOCAL(0))});
3061 3062 3063 3064
  ExpectFailure(
      sigs.i_ii(),
      {WASM_GET_LOCAL(0), WASM_LOOP_X(sig2, WASM_I32_ADD(WASM_NOP, WASM_NOP)),
       WASM_RETURN1(WASM_GET_LOCAL(0))});
3065
  ExpectFailure(sigs.i_ii(),
3066
                {WASM_GET_LOCAL(0), WASM_LOOP_X(sig1, WASM_F32_NEG(WASM_NOP)),
3067
                 WASM_RETURN1(WASM_GET_LOCAL(0))});
3068 3069 3070
}

TEST_F(FunctionBodyDecoderTest, LoopParamBr) {
3071
  WASM_FEATURE_SCOPE(mv);
3072 3073
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
3074
  ExpectValidates(sigs.i_ii(),
3075
                  {WASM_GET_LOCAL(0), WASM_LOOP_X(sig1, WASM_BR(0))});
3076 3077
  ExpectValidates(
      sigs.i_ii(),
3078
      {WASM_GET_LOCAL(0), WASM_LOOP_X(sig1, WASM_BRV(0, WASM_GET_LOCAL(1)))});
3079
  ExpectValidates(sigs.i_ii(), {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3080 3081 3082 3083
                                WASM_LOOP_X(sig2, WASM_BR(0))});
  ExpectValidates(
      sigs.i_ii(),
      {WASM_GET_LOCAL(0), WASM_LOOP_X(sig1, WASM_BLOCK_X(sig1, WASM_BR(1)))});
3084
  ExpectFailure(sigs.i_ii(),
3085
                {WASM_GET_LOCAL(0), WASM_LOOP_X(sig1, WASM_BLOCK(WASM_BR(1))),
3086 3087
                 WASM_RETURN1(WASM_GET_LOCAL(0))});
  ExpectFailure(sigs.i_ii(), {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3088
                              WASM_LOOP_X(sig2, WASM_BLOCK_X(sig1, WASM_BR(1))),
3089
                              WASM_RETURN1(WASM_GET_LOCAL(0))});
3090 3091 3092
}

TEST_F(FunctionBodyDecoderTest, IfParam) {
3093
  WASM_FEATURE_SCOPE(mv);
3094 3095
  byte sig1 = builder.AddSignature(sigs.i_i());
  byte sig2 = builder.AddSignature(sigs.i_ii());
3096 3097
  ExpectValidates(sigs.i_ii(),
                  {WASM_GET_LOCAL(0),
3098
                   WASM_IF_X(sig1, WASM_GET_LOCAL(0),
3099 3100 3101
                             WASM_I32_ADD(WASM_NOP, WASM_GET_LOCAL(1)))});
  ExpectValidates(sigs.i_ii(),
                  {WASM_GET_LOCAL(0),
3102
                   WASM_IF_ELSE_X(sig1, WASM_GET_LOCAL(0),
3103 3104 3105 3106 3107
                                  WASM_I32_ADD(WASM_NOP, WASM_GET_LOCAL(1)),
                                  WASM_I32_EQZ(WASM_NOP))});
  ExpectValidates(
      sigs.i_ii(),
      {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3108
       WASM_IF_ELSE_X(sig2, WASM_GET_LOCAL(0), WASM_I32_ADD(WASM_NOP, WASM_NOP),
3109 3110
                      WASM_I32_MUL(WASM_NOP, WASM_NOP))});
  ExpectValidates(sigs.i_ii(), {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
3111
                                WASM_IF_X(sig1, WASM_GET_LOCAL(0), WASM_NOP),
3112
                                WASM_I32_ADD(WASM_NOP, WASM_NOP)});
3113 3114 3115 3116 3117
  ExpectValidates(sigs.i_ii(),
                  {WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
                   WASM_IF_ELSE_X(sig1, WASM_GET_LOCAL(0), WASM_NOP,
                                  WASM_I32_EQZ(WASM_NOP)),
                   WASM_I32_ADD(WASM_NOP, WASM_NOP)});
3118 3119
}

3120 3121
TEST_F(FunctionBodyDecoderTest, Regression709741) {
  AddLocals(kWasmI32, kV8MaxWasmFunctionLocals - 1);
3122
  ExpectValidates(sigs.v_v(), {WASM_NOP});
3123
  byte code[] = {WASM_NOP, WASM_END};
3124

3125 3126
  for (size_t i = 0; i < arraysize(code); ++i) {
    FunctionBody body(sigs.v_v(), 0, code, code + i);
3127
    WasmFeatures unused_detected_features;
3128
    DecodeResult result =
3129
        VerifyWasmCode(zone()->allocator(), WasmFeatures::All(), nullptr,
3130
                       &unused_detected_features, body);
3131 3132 3133 3134 3135 3136 3137
    if (result.ok()) {
      std::ostringstream str;
      str << "Expected verification to fail";
    }
  }
}

3138 3139
TEST_F(FunctionBodyDecoderTest, MemoryInit) {
  builder.InitializeMemory();
3140
  builder.SetDataSegmentCount(1);
3141

3142 3143
  ExpectFailure(sigs.v_v(),
                {WASM_MEMORY_INIT(0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3144
  WASM_FEATURE_SCOPE(bulk_memory);
3145 3146 3147
  ExpectValidates(sigs.v_v(),
                  {WASM_MEMORY_INIT(0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
  ExpectFailure(sigs.v_v(),
3148
                {WASM_TABLE_INIT(0, 1, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3149 3150
}

3151 3152
TEST_F(FunctionBodyDecoderTest, MemoryInitInvalid) {
  builder.InitializeMemory();
3153
  builder.SetDataSegmentCount(1);
3154 3155 3156 3157 3158

  WASM_FEATURE_SCOPE(bulk_memory);
  byte code[] = {WASM_MEMORY_INIT(0, WASM_ZERO, WASM_ZERO, WASM_ZERO),
                 WASM_END};
  for (size_t i = 0; i <= arraysize(code); ++i) {
3159
    Validate(i == arraysize(code), sigs.v_v(), VectorOf(code, i), kOmitEnd);
3160 3161 3162
  }
}

3163
TEST_F(FunctionBodyDecoderTest, DataDrop) {
3164 3165 3166
  builder.InitializeMemory();
  builder.SetDataSegmentCount(1);

3167
  ExpectFailure(sigs.v_v(), {WASM_DATA_DROP(0)});
3168
  WASM_FEATURE_SCOPE(bulk_memory);
3169 3170
  ExpectValidates(sigs.v_v(), {WASM_DATA_DROP(0)});
  ExpectFailure(sigs.v_v(), {WASM_DATA_DROP(1)});
3171 3172
}

3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
TEST_F(FunctionBodyDecoderTest, DataSegmentIndexUnsigned) {
  builder.InitializeMemory();
  builder.SetDataSegmentCount(65);

  WASM_FEATURE_SCOPE(bulk_memory);
  // Make sure that the index is interpreted as an unsigned number; 64 is
  // interpreted as -64 when decoded as a signed LEB.
  ExpectValidates(sigs.v_v(),
                  {WASM_MEMORY_INIT(64, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
  ExpectValidates(sigs.v_v(), {WASM_DATA_DROP(64)});
}

3185 3186 3187
TEST_F(FunctionBodyDecoderTest, MemoryCopy) {
  builder.InitializeMemory();

3188 3189
  ExpectFailure(sigs.v_v(),
                {WASM_MEMORY_COPY(WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3190
  WASM_FEATURE_SCOPE(bulk_memory);
3191 3192
  ExpectValidates(sigs.v_v(),
                  {WASM_MEMORY_COPY(WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3193 3194 3195 3196 3197
}

TEST_F(FunctionBodyDecoderTest, MemoryFill) {
  builder.InitializeMemory();

3198 3199
  ExpectFailure(sigs.v_v(),
                {WASM_MEMORY_FILL(WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3200
  WASM_FEATURE_SCOPE(bulk_memory);
3201 3202
  ExpectValidates(sigs.v_v(),
                  {WASM_MEMORY_FILL(WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3203 3204 3205 3206
}

TEST_F(FunctionBodyDecoderTest, BulkMemoryOpsWithoutMemory) {
  WASM_FEATURE_SCOPE(bulk_memory);
3207 3208 3209 3210 3211 3212
  ExpectFailure(sigs.v_v(),
                {WASM_MEMORY_INIT(0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
  ExpectFailure(sigs.v_v(),
                {WASM_MEMORY_COPY(WASM_ZERO, WASM_ZERO, WASM_ZERO)});
  ExpectFailure(sigs.v_v(),
                {WASM_MEMORY_FILL(WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3213 3214 3215
}

TEST_F(FunctionBodyDecoderTest, TableInit) {
3216 3217
  builder.InitializeTable(wasm::kWasmFuncRef);
  builder.AddPassiveElementSegment(wasm::kWasmFuncRef);
3218

3219
  ExpectFailure(sigs.v_v(),
3220
                {WASM_TABLE_INIT(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3221
  WASM_FEATURE_SCOPE(bulk_memory);
3222
  ExpectValidates(sigs.v_v(),
3223
                  {WASM_TABLE_INIT(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3224
  ExpectFailure(sigs.v_v(),
3225
                {WASM_TABLE_INIT(0, 1, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3226 3227
}

3228 3229
TEST_F(FunctionBodyDecoderTest, TableInitWrongType) {
  uint32_t table_index = builder.InitializeTable(wasm::kWasmFuncRef);
3230 3231
  uint32_t element_index =
      builder.AddPassiveElementSegment(wasm::kWasmExternRef);
3232 3233

  WASM_FEATURE_SCOPE(bulk_memory);
3234
  WASM_FEATURE_SCOPE(reftypes);
3235 3236 3237 3238
  ExpectFailure(sigs.v_v(), {WASM_TABLE_INIT(table_index, element_index,
                                             WASM_ZERO, WASM_ZERO, WASM_ZERO)});
}

3239
TEST_F(FunctionBodyDecoderTest, TableInitInvalid) {
3240 3241
  builder.InitializeTable(wasm::kWasmFuncRef);
  builder.AddPassiveElementSegment(wasm::kWasmFuncRef);
3242 3243

  WASM_FEATURE_SCOPE(bulk_memory);
3244 3245
  byte code[] = {WASM_TABLE_INIT(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO),
                 WASM_END};
3246
  for (size_t i = 0; i <= arraysize(code); ++i) {
3247
    Validate(i == arraysize(code), sigs.v_v(), VectorOf(code, i), kOmitEnd);
3248 3249 3250
  }
}

3251
TEST_F(FunctionBodyDecoderTest, ElemDrop) {
3252 3253
  builder.InitializeTable(wasm::kWasmFuncRef);
  builder.AddPassiveElementSegment(wasm::kWasmFuncRef);
3254

3255
  ExpectFailure(sigs.v_v(), {WASM_ELEM_DROP(0)});
3256
  WASM_FEATURE_SCOPE(bulk_memory);
3257 3258
  ExpectValidates(sigs.v_v(), {WASM_ELEM_DROP(0)});
  ExpectFailure(sigs.v_v(), {WASM_ELEM_DROP(1)});
3259 3260
}

3261
TEST_F(FunctionBodyDecoderTest, TableInitDeclarativeElem) {
3262
  builder.InitializeTable(wasm::kWasmFuncRef);
3263 3264 3265
  builder.AddDeclarativeElementSegment();

  WASM_FEATURE_SCOPE(bulk_memory);
3266
  WASM_FEATURE_SCOPE(reftypes);
3267 3268 3269 3270 3271 3272 3273 3274
  byte code[] = {WASM_TABLE_INIT(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO),
                 WASM_END};
  for (size_t i = 0; i <= arraysize(code); ++i) {
    Validate(i == arraysize(code), sigs.v_v(), VectorOf(code, i), kOmitEnd);
  }
}

TEST_F(FunctionBodyDecoderTest, DeclarativeElemDrop) {
3275
  builder.InitializeTable(wasm::kWasmFuncRef);
3276 3277 3278 3279
  builder.AddDeclarativeElementSegment();

  ExpectFailure(sigs.v_v(), {WASM_ELEM_DROP(0)});
  WASM_FEATURE_SCOPE(bulk_memory);
3280
  WASM_FEATURE_SCOPE(reftypes);
3281 3282 3283 3284 3285
  ExpectValidates(sigs.v_v(), {WASM_ELEM_DROP(0)});
  ExpectFailure(sigs.v_v(), {WASM_ELEM_DROP(1)});
}

TEST_F(FunctionBodyDecoderTest, RefFuncDeclared) {
3286
  builder.InitializeTable(wasm::kWasmStmt);
3287 3288 3289 3290
  byte function_index = builder.AddFunction(sigs.v_i());

  ExpectFailure(sigs.a_v(), {WASM_REF_FUNC(function_index)});
  WASM_FEATURE_SCOPE(bulk_memory);
3291
  WASM_FEATURE_SCOPE(reftypes);
3292 3293 3294 3295
  ExpectValidates(sigs.a_v(), {WASM_REF_FUNC(function_index)});
}

TEST_F(FunctionBodyDecoderTest, RefFuncUndeclared) {
3296
  builder.InitializeTable(wasm::kWasmStmt);
3297 3298 3299
  byte function_index = builder.AddFunction(sigs.v_i(), false);

  WASM_FEATURE_SCOPE(bulk_memory);
3300
  WASM_FEATURE_SCOPE(reftypes);
3301 3302 3303
  ExpectFailure(sigs.a_v(), {WASM_REF_FUNC(function_index)});
}

3304
TEST_F(FunctionBodyDecoderTest, ElemSegmentIndexUnsigned) {
3305
  builder.InitializeTable(wasm::kWasmFuncRef);
3306
  for (int i = 0; i < 65; ++i) {
3307
    builder.AddPassiveElementSegment(wasm::kWasmFuncRef);
3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
  }

  WASM_FEATURE_SCOPE(bulk_memory);
  // Make sure that the index is interpreted as an unsigned number; 64 is
  // interpreted as -64 when decoded as a signed LEB.
  ExpectValidates(sigs.v_v(),
                  {WASM_TABLE_INIT(0, 64, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
  ExpectValidates(sigs.v_v(), {WASM_ELEM_DROP(64)});
}

3318
TEST_F(FunctionBodyDecoderTest, TableCopy) {
3319
  builder.InitializeTable(wasm::kWasmStmt);
3320

3321 3322
  ExpectFailure(sigs.v_v(),
                {WASM_TABLE_COPY(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3323
  WASM_FEATURE_SCOPE(bulk_memory);
3324
  ExpectValidates(sigs.v_v(),
3325
                  {WASM_TABLE_COPY(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3326 3327
}

3328 3329
TEST_F(FunctionBodyDecoderTest, TableCopyWrongType) {
  uint32_t dst_table_index = builder.InitializeTable(wasm::kWasmFuncRef);
3330
  uint32_t src_table_index = builder.InitializeTable(wasm::kWasmExternRef);
3331 3332

  WASM_FEATURE_SCOPE(bulk_memory);
3333
  WASM_FEATURE_SCOPE(reftypes);
3334 3335 3336 3337
  ExpectFailure(sigs.v_v(), {WASM_TABLE_COPY(dst_table_index, src_table_index,
                                             WASM_ZERO, WASM_ZERO, WASM_ZERO)});
}

3338
TEST_F(FunctionBodyDecoderTest, TableGrow) {
3339
  byte tab_func = builder.AddTable(kWasmFuncRef, 10, true, 20);
3340
  byte tab_ref = builder.AddTable(kWasmExternRef, 10, true, 20);
3341

3342
  ExpectFailure(
3343
      sigs.i_c(),
3344
      {WASM_TABLE_GROW(tab_func, WASM_REF_NULL(kFuncRefCode), WASM_ONE)});
3345
  WASM_FEATURE_SCOPE(reftypes);
3346
  ExpectValidates(
3347
      sigs.i_c(),
3348
      {WASM_TABLE_GROW(tab_func, WASM_REF_NULL(kFuncRefCode), WASM_ONE)});
3349
  ExpectValidates(
3350
      sigs.i_e(),
3351
      {WASM_TABLE_GROW(tab_ref, WASM_REF_NULL(kExternRefCode), WASM_ONE)});
3352
  // FuncRef table cannot be initialized with an ExternRef value.
3353
  ExpectFailure(sigs.i_e(),
3354
                {WASM_TABLE_GROW(tab_func, WASM_GET_LOCAL(0), WASM_ONE)});
3355
  // ExternRef table cannot be initialized with a FuncRef value.
3356
  ExpectFailure(sigs.i_c(),
3357
                {WASM_TABLE_GROW(tab_ref, WASM_GET_LOCAL(0), WASM_ONE)});
3358
  // Check that the table index gets verified.
3359
  ExpectFailure(
3360
      sigs.i_e(),
3361
      {WASM_TABLE_GROW(tab_ref + 2, WASM_REF_NULL(kExternRefCode), WASM_ONE)});
3362 3363
}

3364
TEST_F(FunctionBodyDecoderTest, TableSize) {
3365
  int tab = builder.AddTable(kWasmFuncRef, 10, true, 20);
3366 3367

  ExpectFailure(sigs.i_v(), {WASM_TABLE_SIZE(tab)});
3368
  WASM_FEATURE_SCOPE(reftypes);
3369 3370 3371 3372
  ExpectValidates(sigs.i_v(), {WASM_TABLE_SIZE(tab)});
  ExpectFailure(sigs.i_v(), {WASM_TABLE_SIZE(tab + 2)});
}

3373
TEST_F(FunctionBodyDecoderTest, TableFill) {
3374
  byte tab_func = builder.AddTable(kWasmFuncRef, 10, true, 20);
3375
  byte tab_ref = builder.AddTable(kWasmExternRef, 10, true, 20);
3376

3377
  ExpectFailure(sigs.v_c(),
3378
                {WASM_TABLE_FILL(tab_func, WASM_ONE,
3379
                                 WASM_REF_NULL(kFuncRefCode), WASM_ONE)});
3380
  WASM_FEATURE_SCOPE(reftypes);
3381
  ExpectValidates(sigs.v_c(),
3382
                  {WASM_TABLE_FILL(tab_func, WASM_ONE,
3383
                                   WASM_REF_NULL(kFuncRefCode), WASM_ONE)});
3384
  ExpectValidates(sigs.v_e(),
3385
                  {WASM_TABLE_FILL(tab_ref, WASM_ONE,
3386
                                   WASM_REF_NULL(kExternRefCode), WASM_ONE)});
3387
  // FuncRef table cannot be initialized with an ExternRef value.
3388
  ExpectFailure(sigs.v_e(), {WASM_TABLE_FILL(tab_func, WASM_ONE,
3389
                                             WASM_GET_LOCAL(0), WASM_ONE)});
3390
  // ExternRef table cannot be initialized with a FuncRef value.
3391
  ExpectFailure(sigs.v_c(), {WASM_TABLE_FILL(tab_ref, WASM_ONE,
3392
                                             WASM_GET_LOCAL(0), WASM_ONE)});
3393
  // Check that the table index gets verified.
3394
  ExpectFailure(sigs.v_e(),
3395
                {WASM_TABLE_FILL(tab_ref + 2, WASM_ONE,
3396
                                 WASM_REF_NULL(kExternRefCode), WASM_ONE)});
3397 3398
}

3399 3400
TEST_F(FunctionBodyDecoderTest, TableOpsWithoutTable) {
  {
3401
    WASM_FEATURE_SCOPE(reftypes);
3402 3403
    ExpectFailure(sigs.i_v(), {WASM_TABLE_GROW(0, WASM_REF_NULL(kExternRefCode),
                                               WASM_ONE)});
3404
    ExpectFailure(sigs.i_v(), {WASM_TABLE_SIZE(0)});
3405
    ExpectFailure(sigs.i_e(),
3406
                  {WASM_TABLE_FILL(0, WASM_ONE, WASM_REF_NULL(kExternRefCode),
3407
                                   WASM_ONE)});
3408 3409 3410
  }
  {
    WASM_FEATURE_SCOPE(bulk_memory);
3411
    builder.AddPassiveElementSegment(wasm::kWasmFuncRef);
3412
    ExpectFailure(sigs.v_v(),
3413
                  {WASM_TABLE_INIT(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
3414
    ExpectFailure(sigs.v_v(),
3415 3416 3417 3418 3419 3420
                  {WASM_TABLE_COPY(0, 0, WASM_ZERO, WASM_ZERO, WASM_ZERO)});
  }
}

TEST_F(FunctionBodyDecoderTest, TableCopyMultiTable) {
  WASM_FEATURE_SCOPE(bulk_memory);
3421
  WASM_FEATURE_SCOPE(reftypes);
3422 3423
  {
    TestModuleBuilder builder;
3424
    builder.AddTable(kWasmExternRef, 10, true, 20);
3425
    builder.AddPassiveElementSegment(wasm::kWasmFuncRef);
3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444
    module = builder.module();
    // We added one table, therefore table.copy on table 0 should work.
    int table_src = 0;
    int table_dst = 0;
    ExpectValidates(sigs.v_v(),
                    {WASM_TABLE_COPY(table_dst, table_src, WASM_ZERO, WASM_ZERO,
                                     WASM_ZERO)});
    // There is only one table, so table.copy on table 1 should fail.
    table_src = 0;
    table_dst = 1;
    ExpectFailure(sigs.v_v(), {WASM_TABLE_COPY(table_dst, table_src, WASM_ZERO,
                                               WASM_ZERO, WASM_ZERO)});
    table_src = 1;
    table_dst = 0;
    ExpectFailure(sigs.v_v(), {WASM_TABLE_COPY(table_dst, table_src, WASM_ZERO,
                                               WASM_ZERO, WASM_ZERO)});
  }
  {
    TestModuleBuilder builder;
3445 3446
    builder.AddTable(kWasmExternRef, 10, true, 20);
    builder.AddTable(kWasmExternRef, 10, true, 20);
3447
    builder.AddPassiveElementSegment(wasm::kWasmFuncRef);
3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465
    module = builder.module();
    // We added two tables, therefore table.copy on table 0 should work.
    int table_src = 0;
    int table_dst = 0;
    ExpectValidates(sigs.v_v(),
                    {WASM_TABLE_COPY(table_dst, table_src, WASM_ZERO, WASM_ZERO,
                                     WASM_ZERO)});
    // Also table.copy on table 1 should work now.
    table_src = 1;
    table_dst = 0;
    ExpectValidates(sigs.v_v(),
                    {WASM_TABLE_COPY(table_dst, table_src, WASM_ZERO, WASM_ZERO,
                                     WASM_ZERO)});
    table_src = 0;
    table_dst = 1;
    ExpectValidates(sigs.v_v(),
                    {WASM_TABLE_COPY(table_dst, table_src, WASM_ZERO, WASM_ZERO,
                                     WASM_ZERO)});
3466
  }
3467 3468
}

3469 3470
TEST_F(FunctionBodyDecoderTest, TableInitMultiTable) {
  WASM_FEATURE_SCOPE(bulk_memory);
3471
  WASM_FEATURE_SCOPE(reftypes);
3472 3473
  {
    TestModuleBuilder builder;
3474 3475
    builder.AddTable(kWasmExternRef, 10, true, 20);
    builder.AddPassiveElementSegment(wasm::kWasmExternRef);
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487
    module = builder.module();
    // We added one table, therefore table.init on table 0 should work.
    int table_index = 0;
    ExpectValidates(sigs.v_v(), {WASM_TABLE_INIT(table_index, 0, WASM_ZERO,
                                                 WASM_ZERO, WASM_ZERO)});
    // There is only one table, so table.init on table 1 should fail.
    table_index = 1;
    ExpectFailure(sigs.v_v(), {WASM_TABLE_INIT(table_index, 0, WASM_ZERO,
                                               WASM_ZERO, WASM_ZERO)});
  }
  {
    TestModuleBuilder builder;
3488 3489 3490
    builder.AddTable(kWasmExternRef, 10, true, 20);
    builder.AddTable(kWasmExternRef, 10, true, 20);
    builder.AddPassiveElementSegment(wasm::kWasmExternRef);
3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502
    module = builder.module();
    // We added two tables, therefore table.init on table 0 should work.
    int table_index = 0;
    ExpectValidates(sigs.v_v(), {WASM_TABLE_INIT(table_index, 0, WASM_ZERO,
                                                 WASM_ZERO, WASM_ZERO)});
    // Also table.init on table 1 should work now.
    table_index = 1;
    ExpectValidates(sigs.v_v(), {WASM_TABLE_INIT(table_index, 0, WASM_ZERO,
                                                 WASM_ZERO, WASM_ZERO)});
  }
}

3503
TEST_F(FunctionBodyDecoderTest, UnpackPackedTypes) {
3504
  WASM_FEATURE_SCOPE(reftypes);
3505
  WASM_FEATURE_SCOPE(typed_funcref);
3506 3507 3508 3509 3510
  WASM_FEATURE_SCOPE(gc);
  {
    TestModuleBuilder builder;
    byte type_index = builder.AddStruct({F(kWasmI8, true), F(kWasmI16, false)});
    module = builder.module();
3511 3512 3513 3514 3515 3516 3517
    ExpectValidates(
        sigs.v_v(),
        {WASM_STRUCT_SET(
            type_index, 0,
            WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(1), WASM_I32V(42),
                                     WASM_RTT_CANON(type_index)),
            WASM_I32V(-1))});
3518 3519 3520 3521 3522
  }
  {
    TestModuleBuilder builder;
    byte type_index = builder.AddArray(kWasmI8, true);
    module = builder.module();
3523 3524 3525 3526 3527 3528
    ExpectValidates(sigs.v_v(),
                    {WASM_ARRAY_SET(type_index,
                                    WASM_ARRAY_NEW_WITH_RTT(
                                        type_index, WASM_I32V(10), WASM_I32V(5),
                                        WASM_RTT_CANON(type_index)),
                                    WASM_I32V(3), WASM_I32V(12345678))});
3529 3530 3531
  }
}

3532 3533 3534 3535 3536 3537 3538
ValueType ref(byte type_index) {
  return ValueType::Ref(type_index, kNonNullable);
}
ValueType optref(byte type_index) {
  return ValueType::Ref(type_index, kNullable);
}

3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
TEST_F(FunctionBodyDecoderTest, StructNewDefault) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);
  {
    TestModuleBuilder builder;
    byte type_index = builder.AddStruct({F(kWasmI32, true)});
    byte bad_type_index = builder.AddStruct({F(ref(type_index), true)});
    module = builder.module();
    ExpectValidates(sigs.v_v(), {WASM_STRUCT_NEW_DEFAULT(
                                     type_index, WASM_RTT_CANON(type_index)),
                                 WASM_DROP});
    ExpectFailure(sigs.v_v(),
                  {WASM_STRUCT_NEW_DEFAULT(bad_type_index,
                                           WASM_RTT_CANON(bad_type_index)),
                   WASM_DROP});
  }
  {
    TestModuleBuilder builder;
    byte type_index = builder.AddArray(kWasmI32, true);
    byte bad_type_index = builder.AddArray(ref(type_index), true);
    module = builder.module();
    ExpectValidates(sigs.v_v(),
                    {WASM_ARRAY_NEW_DEFAULT(type_index, WASM_I32V(3),
                                            WASM_RTT_CANON(type_index)),
                     WASM_DROP});
    ExpectFailure(sigs.v_v(),
                  {WASM_ARRAY_NEW_DEFAULT(bad_type_index, WASM_I32V(3),
                                          WASM_RTT_CANON(bad_type_index)),
                   WASM_DROP});
  }
}

3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582
TEST_F(FunctionBodyDecoderTest, DefaultableLocal) {
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(reftypes);
  AddLocals(kWasmExternRef, 1);
  ExpectValidates(sigs.v_v(), {});
}

TEST_F(FunctionBodyDecoderTest, NonDefaultableLocal) {
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(reftypes);
  AddLocals(ValueType::Ref(HeapType::kExtern, kNonNullable), 1);
3583 3584
  ExpectFailure(sigs.v_v(), {}, kAppendEnd,
                "Cannot define function-level local of non-defaultable type");
3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
}

TEST_F(FunctionBodyDecoderTest, RefEq) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(eh);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(simd);
  WASM_FEATURE_SCOPE(gc);

  TestModuleBuilder builder;
  module = builder.module();
  byte struct_type_index = builder.AddStruct({F(kWasmI32, true)});
3597
  ValueType eqref_subtypes[] = {kWasmEqRef,
3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
                                kWasmI31Ref,
                                ValueType::Ref(HeapType::kEq, kNonNullable),
                                ValueType::Ref(HeapType::kI31, kNullable),
                                ref(struct_type_index),
                                optref(struct_type_index)};
  ValueType non_eqref_subtypes[] = {
      kWasmI32,
      kWasmI64,
      kWasmF32,
      kWasmF64,
      kWasmS128,
3609 3610
      kWasmExnRef,
      kWasmExternRef,
3611
      kWasmFuncRef,
3612 3613
      ValueType::Ref(HeapType::kExn, kNonNullable),
      ValueType::Ref(HeapType::kExtern, kNonNullable),
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
      ValueType::Ref(HeapType::kFunc, kNonNullable)};

  for (ValueType type1 : eqref_subtypes) {
    for (ValueType type2 : eqref_subtypes) {
      ValueType reps[] = {kWasmI32, type1, type2};
      FunctionSig sig(1, 2, reps);
      ExpectValidates(&sig,
                      {WASM_REF_EQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))});
    }
  }

  for (ValueType type1 : eqref_subtypes) {
    for (ValueType type2 : non_eqref_subtypes) {
      ValueType reps[] = {kWasmI32, type1, type2};
      FunctionSig sig(1, 2, reps);
      ExpectFailure(&sig, {WASM_REF_EQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))},
                    kAppendEnd, "expected type eqref, found local.get of type");
      ExpectFailure(&sig, {WASM_REF_EQ(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))},
                    kAppendEnd, "expected type eqref, found local.get of type");
    }
  }
}

3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700
TEST_F(FunctionBodyDecoderTest, RefAsNonNull) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(eh);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(simd);
  WASM_FEATURE_SCOPE(gc);

  TestModuleBuilder builder;
  module = builder.module();
  byte struct_type_index = builder.AddStruct({F(kWasmI32, true)});
  byte array_type_index = builder.AddArray(kWasmI32, true);
  uint32_t heap_types[] = {
      struct_type_index, array_type_index,  HeapType::kExn, HeapType::kFunc,
      HeapType::kEq,     HeapType::kExtern, HeapType::kI31};

  ValueType non_compatible_types[] = {kWasmI32, kWasmI64, kWasmF32, kWasmF64,
                                      kWasmS128};

  // It works with nullable types.
  for (uint32_t heap_type : heap_types) {
    ValueType reprs[] = {ValueType::Ref(heap_type, kNonNullable),
                         ValueType::Ref(heap_type, kNullable)};
    FunctionSig sig(1, 1, reprs);
    ExpectValidates(&sig, {WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0))});
  }

  // It works with non-nullable types.
  for (uint32_t heap_type : heap_types) {
    ValueType reprs[] = {ValueType::Ref(heap_type, kNonNullable),
                         ValueType::Ref(heap_type, kNonNullable)};
    FunctionSig sig(1, 1, reprs);
    ExpectValidates(&sig, {WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0))});
  }

  // It fails with other types.
  for (ValueType type : non_compatible_types) {
    FunctionSig sig(0, 1, &type);
    ExpectFailure(
        &sig, {WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0)), kExprDrop}, kAppendEnd,
        "invalid agrument type to ref.as_non_null: Expected reference type, "
        "got");
  }
}

TEST_F(FunctionBodyDecoderTest, RefNull) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(eh);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);

  TestModuleBuilder builder;
  module = builder.module();
  byte struct_type_index = builder.AddStruct({F(kWasmI32, true)});
  byte array_type_index = builder.AddArray(kWasmI32, true);
  uint32_t type_reprs[] = {
      struct_type_index, array_type_index,  HeapType::kExn, HeapType::kFunc,
      HeapType::kEq,     HeapType::kExtern, HeapType::kI31};
  // It works with heap types.
  for (uint32_t type_repr : type_reprs) {
    const ValueType type = ValueType::Ref(type_repr, kNullable);
    const FunctionSig sig(1, 0, &type);
    ExpectValidates(&sig, {WASM_REF_NULL(WASM_HEAP_TYPE(HeapType(type_repr)))});
  }
  // It fails for undeclared types.
3701 3702
  ExpectFailure(sigs.v_v(), {WASM_REF_NULL(42), kExprDrop}, kAppendEnd,
                "Type index 42 is out of bounds");
3703 3704 3705 3706 3707 3708 3709 3710 3711
}

TEST_F(FunctionBodyDecoderTest, RefIsNull) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(eh);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);

  ExpectValidates(sigs.i_i(),
3712
                  {WASM_REF_IS_NULL(WASM_REF_NULL(kExternRefCode))});
3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740
  ExpectFailure(
      sigs.i_i(), {WASM_REF_IS_NULL(WASM_GET_LOCAL(0))}, kAppendEnd,
      "invalid argument type to ref.is_null. Expected reference type, got i32");

  TestModuleBuilder builder;
  module = builder.module();
  byte struct_type_index = builder.AddStruct({F(kWasmI32, true)});
  byte array_type_index = builder.AddArray(kWasmI32, true);
  uint32_t heap_types[] = {
      struct_type_index, array_type_index,  HeapType::kExn, HeapType::kFunc,
      HeapType::kEq,     HeapType::kExtern, HeapType::kI31};

  for (uint32_t heap_type : heap_types) {
    const ValueType types[] = {kWasmI32, ValueType::Ref(heap_type, kNullable)};
    const FunctionSig sig(1, 1, types);
    // It works for nullable references.
    ExpectValidates(&sig, {WASM_REF_IS_NULL(WASM_GET_LOCAL(0))});
    // It works for non-nullable references.
    ExpectValidates(
        &sig, {WASM_REF_IS_NULL(WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0)))});
  }

  // It fails if the argument type is not a reference type.
  ExpectFailure(
      sigs.v_v(), {WASM_REF_IS_NULL(WASM_I32V(0)), kExprDrop}, kAppendEnd,
      "invalid argument type to ref.is_null. Expected reference type, got ");
}

3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762
TEST_F(FunctionBodyDecoderTest, BrOnNull) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);

  const ValueType reps[] = {ValueType::Ref(HeapType::kFunc, kNonNullable),
                            ValueType::Ref(HeapType::kFunc, kNullable)};
  const FunctionSig sig(1, 1, reps);
  ExpectValidates(
      &sig, {WASM_BLOCK_R(reps[0], WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0)),
                          WASM_BR_ON_NULL(0, WASM_GET_LOCAL(0)), WASM_I32V(0),
                          kExprSelectWithType, 1, WASM_REF_TYPE(reps[0]))});

  // Should have block return value on stack before calling br_on_null.
  ExpectFailure(&sig,
                {WASM_BLOCK_R(reps[0], WASM_BR_ON_NULL(0, WASM_GET_LOCAL(0)),
                              WASM_I32V(0), kExprSelectWithType, 1,
                              WASM_REF_TYPE(reps[0]))},
                kAppendEnd,
                "expected 1 elements on the stack for br to @1, found 0");
}

3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
TEST_F(FunctionBodyDecoderTest, GCStruct) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);

  TestModuleBuilder builder;
  module = builder.module();
  byte struct_type_index = builder.AddStruct({F(kWasmI32, true)});
  byte array_type_index = builder.AddArray(kWasmI32, true);
  byte immutable_struct_type_index = builder.AddStruct({F(kWasmI32, false)});
  byte field_index = 0;

  ValueType struct_type = ValueType::Ref(struct_type_index, kNonNullable);
  ValueType reps_i_r[] = {kWasmI32, struct_type};
  ValueType reps_f_r[] = {kWasmF32, struct_type};
  const FunctionSig sig_i_r(1, 1, reps_i_r);
  const FunctionSig sig_v_r(0, 1, &struct_type);
  const FunctionSig sig_r_v(1, 0, &struct_type);
  const FunctionSig sig_f_r(1, 1, reps_f_r);

3783 3784 3785 3786
  /** struct.new_with_rtt **/
  ExpectValidates(
      &sig_r_v, {WASM_STRUCT_NEW_WITH_RTT(struct_type_index, WASM_I32V(0),
                                          WASM_RTT_CANON(struct_type_index))});
3787
  // Too few arguments.
3788 3789 3790 3791 3792 3793
  ExpectFailure(&sig_r_v,
                {WASM_STRUCT_NEW_WITH_RTT(struct_type_index,
                                          WASM_RTT_CANON(struct_type_index))},
                kAppendEnd,
                "not enough arguments on the stack for struct.new_with_rtt, "
                "expected 1 more");
3794 3795 3796
  // Too many arguments.
  ExpectFailure(
      &sig_r_v,
3797 3798
      {WASM_STRUCT_NEW_WITH_RTT(struct_type_index, WASM_I32V(0), WASM_I32V(1),
                                WASM_RTT_CANON(struct_type_index))},
3799 3800 3801
      kAppendEnd,
      "expected 1 elements on the stack for fallthru to @1, found 2");
  // Mistyped arguments.
3802 3803 3804 3805 3806 3807
  ExpectFailure(&sig_v_r,
                {WASM_STRUCT_NEW_WITH_RTT(struct_type_index, WASM_GET_LOCAL(0),
                                          WASM_RTT_CANON(struct_type_index))},
                kAppendEnd,
                "struct.new_with_rtt[0] expected type i32, found local.get of "
                "type (ref 0)");
3808 3809
  // Wrongly typed index.
  ExpectFailure(sigs.v_v(),
3810 3811 3812
                {WASM_STRUCT_NEW_WITH_RTT(array_type_index, WASM_I32V(0),
                                          WASM_RTT_CANON(struct_type_index)),
                 kExprDrop},
3813
                kAppendEnd, "invalid struct index: 1");
3814 3815 3816 3817 3818 3819 3820 3821
  // Wrongly typed rtt.
  ExpectFailure(
      sigs.v_v(),
      {WASM_STRUCT_NEW_WITH_RTT(struct_type_index, WASM_I32V(0),
                                WASM_RTT_CANON(array_type_index)),
       kExprDrop},
      kAppendEnd,
      "struct.new_with_rtt expected rtt for type 0, found rtt for type 1");
3822
  // Out-of-bounds index.
3823 3824 3825 3826
  ExpectFailure(sigs.v_v(),
                {WASM_STRUCT_NEW_WITH_RTT(42, WASM_I32V(0),
                                          WASM_RTT_CANON(struct_type_index)),
                 kExprDrop},
3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873
                kAppendEnd, "invalid struct index: 42");

  /** struct.get **/
  ExpectValidates(&sig_i_r, {WASM_STRUCT_GET(struct_type_index, field_index,
                                             WASM_GET_LOCAL(0))});
  // With non-nullable struct.
  ExpectValidates(&sig_i_r,
                  {WASM_STRUCT_GET(struct_type_index, field_index,
                                   WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0)))});
  // Wrong index.
  ExpectFailure(
      &sig_v_r,
      {WASM_STRUCT_GET(struct_type_index, field_index + 1, WASM_GET_LOCAL(0)),
       kExprDrop},
      kAppendEnd, "invalid field index: 1");
  // Mistyped expected type.
  ExpectFailure(
      &sig_f_r,
      {WASM_STRUCT_GET(struct_type_index, field_index, WASM_GET_LOCAL(0))},
      kAppendEnd, "type error in merge[0] (expected f32, got i32)");

  /** struct.set **/
  ExpectValidates(&sig_v_r, {WASM_STRUCT_SET(struct_type_index, field_index,
                                             WASM_GET_LOCAL(0), WASM_I32V(0))});
  // Non-nullable struct.
  ExpectValidates(
      &sig_v_r,
      {WASM_STRUCT_SET(struct_type_index, field_index,
                       WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0)), WASM_I32V(0))});
  // Wrong index.
  ExpectFailure(&sig_v_r,
                {WASM_STRUCT_SET(struct_type_index, field_index + 1,
                                 WASM_GET_LOCAL(0), WASM_I32V(0))},
                kAppendEnd, "invalid field index: 1");
  // Mistyped input.
  ExpectFailure(&sig_v_r,
                {WASM_STRUCT_SET(struct_type_index, field_index,
                                 WASM_GET_LOCAL(0), WASM_I64V(0))},
                kAppendEnd,
                "struct.set[1] expected type i32, found i64.const of type i64");
  // Expecting output.
  ExpectFailure(&sig_i_r,
                {WASM_STRUCT_SET(struct_type_index, field_index,
                                 WASM_GET_LOCAL(0), WASM_I32V(0))},
                kAppendEnd,
                "expected 1 elements on the stack for fallthru to @1, found 0");
  // Setting immutable field.
3874 3875 3876 3877 3878 3879 3880 3881
  ExpectFailure(
      sigs.v_v(),
      {WASM_STRUCT_SET(
          immutable_struct_type_index, field_index,
          WASM_STRUCT_NEW_WITH_RTT(immutable_struct_type_index, WASM_I32V(42),
                                   WASM_RTT_CANON(immutable_struct_type_index)),
          WASM_I32V(0))},
      kAppendEnd, "setting immutable struct field");
3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897

  // struct.get_s/u fail
  ExpectFailure(
      &sig_i_r,
      {WASM_STRUCT_GET_S(struct_type_index, field_index, WASM_GET_LOCAL(0))},
      kAppendEnd,
      "struct.get_s is only valid for packed struct fields. Use struct.get "
      "instead.");
  ExpectFailure(
      &sig_i_r,
      {WASM_STRUCT_GET_U(struct_type_index, field_index, WASM_GET_LOCAL(0))},
      kAppendEnd,
      "struct.get_u is only valid for packed struct fields. Use struct.get "
      "instead.");
}

3898
TEST_F(FunctionBodyDecoderTest, GCArray) {
3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);

  TestModuleBuilder builder;
  module = builder.module();
  byte array_type_index = builder.AddArray(kWasmFuncRef, true);
  byte struct_type_index = builder.AddStruct({F(kWasmI32, false)});

  ValueType array_type = ValueType::Ref(array_type_index, kNonNullable);
  ValueType reps_c_r[] = {kWasmFuncRef, array_type};
  ValueType reps_f_r[] = {kWasmF32, array_type};
  ValueType reps_i_r[] = {kWasmI32, array_type};
  const FunctionSig sig_c_r(1, 1, reps_c_r);
  const FunctionSig sig_v_r(0, 1, &array_type);
  const FunctionSig sig_r_v(1, 0, &array_type);
  const FunctionSig sig_f_r(1, 1, reps_f_r);
  const FunctionSig sig_v_cr(0, 2, reps_c_r);
  const FunctionSig sig_i_r(1, 1, reps_i_r);

3919 3920 3921
  /** array.new_with_rtt **/
  ExpectValidates(&sig_r_v,
                  {WASM_ARRAY_NEW_WITH_RTT(
3922
                      array_type_index, WASM_REF_NULL(kFuncRefCode),
3923
                      WASM_I32V(10), WASM_RTT_CANON(array_type_index))});
3924
  // Too few arguments.
3925 3926 3927 3928 3929 3930 3931 3932 3933
  ExpectFailure(&sig_r_v,
                {WASM_I32V(10), WASM_RTT_CANON(array_type_index),
                 WASM_GC_OP(kExprArrayNewWithRtt), array_type_index},
                kAppendEnd,
                "not enough arguments on the stack for array.new_with_rtt, "
                "expected 1 more");
  // Mistyped initializer.
  ExpectFailure(&sig_r_v,
                {WASM_ARRAY_NEW_WITH_RTT(
3934
                    array_type_index, WASM_REF_NULL(kExternRefCode),
3935 3936 3937 3938 3939
                    WASM_I32V(10), WASM_RTT_CANON(array_type_index))},
                kAppendEnd,
                "array.new_with_rtt[0] expected type funcref, found ref.null "
                "of type externref");
  // Mistyped length.
3940
  ExpectFailure(
3941
      &sig_r_v,
3942
      {WASM_ARRAY_NEW_WITH_RTT(array_type_index, WASM_REF_NULL(kFuncRefCode),
3943
                               WASM_I64V(5), WASM_RTT_CANON(array_type_index))},
3944
      kAppendEnd,
3945 3946
      "array.new_with_rtt[1] expected type i32, found i64.const of type i64");
  // Mistyped rtt.
3947 3948
  ExpectFailure(
      &sig_r_v,
3949
      {WASM_ARRAY_NEW_WITH_RTT(array_type_index, WASM_REF_NULL(kFuncRefCode),
3950 3951
                               WASM_I32V(5),
                               WASM_RTT_CANON(struct_type_index))},
3952
      kAppendEnd,
3953
      "array.new_with_rtt expected rtt for type 0, found rtt for type 1");
3954
  // Wrong type index.
3955 3956
  ExpectFailure(
      sigs.v_v(),
3957
      {WASM_ARRAY_NEW_WITH_RTT(struct_type_index, WASM_REF_NULL(kFuncRefCode),
3958 3959 3960
                               WASM_I32V(10), WASM_RTT_CANON(array_type_index)),
       kExprDrop},
      kAppendEnd, "invalid array index: 1");
3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987

  /** array.get **/
  ExpectValidates(&sig_c_r, {WASM_ARRAY_GET(array_type_index, WASM_GET_LOCAL(0),
                                            WASM_I32V(5))});
  // With non-nullable array type.
  ExpectValidates(
      &sig_c_r,
      {WASM_ARRAY_GET(array_type_index, WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0)),
                      WASM_I32V(5))});
  // Wrongly typed index.
  ExpectFailure(
      &sig_v_r,
      {WASM_ARRAY_GET(array_type_index, WASM_GET_LOCAL(0), WASM_I64V(5)),
       kExprDrop},
      kAppendEnd,
      "array.get[1] expected type i32, found i64.const of type i64");
  // Mistyped expected type.
  ExpectFailure(
      &sig_f_r,
      {WASM_ARRAY_GET(array_type_index, WASM_GET_LOCAL(0), WASM_I32V(5))},
      kAppendEnd, "type error in merge[0] (expected f32, got funcref)");

  // array.get_s/u fail.
  ExpectFailure(
      &sig_c_r,
      {WASM_ARRAY_GET_S(array_type_index, WASM_GET_LOCAL(0), WASM_I32V(5))},
      kAppendEnd,
3988
      "array.get_s is only valid for packed arrays. Use array.get instead.");
3989 3990 3991 3992
  ExpectFailure(
      &sig_c_r,
      {WASM_ARRAY_GET_U(array_type_index, WASM_GET_LOCAL(0), WASM_I32V(5))},
      kAppendEnd,
3993
      "array.get_u is only valid for packed arrays. Use array.get instead.");
3994 3995

  /** array.set **/
3996 3997 3998
  ExpectValidates(&sig_v_r,
                  {WASM_ARRAY_SET(array_type_index, WASM_GET_LOCAL(0),
                                  WASM_I32V(42), WASM_REF_NULL(kFuncRefCode))});
3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
  // With non-nullable array type.
  ExpectValidates(
      &sig_v_cr,
      {WASM_ARRAY_SET(array_type_index, WASM_GET_LOCAL(1), WASM_I32V(42),
                      WASM_REF_AS_NON_NULL(WASM_GET_LOCAL(0)))});
  // Non-array type index.
  ExpectFailure(&sig_v_cr,
                {WASM_ARRAY_SET(struct_type_index, WASM_GET_LOCAL(1),
                                WASM_I32V(42), WASM_GET_LOCAL(0))},
                kAppendEnd, "invalid array index: 1");
  // Wrongly typed index.
  ExpectFailure(&sig_v_cr,
                {WASM_ARRAY_SET(array_type_index, WASM_GET_LOCAL(1),
                                WASM_I64V(42), WASM_GET_LOCAL(0))},
                kAppendEnd,
                "array.set[1] expected type i32, found i64.const of type i64");
  // Wrongly typed value.
  ExpectFailure(
      &sig_v_cr,
      {WASM_ARRAY_SET(array_type_index, WASM_GET_LOCAL(1), WASM_I32V(42),
                      WASM_I64V(0))},
      kAppendEnd,
      "array.set[2] expected type funcref, found i64.const of type i64");

  /** array.len **/
  ExpectValidates(&sig_i_r,
                  {WASM_ARRAY_LEN(array_type_index, WASM_GET_LOCAL(0))});
  // Wrong return type.
  ExpectFailure(&sig_f_r, {WASM_ARRAY_LEN(array_type_index, WASM_GET_LOCAL(0))},
                kAppendEnd, "type error in merge[0] (expected f32, got i32)");
  // Non-array type index.
  ExpectFailure(&sig_i_r,
                {WASM_ARRAY_LEN(struct_type_index, WASM_GET_LOCAL(0))},
                kAppendEnd, "invalid array index: 1");
}

4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
TEST_F(FunctionBodyDecoderTest, PackedFields) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);

  TestModuleBuilder builder;
  module = builder.module();
  byte array_type_index = builder.AddArray(kWasmI8, true);
  byte struct_type_index = builder.AddStruct({F(kWasmI16, true)});
  byte field_index = 0;

  // *.new with packed fields works.
4047 4048 4049 4050
  ExpectValidates(sigs.v_v(), {WASM_ARRAY_NEW_WITH_RTT(
                                   array_type_index, WASM_I32V(0), WASM_I32V(5),
                                   WASM_RTT_CANON(array_type_index)),
                               kExprDrop});
4051
  ExpectValidates(sigs.v_v(),
4052 4053
                  {WASM_STRUCT_NEW_WITH_RTT(struct_type_index, WASM_I32V(42),
                                            WASM_RTT_CANON(struct_type_index)),
4054 4055 4056 4057
                   kExprDrop});
  // It can't unpack types other that i32.
  ExpectFailure(
      sigs.v_v(),
4058 4059 4060
      {WASM_ARRAY_NEW_WITH_RTT(array_type_index, WASM_I64V(0), WASM_I32V(5),
                               WASM_RTT_CANON(array_type_index)),
       kExprDrop},
4061
      kAppendEnd,
4062 4063 4064 4065 4066 4067 4068 4069
      "array.new_with_rtt[0] expected type i32, found i64.const of type i64");
  ExpectFailure(
      sigs.v_v(),
      {WASM_STRUCT_NEW_WITH_RTT(struct_type_index, WASM_I64V(42),
                                WASM_RTT_CANON(struct_type_index)),
       kExprDrop},
      kAppendEnd,
      "struct.new_with_rtt[0] expected type i32, found i64.const of type i64");
4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086

  // *.set with packed fields works.
  ExpectValidates(sigs.v_v(), {WASM_ARRAY_SET(array_type_index,
                                              WASM_REF_NULL(array_type_index),
                                              WASM_I32V(0), WASM_I32V(5))});
  ExpectValidates(sigs.v_v(), {WASM_STRUCT_SET(struct_type_index, field_index,
                                               WASM_REF_NULL(struct_type_index),
                                               WASM_I32V(42))});
  // It can't unpack into types other that i32.
  ExpectFailure(
      sigs.v_v(),
      {WASM_ARRAY_SET(array_type_index, WASM_REF_NULL(array_type_index),
                      WASM_I32V(0), WASM_I64V(5))},
      kAppendEnd,
      "array.set[2] expected type i32, found i64.const of type i64");
  ExpectFailure(
      sigs.v_v(),
4087 4088 4089
      {WASM_STRUCT_NEW_WITH_RTT(struct_type_index, field_index,
                                WASM_REF_NULL(struct_type_index), WASM_I64V(42),
                                WASM_RTT_CANON(struct_type_index))},
4090
      kAppendEnd,
4091
      "struct.new_with_rtt[0] expected type i32, found i64.const of type i64");
4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129

  // *.get_s/u works.
  ExpectValidates(sigs.i_v(), {WASM_ARRAY_GET_S(array_type_index,
                                                WASM_REF_NULL(array_type_index),
                                                WASM_I32V(0))});
  ExpectValidates(sigs.i_v(), {WASM_ARRAY_GET_U(array_type_index,
                                                WASM_REF_NULL(array_type_index),
                                                WASM_I32V(0))});
  ExpectValidates(sigs.i_v(),
                  {WASM_STRUCT_GET_S(struct_type_index, field_index,
                                     WASM_REF_NULL(struct_type_index))});
  ExpectValidates(sigs.i_v(),
                  {WASM_STRUCT_GET_U(struct_type_index, field_index,
                                     WASM_REF_NULL(struct_type_index))});

  // *.get fails.
  ExpectFailure(sigs.i_v(),
                {WASM_ARRAY_GET(array_type_index,
                                WASM_REF_NULL(array_type_index), WASM_I32V(0))},
                kAppendEnd,
                "array.get used with a field of packed type. Use array.get_s "
                "or array.get_u instead.");
  ExpectFailure(sigs.i_v(),
                {WASM_STRUCT_GET(struct_type_index, field_index,
                                 WASM_REF_NULL(struct_type_index))},
                kAppendEnd,
                "struct.get used with a field of packed type. Use struct.get_s "
                "or struct.get_u instead.");
}

TEST_F(FunctionBodyDecoderTest, PackedTypesAsLocals) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);
  AddLocals(kWasmI8, 1);
  ExpectFailure(sigs.v_v(), {}, kAppendEnd, "invalid local type");
}

4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
TEST_F(FunctionBodyDecoderTest, RttCanon) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);
  WASM_FEATURE_SCOPE(eh);

  TestModuleBuilder builder;
  module = builder.module();
  uint8_t array_type_index = builder.AddArray(kWasmI32, true);
  uint8_t struct_type_index = builder.AddStruct({F(kWasmI64, true)});

  for (HeapType::Representation heap :
       {HeapType::kExtern, HeapType::kEq, HeapType::kExn, HeapType::kI31,
        static_cast<HeapType::Representation>(array_type_index),
        static_cast<HeapType::Representation>(struct_type_index)}) {
    ValueType rtt1 = ValueType::Rtt(HeapType(heap), 1);
    FunctionSig sig1(1, 0, &rtt1);
    ExpectValidates(&sig1, {WASM_RTT_CANON(rtt1.heap_type().code() & 0x7F)});

    ValueType rtt2 = ValueType::Rtt(HeapType(heap), 2);
    FunctionSig sig2(1, 0, &rtt2);
    ExpectFailure(&sig2, {WASM_RTT_CANON(rtt2.heap_type().code() & 0x7F)},
                  kAppendEnd, "type error in merge[0]");
  }
}

TEST_F(FunctionBodyDecoderTest, RttSub) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);
  WASM_FEATURE_SCOPE(eh);

  TestModuleBuilder builder;
  module = builder.module();
  uint8_t array_type_index = builder.AddArray(kWasmI8, true);
  uint8_t super_struct_type_index = builder.AddStruct({F(kWasmI16, true)});
  uint8_t sub_struct_type_index =
      builder.AddStruct({F(kWasmI16, true), F(kWasmI32, false)});

  {
    // Can build an rtt.sub with self type for a generic heap type.
    ValueType type = ValueType::Rtt(HeapType::kFunc, 2);
    FunctionSig sig(1, 0, &type);
4173 4174
    ExpectValidates(&sig,
                    {WASM_RTT_SUB(kFuncRefCode, WASM_RTT_CANON(kFuncRefCode))});
4175 4176 4177 4178
  }

  // Trivial type error.
  ExpectFailure(sigs.v_v(),
4179
                {WASM_RTT_SUB(kFuncRefCode, WASM_I32V(42)), kExprDrop},
4180 4181 4182 4183 4184 4185 4186 4187 4188 4189
                kAppendEnd, "rtt.sub requires a supertype rtt on stack");

  {
    ValueType type = ValueType::Rtt(array_type_index, 2);
    FunctionSig sig(1, 0, &type);
    // Can build an rtt.sub with self type for an array type.
    ExpectValidates(&sig, {WASM_RTT_SUB(array_type_index,
                                        WASM_RTT_CANON(array_type_index))});
    // Can build an rtt.sub for an array from eqref.
    ExpectValidates(
4190
        &sig, {WASM_RTT_SUB(array_type_index, WASM_RTT_CANON(kEqRefCode))});
4191
    // Fails when argument to rtt.sub is not a supertype.
4192 4193 4194 4195
    ExpectFailure(
        sigs.v_v(),
        {WASM_RTT_SUB(kEqRefCode, WASM_RTT_CANON(array_type_index)), kExprDrop},
        kAppendEnd, "rtt.sub requires a supertype rtt on stack");
4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206
  }

  {
    ValueType type = ValueType::Rtt(super_struct_type_index, 2);
    FunctionSig sig(1, 0, &type);
    // Can build an rtt.sub with self type for a struct type.
    ExpectValidates(&sig,
                    {WASM_RTT_SUB(super_struct_type_index,
                                  WASM_RTT_CANON(super_struct_type_index))});
    // Can build an rtt.sub for a struct from eqref.
    ExpectValidates(&sig, {WASM_RTT_SUB(super_struct_type_index,
4207
                                        WASM_RTT_CANON(kEqRefCode))});
4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228
    // Fails when argument to rtt.sub is not a supertype.
    ExpectFailure(sigs.v_v(),
                  {WASM_RTT_SUB(super_struct_type_index,
                                WASM_RTT_CANON(array_type_index))},
                  kAppendEnd, "rtt.sub requires a supertype rtt on stack");
    ExpectFailure(sigs.v_v(),
                  {WASM_RTT_SUB(super_struct_type_index,
                                WASM_RTT_CANON(sub_struct_type_index))},
                  kAppendEnd, "rtt.sub requires a supertype rtt on stack");
  }

  {
    // Can build an rtt from a stuct supertype.
    ValueType type = ValueType::Rtt(sub_struct_type_index, 2);
    FunctionSig sig(1, 0, &type);
    ExpectValidates(&sig,
                    {WASM_RTT_SUB(sub_struct_type_index,
                                  WASM_RTT_CANON(super_struct_type_index))});
  }
}

4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302
TEST_F(FunctionBodyDecoderTest, RefTestCast) {
  WASM_FEATURE_SCOPE(reftypes);
  WASM_FEATURE_SCOPE(typed_funcref);
  WASM_FEATURE_SCOPE(gc);
  WASM_FEATURE_SCOPE(eh);

  TestModuleBuilder builder;
  module = builder.module();
  HeapType::Representation array_heap =
      static_cast<HeapType::Representation>(builder.AddArray(kWasmI8, true));
  HeapType::Representation super_struct_heap =
      static_cast<HeapType::Representation>(
          builder.AddStruct({F(kWasmI16, true)}));

  HeapType::Representation sub_struct_heap =
      static_cast<HeapType::Representation>(
          builder.AddStruct({F(kWasmI16, true), F(kWasmI32, false)}));

  // Passing/failing tests due to static subtyping.
  std::pair<HeapType::Representation, HeapType::Representation> valid_pairs[] =
      {{HeapType::kEq, HeapType::kI31},
       {HeapType::kFunc, HeapType::kFunc},
       {HeapType::kEq, array_heap},
       {HeapType::kEq, super_struct_heap},
       {super_struct_heap, sub_struct_heap}};

  for (auto pair : valid_pairs) {
    HeapType from_heap = HeapType(pair.first);
    HeapType to_heap = HeapType(pair.second);
    ValueType test_reps[] = {kWasmI32, ValueType::Ref(from_heap, kNullable)};
    FunctionSig test_sig(1, 1, test_reps);
    ValueType cast_reps[] = {ValueType::Ref(to_heap, kNonNullable),
                             ValueType::Ref(from_heap, kNullable)};
    FunctionSig cast_sig(1, 1, cast_reps);
    ExpectValidates(&test_sig,
                    {WASM_REF_TEST(WASM_HEAP_TYPE(from_heap),
                                   WASM_HEAP_TYPE(to_heap), WASM_GET_LOCAL(0),
                                   WASM_RTT_CANON(WASM_HEAP_TYPE(to_heap)))});
    ExpectValidates(&cast_sig,
                    {WASM_REF_CAST(WASM_HEAP_TYPE(from_heap),
                                   WASM_HEAP_TYPE(to_heap), WASM_GET_LOCAL(0),
                                   WASM_RTT_CANON(WASM_HEAP_TYPE(to_heap)))});
  }

  std::pair<HeapType::Representation, HeapType::Representation>
      invalid_pairs[] = {{HeapType::kI31, HeapType::kEq},
                         {array_heap, super_struct_heap},
                         {array_heap, HeapType::kEq},
                         {HeapType::kExtern, HeapType::kExn}};

  for (auto pair : invalid_pairs) {
    HeapType from_heap = HeapType(pair.first);
    HeapType to_heap = HeapType(pair.second);
    ValueType test_reps[] = {kWasmI32, ValueType::Ref(from_heap, kNullable)};
    FunctionSig test_sig(1, 1, test_reps);
    ValueType cast_reps[] = {ValueType::Ref(to_heap, kNonNullable),
                             ValueType::Ref(from_heap, kNullable)};
    FunctionSig cast_sig(1, 1, cast_reps);
    ExpectFailure(&test_sig,
                  {WASM_REF_TEST(WASM_HEAP_TYPE(from_heap),
                                 WASM_HEAP_TYPE(to_heap), WASM_GET_LOCAL(0),
                                 WASM_RTT_CANON(WASM_HEAP_TYPE(to_heap)))},
                  kAppendEnd,
                  "ref.test: rtt type must be subtype of object type");
    ExpectFailure(&cast_sig,
                  {WASM_REF_CAST(WASM_HEAP_TYPE(from_heap),
                                 WASM_HEAP_TYPE(to_heap), WASM_GET_LOCAL(0),
                                 WASM_RTT_CANON(WASM_HEAP_TYPE(to_heap)))},
                  kAppendEnd,
                  "ref.cast: rtt type must be subtype of object type");
  }

  // Trivial type error.
  ExpectFailure(sigs.v_v(),
4303 4304
                {WASM_REF_TEST(kEqRefCode, kI31RefCode, WASM_I32V(1),
                               WASM_RTT_CANON(kI31RefCode)),
4305 4306 4307 4308
                 kExprDrop},
                kAppendEnd,
                "ref.test[0] expected type eqref, found i32.const of type i32");
  ExpectFailure(sigs.v_v(),
4309 4310
                {WASM_REF_CAST(kEqRefCode, kI31RefCode, WASM_I32V(1),
                               WASM_RTT_CANON(kI31RefCode)),
4311 4312 4313 4314 4315 4316 4317 4318 4319 4320
                 kExprDrop},
                kAppendEnd,
                "ref.cast[0] expected type eqref, found i32.const of type i32");

  // Mismached object heap immediate.
  {
    ValueType arg_type = ValueType::Ref(HeapType::kEq, kNonNullable);
    FunctionSig sig(0, 1, &arg_type);
    ExpectFailure(
        &sig,
4321 4322
        {WASM_REF_TEST(kEqRefCode, static_cast<byte>(array_heap),
                       WASM_GET_LOCAL(0), WASM_RTT_CANON(kI31RefCode)),
4323 4324 4325 4326
         kExprDrop},
        kAppendEnd, "ref.test: expected rtt for type 0 but got (rtt 1 i31)");
    ExpectFailure(
        &sig,
4327 4328
        {WASM_REF_CAST(kEqRefCode, static_cast<byte>(array_heap),
                       WASM_GET_LOCAL(0), WASM_RTT_CANON(kI31RefCode)),
4329 4330 4331 4332 4333
         kExprDrop},
        kAppendEnd, "ref.cast: expected rtt for type 0 but got (rtt 1 i31)");
  }
}

4334 4335 4336 4337 4338
class BranchTableIteratorTest : public TestWithZone {
 public:
  BranchTableIteratorTest() : TestWithZone() {}
  void CheckBrTableSize(const byte* start, const byte* end) {
    Decoder decoder(start, end);
4339 4340
    BranchTableImmediate<Decoder::kFullValidation> operand(&decoder, start + 1);
    BranchTableIterator<Decoder::kFullValidation> iterator(&decoder, operand);
4341
    EXPECT_EQ(end - start - 1u, iterator.length());
4342
    EXPECT_OK(decoder);
4343 4344 4345
  }
  void CheckBrTableError(const byte* start, const byte* end) {
    Decoder decoder(start, end);
4346 4347
    BranchTableImmediate<Decoder::kFullValidation> operand(&decoder, start + 1);
    BranchTableIterator<Decoder::kFullValidation> iterator(&decoder, operand);
4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391
    iterator.length();
    EXPECT_FALSE(decoder.ok());
  }
};

#define CHECK_BR_TABLE_LENGTH(...)                    \
  {                                                   \
    static byte code[] = {kExprBrTable, __VA_ARGS__}; \
    CheckBrTableSize(code, code + sizeof(code));      \
  }

#define CHECK_BR_TABLE_ERROR(...)                     \
  {                                                   \
    static byte code[] = {kExprBrTable, __VA_ARGS__}; \
    CheckBrTableError(code, code + sizeof(code));     \
  }

TEST_F(BranchTableIteratorTest, count0) {
  CHECK_BR_TABLE_LENGTH(0, U32V_1(1));
  CHECK_BR_TABLE_LENGTH(0, U32V_2(200));
  CHECK_BR_TABLE_LENGTH(0, U32V_3(30000));
  CHECK_BR_TABLE_LENGTH(0, U32V_4(400000));

  CHECK_BR_TABLE_LENGTH(0, U32V_1(2));
  CHECK_BR_TABLE_LENGTH(0, U32V_2(300));
  CHECK_BR_TABLE_LENGTH(0, U32V_3(40000));
  CHECK_BR_TABLE_LENGTH(0, U32V_4(500000));
}

TEST_F(BranchTableIteratorTest, count1) {
  CHECK_BR_TABLE_LENGTH(1, U32V_1(1), U32V_1(6));
  CHECK_BR_TABLE_LENGTH(1, U32V_2(200), U32V_1(8));
  CHECK_BR_TABLE_LENGTH(1, U32V_3(30000), U32V_1(9));
  CHECK_BR_TABLE_LENGTH(1, U32V_4(400000), U32V_1(11));

  CHECK_BR_TABLE_LENGTH(1, U32V_1(2), U32V_2(6));
  CHECK_BR_TABLE_LENGTH(1, U32V_2(300), U32V_2(7));
  CHECK_BR_TABLE_LENGTH(1, U32V_3(40000), U32V_2(8));
  CHECK_BR_TABLE_LENGTH(1, U32V_4(500000), U32V_2(9));
}

TEST_F(BranchTableIteratorTest, error0) {
  CHECK_BR_TABLE_ERROR(0);
  CHECK_BR_TABLE_ERROR(1, U32V_1(33));
4392 4393
}

4394 4395 4396
#undef CHECK_BR_TABLE_LENGTH
#undef CHECK_BR_TABLE_ERROR

4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
struct PrintOpcodes {
  const byte* start;
  const byte* end;
};
std::ostream& operator<<(std::ostream& out, const PrintOpcodes& range) {
  out << "First opcode: \""
      << WasmOpcodes::OpcodeName(static_cast<WasmOpcode>(*range.start))
      << "\"\nall bytes: [";
  for (const byte* b = range.start; b < range.end; ++b) {
    out << (b == range.start ? "" : ", ") << uint32_t{*b} << "/"
        << AsHex(*b, 2, true);
  }
  return out << "]";
}

4412 4413 4414 4415
class WasmOpcodeLengthTest : public TestWithZone {
 public:
  WasmOpcodeLengthTest() : TestWithZone() {}

4416 4417 4418 4419 4420
  template <typename... Bytes>
  void ExpectLength(unsigned expected, Bytes... bytes) {
    const byte code[] = {bytes..., 0, 0, 0, 0, 0, 0, 0, 0};
    EXPECT_EQ(expected, OpcodeLength(code, code + sizeof(code)))
        << PrintOpcodes{code, code + sizeof...(bytes)};
4421
  }
4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435

  // Helper to check for prefixed opcodes, which can have multiple bytes.
  void ExpectLengthPrefixed(unsigned operands, WasmOpcode opcode) {
    uint8_t prefix = (opcode >> 8) & 0xff;
    DCHECK(WasmOpcodes::IsPrefixOpcode(static_cast<WasmOpcode>(prefix)));
    uint8_t index = opcode & 0xff;
    uint8_t encoded[2] = {0, 0};
    uint8_t* p = encoded;
    unsigned len = static_cast<unsigned>(LEBHelper::sizeof_u32v(index));
    DCHECK_GE(2, len);
    LEBHelper::write_u32v(&p, index);
    // length of index, + number of operands + prefix bye
    ExpectLength(len + operands + 1, prefix, encoded[0], encoded[1]);
  }
4436 4437 4438 4439 4440

  template <typename... Bytes>
  void ExpectFailure(Bytes... bytes) {
    const byte code[] = {bytes..., 0, 0, 0, 0, 0, 0, 0, 0};
    WasmFeatures no_features = WasmFeatures::None();
4441 4442 4443 4444
    WasmDecoder<Decoder::kFullValidation> decoder(
        this->zone(), nullptr, no_features, &no_features, nullptr, code,
        code + sizeof(code), 0);
    WasmDecoder<Decoder::kFullValidation>::OpcodeLength(&decoder, code);
4445 4446
    EXPECT_EQ(decoder.failed(), true);
  }
4447
};
4448

4449
TEST_F(WasmOpcodeLengthTest, Statements) {
4450 4451 4452 4453 4454 4455 4456 4457 4458 4459
  ExpectLength(1, kExprNop);
  ExpectLength(1, kExprElse);
  ExpectLength(1, kExprEnd);
  ExpectLength(1, kExprSelect);
  ExpectLength(1, kExprCatch);
  ExpectLength(1, kExprRethrow);
  ExpectLength(2, kExprBr);
  ExpectLength(2, kExprBrIf);
  ExpectLength(2, kExprThrow);
  ExpectLength(3, kExprBrOnExn);
4460 4461 4462 4463
  ExpectLength(2, kExprBlock, kI32Code);
  ExpectLength(2, kExprLoop, kI32Code);
  ExpectLength(2, kExprIf, kI32Code);
  ExpectLength(2, kExprTry, kI32Code);
4464 4465 4466
}

TEST_F(WasmOpcodeLengthTest, MiscExpressions) {
4467 4468
  ExpectLength(5, kExprF32Const);
  ExpectLength(9, kExprF64Const);
4469
  ExpectLength(2, kExprRefNull);
4470 4471
  ExpectLength(2, kExprLocalGet);
  ExpectLength(2, kExprLocalSet);
4472 4473
  ExpectLength(2, kExprGlobalGet);
  ExpectLength(2, kExprGlobalSet);
4474 4475
  ExpectLength(2, kExprCallFunction);
  ExpectLength(3, kExprCallIndirect);
4476 4477
}

4478
TEST_F(WasmOpcodeLengthTest, I32Const) {
4479 4480 4481 4482 4483
  ExpectLength(2, kExprI32Const, U32V_1(1));
  ExpectLength(3, kExprI32Const, U32V_2(999));
  ExpectLength(4, kExprI32Const, U32V_3(9999));
  ExpectLength(5, kExprI32Const, U32V_4(999999));
  ExpectLength(6, kExprI32Const, U32V_5(99999999));
4484
}
4485

4486
TEST_F(WasmOpcodeLengthTest, I64Const) {
4487 4488 4489 4490 4491 4492 4493 4494 4495
  ExpectLength(2, kExprI64Const, U32V_1(1));
  ExpectLength(3, kExprI64Const, U32V_2(99));
  ExpectLength(4, kExprI64Const, U32V_3(9999));
  ExpectLength(5, kExprI64Const, U32V_4(99999));
  ExpectLength(6, kExprI64Const, U32V_5(9999999));
  ExpectLength(7, WASM_I64V_6(777777));
  ExpectLength(8, WASM_I64V_7(7777777));
  ExpectLength(9, WASM_I64V_8(77777777));
  ExpectLength(10, WASM_I64V_9(777777777));
4496 4497
}

4498
TEST_F(WasmOpcodeLengthTest, VariableLength) {
4499 4500 4501 4502 4503
  ExpectLength(2, kExprGlobalGet, U32V_1(1));
  ExpectLength(3, kExprGlobalGet, U32V_2(33));
  ExpectLength(4, kExprGlobalGet, U32V_3(44));
  ExpectLength(5, kExprGlobalGet, U32V_4(66));
  ExpectLength(6, kExprGlobalGet, U32V_5(77));
4504 4505 4506 4507 4508 4509

  ExpectLength(2, kExprRefFunc, U32V_1(1));
  ExpectLength(3, kExprRefFunc, U32V_2(33));
  ExpectLength(4, kExprRefFunc, U32V_3(44));
  ExpectLength(5, kExprRefFunc, U32V_4(66));
  ExpectLength(6, kExprRefFunc, U32V_5(77));
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527

  ExpectLength(2, kExprTableGet, U32V_1(1));
  ExpectLength(3, kExprTableGet, U32V_2(33));
  ExpectLength(4, kExprTableGet, U32V_3(44));
  ExpectLength(5, kExprTableGet, U32V_4(66));
  ExpectLength(6, kExprTableGet, U32V_5(77));

  ExpectLength(2, kExprTableSet, U32V_1(1));
  ExpectLength(3, kExprTableSet, U32V_2(33));
  ExpectLength(4, kExprTableSet, U32V_3(44));
  ExpectLength(5, kExprTableSet, U32V_4(66));
  ExpectLength(6, kExprTableSet, U32V_5(77));

  ExpectLength(3, kExprCallIndirect, U32V_1(1), U32V_1(1));
  ExpectLength(4, kExprCallIndirect, U32V_1(1), U32V_2(33));
  ExpectLength(5, kExprCallIndirect, U32V_1(1), U32V_3(44));
  ExpectLength(6, kExprCallIndirect, U32V_1(1), U32V_4(66));
  ExpectLength(7, kExprCallIndirect, U32V_1(1), U32V_5(77));
4528
}
4529 4530

TEST_F(WasmOpcodeLengthTest, LoadsAndStores) {
4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554
  ExpectLength(3, kExprI32LoadMem8S);
  ExpectLength(3, kExprI32LoadMem8U);
  ExpectLength(3, kExprI32LoadMem16S);
  ExpectLength(3, kExprI32LoadMem16U);
  ExpectLength(3, kExprI32LoadMem);
  ExpectLength(3, kExprI64LoadMem8S);
  ExpectLength(3, kExprI64LoadMem8U);
  ExpectLength(3, kExprI64LoadMem16S);
  ExpectLength(3, kExprI64LoadMem16U);
  ExpectLength(3, kExprI64LoadMem32S);
  ExpectLength(3, kExprI64LoadMem32U);
  ExpectLength(3, kExprI64LoadMem);
  ExpectLength(3, kExprF32LoadMem);
  ExpectLength(3, kExprF64LoadMem);

  ExpectLength(3, kExprI32StoreMem8);
  ExpectLength(3, kExprI32StoreMem16);
  ExpectLength(3, kExprI32StoreMem);
  ExpectLength(3, kExprI64StoreMem8);
  ExpectLength(3, kExprI64StoreMem16);
  ExpectLength(3, kExprI64StoreMem32);
  ExpectLength(3, kExprI64StoreMem);
  ExpectLength(3, kExprF32StoreMem);
  ExpectLength(3, kExprF64StoreMem);
4555 4556 4557
}

TEST_F(WasmOpcodeLengthTest, MiscMemExpressions) {
4558 4559
  ExpectLength(2, kExprMemorySize);
  ExpectLength(2, kExprMemoryGrow);
4560 4561 4562
}

TEST_F(WasmOpcodeLengthTest, SimpleExpressions) {
4563 4564 4565 4566 4567 4568 4569
#define SIMPLE_OPCODE(name, byte, sig) byte,
  static constexpr uint8_t kSimpleOpcodes[] = {
      FOREACH_SIMPLE_OPCODE(SIMPLE_OPCODE)};
#undef SIMPLE_OPCODE
  for (uint8_t simple_opcode : kSimpleOpcodes) {
    ExpectLength(1, simple_opcode);
  }
4570 4571
}

4572
TEST_F(WasmOpcodeLengthTest, SimdExpressions) {
4573
#define TEST_SIMD(name, opcode, sig) ExpectLengthPrefixed(0, kExpr##name);
4574 4575
  FOREACH_SIMD_0_OPERAND_OPCODE(TEST_SIMD)
#undef TEST_SIMD
4576
#define TEST_SIMD(name, opcode, sig) ExpectLengthPrefixed(1, kExpr##name);
4577
  FOREACH_SIMD_1_OPERAND_OPCODE(TEST_SIMD)
4578
#undef TEST_SIMD
4579
  ExpectLengthPrefixed(16, kExprI8x16Shuffle);
4580 4581
  // test for bad simd opcode, 0xFF is encoded in two bytes.
  ExpectLength(3, kSimdPrefix, 0xFF, 0x1);
4582 4583
}

4584
TEST_F(WasmOpcodeLengthTest, IllegalRefIndices) {
4585 4586
  ExpectFailure(kExprBlock, kOptRefCode, U32V_3(kV8MaxWasmTypes + 1));
  ExpectFailure(kExprBlock, kOptRefCode, U32V_4(0x01000000));
4587 4588
}

4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603
TEST_F(WasmOpcodeLengthTest, PrefixedOpcodesLEB) {
  // kExprI32New with a 4-byte LEB-encoded opcode.
  ExpectLength(5, 0xfb, 0xa0, 0x80, 0x80, 0x00);

  // kExprI8x16Splat with a 3-byte LEB-encoded opcode.
  ExpectLength(4, 0xfd, 0x8f, 0x80, 0x00);

  // kExprI32SConvertSatF32 with a 4-byte LEB-encoded opcode.
  ExpectLength(5, 0xfc, 0x80, 0x80, 0x80, 0x00);

  // kExprAtomicNotify with a 2-byte LEB-encoded opcode, and 2 i32 imm for
  // memarg.
  ExpectLength(5, 0xfe, 0x80, 0x00, 0x00, 0x00);
}

4604
using TypesOfLocals = ZoneVector<ValueType>;
4605 4606 4607

class LocalDeclDecoderTest : public TestWithZone {
 public:
4608
  v8::internal::AccountingAllocator allocator;
4609
  WasmFeatures enabled_features_;
4610

4611
  size_t ExpectRun(TypesOfLocals map, size_t pos, ValueType expected,
4612 4613
                   size_t count) {
    for (size_t i = 0; i < count; i++) {
4614
      EXPECT_EQ(expected, map[pos++]);
4615 4616 4617
    }
    return pos;
  }
4618 4619 4620 4621 4622

  bool DecodeLocalDecls(BodyLocalDecls* decls, const byte* start,
                        const byte* end) {
    return i::wasm::DecodeLocalDecls(enabled_features_, decls, start, end);
  }
4623 4624
};

4625
TEST_F(LocalDeclDecoderTest, EmptyLocals) {
4626
  BodyLocalDecls decls(zone());
4627
  bool result = DecodeLocalDecls(&decls, nullptr, nullptr);
4628 4629 4630
  EXPECT_FALSE(result);
}

4631 4632
TEST_F(LocalDeclDecoderTest, NoLocals) {
  static const byte data[] = {0};
4633
  BodyLocalDecls decls(zone());
4634
  bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
4635
  EXPECT_TRUE(result);
4636
  EXPECT_TRUE(decls.type_list.empty());
4637 4638
}

4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653
TEST_F(LocalDeclDecoderTest, WrongLocalDeclsCount1) {
  static const byte data[] = {1};
  BodyLocalDecls decls(zone());
  bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
  EXPECT_FALSE(result);
}

TEST_F(LocalDeclDecoderTest, WrongLocalDeclsCount2) {
  static const byte data[] = {2, 1,
                              static_cast<byte>(kWasmI32.value_type_code())};
  BodyLocalDecls decls(zone());
  bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
  EXPECT_FALSE(result);
}

4654
TEST_F(LocalDeclDecoderTest, OneLocal) {
4655
  WASM_FEATURE_SCOPE(reftypes);
4656 4657
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType type = kValueTypes[i];
4658
    const byte data[] = {1, 1, static_cast<byte>(type.value_type_code())};
4659
    BodyLocalDecls decls(zone());
4660
    bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
4661
    EXPECT_TRUE(result);
4662
    EXPECT_EQ(1u, decls.type_list.size());
4663

4664
    TypesOfLocals map = decls.type_list;
4665
    EXPECT_EQ(type, map[0]);
4666 4667 4668 4669
  }
}

TEST_F(LocalDeclDecoderTest, FiveLocals) {
4670
  WASM_FEATURE_SCOPE(reftypes);
4671 4672
  for (size_t i = 0; i < arraysize(kValueTypes); i++) {
    ValueType type = kValueTypes[i];
4673
    const byte data[] = {1, 5, static_cast<byte>(type.value_type_code())};
4674
    BodyLocalDecls decls(zone());
4675
    bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
4676
    EXPECT_TRUE(result);
4677 4678
    EXPECT_EQ(sizeof(data), decls.encoded_size);
    EXPECT_EQ(5u, decls.type_list.size());
4679

4680
    TypesOfLocals map = decls.type_list;
4681
    EXPECT_EQ(5u, map.size());
4682 4683 4684 4685 4686 4687 4688 4689 4690
    ExpectRun(map, 0, type, 5);
  }
}

TEST_F(LocalDeclDecoderTest, MixedLocals) {
  for (byte a = 0; a < 3; a++) {
    for (byte b = 0; b < 3; b++) {
      for (byte c = 0; c < 3; c++) {
        for (byte d = 0; d < 3; d++) {
4691 4692
          const byte data[] = {4, a,        kI32Code, b,       kI64Code,
                               c, kF32Code, d,        kF64Code};
4693
          BodyLocalDecls decls(zone());
4694
          bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
4695
          EXPECT_TRUE(result);
4696
          EXPECT_EQ(sizeof(data), decls.encoded_size);
4697
          EXPECT_EQ(static_cast<uint32_t>(a + b + c + d),
4698
                    decls.type_list.size());
4699

4700
          TypesOfLocals map = decls.type_list;
4701 4702

          size_t pos = 0;
4703 4704 4705 4706
          pos = ExpectRun(map, pos, kWasmI32, a);
          pos = ExpectRun(map, pos, kWasmI64, b);
          pos = ExpectRun(map, pos, kWasmF32, c);
          pos = ExpectRun(map, pos, kWasmF64, d);
4707 4708 4709 4710 4711 4712 4713 4714 4715
        }
      }
    }
  }
}

TEST_F(LocalDeclDecoderTest, UseEncoder) {
  const byte* data = nullptr;
  const byte* end = nullptr;
4716
  LocalDeclEncoder local_decls(zone());
4717

4718 4719 4720
  local_decls.AddLocals(5, kWasmF32);
  local_decls.AddLocals(1337, kWasmI32);
  local_decls.AddLocals(212, kWasmI64);
4721
  local_decls.Prepend(zone(), &data, &end);
4722

4723
  BodyLocalDecls decls(zone());
4724
  bool result = DecodeLocalDecls(&decls, data, end);
4725
  EXPECT_TRUE(result);
4726
  EXPECT_EQ(5u + 1337u + 212u, decls.type_list.size());
4727

4728
  TypesOfLocals map = decls.type_list;
4729
  size_t pos = 0;
4730 4731 4732
  pos = ExpectRun(map, pos, kWasmF32, 5);
  pos = ExpectRun(map, pos, kWasmI32, 1337);
  pos = ExpectRun(map, pos, kWasmI64, 212);
4733 4734
}

4735
TEST_F(LocalDeclDecoderTest, ExnRef) {
4736
  WASM_FEATURE_SCOPE(eh);
4737
  ValueType type = kWasmExnRef;
4738
  const byte data[] = {1, 1, static_cast<byte>(type.value_type_code())};
4739 4740 4741 4742 4743 4744 4745 4746 4747
  BodyLocalDecls decls(zone());
  bool result = DecodeLocalDecls(&decls, data, data + sizeof(data));
  EXPECT_TRUE(result);
  EXPECT_EQ(1u, decls.type_list.size());

  TypesOfLocals map = decls.type_list;
  EXPECT_EQ(type, map[0]);
}

4748 4749 4750 4751 4752
class BytecodeIteratorTest : public TestWithZone {};

TEST_F(BytecodeIteratorTest, SimpleForeach) {
  byte code[] = {WASM_IF_ELSE(WASM_ZERO, WASM_ZERO, WASM_ZERO)};
  BytecodeIterator iter(code, code + sizeof(code));
4753 4754
  WasmOpcode expected[] = {kExprI32Const, kExprIf,       kExprI32Const,
                           kExprElse,     kExprI32Const, kExprEnd};
4755
  size_t pos = 0;
4756
  for (WasmOpcode opcode : iter.opcodes()) {
4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
    if (pos >= arraysize(expected)) {
      EXPECT_TRUE(false);
      break;
    }
    EXPECT_EQ(expected[pos++], opcode);
  }
  EXPECT_EQ(arraysize(expected), pos);
}

TEST_F(BytecodeIteratorTest, ForeachTwice) {
  byte code[] = {WASM_IF_ELSE(WASM_ZERO, WASM_ZERO, WASM_ZERO)};
  BytecodeIterator iter(code, code + sizeof(code));
  int count = 0;

  count = 0;
4772
  for (WasmOpcode opcode : iter.opcodes()) {
4773 4774 4775 4776 4777 4778
    USE(opcode);
    count++;
  }
  EXPECT_EQ(6, count);

  count = 0;
4779
  for (WasmOpcode opcode : iter.opcodes()) {
4780 4781 4782 4783 4784 4785
    USE(opcode);
    count++;
  }
  EXPECT_EQ(6, count);
}

4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805
TEST_F(BytecodeIteratorTest, ForeachOffset) {
  byte code[] = {WASM_IF_ELSE(WASM_ZERO, WASM_ZERO, WASM_ZERO)};
  BytecodeIterator iter(code, code + sizeof(code));
  int count = 0;

  count = 0;
  for (auto offset : iter.offsets()) {
    USE(offset);
    count++;
  }
  EXPECT_EQ(6, count);

  count = 0;
  for (auto offset : iter.offsets()) {
    USE(offset);
    count++;
  }
  EXPECT_EQ(6, count);
}

4806
TEST_F(BytecodeIteratorTest, WithLocalDecls) {
4807
  byte code[] = {1, 1, kI32Code, WASM_I32V_1(9), WASM_I32V_1(11)};
4808
  BodyLocalDecls decls(zone());
4809 4810
  BytecodeIterator iter(code, code + sizeof(code), &decls);

4811
  EXPECT_EQ(3u, decls.encoded_size);
4812
  EXPECT_EQ(3u, iter.pc_offset());
4813
  EXPECT_TRUE(iter.has_next());
4814
  EXPECT_EQ(kExprI32Const, iter.current());
4815 4816
  iter.next();
  EXPECT_TRUE(iter.has_next());
4817
  EXPECT_EQ(kExprI32Const, iter.current());
4818 4819 4820
  iter.next();
  EXPECT_FALSE(iter.has_next());
}
4821

4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
/*******************************************************************************
 * Memory64 tests
 ******************************************************************************/

TEST_F(FunctionBodyDecoderTest, IndexTypesOn32BitMemory) {
  builder.InitializeMemory(kMemory32);
  ExpectValidates(sigs.i_v(), {WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)});
  ExpectFailure(sigs.i_v(), {WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO64)});
  ExpectValidates(sigs.v_v(),
                  {WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, WASM_ZERO)});
  ExpectFailure(sigs.v_v(),
                {WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO64, WASM_ZERO)});
}

TEST_F(FunctionBodyDecoderTest, IndexTypesOn64BitMemory) {
  builder.InitializeMemory(kMemory64);
  ExpectFailure(sigs.i_v(), {WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)});
  ExpectValidates(sigs.i_v(),
                  {WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO64)});
  ExpectFailure(sigs.v_v(),
                {WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, WASM_ZERO)});
  ExpectValidates(sigs.v_v(), {WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO64,
                                              WASM_ZERO)});
}

4847 4848 4849 4850 4851 4852
#undef B1
#undef B2
#undef B3
#undef WASM_IF_OP
#undef WASM_LOOP_OP
#undef WASM_BRV_IF_ZERO
4853
#undef EXPECT_OK
4854

4855
}  // namespace function_body_decoder_unittest
4856 4857 4858
}  // namespace wasm
}  // namespace internal
}  // namespace v8