test-gc.cc 109 KB
Newer Older
1 2 3 4 5 6
// Copyright 2020 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.

#include <stdint.h>

7
#include "src/base/vector.h"
8
#include "src/codegen/signature.h"
9 10 11
#include "src/utils/utils.h"
#include "src/wasm/module-decoder.h"
#include "src/wasm/struct-types.h"
12
#include "src/wasm/wasm-arguments.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-module-builder.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-objects-inl.h"
#include "src/wasm/wasm-opcodes.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/value-helper.h"
#include "test/cctest/wasm/wasm-run-utils.h"
#include "test/common/wasm/test-signatures.h"
#include "test/common/wasm/wasm-macro-gen.h"
#include "test/common/wasm/wasm-module-runner.h"

namespace v8 {
namespace internal {
namespace wasm {
namespace test_gc {

30 31 32 33
using F = std::pair<ValueType, bool>;

class WasmGCTester {
 public:
34 35
  explicit WasmGCTester(
      TestExecutionTier execution_tier = TestExecutionTier::kTurbofan)
36 37 38
      : flag_gc(&v8::internal::FLAG_experimental_wasm_gc, true),
        flag_typedfuns(&v8::internal::FLAG_experimental_wasm_typed_funcref,
                       true),
39 40 41 42
        flag_liftoff(&v8::internal::FLAG_liftoff,
                     execution_tier == TestExecutionTier::kLiftoff),
        flag_liftoff_only(&v8::internal::FLAG_liftoff_only,
                          execution_tier == TestExecutionTier::kLiftoff),
43 44
        flag_wasm_dynamic_tiering(&v8::internal::FLAG_wasm_dynamic_tiering,
                                  v8::internal::FLAG_liftoff_only != true),
45 46 47
        // Test both setups with canonicalization and without.
        flag_canonicalization(&v8::internal::FLAG_wasm_type_canonicalization,
                              execution_tier == TestExecutionTier::kTurbofan),
48
        flag_tierup(&v8::internal::FLAG_wasm_tier_up, false),
49 50
        zone_(&allocator, ZONE_NAME),
        builder_(&zone_),
51 52 53 54
        isolate_(CcTest::InitIsolateOnce()),
        scope(isolate_),
        thrower(isolate_, "Test wasm GC") {
    testing::SetupIsolateForWasmModule(isolate_);
55 56
  }

57
  byte AddGlobal(ValueType type, bool mutability, WasmInitExpr init) {
58
    return builder_.AddGlobal(type, mutability, init);
59 60
  }

61 62
  byte DefineFunction(FunctionSig* sig, std::initializer_list<ValueType> locals,
                      std::initializer_list<byte> code) {
63 64 65 66 67 68 69
    return DefineFunctionImpl(builder_.AddFunction(sig), locals, code);
  }

  byte DefineFunction(uint32_t sig_index,
                      std::initializer_list<ValueType> locals,
                      std::initializer_list<byte> code) {
    return DefineFunctionImpl(builder_.AddFunction(sig_index), locals, code);
70 71
  }

72 73 74 75
  void DefineExportedFunction(const char* name, FunctionSig* sig,
                              std::initializer_list<byte> code) {
    WasmFunctionBuilder* fun = builder_.AddFunction(sig);
    fun->EmitCode(code.begin(), static_cast<uint32_t>(code.size()));
76
    builder_.AddExport(base::CStrVector(name), fun);
77 78 79 80 81 82 83 84 85 86 87
  }

  MaybeHandle<Object> CallExportedFunction(const char* name, int argc,
                                           Handle<Object> args[]) {
    Handle<WasmExportedFunction> func =
        testing::GetExportedFunction(isolate_, instance_, name)
            .ToHandleChecked();
    return Execution::Call(isolate_, func,
                           isolate_->factory()->undefined_value(), argc, args);
  }

88 89
  byte DefineStruct(std::initializer_list<F> fields,
                    uint32_t supertype = kNoSuperType) {
90
    StructType::Builder type_builder(&zone_,
91 92 93 94
                                     static_cast<uint32_t>(fields.size()));
    for (F field : fields) {
      type_builder.AddField(field.first, field.second);
    }
95
    return builder_.AddStructType(type_builder.Build(), supertype);
96 97
  }

98 99 100 101
  byte DefineArray(ValueType element_type, bool mutability,
                   uint32_t supertype = kNoSuperType) {
    return builder_.AddArrayType(zone_.New<ArrayType>(element_type, mutability),
                                 supertype);
102 103
  }

104 105 106
  byte DefineSignature(FunctionSig* sig, uint32_t supertype = kNoSuperType) {
    return builder_.AddSignature(sig, supertype);
  }
107

108 109 110 111
  byte DefineTable(ValueType type, uint32_t min_size, uint32_t max_size) {
    return builder_.AddTable(type, min_size, max_size);
  }

112
  void CompileModule() {
113
    ZoneBuffer buffer(&zone_);
114
    builder_.WriteTo(&buffer);
115 116
    MaybeHandle<WasmInstanceObject> maybe_instance =
        testing::CompileAndInstantiateForTesting(
117
            isolate_, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()));
118
    if (thrower.error()) FATAL("%s", thrower.error_msg());
119
    instance_ = maybe_instance.ToHandleChecked();
120 121
  }

122
  void CheckResult(uint32_t function_index, int32_t expected) {
123
    const FunctionSig* sig = sigs.i_v();
124 125
    DCHECK(*sig == *instance_->module()->functions[function_index].sig);
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
126
    CheckResultImpl(function_index, sig, &packer, expected);
127 128 129
  }

  void CheckResult(uint32_t function_index, int32_t expected, int32_t arg) {
130
    const FunctionSig* sig = sigs.i_i();
131 132 133
    DCHECK(*sig == *instance_->module()->functions[function_index].sig);
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    packer.Push(arg);
134
    CheckResultImpl(function_index, sig, &packer, expected);
135 136
  }

137 138
  MaybeHandle<Object> GetResultObject(uint32_t function_index) {
    const FunctionSig* sig = instance_->module()->functions[function_index].sig;
139 140
    DCHECK_EQ(sig->parameter_count(), 0);
    DCHECK_EQ(sig->return_count(), 1);
141 142
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    CallFunctionImpl(function_index, sig, &packer);
143
    CHECK(!isolate_->has_pending_exception());
144 145 146 147
    packer.Reset();
    return Handle<Object>(Object(packer.Pop<Address>()), isolate_);
  }

148 149 150
  MaybeHandle<Object> GetResultObject(uint32_t function_index, int32_t arg) {
    const FunctionSig* sig = instance_->module()->functions[function_index].sig;
    DCHECK_EQ(sig->parameter_count(), 1);
151
    DCHECK_EQ(sig->return_count(), 1);
152 153 154 155 156 157 158 159 160
    DCHECK(sig->parameters()[0] == kWasmI32);
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    packer.Push(arg);
    CallFunctionImpl(function_index, sig, &packer);
    CHECK(!isolate_->has_pending_exception());
    packer.Reset();
    return Handle<Object>(Object(packer.Pop<Address>()), isolate_);
  }

161
  void CheckHasThrown(uint32_t function_index, const char* expected = "") {
162
    const FunctionSig* sig = instance_->module()->functions[function_index].sig;
163
    DCHECK_EQ(sig->parameter_count(), 0);
164
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
165
    CheckHasThrownImpl(function_index, sig, &packer, expected);
166 167
  }

168 169 170 171 172
  void CheckHasThrown(uint32_t function_index, int32_t arg,
                      const char* expected = "") {
    const FunctionSig* sig = instance_->module()->functions[function_index].sig;
    DCHECK_EQ(sig->parameter_count(), 1);
    DCHECK(sig->parameters()[0] == kWasmI32);
173 174
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    packer.Push(arg);
175
    CheckHasThrownImpl(function_index, sig, &packer, expected);
176 177
  }

178 179 180 181 182 183 184 185 186 187 188 189 190
  bool HasSimdSupport(TestExecutionTier tier) const {
#if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_IA32
    // Liftoff does not have a fallback for executing SIMD instructions if
    // SSE4_1 is not available.
    if (tier == TestExecutionTier::kLiftoff &&
        !CpuFeatures::IsSupported(SSE4_1)) {
      return false;
    }
#endif
    USE(tier);
    return true;
  }

191
  Handle<WasmInstanceObject> instance() { return instance_; }
192
  Isolate* isolate() { return isolate_; }
193
  WasmModuleBuilder* builder() { return &builder_; }
194
  Zone* zone() { return &zone_; }
195

196 197
  TestSignatures sigs;

198 199 200
 private:
  const FlagScope<bool> flag_gc;
  const FlagScope<bool> flag_typedfuns;
201 202
  const FlagScope<bool> flag_liftoff;
  const FlagScope<bool> flag_liftoff_only;
203
  const FlagScope<bool> flag_wasm_dynamic_tiering;
204
  const FlagScope<bool> flag_canonicalization;
205
  const FlagScope<bool> flag_tierup;
206

207 208 209 210 211 212 213 214 215 216
  byte DefineFunctionImpl(WasmFunctionBuilder* fun,
                          std::initializer_list<ValueType> locals,
                          std::initializer_list<byte> code) {
    for (ValueType local : locals) {
      fun->AddLocal(local);
    }
    fun->EmitCode(code.begin(), static_cast<uint32_t>(code.size()));
    return fun->func_index();
  }

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  void CheckResultImpl(uint32_t function_index, const FunctionSig* sig,
                       CWasmArgumentsPacker* packer, int32_t expected) {
    CallFunctionImpl(function_index, sig, packer);
    if (isolate_->has_pending_exception()) {
      Handle<String> message =
          ErrorUtils::ToString(isolate_,
                               handle(isolate_->pending_exception(), isolate_))
              .ToHandleChecked();
      FATAL("%s", message->ToCString().get());
    }
    packer->Reset();
    CHECK_EQ(expected, packer->Pop<int32_t>());
  }

  void CheckHasThrownImpl(uint32_t function_index, const FunctionSig* sig,
                          CWasmArgumentsPacker* packer, const char* expected) {
    CallFunctionImpl(function_index, sig, packer);
    CHECK(isolate_->has_pending_exception());
    Handle<String> message =
        ErrorUtils::ToString(isolate_,
                             handle(isolate_->pending_exception(), isolate_))
            .ToHandleChecked();
    std::string message_str(message->ToCString().get());
    CHECK_NE(message_str.find(expected), std::string::npos);
    isolate_->clear_pending_exception();
  }

  void CallFunctionImpl(uint32_t function_index, const FunctionSig* sig,
                        CWasmArgumentsPacker* packer) {
246
    WasmCodeRefScope code_ref_scope;
247 248 249 250
    NativeModule* native_module = instance_->module_object().native_module();
    WasmCode* code = native_module->GetCode(function_index);
    Address wasm_call_target = code->instruction_start();
    Handle<Object> object_ref = instance_;
251
    Handle<CodeT> c_wasm_entry =
252 253 254 255 256
        compiler::CompileCWasmEntry(isolate_, sig, native_module->module());
    Execution::CallWasm(isolate_, c_wasm_entry, wasm_call_target, object_ref,
                        packer->argv());
  }

257
  v8::internal::AccountingAllocator allocator;
258
  Zone zone_;
259
  WasmModuleBuilder builder_;
260

261
  Isolate* const isolate_;
262
  const HandleScope scope;
263
  Handle<WasmInstanceObject> instance_;
264 265 266
  ErrorThrower thrower;
};

267
ValueType ref(uint32_t type_index) {
268
  return ValueType::Ref(type_index, kNonNullable);
269 270
}
ValueType optref(uint32_t type_index) {
271
  return ValueType::Ref(type_index, kNullable);
272 273
}

274 275 276
WASM_COMPILED_EXEC_TEST(WasmBasicStruct) {
  WasmGCTester tester(execution_tier);

277
  const byte type_index =
278
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
279 280 281 282 283 284
  const byte empty_struct_index = tester.DefineStruct({});
  ValueType kRefType = ref(type_index);
  ValueType kEmptyStructType = ref(empty_struct_index);
  ValueType kOptRefType = optref(type_index);
  FunctionSig sig_q_v(1, 0, &kRefType);
  FunctionSig sig_qe_v(1, 0, &kEmptyStructType);
285

286
  // Test struct.new and struct.get.
287
  const byte kGet1 = tester.DefineFunction(
288
      tester.sigs.i_v(), {},
289
      {WASM_STRUCT_GET(
290 291 292
           type_index, 0,
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                    WASM_RTT_CANON(type_index))),
293
       kExprEnd});
294

295
  // Test struct.new and struct.get.
296
  const byte kGet2 = tester.DefineFunction(
297
      tester.sigs.i_v(), {},
298
      {WASM_STRUCT_GET(
299 300 301
           type_index, 1,
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                    WASM_RTT_CANON(type_index))),
302
       kExprEnd});
303

304
  // Test struct.new, returning struct reference.
305
  const byte kGetStruct = tester.DefineFunction(
306 307 308
      &sig_q_v, {},
      {WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                WASM_RTT_CANON(type_index)),
309
       kExprEnd});
310

311
  const byte kGetStructNominal = tester.DefineFunction(
312 313 314
      &sig_q_v, {},
      {WASM_STRUCT_NEW_DEFAULT(type_index), WASM_DROP,
       WASM_STRUCT_NEW(type_index, WASM_I32V(42), WASM_I32V(64)), kExprEnd});
315

316 317
  // Test struct.new, returning reference to an empty struct.
  const byte kGetEmptyStruct = tester.DefineFunction(
318 319 320
      &sig_qe_v, {},
      {WASM_STRUCT_NEW_WITH_RTT(empty_struct_index,
                                WASM_RTT_CANON(empty_struct_index)),
321 322
       kExprEnd});

323
  // Test struct.set, struct refs types in locals.
324 325 326
  const byte j_local_index = 0;
  const byte j_field_index = 0;
  const byte kSet = tester.DefineFunction(
327
      tester.sigs.i_v(), {kOptRefType},
328
      {WASM_LOCAL_SET(
329
           j_local_index,
330 331 332 333 334
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                    WASM_RTT_CANON(type_index))),
       WASM_STRUCT_SET(type_index, j_field_index, WASM_LOCAL_GET(j_local_index),
                       WASM_I32V(-99)),
       WASM_STRUCT_GET(type_index, j_field_index,
335
                       WASM_LOCAL_GET(j_local_index)),
336
       kExprEnd});
337

338 339
  tester.CompileModule();

340 341 342
  tester.CheckResult(kGet1, 42);
  tester.CheckResult(kGet2, 64);
  CHECK(tester.GetResultObject(kGetStruct).ToHandleChecked()->IsWasmStruct());
343 344 345
  CHECK(tester.GetResultObject(kGetStructNominal)
            .ToHandleChecked()
            ->IsWasmStruct());
346 347 348
  CHECK(tester.GetResultObject(kGetEmptyStruct)
            .ToHandleChecked()
            ->IsWasmStruct());
349
  tester.CheckResult(kSet, -99);
350 351
}

