summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/wasm/test-run-wasm-bulk-memory.cc
blob: d2ac3434dfff8b215d1a7a70240d903e0a31b455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
// Copyright 2019 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 "test/cctest/cctest.h"
#include "test/cctest/wasm/wasm-run-utils.h"
#include "test/common/wasm/test-signatures.h"
#include "test/common/wasm/wasm-macro-gen.h"

namespace v8 {
namespace internal {
namespace wasm {
namespace test_run_wasm_bulk_memory {

namespace {
void CheckMemoryEquals(TestingModuleBuilder* builder, size_t index,
                       const std::vector<byte>& expected) {
  const byte* mem_start = builder->raw_mem_start<byte>();
  const byte* mem_end = builder->raw_mem_end<byte>();
  size_t mem_size = mem_end - mem_start;
  CHECK_LE(index, mem_size);
  CHECK_LE(index + expected.size(), mem_size);
  for (size_t i = 0; i < expected.size(); ++i) {
    CHECK_EQ(expected[i], mem_start[index + i]);
  }
}

void CheckMemoryEqualsZero(TestingModuleBuilder* builder, size_t index,
                           size_t length) {
  const byte* mem_start = builder->raw_mem_start<byte>();
  const byte* mem_end = builder->raw_mem_end<byte>();
  size_t mem_size = mem_end - mem_start;
  CHECK_LE(index, mem_size);
  CHECK_LE(index + length, mem_size);
  for (size_t i = 0; i < length; ++i) {
    CHECK_EQ(0, mem_start[index + i]);
  }
}

void CheckMemoryEqualsFollowedByZeroes(TestingModuleBuilder* builder,
                                       const std::vector<byte>& expected) {
  CheckMemoryEquals(builder, 0, expected);
  CheckMemoryEqualsZero(builder, expected.size(),
                        builder->mem_size() - expected.size());
}
}  // namespace

WASM_EXEC_TEST(MemoryInit) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  const byte data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  r.builder().AddPassiveDataSegment(ArrayVector(data));
  BUILD(r,
        WASM_MEMORY_INIT(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
                         WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  // All zeroes.
  CheckMemoryEqualsZero(&r.builder(), 0, kWasmPageSize);

  // Copy all bytes from data segment 0, to memory at [10, 20).
  CHECK_EQ(0, r.Call(10, 0, 10));
  CheckMemoryEqualsFollowedByZeroes(
      &r.builder(),
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9});

  // Copy bytes in range [5, 10) from data segment 0, to memory at [0, 5).
  CHECK_EQ(0, r.Call(0, 5, 5));
  CheckMemoryEqualsFollowedByZeroes(
      &r.builder(),
      {5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9});

  // Copy 0 bytes does nothing.
  CHECK_EQ(0, r.Call(10, 1, 0));
  CheckMemoryEqualsFollowedByZeroes(
      &r.builder(),
      {5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9});

  // Copy 0 at end of memory region or data segment is OK.
  CHECK_EQ(0, r.Call(kWasmPageSize, 0, 0));
  CHECK_EQ(0, r.Call(0, sizeof(data), 0));
}

WASM_EXEC_TEST(MemoryInitOutOfBoundsData) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  const byte data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  r.builder().AddPassiveDataSegment(ArrayVector(data));
  BUILD(r,
        WASM_MEMORY_INIT(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
                         WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  const uint32_t last_5_bytes = kWasmPageSize - 5;

  // Write all values up to the out-of-bounds write.
  CHECK_EQ(0xDEADBEEF, r.Call(kWasmPageSize - 5, 0, 6));
  CheckMemoryEquals(&r.builder(), last_5_bytes, {0, 1, 2, 3, 4});

  // Write all values up to the out-of-bounds read.
  r.builder().BlankMemory();
  CHECK_EQ(0xDEADBEEF, r.Call(0, 5, 6));
  CheckMemoryEqualsFollowedByZeroes(&r.builder(), {5, 6, 7, 8, 9});
}

WASM_EXEC_TEST(MemoryInitOutOfBounds) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  const byte data[kWasmPageSize] = {};
  r.builder().AddPassiveDataSegment(ArrayVector(data));
  BUILD(r,
        WASM_MEMORY_INIT(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
                         WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  // OK, copy the full data segment to memory.
  r.Call(0, 0, kWasmPageSize);

  // Source range must not be out of bounds.
  CHECK_EQ(0xDEADBEEF, r.Call(0, 1, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(0, 1000, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(0, kWasmPageSize, 1));

  // Destination range must not be out of bounds.
  CHECK_EQ(0xDEADBEEF, r.Call(1, 0, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(1000, 0, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(kWasmPageSize, 0, 1));

  // Copy 0 out-of-bounds succeeds.
  CHECK_EQ(0, r.Call(kWasmPageSize + 1, 0, 0));
  CHECK_EQ(0, r.Call(0, kWasmPageSize + 1, 0));

  // Make sure bounds aren't checked with 32-bit wrapping.
  CHECK_EQ(0xDEADBEEF, r.Call(1, 1, 0xFFFFFFFF));
}

WASM_EXEC_TEST(MemoryCopy) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  byte* mem = r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_COPY(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);

  const byte initial[] = {0, 11, 22, 33, 44, 55, 66, 77};
  memcpy(mem, initial, sizeof(initial));

  // Copy from [1, 8] to [10, 16].
  CHECK_EQ(0, r.Call(10, 1, 8));
  CheckMemoryEqualsFollowedByZeroes(
      &r.builder(),
      {0, 11, 22, 33, 44, 55, 66, 77, 0, 0, 11, 22, 33, 44, 55, 66, 77});

  // Copy 0 bytes does nothing.
  CHECK_EQ(0, r.Call(10, 2, 0));
  CheckMemoryEqualsFollowedByZeroes(
      &r.builder(),
      {0, 11, 22, 33, 44, 55, 66, 77, 0, 0, 11, 22, 33, 44, 55, 66, 77});

  // Copy 0 at end of memory region is OK.
  CHECK_EQ(0, r.Call(kWasmPageSize, 0, 0));
  CHECK_EQ(0, r.Call(0, kWasmPageSize, 0));
}

WASM_EXEC_TEST(MemoryCopyOverlapping) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  byte* mem = r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_COPY(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);

  const byte initial[] = {10, 20, 30};
  memcpy(mem, initial, sizeof(initial));

  // Copy from [0, 3] -> [2, 5]. The copy must not overwrite 30 before copying
  // it (i.e. cannot copy forward in this case).
  CHECK_EQ(0, r.Call(2, 0, 3));
  CheckMemoryEqualsFollowedByZeroes(&r.builder(), {10, 20, 10, 20, 30});

  // Copy from [2, 5] -> [0, 3]. The copy must not write the first 10 (i.e.
  // cannot copy backward in this case).
  CHECK_EQ(0, r.Call(0, 2, 3));
  CheckMemoryEqualsFollowedByZeroes(&r.builder(), {10, 20, 30, 20, 30});
}

WASM_EXEC_TEST(MemoryCopyOutOfBoundsData) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  byte* mem = r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_COPY(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);

  const byte data[] = {11, 22, 33, 44, 55, 66, 77, 88};
  memcpy(mem, data, sizeof(data));

  const uint32_t last_5_bytes = kWasmPageSize - 5;

  // Copy with source < destination. Copy would happen backwards,
  // but the first byte to copy is out-of-bounds, so no data should be written.
  CHECK_EQ(0xDEADBEEF, r.Call(last_5_bytes, 0, 6));
  CheckMemoryEquals(&r.builder(), last_5_bytes, {0, 0, 0, 0, 0});

  // Copy overlapping with destination < source. Copy will happen forwards, up
  // to the out-of-bounds access.
  r.builder().BlankMemory();
  memcpy(mem + last_5_bytes, data, 5);
  CHECK_EQ(0xDEADBEEF, r.Call(0, last_5_bytes, kWasmPageSize));
  CheckMemoryEquals(&r.builder(), 0, {11, 22, 33, 44, 55});

  // Copy overlapping with source < destination. Copy would happen backwards,
  // but the first byte to copy is out-of-bounds, so no data should be written.
  r.builder().BlankMemory();
  memcpy(mem, data, 5);
  CHECK_EQ(0xDEADBEEF, r.Call(last_5_bytes, 0, kWasmPageSize));
  CheckMemoryEquals(&r.builder(), last_5_bytes, {0, 0, 0, 0, 0});
}

WASM_EXEC_TEST(MemoryCopyOutOfBounds) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_COPY(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);

  // Copy full range is OK.
  CHECK_EQ(0, r.Call(0, 0, kWasmPageSize));

  // Source range must not be out of bounds.
  CHECK_EQ(0xDEADBEEF, r.Call(0, 1, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(0, 1000, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(0, kWasmPageSize, 1));

  // Destination range must not be out of bounds.
  CHECK_EQ(0xDEADBEEF, r.Call(1, 0, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(1000, 0, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(kWasmPageSize, 0, 1));

  // Copy 0 out-of-bounds always succeeds.
  CHECK_EQ(0, r.Call(kWasmPageSize + 1, 0, 0));
  CHECK_EQ(0, r.Call(0, kWasmPageSize + 1, 0));

  // Make sure bounds aren't checked with 32-bit wrapping.
  CHECK_EQ(0xDEADBEEF, r.Call(1, 1, 0xFFFFFFFF));
}

WASM_EXEC_TEST(MemoryFill) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_FILL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);
  CHECK_EQ(0, r.Call(1, 33, 5));
  CheckMemoryEqualsFollowedByZeroes(&r.builder(), {0, 33, 33, 33, 33, 33});

  CHECK_EQ(0, r.Call(4, 66, 4));
  CheckMemoryEqualsFollowedByZeroes(&r.builder(),
                                    {0, 33, 33, 33, 66, 66, 66, 66});

  // Fill 0 bytes does nothing.
  CHECK_EQ(0, r.Call(4, 66, 0));
  CheckMemoryEqualsFollowedByZeroes(&r.builder(),
                                    {0, 33, 33, 33, 66, 66, 66, 66});

  // Fill 0 at end of memory region is OK.
  CHECK_EQ(0, r.Call(kWasmPageSize, 66, 0));
}

WASM_EXEC_TEST(MemoryFillValueWrapsToByte) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_FILL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);
  CHECK_EQ(0, r.Call(0, 1000, 3));
  const byte expected = 1000 & 255;
  CheckMemoryEqualsFollowedByZeroes(&r.builder(),
                                    {expected, expected, expected});
}

WASM_EXEC_TEST(MemoryFillOutOfBoundsData) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_FILL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);
  const byte v = 123;
  CHECK_EQ(0xDEADBEEF, r.Call(kWasmPageSize - 5, v, 999));
  CheckMemoryEquals(&r.builder(), kWasmPageSize - 6, {0, v, v, v, v, v});
}

WASM_EXEC_TEST(MemoryFillOutOfBounds) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  BUILD(
      r,
      WASM_MEMORY_FILL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
      kExprI32Const, 0);

