Debian Codegraph @

src/base/SkSafeMath.h

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 if (b < 0 && a < std::numeric_limits<int>::min() - b) {
45 fOK = false;
46 return a;
47 } else if (b > 0 && a > std::numeric_limits<int>::max() - b) {
48 fOK = false;
49 return a;
50 }
51 return a + b;
52 }
53
54 int mulInt(int x, int y) {
55 int64_t result = (int64_t)x * (int64_t)y;
56 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
57 fOK = false;
58 return x;
59 }
60 return (int)result;
61 }
62
63 size_t alignUp(size_t x, size_t alignment) {
64 SkASSERT(alignment && !(alignment & (alignment - 1)));
65 return add(x, alignment - 1) & ~(alignment - 1);
66 }
67
68 template <typename TDst, typename TSrc> TDst castTo(TSrc value) {
69 if (!SkTFitsIn<TDst, TSrc>(value)) {
70 fOK = false;
71 }
72 return static_cast<TDst>(value);
73 }
74
75 // These saturate to their results
76 static size_t Add(size_t x, size_t y);
77 static size_t Mul(size_t x, size_t y);
78 static size_t Align4(size_t x) {
79 SkSafeMath safe;
80 return safe.alignUp(x, 4);
81 }
82
83 private:
84 uint32_t mul32(uint32_t x, uint32_t y) {
85 uint64_t bx = x;
86 uint64_t by = y;
87 uint64_t result = bx * by;
88 fOK &= result >> 32 == 0;
89 // Overflow information is capture in fOK. Return the result modulo 2^32.
90 return (uint32_t)result;
91 }
92
93 uint64_t mul64(uint64_t x, uint64_t y) {
94 if (x <= std::numeric_limits<uint64_t>::max() >> 32
95 && y <= std::numeric_limits<uint64_t>::max() >> 32) {
96 return x * y;
97 } else {
98 auto hi = [](uint64_t x) { return x >> 32; };
99 auto lo = [](uint64_t x) { return x & 0xFFFFFFFF; };
100
101 uint64_t lx_ly = lo(x) * lo(y);
102 uint64_t hx_ly = hi(x) * lo(y);
103 uint64_t lx_hy = lo(x) * hi(y);
104 uint64_t hx_hy = hi(x) * hi(y);
105 uint64_t result = 0;
106 result = this->add(lx_ly, (hx_ly << 32));
107 result = this->add(result, (lx_hy << 32));
108 fOK &= (hx_hy + (hx_ly >> 32) + (lx_hy >> 32)) == 0;
109
110 #if defined(SK_DEBUG) && defined(__clang__) && defined(__x86_64__)
111 auto double_check = (unsigned __int128)x * y;
112 SkASSERT(result == (double_check & 0xFFFFFFFFFFFFFFFF));
113 SkASSERT(!fOK || (double_check >> 64 == 0));
114 #endif
115
116 return result;
117 }
118 }
119 bool fOK = true;
120 };
121
122 #endif//SkSafeMath_DEFINED