352
// Test struct.get, ref.as_non_null and ref-typed globals.
353 354
WASM_COMPILED_EXEC_TEST(WasmRefAsNonNull) {
  WasmGCTester tester(execution_tier);
355
  const byte type_index =
356 357 358 359 360
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
  ValueType kRefTypes[] = {ref(type_index)};
  ValueType kOptRefType = optref(type_index);
  FunctionSig sig_q_v(1, 0, kRefTypes);

361
  const byte global_index =
362 363 364
      tester.AddGlobal(kOptRefType, true,
                       WasmInitExpr::RefNullConst(
                           static_cast<HeapType::Representation>(type_index)));
365
  const byte field_index = 0;
366
  const byte kNonNull = tester.DefineFunction(
367
      tester.sigs.i_v(), {},
368
      {WASM_GLOBAL_SET(
369 370 371
           global_index,
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(55), WASM_I32V(66),
                                    WASM_RTT_CANON(type_index))),
372 373 374 375 376 377 378 379
       WASM_STRUCT_GET(type_index, field_index,
                       WASM_REF_AS_NON_NULL(WASM_GLOBAL_GET(global_index))),
       kExprEnd});
  const byte kNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_GLOBAL_SET(global_index, WASM_REF_NULL(type_index)),
       WASM_STRUCT_GET(type_index, field_index,
                       WASM_REF_AS_NON_NULL(WASM_GLOBAL_GET(global_index))),
380
       kExprEnd});
381

382
  tester.CompileModule();
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  tester.CheckResult(kNonNull, 55);
  tester.CheckHasThrown(kNull);
}

WASM_COMPILED_EXEC_TEST(WasmRefAsNonNullSkipCheck) {
  FlagScope<bool> no_check(&FLAG_experimental_wasm_skip_null_checks, true);
  WasmGCTester tester(execution_tier);
  const byte type_index =
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
  ValueType kRefType = ref(type_index);
  FunctionSig sig_q_v(1, 0, &kRefType);

  const byte global_index =
      tester.AddGlobal(optref(type_index), true,
                       WasmInitExpr::RefNullConst(
                           static_cast<HeapType::Representation>(type_index)));
  const byte kFunc = tester.DefineFunction(
      &sig_q_v, {},
      {WASM_GLOBAL_SET(global_index, WASM_REF_NULL(type_index)),
       WASM_REF_AS_NON_NULL(WASM_GLOBAL_GET(global_index)), kExprEnd});

  tester.CompileModule();
  Handle<Object> result = tester.GetResultObject(kFunc).ToHandleChecked();
  // Without null checks, ref.as_non_null can actually return null.
  CHECK(result->IsNull());
408 409
}

410 411
WASM_COMPILED_EXEC_TEST(WasmBrOnNull) {
  WasmGCTester tester(execution_tier);
412
  const byte type_index =
413 414 415 416
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
  ValueType kRefTypes[] = {ref(type_index)};
  ValueType kOptRefType = optref(type_index);
  FunctionSig sig_q_v(1, 0, kRefTypes);
417
  const byte local_index = 0;
418
  const byte kTaken = tester.DefineFunction(
419
      tester.sigs.i_v(), {kOptRefType},
420 421 422
      {WASM_BLOCK_I(WASM_I32V(42),
                    // Branch will be taken.
                    // 42 left on stack outside the block (not 52).
423
                    WASM_BR_ON_NULL(0, WASM_LOCAL_GET(local_index)),
424 425
                    WASM_I32V(52), WASM_BR(0)),
       kExprEnd});
426

427
  const byte field_index = 0;
428
  const byte kNotTaken = tester.DefineFunction(
429
      tester.sigs.i_v(), {},
430 431 432
      {WASM_BLOCK_I(
           WASM_I32V(42),
           WASM_STRUCT_GET(
433
               type_index, field_index,
434 435
               // Branch will not be taken.
               // 52 left on stack outside the block (not 42).
436 437 438
               WASM_BR_ON_NULL(0, WASM_STRUCT_NEW_WITH_RTT(
                                      type_index, WASM_I32V(52), WASM_I32V(62),
                                      WASM_RTT_CANON(type_index)))),
439 440
           WASM_BR(0)),
       kExprEnd});
441

442
  tester.CompileModule();
443 444
  tester.CheckResult(kTaken, 42);
  tester.CheckResult(kNotTaken, 52);
445 446
}

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
WASM_COMPILED_EXEC_TEST(WasmBrOnNonNull) {
  WasmGCTester tester(execution_tier);
  const byte type_index =
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
  ValueType kRefType = ref(type_index);
  ValueType kOptRefType = optref(type_index);
  FunctionSig sig_q_v(1, 0, &kRefType);
  const byte field_index = 0;

  const byte kTaken = tester.DefineFunction(
      tester.sigs.i_v(), {kOptRefType, kOptRefType},
      {WASM_LOCAL_SET(
           0, WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(52), WASM_I32V(62),
                                       WASM_RTT_CANON(type_index))),
       WASM_LOCAL_SET(
           1, WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(11), WASM_I32V(22),
                                       WASM_RTT_CANON(type_index))),
       WASM_STRUCT_GET(type_index, field_index,
                       WASM_BLOCK_R(ref(type_index),
                                    // Branch will be taken, and the block will
                                    // return struct(52, 62).
                                    WASM_BR_ON_NON_NULL(0, WASM_LOCAL_GET(0)),
                                    WASM_REF_AS_NON_NULL(WASM_LOCAL_GET(1)))),
       kExprEnd});

  const byte kNotTaken = tester.DefineFunction(
      tester.sigs.i_v(), {kOptRefType, kOptRefType},
      {WASM_LOCAL_SET(0, WASM_REF_NULL(type_index)),
       WASM_LOCAL_SET(
           1, WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(11), WASM_I32V(22),
                                       WASM_RTT_CANON(type_index))),
       WASM_STRUCT_GET(type_index, field_index,
                       WASM_BLOCK_R(ref(type_index),
                                    // Branch will not be taken, and the block
                                    // will return struct(11, 22).
                                    WASM_BR_ON_NON_NULL(0, WASM_LOCAL_GET(0)),
                                    WASM_REF_AS_NON_NULL(WASM_LOCAL_GET(1)))),
       kExprEnd});
  tester.CompileModule();
  tester.CheckResult(kTaken, 52);
  tester.CheckResult(kNotTaken, 11);
}

490 491 492 493
WASM_COMPILED_EXEC_TEST(RefCast) {
  WasmGCTester tester(execution_tier);

  const byte supertype_index = tester.DefineStruct({F(kWasmI32, true)});
494 495 496 497
  const byte subtype1_index = tester.DefineStruct(
      {F(kWasmI32, true), F(kWasmF32, false)}, supertype_index);
  const byte subtype2_index = tester.DefineStruct(
      {F(kWasmI32, true), F(kWasmI64, false)}, supertype_index);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
  auto super_sig = FixedSizeSignature<ValueType>::Params(
                       ValueType::Ref(subtype1_index, kNullable))
                       .Returns(ValueType::Ref(supertype_index, kNullable));
  auto sub_sig1 = FixedSizeSignature<ValueType>::Params(
                      ValueType::Ref(supertype_index, kNullable))
                      .Returns(ValueType::Ref(subtype1_index, kNullable));
  auto sub_sig2 = FixedSizeSignature<ValueType>::Params(
                      ValueType::Ref(supertype_index, kNullable))
                      .Returns(ValueType::Ref(subtype2_index, kNullable));
  const byte function_type_index = tester.DefineSignature(&super_sig);
  const byte function_subtype1_index =
      tester.DefineSignature(&sub_sig1, function_type_index);
  const byte function_subtype2_index =
      tester.DefineSignature(&sub_sig2, function_type_index);
  const byte function_index = tester.DefineFunction(
      function_subtype1_index, {},
      {WASM_STRUCT_NEW_DEFAULT_WITH_RTT(subtype1_index,
                                        WASM_RTT_CANON(subtype1_index)),
       WASM_END});
  // Just so this function counts as "declared".
  tester.AddGlobal(ValueType::Ref(function_type_index, kNullable), false,
                   WasmInitExpr::RefFuncConst(function_index));
520 521 522 523

  const byte kTestSuccessful = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(supertype_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
524
                             subtype1_index, WASM_RTT_CANON(subtype1_index))),
525 526
       WASM_STRUCT_GET(
           subtype1_index, 0,
527
           WASM_REF_CAST(WASM_LOCAL_GET(0), WASM_RTT_CANON(subtype1_index))),
528 529 530 531 532
       WASM_END});

  const byte kTestFailed = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(supertype_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
533
                             subtype1_index, WASM_RTT_CANON(subtype1_index))),
534 535
       WASM_STRUCT_GET(
           subtype2_index, 0,
536
           WASM_REF_CAST(WASM_LOCAL_GET(0), WASM_RTT_CANON(subtype2_index))),
537 538
       WASM_END});

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  const byte kFuncTestSuccessfulSuper = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(function_type_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(function_index)),
       WASM_REF_CAST(WASM_LOCAL_GET(0), WASM_RTT_CANON(function_type_index)),
       WASM_DROP, WASM_I32V(0), WASM_END});

  const byte kFuncTestSuccessfulSub = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(function_type_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(function_index)),
       WASM_REF_CAST(WASM_LOCAL_GET(0),
                     WASM_RTT_CANON(function_subtype1_index)),
       WASM_DROP, WASM_I32V(0), WASM_END});

  const byte kFuncTestFailed = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(function_type_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(function_index)),
       WASM_REF_CAST(WASM_LOCAL_GET(0),
                     WASM_RTT_CANON(function_subtype2_index)),
       WASM_DROP, WASM_I32V(1), WASM_END});

559 560 561
  tester.CompileModule();
  tester.CheckResult(kTestSuccessful, 0);
  tester.CheckHasThrown(kTestFailed);
562 563 564
  tester.CheckResult(kFuncTestSuccessfulSuper, 0);
  tester.CheckResult(kFuncTestSuccessfulSub, 0);
  tester.CheckHasThrown(kFuncTestFailed);
565 566 567 568 569
}

WASM_COMPILED_EXEC_TEST(RefCastStatic) {
  WasmGCTester tester(execution_tier);

570
  const byte supertype_index = tester.DefineStruct({F(kWasmI32, true)});
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  const byte subtype1_index = tester.DefineStruct(
      {F(kWasmI32, true), F(kWasmF32, false)}, supertype_index);
  const byte subtype2_index = tester.DefineStruct(
      {F(kWasmI32, true), F(kWasmI64, false)}, supertype_index);

  const byte kTestSuccessful = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(supertype_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype1_index)),
       WASM_STRUCT_GET(subtype1_index, 0,
                       WASM_REF_CAST_STATIC(WASM_LOCAL_GET(0), subtype1_index)),
       WASM_END});

  const byte kTestFailed = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(supertype_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype1_index)),
       WASM_STRUCT_GET(subtype2_index, 0,
                       WASM_REF_CAST_STATIC(WASM_LOCAL_GET(0), subtype2_index)),
       WASM_END});

  tester.CompileModule();
  tester.CheckResult(kTestSuccessful, 0);
  tester.CheckHasThrown(kTestFailed);
}

WASM_COMPILED_EXEC_TEST(RefCastStaticNoChecks) {
  FlagScope<bool> scope(&FLAG_experimental_wasm_assume_ref_cast_succeeds, true);
  WasmGCTester tester(execution_tier);

599
  const byte supertype_index = tester.DefineStruct({F(kWasmI32, true)});
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  const byte subtype1_index = tester.DefineStruct(
      {F(kWasmI32, true), F(kWasmF32, false)}, supertype_index);
  const byte subtype2_index = tester.DefineStruct(
      {F(kWasmI32, true), F(kWasmI64, false)}, supertype_index);

  const byte kTestSuccessful = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(supertype_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype1_index)),
       WASM_STRUCT_GET(subtype1_index, 0,
                       WASM_REF_CAST_STATIC(WASM_LOCAL_GET(0), subtype1_index)),
       WASM_END});

  const byte kTestFailed = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(supertype_index, kNullable)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype1_index)),
       WASM_STRUCT_GET(subtype2_index, 0,
                       WASM_REF_CAST_STATIC(WASM_LOCAL_GET(0), subtype2_index)),
       WASM_END});

  tester.CompileModule();
  tester.CheckResult(kTestSuccessful, 0);
  tester.CheckResult(kTestFailed, 0);
}

624 625
WASM_COMPILED_EXEC_TEST(BrOnCast) {
  WasmGCTester tester(execution_tier);
626
  ValueType kDataRefNull = ValueType::Ref(HeapType::kData, kNullable);
627
  const byte type_index = tester.DefineStruct({F(kWasmI32, true)});
628
  const byte other_type_index = tester.DefineStruct({F(kWasmF32, true)});
629
  const byte rtt_index =
630
      tester.AddGlobal(ValueType::Rtt(type_index), false,
631 632
                       WasmInitExpr::RttCanon(
                           static_cast<HeapType::Representation>(type_index)));
633
  const byte kTestStruct = tester.DefineFunction(
634
      tester.sigs.i_v(), {kWasmI32, kDataRefNull},
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
      {WASM_BLOCK_R(ValueType::Ref(type_index, kNullable),
                    WASM_LOCAL_SET(0, WASM_I32V(111)),
                    // Pipe a struct through a local so it's statically typed
                    // as dataref.
                    WASM_LOCAL_SET(1, WASM_STRUCT_NEW_WITH_RTT(
                                          other_type_index, WASM_F32(1.0),
                                          WASM_RTT_CANON(other_type_index))),
                    WASM_LOCAL_GET(1),
                    // The type check fails, so this branch isn't taken.
                    WASM_BR_ON_CAST(0, WASM_GLOBAL_GET(rtt_index)), WASM_DROP,

                    WASM_LOCAL_SET(0, WASM_I32V(221)),  // (Final result) - 1
                    WASM_LOCAL_SET(1, WASM_STRUCT_NEW_WITH_RTT(
                                          type_index, WASM_I32V(1),
                                          WASM_GLOBAL_GET(rtt_index))),
                    WASM_LOCAL_GET(1),
                    // This branch is taken.
                    WASM_BR_ON_CAST(0, WASM_GLOBAL_GET(rtt_index)),
                    WASM_GLOBAL_GET(rtt_index), WASM_GC_OP(kExprRefCast),

                    // Not executed due to the branch.
                    WASM_LOCAL_SET(0, WASM_I32V(333))),
       WASM_GC_OP(kExprStructGet), type_index, 0, WASM_LOCAL_GET(0),
       kExprI32Add, kExprEnd});
659

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  const byte kTestStructStatic = tester.DefineFunction(
      tester.sigs.i_v(), {kWasmI32, kDataRefNull},
      {WASM_BLOCK_R(
           ValueType::Ref(type_index, kNullable),
           WASM_LOCAL_SET(0, WASM_I32V(111)),
           // Pipe a struct through a local so it's statically typed
           // as dataref.
           WASM_LOCAL_SET(1, WASM_STRUCT_NEW(other_type_index, WASM_F32(1.0))),
           WASM_LOCAL_GET(1),
           // The type check fails, so this branch isn't taken.
           WASM_BR_ON_CAST_STATIC(0, type_index), WASM_DROP,

           WASM_LOCAL_SET(0, WASM_I32V(221)),  // (Final result) - 1
           WASM_LOCAL_SET(1, WASM_STRUCT_NEW(type_index, WASM_I32V(1))),
           WASM_LOCAL_GET(1),
           // This branch is taken.
           WASM_BR_ON_CAST_STATIC(0, type_index),
           WASM_GC_OP(kExprRefCastStatic), type_index,

           // Not executed due to the branch.
           WASM_LOCAL_SET(0, WASM_I32V(333))),
       WASM_GC_OP(kExprStructGet), type_index, 0, WASM_LOCAL_GET(0),
       kExprI32Add, kExprEnd});

684
  const byte kTestNull = tester.DefineFunction(
685
      tester.sigs.i_v(), {kWasmI32, kDataRefNull},
686 687 688 689 690 691 692 693 694
      {WASM_BLOCK_R(ValueType::Ref(type_index, kNullable),
                    WASM_LOCAL_SET(0, WASM_I32V(111)),
                    WASM_LOCAL_GET(1),  // Put a nullref onto the value stack.
                    // Not taken for nullref.
                    WASM_BR_ON_CAST(0, WASM_GLOBAL_GET(rtt_index)),
                    WASM_RTT_CANON(type_index), WASM_GC_OP(kExprRefCast),

                    WASM_LOCAL_SET(0, WASM_I32V(222))),  // Final result.
       WASM_DROP, WASM_LOCAL_GET(0), kExprEnd});
695

696
  const byte kTypedAfterBranch = tester.DefineFunction(
697
      tester.sigs.i_v(), {kWasmI32, kDataRefNull},
698
      {WASM_LOCAL_SET(1, WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42),
699
                                                  WASM_GLOBAL_GET(rtt_index))),
700 701 702 703 704 705 706 707
       WASM_BLOCK_I(
           // The inner block should take the early branch with a struct
           // on the stack.
           WASM_BLOCK_R(ValueType::Ref(type_index, kNonNullable),
                        WASM_LOCAL_GET(1),
                        WASM_BR_ON_CAST(0, WASM_GLOBAL_GET(rtt_index)),
                        // Returning 123 is the unreachable failure case.
                        WASM_I32V(123), WASM_BR(1)),
708 709
           // The outer block catches the struct left behind by the inner block
           // and reads its field.
710 711
           WASM_GC_OP(kExprStructGet), type_index, 0),
       kExprEnd});
