| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
| 6 | 6 |
| 7 #include "src/v8.h" | 7 #include "src/v8.h" |
| 8 | 8 |
| 9 #include "test/common/wasm/test-signatures.h" | 9 #include "test/common/wasm/test-signatures.h" |
| 10 | 10 |
| 11 #include "src/objects.h" | 11 #include "src/objects.h" |
| 12 | 12 |
| 13 #include "src/wasm/ast-decoder.h" | 13 #include "src/wasm/ast-decoder.h" |
| 14 #include "src/wasm/signature-map.h" |
| 14 #include "src/wasm/wasm-macro-gen.h" | 15 #include "src/wasm/wasm-macro-gen.h" |
| 15 #include "src/wasm/wasm-module.h" | 16 #include "src/wasm/wasm-module.h" |
| 16 #include "src/wasm/wasm-opcodes.h" | 17 #include "src/wasm/wasm-opcodes.h" |
| 17 | 18 |
| 18 namespace v8 { | 19 namespace v8 { |
| 19 namespace internal { | 20 namespace internal { |
| 20 namespace wasm { | 21 namespace wasm { |
| 21 | 22 |
| 22 #define B1(a) WASM_BLOCK(a) | 23 #define B1(a) WASM_BLOCK(a) |
| 23 #define B2(a, b) WASM_BLOCK(a, b) | 24 #define B2(a, b) WASM_BLOCK(a, b) |
| (...skipping 1280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1304 false}); // export | 1305 false}); // export |
| 1305 CHECK(mod.functions.size() <= 127); | 1306 CHECK(mod.functions.size() <= 127); |
| 1306 return static_cast<byte>(mod.functions.size() - 1); | 1307 return static_cast<byte>(mod.functions.size() - 1); |
| 1307 } | 1308 } |
| 1308 byte AddImport(FunctionSig* sig) { | 1309 byte AddImport(FunctionSig* sig) { |
| 1309 byte result = AddFunction(sig); | 1310 byte result = AddFunction(sig); |
| 1310 mod.functions[result].imported = true; | 1311 mod.functions[result].imported = true; |
| 1311 return result; | 1312 return result; |
| 1312 } | 1313 } |
| 1313 | 1314 |
| 1315 void InitializeFunctionTable() { |
| 1316 mod.function_tables.push_back( |
| 1317 {0, 0, true, std::vector<int32_t>(), false, false, SignatureMap()}); |
| 1318 } |
| 1319 |
| 1314 private: | 1320 private: |
| 1315 WasmModule mod; | 1321 WasmModule mod; |
| 1316 }; | 1322 }; |
| 1317 } // namespace | 1323 } // namespace |
| 1318 | 1324 |
| 1319 TEST_F(AstDecoderTest, SimpleCalls) { | 1325 TEST_F(AstDecoderTest, SimpleCalls) { |
| 1320 FunctionSig* sig = sigs.i_i(); | 1326 FunctionSig* sig = sigs.i_i(); |
| 1321 TestModuleEnv module_env; | 1327 TestModuleEnv module_env; |
| 1322 module = &module_env; | 1328 module = &module_env; |
| 1323 | 1329 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1414 } | 1420 } |
| 1415 } | 1421 } |
| 1416 } | 1422 } |
| 1417 } | 1423 } |
| 1418 } | 1424 } |
| 1419 } | 1425 } |
| 1420 | 1426 |
| 1421 TEST_F(AstDecoderTest, SimpleIndirectCalls) { | 1427 TEST_F(AstDecoderTest, SimpleIndirectCalls) { |
| 1422 FunctionSig* sig = sigs.i_i(); | 1428 FunctionSig* sig = sigs.i_i(); |
| 1423 TestModuleEnv module_env; | 1429 TestModuleEnv module_env; |
| 1430 module_env.InitializeFunctionTable(); |
| 1424 module = &module_env; | 1431 module = &module_env; |
| 1425 | 1432 |
| 1426 byte f0 = module_env.AddSignature(sigs.i_v()); | 1433 byte f0 = module_env.AddSignature(sigs.i_v()); |
| 1427 byte f1 = module_env.AddSignature(sigs.i_i()); | 1434 byte f1 = module_env.AddSignature(sigs.i_i()); |
| 1428 byte f2 = module_env.AddSignature(sigs.i_ii()); | 1435 byte f2 = module_env.AddSignature(sigs.i_ii()); |
| 1429 | 1436 |
| 1430 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); | 1437 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); |
| 1431 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_I8(22))); | 1438 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_I8(22))); |
| 1432 EXPECT_VERIFIES_S( | 1439 EXPECT_VERIFIES_S( |
| 1433 sig, WASM_CALL_INDIRECT2(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); | 1440 sig, WASM_CALL_INDIRECT2(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); |
| 1434 } | 1441 } |
| 1435 | 1442 |
| 1436 TEST_F(AstDecoderTest, IndirectCallsOutOfBounds) { | 1443 TEST_F(AstDecoderTest, IndirectCallsOutOfBounds) { |
| 1437 FunctionSig* sig = sigs.i_i(); | 1444 FunctionSig* sig = sigs.i_i(); |
| 1438 TestModuleEnv module_env; | 1445 TestModuleEnv module_env; |
| 1446 module_env.InitializeFunctionTable(); |
| 1439 module = &module_env; | 1447 module = &module_env; |
| 1440 | 1448 |
| 1441 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); | 1449 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); |
| 1442 module_env.AddSignature(sigs.i_v()); | 1450 module_env.AddSignature(sigs.i_v()); |
| 1443 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); | 1451 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); |
| 1444 | 1452 |
| 1445 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(1, WASM_ZERO, WASM_I8(22))); | 1453 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(1, WASM_ZERO, WASM_I8(22))); |
| 1446 module_env.AddSignature(sigs.i_i()); | 1454 module_env.AddSignature(sigs.i_i()); |
| 1447 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT1(1, WASM_ZERO, WASM_I8(27))); | 1455 EXPECT_VERIFIES_S(sig, WASM_CALL_INDIRECT1(1, WASM_ZERO, WASM_I8(27))); |
| 1448 | 1456 |
| 1449 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(2, WASM_ZERO, WASM_I8(27))); | 1457 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(2, WASM_ZERO, WASM_I8(27))); |
| 1450 } | 1458 } |
| 1451 | 1459 |
| 1452 TEST_F(AstDecoderTest, IndirectCallsWithMismatchedSigs3) { | 1460 TEST_F(AstDecoderTest, IndirectCallsWithMismatchedSigs3) { |
| 1453 FunctionSig* sig = sigs.i_i(); | 1461 FunctionSig* sig = sigs.i_i(); |
| 1454 TestModuleEnv module_env; | 1462 TestModuleEnv module_env; |
| 1463 module_env.InitializeFunctionTable(); |
| 1455 module = &module_env; | 1464 module = &module_env; |
| 1456 | 1465 |
| 1457 byte f0 = module_env.AddFunction(sigs.i_f()); | 1466 byte f0 = module_env.AddFunction(sigs.i_f()); |
| 1458 | 1467 |
| 1459 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f0, WASM_ZERO, WASM_I8(17))); | 1468 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f0, WASM_ZERO, WASM_I8(17))); |
| 1460 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f0, WASM_ZERO, WASM_I64V_1(27))); | 1469 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f0, WASM_ZERO, WASM_I64V_1(27))); |
| 1461 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f0, WASM_ZERO, WASM_F64(37.2))); | 1470 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f0, WASM_ZERO, WASM_F64(37.2))); |
| 1462 | 1471 |
| 1463 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(f0, WASM_I8(17))); | 1472 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(f0, WASM_I8(17))); |
| 1464 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(f0, WASM_I64V_1(27))); | 1473 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(f0, WASM_I64V_1(27))); |
| 1465 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(f0, WASM_F64(37.2))); | 1474 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(f0, WASM_F64(37.2))); |
| 1466 | 1475 |
| 1467 byte f1 = module_env.AddFunction(sigs.i_d()); | 1476 byte f1 = module_env.AddFunction(sigs.i_d()); |
| 1468 | 1477 |
| 1469 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_I8(16))); | 1478 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_I8(16))); |
| 1470 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_I64V_1(16))); | 1479 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_I64V_1(16))); |
| 1471 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_F32(17.6))); | 1480 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_F32(17.6))); |
| 1472 } | 1481 } |
| 1473 | 1482 |
| 1483 TEST_F(AstDecoderTest, IndirectCallsWithoutTableCrash) { |
| 1484 FunctionSig* sig = sigs.i_i(); |
| 1485 TestModuleEnv module_env; |
| 1486 module = &module_env; |
| 1487 |
| 1488 byte f0 = module_env.AddSignature(sigs.i_v()); |
| 1489 byte f1 = module_env.AddSignature(sigs.i_i()); |
| 1490 byte f2 = module_env.AddSignature(sigs.i_ii()); |
| 1491 |
| 1492 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); |
| 1493 EXPECT_FAILURE_S(sig, WASM_CALL_INDIRECT1(f1, WASM_ZERO, WASM_I8(22))); |
| 1494 EXPECT_FAILURE_S( |
| 1495 sig, WASM_CALL_INDIRECT2(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); |
| 1496 } |
| 1497 |
| 1474 TEST_F(AstDecoderTest, SimpleImportCalls) { | 1498 TEST_F(AstDecoderTest, SimpleImportCalls) { |
| 1475 FunctionSig* sig = sigs.i_i(); | 1499 FunctionSig* sig = sigs.i_i(); |
| 1476 TestModuleEnv module_env; | 1500 TestModuleEnv module_env; |
| 1477 module = &module_env; | 1501 module = &module_env; |
| 1478 | 1502 |
| 1479 byte f0 = module_env.AddImport(sigs.i_v()); | 1503 byte f0 = module_env.AddImport(sigs.i_v()); |
| 1480 byte f1 = module_env.AddImport(sigs.i_i()); | 1504 byte f1 = module_env.AddImport(sigs.i_i()); |
| 1481 byte f2 = module_env.AddImport(sigs.i_ii()); | 1505 byte f2 = module_env.AddImport(sigs.i_ii()); |
| 1482 | 1506 |
| 1483 EXPECT_VERIFIES_S(sig, WASM_CALL_FUNCTION0(f0)); | 1507 EXPECT_VERIFIES_S(sig, WASM_CALL_FUNCTION0(f0)); |
| (...skipping 1198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2682 iter.next(); | 2706 iter.next(); |
| 2683 EXPECT_TRUE(iter.has_next()); | 2707 EXPECT_TRUE(iter.has_next()); |
| 2684 EXPECT_EQ(kExprI8Const, iter.current()); | 2708 EXPECT_EQ(kExprI8Const, iter.current()); |
| 2685 iter.next(); | 2709 iter.next(); |
| 2686 EXPECT_FALSE(iter.has_next()); | 2710 EXPECT_FALSE(iter.has_next()); |
| 2687 } | 2711 } |
| 2688 | 2712 |
| 2689 } // namespace wasm | 2713 } // namespace wasm |
| 2690 } // namespace internal | 2714 } // namespace internal |
| 2691 } // namespace v8 | 2715 } // namespace v8 |
| OLD | NEW |