  const byte v = 123;

  // Destination range must not be out of bounds.
  CHECK_EQ(0xDEADBEEF, r.Call(1, v, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(1000, v, kWasmPageSize));
  CHECK_EQ(0xDEADBEEF, r.Call(kWasmPageSize, v, 1));

  // Fill 0 out-of-bounds succeeds.
  CHECK_EQ(0, r.Call(kWasmPageSize + 1, v, 0));

  // Make sure bounds aren't checked with 32-bit wrapping.
  CHECK_EQ(0xDEADBEEF, r.Call(1, v, 0xFFFFFFFF));
}

WASM_EXEC_TEST(DataDropTwice) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  const byte data[] = {0};
  r.builder().AddPassiveDataSegment(ArrayVector(data));
  BUILD(r, WASM_DATA_DROP(0), kExprI32Const, 0);

  CHECK_EQ(0, r.Call());
  CHECK_EQ(0xDEADBEEF, r.Call());
}

WASM_EXEC_TEST(DataDropThenMemoryInit) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t> r(execution_tier);
  r.builder().AddMemory(kWasmPageSize);
  const byte data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  r.builder().AddPassiveDataSegment(ArrayVector(data));
  BUILD(r, WASM_DATA_DROP(0),
        WASM_MEMORY_INIT(0, WASM_I32V_1(0), WASM_I32V_1(1), WASM_I32V_1(2)),
        kExprI32Const, 0);