712 713 714

  tester.CompileModule();
  tester.CheckResult(kTestStruct, 222);
715
  tester.CheckResult(kTestStructStatic, 222);
716
  tester.CheckResult(kTestNull, 222);
717 718 719
  tester.CheckResult(kTypedAfterBranch, 42);
}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
WASM_COMPILED_EXEC_TEST(BrOnCastFail) {
  WasmGCTester tester(execution_tier);
  ValueType kDataRefNull = ValueType::Ref(HeapType::kData, kNullable);
  const byte type0 = tester.DefineStruct({F(kWasmI32, true)});
  const byte type1 =
      tester.DefineStruct({F(kWasmI64, true), F(kWasmI32, true)});

  const int field0_value = 5;
  const int field1_value = 25;
  const int null_value = 45;

  //  local_0 = value;
  //  if (!(local_0 instanceof type0)) goto block1;
  //  return static_cast<type0>(local_0).field_0;
  // block1:
  //  if (local_0 == nullptr) goto block2;
  //  return static_cast<type1>(local_0).field_1;
  // block2:
  //  return null_value;
#define FUNCTION_BODY(value)                                                 \
  WASM_LOCAL_SET(0, WASM_SEQ(value)),                                        \
      WASM_BLOCK(                                                            \
          WASM_BLOCK_R(kDataRefNull, WASM_LOCAL_GET(0),                      \
                       WASM_BR_ON_CAST_FAIL(0, WASM_RTT_CANON(type0)),       \
                       WASM_GC_OP(kExprStructGet), type0, 0, kExprReturn),   \
          kExprBrOnNull, 0, WASM_RTT_CANON(type1), WASM_GC_OP(kExprRefCast), \
          WASM_GC_OP(kExprStructGet), type1, 1, kExprReturn),                \
      WASM_I32V(null_value), kExprEnd

  const byte kBranchTaken =
      tester.DefineFunction(tester.sigs.i_v(), {kDataRefNull},
                            {FUNCTION_BODY(WASM_STRUCT_NEW_WITH_RTT(
                                type1, WASM_I64V(10), WASM_I32V(field1_value),
                                WASM_RTT_CANON(type1)))});

  const byte kBranchNotTaken = tester.DefineFunction(
      tester.sigs.i_v(), {kDataRefNull},
      {FUNCTION_BODY(WASM_STRUCT_NEW_WITH_RTT(type0, WASM_I32V(field0_value),
                                              WASM_RTT_CANON(type0)))});

  const byte kNull = tester.DefineFunction(
      tester.sigs.i_v(), {kDataRefNull}, {FUNCTION_BODY(WASM_REF_NULL(type0))});

  const byte kUnrelatedTypes = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(type1, kNullable)},
      {FUNCTION_BODY(WASM_STRUCT_NEW_WITH_RTT(type1, WASM_I64V(10),
                                              WASM_I32V(field1_value),
                                              WASM_RTT_CANON(type1)))});
#undef FUNCTION_BODY

770 771 772 773 774 775 776 777 778 779 780 781
  const byte kBranchTakenStatic = tester.DefineFunction(
      tester.sigs.i_v(), {kDataRefNull},
      {WASM_LOCAL_SET(
           0, WASM_STRUCT_NEW(type1, WASM_I64V(10), WASM_I32V(field1_value))),
       WASM_BLOCK(
           WASM_BLOCK_R(kDataRefNull, WASM_LOCAL_GET(0),
                        WASM_BR_ON_CAST_STATIC_FAIL(0, type0),
                        WASM_GC_OP(kExprStructGet), type0, 0, kExprReturn),
           kExprBrOnNull, 0, WASM_GC_OP(kExprRefCastStatic), type1,
           WASM_GC_OP(kExprStructGet), type1, 1, kExprReturn),
       WASM_I32V(null_value), kExprEnd});

782 783
  tester.CompileModule();
  tester.CheckResult(kBranchTaken, field1_value);
784
  tester.CheckResult(kBranchTakenStatic, field1_value);
785 786 787 788 789
  tester.CheckResult(kBranchNotTaken, field0_value);
  tester.CheckResult(kNull, null_value);
  tester.CheckResult(kUnrelatedTypes, field1_value);
}

790 791
WASM_COMPILED_EXEC_TEST(WasmRefEq) {
  WasmGCTester tester(execution_tier);
792
  byte type_index = tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
793 794 795 796
  ValueType kRefTypes[] = {ref(type_index)};
  ValueType kOptRefType = optref(type_index);
  FunctionSig sig_q_v(1, 0, kRefTypes);

797
  byte local_index = 0;
798
  const byte kFunc = tester.DefineFunction(
799
      tester.sigs.i_v(), {kOptRefType},
800
      {WASM_LOCAL_SET(local_index, WASM_STRUCT_NEW_WITH_RTT(
801 802
                                       type_index, WASM_I32V(55), WASM_I32V(66),
                                       WASM_RTT_CANON(type_index))),
803
       WASM_I32_ADD(
804 805
           WASM_I32_SHL(
               WASM_REF_EQ(  // true
806
                   WASM_LOCAL_GET(local_index), WASM_LOCAL_GET(local_index)),
807
               WASM_I32V(0)),
808 809
           WASM_I32_ADD(
               WASM_I32_SHL(WASM_REF_EQ(  // false
810
                                WASM_LOCAL_GET(local_index),
811 812 813
                                WASM_STRUCT_NEW_WITH_RTT(
                                    type_index, WASM_I32V(55), WASM_I32V(66),
                                    WASM_RTT_CANON(type_index))),
814 815
                            WASM_I32V(1)),
               WASM_I32_ADD(WASM_I32_SHL(  // false
816
                                WASM_REF_EQ(WASM_LOCAL_GET(local_index),
817
                                            WASM_REF_NULL(type_index)),
818 819
                                WASM_I32V(2)),
                            WASM_I32_SHL(WASM_REF_EQ(  // true
820 821
                                             WASM_REF_NULL(type_index),
                                             WASM_REF_NULL(type_index)),
822 823
                                         WASM_I32V(3))))),
       kExprEnd});
824

825
  tester.CompileModule();
826
  tester.CheckResult(kFunc, 0b1001);
827 828
}

829 830
WASM_COMPILED_EXEC_TEST(WasmPackedStructU) {
  WasmGCTester tester(execution_tier);
831

832
  const byte type_index = tester.DefineStruct(
833
      {F(kWasmI8, true), F(kWasmI16, true), F(kWasmI32, true)});
834
  ValueType struct_type = optref(type_index);
835

836
  const byte local_index = 0;
837 838 839 840

  int32_t expected_output_0 = 0x1234;
  int32_t expected_output_1 = -1;

841
  const byte kF0 = tester.DefineFunction(
842
      tester.sigs.i_v(), {struct_type},
843
      {WASM_LOCAL_SET(local_index,
844 845 846 847
                      WASM_STRUCT_NEW_WITH_RTT(
                          type_index, WASM_I32V(expected_output_0),
                          WASM_I32V(expected_output_1), WASM_I32V(0x12345678),
                          WASM_RTT_CANON(type_index))),
848
       WASM_STRUCT_GET_U(type_index, 0, WASM_LOCAL_GET(local_index)),
849 850
       kExprEnd});

851
  const byte kF1 = tester.DefineFunction(
852
      tester.sigs.i_v(), {struct_type},
853
      {WASM_LOCAL_SET(local_index,
854 855 856 857
                      WASM_STRUCT_NEW_WITH_RTT(
                          type_index, WASM_I32V(expected_output_0),
                          WASM_I32V(expected_output_1), WASM_I32V(0x12345678),
                          WASM_RTT_CANON(type_index))),
858
       WASM_STRUCT_GET_U(type_index, 1, WASM_LOCAL_GET(local_index)),
859 860 861
       kExprEnd});
  tester.CompileModule();

862 863
  tester.CheckResult(kF0, static_cast<uint8_t>(expected_output_0));
  tester.CheckResult(kF1, static_cast<uint16_t>(expected_output_1));
864 865
}

866 867
WASM_COMPILED_EXEC_TEST(WasmPackedStructS) {
  WasmGCTester tester(execution_tier);
868

869
  const byte type_index = tester.DefineStruct(
870
      {F(kWasmI8, true), F(kWasmI16, true), F(kWasmI32, true)});
871
  ValueType struct_type = optref(type_index);
872

873
  const byte local_index = 0;
874 875 876 877

  int32_t expected_output_0 = 0x80;
  int32_t expected_output_1 = 42;

878
  const byte kF0 = tester.DefineFunction(
879
      tester.sigs.i_v(), {struct_type},
880
      {WASM_LOCAL_SET(
881
           local_index,
882 883 884
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(expected_output_0),
                                    WASM_I32V(expected_output_1), WASM_I32V(0),
                                    WASM_RTT_CANON(type_index))),
885
       WASM_STRUCT_GET_S(type_index, 0, WASM_LOCAL_GET(local_index)),
886 887
       kExprEnd});

888
  const byte kF1 = tester.DefineFunction(
889
      tester.sigs.i_v(), {struct_type},
890
      {WASM_LOCAL_SET(
891 892 893 894
           local_index,
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(0x80),
                                    WASM_I32V(expected_output_1), WASM_I32V(0),
                                    WASM_RTT_CANON(type_index))),
895
       WASM_STRUCT_GET_S(type_index, 1, WASM_LOCAL_GET(local_index)),
896 897 898 899
       kExprEnd});

  tester.CompileModule();

900 901
  tester.CheckResult(kF0, static_cast<int8_t>(expected_output_0));
  tester.CheckResult(kF1, static_cast<int16_t>(expected_output_1));
902 903
}

904 905
TEST(WasmLetInstruction) {
  WasmGCTester tester;
906
  const byte type_index =
907
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
908

909 910 911
  const byte let_local_index = 0;
  const byte let_field_index = 0;
  const byte kLetTest1 = tester.DefineFunction(
912
      tester.sigs.i_v(), {},
913
      {WASM_LET_1_I(
914
           WASM_SEQ(kRefCode, type_index),
915 916 917
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(52),
                                    WASM_RTT_CANON(type_index)),
           WASM_STRUCT_GET(type_index, let_field_index,
918
                           WASM_LOCAL_GET(let_local_index))),
919 920
       kExprEnd});

921 922
  const byte let_2_field_index = 0;
  const byte kLetTest2 = tester.DefineFunction(
923
      tester.sigs.i_v(), {},
924
      {WASM_LET_2_I(
925 926
           kI32Code, WASM_I32_ADD(WASM_I32V(42), WASM_I32V(-32)),
           WASM_SEQ(kRefCode, type_index),
927 928 929
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(52),
                                    WASM_RTT_CANON(type_index)),
           WASM_I32_MUL(WASM_STRUCT_GET(type_index, let_2_field_index,
930 931
                                        WASM_LOCAL_GET(1)),
                        WASM_LOCAL_GET(0))),
932 933
       kExprEnd});

934
  const byte kLetTestLocals = tester.DefineFunction(
935
      tester.sigs.i_i(), {kWasmI32},
936
      {WASM_LOCAL_SET(1, WASM_I32V(100)),
937
       WASM_LET_2_I(
938
           kI32Code, WASM_I32V(1), kI32Code, WASM_I32V(10),
939 940 941 942
           WASM_I32_SUB(WASM_I32_ADD(WASM_LOCAL_GET(0),     // 1st let-local
                                     WASM_LOCAL_GET(2)),    // Parameter
                        WASM_I32_ADD(WASM_LOCAL_GET(1),     // 2nd let-local
                                     WASM_LOCAL_GET(3)))),  // Function local
943
       kExprEnd});
944
  // Result: (1 + 1000) - (10 + 100) = 891
945

946 947
  const byte let_erase_local_index = 0;
  const byte kLetTestErase = tester.DefineFunction(
948
      tester.sigs.i_v(), {kWasmI32},
949
      {WASM_LOCAL_SET(let_erase_local_index, WASM_I32V(0)),
950
       WASM_LET_1_V(kI32Code, WASM_I32V(1), WASM_NOP),
951
       WASM_LOCAL_GET(let_erase_local_index), kExprEnd});
952 953 954
  // The result should be 0 and not 1, as local_get(0) refers to the original
  // local.

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
  const byte kLetInLoop = tester.DefineFunction(
      tester.sigs.i_i(), {},
      {WASM_LOOP(WASM_LET_1_V(
           kI32Code, WASM_I32V(10),  // --
           WASM_LOCAL_SET(1, WASM_I32_SUB(WASM_LOCAL_GET(1), WASM_I32V(10))),
           WASM_BR_IF(1, WASM_I32_GES(WASM_LOCAL_GET(1), WASM_LOCAL_GET(0))))),
       WASM_LOCAL_GET(0), WASM_END});

  const byte kLetInBlock = tester.DefineFunction(
      tester.sigs.i_i(), {},
      {WASM_BLOCK(WASM_LET_1_V(
           kI32Code, WASM_I32V(10),  // --
           WASM_BR_IF(1, WASM_I32_GES(WASM_LOCAL_GET(1), WASM_LOCAL_GET(0))),
           WASM_LOCAL_SET(1, WASM_I32V(30)))),
       WASM_LOCAL_GET(0), WASM_END});

971
  tester.CompileModule();
972

973 974 975 976
  tester.CheckResult(kLetTest1, 42);
  tester.CheckResult(kLetTest2, 420);
  tester.CheckResult(kLetTestLocals, 891, 1000);
  tester.CheckResult(kLetTestErase, 0);
977 978 979 980
  tester.CheckResult(kLetInLoop, 2, 52);
  tester.CheckResult(kLetInLoop, -11, -1);
  tester.CheckResult(kLetInBlock, 15, 15);
  tester.CheckResult(kLetInBlock, 30, 5);
981 982
}

