File: checked-overflow.h

package info (click to toggle)
libnbd 1.22.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: ansic: 53,855; ml: 12,311; sh: 8,499; python: 4,595; makefile: 2,902; perl: 165; cpp: 24
file content (208 lines) | stat: -rw-r--r-- 8,762 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* nbdkit
 * Copyright Red Hat
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Red Hat nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* This header file defines macros and functions for checking overflow in
 * common integer arithmetic operations.
 *
 * The macros use:
 * - the "statement expression" GCC extension,
 * - the "typeof" GCC extension,
 * - the __builtin_add_overflow() and __builtin_mul_overflow() GCC/Clang
 *   built-ins.
 *
 * If either built-in is unavailable, the corresponding macro replaces it with
 * a call to an inline C function.
 */

#ifndef NBDKIT_CHECKED_OVERFLOW_H
#define NBDKIT_CHECKED_OVERFLOW_H

#if !defined (__GNUC__) && !defined (__clang__)
#error "this file may need to be ported to your compiler"
#endif

#include <stdbool.h>
#include <stdint.h>

#include "static-assert.h"
#include "unique-name.h"

/* Add "a" and "b", both of (possibly different) unsigned integer types, and
 * store the sum in "*r", which must also have some unsigned integer type.
 *
 * Each macro argument is evaluated exactly once, as long as it does not have
 * variably modified type.
 *
 * The macro evaluates to "false" if "*r" can represent the mathematical sum.
 * Otherwise, the macro evaluates to "true", and the low order bits of the
 * mathematical sum are stored to "*r".
 */
#if HAVE_DECL___BUILTIN_ADD_OVERFLOW
#define ADD_OVERFLOW(a, b, r) ADD_OVERFLOW_BUILTIN ((a), (b), (r))
#else
#define ADD_OVERFLOW(a, b, r) ADD_OVERFLOW_FALLBACK ((a), (b), (r))
#endif

/* Multiply "a" and "b", both of (possibly different) unsigned integer types,
 * and store the product in "*r", which must also have some unsigned integer
 * type.
 *
 * Each macro argument is evaluated exactly once, as long as it does not have
 * variably modified type.
 *
 * The macro evaluates to "false" if "*r" can represent the mathematical
 * product. Otherwise, the macro evaluates to "true", and the low order bits of
 * the mathematical product are stored to "*r".
 */
#if HAVE_DECL___BUILTIN_MUL_OVERFLOW
#define MUL_OVERFLOW(a, b, r) MUL_OVERFLOW_BUILTIN ((a), (b), (r))
#else
#define MUL_OVERFLOW(a, b, r) MUL_OVERFLOW_FALLBACK ((a), (b), (r))
#endif

/* The ADD_OVERFLOW_BUILTIN and MUL_OVERFLOW_BUILTIN function-like macros
 * enforce the unsignedness of all their operands even though the underlying
 * compiler built-ins, __builtin_add_overflow() and __builtin_mul_overflow(),
 * don't depend on that. The explanation is that the fallback implementation
 * does depend on the unsignedness of all operands, and the codebase should
 * seamlessly build regardless of the built-in vs. fallback choice.
 *
 * Each macro argument is evaluated exactly once, as long as it does not have
 * variably modified type.
 */
#if HAVE_DECL___BUILTIN_ADD_OVERFLOW
#define ADD_OVERFLOW_BUILTIN(a, b, r)       \
  ({                                        \
    STATIC_ASSERT_UNSIGNED_INT (a);         \
    STATIC_ASSERT_UNSIGNED_INT (b);         \
    STATIC_ASSERT_UNSIGNED_INT (*(r));      \
    __builtin_add_overflow ((a), (b), (r)); \
  })
#endif

#if HAVE_DECL___BUILTIN_MUL_OVERFLOW
#define MUL_OVERFLOW_BUILTIN(a, b, r)       \
  ({                                        \
    STATIC_ASSERT_UNSIGNED_INT (a);         \
    STATIC_ASSERT_UNSIGNED_INT (b);         \
    STATIC_ASSERT_UNSIGNED_INT (*(r));      \
    __builtin_mul_overflow ((a), (b), (r)); \
  })
