test-gc.cc 56.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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>

#include "src/utils/utils.h"
#include "src/utils/vector.h"
#include "src/wasm/module-decoder.h"
#include "src/wasm/struct-types.h"
11
#include "src/wasm/wasm-arguments.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#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 {

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

class WasmGCTester {
 public:
33 34
  explicit WasmGCTester(
      TestExecutionTier execution_tier = TestExecutionTier::kTurbofan)
35
      : flag_gc(&v8::internal::FLAG_experimental_wasm_gc, true),
36
        flag_reftypes(&v8::internal::FLAG_experimental_wasm_reftypes, true),
37 38
        flag_typedfuns(&v8::internal::FLAG_experimental_wasm_typed_funcref,
                       true),
39 40 41 42 43 44
        flag_liftoff(
            &v8::internal::FLAG_liftoff,
            execution_tier == TestExecutionTier::kTurbofan ? false : true),
        flag_liftoff_only(
            &v8::internal::FLAG_liftoff_only,
            execution_tier == TestExecutionTier::kLiftoff ? true : false),
45
        flag_tierup(&v8::internal::FLAG_wasm_tier_up, false),
46
        zone(&allocator, ZONE_NAME),
47
        builder_(&zone),
48 49 50 51
        isolate_(CcTest::InitIsolateOnce()),
        scope(isolate_),
        thrower(isolate_, "Test wasm GC") {
    testing::SetupIsolateForWasmModule(isolate_);
52 53
  }

54
  byte AddGlobal(ValueType type, bool mutability, WasmInitExpr init) {
55
    return builder_.AddGlobal(type, mutability, std::move(init));
56 57
  }

58 59
  byte DefineFunction(FunctionSig* sig, std::initializer_list<ValueType> locals,
                      std::initializer_list<byte> code) {
60
    WasmFunctionBuilder* fun = builder_.AddFunction(sig);
61 62 63 64
    for (ValueType local : locals) {
      fun->AddLocal(local);
    }
    fun->EmitCode(code.begin(), static_cast<uint32_t>(code.size()));
65
    return fun->func_index();
66 67
  }

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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()));
    builder_.AddExport(CStrVector(name), fun);
  }

  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);
  }

84
  byte DefineStruct(std::initializer_list<F> fields) {
85 86 87 88 89
    StructType::Builder type_builder(&zone,
                                     static_cast<uint32_t>(fields.size()));
    for (F field : fields) {
      type_builder.AddField(field.first, field.second);
    }
90
    return builder_.AddStructType(type_builder.Build());
91 92
  }

93
  byte DefineArray(ValueType element_type, bool mutability) {
94
    return builder_.AddArrayType(zone.New<ArrayType>(element_type, mutability));
95 96
  }

97 98
  byte DefineSignature(FunctionSig* sig) { return builder_.AddSignature(sig); }

99 100 101 102
  byte DefineTable(ValueType type, uint32_t min_size, uint32_t max_size) {
    return builder_.AddTable(type, min_size, max_size);
  }

103 104
  void CompileModule() {
    ZoneBuffer buffer(&zone);
105
    builder_.WriteTo(&buffer);
106 107
    MaybeHandle<WasmInstanceObject> maybe_instance =
        testing::CompileAndInstantiateForTesting(
108
            isolate_, &thrower, ModuleWireBytes(buffer.begin(), buffer.end()));
109
    if (thrower.error()) FATAL("%s", thrower.error_msg());
110
    instance_ = maybe_instance.ToHandleChecked();
111 112
  }

113 114 115
  void CallFunctionImpl(uint32_t function_index, const FunctionSig* sig,
                        CWasmArgumentsPacker* packer) {
    WasmCodeRefScope scope;
116 117
    NativeModule* native_module = instance_->module_object().native_module();
    WasmCode* code = native_module->GetCode(function_index);
118 119
    Address wasm_call_target = code->instruction_start();
    Handle<Object> object_ref = instance_;
120 121
    Handle<Code> c_wasm_entry =
        compiler::CompileCWasmEntry(isolate_, sig, native_module->module());
122 123
    Execution::CallWasm(isolate_, c_wasm_entry, wasm_call_target, object_ref,
                        packer->argv());
124 125
  }

126 127 128 129 130
  void CheckResult(uint32_t function_index, int32_t expected) {
    FunctionSig* sig = sigs.i_v();
    DCHECK(*sig == *instance_->module()->functions[function_index].sig);
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    CallFunctionImpl(function_index, sig, &packer);
131
    CHECK(!isolate_->has_pending_exception());
132 133 134 135 136 137 138 139 140 141
    packer.Reset();
    CHECK_EQ(expected, packer.Pop<int32_t>());
  }

  void CheckResult(uint32_t function_index, int32_t expected, int32_t arg) {
    FunctionSig* sig = sigs.i_i();
    DCHECK(*sig == *instance_->module()->functions[function_index].sig);
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    packer.Push(arg);
    CallFunctionImpl(function_index, sig, &packer);
142
    CHECK(!isolate_->has_pending_exception());
143 144
    packer.Reset();
    CHECK_EQ(expected, packer.Pop<int32_t>());
145 146
  }

147 148 149 150
  MaybeHandle<Object> GetResultObject(uint32_t function_index) {
    const FunctionSig* sig = instance_->module()->functions[function_index].sig;
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    CallFunctionImpl(function_index, sig, &packer);
151
    CHECK(!isolate_->has_pending_exception());
152 153 154 155
    packer.Reset();
    return Handle<Object>(Object(packer.Pop<Address>()), isolate_);
  }

156 157 158 159 160 161 162 163
  void CheckHasThrown(uint32_t function_index) {
    const FunctionSig* sig = instance_->module()->functions[function_index].sig;
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    CallFunctionImpl(function_index, sig, &packer);
    CHECK(isolate_->has_pending_exception());
    isolate_->clear_pending_exception();
  }

164 165 166 167 168 169 170
  void CheckHasThrown(uint32_t function_index, int32_t arg) {
    FunctionSig* sig = sigs.i_i();
    DCHECK(*sig == *instance_->module()->functions[function_index].sig);
    CWasmArgumentsPacker packer(CWasmArgumentsPacker::TotalSize(sig));
    packer.Push(arg);
    CallFunctionImpl(function_index, sig, &packer);
    CHECK(isolate_->has_pending_exception());
171
    isolate_->clear_pending_exception();
172 173
  }

174
  Handle<WasmInstanceObject> instance() { return instance_; }
175
  Isolate* isolate() { return isolate_; }
176
  WasmModuleBuilder* builder() { return &builder_; }
177

178 179
  TestSignatures sigs;

