OLD | NEW |
---|---|
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 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_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ |
16 #define CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ | 16 #define CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ |
17 | 17 |
18 #include <mach/mach.h> | 18 #include <mach/mach.h> |
19 | 19 |
20 #include "util/numeric/checked_range.h" | 20 #include "util/numeric/checked_address_range.h" |
21 | 21 |
22 namespace crashpad { | 22 namespace crashpad { |
23 | 23 |
24 //! \brief Ensures that a range, composed of a base and a size, does not | 24 //! \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. | 25 //! overflow the pointer type of the process it describes a range in. |
26 //! | 26 //! |
27 //! This class checks bases of type `mach_vm_address_t` and sizes of type | 27 //! This class checks bases of type `mach_vm_address_t` and sizes of type |
28 //! `mach_vm_address_t` against a process whose pointer type is either 32 or 64 | 28 //! `mach_vm_address_t` against a process whose pointer type is either 32 or 64 |
29 //! 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 using CheckedMachAddressRange = |
Mark Mentovai
2015/04/30 20:58:35
Cool!
| |
34 public: | 34 CheckedAddressRangeGeneric<mach_vm_address_t, mach_vm_size_t>; |
35 //! \brief Initializes a default range. | |
36 //! | |
37 //! The default range has base 0, size 0, and appears to be from a 32-bit | |
38 //! process. | |
39 CheckedMachAddressRange(); | |
40 | |
41 //! \brief Initializes a range. | |
42 //! | |
43 //! See SetRange(). | |
44 CheckedMachAddressRange(bool is_64_bit, | |
45 mach_vm_address_t base, | |
46 mach_vm_size_t size); | |
47 | |
48 //! \brief Sets a range’s fields. | |
49 //! | |
50 //! \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 //! process. | |
53 //! \param[in] base The range’s base address. | |
54 //! \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 | |
57 //! \brief The range’s base address. | |
58 mach_vm_address_t Base() const; | |
59 | |
60 //! \brief The range’s size. | |
61 mach_vm_size_t Size() const; | |
62 | |
63 //! \brief The range’s end address (its base address plus its size). | |
64 mach_vm_address_t End() const; | |
65 | |
66 //! \brief Returns the validity of the address range. | |
67 //! | |
68 //! \return `true` if the address range is valid, `false` otherwise. | |
69 //! | |
70 //! 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 //! be computed without overflowing its data type. | |
73 bool IsValid() const; | |
74 | |
75 //! \brief Returns whether the address range contains another address. | |
76 //! | |
77 //! \param[in] value The (possibly) contained address. | |
78 //! | |
79 //! \return `true` if the address range contains \a value, `false` otherwise. | |
80 //! | |
81 //! 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). | |
83 //! | |
84 //! This method must only be called if IsValid() would return `true`. | |
85 bool ContainsValue(const mach_vm_address_t value) const; | |
86 | |
87 //! \brief Returns whether the address range contains another address range. | |
88 //! | |
89 //! \param[in] that The (possibly) contained address range. | |
90 //! | |
91 //! \return `true` if `this` address range, the containing address range, | |
92 //! contains \a that, the contained address range. `false` otherwise. | |
93 //! | |
94 //! 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 | |
96 //! base, and the contained address range’s end is less than or equal to the | |
97 //! containing address range’s end. | |
98 //! | |
99 //! This method should only be called on two CheckedMachAddressRange objects | |
100 //! representing address ranges in the same process. | |
101 //! | |
102 //! This method must only be called if IsValid() would return `true` for both | |
103 //! CheckedMachAddressRange objects involved. | |
104 bool ContainsRange(const CheckedMachAddressRange& that) const; | |
105 | |
106 private: | |
107 // The field of the union that is expressed is determined by is_64_bit_. | |
108 union { | |
109 CheckedRange<uint32_t> range_32_; | |
110 CheckedRange<uint64_t> range_64_; | |
111 }; | |
112 | |
113 // Determines which field of the union is expressed. | |
114 bool is_64_bit_; | |
115 | |
116 // 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 | |
118 // 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 | |
120 // 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 | |
122 // mach_vm_size_t uniformly, but these types are too wide for the underlying | |
123 // pointer and size types in 32-bit processes. | |
124 bool range_ok_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(CheckedMachAddressRange); | |
127 }; | |
128 | 35 |
129 } // namespace crashpad | 36 } // namespace crashpad |
130 | 37 |
131 #endif // CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ | 38 #endif // CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ |
OLD | NEW |