11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*	$NetBSD: cdefs.h,v 1.58 2004/12/11 05:59:00 christos Exp $	*/
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 1991, 1993
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	The Regents of the University of California.  All rights reserved.
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This code is derived from software contributed to Berkeley by
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Berkeley Software Design, Inc.
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3. Neither the name of the University nor the names of its contributors
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    may be used to endorse or promote products derived from this software
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    without specific prior written permission.
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef	_SYS_CDEFS_H_
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	_SYS_CDEFS_H_
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
41e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes * Testing against Clang-specific extensions.
42e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes */
43e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#ifndef __has_extension
44e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#define __has_extension         __has_feature
45e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#endif
46e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#ifndef __has_feature
47e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#define __has_feature(x)        0
48e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#endif
49e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#ifndef __has_include
50e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#define __has_include(x)        0
51e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#endif
52e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#ifndef __has_builtin
53e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#define __has_builtin(x)        0
54e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes#endif
551aaa17802c92d99ae170245c2b2f15a6c27b133eNick Kralevich#ifndef __has_attribute
561aaa17802c92d99ae170245c2b2f15a6c27b133eNick Kralevich#define __has_attribute(x)      0
571aaa17802c92d99ae170245c2b2f15a6c27b133eNick Kralevich#endif
58e6c57fcb05b0dcbfa8129bb6f7d99093f4819370Elliott Hughes
59998f2d5a50adffcc0447912fb6a7af2dac7accf1Elliott Hughes#define __strong_alias(alias, sym) \
60998f2d5a50adffcc0447912fb6a7af2dac7accf1Elliott Hughes    __asm__(".global " #alias "\n" \
61998f2d5a50adffcc0447912fb6a7af2dac7accf1Elliott Hughes            #alias " = " #sym);
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(__cplusplus)
64d1dd7c58b42ebc101ee004f1102a478f8f1dd24bElliott Hughes#define __BEGIN_DECLS extern "C" {
65d1dd7c58b42ebc101ee004f1102a478f8f1dd24bElliott Hughes#define __END_DECLS }
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else
67d1dd7c58b42ebc101ee004f1102a478f8f1dd24bElliott Hughes#define __BEGIN_DECLS
68d1dd7c58b42ebc101ee004f1102a478f8f1dd24bElliott Hughes#define __END_DECLS
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
715470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#if defined(__cplusplus)
725470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#define __BIONIC_CAST(_k,_t,_v) (_k<_t>(_v))
735470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#else
745470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#define __BIONIC_CAST(_k,_t,_v) ((_t) (_v))
755470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#endif
765470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes
771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in between its arguments.  __CONCAT can also concatenate double-quoted
821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * strings produced by the __STRING macro, but this only works with ANSI C.
831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	___STRING(x)	__STRING(x)
861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	___CONCAT(x,y)	__CONCAT(x,y)
871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
888eda21baa54977e4d164a4174d06b6aa12115fb1David 'Digit' Turner#if defined(__STDC__) || defined(__cplusplus)
891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__P(protos)	protos		/* full-blown ANSI C */
901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__CONCAT(x,y)	x ## y
911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__STRING(x)	#x
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(__cplusplus)
941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__inline	inline		/* convert to C++ keyword */
951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* !__cplusplus */
961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else	/* !(__STDC__ || __cplusplus) */
981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__P(protos)	()		/* traditional C preprocessor */
991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__CONCAT(x,y)	x/**/y
1001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__STRING(x)	"x"
1011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif	/* !(__STDC__ || __cplusplus) */
1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
104f4840507b57db927c3359914649380d53e5bf7bfElliott Hughes#define __always_inline __attribute__((__always_inline__))
10595fa061678400315ee757758bf5f9146176d47a0Elliott Hughes#define __attribute_const__ __attribute__((__const__))
10695fa061678400315ee757758bf5f9146176d47a0Elliott Hughes#define __attribute_pure__ __attribute__((__pure__))
10744ddb31e75ff0cebea8249065606600185d58680Elliott Hughes#define __dead __attribute__((__noreturn__))
108f4840507b57db927c3359914649380d53e5bf7bfElliott Hughes#define __noreturn __attribute__((__noreturn__))
10995fa061678400315ee757758bf5f9146176d47a0Elliott Hughes#define __mallocfunc  __attribute__((__malloc__))
110f4840507b57db927c3359914649380d53e5bf7bfElliott Hughes#define __packed __attribute__((__packed__))
111f4840507b57db927c3359914649380d53e5bf7bfElliott Hughes#define __unused __attribute__((__unused__))
112f4840507b57db927c3359914649380d53e5bf7bfElliott Hughes#define __used __attribute__((__used__))
11306040fd75c1edff9e5ffb2b3d3e2a6e66d57c11dElliott Hughes
114baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert/*
115baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert * _Nonnull is similar to the nonnull attribute in that it will instruct
116baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert * compilers to warn the user if it can prove that a null argument is being
117baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert * passed. Unlike the nonnull attribute, this annotation indicated that a value
118baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert * *should not* be null, not that it *cannot* be null, or even that the behavior
119baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert * is undefined. The important distinction is that the optimizer will perform
120baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert * surprising optimizations like the following:
121baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *
122baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *     void foo(void*) __attribute__(nonnull, 1);
123baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *
124baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *     int bar(int* p) {
125baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *       foo(p);
126baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *
127baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *       // The following null check will be elided because nonnull attribute
128baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *       // means that, since we call foo with p, p can be assumed to not be
129baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *       // null. Thus this will crash if we are called with a null pointer.
1309eb3ae122fd9759251063ee351cb23cc769dbe61Elliott Hughes *       if (p != NULL) {
131baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *         return *p;
132baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *       }
133baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *       return 0;
134baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *     }
135baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *
136baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *     int main() {
137baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *       return bar(NULL);
138baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *     }
139baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert *
140baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert * http://clang.llvm.org/docs/AttributeReference.html#nonnull
141baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert */
142baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert#if !(defined(__clang__) && __has_feature(nullability))
143baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert#define _Nonnull
1445470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#define _Nullable
145baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert#endif
146baa2a973bd776a51bb05a8590ab05d86eea7b321Dan Albert
1479eb3ae122fd9759251063ee351cb23cc769dbe61Elliott Hughes#define __printflike(x, y) __attribute__((__format__(printf, x, y)))
1489eb3ae122fd9759251063ee351cb23cc769dbe61Elliott Hughes#define __scanflike(x, y) __attribute__((__format__(scanf, x, y)))
149d04c18397946f87ac4495351537c31a3d67257daElliott Hughes
1501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
151f4840507b57db927c3359914649380d53e5bf7bfElliott Hughes * GNU C version 2.96 added explicit branch prediction so that
1521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the CPU back-end can hint the processor and also so that
1531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * code blocks can be reordered such that the predicted path
1541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * sees a more linear flow, thus improving cache behavior, etc.
1551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The following two macros provide us with a way to use this
1571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * compiler feature.  Use __predict_true() if you expect the expression
1581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to evaluate to true, and __predict_false() if you expect the
1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * expression to evaluate to false.
1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * A few notes about usage:
1621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	* Generally, __predict_false() error condition checks (unless
1641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  you have some _strong_ reason to do otherwise, in which case
1651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  document it), and/or __predict_true() `no-error' condition
1661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  checks, assuming you want to optimize for the no-error case.
1671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	* Other than that, if you don't know the likelihood of a test
1691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  succeeding from empirical or other `hard' evidence, don't
1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  make predictions.
1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	* These are meant to be used in places that are run `a lot'.
1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  It is wasteful to make predictions in code that is run
1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  seldomly (e.g. at subsystem initialization time) as the
1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  basic block reordering that this affects can often generate
1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	  larger code.
1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__predict_true(exp)	__builtin_expect((exp) != 0, 1)
1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	__predict_false(exp)	__builtin_expect((exp) != 0, 0)
1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18124fad01755d33e6772ea3be56b702570bf257d55Elliott Hughes#define __wur __attribute__((__warn_unused_result__))
18224fad01755d33e6772ea3be56b702570bf257d55Elliott Hughes
1838ba4424a4160d56c520342e946988ab60d31f95aJosh Gao#ifdef __clang__
1847cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __errorattr(msg) __attribute__((unavailable(msg)))
1857cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __warnattr(msg) __attribute__((deprecated(msg)))
1867cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __warnattr_real(msg) __attribute__((deprecated(msg)))
1877cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __enable_if(cond, msg) __attribute__((enable_if(cond, msg)))
1888ba4424a4160d56c520342e946988ab60d31f95aJosh Gao#else
1897cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __errorattr(msg) __attribute__((__error__(msg)))
1907cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __warnattr(msg) __attribute__((__warning__(msg)))
1917cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __warnattr_real __warnattr
1927cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/* enable_if doesn't exist on other compilers; give an error if it's used. */
1937cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV
1947cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/* errordecls really don't work as well in clang as they do in GCC. */
1957cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __errordecl(name, msg) extern void name(void) __errorattr(msg)
1968ba4424a4160d56c520342e946988ab60d31f95aJosh Gao#endif
1978ba4424a4160d56c520342e946988ab60d31f95aJosh Gao
1987cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#if defined(ANDROID_STRICT)
1997cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/*
2007cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * For things that are sketchy, but not necessarily an error. FIXME: Enable
2017cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * this.
2027cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV */
2037cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __warnattr_strict(msg) /* __warnattr(msg) */
2047cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#else
2057cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __warnattr_strict(msg)
2067cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#endif
207c9d09431b982b44ebec5b1ca0913e80f13aba809Yabin Cui
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
2092cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes * Some BSD source needs these macros.
2102cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes * Originally they embedded the rcs versions of each source file
2112cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes * in the generated binary. We strip strings during build anyway,.
2123527fd6f0df794207215790321824b7844cc712dDavid 'Digit' Turner */
2132cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#define __IDSTRING(_prefix,_s) /* nothing */
2142cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#define __COPYRIGHT(_s) /* nothing */
2152cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#define __FBSDID(_s) /* nothing */
2162cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#define __RCSID(_s) /* nothing */
2172cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#define __SCCSID(_s) /* nothing */
2183527fd6f0df794207215790321824b7844cc712dDavid 'Digit' Turner
219f4c948a9e9b616c4f133f9495dacb105e51b3e49Elliott Hughes/*
2203ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes * With bionic, you always get all C and POSIX API.
2213ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes *
2223ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes * If you want BSD and/or GNU extensions, _BSD_SOURCE and/or _GNU_SOURCE are
2233ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes * expected to be defined by callers before *any* standard header file is
2243ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes * included.
225fc8974b7aabd2e7c9884e8d5e3d84395461396ffElliott Hughes *
2263ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes * In our header files we test against __USE_BSD and __USE_GNU.
227f4c948a9e9b616c4f133f9495dacb105e51b3e49Elliott Hughes */
228f4c948a9e9b616c4f133f9495dacb105e51b3e49Elliott Hughes#if defined(_GNU_SOURCE)
2293ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes# define __USE_BSD 1
230f4c948a9e9b616c4f133f9495dacb105e51b3e49Elliott Hughes# define __USE_GNU 1
231fc8974b7aabd2e7c9884e8d5e3d84395461396ffElliott Hughes#endif
232fc8974b7aabd2e7c9884e8d5e3d84395461396ffElliott Hughes
233fc8974b7aabd2e7c9884e8d5e3d84395461396ffElliott Hughes#if defined(_BSD_SOURCE)
234fc8974b7aabd2e7c9884e8d5e3d84395461396ffElliott Hughes# define __USE_BSD 1
235f4c948a9e9b616c4f133f9495dacb105e51b3e49Elliott Hughes#endif
236f4c948a9e9b616c4f133f9495dacb105e51b3e49Elliott Hughes
2373ba55f8022f3d5c5ca94a3df46fa658eda791987Elliott Hughes/* _FILE_OFFSET_BITS 64 support. */
238f5605374da9605686352300de3e1bf342e72032dDan Albert#if !defined(__LP64__) && defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
23968dc20d41193831a94df04b994ff2f601dd38d10Elliott Hughes#define __USE_FILE_OFFSET64 1
240f5605374da9605686352300de3e1bf342e72032dDan Albert#define __RENAME_IF_FILE_OFFSET64(func) __RENAME(func)
241f5605374da9605686352300de3e1bf342e72032dDan Albert#else
242f5605374da9605686352300de3e1bf342e72032dDan Albert#define __RENAME_IF_FILE_OFFSET64(func)
24368dc20d41193831a94df04b994ff2f601dd38d10Elliott Hughes#endif
24468dc20d41193831a94df04b994ff2f601dd38d10Elliott Hughes
2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  __BIONIC__   1
24623d24394e72715ddf580726655c8ba3fbcf11bfeDavid 'Digit' Turner#include <android/api-level.h>
2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
248caa15bfe2d6c065d2caff48583f18351bd96915fElliott Hughes/* glibc compatibility. */
249b36efa4343d79e3fb548d12d039193850246b892Josh Gao#if defined(__LP64__)
250caa15bfe2d6c065d2caff48583f18351bd96915fElliott Hughes#define __WORDSIZE 64
251caa15bfe2d6c065d2caff48583f18351bd96915fElliott Hughes#else
252caa15bfe2d6c065d2caff48583f18351bd96915fElliott Hughes#define __WORDSIZE 32
253caa15bfe2d6c065d2caff48583f18351bd96915fElliott Hughes#endif
254caa15bfe2d6c065d2caff48583f18351bd96915fElliott Hughes
255bd8e6749b78567af62ec126d7cc057386ebee25aNick Kralevich/*
256bd8e6749b78567af62ec126d7cc057386ebee25aNick Kralevich * When _FORTIFY_SOURCE is defined, automatic bounds checking is
257bd8e6749b78567af62ec126d7cc057386ebee25aNick Kralevich * added to commonly used libc functions. If a buffer overrun is
258bd8e6749b78567af62ec126d7cc057386ebee25aNick Kralevich * detected, the program is safely aborted.
259bd8e6749b78567af62ec126d7cc057386ebee25aNick Kralevich *
260ef2b2fe99eb0d348d71a53702bb4981b17d8682eElliott Hughes * https://android-developers.googleblog.com/2017/04/fortify-in-android.html
261bd8e6749b78567af62ec126d7cc057386ebee25aNick Kralevich */
2627cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV
2637cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1)
2647cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV
26516d1af167f8e36a9aa4a07ae77034ad519b00463Nick Kralevich#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
26644ddb31e75ff0cebea8249065606600185d58680Elliott Hughes#  define __BIONIC_FORTIFY 1
26744ddb31e75ff0cebea8249065606600185d58680Elliott Hughes#  if _FORTIFY_SOURCE == 2
2687cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __bos_level 1
26944ddb31e75ff0cebea8249065606600185d58680Elliott Hughes#  else
2707cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __bos_level 0
27144ddb31e75ff0cebea8249065606600185d58680Elliott Hughes#  endif
2727cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __bosn(s, n) __builtin_object_size((s), (n))
2737cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __bos(s) __bosn((s), __bos_level)
2747cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __bos0(s) __bosn((s), 0)
2755470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#  if defined(__clang__)
2767cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __pass_object_size_n(n) __attribute__((pass_object_size(n)))
2777cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/*
2787cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * FORTIFY'ed functions all have either enable_if or pass_object_size, which
2797cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * makes taking their address impossible. Saying (&read)(foo, bar, baz); will
2807cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * therefore call the unFORTIFYed version of read.
2817cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV */
2827cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __call_bypassing_fortify(fn) (&fn)
2837cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/*
2847cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * Because clang-FORTIFY uses overloads, we can't mark functions as `extern
2857cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * inline` without making them available externally.
2867cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV */
2877cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __BIONIC_FORTIFY_INLINE static __inline__ __always_inline
2887cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/* Error functions don't have bodies, so they can just be static. */
2897cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __BIONIC_ERROR_FUNCTION_VISIBILITY static
2905470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#  else
2917cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/*
2927cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * Where they can, GCC and clang-style FORTIFY share implementations.
2937cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * So, make these nops in GCC.
2947cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV */
2957cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __pass_object_size_n(n)
2967cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#    define __call_bypassing_fortify(fn) (fn)
2977cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/* __BIONIC_FORTIFY_NONSTATIC_INLINE is pointless in GCC's FORTIFY */
2985470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#    define __BIONIC_FORTIFY_INLINE extern __inline__ __always_inline __attribute__((gnu_inline)) __attribute__((__artificial__))
2995470c183611737af5ec954984e4bdb5da8c99b73Elliott Hughes#  endif
300bd3d2088d99f45babb8a73192b53b6fd91930af3George Burgess IV#else
301bd3d2088d99f45babb8a73192b53b6fd91930af3George Burgess IV/* Further increase sharing for some inline functions */
302bd3d2088d99f45babb8a73192b53b6fd91930af3George Burgess IV#  define __pass_object_size_n(n)
3037cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#endif
304bd3d2088d99f45babb8a73192b53b6fd91930af3George Burgess IV#define __pass_object_size __pass_object_size_n(__bos_level)
305bd3d2088d99f45babb8a73192b53b6fd91930af3George Burgess IV#define __pass_object_size0 __pass_object_size_n(0)
3067cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV
3076fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes/*
3086fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * Used to support clangisms with FORTIFY. Because these change how symbols are
3096fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * emitted, we need to ensure that bionic itself is built fortified. But lots
3106fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * of external code (especially stuff using configure) likes to declare
3116fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * functions directly, and they can't know that the overloadable attribute
3126fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * exists. This leads to errors like:
3136fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes *
3146fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * dcigettext.c:151:7: error: redeclaration of 'getcwd' must have the 'overloadable' attribute
3156fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * char *getcwd ();
3166fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes *       ^
3176fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes *
3186fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * To avoid this and keep such software building, don't use overloadable if
3196fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes * we're not using fortify.
3207cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV */
3216fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes#if defined(__clang__) && defined(__BIONIC_FORTIFY)
3227cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __overloadable __attribute__((overloadable))
3236fa8df9d3c1d7093778594f4af11faa9b37803e8Elliott Hughes/* We don't use __RENAME directly because on gcc this could result in unnecessary renames. */
3247cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __RENAME_CLANG(x) __RENAME(x)
3257cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#else
3267cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __overloadable
3277cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#  define __RENAME_CLANG(x)
3281aaa17802c92d99ae170245c2b2f15a6c27b133eNick Kralevich#endif
32971a18dd435e96564539b5af71b8ea5093a2109a1Nick Kralevich
3302cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes/* Used to tag non-static symbols that are private and never exposed by the shared library. */
3312cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#define __LIBC_HIDDEN__ __attribute__((visibility("hidden")))
3322cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes
333f47514dd997d33deafaba85e34e2c95fd514d056Elliott Hughes/*
334f47514dd997d33deafaba85e34e2c95fd514d056Elliott Hughes * Used to tag symbols that should be hidden for 64-bit,
335f47514dd997d33deafaba85e34e2c95fd514d056Elliott Hughes * but visible to preserve binary compatibility for LP32.
336f47514dd997d33deafaba85e34e2c95fd514d056Elliott Hughes */
3372cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#ifdef __LP64__
338f47514dd997d33deafaba85e34e2c95fd514d056Elliott Hughes#define __LIBC32_LEGACY_PUBLIC__ __attribute__((visibility("hidden")))
3392cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#else
340f47514dd997d33deafaba85e34e2c95fd514d056Elliott Hughes#define __LIBC32_LEGACY_PUBLIC__ __attribute__((visibility("default")))
3412cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#endif
3422cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes
3432cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes/* Used to rename functions so that the compiler emits a call to 'x' rather than the function this was applied to. */
3442cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes#define __RENAME(x) __asm__(#x)
3452cfb4e8e2e217ef0e4140dcbf9b3da809781158cElliott Hughes
346bfb6bae9fb5ef42e4f9ae2847f30f41938c04900Josh Gao#include <android/versioning.h>
34746b44160e9e4c466be0faf1bb8d6ec70ecc83273Josh Gao
348fed2659869ec41a93f655be8058568ddab419e01Daniel Micay#if __has_builtin(__builtin_umul_overflow) || __GNUC__ >= 5
349b36efa4343d79e3fb548d12d039193850246b892Josh Gao#if defined(__LP64__)
350fed2659869ec41a93f655be8058568ddab419e01Daniel Micay#define __size_mul_overflow(a, b, result) __builtin_umull_overflow(a, b, result)
351fed2659869ec41a93f655be8058568ddab419e01Daniel Micay#else
352fed2659869ec41a93f655be8058568ddab419e01Daniel Micay#define __size_mul_overflow(a, b, result) __builtin_umul_overflow(a, b, result)
353fed2659869ec41a93f655be8058568ddab419e01Daniel Micay#endif
354fed2659869ec41a93f655be8058568ddab419e01Daniel Micay#else
3556b45d57739772bf4418923e960cc396ad5a0706eXia Yangextern __inline__ __always_inline __attribute__((gnu_inline))
3566b45d57739772bf4418923e960cc396ad5a0706eXia Yangint __size_mul_overflow(__SIZE_TYPE__ a, __SIZE_TYPE__ b, __SIZE_TYPE__ *result) {
357fed2659869ec41a93f655be8058568ddab419e01Daniel Micay    *result = a * b;
358fed2659869ec41a93f655be8058568ddab419e01Daniel Micay    static const __SIZE_TYPE__ mul_no_overflow = 1UL << (sizeof(__SIZE_TYPE__) * 4);
359fed2659869ec41a93f655be8058568ddab419e01Daniel Micay    return (a >= mul_no_overflow || b >= mul_no_overflow) && a > 0 && (__SIZE_TYPE__)-1 / a < b;
360fed2659869ec41a93f655be8058568ddab419e01Daniel Micay}
361fed2659869ec41a93f655be8058568ddab419e01Daniel Micay#endif
362fed2659869ec41a93f655be8058568ddab419e01Daniel Micay
3637cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#if defined(__clang__)
3647cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV/*
3657cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * Used when we need to check for overflow when multiplying x and y. This
3667cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * should only be used where __size_mul_overflow can not work, because it makes
3677cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * assumptions that __size_mul_overflow doesn't (x and y are positive, ...),
3687cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * *and* doesn't make use of compiler intrinsics, so it's probably slower than
3697cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV * __size_mul_overflow.
3707cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV */
3717cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#define __unsafe_check_mul_overflow(x, y) ((__SIZE_TYPE__)-1 / (x) < (y))
3727cc779f15c524e1622f7d5b1c7e82e6ffc6677fdGeorge Burgess IV#endif
3733e6929dad5938f57e5734cf3cff638f17a1a2b25Dan Albert
3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* !_SYS_CDEFS_H_ */
375