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