Skip to content

Commit 4eb6bd5

Browse files
nickdesaulnierstorvalds
authored andcommitted
compiler.h: drop fallback overflow checkers
Once upgrading the minimum supported version of GCC to 5.1, we can drop the fallback code for !COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW. This is effectively a revert of commit f090782 ("compiler.h: enable builtin overflow checkers and add fallback code") Link: ClangBuiltLinux/linux#1438 (comment) Suggested-by: Rasmus Villemoes <[email protected]> Signed-off-by: Nick Desaulniers <[email protected]> Acked-by: Kees Cook <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 76ae847 commit 4eb6bd5

File tree

5 files changed

+6
-293
lines changed

5 files changed

+6
-293
lines changed

include/linux/compiler-clang.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,6 @@
6262
#define __no_sanitize_coverage
6363
#endif
6464

65-
/*
66-
* Not all versions of clang implement the type-generic versions
67-
* of the builtin overflow checkers. Fortunately, clang implements
68-
* __has_builtin allowing us to avoid awkward version
69-
* checks. Unfortunately, we don't know which version of gcc clang
70-
* pretends to be, so the macro may or may not be defined.
71-
*/
72-
#if __has_builtin(__builtin_mul_overflow) && \
73-
__has_builtin(__builtin_add_overflow) && \
74-
__has_builtin(__builtin_sub_overflow)
75-
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
76-
#endif
77-
7865
#if __has_feature(shadow_call_stack)
7966
# define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
8067
#endif

include/linux/compiler-gcc.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@
128128
#define __no_sanitize_coverage
129129
#endif
130130

