1 // Copyright 2021 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // #include "src/api/api-inl.h"
6 // #include "test/cctest/wasm/wasm-atomics-utils.h"
7 #include "test/cctest/cctest.h"
8 #include "test/cctest/wasm/wasm-run-utils.h"
9 #include "test/common/wasm/test-signatures.h"
10 #include "test/common/wasm/wasm-macro-gen.h"
11 
12 namespace v8 {
13 namespace internal {
14 namespace wasm {
15 namespace test_liftoff_for_fuzzing {
16 
TEST(MaxSteps)17 TEST(MaxSteps) {
18   WasmRunner<uint32_t> r(TestExecutionTier::kLiftoffForFuzzing);
19 
20   BUILD(r, WASM_LOOP(WASM_BR(0)), WASM_I32V(23));
21   r.SetMaxSteps(10);
22   r.CheckCallViaJSTraps();
23 }
24 
TEST(NondeterminismUnopF32)25 TEST(NondeterminismUnopF32) {
26   WasmRunner<float> r(TestExecutionTier::kLiftoffForFuzzing);
27 
28   BUILD(r, WASM_F32_ABS(WASM_F32(std::nanf(""))));
29   CHECK(!r.HasNondeterminism());
30   r.CheckCallViaJS(std::nanf(""));
31   CHECK(r.HasNondeterminism());
32 }
33 
TEST(NondeterminismUnopF64)34 TEST(NondeterminismUnopF64) {
35   WasmRunner<double> r(TestExecutionTier::kLiftoffForFuzzing);
36 
37   BUILD(r, WASM_F64_ABS(WASM_F64(std::nan(""))));
38   CHECK(!r.HasNondeterminism());
39   r.CheckCallViaJS(std::nan(""));
40   CHECK(r.HasNondeterminism());
41 }
42 
TEST(NondeterminismUnopF32x4AllNaN)43 TEST(NondeterminismUnopF32x4AllNaN) {
44   WasmRunner<int32_t, float> r(TestExecutionTier::kLiftoffForFuzzing);
45 
46   byte value = 0;
47   BUILD(r,
48         WASM_SIMD_UNOP(kExprF32x4Ceil,
49                        WASM_SIMD_F32x4_SPLAT(WASM_LOCAL_GET(value))),
50         kExprDrop, WASM_ONE);
51   CHECK(!r.HasNondeterminism());
52   r.CheckCallViaJS(1, 0.0);
53   CHECK(!r.HasNondeterminism());
54   r.CheckCallViaJS(1, std::nanf(""));
55   CHECK(r.HasNondeterminism());
56 }
57 
TEST(NondeterminismUnopF32x4OneNaN)58 TEST(NondeterminismUnopF32x4OneNaN) {
59   for (byte lane = 0; lane < 4; ++lane) {
60     WasmRunner<int32_t, float> r(TestExecutionTier::kLiftoffForFuzzing);
61     BUILD(r, WASM_SIMD_F32x4_SPLAT(WASM_F32(0)), WASM_LOCAL_GET(0),
62           WASM_SIMD_OP(kExprF32x4ReplaceLane), lane,
63           WASM_SIMD_OP(kExprF32x4Ceil), kExprDrop, WASM_ONE);
64     CHECK(!r.HasNondeterminism());
65     r.CheckCallViaJS(1, 0.0);
66     CHECK(!r.HasNondeterminism());
67     r.CheckCallViaJS(1, std::nanf(""));
68     CHECK(r.HasNondeterminism());
69   }
70 }
71 
TEST(NondeterminismUnopF64x2AllNaN)72 TEST(NondeterminismUnopF64x2AllNaN) {
73   WasmRunner<int32_t, double> r(TestExecutionTier::kLiftoffForFuzzing);
74 
75   byte value = 0;
76   BUILD(r,
77         WASM_SIMD_UNOP(kExprF64x2Ceil,
78                        WASM_SIMD_F64x2_SPLAT(WASM_LOCAL_GET(value))),
79         kExprDrop, WASM_ONE);
80   CHECK(!r.HasNondeterminism());
81   r.CheckCallViaJS(1, 0.0);
82   CHECK(!r.HasNondeterminism());
83   r.CheckCallViaJS(1, std::nan(""));
84   CHECK(r.HasNondeterminism());
85 }
86 
TEST(NondeterminismUnopF64x2OneNaN)87 TEST(NondeterminismUnopF64x2OneNaN) {
88   for (byte lane = 0; lane < 2; ++lane) {
89     WasmRunner<int32_t, double> r(TestExecutionTier::kLiftoffForFuzzing);
90     BUILD(r, WASM_SIMD_F64x2_SPLAT(WASM_F64(0)), WASM_LOCAL_GET(0),
91           WASM_SIMD_OP(kExprF64x2ReplaceLane), lane,
92           WASM_SIMD_OP(kExprF64x2Ceil), kExprDrop, WASM_ONE);
93     CHECK(!r.HasNondeterminism());
94     r.CheckCallViaJS(1, 0.0);
95     CHECK(!r.HasNondeterminism());
96     r.CheckCallViaJS(1, std::nan(""));
97     CHECK(r.HasNondeterminism());
98   }
99 }
100 
TEST(NondeterminismBinop)101 TEST(NondeterminismBinop) {
102   WasmRunner<float> r(TestExecutionTier::kLiftoffForFuzzing);
103 
104   BUILD(r, WASM_F32_ADD(WASM_F32(std::nanf("")), WASM_F32(0)));
105   CHECK(!r.HasNondeterminism());
106   r.CheckCallViaJS(std::nanf(""));
107   CHECK(r.HasNondeterminism());
108 }
109 
110 }  // namespace test_liftoff_for_fuzzing
111 }  // namespace wasm
112 }  // namespace internal
113 }  // namespace v8
114