• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 "base/pickle.h"
6 
7 #include <stdlib.h>
8 
9 #include <algorithm>  // for max()
10 #include <limits>
11 
12 #include "base/bits.h"
13 #include "base/macros.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/numerics/safe_math.h"
16 #include "build/build_config.h"
17 
18 namespace base {
19 
20 // static
21 const int Pickle::kPayloadUnit = 64;
22 
23 static const size_t kCapacityReadOnly = static_cast<size_t>(-1);
24 
PickleIterator(const Pickle & pickle)25 PickleIterator::PickleIterator(const Pickle& pickle)
26     : payload_(pickle.payload()),
27       read_index_(0),
28       end_index_(pickle.payload_size()) {
29 }
30 
31 template <typename Type>
ReadBuiltinType(Type * result)32 inline bool PickleIterator::ReadBuiltinType(Type* result) {
33   const char* read_from = GetReadPointerAndAdvance<Type>();
34   if (!read_from)
35     return false;
36   if (sizeof(Type) > sizeof(uint32_t))
37     memcpy(result, read_from, sizeof(*result));
38   else
39     *result = *reinterpret_cast<const Type*>(read_from);
40   return true;
41 }
42 
Advance(size_t size)43 inline void PickleIterator::Advance(size_t size) {
44   size_t aligned_size = bits::Align(size, sizeof(uint32_t));
45   if (end_index_ - read_index_ < aligned_size) {
46     read_index_ = end_index_;
47   } else {
48     read_index_ += aligned_size;
49   }
50 }
51 
52 template<typename Type>
GetReadPointerAndAdvance()53 inline const char* PickleIterator::GetReadPointerAndAdvance() {
54   if (sizeof(Type) > end_index_ - read_index_) {
55     read_index_ = end_index_;
56     return nullptr;
57   }
58   const char* current_read_ptr = payload_ + read_index_;
59   Advance(sizeof(Type));
60   return current_read_ptr;
61 }
62 
GetReadPointerAndAdvance(int num_bytes)63 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
64   if (num_bytes < 0 ||
65       end_index_ - read_index_ < static_cast<size_t>(num_bytes)) {
66     read_index_ = end_index_;
67     return nullptr;
68   }
69   const char* current_read_ptr = payload_ + read_index_;
70   Advance(num_bytes);
71   return current_read_ptr;
72 }
73 
GetReadPointerAndAdvance(int num_elements,size_t size_element)74 inline const char* PickleIterator::GetReadPointerAndAdvance(
75     int num_elements,
76     size_t size_element) {
77   // Check for int32_t overflow.
78   int num_bytes;
79   if (!CheckMul(num_elements, size_element).AssignIfValid(&num_bytes))
80     return nullptr;
81   return GetReadPointerAndAdvance(num_bytes);
82 }
83 
ReadBool(bool * result)84 bool PickleIterator::ReadBool(bool* result) {
85   return ReadBuiltinType(result);
86 }
87 
ReadInt(int * result)88 bool PickleIterator::ReadInt(int* result) {
89   return ReadBuiltinType(result);
90 }
91 
ReadLong(long * result)92 bool PickleIterator::ReadLong(long* result) {
93   // 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;
102 }
103 
ReadUInt16(uint16_t * result)104 bool PickleIterator::ReadUInt16(uint16_t* result) {
105   return ReadBuiltinType(result);
106 }
107 
ReadUInt32(uint32_t * result)108 bool PickleIterator::ReadUInt32(uint32_t* result) {
109   return ReadBuiltinType(result);
110 }
111 
ReadInt64(int64_t * result)112 bool PickleIterator::ReadInt64(int64_t* result) {
113   return ReadBuiltinType(result);
114 }
115 
ReadUInt64(uint64_t * result)116 bool PickleIterator::ReadUInt64(uint64_t* result) {
117   return ReadBuiltinType(result);
118 }
119 
ReadFloat(float * result)120 bool PickleIterator::ReadFloat(float* result) {
121   // 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;
130 }
131 
ReadDouble(double * result)132 bool 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 
ReadString(std::string * result)144 bool 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 
ReadStringPiece(StringPiece * result)156 bool PickleIterator::ReadStringPiece(StringPiece* result) {
157   int len;
158   if (!ReadInt(&len))
159     return false;
160   const char* read_from = GetReadPointerAndAdvance(len);
161   if (!read_from)
162     return false;
163 
164   *result = StringPiece(read_from, len);
165   return true;
166 }
167 
ReadString16(string16 * result)168 bool 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 
ReadStringPiece16(StringPiece16 * result)180 bool PickleIterator::ReadStringPiece16(StringPiece16* result) {
181   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 
188   *result = StringPiece16(reinterpret_cast<const char16*>(read_from), len);
189   return true;
190 }
191 
ReadData(const char ** data,int * length)192 bool PickleIterator::ReadData(const char** data, int* length) {
193   *length = 0;
194   *data = nullptr;
195 
196   if (!ReadInt(length))
197     return false;
198 
199   return ReadBytes(data, *length);
200 }
201 
ReadBytes(const char ** data,int length)202 bool 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 
210 Pickle::Attachment::Attachment() = default;
211 
212 Pickle::Attachment::~Attachment() = default;
213 
214 // Payload is uint32_t aligned.
215 
Pickle()216 Pickle::Pickle()
217     : header_(nullptr),
218       header_size_(sizeof(Header)),
219       capacity_after_header_(0),
220       write_offset_(0) {
221   static_assert((Pickle::kPayloadUnit & (Pickle::kPayloadUnit - 1)) == 0,
222                 "Pickle::kPayloadUnit must be a power of two");
223   Resize(kPayloadUnit);
224   header_->payload_size = 0;
225 }
226 
Pickle(int header_size)227 Pickle::Pickle(int header_size)
228     : header_(nullptr),
229       header_size_(bits::Align(header_size, sizeof(uint32_t))),
230       capacity_after_header_(0),
231       write_offset_(0) {
232   DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header));
233   DCHECK_LE(header_size, kPayloadUnit);
234   Resize(kPayloadUnit);
235   header_->payload_size = 0;
236 }
237 
Pickle(const char * data,int data_len)238 Pickle::Pickle(const char* data, int data_len)
239     : header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
240       header_size_(0),
241       capacity_after_header_(kCapacityReadOnly),
242       write_offset_(0) {
243   if (data_len >= static_cast<int>(sizeof(Header)))
244     header_size_ = data_len - header_->payload_size;
245 
246   if (header_size_ > static_cast<unsigned int>(data_len))
247     header_size_ = 0;
248 
249   if (header_size_ != bits::Align(header_size_, sizeof(uint32_t)))
250     header_size_ = 0;
251 
252   // If there is anything wrong with the data, we're not going to use it.
253   if (!header_size_)
254     header_ = nullptr;
255 }
256 
Pickle(const Pickle & other)257 Pickle::Pickle(const Pickle& other)
258     : header_(nullptr),
259       header_size_(other.header_size_),
260       capacity_after_header_(0),
261       write_offset_(other.write_offset_) {
262   Resize(other.header_->payload_size);
263   memcpy(header_, other.header_, header_size_ + other.header_->payload_size);
264 }
265 
~Pickle()266 Pickle::~Pickle() {
267   if (capacity_after_header_ != kCapacityReadOnly)
268     free(header_);
269 }
270 
operator =(const Pickle & other)271 Pickle& Pickle::operator=(const Pickle& other) {
272   if (this == &other) {
273     return *this;
274   }
275   if (capacity_after_header_ == kCapacityReadOnly) {
276     header_ = nullptr;
277     capacity_after_header_ = 0;
278   }
279   if (header_size_ != other.header_size_) {
280     free(header_);
281     header_ = nullptr;
282     header_size_ = other.header_size_;
283   }
284   Resize(other.header_->payload_size);
285   memcpy(header_, other.header_,
286          other.header_size_ + other.header_->payload_size);
287   write_offset_ = other.write_offset_;
288   return *this;
289 }
290 
WriteString(const StringPiece & value)291 void Pickle::WriteString(const StringPiece& value) {
292   WriteInt(static_cast<int>(value.size()));
293   WriteBytes(value.data(), static_cast<int>(value.size()));
294 }
295 
WriteString16(const StringPiece16 & value)296 void Pickle::WriteString16(const StringPiece16& value) {
297   WriteInt(static_cast<int>(value.size()));
298   WriteBytes(value.data(), static_cast<int>(value.size()) * sizeof(char16));
299 }
300 
WriteData(const char * data,int length)301 void Pickle::WriteData(const char* data, int length) {
302   DCHECK_GE(length, 0);
303   WriteInt(length);
304   WriteBytes(data, length);
305 }
306 
WriteBytes(const void * data,int length)307 void Pickle::WriteBytes(const void* data, int length) {
308   WriteBytesCommon(data, length);
309 }
310 
Reserve(size_t length)311 void Pickle::Reserve(size_t length) {
312   size_t data_len = bits::Align(length, sizeof(uint32_t));
313   DCHECK_GE(data_len, length);
314 #ifdef ARCH_CPU_64_BITS
315   DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max());
316 #endif
317   DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len);
318   size_t new_size = write_offset_ + data_len;
319   if (new_size > capacity_after_header_)
320     Resize(capacity_after_header_ * 2 + new_size);
321 }
322 
WriteAttachment(scoped_refptr<Attachment> attachment)323 bool Pickle::WriteAttachment(scoped_refptr<Attachment> attachment) {
324   return false;
325 }
326 
ReadAttachment(base::PickleIterator * iter,scoped_refptr<Attachment> * attachment) const327 bool Pickle::ReadAttachment(base::PickleIterator* iter,
328                             scoped_refptr<Attachment>* attachment) const {
329   return false;
330 }
331 
HasAttachments() const332 bool Pickle::HasAttachments() const {
333   return false;
334 }
335 
Resize(size_t new_capacity)336 void Pickle::Resize(size_t new_capacity) {
337   CHECK_NE(capacity_after_header_, kCapacityReadOnly);
338   capacity_after_header_ = bits::Align(new_capacity, kPayloadUnit);
339   void* p = realloc(header_, GetTotalAllocatedSize());
340   CHECK(p);
341   header_ = reinterpret_cast<Header*>(p);
342 }
343 
ClaimBytes(size_t num_bytes)344 void* Pickle::ClaimBytes(size_t num_bytes) {
345   void* p = ClaimUninitializedBytesInternal(num_bytes);
346   CHECK(p);
347   memset(p, 0, num_bytes);
348   return p;
349 }
350 
GetTotalAllocatedSize() const351 size_t Pickle::GetTotalAllocatedSize() const {
352   if (capacity_after_header_ == kCapacityReadOnly)
353     return 0;
354   return header_size_ + capacity_after_header_;
355 }
356 
357 // static
FindNext(size_t header_size,const char * start,const char * end)358 const char* Pickle::FindNext(size_t header_size,
359                              const char* start,
360                              const char* end) {
361   size_t pickle_size = 0;
362   if (!PeekNext(header_size, start, end, &pickle_size))
363     return nullptr;
364 
365   if (pickle_size > static_cast<size_t>(end - start))
366     return nullptr;
367 
368   return start + pickle_size;
369 }
370 
371 // static
PeekNext(size_t header_size,const char * start,const char * end,size_t * pickle_size)372 bool Pickle::PeekNext(size_t header_size,
373                       const char* start,
374                       const char* end,
375                       size_t* pickle_size) {
376   DCHECK_EQ(header_size, bits::Align(header_size, sizeof(uint32_t)));
377   DCHECK_GE(header_size, sizeof(Header));
378   DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
379 
380   size_t length = static_cast<size_t>(end - start);
381   if (length < sizeof(Header))
382     return false;
383 
384   const Header* hdr = reinterpret_cast<const Header*>(start);
385   if (length < header_size)
386     return false;
387 
388   // If payload_size causes an overflow, we return maximum possible
389   // pickle size to indicate that.
390   *pickle_size = ClampAdd(header_size, hdr->payload_size);
391   return true;
392 }
393 
WriteBytesStatic(const void * data)394 template <size_t length> void Pickle::WriteBytesStatic(const void* data) {
395   WriteBytesCommon(data, length);
396 }
397 
398 template void Pickle::WriteBytesStatic<2>(const void* data);
399 template void Pickle::WriteBytesStatic<4>(const void* data);
400 template void Pickle::WriteBytesStatic<8>(const void* data);
401 
ClaimUninitializedBytesInternal(size_t length)402 inline void* Pickle::ClaimUninitializedBytesInternal(size_t length) {
403   DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
404       << "oops: pickle is readonly";
405   size_t data_len = bits::Align(length, sizeof(uint32_t));
406   DCHECK_GE(data_len, length);
407 #ifdef ARCH_CPU_64_BITS
408   DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max());
409 #endif
410   DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len);
411   size_t new_size = write_offset_ + data_len;
412   if (new_size > capacity_after_header_) {
413     size_t new_capacity = capacity_after_header_ * 2;
414     const size_t kPickleHeapAlign = 4096;
415     if (new_capacity > kPickleHeapAlign)
416       new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit;
417     Resize(std::max(new_capacity, new_size));
418   }
419 
420   char* write = mutable_payload() + write_offset_;
421   memset(write + length, 0, data_len - length);  // Always initialize padding
422   header_->payload_size = static_cast<uint32_t>(new_size);
423   write_offset_ = new_size;
424   return write;
425 }
426 
WriteBytesCommon(const void * data,size_t length)427 inline void Pickle::WriteBytesCommon(const void* data, size_t length) {
428   DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
429       << "oops: pickle is readonly";
430   MSAN_CHECK_MEM_IS_INITIALIZED(data, length);
431   void* write = ClaimUninitializedBytesInternal(length);
432   memcpy(write, data, length);
433 }
434 
435 }  // namespace base
436