blob: 201f40a27b79cbed9a28035f6d148dcd8a179bb1 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $NetBSD: cdefs.h,v 1.58 2004/12/11 05:59:00 christos Exp $ */
2
3/*
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Berkeley Software Design, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)cdefs.h 8.8 (Berkeley) 1/9/95
35 */
36
37#ifndef _SYS_CDEFS_H_
38#define _SYS_CDEFS_H_
39
Elliott Hughes50cda382017-09-14 15:30:08 -070040#include <android/api-level.h>
41#include <android/versioning.h>
42
43#define __BIONIC__ 1
44
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045/*
Elliott Hughese6c57fc2014-05-23 20:06:03 -070046 * Testing against Clang-specific extensions.
47 */
Elliott Hughese6c57fc2014-05-23 20:06:03 -070048#ifndef __has_extension
49#define __has_extension __has_feature
50#endif
51#ifndef __has_feature
52#define __has_feature(x) 0
53#endif
54#ifndef __has_include
55#define __has_include(x) 0
56#endif
57#ifndef __has_builtin
58#define __has_builtin(x) 0
59#endif
Nick Kralevich1aaa1782014-10-06 14:49:00 -070060#ifndef __has_attribute
61#define __has_attribute(x) 0
62#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -070063
Elliott Hughes998f2d52014-12-19 19:30:11 -080064#define __strong_alias(alias, sym) \
65 __asm__(".global " #alias "\n" \
66 #alias " = " #sym);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067
68#if defined(__cplusplus)
Elliott Hughesd1dd7c52016-08-10 14:18:01 -070069#define __BEGIN_DECLS extern "C" {
70#define __END_DECLS }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071#else
Elliott Hughesd1dd7c52016-08-10 14:18:01 -070072#define __BEGIN_DECLS
73#define __END_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074#endif
75
Elliott Hughes5470c182016-07-22 11:36:17 -070076#if defined(__cplusplus)
77#define __BIONIC_CAST(_k,_t,_v) (_k<_t>(_v))
78#else
79#define __BIONIC_CAST(_k,_t,_v) ((_t) (_v))
80#endif
81
Dan Alberta613d0d2017-10-05 16:39:33 -070082#define __BIONIC_ALIGN(__value, __alignment) (((__value) + (__alignment)-1) & ~((__alignment)-1))
83
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084/*
85 * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
86 * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
87 * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
88 * in between its arguments. __CONCAT can also concatenate double-quoted
89 * strings produced by the __STRING macro, but this only works with ANSI C.
90 */
91
92#define ___STRING(x) __STRING(x)
93#define ___CONCAT(x,y) __CONCAT(x,y)
94
David 'Digit' Turner8eda21b2011-03-17 10:56:25 +010095#if defined(__STDC__) || defined(__cplusplus)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096#define __P(protos) protos /* full-blown ANSI C */
97#define __CONCAT(x,y) x ## y
98#define __STRING(x) #x
99
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100#if defined(__cplusplus)
101#define __inline inline /* convert to C++ keyword */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102#endif /* !__cplusplus */
103
104#else /* !(__STDC__ || __cplusplus) */
105#define __P(protos) () /* traditional C preprocessor */
106#define __CONCAT(x,y) x/**/y
107#define __STRING(x) "x"
108
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109#endif /* !(__STDC__ || __cplusplus) */
110
Elliott Hughesf4840502016-06-06 17:35:53 -0700111#define __always_inline __attribute__((__always_inline__))
Elliott Hughes95fa0612016-09-28 12:29:52 -0700112#define __attribute_const__ __attribute__((__const__))
113#define __attribute_pure__ __attribute__((__pure__))
Elliott Hughes44ddb312015-09-22 13:00:21 -0700114#define __dead __attribute__((__noreturn__))
Elliott Hughesf4840502016-06-06 17:35:53 -0700115#define __noreturn __attribute__((__noreturn__))
Elliott Hughes95fa0612016-09-28 12:29:52 -0700116#define __mallocfunc __attribute__((__malloc__))
Elliott Hughesf4840502016-06-06 17:35:53 -0700117#define __packed __attribute__((__packed__))
Elliott Hughesf4840502016-06-06 17:35:53 -0700118#define __unused __attribute__((__unused__))
119#define __used __attribute__((__used__))
Elliott Hughes06040fd2013-07-09 13:25:03 -0700120
Elliott Hughes9eb3ae12016-06-30 09:12:40 -0700121#define __printflike(x, y) __attribute__((__format__(printf, x, y)))
122#define __scanflike(x, y) __attribute__((__format__(scanf, x, y)))
Elliott Hughes3376c232018-02-13 23:14:12 -0800123#define __strftimelike(x) __attribute__((__format__(strftime, x, 0)))
Elliott Hughesd04c1832013-05-14 16:08:43 -0700124
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125/*
Elliott Hughesf4840502016-06-06 17:35:53 -0700126 * GNU C version 2.96 added explicit branch prediction so that
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127 * the CPU back-end can hint the processor and also so that
128 * code blocks can be reordered such that the predicted path
129 * sees a more linear flow, thus improving cache behavior, etc.
130 *
131 * The following two macros provide us with a way to use this
132 * compiler feature. Use __predict_true() if you expect the expression
133 * to evaluate to true, and __predict_false() if you expect the
134 * expression to evaluate to false.
135 *
136 * A few notes about usage:
137 *
138 * * Generally, __predict_false() error condition checks (unless
139 * you have some _strong_ reason to do otherwise, in which case
140 * document it), and/or __predict_true() `no-error' condition
141 * checks, assuming you want to optimize for the no-error case.
142 *
143 * * Other than that, if you don't know the likelihood of a test
144 * succeeding from empirical or other `hard' evidence, don't
145 * make predictions.
146 *
147 * * These are meant to be used in places that are run `a lot'.
148 * It is wasteful to make predictions in code that is run
149 * seldomly (e.g. at subsystem initialization time) as the
150 * basic block reordering that this affects can often generate
151 * larger code.
152 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153#define __predict_true(exp) __builtin_expect((exp) != 0, 1)
154#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155
Elliott Hughes24fad012013-02-04 13:44:14 -0800156#define __wur __attribute__((__warn_unused_result__))
Elliott Hughes24fad012013-02-04 13:44:14 -0800157
Josh Gao8ba44242016-04-25 14:53:13 -0700158#ifdef __clang__
George Burgess IV7cc779f2017-02-09 00:00:31 -0800159# define __errorattr(msg) __attribute__((unavailable(msg)))
160# define __warnattr(msg) __attribute__((deprecated(msg)))
161# define __warnattr_real(msg) __attribute__((deprecated(msg)))
162# define __enable_if(cond, msg) __attribute__((enable_if(cond, msg)))
George Burgess IVb97d7dd2017-07-31 16:57:15 -0700163# define __clang_error_if(cond, msg) __attribute__((diagnose_if(cond, msg, "error")))
164# define __clang_warning_if(cond, msg) __attribute__((diagnose_if(cond, msg, "warning")))
Josh Gao8ba44242016-04-25 14:53:13 -0700165#else
George Burgess IV7cc779f2017-02-09 00:00:31 -0800166# define __errorattr(msg) __attribute__((__error__(msg)))
167# define __warnattr(msg) __attribute__((__warning__(msg)))
168# define __warnattr_real __warnattr
169/* enable_if doesn't exist on other compilers; give an error if it's used. */
George Burgess IVb97d7dd2017-07-31 16:57:15 -0700170/* diagnose_if doesn't exist either, but it's often tagged on non-clang-specific functions */
171# define __clang_error_if(cond, msg)
172# define __clang_warning_if(cond, msg)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800173
174/* errordecls really don't work as well in clang as they do in GCC. */
175# define __errordecl(name, msg) extern void name(void) __errorattr(msg)
Josh Gao8ba44242016-04-25 14:53:13 -0700176#endif
177
George Burgess IV7cc779f2017-02-09 00:00:31 -0800178#if defined(ANDROID_STRICT)
179/*
180 * For things that are sketchy, but not necessarily an error. FIXME: Enable
181 * this.
182 */
183# define __warnattr_strict(msg) /* __warnattr(msg) */
184#else
185# define __warnattr_strict(msg)
186#endif
Yabin Cuic9d09432015-02-13 10:52:35 -0800187
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188/*
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700189 * Some BSD source needs these macros.
190 * Originally they embedded the rcs versions of each source file
191 * in the generated binary. We strip strings during build anyway,.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192 */
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700193#define __IDSTRING(_prefix,_s) /* nothing */
194#define __COPYRIGHT(_s) /* nothing */
195#define __FBSDID(_s) /* nothing */
196#define __RCSID(_s) /* nothing */
197#define __SCCSID(_s) /* nothing */
David 'Digit' Turner3527fd62010-06-14 17:18:35 -0700198
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700199/*
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700200 * With bionic, you always get all C and POSIX API.
Elliott Hughesfc8974b2014-09-04 08:52:38 -0700201 *
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700202 * If you want BSD and/or GNU extensions, _BSD_SOURCE and/or _GNU_SOURCE are
203 * expected to be defined by callers before *any* standard header file is
204 * included.
205 *
206 * In our header files we test against __USE_BSD and __USE_GNU.
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700207 */
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700208#if defined(_GNU_SOURCE)
Elliott Hughesa3481742017-11-28 14:47:17 -0800209# define __USE_BSD 1
210# define __USE_GNU 1
Elliott Hughesfc8974b2014-09-04 08:52:38 -0700211#endif
212
213#if defined(_BSD_SOURCE)
Elliott Hughesa3481742017-11-28 14:47:17 -0800214# define __USE_BSD 1
Elliott Hughesf4c948a2014-08-19 11:16:41 -0700215#endif
216
Elliott Hughesa3481742017-11-28 14:47:17 -0800217/*
218 * _FILE_OFFSET_BITS 64 support.
219 * See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md
220 */
Dan Albertf5605372017-07-17 14:05:44 -0700221#if !defined(__LP64__) && defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
Elliott Hughesa3481742017-11-28 14:47:17 -0800222# define __USE_FILE_OFFSET64 1
Elliott Hughes7c5a8512017-07-20 10:00:28 -0700223/*
224 * Note that __RENAME_IF_FILE_OFFSET64 is only valid if the off_t and off64_t
225 * functions were both added at the same API level because if you use this,
226 * you only have one declaration to attach __INTRODUCED_IN to.
227 */
Elliott Hughesa3481742017-11-28 14:47:17 -0800228# define __RENAME_IF_FILE_OFFSET64(func) __RENAME(func)
Dan Albertf5605372017-07-17 14:05:44 -0700229#else
Elliott Hughesa3481742017-11-28 14:47:17 -0800230# define __RENAME_IF_FILE_OFFSET64(func)
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800231#endif
232
Elliott Hughes50cda382017-09-14 15:30:08 -0700233/*
234 * For LP32, `long double` == `double`. Historically many `long double` functions were incorrect
235 * on x86, missing on most architectures, and even if they are present and correct, linking to
236 * them just bloats your ELF file by adding extra relocations. The __BIONIC_LP32_USE_LONG_DOUBLE
237 * macro lets us test the headers both ways (and adds an escape valve).
238 *
239 * Note that some functions have their __RENAME_LDBL commented out as a sign that although we could
240 * use __RENAME_LDBL it would actually cause the function to be introduced later because the
241 * `long double` variant appeared before the `double` variant.
242 */
243#if defined(__LP64__) || defined(__BIONIC_LP32_USE_LONG_DOUBLE)
244#define __RENAME_LDBL(rewrite,rewrite_api_level,regular_api_level) __INTRODUCED_IN(regular_api_level)
245#else
246#define __RENAME_LDBL(rewrite,rewrite_api_level,regular_api_level) __RENAME(rewrite) __INTRODUCED_IN(rewrite_api_level)
247#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
Elliott Hughes25f17e42018-02-12 15:48:01 -0800249/*
250 * On all architectures, `struct stat` == `struct stat64`, but LP32 didn't gain the *64 functions
251 * until API level 21.
252 */
253#if defined(__LP64__) || defined(__BIONIC_LP32_USE_STAT64)
254#define __RENAME_STAT64(rewrite,rewrite_api_level,regular_api_level) __INTRODUCED_IN(regular_api_level)
255#else
256#define __RENAME_STAT64(rewrite,rewrite_api_level,regular_api_level) __RENAME(rewrite) __INTRODUCED_IN(rewrite_api_level)
257#endif
258
Elliott Hughescaa15bf2014-04-03 13:59:14 -0700259/* glibc compatibility. */
Josh Gaob36efa42016-09-15 13:55:41 -0700260#if defined(__LP64__)
Elliott Hughescaa15bf2014-04-03 13:59:14 -0700261#define __WORDSIZE 64
262#else
263#define __WORDSIZE 32
264#endif
265
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700266/*
267 * When _FORTIFY_SOURCE is defined, automatic bounds checking is
268 * added to commonly used libc functions. If a buffer overrun is
269 * detected, the program is safely aborted.
270 *
Elliott Hughesef2b2fe2017-04-16 08:50:58 -0700271 * https://android-developers.googleblog.com/2017/04/fortify-in-android.html
Nick Kralevichbd8e6742013-08-28 13:22:52 -0700272 */
George Burgess IV7cc779f2017-02-09 00:00:31 -0800273
274#define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1)
275
George Burgess IVd9551db2017-08-17 18:51:02 -0700276#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
277# if defined(__clang__)
George Burgess IVe5d66eb2017-10-30 21:41:22 -0700278/*
279 * FORTIFY's _chk functions effectively disable ASAN's stdlib interceptors.
280 * Additionally, the static analyzer/clang-tidy try to pattern match some
281 * standard library functions, and FORTIFY sometimes interferes with this. So,
282 * we turn FORTIFY off in both cases.
283 */
284# if !__has_feature(address_sanitizer) && !defined(__clang_analyzer__)
George Burgess IVd9551db2017-08-17 18:51:02 -0700285# define __BIONIC_FORTIFY 1
286# endif
287# elif defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
288# define __BIONIC_FORTIFY 1
289# endif
290#endif
291
Andreas Gampe00a6d5f2018-04-13 13:52:10 -0700292// As we move some FORTIFY checks to be always on, __bos needs to be
293// always available.
George Burgess IVd9551db2017-08-17 18:51:02 -0700294#if defined(__BIONIC_FORTIFY)
Elliott Hughes44ddb312015-09-22 13:00:21 -0700295# if _FORTIFY_SOURCE == 2
George Burgess IV7cc779f2017-02-09 00:00:31 -0800296# define __bos_level 1
Elliott Hughes44ddb312015-09-22 13:00:21 -0700297# else
George Burgess IV7cc779f2017-02-09 00:00:31 -0800298# define __bos_level 0
Elliott Hughes44ddb312015-09-22 13:00:21 -0700299# endif
Andreas Gampe00a6d5f2018-04-13 13:52:10 -0700300#else
301# define __bos_level 0
302#endif
303
304#define __bosn(s, n) __builtin_object_size((s), (n))
305#define __bos(s) __bosn((s), __bos_level)
306
307#if defined(__BIONIC_FORTIFY)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800308# define __bos0(s) __bosn((s), 0)
Elliott Hughes5470c182016-07-22 11:36:17 -0700309# if defined(__clang__)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800310# define __pass_object_size_n(n) __attribute__((pass_object_size(n)))
311/*
312 * FORTIFY'ed functions all have either enable_if or pass_object_size, which
313 * makes taking their address impossible. Saying (&read)(foo, bar, baz); will
314 * therefore call the unFORTIFYed version of read.
315 */
316# define __call_bypassing_fortify(fn) (&fn)
317/*
318 * Because clang-FORTIFY uses overloads, we can't mark functions as `extern
319 * inline` without making them available externally.
320 */
321# define __BIONIC_FORTIFY_INLINE static __inline__ __always_inline
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -0800322/*
323 * We should use __BIONIC_FORTIFY_VARIADIC instead of __BIONIC_FORTIFY_INLINE
324 * for variadic functions because compilers cannot inline them.
325 * The __always_inline attribute is useless, misleading, and could trigger
326 * clang compiler bug to incorrectly inline variadic functions.
327 */
328# define __BIONIC_FORTIFY_VARIADIC static __inline__
George Burgess IV7cc779f2017-02-09 00:00:31 -0800329/* Error functions don't have bodies, so they can just be static. */
330# define __BIONIC_ERROR_FUNCTION_VISIBILITY static
Elliott Hughes5470c182016-07-22 11:36:17 -0700331# else
George Burgess IV7cc779f2017-02-09 00:00:31 -0800332/*
333 * Where they can, GCC and clang-style FORTIFY share implementations.
334 * So, make these nops in GCC.
335 */
336# define __pass_object_size_n(n)
337# define __call_bypassing_fortify(fn) (fn)
338/* __BIONIC_FORTIFY_NONSTATIC_INLINE is pointless in GCC's FORTIFY */
Elliott Hughes5470c182016-07-22 11:36:17 -0700339# define __BIONIC_FORTIFY_INLINE extern __inline__ __always_inline __attribute__((gnu_inline)) __attribute__((__artificial__))
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -0800340/* __always_inline is probably okay and ignored by gcc in __BIONIC_FORTIFY_VARIADIC */
341# define __BIONIC_FORTIFY_VARIADIC __BIONIC_FORTIFY_INLINE
Elliott Hughes5470c182016-07-22 11:36:17 -0700342# endif
George Burgess IVbd3d2082017-04-04 17:34:02 -0700343#else
344/* Further increase sharing for some inline functions */
345# define __pass_object_size_n(n)
Nick Kralevich1aaa1782014-10-06 14:49:00 -0700346#endif
George Burgess IVbd3d2082017-04-04 17:34:02 -0700347#define __pass_object_size __pass_object_size_n(__bos_level)
348#define __pass_object_size0 __pass_object_size_n(0)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800349
George Burgess IV1de2e352017-08-15 11:03:07 -0700350#if defined(__BIONIC_FORTIFY) || defined(__BIONIC_DECLARE_FORTIFY_HELPERS)
George Burgess IVb97049c2017-07-24 15:05:05 -0700351# define __BIONIC_INCLUDE_FORTIFY_HEADERS 1
352#endif
353
George Burgess IVde45dcb2018-03-16 14:15:01 -0700354#if defined(__clang__)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800355# define __overloadable __attribute__((overloadable))
George Burgess IV7cc779f2017-02-09 00:00:31 -0800356#else
357# define __overloadable
George Burgess IV7cc779f2017-02-09 00:00:31 -0800358#endif
Nick Kralevich71a18dd2012-06-07 14:01:26 -0700359
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700360/* Used to tag non-static symbols that are private and never exposed by the shared library. */
361#define __LIBC_HIDDEN__ __attribute__((visibility("hidden")))
362
Elliott Hughesf47514d2016-07-19 13:56:46 -0700363/*
364 * Used to tag symbols that should be hidden for 64-bit,
365 * but visible to preserve binary compatibility for LP32.
366 */
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700367#ifdef __LP64__
Elliott Hughesf47514d2016-07-19 13:56:46 -0700368#define __LIBC32_LEGACY_PUBLIC__ __attribute__((visibility("hidden")))
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700369#else
Elliott Hughesf47514d2016-07-19 13:56:46 -0700370#define __LIBC32_LEGACY_PUBLIC__ __attribute__((visibility("default")))
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700371#endif
372
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700373/* Used to rename functions so that the compiler emits a call to 'x' rather than the function this was applied to. */
374#define __RENAME(x) __asm__(#x)
375
Daniel Micayfed26592015-07-18 13:55:51 -0400376#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
Josh Gaob36efa42016-09-15 13:55:41 -0700377#if defined(__LP64__)
Daniel Micayfed26592015-07-18 13:55:51 -0400378#define __size_mul_overflow(a, b, result) __builtin_umull_overflow(a, b, result)
379#else
380#define __size_mul_overflow(a, b, result) __builtin_umul_overflow(a, b, result)
381#endif
382#else
Xia Yang6b45d572016-01-20 18:34:59 -0800383extern __inline__ __always_inline __attribute__((gnu_inline))
384int __size_mul_overflow(__SIZE_TYPE__ a, __SIZE_TYPE__ b, __SIZE_TYPE__ *result) {
Daniel Micayfed26592015-07-18 13:55:51 -0400385 *result = a * b;
386 static const __SIZE_TYPE__ mul_no_overflow = 1UL << (sizeof(__SIZE_TYPE__) * 4);
387 return (a >= mul_no_overflow || b >= mul_no_overflow) && a > 0 && (__SIZE_TYPE__)-1 / a < b;
388}
389#endif
390
George Burgess IV7cc779f2017-02-09 00:00:31 -0800391#if defined(__clang__)
392/*
393 * Used when we need to check for overflow when multiplying x and y. This
394 * should only be used where __size_mul_overflow can not work, because it makes
395 * assumptions that __size_mul_overflow doesn't (x and y are positive, ...),
396 * *and* doesn't make use of compiler intrinsics, so it's probably slower than
397 * __size_mul_overflow.
398 */
399#define __unsafe_check_mul_overflow(x, y) ((__SIZE_TYPE__)-1 / (x) < (y))
400#endif
Dan Albert3e6929d2016-07-26 16:27:31 -0700401
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402#endif /* !_SYS_CDEFS_H_ */