180 181 182 183
 private:
  const FlagScope<bool> flag_gc;
  const FlagScope<bool> flag_reftypes;
  const FlagScope<bool> flag_typedfuns;
184 185
  const FlagScope<bool> flag_liftoff;
  const FlagScope<bool> flag_liftoff_only;
186
  const FlagScope<bool> flag_tierup;
187 188 189

  v8::internal::AccountingAllocator allocator;
  Zone zone;
190
  WasmModuleBuilder builder_;
191

192
  Isolate* const isolate_;
193
  const HandleScope scope;
194
  Handle<WasmInstanceObject> instance_;
195 196 197
  ErrorThrower thrower;
};

198
ValueType ref(uint32_t type_index) {
199
  return ValueType::Ref(type_index, kNonNullable);
200 201
}
ValueType optref(uint32_t type_index) {
202
  return ValueType::Ref(type_index, kNullable);
203 204
}

205 206 207
WASM_COMPILED_EXEC_TEST(WasmBasicStruct) {
  WasmGCTester tester(execution_tier);

208
  const byte type_index =
209
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
210 211 212
  const byte empty_struct_index = tester.DefineStruct({});
  ValueType kRefType = ref(type_index);
  ValueType kEmptyStructType = ref(empty_struct_index);
213
  ValueType kOptRefType = optref(type_index);
214 215
  FunctionSig sig_q_v(1, 0, &kRefType);
  FunctionSig sig_qe_v(1, 0, &kEmptyStructType);
216

217
  // Test struct.new and struct.get.
218
  const byte kGet1 = tester.DefineFunction(
219
      tester.sigs.i_v(), {},
220 221
      {WASM_STRUCT_GET(
           type_index, 0,
222 223
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                    WASM_RTT_CANON(type_index))),
224
       kExprEnd});
225

226
  // Test struct.new and struct.get.
227
  const byte kGet2 = tester.DefineFunction(
228
      tester.sigs.i_v(), {},
229 230
      {WASM_STRUCT_GET(
           type_index, 1,
231 232
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                    WASM_RTT_CANON(type_index))),
233
       kExprEnd});
234

235
  // Test struct.new, returning struct reference.
236
  const byte kGetStruct = tester.DefineFunction(
237
      &sig_q_v, {},
238 239 240
      {WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                WASM_RTT_CANON(type_index)),
       kExprEnd});
241

242 243 244 245 246 247 248
  // Test struct.new, returning reference to an empty struct.
  const byte kGetEmptyStruct = tester.DefineFunction(
      &sig_qe_v, {},
      {WASM_STRUCT_NEW_WITH_RTT(empty_struct_index,
                                WASM_RTT_CANON(empty_struct_index)),
       kExprEnd});

249
  // Test struct.set, struct refs types in locals.
250 251 252
  const byte j_local_index = 0;
  const byte j_field_index = 0;
  const byte kSet = tester.DefineFunction(
253
      tester.sigs.i_v(), {kOptRefType},
254
      {WASM_LOCAL_SET(
255 256 257
           j_local_index,
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(64),
                                    WASM_RTT_CANON(type_index))),
258
       WASM_STRUCT_SET(type_index, j_field_index, WASM_LOCAL_GET(j_local_index),
259 260
                       WASM_I32V(-99)),
       WASM_STRUCT_GET(type_index, j_field_index,
261
                       WASM_LOCAL_GET(j_local_index)),
262
       kExprEnd});
263

264 265
  tester.CompileModule();

266 267 268
  tester.CheckResult(kGet1, 42);
  tester.CheckResult(kGet2, 64);
  CHECK(tester.GetResultObject(kGetStruct).ToHandleChecked()->IsWasmStruct());
269 270 271
  CHECK(tester.GetResultObject(kGetEmptyStruct)
            .ToHandleChecked()
            ->IsWasmStruct());
272
  tester.CheckResult(kSet, -99);
273 274 275 276
}

// Test struct.set, ref.as_non_null,
// struct refs types in globals and if-results.
277 278
WASM_COMPILED_EXEC_TEST(WasmRefAsNonNull) {
  WasmGCTester tester(execution_tier);
279
  const byte type_index =
280 281 282 283 284
      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);

285
  const byte global_index =
286 287 288
      tester.AddGlobal(kOptRefType, true,
                       WasmInitExpr::RefNullConst(
                           static_cast<HeapType::Representation>(type_index)));
289 290
  const byte field_index = 0;
  const byte kFunc = tester.DefineFunction(
291
      tester.sigs.i_v(), {},
292
      {WASM_GLOBAL_SET(
293 294 295
           global_index,
           WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(55), WASM_I32V(66),
                                    WASM_RTT_CANON(type_index))),
296
       WASM_STRUCT_GET(
297
           type_index, field_index,
298
           WASM_REF_AS_NON_NULL(WASM_IF_ELSE_R(kOptRefType, WASM_I32V(1),
299
                                               WASM_GLOBAL_GET(global_index),
300
                                               WASM_REF_NULL(type_index)))),
301
       kExprEnd});
302

303
  tester.CompileModule();
304
  tester.CheckResult(kFunc, 55);
305 306
}

307 308
WASM_COMPILED_EXEC_TEST(WasmBrOnNull) {
  WasmGCTester tester(execution_tier);
309
  const byte type_index =
310 311 312 313
      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);
314 315
  const byte l_local_index = 0;
  const byte kTaken = tester.DefineFunction(
316
      tester.sigs.i_v(), {kOptRefType},
317 318 319
      {WASM_BLOCK_I(WASM_I32V(42),
                    // Branch will be taken.
                    // 42 left on stack outside the block (not 52).
320
                    WASM_BR_ON_NULL(0, WASM_LOCAL_GET(l_local_index)),
321 322
                    WASM_I32V(52), WASM_BR(0)),
       kExprEnd});
323

324 325
  const byte m_field_index = 0;
  const byte kNotTaken = tester.DefineFunction(
326
      tester.sigs.i_v(), {},
327 328 329 330 331 332
      {WASM_BLOCK_I(
           WASM_I32V(42),
           WASM_STRUCT_GET(
               type_index, m_field_index,
               // Branch will not be taken.
               // 52 left on stack outside the block (not 42).
333 334 335
               WASM_BR_ON_NULL(0, WASM_STRUCT_NEW_WITH_RTT(
                                      type_index, WASM_I32V(52), WASM_I32V(62),
                                      WASM_RTT_CANON(type_index)))),
336 337
           WASM_BR(0)),
       kExprEnd});
338

339
  tester.CompileModule();
340 341
  tester.CheckResult(kTaken, 42);
  tester.CheckResult(kNotTaken, 52);
