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