xref: /PHP-7.2/Zend/zend_portability.h (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Dmitry Stogov <zeev@zend.com>                               |
18    +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 
23 #ifndef ZEND_PORTABILITY_H
24 #define ZEND_PORTABILITY_H
25 
26 #ifdef __cplusplus
27 #define BEGIN_EXTERN_C() extern "C" {
28 #define END_EXTERN_C() }
29 #else
30 #define BEGIN_EXTERN_C()
31 #define END_EXTERN_C()
32 #endif
33 
34 /*
35  * general definitions
36  */
37 
38 #ifdef ZEND_WIN32
39 # include "zend_config.w32.h"
40 # define ZEND_PATHS_SEPARATOR		';'
41 #elif defined(__riscos__)
42 # include <zend_config.h>
43 # define ZEND_PATHS_SEPARATOR		';'
44 #else
45 # include <zend_config.h>
46 # define ZEND_PATHS_SEPARATOR		':'
47 #endif
48 
49 #include "../TSRM/TSRM.h"
50 
51 #include <stdio.h>
52 #include <assert.h>
53 #include <math.h>
54 
55 #ifdef HAVE_UNIX_H
56 # include <unix.h>
57 #endif
58 
59 #ifdef HAVE_STDARG_H
60 # include <stdarg.h>
61 #endif
62 
63 #ifdef HAVE_DLFCN_H
64 # include <dlfcn.h>
65 #endif
66 
67 #ifdef HAVE_LIMITS_H
68 # include <limits.h>
69 #endif
70 
71 #if HAVE_ALLOCA_H && !defined(_ALLOCA_H)
72 # include <alloca.h>
73 #endif
74 
75 #if defined(ZEND_WIN32)
76 #include <intrin.h>
77 #endif
78 
79 #include "zend_range_check.h"
80 
81 /* GCC x.y.z supplies __GNUC__ = x and __GNUC_MINOR__ = y */
82 #ifdef __GNUC__
83 # define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
84 #else
85 # define ZEND_GCC_VERSION 0
86 #endif
87 
88 /* Compatibility with non-clang compilers */
89 #ifndef __has_attribute
90 # define __has_attribute(x) 0
91 #endif
92 #ifndef __has_builtin
93 # define __has_builtin(x) 0
94 #endif
95 
96 #if defined(ZEND_WIN32) && !defined(__clang__)
97 # define ZEND_ASSUME(c)	__assume(c)
98 #elif ((defined(__GNUC__) && ZEND_GCC_VERSION >= 4005) || __has_builtin(__builtin_unreachable)) && 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 /* Only use this macro if you know for sure that all of the switches values
113    are covered by its case statements */
114 #if ZEND_DEBUG
115 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSERT(0); break;
116 #else
117 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSUME(0); break;
118 #endif
119 
120 #if defined(__GNUC__) && __GNUC__ >= 4
121 # define ZEND_IGNORE_VALUE(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
122 #else
123 # define ZEND_IGNORE_VALUE(x) ((void) (x))
124 #endif
125 
126 #define zend_quiet_write(...) ZEND_IGNORE_VALUE(write(__VA_ARGS__))
127 
128 /* all HAVE_XXX test have to be after the include of zend_config above */
129 
130 #if defined(HAVE_LIBDL) && !defined(ZEND_WIN32)
131 
132 # if defined(__has_feature)
133 #  if __has_feature(address_sanitizer)
134 #   define __SANITIZE_ADDRESS__
135 #  endif
136 # endif
137 
138 # ifndef RTLD_LAZY
139 #  define RTLD_LAZY 1    /* Solaris 1, FreeBSD's (2.1.7.1 and older) */
140 # endif
141 
142 # ifndef RTLD_GLOBAL
143 #  define RTLD_GLOBAL 0
144 # endif
145 
146 # if defined(RTLD_GROUP) && defined(RTLD_WORLD) && defined(RTLD_PARENT)
147 #  define DL_LOAD(libname)			dlopen(libname, RTLD_LAZY | RTLD_GLOBAL | RTLD_GROUP | RTLD_WORLD | RTLD_PARENT)
148 # elif defined(RTLD_DEEPBIND) && !defined(__SANITIZE_ADDRESS__)
149 #  define DL_LOAD(libname)			dlopen(libname, RTLD_LAZY | RTLD_GLOBAL | RTLD_DEEPBIND)
150 # else
151 #  define DL_LOAD(libname)			dlopen(libname, RTLD_LAZY | RTLD_GLOBAL)
152 # endif
153 # define DL_UNLOAD					dlclose
154 # if defined(DLSYM_NEEDS_UNDERSCORE)
155 #  define DL_FETCH_SYMBOL(h,s)		dlsym((h), "_" s)
156 # else
157 #  define DL_FETCH_SYMBOL			dlsym
158 # endif
159 # define DL_ERROR					dlerror
160 # define DL_HANDLE					void *
161 # define ZEND_EXTENSIONS_SUPPORT	1
162 #elif defined(ZEND_WIN32)
163 # define DL_LOAD(libname)			LoadLibrary(libname)
164 # define DL_FETCH_SYMBOL			GetProcAddress
165 # define DL_UNLOAD					FreeLibrary
166 # define DL_HANDLE					HMODULE
167 # define ZEND_EXTENSIONS_SUPPORT	1
168 #else
169 # define DL_HANDLE					void *
170 # define ZEND_EXTENSIONS_SUPPORT	0
171 #endif
172 
173 /* AIX requires this to be the first thing in the file.  */
174 #ifndef __GNUC__
175 # ifndef HAVE_ALLOCA_H
176 #  ifdef _AIX
177 #   pragma alloca
178 #  else
179 #   ifndef alloca /* predefined by HP cc +Olibcalls */
180 char *alloca();
181 #   endif
182 #  endif
183 # endif
184 #endif
185 
186 #if ZEND_GCC_VERSION >= 2096 || __has_attribute(__malloc__)
187 # define ZEND_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
188 #else
189 # define ZEND_ATTRIBUTE_MALLOC
190 #endif
191 
192 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(alloc_size)
193 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X) __attribute__ ((alloc_size(X)))
194 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y) __attribute__ ((alloc_size(X,Y)))
195 #else
196 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X)
197 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y)
198 #endif
199 
200 /* Format string checks are disabled by default, because we use custom format modifiers (like %p),
201  * which cause a large amount of false positives. You can enable format checks by adding
202  * -DZEND_CHECK_FORMAT_STRINGS to CFLAGS. */
203 
204 #if defined(ZEND_CHECK_FORMAT_STRINGS) && (ZEND_GCC_VERSION >= 2007 || __has_attribute(format))
205 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
206 #else
207 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first)
208 #endif
209 
210 #if defined(ZEND_CHECK_FORMAT_STRINGS) && ((ZEND_GCC_VERSION >= 3001 && !defined(__INTEL_COMPILER)) || __has_attribute(format))
211 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
212 #else
213 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first)
214 #endif
215 
216 #if ZEND_GCC_VERSION >= 3001 || __has_attribute(deprecated)
217 # define ZEND_ATTRIBUTE_DEPRECATED  __attribute__((deprecated))
218 #elif defined(ZEND_WIN32)
219 # define ZEND_ATTRIBUTE_DEPRECATED  __declspec(deprecated)
220 #else
221 # define ZEND_ATTRIBUTE_DEPRECATED
222 #endif
223 
224 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 4003
225 # define ZEND_ATTRIBUTE_UNUSED __attribute__((unused))
226 # define ZEND_ATTRIBUTE_UNUSED_LABEL __attribute__((cold, unused));
227 # define ZEND_COLD __attribute__((cold))
228 # define ZEND_HOT __attribute__((hot))
229 #else
230 # define ZEND_ATTRIBUTE_UNUSED
231 # define ZEND_ATTRIBUTE_UNUSED_LABEL
232 # define ZEND_COLD
233 # define ZEND_HOT
234 #endif
235 
236 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004 && defined(__i386__)
237 # define ZEND_FASTCALL __attribute__((fastcall))
238 #elif defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER == 1700
239 # define ZEND_FASTCALL __fastcall
240 #elif defined(_MSC_VER) && _MSC_VER >= 1800 && !defined(__clang__)
241 # define ZEND_FASTCALL __vectorcall
242 #else
243 # define ZEND_FASTCALL
244 #endif
245 
246 #ifndef restrict
247 # if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004
248 # else
249 #  define __restrict__
250 # endif
251 # define restrict __restrict__
252 #endif
253 
254 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)) || __has_attribute(noreturn)
255 # define HAVE_NORETURN
256 # define ZEND_NORETURN __attribute__((noreturn))
257 #elif defined(ZEND_WIN32)
258 # define HAVE_NORETURN
259 # define ZEND_NORETURN __declspec(noreturn)
260 #else
261 # define ZEND_NORETURN
262 #endif
263 
264 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
265 # define HAVE_NORETURN_ALIAS
266 # define HAVE_ATTRIBUTE_WEAK
267 #endif
268 
269 #if ZEND_GCC_VERSION >= 3001 || __has_builtin(__builtin_constant_p)
270 # define HAVE_BUILTIN_CONSTANT_P
271 #endif
272 
273 #ifdef HAVE_BUILTIN_CONSTANT_P
274 # define ZEND_CONST_COND(_condition, _default) \
275 	(__builtin_constant_p(_condition) ? (_condition) : (_default))
276 #else
277 # define ZEND_CONST_COND(_condition, _default) \
278 	(_default)
279 #endif
280 
281 #if ZEND_DEBUG
282 # define zend_always_inline inline
283 # define zend_never_inline
284 #else
285 # if defined(__GNUC__)
286 #  if __GNUC__ >= 3
287 #   define zend_always_inline inline __attribute__((always_inline))
288 #   define zend_never_inline __attribute__((noinline))
289 #  else
290 #   define zend_always_inline inline
291 #   define zend_never_inline
292 #  endif
293 # elif defined(_MSC_VER)
294 #  define zend_always_inline __forceinline
295 #  define zend_never_inline __declspec(noinline)
296 # else
297 #  if __has_attribute(always_inline)
298 #   define zend_always_inline inline __attribute__((always_inline))
299 #  else
300 #   define zend_always_inline inline
301 #  endif
302 #  if __has_attribute(noinline)
303 #   define zend_never_inline __attribute__((noinline))
304 #  else
305 #   define zend_never_inline
306 #  endif
307 # endif
308 #endif /* ZEND_DEBUG */
309 
310 #if PHP_HAVE_BUILTIN_EXPECT
311 # define EXPECTED(condition)   __builtin_expect(!!(condition), 1)
312 # define UNEXPECTED(condition) __builtin_expect(!!(condition), 0)
313 #else
314 # define EXPECTED(condition)   (condition)
315 # define UNEXPECTED(condition) (condition)
316 #endif
317 
318 #ifndef XtOffsetOf
319 # if defined(CRAY) || (defined(__ARMCC_VERSION) && !defined(LINUX))
320 # ifdef __STDC__
321 # define XtOffset(p_type, field) _Offsetof(p_type, field)
322 # else
323 # ifdef CRAY2
324 # define XtOffset(p_type, field) \
325     (sizeof(int)*((unsigned int)&(((p_type)NULL)->field)))
326 
327 # else /* !CRAY2 */
328 
329 # define XtOffset(p_type, field) ((unsigned int)&(((p_type)NULL)->field))
330 
331 # endif /* !CRAY2 */
332 # endif /* __STDC__ */
333 # else /* ! (CRAY || __arm) */
334 
335 # define XtOffset(p_type, field) \
336     ((zend_long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
337 
338 # endif /* !CRAY */
339 
340 # ifdef offsetof
341 # define XtOffsetOf(s_type, field) offsetof(s_type, field)
342 # else
343 # define XtOffsetOf(s_type, field) XtOffset(s_type*, field)
344 # endif
345 
346 #endif
347 
348 #if (HAVE_ALLOCA || (defined (__GNUC__) && __GNUC__ >= 2)) && !(defined(ZTS)) && !(defined(ZTS) && defined(HPUX)) && !defined(DARWIN)
349 # define ZEND_ALLOCA_MAX_SIZE (32 * 1024)
350 # define ALLOCA_FLAG(name) \
351 	zend_bool name;
352 # define SET_ALLOCA_FLAG(name) \
353 	name = 1
354 # define do_alloca_ex(size, limit, use_heap) \
355 	((use_heap = (UNEXPECTED((size) > (limit)))) ? emalloc(size) : alloca(size))
356 # define do_alloca(size, use_heap) \
357 	do_alloca_ex(size, ZEND_ALLOCA_MAX_SIZE, use_heap)
358 # define free_alloca(p, use_heap) \
359 	do { if (UNEXPECTED(use_heap)) efree(p); } while (0)
360 #else
361 # define ALLOCA_FLAG(name)
362 # define SET_ALLOCA_FLAG(name)
363 # define do_alloca(p, use_heap)		emalloc(p)
364 # define free_alloca(p, use_heap)	efree(p)
365 #endif
366 
367 #ifdef HAVE_SIGSETJMP
368 # define SETJMP(a) sigsetjmp(a, 0)
369 # define LONGJMP(a,b) siglongjmp(a, b)
370 # define JMP_BUF sigjmp_buf
371 #else
372 # define SETJMP(a) setjmp(a)
373 # define LONGJMP(a,b) longjmp(a, b)
374 # define JMP_BUF jmp_buf
375 #endif
376 
377 #if ZEND_DEBUG
378 # define ZEND_FILE_LINE_D				const char *__zend_filename, const uint32_t __zend_lineno
379 # define ZEND_FILE_LINE_DC				, ZEND_FILE_LINE_D
380 # define ZEND_FILE_LINE_ORIG_D			const char *__zend_orig_filename, const uint32_t __zend_orig_lineno
381 # define ZEND_FILE_LINE_ORIG_DC			, ZEND_FILE_LINE_ORIG_D
382 # define ZEND_FILE_LINE_RELAY_C			__zend_filename, __zend_lineno
383 # define ZEND_FILE_LINE_RELAY_CC		, ZEND_FILE_LINE_RELAY_C
384 # define ZEND_FILE_LINE_C				__FILE__, __LINE__
385 # define ZEND_FILE_LINE_CC				, ZEND_FILE_LINE_C
386 # define ZEND_FILE_LINE_EMPTY_C			NULL, 0
387 # define ZEND_FILE_LINE_EMPTY_CC		, ZEND_FILE_LINE_EMPTY_C
388 # define ZEND_FILE_LINE_ORIG_RELAY_C	__zend_orig_filename, __zend_orig_lineno
389 # define ZEND_FILE_LINE_ORIG_RELAY_CC	, ZEND_FILE_LINE_ORIG_RELAY_C
390 #else
391 # define ZEND_FILE_LINE_D
392 # define ZEND_FILE_LINE_DC
393 # define ZEND_FILE_LINE_ORIG_D
394 # define ZEND_FILE_LINE_ORIG_DC
395 # define ZEND_FILE_LINE_RELAY_C
396 # define ZEND_FILE_LINE_RELAY_CC
397 # define ZEND_FILE_LINE_C
398 # define ZEND_FILE_LINE_CC
399 # define ZEND_FILE_LINE_EMPTY_C
400 # define ZEND_FILE_LINE_EMPTY_CC
401 # define ZEND_FILE_LINE_ORIG_RELAY_C
402 # define ZEND_FILE_LINE_ORIG_RELAY_CC
403 #endif	/* ZEND_DEBUG */
404 
405 #if ZEND_DEBUG
406 # define Z_DBG(expr)		(expr)
407 #else
408 # define Z_DBG(expr)
409 #endif
410 
411 #ifdef ZTS
412 # define ZTS_V 1
413 #else
414 # define ZTS_V 0
415 #endif
416 
417 #ifndef LONG_MAX
418 # define LONG_MAX 2147483647L
419 #endif
420 
421 #ifndef LONG_MIN
422 # define LONG_MIN (- LONG_MAX - 1)
423 #endif
424 
425 #define MAX_LENGTH_OF_DOUBLE 32
426 
427 #undef MIN
428 #undef MAX
429 #define MAX(a, b)  (((a)>(b))?(a):(b))
430 #define MIN(a, b)  (((a)<(b))?(a):(b))
431 
432 /* We always define a function, even if there's a macro or expression we could
433  * alias, so that using it in contexts where we can't make function calls
434  * won't fail to compile on some machines and not others.
435  */
_zend_get_inf(void)436 static zend_always_inline double _zend_get_inf(void) /* {{{ */
437 {
438 #ifdef INFINITY
439 	return INFINITY;
440 #elif HAVE_HUGE_VAL_INF
441 	return HUGE_VAL;
442 #elif defined(__i386__) || defined(_X86_) || defined(ALPHA) || defined(_ALPHA) || defined(__alpha)
443 # define _zend_DOUBLE_INFINITY_HIGH       0x7ff00000
444 	double val = 0.0;
445 	((uint32_t*)&val)[1] = _zend_DOUBLE_INFINITY_HIGH;
446 	((uint32_t*)&val)[0] = 0;
447 	return val;
448 #elif HAVE_ATOF_ACCEPTS_INF
449 	return atof("INF");
450 #else
451 	return 1.0/0.0;
452 #endif
453 } /* }}} */
454 #define ZEND_INFINITY (_zend_get_inf())
455 
_zend_get_nan(void)456 static zend_always_inline double _zend_get_nan(void) /* {{{ */
457 {
458 #ifdef NAN
459 	return NAN;
460 #elif HAVE_HUGE_VAL_NAN
461 	return HUGE_VAL + -HUGE_VAL;
462 #elif defined(__i386__) || defined(_X86_) || defined(ALPHA) || defined(_ALPHA) || defined(__alpha)
463 # define _zend_DOUBLE_QUIET_NAN_HIGH      0xfff80000
464 	double val = 0.0;
465 	((uint32_t*)&val)[1] = _zend_DOUBLE_QUIET_NAN_HIGH;
466 	((uint32_t*)&val)[0] = 0;
467 	return val;
468 #elif HAVE_ATOF_ACCEPTS_NAN
469 	return atof("NAN");
470 #else
471 	return 0.0/0.0;
472 #endif
473 } /* }}} */
474 #define ZEND_NAN (_zend_get_nan())
475 
476 #define ZEND_STRL(str)		(str), (sizeof(str)-1)
477 #define ZEND_STRS(str)		(str), (sizeof(str))
478 #define ZEND_NORMALIZE_BOOL(n)			\
479 	((n) ? (((n)<0) ? -1 : 1) : 0)
480 #define ZEND_TRUTH(x)		((x) ? 1 : 0)
481 #define ZEND_LOG_XOR(a, b)		(ZEND_TRUTH(a) ^ ZEND_TRUTH(b))
482 
483 #define ZEND_MAX_RESERVED_RESOURCES	6
484 
485 /* excpt.h on Digital Unix 4.0 defines function_table */
486 #undef function_table
487 
488 #ifdef ZEND_WIN32
489 #define ZEND_SECURE_ZERO(var, size) RtlSecureZeroMemory((var), (size))
490 #else
491 #define ZEND_SECURE_ZERO(var, size) explicit_bzero((var), (size))
492 #endif
493 
494 /* This check should only be used on network socket, not file descriptors */
495 #ifdef ZEND_WIN32
496 #define ZEND_VALID_SOCKET(sock) (INVALID_SOCKET != (sock))
497 #else
498 #define ZEND_VALID_SOCKET(sock) ((sock) >= 0)
499 #endif
500 
501 /* va_copy() is __va_copy() in old gcc versions.
502  * According to the autoconf manual, using
503  * memcpy(&dst, &src, sizeof(va_list))
504  * gives maximum portability. */
505 #ifndef va_copy
506 # ifdef __va_copy
507 #  define va_copy(dest, src) __va_copy((dest), (src))
508 # else
509 #  define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
510 # endif
511 #endif
512 
513 #endif /* ZEND_PORTABILITY_H */
514 
515 /*
516  * Local variables:
517  * tab-width: 4
518  * c-basic-offset: 4
519  * indent-tabs-mode: t
520  * End:
521  * vim600: sw=4 ts=4 fdm=marker
522  * vim<600: sw=4 ts=4
523  */
524