342 343
}

344 345
WASM_COMPILED_EXEC_TEST(BrOnCast) {
  WasmGCTester tester(execution_tier);
346
  ValueType kDataRefNull = ValueType::Ref(HeapType::kData, kNullable);
347
  const byte type_index = tester.DefineStruct({F(kWasmI32, true)});
348
  const byte other_type_index = tester.DefineStruct({F(kWasmF32, true)});
349
  const byte rtt_index =
350
      tester.AddGlobal(ValueType::Rtt(type_index, 0), false,
351 352
                       WasmInitExpr::RttCanon(
                           static_cast<HeapType::Representation>(type_index)));
353
  const byte kTestStruct = tester.DefineFunction(
354
      tester.sigs.i_v(), {kWasmI32, kDataRefNull},
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      {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});
379

380
  const byte kTestNull = tester.DefineFunction(
381
      tester.sigs.i_v(), {kWasmI32, kDataRefNull},
382 383 384 385 386 387 388 389 390
      {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});
391

392
  const byte kTypedAfterBranch = tester.DefineFunction(
393
      tester.sigs.i_v(), {kWasmI32, kDataRefNull},
394
      {WASM_LOCAL_SET(1, WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42),
395
                                                  WASM_GLOBAL_GET(rtt_index))),
396 397 398 399 400 401 402 403
       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)),
404 405
           // The outer block catches the struct left behind by the inner block
           // and reads its field.
406 407
           WASM_GC_OP(kExprStructGet), type_index, 0),
       kExprEnd});
408 409 410

  tester.CompileModule();
  tester.CheckResult(kTestStruct, 222);
411
  tester.CheckResult(kTestNull, 222);
412 413 414
  tester.CheckResult(kTypedAfterBranch, 42);
}

415 416
WASM_COMPILED_EXEC_TEST(WasmRefEq) {
  WasmGCTester tester(execution_tier);
417
  byte type_index = tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
418 419 420 421
  ValueType kRefTypes[] = {ref(type_index)};
  ValueType kOptRefType = optref(type_index);
  FunctionSig sig_q_v(1, 0, kRefTypes);

422
  byte local_index = 0;
423
  const byte kFunc = tester.DefineFunction(
424
      tester.sigs.i_v(), {kOptRefType},
425
      {WASM_LOCAL_SET(local_index, WASM_STRUCT_NEW_WITH_RTT(
426 427
                                       type_index, WASM_I32V(55), WASM_I32V(66),
                                       WASM_RTT_CANON(type_index))),
428
       WASM_I32_ADD(
429 430
           WASM_I32_SHL(
               WASM_REF_EQ(  // true
431
                   WASM_LOCAL_GET(local_index), WASM_LOCAL_GET(local_index)),
432
               WASM_I32V(0)),
433 434
           WASM_I32_ADD(
               WASM_I32_SHL(WASM_REF_EQ(  // false
435
                                WASM_LOCAL_GET(local_index),
436 437 438
                                WASM_STRUCT_NEW_WITH_RTT(
                                    type_index, WASM_I32V(55), WASM_I32V(66),
                                    WASM_RTT_CANON(type_index))),
439 440
                            WASM_I32V(1)),
               WASM_I32_ADD(WASM_I32_SHL(  // false
441
                                WASM_REF_EQ(WASM_LOCAL_GET(local_index),
442
                                            WASM_REF_NULL(type_index)),
443 444
                                WASM_I32V(2)),
                            WASM_I32_SHL(WASM_REF_EQ(  // true
445 446
                                             WASM_REF_NULL(type_index),
                                             WASM_REF_NULL(type_index)),
447 448
                                         WASM_I32V(3))))),
       kExprEnd});
449

450
  tester.CompileModule();
451
  tester.CheckResult(kFunc, 0b1001);
452 453
}

454 455
WASM_COMPILED_EXEC_TEST(WasmPackedStructU) {
  WasmGCTester tester(execution_tier);
456

457
  const byte type_index = tester.DefineStruct(
458
      {F(kWasmI8, true), F(kWasmI16, true), F(kWasmI32, true)});
459
  ValueType struct_type = optref(type_index);
460

461
  const byte local_index = 0;
462 463 464 465

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

466
  const byte kF0 = tester.DefineFunction(
467
      tester.sigs.i_v(), {struct_type},
468
      {WASM_LOCAL_SET(local_index,
469 470 471 472
                      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))),
473
       WASM_STRUCT_GET_U(type_index, 0, WASM_LOCAL_GET(local_index)),
474 475
       kExprEnd});

476
  const byte kF1 = tester.DefineFunction(
477
      tester.sigs.i_v(), {struct_type},
478
      {WASM_LOCAL_SET(local_index,
479 480 481 482
                      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))),
483
       WASM_STRUCT_GET_U(type_index, 1, WASM_LOCAL_GET(local_index)),
484 485 486
       kExprEnd});
  tester.CompileModule();

487 488
  tester.CheckResult(kF0, static_cast<uint8_t>(expected_output_0));
  tester.CheckResult(kF1, static_cast<uint16_t>(expected_output_1));
489 490
}

491 492
WASM_COMPILED_EXEC_TEST(WasmPackedStructS) {
  WasmGCTester tester(execution_tier);
493

494
  const byte type_index = tester.DefineStruct(
495
      {F(kWasmI8, true), F(kWasmI16, true), F(kWasmI32, true)});
496
  ValueType struct_type = optref(type_index);
497

498
  const byte local_index = 0;
499 500 501 502

  int32_t expected_output_0 = 0x80;
  int32_t expected_output_1 = 42;

503
  const byte kF0 = tester.DefineFunction(
504
      tester.sigs.i_v(), {struct_type},
505
      {WASM_LOCAL_SET(
506
           local_index,
507 508 509
           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))),
510
       WASM_STRUCT_GET_S(type_index, 0, WASM_LOCAL_GET(local_index)),
511 512
       kExprEnd});

513
  const byte kF1 = tester.DefineFunction(
514
      tester.sigs.i_v(), {struct_type},
515
      {WASM_LOCAL_SET(
516 517 518 519
           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))),
520
       WASM_STRUCT_GET_S(type_index, 1, WASM_LOCAL_GET(local_index)),
521 522 523 524
       kExprEnd});

  tester.CompileModule();

525 526
  tester.CheckResult(kF0, static_cast<int8_t>(expected_output_0));
  tester.CheckResult(kF1, static_cast<int16_t>(expected_output_1));
527 528
}

