1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Dmitry Stogov <dmitry@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #ifndef ZEND_OPERATORS_H
22 #define ZEND_OPERATORS_H
23
24 #include <errno.h>
25 #include <math.h>
26 #include <assert.h>
27 #include <stddef.h>
28
29 #ifdef HAVE_IEEEFP_H
30 #include <ieeefp.h>
31 #endif
32
33 #include "zend_portability.h"
34 #include "zend_strtod.h"
35 #include "zend_multiply.h"
36 #include "zend_object_handlers.h"
37
38 #define LONG_SIGN_MASK ZEND_LONG_MIN
39
40 BEGIN_EXTERN_C()
41 ZEND_API int ZEND_FASTCALL add_function(zval *result, zval *op1, zval *op2);
42 ZEND_API int ZEND_FASTCALL sub_function(zval *result, zval *op1, zval *op2);
43 ZEND_API int ZEND_FASTCALL mul_function(zval *result, zval *op1, zval *op2);
44 ZEND_API int ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2);
45 ZEND_API int ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2);
46 ZEND_API int ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2);
47 ZEND_API int ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2);
48 ZEND_API int ZEND_FASTCALL boolean_not_function(zval *result, zval *op1);
49 ZEND_API int ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1);
50 ZEND_API int ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2);
51 ZEND_API int ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2);
52 ZEND_API int ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2);
53 ZEND_API int ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2);
54 ZEND_API int ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2);
55 ZEND_API int ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2);
56
57 ZEND_API zend_bool ZEND_FASTCALL zend_is_identical(zval *op1, zval *op2);
58
59 ZEND_API int ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2);
60 ZEND_API int ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2);
61 ZEND_API int ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2);
62 ZEND_API int ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2);
63 ZEND_API int ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2);
64 ZEND_API int ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2);
65
66 ZEND_API zend_bool ZEND_FASTCALL instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool is_interface);
67 ZEND_API zend_bool ZEND_FASTCALL instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce);
68
69 /**
70 * Checks whether the string "str" with length "length" is numeric. The value
71 * of allow_errors determines whether it's required to be entirely numeric, or
72 * just its prefix. Leading whitespace is allowed.
73 *
74 * The function returns 0 if the string did not contain a valid number; IS_LONG
75 * if it contained a number that fits within the range of a long; or IS_DOUBLE
76 * if the number was out of long range or contained a decimal point/exponent.
77 * The number's value is returned into the respective pointer, *lval or *dval,
78 * if that pointer is not NULL.
79 *
80 * This variant also gives information if a string that represents an integer
81 * could not be represented as such due to overflow. It writes 1 to oflow_info
82 * if the integer is larger than ZEND_LONG_MAX and -1 if it's smaller than ZEND_LONG_MIN.
83 */
84 ZEND_API zend_uchar ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info);
85
86 ZEND_API const char* ZEND_FASTCALL zend_memnstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end);
87 ZEND_API const char* ZEND_FASTCALL zend_memnrstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end);
88
89 #if SIZEOF_ZEND_LONG == 4
90 # define ZEND_DOUBLE_FITS_LONG(d) (!((d) > (double)ZEND_LONG_MAX || (d) < (double)ZEND_LONG_MIN))
91 #else
92 /* >= as (double)ZEND_LONG_MAX is outside signed range */
93 # define ZEND_DOUBLE_FITS_LONG(d) (!((d) >= (double)ZEND_LONG_MAX || (d) < (double)ZEND_LONG_MIN))
94 #endif
95
96 #if ZEND_DVAL_TO_LVAL_CAST_OK
zend_dval_to_lval(double d)97 static zend_always_inline zend_long zend_dval_to_lval(double d)
98 {
99 if (EXPECTED(zend_finite(d)) && EXPECTED(!zend_isnan(d))) {
100 return (zend_long)d;
101 } else {
102 return 0;
103 }
104 }
105 #else
106 ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d);
107
zend_dval_to_lval(double d)108 static zend_always_inline zend_long zend_dval_to_lval(double d)
109 {
110 if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
111 return 0;
112 } else if (!ZEND_DOUBLE_FITS_LONG(d)) {
113 return zend_dval_to_lval_slow(d);
114 }
115 return (zend_long)d;
116 }
117 #endif
118
zend_dval_to_lval_cap(double d)119 static zend_always_inline zend_long zend_dval_to_lval_cap(double d)
120 {
121 if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
122 return 0;
123 } else if (!ZEND_DOUBLE_FITS_LONG(d)) {
124 return (d > 0 ? ZEND_LONG_MAX : ZEND_LONG_MIN);
125 }
126 return (zend_long)d;
127 }
128 /* }}} */
129
130 #define ZEND_IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
131 #define ZEND_IS_XDIGIT(c) (((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
132
is_numeric_string_ex(const char * str,size_t length,zend_long * lval,double * dval,int allow_errors,int * oflow_info)133 static zend_always_inline zend_uchar is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info)
134 {
135 if (*str > '9') {
136 return 0;
137 }
138 return _is_numeric_string_ex(str, length, lval, dval, allow_errors, oflow_info);
139 }
140
is_numeric_string(const char * str,size_t length,zend_long * lval,double * dval,int allow_errors)141 static zend_always_inline zend_uchar is_numeric_string(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors) {
142 return is_numeric_string_ex(str, length, lval, dval, allow_errors, NULL);
143 }
144
145 ZEND_API zend_uchar ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval);
146
147 static zend_always_inline const char *
zend_memnstr(const char * haystack,const char * needle,size_t needle_len,const char * end)148 zend_memnstr(const char *haystack, const char *needle, size_t needle_len, const char *end)
149 {
150 const char *p = haystack;
151 const char ne = needle[needle_len-1];
152 ptrdiff_t off_p;
153 size_t off_s;
154
155 if (needle_len == 1) {
156 return (const char *)memchr(p, *needle, (end-p));
157 }
158
159 off_p = end - haystack;
160 off_s = (off_p > 0) ? (size_t)off_p : 0;
161
162 if (needle_len > off_s) {
163 return NULL;
164 }
165
166 if (EXPECTED(off_s < 1024 || needle_len < 9)) { /* glibc memchr is faster when needle is too short */
167 end -= needle_len;
168
169 while (p <= end) {
170 if ((p = (const char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
171 if (!memcmp(needle+1, p+1, needle_len-2)) {
172 return p;
173 }
174 }
175
176 if (p == NULL) {
177 return NULL;
178 }
179
180 p++;
181 }
182
183 return NULL;
184 } else {
185 return zend_memnstr_ex(haystack, needle, needle_len, end);
186 }
187 }
188
zend_memrchr(const void * s,int c,size_t n)189 static zend_always_inline const void *zend_memrchr(const void *s, int c, size_t n)
190 {
191 const unsigned char *e;
192 if (0 == n) {
193 return NULL;
194 }
195
196 for (e = (const unsigned char *)s + n - 1; e >= (const unsigned char *)s; e--) {
197 if (*e == (const unsigned char)c) {
198 return (const void *)e;
199 }
200 }
201 return NULL;
202 }
203
204
205 static zend_always_inline const char *
zend_memnrstr(const char * haystack,const char * needle,size_t needle_len,const char * end)206 zend_memnrstr(const char *haystack, const char *needle, size_t needle_len, const char *end)
207 {
208 const char *p = end;
209 const char ne = needle[needle_len-1];
210 ptrdiff_t off_p;
211 size_t off_s;
212
213 if (needle_len == 1) {
214 return (const char *)zend_memrchr(haystack, *needle, (p - haystack));
215 }
216
217 off_p = end - haystack;
218 off_s = (off_p > 0) ? (size_t)off_p : 0;
219
220 if (needle_len > off_s) {
221 return NULL;
222 }
223
224 if (EXPECTED(off_s < 1024 || needle_len < 3)) {
225 p -= needle_len;
226
227 do {
228 p = (const char *)zend_memrchr(haystack, *needle, (p - haystack) + 1);
229 if (!p) {
230 return NULL;
231 }
232 if (ne == p[needle_len-1] && !memcmp(needle + 1, p + 1, needle_len - 2)) {
233 return p;
234 }
235 } while (p-- >= haystack);
236
237 return NULL;
238 } else {
239 return zend_memnrstr_ex(haystack, needle, needle_len, end);
240 }
241 }
242
243 ZEND_API int ZEND_FASTCALL increment_function(zval *op1);
244 ZEND_API int ZEND_FASTCALL decrement_function(zval *op2);
245
246 ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op);
247 ZEND_API void ZEND_FASTCALL _convert_to_cstring(zval *op);
248 ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op);
249 ZEND_API void ZEND_FASTCALL convert_to_long(zval *op);
250 ZEND_API void ZEND_FASTCALL convert_to_double(zval *op);
251 ZEND_API void ZEND_FASTCALL convert_to_long_base(zval *op, int base);
252 ZEND_API void ZEND_FASTCALL convert_to_null(zval *op);
253 ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op);
254 ZEND_API void ZEND_FASTCALL convert_to_array(zval *op);
255 ZEND_API void ZEND_FASTCALL convert_to_object(zval *op);
256 ZEND_API void multi_convert_to_long_ex(int argc, ...);
257 ZEND_API void multi_convert_to_double_ex(int argc, ...);
258 ZEND_API void multi_convert_to_string_ex(int argc, ...);
259
260 ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(zval *op);
261 ZEND_API double ZEND_FASTCALL zval_get_double_func(zval *op);
262 ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op);
263 ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op);
264
zval_get_long(zval * op)265 static zend_always_inline zend_long zval_get_long(zval *op) {
266 return EXPECTED(Z_TYPE_P(op) == IS_LONG) ? Z_LVAL_P(op) : zval_get_long_func(op);
267 }
zval_get_double(zval * op)268 static zend_always_inline double zval_get_double(zval *op) {
269 return EXPECTED(Z_TYPE_P(op) == IS_DOUBLE) ? Z_DVAL_P(op) : zval_get_double_func(op);
270 }
zval_get_string(zval * op)271 static zend_always_inline zend_string *zval_get_string(zval *op) {
272 return EXPECTED(Z_TYPE_P(op) == IS_STRING) ? zend_string_copy(Z_STR_P(op)) : zval_get_string_func(op);
273 }
274
zval_get_tmp_string(zval * op,zend_string ** tmp)275 static zend_always_inline zend_string *zval_get_tmp_string(zval *op, zend_string **tmp) {
276 if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
277 *tmp = NULL;
278 return Z_STR_P(op);
279 } else {
280 return *tmp = zval_get_string_func(op);
281 }
282 }
zend_tmp_string_release(zend_string * tmp)283 static zend_always_inline void zend_tmp_string_release(zend_string *tmp) {
284 if (UNEXPECTED(tmp)) {
285 zend_string_release_ex(tmp, 0);
286 }
287 }
288
289 /* Like zval_get_string, but returns NULL if the conversion fails with an exception. */
zval_try_get_string(zval * op)290 static zend_always_inline zend_string *zval_try_get_string(zval *op) {
291 if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
292 zend_string *ret = zend_string_copy(Z_STR_P(op));
293 ZEND_ASSUME(ret != NULL);
294 return ret;
295 } else {
296 return zval_try_get_string_func(op);
297 }
298 }
299
300 /* Like zval_get_tmp_string, but returns NULL if the conversion fails with an exception. */
zval_try_get_tmp_string(zval * op,zend_string ** tmp)301 static zend_always_inline zend_string *zval_try_get_tmp_string(zval *op, zend_string **tmp) {
302 if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
303 zend_string *ret = Z_STR_P(op);
304 *tmp = NULL;
305 ZEND_ASSUME(ret != NULL);
306 return ret;
307 } else {
308 return *tmp = zval_try_get_string_func(op);
309 }
310 }
311
312 /* Like convert_to_string(), but returns whether the conversion succeeded and does not modify the
313 * zval in-place if it fails. */
314 ZEND_API zend_bool ZEND_FASTCALL _try_convert_to_string(zval *op);
try_convert_to_string(zval * op)315 static zend_always_inline zend_bool try_convert_to_string(zval *op) {
316 if (Z_TYPE_P(op) == IS_STRING) {
317 return 1;
318 }
319 return _try_convert_to_string(op);
320 }
321
322 /* Compatibility macros for 7.2 and below */
323 #define _zval_get_long(op) zval_get_long(op)
324 #define _zval_get_double(op) zval_get_double(op)
325 #define _zval_get_string(op) zval_get_string(op)
326 #define _zval_get_long_func(op) zval_get_long_func(op)
327 #define _zval_get_double_func(op) zval_get_double_func(op)
328 #define _zval_get_string_func(op) zval_get_string_func(op)
329
330 #define convert_to_cstring(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_cstring((op)); }
331 #define convert_to_string(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_string((op)); }
332
333
334 ZEND_API int ZEND_FASTCALL zend_is_true(zval *op);
335 ZEND_API int ZEND_FASTCALL zend_object_is_true(zval *op);
336
337 #define zval_is_true(op) \
338 zend_is_true(op)
339
i_zend_is_true(zval * op)340 static zend_always_inline int i_zend_is_true(zval *op)
341 {
342 int result = 0;
343
344 again:
345 switch (Z_TYPE_P(op)) {
346 case IS_TRUE:
347 result = 1;
348 break;
349 case IS_LONG:
350 if (Z_LVAL_P(op)) {
351 result = 1;
352 }
353 break;
354 case IS_DOUBLE:
355 if (Z_DVAL_P(op)) {
356 result = 1;
357 }
358 break;
359 case IS_STRING:
360 if (Z_STRLEN_P(op) > 1 || (Z_STRLEN_P(op) && Z_STRVAL_P(op)[0] != '0')) {
361 result = 1;
362 }
363 break;
364 case IS_ARRAY:
365 if (zend_hash_num_elements(Z_ARRVAL_P(op))) {
366 result = 1;
367 }
368 break;
369 case IS_OBJECT:
370 if (EXPECTED(Z_OBJ_HT_P(op)->cast_object == zend_std_cast_object_tostring)) {
371 result = 1;
372 } else {
373 result = zend_object_is_true(op);
374 }
375 break;
376 case IS_RESOURCE:
377 if (EXPECTED(Z_RES_HANDLE_P(op))) {
378 result = 1;
379 }
380 break;
381 case IS_REFERENCE:
382 op = Z_REFVAL_P(op);
383 goto again;
384 break;
385 default:
386 break;
387 }
388 return result;
389 }
390
391 ZEND_API int ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2);
392
393 ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2);
394 ZEND_API int ZEND_FASTCALL string_compare_function_ex(zval *op1, zval *op2, zend_bool case_insensitive);
395 ZEND_API int ZEND_FASTCALL string_compare_function(zval *op1, zval *op2);
396 ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2);
397 ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2);
398
399 ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length);
400 ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length);
401 ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length);
402 ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t length);
403 ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower_ex(zend_string *str, int persistent);
404
405 #define zend_string_tolower(str) zend_string_tolower_ex(str, 0)
406
407 ZEND_API int ZEND_FASTCALL zend_binary_zval_strcmp(zval *s1, zval *s2);
408 ZEND_API int ZEND_FASTCALL zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3);
409 ZEND_API int ZEND_FASTCALL zend_binary_zval_strcasecmp(zval *s1, zval *s2);
410 ZEND_API int ZEND_FASTCALL zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3);
411 ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2);
412 ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
413 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2);
414 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
415 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2);
416 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
417
418 ZEND_API int ZEND_FASTCALL zendi_smart_streq(zend_string *s1, zend_string *s2);
419 ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2);
420 ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2);
421 ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2);
422 ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2);
423
424 ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, size_t str_len);
425 ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, size_t str_len);
426
427 ZEND_API void ZEND_FASTCALL zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC);
428
429 #define convert_to_ex_master(pzv, lower_type, upper_type) \
430 if (Z_TYPE_P(pzv)!=upper_type) { \
431 convert_to_##lower_type(pzv); \
432 }
433
434 #define convert_to_explicit_type(pzv, type) \
435 do { \
436 switch (type) { \
437 case IS_NULL: \
438 convert_to_null(pzv); \
439 break; \
440 case IS_LONG: \
441 convert_to_long(pzv); \
442 break; \
443 case IS_DOUBLE: \
444 convert_to_double(pzv); \
445 break; \
446 case _IS_BOOL: \
447 convert_to_boolean(pzv); \
448 break; \
449 case IS_ARRAY: \
450 convert_to_array(pzv); \
451 break; \
452 case IS_OBJECT: \
453 convert_to_object(pzv); \
454 break; \
455 case IS_STRING: \
456 convert_to_string(pzv); \
457 break; \
458 default: \
459 assert(0); \
460 break; \
461 } \
462 } while (0);
463
464 #define convert_to_explicit_type_ex(pzv, str_type) \
465 if (Z_TYPE_P(pzv) != str_type) { \
466 convert_to_explicit_type(pzv, str_type); \
467 }
468
469 #define convert_to_boolean_ex(pzv) do { \
470 if (Z_TYPE_INFO_P(pzv) > IS_TRUE) { \
471 convert_to_boolean(pzv); \
472 } else if (Z_TYPE_INFO_P(pzv) < IS_FALSE) { \
473 ZVAL_FALSE(pzv); \
474 } \
475 } while (0)
476 #define convert_to_long_ex(pzv) convert_to_ex_master(pzv, long, IS_LONG)
477 #define convert_to_double_ex(pzv) convert_to_ex_master(pzv, double, IS_DOUBLE)
478 #define convert_to_string_ex(pzv) convert_to_ex_master(pzv, string, IS_STRING)
479 #define convert_to_array_ex(pzv) convert_to_ex_master(pzv, array, IS_ARRAY)
480 #define convert_to_object_ex(pzv) convert_to_ex_master(pzv, object, IS_OBJECT)
481 #define convert_to_null_ex(pzv) convert_to_ex_master(pzv, null, IS_NULL)
482
483 #define convert_scalar_to_number_ex(pzv) \
484 if (Z_TYPE_P(pzv)!=IS_LONG && Z_TYPE_P(pzv)!=IS_DOUBLE) { \
485 convert_scalar_to_number(pzv); \
486 }
487
488 #if defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER)
489 /* This performance improvement of tolower() on Windows gives 10-18% on bench.php */
490 #define ZEND_USE_TOLOWER_L 1
491 #endif
492
493 #ifdef ZEND_USE_TOLOWER_L
494 ZEND_API void zend_update_current_locale(void);
495 #else
496 #define zend_update_current_locale()
497 #endif
498
499 /* The offset in bytes between the value and type fields of a zval */
500 #define ZVAL_OFFSETOF_TYPE \
501 (offsetof(zval, u1.type_info) - offsetof(zval, value))
502
503 #if defined(HAVE_ASM_GOTO) && !__has_feature(memory_sanitizer)
504 # define ZEND_USE_ASM_ARITHMETIC 1
505 #else
506 # define ZEND_USE_ASM_ARITHMETIC 0
507 #endif
508
fast_long_increment_function(zval * op1)509 static zend_always_inline void fast_long_increment_function(zval *op1)
510 {
511 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
512 __asm__ goto(
513 "addl $1,(%0)\n\t"
514 "jo %l1\n"
515 :
516 : "r"(&op1->value)
517 : "cc", "memory"
518 : overflow);
519 return;
520 overflow: ZEND_ATTRIBUTE_COLD_LABEL
521 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
522 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
523 __asm__ goto(
524 "addq $1,(%0)\n\t"
525 "jo %l1\n"
526 :
527 : "r"(&op1->value)
528 : "cc", "memory"
529 : overflow);
530 return;
531 overflow: ZEND_ATTRIBUTE_COLD_LABEL
532 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
533 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
534 __asm__ goto (
535 "ldr x5, [%0]\n\t"
536 "adds x5, x5, 1\n\t"
537 "bvs %l1\n"
538 "str x5, [%0]"
539 :
540 : "r"(&op1->value)
541 : "x5", "cc", "memory"
542 : overflow);
543 return;
544 overflow: ZEND_ATTRIBUTE_COLD_LABEL
545 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
546 #elif PHP_HAVE_BUILTIN_SADDL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
547 long lresult;
548 if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), 1, &lresult))) {
549 /* switch to double */
550 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
551 } else {
552 Z_LVAL_P(op1) = lresult;
553 }
554 #elif PHP_HAVE_BUILTIN_SADDLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
555 long long llresult;
556 if (UNEXPECTED(__builtin_saddll_overflow(Z_LVAL_P(op1), 1, &llresult))) {
557 /* switch to double */
558 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
559 } else {
560 Z_LVAL_P(op1) = llresult;
561 }
562 #else
563 if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MAX)) {
564 /* switch to double */
565 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
566 } else {
567 Z_LVAL_P(op1)++;
568 }
569 #endif
570 }
571
fast_long_decrement_function(zval * op1)572 static zend_always_inline void fast_long_decrement_function(zval *op1)
573 {
574 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
575 __asm__ goto(
576 "subl $1,(%0)\n\t"
577 "jo %l1\n"
578 :
579 : "r"(&op1->value)
580 : "cc", "memory"
581 : overflow);
582 return;
583 overflow: ZEND_ATTRIBUTE_COLD_LABEL
584 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
585 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
586 __asm__ goto(
587 "subq $1,(%0)\n\t"
588 "jo %l1\n"
589 :
590 : "r"(&op1->value)
591 : "cc", "memory"
592 : overflow);
593 return;
594 overflow: ZEND_ATTRIBUTE_COLD_LABEL
595 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
596 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
597 __asm__ goto (
598 "ldr x5, [%0]\n\t"
599 "subs x5 ,x5, 1\n\t"
600 "bvs %l1\n"
601 "str x5, [%0]"
602 :
603 : "r"(&op1->value)
604 : "x5", "cc", "memory"
605 : overflow);
606 return;
607 overflow: ZEND_ATTRIBUTE_COLD_LABEL
608 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
609 #elif PHP_HAVE_BUILTIN_SSUBL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
610 long lresult;
611 if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), 1, &lresult))) {
612 /* switch to double */
613 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
614 } else {
615 Z_LVAL_P(op1) = lresult;
616 }
617 #elif PHP_HAVE_BUILTIN_SSUBLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
618 long long llresult;
619 if (UNEXPECTED(__builtin_ssubll_overflow(Z_LVAL_P(op1), 1, &llresult))) {
620 /* switch to double */
621 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
622 } else {
623 Z_LVAL_P(op1) = llresult;
624 }
625 #else
626 if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MIN)) {
627 /* switch to double */
628 ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
629 } else {
630 Z_LVAL_P(op1)--;
631 }
632 #endif
633 }
634
fast_long_add_function(zval * result,zval * op1,zval * op2)635 static zend_always_inline void fast_long_add_function(zval *result, zval *op1, zval *op2)
636 {
637 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
638 __asm__ goto(
639 "movl (%1), %%eax\n\t"
640 "addl (%2), %%eax\n\t"
641 "jo %l5\n\t"
642 "movl %%eax, (%0)\n\t"
643 "movl %3, %c4(%0)\n"
644 :
645 : "r"(&result->value),
646 "r"(&op1->value),
647 "r"(&op2->value),
648 "n"(IS_LONG),
649 "n"(ZVAL_OFFSETOF_TYPE)
650 : "eax","cc", "memory"
651 : overflow);
652 return;
653 overflow: ZEND_ATTRIBUTE_COLD_LABEL
654 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
655 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
656 __asm__ goto(
657 "movq (%1), %%rax\n\t"
658 "addq (%2), %%rax\n\t"
659 "jo %l5\n\t"
660 "movq %%rax, (%0)\n\t"
661 "movl %3, %c4(%0)\n"
662 :
663 : "r"(&result->value),
664 "r"(&op1->value),
665 "r"(&op2->value),
666 "n"(IS_LONG),
667 "n"(ZVAL_OFFSETOF_TYPE)
668 : "rax","cc", "memory"
669 : overflow);
670 return;
671 overflow: ZEND_ATTRIBUTE_COLD_LABEL
672 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
673 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
674 __asm__ goto(
675 "ldr x5, [%1]\n\t"
676 "ldr x6, [%2]\n\t"
677 "adds x5, x5, x6\n\t"
678 "bvs %l5\n\t"
679 "mov w6, %3\n\t"
680 "str x5, [%0]\n\t"
681 "str w6, [%0, %c4]\n"
682 :
683 : "r"(&result->value),
684 "r"(&op1->value),
685 "r"(&op2->value),
686 "n"(IS_LONG),
687 "n"(ZVAL_OFFSETOF_TYPE)
688 : "x5", "x6", "cc", "memory"
689 : overflow);
690 return;
691 overflow: ZEND_ATTRIBUTE_COLD_LABEL
692 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
693 #elif PHP_HAVE_BUILTIN_SADDL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
694 long lresult;
695 if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) {
696 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
697 } else {
698 ZVAL_LONG(result, lresult);
699 }
700 #elif PHP_HAVE_BUILTIN_SADDLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
701 long long llresult;
702 if (UNEXPECTED(__builtin_saddll_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) {
703 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
704 } else {
705 ZVAL_LONG(result, llresult);
706 }
707 #else
708 /*
709 * 'result' may alias with op1 or op2, so we need to
710 * ensure that 'result' is not updated until after we
711 * have read the values of op1 and op2.
712 */
713
714 if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK)
715 && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != ((Z_LVAL_P(op1) + Z_LVAL_P(op2)) & LONG_SIGN_MASK))) {
716 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
717 } else {
718 ZVAL_LONG(result, Z_LVAL_P(op1) + Z_LVAL_P(op2));
719 }
720 #endif
721 }
722
fast_add_function(zval * result,zval * op1,zval * op2)723 static zend_always_inline int fast_add_function(zval *result, zval *op1, zval *op2)
724 {
725 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
726 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
727 fast_long_add_function(result, op1, op2);
728 return SUCCESS;
729 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
730 ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
731 return SUCCESS;
732 }
733 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
734 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
735 ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
736 return SUCCESS;
737 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
738 ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
739 return SUCCESS;
740 }
741 }
742 return add_function(result, op1, op2);
743 }
744
fast_long_sub_function(zval * result,zval * op1,zval * op2)745 static zend_always_inline void fast_long_sub_function(zval *result, zval *op1, zval *op2)
746 {
747 #if ZEND_USE_ASM_ARITHMETIC && defined(__i386__) && !(4 == __GNUC__ && 8 == __GNUC_MINOR__)
748 __asm__ goto(
749 "movl (%1), %%eax\n\t"
750 "subl (%2), %%eax\n\t"
751 "jo %l5\n\t"
752 "movl %%eax, (%0)\n\t"
753 "movl %3, %c4(%0)\n"
754 :
755 : "r"(&result->value),
756 "r"(&op1->value),
757 "r"(&op2->value),
758 "n"(IS_LONG),
759 "n"(ZVAL_OFFSETOF_TYPE)
760 : "eax","cc", "memory"
761 : overflow);
762 return;
763 overflow: ZEND_ATTRIBUTE_COLD_LABEL
764 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
765 #elif ZEND_USE_ASM_ARITHMETIC && defined(__x86_64__)
766 __asm__ goto(
767 "movq (%1), %%rax\n\t"
768 "subq (%2), %%rax\n\t"
769 "jo %l5\n\t"
770 "movq %%rax, (%0)\n\t"
771 "movl %3, %c4(%0)\n"
772 :
773 : "r"(&result->value),
774 "r"(&op1->value),
775 "r"(&op2->value),
776 "n"(IS_LONG),
777 "n"(ZVAL_OFFSETOF_TYPE)
778 : "rax","cc", "memory"
779 : overflow);
780 return;
781 overflow: ZEND_ATTRIBUTE_COLD_LABEL
782 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
783 #elif ZEND_USE_ASM_ARITHMETIC && defined(__aarch64__)
784 __asm__ goto(
785 "ldr x5, [%1]\n\t"
786 "ldr x6, [%2]\n\t"
787 "subs x5, x5, x6\n\t"
788 "bvs %l5\n\t"
789 "mov w6, %3\n\t"
790 "str x5, [%0]\n\t"
791 "str w6, [%0, %c4]\n"
792 :
793 : "r"(&result->value),
794 "r"(&op1->value),
795 "r"(&op2->value),
796 "n"(IS_LONG),
797 "n"(ZVAL_OFFSETOF_TYPE)
798 : "x5", "x6", "cc", "memory"
799 : overflow);
800 return;
801 overflow: ZEND_ATTRIBUTE_COLD_LABEL
802 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
803 #elif PHP_HAVE_BUILTIN_SSUBL_OVERFLOW && SIZEOF_LONG == SIZEOF_ZEND_LONG
804 long lresult;
805 if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &lresult))) {
806 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
807 } else {
808 ZVAL_LONG(result, lresult);
809 }
810 #elif PHP_HAVE_BUILTIN_SSUBLL_OVERFLOW && SIZEOF_LONG_LONG == SIZEOF_ZEND_LONG
811 long long llresult;
812 if (UNEXPECTED(__builtin_ssubll_overflow(Z_LVAL_P(op1), Z_LVAL_P(op2), &llresult))) {
813 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
814 } else {
815 ZVAL_LONG(result, llresult);
816 }
817 #else
818 ZVAL_LONG(result, Z_LVAL_P(op1) - Z_LVAL_P(op2));
819
820 if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK)
821 && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(result) & LONG_SIGN_MASK))) {
822 ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
823 }
824 #endif
825 }
826
fast_div_function(zval * result,zval * op1,zval * op2)827 static zend_always_inline int fast_div_function(zval *result, zval *op1, zval *op2)
828 {
829 return div_function(result, op1, op2);
830 }
831
zend_fast_equal_strings(zend_string * s1,zend_string * s2)832 static zend_always_inline int zend_fast_equal_strings(zend_string *s1, zend_string *s2)
833 {
834 if (s1 == s2) {
835 return 1;
836 } else if (ZSTR_VAL(s1)[0] > '9' || ZSTR_VAL(s2)[0] > '9') {
837 return zend_string_equal_content(s1, s2);
838 } else {
839 return zendi_smart_streq(s1, s2);
840 }
841 }
842
fast_equal_check_function(zval * op1,zval * op2)843 static zend_always_inline int fast_equal_check_function(zval *op1, zval *op2)
844 {
845 zval result;
846 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
847 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
848 return Z_LVAL_P(op1) == Z_LVAL_P(op2);
849 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
850 return ((double)Z_LVAL_P(op1)) == Z_DVAL_P(op2);
851 }
852 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
853 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
854 return Z_DVAL_P(op1) == Z_DVAL_P(op2);
855 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
856 return Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2));
857 }
858 } else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
859 if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
860 return zend_fast_equal_strings(Z_STR_P(op1), Z_STR_P(op2));
861 }
862 }
863 compare_function(&result, op1, op2);
864 return Z_LVAL(result) == 0;
865 }
866
fast_equal_check_long(zval * op1,zval * op2)867 static zend_always_inline int fast_equal_check_long(zval *op1, zval *op2)
868 {
869 zval result;
870 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
871 return Z_LVAL_P(op1) == Z_LVAL_P(op2);
872 }
873 compare_function(&result, op1, op2);
874 return Z_LVAL(result) == 0;
875 }
876
fast_equal_check_string(zval * op1,zval * op2)877 static zend_always_inline int fast_equal_check_string(zval *op1, zval *op2)
878 {
879 zval result;
880 if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
881 return zend_fast_equal_strings(Z_STR_P(op1), Z_STR_P(op2));
882 }
883 compare_function(&result, op1, op2);
884 return Z_LVAL(result) == 0;
885 }
886
fast_is_identical_function(zval * op1,zval * op2)887 static zend_always_inline zend_bool fast_is_identical_function(zval *op1, zval *op2)
888 {
889 if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
890 return 0;
891 } else if (Z_TYPE_P(op1) <= IS_TRUE) {
892 return 1;
893 }
894 return zend_is_identical(op1, op2);
895 }
896
fast_is_not_identical_function(zval * op1,zval * op2)897 static zend_always_inline zend_bool fast_is_not_identical_function(zval *op1, zval *op2)
898 {
899 if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
900 return 1;
901 } else if (Z_TYPE_P(op1) <= IS_TRUE) {
902 return 0;
903 }
904 return !zend_is_identical(op1, op2);
905 }
906
907 #define ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode, binary_op) \
908 if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
909 && op1 == result \
910 && UNEXPECTED(Z_OBJ_HANDLER_P(op1, get)) \
911 && EXPECTED(Z_OBJ_HANDLER_P(op1, set))) { \
912 int ret; \
913 zval rv; \
914 zval *objval = Z_OBJ_HANDLER_P(op1, get)(op1, &rv); \
915 Z_TRY_ADDREF_P(objval); \
916 ret = binary_op(objval, objval, op2); \
917 Z_OBJ_HANDLER_P(op1, set)(op1, objval); \
918 zval_ptr_dtor(objval); \
919 return ret; \
920 } else if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
921 && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation))) { \
922 if (EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, op2))) { \
923 return SUCCESS; \
924 } \
925 }
926
927 #define ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode) \
928 if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT) \
929 && UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation)) \
930 && EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op2, do_operation)(opcode, result, op1, op2))) { \
931 return SUCCESS; \
932 }
933
934 #define ZEND_TRY_BINARY_OBJECT_OPERATION(opcode, binary_op) \
935 ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode, binary_op) \
936 else \
937 ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)
938
939 #define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode) \
940 if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT) \
941 && UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation)) \
942 && EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, NULL))) { \
943 return SUCCESS; \
944 }
945
946 /* buf points to the END of the buffer */
zend_print_ulong_to_buf(char * buf,zend_ulong num)947 static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) {
948 *buf = '\0';
949 do {
950 *--buf = (char) (num % 10) + '0';
951 num /= 10;
952 } while (num > 0);
953 return buf;
954 }
955
956 /* buf points to the END of the buffer */
zend_print_long_to_buf(char * buf,zend_long num)957 static zend_always_inline char *zend_print_long_to_buf(char *buf, zend_long num) {
958 if (num < 0) {
959 char *result = zend_print_ulong_to_buf(buf, ~((zend_ulong) num) + 1);
960 *--result = '-';
961 return result;
962 } else {
963 return zend_print_ulong_to_buf(buf, num);
964 }
965 }
966
967 ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num);
968
zend_unwrap_reference(zval * op)969 static zend_always_inline void zend_unwrap_reference(zval *op) /* {{{ */
970 {
971 if (Z_REFCOUNT_P(op) == 1) {
972 ZVAL_UNREF(op);
973 } else {
974 Z_DELREF_P(op);
975 ZVAL_COPY(op, Z_REFVAL_P(op));
976 }
977 }
978 /* }}} */
979
980
981 END_EXTERN_C()
982
983 #endif
984