  CHECK_EQ(0xDEADBEEF, r.Call());
}

void TestTableCopyInbounds(ExecutionTier execution_tier, int table_dst,
                           int table_src) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  const uint32_t kTableSize = 5;
  // Add 10 function tables, even though we only test one table.
  for (int i = 0; i < 10; ++i) {
    r.builder().AddIndirectFunctionTable(nullptr, kTableSize);
  }
  BUILD(r,
        WASM_TABLE_COPY(table_dst, table_src, WASM_GET_LOCAL(0),
                        WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  for (uint32_t i = 0; i <= kTableSize; ++i) {
    r.CheckCallViaJS(0, 0, 0, i);  // nop
    r.CheckCallViaJS(0, 0, i, kTableSize - i);
    r.CheckCallViaJS(0, i, 0, kTableSize - i);
  }
}

WASM_EXEC_TEST(TableCopyInboundsFrom0To0) {
  TestTableCopyInbounds(execution_tier, 0, 0);
}

WASM_EXEC_TEST(TableCopyInboundsFrom3To0) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyInbounds(execution_tier, 3, 0);
}

WASM_EXEC_TEST(TableCopyInboundsFrom5To9) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyInbounds(execution_tier, 5, 9);
}

WASM_EXEC_TEST(TableCopyInboundsFrom6To6) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyInbounds(execution_tier, 6, 6);
}

