OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "modules/indexeddb/IDBValueWrapping.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "bindings/core/v8/ScriptValue.h" | |
10 #include "bindings/core/v8/serialization/SerializationTag.h" | |
11 #include "bindings/core/v8/serialization/SerializedScriptValue.h" | |
12 #include "bindings/modules/v8/V8BindingForModules.h" | |
13 #include "core/fileapi/Blob.h" | |
14 #include "modules/indexeddb/IDBRequest.h" | |
15 #include "modules/indexeddb/IDBValue.h" | |
16 #include "platform/blob/BlobData.h" | |
17 #include "platform/wtf/text/WTFString.h" | |
18 | |
19 namespace blink { | |
20 | |
21 namespace { | |
22 | |
23 // V8 values are stored on disk by IndexedDB using the format implemented in | |
24 // SerializedScriptValue (SSV). The wrapping detection logic in | |
25 // IDBValueUnwrapper::IsWrapped() must be able to distinguish between SSV byte | |
26 // sequences produced and byte sequences expressing the fact that an IDBValue | |
27 // has been wrapped and requires post-processing. | |
28 // | |
29 // The detection logic takes advantage of the highly regular structure around | |
30 // SerializedScriptValue. A version 17 byte sequence always starts with the | |
31 // following four bytes: | |
32 // | |
33 // 1) 0xFF - version tag | |
34 // 2) 0x11 - Blink wrapper version, 17 | |
35 // 3) 0xFF - version tag | |
36 // 4) 0x0D - V8 serialization version, currently 13, doesn't matter | |
37 // | |
38 // It follows that SSV will never produce byte sequences starting with 0xFF, | |
jsbell
2017/05/15 23:37:38
Can you run this past jbroman@ as a sanity check?
pwnall
2017/05/19 18:27:35
Will do.
| |
39 // 0x11, and any value except for 0xFF. If the SSV format changes, the version | |
40 // will have to be bumped. | |
41 | |
42 // The SSV format version whose encoding hole is (ab)used for wrapping. | |
43 const static uint8_t kRequiresProcessingSSVPseudoVersion = 17; | |
44 | |
45 // Identifies IndexedDB values that were wrapped in Blobs. The wrapper has the | |
46 // following format: | |
47 // | |
48 // 1) varint - Blob size | |
49 // 2) varint length-prefixed ASCII string - Blob UUID | |
jsbell
2017/05/15 23:37:39
This is where I unhelpfully wonder if just storing
pwnall
2017/05/19 18:27:35
FWIW, I think this (serialization format) is the m
jsbell
2017/05/22 21:54:19
sgtm...
On 2017/05/19 18:27:35, pwnall wrote:
| |
50 const static uint8_t kBlobWrappedValue = 1; | |
51 | |
52 } // namespace | |
53 | |
54 IDBValueWrapper::IDBValueWrapper(v8::Isolate* isolate, | |
55 v8::Local<v8::Value> value, | |
56 bool write_wasm_to_stream, | |
57 ExceptionState& exception_state) { | |
58 SerializedScriptValue::SerializeOptions options; | |
59 options.blob_info = &blob_info_; | |
60 options.for_storage = SerializedScriptValue::kForStorage; | |
61 options.write_wasm_to_stream = write_wasm_to_stream; | |
62 | |
63 serialized_value_ = SerializedScriptValue::Serialize(isolate, value, options, | |
64 exception_state); | |
65 | |
66 #if DCHECK_IS_ON() | |
67 if (exception_state.HadException()) | |
68 had_exception_ = true; | |
69 #endif // DCHECK_IS_ON() | |
70 } | |
71 | |
72 void IDBValueWrapper::Clone(ScriptState* script_state, ScriptValue* clone) { | |
73 #if DCHECK_IS_ON() | |
74 DCHECK(!had_exception_) << __FUNCTION__ | |
75 << " called on wrapper with serialization exception"; | |
76 DCHECK(!wrap_called_) << "Clone() called after WrapIfBiggerThan()"; | |
77 #endif // DCHECK_IS_ON() | |
78 *clone = DeserializeScriptValue(script_state, serialized_value_.Get(), | |
79 &blob_info_); | |
80 } | |
81 | |
82 void IDBValueWrapper::WriteVarint(unsigned value, Vector<char>& output) { | |
83 // Writes an unsigned integer as a base-128 varint. | |
84 // The number is written, 7 bits at a time, from the least significant to | |
85 // the most significant 7 bits. Each byte, except the last, has the MSB set. | |
86 // See also https://developers.google.com/protocol-buffers/docs/encoding | |
87 do { | |
88 output.push_back((value & 0x7F) | 0x80); | |
89 value >>= 7; | |
90 } while (value); | |
91 output.back() &= 0x7F; | |
92 } | |
93 | |
94 void IDBValueWrapper::WriteAsciiString(const String& value, | |
95 Vector<char>& output) { | |
96 DCHECK(value.Is8Bit() && value.ContainsOnlyASCII()); | |
97 | |
98 IDBValueWrapper::WriteVarint(value.length(), output); | |
99 output.Append(value.Characters8(), value.length()); | |
100 } | |
101 | |
102 bool IDBValueWrapper::WrapIfBiggerThan(unsigned max_bytes) { | |
103 #if DCHECK_IS_ON() | |
104 DCHECK(!had_exception_) << __FUNCTION__ | |
105 << " called on wrapper with serialization exception"; | |
106 DCHECK(!wrap_called_) << __FUNCTION__ << " called twice on the same wrapper"; | |
107 wrap_called_ = true; | |
108 #endif // DCHECK_IS_ON() | |
109 | |
110 serialized_value_->ToWireBytes(wire_bytes_); | |
111 if (wire_bytes_.size() <= max_bytes) | |
112 return false; | |
113 | |
114 // TODO(pwnall): The MIME type should probably be an atomic string. | |
115 String mime_type(kWrapMimeType); | |
116 // TODO(crbug.com/721516): Use WebBlobRegistry::CreateBuilder instead of | |
117 // Blob::Create to avoid a buffer copy. | |
118 Blob* wrapper = | |
119 Blob::Create(reinterpret_cast<unsigned char*>(wire_bytes_.data()), | |
120 wire_bytes_.size(), mime_type); | |
121 | |
122 wrapper_handle_ = std::move(wrapper->GetBlobDataHandle()); | |
123 blob_info_.emplace_back(wrapper_handle_->Uuid(), wrapper_handle_->GetType(), | |
124 wrapper->size()); | |
125 | |
126 wire_bytes_.clear(); | |
127 | |
128 // Version 17 of SSV always writes a V8 envelope after the Blink envelope, so | |
129 // its output starts with 0xFF 0x11 0xFF. Therefore, we can use 0xFF 0x11 0xvv | |
130 // as an escape prefix, for 0x00 <= 0xvv < 0xFF. | |
131 wire_bytes_.push_back(kVersionTag); | |
132 wire_bytes_.push_back(kRequiresProcessingSSVPseudoVersion); | |
133 wire_bytes_.push_back(kBlobWrappedValue); | |
134 IDBValueWrapper::WriteVarint(wrapper->size(), wire_bytes_); | |
135 IDBValueWrapper::WriteAsciiString(wrapper->Uuid(), wire_bytes_); | |
136 return true; | |
137 } | |
138 | |
139 void IDBValueWrapper::ExtractBlobDataHandles( | |
140 Vector<RefPtr<BlobDataHandle>>* blob_data_handles) { | |
141 for (const auto& kvp : serialized_value_->BlobDataHandles()) | |
142 blob_data_handles->push_back(kvp.value); | |
143 if (wrapper_handle_) | |
144 blob_data_handles->push_back(std::move(wrapper_handle_)); | |
145 } | |
146 | |
147 RefPtr<SharedBuffer> IDBValueWrapper::ExtractWireBytes() { | |
148 #if DCHECK_IS_ON() | |
149 DCHECK(!had_exception_) << __FUNCTION__ | |
150 << " called on wrapper with serialization exception"; | |
151 #endif // DCHECK_IS_ON() | |
152 | |
153 return SharedBuffer::AdoptVector(wire_bytes_); | |
154 } | |
155 | |
156 IDBValueUnwrapper::IDBValueUnwrapper() { | |
157 Reset(); | |
158 } | |
159 | |
160 bool IDBValueUnwrapper::IsWrapped(IDBValue* value) { | |
161 DCHECK(value); | |
162 | |
163 uint8_t header[3]; | |
164 if (!value->data_ || value->data_->size() < sizeof(header)) | |
165 return false; | |
166 | |
167 value->data_->GetPartAsBytes(header, static_cast<size_t>(0), sizeof(header)); | |
168 return header[0] == kVersionTag && | |
169 header[1] == kRequiresProcessingSSVPseudoVersion && | |
170 header[2] == kBlobWrappedValue; | |
171 } | |
172 | |
173 RefPtr<IDBValue> IDBValueUnwrapper::CreateUnwrapped( | |
174 IDBValue* wrapped_value, | |
175 RefPtr<SharedBuffer>&& wrapper_blob_content) { | |
176 DCHECK(wrapped_value); | |
177 DCHECK(wrapped_value->data_); | |
178 DCHECK_GT(wrapped_value->blob_info_->size(), 0U); | |
179 DCHECK_EQ(wrapped_value->blob_info_->size(), | |
180 wrapped_value->blob_data_->size()); | |
181 | |
182 // Create an IDBValue with the same blob information, minus the last blob. | |
183 unsigned blob_count = wrapped_value->BlobInfo()->size() - 1; | |
184 std::unique_ptr<Vector<RefPtr<BlobDataHandle>>> blob_data = | |
185 WTF::MakeUnique<Vector<RefPtr<BlobDataHandle>>>(); | |
186 blob_data->ReserveCapacity(blob_count); | |
187 std::unique_ptr<Vector<WebBlobInfo>> blob_info = | |
188 WTF::MakeUnique<Vector<WebBlobInfo>>(); | |
189 blob_info->ReserveCapacity(blob_count); | |
190 | |
191 for (unsigned i = 0; i < blob_count; ++i) { | |
192 blob_data->push_back((*wrapped_value->blob_data_)[i]); | |
193 blob_info->push_back((*wrapped_value->blob_info_)[i]); | |
194 } | |
195 | |
196 return AdoptRef(new IDBValue(std::move(wrapper_blob_content), | |
197 std::move(blob_data), std::move(blob_info))); | |
198 } | |
199 | |
200 bool IDBValueUnwrapper::Parse(IDBValue* value) { | |
201 // Fast path that avoids unnecessary dynamic allocations. | |
202 if (!IDBValueUnwrapper::IsWrapped(value)) | |
203 return false; | |
204 | |
205 const uint8_t* data = reinterpret_cast<const uint8_t*>(value->data_->Data()); | |
206 end_ = data + value->data_->size(); | |
207 current_ = data + 3; | |
208 | |
209 String blob_uuid; | |
210 if (!ReadVarint(blob_size_)) | |
211 return Reset(); | |
212 if (!ReadAsciiString(blob_uuid)) | |
213 return Reset(); | |
214 | |
215 blob_handle_ = value->blob_data_->back(); | |
216 | |
217 // TODO(pwnall): Blobs seem to get different UUIDs when they're resurrected? | |
jsbell
2017/05/15 23:37:38
Did you verify this or ... ?
pwnall
2017/05/19 18:27:35
Done.
I just finished verifying this claim. Detail
| |
218 // If this is true, stashing the UUID is useless. | |
219 if (blob_handle_->size() != blob_size_) | |
220 return Reset(); | |
221 | |
222 return true; | |
223 } | |
224 | |
225 RefPtr<BlobDataHandle> IDBValueUnwrapper::WrapperBlobHandle() { | |
226 DCHECK(blob_handle_); | |
227 | |
228 return std::move(blob_handle_); | |
229 } | |
230 | |
231 bool IDBValueUnwrapper::ReadVarint(unsigned& value) { | |
232 value = 0; | |
233 unsigned shift = 0; | |
234 bool has_another_byte; | |
235 do { | |
236 if (current_ >= end_) | |
237 return false; | |
238 | |
239 if (shift >= sizeof(unsigned) * 8) | |
240 return false; | |
241 uint8_t byte = *current_; | |
242 ++current_; | |
243 value |= static_cast<unsigned>(byte & 0x7F) << shift; | |
244 shift += 7; | |
245 | |
246 has_another_byte = byte & 0x80; | |
247 } while (has_another_byte); | |
248 return true; | |
249 } | |
250 | |
251 bool IDBValueUnwrapper::ReadAsciiString(String& value) { | |
252 unsigned length; | |
253 if (!ReadVarint(length)) | |
254 return false; | |
255 | |
256 DCHECK_LE(current_, end_); | |
257 if (end_ - current_ < static_cast<ptrdiff_t>(length)) | |
258 return false; | |
259 String output(current_, length); | |
260 value.swap(output); | |
261 current_ += length; | |
262 return true; | |
263 } | |
264 | |
265 } // namespace blink | |
OLD | NEW |