| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkSafeMath_DEFINED |
| 9 |
#define SkSafeMath_DEFINED |
| 10 |
|
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "include/private/base/SkDebug.h" // IWYU pragma: keep |
| 13 |
#include "include/private/base/SkTFitsIn.h" |
| 14 |
|
| 15 |
#include <cstddef> |
| 16 |
#include <cstdint> |
| 17 |
#include <limits> |
| 18 |
|
| 19 |
// SkSafeMath always check that a series of operations do not overflow. |
| 20 |
// This must be correct for all platforms, because this is a check for safety at runtime. |
| 21 |
|
| 22 |
class SkSafeMath { |
| 23 |
public: |
| 24 |
SkSafeMath() = default; |
| 25 |
|
| 26 |
bool ok() const { return fOK; } |
| 27 |
explicit operator bool() const { return fOK; } |
| 28 |
|
| 29 |
size_t mul(size_t x, size_t y) { |
| 30 |
return sizeof(size_t) == sizeof(uint64_t) ? mul64(x, y) : mul32(x, y); |
| 31 |
} |
| 32 |
|
| 33 |
size_t add(size_t x, size_t y) { |
| 34 |
size_t result = x + y; |
| 35 |
fOK &= result >= x; |
| 36 |
return result; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Return a + b, unless this result is an overflow/underflow. In those cases, fOK will |
| 41 |
* be set to false, and it is undefined what this returns. |
| 42 |
*/ |
| 43 |
int addInt(int a, int b) { |
| •44 |
static_assert(sizeof(int) == 4, "int is not 4 bytes"); |
| 45 |
if (b < 0 && a < std::numeric_limits<int>::min() - b) { |
| 46 |
fOK = false; |
| 47 |
return a; |
| 48 |
} else if (b > 0 && a > std::numeric_limits<int>::max() - b) { |
| 49 |
fOK = false; |
| 50 |
return a; |
| 51 |
} |
| 52 |
return a + b; |
| 53 |
} |
| 54 |
|
| •55 |
int subInt(int a, int b) { |
| 56 |
if (b == std::numeric_limits<int>::min()) { |
| 57 |
fOK = false; |
| 58 |
return a; |
| 59 |
} |
| 60 |
return addInt(a, -b); |
| 61 |
} |
| 62 |
|
| 63 |
int mulInt(int x, int y) { |
| 64 |
int64_t result = (int64_t)x * (int64_t)y; |
| 65 |
if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) { |
| 66 |
fOK = false; |
| 67 |
return x; |
| 68 |
} |
| 69 |
return (int)result; |
| 70 |
} |
| 71 |
|
| 72 |
size_t alignUp(size_t x, size_t alignment) { |
| 73 |
SkASSERT(alignment && !(alignment & (alignment - 1))); |
| 74 |
return add(x, alignment - 1) & ~(alignment - 1); |
| 75 |
} |
| 76 |
|
| 77 |
template <typename TDst, typename TSrc> TDst castTo(TSrc value) { |
| 78 |
if (!SkTFitsIn<TDst, TSrc>(value)) { |
| 79 |
fOK = false; |
| 80 |
} |
| 81 |
return static_cast<TDst>(value); |
| 82 |
} |
| 83 |
|
| 84 |
// These saturate to their results |
| 85 |
static size_t Add(size_t x, size_t y); |
| 86 |
static size_t Mul(size_t x, size_t y); |
| 87 |
static size_t Align4(size_t x) { |
| 88 |
SkSafeMath safe; |
| 89 |
return safe.alignUp(x, 4); |
| 90 |
} |
| 91 |
|
| 92 |
private: |
| 93 |
uint32_t mul32(uint32_t x, uint32_t y) { |
| 94 |
uint64_t bx = x; |
| 95 |
uint64_t by = y; |
| 96 |
uint64_t result = bx * by; |
| 97 |
fOK &= result >> 32 == 0; |
| 98 |
// Overflow information is capture in fOK. Return the result modulo 2^32. |
| 99 |
return (uint32_t)result; |
| 100 |
} |
| 101 |
|
| 102 |
uint64_t mul64(uint64_t x, uint64_t y) { |
| 103 |
if (x <= std::numeric_limits<uint64_t>::max() >> 32 |
| 104 |
&& y <= std::numeric_limits<uint64_t>::max() >> 32) { |
| 105 |
return x * y; |
| 106 |
} else { |
| 107 |
auto hi = [](uint64_t x) { return x >> 32; }; |
| 108 |
auto lo = [](uint64_t x) { return x & 0xFFFFFFFF; }; |
| 109 |
|
| 110 |
uint64_t lx_ly = lo(x) * lo(y); |
| 111 |
uint64_t hx_ly = hi(x) * lo(y); |
| 112 |
uint64_t lx_hy = lo(x) * hi(y); |
| 113 |
uint64_t hx_hy = hi(x) * hi(y); |
| 114 |
uint64_t result = 0; |
| 115 |
result = this->add(lx_ly, (hx_ly << 32)); |
| 116 |
result = this->add(result, (lx_hy << 32)); |
| 117 |
fOK &= (hx_hy + (hx_ly >> 32) + (lx_hy >> 32)) == 0; |
| 118 |
|
| 119 |
#if defined(SK_DEBUG) && defined(__clang__) && defined(__x86_64__) |
| 120 |
auto double_check = (unsigned __int128)x * y; |
| 121 |
SkASSERT(result == (double_check & 0xFFFFFFFFFFFFFFFF)); |
| 122 |
SkASSERT(!fOK || (double_check >> 64 == 0)); |
| 123 |
#endif |
| 124 |
|
| 125 |
return result; |
| 126 |
} |
| 127 |
} |
| 128 |
bool fOK = true; |
| 129 |
}; |
| 130 |
|
| 131 |
#endif//SkSafeMath_DEFINED |