1 /* 2 +----------------------------------------------------------------------+ 3 | Zend Engine | 4 +----------------------------------------------------------------------+ 5 | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | 6 +----------------------------------------------------------------------+ 7 | This source file is subject to version 2.00 of the Zend license, | 8 | that is bundled with this package in the file LICENSE, and is | 9 | available through the world-wide-web at the following url: | 10 | http://www.zend.com/license/2_00.txt. | 11 | If you did not receive a copy of the Zend license and are unable to | 12 | obtain it through the world-wide-web, please send a note to | 13 | license@zend.com so we can mail you a copy immediately. | 14 +----------------------------------------------------------------------+ 15 | Authors: Andi Gutmans <andi@php.net> | 16 | Zeev Suraski <zeev@php.net> | 17 | Dmitry Stogov <dmitry@php.net> | 18 +----------------------------------------------------------------------+ 19 */ 20 21 #ifndef ZEND_PORTABILITY_H 22 #define ZEND_PORTABILITY_H 23 24 #ifdef __cplusplus 25 #define BEGIN_EXTERN_C() extern "C" { 26 #define END_EXTERN_C() } 27 #else 28 #define BEGIN_EXTERN_C() 29 #define END_EXTERN_C() 30 #endif 31 32 /* 33 * general definitions 34 */ 35 36 #ifdef ZEND_WIN32 37 # include "zend_config.w32.h" 38 # define ZEND_PATHS_SEPARATOR ';' 39 #elif defined(__riscos__) 40 # include <zend_config.h> 41 # define ZEND_PATHS_SEPARATOR ';' 42 #else 43 # include <zend_config.h> 44 # define ZEND_PATHS_SEPARATOR ':' 45 #endif 46 47 #include "../TSRM/TSRM.h" 48 49 #include <stdio.h> 50 #include <assert.h> 51 #include <math.h> 52 53 #ifdef HAVE_UNIX_H 54 # include <unix.h> 55 #endif 56 57 #include <stdarg.h> 58 #include <stddef.h> 59 60 #ifdef HAVE_DLFCN_H 61 # include <dlfcn.h> 62 #endif 63 64 #include <limits.h> 65 66 #if defined(ZEND_WIN32) && !defined(__clang__) 67 #include <intrin.h> 68 #endif 69 70 #include "zend_range_check.h" 71 72 /* GCC x.y.z supplies __GNUC__ = x and __GNUC_MINOR__ = y */ 73 #ifdef __GNUC__ 74 # define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__) 75 #else 76 # define ZEND_GCC_VERSION 0 77 #endif 78 79 /* Compatibility with non-clang compilers */ 80 #ifndef __has_attribute 81 # define __has_attribute(x) 0 82 #endif 83 #ifndef __has_builtin 84 # define __has_builtin(x) 0 85 #endif 86 #ifndef __has_feature 87 # define __has_feature(x) 0 88 #endif 89 90 #if defined(ZEND_WIN32) && !defined(__clang__) 91 # define ZEND_ASSUME(c) __assume(c) 92 #elif defined(__clang__) && __has_builtin(__builtin_assume) 93 # pragma clang diagnostic ignored "-Wassume" 94 # define ZEND_ASSUME(c) __builtin_assume(c) 95 #elif ((defined(__GNUC__) && ZEND_GCC_VERSION >= 4005) || __has_builtin(__builtin_unreachable)) && PHP_HAVE_BUILTIN_EXPECT 96 # define ZEND_ASSUME(c) do { \ 97 if (__builtin_expect(!(c), 0)) __builtin_unreachable(); \ 98 } while (0) 99 #else 100 # define ZEND_ASSUME(c) 101 #endif 102 103 #if ZEND_DEBUG 104 # define ZEND_ASSERT(c) assert(c) 105 #else 106 # define ZEND_ASSERT(c) ZEND_ASSUME(c) 107 #endif 108 109 #if ZEND_DEBUG 110 # define ZEND_UNREACHABLE() do {ZEND_ASSERT(0); ZEND_ASSUME(0);} while (0) 111 #else 112 # define ZEND_UNREACHABLE() ZEND_ASSUME(0) 113 #endif 114 115 /* pseudo fallthrough keyword; */ 116 #if defined(__GNUC__) && __GNUC__ >= 7 117 # define ZEND_FALLTHROUGH __attribute__((__fallthrough__)) 118 #else 119 # define ZEND_FALLTHROUGH ((void)0) 120 #endif 121 122 /* Only use this macro if you know for sure that all of the switches values 123 are covered by its case statements */ 124 #define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_UNREACHABLE(); break; 125 126 #if defined(__GNUC__) && __GNUC__ >= 4 127 # define ZEND_IGNORE_VALUE(x) (({ __typeof__ (x) __x = (x); (void) __x; })) 128 #else 129 # define ZEND_IGNORE_VALUE(x) ((void) (x)) 130 #endif 131 132 #define zend_quiet_write(...) ZEND_IGNORE_VALUE(write(__VA_ARGS__)) 133 134 /* all HAVE_XXX test have to be after the include of zend_config above */ 135 136 #if defined(HAVE_LIBDL) && !defined(ZEND_WIN32) 137 138 # if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__) 139 # define __SANITIZE_ADDRESS__ 140 # endif 141 142 # ifndef RTLD_LAZY 143 # define RTLD_LAZY 1 /* Solaris 1, FreeBSD's (2.1.7.1 and older) */ 144 # endif 145 146 # ifndef RTLD_GLOBAL 147 # define RTLD_GLOBAL 0 148 # endif 149 150 # ifdef PHP_USE_RTLD_NOW 151 # define PHP_RTLD_MODE RTLD_NOW 152 # else 153 # define PHP_RTLD_MODE RTLD_LAZY 154 # endif 155 156 # if defined(RTLD_GROUP) && defined(RTLD_WORLD) && defined(RTLD_PARENT) 157 # define DL_LOAD(libname) dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL | RTLD_GROUP | RTLD_WORLD | RTLD_PARENT) 158 # elif defined(RTLD_DEEPBIND) && !defined(__SANITIZE_ADDRESS__) && !__has_feature(memory_sanitizer) 159 # define DL_LOAD(libname) dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL | RTLD_DEEPBIND) 160 # else 161 # define DL_LOAD(libname) dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL) 162 # endif 163 # define DL_UNLOAD dlclose 164 # if defined(DLSYM_NEEDS_UNDERSCORE) 165 # define DL_FETCH_SYMBOL(h,s) dlsym((h), "_" s) 166 # else 167 # define DL_FETCH_SYMBOL dlsym 168 # endif 169 # define DL_ERROR dlerror 170 # define DL_HANDLE void * 171 # define ZEND_EXTENSIONS_SUPPORT 1 172 #elif defined(ZEND_WIN32) 173 # define DL_LOAD(libname) LoadLibrary(libname) 174 # define DL_FETCH_SYMBOL GetProcAddress 175 # define DL_UNLOAD FreeLibrary 176 # define DL_HANDLE HMODULE 177 # define ZEND_EXTENSIONS_SUPPORT 1 178 #else 179 # define DL_HANDLE void * 180 # define ZEND_EXTENSIONS_SUPPORT 0 181 #endif 182 183 #if defined(HAVE_ALLOCA_H) && !defined(_ALLOCA_H) 184 # include <alloca.h> 185 #endif 186 /* AIX requires this to be the first thing in the file. */ 187 #ifndef __GNUC__ 188 # ifndef HAVE_ALLOCA_H 189 # ifdef _AIX 190 # pragma alloca 191 # else 192 # ifndef alloca /* predefined by HP cc +Olibcalls */ 193 char *alloca(); 194 # endif 195 # endif 196 # endif 197 #endif 198 199 #if !ZEND_DEBUG && (defined(HAVE_ALLOCA) || (defined (__GNUC__) && __GNUC__ >= 2)) && !(defined(ZTS) && defined(HPUX)) && !defined(DARWIN) 200 # define ZEND_ALLOCA_MAX_SIZE (32 * 1024) 201 # define ALLOCA_FLAG(name) \ 202 bool name; 203 # define SET_ALLOCA_FLAG(name) \ 204 name = true 205 # define do_alloca_ex(size, limit, use_heap) \ 206 ((use_heap = (UNEXPECTED((size) > (limit)))) ? emalloc(size) : alloca(size)) 207 # define do_alloca(size, use_heap) \ 208 do_alloca_ex(size, ZEND_ALLOCA_MAX_SIZE, use_heap) 209 # define free_alloca(p, use_heap) \ 210 do { if (UNEXPECTED(use_heap)) efree(p); } while (0) 211 #else 212 # define ALLOCA_FLAG(name) 213 # define SET_ALLOCA_FLAG(name) 214 # define do_alloca(p, use_heap) emalloc(p) 215 # define free_alloca(p, use_heap) efree(p) 216 #endif 217 218 #if ZEND_GCC_VERSION >= 2096 || __has_attribute(__malloc__) 219 # define ZEND_ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) 220 #elif defined(ZEND_WIN32) 221 # define ZEND_ATTRIBUTE_MALLOC __declspec(allocator) __declspec(restrict) 222 #else 223 # define ZEND_ATTRIBUTE_MALLOC 224 #endif 225 226 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(alloc_size) 227 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X) __attribute__ ((alloc_size(X))) 228 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y) __attribute__ ((alloc_size(X,Y))) 229 #else 230 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X) 231 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y) 232 #endif 233 234 #if ZEND_GCC_VERSION >= 3000 235 # define ZEND_ATTRIBUTE_CONST __attribute__((const)) 236 #else 237 # define ZEND_ATTRIBUTE_CONST 238 #endif 239 240 #if ZEND_GCC_VERSION >= 2007 || __has_attribute(format) 241 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first))) 242 #else 243 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first) 244 #endif 245 246 #if (ZEND_GCC_VERSION >= 3001 && !defined(__INTEL_COMPILER)) || __has_attribute(format) 247 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first))) 248 #else 249 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first) 250 #endif 251 252 #if ZEND_GCC_VERSION >= 3001 || __has_attribute(deprecated) 253 # define ZEND_ATTRIBUTE_DEPRECATED __attribute__((deprecated)) 254 #elif defined(ZEND_WIN32) 255 # define ZEND_ATTRIBUTE_DEPRECATED __declspec(deprecated) 256 #else 257 # define ZEND_ATTRIBUTE_DEPRECATED 258 #endif 259 260 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(unused) 261 # define ZEND_ATTRIBUTE_UNUSED __attribute__((unused)) 262 #else 263 # define ZEND_ATTRIBUTE_UNUSED 264 #endif 265 266 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 4003 267 # define ZEND_COLD __attribute__((cold)) 268 # ifdef __OPTIMIZE__ 269 # define ZEND_OPT_SIZE __attribute__((optimize("Os"))) 270 # define ZEND_OPT_SPEED __attribute__((optimize("Ofast"))) 271 # else 272 # define ZEND_OPT_SIZE 273 # define ZEND_OPT_SPEED 274 # endif 275 #else 276 # define ZEND_COLD 277 # define ZEND_OPT_SIZE 278 # define ZEND_OPT_SPEED 279 #endif 280 281 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 5000 282 # define ZEND_ATTRIBUTE_UNUSED_LABEL __attribute__((unused)); 283 # define ZEND_ATTRIBUTE_COLD_LABEL __attribute__((cold)); 284 #else 285 # define ZEND_ATTRIBUTE_UNUSED_LABEL 286 # define ZEND_ATTRIBUTE_COLD_LABEL 287 #endif 288 289 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004 && defined(__i386__) 290 # define ZEND_FASTCALL __attribute__((fastcall)) 291 #elif defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER == 1700 292 # define ZEND_FASTCALL __fastcall 293 #elif defined(_MSC_VER) && _MSC_VER >= 1800 && !defined(__clang__) 294 # define ZEND_FASTCALL __vectorcall 295 #else 296 # define ZEND_FASTCALL 297 #endif 298 299 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)) || __has_attribute(noreturn) 300 # define HAVE_NORETURN 301 # define ZEND_NORETURN __attribute__((noreturn)) 302 #elif defined(ZEND_WIN32) 303 # define HAVE_NORETURN 304 # define ZEND_NORETURN __declspec(noreturn) 305 #else 306 # define ZEND_NORETURN 307 #endif 308 309 #if __has_attribute(force_align_arg_pointer) 310 # define ZEND_STACK_ALIGNED __attribute__((force_align_arg_pointer)) 311 #else 312 # define ZEND_STACK_ALIGNED 313 #endif 314 315 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)) 316 # define HAVE_NORETURN_ALIAS 317 # define HAVE_ATTRIBUTE_WEAK 318 #endif 319 320 #if ZEND_GCC_VERSION >= 3001 || __has_builtin(__builtin_constant_p) 321 # define HAVE_BUILTIN_CONSTANT_P 322 #endif 323 324 #ifdef HAVE_BUILTIN_CONSTANT_P 325 # define ZEND_CONST_COND(_condition, _default) \ 326 (__builtin_constant_p(_condition) ? (_condition) : (_default)) 327 #else 328 # define ZEND_CONST_COND(_condition, _default) \ 329 (_default) 330 #endif 331 332 #if ZEND_DEBUG || defined(ZEND_WIN32_NEVER_INLINE) 333 # define zend_always_inline inline 334 # define zend_never_inline 335 #else 336 # if defined(__GNUC__) 337 # if __GNUC__ >= 3 338 # define zend_always_inline inline __attribute__((always_inline)) 339 # define zend_never_inline __attribute__((noinline)) 340 # else 341 # define zend_always_inline inline 342 # define zend_never_inline 343 # endif 344 # elif defined(_MSC_VER) 345 # define zend_always_inline __forceinline 346 # define zend_never_inline __declspec(noinline) 347 # else 348 # if __has_attribute(always_inline) 349 # define zend_always_inline inline __attribute__((always_inline)) 350 # else 351 # define zend_always_inline inline 352 # endif 353 # if __has_attribute(noinline) 354 # define zend_never_inline __attribute__((noinline)) 355 # else 356 # define zend_never_inline 357 # endif 358 # endif 359 #endif /* ZEND_DEBUG */ 360 361 #ifdef PHP_HAVE_BUILTIN_EXPECT 362 # define EXPECTED(condition) __builtin_expect(!!(condition), 1) 363 # define UNEXPECTED(condition) __builtin_expect(!!(condition), 0) 364 #else 365 # define EXPECTED(condition) (condition) 366 # define UNEXPECTED(condition) (condition) 367 #endif 368 369 #ifndef XtOffsetOf 370 # define XtOffsetOf(s_type, field) offsetof(s_type, field) 371 #endif 372 373 #ifdef HAVE_SIGSETJMP 374 # define SETJMP(a) sigsetjmp(a, 0) 375 # define LONGJMP(a,b) siglongjmp(a, b) 376 # define JMP_BUF sigjmp_buf 377 #else 378 # define SETJMP(a) setjmp(a) 379 # define LONGJMP(a,b) longjmp(a, b) 380 # define JMP_BUF jmp_buf 381 #endif 382 383 #if ZEND_DEBUG 384 # define ZEND_FILE_LINE_D const char *__zend_filename, const uint32_t __zend_lineno 385 # define ZEND_FILE_LINE_DC , ZEND_FILE_LINE_D 386 # define ZEND_FILE_LINE_ORIG_D const char *__zend_orig_filename, const uint32_t __zend_orig_lineno 387 # define ZEND_FILE_LINE_ORIG_DC , ZEND_FILE_LINE_ORIG_D 388 # define ZEND_FILE_LINE_RELAY_C __zend_filename, __zend_lineno 389 # define ZEND_FILE_LINE_RELAY_CC , ZEND_FILE_LINE_RELAY_C 390 # define ZEND_FILE_LINE_C __FILE__, __LINE__ 391 # define ZEND_FILE_LINE_CC , ZEND_FILE_LINE_C 392 # define ZEND_FILE_LINE_EMPTY_C NULL, 0 393 # define ZEND_FILE_LINE_EMPTY_CC , ZEND_FILE_LINE_EMPTY_C 394 # define ZEND_FILE_LINE_ORIG_RELAY_C __zend_orig_filename, __zend_orig_lineno 395 # define ZEND_FILE_LINE_ORIG_RELAY_CC , ZEND_FILE_LINE_ORIG_RELAY_C 396 #else 397 # define ZEND_FILE_LINE_D void 398 # define ZEND_FILE_LINE_DC 399 # define ZEND_FILE_LINE_ORIG_D void 400 # define ZEND_FILE_LINE_ORIG_DC 401 # define ZEND_FILE_LINE_RELAY_C 402 # define ZEND_FILE_LINE_RELAY_CC 403 # define ZEND_FILE_LINE_C 404 # define ZEND_FILE_LINE_CC 405 # define ZEND_FILE_LINE_EMPTY_C 406 # define ZEND_FILE_LINE_EMPTY_CC 407 # define ZEND_FILE_LINE_ORIG_RELAY_C 408 # define ZEND_FILE_LINE_ORIG_RELAY_CC 409 #endif /* ZEND_DEBUG */ 410 411 #if ZEND_DEBUG 412 # define Z_DBG(expr) (expr) 413 #else 414 # define Z_DBG(expr) 415 #endif 416 417 #ifdef ZTS 418 # define ZTS_V 1 419 #else 420 # define ZTS_V 0 421 #endif 422 423 #ifndef LONG_MAX 424 # define LONG_MAX 2147483647L 425 #endif 426 427 #ifndef LONG_MIN 428 # define LONG_MIN (- LONG_MAX - 1) 429 #endif 430 431 #define MAX_LENGTH_OF_DOUBLE 32 432 433 #undef MIN 434 #undef MAX 435 #define MAX(a, b) (((a)>(b))?(a):(b)) 436 #define MIN(a, b) (((a)<(b))?(a):(b)) 437 438 #define ZEND_BIT_TEST(bits, bit) \ 439 (((bits)[(bit) / (sizeof((bits)[0])*8)] >> ((bit) & (sizeof((bits)[0])*8-1))) & 1) 440 441 #define ZEND_INFINITY INFINITY 442 443 #define ZEND_NAN NAN 444 445 #if defined(__cplusplus) && __cplusplus >= 201103L 446 extern "C++" { 447 # include <cmath> 448 } 449 # define zend_isnan std::isnan 450 # define zend_isinf std::isinf 451 # define zend_finite std::isfinite 452 #else 453 # include <math.h> 454 # define zend_isnan(a) isnan(a) 455 # define zend_isinf(a) isinf(a) 456 # define zend_finite(a) isfinite(a) 457 #endif 458 459 #define ZEND_STRL(str) (str), (sizeof(str)-1) 460 #define ZEND_STRS(str) (str), (sizeof(str)) 461 #define ZEND_NORMALIZE_BOOL(n) \ 462 ((n) ? (((n)<0) ? -1 : 1) : 0) 463 #define ZEND_TRUTH(x) ((x) ? 1 : 0) 464 #define ZEND_LOG_XOR(a, b) (ZEND_TRUTH(a) ^ ZEND_TRUTH(b)) 465 466 /** 467 * Do a three-way comparison of two integers and returns -1, 0 or 1 468 * depending on whether #a is smaller, equal or larger than #b. 469 */ 470 #define ZEND_THREEWAY_COMPARE(a, b) ((a) == (b) ? 0 : ((a) < (b) ? -1 : 1)) 471 472 #define ZEND_MAX_RESERVED_RESOURCES 6 473 474 /* excpt.h on Digital Unix 4.0 defines function_table */ 475 #undef function_table 476 477 #ifdef ZEND_WIN32 478 #define ZEND_SECURE_ZERO(var, size) RtlSecureZeroMemory((var), (size)) 479 #else 480 #define ZEND_SECURE_ZERO(var, size) explicit_bzero((var), (size)) 481 #endif 482 483 /* This check should only be used on network socket, not file descriptors */ 484 #ifdef ZEND_WIN32 485 #define ZEND_VALID_SOCKET(sock) (INVALID_SOCKET != (sock)) 486 #else 487 #define ZEND_VALID_SOCKET(sock) ((sock) >= 0) 488 #endif 489 490 /* Intrinsics macros start. */ 491 492 /* Memory sanitizer is incompatible with ifunc resolvers. Even if the resolver 493 * is marked as no_sanitize("memory") it will still be instrumented and crash. */ 494 #if __has_feature(memory_sanitizer) || __has_feature(thread_sanitizer) || \ 495 __has_feature(dataflow_sanitizer) 496 # undef HAVE_FUNC_ATTRIBUTE_IFUNC 497 #endif 498 499 /* Only use ifunc resolvers if we have __builtin_cpu_supports() and __builtin_cpu_init(), 500 * otherwise the use of zend_cpu_supports() may not be safe inside ifunc resolvers. */ 501 #if defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(HAVE_FUNC_ATTRIBUTE_TARGET) && \ 502 defined(PHP_HAVE_BUILTIN_CPU_SUPPORTS) && defined(PHP_HAVE_BUILTIN_CPU_INIT) 503 # define ZEND_INTRIN_HAVE_IFUNC_TARGET 1 504 #endif 505 506 #if (defined(__i386__) || defined(__x86_64__)) 507 # if defined(HAVE_TMMINTRIN_H) 508 # define PHP_HAVE_SSSE3 509 # endif 510 511 # if defined(HAVE_NMMINTRIN_H) 512 # define PHP_HAVE_SSE4_2 513 # endif 514 515 # if defined(HAVE_WMMINTRIN_H) 516 # define PHP_HAVE_PCLMUL 517 # endif 518 519 /* 520 * AVX2 support was added in gcc 4.7, but AVX2 intrinsics don't work in 521 * __attribute__((target("avx2"))) functions until gcc 4.9. 522 */ 523 # if defined(HAVE_IMMINTRIN_H) && \ 524 (defined(__llvm__) || defined(__clang__) || (defined(__GNUC__) && ZEND_GCC_VERSION >= 4009)) 525 # define PHP_HAVE_AVX2 526 # endif 527 #endif 528 529 #ifdef __SSSE3__ 530 /* Instructions compiled directly. */ 531 # define ZEND_INTRIN_SSSE3_NATIVE 1 532 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_SSSE3)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64))) 533 /* Function resolved by ifunc or MINIT. */ 534 # define ZEND_INTRIN_SSSE3_RESOLVER 1 535 #endif 536 537 /* Do not use for conditional declaration of API functions! */ 538 #if defined(ZEND_INTRIN_SSSE3_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) 539 # define ZEND_INTRIN_SSSE3_FUNC_PROTO 1 540 #elif defined(ZEND_INTRIN_SSSE3_RESOLVER) 541 # define ZEND_INTRIN_SSSE3_FUNC_PTR 1 542 #endif 543 544 #ifdef ZEND_INTRIN_SSSE3_RESOLVER 545 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET 546 # define ZEND_INTRIN_SSSE3_FUNC_DECL(func) ZEND_API func __attribute__((target("ssse3"))) 547 # else 548 # define ZEND_INTRIN_SSSE3_FUNC_DECL(func) func 549 # endif 550 #else 551 # define ZEND_INTRIN_SSSE3_FUNC_DECL(func) 552 #endif 553 554 #ifdef __SSE4_2__ 555 /* Instructions compiled directly. */ 556 # define ZEND_INTRIN_SSE4_2_NATIVE 1 557 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_SSE4_2)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64))) 558 /* Function resolved by ifunc or MINIT. */ 559 # define ZEND_INTRIN_SSE4_2_RESOLVER 1 560 #endif 561 562 /* Do not use for conditional declaration of API functions! */ 563 #if defined(ZEND_INTRIN_SSE4_2_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) 564 # define ZEND_INTRIN_SSE4_2_FUNC_PROTO 1 565 #elif defined(ZEND_INTRIN_SSE4_2_RESOLVER) 566 # define ZEND_INTRIN_SSE4_2_FUNC_PTR 1 567 #endif 568 569 #ifdef ZEND_INTRIN_SSE4_2_RESOLVER 570 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET 571 # define ZEND_INTRIN_SSE4_2_FUNC_DECL(func) ZEND_API func __attribute__((target("sse4.2"))) 572 # else 573 # define ZEND_INTRIN_SSE4_2_FUNC_DECL(func) func 574 # endif 575 #else 576 # define ZEND_INTRIN_SSE4_2_FUNC_DECL(func) 577 #endif 578 579 #ifdef __PCLMUL__ 580 /* Instructions compiled directly. */ 581 # define ZEND_INTRIN_PCLMUL_NATIVE 1 582 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_PCLMUL)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64))) 583 /* Function resolved by ifunc or MINIT. */ 584 # define ZEND_INTRIN_PCLMUL_RESOLVER 1 585 #endif 586 587 /* Do not use for conditional declaration of API functions! */ 588 #if defined(ZEND_INTRIN_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000)) 589 /* __builtin_cpu_supports has pclmul from gcc9 */ 590 # define ZEND_INTRIN_PCLMUL_FUNC_PROTO 1 591 #elif defined(ZEND_INTRIN_PCLMUL_RESOLVER) 592 # define ZEND_INTRIN_PCLMUL_FUNC_PTR 1 593 #endif 594 595 #ifdef ZEND_INTRIN_PCLMUL_RESOLVER 596 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET 597 # define ZEND_INTRIN_PCLMUL_FUNC_DECL(func) ZEND_API func __attribute__((target("pclmul"))) 598 # else 599 # define ZEND_INTRIN_PCLMUL_FUNC_DECL(func) func 600 # endif 601 #else 602 # define ZEND_INTRIN_PCLMUL_FUNC_DECL(func) 603 #endif 604 605 #if defined(ZEND_INTRIN_SSE4_2_NATIVE) && defined(ZEND_INTRIN_PCLMUL_NATIVE) 606 /* Instructions compiled directly. */ 607 # define ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE 1 608 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_SSE4_2) && defined(PHP_HAVE_PCLMUL)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64))) 609 /* Function resolved by ifunc or MINIT. */ 610 # define ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER 1 611 #endif 612 613 /* Do not use for conditional declaration of API functions! */ 614 #if defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) && (!defined(__GNUC__) || (ZEND_GCC_VERSION >= 9000)) 615 /* __builtin_cpu_supports has pclmul from gcc9 */ 616 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_PROTO 1 617 #elif defined(ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER) 618 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_PTR 1 619 #endif 620 621 #ifdef ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER 622 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET 623 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_DECL(func) ZEND_API func __attribute__((target("sse4.2,pclmul"))) 624 # else 625 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_DECL(func) func 626 # endif 627 #else 628 # define ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_DECL(func) 629 #endif 630 631 #ifdef __AVX2__ 632 # define ZEND_INTRIN_AVX2_NATIVE 1 633 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_AVX2)) || (defined(ZEND_WIN32) && (!defined(_M_ARM64))) 634 # define ZEND_INTRIN_AVX2_RESOLVER 1 635 #endif 636 637 /* Do not use for conditional declaration of API functions! */ 638 #if defined(ZEND_INTRIN_AVX2_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) 639 # define ZEND_INTRIN_AVX2_FUNC_PROTO 1 640 #elif defined(ZEND_INTRIN_AVX2_RESOLVER) 641 # define ZEND_INTRIN_AVX2_FUNC_PTR 1 642 #endif 643 644 #ifdef ZEND_INTRIN_AVX2_RESOLVER 645 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET 646 # define ZEND_INTRIN_AVX2_FUNC_DECL(func) ZEND_API func __attribute__((target("avx2"))) 647 # else 648 # define ZEND_INTRIN_AVX2_FUNC_DECL(func) func 649 # endif 650 #else 651 # define ZEND_INTRIN_AVX2_FUNC_DECL(func) 652 #endif 653 654 #if PHP_HAVE_AVX512_SUPPORTS && defined(HAVE_FUNC_ATTRIBUTE_TARGET) || defined(ZEND_WIN32) 655 #define ZEND_INTRIN_AVX512_RESOLVER 1 656 #endif 657 658 #if defined(ZEND_INTRIN_AVX512_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) 659 # define ZEND_INTRIN_AVX512_FUNC_PROTO 1 660 #elif defined(ZEND_INTRIN_AVX512_RESOLVER) 661 # define ZEND_INTRIN_AVX512_FUNC_PTR 1 662 #endif 663 664 #ifdef ZEND_INTRIN_AVX512_RESOLVER 665 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET 666 # define ZEND_INTRIN_AVX512_FUNC_DECL(func) ZEND_API func __attribute__((target("avx512f,avx512cd,avx512vl,avx512dq,avx512bw"))) 667 # else 668 # define ZEND_INTRIN_AVX512_FUNC_DECL(func) func 669 # endif 670 #else 671 # define ZEND_INTRIN_AVX512_FUNC_DECL(func) 672 #endif 673 674 #if PHP_HAVE_AVX512_VBMI_SUPPORTS && defined(HAVE_FUNC_ATTRIBUTE_TARGET) 675 #define ZEND_INTRIN_AVX512_VBMI_RESOLVER 1 676 #endif 677 678 #if defined(ZEND_INTRIN_AVX512_VBMI_RESOLVER) && defined(ZEND_INTRIN_HAVE_IFUNC_TARGET) 679 # define ZEND_INTRIN_AVX512_VBMI_FUNC_PROTO 1 680 #elif defined(ZEND_INTRIN_AVX512_VBMI_RESOLVER) 681 # define ZEND_INTRIN_AVX512_VBMI_FUNC_PTR 1 682 #endif 683 684 #ifdef ZEND_INTRIN_AVX512_VBMI_RESOLVER 685 # ifdef HAVE_FUNC_ATTRIBUTE_TARGET 686 # define ZEND_INTRIN_AVX512_VBMI_FUNC_DECL(func) ZEND_API func __attribute__((target("avx512f,avx512cd,avx512vl,avx512dq,avx512bw,avx512vbmi"))) 687 # else 688 # define ZEND_INTRIN_AVX512_VBMI_FUNC_DECL(func) func 689 # endif 690 #else 691 # define ZEND_INTRIN_AVX512_VBMI_FUNC_DECL(func) 692 #endif 693 694 /* Intrinsics macros end. */ 695 696 #ifdef ZEND_WIN32 697 # define ZEND_SET_ALIGNED(alignment, decl) __declspec(align(alignment)) decl 698 #elif defined(HAVE_ATTRIBUTE_ALIGNED) 699 # define ZEND_SET_ALIGNED(alignment, decl) decl __attribute__ ((__aligned__ (alignment))) 700 #else 701 # define ZEND_SET_ALIGNED(alignment, decl) decl 702 #endif 703 704 #define ZEND_SLIDE_TO_ALIGNED(alignment, ptr) (((uintptr_t)(ptr) + ((alignment)-1)) & ~((alignment)-1)) 705 #define ZEND_SLIDE_TO_ALIGNED16(ptr) ZEND_SLIDE_TO_ALIGNED(Z_UL(16), ptr) 706 707 #ifdef ZEND_WIN32 708 # define _ZEND_EXPAND_VA(a) a 709 # define ZEND_EXPAND_VA(code) _ZEND_EXPAND_VA(code) 710 #else 711 # define ZEND_EXPAND_VA(code) code 712 #endif 713 714 /* On CPU with few registers, it's cheaper to reload value then use spill slot */ 715 #if defined(__i386__) || (defined(_WIN32) && !defined(_WIN64)) 716 # define ZEND_PREFER_RELOAD 717 #endif 718 719 #if defined(ZEND_WIN32) && defined(_DEBUG) 720 # define ZEND_IGNORE_LEAKS_BEGIN() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & ~_CRTDBG_ALLOC_MEM_DF) 721 # define ZEND_IGNORE_LEAKS_END() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF) 722 #else 723 # define ZEND_IGNORE_LEAKS_BEGIN() 724 # define ZEND_IGNORE_LEAKS_END() 725 #endif 726 727 /* MSVC yields C4090 when a (const T **) is passed to a (void *); ZEND_VOIDP works around that */ 728 #ifdef _MSC_VER 729 # define ZEND_VOIDP(ptr) ((void *) ptr) 730 #else 731 # define ZEND_VOIDP(ptr) (ptr) 732 #endif 733 734 #if __has_attribute(__indirect_return__) 735 # define ZEND_INDIRECT_RETURN __attribute__((__indirect_return__)) 736 #else 737 # define ZEND_INDIRECT_RETURN 738 #endif 739 740 #define __ZEND_DO_PRAGMA(x) _Pragma(#x) 741 #define _ZEND_DO_PRAGMA(x) __ZEND_DO_PRAGMA(x) 742 #if defined(__clang__) 743 # define ZEND_DIAGNOSTIC_IGNORED_START(warning) \ 744 _Pragma("clang diagnostic push") \ 745 _ZEND_DO_PRAGMA(clang diagnostic ignored warning) 746 # define ZEND_DIAGNOSTIC_IGNORED_END \ 747 _Pragma("clang diagnostic pop") 748 #elif defined(__GNUC__) 749 # define ZEND_DIAGNOSTIC_IGNORED_START(warning) \ 750 _Pragma("GCC diagnostic push") \ 751 _ZEND_DO_PRAGMA(GCC diagnostic ignored warning) 752 # define ZEND_DIAGNOSTIC_IGNORED_END \ 753 _Pragma("GCC diagnostic pop") 754 #else 755 # define ZEND_DIAGNOSTIC_IGNORED_START(warning) 756 # define ZEND_DIAGNOSTIC_IGNORED_END 757 #endif 758 759 /** @deprecated */ 760 #define ZEND_CGG_DIAGNOSTIC_IGNORED_START ZEND_DIAGNOSTIC_IGNORED_START 761 /** @deprecated */ 762 #define ZEND_CGG_DIAGNOSTIC_IGNORED_END ZEND_DIAGNOSTIC_IGNORED_END 763 764 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */ 765 # define ZEND_STATIC_ASSERT(c, m) _Static_assert((c), m) 766 #else 767 # define ZEND_STATIC_ASSERT(c, m) 768 #endif 769 770 #endif /* ZEND_PORTABILITY_H */ 771