1 // Copyright 2015 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 "test/unittests/test-utils.h" 6 7 #include "src/v8.h" 8 9 #include "test/cctest/wasm/test-signatures.h" 10 11 #include "src/objects.h" 12 13 #include "src/wasm/ast-decoder.h" 14 #include "src/wasm/wasm-macro-gen.h" 15 #include "src/wasm/wasm-module.h" 16 17 namespace v8 { 18 namespace internal { 19 namespace wasm { 20 21 static const byte kCodeGetLocal0[] = {kExprGetLocal, 0}; 22 static const byte kCodeGetLocal1[] = {kExprGetLocal, 1}; 23 static const byte kCodeSetLocal0[] = {kExprSetLocal, 0, kExprI8Const, 0}; 24 25 static const LocalType kLocalTypes[] = {kAstI32, kAstI64, kAstF32, kAstF64}; 26 static const MachineType machineTypes[] = { 27 MachineType::Int8(), MachineType::Uint8(), MachineType::Int16(), 28 MachineType::Uint16(), MachineType::Int32(), MachineType::Uint32(), 29 MachineType::Int64(), MachineType::Uint64(), MachineType::Float32(), 30 MachineType::Float64()}; 31 32 static const WasmOpcode kInt32BinopOpcodes[] = { 33 kExprI32Add, kExprI32Sub, kExprI32Mul, kExprI32DivS, kExprI32DivU, 34 kExprI32RemS, kExprI32RemU, kExprI32And, kExprI32Ior, kExprI32Xor, 35 kExprI32Shl, kExprI32ShrU, kExprI32ShrS, kExprI32Eq, kExprI32LtS, 36 kExprI32LeS, kExprI32LtU, kExprI32LeU}; 37 38 39 #define EXPECT_VERIFIES(env, x) Verify(kSuccess, env, x, x + arraysize(x)) 40 41 #define EXPECT_FAILURE(env, x) Verify(kError, env, x, x + arraysize(x)) 42 43 #define EXPECT_VERIFIES_INLINE(env, ...) \ 44 do { \ 45 static byte code[] = {__VA_ARGS__}; \ 46 Verify(kSuccess, env, code, code + arraysize(code)); \ 47 } while (false) 48 49 50 #define EXPECT_FAILURE_INLINE(env, ...) \ 51 do { \ 52 static byte code[] = {__VA_ARGS__}; \ 53 Verify(kError, env, code, code + arraysize(code)); \ 54 } while (false) 55 56 #define VERIFY(...) \ 57 do { \ 58 static const byte code[] = {__VA_ARGS__}; \ 59 Verify(kSuccess, &env_v_i, code, code + sizeof(code)); \ 60 } while (false) 61 62 63 class WasmDecoderTest : public TestWithZone { 64 public: 65 WasmDecoderTest() : TestWithZone(), sigs() { 66 init_env(&env_i_i, sigs.i_i()); 67 init_env(&env_v_v, sigs.v_v()); 68 init_env(&env_v_i, sigs.v_i()); 69 init_env(&env_i_f, sigs.i_f()); 70 init_env(&env_i_d, sigs.i_d()); 71 init_env(&env_l_l, sigs.l_l()); 72 init_env(&env_f_ff, sigs.f_ff()); 73 init_env(&env_d_dd, sigs.d_dd()); 74 } 75 76 TestSignatures sigs; 77 78 FunctionEnv env_i_i; 79 FunctionEnv env_v_v; 80 FunctionEnv env_v_i; 81 FunctionEnv env_i_f; 82 FunctionEnv env_i_d; 83 FunctionEnv env_l_l; 84 FunctionEnv env_f_ff; 85 FunctionEnv env_d_dd; 86 87 static void init_env(FunctionEnv* env, FunctionSig* sig) { 88 env->module = nullptr; 89 env->sig = sig; 90 env->local_int32_count = 0; 91 env->local_int64_count = 0; 92 env->local_float32_count = 0; 93 env->local_float64_count = 0; 94 env->SumLocals(); 95 } 96 97 // A wrapper around VerifyWasmCode() that renders a nice failure message. 98 void Verify(ErrorCode expected, FunctionEnv* env, const byte* start, 99 const byte* end) { 100 TreeResult result = VerifyWasmCode(env, start, end); 101 if (result.error_code != expected) { 102 ptrdiff_t pc = result.error_pc - result.start; 103 ptrdiff_t pt = result.error_pt - result.start; 104 std::ostringstream str; 105 if (expected == kSuccess) { 106 str << "Verification failed: " << result.error_code << " pc = +" << pc; 107 if (result.error_pt) str << ", pt = +" << pt; 108 str << ", msg = " << result.error_msg.get(); 109 } else { 110 str << "Verification expected: " << expected << ", but got " 111 << result.error_code; 112 if (result.error_code != kSuccess) { 113 str << " pc = +" << pc; 114 if (result.error_pt) str << ", pt = +" << pt; 115 } 116 } 117 FATAL(str.str().c_str()); 118 } 119 } 120 121 void TestBinop(WasmOpcode opcode, FunctionSig* success) { 122 // op(local[0], local[1]) 123 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0, kExprGetLocal, 124 1}; 125 FunctionEnv env; 126 init_env(&env, success); 127 EXPECT_VERIFIES(&env, code); 128 129 // Try all combinations of return and parameter types. 130 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 131 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 132 for (size_t k = 0; k < arraysize(kLocalTypes); k++) { 133 LocalType types[] = {kLocalTypes[i], kLocalTypes[j], kLocalTypes[k]}; 134 if (types[0] != success->GetReturn(0) || 135 types[1] != success->GetParam(0) || 136 types[2] != success->GetParam(1)) { 137 // Test signature mismatch. 138 FunctionSig sig(1, 2, types); 139 init_env(&env, &sig); 140 EXPECT_FAILURE(&env, code); 141 } 142 } 143 } 144 } 145 } 146 147 void TestUnop(WasmOpcode opcode, FunctionSig* success) { 148 TestUnop(opcode, success->GetReturn(), success->GetParam(0)); 149 } 150 151 void TestUnop(WasmOpcode opcode, LocalType ret_type, LocalType param_type) { 152 // Return(op(local[0])) 153 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0}; 154 FunctionEnv env; 155 { 156 LocalType types[] = {ret_type, param_type}; 157 FunctionSig sig(1, 1, types); 158 init_env(&env, &sig); 159 EXPECT_VERIFIES(&env, code); 160 } 161 162 // Try all combinations of return and parameter types. 163 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 164 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 165 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; 166 if (types[0] != ret_type || types[1] != param_type) { 167 // Test signature mismatch. 168 FunctionSig sig(1, 1, types); 169 init_env(&env, &sig); 170 EXPECT_FAILURE(&env, code); 171 } 172 } 173 } 174 } 175 }; 176 177 178 static FunctionEnv CreateInt32FunctionEnv(FunctionSig* sig, int count) { 179 FunctionEnv env; 180 env.module = nullptr; 181 env.sig = sig; 182 env.local_int32_count = count; 183 env.local_float64_count = 0; 184 env.local_float32_count = 0; 185 env.total_locals = static_cast<unsigned>(count + sig->parameter_count()); 186 return env; 187 } 188 189 190 TEST_F(WasmDecoderTest, Int8Const) { 191 byte code[] = {kExprI8Const, 0}; 192 for (int i = -128; i < 128; i++) { 193 code[1] = static_cast<byte>(i); 194 EXPECT_VERIFIES(&env_i_i, code); 195 } 196 } 197 198 199 TEST_F(WasmDecoderTest, EmptyFunction) { 200 byte code[] = {0}; 201 Verify(kSuccess, &env_v_v, code, code); 202 Verify(kError, &env_i_i, code, code); 203 } 204 205 206 TEST_F(WasmDecoderTest, IncompleteIf1) { 207 byte code[] = {kExprIf}; 208 EXPECT_FAILURE(&env_v_v, code); 209 EXPECT_FAILURE(&env_i_i, code); 210 } 211 212 213 TEST_F(WasmDecoderTest, IncompleteIf2) { 214 byte code[] = {kExprIf, kExprI8Const, 0}; 215 EXPECT_FAILURE(&env_v_v, code); 216 EXPECT_FAILURE(&env_i_i, code); 217 } 218 219 220 TEST_F(WasmDecoderTest, Int8Const_fallthru) { 221 byte code[] = {kExprI8Const, 0, kExprI8Const, 1}; 222 EXPECT_VERIFIES(&env_i_i, code); 223 } 224 225 226 TEST_F(WasmDecoderTest, Int32Const) { 227 byte code[] = {kExprI32Const, 0, 0, 0, 0}; 228 int32_t* ptr = reinterpret_cast<int32_t*>(code + 1); 229 const int kInc = 4498211; 230 for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { 231 *ptr = i; 232 EXPECT_VERIFIES(&env_i_i, code); 233 } 234 } 235 236 237 TEST_F(WasmDecoderTest, Int8Const_fallthru2) { 238 byte code[] = {kExprI8Const, 0, kExprI32Const, 1, 2, 3, 4}; 239 EXPECT_VERIFIES(&env_i_i, code); 240 } 241 242 243 TEST_F(WasmDecoderTest, Int64Const) { 244 byte code[] = {kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0}; 245 int64_t* ptr = reinterpret_cast<int64_t*>(code + 1); 246 const int kInc = 4498211; 247 for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { 248 *ptr = (static_cast<int64_t>(i) << 32) | i; 249 EXPECT_VERIFIES(&env_l_l, code); 250 } 251 } 252 253 254 // TODO(tizer): Fix on arm and reenable. 255 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 256 257 TEST_F(WasmDecoderTest, Float32Const) { 258 byte code[] = {kExprF32Const, 0, 0, 0, 0}; 259 float* ptr = reinterpret_cast<float*>(code + 1); 260 for (int i = 0; i < 30; i++) { 261 *ptr = i * -7.75f; 262 EXPECT_VERIFIES(&env_f_ff, code); 263 } 264 } 265 266 267 TEST_F(WasmDecoderTest, Float64Const) { 268 byte code[] = {kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0}; 269 double* ptr = reinterpret_cast<double*>(code + 1); 270 for (int i = 0; i < 30; i++) { 271 *ptr = i * 33.45; 272 EXPECT_VERIFIES(&env_d_dd, code); 273 } 274 } 275 276 #endif 277 278 279 TEST_F(WasmDecoderTest, Int32Const_off_end) { 280 byte code[] = {kExprI32Const, 0xaa, 0xbb, 0xcc, 0x44}; 281 282 for (int size = 1; size <= 4; size++) { 283 Verify(kError, &env_i_i, code, code + size); 284 } 285 } 286 287 288 TEST_F(WasmDecoderTest, GetLocal0_param) { 289 EXPECT_VERIFIES(&env_i_i, kCodeGetLocal0); 290 } 291 292 293 TEST_F(WasmDecoderTest, GetLocal0_local) { 294 FunctionEnv env; 295 init_env(&env, sigs.i_v()); 296 env.AddLocals(kAstI32, 1); 297 EXPECT_VERIFIES(&env, kCodeGetLocal0); 298 } 299 300 301 TEST_F(WasmDecoderTest, GetLocal0_param_n) { 302 FunctionSig* array[] = {sigs.i_i(), sigs.i_ii(), sigs.i_iii()}; 303 304 for (size_t i = 0; i < arraysize(array); i++) { 305 FunctionEnv env = CreateInt32FunctionEnv(array[i], 0); 306 EXPECT_VERIFIES(&env, kCodeGetLocal0); 307 } 308 } 309 310 311 TEST_F(WasmDecoderTest, GetLocalN_local) { 312 for (byte i = 1; i < 8; i++) { 313 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), i); 314 for (byte j = 0; j < i; j++) { 315 byte code[] = {kExprGetLocal, j}; 316 EXPECT_VERIFIES(&env, code); 317 } 318 } 319 } 320 321 322 TEST_F(WasmDecoderTest, GetLocal0_fail_no_params) { 323 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), 0); 324 325 EXPECT_FAILURE(&env, kCodeGetLocal0); 326 } 327 328 329 TEST_F(WasmDecoderTest, GetLocal1_fail_no_locals) { 330 EXPECT_FAILURE(&env_i_i, kCodeGetLocal1); 331 } 332 333 334 TEST_F(WasmDecoderTest, GetLocal_off_end) { 335 static const byte code[] = {kExprGetLocal}; 336 EXPECT_FAILURE(&env_i_i, code); 337 } 338 339 340 TEST_F(WasmDecoderTest, GetLocal_varint) { 341 env_i_i.local_int32_count = 1000000000; 342 env_i_i.total_locals += 1000000000; 343 344 { 345 static const byte code[] = {kExprGetLocal, 0xFF, 0x01}; 346 EXPECT_VERIFIES(&env_i_i, code); 347 EXPECT_FAILURE(&env_i_f, code); 348 } 349 350 { 351 static const byte code[] = {kExprGetLocal, 0xF0, 0x80, 0x01}; 352 EXPECT_VERIFIES(&env_i_i, code); 353 EXPECT_FAILURE(&env_i_f, code); 354 } 355 356 { 357 static const byte code[] = {kExprGetLocal, 0xF2, 0x81, 0x82, 0x01}; 358 EXPECT_VERIFIES(&env_i_i, code); 359 EXPECT_FAILURE(&env_i_f, code); 360 } 361 362 { 363 static const byte code[] = {kExprGetLocal, 0xF3, 0xA1, 0xB1, 0xC1, 0x01}; 364 EXPECT_VERIFIES(&env_i_i, code); 365 EXPECT_FAILURE(&env_i_f, code); 366 } 367 } 368 369 370 TEST_F(WasmDecoderTest, Binops_off_end) { 371 byte code1[] = {0}; // [opcode] 372 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { 373 code1[0] = kInt32BinopOpcodes[i]; 374 EXPECT_FAILURE(&env_i_i, code1); 375 } 376 377 byte code3[] = {0, kExprGetLocal, 0}; // [opcode] [expr] 378 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { 379 code3[0] = kInt32BinopOpcodes[i]; 380 EXPECT_FAILURE(&env_i_i, code3); 381 } 382 383 byte code4[] = {0, kExprGetLocal, 0, 0}; // [opcode] [expr] [opcode] 384 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { 385 code4[0] = kInt32BinopOpcodes[i]; 386 code4[3] = kInt32BinopOpcodes[i]; 387 EXPECT_FAILURE(&env_i_i, code4); 388 } 389 } 390 391 392 //=================================================================== 393 //== Statements 394 //=================================================================== 395 TEST_F(WasmDecoderTest, Nop) { 396 static const byte code[] = {kExprNop}; 397 EXPECT_VERIFIES(&env_v_v, code); 398 } 399 400 401 TEST_F(WasmDecoderTest, SetLocal0_param) { 402 static const byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; 403 EXPECT_VERIFIES(&env_i_i, code); 404 } 405 406 407 TEST_F(WasmDecoderTest, SetLocal0_local) { 408 byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; 409 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), 1); 410 411 EXPECT_VERIFIES(&env, code); 412 } 413 414 415 TEST_F(WasmDecoderTest, SetLocalN_local) { 416 for (byte i = 1; i < 8; i++) { 417 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), i); 418 for (byte j = 0; j < i; j++) { 419 byte code[] = {kExprSetLocal, j, kExprI8Const, i}; 420 EXPECT_VERIFIES(&env, code); 421 } 422 } 423 } 424 425 426 TEST_F(WasmDecoderTest, Block0) { 427 static const byte code[] = {kExprBlock, 0}; 428 EXPECT_VERIFIES(&env_v_v, code); 429 } 430 431 432 TEST_F(WasmDecoderTest, Block0_fallthru1) { 433 static const byte code[] = {kExprBlock, 0, kExprBlock, 0}; 434 EXPECT_VERIFIES(&env_v_v, code); 435 } 436 437 438 TEST_F(WasmDecoderTest, Block1) { 439 static const byte code[] = {kExprBlock, 1, kExprSetLocal, 0, kExprI8Const, 0}; 440 EXPECT_VERIFIES(&env_i_i, code); 441 } 442 443 444 TEST_F(WasmDecoderTest, Block0_fallthru2) { 445 static const byte code[] = {kExprBlock, 0, kExprSetLocal, 0, kExprI8Const, 0}; 446 EXPECT_VERIFIES(&env_i_i, code); 447 } 448 449 450 TEST_F(WasmDecoderTest, Block2) { 451 static const byte code[] = {kExprBlock, 2, // -- 452 kExprSetLocal, 0, kExprI8Const, 0, // -- 453 kExprSetLocal, 0, kExprI8Const, 0}; // -- 454 EXPECT_VERIFIES(&env_i_i, code); 455 } 456 457 458 TEST_F(WasmDecoderTest, Block2_fallthru) { 459 static const byte code[] = {kExprBlock, 2, // -- 460 kExprSetLocal, 0, kExprI8Const, 0, // -- 461 kExprSetLocal, 0, kExprI8Const, 0, // -- 462 kExprI8Const, 11}; // -- 463 EXPECT_VERIFIES(&env_i_i, code); 464 } 465 466 467 TEST_F(WasmDecoderTest, BlockN) { 468 byte block[] = {kExprBlock, 2}; 469 470 for (size_t i = 0; i < 10; i++) { 471 size_t total = sizeof(block) + sizeof(kCodeSetLocal0) * i; 472 byte* code = reinterpret_cast<byte*>(malloc(total)); 473 memcpy(code, block, sizeof(block)); 474 code[1] = static_cast<byte>(i); 475 for (size_t j = 0; j < i; j++) { 476 memcpy(code + sizeof(block) + j * sizeof(kCodeSetLocal0), kCodeSetLocal0, 477 sizeof(kCodeSetLocal0)); 478 } 479 Verify(kSuccess, &env_v_i, code, code + total); 480 free(code); 481 } 482 } 483 484 485 TEST_F(WasmDecoderTest, BlockN_off_end) { 486 for (byte i = 2; i < 10; i++) { 487 byte code[] = {kExprBlock, i, kExprNop}; 488 EXPECT_FAILURE(&env_v_v, code); 489 } 490 } 491 492 493 TEST_F(WasmDecoderTest, Block1_break) { 494 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; 495 EXPECT_VERIFIES(&env_v_v, code); 496 } 497 498 499 TEST_F(WasmDecoderTest, Block2_break) { 500 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 0, kExprNop}; 501 EXPECT_VERIFIES(&env_v_v, code); 502 } 503 504 505 TEST_F(WasmDecoderTest, Block1_continue) { 506 static const byte code[] = {kExprBlock, 1, kExprBr, 1, kExprNop}; 507 EXPECT_FAILURE(&env_v_v, code); 508 } 509 510 511 TEST_F(WasmDecoderTest, Block2_continue) { 512 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 1, kExprNop}; 513 EXPECT_FAILURE(&env_v_v, code); 514 } 515 516 517 TEST_F(WasmDecoderTest, ExprBlock0) { 518 static const byte code[] = {kExprBlock, 0}; 519 EXPECT_VERIFIES(&env_v_v, code); 520 } 521 522 523 TEST_F(WasmDecoderTest, ExprBlock1a) { 524 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; 525 EXPECT_VERIFIES(&env_i_i, code); 526 } 527 528 529 TEST_F(WasmDecoderTest, ExprBlock1b) { 530 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; 531 EXPECT_FAILURE(&env_f_ff, code); 532 } 533 534 535 // TODO(tizer): Fix on arm and reenable. 536 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 537 538 TEST_F(WasmDecoderTest, ExprBlock1c) { 539 static const byte code[] = {kExprBlock, 1, kExprF32Const, 0, 0, 0, 0}; 540 EXPECT_VERIFIES(&env_f_ff, code); 541 } 542 543 #endif 544 545 546 TEST_F(WasmDecoderTest, IfEmpty) { 547 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprNop}; 548 EXPECT_VERIFIES(&env_v_i, code); 549 } 550 551 552 TEST_F(WasmDecoderTest, IfSet) { 553 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprSetLocal, 554 0, kExprI8Const, 0, kExprNop}; 555 EXPECT_VERIFIES(&env_v_i, code); 556 } 557 558 559 TEST_F(WasmDecoderTest, IfBlock1) { 560 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprBlock, 561 1, kExprSetLocal, 0, kExprI8Const, 562 0, kExprNop}; 563 EXPECT_VERIFIES(&env_v_i, code); 564 } 565 566 567 TEST_F(WasmDecoderTest, IfBlock2) { 568 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprBlock, 569 2, kExprSetLocal, 0, kExprI8Const, 570 0, kExprSetLocal, 0, kExprI8Const, 571 0}; 572 EXPECT_VERIFIES(&env_v_i, code); 573 } 574 575 576 TEST_F(WasmDecoderTest, IfElseEmpty) { 577 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprNop, 578 kExprNop}; 579 EXPECT_VERIFIES(&env_v_i, code); 580 } 581 582 583 TEST_F(WasmDecoderTest, IfElseSet) { 584 static const byte code[] = {kExprIfElse, 585 kExprGetLocal, 586 0, // -- 587 kExprSetLocal, 588 0, 589 kExprI8Const, 590 0, // -- 591 kExprSetLocal, 592 0, 593 kExprI8Const, 594 1}; // -- 595 EXPECT_VERIFIES(&env_v_i, code); 596 } 597 598 599 TEST_F(WasmDecoderTest, IfElseUnreachable) { 600 static const byte code[] = {kExprIfElse, kExprI8Const, 0, 601 kExprUnreachable, kExprGetLocal, 0}; 602 603 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 604 LocalType types[] = {kAstI32, kLocalTypes[i]}; 605 FunctionEnv env; 606 FunctionSig sig(1, 1, types); 607 init_env(&env, &sig); 608 609 if (kLocalTypes[i] == kAstI32) { 610 EXPECT_VERIFIES(&env, code); 611 } else { 612 EXPECT_FAILURE(&env, code); 613 } 614 } 615 } 616 617 618 TEST_F(WasmDecoderTest, Loop0) { 619 static const byte code[] = {kExprLoop, 0}; 620 EXPECT_VERIFIES(&env_v_v, code); 621 } 622 623 624 TEST_F(WasmDecoderTest, Loop1) { 625 static const byte code[] = {kExprLoop, 1, kExprSetLocal, 0, kExprI8Const, 0}; 626 EXPECT_VERIFIES(&env_v_i, code); 627 } 628 629 630 TEST_F(WasmDecoderTest, Loop2) { 631 static const byte code[] = {kExprLoop, 2, // -- 632 kExprSetLocal, 0, kExprI8Const, 0, // -- 633 kExprSetLocal, 0, kExprI8Const, 0}; // -- 634 EXPECT_VERIFIES(&env_v_i, code); 635 } 636 637 638 TEST_F(WasmDecoderTest, Loop1_continue) { 639 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprNop}; 640 EXPECT_VERIFIES(&env_v_v, code); 641 } 642 643 644 TEST_F(WasmDecoderTest, Loop1_break) { 645 static const byte code[] = {kExprLoop, 1, kExprBr, 1, kExprNop}; 646 EXPECT_VERIFIES(&env_v_v, code); 647 } 648 649 650 TEST_F(WasmDecoderTest, Loop2_continue) { 651 static const byte code[] = {kExprLoop, 2, // -- 652 kExprSetLocal, 0, kExprI8Const, 0, // -- 653 kExprBr, 0, kExprNop}; // -- 654 EXPECT_VERIFIES(&env_v_i, code); 655 } 656 657 658 TEST_F(WasmDecoderTest, Loop2_break) { 659 static const byte code[] = {kExprLoop, 2, // -- 660 kExprSetLocal, 0, kExprI8Const, 0, // -- 661 kExprBr, 1, kExprNop}; // -- 662 EXPECT_VERIFIES(&env_v_i, code); 663 } 664 665 666 TEST_F(WasmDecoderTest, ExprLoop0) { 667 static const byte code[] = {kExprLoop, 0}; 668 EXPECT_VERIFIES(&env_v_v, code); 669 } 670 671 672 TEST_F(WasmDecoderTest, ExprLoop1a) { 673 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; 674 EXPECT_VERIFIES(&env_i_i, code); 675 } 676 677 678 TEST_F(WasmDecoderTest, ExprLoop1b) { 679 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; 680 EXPECT_VERIFIES(&env_i_i, code); 681 } 682 683 684 TEST_F(WasmDecoderTest, ExprLoop2_unreachable) { 685 static const byte code[] = {kExprLoop, 2, kExprBr, 0, 686 kExprI8Const, 0, kExprNop}; 687 EXPECT_VERIFIES(&env_i_i, code); 688 } 689 690 691 TEST_F(WasmDecoderTest, ReturnVoid1) { 692 static const byte code[] = {kExprNop}; 693 EXPECT_VERIFIES(&env_v_v, code); 694 EXPECT_FAILURE(&env_i_i, code); 695 EXPECT_FAILURE(&env_i_f, code); 696 } 697 698 699 TEST_F(WasmDecoderTest, ReturnVoid2) { 700 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; 701 EXPECT_VERIFIES(&env_v_v, code); 702 EXPECT_FAILURE(&env_i_i, code); 703 EXPECT_FAILURE(&env_i_f, code); 704 } 705 706 707 // TODO(tizer): Fix on arm and reenable. 708 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 709 710 TEST_F(WasmDecoderTest, ReturnVoid3) { 711 EXPECT_VERIFIES_INLINE(&env_v_v, kExprI8Const, 0); 712 EXPECT_VERIFIES_INLINE(&env_v_v, kExprI32Const, 0, 0, 0, 0); 713 EXPECT_VERIFIES_INLINE(&env_v_v, kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0); 714 EXPECT_VERIFIES_INLINE(&env_v_v, kExprF32Const, 0, 0, 0, 0); 715 EXPECT_VERIFIES_INLINE(&env_v_v, kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0); 716 717 EXPECT_VERIFIES_INLINE(&env_v_i, kExprGetLocal, 0); 718 } 719 720 #endif 721 722 723 TEST_F(WasmDecoderTest, Unreachable1) { 724 EXPECT_VERIFIES_INLINE(&env_v_v, kExprUnreachable); 725 EXPECT_VERIFIES_INLINE(&env_v_v, kExprUnreachable, kExprUnreachable); 726 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(2, WASM_UNREACHABLE, WASM_ZERO)); 727 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(2, WASM_BR(0), WASM_ZERO)); 728 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(2, WASM_UNREACHABLE, WASM_ZERO)); 729 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(2, WASM_BR(0), WASM_ZERO)); 730 } 731 732 733 TEST_F(WasmDecoderTest, Codeiness) { 734 VERIFY(kExprLoop, 2, // -- 735 kExprSetLocal, 0, kExprI8Const, 0, // -- 736 kExprBr, 0, kExprNop); // -- 737 } 738 739 740 TEST_F(WasmDecoderTest, ExprIf1) { 741 VERIFY(kExprIf, kExprGetLocal, 0, kExprI8Const, 0, kExprI8Const, 1); 742 VERIFY(kExprIf, kExprGetLocal, 0, kExprGetLocal, 0, kExprGetLocal, 0); 743 VERIFY(kExprIf, kExprGetLocal, 0, kExprI32Add, kExprGetLocal, 0, 744 kExprGetLocal, 0, kExprI8Const, 1); 745 } 746 747 748 TEST_F(WasmDecoderTest, ExprIf_off_end) { 749 static const byte kCode[] = {kExprIf, kExprGetLocal, 0, kExprGetLocal, 750 0, kExprGetLocal, 0}; 751 for (size_t len = 1; len < arraysize(kCode); len++) { 752 Verify(kError, &env_i_i, kCode, kCode + len); 753 } 754 } 755 756 757 TEST_F(WasmDecoderTest, ExprIf_type) { 758 { 759 // float|double ? 1 : 2 760 static const byte kCode[] = {kExprIfElse, kExprGetLocal, 0, kExprI8Const, 761 1, kExprI8Const, 2}; 762 EXPECT_FAILURE(&env_i_f, kCode); 763 EXPECT_FAILURE(&env_i_d, kCode); 764 } 765 { 766 // 1 ? float|double : 2 767 static const byte kCode[] = {kExprIfElse, kExprI8Const, 1, kExprGetLocal, 768 0, kExprI8Const, 2}; 769 EXPECT_FAILURE(&env_i_f, kCode); 770 EXPECT_FAILURE(&env_i_d, kCode); 771 } 772 { 773 // stmt ? 0 : 1 774 static const byte kCode[] = {kExprIfElse, kExprNop, kExprI8Const, 775 0, kExprI8Const, 1}; 776 EXPECT_FAILURE(&env_i_i, kCode); 777 } 778 { 779 // 0 ? stmt : 1 780 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, 781 kExprNop, kExprI8Const, 1}; 782 EXPECT_FAILURE(&env_i_i, kCode); 783 } 784 { 785 // 0 ? 1 : stmt 786 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, kExprI8Const, 1, 787 0, kExprBlock}; 788 EXPECT_FAILURE(&env_i_i, kCode); 789 } 790 } 791 792 793 TEST_F(WasmDecoderTest, Int64Local_param) { 794 EXPECT_VERIFIES(&env_l_l, kCodeGetLocal0); 795 } 796 797 798 TEST_F(WasmDecoderTest, Int64Locals) { 799 for (byte i = 1; i < 8; i++) { 800 FunctionEnv env; 801 init_env(&env, sigs.l_v()); 802 env.AddLocals(kAstI64, i); 803 for (byte j = 0; j < i; j++) { 804 byte code[] = {kExprGetLocal, j}; 805 EXPECT_VERIFIES(&env, code); 806 } 807 } 808 } 809 810 811 TEST_F(WasmDecoderTest, Int32Binops) { 812 TestBinop(kExprI32Add, sigs.i_ii()); 813 TestBinop(kExprI32Sub, sigs.i_ii()); 814 TestBinop(kExprI32Mul, sigs.i_ii()); 815 TestBinop(kExprI32DivS, sigs.i_ii()); 816 TestBinop(kExprI32DivU, sigs.i_ii()); 817 TestBinop(kExprI32RemS, sigs.i_ii()); 818 TestBinop(kExprI32RemU, sigs.i_ii()); 819 TestBinop(kExprI32And, sigs.i_ii()); 820 TestBinop(kExprI32Ior, sigs.i_ii()); 821 TestBinop(kExprI32Xor, sigs.i_ii()); 822 TestBinop(kExprI32Shl, sigs.i_ii()); 823 TestBinop(kExprI32ShrU, sigs.i_ii()); 824 TestBinop(kExprI32ShrS, sigs.i_ii()); 825 TestBinop(kExprI32Eq, sigs.i_ii()); 826 TestBinop(kExprI32LtS, sigs.i_ii()); 827 TestBinop(kExprI32LeS, sigs.i_ii()); 828 TestBinop(kExprI32LtU, sigs.i_ii()); 829 TestBinop(kExprI32LeU, sigs.i_ii()); 830 } 831 832 833 TEST_F(WasmDecoderTest, DoubleBinops) { 834 TestBinop(kExprF64Add, sigs.d_dd()); 835 TestBinop(kExprF64Sub, sigs.d_dd()); 836 TestBinop(kExprF64Mul, sigs.d_dd()); 837 TestBinop(kExprF64Div, sigs.d_dd()); 838 839 TestBinop(kExprF64Eq, sigs.i_dd()); 840 TestBinop(kExprF64Lt, sigs.i_dd()); 841 TestBinop(kExprF64Le, sigs.i_dd()); 842 } 843 844 845 TEST_F(WasmDecoderTest, FloatBinops) { 846 TestBinop(kExprF32Add, sigs.f_ff()); 847 TestBinop(kExprF32Sub, sigs.f_ff()); 848 TestBinop(kExprF32Mul, sigs.f_ff()); 849 TestBinop(kExprF32Div, sigs.f_ff()); 850 851 TestBinop(kExprF32Eq, sigs.i_ff()); 852 TestBinop(kExprF32Lt, sigs.i_ff()); 853 TestBinop(kExprF32Le, sigs.i_ff()); 854 } 855 856 857 TEST_F(WasmDecoderTest, TypeConversions) { 858 TestUnop(kExprI32SConvertF32, kAstI32, kAstF32); 859 TestUnop(kExprI32SConvertF64, kAstI32, kAstF64); 860 TestUnop(kExprI32UConvertF32, kAstI32, kAstF32); 861 TestUnop(kExprI32UConvertF64, kAstI32, kAstF64); 862 TestUnop(kExprF64SConvertI32, kAstF64, kAstI32); 863 TestUnop(kExprF64UConvertI32, kAstF64, kAstI32); 864 TestUnop(kExprF64ConvertF32, kAstF64, kAstF32); 865 TestUnop(kExprF32SConvertI32, kAstF32, kAstI32); 866 TestUnop(kExprF32UConvertI32, kAstF32, kAstI32); 867 TestUnop(kExprF32ConvertF64, kAstF32, kAstF64); 868 } 869 870 871 TEST_F(WasmDecoderTest, MacrosStmt) { 872 VERIFY(WASM_SET_LOCAL(0, WASM_I32(87348))); 873 VERIFY(WASM_STORE_MEM(MachineType::Int32(), WASM_I8(24), WASM_I8(40))); 874 VERIFY(WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); 875 VERIFY(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); 876 VERIFY(WASM_NOP); 877 VERIFY(WASM_BLOCK(1, WASM_NOP)); 878 VERIFY(WASM_LOOP(1, WASM_NOP)); 879 VERIFY(WASM_LOOP(1, WASM_BREAK(0))); 880 VERIFY(WASM_LOOP(1, WASM_CONTINUE(0))); 881 } 882 883 884 // TODO(tizer): Fix on arm and reenable. 885 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 886 887 TEST_F(WasmDecoderTest, MacrosBreak) { 888 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BREAK(0))); 889 890 EXPECT_VERIFIES_INLINE(&env_i_i, WASM_LOOP(1, WASM_BREAKV(0, WASM_ZERO))); 891 EXPECT_VERIFIES_INLINE(&env_l_l, WASM_LOOP(1, WASM_BREAKV(0, WASM_I64(0)))); 892 EXPECT_VERIFIES_INLINE(&env_f_ff, 893 WASM_LOOP(1, WASM_BREAKV(0, WASM_F32(0.0)))); 894 EXPECT_VERIFIES_INLINE(&env_d_dd, 895 WASM_LOOP(1, WASM_BREAKV(0, WASM_F64(0.0)))); 896 } 897 898 #endif 899 900 901 TEST_F(WasmDecoderTest, MacrosContinue) { 902 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_CONTINUE(0))); 903 } 904 905 906 TEST_F(WasmDecoderTest, MacrosVariadic) { 907 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_NOP)); 908 VERIFY(WASM_BLOCK(3, WASM_NOP, WASM_NOP, WASM_NOP)); 909 VERIFY(WASM_LOOP(2, WASM_NOP, WASM_NOP)); 910 VERIFY(WASM_LOOP(3, WASM_NOP, WASM_NOP, WASM_NOP)); 911 } 912 913 914 TEST_F(WasmDecoderTest, MacrosNestedBlocks) { 915 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_BLOCK(2, WASM_NOP, WASM_NOP))); 916 VERIFY(WASM_BLOCK(3, WASM_NOP, // -- 917 WASM_BLOCK(2, WASM_NOP, WASM_NOP), // -- 918 WASM_BLOCK(2, WASM_NOP, WASM_NOP))); // -- 919 VERIFY(WASM_BLOCK(1, WASM_BLOCK(1, WASM_BLOCK(2, WASM_NOP, WASM_NOP)))); 920 } 921 922 923 TEST_F(WasmDecoderTest, MultipleReturn) { 924 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; 925 FunctionSig sig_ii_v(2, 0, kIntTypes5); 926 FunctionEnv env_ii_v; 927 init_env(&env_ii_v, &sig_ii_v); 928 EXPECT_VERIFIES_INLINE(&env_ii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); 929 EXPECT_FAILURE_INLINE(&env_ii_v, WASM_RETURN(WASM_ZERO)); 930 931 FunctionSig sig_iii_v(3, 0, kIntTypes5); 932 FunctionEnv env_iii_v; 933 init_env(&env_iii_v, &sig_iii_v); 934 EXPECT_VERIFIES_INLINE(&env_iii_v, 935 WASM_RETURN(WASM_ZERO, WASM_ONE, WASM_I8(44))); 936 EXPECT_FAILURE_INLINE(&env_iii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); 937 } 938 939 940 TEST_F(WasmDecoderTest, MultipleReturn_fallthru) { 941 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; 942 FunctionSig sig_ii_v(2, 0, kIntTypes5); 943 FunctionEnv env_ii_v; 944 init_env(&env_ii_v, &sig_ii_v); 945 946 EXPECT_VERIFIES_INLINE(&env_ii_v, WASM_ZERO, WASM_ONE); 947 EXPECT_FAILURE_INLINE(&env_ii_v, WASM_ZERO); 948 949 FunctionSig sig_iii_v(3, 0, kIntTypes5); 950 FunctionEnv env_iii_v; 951 init_env(&env_iii_v, &sig_iii_v); 952 EXPECT_VERIFIES_INLINE(&env_iii_v, WASM_ZERO, WASM_ONE, WASM_I8(44)); 953 EXPECT_FAILURE_INLINE(&env_iii_v, WASM_ZERO, WASM_ONE); 954 } 955 956 957 TEST_F(WasmDecoderTest, MacrosInt32) { 958 VERIFY(WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_I8(12))); 959 VERIFY(WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(13))); 960 VERIFY(WASM_I32_MUL(WASM_GET_LOCAL(0), WASM_I8(14))); 961 VERIFY(WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I8(15))); 962 VERIFY(WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_I8(16))); 963 VERIFY(WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_I8(17))); 964 VERIFY(WASM_I32_REMU(WASM_GET_LOCAL(0), WASM_I8(18))); 965 VERIFY(WASM_I32_AND(WASM_GET_LOCAL(0), WASM_I8(19))); 966 VERIFY(WASM_I32_IOR(WASM_GET_LOCAL(0), WASM_I8(20))); 967 VERIFY(WASM_I32_XOR(WASM_GET_LOCAL(0), WASM_I8(21))); 968 VERIFY(WASM_I32_SHL(WASM_GET_LOCAL(0), WASM_I8(22))); 969 VERIFY(WASM_I32_SHR(WASM_GET_LOCAL(0), WASM_I8(23))); 970 VERIFY(WASM_I32_SAR(WASM_GET_LOCAL(0), WASM_I8(24))); 971 VERIFY(WASM_I32_EQ(WASM_GET_LOCAL(0), WASM_I8(25))); 972 VERIFY(WASM_I32_NE(WASM_GET_LOCAL(0), WASM_I8(25))); 973 974 VERIFY(WASM_I32_LTS(WASM_GET_LOCAL(0), WASM_I8(26))); 975 VERIFY(WASM_I32_LES(WASM_GET_LOCAL(0), WASM_I8(27))); 976 VERIFY(WASM_I32_LTU(WASM_GET_LOCAL(0), WASM_I8(28))); 977 VERIFY(WASM_I32_LEU(WASM_GET_LOCAL(0), WASM_I8(29))); 978 979 VERIFY(WASM_I32_GTS(WASM_GET_LOCAL(0), WASM_I8(26))); 980 VERIFY(WASM_I32_GES(WASM_GET_LOCAL(0), WASM_I8(27))); 981 VERIFY(WASM_I32_GTU(WASM_GET_LOCAL(0), WASM_I8(28))); 982 VERIFY(WASM_I32_GEU(WASM_GET_LOCAL(0), WASM_I8(29))); 983 } 984 985 986 TEST_F(WasmDecoderTest, MacrosInt64) { 987 FunctionEnv env_i_ll; 988 FunctionEnv env_l_ll; 989 init_env(&env_i_ll, sigs.i_ll()); 990 init_env(&env_l_ll, sigs.l_ll()); 991 992 #define VERIFY_L_LL(...) EXPECT_VERIFIES_INLINE(&env_l_ll, __VA_ARGS__) 993 #define VERIFY_I_LL(...) EXPECT_VERIFIES_INLINE(&env_i_ll, __VA_ARGS__) 994 995 VERIFY_L_LL(WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_I64(12))); 996 VERIFY_L_LL(WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_I64(13))); 997 VERIFY_L_LL(WASM_I64_MUL(WASM_GET_LOCAL(0), WASM_I64(14))); 998 VERIFY_L_LL(WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_I64(15))); 999 VERIFY_L_LL(WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_I64(16))); 1000 VERIFY_L_LL(WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_I64(17))); 1001 VERIFY_L_LL(WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_I64(18))); 1002 VERIFY_L_LL(WASM_I64_AND(WASM_GET_LOCAL(0), WASM_I64(19))); 1003 VERIFY_L_LL(WASM_I64_IOR(WASM_GET_LOCAL(0), WASM_I64(20))); 1004 VERIFY_L_LL(WASM_I64_XOR(WASM_GET_LOCAL(0), WASM_I64(21))); 1005 1006 VERIFY_L_LL(WASM_I64_SHL(WASM_GET_LOCAL(0), WASM_I64(22))); 1007 VERIFY_L_LL(WASM_I64_SHR(WASM_GET_LOCAL(0), WASM_I64(23))); 1008 VERIFY_L_LL(WASM_I64_SAR(WASM_GET_LOCAL(0), WASM_I64(24))); 1009 1010 VERIFY_I_LL(WASM_I64_LTS(WASM_GET_LOCAL(0), WASM_I64(26))); 1011 VERIFY_I_LL(WASM_I64_LES(WASM_GET_LOCAL(0), WASM_I64(27))); 1012 VERIFY_I_LL(WASM_I64_LTU(WASM_GET_LOCAL(0), WASM_I64(28))); 1013 VERIFY_I_LL(WASM_I64_LEU(WASM_GET_LOCAL(0), WASM_I64(29))); 1014 1015 VERIFY_I_LL(WASM_I64_GTS(WASM_GET_LOCAL(0), WASM_I64(26))); 1016 VERIFY_I_LL(WASM_I64_GES(WASM_GET_LOCAL(0), WASM_I64(27))); 1017 VERIFY_I_LL(WASM_I64_GTU(WASM_GET_LOCAL(0), WASM_I64(28))); 1018 VERIFY_I_LL(WASM_I64_GEU(WASM_GET_LOCAL(0), WASM_I64(29))); 1019 1020 VERIFY_I_LL(WASM_I64_EQ(WASM_GET_LOCAL(0), WASM_I64(25))); 1021 VERIFY_I_LL(WASM_I64_NE(WASM_GET_LOCAL(0), WASM_I64(25))); 1022 } 1023 1024 1025 TEST_F(WasmDecoderTest, AllSimpleExpressions) { 1026 // Test all simple expressions which are described by a signature. 1027 #define DECODE_TEST(name, opcode, sig) \ 1028 { \ 1029 FunctionSig* sig = WasmOpcodes::Signature(kExpr##name); \ 1030 if (sig->parameter_count() == 1) { \ 1031 TestUnop(kExpr##name, sig); \ 1032 } else { \ 1033 TestBinop(kExpr##name, sig); \ 1034 } \ 1035 } 1036 1037 FOREACH_SIMPLE_OPCODE(DECODE_TEST); 1038 1039 #undef DECODE_TEST 1040 } 1041 1042 1043 TEST_F(WasmDecoderTest, MemorySize) { 1044 byte code[] = {kExprMemorySize}; 1045 EXPECT_VERIFIES(&env_i_i, code); 1046 EXPECT_FAILURE(&env_f_ff, code); 1047 } 1048 1049 1050 TEST_F(WasmDecoderTest, GrowMemory) { 1051 byte code[] = {kExprGrowMemory, kExprGetLocal, 0}; 1052 EXPECT_VERIFIES(&env_i_i, code); 1053 EXPECT_FAILURE(&env_i_d, code); 1054 } 1055 1056 1057 TEST_F(WasmDecoderTest, LoadMemOffset) { 1058 for (int offset = 0; offset < 128; offset += 7) { 1059 byte code[] = {kExprI32LoadMem, WasmOpcodes::LoadStoreAccessOf(true), 1060 static_cast<byte>(offset), kExprI8Const, 0}; 1061 EXPECT_VERIFIES(&env_i_i, code); 1062 } 1063 } 1064 1065 1066 TEST_F(WasmDecoderTest, StoreMemOffset) { 1067 for (int offset = 0; offset < 128; offset += 7) { 1068 byte code[] = {kExprI32StoreMem, 1069 WasmOpcodes::LoadStoreAccessOf(true), 1070 static_cast<byte>(offset), 1071 kExprI8Const, 1072 0, 1073 kExprI8Const, 1074 0}; 1075 EXPECT_VERIFIES(&env_i_i, code); 1076 } 1077 } 1078 1079 1080 TEST_F(WasmDecoderTest, LoadMemOffset_varint) { 1081 byte code1[] = {kExprI32LoadMem, WasmOpcodes::LoadStoreAccessOf(true), 0, 1082 kExprI8Const, 0}; 1083 byte code2[] = {kExprI32LoadMem, 1084 WasmOpcodes::LoadStoreAccessOf(true), 1085 0x80, 1086 1, 1087 kExprI8Const, 1088 0}; 1089 byte code3[] = {kExprI32LoadMem, 1090 WasmOpcodes::LoadStoreAccessOf(true), 1091 0x81, 1092 0x82, 1093 5, 1094 kExprI8Const, 1095 0}; 1096 byte code4[] = {kExprI32LoadMem, 1097 WasmOpcodes::LoadStoreAccessOf(true), 1098 0x83, 1099 0x84, 1100 0x85, 1101 7, 1102 kExprI8Const, 1103 0}; 1104 1105 EXPECT_VERIFIES(&env_i_i, code1); 1106 EXPECT_VERIFIES(&env_i_i, code2); 1107 EXPECT_VERIFIES(&env_i_i, code3); 1108 EXPECT_VERIFIES(&env_i_i, code4); 1109 } 1110 1111 1112 TEST_F(WasmDecoderTest, StoreMemOffset_varint) { 1113 byte code1[] = {kExprI32StoreMem, 1114 WasmOpcodes::LoadStoreAccessOf(true), 1115 0, 1116 kExprI8Const, 1117 0, 1118 kExprI8Const, 1119 0}; 1120 byte code2[] = {kExprI32StoreMem, 1121 WasmOpcodes::LoadStoreAccessOf(true), 1122 0x80, 1123 1, 1124 kExprI8Const, 1125 0, 1126 kExprI8Const, 1127 0}; 1128 byte code3[] = {kExprI32StoreMem, 1129 WasmOpcodes::LoadStoreAccessOf(true), 1130 0x81, 1131 0x82, 1132 5, 1133 kExprI8Const, 1134 0, 1135 kExprI8Const, 1136 0}; 1137 byte code4[] = {kExprI32StoreMem, 1138 WasmOpcodes::LoadStoreAccessOf(true), 1139 0x83, 1140 0x84, 1141 0x85, 1142 7, 1143 kExprI8Const, 1144 0, 1145 kExprI8Const, 1146 0}; 1147 1148 EXPECT_VERIFIES(&env_i_i, code1); 1149 EXPECT_VERIFIES(&env_i_i, code2); 1150 EXPECT_VERIFIES(&env_i_i, code3); 1151 EXPECT_VERIFIES(&env_i_i, code4); 1152 } 1153 1154 1155 TEST_F(WasmDecoderTest, AllLoadMemCombinations) { 1156 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1157 LocalType local_type = kLocalTypes[i]; 1158 for (size_t j = 0; j < arraysize(machineTypes); j++) { 1159 MachineType mem_type = machineTypes[j]; 1160 byte code[] = { 1161 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, false)), 1162 WasmOpcodes::LoadStoreAccessOf(false), kExprI8Const, 0}; 1163 FunctionEnv env; 1164 FunctionSig sig(1, 0, &local_type); 1165 init_env(&env, &sig); 1166 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { 1167 EXPECT_VERIFIES(&env, code); 1168 } else { 1169 EXPECT_FAILURE(&env, code); 1170 } 1171 } 1172 } 1173 } 1174 1175 1176 TEST_F(WasmDecoderTest, AllStoreMemCombinations) { 1177 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1178 LocalType local_type = kLocalTypes[i]; 1179 for (size_t j = 0; j < arraysize(machineTypes); j++) { 1180 MachineType mem_type = machineTypes[j]; 1181 byte code[] = { 1182 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, true)), 1183 WasmOpcodes::LoadStoreAccessOf(false), 1184 kExprI8Const, 1185 0, 1186 kExprGetLocal, 1187 0}; 1188 FunctionEnv env; 1189 FunctionSig sig(0, 1, &local_type); 1190 init_env(&env, &sig); 1191 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { 1192 EXPECT_VERIFIES(&env, code); 1193 } else { 1194 EXPECT_FAILURE(&env, code); 1195 } 1196 } 1197 } 1198 } 1199 1200 1201 namespace { 1202 // A helper for tests that require a module environment for functions and 1203 // globals. 1204 class TestModuleEnv : public ModuleEnv { 1205 public: 1206 TestModuleEnv() { 1207 mem_start = 0; 1208 mem_end = 0; 1209 module = &mod; 1210 linker = nullptr; 1211 function_code = nullptr; 1212 mod.globals = new std::vector<WasmGlobal>; 1213 mod.signatures = new std::vector<FunctionSig*>; 1214 mod.functions = new std::vector<WasmFunction>; 1215 } 1216 byte AddGlobal(MachineType mem_type) { 1217 mod.globals->push_back({0, mem_type, 0, false}); 1218 CHECK(mod.globals->size() <= 127); 1219 return static_cast<byte>(mod.globals->size() - 1); 1220 } 1221 byte AddSignature(FunctionSig* sig) { 1222 mod.signatures->push_back(sig); 1223 CHECK(mod.signatures->size() <= 127); 1224 return static_cast<byte>(mod.signatures->size() - 1); 1225 } 1226 byte AddFunction(FunctionSig* sig) { 1227 mod.functions->push_back({sig, 0, 0, 0, 0, 0, 0, 0, false, false}); 1228 CHECK(mod.functions->size() <= 127); 1229 return static_cast<byte>(mod.functions->size() - 1); 1230 } 1231 1232 private: 1233 WasmModule mod; 1234 }; 1235 } // namespace 1236 1237 1238 TEST_F(WasmDecoderTest, SimpleCalls) { 1239 FunctionEnv* env = &env_i_i; 1240 TestModuleEnv module_env; 1241 env->module = &module_env; 1242 1243 module_env.AddFunction(sigs.i_v()); 1244 module_env.AddFunction(sigs.i_i()); 1245 module_env.AddFunction(sigs.i_ii()); 1246 1247 EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(0)); 1248 EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I8(27))); 1249 EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(2, WASM_I8(37), WASM_I8(77))); 1250 } 1251 1252 1253 TEST_F(WasmDecoderTest, CallsWithTooFewArguments) { 1254 FunctionEnv* env = &env_i_i; 1255 TestModuleEnv module_env; 1256 env->module = &module_env; 1257 1258 module_env.AddFunction(sigs.i_i()); 1259 module_env.AddFunction(sigs.i_ii()); 1260 module_env.AddFunction(sigs.f_ff()); 1261 1262 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION0(0)); 1263 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_ZERO)); 1264 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0))); 1265 } 1266 1267 1268 // TODO(tizer): Fix on arm and reenable. 1269 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 1270 1271 TEST_F(WasmDecoderTest, CallsWithSpilloverArgs) { 1272 static LocalType a_i_ff[] = {kAstI32, kAstF32, kAstF32}; 1273 FunctionSig sig_i_ff(1, 2, a_i_ff); 1274 FunctionEnv env_i_ff; 1275 init_env(&env_i_ff, &sig_i_ff); 1276 1277 TestModuleEnv module_env; 1278 env_i_ff.module = &module_env; 1279 env_i_i.module = &module_env; 1280 env_f_ff.module = &module_env; 1281 1282 module_env.AddFunction(&sig_i_ff); 1283 1284 EXPECT_VERIFIES_INLINE(&env_i_i, 1285 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); 1286 1287 EXPECT_VERIFIES_INLINE(&env_i_ff, 1288 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); 1289 1290 EXPECT_FAILURE_INLINE(&env_f_ff, 1291 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); 1292 1293 EXPECT_FAILURE_INLINE( 1294 &env_i_i, 1295 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(0.2))); 1296 1297 EXPECT_VERIFIES_INLINE( 1298 &env_f_ff, 1299 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(11))); 1300 } 1301 1302 1303 TEST_F(WasmDecoderTest, CallsWithMismatchedSigs2) { 1304 FunctionEnv* env = &env_i_i; 1305 TestModuleEnv module_env; 1306 env->module = &module_env; 1307 1308 module_env.AddFunction(sigs.i_i()); 1309 1310 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I64(17))); 1311 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F32(17.1))); 1312 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F64(17.1))); 1313 } 1314 1315 1316 TEST_F(WasmDecoderTest, CallsWithMismatchedSigs3) { 1317 FunctionEnv* env = &env_i_i; 1318 TestModuleEnv module_env; 1319 env->module = &module_env; 1320 1321 module_env.AddFunction(sigs.i_f()); 1322 1323 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I8(17))); 1324 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I64(27))); 1325 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F64(37.2))); 1326 1327 module_env.AddFunction(sigs.i_d()); 1328 1329 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I8(16))); 1330 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I64(16))); 1331 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_F32(17.6))); 1332 } 1333 1334 #endif 1335 1336 1337 TEST_F(WasmDecoderTest, SimpleIndirectCalls) { 1338 FunctionEnv* env = &env_i_i; 1339 TestModuleEnv module_env; 1340 env->module = &module_env; 1341 1342 byte f0 = module_env.AddSignature(sigs.i_v()); 1343 byte f1 = module_env.AddSignature(sigs.i_i()); 1344 byte f2 = module_env.AddSignature(sigs.i_ii()); 1345 1346 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); 1347 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(22))); 1348 EXPECT_VERIFIES_INLINE( 1349 env, WASM_CALL_INDIRECT(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); 1350 } 1351 1352 1353 TEST_F(WasmDecoderTest, IndirectCallsOutOfBounds) { 1354 FunctionEnv* env = &env_i_i; 1355 TestModuleEnv module_env; 1356 env->module = &module_env; 1357 1358 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(0, WASM_ZERO)); 1359 module_env.AddSignature(sigs.i_v()); 1360 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT0(0, WASM_ZERO)); 1361 1362 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(22))); 1363 module_env.AddSignature(sigs.i_i()); 1364 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(27))); 1365 1366 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(2, WASM_ZERO, WASM_I8(27))); 1367 } 1368 1369 1370 TEST_F(WasmDecoderTest, IndirectCallsWithMismatchedSigs3) { 1371 FunctionEnv* env = &env_i_i; 1372 TestModuleEnv module_env; 1373 env->module = &module_env; 1374 1375 byte f0 = module_env.AddFunction(sigs.i_f()); 1376 1377 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I8(17))); 1378 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I64(27))); 1379 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_F64(37.2))); 1380 1381 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_I8(17))); 1382 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_I64(27))); 1383 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_F64(37.2))); 1384 1385 byte f1 = module_env.AddFunction(sigs.i_d()); 1386 1387 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(16))); 1388 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I64(16))); 1389 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_F32(17.6))); 1390 } 1391 1392 1393 TEST_F(WasmDecoderTest, Int32Globals) { 1394 FunctionEnv* env = &env_i_i; 1395 TestModuleEnv module_env; 1396 env->module = &module_env; 1397 1398 module_env.AddGlobal(MachineType::Int8()); 1399 module_env.AddGlobal(MachineType::Uint8()); 1400 module_env.AddGlobal(MachineType::Int16()); 1401 module_env.AddGlobal(MachineType::Uint16()); 1402 module_env.AddGlobal(MachineType::Int32()); 1403 module_env.AddGlobal(MachineType::Uint32()); 1404 1405 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); 1406 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(1)); 1407 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(2)); 1408 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(3)); 1409 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(4)); 1410 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(5)); 1411 1412 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1413 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); 1414 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); 1415 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); 1416 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(4, WASM_GET_LOCAL(0))); 1417 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(5, WASM_GET_LOCAL(0))); 1418 } 1419 1420 1421 TEST_F(WasmDecoderTest, Int32Globals_fail) { 1422 FunctionEnv* env = &env_i_i; 1423 TestModuleEnv module_env; 1424 env->module = &module_env; 1425 1426 module_env.AddGlobal(MachineType::Int64()); 1427 module_env.AddGlobal(MachineType::Uint64()); 1428 module_env.AddGlobal(MachineType::Float32()); 1429 module_env.AddGlobal(MachineType::Float64()); 1430 1431 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(0)); 1432 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(1)); 1433 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(2)); 1434 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(3)); 1435 1436 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1437 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); 1438 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); 1439 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); 1440 } 1441 1442 1443 TEST_F(WasmDecoderTest, Int64Globals) { 1444 FunctionEnv* env = &env_l_l; 1445 TestModuleEnv module_env; 1446 env->module = &module_env; 1447 1448 module_env.AddGlobal(MachineType::Int64()); 1449 module_env.AddGlobal(MachineType::Uint64()); 1450 1451 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); 1452 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(1)); 1453 1454 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1455 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); 1456 } 1457 1458 1459 TEST_F(WasmDecoderTest, Float32Globals) { 1460 FunctionEnv env_f_ff; 1461 FunctionEnv* env = &env_f_ff; 1462 init_env(env, sigs.f_ff()); 1463 TestModuleEnv module_env; 1464 env->module = &module_env; 1465 1466 module_env.AddGlobal(MachineType::Float32()); 1467 1468 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); 1469 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1470 } 1471 1472 1473 TEST_F(WasmDecoderTest, Float64Globals) { 1474 FunctionEnv env_d_dd; 1475 FunctionEnv* env = &env_d_dd; 1476 init_env(env, sigs.d_dd()); 1477 TestModuleEnv module_env; 1478 env->module = &module_env; 1479 1480 module_env.AddGlobal(MachineType::Float64()); 1481 1482 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); 1483 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1484 } 1485 1486 1487 TEST_F(WasmDecoderTest, AllLoadGlobalCombinations) { 1488 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1489 LocalType local_type = kLocalTypes[i]; 1490 for (size_t j = 0; j < arraysize(machineTypes); j++) { 1491 MachineType mem_type = machineTypes[j]; 1492 FunctionEnv env; 1493 FunctionSig sig(1, 0, &local_type); 1494 TestModuleEnv module_env; 1495 init_env(&env, &sig); 1496 env.module = &module_env; 1497 module_env.AddGlobal(mem_type); 1498 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { 1499 EXPECT_VERIFIES_INLINE(&env, WASM_LOAD_GLOBAL(0)); 1500 } else { 1501 EXPECT_FAILURE_INLINE(&env, WASM_LOAD_GLOBAL(0)); 1502 } 1503 } 1504 } 1505 } 1506 1507 1508 TEST_F(WasmDecoderTest, AllStoreGlobalCombinations) { 1509 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1510 LocalType local_type = kLocalTypes[i]; 1511 for (size_t j = 0; j < arraysize(machineTypes); j++) { 1512 MachineType mem_type = machineTypes[j]; 1513 FunctionEnv env; 1514 FunctionSig sig(0, 1, &local_type); 1515 TestModuleEnv module_env; 1516 init_env(&env, &sig); 1517 env.module = &module_env; 1518 module_env.AddGlobal(mem_type); 1519 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { 1520 EXPECT_VERIFIES_INLINE(&env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1521 } else { 1522 EXPECT_FAILURE_INLINE(&env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); 1523 } 1524 } 1525 } 1526 } 1527 1528 1529 TEST_F(WasmDecoderTest, BreakNesting1) { 1530 for (int i = 0; i < 5; i++) { 1531 // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) p) 1532 byte code[] = {WASM_BLOCK( 1533 2, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(i, WASM_ZERO)), 1534 WASM_SET_LOCAL(0, WASM_I8(1))), 1535 WASM_GET_LOCAL(0))}; 1536 if (i < 3) { 1537 EXPECT_VERIFIES(&env_i_i, code); 1538 } else { 1539 EXPECT_FAILURE(&env_i_i, code); 1540 } 1541 } 1542 } 1543 1544 1545 TEST_F(WasmDecoderTest, BreakNesting2) { 1546 env_v_v.AddLocals(kAstI32, 1); 1547 for (int i = 0; i < 5; i++) { 1548 // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) (return p)) (11) 1549 byte code[] = { 1550 WASM_BLOCK(1, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(i)), 1551 WASM_SET_LOCAL(0, WASM_I8(1)))), 1552 WASM_I8(11)}; 1553 if (i < 2) { 1554 EXPECT_VERIFIES(&env_v_v, code); 1555 } else { 1556 EXPECT_FAILURE(&env_v_v, code); 1557 } 1558 } 1559 } 1560 1561 1562 TEST_F(WasmDecoderTest, BreakNesting3) { 1563 env_v_v.AddLocals(kAstI32, 1); 1564 for (int i = 0; i < 5; i++) { 1565 // (block[1] (loop[1] (block[1] (if (get p) break[N]) 1566 byte code[] = {WASM_BLOCK( 1567 1, WASM_LOOP( 1568 1, WASM_BLOCK(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(i)))))}; 1569 if (i < 3) { 1570 EXPECT_VERIFIES(&env_v_v, code); 1571 } else { 1572 EXPECT_FAILURE(&env_v_v, code); 1573 } 1574 } 1575 } 1576 1577 1578 // TODO(tizer): Fix on arm and reenable. 1579 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 1580 1581 TEST_F(WasmDecoderTest, BreaksWithMultipleTypes) { 1582 EXPECT_FAILURE_INLINE( 1583 &env_i_i, 1584 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_ZERO, WASM_I8(7)), WASM_F32(7.7))); 1585 EXPECT_FAILURE_INLINE(&env_i_i, 1586 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_ZERO, WASM_I8(7)), 1587 WASM_BRV_IF(0, WASM_ZERO, WASM_F32(7.7)))); 1588 EXPECT_FAILURE_INLINE(&env_i_i, 1589 WASM_BLOCK(3, WASM_BRV_IF(0, WASM_ZERO, WASM_I8(8)), 1590 WASM_BRV_IF(0, WASM_ZERO, WASM_I8(0)), 1591 WASM_BRV_IF(0, WASM_ZERO, WASM_F32(7.7)))); 1592 EXPECT_FAILURE_INLINE(&env_i_i, 1593 WASM_BLOCK(3, WASM_BRV_IF(0, WASM_ZERO, WASM_I8(9)), 1594 WASM_BRV_IF(0, WASM_ZERO, WASM_F32(7.7)), 1595 WASM_BRV_IF(0, WASM_ZERO, WASM_I8(11)))); 1596 } 1597 1598 #endif 1599 1600 1601 TEST_F(WasmDecoderTest, BreakNesting_6_levels) { 1602 for (int mask = 0; mask < 64; mask++) { 1603 for (int i = 0; i < 14; i++) { 1604 byte code[] = { 1605 kExprBlock, 1, // -- 1606 kExprBlock, 1, // -- 1607 kExprBlock, 1, // -- 1608 kExprBlock, 1, // -- 1609 kExprBlock, 1, // -- 1610 kExprBlock, 1, // -- 1611 kExprBr, static_cast<byte>(i), 1612 kExprNop // -- 1613 }; 1614 1615 int depth = 6; 1616 for (int l = 0; l < 6; l++) { 1617 if (mask & (1 << l)) { 1618 code[l * 2] = kExprLoop; 1619 depth++; 1620 } 1621 } 1622 1623 if (i < depth) { 1624 EXPECT_VERIFIES(&env_v_v, code); 1625 } else { 1626 EXPECT_FAILURE(&env_v_v, code); 1627 } 1628 } 1629 } 1630 } 1631 1632 1633 // TODO(tizer): Fix on arm and reenable. 1634 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 1635 1636 TEST_F(WasmDecoderTest, ExprBreak_TypeCheck) { 1637 FunctionEnv* envs[] = {&env_i_i, &env_l_l, &env_f_ff, &env_d_dd}; 1638 for (size_t i = 0; i < arraysize(envs); i++) { 1639 FunctionEnv* env = envs[i]; 1640 // unify X and X => OK 1641 EXPECT_VERIFIES_INLINE( 1642 env, WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), 1643 WASM_GET_LOCAL(0))); 1644 } 1645 1646 // unify i32 and f32 => fail 1647 EXPECT_FAILURE_INLINE( 1648 &env_i_i, 1649 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_ZERO)), WASM_F32(1.2))); 1650 1651 // unify f64 and f64 => OK 1652 EXPECT_VERIFIES_INLINE( 1653 &env_d_dd, 1654 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), 1655 WASM_F64(1.2))); 1656 } 1657 1658 #endif 1659 1660 1661 TEST_F(WasmDecoderTest, ExprBreak_TypeCheckAll) { 1662 byte code1[] = {WASM_BLOCK(2, 1663 WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), 1664 WASM_GET_LOCAL(1))}; 1665 byte code2[] = {WASM_BLOCK( 1666 2, WASM_IF(WASM_ZERO, WASM_BRV_IF(0, WASM_ZERO, WASM_GET_LOCAL(0))), 1667 WASM_GET_LOCAL(1))}; 1668 1669 1670 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1671 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { 1672 FunctionEnv env; 1673 LocalType storage[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j]}; 1674 FunctionSig sig(1, 2, storage); 1675 init_env(&env, &sig); 1676 1677 if (i == j) { 1678 EXPECT_VERIFIES(&env, code1); 1679 EXPECT_VERIFIES(&env, code2); 1680 } else { 1681 EXPECT_FAILURE(&env, code1); 1682 EXPECT_FAILURE(&env, code2); 1683 } 1684 } 1685 } 1686 } 1687 1688 1689 TEST_F(WasmDecoderTest, ExprBr_Unify) { 1690 FunctionEnv env; 1691 1692 for (int which = 0; which < 2; which++) { 1693 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1694 LocalType type = kLocalTypes[i]; 1695 LocalType storage[] = {kAstI32, kAstI32, type}; 1696 FunctionSig sig(1, 2, storage); 1697 init_env(&env, &sig); // (i32, X) -> i32 1698 1699 byte code1[] = { 1700 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(which))), 1701 WASM_GET_LOCAL(which ^ 1))}; 1702 byte code2[] = { 1703 WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BRV(1, WASM_GET_LOCAL(which))), 1704 WASM_GET_LOCAL(which ^ 1))}; 1705 1706 1707 if (type == kAstI32) { 1708 EXPECT_VERIFIES(&env, code1); 1709 EXPECT_VERIFIES(&env, code2); 1710 } else { 1711 EXPECT_FAILURE(&env, code1); 1712 EXPECT_FAILURE(&env, code2); 1713 } 1714 } 1715 } 1716 } 1717 1718 1719 TEST_F(WasmDecoderTest, ExprBrIf_type) { 1720 EXPECT_VERIFIES_INLINE( 1721 &env_i_i, 1722 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)), 1723 WASM_GET_LOCAL(0))); 1724 EXPECT_FAILURE_INLINE( 1725 &env_d_dd, 1726 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(0)), 1727 WASM_GET_LOCAL(0))); 1728 1729 FunctionEnv env; 1730 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1731 LocalType type = kLocalTypes[i]; 1732 LocalType storage[] = {kAstI32, kAstI32, type}; 1733 FunctionSig sig(1, 2, storage); 1734 init_env(&env, &sig); // (i32, X) -> i32 1735 1736 byte code1[] = { 1737 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)), 1738 WASM_GET_LOCAL(0))}; 1739 1740 byte code2[] = { 1741 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(1), WASM_GET_LOCAL(0)), 1742 WASM_GET_LOCAL(0))}; 1743 if (type == kAstI32) { 1744 EXPECT_VERIFIES(&env, code1); 1745 EXPECT_VERIFIES(&env, code2); 1746 } else { 1747 EXPECT_FAILURE(&env, code1); 1748 EXPECT_FAILURE(&env, code2); 1749 } 1750 } 1751 } 1752 1753 1754 TEST_F(WasmDecoderTest, ExprBrIf_Unify) { 1755 FunctionEnv env; 1756 1757 for (int which = 0; which < 2; which++) { 1758 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 1759 LocalType type = kLocalTypes[i]; 1760 LocalType storage[] = {kAstI32, kAstI32, type}; 1761 FunctionSig sig(1, 2, storage); 1762 init_env(&env, &sig); // (i32, X) -> i32 1763 1764 byte code1[] = { 1765 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_ZERO, WASM_GET_LOCAL(which)), 1766 WASM_GET_LOCAL(which ^ 1))}; 1767 byte code2[] = { 1768 WASM_LOOP(2, WASM_BRV_IF(1, WASM_ZERO, WASM_GET_LOCAL(which)), 1769 WASM_GET_LOCAL(which ^ 1))}; 1770 1771 1772 if (type == kAstI32) { 1773 EXPECT_VERIFIES(&env, code1); 1774 EXPECT_VERIFIES(&env, code2); 1775 } else { 1776 EXPECT_FAILURE(&env, code1); 1777 EXPECT_FAILURE(&env, code2); 1778 } 1779 } 1780 } 1781 } 1782 1783 1784 TEST_F(WasmDecoderTest, TableSwitch0) { 1785 static byte code[] = {kExprTableSwitch, 0, 0, 0, 0}; 1786 EXPECT_FAILURE(&env_v_v, code); 1787 } 1788 1789 1790 TEST_F(WasmDecoderTest, TableSwitch0b) { 1791 static byte code[] = {kExprTableSwitch, 0, 0, 0, 0, kExprI8Const, 11}; 1792 EXPECT_FAILURE(&env_v_v, code); 1793 EXPECT_FAILURE(&env_i_i, code); 1794 } 1795 1796 1797 TEST_F(WasmDecoderTest, TableSwitch0c) { 1798 static byte code[] = { 1799 WASM_BLOCK(1, WASM_TABLESWITCH_OP(0, 1, WASM_CASE_BR(0)), WASM_I8(67))}; 1800 EXPECT_VERIFIES(&env_v_v, code); 1801 } 1802 1803 1804 TEST_F(WasmDecoderTest, TableSwitch1) { 1805 static byte code[] = {WASM_TABLESWITCH_OP(1, 1, WASM_CASE(0)), 1806 WASM_TABLESWITCH_BODY(WASM_I8(0), WASM_I8(9))}; 1807 EXPECT_VERIFIES(&env_i_i, code); 1808 EXPECT_VERIFIES(&env_v_v, code); 1809 EXPECT_FAILURE(&env_f_ff, code); 1810 EXPECT_FAILURE(&env_d_dd, code); 1811 } 1812 1813 1814 TEST_F(WasmDecoderTest, TableSwitch_off_end) { 1815 static byte code[] = {WASM_TABLESWITCH_OP(1, 1, WASM_CASE(0)), 1816 WASM_TABLESWITCH_BODY(WASM_I8(0), WASM_I8(9))}; 1817 for (size_t len = arraysize(code) - 1; len > 0; len--) { 1818 Verify(kError, &env_v_v, code, code + len); 1819 } 1820 } 1821 1822 1823 TEST_F(WasmDecoderTest, TableSwitch2) { 1824 static byte code[] = { 1825 WASM_TABLESWITCH_OP(2, 2, WASM_CASE(0), WASM_CASE(1)), 1826 WASM_TABLESWITCH_BODY(WASM_I8(3), WASM_I8(10), WASM_I8(11))}; 1827 EXPECT_VERIFIES(&env_i_i, code); 1828 EXPECT_VERIFIES(&env_v_v, code); 1829 EXPECT_FAILURE(&env_f_ff, code); 1830 EXPECT_FAILURE(&env_d_dd, code); 1831 } 1832 1833 1834 // TODO(tizer): Fix on arm and reenable. 1835 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 1836 1837 TEST_F(WasmDecoderTest, TableSwitch1b) { 1838 EXPECT_VERIFIES_INLINE(&env_i_i, WASM_TABLESWITCH_OP(1, 1, WASM_CASE(0)), 1839 WASM_TABLESWITCH_BODY(WASM_GET_LOCAL(0), WASM_ZERO)); 1840 1841 EXPECT_VERIFIES_INLINE(&env_f_ff, WASM_TABLESWITCH_OP(1, 1, WASM_CASE(0)), 1842 WASM_TABLESWITCH_BODY(WASM_ZERO, WASM_F32(0.0))); 1843 1844 EXPECT_VERIFIES_INLINE(&env_d_dd, WASM_TABLESWITCH_OP(1, 1, WASM_CASE(0)), 1845 WASM_TABLESWITCH_BODY(WASM_ZERO, WASM_F64(0.0))); 1846 } 1847 1848 #endif 1849 1850 1851 TEST_F(WasmDecoderTest, TableSwitch_br) { 1852 EXPECT_VERIFIES_INLINE(&env_i_i, WASM_TABLESWITCH_OP(0, 1, WASM_CASE_BR(0)), 1853 WASM_GET_LOCAL(0)); 1854 for (int depth = 0; depth < 2; depth++) { 1855 EXPECT_VERIFIES_INLINE( 1856 &env_i_i, WASM_BLOCK(1, WASM_TABLESWITCH_OP(0, 1, WASM_CASE_BR(depth)), 1857 WASM_GET_LOCAL(0))); 1858 } 1859 } 1860 1861 1862 TEST_F(WasmDecoderTest, TableSwitch_invalid_br) { 1863 for (int depth = 1; depth < 4; depth++) { 1864 EXPECT_FAILURE_INLINE(&env_i_i, 1865 WASM_TABLESWITCH_OP(0, 1, WASM_CASE_BR(depth)), 1866 WASM_GET_LOCAL(0)); 1867 EXPECT_FAILURE_INLINE( 1868 &env_i_i, 1869 WASM_BLOCK(1, WASM_TABLESWITCH_OP(0, 1, WASM_CASE_BR(depth + 1)), 1870 WASM_GET_LOCAL(0))); 1871 } 1872 } 1873 1874 1875 TEST_F(WasmDecoderTest, TableSwitch_invalid_case_ref) { 1876 EXPECT_FAILURE_INLINE(&env_i_i, WASM_TABLESWITCH_OP(0, 1, WASM_CASE(0)), 1877 WASM_GET_LOCAL(0)); 1878 EXPECT_FAILURE_INLINE(&env_i_i, WASM_TABLESWITCH_OP(1, 1, WASM_CASE(1)), 1879 WASM_TABLESWITCH_BODY(WASM_GET_LOCAL(0), WASM_ZERO)); 1880 } 1881 1882 1883 // TODO(tizer): Fix on arm and reenable. 1884 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 1885 1886 TEST_F(WasmDecoderTest, TableSwitch1_br) { 1887 EXPECT_VERIFIES_INLINE( 1888 &env_i_i, WASM_TABLESWITCH_OP(1, 1, WASM_CASE(0)), 1889 WASM_TABLESWITCH_BODY(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_ZERO))); 1890 } 1891 1892 #endif 1893 1894 1895 TEST_F(WasmDecoderTest, TableSwitch2_br) { 1896 EXPECT_VERIFIES_INLINE( 1897 &env_i_i, WASM_TABLESWITCH_OP(2, 2, WASM_CASE(0), WASM_CASE(1)), 1898 WASM_TABLESWITCH_BODY(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(0)), 1899 WASM_BRV(0, WASM_I8(1)))); 1900 1901 EXPECT_FAILURE_INLINE( 1902 &env_f_ff, WASM_TABLESWITCH_OP(2, 2, WASM_CASE(0), WASM_CASE(1)), 1903 WASM_TABLESWITCH_BODY(WASM_ZERO, WASM_BRV(0, WASM_I8(3)), 1904 WASM_BRV(0, WASM_I8(4)))); 1905 } 1906 1907 1908 TEST_F(WasmDecoderTest, TableSwitch2x2) { 1909 EXPECT_VERIFIES_INLINE( 1910 &env_i_i, WASM_TABLESWITCH_OP(2, 4, WASM_CASE(0), WASM_CASE(1), 1911 WASM_CASE(0), WASM_CASE(1)), 1912 WASM_TABLESWITCH_BODY(WASM_GET_LOCAL(0), WASM_BRV(0, WASM_I8(3)), 1913 WASM_BRV(0, WASM_I8(4)))); 1914 } 1915 1916 1917 // TODO(tizer): Fix on arm and reenable. 1918 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 1919 1920 TEST_F(WasmDecoderTest, ExprBreakNesting1) { 1921 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BRV(0, WASM_ZERO))); 1922 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BR(0))); 1923 EXPECT_VERIFIES_INLINE(&env_v_v, 1924 WASM_BLOCK(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); 1925 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BR_IF(0, WASM_ZERO))); 1926 1927 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BRV(0, WASM_ZERO))); 1928 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR(0))); 1929 EXPECT_VERIFIES_INLINE(&env_v_v, 1930 WASM_LOOP(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); 1931 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO))); 1932 1933 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BRV(1, WASM_ZERO))); 1934 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR(1))); 1935 } 1936 1937 #endif 1938 1939 1940 TEST_F(WasmDecoderTest, Select) { 1941 EXPECT_VERIFIES_INLINE( 1942 &env_i_i, 1943 WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); 1944 } 1945 1946 1947 // TODO(tizer): Fix on arm and reenable. 1948 #if !V8_TARGET_ARCH_ARM && !V8_TARGET_ARCH_ARM64 1949 1950 TEST_F(WasmDecoderTest, Select_TypeCheck) { 1951 EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), 1952 WASM_GET_LOCAL(0))); 1953 1954 EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_GET_LOCAL(0), WASM_F64(0.25), 1955 WASM_GET_LOCAL(0))); 1956 1957 EXPECT_FAILURE_INLINE( 1958 &env_i_i, WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), WASM_I64(0))); 1959 } 1960 1961 #endif 1962 1963 1964 class WasmOpcodeLengthTest : public TestWithZone { 1965 public: 1966 WasmOpcodeLengthTest() : TestWithZone() {} 1967 }; 1968 1969 1970 #define EXPECT_LENGTH(expected, opcode) \ 1971 { \ 1972 static const byte code[] = {opcode, 0, 0, 0, 0, 0, 0, 0, 0}; \ 1973 EXPECT_EQ(expected, OpcodeLength(code)); \ 1974 } 1975 1976 1977 TEST_F(WasmOpcodeLengthTest, Statements) { 1978 EXPECT_LENGTH(1, kExprNop); 1979 EXPECT_LENGTH(2, kExprBlock); 1980 EXPECT_LENGTH(2, kExprLoop); 1981 EXPECT_LENGTH(1, kExprIf); 1982 EXPECT_LENGTH(1, kExprIfElse); 1983 EXPECT_LENGTH(1, kExprSelect); 1984 EXPECT_LENGTH(2, kExprBr); 1985 EXPECT_LENGTH(2, kExprBrIf); 1986 } 1987 1988 1989 TEST_F(WasmOpcodeLengthTest, MiscExpressions) { 1990 EXPECT_LENGTH(2, kExprI8Const); 1991 EXPECT_LENGTH(5, kExprI32Const); 1992 EXPECT_LENGTH(5, kExprF32Const); 1993 EXPECT_LENGTH(9, kExprI64Const); 1994 EXPECT_LENGTH(9, kExprF64Const); 1995 EXPECT_LENGTH(2, kExprGetLocal); 1996 EXPECT_LENGTH(2, kExprSetLocal); 1997 EXPECT_LENGTH(2, kExprLoadGlobal); 1998 EXPECT_LENGTH(2, kExprStoreGlobal); 1999 EXPECT_LENGTH(2, kExprCallFunction); 2000 EXPECT_LENGTH(2, kExprCallIndirect); 2001 EXPECT_LENGTH(1, kExprIf); 2002 EXPECT_LENGTH(1, kExprIfElse); 2003 EXPECT_LENGTH(2, kExprBlock); 2004 EXPECT_LENGTH(2, kExprLoop); 2005 EXPECT_LENGTH(2, kExprBr); 2006 EXPECT_LENGTH(2, kExprBrIf); 2007 } 2008 2009 2010 TEST_F(WasmOpcodeLengthTest, VariableLength) { 2011 byte size2[] = {kExprLoadGlobal, 1}; 2012 byte size3[] = {kExprLoadGlobal, 1 | 0x80, 2}; 2013 byte size4[] = {kExprLoadGlobal, 1 | 0x80, 2 | 0x80, 3}; 2014 byte size5[] = {kExprLoadGlobal, 1 | 0x80, 2 | 0x80, 3 | 0x80, 4}; 2015 byte size6[] = {kExprLoadGlobal, 1 | 0x80, 2 | 0x80, 3 | 0x80, 4 | 0x80, 5}; 2016 2017 EXPECT_EQ(2, OpcodeLength(size2)); 2018 EXPECT_EQ(3, OpcodeLength(size3)); 2019 EXPECT_EQ(4, OpcodeLength(size4)); 2020 EXPECT_EQ(5, OpcodeLength(size5)); 2021 EXPECT_EQ(6, OpcodeLength(size6)); 2022 } 2023 2024 2025 TEST_F(WasmOpcodeLengthTest, LoadsAndStores) { 2026 EXPECT_LENGTH(2, kExprI32LoadMem8S); 2027 EXPECT_LENGTH(2, kExprI32LoadMem8U); 2028 EXPECT_LENGTH(2, kExprI32LoadMem16S); 2029 EXPECT_LENGTH(2, kExprI32LoadMem16U); 2030 EXPECT_LENGTH(2, kExprI32LoadMem); 2031 EXPECT_LENGTH(2, kExprI64LoadMem8S); 2032 EXPECT_LENGTH(2, kExprI64LoadMem8U); 2033 EXPECT_LENGTH(2, kExprI64LoadMem16S); 2034 EXPECT_LENGTH(2, kExprI64LoadMem16U); 2035 EXPECT_LENGTH(2, kExprI64LoadMem32S); 2036 EXPECT_LENGTH(2, kExprI64LoadMem32U); 2037 EXPECT_LENGTH(2, kExprI64LoadMem); 2038 EXPECT_LENGTH(2, kExprF32LoadMem); 2039 EXPECT_LENGTH(2, kExprF64LoadMem); 2040 2041 EXPECT_LENGTH(2, kExprI32StoreMem8); 2042 EXPECT_LENGTH(2, kExprI32StoreMem16); 2043 EXPECT_LENGTH(2, kExprI32StoreMem); 2044 EXPECT_LENGTH(2, kExprI64StoreMem8); 2045 EXPECT_LENGTH(2, kExprI64StoreMem16); 2046 EXPECT_LENGTH(2, kExprI64StoreMem32); 2047 EXPECT_LENGTH(2, kExprI64StoreMem); 2048 EXPECT_LENGTH(2, kExprF32StoreMem); 2049 EXPECT_LENGTH(2, kExprF64StoreMem); 2050 } 2051 2052 2053 TEST_F(WasmOpcodeLengthTest, MiscMemExpressions) { 2054 EXPECT_LENGTH(1, kExprMemorySize); 2055 EXPECT_LENGTH(1, kExprGrowMemory); 2056 } 2057 2058 2059 TEST_F(WasmOpcodeLengthTest, SimpleExpressions) { 2060 EXPECT_LENGTH(1, kExprI32Add); 2061 EXPECT_LENGTH(1, kExprI32Sub); 2062 EXPECT_LENGTH(1, kExprI32Mul); 2063 EXPECT_LENGTH(1, kExprI32DivS); 2064 EXPECT_LENGTH(1, kExprI32DivU); 2065 EXPECT_LENGTH(1, kExprI32RemS); 2066 EXPECT_LENGTH(1, kExprI32RemU); 2067 EXPECT_LENGTH(1, kExprI32And); 2068 EXPECT_LENGTH(1, kExprI32Ior); 2069 EXPECT_LENGTH(1, kExprI32Xor); 2070 EXPECT_LENGTH(1, kExprI32Shl); 2071 EXPECT_LENGTH(1, kExprI32ShrU); 2072 EXPECT_LENGTH(1, kExprI32ShrS); 2073 EXPECT_LENGTH(1, kExprI32Eq); 2074 EXPECT_LENGTH(1, kExprI32Ne); 2075 EXPECT_LENGTH(1, kExprI32LtS); 2076 EXPECT_LENGTH(1, kExprI32LeS); 2077 EXPECT_LENGTH(1, kExprI32LtU); 2078 EXPECT_LENGTH(1, kExprI32LeU); 2079 EXPECT_LENGTH(1, kExprI32GtS); 2080 EXPECT_LENGTH(1, kExprI32GeS); 2081 EXPECT_LENGTH(1, kExprI32GtU); 2082 EXPECT_LENGTH(1, kExprI32GeU); 2083 EXPECT_LENGTH(1, kExprI32Clz); 2084 EXPECT_LENGTH(1, kExprI32Ctz); 2085 EXPECT_LENGTH(1, kExprI32Popcnt); 2086 EXPECT_LENGTH(1, kExprBoolNot); 2087 EXPECT_LENGTH(1, kExprI64Add); 2088 EXPECT_LENGTH(1, kExprI64Sub); 2089 EXPECT_LENGTH(1, kExprI64Mul); 2090 EXPECT_LENGTH(1, kExprI64DivS); 2091 EXPECT_LENGTH(1, kExprI64DivU); 2092 EXPECT_LENGTH(1, kExprI64RemS); 2093 EXPECT_LENGTH(1, kExprI64RemU); 2094 EXPECT_LENGTH(1, kExprI64And); 2095 EXPECT_LENGTH(1, kExprI64Ior); 2096 EXPECT_LENGTH(1, kExprI64Xor); 2097 EXPECT_LENGTH(1, kExprI64Shl); 2098 EXPECT_LENGTH(1, kExprI64ShrU); 2099 EXPECT_LENGTH(1, kExprI64ShrS); 2100 EXPECT_LENGTH(1, kExprI64Eq); 2101 EXPECT_LENGTH(1, kExprI64Ne); 2102 EXPECT_LENGTH(1, kExprI64LtS); 2103 EXPECT_LENGTH(1, kExprI64LeS); 2104 EXPECT_LENGTH(1, kExprI64LtU); 2105 EXPECT_LENGTH(1, kExprI64LeU); 2106 EXPECT_LENGTH(1, kExprI64GtS); 2107 EXPECT_LENGTH(1, kExprI64GeS); 2108 EXPECT_LENGTH(1, kExprI64GtU); 2109 EXPECT_LENGTH(1, kExprI64GeU); 2110 EXPECT_LENGTH(1, kExprI64Clz); 2111 EXPECT_LENGTH(1, kExprI64Ctz); 2112 EXPECT_LENGTH(1, kExprI64Popcnt); 2113 EXPECT_LENGTH(1, kExprF32Add); 2114 EXPECT_LENGTH(1, kExprF32Sub); 2115 EXPECT_LENGTH(1, kExprF32Mul); 2116 EXPECT_LENGTH(1, kExprF32Div); 2117 EXPECT_LENGTH(1, kExprF32Min); 2118 EXPECT_LENGTH(1, kExprF32Max); 2119 EXPECT_LENGTH(1, kExprF32Abs); 2120 EXPECT_LENGTH(1, kExprF32Neg); 2121 EXPECT_LENGTH(1, kExprF32CopySign); 2122 EXPECT_LENGTH(1, kExprF32Ceil); 2123 EXPECT_LENGTH(1, kExprF32Floor); 2124 EXPECT_LENGTH(1, kExprF32Trunc); 2125 EXPECT_LENGTH(1, kExprF32NearestInt); 2126 EXPECT_LENGTH(1, kExprF32Sqrt); 2127 EXPECT_LENGTH(1, kExprF32Eq); 2128 EXPECT_LENGTH(1, kExprF32Ne); 2129 EXPECT_LENGTH(1, kExprF32Lt); 2130 EXPECT_LENGTH(1, kExprF32Le); 2131 EXPECT_LENGTH(1, kExprF32Gt); 2132 EXPECT_LENGTH(1, kExprF32Ge); 2133 EXPECT_LENGTH(1, kExprF64Add); 2134 EXPECT_LENGTH(1, kExprF64Sub); 2135 EXPECT_LENGTH(1, kExprF64Mul); 2136 EXPECT_LENGTH(1, kExprF64Div); 2137 EXPECT_LENGTH(1, kExprF64Min); 2138 EXPECT_LENGTH(1, kExprF64Max); 2139 EXPECT_LENGTH(1, kExprF64Abs); 2140 EXPECT_LENGTH(1, kExprF64Neg); 2141 EXPECT_LENGTH(1, kExprF64CopySign); 2142 EXPECT_LENGTH(1, kExprF64Ceil); 2143 EXPECT_LENGTH(1, kExprF64Floor); 2144 EXPECT_LENGTH(1, kExprF64Trunc); 2145 EXPECT_LENGTH(1, kExprF64NearestInt); 2146 EXPECT_LENGTH(1, kExprF64Sqrt); 2147 EXPECT_LENGTH(1, kExprF64Eq); 2148 EXPECT_LENGTH(1, kExprF64Ne); 2149 EXPECT_LENGTH(1, kExprF64Lt); 2150 EXPECT_LENGTH(1, kExprF64Le); 2151 EXPECT_LENGTH(1, kExprF64Gt); 2152 EXPECT_LENGTH(1, kExprF64Ge); 2153 EXPECT_LENGTH(1, kExprI32SConvertF32); 2154 EXPECT_LENGTH(1, kExprI32SConvertF64); 2155 EXPECT_LENGTH(1, kExprI32UConvertF32); 2156 EXPECT_LENGTH(1, kExprI32UConvertF64); 2157 EXPECT_LENGTH(1, kExprI32ConvertI64); 2158 EXPECT_LENGTH(1, kExprI64SConvertF32); 2159 EXPECT_LENGTH(1, kExprI64SConvertF64); 2160 EXPECT_LENGTH(1, kExprI64UConvertF32); 2161 EXPECT_LENGTH(1, kExprI64UConvertF64); 2162 EXPECT_LENGTH(1, kExprI64SConvertI32); 2163 EXPECT_LENGTH(1, kExprI64UConvertI32); 2164 EXPECT_LENGTH(1, kExprF32SConvertI32); 2165 EXPECT_LENGTH(1, kExprF32UConvertI32); 2166 EXPECT_LENGTH(1, kExprF32SConvertI64); 2167 EXPECT_LENGTH(1, kExprF32UConvertI64); 2168 EXPECT_LENGTH(1, kExprF32ConvertF64); 2169 EXPECT_LENGTH(1, kExprF32ReinterpretI32); 2170 EXPECT_LENGTH(1, kExprF64SConvertI32); 2171 EXPECT_LENGTH(1, kExprF64UConvertI32); 2172 EXPECT_LENGTH(1, kExprF64SConvertI64); 2173 EXPECT_LENGTH(1, kExprF64UConvertI64); 2174 EXPECT_LENGTH(1, kExprF64ConvertF32); 2175 EXPECT_LENGTH(1, kExprF64ReinterpretI64); 2176 EXPECT_LENGTH(1, kExprI32ReinterpretF32); 2177 EXPECT_LENGTH(1, kExprI64ReinterpretF64); 2178 } 2179 2180 2181 class WasmOpcodeArityTest : public TestWithZone { 2182 public: 2183 WasmOpcodeArityTest() : TestWithZone() {} 2184 }; 2185 2186 2187 #define EXPECT_ARITY(expected, ...) \ 2188 { \ 2189 static const byte code[] = {__VA_ARGS__}; \ 2190 EXPECT_EQ(expected, OpcodeArity(&env, code)); \ 2191 } 2192 2193 2194 TEST_F(WasmOpcodeArityTest, Control) { 2195 FunctionEnv env; 2196 EXPECT_ARITY(0, kExprNop); 2197 2198 EXPECT_ARITY(0, kExprBlock, 0); 2199 EXPECT_ARITY(1, kExprBlock, 1); 2200 EXPECT_ARITY(2, kExprBlock, 2); 2201 EXPECT_ARITY(5, kExprBlock, 5); 2202 EXPECT_ARITY(10, kExprBlock, 10); 2203 2204 EXPECT_ARITY(0, kExprLoop, 0); 2205 EXPECT_ARITY(1, kExprLoop, 1); 2206 EXPECT_ARITY(2, kExprLoop, 2); 2207 EXPECT_ARITY(7, kExprLoop, 7); 2208 EXPECT_ARITY(11, kExprLoop, 11); 2209 2210 EXPECT_ARITY(2, kExprIf); 2211 EXPECT_ARITY(3, kExprIfElse); 2212 EXPECT_ARITY(3, kExprSelect); 2213 2214 EXPECT_ARITY(1, kExprBr); 2215 EXPECT_ARITY(2, kExprBrIf); 2216 2217 { 2218 TestSignatures sigs; 2219 FunctionEnv env; 2220 WasmDecoderTest::init_env(&env, sigs.v_v()); 2221 EXPECT_ARITY(0, kExprReturn); 2222 WasmDecoderTest::init_env(&env, sigs.i_i()); 2223 EXPECT_ARITY(1, kExprReturn); 2224 } 2225 } 2226 2227 2228 TEST_F(WasmOpcodeArityTest, Misc) { 2229 FunctionEnv env; 2230 2231 EXPECT_ARITY(0, kExprI8Const); 2232 EXPECT_ARITY(0, kExprI32Const); 2233 EXPECT_ARITY(0, kExprF32Const); 2234 EXPECT_ARITY(0, kExprI64Const); 2235 EXPECT_ARITY(0, kExprF64Const); 2236 EXPECT_ARITY(0, kExprGetLocal); 2237 EXPECT_ARITY(1, kExprSetLocal); 2238 EXPECT_ARITY(0, kExprLoadGlobal); 2239 EXPECT_ARITY(1, kExprStoreGlobal); 2240 } 2241 2242 2243 TEST_F(WasmOpcodeArityTest, Calls) { 2244 TestSignatures sigs; 2245 TestModuleEnv module; 2246 module.AddFunction(sigs.i_ii()); 2247 module.AddFunction(sigs.i_i()); 2248 2249 module.AddSignature(sigs.f_ff()); 2250 module.AddSignature(sigs.i_d()); 2251 2252 { 2253 FunctionEnv env; 2254 WasmDecoderTest::init_env(&env, sigs.i_ii()); 2255 env.module = &module; 2256 2257 EXPECT_ARITY(2, kExprCallFunction, 0); 2258 EXPECT_ARITY(3, kExprCallIndirect, 0); 2259 EXPECT_ARITY(1, kExprBr); 2260 EXPECT_ARITY(2, kExprBrIf); 2261 } 2262 2263 { 2264 FunctionEnv env; 2265 WasmDecoderTest::init_env(&env, sigs.v_v()); 2266 env.module = &module; 2267 2268 EXPECT_ARITY(1, kExprCallFunction, 1); 2269 EXPECT_ARITY(2, kExprCallIndirect, 1); 2270 EXPECT_ARITY(1, kExprBr); 2271 EXPECT_ARITY(2, kExprBrIf); 2272 } 2273 } 2274 2275 2276 TEST_F(WasmOpcodeArityTest, LoadsAndStores) { 2277 FunctionEnv env; 2278 2279 EXPECT_ARITY(1, kExprI32LoadMem8S); 2280 EXPECT_ARITY(1, kExprI32LoadMem8U); 2281 EXPECT_ARITY(1, kExprI32LoadMem16S); 2282 EXPECT_ARITY(1, kExprI32LoadMem16U); 2283 EXPECT_ARITY(1, kExprI32LoadMem); 2284 2285 EXPECT_ARITY(1, kExprI64LoadMem8S); 2286 EXPECT_ARITY(1, kExprI64LoadMem8U); 2287 EXPECT_ARITY(1, kExprI64LoadMem16S); 2288 EXPECT_ARITY(1, kExprI64LoadMem16U); 2289 EXPECT_ARITY(1, kExprI64LoadMem32S); 2290 EXPECT_ARITY(1, kExprI64LoadMem32U); 2291 EXPECT_ARITY(1, kExprI64LoadMem); 2292 EXPECT_ARITY(1, kExprF32LoadMem); 2293 EXPECT_ARITY(1, kExprF64LoadMem); 2294 2295 EXPECT_ARITY(2, kExprI32StoreMem8); 2296 EXPECT_ARITY(2, kExprI32StoreMem16); 2297 EXPECT_ARITY(2, kExprI32StoreMem); 2298 EXPECT_ARITY(2, kExprI64StoreMem8); 2299 EXPECT_ARITY(2, kExprI64StoreMem16); 2300 EXPECT_ARITY(2, kExprI64StoreMem32); 2301 EXPECT_ARITY(2, kExprI64StoreMem); 2302 EXPECT_ARITY(2, kExprF32StoreMem); 2303 EXPECT_ARITY(2, kExprF64StoreMem); 2304 } 2305 2306 2307 TEST_F(WasmOpcodeArityTest, MiscMemExpressions) { 2308 FunctionEnv env; 2309 2310 EXPECT_ARITY(0, kExprMemorySize); 2311 EXPECT_ARITY(1, kExprGrowMemory); 2312 } 2313 2314 2315 TEST_F(WasmOpcodeArityTest, SimpleExpressions) { 2316 FunctionEnv env; 2317 2318 EXPECT_ARITY(2, kExprI32Add); 2319 EXPECT_ARITY(2, kExprI32Sub); 2320 EXPECT_ARITY(2, kExprI32Mul); 2321 EXPECT_ARITY(2, kExprI32DivS); 2322 EXPECT_ARITY(2, kExprI32DivU); 2323 EXPECT_ARITY(2, kExprI32RemS); 2324 EXPECT_ARITY(2, kExprI32RemU); 2325 EXPECT_ARITY(2, kExprI32And); 2326 EXPECT_ARITY(2, kExprI32Ior); 2327 EXPECT_ARITY(2, kExprI32Xor); 2328 EXPECT_ARITY(2, kExprI32Shl); 2329 EXPECT_ARITY(2, kExprI32ShrU); 2330 EXPECT_ARITY(2, kExprI32ShrS); 2331 EXPECT_ARITY(2, kExprI32Eq); 2332 EXPECT_ARITY(2, kExprI32Ne); 2333 EXPECT_ARITY(2, kExprI32LtS); 2334 EXPECT_ARITY(2, kExprI32LeS); 2335 EXPECT_ARITY(2, kExprI32LtU); 2336 EXPECT_ARITY(2, kExprI32LeU); 2337 EXPECT_ARITY(2, kExprI32GtS); 2338 EXPECT_ARITY(2, kExprI32GeS); 2339 EXPECT_ARITY(2, kExprI32GtU); 2340 EXPECT_ARITY(2, kExprI32GeU); 2341 EXPECT_ARITY(1, kExprI32Clz); 2342 EXPECT_ARITY(1, kExprI32Ctz); 2343 EXPECT_ARITY(1, kExprI32Popcnt); 2344 EXPECT_ARITY(1, kExprBoolNot); 2345 EXPECT_ARITY(2, kExprI64Add); 2346 EXPECT_ARITY(2, kExprI64Sub); 2347 EXPECT_ARITY(2, kExprI64Mul); 2348 EXPECT_ARITY(2, kExprI64DivS); 2349 EXPECT_ARITY(2, kExprI64DivU); 2350 EXPECT_ARITY(2, kExprI64RemS); 2351 EXPECT_ARITY(2, kExprI64RemU); 2352 EXPECT_ARITY(2, kExprI64And); 2353 EXPECT_ARITY(2, kExprI64Ior); 2354 EXPECT_ARITY(2, kExprI64Xor); 2355 EXPECT_ARITY(2, kExprI64Shl); 2356 EXPECT_ARITY(2, kExprI64ShrU); 2357 EXPECT_ARITY(2, kExprI64ShrS); 2358 EXPECT_ARITY(2, kExprI64Eq); 2359 EXPECT_ARITY(2, kExprI64Ne); 2360 EXPECT_ARITY(2, kExprI64LtS); 2361 EXPECT_ARITY(2, kExprI64LeS); 2362 EXPECT_ARITY(2, kExprI64LtU); 2363 EXPECT_ARITY(2, kExprI64LeU); 2364 EXPECT_ARITY(2, kExprI64GtS); 2365 EXPECT_ARITY(2, kExprI64GeS); 2366 EXPECT_ARITY(2, kExprI64GtU); 2367 EXPECT_ARITY(2, kExprI64GeU); 2368 EXPECT_ARITY(1, kExprI64Clz); 2369 EXPECT_ARITY(1, kExprI64Ctz); 2370 EXPECT_ARITY(1, kExprI64Popcnt); 2371 EXPECT_ARITY(2, kExprF32Add); 2372 EXPECT_ARITY(2, kExprF32Sub); 2373 EXPECT_ARITY(2, kExprF32Mul); 2374 EXPECT_ARITY(2, kExprF32Div); 2375 EXPECT_ARITY(2, kExprF32Min); 2376 EXPECT_ARITY(2, kExprF32Max); 2377 EXPECT_ARITY(1, kExprF32Abs); 2378 EXPECT_ARITY(1, kExprF32Neg); 2379 EXPECT_ARITY(2, kExprF32CopySign); 2380 EXPECT_ARITY(1, kExprF32Ceil); 2381 EXPECT_ARITY(1, kExprF32Floor); 2382 EXPECT_ARITY(1, kExprF32Trunc); 2383 EXPECT_ARITY(1, kExprF32NearestInt); 2384 EXPECT_ARITY(1, kExprF32Sqrt); 2385 EXPECT_ARITY(2, kExprF32Eq); 2386 EXPECT_ARITY(2, kExprF32Ne); 2387 EXPECT_ARITY(2, kExprF32Lt); 2388 EXPECT_ARITY(2, kExprF32Le); 2389 EXPECT_ARITY(2, kExprF32Gt); 2390 EXPECT_ARITY(2, kExprF32Ge); 2391 EXPECT_ARITY(2, kExprF64Add); 2392 EXPECT_ARITY(2, kExprF64Sub); 2393 EXPECT_ARITY(2, kExprF64Mul); 2394 EXPECT_ARITY(2, kExprF64Div); 2395 EXPECT_ARITY(2, kExprF64Min); 2396 EXPECT_ARITY(2, kExprF64Max); 2397 EXPECT_ARITY(1, kExprF64Abs); 2398 EXPECT_ARITY(1, kExprF64Neg); 2399 EXPECT_ARITY(2, kExprF64CopySign); 2400 EXPECT_ARITY(1, kExprF64Ceil); 2401 EXPECT_ARITY(1, kExprF64Floor); 2402 EXPECT_ARITY(1, kExprF64Trunc); 2403 EXPECT_ARITY(1, kExprF64NearestInt); 2404 EXPECT_ARITY(1, kExprF64Sqrt); 2405 EXPECT_ARITY(2, kExprF64Eq); 2406 EXPECT_ARITY(2, kExprF64Ne); 2407 EXPECT_ARITY(2, kExprF64Lt); 2408 EXPECT_ARITY(2, kExprF64Le); 2409 EXPECT_ARITY(2, kExprF64Gt); 2410 EXPECT_ARITY(2, kExprF64Ge); 2411 EXPECT_ARITY(1, kExprI32SConvertF32); 2412 EXPECT_ARITY(1, kExprI32SConvertF64); 2413 EXPECT_ARITY(1, kExprI32UConvertF32); 2414 EXPECT_ARITY(1, kExprI32UConvertF64); 2415 EXPECT_ARITY(1, kExprI32ConvertI64); 2416 EXPECT_ARITY(1, kExprI64SConvertF32); 2417 EXPECT_ARITY(1, kExprI64SConvertF64); 2418 EXPECT_ARITY(1, kExprI64UConvertF32); 2419 EXPECT_ARITY(1, kExprI64UConvertF64); 2420 EXPECT_ARITY(1, kExprI64SConvertI32); 2421 EXPECT_ARITY(1, kExprI64UConvertI32); 2422 EXPECT_ARITY(1, kExprF32SConvertI32); 2423 EXPECT_ARITY(1, kExprF32UConvertI32); 2424 EXPECT_ARITY(1, kExprF32SConvertI64); 2425 EXPECT_ARITY(1, kExprF32UConvertI64); 2426 EXPECT_ARITY(1, kExprF32ConvertF64); 2427 EXPECT_ARITY(1, kExprF32ReinterpretI32); 2428 EXPECT_ARITY(1, kExprF64SConvertI32); 2429 EXPECT_ARITY(1, kExprF64UConvertI32); 2430 EXPECT_ARITY(1, kExprF64SConvertI64); 2431 EXPECT_ARITY(1, kExprF64UConvertI64); 2432 EXPECT_ARITY(1, kExprF64ConvertF32); 2433 EXPECT_ARITY(1, kExprF64ReinterpretI64); 2434 EXPECT_ARITY(1, kExprI32ReinterpretF32); 2435 EXPECT_ARITY(1, kExprI64ReinterpretF64); 2436 } 2437 } // namespace wasm 2438 } // namespace internal 2439 } // namespace v8 2440