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 <zeev@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 HAVE_ALLOCA_H && !defined(_ALLOCA_H)
67 # include <alloca.h>
68 #endif
69 
70 #if defined(ZEND_WIN32) && !defined(__clang__)
71 #include <intrin.h>
72 #endif
73 
74 #include "zend_range_check.h"
75 
76 /* GCC x.y.z supplies __GNUC__ = x and __GNUC_MINOR__ = y */
77 #ifdef __GNUC__
78 # define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
79 #else
80 # define ZEND_GCC_VERSION 0
81 #endif
82 
83 /* Compatibility with non-clang compilers */
84 #ifndef __has_attribute
85 # define __has_attribute(x) 0
86 #endif
87 #ifndef __has_builtin
88 # define __has_builtin(x) 0
89 #endif
90 #ifndef __has_feature
91 # define __has_feature(x) 0
92 #endif
93 
94 #if defined(ZEND_WIN32) && !defined(__clang__)
95 # define ZEND_ASSUME(c)	__assume(c)
96 #elif ((defined(__GNUC__) && ZEND_GCC_VERSION >= 4005) || __has_builtin(__builtin_unreachable)) && PHP_HAVE_BUILTIN_EXPECT
97 # define ZEND_ASSUME(c)	do { \
98 		if (__builtin_expect(!(c), 0)) __builtin_unreachable(); \
99 	} while (0)
100 #else
101 # define ZEND_ASSUME(c)
102 #endif
103 
104 #if ZEND_DEBUG
105 # define ZEND_ASSERT(c)	assert(c)
106 #else
107 # define ZEND_ASSERT(c) ZEND_ASSUME(c)
108 #endif
109 
110 /* Only use this macro if you know for sure that all of the switches values
111    are covered by its case statements */
112 #if ZEND_DEBUG
113 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSERT(0); break;
114 #else
115 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSUME(0); break;
116 #endif
117 
118 #if defined(__GNUC__) && __GNUC__ >= 4
119 # define ZEND_IGNORE_VALUE(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
120 #else
121 # define ZEND_IGNORE_VALUE(x) ((void) (x))
122 #endif
123 
124 #define zend_quiet_write(...) ZEND_IGNORE_VALUE(write(__VA_ARGS__))
125 
126 /* all HAVE_XXX test have to be after the include of zend_config above */
127 
128 #if defined(HAVE_LIBDL) && !defined(ZEND_WIN32)
129 
130 # if __has_feature(address_sanitizer)
131 #  define __SANITIZE_ADDRESS__
132 # endif
133 
134 # ifndef RTLD_LAZY
135 #  define RTLD_LAZY 1    /* Solaris 1, FreeBSD's (2.1.7.1 and older) */
136 # endif
137 
138 # ifndef RTLD_GLOBAL
139 #  define RTLD_GLOBAL 0
140 # endif
141 
142 # ifdef PHP_USE_RTLD_NOW
143 #  define PHP_RTLD_MODE  RTLD_NOW
144 # else
145 #  define PHP_RTLD_MODE  RTLD_LAZY
146 # endif
147 
148 # if defined(RTLD_GROUP) && defined(RTLD_WORLD) && defined(RTLD_PARENT)
149 #  define DL_LOAD(libname)			dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL | RTLD_GROUP | RTLD_WORLD | RTLD_PARENT)
150 # elif defined(RTLD_DEEPBIND) && !defined(__SANITIZE_ADDRESS__) && !__has_feature(memory_sanitizer)
151 #  define DL_LOAD(libname)			dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL | RTLD_DEEPBIND)
152 # else
153 #  define DL_LOAD(libname)			dlopen(libname, PHP_RTLD_MODE | RTLD_GLOBAL)
154 # endif
155 # define DL_UNLOAD					dlclose
156 # if defined(DLSYM_NEEDS_UNDERSCORE)
157 #  define DL_FETCH_SYMBOL(h,s)		dlsym((h), "_" s)
158 # else
159 #  define DL_FETCH_SYMBOL			dlsym
160 # endif
161 # define DL_ERROR					dlerror
162 # define DL_HANDLE					void *
163 # define ZEND_EXTENSIONS_SUPPORT	1
164 #elif defined(ZEND_WIN32)
165 # define DL_LOAD(libname)			LoadLibrary(libname)
166 # define DL_FETCH_SYMBOL			GetProcAddress
167 # define DL_UNLOAD					FreeLibrary
168 # define DL_HANDLE					HMODULE
169 # define ZEND_EXTENSIONS_SUPPORT	1
170 #else
171 # define DL_HANDLE					void *
172 # define ZEND_EXTENSIONS_SUPPORT	0
173 #endif
174 
175 /* AIX requires this to be the first thing in the file.  */
176 #ifndef __GNUC__
177 # ifndef HAVE_ALLOCA_H
178 #  ifdef _AIX
179 #   pragma alloca
180 #  else
181 #   ifndef alloca /* predefined by HP cc +Olibcalls */
182 char *alloca();
183 #   endif
184 #  endif
185 # endif
186 #endif
187 
188 #if ZEND_GCC_VERSION >= 2096 || __has_attribute(__malloc__)
189 # define ZEND_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
190 #else
191 # define ZEND_ATTRIBUTE_MALLOC
192 #endif
193 
194 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(alloc_size)
195 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X) __attribute__ ((alloc_size(X)))
196 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y) __attribute__ ((alloc_size(X,Y)))
197 #else
198 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X)
199 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y)
200 #endif
201 
202 #if ZEND_GCC_VERSION >= 2007 || __has_attribute(format)
203 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
204 #else
205 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first)
206 #endif
207 
208 #if (ZEND_GCC_VERSION >= 3001 && !defined(__INTEL_COMPILER)) || __has_attribute(format)
209 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
210 #else
211 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first)
212 #endif
213 
214 #if ZEND_GCC_VERSION >= 3001 || __has_attribute(deprecated)
215 # define ZEND_ATTRIBUTE_DEPRECATED  __attribute__((deprecated))
216 #elif defined(ZEND_WIN32)
217 # define ZEND_ATTRIBUTE_DEPRECATED  __declspec(deprecated)
218 #else
219 # define ZEND_ATTRIBUTE_DEPRECATED
220 #endif
221 
222 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(unused)
223 # define ZEND_ATTRIBUTE_UNUSED __attribute__((unused))
224 #else
225 # define ZEND_ATTRIBUTE_UNUSED
226 #endif
227 
228 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 4003
229 # define ZEND_COLD __attribute__((cold))
230 # define ZEND_HOT __attribute__((hot))
231 # ifdef __OPTIMIZE__
232 #  define ZEND_OPT_SIZE  __attribute__((optimize("Os")))
233 #  define ZEND_OPT_SPEED __attribute__((optimize("Ofast")))
234 # else
235 #  define ZEND_OPT_SIZE
236 #  define ZEND_OPT_SPEED
237 # endif
238 #else
239 # define ZEND_COLD
240 # define ZEND_HOT
241 # define ZEND_OPT_SIZE
242 # define ZEND_OPT_SPEED
243 #endif
244 
245 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 5000
246 # define ZEND_ATTRIBUTE_UNUSED_LABEL __attribute__((cold, unused));
247 # define ZEND_ATTRIBUTE_COLD_LABEL __attribute__((cold));
248 # define ZEND_ATTRIBUTE_HOT_LABEL __attribute__((hot));
249 #else
250 # define ZEND_ATTRIBUTE_UNUSED_LABEL
251 # define ZEND_ATTRIBUTE_COLD_LABEL
252 # define ZEND_ATTRIBUTE_HOT_LABEL
253 #endif
254 
255 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004 && defined(__i386__)
256 # define ZEND_FASTCALL __attribute__((fastcall))
257 #elif defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER == 1700
258 # define ZEND_FASTCALL __fastcall
259 #elif defined(_MSC_VER) && _MSC_VER >= 1800 && !defined(__clang__)
260 # define ZEND_FASTCALL __vectorcall
261 #else
262 # define ZEND_FASTCALL
263 #endif
264 
265 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)) || __has_attribute(noreturn)
266 # define HAVE_NORETURN
267 # define ZEND_NORETURN __attribute__((noreturn))
268 #elif defined(ZEND_WIN32)
269 # define HAVE_NORETURN
270 # define ZEND_NORETURN __declspec(noreturn)
271 #else
272 # define ZEND_NORETURN
273 #endif
274 
275 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
276 # define HAVE_NORETURN_ALIAS
277 # define HAVE_ATTRIBUTE_WEAK
278 #endif
279 
280 #if ZEND_GCC_VERSION >= 3001 || __has_builtin(__builtin_constant_p)
281 # define HAVE_BUILTIN_CONSTANT_P
282 #endif
283 
284 #ifdef HAVE_BUILTIN_CONSTANT_P
285 # define ZEND_CONST_COND(_condition, _default) \
286 	(__builtin_constant_p(_condition) ? (_condition) : (_default))
287 #else
288 # define ZEND_CONST_COND(_condition, _default) \
289 	(_default)
290 #endif
291 
292 #if ZEND_DEBUG || defined(ZEND_WIN32_NEVER_INLINE)
293 # define zend_always_inline inline
294 # define zend_never_inline
295 #else
296 # if defined(__GNUC__)
297 #  if __GNUC__ >= 3
298 #   define zend_always_inline inline __attribute__((always_inline))
299 #   define zend_never_inline __attribute__((noinline))
300 #  else
301 #   define zend_always_inline inline
302 #   define zend_never_inline
303 #  endif
304 # elif defined(_MSC_VER)
305 #  define zend_always_inline __forceinline
306 #  define zend_never_inline __declspec(noinline)
307 # else
308 #  if __has_attribute(always_inline)
309 #   define zend_always_inline inline __attribute__((always_inline))
310 #  else
311 #   define zend_always_inline inline
312 #  endif
313 #  if __has_attribute(noinline)
314 #   define zend_never_inline __attribute__((noinline))
315 #  else
316 #   define zend_never_inline
317 #  endif
318 # endif
319 #endif /* ZEND_DEBUG */
320 
321 #if PHP_HAVE_BUILTIN_EXPECT
322 # define EXPECTED(condition)   __builtin_expect(!!(condition), 1)
323 # define UNEXPECTED(condition) __builtin_expect(!!(condition), 0)
324 #else
325 # define EXPECTED(condition)   (condition)
326 # define UNEXPECTED(condition) (condition)
327 #endif
328 
329 #ifndef XtOffsetOf
330 # if defined(CRAY) || (defined(__ARMCC_VERSION) && !defined(LINUX))
331 #  ifdef __STDC__
332 #   define XtOffset(p_type, field) _Offsetof(p_type, field)
333 #  else
334 #   ifdef CRAY2
335 #    define XtOffset(p_type, field) \
336        (sizeof(int)*((unsigned int)&(((p_type)NULL)->field)))
337 #   else /* !CRAY2 */
338 #    define XtOffset(p_type, field) ((unsigned int)&(((p_type)NULL)->field))
339 #   endif /* !CRAY2 */
340 #  endif /* __STDC__ */
341 # else /* ! (CRAY || __arm) */
342 #  define XtOffset(p_type, field) \
343      ((zend_long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
344 # endif /* !CRAY */
345 
346 # ifdef offsetof
347 # define XtOffsetOf(s_type, field) offsetof(s_type, field)
348 # else
349 # define XtOffsetOf(s_type, field) XtOffset(s_type*, field)
350 # endif
351 
352 #endif
353 
354 #if (HAVE_ALLOCA || (defined (__GNUC__) && __GNUC__ >= 2)) && !(defined(ZTS) && defined(HPUX)) && !defined(DARWIN)
355 # define ZEND_ALLOCA_MAX_SIZE (32 * 1024)
356 # define ALLOCA_FLAG(name) \
357 	zend_bool name;
358 # define SET_ALLOCA_FLAG(name) \
359 	name = 1
360 # define do_alloca_ex(size, limit, use_heap) \
361 	((use_heap = (UNEXPECTED((size) > (limit)))) ? emalloc(size) : alloca(size))
362 # define do_alloca(size, use_heap) \
363 	do_alloca_ex(size, ZEND_ALLOCA_MAX_SIZE, use_heap)
364 # define free_alloca(p, use_heap) \
365 	do { if (UNEXPECTED(use_heap)) efree(p); } while (0)
366 #else
367 # define ALLOCA_FLAG(name)
368 # define SET_ALLOCA_FLAG(name)
369 # define do_alloca(p, use_heap)		emalloc(p)
370 # define free_alloca(p, use_heap)	efree(p)
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 /* We always define a function, even if there's a macro or expression we could
442  * alias, so that using it in contexts where we can't make function calls
443  * won't fail to compile on some machines and not others.
444  */
_zend_get_inf(void)445 static zend_always_inline double _zend_get_inf(void) /* {{{ */
446 {
447 #ifdef INFINITY
448 	return INFINITY;
449 #elif HAVE_HUGE_VAL_INF
450 	return HUGE_VAL;
451 #elif defined(__i386__) || defined(_X86_) || defined(ALPHA) || defined(_ALPHA) || defined(__alpha)
452 # define _zend_DOUBLE_INFINITY_HIGH       0x7ff00000
453 	double val = 0.0;
454 	((uint32_t*)&val)[1] = _zend_DOUBLE_INFINITY_HIGH;
455 	((uint32_t*)&val)[0] = 0;
456 	return val;
457 #elif HAVE_ATOF_ACCEPTS_INF
458 	return atof("INF");
459 #else
460 	return 1.0/0.0;
461 #endif
462 } /* }}} */
463 #define ZEND_INFINITY (_zend_get_inf())
464 
_zend_get_nan(void)465 static zend_always_inline double _zend_get_nan(void) /* {{{ */
466 {
467 #ifdef NAN
468 	return NAN;
469 #elif HAVE_HUGE_VAL_NAN
470 	return HUGE_VAL + -HUGE_VAL;
471 #elif defined(__i386__) || defined(_X86_) || defined(ALPHA) || defined(_ALPHA) || defined(__alpha)
472 # define _zend_DOUBLE_QUIET_NAN_HIGH      0xfff80000
473 	double val = 0.0;
474 	((uint32_t*)&val)[1] = _zend_DOUBLE_QUIET_NAN_HIGH;
475 	((uint32_t*)&val)[0] = 0;
476 	return val;
477 #elif HAVE_ATOF_ACCEPTS_NAN
478 	return atof("NAN");
479 #else
480 	return 0.0/0.0;
481 #endif
482 } /* }}} */
483 #define ZEND_NAN (_zend_get_nan())
484 
485 #define ZEND_STRL(str)		(str), (sizeof(str)-1)
486 #define ZEND_STRS(str)		(str), (sizeof(str))
487 #define ZEND_NORMALIZE_BOOL(n)			\
488 	((n) ? (((n)<0) ? -1 : 1) : 0)
489 #define ZEND_TRUTH(x)		((x) ? 1 : 0)
490 #define ZEND_LOG_XOR(a, b)		(ZEND_TRUTH(a) ^ ZEND_TRUTH(b))
491 
492 #define ZEND_MAX_RESERVED_RESOURCES	6
493 
494 /* excpt.h on Digital Unix 4.0 defines function_table */
495 #undef function_table
496 
497 #ifdef ZEND_WIN32
498 #define ZEND_SECURE_ZERO(var, size) RtlSecureZeroMemory((var), (size))
499 #else
500 #define ZEND_SECURE_ZERO(var, size) explicit_bzero((var), (size))
501 #endif
502 
503 /* This check should only be used on network socket, not file descriptors */
504 #ifdef ZEND_WIN32
505 #define ZEND_VALID_SOCKET(sock) (INVALID_SOCKET != (sock))
506 #else
507 #define ZEND_VALID_SOCKET(sock) ((sock) >= 0)
508 #endif
509 
510 /* va_copy() is __va_copy() in old gcc versions.
511  * According to the autoconf manual, using
512  * memcpy(&dst, &src, sizeof(va_list))
513  * gives maximum portability. */
514 #ifndef va_copy
515 # ifdef __va_copy
516 #  define va_copy(dest, src) __va_copy((dest), (src))
517 # else
518 #  define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
519 # endif
520 #endif
521 
522 /* Intrinsics macros start. */
523 
524 /* Memory sanitizer is incompatible with ifunc resolvers. Even if the resolver
525  * is marked as no_sanitize("memory") it will still be instrumented and crash. */
526 #if __has_feature(memory_sanitizer) || __has_feature(thread_sanitizer)
527 # undef HAVE_FUNC_ATTRIBUTE_IFUNC
528 #endif
529 
530 #if defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(HAVE_FUNC_ATTRIBUTE_TARGET)
531 # define ZEND_INTRIN_HAVE_IFUNC_TARGET 1
532 #endif
533 
534 #if (defined(__i386__) || defined(__x86_64__))
535 # if PHP_HAVE_SSSE3_INSTRUCTIONS && defined(HAVE_TMMINTRIN_H)
536 # define PHP_HAVE_SSSE3
537 # endif
538 
539 # if PHP_HAVE_SSE4_2_INSTRUCTIONS && defined(HAVE_NMMINTRIN_H)
540 # define PHP_HAVE_SSE4_2
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 PHP_HAVE_AVX2_INSTRUCTIONS && 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)
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 ZEND_INTRIN_SSSE3_RESOLVER && ZEND_INTRIN_HAVE_IFUNC_TARGET
563 # define ZEND_INTRIN_SSSE3_FUNC_PROTO 1
564 #elif ZEND_INTRIN_SSSE3_RESOLVER
565 # define ZEND_INTRIN_SSSE3_FUNC_PTR 1
566 #endif
567 
568 #if ZEND_INTRIN_SSSE3_RESOLVER
569 # if defined(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)
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 ZEND_INTRIN_SSE4_2_RESOLVER && ZEND_INTRIN_HAVE_IFUNC_TARGET
588 # define ZEND_INTRIN_SSE4_2_FUNC_PROTO 1
589 #elif ZEND_INTRIN_SSE4_2_RESOLVER
590 # define ZEND_INTRIN_SSE4_2_FUNC_PTR 1
591 #endif
592 
593 #if ZEND_INTRIN_SSE4_2_RESOLVER
594 # if defined(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 __AVX2__
604 # define ZEND_INTRIN_AVX2_NATIVE 1
605 #elif (defined(HAVE_FUNC_ATTRIBUTE_TARGET) && defined(PHP_HAVE_AVX2)) || defined(ZEND_WIN32)
606 # define ZEND_INTRIN_AVX2_RESOLVER 1
607 #endif
608 
609 /* Do not use for conditional declaration of API functions! */
610 #if ZEND_INTRIN_AVX2_RESOLVER && ZEND_INTRIN_HAVE_IFUNC_TARGET
611 # define ZEND_INTRIN_AVX2_FUNC_PROTO 1
612 #elif ZEND_INTRIN_AVX2_RESOLVER
613 # define ZEND_INTRIN_AVX2_FUNC_PTR 1
614 #endif
615 
616 #if ZEND_INTRIN_AVX2_RESOLVER
617 # if defined(HAVE_FUNC_ATTRIBUTE_TARGET)
618 #  define ZEND_INTRIN_AVX2_FUNC_DECL(func) ZEND_API func __attribute__((target("avx2")))
619 # else
620 #  define ZEND_INTRIN_AVX2_FUNC_DECL(func) func
621 # endif
622 #else
623 # define ZEND_INTRIN_AVX2_FUNC_DECL(func)
624 #endif
625 
626 /* Intrinsics macros end. */
627 
628 #ifdef ZEND_WIN32
629 # define ZEND_SET_ALIGNED(alignment, decl) __declspec(align(alignment)) decl
630 #elif HAVE_ATTRIBUTE_ALIGNED
631 # define ZEND_SET_ALIGNED(alignment, decl) decl __attribute__ ((__aligned__ (alignment)))
632 #else
633 # define ZEND_SET_ALIGNED(alignment, decl) decl
634 #endif
635 
636 #define ZEND_SLIDE_TO_ALIGNED(alignment, ptr) (((zend_uintptr_t)(ptr) + ((alignment)-1)) & ~((alignment)-1))
637 #define ZEND_SLIDE_TO_ALIGNED16(ptr) ZEND_SLIDE_TO_ALIGNED(Z_UL(16), ptr)
638 
639 #ifdef ZEND_WIN32
640 # define _ZEND_EXPAND_VA(a) a
641 # define ZEND_EXPAND_VA(code) _ZEND_EXPAND_VA(code)
642 #else
643 # define ZEND_EXPAND_VA(code) code
644 #endif
645 
646 /* On CPU with few registers, it's cheaper to reload value then use spill slot */
647 #if defined(__i386__) || (defined(_WIN32) && !defined(_WIN64))
648 # define ZEND_PREFER_RELOAD
649 #endif
650 
651 #if defined(ZEND_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
652 # define ZEND_IGNORE_LEAKS_BEGIN() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & ~_CRTDBG_ALLOC_MEM_DF)
653 # define ZEND_IGNORE_LEAKS_END() _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF)
654 #else
655 # define ZEND_IGNORE_LEAKS_BEGIN()
656 # define ZEND_IGNORE_LEAKS_END()
657 #endif
658 
659 #endif /* ZEND_PORTABILITY_H */
660