983 984
WASM_COMPILED_EXEC_TEST(WasmBasicArray) {
  WasmGCTester tester(execution_tier);
985
  if (!tester.HasSimdSupport(execution_tier)) return;
986

987
  const byte type_index = tester.DefineArray(wasm::kWasmI32, true);
988
  const byte fp_type_index = tester.DefineArray(wasm::kWasmF64, true);
989
  const byte immut_type_index = tester.DefineArray(wasm::kWasmI32, false);
990
  ValueType kRefTypes[] = {ref(type_index)};
991
  FunctionSig sig_q_v(1, 0, kRefTypes);
992
  ValueType kOptRefType = optref(type_index);
993 994

  // f: a = [12, 12, 12]; a[1] = 42; return a[arg0]
995 996
  const byte local_index = 1;
  const byte kGetElem = tester.DefineFunction(
997
      tester.sigs.i_i(), {kOptRefType},
998
      {WASM_LOCAL_SET(local_index, WASM_ARRAY_NEW_WITH_RTT(
999 1000
                                       type_index, WASM_I32V(12), WASM_I32V(3),
                                       WASM_RTT_CANON(type_index))),
1001
       WASM_ARRAY_SET(type_index, WASM_LOCAL_GET(local_index), WASM_I32V(1),
1002
                      WASM_I32V(42)),
1003 1004
       WASM_ARRAY_GET(type_index, WASM_LOCAL_GET(local_index),
                      WASM_LOCAL_GET(0)),
1005
       kExprEnd});
1006

1007
  // Reads and returns an array's length.
1008 1009 1010 1011 1012 1013
  const byte kGetLength =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_ARRAY_LEN(WASM_ARRAY_NEW_WITH_RTT(
                                 type_index, WASM_I32V(0), WASM_I32V(42),
                                 WASM_RTT_CANON(type_index))),
                             kExprEnd});
1014

1015
  // Create an array of length 2, initialized to [42, 42].
1016
  const byte kAllocate = tester.DefineFunction(
1017
      &sig_q_v, {},
1018 1019 1020
      {WASM_ARRAY_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(2),
                               WASM_RTT_CANON(type_index)),
       kExprEnd});
1021

1022 1023 1024 1025 1026
  const byte kAllocateStatic = tester.DefineFunction(
      &sig_q_v, {},
      {WASM_ARRAY_NEW_DEFAULT(type_index, WASM_I32V(2)), WASM_DROP,
       WASM_ARRAY_NEW(type_index, WASM_I32V(42), WASM_I32V(2)), kExprEnd});

1027 1028
  const byte kInit = tester.DefineFunction(
      &sig_q_v, {},
1029 1030
      {WASM_ARRAY_NEW_FIXED(type_index, 3, WASM_I32V(10), WASM_I32V(20),
                            WASM_I32V(30), WASM_RTT_CANON(type_index)),
1031 1032
       kExprEnd});

1033 1034
  const byte kImmutable = tester.DefineFunction(
      tester.sigs.i_v(), {},
1035 1036 1037 1038 1039
      {WASM_ARRAY_GET(immut_type_index,
                      WASM_ARRAY_NEW_FIXED(immut_type_index, 2, WASM_I32V(42),
                                           WASM_I32V(43),
                                           WASM_RTT_CANON(immut_type_index)),
                      WASM_I32V(0)),
1040 1041
       kExprEnd});

1042
  const uint32_t kLongLength = 1u << 16;
1043 1044
  const byte kAllocateLarge = tester.DefineFunction(
      &sig_q_v, {},
1045 1046
      {WASM_ARRAY_NEW_DEFAULT_WITH_RTT(type_index, WASM_I32V(kLongLength),
                                       WASM_RTT_CANON(type_index)),
1047 1048
       kExprEnd});

1049 1050
  ArrayType array_type(kWasmI32, true);
  const uint32_t kTooLong = WasmArray::MaxLength(&array_type) + 1;
1051 1052
  const byte kAllocateTooLarge = tester.DefineFunction(
      &sig_q_v, {},
1053 1054
      {WASM_ARRAY_NEW_DEFAULT_WITH_RTT(type_index, WASM_I32V(kTooLong),
                                       WASM_RTT_CANON(type_index)),
1055 1056
       kExprEnd});

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  // Tests that fp arrays work properly.
  // f: a = [10.0, 10.0, 10.0]; a[1] = 42.42; return static_cast<int64>(a[1]);
  double result_value = 42.42;
  const byte kTestFpArray = tester.DefineFunction(
      tester.sigs.i_v(), {optref(fp_type_index)},
      {WASM_LOCAL_SET(0, WASM_ARRAY_NEW_WITH_RTT(
                             fp_type_index, WASM_F64(10.0), WASM_I32V(3),
                             WASM_RTT_CANON(fp_type_index))),
       WASM_ARRAY_SET(fp_type_index, WASM_LOCAL_GET(0), WASM_I32V(1),
                      WASM_F64(result_value)),
       WASM_I32_SCONVERT_F64(
           WASM_ARRAY_GET(fp_type_index, WASM_LOCAL_GET(0), WASM_I32V(1))),
       kExprEnd});

1071
  tester.CompileModule();
1072

1073 1074 1075 1076 1077 1078
  tester.CheckResult(kGetElem, 12, 0);
  tester.CheckResult(kGetElem, 42, 1);
  tester.CheckResult(kGetElem, 12, 2);
  tester.CheckHasThrown(kGetElem, 3);
  tester.CheckHasThrown(kGetElem, -1);
  tester.CheckResult(kGetLength, 42);
1079
  tester.CheckResult(kImmutable, 42);
1080
  tester.CheckResult(kTestFpArray, static_cast<int32_t>(result_value));
1081

1082 1083 1084 1085 1086 1087 1088
  Handle<Object> h_result = tester.GetResultObject(kAllocate).ToHandleChecked();
  CHECK(h_result->IsWasmArray());
  CHECK_EQ(2, Handle<WasmArray>::cast(h_result)->length());

  h_result = tester.GetResultObject(kAllocateStatic).ToHandleChecked();
  CHECK(h_result->IsWasmArray());
  CHECK_EQ(2, Handle<WasmArray>::cast(h_result)->length());
1089

1090 1091 1092 1093 1094 1095 1096
  Handle<Object> init_result = tester.GetResultObject(kInit).ToHandleChecked();
  CHECK(init_result->IsWasmArray());
  CHECK_EQ(3, Handle<WasmArray>::cast(init_result)->length());
  CHECK_EQ(10, Handle<WasmArray>::cast(init_result)->GetElement(0).to_i32());
  CHECK_EQ(20, Handle<WasmArray>::cast(init_result)->GetElement(1).to_i32());
  CHECK_EQ(30, Handle<WasmArray>::cast(init_result)->GetElement(2).to_i32());

1097 1098 1099 1100 1101 1102 1103
  MaybeHandle<Object> maybe_large_result =
      tester.GetResultObject(kAllocateLarge);
  Handle<Object> large_result = maybe_large_result.ToHandleChecked();
  CHECK(large_result->IsWasmArray());
  CHECK(Handle<WasmArray>::cast(large_result)->Size() >
        kMaxRegularHeapObjectSize);

1104
  tester.CheckHasThrown(kAllocateTooLarge, "requested new array is too large");
1105 1106
}

1107 1108
WASM_COMPILED_EXEC_TEST(WasmPackedArrayU) {
  WasmGCTester tester(execution_tier);
1109
  const byte array_index = tester.DefineArray(kWasmI8, true);
1110
  ValueType array_type = optref(array_index);
1111

1112 1113
  const byte param_index = 0;
  const byte local_index = 1;
1114 1115 1116

  int32_t expected_output_3 = 258;

1117
  const byte kF = tester.DefineFunction(
1118
      tester.sigs.i_i(), {array_type},
1119
      {WASM_LOCAL_SET(local_index, WASM_ARRAY_NEW_WITH_RTT(
1120 1121
                                       array_index, WASM_I32V(0), WASM_I32V(4),
                                       WASM_RTT_CANON(array_index))),
1122
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(0),
1123
                      WASM_I32V(1)),
1124
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(1),
1125
                      WASM_I32V(10)),
1126
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(2),
1127
                      WASM_I32V(200)),
1128
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(3),
1129
                      WASM_I32V(expected_output_3)),
1130 1131
       WASM_ARRAY_GET_U(array_index, WASM_LOCAL_GET(local_index),
                        WASM_LOCAL_GET(param_index)),
1132 1133 1134
       kExprEnd});

  tester.CompileModule();
1135 1136 1137
  tester.CheckResult(kF, 1, 0);
  tester.CheckResult(kF, 10, 1);
  tester.CheckResult(kF, 200, 2);
1138
  // Only the 2 lsb's of 258 should be stored in the array.
1139
  tester.CheckResult(kF, static_cast<uint8_t>(expected_output_3), 3);
1140 1141
}

1142 1143
WASM_COMPILED_EXEC_TEST(WasmPackedArrayS) {
  WasmGCTester tester(execution_tier);
1144
  const byte array_index = tester.DefineArray(kWasmI16, true);
1145
  ValueType array_type = optref(array_index);
1146

1147
  int32_t array_elements[] = {0x12345678, 10, 0xFEDC, 0xFF1234};
1148

1149 1150 1151
  const byte param_index = 0;
  const byte local_index = 1;
  const byte kF = tester.DefineFunction(
1152
      tester.sigs.i_i(), {array_type},
1153
      {WASM_LOCAL_SET(
1154
           local_index,
1155
           WASM_ARRAY_NEW_WITH_RTT(array_index, WASM_I32V(array_elements[0]),
1156
                                   WASM_I32V(4), WASM_RTT_CANON(array_index))),
1157
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(1),
1158
                      WASM_I32V(array_elements[1])),
1159
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(2),
1160
                      WASM_I32V(array_elements[2])),
1161
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(3),
1162
                      WASM_I32V(array_elements[3])),
1163 1164
       WASM_ARRAY_GET_S(array_index, WASM_LOCAL_GET(local_index),
                        WASM_LOCAL_GET(param_index)),
1165 1166 1167 1168
       kExprEnd});

  tester.CompileModule();
  // Exactly the 2 lsb's should be stored by array.new.
1169 1170
  tester.CheckResult(kF, static_cast<int16_t>(array_elements[0]), 0);
  tester.CheckResult(kF, static_cast<int16_t>(array_elements[1]), 1);
1171
  // Sign should be extended.
1172
  tester.CheckResult(kF, static_cast<int16_t>(array_elements[2]), 2);
1173
  // Exactly the 2 lsb's should be stored by array.set.
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
  tester.CheckResult(kF, static_cast<int16_t>(array_elements[3]), 3);
}

WASM_COMPILED_EXEC_TEST(WasmArrayCopy) {
  WasmGCTester tester(execution_tier);
  const byte array32_index = tester.DefineArray(kWasmI32, true);
  const byte array16_index = tester.DefineArray(kWasmI16, true);
  const byte arrayref_index = tester.DefineArray(optref(array32_index), true);

  // Copies i32 ranges: local1[0..3] to local2[6..9].
  const byte kCopyI32 = tester.DefineFunction(
      tester.sigs.i_i(), {optref(array32_index), optref(array32_index)},
1186 1187 1188
      {WASM_LOCAL_SET(
           1, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array32_index))),
1189 1190 1191 1192 1193 1194 1195 1196
       WASM_ARRAY_SET(array32_index, WASM_LOCAL_GET(1), WASM_I32V(0),
                      WASM_I32V(0)),
       WASM_ARRAY_SET(array32_index, WASM_LOCAL_GET(1), WASM_I32V(1),
                      WASM_I32V(1)),
       WASM_ARRAY_SET(array32_index, WASM_LOCAL_GET(1), WASM_I32V(2),
                      WASM_I32V(2)),
       WASM_ARRAY_SET(array32_index, WASM_LOCAL_GET(1), WASM_I32V(3),
                      WASM_I32V(3)),
1197 1198 1199
       WASM_LOCAL_SET(
           2, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array32_index))),
1200 1201 1202 1203 1204 1205 1206 1207 1208
       WASM_ARRAY_COPY(array32_index, array32_index, WASM_LOCAL_GET(2),
                       WASM_I32V(6), WASM_LOCAL_GET(1), WASM_I32V(0),
                       WASM_I32V(4)),
       WASM_ARRAY_GET(array32_index, WASM_LOCAL_GET(2), WASM_LOCAL_GET(0)),
       kExprEnd});

  // Copies i16 ranges: local1[0..3] to local2[6..9].
  const byte kCopyI16 = tester.DefineFunction(
      tester.sigs.i_i(), {optref(array16_index), optref(array16_index)},
1209 1210 1211
      {WASM_LOCAL_SET(
           1, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array16_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array16_index))),
1212 1213 1214 1215 1216 1217 1218 1219
       WASM_ARRAY_SET(array16_index, WASM_LOCAL_GET(1), WASM_I32V(0),
                      WASM_I32V(0)),
       WASM_ARRAY_SET(array16_index, WASM_LOCAL_GET(1), WASM_I32V(1),
                      WASM_I32V(1)),
       WASM_ARRAY_SET(array16_index, WASM_LOCAL_GET(1), WASM_I32V(2),
                      WASM_I32V(2)),
       WASM_ARRAY_SET(array16_index, WASM_LOCAL_GET(1), WASM_I32V(3),
                      WASM_I32V(3)),
1220 1221 1222
       WASM_LOCAL_SET(
           2, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array16_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array16_index))),
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
       WASM_ARRAY_COPY(array16_index, array16_index, WASM_LOCAL_GET(2),
                       WASM_I32V(6), WASM_LOCAL_GET(1), WASM_I32V(0),
                       WASM_I32V(4)),
       WASM_ARRAY_GET_S(array16_index, WASM_LOCAL_GET(2), WASM_LOCAL_GET(0)),
       kExprEnd});

  // Copies reference ranges: local1[0..3] to local2[6..9].
  const byte kCopyRef = tester.DefineFunction(
      FunctionSig::Build(tester.zone(), {optref(array32_index)}, {kWasmI32}),
      {optref(arrayref_index), optref(arrayref_index)},
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
      {WASM_LOCAL_SET(
           1, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(arrayref_index, WASM_I32V(10),
                                              WASM_RTT_CANON(arrayref_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(0),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(6),
                                           WASM_RTT_CANON(array32_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(1),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(7),
                                           WASM_RTT_CANON(array32_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(2),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(8),
                                           WASM_RTT_CANON(array32_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(3),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(9),
                                           WASM_RTT_CANON(array32_index))),
       WASM_LOCAL_SET(
           2, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(arrayref_index, WASM_I32V(10),
                                              WASM_RTT_CANON(arrayref_index))),
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
       WASM_ARRAY_COPY(arrayref_index, arrayref_index, WASM_LOCAL_GET(2),
                       WASM_I32V(6), WASM_LOCAL_GET(1), WASM_I32V(0),
                       WASM_I32V(4)),
       WASM_ARRAY_GET(arrayref_index, WASM_LOCAL_GET(2), WASM_LOCAL_GET(0)),
       kExprEnd});

  // Copies overlapping reference ranges: local1[0..3] to local1[2..5].
  const byte kCopyRefOverlapping = tester.DefineFunction(
      FunctionSig::Build(tester.zone(), {optref(array32_index)}, {kWasmI32}),
      {optref(arrayref_index)},
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
      {WASM_LOCAL_SET(
           1, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(arrayref_index, WASM_I32V(10),
                                              WASM_RTT_CANON(arrayref_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(0),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(2),
                                           WASM_RTT_CANON(array32_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(1),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(3),
                                           WASM_RTT_CANON(array32_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(2),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(4),
                                           WASM_RTT_CANON(array32_index))),
       WASM_ARRAY_SET(
           arrayref_index, WASM_LOCAL_GET(1), WASM_I32V(3),
           WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(5),
                                           WASM_RTT_CANON(array32_index))),
1284 1285 1286 1287 1288 1289 1290 1291
       WASM_ARRAY_COPY(arrayref_index, arrayref_index, WASM_LOCAL_GET(1),
                       WASM_I32V(2), WASM_LOCAL_GET(1), WASM_I32V(0),
                       WASM_I32V(4)),
       WASM_ARRAY_GET(arrayref_index, WASM_LOCAL_GET(1), WASM_LOCAL_GET(0)),
       kExprEnd});

  const byte kOobSource = tester.DefineFunction(
      tester.sigs.v_v(), {optref(array32_index), optref(array32_index)},
1292 1293 1294 1295 1296 1297
      {WASM_LOCAL_SET(
           0, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array32_index))),
       WASM_LOCAL_SET(
           1, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array32_index))),
1298 1299 1300 1301 1302 1303 1304
       WASM_ARRAY_COPY(array32_index, array32_index, WASM_LOCAL_GET(1),
                       WASM_I32V(6), WASM_LOCAL_GET(0), WASM_I32V(8),
                       WASM_I32V(4)),
       kExprEnd});

  const byte kOobDestination = tester.DefineFunction(
      tester.sigs.v_v(), {optref(array32_index), optref(array32_index)},
1305 1306 1307 1308 1309 1310
      {WASM_LOCAL_SET(
           0, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array32_index))),
       WASM_LOCAL_SET(
           1, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array32_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array32_index))),
