| 1 | /* |
| 2 | * Copyright (c) 2000-2018 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 | /* Copyright 1995 NeXT Computer, Inc. All rights reserved. */ |
| 29 | /* |
| 30 | * Copyright (c) 1991, 1993 |
| 31 | *	The Regents of the University of California. All rights reserved. |
| 32 | * |
| 33 | * This code is derived from software contributed to Berkeley by |
| 34 | * Berkeley Software Design, Inc. |
| 35 | * |
| 36 | * Redistribution and use in source and binary forms, with or without |
| 37 | * modification, are permitted provided that the following conditions |
| 38 | * are met: |
| 39 | * 1. Redistributions of source code must retain the above copyright |
| 40 | * notice, this list of conditions and the following disclaimer. |
| 41 | * 2. Redistributions in binary form must reproduce the above copyright |
| 42 | * notice, this list of conditions and the following disclaimer in the |
| 43 | * documentation and/or other materials provided with the distribution. |
| 44 | * 3. All advertising materials mentioning features or use of this software |
| 45 | * must display the following acknowledgement: |
| 46 | *	This product includes software developed by the University of |
| 47 | *	California, Berkeley and its contributors. |
| 48 | * 4. Neither the name of the University nor the names of its contributors |
| 49 | * may be used to endorse or promote products derived from this software |
| 50 | * without specific prior written permission. |
| 51 | * |
| 52 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 53 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 55 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 56 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 57 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 58 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 59 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 60 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 61 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 62 | * SUCH DAMAGE. |
| 63 | * |
| 64 | *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95 |
| 65 | */ |
| 66 | |
| 67 | #ifndef _CDEFS_H_ |
| 68 | #define _CDEFS_H_ |
| 69 | |
| 70 | #if defined(__cplusplus) |
| 71 | #define __BEGIN_DECLS extern "C" { |
| 72 | #define __END_DECLS } |
| 73 | #else |
| 74 | #define __BEGIN_DECLS |
| 75 | #define __END_DECLS |
| 76 | #endif |
| 77 | |
| 78 | /* This SDK is designed to work with clang and specific versions of |
| 79 | * gcc >= 4.0 with Apple's patch sets */ |
| 80 | #if !defined(__GNUC__) || __GNUC__ < 4 |
| 81 | #warning "Unsupported compiler detected" |
| 82 | #endif |
| 83 | |
| 84 | /* |
| 85 | * Compatibility with compilers and environments that don't support compiler |
| 86 | * feature checking function-like macros. |
| 87 | */ |
| 88 | #ifndef __has_builtin |
| 89 | #define __has_builtin(x) 0 |
| 90 | #endif |
| 91 | #ifndef __has_include |
| 92 | #define __has_include(x) 0 |
| 93 | #endif |
| 94 | #ifndef __has_feature |
| 95 | #define __has_feature(x) 0 |
| 96 | #endif |
| 97 | #ifndef __has_attribute |
| 98 | #define __has_attribute(x) 0 |
| 99 | #endif |
| 100 | #ifndef __has_cpp_attribute |
| 101 | #define __has_cpp_attribute(x) 0 |
| 102 | #endif |
| 103 | #ifndef __has_extension |
| 104 | #define __has_extension(x) 0 |
| 105 | #endif |
| 106 | |
| 107 | /* |
| 108 | * The __CONCAT macro is used to concatenate parts of symbol names, e.g. |
| 109 | * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo. |
| 110 | * The __CONCAT macro is a bit tricky -- make sure you don't put spaces |
| 111 | * in between its arguments. __CONCAT can also concatenate double-quoted |
| 112 | * strings produced by the __STRING macro, but this only works with ANSI C. |
| 113 | */ |
| 114 | #if defined(__STDC__) || defined(__cplusplus) |
| 115 | #define __P(protos) protos /* full-blown ANSI C */ |
| 116 | #define __CONCAT(x, y) x ## y |
| 117 | #define __STRING(x) #x |
| 118 | |
| 119 | #define __const const /* define reserved names to standard */ |
| 120 | #define __signed signed |
| 121 | #define __volatile volatile |
| 122 | #if defined(__cplusplus) |
| 123 | #define __inline inline /* convert to C++ keyword */ |
| 124 | #else |
| 125 | #ifndef __GNUC__ |
| 126 | #define __inline /* delete GCC keyword */ |
| 127 | #endif /* !__GNUC__ */ |
| 128 | #endif /* !__cplusplus */ |
| 129 | |
| 130 | #else /* !(__STDC__ || __cplusplus) */ |
| 131 | #define __P(protos) () /* traditional C preprocessor */ |
| 132 | #define __CONCAT(x, y) x /**/ y |
| 133 | #define __STRING(x) "x" |
| 134 | |
| 135 | #ifndef __GNUC__ |
| 136 | #define __const /* delete pseudo-ANSI C keywords */ |
| 137 | #define __inline |
| 138 | #define __signed |
| 139 | #define __volatile |
| 140 | #endif /* !__GNUC__ */ |
| 141 | |
| 142 | /* |
| 143 | * In non-ANSI C environments, new programs will want ANSI-only C keywords |
| 144 | * deleted from the program and old programs will want them left alone. |
| 145 | * When using a compiler other than gcc, programs using the ANSI C keywords |
| 146 | * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS. |
| 147 | * When using "gcc -traditional", we assume that this is the intent; if |
| 148 | * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone. |
| 149 | */ |
| 150 | #ifndef NO_ANSI_KEYWORDS |
| 151 | #define const __const /* convert ANSI C keywords */ |
| 152 | #define inline __inline |
| 153 | #define signed __signed |
| 154 | #define volatile __volatile |
| 155 | #endif /* !NO_ANSI_KEYWORDS */ |
| 156 | #endif /* !(__STDC__ || __cplusplus) */ |
| 157 | |
| 158 | /* |
| 159 | * __pure2 can be used for functions that are only a function of their scalar |
| 160 | * arguments (meaning they can't dereference pointers). |
| 161 | * |
| 162 | * __stateful_pure can be used for functions that have no side effects, |
| 163 | * but depend on the state of the memory. |
| 164 | */ |
| 165 | #define __dead2 __attribute__((__noreturn__)) |
| 166 | #define __pure2 __attribute__((__const__)) |
| 167 | #define __stateful_pure __attribute__((__pure__)) |
| 168 | |
| 169 | /* __unused denotes variables and functions that may not be used, preventing |
| 170 | * the compiler from warning about it if not used. |
| 171 | */ |
| 172 | #define __unused __attribute__((__unused__)) |
| 173 | |
| 174 | /* __used forces variables and functions to be included even if it appears |
| 175 | * to the compiler that they are not used (and would thust be discarded). |
| 176 | */ |
| 177 | #define __used __attribute__((__used__)) |
| 178 | |
| 179 | /* __cold marks code used for debugging or that is rarely taken |
| 180 | * and tells the compiler to optimize for size and outline code. |
| 181 | */ |
| 182 | #if __has_attribute(cold) |
| 183 | #define __cold __attribute__((__cold__)) |
| 184 | #else |
| 185 | #define __cold |
| 186 | #endif |
| 187 | |
| 188 | /* __returns_nonnull marks functions that return a non-null pointer. */ |
| 189 | #if __has_attribute(returns_nonnull) |
| 190 | #define __returns_nonnull __attribute((returns_nonnull)) |
| 191 | #else |
| 192 | #define __returns_nonnull |
| 193 | #endif |
| 194 | |
| 195 | /* __exported denotes symbols that should be exported even when symbols |
| 196 | * are hidden by default. |
| 197 | * __exported_push/_exported_pop are pragmas used to delimit a range of |
| 198 | * symbols that should be exported even when symbols are hidden by default. |
| 199 | */ |
| 200 | #define __exported __attribute__((__visibility__("default"))) |
| 201 | #define __exported_push _Pragma("GCC visibility push(default)") |
| 202 | #ifndef __BUILDING_XNU_LIBRARY__ |
| 203 | #define __exported_push_hidden _Pragma("GCC visibility push(hidden)") |
| 204 | #define __exported_pop _Pragma("GCC visibility pop") |
| 205 | #define __exported_hidden __private_extern__ |
| 206 | #else /* __BUILDING_XNU_LIBRARY__ */ |
| 207 | /* Don't hide symbols that the might be need to be used from outside */ |
| 208 | #define __exported_push_hidden |
| 209 | #define __exported_pop |
| 210 | #define __exported_hidden |
| 211 | #endif /* __BUILDING_XNU_LIBRARY__ */ |
| 212 | |
| 213 | /* __deprecated causes the compiler to produce a warning when encountering |
| 214 | * code using the deprecated functionality. |
| 215 | * __deprecated_msg() does the same, and compilers that support it will print |
| 216 | * a message along with the deprecation warning. |
| 217 | * This may require turning on such warning with the -Wdeprecated flag. |
| 218 | * __deprecated_enum_msg() should be used on enums, and compilers that support |
| 219 | * it will print the deprecation warning. |
| 220 | * __kpi_deprecated() specifically indicates deprecation of kernel programming |
| 221 | * interfaces in Kernel.framework used by KEXTs. |
| 222 | */ |
| 223 | #define __deprecated __attribute__((__deprecated__)) |
| 224 | |
| 225 | #if __has_extension(attribute_deprecated_with_message) || \ |
| 226 | (defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)))) |
| 227 | 	#define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg))) |
| 228 | #else |
| 229 | 	#define __deprecated_msg(_msg) __attribute__((__deprecated__)) |
| 230 | #endif |
| 231 | |
| 232 | #if __has_extension(enumerator_attributes) |
| 233 | 	#define __deprecated_enum_msg(_msg) __deprecated_msg(_msg) |
| 234 | #else |
| 235 | 	#define __deprecated_enum_msg(_msg) |
| 236 | #endif |
| 237 | |
| 238 | #define __kpi_deprecated(_msg) |
| 239 | |
| 240 | /* __unavailable causes the compiler to error out when encountering |
| 241 | * code using the tagged function |
| 242 | */ |
| 243 | #if __has_attribute(unavailable) |
| 244 | #define __unavailable __attribute__((__unavailable__)) |
| 245 | #else |
| 246 | #define __unavailable |
| 247 | #endif |
| 248 | |
| 249 | #define __kpi_unavailable |
| 250 | |
| 251 | #define __kpi_deprecated_arm64_macos_unavailable |
| 252 | |
| 253 | /* Delete pseudo-keywords wherever they are not available or needed. */ |
| 254 | #ifndef __dead |
| 255 | #define __dead |
| 256 | #define __pure |
| 257 | #endif |
| 258 | |
| 259 | /* |
| 260 | * We use `__restrict' as a way to define the `restrict' type qualifier |
| 261 | * without disturbing older software that is unaware of C99 keywords. |
| 262 | */ |
| 263 | #if __STDC_VERSION__ < 199901 |
| 264 | #define __restrict |
| 265 | #else |
| 266 | #define __restrict restrict |
| 267 | #endif |
| 268 | |
| 269 | /* Compatibility with compilers and environments that don't support the |
| 270 | * nullability feature. |
| 271 | */ |
| 272 | |
| 273 | #if !__has_feature(nullability) |
| 274 | #ifndef __nullable |
| 275 | #define __nullable |
| 276 | #endif |
| 277 | #ifndef __nonnull |
| 278 | #define __nonnull |
| 279 | #endif |
| 280 | #ifndef __null_unspecified |
| 281 | #define __null_unspecified |
| 282 | #endif |
| 283 | #ifndef _Nullable |
| 284 | #define _Nullable |
| 285 | #endif |
| 286 | #ifndef _Nonnull |
| 287 | #define _Nonnull |
| 288 | #endif |
| 289 | #ifndef _Null_unspecified |
| 290 | #define _Null_unspecified |
| 291 | #endif |
| 292 | #endif |
| 293 | |
| 294 | /* |
| 295 | * __disable_tail_calls causes the compiler to not perform tail call |
| 296 | * optimization inside the marked function. |
| 297 | */ |
| 298 | #if __has_attribute(disable_tail_calls) |
| 299 | #define __disable_tail_calls __attribute__((__disable_tail_calls__)) |
| 300 | #else |
| 301 | #define __disable_tail_calls |
| 302 | #endif |
| 303 | |
| 304 | /* |
| 305 | * __not_tail_called causes the compiler to prevent tail call optimization |
| 306 | * on statically bound calls to the function. It has no effect on indirect |
| 307 | * calls. Virtual functions, objective-c methods, and functions marked as |
| 308 | * "always_inline" cannot be marked as __not_tail_called. |
| 309 | */ |
| 310 | #if __has_attribute(not_tail_called) |
| 311 | #define __not_tail_called __attribute__((__not_tail_called__)) |
| 312 | #else |
| 313 | #define __not_tail_called |
| 314 | #endif |
| 315 | |
| 316 | /* |
| 317 | * __result_use_check warns callers of a function that not using the function |
| 318 | * return value is a bug, i.e. dismissing malloc() return value results in a |
| 319 | * memory leak. |
| 320 | */ |
| 321 | #if __has_attribute(warn_unused_result) |
| 322 | #define __result_use_check __attribute__((__warn_unused_result__)) |
| 323 | #else |
| 324 | #define __result_use_check |
| 325 | #endif |
| 326 | |
| 327 | /* |
| 328 | * __swift_unavailable causes the compiler to mark a symbol as specifically |
| 329 | * unavailable in Swift, regardless of any other availability in C. |
| 330 | */ |
| 331 | #if __has_feature(attribute_availability_swift) |
| 332 | #define __swift_unavailable(_msg) __attribute__((__availability__(swift, unavailable, message=_msg))) |
| 333 | #else |
| 334 | #define __swift_unavailable(_msg) |
| 335 | #endif |
| 336 | |
| 337 | /* |
| 338 | * Attributes to support Swift concurrency. |
| 339 | */ |
| 340 | #if __has_attribute(__swift_attr__) |
| 341 | #define __swift_unavailable_from_async(_msg) __attribute__((__swift_attr__("@_unavailableFromAsync(message: \"" _msg "\")"))) |
| 342 | #define __swift_nonisolated __attribute__((__swift_attr__("nonisolated"))) |
| 343 | #define __swift_nonisolated_unsafe __attribute__((__swift_attr__("nonisolated(unsafe)"))) |
| 344 | #else |
| 345 | #define __swift_unavailable_from_async(_msg) |
| 346 | #define __swift_nonisolated |
| 347 | #define __swift_nonisolated_unsafe |
| 348 | #endif |
| 349 | |
| 350 | /* |
| 351 | * __abortlike is the attribute to put on functions like abort() that are |
| 352 | * typically used to mark assertions. These optimize the codegen |
| 353 | * for outlining while still maintaining debugability. |
| 354 | */ |
| 355 | #ifndef __abortlike |
| 356 | #define __abortlike __dead2 __cold __not_tail_called |
| 357 | #endif |
| 358 | |
| 359 | /* Declaring inline functions within headers is error-prone due to differences |
| 360 | * across various versions of the C language and extensions. __header_inline |
| 361 | * can be used to declare inline functions within system headers. In cases |
| 362 | * where you want to force inlining instead of letting the compiler make |
| 363 | * the decision, you can use __header_always_inline. |
| 364 | * |
| 365 | * Be aware that using inline for functions which compilers may also provide |
| 366 | * builtins can behave differently under various compilers. If you intend to |
| 367 | * provide an inline version of such a function, you may want to use a macro |
| 368 | * instead. |
| 369 | * |
| 370 | * The check for !__GNUC__ || __clang__ is because gcc doesn't correctly |
| 371 | * support c99 inline in some cases: |
| 372 | * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55965 |
| 373 | */ |
| 374 | |
| 375 | #if defined(__cplusplus) || \ |
| 376 | (__STDC_VERSION__ >= 199901L && \ |
| 377 | !defined(__GNUC_GNU_INLINE__) && \ |
| 378 | (!defined(__GNUC__) || defined(__clang__))) |
| 379 | # define __header_inline inline |
| 380 | #elif defined(__GNUC__) && defined(__GNUC_STDC_INLINE__) |
| 381 | # define __header_inline extern __inline __attribute__((__gnu_inline__)) |
| 382 | #elif defined(__GNUC__) |
| 383 | # define __header_inline extern __inline |
| 384 | #else |
| 385 | /* If we land here, we've encountered an unsupported compiler, |
| 386 | * so hopefully it understands static __inline as a fallback. |
| 387 | */ |
| 388 | # define __header_inline static __inline |
| 389 | #endif |
| 390 | |
| 391 | #ifdef __GNUC__ |
| 392 | # define __header_always_inline __header_inline __attribute__ ((__always_inline__)) |
| 393 | #else |
| 394 | /* Unfortunately, we're using a compiler that we don't know how to force to |
| 395 | * inline. Oh well. |
| 396 | */ |
| 397 | # define __header_always_inline __header_inline |
| 398 | #endif |
| 399 | |
| 400 | /* |
| 401 | * Compiler-dependent macros that bracket portions of code where the |
| 402 | * "-Wunreachable-code" warning should be ignored. Please use sparingly. |
| 403 | */ |
| 404 | #if defined(__clang__) |
| 405 | # define __unreachable_ok_push \ |
| 406 | 	 _Pragma("clang diagnostic push") \ |
| 407 | 	 _Pragma("clang diagnostic ignored \"-Wunreachable-code\"") |
| 408 | # define __unreachable_ok_pop \ |
| 409 | 	 _Pragma("clang diagnostic pop") |
| 410 | #elif defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) |
| 411 | # define __unreachable_ok_push \ |
| 412 | 	 _Pragma("GCC diagnostic push") \ |
| 413 | 	 _Pragma("GCC diagnostic ignored \"-Wunreachable-code\"") |
| 414 | # define __unreachable_ok_pop \ |
| 415 | 	 _Pragma("GCC diagnostic pop") |
| 416 | #else |
| 417 | # define __unreachable_ok_push |
| 418 | # define __unreachable_ok_pop |
| 419 | #endif |
| 420 | |
| 421 | /* |
| 422 | * Compiler-dependent macros to declare that functions take printf-like |
| 423 | * or scanf-like arguments. They are null except for versions of gcc |
| 424 | * that are known to support the features properly. Functions declared |
| 425 | * with these attributes will cause compilation warnings if there is a |
| 426 | * mismatch between the format string and subsequent function parameter |
| 427 | * types. |
| 428 | */ |
| 429 | #define __printflike(fmtarg, firstvararg) \ |
| 430 | 	__attribute__((__format__ (__printf__, fmtarg, firstvararg))) |
| 431 | #define __printf0like(fmtarg, firstvararg) \ |
| 432 | 	__attribute__((__format__ (__printf0__, fmtarg, firstvararg))) |
| 433 | #define __scanflike(fmtarg, firstvararg) \ |
| 434 | 	__attribute__((__format__ (__scanf__, fmtarg, firstvararg))) |
| 435 | #define __osloglike(fmtarg, firstvararg) \ |
| 436 | 	__attribute__((__format__ (__os_log__, fmtarg, firstvararg))) |
| 437 | |
| 438 | #define __IDSTRING(name, string) static const char name[] __used = string |
| 439 | |
| 440 | #ifndef __COPYRIGHT |
| 441 | #define __COPYRIGHT(s) __IDSTRING(copyright,s) |
| 442 | #endif |
| 443 | |
| 444 | #ifndef __RCSID |
| 445 | #define __RCSID(s) __IDSTRING(rcsid,s) |
| 446 | #endif |
| 447 | |
| 448 | #ifndef __SCCSID |
| 449 | #define __SCCSID(s) __IDSTRING(sccsid,s) |
| 450 | #endif |
| 451 | |
| 452 | #ifndef __PROJECT_VERSION |
| 453 | #define __PROJECT_VERSION(s) __IDSTRING(project_version,s) |
| 454 | #endif |
| 455 | |
| 456 | /* Source compatibility only, ID string not emitted in object file */ |
| 457 | #ifndef __FBSDID |
| 458 | #define __FBSDID(s) |
| 459 | #endif |
| 460 | |
| 461 | #ifndef __DECONST |
| 462 | #define __DECONST(type, var) __CAST_AWAY_QUALIFIER(var, const, type) |
| 463 | #endif |
| 464 | |
| 465 | #ifndef __DEVOLATILE |
| 466 | #define __DEVOLATILE(type, var) __CAST_AWAY_QUALIFIER(var, volatile, type) |
| 467 | #endif |
| 468 | |
| 469 | #ifndef __DEQUALIFY |
| 470 | #define __DEQUALIFY(type, var) __CAST_AWAY_QUALIFIER(var, const volatile, type) |
| 471 | #endif |
| 472 | |
| 473 | /* |
| 474 | * __alloc_align can be used to label function arguments that represent the |
| 475 | * alignment of the returned pointer. |
| 476 | */ |
| 477 | #ifndef __alloc_align |
| 478 | #if __has_attribute(alloc_align) |
| 479 | #define __alloc_align(n) __attribute__((alloc_align(n))) |
| 480 | #else |
| 481 | #define __alloc_align(n) |
| 482 | #endif |
| 483 | #endif // __alloc_align |
| 484 | |
| 485 | /* |
| 486 | * __alloc_size can be used to label function arguments that represent the |
| 487 | * size of memory that the function allocates and returns. The one-argument |
| 488 | * form labels a single argument that gives the allocation size (where the |
| 489 | * arguments are numbered from 1): |
| 490 | * |
| 491 | * void	*malloc(size_t __size) __alloc_size(1); |
| 492 | * |
| 493 | * The two-argument form handles the case where the size is calculated as the |
| 494 | * product of two arguments: |
| 495 | * |
| 496 | * void	*calloc(size_t __count, size_t __size) __alloc_size(1,2); |
| 497 | */ |
| 498 | #ifndef __alloc_size |
| 499 | #if __has_attribute(alloc_size) |
| 500 | #define __alloc_size(...) __attribute__((alloc_size(__VA_ARGS__))) |
| 501 | #else |
| 502 | #define __alloc_size(...) |
| 503 | #endif |
| 504 | #endif // __alloc_size |
| 505 | |
| 506 | /* |
| 507 | * Facilities below assist adoption of -Wunsafe-buffer-usage, an off-by-default |
| 508 | * Clang compiler warning that helps the developer minimize unsafe, raw |
| 509 | * buffer manipulation in the code that may lead to buffer overflow |
| 510 | * vulnerabilities. |
| 511 | * |
| 512 | * They are primarily designed for modern C++ code where -Wunsafe-buffer-usage |
| 513 | * comes with automatic fix-it hints that help the developer transform |
| 514 | * their code to use modern C++ containers, which may be made bounds-safe by |
| 515 | * linking against a version of the C++ standard library that offers |
| 516 | * bounds-checked containers. |
| 517 | * They can be used in plain C, but -fbounds-safety is the preferred solution |
| 518 | * for plain C (see also <ptrcheck.h>). |
| 519 | * |
| 520 | * Attribute __unsafe_buffer_usage can be used to label functions that should be |
| 521 | * avoided as they may perform or otherwise introduce unsafe buffer manipulation |
| 522 | * operations. The attribute can also be attached to class/struct fields that |
| 523 | * are used in unsafe buffer manipulations. |
| 524 | * |
| 525 | * Calls to attribute annotated functions are flagged by -Wunsafe-buffer-usage, similar to |
| 526 | * how unchecked buffer manipulation operations are flagged when observed |
| 527 | * by the compiler directly. Similarly, use of and assignment to the struct/class fields |
| 528 | * that have the attribute also get flagged by the compiler. |
| 529 | * |
| 530 | * // An unsafe function that needs to be avoided. |
| 531 | * __unsafe_buffer_usage |
| 532 | * void foo(int *buf, size_t size); |
| 533 | * |
| 534 | * // A safe alternative to foo(). |
| 535 | * void foo(std::span<int> buf); |
| 536 | * |
| 537 | * void bar(size_t idx) { |
| 538 | * int array[5]; |
| 539 | * |
| 540 | * // Direct unsafe buffer manipulation through subscript operator: |
| 541 | * array[idx] = 3; // warning: function introduces unsafe buffer manipulation [-Wunsafe-buffer-usage] |
| 542 | * // Unsafe buffer manipulation through function foo(): |
| 543 | * foo(array, 5); // warning: function introduces unsafe buffer manipulation [-Wunsafe-buffer-usage] |
| 544 | * // Checked buffer manipulation, with bounds information automatically |
| 545 | * // preserved for the purposes of runtime checks in standard library: |
| 546 | * foo(array); // no warning |
| 547 | * } |
| 548 | * |
| 549 | * struct Reader { |
| 550 | * // Field involved in unsafe buffer manipulation |
| 551 | * __unsafe_buffer_usage |
| 552 | * void *ptr; |
| 553 | * |
| 554 | * __unsafe_buffer_usage |
| 555 | * size_t sz, count; |
| 556 | * }; |
| 557 | * |
| 558 | * void add_element(Reader rdr, int value) { |
| 559 | * if(rdr.count < rdr.sz) { // warning: unsafe buffer access [-Wunsafe-buffer-usage] |
| 560 | * rdr.ptr[rdr.count] = value; // warning: unsafe buffer access [-Wunsafe-buffer-usage] |
| 561 | * rdr.count++; // warning: unsafe buffer access [-Wunsafe-buffer-usage] |
| 562 | * } |
| 563 | * } |
| 564 | * |
| 565 | * While annotating a function as __unsafe_buffer_usage has an effect similar |
| 566 | * to annotating it as __deprecated, the __unsafe_buffer_usage attribute |
| 567 | * should be used whenever the resulting warning needs to be controlled |
| 568 | * by the -Wunsafe-buffer-usage flag (which is turned off in codebases that |
| 569 | * don't attempt to achieve bounds safety this way) as opposed to -Wdeprecated |
| 570 | * (enabled in most codebases). |
| 571 | * |
| 572 | * The attribute suppresses all -Wunsafe-buffer-usage warnings inside the |
| 573 | * function's body as it is explictly marked as unsafe by the user and |
| 574 | * introduces new warnings at each call site to help the developers avoid the |
| 575 | * function entirely. Most of the time it does not make sense to annotate a |
| 576 | * function as __unsafe_buffer_usage without providing the users with a safe |
| 577 | * alternative. |
| 578 | * |
| 579 | * Pragmas __unsafe_buffer_usage_begin and __unsafe_buffer_usage_end |
| 580 | * annotate a range of code as intentionally containing unsafe buffer |
| 581 | * operations. They suppress -Wunsafe-buffer-usage warnings |
| 582 | * for unsafe operations in range: |
| 583 | * |
| 584 | * __unsafe_buffer_usage_begin |
| 585 | * array[idx] = 3; // warning suppressed |
| 586 | * foo(array, 5); // warning suppressed |
| 587 | * __unsafe_buffer_usage_end |
| 588 | * |
| 589 | * These pragmas are NOT a way to mass-annotate functions with the attribute |
| 590 | * __unsafe_buffer_usage. Functions declared within the pragma range |
| 591 | * do NOT get annotated automatically. |
| 592 | */ |
| 593 | #if __has_cpp_attribute(clang::unsafe_buffer_usage) |
| 594 | #define __has_safe_buffers 1 |
| 595 | #define __unsafe_buffer_usage [[clang::unsafe_buffer_usage]] |
| 596 | #elif __has_attribute(unsafe_buffer_usage) |
| 597 | #define __has_safe_buffers 1 |
| 598 | #define __unsafe_buffer_usage __attribute__((__unsafe_buffer_usage__)) |
| 599 | #else |
| 600 | #define __has_safe_buffers 0 |
| 601 | #define __unsafe_buffer_usage |
| 602 | #endif |
| 603 | #if __has_safe_buffers |
| 604 | #define __unsafe_buffer_usage_begin _Pragma("clang unsafe_buffer_usage begin") |
| 605 | #define __unsafe_buffer_usage_end _Pragma("clang unsafe_buffer_usage end") |
| 606 | #else |
| 607 | #define __unsafe_buffer_usage_begin |
| 608 | #define __unsafe_buffer_usage_end |
| 609 | #endif |
| 610 | |
| 611 | /* |
| 612 | * COMPILATION ENVIRONMENTS -- see compat(5) for additional detail |
| 613 | * |
| 614 | * DEFAULT	By default newly complied code will get POSIX APIs plus |
| 615 | *		Apple API extensions in scope. |
| 616 | * |
| 617 | *		Most users will use this compilation environment to avoid |
| 618 | *		behavioral differences between 32 and 64 bit code. |
| 619 | * |
| 620 | * LEGACY	Defining _NONSTD_SOURCE will get pre-POSIX APIs plus Apple |
| 621 | *		API extensions in scope. |
| 622 | * |
| 623 | *		This is generally equivalent to the Tiger release compilation |
| 624 | *		environment, except that it cannot be applied to 64 bit code; |
| 625 | *		its use is discouraged. |
| 626 | * |
| 627 | *		We expect this environment to be deprecated in the future. |
| 628 | * |
| 629 | * STRICT	Defining _POSIX_C_SOURCE or _XOPEN_SOURCE restricts the |
| 630 | *		available APIs to exactly the set of APIs defined by the |
| 631 | *		corresponding standard, based on the value defined. |
| 632 | * |
| 633 | *		A correct, portable definition for _POSIX_C_SOURCE is 200112L. |
| 634 | *		A correct, portable definition for _XOPEN_SOURCE is 600L. |
| 635 | * |
| 636 | *		Apple API extensions are not visible in this environment, |
| 637 | *		which can cause Apple specific code to fail to compile, |
| 638 | *		or behave incorrectly if prototypes are not in scope or |
| 639 | *		warnings about missing prototypes are not enabled or ignored. |
| 640 | * |
| 641 | * In any compilation environment, for correct symbol resolution to occur, |
| 642 | * function prototypes must be in scope. It is recommended that all Apple |
| 643 | * tools users add either the "-Wall" or "-Wimplicit-function-declaration" |
| 644 | * compiler flags to their projects to be warned when a function is being |
| 645 | * used without a prototype in scope. |
| 646 | */ |
| 647 | |
| 648 | /* These settings are particular to each product. */ |
| 649 | /* Platform: MacOSX */ |
| 650 | #if defined(__i386__) |
| 651 | #define __DARWIN_ONLY_64_BIT_INO_T 0 |
| 652 | #define __DARWIN_ONLY_UNIX_CONFORMANCE 0 |
| 653 | #define __DARWIN_ONLY_VERS_1050 0 |
| 654 | #elif defined(__x86_64__) |
| 655 | #define __DARWIN_ONLY_64_BIT_INO_T 0 |
| 656 | #define __DARWIN_ONLY_UNIX_CONFORMANCE 1 |
| 657 | #define __DARWIN_ONLY_VERS_1050 0 |
| 658 | #else |
| 659 | #define __DARWIN_ONLY_64_BIT_INO_T 1 |
| 660 | #define __DARWIN_ONLY_UNIX_CONFORMANCE 1 |
| 661 | #define __DARWIN_ONLY_VERS_1050 1 |
| 662 | #endif |
| 663 | |
| 664 | /* |
| 665 | * The __DARWIN_ALIAS macros are used to do symbol renaming; they allow |
| 666 | * legacy code to use the old symbol, thus maintaining binary compatibility |
| 667 | * while new code can use a standards compliant version of the same function. |
| 668 | * |
| 669 | * __DARWIN_ALIAS is used by itself if the function signature has not |
| 670 | * changed, it is used along with a #ifdef check for __DARWIN_UNIX03 |
| 671 | * if the signature has changed. Because the __LP64__ environment |
| 672 | * only supports UNIX03 semantics it causes __DARWIN_UNIX03 to be |
| 673 | * defined, but causes __DARWIN_ALIAS to do no symbol mangling. |
| 674 | * |
| 675 | * As a special case, when XCode is used to target a specific version of the |
| 676 | * OS, the manifest constant __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ |
| 677 | * will be defined by the compiler, with the digits representing major version |
| 678 | * time 100 + minor version times 10 (e.g. 10.5 := 1050). If we are targeting |
| 679 | * pre-10.5, and it is the default compilation environment, revert the |
| 680 | * compilation environment to pre-__DARWIN_UNIX03. |
| 681 | */ |
| 682 | #if !defined(__DARWIN_UNIX03) |
| 683 | # if __DARWIN_ONLY_UNIX_CONFORMANCE |
| 684 | # if defined(_NONSTD_SOURCE) |
| 685 | # error "Can't define _NONSTD_SOURCE when only UNIX conformance is available." |
| 686 | # endif /* _NONSTD_SOURCE */ |
| 687 | # define __DARWIN_UNIX03 1 |
| 688 | # elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1040) |
| 689 | # define __DARWIN_UNIX03 0 |
| 690 | # elif defined(_DARWIN_C_SOURCE) || defined(_XOPEN_SOURCE) || defined(_POSIX_C_SOURCE) |
| 691 | # if defined(_NONSTD_SOURCE) |
| 692 | # error "Can't define both _NONSTD_SOURCE and any of _DARWIN_C_SOURCE, _XOPEN_SOURCE or _POSIX_C_SOURCE." |
| 693 | # endif /* _NONSTD_SOURCE */ |
| 694 | # define __DARWIN_UNIX03 1 |
| 695 | # elif defined(_NONSTD_SOURCE) |
| 696 | # define __DARWIN_UNIX03 0 |
| 697 | # else /* default */ |
| 698 | # if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1050) |
| 699 | # define __DARWIN_UNIX03 0 |
| 700 | # else /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050 */ |
| 701 | # define __DARWIN_UNIX03 1 |
| 702 | # endif /* __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050 */ |
| 703 | # endif /* _DARWIN_C_SOURCE || _XOPEN_SOURCE || _POSIX_C_SOURCE || __LP64__ */ |
| 704 | #endif /* !__DARWIN_UNIX03 */ |
| 705 | |
| 706 | #if !defined(__DARWIN_64_BIT_INO_T) |
| 707 | # if defined(_DARWIN_USE_64_BIT_INODE) |
| 708 | # if defined(_DARWIN_NO_64_BIT_INODE) |
| 709 | # error "Can't define both _DARWIN_USE_64_BIT_INODE and _DARWIN_NO_64_BIT_INODE." |
| 710 | # endif /* _DARWIN_NO_64_BIT_INODE */ |
| 711 | # define __DARWIN_64_BIT_INO_T 1 |
| 712 | # elif defined(_DARWIN_NO_64_BIT_INODE) |
| 713 | # if __DARWIN_ONLY_64_BIT_INO_T |
| 714 | # error "Can't define _DARWIN_NO_64_BIT_INODE when only 64-bit inodes are available." |
| 715 | # endif /* __DARWIN_ONLY_64_BIT_INO_T */ |
| 716 | # define __DARWIN_64_BIT_INO_T 0 |
| 717 | # else /* default */ |
| 718 | # if __DARWIN_ONLY_64_BIT_INO_T |
| 719 | # define __DARWIN_64_BIT_INO_T 1 |
| 720 | # elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1060) || __DARWIN_UNIX03 == 0 |
| 721 | # define __DARWIN_64_BIT_INO_T 0 |
| 722 | # else /* default */ |
| 723 | # define __DARWIN_64_BIT_INO_T 1 |
| 724 | # endif /* __DARWIN_ONLY_64_BIT_INO_T */ |
| 725 | # endif |
| 726 | #endif /* !__DARWIN_64_BIT_INO_T */ |
| 727 | |
| 728 | #if !defined(__DARWIN_VERS_1050) |
| 729 | # if __DARWIN_ONLY_VERS_1050 |
| 730 | # define __DARWIN_VERS_1050 1 |
| 731 | # elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0) < 1050) || __DARWIN_UNIX03 == 0 |
| 732 | # define __DARWIN_VERS_1050 0 |
| 733 | # else /* default */ |
| 734 | # define __DARWIN_VERS_1050 1 |
| 735 | # endif |
| 736 | #endif /* !__DARWIN_VERS_1050 */ |
| 737 | |
| 738 | #if !defined(__DARWIN_NON_CANCELABLE) |
| 739 | # define __DARWIN_NON_CANCELABLE 0 |
| 740 | #endif /* !__DARWIN_NON_CANCELABLE */ |
| 741 | |
| 742 | /* |
| 743 | * symbol suffixes used for symbol versioning |
| 744 | */ |
| 745 | #if __DARWIN_UNIX03 |
| 746 | # if __DARWIN_ONLY_UNIX_CONFORMANCE |
| 747 | # define __DARWIN_SUF_UNIX03 /* nothing */ |
| 748 | # else /* !__DARWIN_ONLY_UNIX_CONFORMANCE */ |
| 749 | # define __DARWIN_SUF_UNIX03 "$UNIX2003" |
| 750 | # endif /* __DARWIN_ONLY_UNIX_CONFORMANCE */ |
| 751 | |
| 752 | # if __DARWIN_64_BIT_INO_T |
| 753 | # if __DARWIN_ONLY_64_BIT_INO_T |
| 754 | # define __DARWIN_SUF_64_BIT_INO_T /* nothing */ |
| 755 | # else /* !__DARWIN_ONLY_64_BIT_INO_T */ |
| 756 | # define __DARWIN_SUF_64_BIT_INO_T "$INODE64" |
| 757 | # endif /* __DARWIN_ONLY_64_BIT_INO_T */ |
| 758 | # else /* !__DARWIN_64_BIT_INO_T */ |
| 759 | # define __DARWIN_SUF_64_BIT_INO_T /* nothing */ |
| 760 | # endif /* __DARWIN_64_BIT_INO_T */ |
| 761 | |
| 762 | # if __DARWIN_VERS_1050 |
| 763 | # if __DARWIN_ONLY_VERS_1050 |
| 764 | # define __DARWIN_SUF_1050 /* nothing */ |
| 765 | # else /* !__DARWIN_ONLY_VERS_1050 */ |
| 766 | # define __DARWIN_SUF_1050 "$1050" |
| 767 | # endif /* __DARWIN_ONLY_VERS_1050 */ |
| 768 | # else /* !__DARWIN_VERS_1050 */ |
| 769 | # define __DARWIN_SUF_1050 /* nothing */ |
| 770 | # endif /* __DARWIN_VERS_1050 */ |
| 771 | |
| 772 | # if __DARWIN_NON_CANCELABLE |
| 773 | # define __DARWIN_SUF_NON_CANCELABLE "$NOCANCEL" |
| 774 | # else /* !__DARWIN_NON_CANCELABLE */ |
| 775 | # define __DARWIN_SUF_NON_CANCELABLE /* nothing */ |
| 776 | # endif /* __DARWIN_NON_CANCELABLE */ |
| 777 | |
| 778 | #else /* !__DARWIN_UNIX03 */ |
| 779 | # define __DARWIN_SUF_UNIX03 /* nothing */ |
| 780 | # define __DARWIN_SUF_64_BIT_INO_T /* nothing */ |
| 781 | # define __DARWIN_SUF_NON_CANCELABLE /* nothing */ |
| 782 | # define __DARWIN_SUF_1050 /* nothing */ |
| 783 | #endif /* __DARWIN_UNIX03 */ |
| 784 | |
| 785 | #define __DARWIN_SUF_EXTSN "$DARWIN_EXTSN" |
| 786 | |
| 787 | /* |
| 788 | * symbol versioning macros |
| 789 | */ |
| 790 | #define __DARWIN_ALIAS(sym) __asm("_" __STRING(sym) __DARWIN_SUF_UNIX03) |
| 791 | #define __DARWIN_ALIAS_C(sym) __asm("_" __STRING(sym) __DARWIN_SUF_NON_CANCELABLE __DARWIN_SUF_UNIX03) |
| 792 | #define __DARWIN_ALIAS_I(sym) __asm("_" __STRING(sym) __DARWIN_SUF_64_BIT_INO_T __DARWIN_SUF_UNIX03) |
| 793 | #define __DARWIN_NOCANCEL(sym) __asm("_" __STRING(sym) __DARWIN_SUF_NON_CANCELABLE) |
| 794 | #define __DARWIN_INODE64(sym) __asm("_" __STRING(sym) __DARWIN_SUF_64_BIT_INO_T) |
| 795 | |
| 796 | #define __DARWIN_1050(sym) __asm("_" __STRING(sym) __DARWIN_SUF_1050) |
| 797 | #define __DARWIN_1050ALIAS(sym) __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_UNIX03) |
| 798 | #define __DARWIN_1050ALIAS_C(sym) __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_NON_CANCELABLE __DARWIN_SUF_UNIX03) |
| 799 | #define __DARWIN_1050ALIAS_I(sym) __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_64_BIT_INO_T __DARWIN_SUF_UNIX03) |
| 800 | #define __DARWIN_1050INODE64(sym) __asm("_" __STRING(sym) __DARWIN_SUF_1050 __DARWIN_SUF_64_BIT_INO_T) |
| 801 | |
| 802 | #define __DARWIN_EXTSN(sym) __asm("_" __STRING(sym) __DARWIN_SUF_EXTSN) |
| 803 | #define __DARWIN_EXTSN_C(sym) __asm("_" __STRING(sym) __DARWIN_SUF_EXTSN __DARWIN_SUF_NON_CANCELABLE) |
| 804 | |
| 805 | /* |
| 806 | * symbol release macros |
| 807 | */ |
| 808 | #include <sys/_symbol_aliasing.h> |
| 809 | |
| 810 | #if defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) |
| 811 | #define __DARWIN_ALIAS_STARTING(_mac, _iphone, x) __DARWIN_ALIAS_STARTING_IPHONE_##_iphone(x) |
| 812 | #elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) |
| 813 | #define __DARWIN_ALIAS_STARTING(_mac, _iphone, x) __DARWIN_ALIAS_STARTING_MAC_##_mac(x) |
| 814 | #else |
| 815 | #define __DARWIN_ALIAS_STARTING(_mac, _iphone, x) x |
| 816 | #endif |
| 817 | |
| 818 | |
| 819 | /* |
| 820 | * POSIX.1 requires that the macros we test be defined before any standard |
| 821 | * header file is included. This permits us to convert values for feature |
| 822 | * testing, as necessary, using only _POSIX_C_SOURCE. |
| 823 | * |
| 824 | * Here's a quick run-down of the versions: |
| 825 | * defined(_POSIX_SOURCE)		1003.1-1988 |
| 826 | * _POSIX_C_SOURCE == 1L		1003.1-1990 |
| 827 | * _POSIX_C_SOURCE == 2L		1003.2-1992 C Language Binding Option |
| 828 | * _POSIX_C_SOURCE == 199309L		1003.1b-1993 |
| 829 | * _POSIX_C_SOURCE == 199506L		1003.1c-1995, 1003.1i-1995, |
| 830 | *					and the omnibus ISO/IEC 9945-1: 1996 |
| 831 | * _POSIX_C_SOURCE == 200112L		1003.1-2001 |
| 832 | * _POSIX_C_SOURCE == 200809L		1003.1-2008 |
| 833 | * |
| 834 | * In addition, the X/Open Portability Guide, which is now the Single UNIX |
| 835 | * Specification, defines a feature-test macro which indicates the version of |
| 836 | * that specification, and which subsumes _POSIX_C_SOURCE. |
| 837 | */ |
| 838 | |
| 839 | /* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1L. */ |
| 840 | #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1L |
| 841 | #undef _POSIX_C_SOURCE |
| 842 | #define _POSIX_C_SOURCE 199009L |
| 843 | #endif |
| 844 | |
| 845 | /* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2L. */ |
| 846 | #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2L |
| 847 | #undef _POSIX_C_SOURCE |
| 848 | #define _POSIX_C_SOURCE 199209L |
| 849 | #endif |
| 850 | |
| 851 | /* Deal with various X/Open Portability Guides and Single UNIX Spec. */ |
| 852 | #ifdef _XOPEN_SOURCE |
| 853 | #if _XOPEN_SOURCE - 0L >= 700L && (!defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE - 0L < 200809L) |
| 854 | #undef _POSIX_C_SOURCE |
| 855 | #define _POSIX_C_SOURCE 200809L |
| 856 | #elif _XOPEN_SOURCE - 0L >= 600L && (!defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE - 0L < 200112L) |
| 857 | #undef _POSIX_C_SOURCE |
| 858 | #define _POSIX_C_SOURCE 200112L |
| 859 | #elif _XOPEN_SOURCE - 0L >= 500L && (!defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE - 0L < 199506L) |
| 860 | #undef _POSIX_C_SOURCE |
| 861 | #define _POSIX_C_SOURCE 199506L |
| 862 | #endif |
| 863 | #endif |
| 864 | |
| 865 | /* |
| 866 | * Deal with all versions of POSIX. The ordering relative to the tests above is |
| 867 | * important. |
| 868 | */ |
| 869 | #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) |
| 870 | #define _POSIX_C_SOURCE 198808L |
| 871 | #endif |
| 872 | |
| 873 | /* POSIX C deprecation macros */ |
| 874 | #include <sys/_posix_availability.h> |
| 875 | |
| 876 | #define __POSIX_C_DEPRECATED(ver) ___POSIX_C_DEPRECATED_STARTING_##ver |
| 877 | |
| 878 | /* |
| 879 | * Set a single macro which will always be defined and can be used to determine |
| 880 | * the appropriate namespace. For POSIX, these values will correspond to |
| 881 | * _POSIX_C_SOURCE value. Currently there are two additional levels corresponding |
| 882 | * to ANSI (_ANSI_SOURCE) and Darwin extensions (_DARWIN_C_SOURCE) |
| 883 | */ |
| 884 | #define __DARWIN_C_ANSI 010000L |
| 885 | #define __DARWIN_C_FULL 900000L |
| 886 | |
| 887 | #if defined(_ANSI_SOURCE) |
| 888 | #define __DARWIN_C_LEVEL __DARWIN_C_ANSI |
| 889 | #elif defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE) && !defined(_NONSTD_SOURCE) |
| 890 | #define __DARWIN_C_LEVEL _POSIX_C_SOURCE |
| 891 | #else |
| 892 | #define __DARWIN_C_LEVEL __DARWIN_C_FULL |
| 893 | #endif |
| 894 | |
| 895 | /* If the developer has neither requested a strict language mode nor a version |
| 896 | * of POSIX, turn on functionality provided by __STDC_WANT_LIB_EXT1__ as part |
| 897 | * of __DARWIN_C_FULL. |
| 898 | */ |
| 899 | #if !defined(__STDC_WANT_LIB_EXT1__) && !defined(__STRICT_ANSI__) && __DARWIN_C_LEVEL >= __DARWIN_C_FULL |
| 900 | #define __STDC_WANT_LIB_EXT1__ 1 |
| 901 | #endif |
| 902 | |
| 903 | /* |
| 904 | * long long is not supported in c89 (__STRICT_ANSI__), but g++ -ansi and |
| 905 | * c99 still want long longs. While not perfect, we allow long longs for |
| 906 | * g++. |
| 907 | */ |
| 908 | #if (defined(__STRICT_ANSI__) && (__STDC_VERSION__ - 0 < 199901L) && !defined(__GNUG__)) |
| 909 | #define __DARWIN_NO_LONG_LONG 1 |
| 910 | #else |
| 911 | #define __DARWIN_NO_LONG_LONG 0 |
| 912 | #endif |
| 913 | |
| 914 | /***************************************** |
| 915 | * Public darwin-specific feature macros |
| 916 | *****************************************/ |
| 917 | |
| 918 | /* |
| 919 | * _DARWIN_FEATURE_64_BIT_INODE indicates that the ino_t type is 64-bit, and |
| 920 | * structures modified for 64-bit inodes (like struct stat) will be used. |
| 921 | */ |
| 922 | #if __DARWIN_64_BIT_INO_T |
| 923 | #define _DARWIN_FEATURE_64_BIT_INODE 1 |
| 924 | #endif |
| 925 | |
| 926 | /* |
| 927 | * _DARWIN_FEATURE_64_ONLY_BIT_INODE indicates that the ino_t type may only |
| 928 | * be 64-bit; there is no support for 32-bit ino_t when this macro is defined |
| 929 | * (and non-zero). There is no struct stat64 either, as the regular |
| 930 | * struct stat will already be the 64-bit version. |
| 931 | */ |
| 932 | #if __DARWIN_ONLY_64_BIT_INO_T |
| 933 | #define _DARWIN_FEATURE_ONLY_64_BIT_INODE 1 |
| 934 | #endif |
| 935 | |
| 936 | /* |
| 937 | * _DARWIN_FEATURE_ONLY_VERS_1050 indicates that only those APIs updated |
| 938 | * in 10.5 exists; no pre-10.5 variants are available. |
| 939 | */ |
| 940 | #if __DARWIN_ONLY_VERS_1050 |
| 941 | #define _DARWIN_FEATURE_ONLY_VERS_1050 1 |
| 942 | #endif |
| 943 | |
| 944 | /* |
| 945 | * _DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE indicates only UNIX conforming API |
| 946 | * are available (the legacy BSD APIs are not available) |
| 947 | */ |
| 948 | #if __DARWIN_ONLY_UNIX_CONFORMANCE |
| 949 | #define _DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE 1 |
| 950 | #endif |
| 951 | |
| 952 | /* |
| 953 | * _DARWIN_FEATURE_UNIX_CONFORMANCE indicates whether UNIX conformance is on, |
| 954 | * and specifies the conformance level (3 is SUSv3) |
| 955 | */ |
| 956 | #if __DARWIN_UNIX03 |
| 957 | #define _DARWIN_FEATURE_UNIX_CONFORMANCE 3 |
| 958 | #endif |
| 959 | |
| 960 | |
| 961 | /* |
| 962 | * This macro casts away the qualifier from the variable |
| 963 | * |
| 964 | * Note: use at your own risk, removing qualifiers can result in |
| 965 | * catastrophic run-time failures. |
| 966 | */ |
| 967 | #ifndef __CAST_AWAY_QUALIFIER |
| 968 | /* |
| 969 | * XXX: this shouldn't ignore anything more than -Wcast-qual, |
| 970 | * but the old implementation made it an almighty cast that |
| 971 | * ignored everything, so things break left and right if you |
| 972 | * make it only ignore -Wcast-qual. |
| 973 | */ |
| 974 | #define __CAST_AWAY_QUALIFIER(variable, qualifier, type) \ |
| 975 | 	_Pragma("GCC diagnostic push") \ |
| 976 | 	_Pragma("GCC diagnostic ignored \"-Wcast-qual\"") \ |
| 977 | 	_Pragma("GCC diagnostic ignored \"-Wcast-align\"") \ |
| 978 | 	_Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"") \ |
| 979 | 	((type)(variable)) \ |
| 980 | 	_Pragma("GCC diagnostic pop") |
| 981 | #endif |
| 982 | |
| 983 | /* |
| 984 | * __XNU_PRIVATE_EXTERN is a linkage decoration indicating that a symbol can be |
| 985 | * used from other compilation units, but not other libraries or executables. |
| 986 | */ |
| 987 | #ifndef __XNU_PRIVATE_EXTERN |
| 988 | #define __XNU_PRIVATE_EXTERN __attribute__((visibility("hidden"))) |
| 989 | #endif |
| 990 | |
| 991 | #if __has_include(<ptrcheck.h>) |
| 992 | #include <ptrcheck.h> |
| 993 | #else |
| 994 | #if __has_feature(bounds_safety) |
| 995 | #error -fbounds-safety is enabled, but <ptrcheck.h> is missing. \ |
| 996 | This will lead to difficult-to-diagnose compilation errors. |
| 997 | #endif /* __has_feature(bounds_safety) */ |
| 998 | |
| 999 | /* |
| 1000 | * We intentionally define to nothing pointer attributes which do not have an |
| 1001 | * impact on the ABI. __indexable and __bidi_indexable are not defined because |
| 1002 | * of the ABI incompatibility that makes the diagnostic preferable. |
| 1003 | */ |
| 1004 | #define __has_ptrcheck 0 |
| 1005 | #define __single |
| 1006 | #define __unsafe_indexable |
| 1007 | #define __counted_by(N) |
| 1008 | #define __counted_by_or_null(N) |
| 1009 | #define __sized_by(N) |
| 1010 | #define __sized_by_or_null(N) |
| 1011 | #define __ended_by(E) |
| 1012 | #define __terminated_by(T) |
| 1013 | #define __null_terminated |
| 1014 | |
| 1015 | /* |
| 1016 | * Similarly, we intentionally define to nothing the |
| 1017 | * __ptrcheck_abi_assume_single and __ptrcheck_abi_assume_unsafe_indexable |
| 1018 | * macros because they do not lead to an ABI incompatibility. However, we do not |
| 1019 | * define the indexable and unsafe_indexable ones because the diagnostic is |
| 1020 | * better than the silent ABI break. |
| 1021 | */ |
| 1022 | #define __ptrcheck_abi_assume_single() |
| 1023 | #define __ptrcheck_abi_assume_unsafe_indexable() |
| 1024 | |
| 1025 | /* __unsafe_forge intrinsics are defined as regular C casts. */ |
| 1026 | #define __unsafe_forge_bidi_indexable(T, P, S) ((T)(P)) |
| 1027 | #define __unsafe_forge_single(T, P) ((T)(P)) |
| 1028 | #define __unsafe_forge_terminated_by(T, P, E) ((T)(P)) |
| 1029 | #define __unsafe_forge_null_terminated(T, P) ((T)(P)) |
| 1030 | #define __terminated_by_to_indexable(P) (P) |
| 1031 | #define __unsafe_terminated_by_to_indexable(P) (P) |
| 1032 | #define __null_terminated_to_indexable(P) (P) |
| 1033 | #define __unsafe_null_terminated_to_indexable(P) (P) |
| 1034 | #define __unsafe_terminated_by_from_indexable(T, P, ...) (P) |
| 1035 | #define __unsafe_null_terminated_from_indexable(P, ...) (P) |
| 1036 | |
| 1037 | /* decay operates normally; attribute is meaningless without pointer checks. */ |
| 1038 | #define __array_decay_dicards_count_in_parameters |
| 1039 | |
| 1040 | /* this is a write-once variable; not useful without pointer checks. */ |
| 1041 | #define __unsafe_late_const |
| 1042 | |
| 1043 | #define __ptrcheck_unavailable |
| 1044 | #define __ptrcheck_unavailable_r(REPLACEMENT) |
| 1045 | |
| 1046 | #endif /* !__has_include(<ptrcheck.h>) */ |
| 1047 | |
| 1048 | |
| 1049 | #define __ASSUME_PTR_ABI_SINGLE_BEGIN __ptrcheck_abi_assume_single() |
| 1050 | #define __ASSUME_PTR_ABI_SINGLE_END __ptrcheck_abi_assume_unsafe_indexable() |
| 1051 | |
| 1052 | #if __has_ptrcheck |
| 1053 | #define __header_indexable __indexable |
| 1054 | #define __header_bidi_indexable __bidi_indexable |
| 1055 | #else |
| 1056 | #define __header_indexable |
| 1057 | #define __header_bidi_indexable |
| 1058 | #endif |
| 1059 | |
| 1060 | /* |
| 1061 | * Architecture validation for current SDK |
| 1062 | */ |
| 1063 | #if !defined(__sys_cdefs_arch_unknown__) && defined(__i386__) |
| 1064 | #elif !defined(__sys_cdefs_arch_unknown__) && defined(__x86_64__) |
| 1065 | #elif !defined(__sys_cdefs_arch_unknown__) && defined(__arm__) |
| 1066 | #elif !defined(__sys_cdefs_arch_unknown__) && defined(__arm64__) |
| 1067 | #else |
| 1068 | #error Unsupported architecture |
| 1069 | #endif |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | #define __compiler_barrier() __asm__ __volatile__("" ::: "memory") |
| 1074 | |
| 1075 | #if __has_attribute(enum_extensibility) |
| 1076 | #define __enum_open __attribute__((__enum_extensibility__(open))) |
| 1077 | #define __enum_closed __attribute__((__enum_extensibility__(closed))) |
| 1078 | #else |
| 1079 | #define __enum_open |
| 1080 | #define __enum_closed |
| 1081 | #endif // __has_attribute(enum_extensibility) |
| 1082 | |
| 1083 | #if __has_attribute(flag_enum) |
| 1084 | #define __enum_options __attribute__((__flag_enum__)) |
| 1085 | #else |
| 1086 | #define __enum_options |
| 1087 | #endif |
| 1088 | |
| 1089 | /* |
| 1090 | * Similar to OS_ENUM/OS_CLOSED_ENUM/OS_OPTIONS/OS_CLOSED_OPTIONS |
| 1091 | * |
| 1092 | * This provides more advanced type checking on compilers supporting |
| 1093 | * the proper extensions, even in C. |
| 1094 | */ |
| 1095 | #if __has_feature(objc_fixed_enum) || __has_extension(cxx_fixed_enum) || \ |
| 1096 | __has_extension(cxx_strong_enums) |
| 1097 | #define __enum_decl(_name, _type, ...) \ |
| 1098 | 	 typedef enum : _type __VA_ARGS__ __enum_open _name |
| 1099 | #define __enum_closed_decl(_name, _type, ...) \ |
| 1100 | 	 typedef enum : _type __VA_ARGS__ __enum_closed _name |
| 1101 | #define __options_decl(_name, _type, ...) \ |
| 1102 | 	 typedef enum : _type __VA_ARGS__ __enum_open __enum_options _name |
| 1103 | #define __options_closed_decl(_name, _type, ...) \ |
| 1104 | 	 typedef enum : _type __VA_ARGS__ __enum_closed __enum_options _name |
| 1105 | #else |
| 1106 | #define __enum_decl(_name, _type, ...) \ |
| 1107 | 	 typedef _type _name; enum __VA_ARGS__ __enum_open |
| 1108 | #define __enum_closed_decl(_name, _type, ...) \ |
| 1109 | 	 typedef _type _name; enum __VA_ARGS__ __enum_closed |
| 1110 | #define __options_decl(_name, _type, ...) \ |
| 1111 | 	 typedef _type _name; enum __VA_ARGS__ __enum_open __enum_options |
| 1112 | #define __options_closed_decl(_name, _type, ...) \ |
| 1113 | 	 typedef _type _name; enum __VA_ARGS__ __enum_closed __enum_options |
| 1114 | #endif |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | #define __kernel_ptr_semantics |
| 1119 | #define __kernel_data_semantics |
| 1120 | #define __kernel_dual_semantics |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | #if defined(KERNEL_PRIVATE) && \ |
| 1125 | __has_attribute(xnu_data_size) && \ |
| 1126 | __has_attribute(xnu_returns_data_pointer) |
| 1127 | /* |
| 1128 | * Annotate function parameters to specify that they semantically |
| 1129 | * represent the size of a data-only backing storage. |
| 1130 | */ |
| 1131 | # define __xnu_data_size __attribute__((xnu_data_size)) |
| 1132 | /* |
| 1133 | * Annotate function declarations to specify that the pointer they return |
| 1134 | * points to a data-only backing storage. |
| 1135 | */ |
| 1136 | # define __xnu_returns_data_pointer __attribute__((xnu_returns_data_pointer)) |
| 1137 | #else |
| 1138 | # define __xnu_data_size |
| 1139 | # define __xnu_returns_data_pointer |
| 1140 | #endif |
| 1141 | |
| 1142 | |
| 1143 | #endif /* !_CDEFS_H_ */ |