1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/public/common/messaging/string_message_codec.h"
#include <memory>
#include <string>
#include <variant>
#include <vector>
#include "base/check_op.h"
#include "base/containers/buffer_iterator.h"
#include "base/containers/span.h"
#include "base/functional/overloaded.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/numerics/checked_math.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "third_party/blink/public/mojom/array_buffer/array_buffer_contents.mojom.h"
namespace blink {
namespace {
// An ArrayBufferPayload impl based on std::vector.
class VectorArrayBuffer : public WebMessageArrayBufferPayload {
public:
VectorArrayBuffer(std::vector<uint8_t> data, size_t position, size_t length)
: data_(std::move(data)), position_(position), length_(length) {
size_t size = base::CheckAdd(position_, length_).ValueOrDie();
CHECK_GE(data_.size(), size);
}
size_t GetLength() const override { return length_; }
bool GetIsResizableByUserJavaScript() const override {
// VectorArrayBuffers are not used for ArrayBuffer transfers and are
// currently always fixed-length. Structured cloning resizables ArrayBuffers
// is not yet supported in SMC.
return false;
}
size_t GetMaxByteLength() const override { return length_; }
std::optional<base::span<const uint8_t>> GetAsSpanIfPossible()
const override {
return AsSpan();
}
void CopyInto(base::span<uint8_t> dest) const override {
dest.copy_from(AsSpan());
}
private:
base::span<const uint8_t> AsSpan() const {
return base::span(data_).subspan(position_, length_);
}
std::vector<uint8_t> data_;
size_t position_;
size_t length_;
};
// An ArrayBufferPayload impl based on mojo::BigBuffer.
class BigBufferArrayBuffer : public WebMessageArrayBufferPayload {
public:
explicit BigBufferArrayBuffer(mojo_base::BigBuffer data,
std::optional<size_t> max_byte_length)
: data_(std::move(data)), max_byte_length_(max_byte_length) {
DCHECK(!max_byte_length || *max_byte_length >= GetLength());
}
size_t GetLength() const override { return data_.size(); }
bool GetIsResizableByUserJavaScript() const override {
return max_byte_length_.has_value();
}
size_t GetMaxByteLength() const override {
return max_byte_length_.value_or(GetLength());
}
std::optional<base::span<const uint8_t>> GetAsSpanIfPossible()
const override {
return base::span(data_);
}
void CopyInto(base::span<uint8_t> dest) const override {
dest.copy_from(base::span(data_));
}
private:
mojo_base::BigBuffer data_;
std::optional<size_t> max_byte_length_;
};
const uint32_t kVarIntShift = 7;
const uint32_t kVarIntMask = (1 << kVarIntShift) - 1;
const uint8_t kVersionTag = 0xFF;
const uint8_t kPaddingTag = '\0';
// serialization_tag, see v8/src/objects/value-serializer.cc
const uint8_t kOneByteStringTag = '"';
const uint8_t kTwoByteStringTag = 'c';
const uint8_t kArrayBuffer = 'B';
const uint8_t kArrayBufferTransferTag = 't';
const uint32_t kVersion = 10;
static size_t BytesNeededForUint32(uint32_t value) {
size_t result = 0;
do {
result++;
value >>= kVarIntShift;
} while (value);
return result;
}
void WriteUint8(uint8_t value, std::vector<uint8_t>* buffer) {
buffer->push_back(value);
}
void WriteUint32(uint32_t value, std::vector<uint8_t>* buffer) {
for (;;) {
uint8_t b = (value & kVarIntMask);
value >>= kVarIntShift;
if (!value) {
WriteUint8(b, buffer);
break;
}
WriteUint8(b | (1 << kVarIntShift), buffer);
}
}
void WriteBytes(base::span<const uint8_t> bytes, std::vector<uint8_t>* buffer) {
buffer->insert(buffer->end(), bytes.begin(), bytes.end());
}
bool ReadUint8(base::BufferIterator<const uint8_t>& iter, uint8_t* value) {
if (const uint8_t* ptr = iter.Object<uint8_t>()) {
*value = *ptr;
return true;
}
return false;
}
bool ReadUint32(base::BufferIterator<const uint8_t>& iter, uint32_t* value) {
*value = 0;
uint8_t current_byte;
int shift = 0;
do {
if (!ReadUint8(iter, ¤t_byte))
return false;
*value |= (static_cast<uint32_t>(current_byte & kVarIntMask) << shift);
shift += kVarIntShift;
} while (current_byte & (1 << kVarIntShift));
return true;
}
bool ContainsOnlyLatin1(const std::u16string& data) {
char16_t x = 0;
for (char16_t c : data)
x |= c;
return !(x & 0xFF00);
}
} // namespace
// static
std::unique_ptr<WebMessageArrayBufferPayload>
WebMessageArrayBufferPayload::CreateFromBigBuffer(
mojo_base::BigBuffer buffer,
std::optional<size_t> max_byte_length) {
return std::make_unique<BigBufferArrayBuffer>(std::move(buffer),
max_byte_length);
}
// static
std::unique_ptr<WebMessageArrayBufferPayload>
WebMessageArrayBufferPayload::CreateForTesting(std::vector<uint8_t> data) {
auto size = data.size();
return std::make_unique<VectorArrayBuffer>(std::move(data), 0, size);
}
TransferableMessage EncodeWebMessagePayload(const WebMessagePayload& payload) {
TransferableMessage message;
std::vector<uint8_t> buffer;
WriteUint8(kVersionTag, &buffer);
WriteUint32(kVersion, &buffer);
std::visit(
base::Overloaded{
[&](const std::u16string& str) {
if (ContainsOnlyLatin1(str)) {
std::string data_latin1(str.cbegin(), str.cend());
WriteUint8(kOneByteStringTag, &buffer);
WriteUint32(data_latin1.size(), &buffer);
WriteBytes(base::as_byte_span(data_latin1), &buffer);
} else {
auto str_as_bytes = base::as_byte_span(str);
if ((buffer.size() + 1 +
BytesNeededForUint32(str_as_bytes.size())) &
1) {
WriteUint8(kPaddingTag, &buffer);
}
WriteUint8(kTwoByteStringTag, &buffer);
WriteUint32(str_as_bytes.size(), &buffer);
WriteBytes(str_as_bytes, &buffer);
}
},
[&](const std::unique_ptr<WebMessageArrayBufferPayload>&
array_buffer) {
WriteUint8(kArrayBufferTransferTag, &buffer);
// Write at the first slot.
WriteUint32(0, &buffer);
mojo_base::BigBuffer big_buffer(array_buffer->GetLength());
array_buffer->CopyInto(base::span(big_buffer));
message.array_buffer_contents_array.push_back(
mojom::SerializedArrayBufferContents::New(
std::move(big_buffer),
array_buffer->GetIsResizableByUserJavaScript(),
array_buffer->GetMaxByteLength()));
}},
payload);
message.owned_encoded_message = std::move(buffer);
message.encoded_message = message.owned_encoded_message;
return message;
}
std::optional<WebMessagePayload> DecodeToWebMessagePayload(
TransferableMessage message) {
base::BufferIterator<const uint8_t> iter(message.encoded_message);
uint8_t tag;
// Discard the outer envelope, including trailer info if applicable.
if (!ReadUint8(iter, &tag))
return std::nullopt;
if (tag == kVersionTag) {
uint32_t version = 0;
if (!ReadUint32(iter, &version))
return std::nullopt;
static constexpr uint32_t kMinWireFormatVersionWithTrailer = 21;
if (version >= kMinWireFormatVersionWithTrailer) {
// In these versions, we expect kTrailerOffsetTag (0xFE) followed by an
// offset and size. See details in
// third_party/blink/renderer/core/v8/serialization/serialization_tag.h.
auto span = iter.Span<uint8_t>(1 + sizeof(uint64_t) + sizeof(uint32_t));
if (span.empty() || span[0] != 0xFE)
return std::nullopt;
}
if (!ReadUint8(iter, &tag))
return std::nullopt;
}
// Discard any leading version and padding tags.
while (tag == kVersionTag || tag == kPaddingTag) {
uint32_t version;
if (tag == kVersionTag && !ReadUint32(iter, &version))
return std::nullopt;
if (!ReadUint8(iter, &tag))
return std::nullopt;
}
switch (tag) {
case kOneByteStringTag: {
// Use of unsigned char rather than char here matters, so that Latin-1
// characters are zero-extended rather than sign-extended
uint32_t num_bytes;
if (!ReadUint32(iter, &num_bytes))
return std::nullopt;
auto span = iter.Span<unsigned char>(num_bytes / sizeof(unsigned char));
std::u16string str(span.begin(), span.end());
return span.size_bytes() == num_bytes
? std::make_optional(WebMessagePayload(std::move(str)))
: std::nullopt;
}
case kTwoByteStringTag: {
uint32_t num_bytes;
if (!ReadUint32(iter, &num_bytes))
return std::nullopt;
auto span = iter.Span<char16_t>(num_bytes / sizeof(char16_t));
std::u16string str(span.begin(), span.end());
return span.size_bytes() == num_bytes
? std::make_optional(WebMessagePayload(std::move(str)))
: std::nullopt;
}
case kArrayBuffer: {
uint32_t num_bytes;
if (!ReadUint32(iter, &num_bytes))
return std::nullopt;
size_t position = iter.position();
return position + num_bytes == iter.total_size()
? std::make_optional(
WebMessagePayload(std::make_unique<VectorArrayBuffer>(
std::move(message.owned_encoded_message), position,
num_bytes)))
: std::nullopt;
}
case kArrayBufferTransferTag: {
uint32_t array_buffer_index;
if (!ReadUint32(iter, &array_buffer_index))
return std::nullopt;
// We only support transfer ArrayBuffer at the first index.
if (array_buffer_index != 0)
return std::nullopt;
if (message.array_buffer_contents_array.size() != 1)
return std::nullopt;
auto& array_buffer_contents = message.array_buffer_contents_array[0];
std::optional<size_t> max_byte_length;
if (array_buffer_contents->is_resizable_by_user_javascript) {
max_byte_length.emplace(array_buffer_contents->max_byte_length);
}
return std::make_optional(
WebMessagePayload(std::make_unique<BigBufferArrayBuffer>(
std::move(array_buffer_contents->contents), max_byte_length)));
}
}
DLOG(WARNING) << "Unexpected tag: " << tag;
return std::nullopt;
}
} // namespace blink
|