overflow.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. #ifndef __LINUX_OVERFLOW_H
  3. #define __LINUX_OVERFLOW_H
  4. #include <linux/compiler.h>
  5. /*
  6. * In the fallback code below, we need to compute the minimum and
  7. * maximum values representable in a given type. These macros may also
  8. * be useful elsewhere, so we provide them outside the
  9. * COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW block.
  10. *
  11. * It would seem more obvious to do something like
  12. *
  13. * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
  14. * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
  15. *
  16. * Unfortunately, the middle expressions, strictly speaking, have
  17. * undefined behaviour, and at least some versions of gcc warn about
  18. * the type_max expression (but not if -fsanitize=undefined is in
  19. * effect; in that case, the warning is deferred to runtime...).
  20. *
  21. * The slightly excessive casting in type_min is to make sure the
  22. * macros also produce sensible values for the exotic type _Bool. [The
  23. * overflow checkers only almost work for _Bool, but that's
  24. * a-feature-not-a-bug, since people shouldn't be doing arithmetic on
  25. * _Bools. Besides, the gcc builtins don't allow _Bool* as third
  26. * argument.]
  27. *
  28. * Idea stolen from
  29. * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
  30. * credit to Christian Biere.
  31. */
  32. #define is_signed_type(type) (((type)(-1)) < (type)1)
  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. #ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
  37. /*
  38. * For simplicity and code hygiene, the fallback code below insists on
  39. * a, b and *d having the same type (similar to the min() and max()
  40. * macros), whereas gcc's type-generic overflow checkers accept
  41. * different types. Hence we don't just make check_add_overflow an
  42. * alias for __builtin_add_overflow, but add type checks similar to
  43. * below.
  44. */
  45. #define check_add_overflow(a, b, d) ({ \
  46. typeof(a) __a = (a); \
  47. typeof(b) __b = (b); \
  48. typeof(d) __d = (d); \
  49. (void) (&__a == &__b); \
  50. (void) (&__a == __d); \
  51. __builtin_add_overflow(__a, __b, __d); \
  52. })
  53. #define check_sub_overflow(a, b, d) ({ \
  54. typeof(a) __a = (a); \
  55. typeof(b) __b = (b); \
  56. typeof(d) __d = (d); \
  57. (void) (&__a == &__b); \
  58. (void) (&__a == __d); \
  59. __builtin_sub_overflow(__a, __b, __d); \
  60. })
  61. #define check_mul_overflow(a, b, d) ({ \
  62. typeof(a) __a = (a); \
  63. typeof(b) __b = (b); \
  64. typeof(d) __d = (d); \
  65. (void) (&__a == &__b); \
  66. (void) (&__a == __d); \
  67. __builtin_mul_overflow(__a, __b, __d); \
  68. })
  69. #else
  70. /* Checking for unsigned overflow is relatively easy without causing UB. */
  71. #define __unsigned_add_overflow(a, b, d) ({ \
  72. typeof(a) __a = (a); \
  73. typeof(b) __b = (b); \
  74. typeof(d) __d = (d); \
  75. (void) (&__a == &__b); \
  76. (void) (&__a == __d); \
  77. *__d = __a + __b; \
  78. *__d < __a; \
  79. })
  80. #define __unsigned_sub_overflow(a, b, d) ({ \
  81. typeof(a) __a = (a); \
  82. typeof(b) __b = (b); \
  83. typeof(d) __d = (d); \
  84. (void) (&__a == &__b); \
  85. (void) (&__a == __d); \
  86. *__d = __a - __b; \
  87. __a < __b; \
  88. })
  89. /*
  90. * If one of a or b is a compile-time constant, this avoids a division.
  91. */
  92. #define __unsigned_mul_overflow(a, b, d) ({ \
  93. typeof(a) __a = (a); \
  94. typeof(b) __b = (b); \
  95. typeof(d) __d = (d); \
  96. (void) (&__a == &__b); \
  97. (void) (&__a == __d); \
  98. *__d = __a * __b; \
  99. __builtin_constant_p(__b) ? \
  100. __b > 0 && __a > type_max(typeof(__a)) / __b : \
  101. __a > 0 && __b > type_max(typeof(__b)) / __a; \
  102. })
  103. /*
  104. * For signed types, detecting overflow is much harder, especially if
  105. * we want to avoid UB. But the interface of these macros is such that
  106. * we must provide a result in *d, and in fact we must produce the
  107. * result promised by gcc's builtins, which is simply the possibly
  108. * wrapped-around value. Fortunately, we can just formally do the
  109. * operations in the widest relevant unsigned type (u64) and then
  110. * truncate the result - gcc is smart enough to generate the same code
  111. * with and without the (u64) casts.
  112. */
  113. /*
  114. * Adding two signed integers can overflow only if they have the same
  115. * sign, and overflow has happened iff the result has the opposite
  116. * sign.
  117. */
  118. #define __signed_add_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 = (u64)__a + (u64)__b; \
  125. (((~(__a ^ __b)) & (*__d ^ __a)) \
  126. & type_min(typeof(__a))) != 0; \
  127. })
  128. /*
  129. * Subtraction is similar, except that overflow can now happen only
  130. * when the signs are opposite. In this case, overflow has happened if
  131. * the result has the opposite sign of a.
  132. */
  133. #define __signed_sub_overflow(a, b, d) ({ \
  134. typeof(a) __a = (a); \
  135. typeof(b) __b = (b); \
  136. typeof(d) __d = (d); \
  137. (void) (&__a == &__b); \
  138. (void) (&__a == __d); \
  139. *__d = (u64)__a - (u64)__b; \
  140. ((((__a ^ __b)) & (*__d ^ __a)) \
  141. & type_min(typeof(__a))) != 0; \
  142. })
  143. /*
  144. * Signed multiplication is rather hard. gcc always follows C99, so
  145. * division is truncated towards 0. This means that we can write the
  146. * overflow check like this:
  147. *
  148. * (a > 0 && (b > MAX/a || b < MIN/a)) ||
  149. * (a < -1 && (b > MIN/a || b < MAX/a) ||
  150. * (a == -1 && b == MIN)
  151. *
  152. * The redundant casts of -1 are to silence an annoying -Wtype-limits
  153. * (included in -Wextra) warning: When the type is u8 or u16, the
  154. * __b_c_e in check_mul_overflow obviously selects
  155. * __unsigned_mul_overflow, but unfortunately gcc still parses this
  156. * code and warns about the limited range of __b.
  157. */
  158. #define __signed_mul_overflow(a, b, d) ({ \
  159. typeof(a) __a = (a); \
  160. typeof(b) __b = (b); \
  161. typeof(d) __d = (d); \
  162. typeof(a) __tmax = type_max(typeof(a)); \
  163. typeof(a) __tmin = type_min(typeof(a)); \
  164. (void) (&__a == &__b); \
  165. (void) (&__a == __d); \
  166. *__d = (u64)__a * (u64)__b; \
  167. (__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \
  168. (__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \
  169. (__b == (typeof(__b))-1 && __a == __tmin); \
  170. })
  171. #define check_add_overflow(a, b, d) \
  172. __builtin_choose_expr(is_signed_type(typeof(a)), \
  173. __signed_add_overflow(a, b, d), \
  174. __unsigned_add_overflow(a, b, d))
  175. #define check_sub_overflow(a, b, d) \
  176. __builtin_choose_expr(is_signed_type(typeof(a)), \
  177. __signed_sub_overflow(a, b, d), \
  178. __unsigned_sub_overflow(a, b, d))
  179. #define check_mul_overflow(a, b, d) \
  180. __builtin_choose_expr(is_signed_type(typeof(a)), \
  181. __signed_mul_overflow(a, b, d), \
  182. __unsigned_mul_overflow(a, b, d))
  183. #endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
  184. /**
  185. * array_size() - Calculate size of 2-dimensional array.
  186. *
  187. * @a: dimension one
  188. * @b: dimension two
  189. *
  190. * Calculates size of 2-dimensional array: @a * @b.
  191. *
  192. * Returns: number of bytes needed to represent the array or SIZE_MAX on
  193. * overflow.
  194. */
  195. static inline __must_check size_t array_size(size_t a, size_t b)
  196. {
  197. size_t bytes;
  198. if (check_mul_overflow(a, b, &bytes))
  199. return SIZE_MAX;
  200. return bytes;
  201. }
  202. /**
  203. * array3_size() - Calculate size of 3-dimensional array.
  204. *
  205. * @a: dimension one
  206. * @b: dimension two
  207. * @c: dimension three
  208. *
  209. * Calculates size of 3-dimensional array: @a * @b * @c.
  210. *
  211. * Returns: number of bytes needed to represent the array or SIZE_MAX on
  212. * overflow.
  213. */
  214. static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
  215. {
  216. size_t bytes;
  217. if (check_mul_overflow(a, b, &bytes))
  218. return SIZE_MAX;
  219. if (check_mul_overflow(bytes, c, &bytes))
  220. return SIZE_MAX;
  221. return bytes;
  222. }
  223. static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
  224. {
  225. size_t bytes;
  226. if (check_mul_overflow(n, size, &bytes))
  227. return SIZE_MAX;
  228. if (check_add_overflow(bytes, c, &bytes))
  229. return SIZE_MAX;
  230. return bytes;
  231. }
  232. /**
  233. * struct_size() - Calculate size of structure with trailing array.
  234. * @p: Pointer to the structure.
  235. * @member: Name of the array member.
  236. * @n: Number of elements in the array.
  237. *
  238. * Calculates size of memory needed for structure @p followed by an
  239. * array of @n @member elements.
  240. *
  241. * Return: number of bytes needed or SIZE_MAX on overflow.
  242. */
  243. #define struct_size(p, member, n) \
  244. __ab_c_size(n, \
  245. sizeof(*(p)->member) + __must_be_array((p)->member),\
  246. sizeof(*(p)))
  247. #endif /* __LINUX_OVERFLOW_H */