OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "src/base/platform/elapsed-timer.h" | 9 #include "src/base/platform/elapsed-timer.h" |
10 | 10 |
11 #include "src/wasm/wasm-macro-gen.h" | 11 #include "src/wasm/wasm-macro-gen.h" |
12 | 12 |
13 #include "test/cctest/cctest.h" | 13 #include "test/cctest/cctest.h" |
14 #include "test/cctest/compiler/value-helper.h" | 14 #include "test/cctest/compiler/value-helper.h" |
15 #include "test/cctest/wasm/test-signatures.h" | 15 #include "test/cctest/wasm/test-signatures.h" |
16 #include "test/cctest/wasm/wasm-run-utils.h" | 16 #include "test/cctest/wasm/wasm-run-utils.h" |
17 | 17 |
18 using namespace v8::base; | 18 using namespace v8::base; |
19 using namespace v8::internal; | 19 using namespace v8::internal; |
20 using namespace v8::internal::compiler; | 20 using namespace v8::internal::compiler; |
21 using namespace v8::internal::wasm; | 21 using namespace v8::internal::wasm; |
22 | 22 |
23 // for even shorter tests. | 23 // for even shorter tests. |
24 #define B2(a, b) kExprBlock, a, b, kExprEnd | 24 #define B2(a, b) kExprBlock, a, b, kExprEnd |
25 #define B1(a) kExprBlock, a, kExprEnd | 25 #define B1(a) kExprBlock, a, kExprEnd |
26 #define RET(x) x, kExprReturn, 1 | 26 #define RET(x) x, kExprReturn, 1 |
27 #define RET_I8(x) kExprI8Const, x, kExprReturn, 1 | 27 #define RET_I8(x) kExprI8Const, x, kExprReturn, 1 |
28 | 28 |
29 TEST(Run_WASM_Int32AsmjsDivS) { | 29 WASM_EXEC_TEST(Int32AsmjsDivS) { |
30 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); | 30 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
31 BUILD(r, WASM_BINOP(kExprI32AsmjsDivS, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 31 BUILD(r, WASM_BINOP(kExprI32AsmjsDivS, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
32 const int32_t kMin = std::numeric_limits<int32_t>::min(); | 32 const int32_t kMin = std::numeric_limits<int32_t>::min(); |
33 CHECK_EQ(0, r.Call(0, 100)); | 33 CHECK_EQ(0, r.Call(0, 100)); |
34 CHECK_EQ(0, r.Call(100, 0)); | 34 CHECK_EQ(0, r.Call(100, 0)); |
35 CHECK_EQ(0, r.Call(-1001, 0)); | 35 CHECK_EQ(0, r.Call(-1001, 0)); |
36 CHECK_EQ(kMin, r.Call(kMin, -1)); | 36 CHECK_EQ(kMin, r.Call(kMin, -1)); |
37 CHECK_EQ(0, r.Call(kMin, 0)); | 37 CHECK_EQ(0, r.Call(kMin, 0)); |
38 } | 38 } |
39 | 39 |
40 TEST(Run_WASM_Int32AsmjsRemS) { | 40 WASM_EXEC_TEST(Int32AsmjsRemS) { |
41 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); | 41 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
42 BUILD(r, WASM_BINOP(kExprI32AsmjsRemS, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 42 BUILD(r, WASM_BINOP(kExprI32AsmjsRemS, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
43 const int32_t kMin = std::numeric_limits<int32_t>::min(); | 43 const int32_t kMin = std::numeric_limits<int32_t>::min(); |
44 CHECK_EQ(33, r.Call(133, 100)); | 44 CHECK_EQ(33, r.Call(133, 100)); |
45 CHECK_EQ(0, r.Call(kMin, -1)); | 45 CHECK_EQ(0, r.Call(kMin, -1)); |
46 CHECK_EQ(0, r.Call(100, 0)); | 46 CHECK_EQ(0, r.Call(100, 0)); |
47 CHECK_EQ(0, r.Call(-1001, 0)); | 47 CHECK_EQ(0, r.Call(-1001, 0)); |
48 CHECK_EQ(0, r.Call(kMin, 0)); | 48 CHECK_EQ(0, r.Call(kMin, 0)); |
49 } | 49 } |
50 | 50 |
51 TEST(Run_WASM_Int32AsmjsDivU) { | 51 WASM_EXEC_TEST(Int32AsmjsDivU) { |
52 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); | 52 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
53 BUILD(r, WASM_BINOP(kExprI32AsmjsDivU, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 53 BUILD(r, WASM_BINOP(kExprI32AsmjsDivU, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
54 const int32_t kMin = std::numeric_limits<int32_t>::min(); | 54 const int32_t kMin = std::numeric_limits<int32_t>::min(); |
55 CHECK_EQ(0, r.Call(0, 100)); | 55 CHECK_EQ(0, r.Call(0, 100)); |
56 CHECK_EQ(0, r.Call(kMin, -1)); | 56 CHECK_EQ(0, r.Call(kMin, -1)); |
57 CHECK_EQ(0, r.Call(100, 0)); | 57 CHECK_EQ(0, r.Call(100, 0)); |
58 CHECK_EQ(0, r.Call(-1001, 0)); | 58 CHECK_EQ(0, r.Call(-1001, 0)); |
59 CHECK_EQ(0, r.Call(kMin, 0)); | 59 CHECK_EQ(0, r.Call(kMin, 0)); |
60 } | 60 } |
61 | 61 |
62 TEST(Run_WASM_Int32AsmjsRemU) { | 62 WASM_EXEC_TEST(Int32AsmjsRemU) { |
63 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); | 63 WasmRunner<int32_t> r(MachineType::Int32(), MachineType::Int32()); |
64 BUILD(r, WASM_BINOP(kExprI32AsmjsRemU, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 64 BUILD(r, WASM_BINOP(kExprI32AsmjsRemU, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
65 const int32_t kMin = std::numeric_limits<int32_t>::min(); | 65 const int32_t kMin = std::numeric_limits<int32_t>::min(); |
66 CHECK_EQ(17, r.Call(217, 100)); | 66 CHECK_EQ(17, r.Call(217, 100)); |
67 CHECK_EQ(0, r.Call(100, 0)); | 67 CHECK_EQ(0, r.Call(100, 0)); |
68 CHECK_EQ(0, r.Call(-1001, 0)); | 68 CHECK_EQ(0, r.Call(-1001, 0)); |
69 CHECK_EQ(0, r.Call(kMin, 0)); | 69 CHECK_EQ(0, r.Call(kMin, 0)); |
70 CHECK_EQ(kMin, r.Call(kMin, -1)); | 70 CHECK_EQ(kMin, r.Call(kMin, -1)); |
71 } | 71 } |
72 | 72 |
73 TEST(Run_Wasm_I32AsmjsSConvertF32) { | 73 WASM_EXEC_TEST(I32AsmjsSConvertF32) { |
74 WasmRunner<int32_t> r(MachineType::Float32()); | 74 WasmRunner<int32_t> r(MachineType::Float32()); |
75 BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF32, WASM_GET_LOCAL(0))); | 75 BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF32, WASM_GET_LOCAL(0))); |
76 | 76 |
77 FOR_FLOAT32_INPUTS(i) { | 77 FOR_FLOAT32_INPUTS(i) { |
78 int32_t expected = DoubleToInt32(*i); | 78 int32_t expected = DoubleToInt32(*i); |
79 CHECK_EQ(expected, r.Call(*i)); | 79 CHECK_EQ(expected, r.Call(*i)); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 TEST(Run_Wasm_I32AsmjsSConvertF64) { | 83 WASM_EXEC_TEST(I32AsmjsSConvertF64) { |
84 WasmRunner<int32_t> r(MachineType::Float64()); | 84 WasmRunner<int32_t> r(MachineType::Float64()); |
85 BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF64, WASM_GET_LOCAL(0))); | 85 BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF64, WASM_GET_LOCAL(0))); |
86 | 86 |
87 FOR_FLOAT64_INPUTS(i) { | 87 FOR_FLOAT64_INPUTS(i) { |
88 int32_t expected = DoubleToInt32(*i); | 88 int32_t expected = DoubleToInt32(*i); |
89 CHECK_EQ(expected, r.Call(*i)); | 89 CHECK_EQ(expected, r.Call(*i)); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 TEST(Run_Wasm_I32AsmjsUConvertF32) { | 93 WASM_EXEC_TEST(I32AsmjsUConvertF32) { |
94 WasmRunner<uint32_t> r(MachineType::Float32()); | 94 WasmRunner<uint32_t> r(MachineType::Float32()); |
95 BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF32, WASM_GET_LOCAL(0))); | 95 BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF32, WASM_GET_LOCAL(0))); |
96 | 96 |
97 FOR_FLOAT32_INPUTS(i) { | 97 FOR_FLOAT32_INPUTS(i) { |
98 uint32_t expected = DoubleToUint32(*i); | 98 uint32_t expected = DoubleToUint32(*i); |
99 CHECK_EQ(expected, r.Call(*i)); | 99 CHECK_EQ(expected, r.Call(*i)); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 TEST(Run_Wasm_I32AsmjsUConvertF64) { | 103 WASM_EXEC_TEST(I32AsmjsUConvertF64) { |
104 WasmRunner<uint32_t> r(MachineType::Float64()); | 104 WasmRunner<uint32_t> r(MachineType::Float64()); |
105 BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF64, WASM_GET_LOCAL(0))); | 105 BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF64, WASM_GET_LOCAL(0))); |
106 | 106 |
107 FOR_FLOAT64_INPUTS(i) { | 107 FOR_FLOAT64_INPUTS(i) { |
108 uint32_t expected = DoubleToUint32(*i); | 108 uint32_t expected = DoubleToUint32(*i); |
109 CHECK_EQ(expected, r.Call(*i)); | 109 CHECK_EQ(expected, r.Call(*i)); |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 TEST(Run_Wasm_LoadMemI32_oob_asm) { | 113 WASM_EXEC_TEST(LoadMemI32_oob_asm) { |
114 TestingModule module; | 114 TestingModule module; |
115 module.origin = kAsmJsOrigin; | |
116 int32_t* memory = module.AddMemoryElems<int32_t>(8); | 115 int32_t* memory = module.AddMemoryElems<int32_t>(8); |
117 WasmRunner<int32_t> r(&module, MachineType::Uint32()); | 116 WasmRunner<int32_t> r(&module, MachineType::Uint32()); |
118 module.RandomizeMemory(1112); | 117 module.RandomizeMemory(1112); |
119 | 118 |
120 BUILD(r, WASM_UNOP(kExprI32AsmjsLoadMem, WASM_GET_LOCAL(0))); | 119 BUILD(r, WASM_UNOP(kExprI32AsmjsLoadMem, WASM_GET_LOCAL(0))); |
121 | 120 |
122 memory[0] = 999999; | 121 memory[0] = 999999; |
123 CHECK_EQ(999999, r.Call(0u)); | 122 CHECK_EQ(999999, r.Call(0u)); |
124 // TODO(titzer): offset 29-31 should also be OOB. | 123 // TODO(titzer): offset 29-31 should also be OOB. |
125 for (uint32_t offset = 32; offset < 40; offset++) { | 124 for (uint32_t offset = 32; offset < 40; offset++) { |
126 CHECK_EQ(0, r.Call(offset)); | 125 CHECK_EQ(0, r.Call(offset)); |
127 } | 126 } |
128 | 127 |
129 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { | 128 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { |
130 CHECK_EQ(0, r.Call(offset)); | 129 CHECK_EQ(0, r.Call(offset)); |
131 } | 130 } |
132 } | 131 } |
133 | 132 |
134 TEST(Run_Wasm_LoadMemF32_oob_asm) { | 133 WASM_EXEC_TEST(LoadMemF32_oob_asm) { |
135 TestingModule module; | 134 TestingModule module; |
136 module.origin = kAsmJsOrigin; | |
137 float* memory = module.AddMemoryElems<float>(8); | 135 float* memory = module.AddMemoryElems<float>(8); |
138 WasmRunner<float> r(&module, MachineType::Uint32()); | 136 WasmRunner<float> r(&module, MachineType::Uint32()); |
139 module.RandomizeMemory(1112); | 137 module.RandomizeMemory(1112); |
140 | 138 |
141 BUILD(r, WASM_UNOP(kExprF32AsmjsLoadMem, WASM_GET_LOCAL(0))); | 139 BUILD(r, WASM_UNOP(kExprF32AsmjsLoadMem, WASM_GET_LOCAL(0))); |
142 | 140 |
143 memory[0] = 9999.5f; | 141 memory[0] = 9999.5f; |
144 CHECK_EQ(9999.5f, r.Call(0u)); | 142 CHECK_EQ(9999.5f, r.Call(0u)); |
145 // TODO(titzer): offset 29-31 should also be OOB. | 143 // TODO(titzer): offset 29-31 should also be OOB. |
146 for (uint32_t offset = 32; offset < 40; offset++) { | 144 for (uint32_t offset = 32; offset < 40; offset++) { |
147 CHECK(std::isnan(r.Call(offset))); | 145 CHECK(std::isnan(r.Call(offset))); |
148 } | 146 } |
149 | 147 |
150 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { | 148 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { |
151 CHECK(std::isnan(r.Call(offset))); | 149 CHECK(std::isnan(r.Call(offset))); |
152 } | 150 } |
153 } | 151 } |
154 | 152 |
155 TEST(Run_Wasm_LoadMemF64_oob_asm) { | 153 WASM_EXEC_TEST(LoadMemF64_oob_asm) { |
156 TestingModule module; | 154 TestingModule module; |
157 module.origin = kAsmJsOrigin; | |
158 double* memory = module.AddMemoryElems<double>(8); | 155 double* memory = module.AddMemoryElems<double>(8); |
159 WasmRunner<double> r(&module, MachineType::Uint32()); | 156 WasmRunner<double> r(&module, MachineType::Uint32()); |
160 module.RandomizeMemory(1112); | 157 module.RandomizeMemory(1112); |
161 | 158 |
162 BUILD(r, WASM_UNOP(kExprF64AsmjsLoadMem, WASM_GET_LOCAL(0))); | 159 BUILD(r, WASM_UNOP(kExprF64AsmjsLoadMem, WASM_GET_LOCAL(0))); |
163 | 160 |
164 memory[0] = 9799.5; | 161 memory[0] = 9799.5; |
165 CHECK_EQ(9799.5, r.Call(0u)); | 162 CHECK_EQ(9799.5, r.Call(0u)); |
166 memory[1] = 11799.25; | 163 memory[1] = 11799.25; |
167 CHECK_EQ(11799.25, r.Call(8u)); | 164 CHECK_EQ(11799.25, r.Call(8u)); |
168 // TODO(titzer): offset 57-63 should also be OOB. | 165 // TODO(titzer): offset 57-63 should also be OOB. |
169 for (uint32_t offset = 64; offset < 80; offset++) { | 166 for (uint32_t offset = 64; offset < 80; offset++) { |
170 CHECK(std::isnan(r.Call(offset))); | 167 CHECK(std::isnan(r.Call(offset))); |
171 } | 168 } |
172 | 169 |
173 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { | 170 for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) { |
174 CHECK(std::isnan(r.Call(offset))); | 171 CHECK(std::isnan(r.Call(offset))); |
175 } | 172 } |
176 } | 173 } |
177 | 174 |
178 TEST(Run_Wasm_StoreMemI32_oob_asm) { | 175 WASM_EXEC_TEST(StoreMemI32_oob_asm) { |
179 TestingModule module; | 176 TestingModule module; |
180 module.origin = kAsmJsOrigin; | |
181 int32_t* memory = module.AddMemoryElems<int32_t>(8); | 177 int32_t* memory = module.AddMemoryElems<int32_t>(8); |
182 WasmRunner<int32_t> r(&module, MachineType::Uint32(), MachineType::Uint32()); | 178 WasmRunner<int32_t> r(&module, MachineType::Uint32(), MachineType::Uint32()); |
183 module.RandomizeMemory(1112); | 179 module.RandomizeMemory(1112); |
184 | 180 |
185 BUILD(r, WASM_BINOP(kExprI32AsmjsStoreMem, WASM_GET_LOCAL(0), | 181 BUILD(r, WASM_BINOP(kExprI32AsmjsStoreMem, WASM_GET_LOCAL(0), |
186 WASM_GET_LOCAL(1))); | 182 WASM_GET_LOCAL(1))); |
187 | 183 |
188 memory[0] = 7777; | 184 memory[0] = 7777; |
189 CHECK_EQ(999999, r.Call(0u, 999999)); | 185 CHECK_EQ(999999, r.Call(0u, 999999)); |
190 CHECK_EQ(999999, memory[0]); | 186 CHECK_EQ(999999, memory[0]); |
191 // TODO(titzer): offset 29-31 should also be OOB. | 187 // TODO(titzer): offset 29-31 should also be OOB. |
192 for (uint32_t offset = 32; offset < 40; offset++) { | 188 for (uint32_t offset = 32; offset < 40; offset++) { |
193 CHECK_EQ(8888, r.Call(offset, 8888)); | 189 CHECK_EQ(8888, r.Call(offset, 8888)); |
194 } | 190 } |
195 | 191 |
196 for (uint32_t offset = 0x10000000; offset < 0xF0000000; offset += 0x1000000) { | 192 for (uint32_t offset = 0x10000000; offset < 0xF0000000; offset += 0x1000000) { |
197 CHECK_EQ(7777, r.Call(offset, 7777)); | 193 CHECK_EQ(7777, r.Call(offset, 7777)); |
198 } | 194 } |
199 } | 195 } |
OLD | NEW |