]> git.saurik.com Git - apple/xnu.git/blame - libkern/os/overflow.h
xnu-3248.60.10.tar.gz
[apple/xnu.git] / libkern / os / overflow.h
CommitLineData
490019cf
A
1/*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * Facilities for performing type- and overflow-checked arithmetic. These
31 * functions return non-zero if overflow occured, zero otherwise. In either case,
32 * the potentially overflowing operation is fully performed, mod the size of the
33 * output type. See:
34 * http://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins
35 * for full details.
36 *
37 * The compiler enforces that users of os_*_overflow() check the return value to
38 * determine whether overflow occured.
39 */
40
41#ifndef _OS_OVERFLOW_H
42#define _OS_OVERFLOW_H
43
44#include <sys/cdefs.h>
45
46/* compile-time assertion that 'x' and 'y' are equivalent types */
47#define __OS_TYPE_CHECK(x, y) do { \
48 _Static_assert(__builtin_types_compatible_p(typeof(x),typeof(y)), \
49 "overflow arithmetic: incompatible types"); \
50} while (0)
51
52#define __os_add_overflow_func(T,U,V) _Generic((T), \
53 unsigned: __builtin_uadd_overflow, \
54 unsigned long: __builtin_uaddl_overflow, \
55 unsigned long long: __builtin_uaddll_overflow, \
56 int: __builtin_sadd_overflow, \
57 long: __builtin_saddl_overflow, \
58 long long: __builtin_saddll_overflow \
59 )(T,U,V)
60
61#define __os_sub_overflow_func(T,U,V) _Generic((T), \
62 unsigned: __builtin_usub_overflow, \
63 unsigned long: __builtin_usubl_overflow, \
64 unsigned long long: __builtin_usubll_overflow, \
65 int: __builtin_ssub_overflow, \
66 long: __builtin_ssubl_overflow, \
67 long long: __builtin_ssubll_overflow \
68 )(T,U,V)
69
70#define __os_mul_overflow_func(T,U,V) _Generic((T), \
71 unsigned: __builtin_umul_overflow, \
72 unsigned long: __builtin_umull_overflow, \
73 unsigned long long: __builtin_umulll_overflow, \
74 int: __builtin_smul_overflow, \
75 long: __builtin_smull_overflow, \
76 long long: __builtin_smulll_overflow \
77 )(T,U,V)
78
79int __header_always_inline __attribute__((__warn_unused_result__))
80__os_warn_unused(const int x)
81{
82 return x;
83}
84
85#define os_add_overflow(a, b, res) __os_warn_unused(({ \
86 __OS_TYPE_CHECK((a), (b)); \
87 __OS_TYPE_CHECK((b), *(res)); \
88 __os_add_overflow_func((a), (b), (res)); \
89}))
90
91#define os_add3_overflow(a, b, c, res) __os_warn_unused(({ \
92 typeof(a) _tmp; \
93 int _s, _t; \
94 _s = os_add_overflow((a), (b), &_tmp); \
95 _t = os_add_overflow((c), _tmp, (res)); \
96 _s | _t; \
97}))
98
99#define os_sub_overflow(a, b, res) __os_warn_unused(({ \
100 __OS_TYPE_CHECK((a), (b)); \
101 __OS_TYPE_CHECK((b), *(res)); \
102 __os_sub_overflow_func((a), (b), (res)); \
103}))
104
105#define os_mul_overflow(a, b, res) __os_warn_unused(({ \
106 __OS_TYPE_CHECK((a), (b)); \
107 __OS_TYPE_CHECK((b), *(res)); \
108 __os_mul_overflow_func((a), (b), (res)); \
109}))
110
111#endif /* _OS_OVERFLOW_H */