| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #ifndef CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ | 15 #ifndef CRASHPAD_UTIL_NUMERIC_CHECKED_ADDRESS_RANGE_H_ |
| 16 #define CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ | 16 #define CRASHPAD_UTIL_NUMERIC_CHECKED_ADDRESS_RANGE_H_ |
| 17 | 17 |
| 18 #include <mach/mach.h> | 18 #include <stdint.h> |
| 19 | 19 |
| 20 #include "build/build_config.h" |
| 20 #include "util/numeric/checked_range.h" | 21 #include "util/numeric/checked_range.h" |
| 21 | 22 |
| 22 namespace crashpad { | 23 namespace crashpad { |
| 24 namespace internal { |
| 23 | 25 |
| 24 //! \brief Ensures that a range, composed of a base and a size, does not | 26 //! \brief Ensures that a range, composed of a base and a size, does not |
| 25 //! overflow the pointer type of the process it describes a range in. | 27 //! overflow the pointer type of the process it describes a range in. |
| 26 //! | 28 //! |
| 27 //! This class checks bases of type `mach_vm_address_t` and sizes of type | 29 //! This class checks bases of type `ValueType` and sizes of type `SizeType` |
| 28 //! `mach_vm_address_t` against a process whose pointer type is either 32 or 64 | 30 //! against a process whose pointer type is either 32 or 64 bits wide. |
| 29 //! bits wide. | |
| 30 //! | 31 //! |
| 31 //! Aside from varying the overall range on the basis of a process’ pointer type | 32 //! Aside from varying the overall range on the basis of a process’ pointer type |
| 32 //! width, this class functions very similarly to CheckedRange. | 33 //! width, this class functions very similarly to CheckedRange. |
| 33 class CheckedMachAddressRange { | 34 //! |
| 35 //! \sa CheckedMachAddressRange |
| 36 template <class ValueType, class SizeType> |
| 37 class CheckedAddressRangeGeneric { |
| 34 public: | 38 public: |
| 35 //! \brief Initializes a default range. | 39 //! \brief Initializes a default range. |
| 36 //! | 40 //! |
| 37 //! The default range has base 0, size 0, and appears to be from a 32-bit | 41 //! The default range has base 0, size 0, and appears to be from a 32-bit |
| 38 //! process. | 42 //! process. |
| 39 CheckedMachAddressRange(); | 43 CheckedAddressRangeGeneric(); |
| 40 | 44 |
| 41 //! \brief Initializes a range. | 45 //! \brief Initializes a range. |
| 42 //! | 46 //! |
| 43 //! See SetRange(). | 47 //! See SetRange(). |
| 44 CheckedMachAddressRange(bool is_64_bit, | 48 CheckedAddressRangeGeneric(bool is_64_bit, ValueType base, SizeType size); |
| 45 mach_vm_address_t base, | |
| 46 mach_vm_size_t size); | |
| 47 | 49 |
| 48 //! \brief Sets a range’s fields. | 50 //! \brief Sets a range’s fields. |
| 49 //! | 51 //! |
| 50 //! \param[in] is_64_bit `true` if \a base and \a size refer to addresses in a | 52 //! \param[in] is_64_bit `true` if \a base and \a size refer to addresses in a |
| 51 //! 64-bit process; `false` if they refer to addresses in a 32-bit | 53 //! 64-bit process; `false` if they refer to addresses in a 32-bit |
| 52 //! process. | 54 //! process. |
| 53 //! \param[in] base The range’s base address. | 55 //! \param[in] base The range’s base address. |
| 54 //! \param[in] size The range’s size. | 56 //! \param[in] size The range’s size. |
| 55 void SetRange(bool is_64_bit, mach_vm_address_t base, mach_vm_size_t size); | 57 void SetRange(bool is_64_bit, ValueType base, SizeType size); |
| 56 | 58 |
| 57 //! \brief The range’s base address. | 59 //! \brief The range’s base address. |
| 58 mach_vm_address_t Base() const; | 60 ValueType Base() const; |
| 59 | 61 |
| 60 //! \brief The range’s size. | 62 //! \brief The range’s size. |
| 61 mach_vm_size_t Size() const; | 63 SizeType Size() const; |
| 62 | 64 |
| 63 //! \brief The range’s end address (its base address plus its size). | 65 //! \brief The range’s end address (its base address plus its size). |
| 64 mach_vm_address_t End() const; | 66 ValueType End() const; |
| 65 | 67 |
| 66 //! \brief Returns the validity of the address range. | 68 //! \brief Returns the validity of the address range. |
| 67 //! | 69 //! |
| 68 //! \return `true` if the address range is valid, `false` otherwise. | 70 //! \return `true` if the address range is valid, `false` otherwise. |
| 69 //! | 71 //! |
| 70 //! An address range is valid if its size can be converted to the address | 72 //! An address range is valid if its size can be converted to the address |
| 71 //! range’s data type without data loss, and if its end (base plus size) can | 73 //! range’s data type without data loss, and if its end (base plus size) can |
| 72 //! be computed without overflowing its data type. | 74 //! be computed without overflowing its data type. |
| 73 bool IsValid() const; | 75 bool IsValid() const; |
| 74 | 76 |
| 77 //! \brief Returns whether this range refers to a 64-bit process. |
| 78 bool Is64Bit() const { return is_64_bit_; } |
| 79 |
| 75 //! \brief Returns whether the address range contains another address. | 80 //! \brief Returns whether the address range contains another address. |
| 76 //! | 81 //! |
| 77 //! \param[in] value The (possibly) contained address. | 82 //! \param[in] value The (possibly) contained address. |
| 78 //! | 83 //! |
| 79 //! \return `true` if the address range contains \a value, `false` otherwise. | 84 //! \return `true` if the address range contains \a value, `false` otherwise. |
| 80 //! | 85 //! |
| 81 //! An address range contains a value if the value is greater than or equal to | 86 //! An address range contains a value if the value is greater than or equal to |
| 82 //! its base address, and less than its end address (base address plus size). | 87 //! its base address, and less than its end address (base address plus size). |
| 83 //! | 88 //! |
| 84 //! This method must only be called if IsValid() would return `true`. | 89 //! This method must only be called if IsValid() would return `true`. |
| 85 bool ContainsValue(const mach_vm_address_t value) const; | 90 bool ContainsValue(const ValueType value) const; |
| 86 | 91 |
| 87 //! \brief Returns whether the address range contains another address range. | 92 //! \brief Returns whether the address range contains another address range. |
| 88 //! | 93 //! |
| 89 //! \param[in] that The (possibly) contained address range. | 94 //! \param[in] that The (possibly) contained address range. |
| 90 //! | 95 //! |
| 91 //! \return `true` if `this` address range, the containing address range, | 96 //! \return `true` if `this` address range, the containing address range, |
| 92 //! contains \a that, the contained address range. `false` otherwise. | 97 //! contains \a that, the contained address range. `false` otherwise. |
| 93 //! | 98 //! |
| 94 //! An address range contains another address range when the contained address | 99 //! An address range contains another address range when the contained address |
| 95 //! range’s base is greater than or equal to the containing address range’s | 100 //! range’s base is greater than or equal to the containing address range’s |
| 96 //! base, and the contained address range’s end is less than or equal to the | 101 //! base, and the contained address range’s end is less than or equal to the |
| 97 //! containing address range’s end. | 102 //! containing address range’s end. |
| 98 //! | 103 //! |
| 99 //! This method should only be called on two CheckedMachAddressRange objects | 104 //! This method should only be called on two CheckedAddressRangeGeneric |
| 100 //! representing address ranges in the same process. | 105 //! objects representing address ranges in the same process. |
| 101 //! | 106 //! |
| 102 //! This method must only be called if IsValid() would return `true` for both | 107 //! This method must only be called if IsValid() would return `true` for both |
| 103 //! CheckedMachAddressRange objects involved. | 108 //! CheckedAddressRangeGeneric objects involved. |
| 104 bool ContainsRange(const CheckedMachAddressRange& that) const; | 109 bool ContainsRange(const CheckedAddressRangeGeneric& that) const; |
| 105 | 110 |
| 106 private: | 111 private: |
| 112 #if defined(COMPILER_MSVC) |
| 113 // MSVC cannot handle a union containing CheckedRange (with constructor, etc.) |
| 114 // currently. |
| 115 CheckedRange<uint32_t> range_32_; |
| 116 CheckedRange<uint64_t> range_64_; |
| 117 #else |
| 107 // The field of the union that is expressed is determined by is_64_bit_. | 118 // The field of the union that is expressed is determined by is_64_bit_. |
| 108 union { | 119 union { |
| 109 CheckedRange<uint32_t> range_32_; | 120 CheckedRange<uint32_t> range_32_; |
| 110 CheckedRange<uint64_t> range_64_; | 121 CheckedRange<uint64_t> range_64_; |
| 111 }; | 122 }; |
| 123 #endif |
| 112 | 124 |
| 113 // Determines which field of the union is expressed. | 125 // Determines which field of the union is expressed. |
| 114 bool is_64_bit_; | 126 bool is_64_bit_; |
| 115 | 127 |
| 116 // Whether the base and size were valid for their data type when set. This is | 128 // Whether the base and size were valid for their data type when set. This is |
| 117 // always true when is_64_bit_ is true because the underlying data types are | 129 // always true when is_64_bit_ is true because the underlying data types are |
| 118 // 64 bits wide and there is no possibility for range and size to overflow. | 130 // 64 bits wide and there is no possibility for range and size to overflow. |
| 119 // When is_64_bit_ is false, range_ok_ will be false if SetRange() was passed | 131 // When is_64_bit_ is false, range_ok_ will be false if SetRange() was passed |
| 120 // a base or size that overflowed the underlying 32-bit data type. This field | 132 // a base or size that overflowed the underlying 32-bit data type. This field |
| 121 // is necessary because the interface exposes mach_vm_address_t and | 133 // is necessary because the interface exposes the address and size types |
| 122 // mach_vm_size_t uniformly, but these types are too wide for the underlying | 134 // uniformly, but these types are too wide for the underlying pointer and size |
| 123 // pointer and size types in 32-bit processes. | 135 // types in 32-bit processes. |
| 124 bool range_ok_; | 136 bool range_ok_; |
| 125 | 137 |
| 126 DISALLOW_COPY_AND_ASSIGN(CheckedMachAddressRange); | 138 DISALLOW_COPY_AND_ASSIGN(CheckedAddressRangeGeneric); |
| 127 }; | 139 }; |
| 128 | 140 |
| 141 } // namespace internal |
| 129 } // namespace crashpad | 142 } // namespace crashpad |
| 130 | 143 |
| 131 #endif // CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ | 144 #endif // CRASHPAD_UTIL_NUMERIC_CHECKED_ADDRESS_RANGE_H_ |
| OLD | NEW |