namespace {
template <typename... Args>
void CheckTable(Isolate* isolate, Handle<WasmTableObject> table, Args... args) {
  uint32_t args_length = static_cast<uint32_t>(sizeof...(args));
  CHECK_EQ(table->current_length(), args_length);
  Handle<Object> handles[] = {args...};
  for (uint32_t i = 0; i < args_length; ++i) {
    CHECK(WasmTableObject::Get(isolate, table, i).is_identical_to(handles[i]));
  }
}

template <typename WasmRunner, typename... Args>
void CheckTableCall(Isolate* isolate, Handle<WasmTableObject> table,
                    WasmRunner* r, uint32_t function_index, Args... args) {
  uint32_t args_length = static_cast<uint32_t>(sizeof...(args));
  CHECK_EQ(table->current_length(), args_length);
  double expected[] = {args...};
  for (uint32_t i = 0; i < args_length; ++i) {
    Handle<Object> buffer[] = {isolate->factory()->NewNumber(i)};
    r->CheckCallApplyViaJS(expected[i], function_index, buffer, 1);
  }
}
}  // namespace

void TestTableInitElems(ExecutionTier execution_tier, int table_index) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  Isolate* isolate = CcTest::InitIsolateOnce();
  HandleScope scope(isolate);
  TestSignatures sigs;
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  const uint32_t kTableSize = 5;
  std::vector<uint32_t> function_indexes;
  const uint32_t sig_index = r.builder().AddSignature(sigs.i_v());

  for (uint32_t i = 0; i < kTableSize; ++i) {
    WasmFunctionCompiler& fn = r.NewFunction(sigs.i_v(), "f");
    BUILD(fn, WASM_I32V_1(i));
    fn.SetSigIndex(sig_index);
    function_indexes.push_back(fn.function_index());
  }

  // Passive element segment has [f0, f1, f2, f3, f4, null].
  function_indexes.push_back(WasmElemSegment::kNullIndex);

  // Add 10 function tables, even though we only test one table.
  for (int i = 0; i < 10; ++i) {
    r.builder().AddIndirectFunctionTable(nullptr, kTableSize);
  }
  r.builder().AddPassiveElementSegment(function_indexes);

  WasmFunctionCompiler& call = r.NewFunction(sigs.i_i(), "call");
  BUILD(call,
        WASM_CALL_INDIRECT_TABLE0(table_index, sig_index, WASM_GET_LOCAL(0)));
  const uint32_t call_index = call.function_index();

  BUILD(r,
        WASM_TABLE_INIT(table_index, 0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
                        WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  auto table =
      handle(WasmTableObject::cast(
                 r.builder().instance_object()->tables().get(table_index)),
             isolate);
  const double null = 0xDEADBEEF;

  CheckTableCall(isolate, table, &r, call_index, null, null, null, null, null);

  // 0 count is ok in bounds, and at end of regions.
  r.CheckCallViaJS(0, 0, 0, 0);
  r.CheckCallViaJS(0, kTableSize, 0, 0);
  r.CheckCallViaJS(0, 0, kTableSize, 0);

  // Test actual writes.
  r.CheckCallViaJS(0, 0, 0, 1);
  CheckTableCall(isolate, table, &r, call_index, 0, null, null, null, null);
  r.CheckCallViaJS(0, 0, 0, 2);
  CheckTableCall(isolate, table, &r, call_index, 0, 1, null, null, null);
  r.CheckCallViaJS(0, 0, 0, 3);
  CheckTableCall(isolate, table, &r, call_index, 0, 1, 2, null, null);
  r.CheckCallViaJS(0, 3, 0, 2);
  CheckTableCall(isolate, table, &r, call_index, 0, 1, 2, 0, 1);
  r.CheckCallViaJS(0, 3, 1, 2);
  CheckTableCall(isolate, table, &r, call_index, 0, 1, 2, 1, 2);
  r.CheckCallViaJS(0, 3, 2, 2);
  CheckTableCall(isolate, table, &r, call_index, 0, 1, 2, 2, 3);
  r.CheckCallViaJS(0, 3, 3, 2);
  CheckTableCall(isolate, table, &r, call_index, 0, 1, 2, 3, 4);
}

WASM_EXEC_TEST(TableInitElems0) { TestTableInitElems(execution_tier, 0); }
WASM_EXEC_TEST(TableInitElems7) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableInitElems(execution_tier, 7);
}
WASM_EXEC_TEST(TableInitElems9) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableInitElems(execution_tier, 9);
}