#endif

/* The fallback macros call inline C functions. The unsignedness of all
 * operands is enforced in order to keep the conversion to uintmax_t
 * value-preserving, and to keep the conversion back from uintmax_t independent
 * of the C language implementation.
 *
 * Each macro argument is evaluated exactly once, as long as it does not have
 * variably modified type.
 *
 * The fallback macros and the inline C functions are defined regardless of
 * HAVE_DECL___BUILTIN_ADD_OVERFLOW and HAVE_DECL___BUILTIN_MUL_OVERFLOW so
 * that the test suite can always test the fallback.
 */
#define ADD_OVERFLOW_FALLBACK(a, b, r)                                \
  ADD_OVERFLOW_FALLBACK_1 ((a), (b), (r),                             \
                           NBDKIT_UNIQUE_NAME (_overflow),            \
                           NBDKIT_UNIQUE_NAME (_tmp))
#define ADD_OVERFLOW_FALLBACK_1(a, b, r, overflow, tmp)               \
  ({                                                                  \
    bool overflow;                                                    \
    uintmax_t tmp;                                                    \
                                                                      \
    STATIC_ASSERT_UNSIGNED_INT (a);                                   \
    STATIC_ASSERT_UNSIGNED_INT (b);                                   \
    STATIC_ASSERT_UNSIGNED_INT (*(r));                                \
    overflow = check_add_overflow ((a), (b),                          \
                                   (typeof (*(r)))-1,                 \
                                   &tmp);                             \
    *(r) = tmp;                                                       \
    overflow;                                                         \
  })

#define MUL_OVERFLOW_FALLBACK(a, b, r)                                \
  MUL_OVERFLOW_FALLBACK_1 ((a), (b), (r),                             \
                           NBDKIT_UNIQUE_NAME (_overflow),            \
                           NBDKIT_UNIQUE_NAME (_tmp))
#define MUL_OVERFLOW_FALLBACK_1(a, b, r, overflow, tmp)               \
  ({                                                                  \
    bool overflow;                                                    \
    uintmax_t tmp;                                                    \
                                                                      \
    STATIC_ASSERT_UNSIGNED_INT (a);                                   \
    STATIC_ASSERT_UNSIGNED_INT (b);                                   \
    STATIC_ASSERT_UNSIGNED_INT (*(r));                                \
    overflow = check_mul_overflow ((a), (b),                          \
                                   (typeof (*(r)))-1,                 \
                                   &tmp);                             \
    *(r) = tmp;                                                       \
    overflow;                                                         \
  })

/* Assert, at compile time, that the expression "x" has some unsigned integer
 * type.
 *
 * The expression "x" is not evaluated, unless it has variably modified type.
 */
#define STATIC_ASSERT_UNSIGNED_INT(x) \
  STATIC_ASSERT ((typeof (x))-1 > 0, _x_has_uint_type)

/* Assign the sum "a + b" to "*r", using uintmax_t modular arithmetic.
 *
 * Return true iff the addition overflows or the result exceeds "max".
 */
static inline bool
check_add_overflow (uintmax_t a, uintmax_t b, uintmax_t max, uintmax_t *r)
{
  bool in_range;

  *r = a + b;
  in_range = a <= UINTMAX_MAX - b && *r <= max;
  return !in_range;
}

/* Assign the product "a * b" to "*r", using uintmax_t modular arithmetic.
 *
 * Return true iff the multiplication overflows or the result exceeds "max".
 */
static inline bool
check_mul_overflow (uintmax_t a, uintmax_t b, uintmax_t max, uintmax_t *r)
{
  bool in_range;

  *r = a * b;
  in_range = b == 0 || (a <= UINTMAX_MAX / b && *r <= max);
  return !in_range;
}

#endif /* NBDKIT_CHECKED_OVERFLOW_H */