Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include <stdint.h>
6 : #include <stdlib.h>
7 : #include <string.h>
8 :
9 : #include <memory>
10 :
11 : #include "src/assembler-inl.h"
12 : #include "src/wasm/wasm-interpreter.h"
13 : #include "test/cctest/cctest.h"
14 : #include "test/cctest/compiler/value-helper.h"
15 : #include "test/cctest/wasm/wasm-run-utils.h"
16 : #include "test/common/wasm/test-signatures.h"
17 : #include "test/common/wasm/wasm-macro-gen.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 : namespace wasm {
22 : namespace test_run_wasm_interpreter {
23 :
24 25879 : TEST(Run_WasmInt8Const_i) {
25 4 : WasmRunner<int32_t> r(ExecutionTier::kInterpreter);
26 : const byte kExpectedValue = 109;
27 : // return(kExpectedValue)
28 4 : BUILD(r, WASM_I32V_2(kExpectedValue));
29 8 : CHECK_EQ(kExpectedValue, r.Call());
30 4 : }
31 :
32 25879 : TEST(Run_WasmIfElse) {
33 4 : WasmRunner<int32_t, int32_t> r(ExecutionTier::kInterpreter);
34 4 : BUILD(r, WASM_IF_ELSE_I(WASM_GET_LOCAL(0), WASM_I32V_1(9), WASM_I32V_1(10)));
35 4 : CHECK_EQ(10, r.Call(0));
36 4 : CHECK_EQ(9, r.Call(1));
37 4 : }
38 :
39 25879 : TEST(Run_WasmIfReturn) {
40 4 : WasmRunner<int32_t, int32_t> r(ExecutionTier::kInterpreter);
41 4 : BUILD(r, WASM_IF(WASM_GET_LOCAL(0), WASM_RETURN1(WASM_I32V_2(77))),
42 : WASM_I32V_2(65));
43 4 : CHECK_EQ(65, r.Call(0));
44 4 : CHECK_EQ(77, r.Call(1));
45 4 : }
46 :
47 25879 : TEST(Run_WasmNopsN) {
48 : const int kMaxNops = 10;
49 : byte code[kMaxNops + 2];
50 48 : for (int nops = 0; nops < kMaxNops; nops++) {
51 40 : byte expected = static_cast<byte>(20 + nops);
52 : memset(code, kExprNop, sizeof(code));
53 40 : code[nops] = kExprI32Const;
54 40 : code[nops + 1] = expected;
55 :
56 40 : WasmRunner<int32_t> r(ExecutionTier::kInterpreter);
57 40 : r.Build(code, code + nops + 2);
58 80 : CHECK_EQ(expected, r.Call());
59 40 : }
60 4 : }
61 :
62 25879 : TEST(Run_WasmConstsN) {
63 : const int kMaxConsts = 5;
64 : byte code[kMaxConsts * 3];
65 : int32_t expected = 0;
66 20 : for (int count = 1; count < kMaxConsts; count++) {
67 40 : for (int i = 0; i < count; i++) {
68 40 : byte val = static_cast<byte>(count * 10 + i);
69 40 : code[i * 3] = kExprI32Const;
70 40 : code[i * 3 + 1] = val;
71 40 : if (i == (count - 1)) {
72 16 : code[i * 3 + 2] = kExprNop;
73 16 : expected = val;
74 : } else {
75 24 : code[i * 3 + 2] = kExprDrop;
76 : }
77 : }
78 :
79 16 : WasmRunner<int32_t> r(ExecutionTier::kInterpreter);
80 16 : r.Build(code, code + (count * 3));
81 16 : CHECK_EQ(expected, r.Call());
82 16 : }
83 4 : }
84 :
85 25879 : TEST(Run_WasmBlocksN) {
86 : const int kMaxNops = 10;
87 : const int kExtra = 5;
88 : byte code[kMaxNops + kExtra];
89 44 : for (int nops = 0; nops < kMaxNops; nops++) {
90 40 : byte expected = static_cast<byte>(30 + nops);
91 : memset(code, kExprNop, sizeof(code));
92 40 : code[0] = kExprBlock;
93 40 : code[1] = kLocalI32;
94 40 : code[2 + nops] = kExprI32Const;
95 40 : code[2 + nops + 1] = expected;
96 40 : code[2 + nops + 2] = kExprEnd;
97 :
98 40 : WasmRunner<int32_t> r(ExecutionTier::kInterpreter);
99 40 : r.Build(code, code + nops + kExtra);
100 80 : CHECK_EQ(expected, r.Call());
101 40 : }
102 4 : }
103 :
104 25879 : TEST(Run_WasmBlockBreakN) {
105 : const int kMaxNops = 10;
106 : const int kExtra = 6;
107 : int run = 0;
108 : byte code[kMaxNops + kExtra];
109 44 : for (int nops = 0; nops < kMaxNops; nops++) {
110 : // Place the break anywhere within the block.
111 180 : for (int index = 0; index < nops; index++) {
112 : memset(code, kExprNop, sizeof(code));
113 180 : code[0] = kExprBlock;
114 180 : code[1] = kLocalI32;
115 180 : code[sizeof(code) - 1] = kExprEnd;
116 :
117 180 : int expected = run++;
118 180 : code[2 + index + 0] = kExprI32Const;
119 180 : code[2 + index + 1] = static_cast<byte>(expected);
120 180 : code[2 + index + 2] = kExprBr;
121 180 : code[2 + index + 3] = 0;
122 :
123 180 : WasmRunner<int32_t> r(ExecutionTier::kInterpreter);
124 180 : r.Build(code, code + kMaxNops + kExtra);
125 180 : CHECK_EQ(expected, r.Call());
126 180 : }
127 : }
128 4 : }
129 :
130 25879 : TEST(Run_Wasm_nested_ifs_i) {
131 4 : WasmRunner<int32_t, int32_t, int32_t> r(ExecutionTier::kInterpreter);
132 :
133 4 : BUILD(
134 : r,
135 : WASM_IF_ELSE_I(
136 : WASM_GET_LOCAL(0),
137 : WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(11), WASM_I32V_1(12)),
138 : WASM_IF_ELSE_I(WASM_GET_LOCAL(1), WASM_I32V_1(13), WASM_I32V_1(14))));
139 :
140 4 : CHECK_EQ(11, r.Call(1, 1));
141 4 : CHECK_EQ(12, r.Call(1, 0));
142 4 : CHECK_EQ(13, r.Call(0, 1));
143 4 : CHECK_EQ(14, r.Call(0, 0));
144 4 : }
145 :
146 : // Make tests more robust by not hard-coding offsets of various operations.
147 : // The {Find} method finds the offsets for the given bytecodes, returning
148 : // the offsets in an array.
149 8 : std::unique_ptr<int[]> Find(byte* code, size_t code_size, int n, ...) {
150 : va_list vl;
151 8 : va_start(vl, n);
152 :
153 8 : std::unique_ptr<int[]> offsets(new int[n]);
154 :
155 24 : for (int i = 0; i < n; i++) {
156 32 : offsets[i] = -1;
157 : }
158 :
159 : int pos = 0;
160 8 : WasmOpcode current = static_cast<WasmOpcode>(va_arg(vl, int));
161 40 : for (size_t i = 0; i < code_size; i++) {
162 40 : if (code[i] == current) {
163 32 : offsets[pos++] = static_cast<int>(i);
164 16 : if (pos == n) break;
165 8 : current = static_cast<WasmOpcode>(va_arg(vl, int));
166 : }
167 : }
168 8 : va_end(vl);
169 :
170 8 : return offsets;
171 : }
172 :
173 25879 : TEST(Breakpoint_I32Add) {
174 : static const int kLocalsDeclSize = 1;
175 : static const int kNumBreakpoints = 3;
176 4 : byte code[] = {WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))};
177 : std::unique_ptr<int[]> offsets =
178 : Find(code, sizeof(code), kNumBreakpoints, kExprGetLocal, kExprGetLocal,
179 4 : kExprI32Add);
180 :
181 8 : WasmRunner<int32_t, uint32_t, uint32_t> r(ExecutionTier::kInterpreter);
182 :
183 4 : r.Build(code, code + arraysize(code));
184 :
185 : WasmInterpreter* interpreter = r.interpreter();
186 4 : WasmInterpreter::Thread* thread = interpreter->GetThread(0);
187 16 : for (int i = 0; i < kNumBreakpoints; i++) {
188 24 : interpreter->SetBreakpoint(r.function(), kLocalsDeclSize + offsets[i],
189 24 : true);
190 : }
191 :
192 464 : FOR_UINT32_INPUTS(a) {
193 928 : for (uint32_t b = 11; b < 3000000000u; b += 1000000000u) {
194 696 : thread->Reset();
195 696 : WasmValue args[] = {WasmValue(a), WasmValue(b)};
196 696 : thread->InitFrame(r.function(), args);
197 :
198 2784 : for (int i = 0; i < kNumBreakpoints; i++) {
199 2088 : thread->Run(); // run to next breakpoint
200 : // Check the thread stopped at the right pc.
201 2088 : CHECK_EQ(WasmInterpreter::PAUSED, thread->state());
202 4176 : CHECK_EQ(static_cast<size_t>(kLocalsDeclSize + offsets[i]),
203 : thread->GetBreakpointPc());
204 : }
205 :
206 696 : thread->Run(); // run to completion
207 :
208 : // Check the thread finished with the right value.
209 696 : CHECK_EQ(WasmInterpreter::FINISHED, thread->state());
210 696 : uint32_t expected = (a) + (b);
211 1392 : CHECK_EQ(expected, thread->GetReturnValue().to<uint32_t>());
212 : }
213 : }
214 4 : }
215 :
216 25879 : TEST(Step_I32Mul) {
217 : static const int kTraceLength = 4;
218 4 : byte code[] = {WASM_I32_MUL(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))};
219 :
220 4 : WasmRunner<int32_t, uint32_t, uint32_t> r(ExecutionTier::kInterpreter);
221 :
222 4 : r.Build(code, code + arraysize(code));
223 :
224 : WasmInterpreter* interpreter = r.interpreter();
225 4 : WasmInterpreter::Thread* thread = interpreter->GetThread(0);
226 :
227 236 : FOR_UINT32_INPUTS(a) {
228 928 : for (uint32_t b = 33; b < 3000000000u; b += 1000000000u) {
229 696 : thread->Reset();
230 696 : WasmValue args[] = {WasmValue(a), WasmValue(b)};
231 696 : thread->InitFrame(r.function(), args);
232 :
233 : // Run instructions one by one.
234 2784 : for (int i = 0; i < kTraceLength - 1; i++) {
235 : thread->Step();
236 : // Check the thread stopped.
237 2088 : CHECK_EQ(WasmInterpreter::PAUSED, thread->state());
238 : }
239 :
240 : // Run last instruction.
241 : thread->Step();
242 :
243 : // Check the thread finished with the right value.
244 696 : CHECK_EQ(WasmInterpreter::FINISHED, thread->state());
245 696 : uint32_t expected = (a) * (b);
246 1392 : CHECK_EQ(expected, thread->GetReturnValue().to<uint32_t>());
247 : }
248 4 : }
249 4 : }
250 :
251 25879 : TEST(Breakpoint_I32And_disable) {
252 : static const int kLocalsDeclSize = 1;
253 : static const int kNumBreakpoints = 1;
254 4 : byte code[] = {WASM_I32_AND(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))};
255 : std::unique_ptr<int[]> offsets =
256 4 : Find(code, sizeof(code), kNumBreakpoints, kExprI32And);
257 :
258 8 : WasmRunner<int32_t, uint32_t, uint32_t> r(ExecutionTier::kInterpreter);
259 :
260 4 : r.Build(code, code + arraysize(code));
261 :
262 : WasmInterpreter* interpreter = r.interpreter();
263 4 : WasmInterpreter::Thread* thread = interpreter->GetThread(0);
264 :
265 236 : FOR_UINT32_INPUTS(a) {
266 928 : for (uint32_t b = 11; b < 3000000000u; b += 1000000000u) {
267 : // Run with and without breakpoints.
268 1392 : for (int do_break = 0; do_break < 2; do_break++) {
269 1392 : interpreter->SetBreakpoint(r.function(), kLocalsDeclSize + offsets[0],
270 4176 : do_break);
271 1392 : thread->Reset();
272 1392 : WasmValue args[] = {WasmValue(a), WasmValue(b)};
273 1392 : thread->InitFrame(r.function(), args);
274 :
275 1392 : if (do_break) {
276 696 : thread->Run(); // run to next breakpoint
277 : // Check the thread stopped at the right pc.
278 696 : CHECK_EQ(WasmInterpreter::PAUSED, thread->state());
279 1392 : CHECK_EQ(static_cast<size_t>(kLocalsDeclSize + offsets[0]),
280 : thread->GetBreakpointPc());
281 : }
282 :
283 1392 : thread->Run(); // run to completion
284 :
285 : // Check the thread finished with the right value.
286 1392 : CHECK_EQ(WasmInterpreter::FINISHED, thread->state());
287 1392 : uint32_t expected = (a) & (b);
288 2784 : CHECK_EQ(expected, thread->GetReturnValue().to<uint32_t>());
289 : }
290 : }
291 : }
292 4 : }
293 :
294 25879 : TEST(MemoryGrow) {
295 : {
296 4 : WasmRunner<int32_t, uint32_t> r(ExecutionTier::kInterpreter);
297 4 : r.builder().AddMemory(kWasmPageSize);
298 4 : r.builder().SetMaxMemPages(10);
299 4 : BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0)));
300 4 : CHECK_EQ(1, r.Call(1));
301 : }
302 : {
303 4 : WasmRunner<int32_t, uint32_t> r(ExecutionTier::kInterpreter);
304 4 : r.builder().AddMemory(kWasmPageSize);
305 4 : r.builder().SetMaxMemPages(10);
306 4 : BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0)));
307 4 : CHECK_EQ(-1, r.Call(11));
308 : }
309 4 : }
310 :
311 25879 : TEST(MemoryGrowPreservesData) {
312 : int32_t index = 16;
313 : int32_t value = 2335;
314 4 : WasmRunner<int32_t, uint32_t> r(ExecutionTier::kInterpreter);
315 4 : r.builder().AddMemory(kWasmPageSize);
316 4 : BUILD(r, WASM_STORE_MEM(MachineType::Int32(), WASM_I32V(index),
317 : WASM_I32V(value)),
318 : WASM_GROW_MEMORY(WASM_GET_LOCAL(0)), WASM_DROP,
319 : WASM_LOAD_MEM(MachineType::Int32(), WASM_I32V(index)));
320 4 : CHECK_EQ(value, r.Call(1));
321 4 : }
322 :
323 25879 : TEST(MemoryGrowInvalidSize) {
324 : // Grow memory by an invalid amount without initial memory.
325 4 : WasmRunner<int32_t, uint32_t> r(ExecutionTier::kInterpreter);
326 4 : r.builder().AddMemory(kWasmPageSize);
327 4 : BUILD(r, WASM_GROW_MEMORY(WASM_GET_LOCAL(0)));
328 4 : CHECK_EQ(-1, r.Call(1048575));
329 4 : }
330 :
331 25879 : TEST(TestPossibleNondeterminism) {
332 : {
333 4 : WasmRunner<int32_t, float> r(ExecutionTier::kInterpreter);
334 4 : BUILD(r, WASM_I32_REINTERPRET_F32(WASM_GET_LOCAL(0)));
335 4 : r.Call(1048575.5f);
336 4 : CHECK(!r.possible_nondeterminism());
337 4 : r.Call(std::numeric_limits<float>::quiet_NaN());
338 4 : CHECK(!r.possible_nondeterminism());
339 : }
340 : {
341 4 : WasmRunner<int64_t, double> r(ExecutionTier::kInterpreter);
342 4 : BUILD(r, WASM_I64_REINTERPRET_F64(WASM_GET_LOCAL(0)));
343 4 : r.Call(16.0);
344 4 : CHECK(!r.possible_nondeterminism());
345 4 : r.Call(std::numeric_limits<double>::quiet_NaN());
346 4 : CHECK(!r.possible_nondeterminism());
347 : }
348 : {
349 4 : WasmRunner<float, float> r(ExecutionTier::kInterpreter);
350 4 : BUILD(r, WASM_F32_COPYSIGN(WASM_F32(42.0f), WASM_GET_LOCAL(0)));
351 4 : r.Call(16.0f);
352 4 : CHECK(!r.possible_nondeterminism());
353 4 : r.Call(std::numeric_limits<double>::quiet_NaN());
354 4 : CHECK(!r.possible_nondeterminism());
355 : }
356 : {
357 4 : WasmRunner<double, double> r(ExecutionTier::kInterpreter);
358 4 : BUILD(r, WASM_F64_COPYSIGN(WASM_F64(42.0), WASM_GET_LOCAL(0)));
359 4 : r.Call(16.0);
360 4 : CHECK(!r.possible_nondeterminism());
361 4 : r.Call(std::numeric_limits<double>::quiet_NaN());
362 4 : CHECK(!r.possible_nondeterminism());
363 : }
364 : {
365 : int32_t index = 16;
366 4 : WasmRunner<int32_t, float> r(ExecutionTier::kInterpreter);
367 4 : r.builder().AddMemory(kWasmPageSize);
368 4 : BUILD(r, WASM_STORE_MEM(MachineType::Float32(), WASM_I32V(index),
369 : WASM_GET_LOCAL(0)),
370 : WASM_I32V(index));
371 4 : r.Call(1345.3456f);
372 4 : CHECK(!r.possible_nondeterminism());
373 4 : r.Call(std::numeric_limits<float>::quiet_NaN());
374 4 : CHECK(!r.possible_nondeterminism());
375 : }
376 : {
377 : int32_t index = 16;
378 4 : WasmRunner<int32_t, double> r(ExecutionTier::kInterpreter);
379 4 : r.builder().AddMemory(kWasmPageSize);
380 4 : BUILD(r, WASM_STORE_MEM(MachineType::Float64(), WASM_I32V(index),
381 : WASM_GET_LOCAL(0)),
382 : WASM_I32V(index));
383 4 : r.Call(1345.3456);
384 4 : CHECK(!r.possible_nondeterminism());
385 4 : r.Call(std::numeric_limits<double>::quiet_NaN());
386 4 : CHECK(!r.possible_nondeterminism());
387 : }
388 : {
389 4 : WasmRunner<float, float> r(ExecutionTier::kInterpreter);
390 4 : BUILD(r, WASM_F32_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
391 4 : r.Call(1048575.5f);
392 4 : CHECK(!r.possible_nondeterminism());
393 4 : r.Call(std::numeric_limits<float>::quiet_NaN());
394 4 : CHECK(r.possible_nondeterminism());
395 : }
396 : {
397 4 : WasmRunner<double, double> r(ExecutionTier::kInterpreter);
398 4 : BUILD(r, WASM_F64_ADD(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
399 4 : r.Call(16.0);
400 4 : CHECK(!r.possible_nondeterminism());
401 4 : r.Call(std::numeric_limits<double>::quiet_NaN());
402 4 : CHECK(r.possible_nondeterminism());
403 : }
404 : {
405 4 : WasmRunner<int32_t, float> r(ExecutionTier::kInterpreter);
406 4 : BUILD(r, WASM_F32_EQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
407 4 : r.Call(16.0);
408 4 : CHECK(!r.possible_nondeterminism());
409 4 : r.Call(std::numeric_limits<float>::quiet_NaN());
410 4 : CHECK(!r.possible_nondeterminism());
411 : }
412 : {
413 4 : WasmRunner<int32_t, double> r(ExecutionTier::kInterpreter);
414 4 : BUILD(r, WASM_F64_EQ(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
415 4 : r.Call(16.0);
416 4 : CHECK(!r.possible_nondeterminism());
417 4 : r.Call(std::numeric_limits<double>::quiet_NaN());
418 4 : CHECK(!r.possible_nondeterminism());
419 : }
420 : {
421 4 : WasmRunner<float, float> r(ExecutionTier::kInterpreter);
422 4 : BUILD(r, WASM_F32_MIN(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
423 4 : r.Call(1048575.5f);
424 4 : CHECK(!r.possible_nondeterminism());
425 4 : r.Call(std::numeric_limits<float>::quiet_NaN());
426 4 : CHECK(r.possible_nondeterminism());
427 : }
428 : {
429 4 : WasmRunner<double, double> r(ExecutionTier::kInterpreter);
430 4 : BUILD(r, WASM_F64_MAX(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)));
431 4 : r.Call(16.0);
432 4 : CHECK(!r.possible_nondeterminism());
433 4 : r.Call(std::numeric_limits<double>::quiet_NaN());
434 4 : CHECK(r.possible_nondeterminism());
435 : }
436 4 : }
437 :
438 25879 : TEST(WasmInterpreterActivations) {
439 4 : WasmRunner<void> r(ExecutionTier::kInterpreter);
440 4 : Isolate* isolate = r.main_isolate();
441 4 : BUILD(r, WASM_UNREACHABLE);
442 :
443 : WasmInterpreter* interpreter = r.interpreter();
444 4 : WasmInterpreter::Thread* thread = interpreter->GetThread(0);
445 4 : CHECK_EQ(0, thread->NumActivations());
446 4 : uint32_t act0 = thread->StartActivation();
447 4 : CHECK_EQ(0, act0);
448 4 : thread->InitFrame(r.function(), nullptr);
449 4 : uint32_t act1 = thread->StartActivation();
450 4 : CHECK_EQ(1, act1);
451 4 : thread->InitFrame(r.function(), nullptr);
452 4 : CHECK_EQ(2, thread->NumActivations());
453 4 : CHECK_EQ(2, thread->GetFrameCount());
454 4 : CHECK_EQ(WasmInterpreter::TRAPPED, thread->Run());
455 4 : thread->RaiseException(isolate, handle(Smi::kZero, isolate));
456 4 : CHECK_EQ(1, thread->GetFrameCount());
457 4 : CHECK_EQ(2, thread->NumActivations());
458 4 : thread->FinishActivation(act1);
459 4 : isolate->clear_pending_exception();
460 4 : CHECK_EQ(1, thread->GetFrameCount());
461 4 : CHECK_EQ(1, thread->NumActivations());
462 4 : CHECK_EQ(WasmInterpreter::TRAPPED, thread->Run());
463 4 : thread->RaiseException(isolate, handle(Smi::kZero, isolate));
464 4 : CHECK_EQ(0, thread->GetFrameCount());
465 4 : CHECK_EQ(1, thread->NumActivations());
466 4 : thread->FinishActivation(act0);
467 4 : isolate->clear_pending_exception();
468 4 : CHECK_EQ(0, thread->NumActivations());
469 4 : }
470 :
471 25879 : TEST(InterpreterLoadWithoutMemory) {
472 4 : WasmRunner<int32_t, int32_t> r(ExecutionTier::kInterpreter);
473 4 : r.builder().AddMemory(0);
474 4 : BUILD(r, WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(0)));
475 8 : CHECK_TRAP32(r.Call(0));
476 4 : }
477 :
478 : } // namespace test_run_wasm_interpreter
479 : } // namespace wasm
480 : } // namespace internal
481 77625 : } // namespace v8
|