void TestTableInitOob(ExecutionTier execution_tier, int table_index) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  Isolate* isolate = CcTest::InitIsolateOnce();
  HandleScope scope(isolate);
  TestSignatures sigs;
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  const uint32_t kTableSize = 5;
  std::vector<uint32_t> function_indexes;
  const uint32_t sig_index = r.builder().AddSignature(sigs.i_v());

  for (uint32_t i = 0; i < kTableSize; ++i) {
    WasmFunctionCompiler& fn = r.NewFunction(sigs.i_v(), "f");
    BUILD(fn, WASM_I32V_1(i));
    fn.SetSigIndex(sig_index);
    function_indexes.push_back(fn.function_index());
  }

  for (int i = 0; i < 10; ++i) {
    r.builder().AddIndirectFunctionTable(nullptr, kTableSize);
  }
  r.builder().AddPassiveElementSegment(function_indexes);

  WasmFunctionCompiler& call = r.NewFunction(sigs.i_i(), "call");
  BUILD(call,
        WASM_CALL_INDIRECT_TABLE0(table_index, sig_index, WASM_GET_LOCAL(0)));
  const uint32_t call_index = call.function_index();

  BUILD(r,
        WASM_TABLE_INIT(table_index, 0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
                        WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  auto table =
      handle(WasmTableObject::cast(
                 r.builder().instance_object()->tables().get(table_index)),
             isolate);
  const double null = 0xDEADBEEF;

  CheckTableCall(isolate, table, &r, call_index, null, null, null, null, null);

  // Write all values up to the out-of-bounds write.
  r.CheckCallViaJS(0xDEADBEEF, 3, 0, 3);
  CheckTableCall(isolate, table, &r, call_index, null, null, null, 0, 1);

  // Write all values up to the out-of-bounds read.
  r.CheckCallViaJS(0xDEADBEEF, 0, 3, 3);
  CheckTableCall(isolate, table, &r, call_index, 3, 4, null, 0, 1);

  // 0-count is never oob.
  r.CheckCallViaJS(0, kTableSize + 1, 0, 0);
  r.CheckCallViaJS(0, 0, kTableSize + 1, 0);

  r.CheckCallViaJS(0xDEADBEEF, 0, 0, 6);
  r.CheckCallViaJS(0xDEADBEEF, 0, 1, 5);
  r.CheckCallViaJS(0xDEADBEEF, 0, 2, 4);
  r.CheckCallViaJS(0xDEADBEEF, 0, 3, 3);
  r.CheckCallViaJS(0xDEADBEEF, 0, 4, 2);
  r.CheckCallViaJS(0xDEADBEEF, 0, 5, 1);

  r.CheckCallViaJS(0xDEADBEEF, 0, 0, 6);
  r.CheckCallViaJS(0xDEADBEEF, 1, 0, 5);
  r.CheckCallViaJS(0xDEADBEEF, 2, 0, 4);
  r.CheckCallViaJS(0xDEADBEEF, 3, 0, 3);
  r.CheckCallViaJS(0xDEADBEEF, 4, 0, 2);
  r.CheckCallViaJS(0xDEADBEEF, 5, 0, 1);

  r.CheckCallViaJS(0xDEADBEEF, 10, 0, 1);
  r.CheckCallViaJS(0xDEADBEEF, 0, 10, 1);
}

WASM_EXEC_TEST(TableInitOob0) { TestTableInitOob(execution_tier, 0); }
WASM_EXEC_TEST(TableInitOob7) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableInitOob(execution_tier, 7);
}
WASM_EXEC_TEST(TableInitOob9) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableInitOob(execution_tier, 9);
}