529 530
TEST(WasmLetInstruction) {
  WasmGCTester tester;
531
  const byte type_index =
532
      tester.DefineStruct({F(kWasmI32, true), F(kWasmI32, true)});
533

534 535 536
  const byte let_local_index = 0;
  const byte let_field_index = 0;
  const byte kLetTest1 = tester.DefineFunction(
537
      tester.sigs.i_v(), {},
538
      {WASM_LET_1_I(
539
           WASM_SEQ(kRefCode, type_index),
540 541 542
           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,
543
                           WASM_LOCAL_GET(let_local_index))),
544 545
       kExprEnd});

546 547
  const byte let_2_field_index = 0;
  const byte kLetTest2 = tester.DefineFunction(
548
      tester.sigs.i_v(), {},
549
      {WASM_LET_2_I(
550 551
           kI32Code, WASM_I32_ADD(WASM_I32V(42), WASM_I32V(-32)),
           WASM_SEQ(kRefCode, type_index),
552 553 554
           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,
555 556
                                        WASM_LOCAL_GET(1)),
                        WASM_LOCAL_GET(0))),
557 558
       kExprEnd});

559
  const byte kLetTestLocals = tester.DefineFunction(
560
      tester.sigs.i_i(), {kWasmI32},
561
      {WASM_LOCAL_SET(1, WASM_I32V(100)),
562
       WASM_LET_2_I(
563
           kI32Code, WASM_I32V(1), kI32Code, WASM_I32V(10),
564 565 566 567
           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
568
       kExprEnd});
569
  // Result: (1 + 1000) - (10 + 100) = 891
570

571 572
  const byte let_erase_local_index = 0;
  const byte kLetTestErase = tester.DefineFunction(
573
      tester.sigs.i_v(), {kWasmI32},
574
      {WASM_LOCAL_SET(let_erase_local_index, WASM_I32V(0)),
575
       WASM_LET_1_V(kI32Code, WASM_I32V(1), WASM_NOP),
576
       WASM_LOCAL_GET(let_erase_local_index), kExprEnd});
577 578 579
  // The result should be 0 and not 1, as local_get(0) refers to the original
  // local.

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
  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});

596
  tester.CompileModule();
597

598 599 600 601
  tester.CheckResult(kLetTest1, 42);
  tester.CheckResult(kLetTest2, 420);
  tester.CheckResult(kLetTestLocals, 891, 1000);
  tester.CheckResult(kLetTestErase, 0);
602 603 604 605
  tester.CheckResult(kLetInLoop, 2, 52);
  tester.CheckResult(kLetInLoop, -11, -1);
  tester.CheckResult(kLetInBlock, 15, 15);
  tester.CheckResult(kLetInBlock, 30, 5);
606 607
}

608 609 610
WASM_COMPILED_EXEC_TEST(WasmBasicArray) {
  WasmGCTester tester(execution_tier);

611
  const byte type_index = tester.DefineArray(wasm::kWasmI32, true);
612
  ValueType kRefTypes[] = {ref(type_index)};
613
  FunctionSig sig_q_v(1, 0, kRefTypes);
614
  ValueType kOptRefType = optref(type_index);
615 616

  // f: a = [12, 12, 12]; a[1] = 42; return a[arg0]
617 618
  const byte local_index = 1;
  const byte kGetElem = tester.DefineFunction(
619
      tester.sigs.i_i(), {kOptRefType},
620
      {WASM_LOCAL_SET(local_index, WASM_ARRAY_NEW_WITH_RTT(
621 622
                                       type_index, WASM_I32V(12), WASM_I32V(3),
                                       WASM_RTT_CANON(type_index))),
623
       WASM_ARRAY_SET(type_index, WASM_LOCAL_GET(local_index), WASM_I32V(1),
624
                      WASM_I32V(42)),
625 626
       WASM_ARRAY_GET(type_index, WASM_LOCAL_GET(local_index),
                      WASM_LOCAL_GET(0)),
627
       kExprEnd});
628

629
  // Reads and returns an array's length.
630
  const byte kGetLength = tester.DefineFunction(
631
      tester.sigs.i_v(), {},
632 633 634
      {WASM_ARRAY_LEN(type_index, WASM_ARRAY_NEW_WITH_RTT(
                                      type_index, WASM_I32V(0), WASM_I32V(42),
                                      WASM_RTT_CANON(type_index))),
635 636
       kExprEnd});

637
  // Create an array of length 2, initialized to [42, 42].
638
  const byte kAllocate = tester.DefineFunction(
639
      &sig_q_v, {},
640 641 642
      {WASM_ARRAY_NEW_WITH_RTT(type_index, WASM_I32V(42), WASM_I32V(2),
                               WASM_RTT_CANON(type_index)),
       kExprEnd});
643

644
  const uint32_t kLongLength = 1u << 16;
645 646 647 648 649 650 651 652 653 654 655 656 657
  const byte kAllocateLarge = tester.DefineFunction(
      &sig_q_v, {},
      {WASM_ARRAY_NEW_DEFAULT(type_index, WASM_I32V(kLongLength),
                              WASM_RTT_CANON(type_index)),
       kExprEnd});

  const uint32_t kTooLong = kV8MaxWasmArrayLength + 1;
  const byte kAllocateTooLarge = tester.DefineFunction(
      &sig_q_v, {},
      {WASM_ARRAY_NEW_DEFAULT(type_index, WASM_I32V(kTooLong),
                              WASM_RTT_CANON(type_index)),
       kExprEnd});

658
  tester.CompileModule();
659

660 661 662 663 664 665
  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);
666

667
  MaybeHandle<Object> h_result = tester.GetResultObject(kAllocate);
668
  CHECK(h_result.ToHandleChecked()->IsWasmArray());
669
#if OBJECT_PRINT
670
  h_result.ToHandleChecked()->Print();
671
#endif
672 673 674 675 676 677 678 679 680

  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);

  tester.CheckHasThrown(kAllocateTooLarge);
681 682
}

683 684
WASM_COMPILED_EXEC_TEST(WasmPackedArrayU) {
  WasmGCTester tester(execution_tier);
685
  const byte array_index = tester.DefineArray(kWasmI8, true);
686
  ValueType array_type = optref(array_index);
687

688 689
  const byte param_index = 0;
  const byte local_index = 1;
690 691 692

  int32_t expected_output_3 = 258;

693
  const byte kF = tester.DefineFunction(
694
      tester.sigs.i_i(), {array_type},
695
      {WASM_LOCAL_SET(local_index, WASM_ARRAY_NEW_WITH_RTT(
696 697
                                       array_index, WASM_I32V(0), WASM_I32V(4),
                                       WASM_RTT_CANON(array_index))),
698
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(0),
699
                      WASM_I32V(1)),
700
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(1),
701
                      WASM_I32V(10)),
702
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(2),
703
                      WASM_I32V(200)),
