Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: util/numeric/checked_address_range.h

Issue 1052813002: win: make CrashpadInfo retrievable (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 {
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()
43 : range_32_(0, 0),
44 #if defined(COMPILER_MSVC)
45 range_64_(0, 0),
46 #endif // COMPILER_MSVC
47 is_64_bit_(false),
48 range_ok_(true) {
49 }
40 50
41 //! \brief Initializes a range. 51 //! \brief Initializes a range.
42 //! 52 //!
43 //! See SetRange(). 53 //! See SetRange().
44 CheckedMachAddressRange(bool is_64_bit, 54 CheckedAddressRangeGeneric(bool is_64_bit, ValueType base, SizeType size)
45 mach_vm_address_t base, 55 #if defined(COMPILER_MSVC)
46 mach_vm_size_t size); 56 : range_32_(0, 0),
57 range_64_(0, 0)
58 #endif // COMPILER_MSVC
59 {
60 SetRange(is_64_bit, base, size);
61 }
47 62
48 //! \brief Sets a range’s fields. 63 //! \brief Sets a range’s fields.
49 //! 64 //!
50 //! \param[in] is_64_bit `true` if \a base and \a size refer to addresses in a 65 //! \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 66 //! 64-bit process; `false` if they refer to addresses in a 32-bit
52 //! process. 67 //! process.
53 //! \param[in] base The range’s base address. 68 //! \param[in] base The range’s base address.
54 //! \param[in] size The range’s size. 69 //! \param[in] size The range’s size.
55 void SetRange(bool is_64_bit, mach_vm_address_t base, mach_vm_size_t size); 70 void SetRange(bool is_64_bit, ValueType base, SizeType size);
56 71
57 //! \brief The range’s base address. 72 //! \brief The range’s base address.
58 mach_vm_address_t Base() const; 73 ValueType Base() const {
74 return is_64_bit_ ? range_64_.base() : range_32_.base();
75 }
59 76
60 //! \brief The range’s size. 77 //! \brief The range’s size.
61 mach_vm_size_t Size() const; 78 SizeType Size() const {
79 return is_64_bit_ ? range_64_.size() : range_32_.size();
80 }
62 81
63 //! \brief The range’s end address (its base address plus its size). 82 //! \brief The range’s end address (its base address plus its size).
64 mach_vm_address_t End() const; 83 ValueType End() const {
84 return is_64_bit_ ? range_64_.end() : range_32_.end();
85 }
65 86
66 //! \brief Returns the validity of the address range. 87 //! \brief Returns the validity of the address range.
67 //! 88 //!
68 //! \return `true` if the address range is valid, `false` otherwise. 89 //! \return `true` if the address range is valid, `false` otherwise.
69 //! 90 //!
70 //! An address range is valid if its size can be converted to the address 91 //! 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 92 //! range’s data type without data loss, and if its end (base plus size) can
72 //! be computed without overflowing its data type. 93 //! be computed without overflowing its data type.
73 bool IsValid() const; 94 bool IsValid() const {
95 return range_ok_ &&
96 (is_64_bit_ ? range_64_.IsValid() : range_32_.IsValid());
97 }
74 98
75 //! \brief Returns whether the address range contains another address. 99 //! \brief Returns whether the address range contains another address.
76 //! 100 //!
77 //! \param[in] value The (possibly) contained address. 101 //! \param[in] value The (possibly) contained address.
78 //! 102 //!
79 //! \return `true` if the address range contains \a value, `false` otherwise. 103 //! \return `true` if the address range contains \a value, `false` otherwise.
80 //! 104 //!
81 //! An address range contains a value if the value is greater than or equal to 105 //! 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). 106 //! its base address, and less than its end address (base address plus size).
83 //! 107 //!
84 //! This method must only be called if IsValid() would return `true`. 108 //! This method must only be called if IsValid() would return `true`.
85 bool ContainsValue(const mach_vm_address_t value) const; 109 bool ContainsValue(const ValueType value) const;
86 110
87 //! \brief Returns whether the address range contains another address range. 111 //! \brief Returns whether the address range contains another address range.
88 //! 112 //!
89 //! \param[in] that The (possibly) contained address range. 113 //! \param[in] that The (possibly) contained address range.
90 //! 114 //!
91 //! \return `true` if `this` address range, the containing address range, 115 //! \return `true` if `this` address range, the containing address range,
92 //! contains \a that, the contained address range. `false` otherwise. 116 //! contains \a that, the contained address range. `false` otherwise.
93 //! 117 //!
94 //! An address range contains another address range when the contained address 118 //! 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 119 //! 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 120 //! base, and the contained address range’s end is less than or equal to the
97 //! containing address range’s end. 121 //! containing address range’s end.
98 //! 122 //!
99 //! This method should only be called on two CheckedMachAddressRange objects 123 //! This method should only be called on two CheckedAddressRangeGeneric
100 //! representing address ranges in the same process. 124 //! objects representing address ranges in the same process.
101 //! 125 //!
102 //! This method must only be called if IsValid() would return `true` for both 126 //! This method must only be called if IsValid() would return `true` for both
103 //! CheckedMachAddressRange objects involved. 127 //! CheckedAddressRangeGeneric objects involved.
104 bool ContainsRange(const CheckedMachAddressRange& that) const; 128 bool ContainsRange(const CheckedAddressRangeGeneric& that) const;
105 129
106 private: 130 private:
131 #if defined(COMPILER_MSVC)
132 // MSVC cannot handle a union containing CheckedRange (with constructor, etc.)
133 // currently.
134 CheckedRange<uint32_t> range_32_;
135 CheckedRange<uint64_t> range_64_;
136 #else
107 // The field of the union that is expressed is determined by is_64_bit_. 137 // The field of the union that is expressed is determined by is_64_bit_.
108 union { 138 union {
109 CheckedRange<uint32_t> range_32_; 139 CheckedRange<uint32_t> range_32_;
110 CheckedRange<uint64_t> range_64_; 140 CheckedRange<uint64_t> range_64_;
111 }; 141 };
142 #endif
112 143
113 // Determines which field of the union is expressed. 144 // Determines which field of the union is expressed.
114 bool is_64_bit_; 145 bool is_64_bit_;
115 146
116 // Whether the base and size were valid for their data type when set. This is 147 // 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 148 // 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. 149 // 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 150 // 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 151 // 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 152 // 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 153 // uniformly, but these types are too wide for the underlying pointer and size
123 // pointer and size types in 32-bit processes. 154 // types in 32-bit processes.
124 bool range_ok_; 155 bool range_ok_;
125 156
126 DISALLOW_COPY_AND_ASSIGN(CheckedMachAddressRange); 157 DISALLOW_COPY_AND_ASSIGN(CheckedAddressRangeGeneric);
127 }; 158 };
128 159
160 template <class ValueType, class SizeType>
161 void CheckedAddressRangeGeneric<ValueType, SizeType>::SetRange(bool is_64_bit,
162 ValueType base,
163 SizeType size) {
164 is_64_bit_ = is_64_bit;
165 if (is_64_bit_) {
166 range_64_.SetRange(base, size);
167 range_ok_ = true;
168 } else {
169 range_32_.SetRange(static_cast<uint32_t>(base),
170 static_cast<uint32_t>(size));
171 range_ok_ = base::IsValueInRangeForNumericType<uint32_t>(base) &&
172 base::IsValueInRangeForNumericType<uint32_t>(size);
173 }
174 }
175
176 template <class ValueType, class SizeType>
177 bool CheckedAddressRangeGeneric<ValueType, SizeType>::ContainsValue(
178 ValueType value) const {
179 DCHECK(range_ok_);
180
181 if (is_64_bit_) {
182 return range_64_.ContainsValue(value);
183 }
184
185 if (!base::IsValueInRangeForNumericType<uint32_t>(value)) {
186 return false;
187 }
188
189 return range_32_.ContainsValue(static_cast<uint32_t>(value));
190 }
191
192 template <class ValueType, class SizeType>
193 bool CheckedAddressRangeGeneric<ValueType, SizeType>::ContainsRange(
194 const CheckedAddressRangeGeneric& that) const {
195 DCHECK_EQ(is_64_bit_, that.is_64_bit_);
196 DCHECK(range_ok_);
197 DCHECK(that.range_ok_);
198
199 return is_64_bit_ ? range_64_.ContainsRange(that.range_64_)
200 : range_32_.ContainsRange(that.range_32_);
201 }
202
203 using CheckedAddressRange = CheckedAddressRangeGeneric<uintptr_t, uintptr_t>;
Mark Mentovai 2015/04/30 20:58:36 I think that inlining this results in lots of bloa
scottmg 2015/04/30 22:09:44 Done.
204
129 } // namespace crashpad 205 } // namespace crashpad
130 206
131 #endif // CRASHPAD_UTIL_MAC_CHECKED_MACH_ADDRESS_RANGE_H_ 207 #endif // CRASHPAD_UTIL_NUMERIC_CHECKED_ADDRESS_RANGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698