void TestTableCopyElems(ExecutionTier execution_tier, int table_dst,
                        int table_src) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  Isolate* isolate = CcTest::InitIsolateOnce();
  HandleScope scope(isolate);
  TestSignatures sigs;
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  const uint32_t kTableSize = 5;
  uint16_t function_indexes[kTableSize];
  const uint32_t sig_index = r.builder().AddSignature(sigs.i_v());

  for (uint32_t i = 0; i < kTableSize; ++i) {
    WasmFunctionCompiler& fn = r.NewFunction(sigs.i_v(), "f");
    BUILD(fn, WASM_I32V_1(i));
    fn.SetSigIndex(sig_index);
    function_indexes[i] = fn.function_index();
  }

  for (int i = 0; i < 10; ++i) {
    r.builder().AddIndirectFunctionTable(function_indexes, kTableSize);
  }

  BUILD(r,
        WASM_TABLE_COPY(table_dst, table_src, WASM_GET_LOCAL(0),
                        WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  r.builder().FreezeSignatureMapAndInitializeWrapperCache();

  auto table =
      handle(WasmTableObject::cast(
                 r.builder().instance_object()->tables().get(table_dst)),
             isolate);
  r.CheckCallViaJS(0, 0, 0, kTableSize);
  auto f0 = WasmTableObject::Get(isolate, table, 0);
  auto f1 = WasmTableObject::Get(isolate, table, 1);
  auto f2 = WasmTableObject::Get(isolate, table, 2);
  auto f3 = WasmTableObject::Get(isolate, table, 3);
  auto f4 = WasmTableObject::Get(isolate, table, 4);

  if (table_dst == table_src) {
    CheckTable(isolate, table, f0, f1, f2, f3, f4);
    r.CheckCallViaJS(0, 0, 1, 1);
    CheckTable(isolate, table, f1, f1, f2, f3, f4);
    r.CheckCallViaJS(0, 0, 1, 2);
    CheckTable(isolate, table, f1, f2, f2, f3, f4);
    r.CheckCallViaJS(0, 3, 0, 2);
    CheckTable(isolate, table, f1, f2, f2, f1, f2);
    r.CheckCallViaJS(0, 1, 0, 2);
    CheckTable(isolate, table, f1, f1, f2, f1, f2);
  } else {
    CheckTable(isolate, table, f0, f1, f2, f3, f4);
    r.CheckCallViaJS(0, 0, 1, 1);
    CheckTable(isolate, table, f1, f1, f2, f3, f4);
    r.CheckCallViaJS(0, 0, 1, 2);
    CheckTable(isolate, table, f1, f2, f2, f3, f4);
    r.CheckCallViaJS(0, 3, 0, 2);
    CheckTable(isolate, table, f1, f2, f2, f0, f1);
    r.CheckCallViaJS(0, 1, 0, 2);
    CheckTable(isolate, table, f1, f0, f1, f0, f1);
  }
}

WASM_EXEC_TEST(TableCopyElemsFrom0To0) {
  TestTableCopyElems(execution_tier, 0, 0);
}

WASM_EXEC_TEST(TableCopyElemsFrom3To0) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyElems(execution_tier, 3, 0);
}

WASM_EXEC_TEST(TableCopyElemsFrom5To9) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyElems(execution_tier, 5, 9);
}

WASM_EXEC_TEST(TableCopyElemsFrom6To6) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyElems(execution_tier, 6, 6);
}