704
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(3),
705
                      WASM_I32V(expected_output_3)),
706 707
       WASM_ARRAY_GET_U(array_index, WASM_LOCAL_GET(local_index),
                        WASM_LOCAL_GET(param_index)),
708 709 710
       kExprEnd});

  tester.CompileModule();
711 712 713
  tester.CheckResult(kF, 1, 0);
  tester.CheckResult(kF, 10, 1);
  tester.CheckResult(kF, 200, 2);
714
  // Only the 2 lsb's of 258 should be stored in the array.
715
  tester.CheckResult(kF, static_cast<uint8_t>(expected_output_3), 3);
716 717
}

718 719
WASM_COMPILED_EXEC_TEST(WasmPackedArrayS) {
  WasmGCTester tester(execution_tier);
720
  const byte array_index = tester.DefineArray(kWasmI16, true);
721
  ValueType array_type = optref(array_index);
722 723 724

  int32_t expected_outputs[] = {0x12345678, 10, 0xFEDC, 0xFF1234};

725 726 727
  const byte param_index = 0;
  const byte local_index = 1;
  const byte kF = tester.DefineFunction(
728
      tester.sigs.i_i(), {array_type},
729
      {WASM_LOCAL_SET(
730
           local_index,
731 732
           WASM_ARRAY_NEW_WITH_RTT(array_index, WASM_I32V(0x12345678),
                                   WASM_I32V(4), WASM_RTT_CANON(array_index))),
733
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(1),
734
                      WASM_I32V(10)),
735
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(2),
736
                      WASM_I32V(0xFEDC)),
737
       WASM_ARRAY_SET(array_index, WASM_LOCAL_GET(local_index), WASM_I32V(3),
738
                      WASM_I32V(0xFF1234)),
739 740
       WASM_ARRAY_GET_S(array_index, WASM_LOCAL_GET(local_index),
                        WASM_LOCAL_GET(param_index)),
741 742 743 744
       kExprEnd});

  tester.CompileModule();
  // Exactly the 2 lsb's should be stored by array.new.
745 746
  tester.CheckResult(kF, static_cast<int16_t>(expected_outputs[0]), 0);
  tester.CheckResult(kF, static_cast<int16_t>(expected_outputs[1]), 1);
747
  // Sign should be extended.
748
  tester.CheckResult(kF, static_cast<int16_t>(expected_outputs[2]), 2);
749
  // Exactly the 2 lsb's should be stored by array.set.
750
  tester.CheckResult(kF, static_cast<int16_t>(expected_outputs[3]), 3);
751 752
}

753 754
WASM_COMPILED_EXEC_TEST(NewDefault) {
  WasmGCTester tester(execution_tier);
755 756 757 758 759 760
  const byte struct_type = tester.DefineStruct(
      {F(wasm::kWasmI32, true), F(wasm::kWasmF64, true), F(optref(0), true)});
  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)},
761
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_DEFAULT(struct_type,
762 763
                                                 WASM_RTT_CANON(struct_type))),
       WASM_I32_ADD(
764
           WASM_I32_ADD(WASM_STRUCT_GET(struct_type, 0, WASM_LOCAL_GET(0)),
765
                        WASM_I32_SCONVERT_F64(WASM_STRUCT_GET(
766
                            struct_type, 1, WASM_LOCAL_GET(0)))),
767
           WASM_I32_XOR(WASM_REF_IS_NULL(
768
                            WASM_STRUCT_GET(struct_type, 2, WASM_LOCAL_GET(0))),
769 770 771 772
                        WASM_I32V(1))),
       kExprEnd});
  const byte allocate_array = tester.DefineFunction(
      tester.sigs.i_v(), {optref(array_type)},
773
      {WASM_LOCAL_SET(0, WASM_ARRAY_NEW_DEFAULT(array_type, WASM_I32V(2),
774 775
                                                WASM_RTT_CANON(array_type))),
       WASM_I32_ADD(
776 777
           WASM_ARRAY_GET(array_type, WASM_LOCAL_GET(0), WASM_I32V(0)),
           WASM_ARRAY_GET(array_type, WASM_LOCAL_GET(0), WASM_I32V(1))),
778 779 780 781 782 783 784 785
       kExprEnd});

  tester.CompileModule();

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

786 787
WASM_COMPILED_EXEC_TEST(BasicRtt) {
  WasmGCTester tester(execution_tier);
788

789 790
  const byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
  const byte subtype_index =
791
      tester.DefineStruct({F(wasm::kWasmI32, true), F(wasm::kWasmI32, true)});
792

793
  ValueType kRttTypes[] = {ValueType::Rtt(type_index, 0)};
794
  FunctionSig sig_t_v(1, 0, kRttTypes);
795
  ValueType kRttSubtypes[] = {ValueType::Rtt(subtype_index, 1)};
796
  FunctionSig sig_t2_v(1, 0, kRttSubtypes);
797
  ValueType kRttTypesDeeper[] = {ValueType::Rtt(type_index, 1)};
798
  FunctionSig sig_t3_v(1, 0, kRttTypesDeeper);
799
  ValueType kRefTypes[] = {ref(type_index)};
800
  FunctionSig sig_q_v(1, 0, kRefTypes);
801

802
  const byte kRttCanon = tester.DefineFunction(
803
      &sig_t_v, {}, {WASM_RTT_CANON(type_index), kExprEnd});
804
  const byte kRttSub = tester.DefineFunction(
805
      &sig_t2_v, {},
806
      {WASM_RTT_SUB(subtype_index, WASM_RTT_CANON(type_index)), kExprEnd});
807
  const byte kStructWithRtt = tester.DefineFunction(
808 809 810 811
      &sig_q_v, {},
      {WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42),
                                WASM_RTT_CANON(type_index)),
       kExprEnd});
812

813
  const int kFieldIndex = 1;
814
  const int kStructIndexCode = 0;
815 816
  // This implements the following function:
  //   var local_struct: type0;
817 818 819
  //   local_struct = new type1 with rtt 'kRttSub()';
  //   return (ref.test local_struct kRttSub()) +
  //          ((ref.cast local_struct kRttSub())[field0]);
820 821
  //   }
  // The expected return value is 1+42 = 43.
822
  const byte kRefCast = tester.DefineFunction(
823
      tester.sigs.i_v(), {optref(type_index)},
824 825 826 827 828 829 830 831 832 833
      {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)))),
834
       kExprEnd});
835 836 837

  tester.CompileModule();

838 839
  Handle<Object> ref_result =
      tester.GetResultObject(kRttCanon).ToHandleChecked();
