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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 67 } |
68 } | 68 } |
69 | 69 |
70 const Local<Context>& serialization_context() { | 70 const Local<Context>& serialization_context() { |
71 return serialization_context_; | 71 return serialization_context_; |
72 } | 72 } |
73 const Local<Context>& deserialization_context() { | 73 const Local<Context>& deserialization_context() { |
74 return deserialization_context_; | 74 return deserialization_context_; |
75 } | 75 } |
76 | 76 |
| 77 bool ExpectInlineWasm() const { return expect_inline_wasm_; } |
| 78 void SetExpectInlineWasm(bool value) { expect_inline_wasm_ = value; } |
| 79 |
77 // Overridden in more specific fixtures. | 80 // Overridden in more specific fixtures. |
78 virtual ValueSerializer::Delegate* GetSerializerDelegate() { return nullptr; } | 81 virtual ValueSerializer::Delegate* GetSerializerDelegate() { return nullptr; } |
79 virtual void BeforeEncode(ValueSerializer*) {} | 82 virtual void BeforeEncode(ValueSerializer*) {} |
80 virtual void AfterEncode() {} | 83 virtual void AfterEncode() {} |
81 virtual ValueDeserializer::Delegate* GetDeserializerDelegate() { | 84 virtual ValueDeserializer::Delegate* GetDeserializerDelegate() { |
82 return nullptr; | 85 return nullptr; |
83 } | 86 } |
84 virtual void BeforeDecode(ValueDeserializer*) {} | 87 virtual void BeforeDecode(ValueDeserializer*) {} |
85 | 88 |
86 template <typename InputFunctor, typename OutputFunctor> | 89 template <typename InputFunctor, typename OutputFunctor> |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 template <typename OutputFunctor> | 168 template <typename OutputFunctor> |
166 void DecodeTest(const std::vector<uint8_t>& data, | 169 void DecodeTest(const std::vector<uint8_t>& data, |
167 const OutputFunctor& output_functor) { | 170 const OutputFunctor& output_functor) { |
168 Local<Context> context = deserialization_context(); | 171 Local<Context> context = deserialization_context(); |
169 Context::Scope scope(context); | 172 Context::Scope scope(context); |
170 TryCatch try_catch(isolate()); | 173 TryCatch try_catch(isolate()); |
171 ValueDeserializer deserializer(isolate(), &data[0], | 174 ValueDeserializer deserializer(isolate(), &data[0], |
172 static_cast<int>(data.size()), | 175 static_cast<int>(data.size()), |
173 GetDeserializerDelegate()); | 176 GetDeserializerDelegate()); |
174 deserializer.SetSupportsLegacyWireFormat(true); | 177 deserializer.SetSupportsLegacyWireFormat(true); |
| 178 deserializer.SetExpectInlineWasm(ExpectInlineWasm()); |
175 BeforeDecode(&deserializer); | 179 BeforeDecode(&deserializer); |
176 ASSERT_TRUE(deserializer.ReadHeader(context).FromMaybe(false)); | 180 ASSERT_TRUE(deserializer.ReadHeader(context).FromMaybe(false)); |
177 Local<Value> result; | 181 Local<Value> result; |
178 ASSERT_TRUE(deserializer.ReadValue(context).ToLocal(&result)); | 182 ASSERT_TRUE(deserializer.ReadValue(context).ToLocal(&result)); |
179 ASSERT_FALSE(result.IsEmpty()); | 183 ASSERT_FALSE(result.IsEmpty()); |
180 ASSERT_FALSE(try_catch.HasCaught()); | 184 ASSERT_FALSE(try_catch.HasCaught()); |
181 ASSERT_TRUE( | 185 ASSERT_TRUE( |
182 context->Global() | 186 context->Global() |
183 ->CreateDataProperty(context, StringFromUtf8("result"), result) | 187 ->CreateDataProperty(context, StringFromUtf8("result"), result) |
184 .FromMaybe(false)); | 188 .FromMaybe(false)); |
185 output_functor(result); | 189 output_functor(result); |
186 ASSERT_FALSE(try_catch.HasCaught()); | 190 ASSERT_FALSE(try_catch.HasCaught()); |
187 } | 191 } |
188 | 192 |
189 template <typename OutputFunctor> | 193 template <typename OutputFunctor> |
190 void DecodeTestForVersion0(const std::vector<uint8_t>& data, | 194 void DecodeTestForVersion0(const std::vector<uint8_t>& data, |
191 const OutputFunctor& output_functor) { | 195 const OutputFunctor& output_functor) { |
192 Local<Context> context = deserialization_context(); | 196 Local<Context> context = deserialization_context(); |
193 Context::Scope scope(context); | 197 Context::Scope scope(context); |
194 TryCatch try_catch(isolate()); | 198 TryCatch try_catch(isolate()); |
195 ValueDeserializer deserializer(isolate(), &data[0], | 199 ValueDeserializer deserializer(isolate(), &data[0], |
196 static_cast<int>(data.size()), | 200 static_cast<int>(data.size()), |
197 GetDeserializerDelegate()); | 201 GetDeserializerDelegate()); |
198 deserializer.SetSupportsLegacyWireFormat(true); | 202 deserializer.SetSupportsLegacyWireFormat(true); |
| 203 deserializer.SetExpectInlineWasm(ExpectInlineWasm()); |
199 BeforeDecode(&deserializer); | 204 BeforeDecode(&deserializer); |
200 ASSERT_TRUE(deserializer.ReadHeader(context).FromMaybe(false)); | 205 ASSERT_TRUE(deserializer.ReadHeader(context).FromMaybe(false)); |
201 ASSERT_EQ(0u, deserializer.GetWireFormatVersion()); | 206 ASSERT_EQ(0u, deserializer.GetWireFormatVersion()); |
202 Local<Value> result; | 207 Local<Value> result; |
203 ASSERT_TRUE(deserializer.ReadValue(context).ToLocal(&result)); | 208 ASSERT_TRUE(deserializer.ReadValue(context).ToLocal(&result)); |
204 ASSERT_FALSE(result.IsEmpty()); | 209 ASSERT_FALSE(result.IsEmpty()); |
205 ASSERT_FALSE(try_catch.HasCaught()); | 210 ASSERT_FALSE(try_catch.HasCaught()); |
206 ASSERT_TRUE( | 211 ASSERT_TRUE( |
207 context->Global() | 212 context->Global() |
208 ->CreateDataProperty(context, StringFromUtf8("result"), result) | 213 ->CreateDataProperty(context, StringFromUtf8("result"), result) |
209 .FromMaybe(false)); | 214 .FromMaybe(false)); |
210 output_functor(result); | 215 output_functor(result); |
211 ASSERT_FALSE(try_catch.HasCaught()); | 216 ASSERT_FALSE(try_catch.HasCaught()); |
212 } | 217 } |
213 | 218 |
214 void InvalidDecodeTest(const std::vector<uint8_t>& data) { | 219 void InvalidDecodeTest(const std::vector<uint8_t>& data) { |
215 Local<Context> context = deserialization_context(); | 220 Local<Context> context = deserialization_context(); |
216 Context::Scope scope(context); | 221 Context::Scope scope(context); |
217 TryCatch try_catch(isolate()); | 222 TryCatch try_catch(isolate()); |
218 ValueDeserializer deserializer(isolate(), &data[0], | 223 ValueDeserializer deserializer(isolate(), &data[0], |
219 static_cast<int>(data.size()), | 224 static_cast<int>(data.size()), |
220 GetDeserializerDelegate()); | 225 GetDeserializerDelegate()); |
221 deserializer.SetSupportsLegacyWireFormat(true); | 226 deserializer.SetSupportsLegacyWireFormat(true); |
| 227 deserializer.SetExpectInlineWasm(ExpectInlineWasm()); |
222 BeforeDecode(&deserializer); | 228 BeforeDecode(&deserializer); |
223 Maybe<bool> header_result = deserializer.ReadHeader(context); | 229 Maybe<bool> header_result = deserializer.ReadHeader(context); |
224 if (header_result.IsNothing()) { | 230 if (header_result.IsNothing()) { |
225 EXPECT_TRUE(try_catch.HasCaught()); | 231 EXPECT_TRUE(try_catch.HasCaught()); |
226 return; | 232 return; |
227 } | 233 } |
228 ASSERT_TRUE(header_result.ToChecked()); | 234 ASSERT_TRUE(header_result.ToChecked()); |
229 ASSERT_TRUE(deserializer.ReadValue(context).IsEmpty()); | 235 ASSERT_TRUE(deserializer.ReadValue(context).IsEmpty()); |
230 EXPECT_TRUE(try_catch.HasCaught()); | 236 EXPECT_TRUE(try_catch.HasCaught()); |
231 } | 237 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 Local<ArrayBuffer> ab = | 274 Local<ArrayBuffer> ab = |
269 ArrayBuffer::New(isolate(), static_cast<void*>(data), sizeof(data)); | 275 ArrayBuffer::New(isolate(), static_cast<void*>(data), sizeof(data)); |
270 return Uint8Array::New(ab, 0, sizeof(data)); | 276 return Uint8Array::New(ab, 0, sizeof(data)); |
271 } | 277 } |
272 | 278 |
273 private: | 279 private: |
274 Local<Context> serialization_context_; | 280 Local<Context> serialization_context_; |
275 Local<Context> deserialization_context_; | 281 Local<Context> deserialization_context_; |
276 Local<FunctionTemplate> host_object_constructor_template_; | 282 Local<FunctionTemplate> host_object_constructor_template_; |
277 i::Isolate* isolate_; | 283 i::Isolate* isolate_; |
| 284 bool expect_inline_wasm_ = false; |
278 | 285 |
279 DISALLOW_COPY_AND_ASSIGN(ValueSerializerTest); | 286 DISALLOW_COPY_AND_ASSIGN(ValueSerializerTest); |
280 }; | 287 }; |
281 | 288 |
282 TEST_F(ValueSerializerTest, DecodeInvalid) { | 289 TEST_F(ValueSerializerTest, DecodeInvalid) { |
283 // Version tag but no content. | 290 // Version tag but no content. |
284 InvalidDecodeTest({0xff}); | 291 InvalidDecodeTest({0xff}); |
285 // Version too large. | 292 // Version too large. |
286 InvalidDecodeTest({0xff, 0x7f, 0x5f}); | 293 InvalidDecodeTest({0xff, 0x7f, 0x5f}); |
287 // Nonsense tag. | 294 // Nonsense tag. |
(...skipping 2299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2587 EXPECT_TRUE( | 2594 EXPECT_TRUE( |
2588 EvaluateScriptForResultBool("result.a.toString() === '4,5,6'")); | 2595 EvaluateScriptForResultBool("result.a.toString() === '4,5,6'")); |
2589 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 2596 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
2590 }); | 2597 }); |
2591 } | 2598 } |
2592 | 2599 |
2593 // It's expected that WebAssembly has more exhaustive tests elsewhere; this | 2600 // It's expected that WebAssembly has more exhaustive tests elsewhere; this |
2594 // mostly checks that the logic to embed it in structured clone serialization | 2601 // mostly checks that the logic to embed it in structured clone serialization |
2595 // works correctly. | 2602 // works correctly. |
2596 | 2603 |
2597 class ValueSerializerTestWithWasm : public ValueSerializerTest { | |
2598 protected: | |
2599 static void SetUpTestCase() { | |
2600 g_saved_flag = i::FLAG_expose_wasm; | |
2601 i::FLAG_expose_wasm = true; | |
2602 ValueSerializerTest::SetUpTestCase(); | |
2603 } | |
2604 | |
2605 static void TearDownTestCase() { | |
2606 ValueSerializerTest::TearDownTestCase(); | |
2607 i::FLAG_expose_wasm = g_saved_flag; | |
2608 g_saved_flag = false; | |
2609 } | |
2610 | |
2611 private: | |
2612 static bool g_saved_flag; | |
2613 }; | |
2614 | |
2615 bool ValueSerializerTestWithWasm::g_saved_flag = false; | |
2616 | |
2617 // A simple module which exports an "increment" function. | 2604 // A simple module which exports an "increment" function. |
2618 // Copied from test/mjsunit/wasm/incrementer.wasm. | 2605 // Copied from test/mjsunit/wasm/incrementer.wasm. |
2619 const unsigned char kIncrementerWasm[] = { | 2606 const unsigned char kIncrementerWasm[] = { |
2620 0, 97, 115, 109, 1, 0, 0, 0, 1, 6, 1, 96, 1, 127, 1, 127, | 2607 0, 97, 115, 109, 1, 0, 0, 0, 1, 6, 1, 96, 1, 127, 1, 127, |
2621 3, 2, 1, 0, 7, 13, 1, 9, 105, 110, 99, 114, 101, 109, 101, 110, | 2608 3, 2, 1, 0, 7, 13, 1, 9, 105, 110, 99, 114, 101, 109, 101, 110, |
2622 116, 0, 0, 10, 9, 1, 7, 0, 32, 0, 65, 1, 106, 11, | 2609 116, 0, 0, 10, 9, 1, 7, 0, 32, 0, 65, 1, 106, 11, |
2623 }; | 2610 }; |
2624 | 2611 |
2625 TEST_F(ValueSerializerTestWithWasm, RoundTripWasmModule) { | 2612 class ValueSerializerTestWithWasm : public ValueSerializerTest { |
2626 RoundTripTest( | 2613 public: |
2627 [this]() { | 2614 static const char* kUnsupportedSerialization; |
2628 return WasmCompiledModule::DeserializeOrCompile( | 2615 |
2629 isolate(), {nullptr, 0}, | 2616 ValueSerializerTestWithWasm() |
2630 {kIncrementerWasm, sizeof(kIncrementerWasm)}) | 2617 : serialize_delegate_(&transfer_modules_), |
| 2618 deserialize_delegate_(&transfer_modules_) {} |
| 2619 |
| 2620 void Reset() { |
| 2621 current_serializer_delegate_ = nullptr; |
| 2622 transfer_modules_.clear(); |
| 2623 SetExpectInlineWasm(false); |
| 2624 } |
| 2625 |
| 2626 void EnableTransferSerialization() { |
| 2627 current_serializer_delegate_ = &serialize_delegate_; |
| 2628 } |
| 2629 |
| 2630 void EnableTransferDeserialization() { |
| 2631 current_deserializer_delegate_ = &deserialize_delegate_; |
| 2632 } |
| 2633 |
| 2634 void EnableThrowingSerializer() { |
| 2635 current_serializer_delegate_ = &throwing_serializer_; |
| 2636 } |
| 2637 |
| 2638 void EnableDefaultDeserializer() { |
| 2639 current_deserializer_delegate_ = &default_deserializer_; |
| 2640 } |
| 2641 |
| 2642 protected: |
| 2643 static void SetUpTestCase() { |
| 2644 g_saved_flag = i::FLAG_expose_wasm; |
| 2645 i::FLAG_expose_wasm = true; |
| 2646 ValueSerializerTest::SetUpTestCase(); |
| 2647 } |
| 2648 |
| 2649 static void TearDownTestCase() { |
| 2650 ValueSerializerTest::TearDownTestCase(); |
| 2651 i::FLAG_expose_wasm = g_saved_flag; |
| 2652 g_saved_flag = false; |
| 2653 } |
| 2654 |
| 2655 class ThrowingSerializer : public ValueSerializer::Delegate { |
| 2656 public: |
| 2657 Maybe<uint32_t> GetWasmModuleTransferId( |
| 2658 Isolate* isolate, Local<WasmCompiledModule> module) override { |
| 2659 isolate->ThrowException(Exception::Error( |
| 2660 String::NewFromOneByte( |
| 2661 isolate, |
| 2662 reinterpret_cast<const uint8_t*>(kUnsupportedSerialization), |
| 2663 NewStringType::kNormal) |
| 2664 .ToLocalChecked())); |
| 2665 return Nothing<uint32_t>(); |
| 2666 } |
| 2667 |
| 2668 void ThrowDataCloneError(Local<String> message) override { UNREACHABLE(); } |
| 2669 }; |
| 2670 |
| 2671 class SerializeToTransfer : public ValueSerializer::Delegate { |
| 2672 public: |
| 2673 SerializeToTransfer( |
| 2674 std::vector<WasmCompiledModule::TransferrableModule>* modules) |
| 2675 : modules_(modules) {} |
| 2676 Maybe<uint32_t> GetWasmModuleTransferId( |
| 2677 Isolate* isolate, Local<WasmCompiledModule> module) override { |
| 2678 modules_->push_back(module->GetTransferrableModule()); |
| 2679 return Just(static_cast<uint32_t>(modules_->size()) - 1); |
| 2680 } |
| 2681 |
| 2682 void ThrowDataCloneError(Local<String> message) override { UNREACHABLE(); } |
| 2683 |
| 2684 private: |
| 2685 std::vector<WasmCompiledModule::TransferrableModule>* modules_; |
| 2686 }; |
| 2687 |
| 2688 class DeserializeFromTransfer : public ValueDeserializer::Delegate { |
| 2689 public: |
| 2690 DeserializeFromTransfer( |
| 2691 std::vector<WasmCompiledModule::TransferrableModule>* modules) |
| 2692 : modules_(modules) {} |
| 2693 |
| 2694 MaybeLocal<WasmCompiledModule> GetWasmModuleFromId(Isolate* isolate, |
| 2695 uint32_t id) override { |
| 2696 return WasmCompiledModule::FromTransferrableModule(isolate, |
| 2697 modules_->at(id)); |
| 2698 } |
| 2699 |
| 2700 private: |
| 2701 std::vector<WasmCompiledModule::TransferrableModule>* modules_; |
| 2702 }; |
| 2703 |
| 2704 ValueSerializer::Delegate* GetSerializerDelegate() override { |
| 2705 return current_serializer_delegate_; |
| 2706 } |
| 2707 |
| 2708 ValueDeserializer::Delegate* GetDeserializerDelegate() override { |
| 2709 return current_deserializer_delegate_; |
| 2710 } |
| 2711 |
| 2712 Local<WasmCompiledModule> MakeWasm() { |
| 2713 return WasmCompiledModule::DeserializeOrCompile( |
| 2714 isolate(), {nullptr, 0}, |
| 2715 {kIncrementerWasm, sizeof(kIncrementerWasm)}) |
| 2716 .ToLocalChecked(); |
| 2717 } |
| 2718 |
| 2719 void ExpectPass() { |
| 2720 RoundTripTest( |
| 2721 [this]() { return MakeWasm(); }, |
| 2722 [this](Local<Value> value) { |
| 2723 ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); |
| 2724 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2725 "new WebAssembly.Instance(result).exports.increment(8) === 9")); |
| 2726 }); |
| 2727 } |
| 2728 |
| 2729 void ExpectFail() { |
| 2730 EncodeTest( |
| 2731 [this]() { return MakeWasm(); }, |
| 2732 [this](const std::vector<uint8_t>& data) { InvalidDecodeTest(data); }); |
| 2733 } |
| 2734 |
| 2735 Local<Value> GetComplexObjectWithDuplicate() { |
| 2736 Local<Value> wasm_module = MakeWasm(); |
| 2737 serialization_context() |
| 2738 ->Global() |
| 2739 ->CreateDataProperty(serialization_context(), |
| 2740 StringFromUtf8("wasm_module"), wasm_module) |
| 2741 .FromMaybe(false); |
| 2742 Local<Script> script = |
| 2743 Script::Compile( |
| 2744 serialization_context(), |
| 2745 StringFromUtf8("({mod1: wasm_module, num: 2, mod2: wasm_module})")) |
2631 .ToLocalChecked(); | 2746 .ToLocalChecked(); |
2632 }, | 2747 return script->Run(serialization_context()).ToLocalChecked(); |
2633 [this](Local<Value> value) { | 2748 } |
2634 ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); | 2749 |
2635 EXPECT_TRUE(EvaluateScriptForResultBool( | 2750 void VerifyComplexObject(Local<Value> value) { |
2636 "new WebAssembly.Instance(result).exports.increment(8) === 9")); | 2751 ASSERT_TRUE(value->IsObject()); |
2637 }); | 2752 EXPECT_TRUE(EvaluateScriptForResultBool( |
2638 } | 2753 "result.mod1 instanceof WebAssembly.Module")); |
2639 | 2754 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2755 "result.mod2 instanceof WebAssembly.Module")); |
| 2756 EXPECT_TRUE(EvaluateScriptForResultBool("result.num === 2")); |
| 2757 } |
| 2758 |
| 2759 Local<Value> GetComplexObjectWithMany() { |
| 2760 Local<Value> wasm_module1 = MakeWasm(); |
| 2761 Local<Value> wasm_module2 = MakeWasm(); |
| 2762 serialization_context() |
| 2763 ->Global() |
| 2764 ->CreateDataProperty(serialization_context(), |
| 2765 StringFromUtf8("wasm_module1"), wasm_module1) |
| 2766 .FromMaybe(false); |
| 2767 serialization_context() |
| 2768 ->Global() |
| 2769 ->CreateDataProperty(serialization_context(), |
| 2770 StringFromUtf8("wasm_module2"), wasm_module2) |
| 2771 .FromMaybe(false); |
| 2772 Local<Script> script = |
| 2773 Script::Compile( |
| 2774 serialization_context(), |
| 2775 StringFromUtf8( |
| 2776 "({mod1: wasm_module1, num: 2, mod2: wasm_module2})")) |
| 2777 .ToLocalChecked(); |
| 2778 return script->Run(serialization_context()).ToLocalChecked(); |
| 2779 } |
| 2780 |
| 2781 private: |
| 2782 static bool g_saved_flag; |
| 2783 std::vector<WasmCompiledModule::TransferrableModule> transfer_modules_; |
| 2784 SerializeToTransfer serialize_delegate_; |
| 2785 DeserializeFromTransfer deserialize_delegate_; |
| 2786 ValueSerializer::Delegate* current_serializer_delegate_ = nullptr; |
| 2787 ValueDeserializer::Delegate* current_deserializer_delegate_ = nullptr; |
| 2788 ThrowingSerializer throwing_serializer_; |
| 2789 ValueDeserializer::Delegate default_deserializer_; |
| 2790 }; |
| 2791 |
| 2792 bool ValueSerializerTestWithWasm::g_saved_flag = false; |
| 2793 const char* ValueSerializerTestWithWasm::kUnsupportedSerialization = |
| 2794 "Wasm Serialization Not Supported"; |
| 2795 |
| 2796 // The default implementation of the serialization |
| 2797 // delegate throws when trying to serialize wasm. The |
| 2798 // embedder must decide serialization policy. |
| 2799 TEST_F(ValueSerializerTestWithWasm, DefaultSerializationDelegate) { |
| 2800 EnableThrowingSerializer(); |
| 2801 InvalidEncodeTest( |
| 2802 [this]() { return MakeWasm(); }, |
| 2803 [](Local<Message> message) { |
| 2804 size_t msg_len = static_cast<size_t>(message->Get()->Length()); |
| 2805 std::unique_ptr<char[]> buff(new char[msg_len + 1]); |
| 2806 message->Get()->WriteOneByte(reinterpret_cast<uint8_t*>(buff.get())); |
| 2807 // the message ends with the custom error string |
| 2808 size_t custom_msg_len = strlen(kUnsupportedSerialization); |
| 2809 ASSERT_GE(msg_len, custom_msg_len); |
| 2810 size_t start_pos = msg_len - custom_msg_len; |
| 2811 ASSERT_EQ(strcmp(&buff.get()[start_pos], kUnsupportedSerialization), 0); |
| 2812 }); |
| 2813 } |
| 2814 |
| 2815 // The default deserializer throws if wasm transfer is attempted |
| 2816 TEST_F(ValueSerializerTestWithWasm, DefaultDeserializationDelegate) { |
| 2817 EnableTransferSerialization(); |
| 2818 EnableDefaultDeserializer(); |
| 2819 EncodeTest( |
| 2820 [this]() { return MakeWasm(); }, |
| 2821 [this](const std::vector<uint8_t>& data) { InvalidDecodeTest(data); }); |
| 2822 } |
| 2823 |
| 2824 // We only want to allow deserialization through |
| 2825 // transferred modules - which requres both serializer |
| 2826 // and deserializer to understand that - or through |
| 2827 // explicitly allowing inlined data, which requires |
| 2828 // deserializer opt-in (we default the serializer to |
| 2829 // inlined data because we don't trust that data on the |
| 2830 // receiving end anyway). |
| 2831 |
| 2832 TEST_F(ValueSerializerTestWithWasm, RoundtripWasmTransfer) { |
| 2833 EnableTransferSerialization(); |
| 2834 EnableTransferDeserialization(); |
| 2835 ExpectPass(); |
| 2836 } |
| 2837 |
| 2838 TEST_F(ValueSerializerTestWithWasm, RountripWasmInline) { |
| 2839 SetExpectInlineWasm(true); |
| 2840 ExpectPass(); |
| 2841 } |
| 2842 |
| 2843 TEST_F(ValueSerializerTestWithWasm, CannotDeserializeWasmInlineData) { |
| 2844 ExpectFail(); |
| 2845 } |
| 2846 |
| 2847 TEST_F(ValueSerializerTestWithWasm, CannotTransferWasmWhenExpectingInline) { |
| 2848 EnableTransferSerialization(); |
| 2849 SetExpectInlineWasm(true); |
| 2850 ExpectFail(); |
| 2851 } |
| 2852 |
| 2853 TEST_F(ValueSerializerTestWithWasm, ComplexObjectDuplicateTransfer) { |
| 2854 EnableTransferSerialization(); |
| 2855 EnableTransferDeserialization(); |
| 2856 RoundTripTest( |
| 2857 [this]() { return GetComplexObjectWithDuplicate(); }, |
| 2858 [this](Local<Value> value) { |
| 2859 VerifyComplexObject(value); |
| 2860 EXPECT_TRUE(EvaluateScriptForResultBool("result.mod1 === result.mod2")); |
| 2861 }); |
| 2862 } |
| 2863 |
| 2864 TEST_F(ValueSerializerTestWithWasm, ComplexObjectDuplicateInline) { |
| 2865 SetExpectInlineWasm(true); |
| 2866 RoundTripTest( |
| 2867 [this]() { return GetComplexObjectWithDuplicate(); }, |
| 2868 [this](Local<Value> value) { |
| 2869 VerifyComplexObject(value); |
| 2870 EXPECT_TRUE(EvaluateScriptForResultBool("result.mod1 === result.mod2")); |
| 2871 }); |
| 2872 } |
| 2873 |
| 2874 TEST_F(ValueSerializerTestWithWasm, ComplexObjectWithManyTransfer) { |
| 2875 EnableTransferSerialization(); |
| 2876 EnableTransferDeserialization(); |
| 2877 RoundTripTest( |
| 2878 [this]() { return GetComplexObjectWithMany(); }, |
| 2879 [this](Local<Value> value) { |
| 2880 VerifyComplexObject(value); |
| 2881 EXPECT_TRUE(EvaluateScriptForResultBool("result.mod1 != result.mod2")); |
| 2882 }); |
| 2883 } |
| 2884 |
| 2885 TEST_F(ValueSerializerTestWithWasm, ComplexObjectWithManyInline) { |
| 2886 SetExpectInlineWasm(true); |
| 2887 RoundTripTest( |
| 2888 [this]() { return GetComplexObjectWithMany(); }, |
| 2889 [this](Local<Value> value) { |
| 2890 VerifyComplexObject(value); |
| 2891 EXPECT_TRUE(EvaluateScriptForResultBool("result.mod1 != result.mod2")); |
| 2892 }); |
| 2893 } |
| 2894 |
2640 // As produced around Chrome 56. | 2895 // As produced around Chrome 56. |
2641 const unsigned char kSerializedIncrementerWasm[] = { | 2896 const unsigned char kSerializedIncrementerWasm[] = { |
2642 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d, 0x0d, | 2897 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d, 0x0d, |
2643 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03, | 2898 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03, |
2644 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e, 0x63, 0x72, 0x65, | 2899 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e, 0x63, 0x72, 0x65, |
2645 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08, 0x01, 0x06, 0x00, 0x20, | 2900 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08, 0x01, 0x06, 0x00, 0x20, |
2646 0x00, 0x41, 0x01, 0x6a, 0xf8, 0x04, 0xa1, 0x06, 0xde, 0xc0, 0xc6, 0x44, | 2901 0x00, 0x41, 0x01, 0x6a, 0xf8, 0x04, 0xa1, 0x06, 0xde, 0xc0, 0xc6, 0x44, |
2647 0x3c, 0x29, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x02, 0x00, 0x00, 0x81, 0x4e, | 2902 0x3c, 0x29, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x02, 0x00, 0x00, 0x81, 0x4e, |
2648 0xce, 0x7c, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x02, | 2903 0xce, 0x7c, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x02, |
2649 0x00, 0x00, 0xb0, 0x25, 0x30, 0xe3, 0xf2, 0xdb, 0x2e, 0x48, 0x00, 0x00, | 2904 0x00, 0x00, 0xb0, 0x25, 0x30, 0xe3, 0xf2, 0xdb, 0x2e, 0x48, 0x00, 0x00, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2744 InvalidDecodeTest(raw); | 2999 InvalidDecodeTest(raw); |
2745 } | 3000 } |
2746 | 3001 |
2747 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { | 3002 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { |
2748 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); | 3003 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); |
2749 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); | 3004 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); |
2750 } | 3005 } |
2751 | 3006 |
2752 } // namespace | 3007 } // namespace |
2753 } // namespace v8 | 3008 } // namespace v8 |
OLD | NEW |