OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Slightly adapted for inclusion in V8. |
| 6 // Copyright 2014 the V8 project authors. All rights reserved. |
| 7 |
| 8 #ifndef V8_BASE_SAFE_CONVERSIONS_IMPL_H_ |
| 9 #define V8_BASE_SAFE_CONVERSIONS_IMPL_H_ |
| 10 |
| 11 #include <limits> |
| 12 |
| 13 #include "src/base/macros.h" |
| 14 |
| 15 namespace v8 { |
| 16 namespace base { |
| 17 namespace internal { |
| 18 |
| 19 // The std library doesn't provide a binary max_exponent for integers, however |
| 20 // we can compute one by adding one to the number of non-sign bits. This allows |
| 21 // for accurate range comparisons between floating point and integer types. |
| 22 template <typename NumericType> |
| 23 struct MaxExponent { |
| 24 static const int value = std::numeric_limits<NumericType>::is_iec559 |
| 25 ? std::numeric_limits<NumericType>::max_exponent |
| 26 : (sizeof(NumericType) * 8 + 1 - |
| 27 std::numeric_limits<NumericType>::is_signed); |
| 28 }; |
| 29 |
| 30 enum IntegerRepresentation { |
| 31 INTEGER_REPRESENTATION_UNSIGNED, |
| 32 INTEGER_REPRESENTATION_SIGNED |
| 33 }; |
| 34 |
| 35 // A range for a given nunmeric Src type is contained for a given numeric Dst |
| 36 // type if both numeric_limits<Src>::max() <= numeric_limits<Dst>::max() and |
| 37 // numeric_limits<Src>::min() >= numeric_limits<Dst>::min() are true. |
| 38 // We implement this as template specializations rather than simple static |
| 39 // comparisons to ensure type correctness in our comparisons. |
| 40 enum NumericRangeRepresentation { |
| 41 NUMERIC_RANGE_NOT_CONTAINED, |
| 42 NUMERIC_RANGE_CONTAINED |
| 43 }; |
| 44 |
| 45 // Helper templates to statically determine if our destination type can contain |
| 46 // maximum and minimum values represented by the source type. |
| 47 |
| 48 template < |
| 49 typename Dst, |
| 50 typename Src, |
| 51 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
| 52 ? INTEGER_REPRESENTATION_SIGNED |
| 53 : INTEGER_REPRESENTATION_UNSIGNED, |
| 54 IntegerRepresentation SrcSign = |
| 55 std::numeric_limits<Src>::is_signed |
| 56 ? INTEGER_REPRESENTATION_SIGNED |
| 57 : INTEGER_REPRESENTATION_UNSIGNED > |
| 58 struct StaticDstRangeRelationToSrcRange; |
| 59 |
| 60 // Same sign: Dst is guaranteed to contain Src only if its range is equal or |
| 61 // larger. |
| 62 template <typename Dst, typename Src, IntegerRepresentation Sign> |
| 63 struct StaticDstRangeRelationToSrcRange<Dst, Src, Sign, Sign> { |
| 64 static const NumericRangeRepresentation value = |
| 65 MaxExponent<Dst>::value >= MaxExponent<Src>::value |
| 66 ? NUMERIC_RANGE_CONTAINED |
| 67 : NUMERIC_RANGE_NOT_CONTAINED; |
| 68 }; |
| 69 |
| 70 // Unsigned to signed: Dst is guaranteed to contain source only if its range is |
| 71 // larger. |
| 72 template <typename Dst, typename Src> |
| 73 struct StaticDstRangeRelationToSrcRange<Dst, |
| 74 Src, |
| 75 INTEGER_REPRESENTATION_SIGNED, |
| 76 INTEGER_REPRESENTATION_UNSIGNED> { |
| 77 static const NumericRangeRepresentation value = |
| 78 MaxExponent<Dst>::value > MaxExponent<Src>::value |
| 79 ? NUMERIC_RANGE_CONTAINED |
| 80 : NUMERIC_RANGE_NOT_CONTAINED; |
| 81 }; |
| 82 |
| 83 // Signed to unsigned: Dst cannot be statically determined to contain Src. |
| 84 template <typename Dst, typename Src> |
| 85 struct StaticDstRangeRelationToSrcRange<Dst, |
| 86 Src, |
| 87 INTEGER_REPRESENTATION_UNSIGNED, |
| 88 INTEGER_REPRESENTATION_SIGNED> { |
| 89 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED; |
| 90 }; |
| 91 |
| 92 enum RangeConstraint { |
| 93 RANGE_VALID = 0x0, // Value can be represented by the destination type. |
| 94 RANGE_UNDERFLOW = 0x1, // Value would overflow. |
| 95 RANGE_OVERFLOW = 0x2, // Value would underflow. |
| 96 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). |
| 97 }; |
| 98 |
| 99 // Helper function for coercing an int back to a RangeContraint. |
| 100 inline RangeConstraint GetRangeConstraint(int integer_range_constraint) { |
| 101 // TODO(jochen/jkummerow): Re-enable this when checks.h is available in base. |
| 102 // ASSERT(integer_range_constraint >= RANGE_VALID && |
| 103 // integer_range_constraint <= RANGE_INVALID); |
| 104 return static_cast<RangeConstraint>(integer_range_constraint); |
| 105 } |
| 106 |
| 107 // This function creates a RangeConstraint from an upper and lower bound |
| 108 // check by taking advantage of the fact that only NaN can be out of range in |
| 109 // both directions at once. |
| 110 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, |
| 111 bool is_in_lower_bound) { |
| 112 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | |
| 113 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); |
| 114 } |
| 115 |
| 116 template < |
| 117 typename Dst, |
| 118 typename Src, |
| 119 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
| 120 ? INTEGER_REPRESENTATION_SIGNED |
| 121 : INTEGER_REPRESENTATION_UNSIGNED, |
| 122 IntegerRepresentation SrcSign = std::numeric_limits<Src>::is_signed |
| 123 ? INTEGER_REPRESENTATION_SIGNED |
| 124 : INTEGER_REPRESENTATION_UNSIGNED, |
| 125 NumericRangeRepresentation DstRange = |
| 126 StaticDstRangeRelationToSrcRange<Dst, Src>::value > |
| 127 struct DstRangeRelationToSrcRangeImpl; |
| 128 |
| 129 // The following templates are for ranges that must be verified at runtime. We |
| 130 // split it into checks based on signedness to avoid confusing casts and |
| 131 // compiler warnings on signed an unsigned comparisons. |
| 132 |
| 133 // Dst range is statically determined to contain Src: Nothing to check. |
| 134 template <typename Dst, |
| 135 typename Src, |
| 136 IntegerRepresentation DstSign, |
| 137 IntegerRepresentation SrcSign> |
| 138 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 139 Src, |
| 140 DstSign, |
| 141 SrcSign, |
| 142 NUMERIC_RANGE_CONTAINED> { |
| 143 static RangeConstraint Check(Src value) { return RANGE_VALID; } |
| 144 }; |
| 145 |
| 146 // Signed to signed narrowing: Both the upper and lower boundaries may be |
| 147 // exceeded. |
| 148 template <typename Dst, typename Src> |
| 149 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 150 Src, |
| 151 INTEGER_REPRESENTATION_SIGNED, |
| 152 INTEGER_REPRESENTATION_SIGNED, |
| 153 NUMERIC_RANGE_NOT_CONTAINED> { |
| 154 static RangeConstraint Check(Src value) { |
| 155 return std::numeric_limits<Dst>::is_iec559 |
| 156 ? GetRangeConstraint(value <= std::numeric_limits<Dst>::max(), |
| 157 value >= -std::numeric_limits<Dst>::max()) |
| 158 : GetRangeConstraint(value <= std::numeric_limits<Dst>::max(), |
| 159 value >= std::numeric_limits<Dst>::min()); |
| 160 } |
| 161 }; |
| 162 |
| 163 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. |
| 164 template <typename Dst, typename Src> |
| 165 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 166 Src, |
| 167 INTEGER_REPRESENTATION_UNSIGNED, |
| 168 INTEGER_REPRESENTATION_UNSIGNED, |
| 169 NUMERIC_RANGE_NOT_CONTAINED> { |
| 170 static RangeConstraint Check(Src value) { |
| 171 return GetRangeConstraint(value <= std::numeric_limits<Dst>::max(), true); |
| 172 } |
| 173 }; |
| 174 |
| 175 // Unsigned to signed: The upper boundary may be exceeded. |
| 176 template <typename Dst, typename Src> |
| 177 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 178 Src, |
| 179 INTEGER_REPRESENTATION_SIGNED, |
| 180 INTEGER_REPRESENTATION_UNSIGNED, |
| 181 NUMERIC_RANGE_NOT_CONTAINED> { |
| 182 static RangeConstraint Check(Src value) { |
| 183 return sizeof(Dst) > sizeof(Src) |
| 184 ? RANGE_VALID |
| 185 : GetRangeConstraint( |
| 186 value <= static_cast<Src>(std::numeric_limits<Dst>::max()), |
| 187 true); |
| 188 } |
| 189 }; |
| 190 |
| 191 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, |
| 192 // and any negative value exceeds the lower boundary. |
| 193 template <typename Dst, typename Src> |
| 194 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 195 Src, |
| 196 INTEGER_REPRESENTATION_UNSIGNED, |
| 197 INTEGER_REPRESENTATION_SIGNED, |
| 198 NUMERIC_RANGE_NOT_CONTAINED> { |
| 199 static RangeConstraint Check(Src value) { |
| 200 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) |
| 201 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) |
| 202 : GetRangeConstraint( |
| 203 value <= static_cast<Src>(std::numeric_limits<Dst>::max()), |
| 204 value >= static_cast<Src>(0)); |
| 205 } |
| 206 }; |
| 207 |
| 208 template <typename Dst, typename Src> |
| 209 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { |
| 210 // Both source and destination must be numeric. |
| 211 STATIC_ASSERT(std::numeric_limits<Src>::is_specialized); |
| 212 STATIC_ASSERT(std::numeric_limits<Dst>::is_specialized); |
| 213 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); |
| 214 } |
| 215 |
| 216 } // namespace internal |
| 217 } // namespace base |
| 218 } // namespace v8 |
| 219 |
| 220 #endif // V8_BASE_SAFE_CONVERSIONS_IMPL_H_ |
OLD | NEW |