840 841 842 843 844 845 846

  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());
847

848 849
  Handle<Object> subref_result =
      tester.GetResultObject(kRttSub).ToHandleChecked();
850 851 852 853 854
  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());
855 856 857 858
  Handle<Object> subref_result_canonicalized =
      tester.GetResultObject(kRttSub).ToHandleChecked();
  CHECK(subref_result.is_identical_to(subref_result_canonicalized));

859
  Handle<Object> s = tester.GetResultObject(kStructWithRtt).ToHandleChecked();
860 861
  CHECK(s->IsWasmStruct());
  CHECK_EQ(Handle<WasmStruct>::cast(s)->map(), *map);
862 863

  tester.CheckResult(kRefCast, 43);
864 865
}

866 867
WASM_EXEC_TEST(NoDepthRtt) {
  WasmGCTester tester(execution_tier);
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887

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

  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 kRttSubtypeSub = tester.DefineFunction(
      &sig_t2_v_nd, {},
      {WASM_RTT_SUB(subtype_index, WASM_RTT_CANON(type_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))),
888
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION0(kRttSubtypeCanon)),
889 890 891 892 893 894 895 896
       kExprEnd});

  const byte kTestSub = 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_SUB(subtype_index, WASM_RTT_CANON(type_index)))),
897
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION0(kRttSubtypeSub)),
898 899 900 901 902 903 904 905
       kExprEnd});

  const byte kTestSubVsEmpty = 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_SUB(subtype_index,
                                          WASM_RTT_CANON(empty_struct_index)))),
906
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION0(kRttSubtypeSub)),
907 908 909 910 911 912 913
       kExprEnd});

  const byte kTestSubVsCanon = 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))),
914
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION0(kRttSubtypeSub)),
915 916 917 918 919 920 921 922
       kExprEnd});

  const byte kTestCanonVsSub = 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_SUB(subtype_index, WASM_RTT_CANON(type_index)))),
923
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION0(kRttSubtypeCanon)),
924 925 926 927 928 929
       kExprEnd});

  const byte kTestSuperVsSub = tester.DefineFunction(
      tester.sigs.i_v(), {optref(type_index)},
      {WASM_LOCAL_SET(0, WASM_STRUCT_NEW_WITH_RTT(type_index, WASM_I32V(42),
                                                  WASM_RTT_CANON(type_index))),
930
       WASM_REF_TEST(WASM_LOCAL_GET(0), WASM_CALL_FUNCTION0(kRttSubtypeCanon)),
931 932 933 934 935 936 937 938 939 940 941 942
       kExprEnd});

  tester.CompileModule();

  tester.CheckResult(kTestCanon, 1);
  tester.CheckResult(kTestSub, 1);
  tester.CheckResult(kTestSubVsEmpty, 0);
  tester.CheckResult(kTestSubVsCanon, 0);
  tester.CheckResult(kTestCanonVsSub, 0);
  tester.CheckResult(kTestSuperVsSub, 0);
}

943 944 945
WASM_COMPILED_EXEC_TEST(ArrayNewMap) {
  WasmGCTester tester(execution_tier);

946
  const byte type_index = tester.DefineArray(kWasmI32, true);
947 948 949

  ValueType array_type = ValueType::Ref(type_index, kNonNullable);
  FunctionSig sig(1, 0, &array_type);
950
  const byte array_new_with_rtt = tester.DefineFunction(
951 952 953 954 955
      &sig, {},
      {WASM_ARRAY_NEW_WITH_RTT(type_index, WASM_I32V(10), WASM_I32V(42),
                               WASM_RTT_CANON(type_index)),
       kExprEnd});

956
  ValueType rtt_type = ValueType::Rtt(type_index, 0);
957
  FunctionSig rtt_canon_sig(1, 0, &rtt_type);
958
  const byte kRttCanon = tester.DefineFunction(
959 960 961 962 963 964 965 966 967 968 969 970
      &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);
}

971 972
WASM_COMPILED_EXEC_TEST(FunctionRefs) {
  WasmGCTester tester(execution_tier);
973 974 975 976 977 978 979 980 981 982
  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));

983
  ValueType func_type = ValueType::Ref(sig_index, kNullable);
984 985
  FunctionSig sig_func(1, 0, &func_type);

986 987
  ValueType rtt0 = ValueType::Rtt(sig_index, 0);
  FunctionSig sig_rtt0(1, 0, &rtt0);
988
  const byte rtt_canon = tester.DefineFunction(
989
      &sig_rtt0, {}, {WASM_RTT_CANON(sig_index), kExprEnd});
990 991 992

  const byte cast = tester.DefineFunction(
      &sig_func, {kWasmFuncRef},
993
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(func_index)),
994
       WASM_REF_CAST(WASM_LOCAL_GET(0), WASM_RTT_CANON(sig_index)), kExprEnd});
995 996 997 998

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

999
  const byte test = tester.DefineFunction(
1000 1001 1002 1003 1004
      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});

  const byte test_fail_1 = tester.DefineFunction(
1005 1006 1007 1008
      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});
1009

1010 1011 1012 1013 1014 1015 1016
  const byte test_fail_2 = tester.DefineFunction(
      tester.sigs.i_v(), {kWasmFuncRef},
      {WASM_LOCAL_SET(0, WASM_REF_FUNC(func_index)),
       WASM_REF_TEST(WASM_LOCAL_GET(0),
                     WASM_RTT_SUB(sig_index, WASM_RTT_CANON(sig_index))),
       kExprEnd});

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  tester.CompileModule();

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

  Handle<Object> result_cast = tester.GetResultObject(cast).ToHandleChecked();
  CHECK(result_cast->IsJSFunction());
  Handle<JSFunction> cast_function = Handle<JSFunction>::cast(result_cast);

  Handle<Object> result_cast_reference =
      tester.GetResultObject(cast_reference).ToHandleChecked();
  CHECK(result_cast_reference->IsJSFunction());
  Handle<JSFunction> cast_function_reference =
      Handle<JSFunction>::cast(result_cast_reference);

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

1038 1039 1040
  tester.CheckResult(test, 1);
  tester.CheckResult(test_fail_1, 0);
  tester.CheckResult(test_fail_2, 0);
1041 1042
}