void TestTableCopyCalls(ExecutionTier execution_tier, int table_dst,
                        int table_src) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  Isolate* isolate = CcTest::InitIsolateOnce();
  HandleScope scope(isolate);
  TestSignatures sigs;
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  const uint32_t kTableSize = 5;
  uint16_t function_indexes[kTableSize];
  const uint32_t sig_index = r.builder().AddSignature(sigs.i_v());

  for (uint32_t i = 0; i < kTableSize; ++i) {
    WasmFunctionCompiler& fn = r.NewFunction(sigs.i_v(), "f");
    BUILD(fn, WASM_I32V_1(i));
    fn.SetSigIndex(sig_index);
    function_indexes[i] = fn.function_index();
  }

  for (int i = 0; i < 10; ++i) {
    r.builder().AddIndirectFunctionTable(function_indexes, kTableSize);
  }

  WasmFunctionCompiler& call = r.NewFunction(sigs.i_i(), "call");
  BUILD(call,
        WASM_CALL_INDIRECT_TABLE0(table_dst, sig_index, WASM_GET_LOCAL(0)));
  const uint32_t call_index = call.function_index();

  BUILD(r,
        WASM_TABLE_COPY(table_dst, table_src, WASM_GET_LOCAL(0),
                        WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  auto table =
      handle(WasmTableObject::cast(
                 r.builder().instance_object()->tables().get(table_dst)),
             isolate);

  if (table_dst == table_src) {
    CheckTableCall(isolate, table, &r, call_index, 0, 1, 2, 3, 4);
    r.CheckCallViaJS(0, 0, 1, 1);
    CheckTableCall(isolate, table, &r, call_index, 1, 1, 2, 3, 4);
    r.CheckCallViaJS(0, 0, 1, 2);
    CheckTableCall(isolate, table, &r, call_index, 1, 2, 2, 3, 4);
    r.CheckCallViaJS(0, 3, 0, 2);
    CheckTableCall(isolate, table, &r, call_index, 1, 2, 2, 1, 2);
  } else {
    CheckTableCall(isolate, table, &r, call_index, 0, 1, 2, 3, 4);
    r.CheckCallViaJS(0, 0, 1, 1);
    CheckTableCall(isolate, table, &r, call_index, 1, 1, 2, 3, 4);
    r.CheckCallViaJS(0, 0, 1, 2);
    CheckTableCall(isolate, table, &r, call_index, 1, 2, 2, 3, 4);
    r.CheckCallViaJS(0, 3, 0, 2);
    CheckTableCall(isolate, table, &r, call_index, 1, 2, 2, 0, 1);
  }
}

WASM_EXEC_TEST(TableCopyCallsFrom0To0) {
  TestTableCopyCalls(execution_tier, 0, 0);
}

WASM_EXEC_TEST(TableCopyCallsFrom3To0) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyCalls(execution_tier, 3, 0);
}

WASM_EXEC_TEST(TableCopyCallsFrom5To9) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyCalls(execution_tier, 5, 9);
}

WASM_EXEC_TEST(TableCopyCallsFrom6To6) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyCalls(execution_tier, 6, 6);
}

void TestTableCopyOobWrites(ExecutionTier execution_tier, int table_dst,
                            int table_src) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  Isolate* isolate = CcTest::InitIsolateOnce();
  HandleScope scope(isolate);
  TestSignatures sigs;
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  const uint32_t kTableSize = 5;
  uint16_t function_indexes[kTableSize];
  const uint32_t sig_index = r.builder().AddSignature(sigs.i_v());

  for (uint32_t i = 0; i < kTableSize; ++i) {
    WasmFunctionCompiler& fn = r.NewFunction(sigs.i_v(), "f");
    BUILD(fn, WASM_I32V_1(i));
    fn.SetSigIndex(sig_index);
    function_indexes[i] = fn.function_index();
  }

  for (int i = 0; i < 10; ++i) {
    r.builder().AddIndirectFunctionTable(function_indexes, kTableSize);
  }

  BUILD(r,
        WASM_TABLE_COPY(table_dst, table_src, WASM_GET_LOCAL(0),
                        WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  r.builder().FreezeSignatureMapAndInitializeWrapperCache();

  auto table =
      handle(WasmTableObject::cast(
                 r.builder().instance_object()->tables().get(table_dst)),
             isolate);
  // Fill the dst table with values from the src table, to make checks easier.
  r.CheckCallViaJS(0, 0, 0, kTableSize);
  auto f0 = WasmTableObject::Get(isolate, table, 0);
  auto f1 = WasmTableObject::Get(isolate, table, 1);
  auto f2 = WasmTableObject::Get(isolate, table, 2);
  auto f3 = WasmTableObject::Get(isolate, table, 3);
  auto f4 = WasmTableObject::Get(isolate, table, 4);

  CheckTable(isolate, table, f0, f1, f2, f3, f4);

  // Non-overlapping, src < dst. Because of src < dst, we copy backwards.
  // Therefore the first access already traps, and the table is not changed.
  r.CheckCallViaJS(0xDEADBEEF, 3, 0, 3);
  CheckTable(isolate, table, f0, f1, f2, f3, f4);

  // Non-overlapping, dst < src.
  r.CheckCallViaJS(0xDEADBEEF, 0, 4, 2);
  CheckTable(isolate, table, f4, f1, f2, f3, f4);

  // Overlapping, src < dst. This is required to copy backward, but the first
  // access will be out-of-bounds, so nothing changes.
  r.CheckCallViaJS(0xDEADBEEF, 3, 0, 99);
  CheckTable(isolate, table, f4, f1, f2, f3, f4);

  // Overlapping, dst < src.
  r.CheckCallViaJS(0xDEADBEEF, 0, 1, 99);
  CheckTable(isolate, table, f1, f2, f3, f4, f4);
}

WASM_EXEC_TEST(TableCopyOobWritesFrom0To0) {
  TestTableCopyOobWrites(execution_tier, 0, 0);
}

WASM_EXEC_TEST(TableCopyOobWritesFrom3To0) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyOobWrites(execution_tier, 3, 0);
}

