1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
36
37
38 #ifdef ZEND_WIN32
39 # include "zend_config.w32.h"
40 # define ZEND_PATHS_SEPARATOR ';'
41 #elif defined(NETWARE)
42 # include <zend_config.h>
43 # define ZEND_PATHS_SEPARATOR ';'
44 #elif defined(__riscos__)
45 # include <zend_config.h>
46 # define ZEND_PATHS_SEPARATOR ';'
47 #else
48 # include <zend_config.h>
49 # define ZEND_PATHS_SEPARATOR ':'
50 #endif
51
52 #include "../TSRM/TSRM.h"
53
54 #include <stdio.h>
55 #include <assert.h>
56
57 #ifdef HAVE_UNIX_H
58 # include <unix.h>
59 #endif
60
61 #ifdef HAVE_STDARG_H
62 # include <stdarg.h>
63 #endif
64
65 #ifdef HAVE_DLFCN_H
66 # include <dlfcn.h>
67 #endif
68
69 #ifdef HAVE_LIMITS_H
70 # include <limits.h>
71 #endif
72
73 #if HAVE_ALLOCA_H && !defined(_ALLOCA_H)
74 # include <alloca.h>
75 #endif
76
77 #if defined(ZEND_WIN32)
78 #include <intrin.h>
79 #endif
80
81 #include "zend_range_check.h"
82
83
84 #ifdef __GNUC__
85 # define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
86 #else
87 # define ZEND_GCC_VERSION 0
88 #endif
89
90
91 #ifndef __has_attribute
92 # define __has_attribute(x) 0
93 #endif
94 #ifndef __has_builtin
95 # define __has_builtin(x) 0
96 #endif
97
98 #if defined(ZEND_WIN32) && !defined(__clang__)
99 # define ZEND_ASSUME(c) __assume(c)
100 #elif ((defined(__GNUC__) && ZEND_GCC_VERSION >= 4005) || __has_builtin(__builtin_unreachable)) && PHP_HAVE_BUILTIN_EXPECT
101 # define ZEND_ASSUME(c) do { \
102 if (__builtin_expect(!(c), 0)) __builtin_unreachable(); \
103 } while (0)
104 #else
105 # define ZEND_ASSUME(c)
106 #endif
107
108 #if ZEND_DEBUG
109 # define ZEND_ASSERT(c) assert(c)
110 #else
111 # define ZEND_ASSERT(c) ZEND_ASSUME(c)
112 #endif
113
114
115
116 #if ZEND_DEBUG
117 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSERT(0); break;
118 #else
119 # define EMPTY_SWITCH_DEFAULT_CASE() default: ZEND_ASSUME(0); break;
120 #endif
121
122 #if defined(__GNUC__) && __GNUC__ >= 4
123 # define ZEND_IGNORE_VALUE(x) (({ __typeof__ (x) __x = (x); (void) __x; }))
124 #else
125 # define ZEND_IGNORE_VALUE(x) ((void) (x))
126 #endif
127
128
129
130 #if defined(HAVE_LIBDL) && !defined(ZEND_WIN32)
131
132 # ifndef RTLD_LAZY
133 # define RTLD_LAZY 1
134 # endif
135
136 # ifndef RTLD_GLOBAL
137 # define RTLD_GLOBAL 0
138 # endif
139
140 # if defined(RTLD_GROUP) && defined(RTLD_WORLD) && defined(RTLD_PARENT)
141 # define DL_LOAD(libname) dlopen(libname, RTLD_LAZY | RTLD_GLOBAL | RTLD_GROUP | RTLD_WORLD | RTLD_PARENT)
142 # elif defined(RTLD_DEEPBIND)
143 # define DL_LOAD(libname) dlopen(libname, RTLD_LAZY | RTLD_GLOBAL | RTLD_DEEPBIND)
144 # else
145 # define DL_LOAD(libname) dlopen(libname, RTLD_LAZY | RTLD_GLOBAL)
146 # endif
147 # define DL_UNLOAD dlclose
148 # if defined(DLSYM_NEEDS_UNDERSCORE)
149 # define DL_FETCH_SYMBOL(h,s) dlsym((h), "_" s)
150 # else
151 # define DL_FETCH_SYMBOL dlsym
152 # endif
153 # define DL_ERROR dlerror
154 # define DL_HANDLE void *
155 # define ZEND_EXTENSIONS_SUPPORT 1
156 #elif defined(ZEND_WIN32)
157 # define DL_LOAD(libname) LoadLibrary(libname)
158 # define DL_FETCH_SYMBOL GetProcAddress
159 # define DL_UNLOAD FreeLibrary
160 # define DL_HANDLE HMODULE
161 # define ZEND_EXTENSIONS_SUPPORT 1
162 #else
163 # define DL_HANDLE void *
164 # define ZEND_EXTENSIONS_SUPPORT 0
165 #endif
166
167
168 #ifndef __GNUC__
169 # ifndef HAVE_ALLOCA_H
170 # ifdef _AIX
171 # pragma alloca
172 # else
173 # ifndef alloca
174 char *alloca();
175 # endif
176 # endif
177 # endif
178 #endif
179
180 #if ZEND_GCC_VERSION >= 2096 || __has_attribute(__malloc__)
181 # define ZEND_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
182 #else
183 # define ZEND_ATTRIBUTE_MALLOC
184 #endif
185
186 #if ZEND_GCC_VERSION >= 4003 || __has_attribute(alloc_size)
187 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X) __attribute__ ((alloc_size(X)))
188 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y) __attribute__ ((alloc_size(X,Y)))
189 #else
190 # define ZEND_ATTRIBUTE_ALLOC_SIZE(X)
191 # define ZEND_ATTRIBUTE_ALLOC_SIZE2(X,Y)
192 #endif
193
194
195
196
197
198 #if defined(ZEND_CHECK_FORMAT_STRINGS) && (ZEND_GCC_VERSION >= 2007 || __has_attribute(format))
199 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
200 #else
201 # define ZEND_ATTRIBUTE_FORMAT(type, idx, first)
202 #endif
203
204 #if defined(ZEND_CHECK_FORMAT_STRINGS) && ((ZEND_GCC_VERSION >= 3001 && !defined(__INTEL_COMPILER)) || __has_attribute(format))
205 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first) __attribute__ ((format(type, idx, first)))
206 #else
207 # define ZEND_ATTRIBUTE_PTR_FORMAT(type, idx, first)
208 #endif
209
210 #if ZEND_GCC_VERSION >= 3001 || __has_attribute(deprecated)
211 # define ZEND_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
212 #elif defined(ZEND_WIN32)
213 # define ZEND_ATTRIBUTE_DEPRECATED __declspec(deprecated)
214 #else
215 # define ZEND_ATTRIBUTE_DEPRECATED
216 #endif
217
218 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 4003
219 # define ZEND_ATTRIBUTE_UNUSED __attribute__((unused))
220 # define ZEND_ATTRIBUTE_UNUSED_LABEL __attribute__((cold, unused));
221 # define ZEND_COLD __attribute__((cold))
222 # define ZEND_HOT __attribute__((hot))
223 #else
224 # define ZEND_ATTRIBUTE_UNUSED
225 # define ZEND_ATTRIBUTE_UNUSED_LABEL
226 # define ZEND_COLD
227 # define ZEND_HOT
228 #endif
229
230 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004 && defined(__i386__)
231 # define ZEND_FASTCALL __attribute__((fastcall))
232 #elif defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER == 1700
233 # define ZEND_FASTCALL __fastcall
234 #elif defined(_MSC_VER) && _MSC_VER >= 1800 && !defined(__clang__)
235 # define ZEND_FASTCALL __vectorcall
236 #else
237 # define ZEND_FASTCALL
238 #endif
239
240 #if defined(__GNUC__) && ZEND_GCC_VERSION >= 3004
241 #else
242 # define __restrict__
243 #endif
244 #define restrict __restrict__
245
246 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)) || __has_attribute(noreturn)
247 # define HAVE_NORETURN
248 # define ZEND_NORETURN __attribute__((noreturn))
249 #elif defined(ZEND_WIN32)
250 # define HAVE_NORETURN
251 # define ZEND_NORETURN __declspec(noreturn)
252 #else
253 # define ZEND_NORETURN
254 #endif
255
256 #if (defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__))
257 # define HAVE_NORETURN_ALIAS
258 # define HAVE_ATTRIBUTE_WEAK
259 #endif
260
261 #if ZEND_GCC_VERSION >= 3001 || __has_builtin(__builtin_constant_p)
262 # define HAVE_BUILTIN_CONSTANT_P
263 #endif
264
265 #ifdef HAVE_BUILTIN_CONSTANT_P
266 # define ZEND_CONST_COND(_condition, _default) \
267 (__builtin_constant_p(_condition) ? (_condition) : (_default))
268 #else
269 # define ZEND_CONST_COND(_condition, _default) \
270 (_default)
271 #endif
272
273 #if ZEND_DEBUG
274 # define zend_always_inline inline
275 # define zend_never_inline
276 #else
277 # if defined(__GNUC__)
278 # if __GNUC__ >= 3
279 # define zend_always_inline inline __attribute__((always_inline))
280 # define zend_never_inline __attribute__((noinline))
281 # else
282 # define zend_always_inline inline
283 # define zend_never_inline
284 # endif
285 # elif defined(_MSC_VER)
286 # define zend_always_inline __forceinline
287 # define zend_never_inline
288 # else
289 # if __has_attribute(always_inline)
290 # define zend_always_inline inline __attribute__((always_inline))
291 # else
292 # define zend_always_inline inline
293 # endif
294 # if __has_attribute(noinline)
295 # define zend_never_inline __attribute__((noinline))
296 # else
297 # define zend_never_inline
298 # endif
299 # endif
300 #endif
301
302 #if PHP_HAVE_BUILTIN_EXPECT
303 # define EXPECTED(condition) __builtin_expect(!!(condition), 1)
304 # define UNEXPECTED(condition) __builtin_expect(!!(condition), 0)
305 #else
306 # define EXPECTED(condition) (condition)
307 # define UNEXPECTED(condition) (condition)
308 #endif
309
310 #ifndef XtOffsetOf
311 # if defined(CRAY) || (defined(__ARMCC_VERSION) && !defined(LINUX))
312 # ifdef __STDC__
313 # define XtOffset(p_type, field) _Offsetof(p_type, field)
314 # else
315 # ifdef CRAY2
316 # define XtOffset(p_type, field) \
317 (sizeof(int)*((unsigned int)&(((p_type)NULL)->field)))
318
319 # else
320
321 # define XtOffset(p_type, field) ((unsigned int)&(((p_type)NULL)->field))
322
323 # endif
324 # endif
325 # else
326
327 # define XtOffset(p_type, field) \
328 ((zend_long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
329
330 # endif
331
332 # ifdef offsetof
333 # define XtOffsetOf(s_type, field) offsetof(s_type, field)
334 # else
335 # define XtOffsetOf(s_type, field) XtOffset(s_type*, field)
336 # endif
337
338 #endif
339
340 #if (HAVE_ALLOCA || (defined (__GNUC__) && __GNUC__ >= 2)) && !(defined(ZTS) && defined(NETWARE)) && !(defined(ZTS) && defined(HPUX)) && !defined(DARWIN)
341 # define ZEND_ALLOCA_MAX_SIZE (32 * 1024)
342 # define ALLOCA_FLAG(name) \
343 zend_bool name;
344 # define SET_ALLOCA_FLAG(name) \
345 name = 1
346 # define do_alloca_ex(size, limit, use_heap) \
347 ((use_heap = (UNEXPECTED((size) > (limit)))) ? emalloc(size) : alloca(size))
348 # define do_alloca(size, use_heap) \
349 do_alloca_ex(size, ZEND_ALLOCA_MAX_SIZE, use_heap)
350 # define free_alloca(p, use_heap) \
351 do { if (UNEXPECTED(use_heap)) efree(p); } while (0)
352 #else
353 # define ALLOCA_FLAG(name)
354 # define SET_ALLOCA_FLAG(name)
355 # define do_alloca(p, use_heap) emalloc(p)
356 # define free_alloca(p, use_heap) efree(p)
357 #endif
358
359 #ifdef HAVE_SIGSETJMP
360 # define SETJMP(a) sigsetjmp(a, 0)
361 # define LONGJMP(a,b) siglongjmp(a, b)
362 # define JMP_BUF sigjmp_buf
363 #else
364 # define SETJMP(a) setjmp(a)
365 # define LONGJMP(a,b) longjmp(a, b)
366 # define JMP_BUF jmp_buf
367 #endif
368
369 #if ZEND_DEBUG
370 # define ZEND_FILE_LINE_D const char *__zend_filename, const uint __zend_lineno
371 # define ZEND_FILE_LINE_DC , ZEND_FILE_LINE_D
372 # define ZEND_FILE_LINE_ORIG_D const char *__zend_orig_filename, const uint __zend_orig_lineno
373 # define ZEND_FILE_LINE_ORIG_DC , ZEND_FILE_LINE_ORIG_D
374 # define ZEND_FILE_LINE_RELAY_C __zend_filename, __zend_lineno
375 # define ZEND_FILE_LINE_RELAY_CC , ZEND_FILE_LINE_RELAY_C
376 # define ZEND_FILE_LINE_C __FILE__, __LINE__
377 # define ZEND_FILE_LINE_CC , ZEND_FILE_LINE_C
378 # define ZEND_FILE_LINE_EMPTY_C NULL, 0
379 # define ZEND_FILE_LINE_EMPTY_CC , ZEND_FILE_LINE_EMPTY_C
380 # define ZEND_FILE_LINE_ORIG_RELAY_C __zend_orig_filename, __zend_orig_lineno
381 # define ZEND_FILE_LINE_ORIG_RELAY_CC , ZEND_FILE_LINE_ORIG_RELAY_C
382 #else
383 # define ZEND_FILE_LINE_D
384 # define ZEND_FILE_LINE_DC
385 # define ZEND_FILE_LINE_ORIG_D
386 # define ZEND_FILE_LINE_ORIG_DC
387 # define ZEND_FILE_LINE_RELAY_C
388 # define ZEND_FILE_LINE_RELAY_CC
389 # define ZEND_FILE_LINE_C
390 # define ZEND_FILE_LINE_CC
391 # define ZEND_FILE_LINE_EMPTY_C
392 # define ZEND_FILE_LINE_EMPTY_CC
393 # define ZEND_FILE_LINE_ORIG_RELAY_C
394 # define ZEND_FILE_LINE_ORIG_RELAY_CC
395 #endif
396
397 #if ZEND_DEBUG
398 # define Z_DBG(expr) (expr)
399 #else
400 # define Z_DBG(expr)
401 #endif
402
403 #ifdef ZTS
404 # define ZTS_V 1
405 #else
406 # define ZTS_V 0
407 #endif
408
409 #ifndef LONG_MAX
410 # define LONG_MAX 2147483647L
411 #endif
412
413 #ifndef LONG_MIN
414 # define LONG_MIN (- LONG_MAX - 1)
415 #endif
416
417 #define MAX_LENGTH_OF_DOUBLE 32
418
419 #undef MIN
420 #undef MAX
421 #define MAX(a, b) (((a)>(b))?(a):(b))
422 #define MIN(a, b) (((a)<(b))?(a):(b))
423 #define ZEND_STRL(str) (str), (sizeof(str)-1)
424 #define ZEND_STRS(str) (str), (sizeof(str))
425 #define ZEND_NORMALIZE_BOOL(n) \
426 ((n) ? (((n)>0) ? 1 : -1) : 0)
427 #define ZEND_TRUTH(x) ((x) ? 1 : 0)
428 #define ZEND_LOG_XOR(a, b) (ZEND_TRUTH(a) ^ ZEND_TRUTH(b))
429
430 #define ZEND_MAX_RESERVED_RESOURCES 4
431
432
433 #undef function_table
434
435 #ifdef ZEND_WIN32
436 #define ZEND_SECURE_ZERO(var, size) RtlSecureZeroMemory((var), (size))
437 #else
438 #define ZEND_SECURE_ZERO(var, size) memset((var), 0, (size))
439 #endif
440
441
442 #ifdef ZEND_WIN32
443 #define ZEND_VALID_SOCKET(sock) (INVALID_SOCKET != (sock))
444 #else
445 #define ZEND_VALID_SOCKET(sock) ((sock) >= 0)
446 #endif
447
448 #endif
449
450
451
452
453
454
455
456