Chromium Code Reviews| 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/mac/process_reader.h" | |
|
Robert Sesek
2014/08/28 13:58:30
Optional: Could forward declare instead.
| |
| 21 #include "util/numeric/checked_range.h" | |
| 22 | |
| 23 namespace crashpad { | |
| 24 | |
| 25 //! \brief Ensures that a range, composed of a base and a size, does not | |
| 26 //! overflow the pointer type of the process it describes a range in. | |
| 27 //! | |
| 28 //! This class checks bases of type `mach_vm_address_t` and sizes of type | |
| 29 //! `mach_vm_address_t` against a process whose pointer type can be determined | |
| 30 //! from its ProcessReader. | |
| 31 //! | |
| 32 //! Aside from varying the overall range on the basis of a process’ pointer type | |
| 33 //! width, this class functions very similarly to CheckedRange. | |
| 34 class CheckedMachAddressRange { | |
| 35 public: | |
| 36 //! \brief Initializes a default range. | |
| 37 //! | |
| 38 //! The default range has base 0, size 0, and appears to be from a 32-bit | |
| 39 //! process. | |
| 40 CheckedMachAddressRange(); | |
| 41 | |
| 42 //! \brief Initializes a range. | |
| 43 //! | |
| 44 //! See SetRange(). | |
| 45 CheckedMachAddressRange(ProcessReader* process_reader, | |
|
Robert Sesek
2014/08/28 13:58:30
I think ProcessReader could be const here.
| |
| 46 mach_vm_address_t base, | |
| 47 mach_vm_size_t size); | |
| 48 | |
| 49 //! \brief Sets a range’s fields. | |
| 50 //! | |
| 51 //! \param[in] process_reader The ProcessReader that can read the process that | |
| 52 //! \a base is a pointer to. | |
| 53 //! \param[in] base The range’s base address. | |
| 54 //! \param[in] size The range’s size. | |
| 55 void SetRange(ProcessReader* process_reader, | |
| 56 mach_vm_address_t base, | |
| 57 mach_vm_size_t size); | |
| 58 | |
| 59 //! \brief The range’s base address. | |
| 60 mach_vm_address_t Base() const; | |
| 61 | |
| 62 //! \brief The range’s size. | |
| 63 mach_vm_size_t Size() const; | |
| 64 | |
| 65 //! \brief The range’s end address (its base address plus its size). | |
| 66 mach_vm_address_t End() const; | |
| 67 | |
| 68 //! \brief Returns the validity of the address range. | |
| 69 //! | |
| 70 //! \return `true` if the address range is valid, `false` otherwise. | |
| 71 //! | |
| 72 //! An address range is valid if its size can be converted to the address | |
| 73 //! range’s data type without data loss, and if its end (base plus size) can | |
| 74 //! be computed without overflowing its data type. | |
| 75 bool IsValid() const; | |
| 76 | |
| 77 //! \brief Returns whether the address range contains another address. | |
| 78 //! | |
| 79 //! \param[in] value The (possibly) contained address. | |
| 80 //! | |
| 81 //! \return `true` if the address range contains \a value, `false` otherwise. | |
| 82 //! | |
| 83 //! An address range contains a value if the value is greater than or equal to | |
| 84 //! its base address, and less than its end address (base address plus size). | |
| 85 //! | |
| 86 //! This method must only be called if IsValid() would return `true`. | |
| 87 bool ContainsValue(const mach_vm_address_t value) const; | |
| 88 | |
| 89 //! \brief Returns whether the address range contains another address range. | |
| 90 //! | |
| 91 //! \param[in] that The (possibly) contained address range. | |
| 92 //! | |
| 93 //! \return `true` if `this` address range, the containing address range, | |
| 94 //! contains \a that, the contained address range. `false` otherwise. | |
| 95 //! | |
| 96 //! An address range contains another address range when the contained address | |
| 97 //! range’s base is greater than or equal to the containing address range’s | |
| 98 //! base, and the contained address range’s end is less than or equal to the | |
| 99 //! containing address range’s end. | |
| 100 //! | |
| 101 //! This method should only be called on two CheckedMachAddressRange objects | |
| 102 //! sharing the same ProcessReader. | |
| 103 //! | |
| 104 //! This method must only be called if IsValid() would return `true` for both | |
| 105 //! CheckedMachAddressRange objects involved. | |
| 106 bool ContainsRange(const CheckedMachAddressRange& that) const; | |
| 107 | |
| 108 private: | |
| 109 // The field of the union that is expressed is determined by is_64_bit_. | |
| 110 union { | |
| 111 CheckedRange<uint32_t> range_32_; | |
| 112 CheckedRange<uint64_t> range_64_; | |
| 113 }; | |
| 114 | |
| 115 // Determines which field of the union is expressed. | |
| 116 bool is_64_bit_; | |
| 117 | |
| 118 // Whether the base and size were valid for their data type when set. This is | |
| 119 // always true when is_64_bit_ is true because the underlying data types are | |
| 120 // 64 bits wide and there is no possibility for range and size to overflow. | |
| 121 // When is_64_bit_ is false, range_ok_ will be false if SetRange() was passed | |
| 122 // a base or size that overflowed the underlying 32-bit data type. This field | |
| 123 // is necessary because the interface exposes mach_vm_address_t and | |
| 124 // mach_vm_size_t uniformly, but these types are too wide for the underlying | |
| 125 // pointer and size types in 32-bit processes. | |
| 126 bool range_ok_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(CheckedMachAddressRange); | |
| 129 }; | |
| 130 | |
| 131 } // namespace crashpad | |
| 132 | |
| 133 #endif // CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ | |
| OLD | NEW |