|
6 | 6 | #include <linux/limits.h>
|
7 | 7 |
|
8 | 8 | /*
|
9 |
| - * In the fallback code below, we need to compute the minimum and |
10 |
| - * maximum values representable in a given type. These macros may also |
11 |
| - * be useful elsewhere, so we provide them outside the |
12 |
| - * COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW block. |
13 |
| - * |
14 |
| - * It would seem more obvious to do something like |
| 9 | + * We need to compute the minimum and maximum values representable in a given |
| 10 | + * type. These macros may also be useful elsewhere. It would seem more obvious |
| 11 | + * to do something like: |
15 | 12 | *
|
16 | 13 | * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
|
17 | 14 | * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
|
@@ -54,7 +51,6 @@ static inline bool __must_check __must_check_overflow(bool overflow)
|
54 | 51 | return unlikely(overflow);
|
55 | 52 | }
|
56 | 53 |
|
57 |
| -#ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW |
58 | 54 | /*
|
59 | 55 | * For simplicity and code hygiene, the fallback code below insists on
|
60 | 56 | * a, b and *d having the same type (similar to the min() and max()
|
@@ -90,134 +86,6 @@ static inline bool __must_check __must_check_overflow(bool overflow)
|
90 | 86 | __builtin_mul_overflow(__a, __b, __d); \
|
91 | 87 | }))
|
92 | 88 |
|
93 |
| -#else |
94 |
| - |
95 |
| - |
96 |
| -/* Checking for unsigned overflow is relatively easy without causing UB. */ |
97 |
| -#define __unsigned_add_overflow(a, b, d) ({ \ |
98 |
| - typeof(a) __a = (a); \ |
99 |
| - typeof(b) __b = (b); \ |
100 |
| - typeof(d) __d = (d); \ |
101 |
| - (void) (&__a == &__b); \ |
102 |
| - (void) (&__a == __d); \ |
103 |
| - *__d = __a + __b; \ |
104 |
| - *__d < __a; \ |
105 |
| -}) |
106 |
| -#define __unsigned_sub_overflow(a, b, d) ({ \ |
107 |
| - typeof(a) __a = (a); \ |
108 |
| - typeof(b) __b = (b); \ |
109 |
| - typeof(d) __d = (d); \ |
110 |
| - (void) (&__a == &__b); \ |
111 |
| - (void) (&__a == __d); \ |
112 |
| - *__d = __a - __b; \ |
113 |
| - __a < __b; \ |
114 |
| -}) |
115 |
| -/* |
116 |
| - * If one of a or b is a compile-time constant, this avoids a division. |
117 |
| - */ |
118 |
| -#define __unsigned_mul_overflow(a, b, d) ({ \ |
119 |
| - typeof(a) __a = (a); \ |
120 |
| - typeof(b) __b = (b); \ |
121 |
| - typeof(d) __d = (d); \ |
122 |
| - (void) (&__a == &__b); \ |
123 |
| - (void) (&__a == __d); \ |
124 |
| - *__d = __a * __b; \ |
125 |
| - __builtin_constant_p(__b) ? \ |
126 |
| - __b > 0 && __a > type_max(typeof(__a)) / __b : \ |
127 |
| - __a > 0 && __b > type_max(typeof(__b)) / __a; \ |
128 |
| -}) |
129 |
| - |
130 |
| -/* |
131 |
| - * For signed types, detecting overflow is much harder, especially if |
132 |
| - * we want to avoid UB. But the interface of these macros is such that |
133 |
| - * we must provide a result in *d, and in fact we must produce the |
134 |
| - * result promised by gcc's builtins, which is simply the possibly |
135 |
| - * wrapped-around value. Fortunately, we can just formally do the |
136 |
| - * operations in the widest relevant unsigned type (u64) and then |
137 |
| - * truncate the result - gcc is smart enough to generate the same code |
138 |
| - * with and without the (u64) casts. |
139 |
| - */ |
140 |
| - |
141 |
| -/* |
142 |
| - * Adding two signed integers can overflow only if they have the same |
143 |
| - * sign, and overflow has happened iff the result has the opposite |
144 |
| - * sign. |
145 |
| - */ |
146 |
| -#define __signed_add_overflow(a, b, d) ({ \ |
147 |
| - typeof(a) __a = (a); \ |
148 |
| - typeof(b) __b = (b); \ |
149 |
| - typeof(d) __d = (d); \ |
150 |
| - (void) (&__a == &__b); \ |
151 |
| - (void) (&__a == __d); \ |
152 |
| - *__d = (u64)__a + (u64)__b; \ |
153 |
| - (((~(__a ^ __b)) & (*__d ^ __a)) \ |
154 |
| - & type_min(typeof(__a))) != 0; \ |
155 |
| -}) |
156 |
| - |
157 |
| -/* |
158 |
| - * Subtraction is similar, except that overflow can now happen only |
159 |
| - * when the signs are opposite. In this case, overflow has happened if |
160 |
| - * the result has the opposite sign of a. |
161 |
| - */ |
162 |
| -#define __signed_sub_overflow(a, b, d) ({ \ |
163 |
| - typeof(a) __a = (a); \ |
164 |
| - typeof(b) __b = (b); \ |
165 |
| - typeof(d) __d = (d); \ |
166 |
| - (void) (&__a == &__b); \ |
167 |
| - (void) (&__a == __d); \ |
168 |
| - *__d = (u64)__a - (u64)__b; \ |
169 |
| - ((((__a ^ __b)) & (*__d ^ __a)) \ |
170 |
| - & type_min(typeof(__a))) != 0; \ |
171 |
| -}) |
172 |
| - |
173 |
| -/* |
174 |
| - * Signed multiplication is rather hard. gcc always follows C99, so |
175 |
| - * division is truncated towards 0. This means that we can write the |
176 |
| - * overflow check like this: |
177 |
| - * |
178 |
| - * (a > 0 && (b > MAX/a || b < MIN/a)) || |
179 |
| - * (a < -1 && (b > MIN/a || b < MAX/a) || |
180 |
| - * (a == -1 && b == MIN) |
181 |
| - * |
182 |
| - * The redundant casts of -1 are to silence an annoying -Wtype-limits |
183 |
| - * (included in -Wextra) warning: When the type is u8 or u16, the |
184 |
| - * __b_c_e in check_mul_overflow obviously selects |
185 |
| - * __unsigned_mul_overflow, but unfortunately gcc still parses this |
186 |
| - * code and warns about the limited range of __b. |
187 |
| - */ |
188 |
| - |
189 |
| -#define __signed_mul_overflow(a, b, d) ({ \ |
190 |
| - typeof(a) __a = (a); \ |
191 |
| - typeof(b) __b = (b); \ |
192 |
| - typeof(d) __d = (d); \ |
193 |
| - typeof(a) __tmax = type_max(typeof(a)); \ |
194 |
| - typeof(a) __tmin = type_min(typeof(a)); \ |
195 |
| - (void) (&__a == &__b); \ |
196 |
| - (void) (&__a == __d); \ |
197 |
| - *__d = (u64)__a * (u64)__b; \ |
198 |
| - (__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \ |
199 |
| - (__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \ |
200 |
| - (__b == (typeof(__b))-1 && __a == __tmin); \ |
201 |
| -}) |
202 |
| - |
203 |
| - |
204 |
| -#define check_add_overflow(a, b, d) __must_check_overflow( \ |
205 |
| - __builtin_choose_expr(is_signed_type(typeof(a)), \ |
206 |
| - __signed_add_overflow(a, b, d), \ |
207 |
| - __unsigned_add_overflow(a, b, d))) |
208 |
| - |
209 |
| -#define check_sub_overflow(a, b, d) __must_check_overflow( \ |
210 |
| - __builtin_choose_expr(is_signed_type(typeof(a)), \ |
211 |
| - __signed_sub_overflow(a, b, d), \ |
212 |
| - __unsigned_sub_overflow(a, b, d))) |
213 |
| - |
214 |
| -#define check_mul_overflow(a, b, d) __must_check_overflow( \ |
215 |
| - __builtin_choose_expr(is_signed_type(typeof(a)), \ |
216 |
| - __signed_mul_overflow(a, b, d), \ |
217 |
| - __unsigned_mul_overflow(a, b, d))) |
218 |
| - |
219 |
| -#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */ |
220 |
| - |
221 | 89 | /** check_shl_overflow() - Calculate a left-shifted value and check overflow
|
222 | 90 | *
|
223 | 91 | * @a: Value to be shifted
|
|
0 commit comments