Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: test/unittests/value-serializer-unittest.cc

Issue 2471923002: Support structured clone of compiled WebAssembly modules. (Closed)
Patch Set: correct one invalid decode test Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/value-serializer.cc ('K') | « src/value-serializer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "src/api.h" 11 #include "src/api.h"
12 #include "src/base/build_config.h" 12 #include "src/base/build_config.h"
13 #include "src/wasm/wasm-js.h"
13 #include "test/unittests/test-utils.h" 14 #include "test/unittests/test-utils.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace v8 { 18 namespace v8 {
18 namespace { 19 namespace {
19 20
20 using ::testing::_; 21 using ::testing::_;
21 using ::testing::Invoke; 22 using ::testing::Invoke;
22 23
(...skipping 2342 matching lines...) Expand 10 before | Expand all | Expand 10 after
2365 })); 2366 }));
2366 RoundTripTest( 2367 RoundTripTest(
2367 "({ a: new ExampleHostObject(), get b() { return this.a; }})", 2368 "({ a: new ExampleHostObject(), get b() { return this.a; }})",
2368 [this](Local<Value> value) { 2369 [this](Local<Value> value) {
2369 EXPECT_TRUE(EvaluateScriptForResultBool( 2370 EXPECT_TRUE(EvaluateScriptForResultBool(
2370 "result.a instanceof ExampleHostObject")); 2371 "result.a instanceof ExampleHostObject"));
2371 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); 2372 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b"));
2372 }); 2373 });
2373 } 2374 }
2374 2375
2376 // It's expected that WebAssembly has more exhaustive tests elsewhere; this
2377 // mostly checks that the logic to embed it in structured clone serialization
2378 // works correctly.
2379
2380 void InstallWasmJs(Local<Context> api_context) {
2381 Context::Scope scope(api_context);
2382 i::Isolate* isolate =
2383 reinterpret_cast<i::Isolate*>(api_context->GetIsolate());
2384 i::HandleScope handle_scope(isolate);
2385 i::Handle<i::Context> context = Utils::OpenHandle(*api_context);
2386 i::WasmJs::InstallWasmMapsIfNeeded(isolate, context);
2387 i::WasmJs::InstallWasmModuleSymbolIfNeeded(
2388 isolate, i::handle(context->global_object()), context);
2389 }
2390
2391 class ValueSerializerTestWithWasm : public ValueSerializerTest {
2392 protected:
2393 ValueSerializerTestWithWasm() : saved_flag_(i::FLAG_serialize_wasm_modules) {
2394 i::FLAG_serialize_wasm_modules = true;
2395
2396 // This forces the WebAssembly stuff to be installed, even if the unit tests
2397 // aren't run with --expose-wasm.
2398 InstallWasmJs(serialization_context());
2399 InstallWasmJs(deserialization_context());
2400 }
2401
2402 ~ValueSerializerTestWithWasm() {
2403 i::FLAG_serialize_wasm_modules = saved_flag_;
2404 }
2405
2406 private:
2407 bool saved_flag_;
2408 };
2409
2410 // A simple module which exports an "increment" function.
2411 // Copied from test/mjsunit/wasm/incrementer.wasm.
2412 const unsigned char kIncrementerWasm[] = {
2413 0x00, 0x61, 0x73, 0x6d, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60,
2414 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09,
2415 0x69, 0x6e, 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a,
2416 0x08, 0x01, 0x06, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a};
2417
2418 TEST_F(ValueSerializerTestWithWasm, RoundTripWasmModule) {
2419 RoundTripTest(
2420 [this]() {
2421 return WasmCompiledModule::DeserializeOrCompile(
2422 isolate(), {nullptr, 0},
2423 {kIncrementerWasm, sizeof(kIncrementerWasm)})
2424 .ToLocalChecked();
2425 },
2426 [this](Local<Value> value) {
2427 ASSERT_TRUE(value->IsWebAssemblyCompiledModule());
2428 EXPECT_TRUE(EvaluateScriptForResultBool(
2429 "new WebAssembly.Instance(result).exports.increment(8) === 9"));
2430 });
2431 }
2432
2433 // As produced around Chrome 56.
2434 const unsigned char kSerializedIncrementerWasm[] = {
2435 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d, 0x0d,
2436 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03,
2437 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e, 0x63, 0x72, 0x65,
2438 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08, 0x01, 0x06, 0x00, 0x20,
2439 0x00, 0x41, 0x01, 0x6a, 0xf8, 0x04, 0xa1, 0x06, 0xde, 0xc0, 0xc6, 0x44,
2440 0x3c, 0x29, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x02, 0x00, 0x00, 0x81, 0x4e,
2441 0xce, 0x7c, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x02,
2442 0x00, 0x00, 0xb0, 0x25, 0x30, 0xe3, 0xf2, 0xdb, 0x2e, 0x48, 0x00, 0x00,
2443 0x00, 0x80, 0xe8, 0x00, 0x00, 0x80, 0xe0, 0x01, 0x00, 0x80, 0x00, 0x00,
2444 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x07, 0x08, 0x00, 0x00, 0x09, 0x04,
2445 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x3c, 0x8c, 0xc0, 0x00, 0x00,
2446 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x10, 0x8c, 0xc0, 0x00, 0x00,
2447 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x70, 0x94, 0x01, 0x0c, 0x8b,
2448 0xc1, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x25, 0xdc, 0x00,
2449 0x00, 0x00, 0x00, 0x00, 0x00, 0x9e, 0x01, 0x10, 0x8c, 0xc0, 0x00, 0x00,
2450 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x84, 0xc0, 0x00, 0x00, 0x00, 0x00,
2451 0x00, 0x00, 0x00, 0x00, 0x05, 0x7d, 0x01, 0x1a, 0xe1, 0x02, 0x00, 0x00,
2452 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x23, 0x88, 0x42, 0x32, 0x03,
2453 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x00,
2454 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x04, 0x00,
2455 0x00, 0x02, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
2456 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x49, 0x3b, 0xa5, 0x60, 0x0c, 0x00,
2457 0x00, 0x0f, 0x86, 0x04, 0x00, 0x00, 0x00, 0x83, 0xc0, 0x01, 0xc3, 0x55,
2458 0x48, 0x89, 0xe5, 0x49, 0xba, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
2459 0x00, 0x41, 0x52, 0x48, 0x83, 0xec, 0x08, 0x48, 0x89, 0x45, 0xf0, 0x48,
2460 0xbb, 0xb0, 0x67, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0xc0, 0x48,
2461 0xbe, 0xe1, 0x57, 0x81, 0x85, 0xf6, 0x14, 0x00, 0x00, 0xe8, 0xfc, 0x3c,
2462 0xea, 0xff, 0x48, 0x8b, 0x45, 0xf0, 0x48, 0x8b, 0xe5, 0x5d, 0xeb, 0xbf,
2463 0x66, 0x90, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x44, 0x00,
2464 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
2465 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2466 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2467 0x00, 0x00, 0x0f, 0x20, 0x84, 0x0f, 0x7d, 0x01, 0x0d, 0x00, 0x0f, 0x04,
2468 0x6d, 0x08, 0x0f, 0xf0, 0x02, 0x80, 0x94, 0x01, 0x0c, 0x8b, 0xc1, 0x00,
2469 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xed, 0xa9, 0x2d, 0x00, 0x00,
2470 0x00, 0x00, 0x00, 0x9e, 0xe0, 0x38, 0x1a, 0x61, 0x03, 0x00, 0x00, 0x00,
2471 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x23, 0x88, 0x42, 0x32, 0x03, 0x00,
2472 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00,
2473 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
2474 0x02, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
2475 0xff, 0x00, 0x00, 0x00, 0x00, 0x55, 0x48, 0x89, 0xe5, 0x56, 0x57, 0x48,
2476 0x8b, 0x45, 0x10, 0xe8, 0x11, 0xed, 0xed, 0xff, 0xa8, 0x01, 0x0f, 0x85,
2477 0x2d, 0x00, 0x00, 0x00, 0x48, 0xc1, 0xe8, 0x20, 0xc5, 0xf9, 0x57, 0xc0,
2478 0xc5, 0xfb, 0x2a, 0xc0, 0xc4, 0xe1, 0xfb, 0x2c, 0xc0, 0x48, 0x83, 0xf8,
2479 0x01, 0x0f, 0x80, 0x34, 0x00, 0x00, 0x00, 0x8b, 0xc0, 0xe8, 0x27, 0xfe,
2480 0xff, 0xff, 0x48, 0xc1, 0xe0, 0x20, 0x48, 0x8b, 0xe5, 0x5d, 0xc2, 0x10,
2481 0x00, 0x49, 0x39, 0x45, 0xa0, 0x0f, 0x84, 0x07, 0x00, 0x00, 0x00, 0xc5,
2482 0xfb, 0x10, 0x40, 0x07, 0xeb, 0xce, 0x49, 0xba, 0x00, 0x00, 0x00, 0x00,
2483 0x00, 0x00, 0xf8, 0x7f, 0xc4, 0xc1, 0xf9, 0x6e, 0xc2, 0xeb, 0xbd, 0x48,
2484 0x83, 0xec, 0x08, 0xc5, 0xfb, 0x11, 0x04, 0x24, 0xe8, 0xcc, 0xfe, 0xff,
2485 0xff, 0x48, 0x83, 0xc4, 0x08, 0xeb, 0xb8, 0x66, 0x90, 0x02, 0x00, 0x00,
2486 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
2487 0x0f, 0x39, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x00,
2488 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x20, 0x84,
2489 0x0f, 0xcc, 0x6e, 0x7d, 0x01, 0x72, 0x98, 0x00, 0x0f, 0xdc, 0x6d, 0x0c,
2490 0x0f, 0xb0, 0x84, 0x0d, 0x04, 0x84, 0xe3, 0xc0, 0x00, 0x00, 0x00, 0x00,
2491 0x00, 0x00, 0x00, 0x00, 0x84, 0xe0, 0x84, 0x84, 0x18, 0x2f, 0x2f, 0x2f,
2492 0x2f, 0x2f};
2493
2494 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModule) {
2495 std::vector<uint8_t> raw(
2496 kSerializedIncrementerWasm,
2497 kSerializedIncrementerWasm + sizeof(kSerializedIncrementerWasm));
2498 DecodeTest(raw, [this](Local<Value> value) {
2499 ASSERT_TRUE(value->IsWebAssemblyCompiledModule());
2500 EXPECT_TRUE(EvaluateScriptForResultBool(
2501 "new WebAssembly.Instance(result).exports.increment(8) === 9"));
2502 });
2503 }
2504
2505 // As above, but with empty compiled data. Should work due to fallback to wire
2506 // data.
2507 const unsigned char kSerializedIncrementerWasmWithInvalidCompiledData[] = {
2508 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d,
2509 0x0d, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01,
2510 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e,
2511 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08,
2512 0x01, 0x06, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a, 0x00};
2513
2514 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidCompiledData) {
2515 std::vector<uint8_t> raw(
2516 kSerializedIncrementerWasmWithInvalidCompiledData,
2517 kSerializedIncrementerWasmWithInvalidCompiledData +
2518 sizeof(kSerializedIncrementerWasmWithInvalidCompiledData));
2519 DecodeTest(raw, [this](Local<Value> value) {
2520 ASSERT_TRUE(value->IsWebAssemblyCompiledModule());
2521 EXPECT_TRUE(EvaluateScriptForResultBool(
2522 "new WebAssembly.Instance(result).exports.increment(8) === 9"));
2523 });
2524 }
2525
2526 // As above, but also with empty wire data. Should fail.
2527 const unsigned char kSerializedIncrementerWasmInvalid[] = {
2528 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x00};
2529
2530 TEST_F(ValueSerializerTestWithWasm,
2531 DecodeWasmModuleWithInvalidCompiledAndWireData) {
2532 std::vector<uint8_t> raw(kSerializedIncrementerWasmInvalid,
2533 kSerializedIncrementerWasmInvalid +
2534 sizeof(kSerializedIncrementerWasmInvalid));
2535 InvalidDecodeTest(raw);
2536 }
2537
2538 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) {
2539 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00});
2540 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f});
2541 }
2542
2375 } // namespace 2543 } // namespace
2376 } // namespace v8 2544 } // namespace v8
OLDNEW
« src/value-serializer.cc ('K') | « src/value-serializer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698