OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 17 matching lines...) Expand all Loading... |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "bindings/core/v8/SerializedScriptValue.h" | 31 #include "bindings/core/v8/SerializedScriptValue.h" |
32 | 32 |
33 #include <memory> | 33 #include <memory> |
34 #include "bindings/core/v8/DOMDataStore.h" | 34 #include "bindings/core/v8/DOMDataStore.h" |
35 #include "bindings/core/v8/DOMWrapperWorld.h" | 35 #include "bindings/core/v8/DOMWrapperWorld.h" |
36 #include "bindings/core/v8/ExceptionState.h" | 36 #include "bindings/core/v8/ExceptionState.h" |
37 #include "bindings/core/v8/ScriptState.h" | 37 #include "bindings/core/v8/ScriptState.h" |
| 38 #include "bindings/core/v8/SerializationTag.h" |
38 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 39 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
39 #include "bindings/core/v8/Transferables.h" | 40 #include "bindings/core/v8/Transferables.h" |
40 #include "bindings/core/v8/V8ArrayBuffer.h" | 41 #include "bindings/core/v8/V8ArrayBuffer.h" |
41 #include "bindings/core/v8/V8ImageBitmap.h" | 42 #include "bindings/core/v8/V8ImageBitmap.h" |
42 #include "bindings/core/v8/V8MessagePort.h" | 43 #include "bindings/core/v8/V8MessagePort.h" |
43 #include "bindings/core/v8/V8OffscreenCanvas.h" | 44 #include "bindings/core/v8/V8OffscreenCanvas.h" |
44 #include "bindings/core/v8/V8SharedArrayBuffer.h" | 45 #include "bindings/core/v8/V8SharedArrayBuffer.h" |
45 #include "core/dom/DOMArrayBuffer.h" | 46 #include "core/dom/DOMArrayBuffer.h" |
46 #include "core/dom/DOMSharedArrayBuffer.h" | 47 #include "core/dom/DOMSharedArrayBuffer.h" |
47 #include "core/dom/ExceptionCode.h" | 48 #include "core/dom/ExceptionCode.h" |
48 #include "core/dom/MessagePort.h" | 49 #include "core/dom/MessagePort.h" |
49 #include "core/frame/ImageBitmap.h" | 50 #include "core/frame/ImageBitmap.h" |
50 #include "platform/SharedBuffer.h" | 51 #include "platform/SharedBuffer.h" |
51 #include "platform/blob/BlobData.h" | 52 #include "platform/blob/BlobData.h" |
52 #include "platform/heap/Handle.h" | 53 #include "platform/heap/Handle.h" |
53 #include "platform/wtf/Assertions.h" | 54 #include "platform/wtf/Assertions.h" |
54 #include "platform/wtf/ByteOrder.h" | 55 #include "platform/wtf/ByteOrder.h" |
55 #include "platform/wtf/PtrUtil.h" | 56 #include "platform/wtf/PtrUtil.h" |
56 #include "platform/wtf/Vector.h" | 57 #include "platform/wtf/Vector.h" |
| 58 #include "platform/wtf/dtoa/utils.h" |
57 #include "platform/wtf/text/StringBuffer.h" | 59 #include "platform/wtf/text/StringBuffer.h" |
58 #include "platform/wtf/text/StringHash.h" | 60 #include "platform/wtf/text/StringHash.h" |
59 | 61 |
60 namespace blink { | 62 namespace blink { |
61 | 63 |
62 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Serialize( | 64 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Serialize( |
63 v8::Isolate* isolate, | 65 v8::Isolate* isolate, |
64 v8::Local<v8::Value> value, | 66 v8::Local<v8::Value> value, |
65 const SerializeOptions& options, | 67 const SerializeOptions& options, |
66 ExceptionState& exception) { | 68 ExceptionState& exception) { |
(...skipping 15 matching lines...) Expand all Loading... |
82 | 84 |
83 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Create() { | 85 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Create() { |
84 return AdoptRef(new SerializedScriptValue); | 86 return AdoptRef(new SerializedScriptValue); |
85 } | 87 } |
86 | 88 |
87 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Create( | 89 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Create( |
88 const String& data) { | 90 const String& data) { |
89 return AdoptRef(new SerializedScriptValue(data)); | 91 return AdoptRef(new SerializedScriptValue(data)); |
90 } | 92 } |
91 | 93 |
| 94 // Versions 16 and below (prior to April 2017) used ntohs() to byte-swap SSV |
| 95 // data when converting it to the wire format. This was a historical accient. |
| 96 // |
| 97 // As IndexedDB stores SSVs to disk indefinitely, we still need to keep around |
| 98 // the code needed to deserialize the old format. |
| 99 inline static bool IsByteSwappedWiredData(const char* data, size_t length) { |
| 100 // TODO(pwnall): Return false early if we're on big-endian hardware. Chromium |
| 101 // doesn't currently support big-endian hardware, and there's no header |
| 102 // exposing endianness to Blink yet. ARCH_CPU_LITTLE_ENDIAN seems promising, |
| 103 // but Blink is not currently allowed to include files from build/. |
| 104 |
| 105 // The first SSV version without byte-swapping has two envelopes (Blink, V8), |
| 106 // each of which is at least 2 bytes long. |
| 107 if (length < 4) |
| 108 return true; |
| 109 |
| 110 // This code handles the following cases: |
| 111 // |
| 112 // v0 (byte-swapped) - [d, t, ...], t = tag byte, d = first data byte |
| 113 // v1-16 (byte-swapped) - [v, 0xFF, ...], v = version (1 <= v <= 16) |
| 114 // v17+ - [0xFF, v, ...], v = first byte of version varint |
| 115 |
| 116 if (static_cast<uint8_t>(data[0]) == kVersionTag) { |
| 117 // The only case where byte-swapped data can have 0xFF in byte zero is |
| 118 // version 0. This can only happen if byte one is a tag (supported in |
| 119 // version 0) that takes in extra data, and the first byte of extra data is |
| 120 // 0xFF. There are 13 such tags, listed below. These tags cannot be used as |
| 121 // version numbers in the Blink-side SSV envelope. |
| 122 // |
| 123 // 35 - 0x23 - # - ImageDataTag |
| 124 // 64 - 0x40 - @ - SparseArrayTag |
| 125 // 68 - 0x44 - D - DateTag |
| 126 // 73 - 0x49 - I - Int32Tag |
| 127 // 78 - 0x4E - N - NumberTag |
| 128 // 82 - 0x52 - R - RegExpTag |
| 129 // 83 - 0x53 - S - StringTag |
| 130 // 85 - 0x55 - U - Uint32Tag |
| 131 // 91 - 0x5B - [ - ArrayTag |
| 132 // 98 - 0x62 - b - BlobTag |
| 133 // 102 - 0x66 - f - FileTag |
| 134 // 108 - 0x6C - l - FileListTag |
| 135 // 123 - 0x7B - { - ObjectTag |
| 136 // |
| 137 // Why we care about version 0: |
| 138 // |
| 139 // IndexedDB stores values using the SSV format. Currently, IndexedDB does |
| 140 // not do any sort of migration, so a value written with a SSV version will |
| 141 // be stored with that version until it is removed via an update or delete. |
| 142 // |
| 143 // IndexedDB was shipped in Chrome 11, which was released on April 27, 2011. |
| 144 // SSV version 1 was added in WebKit r91698, which was shipped in Chrome 14, |
| 145 // which was released on September 16, 2011. |
| 146 static_assert( |
| 147 SerializedScriptValue::kWireFormatVersion != 35 && |
| 148 SerializedScriptValue::kWireFormatVersion != 64 && |
| 149 SerializedScriptValue::kWireFormatVersion != 68 && |
| 150 SerializedScriptValue::kWireFormatVersion != 73 && |
| 151 SerializedScriptValue::kWireFormatVersion != 78 && |
| 152 SerializedScriptValue::kWireFormatVersion != 82 && |
| 153 SerializedScriptValue::kWireFormatVersion != 83 && |
| 154 SerializedScriptValue::kWireFormatVersion != 85 && |
| 155 SerializedScriptValue::kWireFormatVersion != 91 && |
| 156 SerializedScriptValue::kWireFormatVersion != 98 && |
| 157 SerializedScriptValue::kWireFormatVersion != 102 && |
| 158 SerializedScriptValue::kWireFormatVersion != 108 && |
| 159 SerializedScriptValue::kWireFormatVersion != 123, |
| 160 "Using a burned version will prevent us from reading SSV version 0"); |
| 161 |
| 162 // Fast path until the Blink-side SSV envelope reaches version 35. |
| 163 if (SerializedScriptValue::kWireFormatVersion < 35) { |
| 164 if (static_cast<uint8_t>(data[1]) < 35) |
| 165 return false; |
| 166 |
| 167 // TODO(pwnall): Add UMA metric here. |
| 168 return true; |
| 169 } |
| 170 |
| 171 // Slower path that would kick in after version 35, assuming we don't remove |
| 172 // support for SSV version 0 by then. |
| 173 static uint8_t version0Tags[] = {35, 64, 68, 73, 78, 82, 83, |
| 174 85, 91, 98, 102, 108, 123}; |
| 175 |
| 176 uint8_t maybeVersion0Tag = static_cast<uint8_t>(data[1]); |
| 177 for (size_t i = 0; i < ARRAY_SIZE(version0Tags); ++i) { |
| 178 if (maybeVersion0Tag == version0Tags[i]) |
| 179 return true; |
| 180 } |
| 181 return false; |
| 182 } |
| 183 |
| 184 if (static_cast<uint8_t>(data[1]) == kVersionTag) { |
| 185 // The last SSV format that used byte-flipping was version 16. The version |
| 186 // number is stored (before byte-flipping) after a serialization tag, which |
| 187 // is 0xFF. |
| 188 return static_cast<uint8_t>(data[0]) != kVersionTag; |
| 189 } |
| 190 |
| 191 // If kVersionTag isn't in any of the first two bytes, this is SSV version 0, |
| 192 // which was byte-flipped. |
| 193 return true; |
| 194 } |
| 195 |
92 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Create( | 196 PassRefPtr<SerializedScriptValue> SerializedScriptValue::Create( |
93 const char* data, | 197 const char* data, |
94 size_t length) { | 198 size_t length) { |
95 if (!data) | 199 if (!data) |
96 return Create(); | 200 return Create(); |
97 | 201 |
98 // Decode wire data from big endian to host byte order. | |
99 DCHECK(!(length % sizeof(UChar))); | 202 DCHECK(!(length % sizeof(UChar))); |
| 203 const UChar* src = reinterpret_cast<const UChar*>(data); |
100 size_t string_length = length / sizeof(UChar); | 204 size_t string_length = length / sizeof(UChar); |
101 StringBuffer<UChar> buffer(string_length); | |
102 const UChar* src = reinterpret_cast<const UChar*>(data); | |
103 UChar* dst = buffer.Characters(); | |
104 for (size_t i = 0; i < string_length; i++) | |
105 dst[i] = ntohs(src[i]); | |
106 | 205 |
107 return AdoptRef(new SerializedScriptValue(String::Adopt(buffer))); | 206 if (IsByteSwappedWiredData(data, length)) { |
| 207 // Decode wire data from big endian to host byte order. |
| 208 StringBuffer<UChar> buffer(string_length); |
| 209 UChar* dst = buffer.Characters(); |
| 210 for (size_t i = 0; i < string_length; ++i) |
| 211 dst[i] = ntohs(src[i]); |
| 212 |
| 213 return AdoptRef(new SerializedScriptValue(String::Adopt(buffer))); |
| 214 } |
| 215 |
| 216 return AdoptRef(new SerializedScriptValue(String(src, string_length))); |
108 } | 217 } |
109 | 218 |
110 SerializedScriptValue::SerializedScriptValue() | 219 SerializedScriptValue::SerializedScriptValue() |
111 : has_registered_external_allocation_(false), | 220 : has_registered_external_allocation_(false), |
112 transferables_need_external_allocation_registration_(false) {} | 221 transferables_need_external_allocation_registration_(false) {} |
113 | 222 |
114 SerializedScriptValue::SerializedScriptValue(const String& wire_data) | 223 SerializedScriptValue::SerializedScriptValue(const String& wire_data) |
115 : has_registered_external_allocation_(false), | 224 : has_registered_external_allocation_(false), |
116 transferables_need_external_allocation_registration_(false) { | 225 transferables_need_external_allocation_registration_(false) { |
117 size_t byte_length = wire_data.length() * 2; | 226 size_t byte_length = wire_data.length() * 2; |
118 data_buffer_.reset(static_cast<uint8_t*>(WTF::Partitions::BufferMalloc( | 227 data_buffer_.reset(static_cast<uint8_t*>(WTF::Partitions::BufferMalloc( |
119 byte_length, "SerializedScriptValue buffer"))); | 228 byte_length, "SerializedScriptValue buffer"))); |
120 data_buffer_size_ = byte_length; | 229 data_buffer_size_ = byte_length; |
121 wire_data.CopyTo(reinterpret_cast<UChar*>(data_buffer_.get()), 0, | 230 wire_data.CopyTo(reinterpret_cast<UChar*>(data_buffer_.get()), 0, |
122 wire_data.length()); | 231 wire_data.length()); |
123 } | 232 } |
124 | 233 |
125 SerializedScriptValue::~SerializedScriptValue() { | 234 SerializedScriptValue::~SerializedScriptValue() { |
126 // If the allocated memory was not registered before, then this class is | 235 // If the allocated memory was not registered before, then this class is |
127 // likely used in a context other than Worker's onmessage environment and the | 236 // likely used in a context other than Worker's onmessage environment and the |
128 // presence of current v8 context is not guaranteed. Avoid calling v8 then. | 237 // presence of current v8 context is not guaranteed. Avoid calling v8 then. |
129 if (has_registered_external_allocation_) { | 238 if (has_registered_external_allocation_) { |
130 ASSERT(v8::Isolate::GetCurrent()); | 239 ASSERT(v8::Isolate::GetCurrent()); |
131 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( | 240 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( |
132 -static_cast<int64_t>(DataLengthInBytes())); | 241 -static_cast<int64_t>(DataLengthInBytes())); |
133 } | 242 } |
134 } | 243 } |
135 | 244 |
136 PassRefPtr<SerializedScriptValue> SerializedScriptValue::NullValue() { | 245 PassRefPtr<SerializedScriptValue> SerializedScriptValue::NullValue() { |
137 // UChar rather than uint8_t here to get host endian behavior. | 246 static const uint8_t kNullData[] = { |
138 static const UChar kNullData[] = {0xff09, 0x3000}; | 247 kVersionTag, kWireFormatVersion, // Blink envelope |
| 248 // 13 is v8::ValueSerializer::GetCurrentDataFormatVersion(). Having a |
| 249 // hard-coded version here is better than getting V8's version directly. |
| 250 // This approach lets v8 make drastic changes to its serialization format |
| 251 // without a 3-sided patch, as long as it retains the ability to |
| 252 // deserialize the version number listed here. |
| 253 kVersionTag, 13, // V8 envelope |
| 254 '0', // kNull in v8::internal::SerializationTag |
| 255 0x00}; // Padding to an integral number of UChars |
139 return Create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData)); | 256 return Create(reinterpret_cast<const char*>(kNullData), sizeof(kNullData)); |
140 } | 257 } |
141 | 258 |
142 String SerializedScriptValue::ToWireString() const { | 259 String SerializedScriptValue::ToWireString() const { |
143 // Add the padding '\0', but don't put it in |m_dataBuffer|. | 260 // Add the padding '\0', but don't put it in |m_dataBuffer|. |
144 // This requires direct use of uninitialized strings, though. | 261 // This requires direct use of uninitialized strings, though. |
145 UChar* destination; | 262 UChar* destination; |
146 size_t string_size_bytes = (data_buffer_size_ + 1) & ~1; | 263 size_t string_size_bytes = (data_buffer_size_ + 1) & ~1; |
147 String wire_string = | 264 String wire_string = |
148 String::CreateUninitialized(string_size_bytes / 2, destination); | 265 String::CreateUninitialized(string_size_bytes / 2, destination); |
149 memcpy(destination, data_buffer_.get(), data_buffer_size_); | 266 memcpy(destination, data_buffer_.get(), data_buffer_size_); |
150 if (string_size_bytes > data_buffer_size_) | 267 if (string_size_bytes > data_buffer_size_) |
151 reinterpret_cast<char*>(destination)[string_size_bytes - 1] = '\0'; | 268 reinterpret_cast<char*>(destination)[string_size_bytes - 1] = '\0'; |
152 return wire_string; | 269 return wire_string; |
153 } | 270 } |
154 | 271 |
155 // Convert serialized string to big endian wire data. | |
156 void SerializedScriptValue::ToWireBytes(Vector<char>& result) const { | 272 void SerializedScriptValue::ToWireBytes(Vector<char>& result) const { |
157 DCHECK(result.IsEmpty()); | 273 DCHECK(result.IsEmpty()); |
158 | 274 |
159 size_t wire_size_bytes = (data_buffer_size_ + 1) & ~1; | 275 size_t result_size = (data_buffer_size_ + 1) & ~1; |
160 result.Resize(wire_size_bytes); | 276 result.Resize(result_size); |
| 277 memcpy(result.Data(), data_buffer_.get(), data_buffer_size_); |
161 | 278 |
162 const UChar* src = reinterpret_cast<UChar*>(data_buffer_.get()); | 279 if (result_size > data_buffer_size_) { |
163 UChar* dst = reinterpret_cast<UChar*>(result.Data()); | 280 DCHECK_EQ(result_size, data_buffer_size_ + 1); |
164 for (size_t i = 0; i < data_buffer_size_ / 2; i++) | 281 result[data_buffer_size_] = 0; |
165 dst[i] = htons(src[i]); | 282 } |
166 | |
167 // This is equivalent to swapping the byte order of the two bytes (x, 0), | |
168 // depending on endianness. | |
169 if (data_buffer_size_ % 2) | |
170 dst[wire_size_bytes / 2 - 1] = data_buffer_[data_buffer_size_ - 1] << 8; | |
171 } | 283 } |
172 | 284 |
173 static void AccumulateArrayBuffersForAllWorlds( | 285 static void AccumulateArrayBuffersForAllWorlds( |
174 v8::Isolate* isolate, | 286 v8::Isolate* isolate, |
175 DOMArrayBuffer* object, | 287 DOMArrayBuffer* object, |
176 Vector<v8::Local<v8::ArrayBuffer>, 4>& buffers) { | 288 Vector<v8::Local<v8::ArrayBuffer>, 4>& buffers) { |
177 Vector<RefPtr<DOMWrapperWorld>> worlds; | 289 Vector<RefPtr<DOMWrapperWorld>> worlds; |
178 DOMWrapperWorld::AllWorldsInCurrentThread(worlds); | 290 DOMWrapperWorld::AllWorldsInCurrentThread(worlds); |
179 for (const auto& world : worlds) { | 291 for (const auto& world : worlds) { |
180 v8::Local<v8::Object> wrapper = world->DomDataStore().Get(object, isolate); | 292 v8::Local<v8::Object> wrapper = world->DomDataStore().Get(object, isolate); |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 // Only (re)register allocation cost for transferables if this | 574 // Only (re)register allocation cost for transferables if this |
463 // SerializedScriptValue has explicitly unregistered them before. | 575 // SerializedScriptValue has explicitly unregistered them before. |
464 if (array_buffer_contents_array_ && | 576 if (array_buffer_contents_array_ && |
465 transferables_need_external_allocation_registration_) { | 577 transferables_need_external_allocation_registration_) { |
466 for (auto& buffer : *array_buffer_contents_array_) | 578 for (auto& buffer : *array_buffer_contents_array_) |
467 buffer.RegisterExternalAllocationWithCurrentContext(); | 579 buffer.RegisterExternalAllocationWithCurrentContext(); |
468 } | 580 } |
469 } | 581 } |
470 | 582 |
471 } // namespace blink | 583 } // namespace blink |
OLD | NEW |