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