xref: /php-src/Zend/zend_types.h (revision 58aa6fc8)
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    |          Xinchen Hui <laruence@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #ifndef ZEND_TYPES_H
23 #define ZEND_TYPES_H
24 
25 #include "zend_portability.h"
26 #include "zend_long.h"
27 #include <stdbool.h>
28 #include <stdint.h>
29 
30 #ifdef __SSE2__
31 # include <mmintrin.h>
32 # include <emmintrin.h>
33 #endif
34 #if defined(__AVX2__)
35 # include <immintrin.h>
36 #endif
37 #if defined(__aarch64__) || defined(_M_ARM64)
38 # include <arm_neon.h>
39 #endif
40 
41 #ifdef WORDS_BIGENDIAN
42 # define ZEND_ENDIAN_LOHI(lo, hi)          hi; lo;
43 # define ZEND_ENDIAN_LOHI_3(lo, mi, hi)    hi; mi; lo;
44 # define ZEND_ENDIAN_LOHI_4(a, b, c, d)    d; c; b; a;
45 # define ZEND_ENDIAN_LOHI_C(lo, hi)        hi, lo
46 # define ZEND_ENDIAN_LOHI_C_3(lo, mi, hi)  hi, mi, lo,
47 # define ZEND_ENDIAN_LOHI_C_4(a, b, c, d)  d, c, b, a
48 #else
49 # define ZEND_ENDIAN_LOHI(lo, hi)          lo; hi;
50 # define ZEND_ENDIAN_LOHI_3(lo, mi, hi)    lo; mi; hi;
51 # define ZEND_ENDIAN_LOHI_4(a, b, c, d)    a; b; c; d;
52 # define ZEND_ENDIAN_LOHI_C(lo, hi)        lo, hi
53 # define ZEND_ENDIAN_LOHI_C_3(lo, mi, hi)  lo, mi, hi,
54 # define ZEND_ENDIAN_LOHI_C_4(a, b, c, d)  a, b, c, d
55 #endif
56 
57 typedef unsigned char zend_uchar;
58 
59 typedef enum {
60   SUCCESS =  0,
61   FAILURE = -1,		/* this MUST stay a negative number, or it may affect functions! */
62 } ZEND_RESULT_CODE;
63 
64 typedef ZEND_RESULT_CODE zend_result;
65 
66 #ifdef ZEND_ENABLE_ZVAL_LONG64
67 # ifdef ZEND_WIN32
68 #  define ZEND_SIZE_MAX  _UI64_MAX
69 # else
70 #  define ZEND_SIZE_MAX  SIZE_MAX
71 # endif
72 #else
73 # if defined(ZEND_WIN32)
74 #  define ZEND_SIZE_MAX  _UI32_MAX
75 # else
76 #  define ZEND_SIZE_MAX SIZE_MAX
77 # endif
78 #endif
79 
80 #ifdef ZTS
81 #define ZEND_TLS static TSRM_TLS
82 #define ZEND_EXT_TLS TSRM_TLS
83 #else
84 #define ZEND_TLS static
85 #define ZEND_EXT_TLS
86 #endif
87 
88 typedef struct _zend_object_handlers zend_object_handlers;
89 typedef struct _zend_class_entry     zend_class_entry;
90 typedef union  _zend_function        zend_function;
91 typedef struct _zend_execute_data    zend_execute_data;
92 
93 typedef struct _zval_struct     zval;
94 
95 typedef struct _zend_refcounted zend_refcounted;
96 typedef struct _zend_string     zend_string;
97 typedef struct _zend_array      zend_array;
98 typedef struct _zend_object     zend_object;
99 typedef struct _zend_resource   zend_resource;
100 typedef struct _zend_reference  zend_reference;
101 typedef struct _zend_ast_ref    zend_ast_ref;
102 typedef struct _zend_ast        zend_ast;
103 
104 typedef int  (*compare_func_t)(const void *, const void *);
105 typedef void (*swap_func_t)(void *, void *);
106 typedef void (*sort_func_t)(void *, size_t, size_t, compare_func_t, swap_func_t);
107 typedef void (*dtor_func_t)(zval *pDest);
108 typedef void (*copy_ctor_func_t)(zval *pElement);
109 
110 /*
111  * zend_type - is an abstraction layer to represent information about type hint.
112  * It shouldn't be used directly. Only through ZEND_TYPE_* macros.
113  *
114  * ZEND_TYPE_IS_SET()        - checks if there is a type-hint
115  * ZEND_TYPE_IS_ONLY_MASK()  - checks if type-hint refer to standard type only
116  * ZEND_TYPE_IS_COMPLEX()    - checks if type is a type_list, or contains a class either as a CE or as a name
117  * ZEND_TYPE_HAS_NAME()      - checks if type-hint contains some class as zend_string *
118  * ZEND_TYPE_HAS_LITERAL_NAME()	- checks if type-hint contains some class as const char *
119  * ZEND_TYPE_IS_INTERSECTION() - checks if the type_list represents an intersection type list
120  * ZEND_TYPE_IS_UNION()      - checks if the type_list represents a union type list
121  *
122  * ZEND_TYPE_NAME()       - returns referenced class name
123  * ZEND_TYPE_PURE_MASK()  - returns MAY_BE_* type mask
124  * ZEND_TYPE_FULL_MASK()  - returns MAY_BE_* type mask together with other flags
125  *
126  * ZEND_TYPE_ALLOW_NULL() - checks if NULL is allowed
127  *
128  * ZEND_TYPE_INIT_*() should be used for construction.
129  */
130 
131 typedef struct {
132 	/* Not using a union here, because there's no good way to initialize them
133 	 * in a way that is supported in both C and C++ (designated initializers
134 	 * are only supported since C++20). */
135 	void *ptr;
136 	uint32_t type_mask;
137 	/* TODO: We could use the extra 32-bit of padding on 64-bit systems. */
138 } zend_type;
139 
140 typedef struct {
141 	uint32_t num_types;
142 	zend_type types[1];
143 } zend_type_list;
144 
145 #define _ZEND_TYPE_EXTRA_FLAGS_SHIFT 25
146 #define _ZEND_TYPE_MASK ((1u << 25) - 1)
147 /* Only one of these bits may be set. */
148 #define _ZEND_TYPE_NAME_BIT (1u << 24)
149 // Used to signify that type.ptr is not a `zend_string*` but a `const char*`,
150 #define _ZEND_TYPE_LITERAL_NAME_BIT (1u << 23)
151 #define _ZEND_TYPE_LIST_BIT (1u << 22)
152 #define _ZEND_TYPE_KIND_MASK (_ZEND_TYPE_LIST_BIT|_ZEND_TYPE_NAME_BIT|_ZEND_TYPE_LITERAL_NAME_BIT)
153 /* For BC behaviour with iterable type */
154 #define _ZEND_TYPE_ITERABLE_BIT (1u << 21)
155 /* Whether the type list is arena allocated */
156 #define _ZEND_TYPE_ARENA_BIT (1u << 20)
157 /* Whether the type list is an intersection type */
158 #define _ZEND_TYPE_INTERSECTION_BIT (1u << 19)
159 /* Whether the type is a union type */
160 #define _ZEND_TYPE_UNION_BIT (1u << 18)
161 /* Type mask excluding the flags above. */
162 #define _ZEND_TYPE_MAY_BE_MASK ((1u << 18) - 1)
163 /* Must have same value as MAY_BE_NULL */
164 #define _ZEND_TYPE_NULLABLE_BIT 0x2u
165 
166 #define ZEND_TYPE_IS_SET(t) \
167 	(((t).type_mask & _ZEND_TYPE_MASK) != 0)
168 
169 /* If a type is complex it means it's either a list with a union or intersection,
170  * or the void pointer is a class name */
171 #define ZEND_TYPE_IS_COMPLEX(t) \
172 	((((t).type_mask) & _ZEND_TYPE_KIND_MASK) != 0)
173 
174 #define ZEND_TYPE_HAS_NAME(t) \
175 	((((t).type_mask) & _ZEND_TYPE_NAME_BIT) != 0)
176 
177 #define ZEND_TYPE_HAS_LITERAL_NAME(t) \
178 	((((t).type_mask) & _ZEND_TYPE_LITERAL_NAME_BIT) != 0)
179 
180 #define ZEND_TYPE_HAS_LIST(t) \
181 	((((t).type_mask) & _ZEND_TYPE_LIST_BIT) != 0)
182 
183 #define ZEND_TYPE_IS_ITERABLE_FALLBACK(t) \
184 	((((t).type_mask) & _ZEND_TYPE_ITERABLE_BIT) != 0)
185 
186 #define ZEND_TYPE_IS_INTERSECTION(t) \
187 	((((t).type_mask) & _ZEND_TYPE_INTERSECTION_BIT) != 0)
188 
189 #define ZEND_TYPE_IS_UNION(t) \
190 	((((t).type_mask) & _ZEND_TYPE_UNION_BIT) != 0)
191 
192 #define ZEND_TYPE_USES_ARENA(t) \
193 	((((t).type_mask) & _ZEND_TYPE_ARENA_BIT) != 0)
194 
195 #define ZEND_TYPE_IS_ONLY_MASK(t) \
196 	(ZEND_TYPE_IS_SET(t) && (t).ptr == NULL)
197 
198 #define ZEND_TYPE_NAME(t) \
199 	((zend_string *) (t).ptr)
200 
201 #define ZEND_TYPE_LITERAL_NAME(t) \
202 	((const char *) (t).ptr)
203 
204 #define ZEND_TYPE_LIST(t) \
205 	((zend_type_list *) (t).ptr)
206 
207 #define ZEND_TYPE_LIST_SIZE(num_types) \
208 	(sizeof(zend_type_list) + ((num_types) - 1) * sizeof(zend_type))
209 
210 /* This iterates over a zend_type_list. */
211 #define ZEND_TYPE_LIST_FOREACH(list, type_ptr) do { \
212 	zend_type *_list = (list)->types; \
213 	zend_type *_end = _list + (list)->num_types; \
214 	for (; _list < _end; _list++) { \
215 		type_ptr = _list;
216 
217 #define ZEND_TYPE_LIST_FOREACH_END() \
218 	} \
219 } while (0)
220 
221 /* This iterates over any zend_type. If it's a type list, all list elements will
222  * be visited. If it's a single type, only the single type is visited. */
223 #define ZEND_TYPE_FOREACH(type, type_ptr) do { \
224 	zend_type *_cur, *_end; \
225 	if (ZEND_TYPE_HAS_LIST(type)) { \
226 		zend_type_list *_list = ZEND_TYPE_LIST(type); \
227 		_cur = _list->types; \
228 		_end = _cur + _list->num_types; \
229 	} else { \
230 		_cur = &(type); \
231 		_end = _cur + 1; \
232 	} \
233 	do { \
234 		type_ptr = _cur;
235 
236 #define ZEND_TYPE_FOREACH_END() \
237 	} while (++_cur < _end); \
238 } while (0)
239 
240 #define ZEND_TYPE_SET_PTR(t, _ptr) \
241 	((t).ptr = (_ptr))
242 
243 #define ZEND_TYPE_SET_PTR_AND_KIND(t, _ptr, kind_bit) do { \
244 	(t).ptr = (_ptr); \
245 	(t).type_mask &= ~_ZEND_TYPE_KIND_MASK; \
246 	(t).type_mask |= (kind_bit); \
247 } while (0)
248 
249 #define ZEND_TYPE_SET_LIST(t, list) \
250 	ZEND_TYPE_SET_PTR_AND_KIND(t, list, _ZEND_TYPE_LIST_BIT)
251 
252 /* FULL_MASK() includes the MAY_BE_* type mask, as well as additional metadata bits.
253  * The PURE_MASK() only includes the MAY_BE_* type mask. */
254 #define ZEND_TYPE_FULL_MASK(t) \
255 	((t).type_mask)
256 
257 #define ZEND_TYPE_PURE_MASK(t) \
258 	((t).type_mask & _ZEND_TYPE_MAY_BE_MASK)
259 
260 #define ZEND_TYPE_FULL_MASK_WITHOUT_NULL(t) \
261 	((t).type_mask & ~_ZEND_TYPE_NULLABLE_BIT)
262 
263 #define ZEND_TYPE_PURE_MASK_WITHOUT_NULL(t) \
264 	((t).type_mask & _ZEND_TYPE_MAY_BE_MASK & ~_ZEND_TYPE_NULLABLE_BIT)
265 
266 #define ZEND_TYPE_CONTAINS_CODE(t, code) \
267 	(((t).type_mask & (1u << (code))) != 0)
268 
269 #define ZEND_TYPE_ALLOW_NULL(t) \
270 	(((t).type_mask & _ZEND_TYPE_NULLABLE_BIT) != 0)
271 
272 #if defined(__cplusplus) && defined(_MSC_VER)
273 # define _ZEND_TYPE_PREFIX zend_type
274 #else
275 /* FIXME: We could add (zend_type) here at some point but this breaks in MSVC because
276  * (zend_type)(zend_type){} is no longer considered constant. */
277 # define _ZEND_TYPE_PREFIX
278 #endif
279 
280 #define ZEND_TYPE_INIT_NONE(extra_flags) \
281 	_ZEND_TYPE_PREFIX { NULL, (extra_flags) }
282 
283 #define ZEND_TYPE_INIT_MASK(_type_mask) \
284 	_ZEND_TYPE_PREFIX { NULL, (_type_mask) }
285 
286 #define ZEND_TYPE_INIT_CODE(code, allow_null, extra_flags) \
287 	ZEND_TYPE_INIT_MASK(((code) == _IS_BOOL ? MAY_BE_BOOL : ( (code) == IS_ITERABLE ? _ZEND_TYPE_ITERABLE_BIT : ((code) == IS_MIXED ? MAY_BE_ANY : (1 << (code))))) \
288 		| ((allow_null) ? _ZEND_TYPE_NULLABLE_BIT : 0) | (extra_flags))
289 
290 #define ZEND_TYPE_INIT_PTR(ptr, type_kind, allow_null, extra_flags) \
291 	_ZEND_TYPE_PREFIX { (void *) (ptr), \
292 		(type_kind) | ((allow_null) ? _ZEND_TYPE_NULLABLE_BIT : 0) | (extra_flags) }
293 
294 #define ZEND_TYPE_INIT_PTR_MASK(ptr, type_mask) \
295 	_ZEND_TYPE_PREFIX { (void *) (ptr), (type_mask) }
296 
297 #define ZEND_TYPE_INIT_UNION(ptr, extra_flags) \
298 	_ZEND_TYPE_PREFIX { (void *) (ptr), (_ZEND_TYPE_LIST_BIT|_ZEND_TYPE_UNION_BIT) | (extra_flags) }
299 
300 #define ZEND_TYPE_INIT_INTERSECTION(ptr, extra_flags) \
301 	_ZEND_TYPE_PREFIX { (void *) (ptr), (_ZEND_TYPE_LIST_BIT|_ZEND_TYPE_INTERSECTION_BIT) | (extra_flags) }
302 
303 #define ZEND_TYPE_INIT_CLASS(class_name, allow_null, extra_flags) \
304 	ZEND_TYPE_INIT_PTR(class_name, _ZEND_TYPE_NAME_BIT, allow_null, extra_flags)
305 
306 #define ZEND_TYPE_INIT_CLASS_MASK(class_name, type_mask) \
307 	ZEND_TYPE_INIT_PTR_MASK(class_name, _ZEND_TYPE_NAME_BIT | (type_mask))
308 
309 #define ZEND_TYPE_INIT_CLASS_CONST(class_name, allow_null, extra_flags) \
310 	ZEND_TYPE_INIT_PTR(class_name, _ZEND_TYPE_LITERAL_NAME_BIT, allow_null, extra_flags)
311 
312 #define ZEND_TYPE_INIT_CLASS_CONST_MASK(class_name, type_mask) \
313 	ZEND_TYPE_INIT_PTR_MASK(class_name, (_ZEND_TYPE_LITERAL_NAME_BIT | (type_mask)))
314 
315 typedef union _zend_value {
316 	zend_long         lval;				/* long value */
317 	double            dval;				/* double value */
318 	zend_refcounted  *counted;
319 	zend_string      *str;
320 	zend_array       *arr;
321 	zend_object      *obj;
322 	zend_resource    *res;
323 	zend_reference   *ref;
324 	zend_ast_ref     *ast;
325 	zval             *zv;
326 	void             *ptr;
327 	zend_class_entry *ce;
328 	zend_function    *func;
329 	struct {
330 		uint32_t w1;
331 		uint32_t w2;
332 	} ww;
333 } zend_value;
334 
335 struct _zval_struct {
336 	zend_value        value;			/* value */
337 	union {
338 		uint32_t type_info;
339 		struct {
340 			ZEND_ENDIAN_LOHI_3(
341 				uint8_t    type,			/* active type */
342 				uint8_t    type_flags,
343 				union {
344 					uint16_t  extra;        /* not further specified */
345 				} u)
346 		} v;
347 	} u1;
348 	union {
349 		uint32_t     next;                 /* hash collision chain */
350 		uint32_t     cache_slot;           /* cache slot (for RECV_INIT) */
351 		uint32_t     opline_num;           /* opline number (for FAST_CALL) */
352 		uint32_t     lineno;               /* line number (for ast nodes) */
353 		uint32_t     num_args;             /* arguments number for EX(This) */
354 		uint32_t     fe_pos;               /* foreach position */
355 		uint32_t     fe_iter_idx;          /* foreach iterator index */
356 		uint32_t     guard;                /* recursion and single property guard */
357 		uint32_t     constant_flags;       /* constant flags */
358 		uint32_t     extra;                /* not further specified */
359 	} u2;
360 };
361 
362 typedef struct _zend_refcounted_h {
363 	uint32_t         refcount;			/* reference counter 32-bit */
364 	union {
365 		uint32_t type_info;
366 	} u;
367 } zend_refcounted_h;
368 
369 struct _zend_refcounted {
370 	zend_refcounted_h gc;
371 };
372 
373 struct _zend_string {
374 	zend_refcounted_h gc;
375 	zend_ulong        h;                /* hash value */
376 	size_t            len;
377 	char              val[1];
378 };
379 
380 typedef struct _Bucket {
381 	zval              val;
382 	zend_ulong        h;                /* hash value (or numeric index)   */
383 	zend_string      *key;              /* string key or NULL for numerics */
384 } Bucket;
385 
386 typedef struct _zend_array HashTable;
387 
388 struct _zend_array {
389 	zend_refcounted_h gc;
390 	union {
391 		struct {
392 			ZEND_ENDIAN_LOHI_4(
393 				uint8_t    flags,
394 				uint8_t    _unused,
395 				uint8_t    nIteratorsCount,
396 				uint8_t    _unused2)
397 		} v;
398 		uint32_t flags;
399 	} u;
400 	uint32_t          nTableMask;
401 	union {
402 		uint32_t     *arHash;   /* hash table (allocated above this pointer) */
403 		Bucket       *arData;   /* array of hash buckets */
404 		zval         *arPacked; /* packed array of zvals */
405 	};
406 	uint32_t          nNumUsed;
407 	uint32_t          nNumOfElements;
408 	uint32_t          nTableSize;
409 	uint32_t          nInternalPointer;
410 	zend_long         nNextFreeElement;
411 	dtor_func_t       pDestructor;
412 };
413 
414 /*
415  * HashTable Data Layout
416  * =====================
417  *
418  *                 +=============================+
419  *                 | HT_HASH(ht, ht->nTableMask) |                   +=============================+
420  *                 | ...                         |                   | HT_INVALID_IDX              |
421  *                 | HT_HASH(ht, -1)             |                   | HT_INVALID_IDX              |
422  *                 +-----------------------------+                   +-----------------------------+
423  * ht->arData ---> | Bucket[0]                   | ht->arPacked ---> | ZVAL[0]                     |
424  *                 | ...                         |                   | ...                         |
425  *                 | Bucket[ht->nTableSize-1]    |                   | ZVAL[ht->nTableSize-1]      |
426  *                 +=============================+                   +=============================+
427  */
428 
429 #define HT_INVALID_IDX ((uint32_t) -1)
430 
431 #define HT_MIN_MASK ((uint32_t) -2)
432 #define HT_MIN_SIZE 8
433 
434 /* HT_MAX_SIZE is chosen to satisfy the following constraints:
435  * - HT_SIZE_TO_MASK(HT_MAX_SIZE) != 0
436  * - HT_SIZE_EX(HT_MAX_SIZE, HT_SIZE_TO_MASK(HT_MAX_SIZE)) does not overflow or
437  *   wrapparound, and is <= the addressable space size
438  * - HT_MAX_SIZE must be a power of two:
439  *   (nTableSize<HT_MAX_SIZE ? nTableSize+nTableSize : nTableSize) <= HT_MAX_SIZE
440  */
441 #if SIZEOF_SIZE_T == 4
442 # define HT_MAX_SIZE 0x02000000
443 # define HT_HASH_TO_BUCKET_EX(data, idx) \
444 	((Bucket*)((char*)(data) + (idx)))
445 # define HT_IDX_TO_HASH(idx) \
446 	((idx) * sizeof(Bucket))
447 # define HT_HASH_TO_IDX(idx) \
448 	((idx) / sizeof(Bucket))
449 #elif SIZEOF_SIZE_T == 8
450 # define HT_MAX_SIZE 0x40000000
451 # define HT_HASH_TO_BUCKET_EX(data, idx) \
452 	((data) + (idx))
453 # define HT_IDX_TO_HASH(idx) \
454 	(idx)
455 # define HT_HASH_TO_IDX(idx) \
456 	(idx)
457 #else
458 # error "Unknown SIZEOF_SIZE_T"
459 #endif
460 
461 #define HT_HASH_EX(data, idx) \
462 	((uint32_t*)(data))[(int32_t)(idx)]
463 #define HT_HASH(ht, idx) \
464 	HT_HASH_EX((ht)->arHash, idx)
465 
466 #define HT_SIZE_TO_MASK(nTableSize) \
467 	((uint32_t)(-((nTableSize) + (nTableSize))))
468 #define HT_HASH_SIZE(nTableMask) \
469 	(((size_t)-(uint32_t)(nTableMask)) * sizeof(uint32_t))
470 #define HT_DATA_SIZE(nTableSize) \
471 	((size_t)(nTableSize) * sizeof(Bucket))
472 #define HT_SIZE_EX(nTableSize, nTableMask) \
473 	(HT_DATA_SIZE((nTableSize)) + HT_HASH_SIZE((nTableMask)))
474 #define HT_SIZE(ht) \
475 	HT_SIZE_EX((ht)->nTableSize, (ht)->nTableMask)
476 #define HT_USED_SIZE(ht) \
477 	(HT_HASH_SIZE((ht)->nTableMask) + ((size_t)(ht)->nNumUsed * sizeof(Bucket)))
478 #define HT_PACKED_DATA_SIZE(nTableSize) \
479 	((size_t)(nTableSize) * sizeof(zval))
480 #define HT_PACKED_SIZE_EX(nTableSize, nTableMask) \
481 	(HT_PACKED_DATA_SIZE((nTableSize)) + HT_HASH_SIZE((nTableMask)))
482 #define HT_PACKED_SIZE(ht) \
483 	HT_PACKED_SIZE_EX((ht)->nTableSize, (ht)->nTableMask)
484 #define HT_PACKED_USED_SIZE(ht) \
485 	(HT_HASH_SIZE((ht)->nTableMask) + ((size_t)(ht)->nNumUsed * sizeof(zval)))
486 #if defined(__AVX2__)
487 # define HT_HASH_RESET(ht) do { \
488 		char *p = (char*)&HT_HASH(ht, (ht)->nTableMask); \
489 		size_t size = HT_HASH_SIZE((ht)->nTableMask); \
490 		__m256i ymm0 = _mm256_setzero_si256(); \
491 		ymm0 = _mm256_cmpeq_epi64(ymm0, ymm0); \
492 		ZEND_ASSERT(size >= 64 && ((size & 0x3f) == 0)); \
493 		do { \
494 			_mm256_storeu_si256((__m256i*)p, ymm0); \
495 			_mm256_storeu_si256((__m256i*)(p+32), ymm0); \
496 			p += 64; \
497 			size -= 64; \
498 		} while (size != 0); \
499 	} while (0)
500 #elif defined(__SSE2__)
501 # define HT_HASH_RESET(ht) do { \
502 		char *p = (char*)&HT_HASH(ht, (ht)->nTableMask); \
503 		size_t size = HT_HASH_SIZE((ht)->nTableMask); \
504 		__m128i xmm0 = _mm_setzero_si128(); \
505 		xmm0 = _mm_cmpeq_epi8(xmm0, xmm0); \
506 		ZEND_ASSERT(size >= 64 && ((size & 0x3f) == 0)); \
507 		do { \
508 			_mm_storeu_si128((__m128i*)p, xmm0); \
509 			_mm_storeu_si128((__m128i*)(p+16), xmm0); \
510 			_mm_storeu_si128((__m128i*)(p+32), xmm0); \
511 			_mm_storeu_si128((__m128i*)(p+48), xmm0); \
512 			p += 64; \
513 			size -= 64; \
514 		} while (size != 0); \
515 	} while (0)
516 #elif defined(__aarch64__) || defined(_M_ARM64)
517 # define HT_HASH_RESET(ht) do { \
518 		char *p = (char*)&HT_HASH(ht, (ht)->nTableMask); \
519 		size_t size = HT_HASH_SIZE((ht)->nTableMask); \
520 		int32x4_t t = vdupq_n_s32(-1); \
521 		ZEND_ASSERT(size >= 64 && ((size & 0x3f) == 0)); \
522 		do { \
523 			vst1q_s32((int32_t*)p, t); \
524 			vst1q_s32((int32_t*)(p+16), t); \
525 			vst1q_s32((int32_t*)(p+32), t); \
526 			vst1q_s32((int32_t*)(p+48), t); \
527 			p += 64; \
528 			size -= 64; \
529 		} while (size != 0); \
530 	} while (0)
531 #else
532 # define HT_HASH_RESET(ht) \
533 	memset(&HT_HASH(ht, (ht)->nTableMask), HT_INVALID_IDX, HT_HASH_SIZE((ht)->nTableMask))
534 #endif
535 #define HT_HASH_RESET_PACKED(ht) do { \
536 		HT_HASH(ht, -2) = HT_INVALID_IDX; \
537 		HT_HASH(ht, -1) = HT_INVALID_IDX; \
538 	} while (0)
539 #define HT_HASH_TO_BUCKET(ht, idx) \
540 	HT_HASH_TO_BUCKET_EX((ht)->arData, idx)
541 
542 #define HT_SET_DATA_ADDR(ht, ptr) do { \
543 		(ht)->arData = (Bucket*)(((char*)(ptr)) + HT_HASH_SIZE((ht)->nTableMask)); \
544 	} while (0)
545 #define HT_GET_DATA_ADDR(ht) \
546 	((char*)((ht)->arData) - HT_HASH_SIZE((ht)->nTableMask))
547 
548 typedef uint32_t HashPosition;
549 
550 typedef struct _HashTableIterator {
551 	HashTable    *ht;
552 	HashPosition  pos;
553 	uint32_t      next_copy; // circular linked list via index into EG(ht_iterators)
554 } HashTableIterator;
555 
556 struct _zend_object {
557 	zend_refcounted_h gc;
558 	uint32_t          handle; // TODO: may be removed ???
559 	uint32_t          extra_flags; /* OBJ_EXTRA_FLAGS() */
560 	zend_class_entry *ce;
561 	const zend_object_handlers *handlers;
562 	HashTable        *properties;
563 	zval              properties_table[1];
564 };
565 
566 struct _zend_resource {
567 	zend_refcounted_h gc;
568 	zend_long         handle; // TODO: may be removed ???
569 	int               type;
570 	void             *ptr;
571 };
572 
573 typedef struct {
574 	size_t num;
575 	size_t num_allocated;
576 	struct _zend_property_info *ptr[1];
577 } zend_property_info_list;
578 
579 typedef union {
580 	struct _zend_property_info *ptr;
581 	uintptr_t list;
582 } zend_property_info_source_list;
583 
584 #define ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(list) (0x1 | (uintptr_t) (list))
585 #define ZEND_PROPERTY_INFO_SOURCE_TO_LIST(list) ((zend_property_info_list *) ((list) & ~0x1))
586 #define ZEND_PROPERTY_INFO_SOURCE_IS_LIST(list) ((list) & 0x1)
587 
588 struct _zend_reference {
589 	zend_refcounted_h              gc;
590 	zval                           val;
591 	zend_property_info_source_list sources;
592 };
593 
594 struct _zend_ast_ref {
595 	zend_refcounted_h gc;
596 	/*zend_ast        ast; zend_ast follows the zend_ast_ref structure */
597 };
598 
599 /* Regular data types: Must be in sync with zend_variables.c. */
600 #define IS_UNDEF					0
601 #define IS_NULL						1
602 #define IS_FALSE					2
603 #define IS_TRUE						3
604 #define IS_LONG						4
605 #define IS_DOUBLE					5
606 #define IS_STRING					6
607 #define IS_ARRAY					7
608 #define IS_OBJECT					8
609 #define IS_RESOURCE					9
610 #define IS_REFERENCE				10
611 #define IS_CONSTANT_AST				11 /* Constant expressions */
612 
613 /* Fake types used only for type hinting.
614  * These are allowed to overlap with the types below. */
615 #define IS_CALLABLE					12
616 #define IS_ITERABLE					13
617 #define IS_VOID						14
618 #define IS_STATIC					15
619 #define IS_MIXED					16
620 #define IS_NEVER					17
621 
622 /* internal types */
623 #define IS_INDIRECT             	12
624 #define IS_PTR						13
625 #define IS_ALIAS_PTR				14
626 #define _IS_ERROR					15
627 
628 /* used for casts */
629 #define _IS_BOOL					18
630 #define _IS_NUMBER					19
631 
632 /* guard flags */
633 #define ZEND_GUARD_PROPERTY_GET		(1<<0)
634 #define ZEND_GUARD_PROPERTY_SET		(1<<1)
635 #define ZEND_GUARD_PROPERTY_UNSET	(1<<2)
636 #define ZEND_GUARD_PROPERTY_ISSET	(1<<3)
637 #define ZEND_GUARD_PROPERTY_HOOK	(1<<4)
638 #define ZEND_GUARD_PROPERTY_MASK	31
639 #define ZEND_GUARD_RECURSION_DEBUG	(1<<5)
640 #define ZEND_GUARD_RECURSION_EXPORT	(1<<6)
641 #define ZEND_GUARD_RECURSION_JSON	(1<<7)
642 
643 #define ZEND_GUARD_RECURSION_TYPE(t) ZEND_GUARD_RECURSION_ ## t
644 
645 #define ZEND_GUARD_IS_RECURSIVE(pg, t)			((*pg & ZEND_GUARD_RECURSION_TYPE(t)) != 0)
646 #define ZEND_GUARD_PROTECT_RECURSION(pg, t)		*pg |= ZEND_GUARD_RECURSION_TYPE(t)
647 #define ZEND_GUARD_UNPROTECT_RECURSION(pg, t)	*pg &= ~ZEND_GUARD_RECURSION_TYPE(t)
648 
zval_get_type(const zval * pz)649 static zend_always_inline uint8_t zval_get_type(const zval* pz) {
650 	return pz->u1.v.type;
651 }
652 
653 #define ZEND_SAME_FAKE_TYPE(faketype, realtype) ( \
654 	(faketype) == (realtype) \
655 	|| ((faketype) == _IS_BOOL && ((realtype) == IS_TRUE || (realtype) == IS_FALSE)) \
656 )
657 
658 /* we should never set just Z_TYPE, we should set Z_TYPE_INFO */
659 #define Z_TYPE(zval)				zval_get_type(&(zval))
660 #define Z_TYPE_P(zval_p)			Z_TYPE(*(zval_p))
661 
662 #define Z_TYPE_FLAGS(zval)			(zval).u1.v.type_flags
663 #define Z_TYPE_FLAGS_P(zval_p)		Z_TYPE_FLAGS(*(zval_p))
664 
665 #define Z_TYPE_EXTRA(zval)			(zval).u1.v.u.extra
666 #define Z_TYPE_EXTRA_P(zval_p)		Z_TYPE_EXTRA(*(zval_p))
667 
668 #define Z_TYPE_INFO(zval)			(zval).u1.type_info
669 #define Z_TYPE_INFO_P(zval_p)		Z_TYPE_INFO(*(zval_p))
670 
671 #define Z_NEXT(zval)				(zval).u2.next
672 #define Z_NEXT_P(zval_p)			Z_NEXT(*(zval_p))
673 
674 #define Z_CACHE_SLOT(zval)			(zval).u2.cache_slot
675 #define Z_CACHE_SLOT_P(zval_p)		Z_CACHE_SLOT(*(zval_p))
676 
677 #define Z_LINENO(zval)				(zval).u2.lineno
678 #define Z_LINENO_P(zval_p)			Z_LINENO(*(zval_p))
679 
680 #define Z_OPLINE_NUM(zval)			(zval).u2.opline_num
681 #define Z_OPLINE_NUM_P(zval_p)		Z_OPLINE_NUM(*(zval_p))
682 
683 #define Z_FE_POS(zval)				(zval).u2.fe_pos
684 #define Z_FE_POS_P(zval_p)			Z_FE_POS(*(zval_p))
685 
686 #define Z_FE_ITER(zval)				(zval).u2.fe_iter_idx
687 #define Z_FE_ITER_P(zval_p)			Z_FE_ITER(*(zval_p))
688 
689 #define Z_GUARD(zval)				(zval).u2.guard
690 #define Z_GUARD_P(zval_p)			Z_GUARD(*(zval_p))
691 
692 #define Z_CONSTANT_FLAGS(zval)		(zval).u2.constant_flags
693 #define Z_CONSTANT_FLAGS_P(zval_p)	Z_CONSTANT_FLAGS(*(zval_p))
694 
695 #define Z_EXTRA(zval)				(zval).u2.extra
696 #define Z_EXTRA_P(zval_p)			Z_EXTRA(*(zval_p))
697 
698 #define Z_COUNTED(zval)				(zval).value.counted
699 #define Z_COUNTED_P(zval_p)			Z_COUNTED(*(zval_p))
700 
701 #define Z_TYPE_MASK					0xff
702 #define Z_TYPE_FLAGS_MASK			0xff00
703 
704 #define Z_TYPE_FLAGS_SHIFT			8
705 #define Z_TYPE_INFO_EXTRA_SHIFT		16
706 
707 #define GC_REFCOUNT(p)				zend_gc_refcount(&(p)->gc)
708 #define GC_SET_REFCOUNT(p, rc)		zend_gc_set_refcount(&(p)->gc, rc)
709 #define GC_ADDREF(p)				zend_gc_addref(&(p)->gc)
710 #define GC_DELREF(p)				zend_gc_delref(&(p)->gc)
711 #define GC_ADDREF_EX(p, rc)			zend_gc_addref_ex(&(p)->gc, rc)
712 #define GC_DELREF_EX(p, rc)			zend_gc_delref_ex(&(p)->gc, rc)
713 #define GC_TRY_ADDREF(p)			zend_gc_try_addref(&(p)->gc)
714 #define GC_TRY_DELREF(p)			zend_gc_try_delref(&(p)->gc)
715 
716 #define GC_DTOR(p) \
717 	do { \
718 		zend_refcounted_h *_p = &(p)->gc; \
719 		if (zend_gc_delref(_p) == 0) { \
720 			rc_dtor_func((zend_refcounted *)_p); \
721 		} else { \
722 			gc_check_possible_root((zend_refcounted *)_p); \
723 		} \
724 	} while (0)
725 
726 #define GC_DTOR_NO_REF(p) \
727 	do { \
728 		zend_refcounted_h *_p = &(p)->gc; \
729 		if (zend_gc_delref(_p) == 0) { \
730 			rc_dtor_func((zend_refcounted *)_p); \
731 		} else { \
732 			gc_check_possible_root_no_ref((zend_refcounted *)_p); \
733 		} \
734 	} while (0)
735 
736 #define GC_TYPE_MASK				0x0000000f
737 #define GC_FLAGS_MASK				0x000003f0
738 #define GC_INFO_MASK				0xfffffc00
739 #define GC_FLAGS_SHIFT				0
740 #define GC_INFO_SHIFT				10
741 
zval_gc_type(uint32_t gc_type_info)742 static zend_always_inline uint8_t zval_gc_type(uint32_t gc_type_info) {
743 	return (gc_type_info & GC_TYPE_MASK);
744 }
745 
zval_gc_flags(uint32_t gc_type_info)746 static zend_always_inline uint32_t zval_gc_flags(uint32_t gc_type_info) {
747 	return (gc_type_info >> GC_FLAGS_SHIFT) & (GC_FLAGS_MASK >> GC_FLAGS_SHIFT);
748 }
749 
zval_gc_info(uint32_t gc_type_info)750 static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {
751 	return (gc_type_info >> GC_INFO_SHIFT);
752 }
753 
754 #define GC_TYPE_INFO(p)				(p)->gc.u.type_info
755 #define GC_TYPE(p)					zval_gc_type(GC_TYPE_INFO(p))
756 #define GC_FLAGS(p)					zval_gc_flags(GC_TYPE_INFO(p))
757 #define GC_INFO(p)					zval_gc_info(GC_TYPE_INFO(p))
758 
759 #define GC_ADD_FLAGS(p, flags) do { \
760 		GC_TYPE_INFO(p) |= (flags) << GC_FLAGS_SHIFT; \
761 	} while (0)
762 #define GC_DEL_FLAGS(p, flags) do { \
763 		GC_TYPE_INFO(p) &= ~((flags) << GC_FLAGS_SHIFT); \
764 	} while (0)
765 
766 #define Z_GC_TYPE(zval)				GC_TYPE(Z_COUNTED(zval))
767 #define Z_GC_TYPE_P(zval_p)			Z_GC_TYPE(*(zval_p))
768 
769 #define Z_GC_FLAGS(zval)			GC_FLAGS(Z_COUNTED(zval))
770 #define Z_GC_FLAGS_P(zval_p)		Z_GC_FLAGS(*(zval_p))
771 
772 #define Z_GC_INFO(zval)				GC_INFO(Z_COUNTED(zval))
773 #define Z_GC_INFO_P(zval_p)			Z_GC_INFO(*(zval_p))
774 #define Z_GC_TYPE_INFO(zval)		GC_TYPE_INFO(Z_COUNTED(zval))
775 #define Z_GC_TYPE_INFO_P(zval_p)	Z_GC_TYPE_INFO(*(zval_p))
776 
777 /* zval_gc_flags(zval.value->gc.u.type_info) (common flags) */
778 #define GC_NOT_COLLECTABLE			(1<<4)
779 #define GC_PROTECTED                (1<<5) /* used for recursion detection */
780 #define GC_IMMUTABLE                (1<<6) /* can't be changed in place */
781 #define GC_PERSISTENT               (1<<7) /* allocated using malloc */
782 #define GC_PERSISTENT_LOCAL         (1<<8) /* persistent, but thread-local */
783 
784 #define GC_NULL						(IS_NULL         | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
785 #define GC_STRING					(IS_STRING       | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
786 #define GC_ARRAY					IS_ARRAY
787 #define GC_OBJECT					IS_OBJECT
788 #define GC_RESOURCE					(IS_RESOURCE     | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
789 #define GC_REFERENCE				(IS_REFERENCE    | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
790 #define GC_CONSTANT_AST				(IS_CONSTANT_AST | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
791 
792 /* zval.u1.v.type_flags */
793 #define IS_TYPE_REFCOUNTED			(1<<0)
794 #define IS_TYPE_COLLECTABLE			(1<<1)
795 
796 #if 1
797 /* This optimized version assumes that we have a single "type_flag" */
798 /* IS_TYPE_COLLECTABLE may be used only with IS_TYPE_REFCOUNTED */
799 # define Z_TYPE_INFO_REFCOUNTED(t)	(((t) & Z_TYPE_FLAGS_MASK) != 0)
800 #else
801 # define Z_TYPE_INFO_REFCOUNTED(t)	(((t) & (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT)) != 0)
802 #endif
803 
804 /* extended types */
805 #define IS_INTERNED_STRING_EX		IS_STRING
806 
807 #define IS_STRING_EX				(IS_STRING         | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
808 #define IS_ARRAY_EX					(IS_ARRAY          | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT) | (IS_TYPE_COLLECTABLE << Z_TYPE_FLAGS_SHIFT))
809 #define IS_OBJECT_EX				(IS_OBJECT         | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT) | (IS_TYPE_COLLECTABLE << Z_TYPE_FLAGS_SHIFT))
810 #define IS_RESOURCE_EX				(IS_RESOURCE       | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
811 #define IS_REFERENCE_EX				(IS_REFERENCE      | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
812 
813 #define IS_CONSTANT_AST_EX			(IS_CONSTANT_AST   | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
814 
815 /* string flags (zval.value->gc.u.flags) */
816 #define IS_STR_CLASS_NAME_MAP_PTR   GC_PROTECTED  /* refcount is a map_ptr offset of class_entry */
817 #define IS_STR_INTERNED				GC_IMMUTABLE  /* interned string */
818 #define IS_STR_PERSISTENT			GC_PERSISTENT /* allocated using malloc */
819 #define IS_STR_PERMANENT        	(1<<8)        /* relives request boundary */
820 #define IS_STR_VALID_UTF8           (1<<9)        /* valid UTF-8 according to PCRE */
821 
822 /* array flags */
823 #define IS_ARRAY_IMMUTABLE			GC_IMMUTABLE
824 #define IS_ARRAY_PERSISTENT			GC_PERSISTENT
825 
826 /* object flags (zval.value->gc.u.flags) */
827 #define IS_OBJ_WEAKLY_REFERENCED	GC_PERSISTENT
828 #define IS_OBJ_DESTRUCTOR_CALLED	(1<<8)
829 #define IS_OBJ_FREE_CALLED			(1<<9)
830 
831 #define OBJ_FLAGS(obj)              GC_FLAGS(obj)
832 
833 /* object extra flags (zend_object.flags) */
834 
835 #define IS_OBJ_LAZY_UNINITIALIZED   (1U<<31) /* Virtual proxy or uninitialized Ghost */
836 #define IS_OBJ_LAZY_PROXY           (1U<<30) /* Virtual proxy (may be initialized) */
837 
838 #define OBJ_EXTRA_FLAGS(obj)		((obj)->extra_flags)
839 
840 /* Fast class cache */
841 #define ZSTR_HAS_CE_CACHE(s)		(GC_FLAGS(s) & IS_STR_CLASS_NAME_MAP_PTR)
842 #define ZSTR_GET_CE_CACHE(s)		ZSTR_GET_CE_CACHE_EX(s, 1)
843 #define ZSTR_SET_CE_CACHE(s, ce)	ZSTR_SET_CE_CACHE_EX(s, ce, 1)
844 
845 #define ZSTR_VALID_CE_CACHE(s)		EXPECTED((GC_REFCOUNT(s)-1)/sizeof(void *) < CG(map_ptr_last))
846 
847 #define ZSTR_GET_CE_CACHE_EX(s, validate) \
848 	((!(validate) || ZSTR_VALID_CE_CACHE(s)) ? GET_CE_CACHE(GC_REFCOUNT(s)) : NULL)
849 
850 #define ZSTR_SET_CE_CACHE_EX(s, ce, validate) do { \
851 		if (!(validate) || ZSTR_VALID_CE_CACHE(s)) { \
852 			ZEND_ASSERT((validate) || ZSTR_VALID_CE_CACHE(s)); \
853 			SET_CE_CACHE(GC_REFCOUNT(s), ce); \
854 		} \
855 	} while (0)
856 
857 #define GET_CE_CACHE(ce_cache) \
858 	(*(zend_class_entry **)ZEND_MAP_PTR_OFFSET2PTR(ce_cache))
859 
860 #define SET_CE_CACHE(ce_cache, ce) do { \
861 		*((zend_class_entry **)ZEND_MAP_PTR_OFFSET2PTR(ce_cache)) = ce; \
862 	} while (0)
863 
864 /* Recursion protection macros must be used only for arrays and objects */
865 #define GC_IS_RECURSIVE(p) \
866 	(GC_FLAGS(p) & GC_PROTECTED)
867 
868 #define GC_PROTECT_RECURSION(p) do { \
869 		GC_ADD_FLAGS(p, GC_PROTECTED); \
870 	} while (0)
871 
872 #define GC_UNPROTECT_RECURSION(p) do { \
873 		GC_DEL_FLAGS(p, GC_PROTECTED); \
874 	} while (0)
875 
876 #define GC_TRY_PROTECT_RECURSION(p) do { \
877 		if (!(GC_FLAGS(p) & GC_IMMUTABLE)) GC_PROTECT_RECURSION(p); \
878 	} while (0)
879 
880 #define GC_TRY_UNPROTECT_RECURSION(p) do { \
881 		if (!(GC_FLAGS(p) & GC_IMMUTABLE)) GC_UNPROTECT_RECURSION(p); \
882 	} while (0)
883 
884 #define Z_IS_RECURSIVE(zval)        GC_IS_RECURSIVE(Z_COUNTED(zval))
885 #define Z_PROTECT_RECURSION(zval)   GC_PROTECT_RECURSION(Z_COUNTED(zval))
886 #define Z_UNPROTECT_RECURSION(zval) GC_UNPROTECT_RECURSION(Z_COUNTED(zval))
887 #define Z_IS_RECURSIVE_P(zv)        Z_IS_RECURSIVE(*(zv))
888 #define Z_PROTECT_RECURSION_P(zv)   Z_PROTECT_RECURSION(*(zv))
889 #define Z_UNPROTECT_RECURSION_P(zv) Z_UNPROTECT_RECURSION(*(zv))
890 
891 #define ZEND_GUARD_OR_GC_IS_RECURSIVE(pg, t, zobj) \
892 	(pg ? ZEND_GUARD_IS_RECURSIVE(pg, t) : GC_IS_RECURSIVE(zobj))
893 
894 #define ZEND_GUARD_OR_GC_PROTECT_RECURSION(pg, t, zobj) do { \
895 		if (pg) { \
896 			ZEND_GUARD_PROTECT_RECURSION(pg, t); \
897 		} else { \
898 			GC_PROTECT_RECURSION(zobj); \
899 		} \
900 	} while(0)
901 
902 #define ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(pg, t, zobj) do { \
903 		if (pg) { \
904 			ZEND_GUARD_UNPROTECT_RECURSION(pg, t); \
905 		} else { \
906 			GC_UNPROTECT_RECURSION(zobj); \
907 		} \
908 	} while(0)
909 
910 /* All data types < IS_STRING have their constructor/destructors skipped */
911 #define Z_CONSTANT(zval)			(Z_TYPE(zval) == IS_CONSTANT_AST)
912 #define Z_CONSTANT_P(zval_p)		Z_CONSTANT(*(zval_p))
913 
914 #if 1
915 /* This optimized version assumes that we have a single "type_flag" */
916 /* IS_TYPE_COLLECTABLE may be used only with IS_TYPE_REFCOUNTED */
917 #define Z_REFCOUNTED(zval)			(Z_TYPE_FLAGS(zval) != 0)
918 #else
919 #define Z_REFCOUNTED(zval)			((Z_TYPE_FLAGS(zval) & IS_TYPE_REFCOUNTED) != 0)
920 #endif
921 #define Z_REFCOUNTED_P(zval_p)		Z_REFCOUNTED(*(zval_p))
922 
923 #define Z_COLLECTABLE(zval)			((Z_TYPE_FLAGS(zval) & IS_TYPE_COLLECTABLE) != 0)
924 #define Z_COLLECTABLE_P(zval_p)		Z_COLLECTABLE(*(zval_p))
925 
926 /* deprecated: (COPYABLE is the same as IS_ARRAY) */
927 #define Z_COPYABLE(zval)			(Z_TYPE(zval) == IS_ARRAY)
928 #define Z_COPYABLE_P(zval_p)		Z_COPYABLE(*(zval_p))
929 
930 /* deprecated: (IMMUTABLE is the same as IS_ARRAY && !REFCOUNTED) */
931 #define Z_IMMUTABLE(zval)			(Z_TYPE_INFO(zval) == IS_ARRAY)
932 #define Z_IMMUTABLE_P(zval_p)		Z_IMMUTABLE(*(zval_p))
933 #define Z_OPT_IMMUTABLE(zval)		Z_IMMUTABLE(zval_p)
934 #define Z_OPT_IMMUTABLE_P(zval_p)	Z_IMMUTABLE(*(zval_p))
935 
936 /* the following Z_OPT_* macros make better code when Z_TYPE_INFO accessed before */
937 #define Z_OPT_TYPE(zval)			(Z_TYPE_INFO(zval) & Z_TYPE_MASK)
938 #define Z_OPT_TYPE_P(zval_p)		Z_OPT_TYPE(*(zval_p))
939 
940 #define Z_OPT_CONSTANT(zval)		(Z_OPT_TYPE(zval) == IS_CONSTANT_AST)
941 #define Z_OPT_CONSTANT_P(zval_p)	Z_OPT_CONSTANT(*(zval_p))
942 
943 #define Z_OPT_REFCOUNTED(zval)		Z_TYPE_INFO_REFCOUNTED(Z_TYPE_INFO(zval))
944 #define Z_OPT_REFCOUNTED_P(zval_p)	Z_OPT_REFCOUNTED(*(zval_p))
945 
946 /* deprecated: (COPYABLE is the same as IS_ARRAY) */
947 #define Z_OPT_COPYABLE(zval)		(Z_OPT_TYPE(zval) == IS_ARRAY)
948 #define Z_OPT_COPYABLE_P(zval_p)	Z_OPT_COPYABLE(*(zval_p))
949 
950 #define Z_OPT_ISREF(zval)			(Z_OPT_TYPE(zval) == IS_REFERENCE)
951 #define Z_OPT_ISREF_P(zval_p)		Z_OPT_ISREF(*(zval_p))
952 
953 #define Z_ISREF(zval)				(Z_TYPE(zval) == IS_REFERENCE)
954 #define Z_ISREF_P(zval_p)			Z_ISREF(*(zval_p))
955 
956 #define Z_ISUNDEF(zval)				(Z_TYPE(zval) == IS_UNDEF)
957 #define Z_ISUNDEF_P(zval_p)			Z_ISUNDEF(*(zval_p))
958 
959 #define Z_ISNULL(zval)				(Z_TYPE(zval) == IS_NULL)
960 #define Z_ISNULL_P(zval_p)			Z_ISNULL(*(zval_p))
961 
962 #define Z_ISERROR(zval)				(Z_TYPE(zval) == _IS_ERROR)
963 #define Z_ISERROR_P(zval_p)			Z_ISERROR(*(zval_p))
964 
965 #define Z_LVAL(zval)				(zval).value.lval
966 #define Z_LVAL_P(zval_p)			Z_LVAL(*(zval_p))
967 
968 #define Z_DVAL(zval)				(zval).value.dval
969 #define Z_DVAL_P(zval_p)			Z_DVAL(*(zval_p))
970 
971 #define Z_STR(zval)					(zval).value.str
972 #define Z_STR_P(zval_p)				Z_STR(*(zval_p))
973 
974 #define Z_STRVAL(zval)				ZSTR_VAL(Z_STR(zval))
975 #define Z_STRVAL_P(zval_p)			Z_STRVAL(*(zval_p))
976 
977 #define Z_STRLEN(zval)				ZSTR_LEN(Z_STR(zval))
978 #define Z_STRLEN_P(zval_p)			Z_STRLEN(*(zval_p))
979 
980 #define Z_STRHASH(zval)				ZSTR_HASH(Z_STR(zval))
981 #define Z_STRHASH_P(zval_p)			Z_STRHASH(*(zval_p))
982 
983 #define Z_ARR(zval)					(zval).value.arr
984 #define Z_ARR_P(zval_p)				Z_ARR(*(zval_p))
985 
986 #define Z_ARRVAL(zval)				Z_ARR(zval)
987 #define Z_ARRVAL_P(zval_p)			Z_ARRVAL(*(zval_p))
988 
989 #define Z_OBJ(zval)					(zval).value.obj
990 #define Z_OBJ_P(zval_p)				Z_OBJ(*(zval_p))
991 
992 #define Z_OBJ_HT(zval)				Z_OBJ(zval)->handlers
993 #define Z_OBJ_HT_P(zval_p)			Z_OBJ_HT(*(zval_p))
994 
995 #define Z_OBJ_HANDLER(zval, hf)		Z_OBJ_HT((zval))->hf
996 #define Z_OBJ_HANDLER_P(zv_p, hf)	Z_OBJ_HANDLER(*(zv_p), hf)
997 
998 #define Z_OBJ_HANDLE(zval)          (Z_OBJ((zval)))->handle
999 #define Z_OBJ_HANDLE_P(zval_p)      Z_OBJ_HANDLE(*(zval_p))
1000 
1001 #define Z_OBJCE(zval)				(Z_OBJ(zval)->ce)
1002 #define Z_OBJCE_P(zval_p)			Z_OBJCE(*(zval_p))
1003 
1004 #define Z_OBJPROP(zval)				Z_OBJ_HT((zval))->get_properties(Z_OBJ(zval))
1005 #define Z_OBJPROP_P(zval_p)			Z_OBJPROP(*(zval_p))
1006 
1007 #define Z_RES(zval)					(zval).value.res
1008 #define Z_RES_P(zval_p)				Z_RES(*zval_p)
1009 
1010 #define Z_RES_HANDLE(zval)			Z_RES(zval)->handle
1011 #define Z_RES_HANDLE_P(zval_p)		Z_RES_HANDLE(*zval_p)
1012 
1013 #define Z_RES_TYPE(zval)			Z_RES(zval)->type
1014 #define Z_RES_TYPE_P(zval_p)		Z_RES_TYPE(*zval_p)
1015 
1016 #define Z_RES_VAL(zval)				Z_RES(zval)->ptr
1017 #define Z_RES_VAL_P(zval_p)			Z_RES_VAL(*zval_p)
1018 
1019 #define Z_REF(zval)					(zval).value.ref
1020 #define Z_REF_P(zval_p)				Z_REF(*(zval_p))
1021 
1022 #define Z_REFVAL(zval)				&Z_REF(zval)->val
1023 #define Z_REFVAL_P(zval_p)			Z_REFVAL(*(zval_p))
1024 
1025 #define Z_AST(zval)					(zval).value.ast
1026 #define Z_AST_P(zval_p)				Z_AST(*(zval_p))
1027 
1028 #define GC_AST(p)					((zend_ast*)(((char*)p) + sizeof(zend_ast_ref)))
1029 
1030 #define Z_ASTVAL(zval)				GC_AST(Z_AST(zval))
1031 #define Z_ASTVAL_P(zval_p)			Z_ASTVAL(*(zval_p))
1032 
1033 #define Z_INDIRECT(zval)			(zval).value.zv
1034 #define Z_INDIRECT_P(zval_p)		Z_INDIRECT(*(zval_p))
1035 
1036 #define Z_CE(zval)					(zval).value.ce
1037 #define Z_CE_P(zval_p)				Z_CE(*(zval_p))
1038 
1039 #define Z_FUNC(zval)				(zval).value.func
1040 #define Z_FUNC_P(zval_p)			Z_FUNC(*(zval_p))
1041 
1042 #define Z_PTR(zval)					(zval).value.ptr
1043 #define Z_PTR_P(zval_p)				Z_PTR(*(zval_p))
1044 
1045 #define ZVAL_UNDEF(z) do {				\
1046 		Z_TYPE_INFO_P(z) = IS_UNDEF;	\
1047 	} while (0)
1048 
1049 #define ZVAL_NULL(z) do {				\
1050 		Z_TYPE_INFO_P(z) = IS_NULL;		\
1051 	} while (0)
1052 
1053 #define ZVAL_FALSE(z) do {				\
1054 		Z_TYPE_INFO_P(z) = IS_FALSE;	\
1055 	} while (0)
1056 
1057 #define ZVAL_TRUE(z) do {				\
1058 		Z_TYPE_INFO_P(z) = IS_TRUE;		\
1059 	} while (0)
1060 
1061 #define ZVAL_BOOL(z, b) do {			\
1062 		Z_TYPE_INFO_P(z) =				\
1063 			(b) ? IS_TRUE : IS_FALSE;	\
1064 	} while (0)
1065 
1066 #define ZVAL_LONG(z, l) do {			\
1067 		zval *__z = (z);				\
1068 		Z_LVAL_P(__z) = l;				\
1069 		Z_TYPE_INFO_P(__z) = IS_LONG;	\
1070 	} while (0)
1071 
1072 #define ZVAL_DOUBLE(z, d) do {			\
1073 		zval *__z = (z);				\
1074 		Z_DVAL_P(__z) = d;				\
1075 		Z_TYPE_INFO_P(__z) = IS_DOUBLE;	\
1076 	} while (0)
1077 
1078 #define ZVAL_STR(z, s) do {						\
1079 		zval *__z = (z);						\
1080 		zend_string *__s = (s);					\
1081 		Z_STR_P(__z) = __s;						\
1082 		/* interned strings support */			\
1083 		Z_TYPE_INFO_P(__z) = ZSTR_IS_INTERNED(__s) ? \
1084 			IS_INTERNED_STRING_EX : 			\
1085 			IS_STRING_EX;						\
1086 	} while (0)
1087 
1088 #define ZVAL_INTERNED_STR(z, s) do {				\
1089 		zval *__z = (z);							\
1090 		zend_string *__s = (s);						\
1091 		Z_STR_P(__z) = __s;							\
1092 		Z_TYPE_INFO_P(__z) = IS_INTERNED_STRING_EX;	\
1093 	} while (0)
1094 
1095 #define ZVAL_NEW_STR(z, s) do {					\
1096 		zval *__z = (z);						\
1097 		zend_string *__s = (s);					\
1098 		Z_STR_P(__z) = __s;						\
1099 		Z_TYPE_INFO_P(__z) = IS_STRING_EX;		\
1100 	} while (0)
1101 
1102 #define ZVAL_STR_COPY(z, s) do {						\
1103 		zval *__z = (z);								\
1104 		zend_string *__s = (s);							\
1105 		Z_STR_P(__z) = __s;								\
1106 		/* interned strings support */					\
1107 		if (ZSTR_IS_INTERNED(__s)) {					\
1108 			Z_TYPE_INFO_P(__z) = IS_INTERNED_STRING_EX;	\
1109 		} else {										\
1110 			GC_ADDREF(__s);								\
1111 			Z_TYPE_INFO_P(__z) = IS_STRING_EX;			\
1112 		}												\
1113 	} while (0)
1114 
1115 #define ZVAL_ARR(z, a) do {						\
1116 		zend_array *__arr = (a);				\
1117 		zval *__z = (z);						\
1118 		Z_ARR_P(__z) = __arr;					\
1119 		Z_TYPE_INFO_P(__z) = IS_ARRAY_EX;		\
1120 	} while (0)
1121 
1122 #define ZVAL_NEW_PERSISTENT_ARR(z) do {							\
1123 		zval *__z = (z);										\
1124 		zend_array *_arr =										\
1125 		(zend_array *) malloc(sizeof(zend_array));				\
1126 		Z_ARR_P(__z) = _arr;									\
1127 		Z_TYPE_INFO_P(__z) = IS_ARRAY_EX;						\
1128 	} while (0)
1129 
1130 #define ZVAL_OBJ(z, o) do {						\
1131 		zval *__z = (z);						\
1132 		Z_OBJ_P(__z) = (o);						\
1133 		Z_TYPE_INFO_P(__z) = IS_OBJECT_EX;		\
1134 	} while (0)
1135 
1136 #define ZVAL_OBJ_COPY(z, o) do {				\
1137 		zval *__z = (z);						\
1138 		zend_object *__o = (o);					\
1139 		GC_ADDREF(__o);							\
1140 		Z_OBJ_P(__z) = __o;						\
1141 		Z_TYPE_INFO_P(__z) = IS_OBJECT_EX;		\
1142 	} while (0)
1143 
1144 #define ZVAL_RES(z, r) do {						\
1145 		zval *__z = (z);						\
1146 		Z_RES_P(__z) = (r);						\
1147 		Z_TYPE_INFO_P(__z) = IS_RESOURCE_EX;	\
1148 	} while (0)
1149 
1150 #define ZVAL_NEW_RES(z, h, p, t) do {							\
1151 		zend_resource *_res =									\
1152 		(zend_resource *) emalloc(sizeof(zend_resource));		\
1153 		zval *__z;												\
1154 		GC_SET_REFCOUNT(_res, 1);								\
1155 		GC_TYPE_INFO(_res) = GC_RESOURCE;						\
1156 		_res->handle = (h);										\
1157 		_res->type = (t);										\
1158 		_res->ptr = (p);										\
1159 		__z = (z);												\
1160 		Z_RES_P(__z) = _res;									\
1161 		Z_TYPE_INFO_P(__z) = IS_RESOURCE_EX;					\
1162 	} while (0)
1163 
1164 #define ZVAL_NEW_PERSISTENT_RES(z, h, p, t) do {				\
1165 		zend_resource *_res =									\
1166 		(zend_resource *) malloc(sizeof(zend_resource));		\
1167 		zval *__z;												\
1168 		GC_SET_REFCOUNT(_res, 1);								\
1169 		GC_TYPE_INFO(_res) = GC_RESOURCE |						\
1170 			(GC_PERSISTENT << GC_FLAGS_SHIFT);					\
1171 		_res->handle = (h);										\
1172 		_res->type = (t);										\
1173 		_res->ptr = (p);										\
1174 		__z = (z);												\
1175 		Z_RES_P(__z) = _res;									\
1176 		Z_TYPE_INFO_P(__z) = IS_RESOURCE_EX;					\
1177 	} while (0)
1178 
1179 #define ZVAL_REF(z, r) do {										\
1180 		zval *__z = (z);										\
1181 		Z_REF_P(__z) = (r);										\
1182 		Z_TYPE_INFO_P(__z) = IS_REFERENCE_EX;					\
1183 	} while (0)
1184 
1185 #define ZVAL_NEW_EMPTY_REF(z) do {								\
1186 		zend_reference *_ref =									\
1187 		(zend_reference *) emalloc(sizeof(zend_reference));		\
1188 		GC_SET_REFCOUNT(_ref, 1);								\
1189 		GC_TYPE_INFO(_ref) = GC_REFERENCE;						\
1190 		_ref->sources.ptr = NULL;									\
1191 		Z_REF_P(z) = _ref;										\
1192 		Z_TYPE_INFO_P(z) = IS_REFERENCE_EX;						\
1193 	} while (0)
1194 
1195 #define ZVAL_NEW_REF(z, r) do {									\
1196 		zend_reference *_ref =									\
1197 		(zend_reference *) emalloc(sizeof(zend_reference));		\
1198 		GC_SET_REFCOUNT(_ref, 1);								\
1199 		GC_TYPE_INFO(_ref) = GC_REFERENCE;						\
1200 		ZVAL_COPY_VALUE(&_ref->val, r);							\
1201 		_ref->sources.ptr = NULL;									\
1202 		Z_REF_P(z) = _ref;										\
1203 		Z_TYPE_INFO_P(z) = IS_REFERENCE_EX;						\
1204 	} while (0)
1205 
1206 #define ZVAL_MAKE_REF_EX(z, refcount) do {						\
1207 		zval *_z = (z);											\
1208 		zend_reference *_ref =									\
1209 			(zend_reference *) emalloc(sizeof(zend_reference));	\
1210 		GC_SET_REFCOUNT(_ref, (refcount));						\
1211 		GC_TYPE_INFO(_ref) = GC_REFERENCE;						\
1212 		ZVAL_COPY_VALUE(&_ref->val, _z);						\
1213 		_ref->sources.ptr = NULL;									\
1214 		Z_REF_P(_z) = _ref;										\
1215 		Z_TYPE_INFO_P(_z) = IS_REFERENCE_EX;					\
1216 	} while (0)
1217 
1218 #define ZVAL_NEW_PERSISTENT_REF(z, r) do {						\
1219 		zend_reference *_ref =									\
1220 		(zend_reference *) malloc(sizeof(zend_reference));		\
1221 		GC_SET_REFCOUNT(_ref, 1);								\
1222 		GC_TYPE_INFO(_ref) = GC_REFERENCE |						\
1223 			(GC_PERSISTENT << GC_FLAGS_SHIFT);					\
1224 		ZVAL_COPY_VALUE(&_ref->val, r);							\
1225 		_ref->sources.ptr = NULL;									\
1226 		Z_REF_P(z) = _ref;										\
1227 		Z_TYPE_INFO_P(z) = IS_REFERENCE_EX;						\
1228 	} while (0)
1229 
1230 #define ZVAL_AST(z, ast) do {									\
1231 		zval *__z = (z);										\
1232 		Z_AST_P(__z) = ast;										\
1233 		Z_TYPE_INFO_P(__z) = IS_CONSTANT_AST_EX;				\
1234 	} while (0)
1235 
1236 #define ZVAL_INDIRECT(z, v) do {								\
1237 		Z_INDIRECT_P(z) = (v);									\
1238 		Z_TYPE_INFO_P(z) = IS_INDIRECT;							\
1239 	} while (0)
1240 
1241 #define ZVAL_PTR(z, p) do {										\
1242 		Z_PTR_P(z) = (p);										\
1243 		Z_TYPE_INFO_P(z) = IS_PTR;								\
1244 	} while (0)
1245 
1246 #define ZVAL_FUNC(z, f) do {									\
1247 		Z_FUNC_P(z) = (f);										\
1248 		Z_TYPE_INFO_P(z) = IS_PTR;								\
1249 	} while (0)
1250 
1251 #define ZVAL_CE(z, c) do {										\
1252 		Z_CE_P(z) = (c);										\
1253 		Z_TYPE_INFO_P(z) = IS_PTR;								\
1254 	} while (0)
1255 
1256 #define ZVAL_ALIAS_PTR(z, p) do {								\
1257 		Z_PTR_P(z) = (p);										\
1258 		Z_TYPE_INFO_P(z) = IS_ALIAS_PTR;						\
1259 	} while (0)
1260 
1261 #define ZVAL_ERROR(z) do {				\
1262 		Z_TYPE_INFO_P(z) = _IS_ERROR;	\
1263 	} while (0)
1264 
1265 #define Z_REFCOUNT_P(pz)			zval_refcount_p(pz)
1266 #define Z_SET_REFCOUNT_P(pz, rc)	zval_set_refcount_p(pz, rc)
1267 #define Z_ADDREF_P(pz)				zval_addref_p(pz)
1268 #define Z_DELREF_P(pz)				zval_delref_p(pz)
1269 
1270 #define Z_REFCOUNT(z)				Z_REFCOUNT_P(&(z))
1271 #define Z_SET_REFCOUNT(z, rc)		Z_SET_REFCOUNT_P(&(z), rc)
1272 #define Z_ADDREF(z)					Z_ADDREF_P(&(z))
1273 #define Z_DELREF(z)					Z_DELREF_P(&(z))
1274 
1275 #define Z_TRY_ADDREF_P(pz) do {		\
1276 	if (Z_REFCOUNTED_P((pz))) {		\
1277 		Z_ADDREF_P((pz));			\
1278 	}								\
1279 } while (0)
1280 
1281 #define Z_TRY_DELREF_P(pz) do {		\
1282 	if (Z_REFCOUNTED_P((pz))) {		\
1283 		Z_DELREF_P((pz));			\
1284 	}								\
1285 } while (0)
1286 
1287 #define Z_TRY_ADDREF(z)				Z_TRY_ADDREF_P(&(z))
1288 #define Z_TRY_DELREF(z)				Z_TRY_DELREF_P(&(z))
1289 
1290 #ifndef ZEND_RC_DEBUG
1291 # define ZEND_RC_DEBUG 0
1292 #endif
1293 
1294 #if ZEND_RC_DEBUG
1295 extern ZEND_API bool zend_rc_debug;
1296 /* The GC_PERSISTENT flag is reused for IS_OBJ_WEAKLY_REFERENCED on objects.
1297  * Skip checks for OBJECT/NULL type to avoid interpreting the flag incorrectly. */
1298 # define ZEND_RC_MOD_CHECK(p) do { \
1299 		if (zend_rc_debug) { \
1300 			uint8_t type = zval_gc_type((p)->u.type_info); \
1301 			if (type != IS_OBJECT && type != IS_NULL) { \
1302 				ZEND_ASSERT(!(zval_gc_flags((p)->u.type_info) & GC_IMMUTABLE)); \
1303 				ZEND_ASSERT((zval_gc_flags((p)->u.type_info) & (GC_PERSISTENT|GC_PERSISTENT_LOCAL)) != GC_PERSISTENT); \
1304 			} \
1305 		} \
1306 	} while (0)
1307 # define GC_MAKE_PERSISTENT_LOCAL(p) do { \
1308 		GC_ADD_FLAGS(p, GC_PERSISTENT_LOCAL); \
1309 	} while (0)
1310 #else
1311 # define ZEND_RC_MOD_CHECK(p) \
1312 	do { } while (0)
1313 # define GC_MAKE_PERSISTENT_LOCAL(p) \
1314 	do { } while (0)
1315 #endif
1316 
zend_gc_refcount(const zend_refcounted_h * p)1317 static zend_always_inline uint32_t zend_gc_refcount(const zend_refcounted_h *p) {
1318 	return p->refcount;
1319 }
1320 
zend_gc_set_refcount(zend_refcounted_h * p,uint32_t rc)1321 static zend_always_inline uint32_t zend_gc_set_refcount(zend_refcounted_h *p, uint32_t rc) {
1322 	p->refcount = rc;
1323 	return p->refcount;
1324 }
1325 
zend_gc_addref(zend_refcounted_h * p)1326 static zend_always_inline uint32_t zend_gc_addref(zend_refcounted_h *p) {
1327 	ZEND_RC_MOD_CHECK(p);
1328 	return ++(p->refcount);
1329 }
1330 
zend_gc_try_addref(zend_refcounted_h * p)1331 static zend_always_inline void zend_gc_try_addref(zend_refcounted_h *p) {
1332 	if (!(p->u.type_info & GC_IMMUTABLE)) {
1333 		ZEND_RC_MOD_CHECK(p);
1334 		++p->refcount;
1335 	}
1336 }
1337 
zend_gc_try_delref(zend_refcounted_h * p)1338 static zend_always_inline void zend_gc_try_delref(zend_refcounted_h *p) {
1339 	if (!(p->u.type_info & GC_IMMUTABLE)) {
1340 		ZEND_RC_MOD_CHECK(p);
1341 		--p->refcount;
1342 	}
1343 }
1344 
zend_gc_delref(zend_refcounted_h * p)1345 static zend_always_inline uint32_t zend_gc_delref(zend_refcounted_h *p) {
1346 	ZEND_ASSERT(p->refcount > 0);
1347 	ZEND_RC_MOD_CHECK(p);
1348 	return --(p->refcount);
1349 }
1350 
zend_gc_addref_ex(zend_refcounted_h * p,uint32_t rc)1351 static zend_always_inline uint32_t zend_gc_addref_ex(zend_refcounted_h *p, uint32_t rc) {
1352 	ZEND_RC_MOD_CHECK(p);
1353 	p->refcount += rc;
1354 	return p->refcount;
1355 }
1356 
zend_gc_delref_ex(zend_refcounted_h * p,uint32_t rc)1357 static zend_always_inline uint32_t zend_gc_delref_ex(zend_refcounted_h *p, uint32_t rc) {
1358 	ZEND_RC_MOD_CHECK(p);
1359 	p->refcount -= rc;
1360 	return p->refcount;
1361 }
1362 
zval_refcount_p(const zval * pz)1363 static zend_always_inline uint32_t zval_refcount_p(const zval* pz) {
1364 #if ZEND_DEBUG
1365 	ZEND_ASSERT(Z_REFCOUNTED_P(pz) || Z_TYPE_P(pz) == IS_ARRAY);
1366 #endif
1367 	return GC_REFCOUNT(Z_COUNTED_P(pz));
1368 }
1369 
zval_set_refcount_p(zval * pz,uint32_t rc)1370 static zend_always_inline uint32_t zval_set_refcount_p(zval* pz, uint32_t rc) {
1371 	ZEND_ASSERT(Z_REFCOUNTED_P(pz));
1372 	return GC_SET_REFCOUNT(Z_COUNTED_P(pz), rc);
1373 }
1374 
zval_addref_p(zval * pz)1375 static zend_always_inline uint32_t zval_addref_p(zval* pz) {
1376 	ZEND_ASSERT(Z_REFCOUNTED_P(pz));
1377 	return GC_ADDREF(Z_COUNTED_P(pz));
1378 }
1379 
zval_delref_p(zval * pz)1380 static zend_always_inline uint32_t zval_delref_p(zval* pz) {
1381 	ZEND_ASSERT(Z_REFCOUNTED_P(pz));
1382 	return GC_DELREF(Z_COUNTED_P(pz));
1383 }
1384 
1385 #if SIZEOF_SIZE_T == 4
1386 # define ZVAL_COPY_VALUE_EX(z, v, gc, t)				\
1387 	do {												\
1388 		uint32_t _w2 = v->value.ww.w2;					\
1389 		Z_COUNTED_P(z) = gc;							\
1390 		z->value.ww.w2 = _w2;							\
1391 		Z_TYPE_INFO_P(z) = t;							\
1392 	} while (0)
1393 #elif SIZEOF_SIZE_T == 8
1394 # define ZVAL_COPY_VALUE_EX(z, v, gc, t)				\
1395 	do {												\
1396 		Z_COUNTED_P(z) = gc;							\
1397 		Z_TYPE_INFO_P(z) = t;							\
1398 	} while (0)
1399 #else
1400 # error "Unknown SIZEOF_SIZE_T"
1401 #endif
1402 
1403 #define ZVAL_COPY_VALUE(z, v)							\
1404 	do {												\
1405 		zval *_z1 = (z);								\
1406 		const zval *_z2 = (v);							\
1407 		zend_refcounted *_gc = Z_COUNTED_P(_z2);		\
1408 		uint32_t _t = Z_TYPE_INFO_P(_z2);				\
1409 		ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);			\
1410 	} while (0)
1411 
1412 #define ZVAL_COPY(z, v)									\
1413 	do {												\
1414 		zval *_z1 = (z);								\
1415 		const zval *_z2 = (v);							\
1416 		zend_refcounted *_gc = Z_COUNTED_P(_z2);		\
1417 		uint32_t _t = Z_TYPE_INFO_P(_z2);				\
1418 		ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);			\
1419 		if (Z_TYPE_INFO_REFCOUNTED(_t)) {				\
1420 			GC_ADDREF(_gc);								\
1421 		}												\
1422 	} while (0)
1423 
1424 #define ZVAL_DUP(z, v)									\
1425 	do {												\
1426 		zval *_z1 = (z);								\
1427 		const zval *_z2 = (v);							\
1428 		zend_refcounted *_gc = Z_COUNTED_P(_z2);		\
1429 		uint32_t _t = Z_TYPE_INFO_P(_z2);				\
1430 		if ((_t & Z_TYPE_MASK) == IS_ARRAY) {			\
1431 			ZVAL_ARR(_z1, zend_array_dup((zend_array*)_gc));\
1432 		} else {										\
1433 			if (Z_TYPE_INFO_REFCOUNTED(_t)) {			\
1434 				GC_ADDREF(_gc);							\
1435 			}											\
1436 			ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);		\
1437 		}												\
1438 	} while (0)
1439 
1440 
1441 /* ZVAL_COPY_OR_DUP() should be used instead of ZVAL_COPY() and ZVAL_DUP()
1442  * in all places where the source may be a persistent zval.
1443  */
1444 #define ZVAL_COPY_OR_DUP(z, v)											\
1445 	do {																\
1446 		zval *_z1 = (z);												\
1447 		const zval *_z2 = (v);											\
1448 		zend_refcounted *_gc = Z_COUNTED_P(_z2);						\
1449 		uint32_t _t = Z_TYPE_INFO_P(_z2);								\
1450 		ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);							\
1451 		if (Z_TYPE_INFO_REFCOUNTED(_t)) {								\
1452 			/* Objects reuse PERSISTENT as WEAKLY_REFERENCED */			\
1453 			if (EXPECTED(!(GC_FLAGS(_gc) & GC_PERSISTENT)				\
1454 					|| GC_TYPE(_gc) == IS_OBJECT)) {					\
1455 				GC_ADDREF(_gc);											\
1456 			} else {													\
1457 				zval_copy_ctor_func(_z1);								\
1458 			}															\
1459 		}																\
1460 	} while (0)
1461 
1462 #define ZVAL_DEREF(z) do {								\
1463 		if (UNEXPECTED(Z_ISREF_P(z))) {					\
1464 			(z) = Z_REFVAL_P(z);						\
1465 		}												\
1466 	} while (0)
1467 
1468 #define ZVAL_DEINDIRECT(z) do {							\
1469 		if (Z_TYPE_P(z) == IS_INDIRECT) {				\
1470 			(z) = Z_INDIRECT_P(z);						\
1471 		}												\
1472 	} while (0)
1473 
1474 #define ZVAL_OPT_DEREF(z) do {							\
1475 		if (UNEXPECTED(Z_OPT_ISREF_P(z))) {				\
1476 			(z) = Z_REFVAL_P(z);						\
1477 		}												\
1478 	} while (0)
1479 
1480 #define ZVAL_MAKE_REF(zv) do {							\
1481 		zval *__zv = (zv);								\
1482 		if (!Z_ISREF_P(__zv)) {							\
1483 			ZVAL_NEW_REF(__zv, __zv);					\
1484 		}												\
1485 	} while (0)
1486 
1487 #define ZVAL_UNREF(z) do {								\
1488 		zval *_z = (z);									\
1489 		zend_reference *ref;							\
1490 		ZEND_ASSERT(Z_ISREF_P(_z));						\
1491 		ref = Z_REF_P(_z);								\
1492 		ZVAL_COPY_VALUE(_z, &ref->val);					\
1493 		efree_size(ref, sizeof(zend_reference));		\
1494 	} while (0)
1495 
1496 #define ZVAL_COPY_DEREF(z, v) do {						\
1497 		zval *_z3 = (v);								\
1498 		if (Z_OPT_REFCOUNTED_P(_z3)) {					\
1499 			if (UNEXPECTED(Z_OPT_ISREF_P(_z3))) {		\
1500 				_z3 = Z_REFVAL_P(_z3);					\
1501 				if (Z_OPT_REFCOUNTED_P(_z3)) {			\
1502 					Z_ADDREF_P(_z3);					\
1503 				}										\
1504 			} else {									\
1505 				Z_ADDREF_P(_z3);						\
1506 			}											\
1507 		}												\
1508 		ZVAL_COPY_VALUE(z, _z3);						\
1509 	} while (0)
1510 
1511 
1512 #define SEPARATE_STRING(zv) do {						\
1513 		zval *_zv = (zv);								\
1514 		if (Z_REFCOUNT_P(_zv) > 1) {					\
1515 			zend_string *_str = Z_STR_P(_zv);			\
1516 			ZEND_ASSERT(Z_REFCOUNTED_P(_zv));			\
1517 			ZEND_ASSERT(!ZSTR_IS_INTERNED(_str));		\
1518 			ZVAL_NEW_STR(_zv, zend_string_init(			\
1519 				ZSTR_VAL(_str),	ZSTR_LEN(_str), 0));	\
1520 			GC_DELREF(_str);							\
1521 		}												\
1522 	} while (0)
1523 
1524 #define SEPARATE_ARRAY(zv) do {							\
1525 		zval *__zv = (zv);								\
1526 		zend_array *_arr = Z_ARR_P(__zv);				\
1527 		if (UNEXPECTED(GC_REFCOUNT(_arr) > 1)) {		\
1528 			ZVAL_ARR(__zv, zend_array_dup(_arr));		\
1529 			GC_TRY_DELREF(_arr);						\
1530 		}												\
1531 	} while (0)
1532 
1533 #define SEPARATE_ZVAL_NOREF(zv) do {					\
1534 		zval *_zv = (zv);								\
1535 		ZEND_ASSERT(Z_TYPE_P(_zv) != IS_REFERENCE);		\
1536 		if (Z_TYPE_P(_zv) == IS_ARRAY) {				\
1537 			SEPARATE_ARRAY(_zv);						\
1538 		}												\
1539 	} while (0)
1540 
1541 #define SEPARATE_ZVAL(zv) do {							\
1542 		zval *_zv = (zv);								\
1543 		if (Z_ISREF_P(_zv)) {							\
1544 			zend_reference *_r = Z_REF_P(_zv);			\
1545 			ZVAL_COPY_VALUE(_zv, &_r->val);				\
1546 			if (GC_DELREF(_r) == 0) {					\
1547 				efree_size(_r, sizeof(zend_reference));	\
1548 			} else if (Z_OPT_TYPE_P(_zv) == IS_ARRAY) {	\
1549 				ZVAL_ARR(_zv, zend_array_dup(Z_ARR_P(_zv)));\
1550 				break;									\
1551 			} else if (Z_OPT_REFCOUNTED_P(_zv)) {		\
1552 				Z_ADDREF_P(_zv);						\
1553 				break;									\
1554 			}											\
1555 		}												\
1556 		if (Z_TYPE_P(_zv) == IS_ARRAY) {				\
1557 			SEPARATE_ARRAY(_zv);						\
1558 		}												\
1559 	} while (0)
1560 
1561 /* Properties store a flag distinguishing unset and uninitialized properties
1562  * (both use IS_UNDEF type) in the Z_EXTRA space. As such we also need to copy
1563  * the Z_EXTRA space when copying property default values etc. We define separate
1564  * macros for this purpose, so this workaround is easier to remove in the future. */
1565 #define IS_PROP_UNINIT (1<<0)
1566 #define IS_PROP_REINITABLE (1<<1)  /* It has impact only on readonly properties */
1567 #define IS_PROP_LAZY (1<<2)
1568 #define Z_PROP_FLAG_P(z) Z_EXTRA_P(z)
1569 #define ZVAL_COPY_VALUE_PROP(z, v) \
1570 	do { *(z) = *(v); } while (0)
1571 #define ZVAL_COPY_PROP(z, v) \
1572 	do { ZVAL_COPY(z, v); Z_PROP_FLAG_P(z) = Z_PROP_FLAG_P(v); } while (0)
1573 #define ZVAL_COPY_OR_DUP_PROP(z, v) \
1574 	do { ZVAL_COPY_OR_DUP(z, v); Z_PROP_FLAG_P(z) = Z_PROP_FLAG_P(v); } while (0)
1575 
1576 
zend_may_modify_arg_in_place(const zval * arg)1577 static zend_always_inline bool zend_may_modify_arg_in_place(const zval *arg)
1578 {
1579 	return Z_REFCOUNTED_P(arg) && !(GC_FLAGS(Z_COUNTED_P(arg)) & (GC_IMMUTABLE | GC_PERSISTENT)) && Z_REFCOUNT_P(arg) == 1;
1580 }
1581 
1582 #endif /* ZEND_TYPES_H */
1583