OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/value-serializer.h" | 5 #include "src/value-serializer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "include/v8.h" | 10 #include "include/v8.h" |
(...skipping 2354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2365 })); | 2365 })); |
2366 RoundTripTest( | 2366 RoundTripTest( |
2367 "({ a: new ExampleHostObject(), get b() { return this.a; }})", | 2367 "({ a: new ExampleHostObject(), get b() { return this.a; }})", |
2368 [this](Local<Value> value) { | 2368 [this](Local<Value> value) { |
2369 EXPECT_TRUE(EvaluateScriptForResultBool( | 2369 EXPECT_TRUE(EvaluateScriptForResultBool( |
2370 "result.a instanceof ExampleHostObject")); | 2370 "result.a instanceof ExampleHostObject")); |
2371 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 2371 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
2372 }); | 2372 }); |
2373 } | 2373 } |
2374 | 2374 |
| 2375 // It's expected that WebAssembly has more exhaustive tests elsewhere; this |
| 2376 // mostly checks that the logic to embed it in structured clone serialization |
| 2377 // works correctly. |
| 2378 |
| 2379 class ValueSerializerTestWithWasm : public ValueSerializerTest { |
| 2380 protected: |
| 2381 static void SetUpTestCase() { |
| 2382 g_saved_flag = i::FLAG_expose_wasm; |
| 2383 i::FLAG_expose_wasm = true; |
| 2384 ValueSerializerTest::SetUpTestCase(); |
| 2385 } |
| 2386 |
| 2387 static void TearDownTestCase() { |
| 2388 ValueSerializerTest::TearDownTestCase(); |
| 2389 i::FLAG_expose_wasm = g_saved_flag; |
| 2390 g_saved_flag = false; |
| 2391 } |
| 2392 |
| 2393 private: |
| 2394 static bool g_saved_flag; |
| 2395 }; |
| 2396 |
| 2397 bool ValueSerializerTestWithWasm::g_saved_flag = false; |
| 2398 |
| 2399 // A simple module which exports an "increment" function. |
| 2400 // Copied from test/mjsunit/wasm/incrementer.wasm. |
| 2401 const unsigned char kIncrementerWasm[] = { |
| 2402 0x00, 0x61, 0x73, 0x6d, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, |
| 2403 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, |
| 2404 0x69, 0x6e, 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, |
| 2405 0x08, 0x01, 0x06, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a}; |
| 2406 |
| 2407 TEST_F(ValueSerializerTestWithWasm, RoundTripWasmModule) { |
| 2408 RoundTripTest( |
| 2409 [this]() { |
| 2410 return WasmCompiledModule::DeserializeOrCompile( |
| 2411 isolate(), {nullptr, 0}, |
| 2412 {kIncrementerWasm, sizeof(kIncrementerWasm)}) |
| 2413 .ToLocalChecked(); |
| 2414 }, |
| 2415 [this](Local<Value> value) { |
| 2416 ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); |
| 2417 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2418 "new WebAssembly.Instance(result).exports.increment(8) === 9")); |
| 2419 }); |
| 2420 } |
| 2421 |
| 2422 // As produced around Chrome 56. |
| 2423 const unsigned char kSerializedIncrementerWasm[] = { |
| 2424 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d, 0x0d, |
| 2425 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03, |
| 2426 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e, 0x63, 0x72, 0x65, |
| 2427 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08, 0x01, 0x06, 0x00, 0x20, |
| 2428 0x00, 0x41, 0x01, 0x6a, 0xf8, 0x04, 0xa1, 0x06, 0xde, 0xc0, 0xc6, 0x44, |
| 2429 0x3c, 0x29, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x02, 0x00, 0x00, 0x81, 0x4e, |
| 2430 0xce, 0x7c, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x02, |
| 2431 0x00, 0x00, 0xb0, 0x25, 0x30, 0xe3, 0xf2, 0xdb, 0x2e, 0x48, 0x00, 0x00, |
| 2432 0x00, 0x80, 0xe8, 0x00, 0x00, 0x80, 0xe0, 0x01, 0x00, 0x80, 0x00, 0x00, |
| 2433 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x07, 0x08, 0x00, 0x00, 0x09, 0x04, |
| 2434 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x3c, 0x8c, 0xc0, 0x00, 0x00, |
| 2435 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x10, 0x8c, 0xc0, 0x00, 0x00, |
| 2436 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x70, 0x94, 0x01, 0x0c, 0x8b, |
| 2437 0xc1, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x25, 0xdc, 0x00, |
| 2438 0x00, 0x00, 0x00, 0x00, 0x00, 0x9e, 0x01, 0x10, 0x8c, 0xc0, 0x00, 0x00, |
| 2439 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x84, 0xc0, 0x00, 0x00, 0x00, 0x00, |
| 2440 0x00, 0x00, 0x00, 0x00, 0x05, 0x7d, 0x01, 0x1a, 0xe1, 0x02, 0x00, 0x00, |
| 2441 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x23, 0x88, 0x42, 0x32, 0x03, |
| 2442 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x00, |
| 2443 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x04, 0x00, |
| 2444 0x00, 0x02, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, |
| 2445 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x49, 0x3b, 0xa5, 0x60, 0x0c, 0x00, |
| 2446 0x00, 0x0f, 0x86, 0x04, 0x00, 0x00, 0x00, 0x83, 0xc0, 0x01, 0xc3, 0x55, |
| 2447 0x48, 0x89, 0xe5, 0x49, 0xba, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, |
| 2448 0x00, 0x41, 0x52, 0x48, 0x83, 0xec, 0x08, 0x48, 0x89, 0x45, 0xf0, 0x48, |
| 2449 0xbb, 0xb0, 0x67, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0xc0, 0x48, |
| 2450 0xbe, 0xe1, 0x57, 0x81, 0x85, 0xf6, 0x14, 0x00, 0x00, 0xe8, 0xfc, 0x3c, |
| 2451 0xea, 0xff, 0x48, 0x8b, 0x45, 0xf0, 0x48, 0x8b, 0xe5, 0x5d, 0xeb, 0xbf, |
| 2452 0x66, 0x90, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x44, 0x00, |
| 2453 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
| 2454 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2455 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2456 0x00, 0x00, 0x0f, 0x20, 0x84, 0x0f, 0x7d, 0x01, 0x0d, 0x00, 0x0f, 0x04, |
| 2457 0x6d, 0x08, 0x0f, 0xf0, 0x02, 0x80, 0x94, 0x01, 0x0c, 0x8b, 0xc1, 0x00, |
| 2458 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xed, 0xa9, 0x2d, 0x00, 0x00, |
| 2459 0x00, 0x00, 0x00, 0x9e, 0xe0, 0x38, 0x1a, 0x61, 0x03, 0x00, 0x00, 0x00, |
| 2460 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x23, 0x88, 0x42, 0x32, 0x03, 0x00, |
| 2461 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, |
| 2462 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, |
| 2463 0x02, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, |
| 2464 0xff, 0x00, 0x00, 0x00, 0x00, 0x55, 0x48, 0x89, 0xe5, 0x56, 0x57, 0x48, |
| 2465 0x8b, 0x45, 0x10, 0xe8, 0x11, 0xed, 0xed, 0xff, 0xa8, 0x01, 0x0f, 0x85, |
| 2466 0x2d, 0x00, 0x00, 0x00, 0x48, 0xc1, 0xe8, 0x20, 0xc5, 0xf9, 0x57, 0xc0, |
| 2467 0xc5, 0xfb, 0x2a, 0xc0, 0xc4, 0xe1, 0xfb, 0x2c, 0xc0, 0x48, 0x83, 0xf8, |
| 2468 0x01, 0x0f, 0x80, 0x34, 0x00, 0x00, 0x00, 0x8b, 0xc0, 0xe8, 0x27, 0xfe, |
| 2469 0xff, 0xff, 0x48, 0xc1, 0xe0, 0x20, 0x48, 0x8b, 0xe5, 0x5d, 0xc2, 0x10, |
| 2470 0x00, 0x49, 0x39, 0x45, 0xa0, 0x0f, 0x84, 0x07, 0x00, 0x00, 0x00, 0xc5, |
| 2471 0xfb, 0x10, 0x40, 0x07, 0xeb, 0xce, 0x49, 0xba, 0x00, 0x00, 0x00, 0x00, |
| 2472 0x00, 0x00, 0xf8, 0x7f, 0xc4, 0xc1, 0xf9, 0x6e, 0xc2, 0xeb, 0xbd, 0x48, |
| 2473 0x83, 0xec, 0x08, 0xc5, 0xfb, 0x11, 0x04, 0x24, 0xe8, 0xcc, 0xfe, 0xff, |
| 2474 0xff, 0x48, 0x83, 0xc4, 0x08, 0xeb, 0xb8, 0x66, 0x90, 0x02, 0x00, 0x00, |
| 2475 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, |
| 2476 0x0f, 0x39, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x00, |
| 2477 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x20, 0x84, |
| 2478 0x0f, 0xcc, 0x6e, 0x7d, 0x01, 0x72, 0x98, 0x00, 0x0f, 0xdc, 0x6d, 0x0c, |
| 2479 0x0f, 0xb0, 0x84, 0x0d, 0x04, 0x84, 0xe3, 0xc0, 0x00, 0x00, 0x00, 0x00, |
| 2480 0x00, 0x00, 0x00, 0x00, 0x84, 0xe0, 0x84, 0x84, 0x18, 0x2f, 0x2f, 0x2f, |
| 2481 0x2f, 0x2f}; |
| 2482 |
| 2483 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModule) { |
| 2484 std::vector<uint8_t> raw( |
| 2485 kSerializedIncrementerWasm, |
| 2486 kSerializedIncrementerWasm + sizeof(kSerializedIncrementerWasm)); |
| 2487 DecodeTest(raw, [this](Local<Value> value) { |
| 2488 ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); |
| 2489 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2490 "new WebAssembly.Instance(result).exports.increment(8) === 9")); |
| 2491 }); |
| 2492 } |
| 2493 |
| 2494 // As above, but with empty compiled data. Should work due to fallback to wire |
| 2495 // data. |
| 2496 const unsigned char kSerializedIncrementerWasmWithInvalidCompiledData[] = { |
| 2497 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d, |
| 2498 0x0d, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, |
| 2499 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e, |
| 2500 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08, |
| 2501 0x01, 0x06, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a, 0x00}; |
| 2502 |
| 2503 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidCompiledData) { |
| 2504 std::vector<uint8_t> raw( |
| 2505 kSerializedIncrementerWasmWithInvalidCompiledData, |
| 2506 kSerializedIncrementerWasmWithInvalidCompiledData + |
| 2507 sizeof(kSerializedIncrementerWasmWithInvalidCompiledData)); |
| 2508 DecodeTest(raw, [this](Local<Value> value) { |
| 2509 ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); |
| 2510 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2511 "new WebAssembly.Instance(result).exports.increment(8) === 9")); |
| 2512 }); |
| 2513 } |
| 2514 |
| 2515 // As above, but also with empty wire data. Should fail. |
| 2516 const unsigned char kSerializedIncrementerWasmInvalid[] = { |
| 2517 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x00}; |
| 2518 |
| 2519 TEST_F(ValueSerializerTestWithWasm, |
| 2520 DecodeWasmModuleWithInvalidCompiledAndWireData) { |
| 2521 std::vector<uint8_t> raw(kSerializedIncrementerWasmInvalid, |
| 2522 kSerializedIncrementerWasmInvalid + |
| 2523 sizeof(kSerializedIncrementerWasmInvalid)); |
| 2524 InvalidDecodeTest(raw); |
| 2525 } |
| 2526 |
| 2527 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { |
| 2528 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); |
| 2529 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); |
| 2530 } |
| 2531 |
2375 } // namespace | 2532 } // namespace |
2376 } // namespace v8 | 2533 } // namespace v8 |
OLD | NEW |