1311 1312 1313 1314 1315
       WASM_ARRAY_COPY(array32_index, array32_index, WASM_LOCAL_GET(1),
                       WASM_I32V(6), WASM_LOCAL_GET(0), WASM_I32V(3),
                       WASM_I32V(5)),
       kExprEnd});

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
  const byte kZeroLength = tester.DefineFunction(
      tester.sigs.i_v(), {optref(arrayref_index), optref(arrayref_index)},
      {WASM_LOCAL_SET(
           0, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(arrayref_index, WASM_I32V(10),
                                              WASM_RTT_CANON(arrayref_index))),
       WASM_LOCAL_SET(
           1, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(arrayref_index, WASM_I32V(10),
                                              WASM_RTT_CANON(arrayref_index))),
       WASM_ARRAY_COPY(arrayref_index, arrayref_index, WASM_LOCAL_GET(1),
                       WASM_I32V(6), WASM_LOCAL_GET(0), WASM_I32V(3),
                       WASM_I32V(0)),
       WASM_I32V(0), kExprEnd});

1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 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
  tester.CompileModule();

  tester.CheckResult(kCopyI32, 0, 5);
  tester.CheckResult(kCopyI32, 0, 6);
  tester.CheckResult(kCopyI32, 1, 7);
  tester.CheckResult(kCopyI32, 2, 8);
  tester.CheckResult(kCopyI32, 3, 9);

  tester.CheckResult(kCopyI16, 0, 5);
  tester.CheckResult(kCopyI16, 0, 6);
  tester.CheckResult(kCopyI16, 1, 7);
  tester.CheckResult(kCopyI16, 2, 8);
  tester.CheckResult(kCopyI16, 3, 9);

  {
    Handle<Object> result5 =
        tester.GetResultObject(kCopyRef, 5).ToHandleChecked();
    CHECK(result5->IsNull());
    for (int i = 6; i <= 9; i++) {
      Handle<Object> res =
          tester.GetResultObject(kCopyRef, i).ToHandleChecked();
      CHECK(res->IsWasmArray());
      CHECK_EQ(Handle<WasmArray>::cast(res)->length(),
               static_cast<uint32_t>(i));
    }
  }
  CHECK(tester.GetResultObject(kCopyRefOverlapping, 6)
            .ToHandleChecked()
            ->IsNull());
  Handle<Object> res0 =
      tester.GetResultObject(kCopyRefOverlapping, 0).ToHandleChecked();
  CHECK(res0->IsWasmArray());
  CHECK_EQ(Handle<WasmArray>::cast(res0)->length(), static_cast<uint32_t>(2));
  for (int i = 2; i <= 5; i++) {
    Handle<Object> res =
        tester.GetResultObject(kCopyRefOverlapping, i).ToHandleChecked();
    CHECK(res->IsWasmArray());
    CHECK_EQ(Handle<WasmArray>::cast(res)->length(), static_cast<uint32_t>(i));
  }

  tester.CheckHasThrown(kOobSource);
  tester.CheckHasThrown(kOobDestination);
1371
  tester.CheckResult(kZeroLength, 0);  // Does not throw.
1372 1373
}

1374 1375
WASM_COMPILED_EXEC_TEST(NewDefault) {
  WasmGCTester tester(execution_tier);
1376
  if (!tester.HasSimdSupport(execution_tier)) return;
1377 1378

  tester.builder()->StartRecursiveTypeGroup();
1379 1380
  const byte struct_type = tester.DefineStruct(
      {F(wasm::kWasmI32, true), F(wasm::kWasmF64, true), F(optref(0), true)});
1381 1382
  tester.builder()->EndRecursiveTypeGroup();

1383 1384 1385 1386
  const byte array_type = tester.DefineArray(wasm::kWasmI32, true);
  // Returns: struct[0] + f64_to_i32(struct[1]) + (struct[2].is_null ^ 1) == 0.
  const byte allocate_struct = tester.DefineFunction(
      tester.sigs.i_v(), {optref(struct_type)},
1387 1388
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                             struct_type, WASM_RTT_CANON(struct_type))),
1389
       WASM_I32_ADD(
1390
           WASM_I32_ADD(WASM_STRUCT_GET(struct_type, 0, WASM_LOCAL_GET(0)),
1391
                        WASM_I32_SCONVERT_F64(WASM_STRUCT_GET(
1392
                            struct_type, 1, WASM_LOCAL_GET(0)))),
1393
           WASM_I32_XOR(WASM_REF_IS_NULL(
1394
                            WASM_STRUCT_GET(struct_type, 2, WASM_LOCAL_GET(0))),
1395 1396 1397 1398
                        WASM_I32V(1))),
       kExprEnd});
  const byte allocate_array = tester.DefineFunction(
      tester.sigs.i_v(), {optref(array_type)},
1399 1400 1401
      {WASM_LOCAL_SET(
           0, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_type, WASM_I32V(2),
                                              WASM_RTT_CANON(array_type))),
1402
       WASM_I32_ADD(
1403 1404
           WASM_ARRAY_GET(array_type, WASM_LOCAL_GET(0), WASM_I32V(0)),
           WASM_ARRAY_GET(array_type, WASM_LOCAL_GET(0), WASM_I32V(1))),
1405 1406 1407 1408 1409 1410 1411 1412
       kExprEnd});

  tester.CompileModule();

  tester.CheckResult(allocate_struct, 0);
  tester.CheckResult(allocate_array, 0);
}

1413 1414
WASM_COMPILED_EXEC_TEST(BasicRtt) {
  WasmGCTester tester(execution_tier);
1415

1416
  const byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
1417 1418
  const byte subtype_index = tester.DefineStruct(
      {F(wasm::kWasmI32, true), F(wasm::kWasmI32, true)}, type_index);
1419

1420
  ValueType kRttTypes[] = {ValueType::Rtt(type_index)};
1421
  FunctionSig sig_t_v(1, 0, kRttTypes);
1422
  ValueType kRttSubtypes[] = {ValueType::Rtt(subtype_index)};
1423
  FunctionSig sig_t2_v(1, 0, kRttSubtypes);
1424
  ValueType kRttTypesDeeper[] = {ValueType::Rtt(type_index)};
1425
  FunctionSig sig_t3_v(1, 0, kRttTypesDeeper);
1426
  ValueType kRefTypes[] = {ref(type_index)};
1427
  FunctionSig sig_q_v(1, 0, kRefTypes);
1428

1429
  const byte kRttCanon = tester.DefineFunction(
1430
      &sig_t_v, {}, {WASM_RTT_CANON(type_index), kExprEnd});
1431
  const byte kRttSub = tester.DefineFunction(
1432
      &sig_t2_v, {}, {WASM_RTT_CANON(subtype_index), kExprEnd});
1433
  const byte kStructWithRtt = tester.DefineFunction(
1434 1435 1436 1437
      &sig_q_v, {},
      {WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42),
                                WASM_RTT_CANON(type_index)),
       kExprEnd});
1438

1439
  const int kFieldIndex = 1;
1440
  const int kStructIndexCode = 0;
1441 1442
  // This implements the following function:
  //   var local_struct: type0;
1443 1444 1445
  //   local_struct = new type1 with rtt 'kRttSub()';
  //   return (ref.test local_struct kRttSub()) +
  //          ((ref.cast local_struct kRttSub())[field0]);
1446 1447
  //   }
  // The expected return value is 1+42 = 43.
1448
  const byte kRefCast = tester.DefineFunction(
1449
      tester.sigs.i_v(), {optref(type_index)},
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
      {WASM_LOCAL_SET(
           kStructIndexCode,
           WASM_STRUCT_NEW_WITH_RTT(subtype_index, WASM_I32V(11), WASM_I32V(42),
                                    WASM_CALL_FUNCTION0(kRttSub))),
       WASM_I32_ADD(
           WASM_REF_TEST(WASM_LOCAL_GET(kStructIndexCode),
                         WASM_CALL_FUNCTION0(kRttSub)),
           WASM_STRUCT_GET(subtype_index, kFieldIndex,
                           WASM_REF_CAST(WASM_LOCAL_GET(kStructIndexCode),
                                         WASM_CALL_FUNCTION0(kRttSub)))),
1460
       kExprEnd});
1461 1462 1463

  tester.CompileModule();

1464 1465
  Handle<Object> ref_result =
      tester.GetResultObject(kRttCanon).ToHandleChecked();
1466 1467 1468 1469 1470 1471 1472

  CHECK(ref_result->IsMap());
  Handle<Map> map = Handle<Map>::cast(ref_result);
  CHECK(map->IsWasmStructMap());
  CHECK_EQ(reinterpret_cast<Address>(
               tester.instance()->module()->struct_type(type_index)),
           map->wasm_type_info().foreign_address());
1473

1474 1475
  Handle<Object> subref_result =
      tester.GetResultObject(kRttSub).ToHandleChecked();
1476 1477 1478 1479 1480
  CHECK(subref_result->IsMap());
  Handle<Map> submap = Handle<Map>::cast(subref_result);
  CHECK_EQ(reinterpret_cast<Address>(
               tester.instance()->module()->struct_type(subtype_index)),
           submap->wasm_type_info().foreign_address());
1481 1482 1483 1484
  Handle<Object> subref_result_canonicalized =
      tester.GetResultObject(kRttSub).ToHandleChecked();
  CHECK(subref_result.is_identical_to(subref_result_canonicalized));

1485
  Handle<Object> s = tester.GetResultObject(kStructWithRtt).ToHandleChecked();
1486 1487
  CHECK(s->IsWasmStruct());
  CHECK_EQ(Handle<WasmStruct>::cast(s)->map(), *map);
1488 1489

  tester.CheckResult(kRefCast, 43);
1490 1491
}

1492
WASM_COMPILED_EXEC_TEST(RefTrivialCasts) {
1493
  // TODO(7748): Add tests for branch_on_*.
1494 1495
  WasmGCTester tester(execution_tier);
  byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
1496 1497
  byte subtype_index = tester.DefineStruct(
      {F(wasm::kWasmI32, true), F(wasm::kWasmS128, false)}, type_index);
1498 1499 1500 1501 1502 1503 1504 1505
  ValueType sig_types[] = {kWasmS128, kWasmI32, kWasmF64};
  FunctionSig sig(1, 2, sig_types);
  byte sig_index = tester.DefineSignature(&sig);

  const byte kRefTestNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST(WASM_REF_NULL(type_index), WASM_RTT_CANON(subtype_index)),
       kExprEnd});
1506
  // Upcasts should not be optimized away for structural types.
1507
  const byte kRefTestUpcast = tester.DefineFunction(
1508 1509 1510 1511 1512
      tester.sigs.i_v(), {},
      {WASM_REF_TEST(WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                         subtype_index, WASM_RTT_CANON(subtype_index)),
                     WASM_RTT_CANON(type_index)),
       kExprEnd});
1513 1514 1515 1516 1517 1518
  const byte kRefTestUpcastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST(WASM_REF_NULL(subtype_index), WASM_RTT_CANON(type_index)),
       kExprEnd});
  const byte kRefTestUnrelated = tester.DefineFunction(
      tester.sigs.i_v(), {},
1519 1520 1521
      {WASM_REF_TEST(WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                         subtype_index, WASM_RTT_CANON(subtype_index)),
                     WASM_RTT_CANON(sig_index)),
1522 1523 1524 1525 1526 1527 1528
       kExprEnd});
  const byte kRefTestUnrelatedNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST(WASM_REF_NULL(subtype_index), WASM_RTT_CANON(sig_index)),
       kExprEnd});
  const byte kRefTestUnrelatedNonNullable = tester.DefineFunction(
      tester.sigs.i_v(), {},
1529 1530 1531
      {WASM_REF_TEST(WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                         type_index, WASM_RTT_CANON(type_index)),
                     WASM_RTT_CANON(sig_index)),
1532 1533 1534 1535 1536 1537 1538 1539 1540
       kExprEnd});

  const byte kRefCastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_CAST(WASM_REF_NULL(type_index),
                                      WASM_RTT_CANON(subtype_index))),
       kExprEnd});
  const byte kRefCastUpcast = tester.DefineFunction(
      tester.sigs.i_v(), {},
1541 1542 1543 1544
      {WASM_REF_IS_NULL(
           WASM_REF_CAST(WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                             subtype_index, WASM_RTT_CANON(subtype_index)),
                         WASM_RTT_CANON(type_index))),
1545 1546 1547 1548 1549 1550 1551 1552
       kExprEnd});
  const byte kRefCastUpcastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_CAST(WASM_REF_NULL(subtype_index),
                                      WASM_RTT_CANON(type_index))),
       kExprEnd});
  const byte kRefCastUnrelated = tester.DefineFunction(
      tester.sigs.i_v(), {},
1553 1554 1555 1556
      {WASM_REF_IS_NULL(
           WASM_REF_CAST(WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                             subtype_index, WASM_RTT_CANON(subtype_index)),
                         WASM_RTT_CANON(sig_index))),
1557 1558 1559 1560 1561 1562
       kExprEnd});
  const byte kRefCastUnrelatedNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_CAST(WASM_REF_NULL(subtype_index),
                                      WASM_RTT_CANON(sig_index))),
       kExprEnd});
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
  const byte kRefCastUnrelatedNonNullable =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_IS_NULL(WASM_REF_CAST(
                                 WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                                     type_index, WASM_RTT_CANON(type_index)),
                                 WASM_RTT_CANON(sig_index))),
                             kExprEnd});

  tester.CompileModule();

  tester.CheckResult(kRefTestNull, 0);
  tester.CheckResult(kRefTestUpcast, 1);
  tester.CheckResult(kRefTestUpcastNull, 0);
  tester.CheckResult(kRefTestUnrelated, 0);
  tester.CheckResult(kRefTestUnrelatedNull, 0);
  tester.CheckResult(kRefTestUnrelatedNonNullable, 0);

  tester.CheckResult(kRefCastNull, 1);
  tester.CheckResult(kRefCastUpcast, 0);
  tester.CheckResult(kRefCastUpcastNull, 1);
  tester.CheckHasThrown(kRefCastUnrelated);
  tester.CheckResult(kRefCastUnrelatedNull, 1);
  tester.CheckHasThrown(kRefCastUnrelatedNonNullable);
}

WASM_COMPILED_EXEC_TEST(RefTrivialCastsStatic) {
  WasmGCTester tester(execution_tier);
1590
  byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
  byte subtype_index = tester.DefineStruct(
      {F(wasm::kWasmI32, true), F(wasm::kWasmS128, false)}, type_index);
  ValueType sig_types[] = {kWasmS128, kWasmI32, kWasmF64};
  FunctionSig sig(1, 2, sig_types);
  byte sig_index = tester.DefineSignature(&sig);

  const byte kRefTestNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST_STATIC(WASM_REF_NULL(type_index), subtype_index),
       kExprEnd});
1601
  // Upcasts should be optimized away for nominal types.