1043 1044
WASM_COMPILED_EXEC_TEST(CallRef) {
  WasmGCTester tester(execution_tier);
1045 1046
  byte callee = tester.DefineFunction(
      tester.sigs.i_ii(), {},
1047
      {WASM_I32_ADD(WASM_LOCAL_GET(0), WASM_LOCAL_GET(1)), kExprEnd});
1048 1049
  byte caller = tester.DefineFunction(
      tester.sigs.i_i(), {},
1050
      {WASM_CALL_REF(WASM_REF_FUNC(callee), WASM_I32V(42), WASM_LOCAL_GET(0)),
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
       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);
}

1062 1063
WASM_COMPILED_EXEC_TEST(RefTestCastNull) {
  WasmGCTester tester(execution_tier);
1064
  byte type_index = tester.DefineStruct({F(wasm::kWasmI32, true)});
1065

1066
  const byte kRefTestNull = tester.DefineFunction(
1067
      tester.sigs.i_v(), {},
1068
      {WASM_REF_TEST(WASM_REF_NULL(type_index), WASM_RTT_CANON(type_index)),
1069 1070
       kExprEnd});

1071
  const byte kRefCastNull = tester.DefineFunction(
1072
      tester.sigs.i_v(), {},
1073
      {WASM_REF_IS_NULL(WASM_REF_CAST(WASM_REF_NULL(type_index),
1074 1075
                                      WASM_RTT_CANON(type_index))),
       kExprEnd});
1076 1077
  tester.CompileModule();
  tester.CheckResult(kRefTestNull, 0);
1078
  tester.CheckResult(kRefCastNull, 1);
1079 1080
}

1081 1082
WASM_COMPILED_EXEC_TEST(AbstractTypeChecks) {
  WasmGCTester tester(execution_tier);
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

  byte array_index = tester.DefineArray(kWasmI32, true);
  byte function_index =
      tester.DefineFunction(tester.sigs.v_v(), {}, {kExprEnd});
  byte sig_index = 1;

  // 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});
  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});
  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})

  byte kDataCheckSuccess =
      TYPE_CHECK(DATA, WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array_index)));
  byte kDataCheckFailure = TYPE_CHECK(DATA, WASM_I31_NEW(WASM_I32V(42)));
  byte kFuncCheckSuccess = TYPE_CHECK(FUNC, WASM_REF_FUNC(function_index));
  byte kFuncCheckFailure =
      TYPE_CHECK(FUNC, WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                              WASM_RTT_CANON(array_index)));
  byte kI31CheckSuccess = TYPE_CHECK(I31, WASM_I31_NEW(WASM_I32V(42)));
  byte kI31CheckFailure =
      TYPE_CHECK(I31, WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                             WASM_RTT_CANON(array_index)));
#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})

  byte kDataCastSuccess =
      TYPE_CAST(DATA, WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                             WASM_RTT_CANON(array_index)));
  byte kDataCastFailure = TYPE_CAST(DATA, WASM_I31_NEW(WASM_I32V(42)));
  byte kFuncCastSuccess = TYPE_CAST(FUNC, WASM_REF_FUNC(function_index));
  byte kFuncCastFailure =
      TYPE_CAST(FUNC, WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                             WASM_RTT_CANON(array_index)));
  byte kI31CastSuccess = TYPE_CAST(I31, WASM_I31_NEW(WASM_I32V(42)));
  byte kI31CastFailure =
      TYPE_CAST(I31, WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                            WASM_RTT_CANON(array_index)));
#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.
#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_BR_ON_##TYPE(0, WASM_LOCAL_GET(0)), \
           WASM_RETURN(WASM_I32V(0)))),                               \
       kExprEnd})

  byte kBrOnDataTaken =
      BR_ON(DATA, Data,
            WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                   WASM_RTT_CANON(array_index)));
  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)));
  byte kBrOnI31Taken = BR_ON(I31, I31, WASM_I31_NEW(WASM_I32V(42)));
  byte kBrOnI31NotTaken =
      BR_ON(I31, I31,
            WASM_ARRAY_NEW_DEFAULT(array_index, WASM_I32V(10),
                                   WASM_RTT_CANON(array_index)));
#undef BR_ON

  tester.CompileModule();

  tester.CheckResult(kDataCheckNull, 0);
  tester.CheckHasThrown(kDataCastNull);
  tester.CheckResult(kDataCheckSuccess, 1);
  tester.CheckResult(kDataCheckFailure, 0);
  tester.CheckResult(kDataCastSuccess, 1);
  tester.CheckHasThrown(kDataCastFailure);
  tester.CheckResult(kBrOnDataTaken, 1);
  tester.CheckResult(kBrOnDataNotTaken, 0);

  tester.CheckResult(kFuncCheckNull, 0);
  tester.CheckHasThrown(kFuncCastNull);
  tester.CheckResult(kFuncCheckSuccess, 1);
  tester.CheckResult(kFuncCheckFailure, 0);
  tester.CheckResult(kFuncCastSuccess, 1);
  tester.CheckHasThrown(kFuncCastFailure);
  tester.CheckResult(kBrOnFuncTaken, 1);
  tester.CheckResult(kBrOnFuncNotTaken, 0);

  tester.CheckResult(kI31CheckNull, 0);
  tester.CheckHasThrown(kI31CastNull);
  tester.CheckResult(kI31CheckSuccess, 1);
  tester.CheckResult(kI31CheckFailure, 0);
  tester.CheckResult(kI31CastSuccess, 1);
  tester.CheckHasThrown(kI31CastFailure);
  tester.CheckResult(kBrOnI31Taken, 1);
  tester.CheckResult(kBrOnI31NotTaken, 0);
}

1210 1211
WASM_COMPILED_EXEC_TEST(BasicI31) {
  WasmGCTester tester(execution_tier);
1212
  const byte kSigned = tester.DefineFunction(
1213
      tester.sigs.i_i(), {},
1214
      {WASM_I31_GET_S(WASM_I31_NEW(WASM_LOCAL_GET(0))), kExprEnd});
1215
  const byte kUnsigned = tester.DefineFunction(
1216
      tester.sigs.i_i(), {},
1217
      {WASM_I31_GET_U(WASM_I31_NEW(WASM_LOCAL_GET(0))), kExprEnd});
1218
  tester.CompileModule();
1219 1220
  tester.CheckResult(kSigned, 123, 123);
  tester.CheckResult(kUnsigned, 123, 123);
1221
  // Truncation:
1222 1223
  tester.CheckResult(kSigned, 0x1234, static_cast<int32_t>(0x80001234));
  tester.CheckResult(kUnsigned, 0x1234, static_cast<int32_t>(0x80001234));
1224
  // Sign/zero extension:
1225 1226 1227 1228
  tester.CheckResult(kSigned, -1, 0x7FFFFFFF);
  tester.CheckResult(kUnsigned, 0x7FFFFFFF, 0x7FFFFFFF);
}

