blob: cfb316c13e56974abc7ffb98fa7e4693e40a7301 [file] [log] [blame]
[email protected]ce208f872012-03-07 20:42:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
initial.commitd7cae122008-07-26 21:49:385#include "base/pickle.h"
6
[email protected]c9046af2008-08-06 20:35:177#include <stdlib.h>
8
[email protected]b7a5d992009-10-28 04:21:019#include <algorithm> // for max()
dskiba6f3790a2015-09-30 17:24:3010#include <limits>
[email protected]c9046af2008-08-06 20:35:1711
primiano32a7f502015-07-24 20:13:3212#include "base/bits.h"
13#include "base/macros.h"
jam03d8a782016-02-10 20:13:3914#include "base/numerics/safe_conversions.h"
avi9b6f42932015-12-26 22:15:1415#include "build/build_config.h"
primiano32a7f502015-07-24 20:13:3216
brettwa89dd422015-06-03 16:20:1417namespace base {
[email protected]476dafb2013-12-03 00:39:2618
initial.commitd7cae122008-07-26 21:49:3819// static
20const int Pickle::kPayloadUnit = 64;
21
[email protected]9193d2f2011-10-10 22:20:3322static const size_t kCapacityReadOnly = static_cast<size_t>(-1);
[email protected]836061b2008-08-13 14:57:5123
[email protected]ce208f872012-03-07 20:42:5624PickleIterator::PickleIterator(const Pickle& pickle)
[email protected]a15016f2014-06-02 23:23:4925 : payload_(pickle.payload()),
26 read_index_(0),
27 end_index_(pickle.payload_size()) {
[email protected]ce208f872012-03-07 20:42:5628}
29
30template <typename Type>
31inline bool PickleIterator::ReadBuiltinType(Type* result) {
32 const char* read_from = GetReadPointerAndAdvance<Type>();
33 if (!read_from)
34 return false;
avic0279142015-12-04 22:38:5235 if (sizeof(Type) > sizeof(uint32_t))
[email protected]ce208f872012-03-07 20:42:5636 memcpy(result, read_from, sizeof(*result));
37 else
38 *result = *reinterpret_cast<const Type*>(read_from);
39 return true;
40}
41
[email protected]a15016f2014-06-02 23:23:4942inline void PickleIterator::Advance(size_t size) {
primiano32a7f502015-07-24 20:13:3243 size_t aligned_size = bits::Align(size, sizeof(uint32_t));
[email protected]a15016f2014-06-02 23:23:4944 if (end_index_ - read_index_ < aligned_size) {
45 read_index_ = end_index_;
46 } else {
47 read_index_ += aligned_size;
48 }
49}
50
[email protected]ce208f872012-03-07 20:42:5651template<typename Type>
52inline const char* PickleIterator::GetReadPointerAndAdvance() {
[email protected]a15016f2014-06-02 23:23:4953 if (sizeof(Type) > end_index_ - read_index_) {
54 read_index_ = end_index_;
[email protected]ce208f872012-03-07 20:42:5655 return NULL;
[email protected]a15016f2014-06-02 23:23:4956 }
57 const char* current_read_ptr = payload_ + read_index_;
58 Advance(sizeof(Type));
[email protected]ce208f872012-03-07 20:42:5659 return current_read_ptr;
60}
61
62const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
[email protected]a15016f2014-06-02 23:23:4963 if (num_bytes < 0 ||
64 end_index_ - read_index_ < static_cast<size_t>(num_bytes)) {
65 read_index_ = end_index_;
[email protected]2d936052012-03-13 17:17:5666 return NULL;
[email protected]a15016f2014-06-02 23:23:4967 }
68 const char* current_read_ptr = payload_ + read_index_;
69 Advance(num_bytes);
[email protected]ce208f872012-03-07 20:42:5670 return current_read_ptr;
71}
72
[email protected]a15016f2014-06-02 23:23:4973inline const char* PickleIterator::GetReadPointerAndAdvance(
74 int num_elements,
75 size_t size_element) {
avi9b6f42932015-12-26 22:15:1476 // Check for int32_t overflow.
avic0279142015-12-04 22:38:5277 int64_t num_bytes = static_cast<int64_t>(num_elements) * size_element;
[email protected]ce208f872012-03-07 20:42:5678 int num_bytes32 = static_cast<int>(num_bytes);
avic0279142015-12-04 22:38:5279 if (num_bytes != static_cast<int64_t>(num_bytes32))
[email protected]ce208f872012-03-07 20:42:5680 return NULL;
81 return GetReadPointerAndAdvance(num_bytes32);
82}
83
84bool PickleIterator::ReadBool(bool* result) {
85 return ReadBuiltinType(result);
86}
87
88bool PickleIterator::ReadInt(int* result) {
89 return ReadBuiltinType(result);
90}
91
92bool PickleIterator::ReadLong(long* result) {
jam03d8a782016-02-10 20:13:3993 // Always read long as a 64-bit value to ensure compatibility between 32-bit
94 // and 64-bit processes.
95 int64_t result_int64 = 0;
96 if (!ReadBuiltinType(&result_int64))
97 return false;
98 // CHECK if the cast truncates the value so that we know to change this IPC
99 // parameter to use int64_t.
100 *result = base::checked_cast<long>(result_int64);
101 return true;
[email protected]ce208f872012-03-07 20:42:56102}
103
avic0279142015-12-04 22:38:52104bool PickleIterator::ReadUInt16(uint16_t* result) {
[email protected]ce208f872012-03-07 20:42:56105 return ReadBuiltinType(result);
106}
107
avic0279142015-12-04 22:38:52108bool PickleIterator::ReadUInt32(uint32_t* result) {
[email protected]ce208f872012-03-07 20:42:56109 return ReadBuiltinType(result);
110}
111
avic0279142015-12-04 22:38:52112bool PickleIterator::ReadInt64(int64_t* result) {
[email protected]ce208f872012-03-07 20:42:56113 return ReadBuiltinType(result);
114}
115
avic0279142015-12-04 22:38:52116bool PickleIterator::ReadUInt64(uint64_t* result) {
[email protected]ce208f872012-03-07 20:42:56117 return ReadBuiltinType(result);
118}
119
[email protected]b1f61b032012-11-28 15:40:58120bool PickleIterator::ReadFloat(float* result) {
[email protected]522fbea2013-11-18 00:50:25121 // crbug.com/315213
122 // The source data may not be properly aligned, and unaligned float reads
123 // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data
124 // into the result.
125 const char* read_from = GetReadPointerAndAdvance<float>();
126 if (!read_from)
127 return false;
128 memcpy(result, read_from, sizeof(*result));
129 return true;
[email protected]b1f61b032012-11-28 15:40:58130}
131
[email protected]915cc7d2014-07-14 22:50:32132bool PickleIterator::ReadDouble(double* result) {
133 // crbug.com/315213
134 // The source data may not be properly aligned, and unaligned double reads
135 // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data
136 // into the result.
137 const char* read_from = GetReadPointerAndAdvance<double>();
138 if (!read_from)
139 return false;
140 memcpy(result, read_from, sizeof(*result));
141 return true;
142}
143
[email protected]ce208f872012-03-07 20:42:56144bool PickleIterator::ReadString(std::string* result) {
145 int len;
146 if (!ReadInt(&len))
147 return false;
148 const char* read_from = GetReadPointerAndAdvance(len);
149 if (!read_from)
150 return false;
151
152 result->assign(read_from, len);
153 return true;
154}
155
brettwa89dd422015-06-03 16:20:14156bool PickleIterator::ReadStringPiece(StringPiece* result) {
brucedawsoneaa38962015-03-10 01:46:50157 int len;
158 if (!ReadInt(&len))
159 return false;
160 const char* read_from = GetReadPointerAndAdvance(len);
161 if (!read_from)
162 return false;
163
brettwa89dd422015-06-03 16:20:14164 *result = StringPiece(read_from, len);
brucedawsoneaa38962015-03-10 01:46:50165 return true;
166}
167
[email protected]ce208f872012-03-07 20:42:56168bool PickleIterator::ReadString16(string16* result) {
169 int len;
170 if (!ReadInt(&len))
171 return false;
172 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16));
173 if (!read_from)
174 return false;
175
176 result->assign(reinterpret_cast<const char16*>(read_from), len);
177 return true;
178}
179
brettwa89dd422015-06-03 16:20:14180bool PickleIterator::ReadStringPiece16(StringPiece16* result) {
brucedawsoneaa38962015-03-10 01:46:50181 int len;
182 if (!ReadInt(&len))
183 return false;
184 const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16));
185 if (!read_from)
186 return false;
187
brettwa89dd422015-06-03 16:20:14188 *result = StringPiece16(reinterpret_cast<const char16*>(read_from), len);
brucedawsoneaa38962015-03-10 01:46:50189 return true;
190}
191
[email protected]ce208f872012-03-07 20:42:56192bool PickleIterator::ReadData(const char** data, int* length) {
193 *length = 0;
194 *data = 0;
195
196 if (!ReadInt(length))
197 return false;
198
199 return ReadBytes(data, *length);
200}
201
202bool PickleIterator::ReadBytes(const char** data, int length) {
203 const char* read_from = GetReadPointerAndAdvance(length);
204 if (!read_from)
205 return false;
206 *data = read_from;
207 return true;
208}
209
rockot0457af102016-02-05 02:12:32210PickleSizer::PickleSizer() {}
211
212PickleSizer::~PickleSizer() {}
213
214void PickleSizer::AddString(const StringPiece& value) {
215 AddInt();
216 AddBytes(static_cast<int>(value.size()));
217}
218
219void PickleSizer::AddString16(const StringPiece16& value) {
220 AddInt();
221 AddBytes(static_cast<int>(value.size() * sizeof(char16)));
222}
223
224void PickleSizer::AddData(int length) {
225 CHECK_GE(length, 0);
226 AddInt();
227 AddBytes(length);
228}
229
230void PickleSizer::AddBytes(int length) {
231 payload_size_ += bits::Align(length, sizeof(uint32_t));
232}
233
jam45eceef2016-05-11 21:05:05234void PickleSizer::AddAttachment() {
235 // From IPC::Message::WriteAttachment
236 AddBool();
237 AddInt();
238}
239
rockot0457af102016-02-05 02:12:32240template <size_t length> void PickleSizer::AddBytesStatic() {
241 DCHECK_LE(length, static_cast<size_t>(std::numeric_limits<int>::max()));
242 AddBytes(length);
243}
244
245template void PickleSizer::AddBytesStatic<2>();
246template void PickleSizer::AddBytesStatic<4>();
247template void PickleSizer::AddBytesStatic<8>();
248
rockot502c94f2016-02-03 20:20:16249Pickle::Attachment::Attachment() {}
250
251Pickle::Attachment::~Attachment() {}
252
avic0279142015-12-04 22:38:52253// Payload is uint32_t aligned.
initial.commitd7cae122008-07-26 21:49:38254
255Pickle::Pickle()
256 : header_(NULL),
257 header_size_(sizeof(Header)),
[email protected]d1b319fc2013-10-31 04:03:02258 capacity_after_header_(0),
259 write_offset_(0) {
primiano32a7f502015-07-24 20:13:32260 static_assert((Pickle::kPayloadUnit & (Pickle::kPayloadUnit - 1)) == 0,
261 "Pickle::kPayloadUnit must be a power of two");
initial.commitd7cae122008-07-26 21:49:38262 Resize(kPayloadUnit);
263 header_->payload_size = 0;
264}
265
266Pickle::Pickle(int header_size)
267 : header_(NULL),
avic0279142015-12-04 22:38:52268 header_size_(bits::Align(header_size, sizeof(uint32_t))),
[email protected]d1b319fc2013-10-31 04:03:02269 capacity_after_header_(0),
270 write_offset_(0) {
[email protected]8e03fec2011-03-31 20:34:25271 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header));
[email protected]d7a93ad2011-04-22 13:13:07272 DCHECK_LE(header_size, kPayloadUnit);
initial.commitd7cae122008-07-26 21:49:38273 Resize(kPayloadUnit);
274 header_->payload_size = 0;
275}
276
[email protected]753bb252013-11-04 22:28:12277Pickle::Pickle(const char* data, int data_len)
initial.commitd7cae122008-07-26 21:49:38278 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
[email protected]d87f8e6f2010-11-15 19:31:23279 header_size_(0),
[email protected]d1b319fc2013-10-31 04:03:02280 capacity_after_header_(kCapacityReadOnly),
281 write_offset_(0) {
[email protected]753bb252013-11-04 22:28:12282 if (data_len >= static_cast<int>(sizeof(Header)))
[email protected]d87f8e6f2010-11-15 19:31:23283 header_size_ = data_len - header_->payload_size;
284
[email protected]753bb252013-11-04 22:28:12285 if (header_size_ > static_cast<unsigned int>(data_len))
[email protected]d87f8e6f2010-11-15 19:31:23286 header_size_ = 0;
287
avic0279142015-12-04 22:38:52288 if (header_size_ != bits::Align(header_size_, sizeof(uint32_t)))
[email protected]d87f8e6f2010-11-15 19:31:23289 header_size_ = 0;
290
291 // If there is anything wrong with the data, we're not going to use it.
292 if (!header_size_)
293 header_ = NULL;
initial.commitd7cae122008-07-26 21:49:38294}
295
296Pickle::Pickle(const Pickle& other)
297 : header_(NULL),
298 header_size_(other.header_size_),
[email protected]d1b319fc2013-10-31 04:03:02299 capacity_after_header_(0),
300 write_offset_(other.write_offset_) {
erikchenf9ca8f5f02015-09-08 23:36:29301 Resize(other.header_->payload_size);
302 memcpy(header_, other.header_, header_size_ + other.header_->payload_size);
initial.commitd7cae122008-07-26 21:49:38303}
304
305Pickle::~Pickle() {
[email protected]d1b319fc2013-10-31 04:03:02306 if (capacity_after_header_ != kCapacityReadOnly)
initial.commitd7cae122008-07-26 21:49:38307 free(header_);
308}
309
310Pickle& Pickle::operator=(const Pickle& other) {
[email protected]b944f612009-08-07 23:13:35311 if (this == &other) {
312 NOTREACHED();
313 return *this;
314 }
[email protected]d1b319fc2013-10-31 04:03:02315 if (capacity_after_header_ == kCapacityReadOnly) {
[email protected]fb6ec9992009-08-03 07:01:47316 header_ = NULL;
[email protected]d1b319fc2013-10-31 04:03:02317 capacity_after_header_ = 0;
[email protected]fb6ec9992009-08-03 07:01:47318 }
319 if (header_size_ != other.header_size_) {
initial.commitd7cae122008-07-26 21:49:38320 free(header_);
321 header_ = NULL;
322 header_size_ = other.header_size_;
323 }
[email protected]d1b319fc2013-10-31 04:03:02324 Resize(other.header_->payload_size);
[email protected]b944f612009-08-07 23:13:35325 memcpy(header_, other.header_,
326 other.header_size_ + other.header_->payload_size);
[email protected]d1b319fc2013-10-31 04:03:02327 write_offset_ = other.write_offset_;
initial.commitd7cae122008-07-26 21:49:38328 return *this;
329}
330
brettwa89dd422015-06-03 16:20:14331bool Pickle::WriteString(const StringPiece& value) {
initial.commitd7cae122008-07-26 21:49:38332 if (!WriteInt(static_cast<int>(value.size())))
333 return false;
334
335 return WriteBytes(value.data(), static_cast<int>(value.size()));
336}
337
brettwa89dd422015-06-03 16:20:14338bool Pickle::WriteString16(const StringPiece16& value) {
[email protected]3a2a5d22009-03-04 03:36:36339 if (!WriteInt(static_cast<int>(value.size())))
340 return false;
341
342 return WriteBytes(value.data(),
343 static_cast<int>(value.size()) * sizeof(char16));
initial.commitd7cae122008-07-26 21:49:38344}
345
346bool Pickle::WriteData(const char* data, int length) {
[email protected]e64ff5e2009-07-28 21:00:03347 return length >= 0 && WriteInt(length) && WriteBytes(data, length);
initial.commitd7cae122008-07-26 21:49:38348}
349
[email protected]d1b319fc2013-10-31 04:03:02350bool Pickle::WriteBytes(const void* data, int length) {
351 WriteBytesCommon(data, length);
[email protected]9989c9bb2011-01-07 20:23:43352 return true;
353}
354
[email protected]d1b319fc2013-10-31 04:03:02355void Pickle::Reserve(size_t length) {
avic0279142015-12-04 22:38:52356 size_t data_len = bits::Align(length, sizeof(uint32_t));
[email protected]d1b319fc2013-10-31 04:03:02357 DCHECK_GE(data_len, length);
[email protected]9989c9bb2011-01-07 20:23:43358#ifdef ARCH_CPU_64_BITS
avic0279142015-12-04 22:38:52359 DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max());
[email protected]9989c9bb2011-01-07 20:23:43360#endif
avic0279142015-12-04 22:38:52361 DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len);
[email protected]d1b319fc2013-10-31 04:03:02362 size_t new_size = write_offset_ + data_len;
363 if (new_size > capacity_after_header_)
364 Resize(capacity_after_header_ * 2 + new_size);
[email protected]9989c9bb2011-01-07 20:23:43365}
366
rockot502c94f2016-02-03 20:20:16367bool Pickle::WriteAttachment(scoped_refptr<Attachment> attachment) {
368 return false;
369}
370
371bool Pickle::ReadAttachment(base::PickleIterator* iter,
372 scoped_refptr<Attachment>* attachment) const {
373 return false;
374}
375
376bool Pickle::HasAttachments() const {
377 return false;
378}
379
[email protected]d1b319fc2013-10-31 04:03:02380void Pickle::Resize(size_t new_capacity) {
[email protected]d1b319fc2013-10-31 04:03:02381 CHECK_NE(capacity_after_header_, kCapacityReadOnly);
primiano32a7f502015-07-24 20:13:32382 capacity_after_header_ = bits::Align(new_capacity, kPayloadUnit);
primiano9882cf342015-06-11 21:40:10383 void* p = realloc(header_, GetTotalAllocatedSize());
[email protected]d1b319fc2013-10-31 04:03:02384 CHECK(p);
initial.commitd7cae122008-07-26 21:49:38385 header_ = reinterpret_cast<Header*>(p);
primiano9882cf342015-06-11 21:40:10386}
387
rockot0776a502015-12-17 06:19:49388void* Pickle::ClaimBytes(size_t num_bytes) {
389 void* p = ClaimUninitializedBytesInternal(num_bytes);
390 CHECK(p);
391 memset(p, 0, num_bytes);
392 return p;
393}
394
primiano9882cf342015-06-11 21:40:10395size_t Pickle::GetTotalAllocatedSize() const {
396 if (capacity_after_header_ == kCapacityReadOnly)
397 return 0;
398 return header_size_ + capacity_after_header_;
initial.commitd7cae122008-07-26 21:49:38399}
400
401// static
402const char* Pickle::FindNext(size_t header_size,
403 const char* start,
404 const char* end) {
dskiba6f3790a2015-09-30 17:24:30405 size_t pickle_size = 0;
406 if (!PeekNext(header_size, start, end, &pickle_size))
407 return NULL;
408
409 if (pickle_size > static_cast<size_t>(end - start))
410 return NULL;
411
412 return start + pickle_size;
413}
414
415// static
416bool Pickle::PeekNext(size_t header_size,
417 const char* start,
418 const char* end,
419 size_t* pickle_size) {
avic0279142015-12-04 22:38:52420 DCHECK_EQ(header_size, bits::Align(header_size, sizeof(uint32_t)));
dskiba6f3790a2015-09-30 17:24:30421 DCHECK_GE(header_size, sizeof(Header));
[email protected]d7a93ad2011-04-22 13:13:07422 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
initial.commitd7cae122008-07-26 21:49:38423
[email protected]33a38dd2013-11-01 09:06:26424 size_t length = static_cast<size_t>(end - start);
425 if (length < sizeof(Header))
dskiba6f3790a2015-09-30 17:24:30426 return false;
[email protected]137d2372011-01-26 13:02:27427
initial.commitd7cae122008-07-26 21:49:38428 const Header* hdr = reinterpret_cast<const Header*>(start);
dskiba6f3790a2015-09-30 17:24:30429 if (length < header_size)
430 return false;
431
432 if (hdr->payload_size > std::numeric_limits<size_t>::max() - header_size) {
433 // If payload_size causes an overflow, we return maximum possible
434 // pickle size to indicate that.
435 *pickle_size = std::numeric_limits<size_t>::max();
436 } else {
437 *pickle_size = header_size + hdr->payload_size;
438 }
439 return true;
initial.commitd7cae122008-07-26 21:49:38440}
[email protected]d1b319fc2013-10-31 04:03:02441
442template <size_t length> void Pickle::WriteBytesStatic(const void* data) {
443 WriteBytesCommon(data, length);
444}
445
446template void Pickle::WriteBytesStatic<2>(const void* data);
447template void Pickle::WriteBytesStatic<4>(const void* data);
448template void Pickle::WriteBytesStatic<8>(const void* data);
449
rockot0776a502015-12-17 06:19:49450inline void* Pickle::ClaimUninitializedBytesInternal(size_t length) {
[email protected]d1b319fc2013-10-31 04:03:02451 DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
452 << "oops: pickle is readonly";
avic0279142015-12-04 22:38:52453 size_t data_len = bits::Align(length, sizeof(uint32_t));
[email protected]d1b319fc2013-10-31 04:03:02454 DCHECK_GE(data_len, length);
455#ifdef ARCH_CPU_64_BITS
avic0279142015-12-04 22:38:52456 DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max());
[email protected]d1b319fc2013-10-31 04:03:02457#endif
avic0279142015-12-04 22:38:52458 DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len);
[email protected]d1b319fc2013-10-31 04:03:02459 size_t new_size = write_offset_ + data_len;
460 if (new_size > capacity_after_header_) {
primiano32a7f502015-07-24 20:13:32461 size_t new_capacity = capacity_after_header_ * 2;
462 const size_t kPickleHeapAlign = 4096;
463 if (new_capacity > kPickleHeapAlign)
464 new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit;
465 Resize(std::max(new_capacity, new_size));
[email protected]d1b319fc2013-10-31 04:03:02466 }
467
468 char* write = mutable_payload() + write_offset_;
rockot0776a502015-12-17 06:19:49469 memset(write + length, 0, data_len - length); // Always initialize padding
avic0279142015-12-04 22:38:52470 header_->payload_size = static_cast<uint32_t>(new_size);
[email protected]d1b319fc2013-10-31 04:03:02471 write_offset_ = new_size;
rockot0776a502015-12-17 06:19:49472 return write;
473}
474
475inline void Pickle::WriteBytesCommon(const void* data, size_t length) {
476 DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
477 << "oops: pickle is readonly";
478 MSAN_CHECK_MEM_IS_INITIALIZED(data, length);
479 void* write = ClaimUninitializedBytesInternal(length);
480 memcpy(write, data, length);
[email protected]d1b319fc2013-10-31 04:03:02481}
brettwa89dd422015-06-03 16:20:14482
483} // namespace base