1 // Copyright 2011 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 // memory_range.h: Define the google_breakpad::MemoryRange class, which 30 // is a lightweight wrapper with a pointer and a length to encapsulate 31 // a contiguous range of memory. 32 33 #ifndef COMMON_MEMORY_RANGE_H_ 34 #define COMMON_MEMORY_RANGE_H_ 35 36 #include <stddef.h> 37 38 #include "google_breakpad/common/breakpad_types.h" 39 40 namespace google_breakpad { 41 42 // A lightweight wrapper with a pointer and a length to encapsulate a 43 // contiguous range of memory. It provides helper methods for checked 44 // access of a subrange of the memory. Its implemementation does not 45 // allocate memory or call into libc functions, and is thus safer to use 46 // in a crashed environment. 47 class MemoryRange { 48 public: MemoryRange()49 MemoryRange() : data_(NULL), length_(0) {} 50 MemoryRange(const void * data,size_t length)51 MemoryRange(const void* data, size_t length) { 52 Set(data, length); 53 } 54 55 // Returns true if this memory range contains no data. IsEmpty()56 bool IsEmpty() const { 57 // Set() guarantees that |length_| is zero if |data_| is NULL. 58 return length_ == 0; 59 } 60 61 // Resets to an empty range. Reset()62 void Reset() { 63 data_ = NULL; 64 length_ = 0; 65 } 66 67 // Sets this memory range to point to |data| and its length to |length|. Set(const void * data,size_t length)68 void Set(const void* data, size_t length) { 69 data_ = reinterpret_cast<const uint8_t*>(data); 70 // Always set |length_| to zero if |data_| is NULL. 71 length_ = data ? length : 0; 72 } 73 74 // Returns true if this range covers a subrange of |sub_length| bytes 75 // at |sub_offset| bytes of this memory range, or false otherwise. Covers(size_t sub_offset,size_t sub_length)76 bool Covers(size_t sub_offset, size_t sub_length) const { 77 // The following checks verify that: 78 // 1. sub_offset is within [ 0 .. length_ - 1 ] 79 // 2. sub_offset + sub_length is within 80 // [ sub_offset .. length_ ] 81 return sub_offset < length_ && 82 sub_offset + sub_length >= sub_offset && 83 sub_offset + sub_length <= length_; 84 } 85 86 // Returns a raw data pointer to a subrange of |sub_length| bytes at 87 // |sub_offset| bytes of this memory range, or NULL if the subrange 88 // is out of bounds. GetData(size_t sub_offset,size_t sub_length)89 const void* GetData(size_t sub_offset, size_t sub_length) const { 90 return Covers(sub_offset, sub_length) ? (data_ + sub_offset) : NULL; 91 } 92 93 // Same as the two-argument version of GetData() but uses sizeof(DataType) 94 // as the subrange length and returns an |DataType| pointer for convenience. 95 template <typename DataType> GetData(size_t sub_offset)96 const DataType* GetData(size_t sub_offset) const { 97 return reinterpret_cast<const DataType*>( 98 GetData(sub_offset, sizeof(DataType))); 99 } 100 101 // Returns a raw pointer to the |element_index|-th element of an array 102 // of elements of length |element_size| starting at |sub_offset| bytes 103 // of this memory range, or NULL if the element is out of bounds. GetArrayElement(size_t element_offset,size_t element_size,unsigned element_index)104 const void* GetArrayElement(size_t element_offset, 105 size_t element_size, 106 unsigned element_index) const { 107 size_t sub_offset = element_offset + element_index * element_size; 108 return GetData(sub_offset, element_size); 109 } 110 111 // Same as the three-argument version of GetArrayElement() but deduces 112 // the element size using sizeof(ElementType) and returns an |ElementType| 113 // pointer for convenience. 114 template <typename ElementType> GetArrayElement(size_t element_offset,unsigned element_index)115 const ElementType* GetArrayElement(size_t element_offset, 116 unsigned element_index) const { 117 return reinterpret_cast<const ElementType*>( 118 GetArrayElement(element_offset, sizeof(ElementType), element_index)); 119 } 120 121 // Returns a subrange of |sub_length| bytes at |sub_offset| bytes of 122 // this memory range, or an empty range if the subrange is out of bounds. Subrange(size_t sub_offset,size_t sub_length)123 MemoryRange Subrange(size_t sub_offset, size_t sub_length) const { 124 return Covers(sub_offset, sub_length) ? 125 MemoryRange(data_ + sub_offset, sub_length) : MemoryRange(); 126 } 127 128 // Returns a pointer to the beginning of this memory range. data()129 const uint8_t* data() const { return data_; } 130 131 // Returns the length, in bytes, of this memory range. length()132 size_t length() const { return length_; } 133 134 private: 135 // Pointer to the beginning of this memory range. 136 const uint8_t* data_; 137 138 // Length, in bytes, of this memory range. 139 size_t length_; 140 }; 141 142 } // namespace google_breakpad 143 144 #endif // COMMON_MEMORY_RANGE_H_ 145