1229 1230
// 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.
1231 1232
WASM_COMPILED_EXEC_TEST(CastsBenchmark) {
  WasmGCTester tester(execution_tier);
1233 1234 1235
  const byte SuperType = tester.DefineStruct({F(wasm::kWasmI32, true)});
  const byte SubType =
      tester.DefineStruct({F(wasm::kWasmI32, true), F(wasm::kWasmI32, true)});
1236 1237 1238

  ValueType kDataRefNull = ValueType::Ref(HeapType::kData, kNullable);
  const byte ListType = tester.DefineArray(kDataRefNull, true);
1239 1240 1241 1242 1243 1244

  const byte List =
      tester.AddGlobal(ValueType::Ref(ListType, kNullable), true,
                       WasmInitExpr::RefNullConst(
                           static_cast<HeapType::Representation>(ListType)));
  const byte RttSuper = tester.AddGlobal(
1245
      ValueType::Rtt(SuperType, 0), false,
1246 1247
      WasmInitExpr::RttCanon(static_cast<HeapType::Representation>(SuperType)));
  const byte RttSub = tester.AddGlobal(
1248
      ValueType::Rtt(SubType, 1), false,
1249 1250 1251
      WasmInitExpr::RttSub(static_cast<HeapType::Representation>(SubType),
                           WasmInitExpr::GlobalGet(RttSuper)));
  const byte RttList = tester.AddGlobal(
1252
      ValueType::Rtt(ListType, 0), false,
1253 1254 1255 1256 1257 1258 1259
      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];
1260
       WASM_GLOBAL_SET(List,
1261
                       WASM_ARRAY_NEW_DEFAULT(ListType, WASM_I32V(kListLength),
1262
                                              WASM_GLOBAL_GET(RttList))),
1263 1264 1265 1266 1267 1268
       // for (int i = 0; i < kListLength; ) {
       //   List[i] = new Super(i);
       //   i++;
       //   List[i] = new Sub(i, 0);
       //   i++;
       // }
1269
       WASM_LOCAL_SET(i, WASM_I32V_1(0)),
1270
       WASM_LOOP(
1271
           WASM_ARRAY_SET(ListType, WASM_GLOBAL_GET(List), WASM_LOCAL_GET(i),
1272
                          WASM_STRUCT_NEW_WITH_RTT(SuperType, WASM_LOCAL_GET(i),
1273
                                                   WASM_GLOBAL_GET(RttSuper))),
1274
           WASM_LOCAL_SET(i, WASM_I32_ADD(WASM_LOCAL_GET(i), WASM_I32V_1(1))),
1275
           WASM_ARRAY_SET(ListType, WASM_GLOBAL_GET(List), WASM_LOCAL_GET(i),
1276
                          WASM_STRUCT_NEW_WITH_RTT(SubType, WASM_LOCAL_GET(i),
1277
                                                   WASM_I32V_1(0),
1278
                                                   WASM_GLOBAL_GET(RttSub))),
1279
           WASM_LOCAL_SET(i, WASM_I32_ADD(WASM_LOCAL_GET(i), WASM_I32V_1(1))),
1280
           WASM_BR_IF(0,
1281
                      WASM_I32_NE(WASM_LOCAL_GET(i), WASM_I32V(kListLength)))),
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
       // 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),
      },
1296
      {WASM_LOCAL_SET(list, WASM_GLOBAL_GET(List)),
1297
       // sum = 0;
1298
       WASM_LOCAL_SET(sum, WASM_I32V_1(0)),
1299 1300 1301
       // for (int i = 0; i < kIterations; i++) {
       //   sum += ref.cast<super>(List[i & kListLength]).x
       // }
1302
       WASM_LOCAL_SET(i, WASM_I32V_1(0)),
1303
       WASM_LOOP(
1304
           WASM_LOCAL_SET(
1305
               sum, WASM_I32_ADD(
1306
                        WASM_LOCAL_GET(sum),
1307 1308 1309 1310
                        WASM_STRUCT_GET(
                            SuperType, 0,
                            WASM_REF_CAST(
                                WASM_ARRAY_GET(
1311 1312
                                    ListType, WASM_LOCAL_GET(list),
                                    WASM_I32_AND(WASM_LOCAL_GET(i),
1313
                                                 WASM_I32V(kListLength - 1))),
1314
                                WASM_GLOBAL_GET(RttSuper))))),
1315
           WASM_LOCAL_SET(i, WASM_I32_ADD(WASM_LOCAL_GET(i), WASM_I32V_1(1))),
1316
           WASM_BR_IF(0,
1317
                      WASM_I32_LTS(WASM_LOCAL_GET(i), WASM_I32V(kIterations)))),
1318
       // return sum;
1319
       WASM_LOCAL_GET(sum), kExprEnd});
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

  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);
}

1330 1331
WASM_COMPILED_EXEC_TEST(GlobalInitReferencingGlobal) {
  WasmGCTester tester(execution_tier);
1332 1333 1334 1335 1336
  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(), {},
1337
                                          {WASM_GLOBAL_GET(to), kExprEnd});
1338 1339 1340 1341 1342 1343

  tester.CompileModule();

  tester.CheckResult(func, 42);
}

1344 1345
WASM_COMPILED_EXEC_TEST(IndirectNullSetManually) {
  WasmGCTester tester(execution_tier);
1346 1347 1348 1349 1350
  byte sig_index = tester.DefineSignature(tester.sigs.i_i());
  tester.DefineTable(ValueType::Ref(sig_index, kNullable), 1, 1);
  byte func_index = tester.DefineFunction(
      tester.sigs.i_i(), {},
      {WASM_TABLE_SET(0, WASM_I32V(0), WASM_REF_NULL(sig_index)),
1351
       WASM_CALL_INDIRECT(sig_index, WASM_I32V(0), WASM_LOCAL_GET(0)),
1352 1353 1354 1355 1356 1357 1358
       kExprEnd});

  tester.CompileModule();

  tester.CheckHasThrown(func_index, 42);
}

1359 1360
WASM_COMPILED_EXEC_TEST(JsAccess) {
  WasmGCTester tester(execution_tier);
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
  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,
1381 1382 1383
      {WASM_STRUCT_GET(
           type_index, 0,
           WASM_REF_CAST(WASM_LOCAL_GET(0), WASM_RTT_CANON(type_index))),
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
       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);
1419
  }
1420 1421 1422 1423
  CHECK(maybe_result.is_null());
  CHECK(try_catch.HasCaught());
  try_catch.Reset();
  isolate->clear_pending_exception();
1424 1425
}

1426 1427 1428 1429
}  // namespace test_gc
}  // namespace wasm
}  // namespace internal
}  // namespace v8