1602 1603 1604 1605 1606 1607 1608 1609
  const byte kRefTestUpcast = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST_STATIC(WASM_STRUCT_NEW_DEFAULT(subtype_index), type_index),
       kExprEnd});
  const byte kRefTestUpcastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST_STATIC(WASM_REF_NULL(subtype_index), type_index),
       kExprEnd});
1610 1611 1612 1613
  const byte kRefTestUnrelatedNullable = tester.DefineFunction(
      tester.sigs.i_v(), {optref(subtype_index)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype_index)),
       WASM_REF_TEST_STATIC(WASM_LOCAL_GET(0), sig_index), kExprEnd});
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
  const byte kRefTestUnrelatedNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST_STATIC(WASM_REF_NULL(subtype_index), sig_index),
       kExprEnd});
  const byte kRefTestUnrelatedNonNullable = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_TEST_STATIC(WASM_STRUCT_NEW_DEFAULT(type_index), sig_index),
       kExprEnd});

  const byte kRefCastNull =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_IS_NULL(WASM_REF_CAST_STATIC(
                                 WASM_REF_NULL(type_index), subtype_index)),
                             kExprEnd});
  const byte kRefCastUpcast = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_CAST_STATIC(
           WASM_STRUCT_NEW_DEFAULT(subtype_index), type_index)),
       kExprEnd});
  const byte kRefCastUpcastNull =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_IS_NULL(WASM_REF_CAST_STATIC(
                                 WASM_REF_NULL(subtype_index), type_index)),
                             kExprEnd});
1638 1639 1640 1641
  const byte kRefCastUnrelatedNullable = tester.DefineFunction(
      tester.sigs.i_v(), {optref(subtype_index)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype_index)),
       WASM_REF_IS_NULL(WASM_REF_CAST_STATIC(WASM_LOCAL_GET(0), sig_index)),
1642 1643 1644 1645 1646 1647
       kExprEnd});
  const byte kRefCastUnrelatedNull =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_IS_NULL(WASM_REF_CAST_STATIC(
                                 WASM_REF_NULL(subtype_index), sig_index)),
                             kExprEnd});
1648 1649
  const byte kRefCastUnrelatedNonNullable = tester.DefineFunction(
      tester.sigs.i_v(), {},
1650 1651
      {WASM_REF_IS_NULL(WASM_REF_CAST_STATIC(
           WASM_STRUCT_NEW_DEFAULT(type_index), sig_index)),
1652 1653
       kExprEnd});

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
  const byte kBrOnCastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(subtype_index), WASM_REF_NULL(type_index),
                    WASM_BR_ON_CAST_STATIC(0, subtype_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastUpcast = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(type_index), WASM_STRUCT_NEW_DEFAULT(subtype_index),
                    WASM_BR_ON_CAST_STATIC(0, type_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastUpcastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(type_index), WASM_REF_NULL(subtype_index),
                    WASM_BR_ON_CAST_STATIC(0, type_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastUnrelatedNullable = tester.DefineFunction(
      tester.sigs.i_v(), {optref(subtype_index)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype_index)),
       WASM_BLOCK_R(optref(sig_index), WASM_LOCAL_GET(0),
                    WASM_BR_ON_CAST_STATIC(0, sig_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastUnrelatedNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(sig_index), WASM_REF_NULL(subtype_index),
                    WASM_BR_ON_CAST_STATIC(0, sig_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastUnrelatedNonNullable = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(sig_index), WASM_STRUCT_NEW_DEFAULT(subtype_index),
                    WASM_BR_ON_CAST_STATIC(0, sig_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastFailNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(type_index), WASM_REF_NULL(type_index),
                    WASM_BR_ON_CAST_STATIC_FAIL(0, subtype_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastFailUpcast = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(subtype_index),
                    WASM_STRUCT_NEW_DEFAULT(subtype_index),
                    WASM_BR_ON_CAST_STATIC_FAIL(0, type_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastFailUpcastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(subtype_index), WASM_REF_NULL(subtype_index),
                    WASM_BR_ON_CAST_STATIC_FAIL(0, type_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastFailUnrelatedNullable = tester.DefineFunction(
      tester.sigs.i_v(), {optref(subtype_index)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(subtype_index)),
       WASM_BLOCK_R(optref(subtype_index), WASM_LOCAL_GET(0),
                    WASM_BR_ON_CAST_STATIC_FAIL(0, sig_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastFailUnrelatedNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(subtype_index), WASM_REF_NULL(subtype_index),
                    WASM_BR_ON_CAST_STATIC_FAIL(0, sig_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

  const byte kBrOnCastFailUnrelatedNonNullable = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_BLOCK_R(optref(subtype_index),
                    WASM_STRUCT_NEW_DEFAULT(subtype_index),
                    WASM_BR_ON_CAST_STATIC_FAIL(0, sig_index), WASM_DROP,
                    WASM_RETURN(WASM_I32V(0))),
       WASM_DROP, WASM_I32V(1), WASM_END});

1742 1743 1744 1745 1746
  tester.CompileModule();

  tester.CheckResult(kRefTestNull, 0);
  tester.CheckResult(kRefTestUpcast, 1);
  tester.CheckResult(kRefTestUpcastNull, 0);
1747
  tester.CheckResult(kRefTestUnrelatedNullable, 0);
1748 1749 1750 1751 1752 1753
  tester.CheckResult(kRefTestUnrelatedNull, 0);
  tester.CheckResult(kRefTestUnrelatedNonNullable, 0);

  tester.CheckResult(kRefCastNull, 1);
  tester.CheckResult(kRefCastUpcast, 0);
  tester.CheckResult(kRefCastUpcastNull, 1);
1754
  tester.CheckHasThrown(kRefCastUnrelatedNullable);
1755 1756
  tester.CheckResult(kRefCastUnrelatedNull, 1);
  tester.CheckHasThrown(kRefCastUnrelatedNonNullable);
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770

  tester.CheckResult(kBrOnCastNull, 0);
  tester.CheckResult(kBrOnCastUpcast, 1);
  tester.CheckResult(kBrOnCastUpcastNull, 0);
  tester.CheckResult(kBrOnCastUnrelatedNullable, 0);
  tester.CheckResult(kBrOnCastUnrelatedNull, 0);
  tester.CheckResult(kBrOnCastUnrelatedNonNullable, 0);

  tester.CheckResult(kBrOnCastFailNull, 1);
  tester.CheckResult(kBrOnCastFailUpcast, 0);
  tester.CheckResult(kBrOnCastFailUpcastNull, 1);
  tester.CheckResult(kBrOnCastFailUnrelatedNullable, 1);
  tester.CheckResult(kBrOnCastFailUnrelatedNull, 1);
  tester.CheckResult(kBrOnCastFailUnrelatedNonNullable, 1);
1771 1772
}

1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
WASM_COMPILED_EXEC_TEST(TrivialAbstractCasts) {
  // TODO(7748): Add tests for branch_on_*.
  WasmGCTester tester(execution_tier);
  byte type_index = tester.DefineArray(wasm::kWasmI32, true);
  byte struct_type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
  ValueType sig_types[] = {kWasmS128, kWasmI32, kWasmF64};
  FunctionSig sig(1, 2, sig_types);

  const byte kIsArrayNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_ARRAY(WASM_REF_NULL(kAnyRefCode)), kExprEnd});
  const byte kIsArrayUpcast = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_ARRAY(WASM_ARRAY_NEW_DEFAULT_WITH_RTT(
           type_index, WASM_I32V(10), WASM_RTT_CANON(type_index))),
       kExprEnd});
  const byte kIsArrayUpcastNullable = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(type_index, kNullable)},
      {WASM_LOCAL_SET(
           0, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(type_index, WASM_I32V(10),
                                              WASM_RTT_CANON(type_index))),
       WASM_REF_IS_ARRAY(WASM_LOCAL_GET(0)), kExprEnd});
  const byte kIsArrayUpcastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_ARRAY(WASM_REF_NULL(type_index)), kExprEnd});
  const byte kIsArrayUnrelated = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(struct_type_index, kNullable)},
      {WASM_LOCAL_SET(
           0, WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                  struct_type_index, WASM_RTT_CANON(struct_type_index))),
       WASM_REF_IS_ARRAY(WASM_LOCAL_GET(0)), kExprEnd});
  const byte kIsArrayUnrelatedNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_ARRAY(WASM_REF_NULL(kI31RefCode)), kExprEnd});
  const byte kIsArrayUnrelatedNonNullable = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_ARRAY(WASM_I31_NEW(WASM_I32V(10))), kExprEnd});

  const byte kAsArrayNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_AS_ARRAY(WASM_REF_NULL(kAnyRefCode))),
       kExprEnd});
  const byte kAsArrayUpcast = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_AS_ARRAY(WASM_ARRAY_NEW_DEFAULT_WITH_RTT(
           type_index, WASM_I32V(10), WASM_RTT_CANON(type_index)))),
       kExprEnd});
  const byte kAsArrayUpcastNullable = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(type_index, kNullable)},
      {WASM_LOCAL_SET(
           0, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(type_index, WASM_I32V(10),
                                              WASM_RTT_CANON(type_index))),
       WASM_REF_IS_NULL(WASM_REF_AS_ARRAY(WASM_LOCAL_GET(0))), kExprEnd});
  const byte kAsArrayUpcastNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_AS_ARRAY(WASM_REF_NULL(type_index))),
       kExprEnd});
  const byte kAsArrayUnrelated = tester.DefineFunction(
      tester.sigs.i_v(), {ValueType::Ref(struct_type_index, kNullable)},
      {WASM_LOCAL_SET(
           0, WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                  struct_type_index, WASM_RTT_CANON(struct_type_index))),
       WASM_REF_IS_NULL(WASM_REF_AS_ARRAY(WASM_LOCAL_GET(0))), kExprEnd});
  const byte kAsArrayUnrelatedNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_AS_ARRAY(WASM_REF_NULL(kI31RefCode))),
       kExprEnd});
  const byte kAsArrayUnrelatedNonNullable = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_NULL(WASM_REF_AS_ARRAY(WASM_I31_NEW(WASM_I32V(10)))),
       kExprEnd});

  tester.CompileModule();

  tester.CheckResult(kIsArrayNull, 0);
  tester.CheckResult(kIsArrayUpcast, 1);
  tester.CheckResult(kIsArrayUpcastNullable, 1);
  tester.CheckResult(kIsArrayUpcastNull, 0);
  tester.CheckResult(kIsArrayUnrelated, 0);
  tester.CheckResult(kIsArrayUnrelatedNull, 0);
  tester.CheckResult(kIsArrayUnrelatedNonNullable, 0);

  tester.CheckHasThrown(kAsArrayNull);
  tester.CheckResult(kAsArrayUpcast, 0);
  tester.CheckResult(kAsArrayUpcastNullable, 0);
  tester.CheckHasThrown(kAsArrayUpcastNull);
  tester.CheckHasThrown(kAsArrayUnrelated);
  tester.CheckHasThrown(kAsArrayUnrelatedNull);
  tester.CheckHasThrown(kAsArrayUnrelatedNonNullable);
}

1864
WASM_COMPILED_EXEC_TEST(NoDepthRtt) {
1865
  WasmGCTester tester(execution_tier);
1866 1867

  const byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
1868 1869
  const byte subtype_index = tester.DefineStruct(
      {F(wasm::kWasmI32, true), F(wasm::kWasmI32, true)}, type_index);
1870

1871 1872
  ValueType kRttTypeNoDepth = ValueType::Rtt(type_index);
  FunctionSig sig_t1_v_nd(1, 0, &kRttTypeNoDepth);
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
  ValueType kRttSubtypeNoDepth = ValueType::Rtt(subtype_index);
  FunctionSig sig_t2_v_nd(1, 0, &kRttSubtypeNoDepth);

  const byte kRttSubtypeCanon = tester.DefineFunction(
      &sig_t2_v_nd, {}, {WASM_RTT_CANON(subtype_index), kExprEnd});

  const byte kTestCanon = tester.DefineFunction(
      tester.sigs.i_v(), {optref(type_index)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_WITH_RTT(
                             subtype_index, WASM_I32V(11), WASM_I32V(42),
                             WASM_RTT_CANON(subtype_index))),
1884
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION0(kRttSubtypeCanon)),
1885 1886 1887 1888 1889 1890 1891
       kExprEnd});

  tester.CompileModule();

  tester.CheckResult(kTestCanon, 1);
}

1892 1893 1894
WASM_COMPILED_EXEC_TEST(ArrayNewMap) {
  WasmGCTester tester(execution_tier);

1895
  const byte type_index = tester.DefineArray(kWasmI32, true);
1896 1897 1898

  ValueType array_type = ValueType::Ref(type_index, kNonNullable);
  FunctionSig sig(1, 0, &array_type);
1899
  const byte array_new_with_rtt = tester.DefineFunction(
1900 1901 1902 1903
      &sig, {},
      {WASM_ARRAY_NEW_WITH_RTT(type_index, WASM_I32V(10), WASM_I32V(42),
                               WASM_RTT_CANON(type_index)),
       kExprEnd});
1904 1905 1906
  const byte array_new_nominal = tester.DefineFunction(
      &sig, {},
      {WASM_ARRAY_NEW(type_index, WASM_I32V(10), WASM_I32V(42)), kExprEnd});
1907

1908
  ValueType rtt_type = ValueType::Rtt(type_index);
1909
  FunctionSig rtt_canon_sig(1, 0, &rtt_type);
1910
  const byte kRttCanon = tester.DefineFunction(
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
      &rtt_canon_sig, {}, {WASM_RTT_CANON(type_index), kExprEnd});

  tester.CompileModule();

  Handle<Object> map = tester.GetResultObject(kRttCanon).ToHandleChecked();

  Handle<Object> result =
      tester.GetResultObject(array_new_with_rtt).ToHandleChecked();
  CHECK(result->IsWasmArray());
  CHECK_EQ(Handle<WasmArray>::cast(result)->map(), *map);
1921 1922 1923 1924

  result = tester.GetResultObject(array_new_nominal).ToHandleChecked();
  CHECK(result->IsWasmArray());
  CHECK_EQ(Handle<WasmArray>::cast(result)->map(), *map);
1925 1926
}

1927 1928
WASM_COMPILED_EXEC_TEST(FunctionRefs) {
  WasmGCTester tester(execution_tier);
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
  const byte func_index =
      tester.DefineFunction(tester.sigs.i_v(), {}, {WASM_I32V(42), kExprEnd});
  const byte sig_index = 0;

  const byte other_sig_index = tester.DefineSignature(tester.sigs.d_d());

  // This is just so func_index counts as "declared".
  tester.AddGlobal(ValueType::Ref(sig_index, kNullable), false,
                   WasmInitExpr::RefFuncConst(func_index));

1939
  ValueType func_type = ValueType::Ref(sig_index, kNullable);
1940 1941
  FunctionSig sig_func(1, 0, &func_type);

1942
  ValueType rtt0 = ValueType::Rtt(sig_index);
1943
  FunctionSig sig_rtt0(1, 0, &rtt0);
1944
  const byte rtt_canon = tester.DefineFunction(
1945
      &sig_rtt0, {}, {WASM_RTT_CANON(sig_index), kExprEnd});
1946 1947 1948

  const byte cast = tester.DefineFunction(
      &sig_func, {kWasmFuncRef},
1949
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(func_index)),
1950
       WASM_REF_CAST(WASM_LOCAL_GET(0), WASM_RTT_CANON(sig_index)), kExprEnd});
1951 1952 1953 1954

  const byte cast_reference = tester.DefineFunction(
      &sig_func, {}, {WASM_REF_FUNC(sig_index), kExprEnd});

