1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_PICKLE_H_ 6 #define BASE_PICKLE_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <optional> 12 #include <string> 13 #include <string_view> 14 15 #include "base/base_export.h" 16 #include "base/check_op.h" 17 #include "base/containers/span.h" 18 #include "base/gtest_prod_util.h" 19 #include "base/memory/raw_ptr_exclusion.h" 20 #include "base/memory/ref_counted.h" 21 #include "base/strings/string_piece.h" 22 23 namespace base { 24 25 class Pickle; 26 27 // PickleIterator reads data from a Pickle. The Pickle object must remain valid 28 // while the PickleIterator object is in use. 29 class BASE_EXPORT PickleIterator { 30 public: PickleIterator()31 PickleIterator() : payload_(nullptr), read_index_(0), end_index_(0) {} 32 explicit PickleIterator(const Pickle& pickle); 33 34 // Methods for reading the payload of the Pickle. To read from the start of 35 // the Pickle, create a PickleIterator from a Pickle. If successful, these 36 // methods return true. Otherwise, false is returned to indicate that the 37 // result could not be extracted. It is not possible to read from the iterator 38 // after that. 39 [[nodiscard]] bool ReadBool(bool* result); 40 [[nodiscard]] bool ReadInt(int* result); 41 [[nodiscard]] bool ReadLong(long* result); 42 [[nodiscard]] bool ReadUInt16(uint16_t* result); 43 [[nodiscard]] bool ReadUInt32(uint32_t* result); 44 [[nodiscard]] bool ReadInt64(int64_t* result); 45 [[nodiscard]] bool ReadUInt64(uint64_t* result); 46 [[nodiscard]] bool ReadFloat(float* result); 47 [[nodiscard]] bool ReadDouble(double* result); 48 [[nodiscard]] bool ReadString(std::string* result); 49 // The StringPiece data will only be valid for the lifetime of the message. 50 [[nodiscard]] bool ReadStringPiece(StringPiece* result); 51 [[nodiscard]] bool ReadString16(std::u16string* result); 52 // The StringPiece16 data will only be valid for the lifetime of the message. 53 [[nodiscard]] bool ReadStringPiece16(StringPiece16* result); 54 55 // A pointer to the data will be placed in |*data|, and the length will be 56 // placed in |*length|. The pointer placed into |*data| points into the 57 // message's buffer so it will be scoped to the lifetime of the message (or 58 // until the message data is mutated). Do not keep the pointer around! 59 [[nodiscard]] bool ReadData(const char** data, size_t* length); 60 61 // Similar, but using base::span for convenience. 62 [[nodiscard]] std::optional<base::span<const uint8_t>> ReadData(); 63 64 // A pointer to the data will be placed in |*data|. The caller specifies the 65 // number of bytes to read, and ReadBytes will validate this length. The 66 // pointer placed into |*data| points into the message's buffer so it will be 67 // scoped to the lifetime of the message (or until the message data is 68 // mutated). Do not keep the pointer around! 69 [[nodiscard]] bool ReadBytes(const char** data, size_t length); 70 71 // A version of ReadInt() that checks for the result not being negative. Use 72 // it for reading the object sizes. ReadLength(size_t * result)73 [[nodiscard]] bool ReadLength(size_t* result) { 74 int result_int; 75 if (!ReadInt(&result_int) || result_int < 0) 76 return false; 77 *result = static_cast<size_t>(result_int); 78 return true; 79 } 80 81 // Skips bytes in the read buffer and returns true if there are at least 82 // num_bytes available. Otherwise, does nothing and returns false. SkipBytes(size_t num_bytes)83 [[nodiscard]] bool SkipBytes(size_t num_bytes) { 84 return !!GetReadPointerAndAdvance(num_bytes); 85 } 86 ReachedEnd()87 bool ReachedEnd() const { return read_index_ == end_index_; } 88 89 private: 90 // Read Type from Pickle. 91 template <typename Type> 92 bool ReadBuiltinType(Type* result); 93 94 // Advance read_index_ but do not allow it to exceed end_index_. 95 // Keeps read_index_ aligned. 96 void Advance(size_t size); 97 98 // Get read pointer for Type and advance read pointer. 99 template<typename Type> 100 const char* GetReadPointerAndAdvance(); 101 102 // Get read pointer for |num_bytes| and advance read pointer. This method 103 // checks num_bytes for wrapping. 104 const char* GetReadPointerAndAdvance(size_t num_bytes); 105 106 // Get read pointer for (num_elements * size_element) bytes and advance read 107 // pointer. This method checks for overflow and wrapping. 108 const char* GetReadPointerAndAdvance(size_t num_elements, 109 size_t size_element); 110 111 const char* payload_; // Start of our pickle's payload. 112 size_t read_index_; // Offset of the next readable byte in payload. 113 size_t end_index_; // Payload size. 114 115 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance); 116 }; 117 118 // This class provides facilities for basic binary value packing and unpacking. 119 // 120 // The Pickle class supports appending primitive values (ints, strings, etc.) 121 // to a pickle instance. The Pickle instance grows its internal memory buffer 122 // dynamically to hold the sequence of primitive values. The internal memory 123 // buffer is exposed as the "data" of the Pickle. This "data" can be passed 124 // to a Pickle object to initialize it for reading. 125 // 126 // When reading from a Pickle object, it is important for the consumer to know 127 // what value types to read and in what order to read them as the Pickle does 128 // not keep track of the type of data written to it. 129 // 130 // The Pickle's data has a header which contains the size of the Pickle's 131 // payload. It can optionally support additional space in the header. That 132 // space is controlled by the header_size parameter passed to the Pickle 133 // constructor. 134 // 135 class BASE_EXPORT Pickle { 136 public: 137 // Auxiliary data attached to a Pickle. Pickle must be subclassed along with 138 // this interface in order to provide a concrete implementation of support 139 // for attachments. The base Pickle implementation does not accept 140 // attachments. 141 class BASE_EXPORT Attachment : public RefCountedThreadSafe<Attachment> { 142 public: 143 Attachment(); 144 Attachment(const Attachment&) = delete; 145 Attachment& operator=(const Attachment&) = delete; 146 147 protected: 148 friend class RefCountedThreadSafe<Attachment>; 149 virtual ~Attachment(); 150 }; 151 152 // Initialize a Pickle object using the default header size. 153 Pickle(); 154 155 // Initialize a Pickle object with the specified header size in bytes, which 156 // must be greater-than-or-equal-to `sizeof(Pickle::Header)`. The header size 157 // will be rounded up to ensure that the header size is 32bit-aligned. Note 158 // that the extra memory allocated due to the size difference between the 159 // requested header size and the size of a standard header is not initialized. 160 explicit Pickle(size_t header_size); 161 162 // Returns a Pickle initialized from a block of data. The Pickle obtained by 163 // this call makes a copy of the data from which it is initialized, so it is 164 // safe to pass around without concern for the pointer to the original data 165 // dangling. The header padding size is deduced from the data length. 166 static Pickle WithData(span<const uint8_t> data); 167 168 // Returns a Pickle initialized from a const block of data. The data is not 169 // copied, only referenced, which can be dangerous; please only use this 170 // initialization when the speed gain of not copying the data outweighs the 171 // danger of dangling pointers. If a Pickle is obtained from this call, it is 172 // a requirement that only const methods be called. The header padding size is 173 // deduced from the data length. 174 static Pickle WithUnownedBuffer(span<const uint8_t> data); 175 176 // Initializes a Pickle as a copy of another Pickle. If the original Pickle's 177 // data is unowned, the copy will have its own internalized copy of the data. 178 Pickle(const Pickle& other); 179 180 // Note: Other classes are derived from this class, and they may well 181 // delete through this parent class, e.g. std::unique_ptr<Pickle> exists 182 // in several places the code. 183 virtual ~Pickle(); 184 185 // Performs a deep copy. 186 Pickle& operator=(const Pickle& other); 187 188 // Returns the number of bytes written in the Pickle, including the header. size()189 size_t size() const { 190 return header_ ? header_size_ + header_->payload_size : 0; 191 } 192 193 // Returns the data for this Pickle. data()194 const uint8_t* data() const { 195 return reinterpret_cast<const uint8_t*>(header_); 196 } 197 198 // Handy method to simplify calling data() with a reinterpret_cast. data_as_char()199 const char* data_as_char() const { 200 return reinterpret_cast<const char*>(data()); 201 } 202 203 // Returns the effective memory capacity of this Pickle, that is, the total 204 // number of bytes currently dynamically allocated or 0 in the case of a 205 // read-only Pickle. This should be used only for diagnostic / profiling 206 // purposes. 207 size_t GetTotalAllocatedSize() const; 208 209 // Methods for adding to the payload of the Pickle. These values are 210 // appended to the end of the Pickle's payload. When reading values from a 211 // Pickle, it is important to read them in the order in which they were added 212 // to the Pickle. 213 WriteBool(bool value)214 void WriteBool(bool value) { WriteInt(value ? 1 : 0); } WriteInt(int value)215 void WriteInt(int value) { WritePOD(value); } WriteLong(long value)216 void WriteLong(long value) { 217 // Always write long as a 64-bit value to ensure compatibility between 218 // 32-bit and 64-bit processes. 219 WritePOD(static_cast<int64_t>(value)); 220 } WriteUInt16(uint16_t value)221 void WriteUInt16(uint16_t value) { WritePOD(value); } WriteUInt32(uint32_t value)222 void WriteUInt32(uint32_t value) { WritePOD(value); } WriteInt64(int64_t value)223 void WriteInt64(int64_t value) { WritePOD(value); } WriteUInt64(uint64_t value)224 void WriteUInt64(uint64_t value) { WritePOD(value); } WriteFloat(float value)225 void WriteFloat(float value) { WritePOD(value); } WriteDouble(double value)226 void WriteDouble(double value) { WritePOD(value); } 227 void WriteString(const StringPiece& value); 228 void WriteString16(const StringPiece16& value); 229 // "Data" is a blob with a length. When you read it out you will be given the 230 // length. See also WriteBytes. 231 // TODO(https://crbug.com/40284755): Migrate callers to the span versions. 232 void WriteData(const char* data, size_t length); 233 void WriteData(span<const uint8_t> data); 234 void WriteData(std::string_view data); 235 // "Bytes" is a blob with no length. The caller must specify the length both 236 // when reading and writing. It is normally used to serialize PoD types of a 237 // known size. See also WriteData. 238 // TODO(https://crbug.com/40284755): Migrate callers to the span version. 239 void WriteBytes(const void* data, size_t length); 240 void WriteBytes(span<const uint8_t> data); 241 242 // WriteAttachment appends |attachment| to the pickle. It returns 243 // false iff the set is full or if the Pickle implementation does not support 244 // attachments. 245 virtual bool WriteAttachment(scoped_refptr<Attachment> attachment); 246 247 // ReadAttachment parses an attachment given the parsing state |iter| and 248 // writes it to |*attachment|. It returns true on success. 249 virtual bool ReadAttachment(base::PickleIterator* iter, 250 scoped_refptr<Attachment>* attachment) const; 251 252 // Indicates whether the pickle has any attachments. 253 virtual bool HasAttachments() const; 254 255 // Reserves space for upcoming writes when multiple writes will be made and 256 // their sizes are computed in advance. It can be significantly faster to call 257 // Reserve() before calling WriteFoo() multiple times. 258 void Reserve(size_t additional_capacity); 259 260 // Payload follows after allocation of Header (header size is customizable). 261 struct Header { 262 uint32_t payload_size; // Specifies the size of the payload. 263 }; 264 265 // Returns the header, cast to a user-specified type T. The type T must be a 266 // subclass of Header and its size must correspond to the header_size passed 267 // to the Pickle constructor. 268 template <class T> headerT()269 T* headerT() { 270 DCHECK_EQ(header_size_, sizeof(T)); 271 return static_cast<T*>(header_); 272 } 273 template <class T> headerT()274 const T* headerT() const { 275 DCHECK_EQ(header_size_, sizeof(T)); 276 return static_cast<const T*>(header_); 277 } 278 279 // The payload is the pickle data immediately following the header. payload_size()280 size_t payload_size() const { 281 return header_ ? header_->payload_size : 0; 282 } 283 payload_bytes()284 base::span<const uint8_t> payload_bytes() const { 285 return base::as_bytes(base::make_span(payload(), payload_size())); 286 } 287 288 protected: 289 // The protected constructor. Note that this creates a Pickle that does not 290 // own its own data. 291 enum UnownedData { kUnownedData }; 292 explicit Pickle(UnownedData, span<const uint8_t> data); 293 294 // Returns size of the header, which can have default value, set by user or 295 // calculated by passed raw data. header_size()296 size_t header_size() const { return header_size_; } 297 payload()298 const char* payload() const { 299 return reinterpret_cast<const char*>(header_) + header_size_; 300 } 301 302 // Returns the address of the byte immediately following the currently valid 303 // header + payload. end_of_payload()304 const char* end_of_payload() const { 305 // This object may be invalid. 306 return header_ ? payload() + payload_size() : NULL; 307 } 308 mutable_payload()309 char* mutable_payload() { 310 return reinterpret_cast<char*>(header_) + header_size_; 311 } 312 capacity_after_header()313 size_t capacity_after_header() const { 314 return capacity_after_header_; 315 } 316 317 // Resize the capacity, note that the input value should not include the size 318 // of the header. 319 void Resize(size_t new_capacity); 320 321 // Claims |num_bytes| bytes of payload. This is similar to Reserve() in that 322 // it may grow the capacity, but it also advances the write offset of the 323 // pickle by |num_bytes|. Claimed memory, including padding, is zeroed. 324 // 325 // Returns the address of the first byte claimed. 326 void* ClaimBytes(size_t num_bytes); 327 328 // Find the end of the pickled data that starts at range_start. Returns NULL 329 // if the entire Pickle is not found in the given data range. 330 static const char* FindNext(size_t header_size, 331 const char* range_start, 332 const char* range_end); 333 334 // Parse pickle header and return total size of the pickle. Data range 335 // doesn't need to contain entire pickle. 336 // Returns true if pickle header was found and parsed. Callers must check 337 // returned |pickle_size| for sanity (against maximum message size, etc). 338 // NOTE: when function successfully parses a header, but encounters an 339 // overflow during pickle size calculation, it sets |pickle_size| to the 340 // maximum size_t value and returns true. 341 static bool PeekNext(size_t header_size, 342 const char* range_start, 343 const char* range_end, 344 size_t* pickle_size); 345 346 // The allocation granularity of the payload. 347 static const size_t kPayloadUnit; 348 349 private: 350 friend class PickleIterator; 351 352 // `header_` is not a raw_ptr<...> for performance reasons (based on analysis 353 // of sampling profiler data). 354 RAW_PTR_EXCLUSION Header* header_; 355 size_t header_size_; // Supports extra data between header and payload. 356 // Allocation size of payload (or -1 if allocation is const). Note: this 357 // doesn't count the header. 358 size_t capacity_after_header_; 359 // The offset at which we will write the next field. Note: this doesn't count 360 // the header. 361 size_t write_offset_; 362 363 // Just like WriteBytes, but with a compile-time size, for performance. 364 template<size_t length> void BASE_EXPORT WriteBytesStatic(const void* data); 365 366 // Writes a POD by copying its bytes. WritePOD(const T & data)367 template <typename T> bool WritePOD(const T& data) { 368 WriteBytesStatic<sizeof(data)>(&data); 369 return true; 370 } 371 372 inline void* ClaimUninitializedBytesInternal(size_t num_bytes); 373 inline void WriteBytesCommon(span<const uint8_t> data); 374 375 FRIEND_TEST_ALL_PREFIXES(PickleTest, DeepCopyResize); 376 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); 377 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); 378 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); 379 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); 380 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); 381 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); 382 }; 383 384 } // namespace base 385 386 #endif // BASE_PICKLE_H_ 387