OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include "util/numeric/checked_address_range.h" |
| 16 |
| 17 #if defined(OS_MACOSX) |
| 18 #include <mach/mach.h> |
| 19 #elif defined(OS_WIN) |
| 20 #include "util/win/address_types.h" |
| 21 #endif // OS_MACOSX |
| 22 |
| 23 namespace crashpad { |
| 24 namespace internal { |
| 25 |
| 26 template <class ValueType, class SizeType> |
| 27 CheckedAddressRangeGeneric<ValueType, SizeType>::CheckedAddressRangeGeneric() |
| 28 : range_32_(0, 0), |
| 29 #if defined(COMPILER_MSVC) |
| 30 range_64_(0, 0), |
| 31 #endif // COMPILER_MSVC |
| 32 is_64_bit_(false), |
| 33 range_ok_(true) { |
| 34 } |
| 35 |
| 36 template <class ValueType, class SizeType> |
| 37 CheckedAddressRangeGeneric<ValueType, SizeType>::CheckedAddressRangeGeneric( |
| 38 bool is_64_bit, |
| 39 ValueType base, |
| 40 SizeType size) |
| 41 #if defined(COMPILER_MSVC) |
| 42 : range_32_(0, 0), |
| 43 range_64_(0, 0) |
| 44 #endif // COMPILER_MSVC |
| 45 { |
| 46 SetRange(is_64_bit, base, size); |
| 47 } |
| 48 |
| 49 template <class ValueType, class SizeType> |
| 50 ValueType CheckedAddressRangeGeneric<ValueType, SizeType>::Base() const { |
| 51 return is_64_bit_ ? range_64_.base() : range_32_.base(); |
| 52 } |
| 53 |
| 54 template <class ValueType, class SizeType> |
| 55 SizeType CheckedAddressRangeGeneric<ValueType, SizeType>::Size() const { |
| 56 return is_64_bit_ ? range_64_.size() : range_32_.size(); |
| 57 } |
| 58 |
| 59 template <class ValueType, class SizeType> |
| 60 ValueType CheckedAddressRangeGeneric<ValueType, SizeType>::End() const { |
| 61 return is_64_bit_ ? range_64_.end() : range_32_.end(); |
| 62 } |
| 63 |
| 64 template <class ValueType, class SizeType> |
| 65 bool CheckedAddressRangeGeneric<ValueType, SizeType>::IsValid() const { |
| 66 return range_ok_ && (is_64_bit_ ? range_64_.IsValid() : range_32_.IsValid()); |
| 67 } |
| 68 |
| 69 template <class ValueType, class SizeType> |
| 70 void CheckedAddressRangeGeneric<ValueType, SizeType>::SetRange(bool is_64_bit, |
| 71 ValueType base, |
| 72 SizeType size) { |
| 73 is_64_bit_ = is_64_bit; |
| 74 if (is_64_bit_) { |
| 75 range_64_.SetRange(base, size); |
| 76 range_ok_ = true; |
| 77 } else { |
| 78 range_32_.SetRange(static_cast<uint32_t>(base), |
| 79 static_cast<uint32_t>(size)); |
| 80 range_ok_ = base::IsValueInRangeForNumericType<uint32_t>(base) && |
| 81 base::IsValueInRangeForNumericType<uint32_t>(size); |
| 82 } |
| 83 } |
| 84 |
| 85 template <class ValueType, class SizeType> |
| 86 bool CheckedAddressRangeGeneric<ValueType, SizeType>::ContainsValue( |
| 87 ValueType value) const { |
| 88 DCHECK(range_ok_); |
| 89 |
| 90 if (is_64_bit_) { |
| 91 return range_64_.ContainsValue(value); |
| 92 } |
| 93 |
| 94 if (!base::IsValueInRangeForNumericType<uint32_t>(value)) { |
| 95 return false; |
| 96 } |
| 97 |
| 98 return range_32_.ContainsValue(static_cast<uint32_t>(value)); |
| 99 } |
| 100 |
| 101 template <class ValueType, class SizeType> |
| 102 bool CheckedAddressRangeGeneric<ValueType, SizeType>::ContainsRange( |
| 103 const CheckedAddressRangeGeneric& that) const { |
| 104 DCHECK_EQ(is_64_bit_, that.is_64_bit_); |
| 105 DCHECK(range_ok_); |
| 106 DCHECK(that.range_ok_); |
| 107 |
| 108 return is_64_bit_ ? range_64_.ContainsRange(that.range_64_) |
| 109 : range_32_.ContainsRange(that.range_32_); |
| 110 } |
| 111 |
| 112 // Explicit instantiations for the cases we use. |
| 113 #if defined(OS_MACOSX) |
| 114 template class CheckedAddressRangeGeneric<mach_vm_address_t, mach_vm_size_t>; |
| 115 #elif defined(OS_WIN) |
| 116 template class CheckedAddressRangeGeneric<WinVMAddress, WinVMSize>; |
| 117 #endif // OS_MACOSX |
| 118 |
| 119 } // namespace internal |
| 120 } // namespace crashpad |
OLD | NEW |