1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 #ifndef __LINUX_OVERFLOW_H 3 #define __LINUX_OVERFLOW_H 4 5 #include <linux/compiler.h> 6 #include <linux/limits.h> 7 #include <linux/const.h> 8 9 /* 10 * We need to compute the minimum and maximum values representable in a given 11 * type. These macros may also be useful elsewhere. It would seem more obvious 12 * to do something like: 13 * 14 * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0) 15 * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0) 16 * 17 * Unfortunately, the middle expressions, strictly speaking, have 18 * undefined behaviour, and at least some versions of gcc warn about 19 * the type_max expression (but not if -fsanitize=undefined is in 20 * effect; in that case, the warning is deferred to runtime...). 21 * 22 * The slightly excessive casting in type_min is to make sure the 23 * macros also produce sensible values for the exotic type _Bool. [The 24 * overflow checkers only almost work for _Bool, but that's 25 * a-feature-not-a-bug, since people shouldn't be doing arithmetic on 26 * _Bools. Besides, the gcc builtins don't allow _Bool* as third 27 * argument.] 28 * 29 * Idea stolen from 30 * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html - 31 * credit to Christian Biere. 32 */ 33 #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type))) 34 #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T))) 35 #define type_min(T) ((T)((T)-type_max(T)-(T)1)) 36 37 /* 38 * Avoids triggering -Wtype-limits compilation warning, 39 * while using unsigned data types to check a < 0. 40 */ 41 #define is_non_negative(a) ((a) > 0 || (a) == 0) 42 #define is_negative(a) (!(is_non_negative(a))) 43 44 /* 45 * Allows for effectively applying __must_check to a macro so we can have 46 * both the type-agnostic benefits of the macros while also being able to 47 * enforce that the return value is, in fact, checked. 48 */ 49 static inline bool __must_check __must_check_overflow(bool overflow) 50 { 51 return unlikely(overflow); 52 } 53 54 /** check_add_overflow() - Calculate addition with overflow checking 55 * 56 * @a: first addend 57 * @b: second addend 58 * @d: pointer to store sum 59 * 60 * Returns 0 on success. 61 * 62 * *@d holds the results of the attempted addition, but is not considered 63 * "safe for use" on a non-zero return value, which indicates that the 64 * sum has overflowed or been truncated. 65 */ 66 #define check_add_overflow(a, b, d) \ 67 __must_check_overflow(__builtin_add_overflow(a, b, d)) 68 69 /** check_sub_overflow() - Calculate subtraction with overflow checking 70 * 71 * @a: minuend; value to subtract from 72 * @b: subtrahend; value to subtract from @a 73 * @d: pointer to store difference 74 * 75 * Returns 0 on success. 76 * 77 * *@d holds the results of the attempted subtraction, but is not considered 78 * "safe for use" on a non-zero return value, which indicates that the 79 * difference has underflowed or been truncated. 80 */ 81 #define check_sub_overflow(a, b, d) \ 82 __must_check_overflow(__builtin_sub_overflow(a, b, d)) 83 84 /** check_mul_overflow() - Calculate multiplication with overflow checking 85 * 86 * @a: first factor 87 * @b: second factor 88 * @d: pointer to store product 89 * 90 * Returns 0 on success. 91 * 92 * *@d holds the results of the attempted multiplication, but is not 93 * considered "safe for use" on a non-zero return value, which indicates 94 * that the product has overflowed or been truncated. 95 */ 96 #define check_mul_overflow(a, b, d) \ 97 __must_check_overflow(__builtin_mul_overflow(a, b, d)) 98 99 /** check_shl_overflow() - Calculate a left-shifted value and check overflow 100 * 101 * @a: Value to be shifted 102 * @s: How many bits left to shift 103 * @d: Pointer to where to store the result 104 * 105 * Computes *@d = (@a << @s) 106 * 107 * Returns true if '*d' cannot hold the result or when 'a << s' doesn't 108 * make sense. Example conditions: 109 * - 'a << s' causes bits to be lost when stored in *d. 110 * - 's' is garbage (e.g. negative) or so large that the result of 111 * 'a << s' is guaranteed to be 0. 112 * - 'a' is negative. 113 * - 'a << s' sets the sign bit, if any, in '*d'. 114 * 115 * '*d' will hold the results of the attempted shift, but is not 116 * considered "safe for use" if true is returned. 117 */ 118 #define check_shl_overflow(a, s, d) __must_check_overflow(({ \ 119 typeof(a) _a = a; \ 120 typeof(s) _s = s; \ 121 typeof(d) _d = d; \ 122 u64 _a_full = _a; \ 123 unsigned int _to_shift = \ 124 is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \ 125 *_d = (_a_full << _to_shift); \ 126 (_to_shift != _s || is_negative(*_d) || is_negative(_a) || \ 127 (*_d >> _to_shift) != _a); \ 128 })) 129 130 /** 131 * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX 132 * 133 * @factor1: first factor 134 * @factor2: second factor 135 * 136 * Returns: calculate @factor1 * @factor2, both promoted to size_t, 137 * with any overflow causing the return value to be SIZE_MAX. The 138 * lvalue must be size_t to avoid implicit type conversion. 139 */ 140 static inline size_t __must_check size_mul(size_t factor1, size_t factor2) 141 { 142 size_t bytes; 143 144 if (check_mul_overflow(factor1, factor2, &bytes)) 145 return SIZE_MAX; 146 147 return bytes; 148 } 149 150 /** 151 * size_add() - Calculate size_t addition with saturation at SIZE_MAX 152 * 153 * @addend1: first addend 154 * @addend2: second addend 155 * 156 * Returns: calculate @addend1 + @addend2, both promoted to size_t, 157 * with any overflow causing the return value to be SIZE_MAX. The 158 * lvalue must be size_t to avoid implicit type conversion. 159 */ 160 static inline size_t __must_check size_add(size_t addend1, size_t addend2) 161 { 162 size_t bytes; 163 164 if (check_add_overflow(addend1, addend2, &bytes)) 165 return SIZE_MAX; 166 167 return bytes; 168 } 169 170 /** 171 * size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX 172 * 173 * @minuend: value to subtract from 174 * @subtrahend: value to subtract from @minuend 175 * 176 * Returns: calculate @minuend - @subtrahend, both promoted to size_t, 177 * with any overflow causing the return value to be SIZE_MAX. For 178 * composition with the size_add() and size_mul() helpers, neither 179 * argument may be SIZE_MAX (or the result with be forced to SIZE_MAX). 180 * The lvalue must be size_t to avoid implicit type conversion. 181 */ 182 static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) 183 { 184 size_t bytes; 185 186 if (minuend == SIZE_MAX || subtrahend == SIZE_MAX || 187 check_sub_overflow(minuend, subtrahend, &bytes)) 188 return SIZE_MAX; 189 190 return bytes; 191 } 192 193 /** 194 * array_size() - Calculate size of 2-dimensional array. 195 * 196 * @a: dimension one 197 * @b: dimension two 198 * 199 * Calculates size of 2-dimensional array: @a * @b. 200 * 201 * Returns: number of bytes needed to represent the array or SIZE_MAX on 202 * overflow. 203 */ 204 #define array_size(a, b) size_mul(a, b) 205 206 /** 207 * array3_size() - Calculate size of 3-dimensional array. 208 * 209 * @a: dimension one 210 * @b: dimension two 211 * @c: dimension three 212 * 213 * Calculates size of 3-dimensional array: @a * @b * @c. 214 * 215 * Returns: number of bytes needed to represent the array or SIZE_MAX on 216 * overflow. 217 */ 218 #define array3_size(a, b, c) size_mul(size_mul(a, b), c) 219 220 /** 221 * flex_array_size() - Calculate size of a flexible array member 222 * within an enclosing structure. 223 * 224 * @p: Pointer to the structure. 225 * @member: Name of the flexible array member. 226 * @count: Number of elements in the array. 227 * 228 * Calculates size of a flexible array of @count number of @member 229 * elements, at the end of structure @p. 230 * 231 * Return: number of bytes needed or SIZE_MAX on overflow. 232 */ 233 #define flex_array_size(p, member, count) \ 234 __builtin_choose_expr(__is_constexpr(count), \ 235 (count) * sizeof(*(p)->member) + __must_be_array((p)->member), \ 236 size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member))) 237 238 /** 239 * struct_size() - Calculate size of structure with trailing flexible array. 240 * 241 * @p: Pointer to the structure. 242 * @member: Name of the array member. 243 * @count: Number of elements in the array. 244 * 245 * Calculates size of memory needed for structure @p followed by an 246 * array of @count number of @member elements. 247 * 248 * Return: number of bytes needed or SIZE_MAX on overflow. 249 */ 250 #define struct_size(p, member, count) \ 251 __builtin_choose_expr(__is_constexpr(count), \ 252 sizeof(*(p)) + flex_array_size(p, member, count), \ 253 size_add(sizeof(*(p)), flex_array_size(p, member, count))) 254 255 #endif /* __LINUX_OVERFLOW_H */ 256