131-
#if GCC_VERSION >= 50100
132-
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
133-
#endif
134-
135131
/*
136132
* Turn individual warnings and errors on and off locally, depending
137133
* on version.

include/linux/overflow.h

Lines changed: 3 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
#include <linux/limits.h>
77

88
/*
9-
* In the fallback code below, we need to compute the minimum and
10-
* maximum values representable in a given type. These macros may also
11-
* be useful elsewhere, so we provide them outside the
12-
* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW block.
13-
*
14-
* It would seem more obvious to do something like
9+
* We need to compute the minimum and maximum values representable in a given
10+
* type. These macros may also be useful elsewhere. It would seem more obvious
11+
* to do something like:
1512
*
1613
* #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
1714
* #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
@@ -54,7 +51,6 @@ static inline bool __must_check __must_check_overflow(bool overflow)
5451
return unlikely(overflow);
5552
}
5653

57-
#ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
5854
/*
5955
* For simplicity and code hygiene, the fallback code below insists on
6056
* a, b and *d having the same type (similar to the min() and max()
@@ -90,134 +86,6 @@ static inline bool __must_check __must_check_overflow(bool overflow)
9086
__builtin_mul_overflow(__a, __b, __d); \
9187
}))
9288

93-
#else
94-
95-
96-
/* Checking for unsigned overflow is relatively easy without causing UB. */
97-
#define __unsigned_add_overflow(a, b, d) ({ \
98-
typeof(a) __a = (a); \
99-
typeof(b) __b = (b); \
100-
typeof(d) __d = (d); \
101-
(void) (&__a == &__b); \
102-
(void) (&__a == __d); \
103-
*__d = __a + __b; \
104-
*__d < __a; \
105-
})
106-
#define __unsigned_sub_overflow(a, b, d) ({ \
107-
typeof(a) __a = (a); \
108-
typeof(b) __b = (b); \
109-
typeof(d) __d = (d); \
110-
(void) (&__a == &__b); \
111-
(void) (&__a == __d); \
112-
*__d = __a - __b; \
113-
__a < __b; \
114-
})
115-
/*
116-
* If one of a or b is a compile-time constant, this avoids a division.
117-
*/
118-
#define __unsigned_mul_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 = __a * __b; \
125-
__builtin_constant_p(__b) ? \
126-
__b > 0 && __a > type_max(typeof(__a)) / __b : \
127-
__a > 0 && __b > type_max(typeof(__b)) / __a; \
128-
})
129-
130-
/*
131-
* For signed types, detecting overflow is much harder, especially if
132-
* we want to avoid UB. But the interface of these macros is such that
133-
* we must provide a result in *d, and in fact we must produce the
134-
* result promised by gcc's builtins, which is simply the possibly
135-
* wrapped-around value. Fortunately, we can just formally do the
136-
* operations in the widest relevant unsigned type (u64) and then
137-
* truncate the result - gcc is smart enough to generate the same code
138-
* with and without the (u64) casts.
139-
*/
140-
141-
/*
142-
* Adding two signed integers can overflow only if they have the same
143-
* sign, and overflow has happened iff the result has the opposite
144-
* sign.
145-
*/
146-
#define __signed_add_overflow(a, b, d) ({ \
147-
typeof(a) __a = (a); \
148-
typeof(b) __b = (b); \
149-
typeof(d) __d = (d); \
150-
(void) (&__a == &__b); \
151-
(void) (&__a == __d); \
152-
*__d = (u64)__a + (u64)__b; \
153-
(((~(__a ^ __b)) & (*__d ^ __a)) \
154-
& type_min(typeof(__a))) != 0; \
155-
})
156-
157-
/*
158-
* Subtraction is similar, except that overflow can now happen only
159-
* when the signs are opposite. In this case, overflow has happened if
160-
* the result has the opposite sign of a.
161-
*/
162-
#define __signed_sub_overflow(a, b, d) ({ \
163-
typeof(a) __a = (a); \
164-
typeof(b) __b = (b); \
165-
typeof(d) __d = (d); \
166-
(void) (&__a == &__b); \
167-
(void) (&__a == __d); \
168-
*__d = (u64)__a - (u64)__b; \
169-
((((__a ^ __b)) & (*__d ^ __a)) \
170-
& type_min(typeof(__a))) != 0; \
171-
})
172-
173-
/*
174-
* Signed multiplication is rather hard. gcc always follows C99, so
175-
* division is truncated towards 0. This means that we can write the
176-
* overflow check like this:
177-
*
178-
* (a > 0 && (b > MAX/a || b < MIN/a)) ||
179-
* (a < -1 && (b > MIN/a || b < MAX/a) ||
180-
* (a == -1 && b == MIN)
181-
*
182-
* The redundant casts of -1 are to silence an annoying -Wtype-limits
183-
* (included in -Wextra) warning: When the type is u8 or u16, the
184-
* __b_c_e in check_mul_overflow obviously selects
185-
* __unsigned_mul_overflow, but unfortunately gcc still parses this
186-
* code and warns about the limited range of __b.
187-
*/
188-
189-
#define __signed_mul_overflow(a, b, d) ({ \
190-
typeof(a) __a = (a); \
191-
typeof(b) __b = (b); \
192-
typeof(d) __d = (d); \
193-
typeof(a) __tmax = type_max(typeof(a)); \
194-
typeof(a) __tmin = type_min(typeof(a)); \
195-
(void) (&__a == &__b); \
196-
(void) (&__a == __d); \
197-
*__d = (u64)__a * (u64)__b; \
198-
(__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \
199-
(__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \
200-
(__b == (typeof(__b))-1 && __a == __tmin); \
201-
})
202-
203-
204-
#define check_add_overflow(a, b, d) __must_check_overflow( \
205-
__builtin_choose_expr(is_signed_type(typeof(a)), \
206-
__signed_add_overflow(a, b, d), \
207-
__unsigned_add_overflow(a, b, d)))
208-
209-
#define check_sub_overflow(a, b, d) __must_check_overflow( \
210-
__builtin_choose_expr(is_signed_type(typeof(a)), \
211-
__signed_sub_overflow(a, b, d), \
212-
__unsigned_sub_overflow(a, b, d)))
213-
214-
#define check_mul_overflow(a, b, d) __must_check_overflow( \
215-
__builtin_choose_expr(is_signed_type(typeof(a)), \
216-
__signed_mul_overflow(a, b, d), \
217-
__unsigned_mul_overflow(a, b, d)))
218-
219-
#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
220-
22189
/** check_shl_overflow() - Calculate a left-shifted value and check overflow
22290
*
22391
* @a: Value to be shifted

tools/include/linux/compiler-gcc.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,3 @@
3838
#endif
3939
#define __printf(a, b) __attribute__((format(printf, a, b)))
4040
#define __scanf(a, b) __attribute__((format(scanf, a, b)))
41-
42-
#if GCC_VERSION >= 50100
43-
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
44-
#endif

tools/include/linux/overflow.h

Lines changed: 3 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
#include <linux/compiler.h>
66

77
/*
8-
* In the fallback code below, we need to compute the minimum and
9-
* maximum values representable in a given type. These macros may also
10-
* be useful elsewhere, so we provide them outside the
11-
* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW block.
12-
*
13-
* It would seem more obvious to do something like
8+
* We need to compute the minimum and maximum values representable in a given
9+
* type. These macros may also be useful elsewhere. It would seem more obvious
10+
* to do something like:
1411
*
1512
* #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
1613
* #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
@@ -36,8 +33,6 @@
3633
#define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
3734
#define type_min(T) ((T)((T)-type_max(T)-(T)1))
3835

39-
40-
#ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
4136
/*
4237
* For simplicity and code hygiene, the fallback code below insists on
4338
* a, b and *d having the same type (similar to the min() and max()
@@ -73,135 +68,6 @@
7368
__builtin_mul_overflow(__a, __b, __d); \
7469
})
7570

76-
#else
77-
78-
79-
/* Checking for unsigned overflow is relatively easy without causing UB. */
80-
#define __unsigned_add_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-
*__d < __a; \
88-
})
89-
#define __unsigned_sub_overflow(a, b, d) ({ \
90-
typeof(a) __a = (a); \
91-
typeof(b) __b = (b); \
92-
typeof(d) __d = (d); \
93-
(void) (&__a == &__b); \
94-
(void) (&__a == __d); \
95-
*__d = __a - __b; \
96-
__a < __b; \
97-
})
98-
/*
99-
* If one of a or b is a compile-time constant, this avoids a division.
100-
*/
101-
#define __unsigned_mul_overflow(a, b, d) ({ \
102-
typeof(a) __a = (a); \
103-
typeof(b) __b = (b); \
104-
typeof(d) __d = (d); \
105-
(void) (&__a == &__b); \
106-
(void) (&__a == __d); \
107-
*__d = __a * __b; \
108-
__builtin_constant_p(__b) ? \
109-
__b > 0 && __a > type_max(typeof(__a)) / __b : \
110-
__a > 0 && __b > type_max(typeof(__b)) / __a; \
111-
})
112-
113-
/*
114-
* For signed types, detecting overflow is much harder, especially if
115-
* we want to avoid UB. But the interface of these macros is such that
116-
* we must provide a result in *d, and in fact we must produce the
117-
* result promised by gcc's builtins, which is simply the possibly
118-
* wrapped-around value. Fortunately, we can just formally do the
119-
* operations in the widest relevant unsigned type (u64) and then
120-
* truncate the result - gcc is smart enough to generate the same code
121-
* with and without the (u64) casts.
122-
*/
123-
124-
/*
125-
* Adding two signed integers can overflow only if they have the same
126-
* sign, and overflow has happened iff the result has the opposite
127-
* sign.
128-
*/
129-
#define __signed_add_overflow(a, b, d) ({ \
130-
typeof(a) __a = (a); \
131-
typeof(b) __b = (b); \
132-
typeof(d) __d = (d); \
133-
(void) (&__a == &__b); \
134-
(void) (&__a == __d); \
135-
*__d = (u64)__a + (u64)__b; \
136-
(((~(__a ^ __b)) & (*__d ^ __a)) \
137-
& type_min(typeof(__a))) != 0; \
138-
})
139-
140-
/*
141-
* Subtraction is similar, except that overflow can now happen only
142-
* when the signs are opposite. In this case, overflow has happened if
143-
* the result has the opposite sign of a.
144-
*/
145-
#define __signed_sub_overflow(a, b, d) ({ \
146-
typeof(a) __a = (a); \
147-
typeof(b) __b = (b); \
148-
typeof(d) __d = (d); \
149-
(void) (&__a == &__b); \
150-
(void) (&__a == __d); \
151-
*__d = (u64)__a - (u64)__b; \
152-
((((__a ^ __b)) & (*__d ^ __a)) \
153-
& type_min(typeof(__a))) != 0; \
154-
})
155-
156-
/*
157-
* Signed multiplication is rather hard. gcc always follows C99, so
158-
* division is truncated towards 0. This means that we can write the
159-
* overflow check like this:
160-
*
161-
* (a > 0 && (b > MAX/a || b < MIN/a)) ||
162-
* (a < -1 && (b > MIN/a || b < MAX/a) ||
163-
* (a == -1 && b == MIN)
164-
*
165-
* The redundant casts of -1 are to silence an annoying -Wtype-limits
166-
* (included in -Wextra) warning: When the type is u8 or u16, the
167-
* __b_c_e in check_mul_overflow obviously selects
168-
* __unsigned_mul_overflow, but unfortunately gcc still parses this
169-
* code and warns about the limited range of __b.
170-
*/
171-
172-
#define __signed_mul_overflow(a, b, d) ({ \
173-
typeof(a) __a = (a); \
174-
typeof(b) __b = (b); \
175-
typeof(d) __d = (d); \
176-
typeof(a) __tmax = type_max(typeof(a)); \
177-
typeof(a) __tmin = type_min(typeof(a)); \
178-
(void) (&__a == &__b); \
179-
(void) (&__a == __d); \
180-
*__d = (u64)__a * (u64)__b; \
181-
(__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \
182-
(__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \
183-
(__b == (typeof(__b))-1 && __a == __tmin); \
184-
})
185-
186-
187-
#define check_add_overflow(a, b, d) \
188-
__builtin_choose_expr(is_signed_type(typeof(a)), \
189-
__signed_add_overflow(a, b, d), \
190-
__unsigned_add_overflow(a, b, d))
191-
192-
#define check_sub_overflow(a, b, d) \
193-
__builtin_choose_expr(is_signed_type(typeof(a)), \
194-
__signed_sub_overflow(a, b, d), \
195-
__unsigned_sub_overflow(a, b, d))
196-
197-
#define check_mul_overflow(a, b, d) \
198-
__builtin_choose_expr(is_signed_type(typeof(a)), \
199-
__signed_mul_overflow(a, b, d), \
200-
__unsigned_mul_overflow(a, b, d))
201-
202-
203-
#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
204-
20571
/**
20672
* array_size() - Calculate size of 2-dimensional array.
20773
*

0 commit comments

Comments
 (0)