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