1955
  const byte test = tester.DefineFunction(
1956 1957 1958 1959
      tester.sigs.i_v(), {kWasmFuncRef},
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(func_index)),
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_RTT_CANON(sig_index)), kExprEnd});

1960
  const byte test_fail = tester.DefineFunction(
1961 1962 1963 1964
      tester.sigs.i_v(), {kWasmFuncRef},
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(func_index)),
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_RTT_CANON(other_sig_index)),
       kExprEnd});
1965 1966 1967 1968 1969 1970 1971

  tester.CompileModule();

  Handle<Object> result_canon =
      tester.GetResultObject(rtt_canon).ToHandleChecked();
  CHECK(result_canon->IsMap());
  Handle<Map> map_canon = Handle<Map>::cast(result_canon);
1972
  CHECK(map_canon->IsWasmInternalFunctionMap());
1973 1974

  Handle<Object> result_cast = tester.GetResultObject(cast).ToHandleChecked();
1975 1976 1977 1978
  CHECK(result_cast->IsWasmInternalFunction());
  Handle<JSFunction> cast_function = Handle<JSFunction>::cast(
      handle(Handle<WasmInternalFunction>::cast(result_cast)->external(),
             tester.isolate()));
1979 1980 1981

  Handle<Object> result_cast_reference =
      tester.GetResultObject(cast_reference).ToHandleChecked();
1982 1983 1984 1985
  CHECK(result_cast_reference->IsWasmInternalFunction());
  Handle<JSFunction> cast_function_reference = Handle<JSFunction>::cast(handle(
      Handle<WasmInternalFunction>::cast(result_cast_reference)->external(),
      tester.isolate()));
1986

1987 1988
  CHECK_EQ(cast_function->code().raw_instruction_start(),
           cast_function_reference->code().raw_instruction_start());
1989

1990
  tester.CheckResult(test, 1);
1991
  tester.CheckResult(test_fail, 0);
1992 1993
}

1994 1995
WASM_COMPILED_EXEC_TEST(CallRef) {
  WasmGCTester tester(execution_tier);
1996 1997
  byte callee = tester.DefineFunction(
      tester.sigs.i_ii(), {},
1998
      {WASM_I32_ADD(WASM_LOCAL_GET(0), WASM_LOCAL_GET(1)), kExprEnd});
1999 2000
  byte caller = tester.DefineFunction(
      tester.sigs.i_i(), {},
2001
      {WASM_CALL_REF(WASM_REF_FUNC(callee), WASM_I32V(42), WASM_LOCAL_GET(0)),
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
       kExprEnd});

  // This is just so func_index counts as "declared".
  tester.AddGlobal(ValueType::Ref(0, kNullable), false,
                   WasmInitExpr::RefFuncConst(callee));

  tester.CompileModule();

  tester.CheckResult(caller, 47, 5);
}

2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
WASM_COMPILED_EXEC_TEST(CallReftypeParameters) {
  WasmGCTester tester(execution_tier);
  byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
  ValueType kRefType{optref(type_index)};
  ValueType sig_types[] = {kWasmI32, kRefType, kRefType, kRefType, kRefType,
                           kWasmI32, kWasmI32, kWasmI32, kWasmI32};
  FunctionSig sig(1, 8, sig_types);
  byte adder = tester.DefineFunction(
      &sig, {},
      {WASM_I32_ADD(
           WASM_STRUCT_GET(type_index, 0, WASM_LOCAL_GET(0)),
           WASM_I32_ADD(
               WASM_STRUCT_GET(type_index, 0, WASM_LOCAL_GET(1)),
               WASM_I32_ADD(
                   WASM_STRUCT_GET(type_index, 0, WASM_LOCAL_GET(2)),
                   WASM_I32_ADD(
                       WASM_STRUCT_GET(type_index, 0, WASM_LOCAL_GET(3)),
                       WASM_I32_ADD(
                           WASM_LOCAL_GET(4),
                           WASM_I32_ADD(WASM_LOCAL_GET(5),
                                        WASM_I32_ADD(WASM_LOCAL_GET(6),
                                                     WASM_LOCAL_GET(7)))))))),
       kExprEnd});
  byte caller = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_CALL_FUNCTION(adder,
                          WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(2),
                                                   WASM_RTT_CANON(type_index)),
                          WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(4),
                                                   WASM_RTT_CANON(type_index)),
                          WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(8),
                                                   WASM_RTT_CANON(type_index)),
                          WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(16),
                                                   WASM_RTT_CANON(type_index)),
                          WASM_I32V(32), WASM_I32V(64), WASM_I32V(128),
                          WASM_I32V(256)),
       kExprEnd});

  tester.CompileModule();
  tester.CheckResult(caller, 510);
}

2055 2056
WASM_COMPILED_EXEC_TEST(AbstractTypeChecks) {
  WasmGCTester tester(execution_tier);
2057 2058

  byte array_index = tester.DefineArray(kWasmI32, true);
2059
  byte struct_index = tester.DefineStruct({F(kWasmI32, true)});
2060 2061
  byte function_index =
      tester.DefineFunction(tester.sigs.v_v(), {}, {kExprEnd});
2062
  byte sig_index = 2;
2063 2064 2065 2066 2067 2068 2069 2070

  // This is just so func_index counts as "declared".
  tester.AddGlobal(ValueType::Ref(sig_index, kNullable), false,
                   WasmInitExpr::RefFuncConst(function_index));

  byte kDataCheckNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_DATA(WASM_REF_NULL(kAnyRefCode)), kExprEnd});
2071 2072 2073
  byte kArrayCheckNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_ARRAY(WASM_REF_NULL(kAnyRefCode)), kExprEnd});
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
  byte kFuncCheckNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_FUNC(WASM_REF_NULL(kAnyRefCode)), kExprEnd});
  byte kI31CheckNull = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_REF_IS_I31(WASM_REF_NULL(kAnyRefCode)), kExprEnd});

  byte kDataCastNull =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_AS_DATA(WASM_REF_NULL(kAnyRefCode)),
                             WASM_DROP, WASM_I32V(1), kExprEnd});
2085 2086 2087 2088
  byte kArrayCastNull =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_AS_ARRAY(WASM_REF_NULL(kAnyRefCode)),
                             WASM_DROP, WASM_I32V(1), kExprEnd});
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
  byte kFuncCastNull =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_AS_FUNC(WASM_REF_NULL(kAnyRefCode)),
                             WASM_DROP, WASM_I32V(1), kExprEnd});
  byte kI31CastNull =
      tester.DefineFunction(tester.sigs.i_v(), {},
                            {WASM_REF_AS_I31(WASM_REF_NULL(kAnyRefCode)),
                             WASM_DROP, WASM_I32V(1), kExprEnd});

#define TYPE_CHECK(type, value)                              \
  tester.DefineFunction(tester.sigs.i_v(), {kWasmAnyRef},    \
                        {WASM_LOCAL_SET(0, WASM_SEQ(value)), \
                         WASM_REF_IS_##type(WASM_LOCAL_GET(0)), kExprEnd})

2103 2104 2105
  byte kDataCheckSuccess = TYPE_CHECK(
      DATA, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
2106
  byte kDataCheckFailure = TYPE_CHECK(DATA, WASM_I31_NEW(WASM_I32V(42)));
2107 2108 2109 2110 2111 2112
  byte kArrayCheckSuccess = TYPE_CHECK(
      ARRAY, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                             WASM_RTT_CANON(array_index)));
  byte kArrayCheckFailure =
      TYPE_CHECK(ARRAY, WASM_STRUCT_NEW_DEFAULT_WITH_RTT(
                            struct_index, WASM_RTT_CANON(struct_index)));
2113
  byte kFuncCheckSuccess = TYPE_CHECK(FUNC, WASM_REF_FUNC(function_index));
2114 2115 2116
  byte kFuncCheckFailure = TYPE_CHECK(
      FUNC, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
2117
  byte kI31CheckSuccess = TYPE_CHECK(I31, WASM_I31_NEW(WASM_I32V(42)));
2118 2119 2120
  byte kI31CheckFailure = TYPE_CHECK(
      I31, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                           WASM_RTT_CANON(array_index)));
2121 2122 2123 2124 2125 2126 2127 2128
#undef TYPE_CHECK

#define TYPE_CAST(type, value)                                             \
  tester.DefineFunction(tester.sigs.i_v(), {kWasmAnyRef},                  \
                        {WASM_LOCAL_SET(0, WASM_SEQ(value)),               \
                         WASM_REF_AS_##type(WASM_LOCAL_GET(0)), WASM_DROP, \
                         WASM_I32V(1), kExprEnd})

2129 2130 2131
  byte kDataCastSuccess = TYPE_CAST(
      DATA, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
2132
  byte kDataCastFailure = TYPE_CAST(DATA, WASM_I31_NEW(WASM_I32V(42)));
2133 2134 2135 2136
  byte kArrayCastSuccess = TYPE_CAST(
      DATA, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
  byte kArrayCastFailure = TYPE_CAST(DATA, WASM_I31_NEW(WASM_I32V(42)));
2137
  byte kFuncCastSuccess = TYPE_CAST(FUNC, WASM_REF_FUNC(function_index));
2138 2139
  byte kFuncCastFailure = TYPE_CAST(
      FUNC, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
2140
                                            WASM_RTT_CANON(array_index)));
2141 2142 2143 2144
  byte kI31CastSuccess = TYPE_CAST(I31, WASM_I31_NEW(WASM_I32V(42)));
  byte kI31CastFailure = TYPE_CAST(
      I31, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                           WASM_RTT_CANON(array_index)));
2145 2146 2147 2148
#undef TYPE_CAST

// If the branch is not taken, we return 0. If it is taken, then the respective
// type check should succeed, and we return 1.
2149 2150 2151 2152 2153 2154 2155
#define BR_ON(TYPE, type, value)                                            \
  tester.DefineFunction(                                                    \
      tester.sigs.i_v(), {kWasmAnyRef},                                     \
      {WASM_LOCAL_SET(0, WASM_SEQ(value)),                                  \
       WASM_REF_IS_##TYPE(WASM_BLOCK_R(kWasm##type##Ref, WASM_LOCAL_GET(0), \
                                       WASM_BR_ON_##TYPE(0),                \
                                       WASM_RETURN(WASM_I32V(0)))),         \
2156 2157 2158 2159
       kExprEnd})

  byte kBrOnDataTaken =
      BR_ON(DATA, Data,
2160 2161
            WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
2162 2163 2164
  byte kBrOnDataNotTaken = BR_ON(DATA, Data, WASM_REF_FUNC(function_index));
  byte kBrOnFuncTaken = BR_ON(FUNC, Func, WASM_REF_FUNC(function_index));
  byte kBrOnFuncNotTaken = BR_ON(FUNC, Func, WASM_I31_NEW(WASM_I32V(42)));
2165 2166 2167 2168 2169
  byte kBrOnArrayTaken =
      BR_ON(ARRAY, Array,
            WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
  byte kBrOnArrayNotTaken = BR_ON(ARRAY, Array, WASM_I31_NEW(WASM_I32V(42)));
2170 2171 2172
  byte kBrOnI31Taken = BR_ON(I31, I31, WASM_I31_NEW(WASM_I32V(42)));
  byte kBrOnI31NotTaken =
      BR_ON(I31, I31,
2173 2174
            WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
2175 2176
#undef BR_ON

2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
// If the branch is not taken, we return 1. If it is taken, then the respective
// type check should fail, and we return 0.
#define BR_ON_NON(TYPE, type, value)                                   \
  tester.DefineFunction(                                               \
      tester.sigs.i_v(), {kWasmAnyRef},                                \
      {WASM_LOCAL_SET(0, WASM_SEQ(value)),                             \
       WASM_REF_IS_##TYPE(WASM_BLOCK_R(kWasmAnyRef, WASM_LOCAL_GET(0), \
                                       WASM_BR_ON_NON_##TYPE(0),       \
                                       WASM_RETURN(WASM_I32V(1)))),    \
       kExprEnd})

  byte kBrOnNonDataNotTaken =
      BR_ON_NON(DATA, Data,
2190 2191
                WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                                WASM_RTT_CANON(array_index)));
2192 2193 2194 2195
  byte kBrOnNonDataTaken = BR_ON_NON(DATA, Data, WASM_REF_FUNC(function_index));
  byte kBrOnNonFuncNotTaken =
      BR_ON_NON(FUNC, Func, WASM_REF_FUNC(function_index));
  byte kBrOnNonFuncTaken = BR_ON_NON(FUNC, Func, WASM_I31_NEW(WASM_I32V(42)));
2196 2197 2198 2199 2200 2201
  byte kBrOnNonArrayNotTaken =
      BR_ON_NON(ARRAY, Array,
                WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                                WASM_RTT_CANON(array_index)));
  byte kBrOnNonArrayTaken =
      BR_ON_NON(ARRAY, Array, WASM_I31_NEW(WASM_I32V(42)));
2202 2203 2204
  byte kBrOnNonI31NotTaken = BR_ON_NON(I31, I31, WASM_I31_NEW(WASM_I32V(42)));
  byte kBrOnNonI31Taken =
      BR_ON_NON(I31, I31,
2205 2206
                WASM_ARRAY_NEW_DEFAULT_WITH_RTT(array_index, WASM_I32V(10),
                                                WASM_RTT_CANON(array_index)));
2207 2208
#undef BR_ON_NON

2209 2210 2211
  tester.CompileModule();

  tester.CheckResult(kDataCheckNull, 0);
2212
  tester.CheckResult(kArrayCheckNull, 0);
2213 2214 2215
  tester.CheckResult(kFuncCheckNull, 0);
  tester.CheckResult(kI31CheckNull, 0);

2216
  tester.CheckHasThrown(kDataCastNull);
2217
  tester.CheckHasThrown(kArrayCastNull);
2218 2219 2220
  tester.CheckHasThrown(kFuncCastNull);
  tester.CheckHasThrown(kI31CastNull);

2221
  tester.CheckResult(kDataCheckSuccess, 1);
2222
  tester.CheckResult(kArrayCheckSuccess, 1);
2223 2224 2225
  tester.CheckResult(kFuncCheckSuccess, 1);
  tester.CheckResult(kI31CheckSuccess, 1);

2226
  tester.CheckResult(kDataCheckFailure, 0);
2227
  tester.CheckResult(kArrayCheckFailure, 0);
2228 2229 2230
  tester.CheckResult(kFuncCheckFailure, 0);
  tester.CheckResult(kI31CheckFailure, 0);

2231
  tester.CheckResult(kDataCastSuccess, 1);
2232
  tester.CheckResult(kArrayCastSuccess, 1);
2233 2234 2235
  tester.CheckResult(kFuncCastSuccess, 1);
  tester.CheckResult(kI31CastSuccess, 1);

2236
  tester.CheckHasThrown(kDataCastFailure);
2237
  tester.CheckHasThrown(kArrayCastFailure);
2238 2239 2240
  tester.CheckHasThrown(kFuncCastFailure);
  tester.CheckHasThrown(kI31CastFailure);

2241 2242 2243 2244
  tester.CheckResult(kBrOnDataTaken, 1);
  tester.CheckResult(kBrOnDataNotTaken, 0);
  tester.CheckResult(kBrOnFuncTaken, 1);
  tester.CheckResult(kBrOnFuncNotTaken, 0);
2245 2246
  tester.CheckResult(kBrOnArrayTaken, 1);
  tester.CheckResult(kBrOnArrayNotTaken, 0);
2247 2248
  tester.CheckResult(kBrOnI31Taken, 1);
  tester.CheckResult(kBrOnI31NotTaken, 0);