WASM_EXEC_TEST(TableCopyOobWritesFrom5To9) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyOobWrites(execution_tier, 5, 9);
}

WASM_EXEC_TEST(TableCopyOobWritesFrom6To6) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyOobWrites(execution_tier, 6, 6);
}

void TestTableCopyOob1(ExecutionTier execution_tier, int table_dst,
                       int table_src) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t, uint32_t, uint32_t, uint32_t> r(execution_tier);
  const uint32_t kTableSize = 5;

  for (int i = 0; i < 10; ++i) {
    r.builder().AddIndirectFunctionTable(nullptr, kTableSize);
  }

  BUILD(r,
        WASM_TABLE_COPY(table_dst, table_src, WASM_GET_LOCAL(0),
                        WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)),
        kExprI32Const, 0);

  r.CheckCallViaJS(0, 0, 0, 1);           // nop
  r.CheckCallViaJS(0, 0, 0, kTableSize);  // nop
  r.CheckCallViaJS(0xDEADBEEF, 0, 0, kTableSize + 1);
  r.CheckCallViaJS(0xDEADBEEF, 1, 0, kTableSize);
  r.CheckCallViaJS(0xDEADBEEF, 0, 1, kTableSize);

  {
    const uint32_t big = 1000000;
    r.CheckCallViaJS(0, big, 0, 0);
    r.CheckCallViaJS(0, 0, big, 0);
  }

  for (uint32_t big = 4294967295; big > 1000; big >>= 1) {
    r.CheckCallViaJS(0xDEADBEEF, big, 0, 1);
    r.CheckCallViaJS(0xDEADBEEF, 0, big, 1);
    r.CheckCallViaJS(0xDEADBEEF, 0, 0, big);
  }

  for (uint32_t big = -1000; big != 0; big <<= 1) {
    r.CheckCallViaJS(0xDEADBEEF, big, 0, 1);
    r.CheckCallViaJS(0xDEADBEEF, 0, big, 1);
    r.CheckCallViaJS(0xDEADBEEF, 0, 0, big);
  }
}

WASM_EXEC_TEST(TableCopyOob1From0To0) {
  TestTableCopyOob1(execution_tier, 0, 0);
}

WASM_EXEC_TEST(TableCopyOob1From3To0) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyOob1(execution_tier, 3, 0);
}

WASM_EXEC_TEST(TableCopyOob1From5To9) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyOob1(execution_tier, 5, 9);
}

WASM_EXEC_TEST(TableCopyOob1From6To6) {
  EXPERIMENTAL_FLAG_SCOPE(anyref);
  TestTableCopyOob1(execution_tier, 6, 6);
}

WASM_EXEC_TEST(ElemDropTwice) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t> r(execution_tier);
  r.builder().AddIndirectFunctionTable(nullptr, 1);
  r.builder().AddPassiveElementSegment({});
  BUILD(r, WASM_ELEM_DROP(0), kExprI32Const, 0);

  r.CheckCallViaJS(0);
  r.CheckCallViaJS(0xDEADBEEF);
}

WASM_EXEC_TEST(ElemDropThenTableInit) {
  EXPERIMENTAL_FLAG_SCOPE(bulk_memory);
  WasmRunner<uint32_t> r(execution_tier);
  r.builder().AddIndirectFunctionTable(nullptr, 1);
  r.builder().AddPassiveElementSegment({});
  BUILD(r, WASM_ELEM_DROP(0),
        WASM_TABLE_INIT(0, 0, WASM_I32V_1(0), WASM_I32V_1(0), WASM_I32V_1(0)),
        kExprI32Const, 0);

  r.CheckCallViaJS(0xDEADBEEF);
}

}  // namespace test_run_wasm_bulk_memory
}  // namespace wasm
}  // namespace internal
}  // namespace v8