1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Jani Lehtimäki <jkl@njet.net> |
14 | Thies C. Arntzen <thies@thieso.net> |
15 | Sascha Schumann <sascha@schumann.cx> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* {{{ includes */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include "php.h"
24 #include "php_string.h"
25 #include "php_var.h"
26 #include "zend_smart_str.h"
27 #include "basic_functions.h"
28 #include "php_incomplete_class.h"
29 #include "zend_enum.h"
30 #include "zend_exceptions.h"
31 /* }}} */
32
33 struct php_serialize_data {
34 HashTable ht;
35 uint32_t n;
36 };
37
38 #define COMMON (is_ref ? "&" : "")
39
php_array_element_dump(zval * zv,zend_ulong index,zend_string * key,int level)40 static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
41 {
42 if (key == NULL) { /* numeric key */
43 php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
44 } else { /* string key */
45 php_printf("%*c[\"", level + 1, ' ');
46 PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
47 php_printf("\"]=>\n");
48 }
49 php_var_dump(zv, level + 2);
50 }
51 /* }}} */
52
php_object_property_dump(zend_property_info * prop_info,zval * zv,zend_ulong index,zend_string * key,int level)53 static void php_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
54 {
55 const char *prop_name, *class_name;
56
57 if (key == NULL) { /* numeric key */
58 php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
59 } else { /* string key */
60 int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
61 php_printf("%*c[", level + 1, ' ');
62
63 if (class_name && unmangle == SUCCESS) {
64 if (class_name[0] == '*') {
65 php_printf("\"%s\":protected", prop_name);
66 } else {
67 php_printf("\"%s\":\"%s\":private", prop_name, class_name);
68 }
69 } else {
70 php_printf("\"");
71 PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
72 php_printf("\"");
73 }
74 ZEND_PUTS("]=>\n");
75 }
76
77 if (Z_TYPE_P(zv) == IS_UNDEF) {
78 ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type));
79 zend_string *type_str = zend_type_to_string(prop_info->type);
80 php_printf("%*cuninitialized(%s)\n",
81 level + 1, ' ', ZSTR_VAL(type_str));
82 zend_string_release(type_str);
83 } else {
84 php_var_dump(zv, level + 2);
85 }
86 }
87 /* }}} */
88
php_var_dump(zval * struc,int level)89 PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
90 {
91 HashTable *myht;
92 zend_string *class_name;
93 int is_ref = 0;
94 zend_ulong num;
95 zend_string *key;
96 zval *val;
97 uint32_t count;
98
99 if (level > 1) {
100 php_printf("%*c", level - 1, ' ');
101 }
102
103 again:
104 switch (Z_TYPE_P(struc)) {
105 case IS_FALSE:
106 php_printf("%sbool(false)\n", COMMON);
107 break;
108 case IS_TRUE:
109 php_printf("%sbool(true)\n", COMMON);
110 break;
111 case IS_NULL:
112 php_printf("%sNULL\n", COMMON);
113 break;
114 case IS_LONG:
115 php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
116 break;
117 case IS_DOUBLE:
118 php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
119 break;
120 case IS_STRING:
121 php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
122 PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
123 PUTS("\"\n");
124 break;
125 case IS_ARRAY:
126 myht = Z_ARRVAL_P(struc);
127 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
128 if (GC_IS_RECURSIVE(myht)) {
129 PUTS("*RECURSION*\n");
130 return;
131 }
132 GC_ADDREF(myht);
133 GC_PROTECT_RECURSION(myht);
134 }
135 count = zend_hash_num_elements(myht);
136 php_printf("%sarray(%d) {\n", COMMON, count);
137 ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) {
138 php_array_element_dump(val, num, key, level);
139 } ZEND_HASH_FOREACH_END();
140 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
141 GC_UNPROTECT_RECURSION(myht);
142 GC_DELREF(myht);
143 }
144 if (level > 1) {
145 php_printf("%*c", level-1, ' ');
146 }
147 PUTS("}\n");
148 break;
149 case IS_OBJECT: {
150 zend_class_entry *ce = Z_OBJCE_P(struc);
151 if (ce->ce_flags & ZEND_ACC_ENUM) {
152 zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
153 php_printf("%senum(%s::%s)\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval));
154 return;
155 }
156
157 if (Z_IS_RECURSIVE_P(struc)) {
158 PUTS("*RECURSION*\n");
159 return;
160 }
161 Z_PROTECT_RECURSION_P(struc);
162
163 myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
164 class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
165 php_printf("%sobject(%s)#%d (%d) {\n", COMMON, ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0);
166 zend_string_release_ex(class_name, 0);
167
168 if (myht) {
169 zend_ulong num;
170 zend_string *key;
171 zval *val;
172
173 ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) {
174 zend_property_info *prop_info = NULL;
175
176 if (Z_TYPE_P(val) == IS_INDIRECT) {
177 val = Z_INDIRECT_P(val);
178 if (key) {
179 prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
180 }
181 }
182
183 if (!Z_ISUNDEF_P(val) || prop_info) {
184 php_object_property_dump(prop_info, val, num, key, level);
185 }
186 } ZEND_HASH_FOREACH_END();
187 zend_release_properties(myht);
188 }
189 if (level > 1) {
190 php_printf("%*c", level-1, ' ');
191 }
192 PUTS("}\n");
193 Z_UNPROTECT_RECURSION_P(struc);
194 break;
195 }
196 case IS_RESOURCE: {
197 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
198 php_printf("%sresource(" ZEND_LONG_FMT ") of type (%s)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown");
199 break;
200 }
201 case IS_REFERENCE:
202 //??? hide references with refcount==1 (for compatibility)
203 if (Z_REFCOUNT_P(struc) > 1) {
204 is_ref = 1;
205 }
206 struc = Z_REFVAL_P(struc);
207 goto again;
208 break;
209 default:
210 php_printf("%sUNKNOWN:0\n", COMMON);
211 break;
212 }
213 }
214 /* }}} */
215
216 /* {{{ Dumps a string representation of variable to output */
PHP_FUNCTION(var_dump)217 PHP_FUNCTION(var_dump)
218 {
219 zval *args;
220 int argc;
221 int i;
222
223 ZEND_PARSE_PARAMETERS_START(1, -1)
224 Z_PARAM_VARIADIC('+', args, argc)
225 ZEND_PARSE_PARAMETERS_END();
226
227 for (i = 0; i < argc; i++) {
228 php_var_dump(&args[i], 1);
229 }
230 }
231 /* }}} */
232
zval_array_element_dump(zval * zv,zend_ulong index,zend_string * key,int level)233 static void zval_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
234 {
235 if (key == NULL) { /* numeric key */
236 php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
237 } else { /* string key */
238 php_printf("%*c[\"", level + 1, ' ');
239 PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
240 php_printf("\"]=>\n");
241 }
242 php_debug_zval_dump(zv, level + 2);
243 }
244 /* }}} */
245
zval_object_property_dump(zend_property_info * prop_info,zval * zv,zend_ulong index,zend_string * key,int level)246 static void zval_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
247 {
248 const char *prop_name, *class_name;
249
250 if (key == NULL) { /* numeric key */
251 php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
252 } else { /* string key */
253 zend_unmangle_property_name(key, &class_name, &prop_name);
254 php_printf("%*c[", level + 1, ' ');
255
256 if (class_name) {
257 if (class_name[0] == '*') {
258 php_printf("\"%s\":protected", prop_name);
259 } else {
260 php_printf("\"%s\":\"%s\":private", prop_name, class_name);
261 }
262 } else {
263 php_printf("\"%s\"", prop_name);
264 }
265 ZEND_PUTS("]=>\n");
266 }
267 if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) {
268 zend_string *type_str = zend_type_to_string(prop_info->type);
269 php_printf("%*cuninitialized(%s)\n",
270 level + 1, ' ', ZSTR_VAL(type_str));
271 zend_string_release(type_str);
272 } else {
273 php_debug_zval_dump(zv, level + 2);
274 }
275 }
276 /* }}} */
277
php_debug_zval_dump(zval * struc,int level)278 PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */
279 {
280 HashTable *myht = NULL;
281 zend_string *class_name;
282 zend_ulong index;
283 zend_string *key;
284 zval *val;
285 uint32_t count;
286
287 if (level > 1) {
288 php_printf("%*c", level - 1, ' ');
289 }
290
291 switch (Z_TYPE_P(struc)) {
292 case IS_FALSE:
293 PUTS("bool(false)\n");
294 break;
295 case IS_TRUE:
296 PUTS("bool(true)\n");
297 break;
298 case IS_NULL:
299 PUTS("NULL\n");
300 break;
301 case IS_LONG:
302 php_printf("int(" ZEND_LONG_FMT ")\n", Z_LVAL_P(struc));
303 break;
304 case IS_DOUBLE:
305 php_printf_unchecked("float(%.*H)\n", (int) PG(serialize_precision), Z_DVAL_P(struc));
306 break;
307 case IS_STRING:
308 php_printf("string(%zd) \"", Z_STRLEN_P(struc));
309 PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
310 if (Z_REFCOUNTED_P(struc)) {
311 php_printf("\" refcount(%u)\n", Z_REFCOUNT_P(struc));
312 } else {
313 PUTS("\" interned\n");
314 }
315 break;
316 case IS_ARRAY:
317 myht = Z_ARRVAL_P(struc);
318 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
319 if (GC_IS_RECURSIVE(myht)) {
320 PUTS("*RECURSION*\n");
321 return;
322 }
323 GC_ADDREF(myht);
324 GC_PROTECT_RECURSION(myht);
325 }
326 count = zend_hash_num_elements(myht);
327 if (Z_REFCOUNTED_P(struc)) {
328 /* -1 because of ADDREF above. */
329 php_printf("array(%d) refcount(%u){\n", count, Z_REFCOUNT_P(struc) - 1);
330 } else {
331 php_printf("array(%d) interned {\n", count);
332 }
333 ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
334 zval_array_element_dump(val, index, key, level);
335 } ZEND_HASH_FOREACH_END();
336 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
337 GC_UNPROTECT_RECURSION(myht);
338 GC_DELREF(myht);
339 }
340 if (level > 1) {
341 php_printf("%*c", level - 1, ' ');
342 }
343 PUTS("}\n");
344 break;
345 case IS_OBJECT:
346 /* Check if this is already recursing on the object before calling zend_get_properties_for,
347 * to allow infinite recursion detection to work even if classes return temporary arrays,
348 * and to avoid the need to update the properties table in place to reflect the state
349 * if the result won't be used. (https://github.com/php/php-src/issues/8044) */
350 if (Z_IS_RECURSIVE_P(struc)) {
351 PUTS("*RECURSION*\n");
352 return;
353 }
354 Z_PROTECT_RECURSION_P(struc);
355
356 myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
357 class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
358 php_printf("object(%s)#%d (%d) refcount(%u){\n", ZSTR_VAL(class_name), Z_OBJ_HANDLE_P(struc), myht ? zend_array_count(myht) : 0, Z_REFCOUNT_P(struc));
359 zend_string_release_ex(class_name, 0);
360 if (myht) {
361 ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
362 zend_property_info *prop_info = NULL;
363
364 if (Z_TYPE_P(val) == IS_INDIRECT) {
365 val = Z_INDIRECT_P(val);
366 if (key) {
367 prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
368 }
369 }
370
371 if (!Z_ISUNDEF_P(val) || prop_info) {
372 zval_object_property_dump(prop_info, val, index, key, level);
373 }
374 } ZEND_HASH_FOREACH_END();
375 zend_release_properties(myht);
376 }
377 if (level > 1) {
378 php_printf("%*c", level - 1, ' ');
379 }
380 PUTS("}\n");
381 Z_UNPROTECT_RECURSION_P(struc);
382 break;
383 case IS_RESOURCE: {
384 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
385 php_printf("resource(" ZEND_LONG_FMT ") of type (%s) refcount(%u)\n", Z_RES_P(struc)->handle, type_name ? type_name : "Unknown", Z_REFCOUNT_P(struc));
386 break;
387 }
388 case IS_REFERENCE:
389 php_printf("reference refcount(%u) {\n", Z_REFCOUNT_P(struc));
390 php_debug_zval_dump(Z_REFVAL_P(struc), level + 2);
391 if (level > 1) {
392 php_printf("%*c", level - 1, ' ');
393 }
394 PUTS("}\n");
395 break;
396 default:
397 PUTS("UNKNOWN:0\n");
398 break;
399 }
400 }
401 /* }}} */
402
403 /* {{{ Dumps a string representation of an internal zend value to output. */
PHP_FUNCTION(debug_zval_dump)404 PHP_FUNCTION(debug_zval_dump)
405 {
406 zval *args;
407 int argc;
408 int i;
409
410 ZEND_PARSE_PARAMETERS_START(1, -1)
411 Z_PARAM_VARIADIC('+', args, argc)
412 ZEND_PARSE_PARAMETERS_END();
413
414 for (i = 0; i < argc; i++) {
415 php_debug_zval_dump(&args[i], 1);
416 }
417 }
418 /* }}} */
419
420 #define buffer_append_spaces(buf, num_spaces) \
421 do { \
422 char *tmp_spaces; \
423 size_t tmp_spaces_len; \
424 tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \
425 smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \
426 efree(tmp_spaces); \
427 } while(0);
428
php_array_element_export(zval * zv,zend_ulong index,zend_string * key,int level,smart_str * buf)429 static void php_array_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
430 {
431 if (key == NULL) { /* numeric key */
432 buffer_append_spaces(buf, level+1);
433 smart_str_append_long(buf, (zend_long) index);
434 smart_str_appendl(buf, " => ", 4);
435
436 } else { /* string key */
437 zend_string *tmp_str;
438 zend_string *ckey = php_addcslashes(key, "'\\", 2);
439 tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12);
440
441 buffer_append_spaces(buf, level + 1);
442
443 smart_str_appendc(buf, '\'');
444 smart_str_append(buf, tmp_str);
445 smart_str_appendl(buf, "' => ", 5);
446
447 zend_string_free(ckey);
448 zend_string_free(tmp_str);
449 }
450 php_var_export_ex(zv, level + 2, buf);
451
452 smart_str_appendc(buf, ',');
453 smart_str_appendc(buf, '\n');
454 }
455 /* }}} */
456
php_object_element_export(zval * zv,zend_ulong index,zend_string * key,int level,smart_str * buf)457 static void php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
458 {
459 buffer_append_spaces(buf, level + 2);
460 if (key != NULL) {
461 const char *class_name, *prop_name;
462 size_t prop_name_len;
463 zend_string *pname_esc;
464
465 zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len);
466 pname_esc = php_addcslashes_str(prop_name, prop_name_len, "'\\", 2);
467
468 smart_str_appendc(buf, '\'');
469 smart_str_append(buf, pname_esc);
470 smart_str_appendc(buf, '\'');
471 zend_string_release_ex(pname_esc, 0);
472 } else {
473 smart_str_append_long(buf, (zend_long) index);
474 }
475 smart_str_appendl(buf, " => ", 4);
476 php_var_export_ex(zv, level + 2, buf);
477 smart_str_appendc(buf, ',');
478 smart_str_appendc(buf, '\n');
479 }
480 /* }}} */
481
php_var_export_ex(zval * struc,int level,smart_str * buf)482 PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
483 {
484 HashTable *myht;
485 zend_string *ztmp, *ztmp2;
486 zend_ulong index;
487 zend_string *key;
488 zval *val;
489
490 again:
491 switch (Z_TYPE_P(struc)) {
492 case IS_FALSE:
493 smart_str_appendl(buf, "false", 5);
494 break;
495 case IS_TRUE:
496 smart_str_appendl(buf, "true", 4);
497 break;
498 case IS_NULL:
499 smart_str_appendl(buf, "NULL", 4);
500 break;
501 case IS_LONG:
502 /* INT_MIN as a literal will be parsed as a float. Emit something like
503 * -9223372036854775807-1 to avoid this. */
504 if (Z_LVAL_P(struc) == ZEND_LONG_MIN) {
505 smart_str_append_long(buf, ZEND_LONG_MIN+1);
506 smart_str_appends(buf, "-1");
507 break;
508 }
509 smart_str_append_long(buf, Z_LVAL_P(struc));
510 break;
511 case IS_DOUBLE:
512 smart_str_append_double(
513 buf, Z_DVAL_P(struc), (int) PG(serialize_precision), /* zero_fraction */ true);
514 break;
515 case IS_STRING:
516 ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2);
517 ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12);
518
519 smart_str_appendc(buf, '\'');
520 smart_str_append(buf, ztmp2);
521 smart_str_appendc(buf, '\'');
522
523 zend_string_free(ztmp);
524 zend_string_free(ztmp2);
525 break;
526 case IS_ARRAY:
527 myht = Z_ARRVAL_P(struc);
528 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
529 if (GC_IS_RECURSIVE(myht)) {
530 smart_str_appendl(buf, "NULL", 4);
531 zend_error(E_WARNING, "var_export does not handle circular references");
532 return;
533 }
534 GC_ADDREF(myht);
535 GC_PROTECT_RECURSION(myht);
536 }
537 if (level > 1) {
538 smart_str_appendc(buf, '\n');
539 buffer_append_spaces(buf, level - 1);
540 }
541 smart_str_appendl(buf, "array (\n", 8);
542 ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
543 php_array_element_export(val, index, key, level, buf);
544 } ZEND_HASH_FOREACH_END();
545 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
546 GC_UNPROTECT_RECURSION(myht);
547 GC_DELREF(myht);
548 }
549 if (level > 1) {
550 buffer_append_spaces(buf, level - 1);
551 }
552 smart_str_appendc(buf, ')');
553
554 break;
555
556 case IS_OBJECT:
557 /* Check if this is already recursing on the object before calling zend_get_properties_for,
558 * to allow infinite recursion detection to work even if classes return temporary arrays,
559 * and to avoid the need to update the properties table in place to reflect the state
560 * if the result won't be used. (https://github.com/php/php-src/issues/8044) */
561 if (Z_IS_RECURSIVE_P(struc)) {
562 smart_str_appendl(buf, "NULL", 4);
563 zend_error(E_WARNING, "var_export does not handle circular references");
564 return;
565 }
566 Z_PROTECT_RECURSION_P(struc);
567 myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT);
568 if (level > 1) {
569 smart_str_appendc(buf, '\n');
570 buffer_append_spaces(buf, level - 1);
571 }
572
573 zend_class_entry *ce = Z_OBJCE_P(struc);
574 bool is_enum = ce->ce_flags & ZEND_ACC_ENUM;
575
576 /* stdClass has no __set_state method, but can be casted to */
577 if (ce == zend_standard_class_def) {
578 smart_str_appendl(buf, "(object) array(\n", 16);
579 } else {
580 smart_str_appendc(buf, '\\');
581 smart_str_append(buf, ce->name);
582 if (is_enum) {
583 zend_object *zobj = Z_OBJ_P(struc);
584 zval *case_name_zval = zend_enum_fetch_case_name(zobj);
585 smart_str_appendl(buf, "::", 2);
586 smart_str_append(buf, Z_STR_P(case_name_zval));
587 } else {
588 smart_str_appendl(buf, "::__set_state(array(\n", 21);
589 }
590 }
591
592 if (myht) {
593 if (!is_enum) {
594 ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
595 php_object_element_export(val, index, key, level, buf);
596 } ZEND_HASH_FOREACH_END();
597 }
598 zend_release_properties(myht);
599 }
600 Z_UNPROTECT_RECURSION_P(struc);
601 if (level > 1 && !is_enum) {
602 buffer_append_spaces(buf, level - 1);
603 }
604 if (ce == zend_standard_class_def) {
605 smart_str_appendc(buf, ')');
606 } else if (!is_enum) {
607 smart_str_appendl(buf, "))", 2);
608 }
609
610 break;
611 case IS_REFERENCE:
612 struc = Z_REFVAL_P(struc);
613 goto again;
614 break;
615 default:
616 smart_str_appendl(buf, "NULL", 4);
617 break;
618 }
619 }
620 /* }}} */
621
622 /* FOR BC reasons, this will always perform and then print */
php_var_export(zval * struc,int level)623 PHPAPI void php_var_export(zval *struc, int level) /* {{{ */
624 {
625 smart_str buf = {0};
626 php_var_export_ex(struc, level, &buf);
627 smart_str_0(&buf);
628 PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
629 smart_str_free(&buf);
630 }
631 /* }}} */
632
633 /* {{{ Outputs or returns a string representation of a variable */
PHP_FUNCTION(var_export)634 PHP_FUNCTION(var_export)
635 {
636 zval *var;
637 bool return_output = 0;
638 smart_str buf = {0};
639
640 ZEND_PARSE_PARAMETERS_START(1, 2)
641 Z_PARAM_ZVAL(var)
642 Z_PARAM_OPTIONAL
643 Z_PARAM_BOOL(return_output)
644 ZEND_PARSE_PARAMETERS_END();
645
646 php_var_export_ex(var, 1, &buf);
647 smart_str_0 (&buf);
648
649 if (return_output) {
650 RETURN_STR(smart_str_extract(&buf));
651 } else {
652 PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
653 smart_str_free(&buf);
654 }
655 }
656 /* }}} */
657
658 static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash);
659
php_add_var_hash(php_serialize_data_t data,zval * var)660 static inline zend_long php_add_var_hash(php_serialize_data_t data, zval *var) /* {{{ */
661 {
662 zval *zv;
663 zend_ulong key;
664 bool is_ref = Z_ISREF_P(var);
665
666 data->n += 1;
667
668 if (is_ref) {
669 /* pass */
670 } else if (Z_TYPE_P(var) != IS_OBJECT) {
671 return 0;
672 } else if (Z_REFCOUNT_P(var) == 1 && (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1)) {
673 return 0;
674 }
675
676 /* References to objects are treated as if the reference didn't exist */
677 if (is_ref && Z_TYPE_P(Z_REFVAL_P(var)) == IS_OBJECT) {
678 var = Z_REFVAL_P(var);
679 }
680
681 /* Index for the variable is stored using the numeric value of the pointer to
682 * the zend_refcounted struct */
683 key = (zend_ulong) (uintptr_t) Z_COUNTED_P(var);
684 zv = zend_hash_index_find(&data->ht, key);
685
686 if (zv) {
687 /* References are only counted once, undo the data->n increment above */
688 if (is_ref && Z_LVAL_P(zv) != -1) {
689 data->n -= 1;
690 }
691
692 return Z_LVAL_P(zv);
693 } else {
694 zval zv_n;
695 ZVAL_LONG(&zv_n, data->n);
696 zend_hash_index_add_new(&data->ht, key, &zv_n);
697
698 /* Additionally to the index, we also store the variable, to ensure that it is
699 * not destroyed during serialization and its pointer reused. The variable is
700 * stored at the numeric value of the pointer + 1, which cannot be the location
701 * of another zend_refcounted structure. */
702 zend_hash_index_add_new(&data->ht, key + 1, var);
703 Z_ADDREF_P(var);
704
705 return 0;
706 }
707 }
708 /* }}} */
709
php_var_serialize_long(smart_str * buf,zend_long val)710 static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */
711 {
712 char b[32];
713 char *s = zend_print_long_to_buf(b + sizeof(b) - 1, val);
714 size_t l = b + sizeof(b) - 1 - s;
715 char *res = smart_str_extend(buf, 2 + l + 1);
716 memcpy(res, "i:", 2);
717 res += 2;
718 memcpy(res, s, l);
719 res[l] = ';';
720 }
721 /* }}} */
722
php_var_serialize_string(smart_str * buf,char * str,size_t len)723 static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */
724 {
725 char b[32];
726 char *s = zend_print_long_to_buf(b + sizeof(b) - 1, len);
727 size_t l = b + sizeof(b) - 1 - s;
728 char *res = smart_str_extend(buf, 2 + l + 2 + len + 2);
729 memcpy(res, "s:", 2);
730 res += 2;
731 memcpy(res, s, l);
732 res += l;
733 memcpy(res, ":\"", 2);
734 res += 2;
735 memcpy(res, str, len);
736 res += len;
737 memcpy(res, "\";", 2);
738 }
739 /* }}} */
740
php_var_serialize_class_name(smart_str * buf,zval * struc)741 static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */
742 {
743 char b[32];
744 PHP_CLASS_ATTRIBUTES;
745
746 PHP_SET_CLASS_ATTRIBUTES(struc);
747 size_t class_name_len = ZSTR_LEN(class_name);
748 char *s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len);
749 size_t l = b + sizeof(b) - 1 - s;
750 char *res = smart_str_extend(buf, 2 + l + 2 + class_name_len + 2);
751 memcpy(res, "O:", 2);
752 res += 2;
753 memcpy(res, s, l);
754 res += l;
755 memcpy(res, ":\"", 2);
756 res += 2;
757 memcpy(res, ZSTR_VAL(class_name), class_name_len);
758 res += class_name_len;
759 memcpy(res, "\":", 2);
760 PHP_CLEANUP_CLASS_ATTRIBUTES();
761 return incomplete_class;
762 }
763 /* }}} */
764
php_var_serialize_call_sleep(zend_object * obj,zend_function * fn)765 static HashTable* php_var_serialize_call_sleep(zend_object *obj, zend_function *fn) /* {{{ */
766 {
767 zend_result res;
768 zend_fcall_info fci;
769 zend_fcall_info_cache fci_cache;
770 zval retval;
771
772 fci.size = sizeof(fci);
773 fci.object = obj;
774 fci.retval = &retval;
775 fci.param_count = 0;
776 fci.params = NULL;
777 fci.named_params = NULL;
778 ZVAL_UNDEF(&fci.function_name);
779
780 fci_cache.function_handler = fn;
781 fci_cache.object = obj;
782 fci_cache.called_scope = obj->ce;
783
784 BG(serialize_lock)++;
785 res = zend_call_function(&fci, &fci_cache);
786 BG(serialize_lock)--;
787
788 if (res == FAILURE || Z_ISUNDEF(retval)) {
789 zval_ptr_dtor(&retval);
790 return NULL;
791 }
792
793 if (Z_TYPE(retval) != IS_ARRAY) {
794 zval_ptr_dtor(&retval);
795 php_error_docref(NULL, E_WARNING, "%s::__sleep() should return an array only containing the names of instance-variables to serialize", ZSTR_VAL(obj->ce->name));
796 return NULL;
797 }
798
799 return Z_ARRVAL(retval);
800 }
801 /* }}} */
802
php_var_serialize_call_magic_serialize(zval * retval,zval * obj)803 static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */
804 {
805 BG(serialize_lock)++;
806 zend_call_known_instance_method_with_0_params(
807 Z_OBJCE_P(obj)->__serialize, Z_OBJ_P(obj), retval);
808 BG(serialize_lock)--;
809
810 if (EG(exception)) {
811 zval_ptr_dtor(retval);
812 return FAILURE;
813 }
814
815 if (Z_TYPE_P(retval) != IS_ARRAY) {
816 zval_ptr_dtor(retval);
817 zend_type_error("%s::__serialize() must return an array", ZSTR_VAL(Z_OBJCE_P(obj)->name));
818 return FAILURE;
819 }
820
821 return SUCCESS;
822 }
823 /* }}} */
824
php_var_serialize_try_add_sleep_prop(HashTable * ht,HashTable * props,zend_string * name,zend_string * error_name,zval * struc)825 static int php_var_serialize_try_add_sleep_prop(
826 HashTable *ht, HashTable *props, zend_string *name, zend_string *error_name, zval *struc) /* {{{ */
827 {
828 zval *val = zend_hash_find(props, name);
829 if (val == NULL) {
830 return FAILURE;
831 }
832
833 if (Z_TYPE_P(val) == IS_INDIRECT) {
834 val = Z_INDIRECT_P(val);
835 if (Z_TYPE_P(val) == IS_UNDEF) {
836 zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
837 if (info) {
838 return SUCCESS;
839 }
840 return FAILURE;
841 }
842 }
843
844 if (!zend_hash_add(ht, name, val)) {
845 php_error_docref(NULL, E_WARNING,
846 "\"%s\" is returned from __sleep() multiple times", ZSTR_VAL(error_name));
847 return SUCCESS;
848 }
849
850 Z_TRY_ADDREF_P(val);
851 return SUCCESS;
852 }
853 /* }}} */
854
php_var_serialize_get_sleep_props(HashTable * ht,zval * struc,HashTable * sleep_retval)855 static int php_var_serialize_get_sleep_props(
856 HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */
857 {
858 zend_class_entry *ce = Z_OBJCE_P(struc);
859 HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
860 zval *name_val;
861 int retval = SUCCESS;
862
863 zend_hash_init(ht, zend_hash_num_elements(sleep_retval), NULL, ZVAL_PTR_DTOR, 0);
864 /* TODO: Rewrite this by fetching the property info instead of trying out different
865 * name manglings? */
866 ZEND_HASH_FOREACH_VAL_IND(sleep_retval, name_val) {
867 zend_string *name, *tmp_name, *priv_name, *prot_name;
868
869 ZVAL_DEREF(name_val);
870 if (Z_TYPE_P(name_val) != IS_STRING) {
871 php_error_docref(NULL, E_WARNING,
872 "%s::__sleep() should return an array only containing the names of instance-variables to serialize",
873 ZSTR_VAL(ce->name));
874 }
875
876 name = zval_get_tmp_string(name_val, &tmp_name);
877 if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) {
878 zend_tmp_string_release(tmp_name);
879 continue;
880 }
881
882 if (EG(exception)) {
883 zend_tmp_string_release(tmp_name);
884 retval = FAILURE;
885 break;
886 }
887
888 priv_name = zend_mangle_property_name(
889 ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
890 ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
891 if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) {
892 zend_tmp_string_release(tmp_name);
893 zend_string_release(priv_name);
894 continue;
895 }
896 zend_string_release(priv_name);
897
898 if (EG(exception)) {
899 zend_tmp_string_release(tmp_name);
900 retval = FAILURE;
901 break;
902 }
903
904 prot_name = zend_mangle_property_name(
905 "*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
906 if (php_var_serialize_try_add_sleep_prop(ht, props, prot_name, name, struc) == SUCCESS) {
907 zend_tmp_string_release(tmp_name);
908 zend_string_release(prot_name);
909 continue;
910 }
911 zend_string_release(prot_name);
912
913 if (EG(exception)) {
914 zend_tmp_string_release(tmp_name);
915 retval = FAILURE;
916 break;
917 }
918
919 php_error_docref(NULL, E_WARNING,
920 "\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name));
921 zend_tmp_string_release(tmp_name);
922 } ZEND_HASH_FOREACH_END();
923
924 zend_release_properties(props);
925 return retval;
926 }
927 /* }}} */
928
php_var_serialize_nested_data(smart_str * buf,zval * struc,HashTable * ht,uint32_t count,bool incomplete_class,php_serialize_data_t var_hash)929 static void php_var_serialize_nested_data(smart_str *buf, zval *struc, HashTable *ht, uint32_t count, bool incomplete_class, php_serialize_data_t var_hash) /* {{{ */
930 {
931 smart_str_append_unsigned(buf, count);
932 smart_str_appendl(buf, ":{", 2);
933 if (count > 0) {
934 zend_string *key;
935 zval *data;
936 zend_ulong index;
937
938 ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) {
939 if (incomplete_class && zend_string_equals_literal(key, MAGIC_MEMBER)) {
940 incomplete_class = 0;
941 continue;
942 }
943
944 if (!key) {
945 php_var_serialize_long(buf, index);
946 } else {
947 php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
948 }
949
950 if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
951 data = Z_REFVAL_P(data);
952 }
953
954 /* we should still add element even if it's not OK,
955 * since we already wrote the length of the array before */
956 if (Z_TYPE_P(data) == IS_ARRAY) {
957 if (UNEXPECTED(Z_IS_RECURSIVE_P(data))
958 || UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
959 php_add_var_hash(var_hash, struc);
960 smart_str_appendl(buf, "N;", 2);
961 } else {
962 if (Z_REFCOUNTED_P(data)) {
963 Z_PROTECT_RECURSION_P(data);
964 }
965 php_var_serialize_intern(buf, data, var_hash);
966 if (Z_REFCOUNTED_P(data)) {
967 Z_UNPROTECT_RECURSION_P(data);
968 }
969 }
970 } else {
971 php_var_serialize_intern(buf, data, var_hash);
972 }
973 } ZEND_HASH_FOREACH_END();
974 }
975 smart_str_appendc(buf, '}');
976 }
977 /* }}} */
978
php_var_serialize_class(smart_str * buf,zval * struc,HashTable * ht,php_serialize_data_t var_hash)979 static void php_var_serialize_class(smart_str *buf, zval *struc, HashTable *ht, php_serialize_data_t var_hash) /* {{{ */
980 {
981 HashTable props;
982
983 if (php_var_serialize_get_sleep_props(&props, struc, ht) == SUCCESS) {
984 php_var_serialize_class_name(buf, struc);
985 php_var_serialize_nested_data(
986 buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ 0, var_hash);
987 }
988 zend_hash_destroy(&props);
989 }
990 /* }}} */
991
php_var_serialize_intern(smart_str * buf,zval * struc,php_serialize_data_t var_hash)992 static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash) /* {{{ */
993 {
994 zend_long var_already;
995 HashTable *myht;
996
997 if (EG(exception)) {
998 return;
999 }
1000
1001 if (var_hash && (var_already = php_add_var_hash(var_hash, struc))) {
1002 if (var_already == -1) {
1003 /* Reference to an object that failed to serialize, replace with null. */
1004 smart_str_appendl(buf, "N;", 2);
1005 return;
1006 } else if (Z_ISREF_P(struc)) {
1007 smart_str_appendl(buf, "R:", 2);
1008 smart_str_append_long(buf, var_already);
1009 smart_str_appendc(buf, ';');
1010 return;
1011 } else if (Z_TYPE_P(struc) == IS_OBJECT) {
1012 smart_str_appendl(buf, "r:", 2);
1013 smart_str_append_long(buf, var_already);
1014 smart_str_appendc(buf, ';');
1015 return;
1016 }
1017 }
1018
1019 again:
1020 switch (Z_TYPE_P(struc)) {
1021 case IS_FALSE:
1022 smart_str_appendl(buf, "b:0;", 4);
1023 return;
1024
1025 case IS_TRUE:
1026 smart_str_appendl(buf, "b:1;", 4);
1027 return;
1028
1029 case IS_NULL:
1030 smart_str_appendl(buf, "N;", 2);
1031 return;
1032
1033 case IS_LONG:
1034 php_var_serialize_long(buf, Z_LVAL_P(struc));
1035 return;
1036
1037 case IS_DOUBLE: {
1038 char tmp_str[ZEND_DOUBLE_MAX_LENGTH];
1039 zend_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
1040
1041 size_t len = strlen(tmp_str);
1042 char *res = smart_str_extend(buf, 2 + len + 1);
1043 memcpy(res, "d:", 2);
1044 res += 2;
1045 memcpy(res, tmp_str, len);
1046 res[len] = ';';
1047 return;
1048 }
1049
1050 case IS_STRING:
1051 php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));
1052 return;
1053
1054 case IS_OBJECT: {
1055 zend_class_entry *ce = Z_OBJCE_P(struc);
1056 bool incomplete_class;
1057 uint32_t count;
1058
1059 if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1060 zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed",
1061 ZSTR_VAL(ce->name));
1062 return;
1063 }
1064
1065 if (ce->ce_flags & ZEND_ACC_ENUM) {
1066 PHP_CLASS_ATTRIBUTES;
1067
1068 zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
1069
1070 PHP_SET_CLASS_ATTRIBUTES(struc);
1071 smart_str_appendl(buf, "E:", 2);
1072 smart_str_append_unsigned(buf, ZSTR_LEN(class_name) + strlen(":") + Z_STRLEN_P(case_name_zval));
1073 smart_str_appendl(buf, ":\"", 2);
1074 smart_str_append(buf, class_name);
1075 smart_str_appendc(buf, ':');
1076 smart_str_append(buf, Z_STR_P(case_name_zval));
1077 smart_str_appendl(buf, "\";", 2);
1078 PHP_CLEANUP_CLASS_ATTRIBUTES();
1079 return;
1080 }
1081
1082 if (ce->__serialize) {
1083 zval retval, obj;
1084 zend_string *key;
1085 zval *data;
1086 zend_ulong index;
1087
1088 ZVAL_OBJ_COPY(&obj, Z_OBJ_P(struc));
1089 if (php_var_serialize_call_magic_serialize(&retval, &obj) == FAILURE) {
1090 if (!EG(exception)) {
1091 smart_str_appendl(buf, "N;", 2);
1092 }
1093 zval_ptr_dtor(&obj);
1094 return;
1095 }
1096
1097 php_var_serialize_class_name(buf, &obj);
1098 smart_str_append_unsigned(buf, zend_hash_num_elements(Z_ARRVAL(retval)));
1099 smart_str_appendl(buf, ":{", 2);
1100 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(retval), index, key, data) {
1101 if (!key) {
1102 php_var_serialize_long(buf, index);
1103 } else {
1104 php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
1105 }
1106
1107 if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
1108 data = Z_REFVAL_P(data);
1109 }
1110 php_var_serialize_intern(buf, data, var_hash);
1111 } ZEND_HASH_FOREACH_END();
1112 smart_str_appendc(buf, '}');
1113
1114 zval_ptr_dtor(&obj);
1115 zval_ptr_dtor(&retval);
1116 return;
1117 }
1118
1119 if (ce->serialize != NULL) {
1120 /* has custom handler */
1121 unsigned char *serialized_data = NULL;
1122 size_t serialized_length;
1123
1124 if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) {
1125 char b1[32], b2[32];
1126 char *s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name));
1127 size_t l1 = b1 + sizeof(b1) - 1 - s1;
1128 char *s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length);
1129 size_t l2 = b2 + sizeof(b2) - 1 - s2;
1130 char *res = smart_str_extend(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1);
1131 memcpy(res, "C:", 2);
1132 res += 2;
1133 memcpy(res, s1, l1);
1134 res += l1;
1135 memcpy(res, ":\"", 2);
1136 res += 2;
1137 memcpy(res, ZSTR_VAL(Z_OBJCE_P(struc)->name), ZSTR_LEN(Z_OBJCE_P(struc)->name));
1138 res += ZSTR_LEN(Z_OBJCE_P(struc)->name);
1139 memcpy(res, "\":", 2);
1140 res += 2;
1141
1142 memcpy(res, s2, l2);
1143 res += l2;
1144 memcpy(res, ":{", 2);
1145 res += 2;
1146 memcpy(res, (char *) serialized_data, serialized_length);
1147 res[serialized_length] = '}';
1148 } else {
1149 /* Mark this value in the var_hash, to avoid creating references to it. */
1150 zval *var_idx = zend_hash_index_find(&var_hash->ht,
1151 (zend_ulong) (uintptr_t) Z_COUNTED_P(struc));
1152 if (var_idx) {
1153 ZVAL_LONG(var_idx, -1);
1154 }
1155 smart_str_appendl(buf, "N;", 2);
1156 }
1157 if (serialized_data) {
1158 efree(serialized_data);
1159 }
1160 return;
1161 }
1162
1163 if (ce != PHP_IC_ENTRY) {
1164 zval *zv = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP));
1165
1166 if (zv) {
1167 HashTable *ht;
1168 zval tmp;
1169
1170 ZVAL_OBJ_COPY(&tmp, Z_OBJ_P(struc));
1171 if (!(ht = php_var_serialize_call_sleep(Z_OBJ(tmp), Z_FUNC_P(zv)))) {
1172 if (!EG(exception)) {
1173 /* we should still add element even if it's not OK,
1174 * since we already wrote the length of the array before */
1175 smart_str_appendl(buf, "N;", 2);
1176 }
1177 OBJ_RELEASE(Z_OBJ(tmp));
1178 return;
1179 }
1180
1181 php_var_serialize_class(buf, &tmp, ht, var_hash);
1182 zend_array_release(ht);
1183 OBJ_RELEASE(Z_OBJ(tmp));
1184 return;
1185 }
1186 }
1187
1188 incomplete_class = php_var_serialize_class_name(buf, struc);
1189
1190 if (Z_OBJ_P(struc)->properties == NULL
1191 && Z_OBJ_HT_P(struc)->get_properties_for == NULL
1192 && Z_OBJ_HT_P(struc)->get_properties == zend_std_get_properties) {
1193 /* Optimized version without rebulding properties HashTable */
1194 zend_object *obj = Z_OBJ_P(struc);
1195 zend_class_entry *ce = obj->ce;
1196 zend_property_info *prop_info;
1197 zval *prop;
1198 int i;
1199
1200 count = ce->default_properties_count;
1201 for (i = 0; i < ce->default_properties_count; i++) {
1202 prop_info = ce->properties_info_table[i];
1203 if (!prop_info) {
1204 count--;
1205 continue;
1206 }
1207 prop = OBJ_PROP(obj, prop_info->offset);
1208 if (Z_TYPE_P(prop) == IS_UNDEF) {
1209 count--;
1210 continue;
1211 }
1212 }
1213 if (count) {
1214 smart_str_append_unsigned(buf, count);
1215 smart_str_appendl(buf, ":{", 2);
1216 for (i = 0; i < ce->default_properties_count; i++) {
1217 prop_info = ce->properties_info_table[i];
1218 if (!prop_info) {
1219 continue;
1220 }
1221 prop = OBJ_PROP(obj, prop_info->offset);
1222 if (Z_TYPE_P(prop) == IS_UNDEF) {
1223 continue;
1224 }
1225
1226 php_var_serialize_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name));
1227
1228 if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
1229 prop = Z_REFVAL_P(prop);
1230 }
1231
1232 php_var_serialize_intern(buf, prop, var_hash);
1233 }
1234 smart_str_appendc(buf, '}');
1235 } else {
1236 smart_str_appendl(buf, "0:{}", 4);
1237 }
1238 return;
1239 }
1240 myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
1241 /* count after serializing name, since php_var_serialize_class_name
1242 * changes the count if the variable is incomplete class */
1243 count = zend_array_count(myht);
1244 if (count > 0 && incomplete_class) {
1245 --count;
1246 }
1247 php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash);
1248 zend_release_properties(myht);
1249 return;
1250 }
1251 case IS_ARRAY:
1252 smart_str_appendl(buf, "a:", 2);
1253 myht = Z_ARRVAL_P(struc);
1254 php_var_serialize_nested_data(
1255 buf, struc, myht, zend_array_count(myht), /* incomplete_class */ 0, var_hash);
1256 return;
1257 case IS_REFERENCE:
1258 struc = Z_REFVAL_P(struc);
1259 goto again;
1260 default:
1261 smart_str_appendl(buf, "i:0;", 4);
1262 return;
1263 }
1264 }
1265 /* }}} */
1266
php_var_serialize(smart_str * buf,zval * struc,php_serialize_data_t * data)1267 PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */
1268 {
1269 php_var_serialize_intern(buf, struc, *data);
1270 smart_str_0(buf);
1271 }
1272 /* }}} */
1273
php_var_serialize_init(void)1274 PHPAPI php_serialize_data_t php_var_serialize_init(void) {
1275 struct php_serialize_data *d;
1276 /* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
1277 if (BG(serialize_lock) || !BG(serialize).level) {
1278 d = emalloc(sizeof(struct php_serialize_data));
1279 zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0);
1280 d->n = 0;
1281 if (!BG(serialize_lock)) {
1282 BG(serialize).data = d;
1283 BG(serialize).level = 1;
1284 }
1285 } else {
1286 d = BG(serialize).data;
1287 ++BG(serialize).level;
1288 }
1289 return d;
1290 }
1291
php_var_serialize_destroy(php_serialize_data_t d)1292 PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) {
1293 /* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
1294 if (BG(serialize_lock) || BG(serialize).level == 1) {
1295 zend_hash_destroy(&d->ht);
1296 efree(d);
1297 }
1298 if (!BG(serialize_lock) && !--BG(serialize).level) {
1299 BG(serialize).data = NULL;
1300 }
1301 }
1302
1303 /* {{{ Returns a string representation of variable (which can later be unserialized) */
PHP_FUNCTION(serialize)1304 PHP_FUNCTION(serialize)
1305 {
1306 zval *struc;
1307 php_serialize_data_t var_hash;
1308 smart_str buf = {0};
1309
1310 ZEND_PARSE_PARAMETERS_START(1, 1)
1311 Z_PARAM_ZVAL(struc)
1312 ZEND_PARSE_PARAMETERS_END();
1313
1314 PHP_VAR_SERIALIZE_INIT(var_hash);
1315 php_var_serialize(&buf, struc, &var_hash);
1316 PHP_VAR_SERIALIZE_DESTROY(var_hash);
1317
1318 if (EG(exception)) {
1319 smart_str_free(&buf);
1320 RETURN_THROWS();
1321 }
1322
1323 RETURN_STR(smart_str_extract(&buf));
1324 }
1325 /* }}} */
1326
1327 /* {{{ Takes a string representation of variable and recreates it, subject to the optional unserialize options HashTable */
php_unserialize_with_options(zval * return_value,const char * buf,const size_t buf_len,HashTable * options,const char * function_name)1328 PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, const size_t buf_len, HashTable *options, const char* function_name)
1329 {
1330 const unsigned char *p;
1331 php_unserialize_data_t var_hash;
1332 zval *retval;
1333 HashTable *class_hash = NULL, *prev_class_hash;
1334 zend_long prev_max_depth, prev_cur_depth;
1335
1336 if (buf_len == 0) {
1337 RETURN_FALSE;
1338 }
1339
1340 p = (const unsigned char*) buf;
1341 PHP_VAR_UNSERIALIZE_INIT(var_hash);
1342
1343 prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash);
1344 prev_max_depth = php_var_unserialize_get_max_depth(var_hash);
1345 prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash);
1346 if (options != NULL) {
1347 zval *classes, *max_depth;
1348
1349 classes = zend_hash_str_find_deref(options, "allowed_classes", sizeof("allowed_classes")-1);
1350 if (classes && Z_TYPE_P(classes) != IS_ARRAY && Z_TYPE_P(classes) != IS_TRUE && Z_TYPE_P(classes) != IS_FALSE) {
1351 zend_type_error("%s(): Option \"allowed_classes\" must be of type array|bool, %s given", function_name, zend_zval_value_name(classes));
1352 goto cleanup;
1353 }
1354
1355 if(classes && (Z_TYPE_P(classes) == IS_ARRAY || !zend_is_true(classes))) {
1356 ALLOC_HASHTABLE(class_hash);
1357 zend_hash_init(class_hash, (Z_TYPE_P(classes) == IS_ARRAY)?zend_hash_num_elements(Z_ARRVAL_P(classes)):0, NULL, NULL, 0);
1358 }
1359 if(class_hash && Z_TYPE_P(classes) == IS_ARRAY) {
1360 zval *entry;
1361 zend_string *lcname;
1362
1363 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classes), entry) {
1364 convert_to_string(entry);
1365 lcname = zend_string_tolower(Z_STR_P(entry));
1366 zend_hash_add_empty_element(class_hash, lcname);
1367 zend_string_release_ex(lcname, 0);
1368 } ZEND_HASH_FOREACH_END();
1369
1370 /* Exception during string conversion. */
1371 if (EG(exception)) {
1372 goto cleanup;
1373 }
1374 }
1375 php_var_unserialize_set_allowed_classes(var_hash, class_hash);
1376
1377 max_depth = zend_hash_str_find_deref(options, "max_depth", sizeof("max_depth") - 1);
1378 if (max_depth) {
1379 if (Z_TYPE_P(max_depth) != IS_LONG) {
1380 zend_type_error("%s(): Option \"max_depth\" must be of type int, %s given", function_name, zend_zval_value_name(max_depth));
1381 goto cleanup;
1382 }
1383 if (Z_LVAL_P(max_depth) < 0) {
1384 zend_value_error("%s(): Option \"max_depth\" must be greater than or equal to 0", function_name);
1385 goto cleanup;
1386 }
1387
1388 php_var_unserialize_set_max_depth(var_hash, Z_LVAL_P(max_depth));
1389 /* If the max_depth for a nested unserialize() call has been overridden,
1390 * start counting from zero again (for the nested call only). */
1391 php_var_unserialize_set_cur_depth(var_hash, 0);
1392 }
1393 }
1394
1395 if (BG(unserialize).level > 1) {
1396 retval = var_tmp_var(&var_hash);
1397 } else {
1398 retval = return_value;
1399 }
1400 if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) {
1401 if (!EG(exception)) {
1402 php_error_docref(NULL, E_WARNING, "Error at offset " ZEND_LONG_FMT " of %zd bytes",
1403 (zend_long)((char*)p - buf), buf_len);
1404 }
1405 if (BG(unserialize).level <= 1) {
1406 zval_ptr_dtor(return_value);
1407 }
1408 RETVAL_FALSE;
1409 } else if (BG(unserialize).level > 1) {
1410 ZVAL_COPY(return_value, retval);
1411 } else if (Z_REFCOUNTED_P(return_value)) {
1412 zend_refcounted *ref = Z_COUNTED_P(return_value);
1413 gc_check_possible_root(ref);
1414 }
1415
1416 cleanup:
1417 if (class_hash) {
1418 zend_hash_destroy(class_hash);
1419 FREE_HASHTABLE(class_hash);
1420 }
1421
1422 /* Reset to previous options in case this is a nested call */
1423 php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash);
1424 php_var_unserialize_set_max_depth(var_hash, prev_max_depth);
1425 php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth);
1426 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1427
1428 /* Per calling convention we must not return a reference here, so unwrap. We're doing this at
1429 * the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect
1430 * the value we unwrap here. This is compatible with behavior in PHP <=7.0. */
1431 if (Z_ISREF_P(return_value)) {
1432 zend_unwrap_reference(return_value);
1433 }
1434 }
1435 /* }}} */
1436
1437 /* {{{ Takes a string representation of variable and recreates it */
PHP_FUNCTION(unserialize)1438 PHP_FUNCTION(unserialize)
1439 {
1440 char *buf = NULL;
1441 size_t buf_len;
1442 HashTable *options = NULL;
1443
1444 ZEND_PARSE_PARAMETERS_START(1, 2)
1445 Z_PARAM_STRING(buf, buf_len)
1446 Z_PARAM_OPTIONAL
1447 Z_PARAM_ARRAY_HT(options)
1448 ZEND_PARSE_PARAMETERS_END();
1449
1450 php_unserialize_with_options(return_value, buf, buf_len, options, "unserialize");
1451 }
1452 /* }}} */
1453
1454 /* {{{ Returns the allocated by PHP memory */
PHP_FUNCTION(memory_get_usage)1455 PHP_FUNCTION(memory_get_usage) {
1456 bool real_usage = 0;
1457
1458 ZEND_PARSE_PARAMETERS_START(0, 1)
1459 Z_PARAM_OPTIONAL
1460 Z_PARAM_BOOL(real_usage)
1461 ZEND_PARSE_PARAMETERS_END();
1462
1463 RETURN_LONG(zend_memory_usage(real_usage));
1464 }
1465 /* }}} */
1466
1467 /* {{{ Returns the peak allocated by PHP memory */
PHP_FUNCTION(memory_get_peak_usage)1468 PHP_FUNCTION(memory_get_peak_usage) {
1469 bool real_usage = 0;
1470
1471 ZEND_PARSE_PARAMETERS_START(0, 1)
1472 Z_PARAM_OPTIONAL
1473 Z_PARAM_BOOL(real_usage)
1474 ZEND_PARSE_PARAMETERS_END();
1475
1476 RETURN_LONG(zend_memory_peak_usage(real_usage));
1477 }
1478 /* }}} */
1479
1480 /* {{{ Resets the peak PHP memory usage */
PHP_FUNCTION(memory_reset_peak_usage)1481 PHP_FUNCTION(memory_reset_peak_usage) {
1482 ZEND_PARSE_PARAMETERS_NONE();
1483
1484 zend_memory_reset_peak_usage();
1485 }
1486 /* }}} */
1487
1488 PHP_INI_BEGIN()
1489 STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals)
PHP_INI_END()1490 PHP_INI_END()
1491
1492 PHP_MINIT_FUNCTION(var)
1493 {
1494 REGISTER_INI_ENTRIES();
1495 return SUCCESS;
1496 }
1497