xref: /xnu-11417.140.69/libkern/os/overflow.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2015-2018 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions  * Facilities for performing type- and overflow-checked arithmetic. These
31*43a90889SApple OSS Distributions  * functions return non-zero if overflow occured, zero otherwise. In either case,
32*43a90889SApple OSS Distributions  * the potentially overflowing operation is fully performed, mod the size of the
33*43a90889SApple OSS Distributions  * output type. See:
34*43a90889SApple OSS Distributions  * http://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins
35*43a90889SApple OSS Distributions  * for full details.
36*43a90889SApple OSS Distributions  *
37*43a90889SApple OSS Distributions  * The compiler enforces that users of os_*_overflow() check the return value to
38*43a90889SApple OSS Distributions  * determine whether overflow occured.
39*43a90889SApple OSS Distributions  */
40*43a90889SApple OSS Distributions 
41*43a90889SApple OSS Distributions #ifndef _OS_OVERFLOW_H
42*43a90889SApple OSS Distributions #define _OS_OVERFLOW_H
43*43a90889SApple OSS Distributions 
44*43a90889SApple OSS Distributions #include <sys/cdefs.h>
45*43a90889SApple OSS Distributions #include <stdbool.h>
46*43a90889SApple OSS Distributions #include <os/base.h>
47*43a90889SApple OSS Distributions 
48*43a90889SApple OSS Distributions bool __header_always_inline OS_WARN_RESULT
__os_warn_unused(__const bool x)49*43a90889SApple OSS Distributions __os_warn_unused(__const bool x)
50*43a90889SApple OSS Distributions {
51*43a90889SApple OSS Distributions 	return x;
52*43a90889SApple OSS Distributions }
53*43a90889SApple OSS Distributions 
54*43a90889SApple OSS Distributions #if __has_builtin(__builtin_add_overflow) && \
55*43a90889SApple OSS Distributions         __has_builtin(__builtin_sub_overflow) && \
56*43a90889SApple OSS Distributions         __has_builtin(__builtin_mul_overflow)
57*43a90889SApple OSS Distributions 
58*43a90889SApple OSS Distributions #define os_add_overflow(a, b, res) __os_warn_unused(__builtin_add_overflow((a), (b), (res)))
59*43a90889SApple OSS Distributions #define os_sub_overflow(a, b, res) __os_warn_unused(__builtin_sub_overflow((a), (b), (res)))
60*43a90889SApple OSS Distributions #define os_mul_overflow(a, b, res) __os_warn_unused(__builtin_mul_overflow((a), (b), (res)))
61*43a90889SApple OSS Distributions 
62*43a90889SApple OSS Distributions #else
63*43a90889SApple OSS Distributions # error os_overflow expects type-generic builtins
64*43a90889SApple OSS Distributions #endif /* __has_builtin(...) */
65*43a90889SApple OSS Distributions 
66*43a90889SApple OSS Distributions /* os_add3_overflow(a, b, c) -> (a + b + c) */
67*43a90889SApple OSS Distributions #define os_add3_overflow(a, b, c, res) __os_warn_unused(__extension__({ \
68*43a90889SApple OSS Distributions 	__typeof(*(res)) _tmp; \
69*43a90889SApple OSS Distributions 	bool _s, _t; \
70*43a90889SApple OSS Distributions 	_s = os_add_overflow((a), (b), &_tmp); \
71*43a90889SApple OSS Distributions 	_t = os_add_overflow((c), _tmp, (res)); \
72*43a90889SApple OSS Distributions 	_s | _t; \
73*43a90889SApple OSS Distributions }))
74*43a90889SApple OSS Distributions 
75*43a90889SApple OSS Distributions /* os_sub3_overflow(a, b, c) -> ((a - b) - c) */
76*43a90889SApple OSS Distributions #define os_sub3_overflow(a, b, c, res) __os_warn_unused(__extension__({ \
77*43a90889SApple OSS Distributions 	__typeof(*(res)) _tmp; \
78*43a90889SApple OSS Distributions 	bool _s, _t; \
79*43a90889SApple OSS Distributions 	_s = os_sub_overflow((a), (b), &_tmp); \
80*43a90889SApple OSS Distributions 	_t = os_sub_overflow(_tmp, (c), (res)); \
81*43a90889SApple OSS Distributions 	_s | _t; \
82*43a90889SApple OSS Distributions }))
83*43a90889SApple OSS Distributions 
84*43a90889SApple OSS Distributions /* os_mul3_overflow(a, b, c) -> (a * b * c) */
85*43a90889SApple OSS Distributions #define os_mul3_overflow(a, b, c, res) __os_warn_unused(__extension__({ \
86*43a90889SApple OSS Distributions 	__typeof(*(res)) _tmp; \
87*43a90889SApple OSS Distributions 	bool _s, _t; \
88*43a90889SApple OSS Distributions 	_s = os_mul_overflow((a), (b), &_tmp); \
89*43a90889SApple OSS Distributions 	_t = os_mul_overflow((c), _tmp, (res)); \
90*43a90889SApple OSS Distributions 	_s | _t; \
91*43a90889SApple OSS Distributions }))
92*43a90889SApple OSS Distributions 
93*43a90889SApple OSS Distributions /* os_add_and_mul_overflow(a, b, x) -> (a + b)*x */
94*43a90889SApple OSS Distributions #define os_add_and_mul_overflow(a, b, x, res) __os_warn_unused(__extension__({ \
95*43a90889SApple OSS Distributions 	__typeof(*(res)) _tmp; \
96*43a90889SApple OSS Distributions 	bool _s, _t; \
97*43a90889SApple OSS Distributions 	_s = os_add_overflow((a), (b), &_tmp); \
98*43a90889SApple OSS Distributions 	_t = os_mul_overflow((x), _tmp, (res)); \
99*43a90889SApple OSS Distributions 	_s | _t; \
100*43a90889SApple OSS Distributions }))
101*43a90889SApple OSS Distributions 
102*43a90889SApple OSS Distributions /* os_mul_and_add_overflow(a, x, b) -> a*x + b */
103*43a90889SApple OSS Distributions #define os_mul_and_add_overflow(a, x, b, res) __os_warn_unused(__extension__({ \
104*43a90889SApple OSS Distributions 	__typeof(*(res)) _tmp; \
105*43a90889SApple OSS Distributions 	bool _s, _t; \
106*43a90889SApple OSS Distributions 	_s = os_mul_overflow((a), (x), &_tmp); \
107*43a90889SApple OSS Distributions 	_t = os_add_overflow((b), _tmp, (res)); \
108*43a90889SApple OSS Distributions 	_s | _t; \
109*43a90889SApple OSS Distributions }))
110*43a90889SApple OSS Distributions 
111*43a90889SApple OSS Distributions /* os_convert_overflow(a) -> a [converted to the result type] */
112*43a90889SApple OSS Distributions #define os_convert_overflow(a, res) os_add_overflow((a), 0, (res))
113*43a90889SApple OSS Distributions 
114*43a90889SApple OSS Distributions /* os_inc_overflow(res) -> *res += 1 */
115*43a90889SApple OSS Distributions #define os_inc_overflow(res) __os_warn_unused(__extension__({ \
116*43a90889SApple OSS Distributions 	__typeof((res)) _tmp = (res); \
117*43a90889SApple OSS Distributions 	os_add_overflow(*_tmp, 1, _tmp); \
118*43a90889SApple OSS Distributions }))
119*43a90889SApple OSS Distributions 
120*43a90889SApple OSS Distributions /* os_dec_overflow(res) -> *res -= 1 */
121*43a90889SApple OSS Distributions #define os_dec_overflow(res) __os_warn_unused(__extension__({ \
122*43a90889SApple OSS Distributions 	__typeof((res)) _tmp = (res); \
123*43a90889SApple OSS Distributions 	os_sub_overflow(*_tmp, 1, _tmp); \
124*43a90889SApple OSS Distributions }))
125*43a90889SApple OSS Distributions 
126*43a90889SApple OSS Distributions 
127*43a90889SApple OSS Distributions #endif /* _OS_OVERFLOW_H */
128