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