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