2249 2250 2251 2252 2253

  tester.CheckResult(kBrOnNonDataTaken, 0);
  tester.CheckResult(kBrOnNonDataNotTaken, 1);
  tester.CheckResult(kBrOnNonFuncTaken, 0);
  tester.CheckResult(kBrOnNonFuncNotTaken, 1);
2254 2255
  tester.CheckResult(kBrOnNonArrayTaken, 0);
  tester.CheckResult(kBrOnNonArrayNotTaken, 1);
2256 2257
  tester.CheckResult(kBrOnNonI31Taken, 0);
  tester.CheckResult(kBrOnNonI31NotTaken, 1);
2258 2259
}

2260 2261
WASM_COMPILED_EXEC_TEST(BasicI31) {
  WasmGCTester tester(execution_tier);
2262
  const byte kSigned = tester.DefineFunction(
2263
      tester.sigs.i_i(), {},
2264
      {WASM_I31_GET_S(WASM_I31_NEW(WASM_LOCAL_GET(0))), kExprEnd});
2265
  const byte kUnsigned = tester.DefineFunction(
2266
      tester.sigs.i_i(), {},
2267
      {WASM_I31_GET_U(WASM_I31_NEW(WASM_LOCAL_GET(0))), kExprEnd});
2268
  tester.CompileModule();
2269 2270
  tester.CheckResult(kSigned, 123, 123);
  tester.CheckResult(kUnsigned, 123, 123);
2271
  // Truncation:
2272 2273
  tester.CheckResult(kSigned, 0x1234, static_cast<int32_t>(0x80001234));
  tester.CheckResult(kUnsigned, 0x1234, static_cast<int32_t>(0x80001234));
2274
  // Sign/zero extension:
2275 2276 2277 2278
  tester.CheckResult(kSigned, -1, 0x7FFFFFFF);
  tester.CheckResult(kUnsigned, 0x7FFFFFFF, 0x7FFFFFFF);
}

2279 2280
// This flushed out a few bugs, so it serves as a regression test. It can also
// be modified (made to run longer) to measure performance of casts.
2281 2282
WASM_COMPILED_EXEC_TEST(CastsBenchmark) {
  WasmGCTester tester(execution_tier);
2283
  const byte SuperType = tester.DefineStruct({F(wasm::kWasmI32, true)});
2284 2285
  const byte SubType = tester.DefineStruct(
      {F(wasm::kWasmI32, true), F(wasm::kWasmI32, true)}, SuperType);
2286 2287 2288

  ValueType kDataRefNull = ValueType::Ref(HeapType::kData, kNullable);
  const byte ListType = tester.DefineArray(kDataRefNull, true);
2289 2290 2291 2292 2293 2294

  const byte List =
      tester.AddGlobal(ValueType::Ref(ListType, kNullable), true,
                       WasmInitExpr::RefNullConst(
                           static_cast<HeapType::Representation>(ListType)));
  const byte RttSuper = tester.AddGlobal(
2295
      ValueType::Rtt(SuperType), false,
2296
      WasmInitExpr::RttCanon(static_cast<HeapType::Representation>(SuperType)));
2297
  const byte RttSub = tester.AddGlobal(ValueType::Rtt(SubType), false,
2298
                                       WasmInitExpr::RttCanon(SubType));
2299
  const byte RttList = tester.AddGlobal(
2300
      ValueType::Rtt(ListType), false,
2301 2302 2303 2304 2305 2306 2307
      WasmInitExpr::RttCanon(static_cast<HeapType::Representation>(ListType)));

  const uint32_t kListLength = 1024;
  const uint32_t i = 0;
  const byte Prepare = tester.DefineFunction(
      tester.sigs.i_v(), {wasm::kWasmI32},
      {// List = new eqref[kListLength];
2308 2309 2310
       WASM_GLOBAL_SET(List, WASM_ARRAY_NEW_DEFAULT_WITH_RTT(
                                 ListType, WASM_I32V(kListLength),
                                 WASM_GLOBAL_GET(RttList))),
2311 2312 2313 2314 2315 2316
       // for (int i = 0; i < kListLength; ) {
       //   List[i] = new Super(i);
       //   i++;
       //   List[i] = new Sub(i, 0);
       //   i++;
       // }
2317
       WASM_LOCAL_SET(i, WASM_I32V_1(0)),
2318
       WASM_LOOP(
2319
           WASM_ARRAY_SET(ListType, WASM_GLOBAL_GET(List), WASM_LOCAL_GET(i),
2320
                          WASM_STRUCT_NEW_WITH_RTT(SuperType, WASM_LOCAL_GET(i),
2321
                                                   WASM_GLOBAL_GET(RttSuper))),
2322
           WASM_LOCAL_SET(i, WASM_I32_ADD(WASM_LOCAL_GET(i), WASM_I32V_1(1))),
2323
           WASM_ARRAY_SET(ListType, WASM_GLOBAL_GET(List), WASM_LOCAL_GET(i),
2324
                          WASM_STRUCT_NEW_WITH_RTT(SubType, WASM_LOCAL_GET(i),
2325
                                                   WASM_I32V_1(0),
2326
                                                   WASM_GLOBAL_GET(RttSub))),
2327
           WASM_LOCAL_SET(i, WASM_I32_ADD(WASM_LOCAL_GET(i), WASM_I32V_1(1))),
2328
           WASM_BR_IF(0,
2329
                      WASM_I32_NE(WASM_LOCAL_GET(i), WASM_I32V(kListLength)))),
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
       // return 42;  // Dummy value, due to test framework.
       WASM_I32V_1(42), kExprEnd});

  const uint32_t sum = 1;  // Index of the local.
  const uint32_t list = 2;
  const uint32_t kLoops = 2;
  const uint32_t kIterations = kLoops * kListLength;
  const byte Main = tester.DefineFunction(
      tester.sigs.i_v(),
      {
          wasm::kWasmI32,
          wasm::kWasmI32,
          ValueType::Ref(ListType, kNullable),
      },
2344
      {WASM_LOCAL_SET(list, WASM_GLOBAL_GET(List)),
2345
       // sum = 0;
2346
       WASM_LOCAL_SET(sum, WASM_I32V_1(0)),
2347 2348 2349
       // for (int i = 0; i < kIterations; i++) {
       //   sum += ref.cast<super>(List[i & kListLength]).x
       // }
2350
       WASM_LOCAL_SET(i, WASM_I32V_1(0)),
2351
       WASM_LOOP(
2352
           WASM_LOCAL_SET(
2353
               sum, WASM_I32_ADD(
2354
                        WASM_LOCAL_GET(sum),
2355 2356 2357 2358
                        WASM_STRUCT_GET(
                            SuperType, 0,
                            WASM_REF_CAST(
                                WASM_ARRAY_GET(
2359 2360
                                    ListType, WASM_LOCAL_GET(list),
                                    WASM_I32_AND(WASM_LOCAL_GET(i),
2361
                                                 WASM_I32V(kListLength - 1))),
2362
                                WASM_GLOBAL_GET(RttSuper))))),
2363
           WASM_LOCAL_SET(i, WASM_I32_ADD(WASM_LOCAL_GET(i), WASM_I32V_1(1))),
2364
           WASM_BR_IF(0,
2365
                      WASM_I32_LTS(WASM_LOCAL_GET(i), WASM_I32V(kIterations)))),
2366
       // return sum;
2367
       WASM_LOCAL_GET(sum), kExprEnd});
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377

  tester.CompileModule();
  tester.CheckResult(Prepare, 42);

  // Time this section to get a benchmark for subtyping checks.
  // Note: if you bump kIterations or kListLength, you may have to take i32
  // overflow into account.
  tester.CheckResult(Main, (kListLength * (kListLength - 1) / 2) * kLoops);
}

2378 2379
WASM_COMPILED_EXEC_TEST(GlobalInitReferencingGlobal) {
  WasmGCTester tester(execution_tier);
2380 2381 2382 2383 2384
  const byte from = tester.AddGlobal(kWasmI32, false, WasmInitExpr(42));
  const byte to =
      tester.AddGlobal(kWasmI32, false, WasmInitExpr::GlobalGet(from));

  const byte func = tester.DefineFunction(tester.sigs.i_v(), {},
2385
                                          {WASM_GLOBAL_GET(to), kExprEnd});
2386 2387 2388 2389 2390 2391

  tester.CompileModule();

  tester.CheckResult(func, 42);
}

2392
WASM_COMPILED_EXEC_TEST(GCTables) {
2393
  WasmGCTester tester(execution_tier);
2394 2395

  byte super_struct = tester.DefineStruct({F(kWasmI32, false)});
2396 2397
  byte sub_struct = tester.DefineStruct({F(kWasmI32, false), F(kWasmI32, true)},
                                        super_struct);
2398 2399 2400 2401 2402
  FunctionSig* super_sig =
      FunctionSig::Build(tester.zone(), {kWasmI32}, {optref(sub_struct)});
  byte super_sig_index = tester.DefineSignature(super_sig);
  FunctionSig* sub_sig =
      FunctionSig::Build(tester.zone(), {kWasmI32}, {optref(super_struct)});
2403
  byte sub_sig_index = tester.DefineSignature(sub_sig, super_sig_index);
2404 2405 2406 2407

  tester.DefineTable(optref(super_sig_index), 10, 10);

  byte super_func = tester.DefineFunction(
2408
      super_sig_index, {},
2409 2410 2411 2412 2413
      {WASM_I32_ADD(WASM_STRUCT_GET(sub_struct, 0, WASM_LOCAL_GET(0)),
                    WASM_STRUCT_GET(sub_struct, 1, WASM_LOCAL_GET(0))),
       WASM_END});

  byte sub_func = tester.DefineFunction(
2414
      sub_sig_index, {},
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
      {WASM_STRUCT_GET(super_struct, 0, WASM_LOCAL_GET(0)), WASM_END});

  byte setup_func = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_TABLE_SET(0, WASM_I32V(0), WASM_REF_NULL(super_sig_index)),
       WASM_TABLE_SET(0, WASM_I32V(1), WASM_REF_FUNC(super_func)),
       WASM_TABLE_SET(0, WASM_I32V(2), WASM_REF_FUNC(sub_func)), WASM_I32V(0),
       WASM_END});

  byte super_struct_producer = tester.DefineFunction(
      FunctionSig::Build(tester.zone(), {ref(super_struct)}, {}), {},
      {WASM_STRUCT_NEW_WITH_RTT(super_struct, WASM_I32V(-5),
                                WASM_RTT_CANON(super_struct)),
       WASM_END});
  byte sub_struct_producer = tester.DefineFunction(
      FunctionSig::Build(tester.zone(), {ref(sub_struct)}, {}), {},
      {WASM_STRUCT_NEW_WITH_RTT(sub_struct, WASM_I32V(7), WASM_I32V(11),
                                WASM_RTT_CANON(sub_struct)),
       WASM_END});

  // Calling a null entry should trap.
  byte call_null = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_CALL_INDIRECT(super_sig_index,
                          WASM_CALL_FUNCTION0(sub_struct_producer),
                          WASM_I32V(0)),
       WASM_END});
  // Calling with a signature identical to the type of the table should work,
  // provided the entry has the same signature.
  byte call_same_type = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_CALL_INDIRECT(super_sig_index,
                          WASM_CALL_FUNCTION0(sub_struct_producer),
                          WASM_I32V(1)),
       WASM_END});
  // Calling with a signature that is a subtype of the type of the table should
  // work, provided the entry has the same signature.
  byte call_subtype = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_CALL_INDIRECT(sub_sig_index,
                          WASM_CALL_FUNCTION0(super_struct_producer),
                          WASM_I32V(2)),
       WASM_END});
  // Calling with a signature that is mismatched to that of the entry should
  // trap.
  byte call_type_mismatch = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_CALL_INDIRECT(super_sig_index,
                          WASM_CALL_FUNCTION0(sub_struct_producer),
                          WASM_I32V(2)),
       WASM_END});
  // Getting a table element and then calling it with call_ref should work.
  byte table_get_and_call_ref = tester.DefineFunction(
      tester.sigs.i_v(), {},
      {WASM_CALL_REF(WASM_TABLE_GET(0, WASM_I32V(2)),
                     WASM_CALL_FUNCTION0(sub_struct_producer)),
       WASM_END});

  // Only here so these functions count as "declared".
  tester.AddGlobal(optref(super_sig_index), false,
                   WasmInitExpr::RefFuncConst(super_func));
  tester.AddGlobal(optref(sub_sig_index), false,
                   WasmInitExpr::RefFuncConst(sub_func));
2478 2479 2480

  tester.CompileModule();

2481 2482 2483 2484 2485 2486
  tester.CheckResult(setup_func, 0);
  tester.CheckHasThrown(call_null);
  tester.CheckResult(call_same_type, 18);
  tester.CheckResult(call_subtype, -5);
  tester.CheckHasThrown(call_type_mismatch);
  tester.CheckResult(table_get_and_call_ref, 7);
2487 2488
}

2489 2490
WASM_COMPILED_EXEC_TEST(JsAccess) {
  WasmGCTester tester(execution_tier);
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
  const byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
  ValueType kRefType = ref(type_index);
  ValueType kSupertypeToI[] = {kWasmI32, kWasmDataRef};
  FunctionSig sig_t_v(1, 0, &kRefType);
  FunctionSig sig_super_v(1, 0, &kWasmDataRef);
  FunctionSig sig_i_super(1, 1, kSupertypeToI);

  tester.DefineExportedFunction(
      "disallowed", &sig_t_v,
      {WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42),
                                WASM_RTT_CANON(type_index)),
       kExprEnd});
  // Same code, different signature.
  tester.DefineExportedFunction(
      "producer", &sig_super_v,
      {WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42),
                                WASM_RTT_CANON(type_index)),
       kExprEnd});
  tester.DefineExportedFunction(
      "consumer", &sig_i_super,
2511 2512 2513
      {WASM_STRUCT_GET(
           type_index, 0,
           WASM_REF_CAST(WASM_LOCAL_GET(0), WASM_RTT_CANON(type_index))),
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
       kExprEnd});

  tester.CompileModule();
  Isolate* isolate = tester.isolate();
  TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
  MaybeHandle<Object> maybe_result =
      tester.CallExportedFunction("disallowed", 0, nullptr);
  CHECK(maybe_result.is_null());
  CHECK(try_catch.HasCaught());
  try_catch.Reset();
  isolate->clear_pending_exception();

  maybe_result = tester.CallExportedFunction("producer", 0, nullptr);
  if (maybe_result.is_null()) {
    FATAL("Calling 'producer' failed: %s",
          *v8::String::Utf8Value(reinterpret_cast<v8::Isolate*>(isolate),
                                 try_catch.Message()->Get()));
  }
  {
    Handle<Object> args[] = {maybe_result.ToHandleChecked()};
    maybe_result = tester.CallExportedFunction("consumer", 1, args);
  }
  if (maybe_result.is_null()) {
    FATAL("Calling 'consumer' failed: %s",
          *v8::String::Utf8Value(reinterpret_cast<v8::Isolate*>(isolate),
                                 try_catch.Message()->Get()));
  }
  Handle<Object> result = maybe_result.ToHandleChecked();
  CHECK(result->IsSmi());
  CHECK_EQ(42, Smi::cast(*result).value());
  // Calling {consumer} with any other object (e.g. the Smi we just got as
  // {result}) should trap.
  {
    Handle<Object> args[] = {result};
    maybe_result = tester.CallExportedFunction("consumer", 1, args);
2549
  }
2550 2551 2552 2553
  CHECK(maybe_result.is_null());
  CHECK(try_catch.HasCaught());
  try_catch.Reset();
  isolate->clear_pending_exception();
2554 2555
}

2556 2557 2558 2559
}  // namespace test_gc
}  // namespace wasm
}  // namespace internal
}  // namespace v8