[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / pickle.cc
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/numerics/safe_conversions.h"
14 #include "base/numerics/safe_math.h"
15 #include "build/build_config.h"
16
17 namespace base {
18
19 // static
20 const int Pickle::kPayloadUnit = 64;
21
22 static const size_t kCapacityReadOnly = static_cast<size_t>(-1);
23
24 PickleIterator::PickleIterator(const Pickle& pickle)
25     : payload_(pickle.payload()),
26       read_index_(0),
27       end_index_(pickle.payload_size()) {
28 }
29
30 template <typename Type>
31 inline bool PickleIterator::ReadBuiltinType(Type* result) {
32   const char* read_from = GetReadPointerAndAdvance<Type>();
33   if (!read_from)
34     return false;
35   if (sizeof(Type) > sizeof(uint32_t))
36     memcpy(result, read_from, sizeof(*result));
37   else
38     *result = *reinterpret_cast<const Type*>(read_from);
39   return true;
40 }
41
42 inline void PickleIterator::Advance(size_t size) {
43   size_t aligned_size = bits::AlignUp(size, sizeof(uint32_t));
44   if (end_index_ - read_index_ < aligned_size) {
45     read_index_ = end_index_;
46   } else {
47     read_index_ += aligned_size;
48   }
49 }
50
51 template<typename Type>
52 inline const char* PickleIterator::GetReadPointerAndAdvance() {
53   if (sizeof(Type) > end_index_ - read_index_) {
54     read_index_ = end_index_;
55     return nullptr;
56   }
57   const char* current_read_ptr = payload_ + read_index_;
58   Advance(sizeof(Type));
59   return current_read_ptr;
60 }
61
62 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
63   if (num_bytes < 0 ||
64       end_index_ - read_index_ < static_cast<size_t>(num_bytes)) {
65     read_index_ = end_index_;
66     return nullptr;
67   }
68   const char* current_read_ptr = payload_ + read_index_;
69   Advance(num_bytes);
70   return current_read_ptr;
71 }
72
73 inline const char* PickleIterator::GetReadPointerAndAdvance(
74     int num_elements,
75     size_t size_element) {
76   // Check for int32_t overflow.
77   int num_bytes;
78   if (!CheckMul(num_elements, size_element).AssignIfValid(&num_bytes))
79     return nullptr;
80   return GetReadPointerAndAdvance(num_bytes);
81 }
82
83 bool PickleIterator::ReadBool(bool* result) {
84   return ReadBuiltinType(result);
85 }
86
87 bool PickleIterator::ReadInt(int* result) {
88   return ReadBuiltinType(result);
89 }
90
91 bool PickleIterator::ReadLong(long* result) {
92   // Always read long as a 64-bit value to ensure compatibility between 32-bit
93   // and 64-bit processes.
94   int64_t result_int64 = 0;
95   if (!ReadBuiltinType(&result_int64))
96     return false;
97   // CHECK if the cast truncates the value so that we know to change this IPC
98   // parameter to use int64_t.
99   *result = base::checked_cast<long>(result_int64);
100   return true;
101 }
102
103 bool PickleIterator::ReadUInt16(uint16_t* result) {
104   return ReadBuiltinType(result);
105 }
106
107 bool PickleIterator::ReadUInt32(uint32_t* result) {
108   return ReadBuiltinType(result);
109 }
110
111 bool PickleIterator::ReadInt64(int64_t* result) {
112   return ReadBuiltinType(result);
113 }
114
115 bool PickleIterator::ReadUInt64(uint64_t* result) {
116   return ReadBuiltinType(result);
117 }
118
119 bool PickleIterator::ReadFloat(float* result) {
120   // crbug.com/315213
121   // The source data may not be properly aligned, and unaligned float reads
122   // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data
123   // into the result.
124   const char* read_from = GetReadPointerAndAdvance<float>();
125   if (!read_from)
126     return false;
127   memcpy(result, read_from, sizeof(*result));
128   return true;
129 }
130
131 bool PickleIterator::ReadDouble(double* result) {
132   // crbug.com/315213
133   // The source data may not be properly aligned, and unaligned double reads
134   // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data
135   // into the result.
136   const char* read_from = GetReadPointerAndAdvance<double>();
137   if (!read_from)
138     return false;
139   memcpy(result, read_from, sizeof(*result));
140   return true;
141 }
142
143 bool PickleIterator::ReadString(std::string* result) {
144   int len;
145   if (!ReadInt(&len))
146     return false;
147   const char* read_from = GetReadPointerAndAdvance(len);
148   if (!read_from)
149     return false;
150
151   result->assign(read_from, len);
152   return true;
153 }
154
155 bool PickleIterator::ReadStringPiece(StringPiece* result) {
156   int len;
157   if (!ReadInt(&len))
158     return false;
159   const char* read_from = GetReadPointerAndAdvance(len);
160   if (!read_from)
161     return false;
162
163   *result = StringPiece(read_from, len);
164   return true;
165 }
166
167 bool PickleIterator::ReadString16(std::u16string* result) {
168   int len;
169   if (!ReadInt(&len))
170     return false;
171   const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16_t));
172   if (!read_from)
173     return false;
174
175   result->assign(reinterpret_cast<const char16_t*>(read_from), len);
176   return true;
177 }
178
179 bool PickleIterator::ReadStringPiece16(StringPiece16* result) {
180   int len;
181   if (!ReadInt(&len))
182     return false;
183   const char* read_from = GetReadPointerAndAdvance(len, sizeof(char16_t));
184   if (!read_from)
185     return false;
186
187   *result = StringPiece16(reinterpret_cast<const char16_t*>(read_from), len);
188   return true;
189 }
190
191 bool PickleIterator::ReadData(const char** data, int* length) {
192   *length = 0;
193   *data = nullptr;
194
195   if (!ReadInt(length))
196     return false;
197
198   return ReadBytes(data, *length);
199 }
200
201 bool PickleIterator::ReadData(base::span<const uint8_t>* data) {
202   const char* ptr;
203   int length;
204
205   if (!ReadData(&ptr, &length))
206     return false;
207
208   *data = base::as_bytes(base::make_span(ptr, length));
209   return true;
210 }
211
212 bool PickleIterator::ReadBytes(const char** data, int length) {
213   const char* read_from = GetReadPointerAndAdvance(length);
214   if (!read_from)
215     return false;
216   *data = read_from;
217   return true;
218 }
219
220 Pickle::Attachment::Attachment() = default;
221
222 Pickle::Attachment::~Attachment() = default;
223
224 // Payload is uint32_t aligned.
225
226 Pickle::Pickle()
227     : header_(nullptr),
228       header_size_(sizeof(Header)),
229       capacity_after_header_(0),
230       write_offset_(0) {
231   static_assert(base::bits::IsPowerOfTwo(Pickle::kPayloadUnit),
232                 "Pickle::kPayloadUnit must be a power of two");
233   Resize(kPayloadUnit);
234   header_->payload_size = 0;
235 }
236
237 Pickle::Pickle(int header_size)
238     : header_(nullptr),
239       header_size_(bits::AlignUp(header_size, sizeof(uint32_t))),
240       capacity_after_header_(0),
241       write_offset_(0) {
242   DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header));
243   DCHECK_LE(header_size, kPayloadUnit);
244   Resize(kPayloadUnit);
245   header_->payload_size = 0;
246 }
247
248 Pickle::Pickle(const char* data, size_t data_len)
249     : header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
250       header_size_(0),
251       capacity_after_header_(kCapacityReadOnly),
252       write_offset_(0) {
253   if (data_len >= static_cast<int>(sizeof(Header)))
254     header_size_ = data_len - header_->payload_size;
255
256   if (header_size_ > static_cast<unsigned int>(data_len))
257     header_size_ = 0;
258
259   if (header_size_ != bits::AlignUp(header_size_, sizeof(uint32_t)))
260     header_size_ = 0;
261
262   // If there is anything wrong with the data, we're not going to use it.
263   if (!header_size_)
264     header_ = nullptr;
265 }
266
267 Pickle::Pickle(const Pickle& other)
268     : header_(nullptr),
269       header_size_(other.header_size_),
270       capacity_after_header_(0),
271       write_offset_(other.write_offset_) {
272   Resize(other.header_->payload_size);
273   memcpy(header_, other.header_, header_size_ + other.header_->payload_size);
274 }
275
276 Pickle::~Pickle() {
277   if (capacity_after_header_ != kCapacityReadOnly)
278     free(header_);
279 }
280
281 Pickle& Pickle::operator=(const Pickle& other) {
282   if (this == &other) {
283     return *this;
284   }
285   if (capacity_after_header_ == kCapacityReadOnly) {
286     header_ = nullptr;
287     capacity_after_header_ = 0;
288   }
289   if (header_size_ != other.header_size_) {
290     free(header_);
291     header_ = nullptr;
292     header_size_ = other.header_size_;
293   }
294   Resize(other.header_->payload_size);
295   memcpy(header_, other.header_,
296          other.header_size_ + other.header_->payload_size);
297   write_offset_ = other.write_offset_;
298   return *this;
299 }
300
301 void Pickle::WriteString(const StringPiece& value) {
302   WriteInt(static_cast<int>(value.size()));
303   WriteBytes(value.data(), static_cast<int>(value.size()));
304 }
305
306 void Pickle::WriteString16(const StringPiece16& value) {
307   WriteInt(static_cast<int>(value.size()));
308   WriteBytes(value.data(), static_cast<int>(value.size()) * sizeof(char16_t));
309 }
310
311 void Pickle::WriteData(const char* data, int length) {
312   DCHECK_GE(length, 0);
313   WriteInt(length);
314   WriteBytes(data, length);
315 }
316
317 void Pickle::WriteBytes(const void* data, int length) {
318   WriteBytesCommon(data, length);
319 }
320
321 void Pickle::Reserve(size_t length) {
322   size_t data_len = bits::AlignUp(length, sizeof(uint32_t));
323   DCHECK_GE(data_len, length);
324 #ifdef ARCH_CPU_64_BITS
325   DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max());
326 #endif
327   DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len);
328   size_t new_size = write_offset_ + data_len;
329   if (new_size > capacity_after_header_)
330     Resize(capacity_after_header_ * 2 + new_size);
331 }
332
333 bool Pickle::WriteAttachment(scoped_refptr<Attachment> attachment) {
334   return false;
335 }
336
337 bool Pickle::ReadAttachment(base::PickleIterator* iter,
338                             scoped_refptr<Attachment>* attachment) const {
339   return false;
340 }
341
342 bool Pickle::HasAttachments() const {
343   return false;
344 }
345
346 void Pickle::Resize(size_t new_capacity) {
347   CHECK_NE(capacity_after_header_, kCapacityReadOnly);
348   capacity_after_header_ = bits::AlignUp(new_capacity, kPayloadUnit);
349   void* p = realloc(header_, GetTotalAllocatedSize());
350   CHECK(p);
351   header_ = reinterpret_cast<Header*>(p);
352 }
353
354 void* Pickle::ClaimBytes(size_t num_bytes) {
355   void* p = ClaimUninitializedBytesInternal(num_bytes);
356   CHECK(p);
357   memset(p, 0, num_bytes);
358   return p;
359 }
360
361 size_t Pickle::GetTotalAllocatedSize() const {
362   if (capacity_after_header_ == kCapacityReadOnly)
363     return 0;
364   return header_size_ + capacity_after_header_;
365 }
366
367 // static
368 const char* Pickle::FindNext(size_t header_size,
369                              const char* start,
370                              const char* end) {
371   size_t pickle_size = 0;
372   if (!PeekNext(header_size, start, end, &pickle_size))
373     return nullptr;
374
375   if (pickle_size > static_cast<size_t>(end - start))
376     return nullptr;
377
378   return start + pickle_size;
379 }
380
381 // static
382 bool Pickle::PeekNext(size_t header_size,
383                       const char* start,
384                       const char* end,
385                       size_t* pickle_size) {
386   DCHECK_EQ(header_size, bits::AlignUp(header_size, sizeof(uint32_t)));
387   DCHECK_GE(header_size, sizeof(Header));
388   DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
389
390   size_t length = static_cast<size_t>(end - start);
391   if (length < sizeof(Header))
392     return false;
393
394   const Header* hdr = reinterpret_cast<const Header*>(start);
395   if (length < header_size)
396     return false;
397
398   // If payload_size causes an overflow, we return maximum possible
399   // pickle size to indicate that.
400   *pickle_size = ClampAdd(header_size, hdr->payload_size);
401   return true;
402 }
403
404 template <size_t length> void Pickle::WriteBytesStatic(const void* data) {
405   WriteBytesCommon(data, length);
406 }
407
408 template void Pickle::WriteBytesStatic<2>(const void* data);
409 template void Pickle::WriteBytesStatic<4>(const void* data);
410 template void Pickle::WriteBytesStatic<8>(const void* data);
411
412 inline void* Pickle::ClaimUninitializedBytesInternal(size_t length) {
413   DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
414       << "oops: pickle is readonly";
415   size_t data_len = bits::AlignUp(length, sizeof(uint32_t));
416   DCHECK_GE(data_len, length);
417 #ifdef ARCH_CPU_64_BITS
418   DCHECK_LE(data_len, std::numeric_limits<uint32_t>::max());
419 #endif
420   DCHECK_LE(write_offset_, std::numeric_limits<uint32_t>::max() - data_len);
421   size_t new_size = write_offset_ + data_len;
422   if (new_size > capacity_after_header_) {
423     size_t new_capacity = capacity_after_header_ * 2;
424     const size_t kPickleHeapAlign = 4096;
425     if (new_capacity > kPickleHeapAlign) {
426       new_capacity =
427           bits::AlignUp(new_capacity, kPickleHeapAlign) - kPayloadUnit;
428     }
429     Resize(std::max(new_capacity, new_size));
430   }
431
432   char* write = mutable_payload() + write_offset_;
433   memset(write + length, 0, data_len - length);  // Always initialize padding
434   header_->payload_size = static_cast<uint32_t>(new_size);
435   write_offset_ = new_size;
436   return write;
437 }
438
439 inline void Pickle::WriteBytesCommon(const void* data, size_t length) {
440   DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
441       << "oops: pickle is readonly";
442   MSAN_CHECK_MEM_IS_INITIALIZED(data, length);
443   void* write = ClaimUninitializedBytesInternal(length);
444   memcpy(write, data, length);
445 }
446
447 }  // namespace base