2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef CheckedArithmetic_h
27 #define CheckedArithmetic_h
29 #include <wtf/Assertions.h>
30 #include <wtf/TypeTraits.h>
37 * This class provides a mechanism to perform overflow-safe integer arithmetic
38 * without having to manually ensure that you have all the required bounds checks
39 * directly in your code.
41 * There are two modes of operation:
42 * - The default is Checked<T, CrashOnOverflow>, and crashes at the point
43 * and overflow has occurred.
44 * - The alternative is Checked<T, RecordOverflow>, which uses an additional
45 * byte of storage to track whether an overflow has occurred, subsequent
46 * unchecked operations will crash if an overflow has occured
48 * It is possible to provide a custom overflow handler, in which case you need
49 * to support these functions:
50 * - void overflowed();
51 * This function is called when an operation has produced an overflow.
52 * - bool hasOverflowed();
53 * This function must return true if overflowed() has been called on an
54 * instance and false if it has not.
55 * - void clearOverflow();
56 * Used to reset overflow tracking when a value is being overwritten with
59 * Checked<T> works for all integer types, with the following caveats:
60 * - Mixing signedness of operands is only supported for types narrower than
62 * - It does have a performance impact, so tight loops may want to be careful
69 class CrashOnOverflow {
71 NO_RETURN_DUE_TO_CRASH void overflowed()
76 void clearOverflow() { }
79 bool hasOverflowed() const { return false; }
82 class RecordOverflow {
100 bool hasOverflowed() const { return m_overflowed; }
103 unsigned char m_overflowed;
106 template <typename T, class OverflowHandler = CrashOnOverflow> class Checked;
107 template <typename T> struct RemoveChecked;
108 template <typename T> struct RemoveChecked<Checked<T> >;
110 template <typename Target, typename Source, bool targetSigned = std::numeric_limits<Target>::is_signed, bool sourceSigned = std::numeric_limits<Source>::is_signed> struct BoundsChecker;
111 template <typename Target, typename Source> struct BoundsChecker<Target, Source, false, false> {
112 static bool inBounds(Source value)
114 // Same signedness so implicit type conversion will always increase precision
116 return value <= std::numeric_limits<Target>::max();
120 template <typename Target, typename Source> struct BoundsChecker<Target, Source, true, true> {
121 static bool inBounds(Source value)
123 // Same signedness so implicit type conversion will always increase precision
125 return std::numeric_limits<Target>::min() <= value && value <= std::numeric_limits<Target>::max();
129 template <typename Target, typename Source> struct BoundsChecker<Target, Source, false, true> {
130 static bool inBounds(Source value)
132 // Target is unsigned so any value less than zero is clearly unsafe
135 // If our (unsigned) Target is the same or greater width we can
136 // convert value to type Target without losing precision
137 if (sizeof(Target) >= sizeof(Source))
138 return static_cast<Target>(value) <= std::numeric_limits<Target>::max();
139 // The signed Source type has greater precision than the target so
140 // max(Target) -> Source will widen.
141 return value <= static_cast<Source>(std::numeric_limits<Target>::max());
145 template <typename Target, typename Source> struct BoundsChecker<Target, Source, true, false> {
146 static bool inBounds(Source value)
148 // Signed target with an unsigned source
149 if (sizeof(Target) <= sizeof(Source))
150 return value <= static_cast<Source>(std::numeric_limits<Target>::max());
151 // Target is Wider than Source so we're guaranteed to fit any value in
157 template <typename Target, typename Source, bool SameType = IsSameType<Target, Source>::value> struct BoundsCheckElider;
158 template <typename Target, typename Source> struct BoundsCheckElider<Target, Source, true> {
159 static bool inBounds(Source) { return true; }
161 template <typename Target, typename Source> struct BoundsCheckElider<Target, Source, false> : public BoundsChecker<Target, Source> {
164 template <typename Target, typename Source> static inline bool isInBounds(Source value)
166 return BoundsCheckElider<Target, Source>::inBounds(value);
169 template <typename T> struct RemoveChecked {
171 static const CleanType DefaultValue = 0;
174 template <typename T> struct RemoveChecked<Checked<T, CrashOnOverflow> > {
175 typedef typename RemoveChecked<T>::CleanType CleanType;
176 static const CleanType DefaultValue = 0;
179 template <typename T> struct RemoveChecked<Checked<T, RecordOverflow> > {
180 typedef typename RemoveChecked<T>::CleanType CleanType;
181 static const CleanType DefaultValue = 0;
184 // The ResultBase and SignednessSelector are used to workaround typeof not being
186 template <typename U, typename V, bool uIsBigger = (sizeof(U) > sizeof(V)), bool sameSize = (sizeof(U) == sizeof(V))> struct ResultBase;
187 template <typename U, typename V> struct ResultBase<U, V, true, false> {
188 typedef U ResultType;
191 template <typename U, typename V> struct ResultBase<U, V, false, false> {
192 typedef V ResultType;
195 template <typename U> struct ResultBase<U, U, false, true> {
196 typedef U ResultType;
199 template <typename U, typename V, bool uIsSigned = std::numeric_limits<U>::is_signed, bool vIsSigned = std::numeric_limits<V>::is_signed> struct SignednessSelector;
200 template <typename U, typename V> struct SignednessSelector<U, V, true, true> {
201 typedef U ResultType;
204 template <typename U, typename V> struct SignednessSelector<U, V, false, false> {
205 typedef U ResultType;
208 template <typename U, typename V> struct SignednessSelector<U, V, true, false> {
209 typedef V ResultType;
212 template <typename U, typename V> struct SignednessSelector<U, V, false, true> {
213 typedef U ResultType;
216 template <typename U, typename V> struct ResultBase<U, V, false, true> {
217 typedef typename SignednessSelector<U, V>::ResultType ResultType;
220 template <typename U, typename V> struct Result : ResultBase<typename RemoveChecked<U>::CleanType, typename RemoveChecked<V>::CleanType> {
223 template <typename LHS, typename RHS, typename ResultType = typename Result<LHS, RHS>::ResultType,
224 bool lhsSigned = std::numeric_limits<LHS>::is_signed, bool rhsSigned = std::numeric_limits<RHS>::is_signed> struct ArithmeticOperations;
226 template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOperations<LHS, RHS, ResultType, true, true> {
227 // LHS and RHS are signed types
230 static inline bool signsMatch(LHS lhs, RHS rhs)
232 return (lhs ^ rhs) >= 0;
235 static inline bool add(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN
237 if (signsMatch(lhs, rhs)) {
239 if ((std::numeric_limits<ResultType>::max() - rhs) < lhs)
242 ResultType temp = lhs - std::numeric_limits<ResultType>::min();
246 } // if the signs do not match this operation can't overflow
251 static inline bool sub(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN
253 if (!signsMatch(lhs, rhs)) {
255 if (lhs > std::numeric_limits<ResultType>::max() + rhs)
258 if (rhs > std::numeric_limits<ResultType>::max() + lhs)
261 } // if the signs match this operation can't overflow
266 static inline bool multiply(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN
268 if (signsMatch(lhs, rhs)) {
270 if (lhs && (std::numeric_limits<ResultType>::max() / lhs) < rhs)
273 if (lhs == std::numeric_limits<ResultType>::min() || rhs == std::numeric_limits<ResultType>::min())
275 if ((std::numeric_limits<ResultType>::max() / -lhs) < -rhs)
280 if (rhs && lhs < (std::numeric_limits<ResultType>::min() / rhs))
283 if (lhs && rhs < (std::numeric_limits<ResultType>::min() / lhs))
291 static inline bool equals(LHS lhs, RHS rhs) { return lhs == rhs; }
295 template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOperations<LHS, RHS, ResultType, false, false> {
296 // LHS and RHS are unsigned types so bounds checks are nice and easy
297 static inline bool add(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN
299 ResultType temp = lhs + rhs;
306 static inline bool sub(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN
308 ResultType temp = lhs - rhs;
315 static inline bool multiply(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN
317 ResultType temp = lhs * rhs;
324 static inline bool equals(LHS lhs, RHS rhs) { return lhs == rhs; }
328 template <typename ResultType> struct ArithmeticOperations<int, unsigned, ResultType, true, false> {
329 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result)
331 int64_t temp = lhs + rhs;
332 if (temp < std::numeric_limits<ResultType>::min())
334 if (temp > std::numeric_limits<ResultType>::max())
336 result = static_cast<ResultType>(temp);
340 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result)
342 int64_t temp = lhs - rhs;
343 if (temp < std::numeric_limits<ResultType>::min())
345 if (temp > std::numeric_limits<ResultType>::max())
347 result = static_cast<ResultType>(temp);
351 static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result)
353 int64_t temp = lhs * rhs;
354 if (temp < std::numeric_limits<ResultType>::min())
356 if (temp > std::numeric_limits<ResultType>::max())
358 result = static_cast<ResultType>(temp);
362 static inline bool equals(int lhs, unsigned rhs)
364 return static_cast<int64_t>(lhs) == static_cast<int64_t>(rhs);
368 template <typename ResultType> struct ArithmeticOperations<unsigned, int, ResultType, false, true> {
369 static inline bool add(int64_t lhs, int64_t rhs, ResultType& result)
371 return ArithmeticOperations<int, unsigned, ResultType>::add(rhs, lhs, result);
374 static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result)
376 return ArithmeticOperations<int, unsigned, ResultType>::sub(lhs, rhs, result);
379 static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result)
381 return ArithmeticOperations<int, unsigned, ResultType>::multiply(rhs, lhs, result);
384 static inline bool equals(unsigned lhs, int rhs)
386 return ArithmeticOperations<int, unsigned, ResultType>::equals(rhs, lhs);
390 template <typename U, typename V, typename R> static inline bool safeAdd(U lhs, V rhs, R& result)
392 return ArithmeticOperations<U, V, R>::add(lhs, rhs, result);
395 template <typename U, typename V, typename R> static inline bool safeSub(U lhs, V rhs, R& result)
397 return ArithmeticOperations<U, V, R>::sub(lhs, rhs, result);
400 template <typename U, typename V, typename R> static inline bool safeMultiply(U lhs, V rhs, R& result)
402 return ArithmeticOperations<U, V, R>::multiply(lhs, rhs, result);
405 template <typename U, typename V> static inline bool safeEquals(U lhs, V rhs)
407 return ArithmeticOperations<U, V>::equals(lhs, rhs);
410 enum ResultOverflowedTag { ResultOverflowed };
412 // FIXME: Needed to workaround http://llvm.org/bugs/show_bug.cgi?id=10801
413 static inline bool workAroundClangBug() { return true; }
415 template <typename T, class OverflowHandler> class Checked : public OverflowHandler {
417 template <typename _T, class _OverflowHandler> friend class Checked;
423 Checked(ResultOverflowedTag)
426 // FIXME: Remove this when clang fixes http://llvm.org/bugs/show_bug.cgi?id=10801
427 if (workAroundClangBug())
431 template <typename U> Checked(U value)
433 if (!isInBounds<T>(value))
435 m_value = static_cast<T>(value);
438 template <typename V> Checked(const Checked<T, V>& rhs)
439 : m_value(rhs.m_value)
441 if (rhs.hasOverflowed())
445 template <typename U> Checked(const Checked<U, OverflowHandler>& rhs)
446 : OverflowHandler(rhs)
448 if (!isInBounds<T>(rhs.m_value))
450 m_value = static_cast<T>(rhs.m_value);
453 template <typename U, typename V> Checked(const Checked<U, V>& rhs)
455 if (rhs.hasOverflowed())
457 if (!isInBounds<T>(rhs.m_value))
459 m_value = static_cast<T>(rhs.m_value);
462 const Checked& operator=(Checked rhs)
464 this->clearOverflow();
465 if (rhs.hasOverflowed())
467 m_value = static_cast<T>(rhs.m_value);
471 template <typename U> const Checked& operator=(U value)
473 return *this = Checked(value);
476 template <typename U, typename V> const Checked& operator=(const Checked<U, V>& rhs)
478 return *this = Checked(rhs);
482 const Checked& operator++()
484 if (m_value == std::numeric_limits<T>::max())
490 const Checked& operator--()
492 if (m_value == std::numeric_limits<T>::min())
499 const Checked operator++(int)
501 if (m_value == std::numeric_limits<T>::max())
503 return Checked(m_value++);
506 const Checked operator--(int)
508 if (m_value == std::numeric_limits<T>::min())
510 return Checked(m_value--);
514 bool operator!() const
516 if (this->hasOverflowed())
521 typedef void* (Checked::*UnspecifiedBoolType);
522 operator UnspecifiedBoolType*() const
524 if (this->hasOverflowed())
526 return (m_value) ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0;
529 // Value accessors. unsafeGet() will crash if there's been an overflow.
532 if (this->hasOverflowed())
537 bool safeGet(T& value) const WARN_UNUSED_RETURN
540 return this->hasOverflowed();
543 // Mutating assignment
544 template <typename U> const Checked operator+=(U rhs)
546 if (!safeAdd(m_value, rhs, m_value))
551 template <typename U> const Checked operator-=(U rhs)
553 if (!safeSub(m_value, rhs, m_value))
558 template <typename U> const Checked operator*=(U rhs)
560 if (!safeMultiply(m_value, rhs, m_value))
565 const Checked operator*=(double rhs)
567 double result = rhs * m_value;
568 // Handle +/- infinity and NaN
569 if (!(std::numeric_limits<T>::min() <= result && std::numeric_limits<T>::max() >= result))
575 const Checked operator*=(float rhs)
577 return *this *= (double)rhs;
580 template <typename U, typename V> const Checked operator+=(Checked<U, V> rhs)
582 if (rhs.hasOverflowed())
584 return *this += rhs.m_value;
587 template <typename U, typename V> const Checked operator-=(Checked<U, V> rhs)
589 if (rhs.hasOverflowed())
591 return *this -= rhs.m_value;
594 template <typename U, typename V> const Checked operator*=(Checked<U, V> rhs)
596 if (rhs.hasOverflowed())
598 return *this *= rhs.m_value;
601 // Equality comparisons
602 template <typename V> bool operator==(Checked<T, V> rhs)
604 return unsafeGet() == rhs.unsafeGet();
607 template <typename U> bool operator==(U rhs)
609 if (this->hasOverflowed())
611 return safeEquals(m_value, rhs);
614 template <typename U, typename V> const Checked operator==(Checked<U, V> rhs)
616 return unsafeGet() == Checked(rhs.unsafeGet());
619 template <typename U> bool operator!=(U rhs)
621 return !(*this == rhs);
625 // Disallow implicit conversion of floating point to integer types
628 void operator=(float);
629 void operator=(double);
630 void operator+=(float);
631 void operator+=(double);
632 void operator-=(float);
633 void operator-=(double);
637 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator+(Checked<U, OverflowHandler> lhs, Checked<V, OverflowHandler> rhs)
641 bool overflowed = lhs.safeGet(x) || rhs.safeGet(y);
642 typename Result<U, V>::ResultType result = 0;
643 overflowed |= !safeAdd(x, y, result);
645 return ResultOverflowed;
649 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator-(Checked<U, OverflowHandler> lhs, Checked<V, OverflowHandler> rhs)
653 bool overflowed = lhs.safeGet(x) || rhs.safeGet(y);
654 typename Result<U, V>::ResultType result = 0;
655 overflowed |= !safeSub(x, y, result);
657 return ResultOverflowed;
661 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator*(Checked<U, OverflowHandler> lhs, Checked<V, OverflowHandler> rhs)
665 bool overflowed = lhs.safeGet(x) || rhs.safeGet(y);
666 typename Result<U, V>::ResultType result = 0;
667 overflowed |= !safeMultiply(x, y, result);
669 return ResultOverflowed;
673 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator+(Checked<U, OverflowHandler> lhs, V rhs)
675 return lhs + Checked<V, OverflowHandler>(rhs);
678 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator-(Checked<U, OverflowHandler> lhs, V rhs)
680 return lhs - Checked<V, OverflowHandler>(rhs);
683 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator*(Checked<U, OverflowHandler> lhs, V rhs)
685 return lhs * Checked<V, OverflowHandler>(rhs);
688 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator+(U lhs, Checked<V, OverflowHandler> rhs)
690 return Checked<U, OverflowHandler>(lhs) + rhs;
693 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator-(U lhs, Checked<V, OverflowHandler> rhs)
695 return Checked<U, OverflowHandler>(lhs) - rhs;
698 template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator*(U lhs, Checked<V, OverflowHandler> rhs)
700 return Checked<U, OverflowHandler>(lhs) * rhs;
706 using WTF::RecordOverflow;