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 { |
23 | 24 |
24 //! \brief Ensures that a range, composed of a base and a size, does not | 25 //! \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. | 26 //! overflow the pointer type of the process it describes a range in. |
26 //! | 27 //! |
27 //! This class checks bases of type `mach_vm_address_t` and sizes of type | 28 //! 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 | 29 //! against a process whose pointer type is either 32 or 64 bits wide. |
29 //! bits wide. | |
30 //! | 30 //! |
31 //! Aside from varying the overall range on the basis of a process’ pointer type | 31 //! Aside from varying the overall range on the basis of a process’ pointer type |
32 //! width, this class functions very similarly to CheckedRange. | 32 //! width, this class functions very similarly to CheckedRange. |
33 class CheckedMachAddressRange { | 33 //! |
34 //! \sa CheckedMachAddressRange | |
35 template <class ValueType, class SizeType> | |
36 class CheckedAddressRangeGeneric { | |
Mark Mentovai
2015/05/01 19:05:31
This can move into namespace internal since there
scottmg
2015/05/01 19:46:31
Done.
| |
34 public: | 37 public: |
35 //! \brief Initializes a default range. | 38 //! \brief Initializes a default range. |
36 //! | 39 //! |
37 //! The default range has base 0, size 0, and appears to be from a 32-bit | 40 //! The default range has base 0, size 0, and appears to be from a 32-bit |
38 //! process. | 41 //! process. |
39 CheckedMachAddressRange(); | 42 CheckedAddressRangeGeneric(); |
40 | 43 |
41 //! \brief Initializes a range. | 44 //! \brief Initializes a range. |
42 //! | 45 //! |
43 //! See SetRange(). | 46 //! See SetRange(). |
44 CheckedMachAddressRange(bool is_64_bit, | 47 CheckedAddressRangeGeneric(bool is_64_bit, ValueType base, SizeType size); |
45 mach_vm_address_t base, | |
46 mach_vm_size_t size); | |
47 | 48 |
48 //! \brief Sets a range’s fields. | 49 //! \brief Sets a range’s fields. |
49 //! | 50 //! |
50 //! \param[in] is_64_bit `true` if \a base and \a size refer to addresses in a | 51 //! \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 | 52 //! 64-bit process; `false` if they refer to addresses in a 32-bit |
52 //! process. | 53 //! process. |
53 //! \param[in] base The range’s base address. | 54 //! \param[in] base The range’s base address. |
54 //! \param[in] size The range’s size. | 55 //! \param[in] size The range’s size. |
55 void SetRange(bool is_64_bit, mach_vm_address_t base, mach_vm_size_t size); | 56 void SetRange(bool is_64_bit, ValueType base, SizeType size); |
56 | 57 |
57 //! \brief The range’s base address. | 58 //! \brief The range’s base address. |
58 mach_vm_address_t Base() const; | 59 ValueType Base() const; |
59 | 60 |
60 //! \brief The range’s size. | 61 //! \brief The range’s size. |
61 mach_vm_size_t Size() const; | 62 SizeType Size() const; |
62 | 63 |
63 //! \brief The range’s end address (its base address plus its size). | 64 //! \brief The range’s end address (its base address plus its size). |
64 mach_vm_address_t End() const; | 65 ValueType End() const; |
65 | 66 |
66 //! \brief Returns the validity of the address range. | 67 //! \brief Returns the validity of the address range. |
67 //! | 68 //! |
68 //! \return `true` if the address range is valid, `false` otherwise. | 69 //! \return `true` if the address range is valid, `false` otherwise. |
69 //! | 70 //! |
70 //! An address range is valid if its size can be converted to the address | 71 //! 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 | 72 //! range’s data type without data loss, and if its end (base plus size) can |
72 //! be computed without overflowing its data type. | 73 //! be computed without overflowing its data type. |
73 bool IsValid() const; | 74 bool IsValid() const; |
74 | 75 |
76 //! \brief Returns whether this range refers to a 64-bit process. | |
77 bool Is64Bit() const { return is_64_bit_; } | |
78 | |
75 //! \brief Returns whether the address range contains another address. | 79 //! \brief Returns whether the address range contains another address. |
76 //! | 80 //! |
77 //! \param[in] value The (possibly) contained address. | 81 //! \param[in] value The (possibly) contained address. |
78 //! | 82 //! |
79 //! \return `true` if the address range contains \a value, `false` otherwise. | 83 //! \return `true` if the address range contains \a value, `false` otherwise. |
80 //! | 84 //! |
81 //! An address range contains a value if the value is greater than or equal to | 85 //! 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). | 86 //! its base address, and less than its end address (base address plus size). |
83 //! | 87 //! |
84 //! This method must only be called if IsValid() would return `true`. | 88 //! This method must only be called if IsValid() would return `true`. |
85 bool ContainsValue(const mach_vm_address_t value) const; | 89 bool ContainsValue(const ValueType value) const; |
86 | 90 |
87 //! \brief Returns whether the address range contains another address range. | 91 //! \brief Returns whether the address range contains another address range. |
88 //! | 92 //! |
89 //! \param[in] that The (possibly) contained address range. | 93 //! \param[in] that The (possibly) contained address range. |
90 //! | 94 //! |
91 //! \return `true` if `this` address range, the containing address range, | 95 //! \return `true` if `this` address range, the containing address range, |
92 //! contains \a that, the contained address range. `false` otherwise. | 96 //! contains \a that, the contained address range. `false` otherwise. |
93 //! | 97 //! |
94 //! An address range contains another address range when the contained address | 98 //! 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 | 99 //! 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 | 100 //! base, and the contained address range’s end is less than or equal to the |
97 //! containing address range’s end. | 101 //! containing address range’s end. |
98 //! | 102 //! |
99 //! This method should only be called on two CheckedMachAddressRange objects | 103 //! This method should only be called on two CheckedAddressRangeGeneric |
100 //! representing address ranges in the same process. | 104 //! objects representing address ranges in the same process. |
101 //! | 105 //! |
102 //! This method must only be called if IsValid() would return `true` for both | 106 //! This method must only be called if IsValid() would return `true` for both |
103 //! CheckedMachAddressRange objects involved. | 107 //! CheckedAddressRangeGeneric objects involved. |
104 bool ContainsRange(const CheckedMachAddressRange& that) const; | 108 bool ContainsRange(const CheckedAddressRangeGeneric& that) const; |
105 | 109 |
106 private: | 110 private: |
111 #if defined(COMPILER_MSVC) | |
112 // MSVC cannot handle a union containing CheckedRange (with constructor, etc.) | |
113 // currently. | |
114 CheckedRange<uint32_t> range_32_; | |
115 CheckedRange<uint64_t> range_64_; | |
116 #else | |
107 // The field of the union that is expressed is determined by is_64_bit_. | 117 // The field of the union that is expressed is determined by is_64_bit_. |
108 union { | 118 union { |
109 CheckedRange<uint32_t> range_32_; | 119 CheckedRange<uint32_t> range_32_; |
110 CheckedRange<uint64_t> range_64_; | 120 CheckedRange<uint64_t> range_64_; |
111 }; | 121 }; |
122 #endif | |
112 | 123 |
113 // Determines which field of the union is expressed. | 124 // Determines which field of the union is expressed. |
114 bool is_64_bit_; | 125 bool is_64_bit_; |
115 | 126 |
116 // Whether the base and size were valid for their data type when set. This is | 127 // 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 | 128 // 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. | 129 // 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 | 130 // 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 | 131 // 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 | 132 // 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 | 133 // uniformly, but these types are too wide for the underlying pointer and size |
123 // pointer and size types in 32-bit processes. | 134 // types in 32-bit processes. |
124 bool range_ok_; | 135 bool range_ok_; |
125 | 136 |
126 DISALLOW_COPY_AND_ASSIGN(CheckedMachAddressRange); | 137 DISALLOW_COPY_AND_ASSIGN(CheckedAddressRangeGeneric); |
127 }; | 138 }; |
128 | 139 |
129 } // namespace crashpad | 140 } // namespace crashpad |
130 | 141 |
131 #endif // CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ | 142 #endif // CRASHPAD_UTIL_NUMERIC_CHECKED_ADDRESS_RANGE_H_ |
OLD | NEW |