Index: test/unittests/value-serializer-unittest.cc |
diff --git a/test/unittests/value-serializer-unittest.cc b/test/unittests/value-serializer-unittest.cc |
index 44ad79447b33eee05fd77cbc001d1caa3a27c841..fafba2313e5aa16dca4ba459dcb63b527e8d7c6f 100644 |
--- a/test/unittests/value-serializer-unittest.cc |
+++ b/test/unittests/value-serializer-unittest.cc |
@@ -2372,5 +2372,162 @@ TEST_F(ValueSerializerTestWithHostObject, RoundTripSameObject) { |
}); |
} |
+// It's expected that WebAssembly has more exhaustive tests elsewhere; this |
+// mostly checks that the logic to embed it in structured clone serialization |
+// works correctly. |
+ |
+class ValueSerializerTestWithWasm : public ValueSerializerTest { |
+ protected: |
+ static void SetUpTestCase() { |
+ g_saved_flag = i::FLAG_expose_wasm; |
+ i::FLAG_expose_wasm = true; |
+ ValueSerializerTest::SetUpTestCase(); |
+ } |
+ |
+ static void TearDownTestCase() { |
+ ValueSerializerTest::TearDownTestCase(); |
+ i::FLAG_expose_wasm = g_saved_flag; |
+ g_saved_flag = false; |
+ } |
+ |
+ private: |
+ static bool g_saved_flag; |
+}; |
+ |
+bool ValueSerializerTestWithWasm::g_saved_flag = false; |
+ |
+// A simple module which exports an "increment" function. |
+// Copied from test/mjsunit/wasm/incrementer.wasm. |
+const unsigned char kIncrementerWasm[] = { |
+ 0x00, 0x61, 0x73, 0x6d, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, |
+ 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, |
+ 0x69, 0x6e, 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, |
+ 0x08, 0x01, 0x06, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a}; |
+ |
+TEST_F(ValueSerializerTestWithWasm, RoundTripWasmModule) { |
+ RoundTripTest( |
+ [this]() { |
+ return WasmCompiledModule::DeserializeOrCompile( |
+ isolate(), {nullptr, 0}, |
+ {kIncrementerWasm, sizeof(kIncrementerWasm)}) |
+ .ToLocalChecked(); |
+ }, |
+ [this](Local<Value> value) { |
+ ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); |
+ EXPECT_TRUE(EvaluateScriptForResultBool( |
+ "new WebAssembly.Instance(result).exports.increment(8) === 9")); |
+ }); |
+} |
+ |
+// As produced around Chrome 56. |
+const unsigned char kSerializedIncrementerWasm[] = { |
+ 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d, 0x0d, |
+ 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x03, |
+ 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e, 0x63, 0x72, 0x65, |
+ 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08, 0x01, 0x06, 0x00, 0x20, |
+ 0x00, 0x41, 0x01, 0x6a, 0xf8, 0x04, 0xa1, 0x06, 0xde, 0xc0, 0xc6, 0x44, |
+ 0x3c, 0x29, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x02, 0x00, 0x00, 0x81, 0x4e, |
+ 0xce, 0x7c, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x30, 0x02, |
+ 0x00, 0x00, 0xb0, 0x25, 0x30, 0xe3, 0xf2, 0xdb, 0x2e, 0x48, 0x00, 0x00, |
+ 0x00, 0x80, 0xe8, 0x00, 0x00, 0x80, 0xe0, 0x01, 0x00, 0x80, 0x00, 0x00, |
+ 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x07, 0x08, 0x00, 0x00, 0x09, 0x04, |
+ 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x3c, 0x8c, 0xc0, 0x00, 0x00, |
+ 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x10, 0x8c, 0xc0, 0x00, 0x00, |
+ 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x70, 0x94, 0x01, 0x0c, 0x8b, |
+ 0xc1, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x25, 0xdc, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x9e, 0x01, 0x10, 0x8c, 0xc0, 0x00, 0x00, |
+ 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x84, 0xc0, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x05, 0x7d, 0x01, 0x1a, 0xe1, 0x02, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x23, 0x88, 0x42, 0x32, 0x03, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x04, 0x00, |
+ 0x00, 0x02, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, |
+ 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x49, 0x3b, 0xa5, 0x60, 0x0c, 0x00, |
+ 0x00, 0x0f, 0x86, 0x04, 0x00, 0x00, 0x00, 0x83, 0xc0, 0x01, 0xc3, 0x55, |
+ 0x48, 0x89, 0xe5, 0x49, 0xba, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, |
+ 0x00, 0x41, 0x52, 0x48, 0x83, 0xec, 0x08, 0x48, 0x89, 0x45, 0xf0, 0x48, |
+ 0xbb, 0xb0, 0x67, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0xc0, 0x48, |
+ 0xbe, 0xe1, 0x57, 0x81, 0x85, 0xf6, 0x14, 0x00, 0x00, 0xe8, 0xfc, 0x3c, |
+ 0xea, 0xff, 0x48, 0x8b, 0x45, 0xf0, 0x48, 0x8b, 0xe5, 0x5d, 0xeb, 0xbf, |
+ 0x66, 0x90, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x44, 0x00, |
+ 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x0f, 0x20, 0x84, 0x0f, 0x7d, 0x01, 0x0d, 0x00, 0x0f, 0x04, |
+ 0x6d, 0x08, 0x0f, 0xf0, 0x02, 0x80, 0x94, 0x01, 0x0c, 0x8b, 0xc1, 0x00, |
+ 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xed, 0xa9, 0x2d, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x9e, 0xe0, 0x38, 0x1a, 0x61, 0x03, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x23, 0x88, 0x42, 0x32, 0x03, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, |
+ 0x02, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, |
+ 0xff, 0x00, 0x00, 0x00, 0x00, 0x55, 0x48, 0x89, 0xe5, 0x56, 0x57, 0x48, |
+ 0x8b, 0x45, 0x10, 0xe8, 0x11, 0xed, 0xed, 0xff, 0xa8, 0x01, 0x0f, 0x85, |
+ 0x2d, 0x00, 0x00, 0x00, 0x48, 0xc1, 0xe8, 0x20, 0xc5, 0xf9, 0x57, 0xc0, |
+ 0xc5, 0xfb, 0x2a, 0xc0, 0xc4, 0xe1, 0xfb, 0x2c, 0xc0, 0x48, 0x83, 0xf8, |
+ 0x01, 0x0f, 0x80, 0x34, 0x00, 0x00, 0x00, 0x8b, 0xc0, 0xe8, 0x27, 0xfe, |
+ 0xff, 0xff, 0x48, 0xc1, 0xe0, 0x20, 0x48, 0x8b, 0xe5, 0x5d, 0xc2, 0x10, |
+ 0x00, 0x49, 0x39, 0x45, 0xa0, 0x0f, 0x84, 0x07, 0x00, 0x00, 0x00, 0xc5, |
+ 0xfb, 0x10, 0x40, 0x07, 0xeb, 0xce, 0x49, 0xba, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0xf8, 0x7f, 0xc4, 0xc1, 0xf9, 0x6e, 0xc2, 0xeb, 0xbd, 0x48, |
+ 0x83, 0xec, 0x08, 0xc5, 0xfb, 0x11, 0x04, 0x24, 0xe8, 0xcc, 0xfe, 0xff, |
+ 0xff, 0x48, 0x83, 0xc4, 0x08, 0xeb, 0xb8, 0x66, 0x90, 0x02, 0x00, 0x00, |
+ 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, |
+ 0x0f, 0x39, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0xff, 0xff, 0x00, |
+ 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x20, 0x84, |
+ 0x0f, 0xcc, 0x6e, 0x7d, 0x01, 0x72, 0x98, 0x00, 0x0f, 0xdc, 0x6d, 0x0c, |
+ 0x0f, 0xb0, 0x84, 0x0d, 0x04, 0x84, 0xe3, 0xc0, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x84, 0xe0, 0x84, 0x84, 0x18, 0x2f, 0x2f, 0x2f, |
+ 0x2f, 0x2f}; |
+ |
+TEST_F(ValueSerializerTestWithWasm, DecodeWasmModule) { |
+ std::vector<uint8_t> raw( |
+ kSerializedIncrementerWasm, |
+ kSerializedIncrementerWasm + sizeof(kSerializedIncrementerWasm)); |
+ DecodeTest(raw, [this](Local<Value> value) { |
+ ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); |
+ EXPECT_TRUE(EvaluateScriptForResultBool( |
+ "new WebAssembly.Instance(result).exports.increment(8) === 9")); |
+ }); |
+} |
+ |
+// As above, but with empty compiled data. Should work due to fallback to wire |
+// data. |
+const unsigned char kSerializedIncrementerWasmWithInvalidCompiledData[] = { |
+ 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x2d, 0x00, 0x61, 0x73, 0x6d, |
+ 0x0d, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, |
+ 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x69, 0x6e, |
+ 0x63, 0x72, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x0a, 0x08, |
+ 0x01, 0x06, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a, 0x00}; |
+ |
+TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidCompiledData) { |
+ std::vector<uint8_t> raw( |
+ kSerializedIncrementerWasmWithInvalidCompiledData, |
+ kSerializedIncrementerWasmWithInvalidCompiledData + |
+ sizeof(kSerializedIncrementerWasmWithInvalidCompiledData)); |
+ DecodeTest(raw, [this](Local<Value> value) { |
+ ASSERT_TRUE(value->IsWebAssemblyCompiledModule()); |
+ EXPECT_TRUE(EvaluateScriptForResultBool( |
+ "new WebAssembly.Instance(result).exports.increment(8) === 9")); |
+ }); |
+} |
+ |
+// As above, but also with empty wire data. Should fail. |
+const unsigned char kSerializedIncrementerWasmInvalid[] = { |
+ 0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x00}; |
+ |
+TEST_F(ValueSerializerTestWithWasm, |
+ DecodeWasmModuleWithInvalidCompiledAndWireData) { |
+ std::vector<uint8_t> raw(kSerializedIncrementerWasmInvalid, |
+ kSerializedIncrementerWasmInvalid + |
+ sizeof(kSerializedIncrementerWasmInvalid)); |
+ InvalidDecodeTest(raw); |
+} |
+ |
+TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { |
+ InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); |
+ InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); |
+} |
+ |
} // namespace |
} // namespace v8 |