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 myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
347 if (myht) {
348 if (GC_IS_RECURSIVE(myht)) {
349 PUTS("*RECURSION*\n");
350 zend_release_properties(myht);
351 return;
352 }
353 GC_PROTECT_RECURSION(myht);
354 }
355 class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
356 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));
357 zend_string_release_ex(class_name, 0);
358 if (myht) {
359 ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
360 zend_property_info *prop_info = NULL;
361
362 if (Z_TYPE_P(val) == IS_INDIRECT) {
363 val = Z_INDIRECT_P(val);
364 if (key) {
365 prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
366 }
367 }
368
369 if (!Z_ISUNDEF_P(val) || prop_info) {
370 zval_object_property_dump(prop_info, val, index, key, level);
371 }
372 } ZEND_HASH_FOREACH_END();
373 GC_UNPROTECT_RECURSION(myht);
374 zend_release_properties(myht);
375 }
376 if (level > 1) {
377 php_printf("%*c", level - 1, ' ');
378 }
379 PUTS("}\n");
380 break;
381 case IS_RESOURCE: {
382 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
383 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));
384 break;
385 }
386 case IS_REFERENCE:
387 php_printf("reference refcount(%u) {\n", Z_REFCOUNT_P(struc));
388 php_debug_zval_dump(Z_REFVAL_P(struc), level + 2);
389 if (level > 1) {
390 php_printf("%*c", level - 1, ' ');
391 }
392 PUTS("}\n");
393 break;
394 default:
395 PUTS("UNKNOWN:0\n");
396 break;
397 }
398 }
399 /* }}} */
400
401 /* {{{ Dumps a string representation of an internal zend value to output. */
PHP_FUNCTION(debug_zval_dump)402 PHP_FUNCTION(debug_zval_dump)
403 {
404 zval *args;
405 int argc;
406 int i;
407
408 ZEND_PARSE_PARAMETERS_START(1, -1)
409 Z_PARAM_VARIADIC('+', args, argc)
410 ZEND_PARSE_PARAMETERS_END();
411
412 for (i = 0; i < argc; i++) {
413 php_debug_zval_dump(&args[i], 1);
414 }
415 }
416 /* }}} */
417
418 #define buffer_append_spaces(buf, num_spaces) \
419 do { \
420 char *tmp_spaces; \
421 size_t tmp_spaces_len; \
422 tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \
423 smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \
424 efree(tmp_spaces); \
425 } while(0);
426
php_array_element_export(zval * zv,zend_ulong index,zend_string * key,int level,smart_str * buf)427 static void php_array_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
428 {
429 if (key == NULL) { /* numeric key */
430 buffer_append_spaces(buf, level+1);
431 smart_str_append_long(buf, (zend_long) index);
432 smart_str_appendl(buf, " => ", 4);
433
434 } else { /* string key */
435 zend_string *tmp_str;
436 zend_string *ckey = php_addcslashes(key, "'\\", 2);
437 tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12);
438
439 buffer_append_spaces(buf, level + 1);
440
441 smart_str_appendc(buf, '\'');
442 smart_str_append(buf, tmp_str);
443 smart_str_appendl(buf, "' => ", 5);
444
445 zend_string_free(ckey);
446 zend_string_free(tmp_str);
447 }
448 php_var_export_ex(zv, level + 2, buf);
449
450 smart_str_appendc(buf, ',');
451 smart_str_appendc(buf, '\n');
452 }
453 /* }}} */
454
php_object_element_export(zval * zv,zend_ulong index,zend_string * key,int level,smart_str * buf)455 static void php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
456 {
457 buffer_append_spaces(buf, level + 2);
458 if (key != NULL) {
459 const char *class_name, *prop_name;
460 size_t prop_name_len;
461 zend_string *pname_esc;
462
463 zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len);
464 pname_esc = php_addcslashes_str(prop_name, prop_name_len, "'\\", 2);
465
466 smart_str_appendc(buf, '\'');
467 smart_str_append(buf, pname_esc);
468 smart_str_appendc(buf, '\'');
469 zend_string_release_ex(pname_esc, 0);
470 } else {
471 smart_str_append_long(buf, (zend_long) index);
472 }
473 smart_str_appendl(buf, " => ", 4);
474 php_var_export_ex(zv, level + 2, buf);
475 smart_str_appendc(buf, ',');
476 smart_str_appendc(buf, '\n');
477 }
478 /* }}} */
479
php_var_export_ex(zval * struc,int level,smart_str * buf)480 PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
481 {
482 HashTable *myht;
483 zend_string *ztmp, *ztmp2;
484 zend_ulong index;
485 zend_string *key;
486 zval *val;
487
488 again:
489 switch (Z_TYPE_P(struc)) {
490 case IS_FALSE:
491 smart_str_appendl(buf, "false", 5);
492 break;
493 case IS_TRUE:
494 smart_str_appendl(buf, "true", 4);
495 break;
496 case IS_NULL:
497 smart_str_appendl(buf, "NULL", 4);
498 break;
499 case IS_LONG:
500 /* INT_MIN as a literal will be parsed as a float. Emit something like
501 * -9223372036854775807-1 to avoid this. */
502 if (Z_LVAL_P(struc) == ZEND_LONG_MIN) {
503 smart_str_append_long(buf, ZEND_LONG_MIN+1);
504 smart_str_appends(buf, "-1");
505 break;
506 }
507 smart_str_append_long(buf, Z_LVAL_P(struc));
508 break;
509 case IS_DOUBLE:
510 smart_str_append_double(
511 buf, Z_DVAL_P(struc), (int) PG(serialize_precision), /* zero_fraction */ true);
512 break;
513 case IS_STRING:
514 ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2);
515 ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12);
516
517 smart_str_appendc(buf, '\'');
518 smart_str_append(buf, ztmp2);
519 smart_str_appendc(buf, '\'');
520
521 zend_string_free(ztmp);
522 zend_string_free(ztmp2);
523 break;
524 case IS_ARRAY:
525 myht = Z_ARRVAL_P(struc);
526 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
527 if (GC_IS_RECURSIVE(myht)) {
528 smart_str_appendl(buf, "NULL", 4);
529 zend_error(E_WARNING, "var_export does not handle circular references");
530 return;
531 }
532 GC_ADDREF(myht);
533 GC_PROTECT_RECURSION(myht);
534 }
535 if (level > 1) {
536 smart_str_appendc(buf, '\n');
537 buffer_append_spaces(buf, level - 1);
538 }
539 smart_str_appendl(buf, "array (\n", 8);
540 ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
541 php_array_element_export(val, index, key, level, buf);
542 } ZEND_HASH_FOREACH_END();
543 if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
544 GC_UNPROTECT_RECURSION(myht);
545 GC_DELREF(myht);
546 }
547 if (level > 1) {
548 buffer_append_spaces(buf, level - 1);
549 }
550 smart_str_appendc(buf, ')');
551
552 break;
553
554 case IS_OBJECT:
555 myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT);
556 if (myht) {
557 if (GC_IS_RECURSIVE(myht)) {
558 smart_str_appendl(buf, "NULL", 4);
559 zend_error(E_WARNING, "var_export does not handle circular references");
560 zend_release_properties(myht);
561 return;
562 } else {
563 GC_TRY_PROTECT_RECURSION(myht);
564 }
565 }
566 if (level > 1) {
567 smart_str_appendc(buf, '\n');
568 buffer_append_spaces(buf, level - 1);
569 }
570
571 zend_class_entry *ce = Z_OBJCE_P(struc);
572 bool is_enum = ce->ce_flags & ZEND_ACC_ENUM;
573
574 /* stdClass has no __set_state method, but can be casted to */
575 if (ce == zend_standard_class_def) {
576 smart_str_appendl(buf, "(object) array(\n", 16);
577 } else {
578 smart_str_append(buf, ce->name);
579 if (is_enum) {
580 zend_object *zobj = Z_OBJ_P(struc);
581 zval *case_name_zval = zend_enum_fetch_case_name(zobj);
582 smart_str_appendl(buf, "::", 2);
583 smart_str_append(buf, Z_STR_P(case_name_zval));
584 } else {
585 smart_str_appendl(buf, "::__set_state(array(\n", 21);
586 }
587 }
588
589 if (myht) {
590 if (!is_enum) {
591 ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
592 php_object_element_export(val, index, key, level, buf);
593 } ZEND_HASH_FOREACH_END();
594 }
595 GC_TRY_UNPROTECT_RECURSION(myht);
596 zend_release_properties(myht);
597 }
598 if (level > 1 && !is_enum) {
599 buffer_append_spaces(buf, level - 1);
600 }
601 if (ce == zend_standard_class_def) {
602 smart_str_appendc(buf, ')');
603 } else if (!is_enum) {
604 smart_str_appendl(buf, "))", 2);
605 }
606
607 break;
608 case IS_REFERENCE:
609 struc = Z_REFVAL_P(struc);
610 goto again;
611 break;
612 default:
613 smart_str_appendl(buf, "NULL", 4);
614 break;
615 }
616 }
617 /* }}} */
618
619 /* FOR BC reasons, this will always perform and then print */
php_var_export(zval * struc,int level)620 PHPAPI void php_var_export(zval *struc, int level) /* {{{ */
621 {
622 smart_str buf = {0};
623 php_var_export_ex(struc, level, &buf);
624 smart_str_0(&buf);
625 PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
626 smart_str_free(&buf);
627 }
628 /* }}} */
629
630 /* {{{ Outputs or returns a string representation of a variable */
PHP_FUNCTION(var_export)631 PHP_FUNCTION(var_export)
632 {
633 zval *var;
634 bool return_output = 0;
635 smart_str buf = {0};
636
637 ZEND_PARSE_PARAMETERS_START(1, 2)
638 Z_PARAM_ZVAL(var)
639 Z_PARAM_OPTIONAL
640 Z_PARAM_BOOL(return_output)
641 ZEND_PARSE_PARAMETERS_END();
642
643 php_var_export_ex(var, 1, &buf);
644 smart_str_0 (&buf);
645
646 if (return_output) {
647 RETURN_NEW_STR(buf.s);
648 } else {
649 PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
650 smart_str_free(&buf);
651 }
652 }
653 /* }}} */
654
655 static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash, bool in_rcn_array, bool is_root);
656
657 /**
658 * @param bool in_rcn_array Whether the element appears in a potentially nested array with RC > 1.
659 */
php_add_var_hash(php_serialize_data_t data,zval * var,bool in_rcn_array)660 static inline zend_long php_add_var_hash(php_serialize_data_t data, zval *var, bool in_rcn_array) /* {{{ */
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 (!in_rcn_array
673 && Z_REFCOUNT_P(var) == 1
674 && (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1)) {
675 return 0;
676 }
677
678 /* References to objects are treated as if the reference didn't exist */
679 if (is_ref && Z_TYPE_P(Z_REFVAL_P(var)) == IS_OBJECT) {
680 var = Z_REFVAL_P(var);
681 }
682
683 /* Index for the variable is stored using the numeric value of the pointer to
684 * the zend_refcounted struct */
685 key = (zend_ulong) (zend_uintptr_t) Z_COUNTED_P(var);
686 zv = zend_hash_index_find(&data->ht, key);
687
688 if (zv) {
689 /* References are only counted once, undo the data->n increment above */
690 if (is_ref && Z_LVAL_P(zv) != -1) {
691 data->n -= 1;
692 }
693
694 return Z_LVAL_P(zv);
695 } else {
696 zval zv_n;
697 ZVAL_LONG(&zv_n, data->n);
698 zend_hash_index_add_new(&data->ht, key, &zv_n);
699
700 /* Additionally to the index, we also store the variable, to ensure that it is
701 * not destroyed during serialization and its pointer reused. The variable is
702 * stored at the numeric value of the pointer + 1, which cannot be the location
703 * of another zend_refcounted structure. */
704 zend_hash_index_add_new(&data->ht, key + 1, var);
705 Z_ADDREF_P(var);
706
707 return 0;
708 }
709 }
710 /* }}} */
711
php_var_serialize_long(smart_str * buf,zend_long val)712 static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */
713 {
714 char b[32];
715 char *s = zend_print_long_to_buf(b + sizeof(b) - 1, val);
716 size_t l = b + sizeof(b) - 1 - s;
717 char *res = smart_str_extend(buf, 2 + l + 1);
718 memcpy(res, "i:", 2);
719 res += 2;
720 memcpy(res, s, l);
721 res[l] = ';';
722 }
723 /* }}} */
724
php_var_serialize_string(smart_str * buf,char * str,size_t len)725 static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */
726 {
727 char b[32];
728 char *s = zend_print_long_to_buf(b + sizeof(b) - 1, len);
729 size_t l = b + sizeof(b) - 1 - s;
730 char *res = smart_str_extend(buf, 2 + l + 2 + len + 2);
731 memcpy(res, "s:", 2);
732 res += 2;
733 memcpy(res, s, l);
734 res += l;
735 memcpy(res, ":\"", 2);
736 res += 2;
737 memcpy(res, str, len);
738 res += len;
739 memcpy(res, "\";", 2);
740 }
741 /* }}} */
742
php_var_serialize_class_name(smart_str * buf,zval * struc)743 static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */
744 {
745 char b[32];
746 PHP_CLASS_ATTRIBUTES;
747
748 PHP_SET_CLASS_ATTRIBUTES(struc);
749 size_t class_name_len = ZSTR_LEN(class_name);
750 char *s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len);
751 size_t l = b + sizeof(b) - 1 - s;
752 char *res = smart_str_extend(buf, 2 + l + 2 + class_name_len + 2);
753 memcpy(res, "O:", 2);
754 res += 2;
755 memcpy(res, s, l);
756 res += l;
757 memcpy(res, ":\"", 2);
758 res += 2;
759 memcpy(res, ZSTR_VAL(class_name), class_name_len);
760 res += class_name_len;
761 memcpy(res, "\":", 2);
762 PHP_CLEANUP_CLASS_ATTRIBUTES();
763 return incomplete_class;
764 }
765 /* }}} */
766
php_var_serialize_call_sleep(zend_object * obj,zend_function * fn)767 static HashTable* php_var_serialize_call_sleep(zend_object *obj, zend_function *fn) /* {{{ */
768 {
769 zend_result res;
770 zend_fcall_info fci;
771 zend_fcall_info_cache fci_cache;
772 zval retval;
773
774 fci.size = sizeof(fci);
775 fci.object = obj;
776 fci.retval = &retval;
777 fci.param_count = 0;
778 fci.params = NULL;
779 fci.named_params = NULL;
780 ZVAL_UNDEF(&fci.function_name);
781
782 fci_cache.function_handler = fn;
783 fci_cache.object = obj;
784 fci_cache.called_scope = obj->ce;
785
786 BG(serialize_lock)++;
787 res = zend_call_function(&fci, &fci_cache);
788 BG(serialize_lock)--;
789
790 if (res == FAILURE || Z_ISUNDEF(retval)) {
791 zval_ptr_dtor(&retval);
792 return NULL;
793 }
794
795 if (Z_TYPE(retval) != IS_ARRAY) {
796 zval_ptr_dtor(&retval);
797 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));
798 return NULL;
799 }
800
801 return Z_ARRVAL(retval);
802 }
803 /* }}} */
804
php_var_serialize_call_magic_serialize(zval * retval,zval * obj)805 static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */
806 {
807 BG(serialize_lock)++;
808 zend_call_known_instance_method_with_0_params(
809 Z_OBJCE_P(obj)->__serialize, Z_OBJ_P(obj), retval);
810 BG(serialize_lock)--;
811
812 if (EG(exception)) {
813 zval_ptr_dtor(retval);
814 return FAILURE;
815 }
816
817 if (Z_TYPE_P(retval) != IS_ARRAY) {
818 zval_ptr_dtor(retval);
819 zend_type_error("%s::__serialize() must return an array", ZSTR_VAL(Z_OBJCE_P(obj)->name));
820 return FAILURE;
821 }
822
823 return SUCCESS;
824 }
825 /* }}} */
826
php_var_serialize_try_add_sleep_prop(HashTable * ht,HashTable * props,zend_string * name,zend_string * error_name,zval * struc)827 static int php_var_serialize_try_add_sleep_prop(
828 HashTable *ht, HashTable *props, zend_string *name, zend_string *error_name, zval *struc) /* {{{ */
829 {
830 zval *val = zend_hash_find(props, name);
831 if (val == NULL) {
832 return FAILURE;
833 }
834
835 if (Z_TYPE_P(val) == IS_INDIRECT) {
836 val = Z_INDIRECT_P(val);
837 if (Z_TYPE_P(val) == IS_UNDEF) {
838 zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
839 if (info) {
840 return SUCCESS;
841 }
842 return FAILURE;
843 }
844 }
845
846 if (!zend_hash_add(ht, name, val)) {
847 php_error_docref(NULL, E_NOTICE,
848 "\"%s\" is returned from __sleep() multiple times", ZSTR_VAL(error_name));
849 return SUCCESS;
850 }
851
852 Z_TRY_ADDREF_P(val);
853 return SUCCESS;
854 }
855 /* }}} */
856
php_var_serialize_get_sleep_props(HashTable * ht,zval * struc,HashTable * sleep_retval)857 static int php_var_serialize_get_sleep_props(
858 HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */
859 {
860 zend_class_entry *ce = Z_OBJCE_P(struc);
861 HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
862 zval *name_val;
863 int retval = SUCCESS;
864
865 zend_hash_init(ht, zend_hash_num_elements(sleep_retval), NULL, ZVAL_PTR_DTOR, 0);
866 /* TODO: Rewrite this by fetching the property info instead of trying out different
867 * name manglings? */
868 ZEND_HASH_FOREACH_VAL_IND(sleep_retval, name_val) {
869 zend_string *name, *tmp_name, *priv_name, *prot_name;
870
871 ZVAL_DEREF(name_val);
872 if (Z_TYPE_P(name_val) != IS_STRING) {
873 php_error_docref(NULL, E_WARNING,
874 "%s::__sleep() should return an array only containing the names of instance-variables to serialize",
875 ZSTR_VAL(ce->name));
876 }
877
878 name = zval_get_tmp_string(name_val, &tmp_name);
879 if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) {
880 zend_tmp_string_release(tmp_name);
881 continue;
882 }
883
884 if (EG(exception)) {
885 zend_tmp_string_release(tmp_name);
886 retval = FAILURE;
887 break;
888 }
889
890 priv_name = zend_mangle_property_name(
891 ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
892 ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
893 if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) {
894 zend_tmp_string_release(tmp_name);
895 zend_string_release(priv_name);
896 continue;
897 }
898 zend_string_release(priv_name);
899
900 if (EG(exception)) {
901 zend_tmp_string_release(tmp_name);
902 retval = FAILURE;
903 break;
904 }
905
906 prot_name = zend_mangle_property_name(
907 "*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
908 if (php_var_serialize_try_add_sleep_prop(ht, props, prot_name, name, struc) == SUCCESS) {
909 zend_tmp_string_release(tmp_name);
910 zend_string_release(prot_name);
911 continue;
912 }
913 zend_string_release(prot_name);
914
915 if (EG(exception)) {
916 zend_tmp_string_release(tmp_name);
917 retval = FAILURE;
918 break;
919 }
920
921 php_error_docref(NULL, E_WARNING,
922 "\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name));
923 zend_tmp_string_release(tmp_name);
924 } ZEND_HASH_FOREACH_END();
925
926 zend_release_properties(props);
927 return retval;
928 }
929 /* }}} */
930
php_var_serialize_nested_data(smart_str * buf,zval * struc,HashTable * ht,uint32_t count,bool incomplete_class,php_serialize_data_t var_hash,bool in_rcn_array)931 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, bool in_rcn_array) /* {{{ */
932 {
933 smart_str_append_unsigned(buf, count);
934 smart_str_appendl(buf, ":{", 2);
935 if (count > 0) {
936 zend_string *key;
937 zval *data;
938 zend_ulong index;
939
940 ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) {
941 if (incomplete_class && zend_string_equals_literal(key, MAGIC_MEMBER)) {
942 incomplete_class = 0;
943 continue;
944 }
945
946 if (!key) {
947 php_var_serialize_long(buf, index);
948 } else {
949 php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
950 }
951
952 if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
953 data = Z_REFVAL_P(data);
954 }
955
956 /* we should still add element even if it's not OK,
957 * since we already wrote the length of the array before */
958 if (Z_TYPE_P(data) == IS_ARRAY) {
959 if (UNEXPECTED(Z_IS_RECURSIVE_P(data))
960 || UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
961 php_add_var_hash(var_hash, struc, in_rcn_array);
962 smart_str_appendl(buf, "N;", 2);
963 } else {
964 if (Z_REFCOUNTED_P(data)) {
965 Z_PROTECT_RECURSION_P(data);
966 }
967 php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
968 if (Z_REFCOUNTED_P(data)) {
969 Z_UNPROTECT_RECURSION_P(data);
970 }
971 }
972 } else {
973 php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
974 }
975 } ZEND_HASH_FOREACH_END();
976 }
977 smart_str_appendc(buf, '}');
978 }
979 /* }}} */
980
php_var_serialize_class(smart_str * buf,zval * struc,HashTable * ht,php_serialize_data_t var_hash)981 static void php_var_serialize_class(smart_str *buf, zval *struc, HashTable *ht, php_serialize_data_t var_hash) /* {{{ */
982 {
983 HashTable props;
984
985 if (php_var_serialize_get_sleep_props(&props, struc, ht) == SUCCESS) {
986 php_var_serialize_class_name(buf, struc);
987 php_var_serialize_nested_data(
988 buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ 0, var_hash, GC_REFCOUNT(&props) > 1);
989 }
990 zend_hash_destroy(&props);
991 }
992 /* }}} */
993
php_var_serialize_intern(smart_str * buf,zval * struc,php_serialize_data_t var_hash,bool in_rcn_array,bool is_root)994 static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash, bool in_rcn_array, bool is_root) /* {{{ */
995 {
996 zend_long var_already;
997 HashTable *myht;
998
999 if (EG(exception)) {
1000 return;
1001 }
1002
1003 if (var_hash && (var_already = php_add_var_hash(var_hash, struc, in_rcn_array))) {
1004 if (var_already == -1) {
1005 /* Reference to an object that failed to serialize, replace with null. */
1006 smart_str_appendl(buf, "N;", 2);
1007 return;
1008 } else if (Z_ISREF_P(struc)) {
1009 smart_str_appendl(buf, "R:", 2);
1010 smart_str_append_long(buf, var_already);
1011 smart_str_appendc(buf, ';');
1012 return;
1013 } else if (Z_TYPE_P(struc) == IS_OBJECT) {
1014 smart_str_appendl(buf, "r:", 2);
1015 smart_str_append_long(buf, var_already);
1016 smart_str_appendc(buf, ';');
1017 return;
1018 }
1019 }
1020
1021 again:
1022 switch (Z_TYPE_P(struc)) {
1023 case IS_FALSE:
1024 smart_str_appendl(buf, "b:0;", 4);
1025 return;
1026
1027 case IS_TRUE:
1028 smart_str_appendl(buf, "b:1;", 4);
1029 return;
1030
1031 case IS_NULL:
1032 smart_str_appendl(buf, "N;", 2);
1033 return;
1034
1035 case IS_LONG:
1036 php_var_serialize_long(buf, Z_LVAL_P(struc));
1037 return;
1038
1039 case IS_DOUBLE: {
1040 char tmp_str[ZEND_DOUBLE_MAX_LENGTH];
1041 zend_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
1042
1043 size_t len = strlen(tmp_str);
1044 char *res = smart_str_extend(buf, 2 + len + 1);
1045 memcpy(res, "d:", 2);
1046 res += 2;
1047 memcpy(res, tmp_str, len);
1048 res[len] = ';';
1049 return;
1050 }
1051
1052 case IS_STRING:
1053 php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));
1054 return;
1055
1056 case IS_OBJECT: {
1057 zend_class_entry *ce = Z_OBJCE_P(struc);
1058 bool incomplete_class;
1059 uint32_t count;
1060
1061 if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1062 zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed",
1063 ZSTR_VAL(ce->name));
1064 return;
1065 }
1066
1067 if (ce->ce_flags & ZEND_ACC_ENUM) {
1068 PHP_CLASS_ATTRIBUTES;
1069
1070 zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
1071
1072 PHP_SET_CLASS_ATTRIBUTES(struc);
1073 smart_str_appendl(buf, "E:", 2);
1074 smart_str_append_unsigned(buf, ZSTR_LEN(class_name) + strlen(":") + Z_STRLEN_P(case_name_zval));
1075 smart_str_appendl(buf, ":\"", 2);
1076 smart_str_append(buf, class_name);
1077 smart_str_appendc(buf, ':');
1078 smart_str_append(buf, Z_STR_P(case_name_zval));
1079 smart_str_appendl(buf, "\";", 2);
1080 PHP_CLEANUP_CLASS_ATTRIBUTES();
1081 return;
1082 }
1083
1084 if (ce->__serialize) {
1085 zval retval, obj;
1086 zend_string *key;
1087 zval *data;
1088 zend_ulong index;
1089
1090 ZVAL_OBJ_COPY(&obj, Z_OBJ_P(struc));
1091 if (php_var_serialize_call_magic_serialize(&retval, &obj) == FAILURE) {
1092 if (!EG(exception)) {
1093 smart_str_appendl(buf, "N;", 2);
1094 }
1095 zval_ptr_dtor(&obj);
1096 return;
1097 }
1098
1099 php_var_serialize_class_name(buf, &obj);
1100 smart_str_append_unsigned(buf, zend_hash_num_elements(Z_ARRVAL(retval)));
1101 smart_str_appendl(buf, ":{", 2);
1102 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(retval), index, key, data) {
1103 if (!key) {
1104 php_var_serialize_long(buf, index);
1105 } else {
1106 php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
1107 }
1108
1109 if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
1110 data = Z_REFVAL_P(data);
1111 }
1112 php_var_serialize_intern(buf, data, var_hash, Z_REFCOUNT(retval) > 1, false);
1113 } ZEND_HASH_FOREACH_END();
1114 smart_str_appendc(buf, '}');
1115
1116 zval_ptr_dtor(&obj);
1117 zval_ptr_dtor(&retval);
1118 return;
1119 }
1120
1121 if (ce->serialize != NULL) {
1122 /* has custom handler */
1123 unsigned char *serialized_data = NULL;
1124 size_t serialized_length;
1125
1126 if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) {
1127 char b1[32], b2[32];
1128 char *s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name));
1129 size_t l1 = b1 + sizeof(b1) - 1 - s1;
1130 char *s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length);
1131 size_t l2 = b2 + sizeof(b2) - 1 - s2;
1132 char *res = smart_str_extend(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1);
1133 memcpy(res, "C:", 2);
1134 res += 2;
1135 memcpy(res, s1, l1);
1136 res += l1;
1137 memcpy(res, ":\"", 2);
1138 res += 2;
1139 memcpy(res, ZSTR_VAL(Z_OBJCE_P(struc)->name), ZSTR_LEN(Z_OBJCE_P(struc)->name));
1140 res += ZSTR_LEN(Z_OBJCE_P(struc)->name);
1141 memcpy(res, "\":", 2);
1142 res += 2;
1143
1144 memcpy(res, s2, l2);
1145 res += l2;
1146 memcpy(res, ":{", 2);
1147 res += 2;
1148 memcpy(res, (char *) serialized_data, serialized_length);
1149 res[serialized_length] = '}';
1150 } else {
1151 /* Mark this value in the var_hash, to avoid creating references to it. */
1152 zval *var_idx = zend_hash_index_find(&var_hash->ht,
1153 (zend_ulong) (zend_uintptr_t) Z_COUNTED_P(struc));
1154 if (var_idx) {
1155 ZVAL_LONG(var_idx, -1);
1156 }
1157 smart_str_appendl(buf, "N;", 2);
1158 }
1159 if (serialized_data) {
1160 efree(serialized_data);
1161 }
1162 return;
1163 }
1164
1165 if (ce != PHP_IC_ENTRY) {
1166 zval *zv = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP));
1167
1168 if (zv) {
1169 HashTable *ht;
1170 zval tmp;
1171
1172 ZVAL_OBJ_COPY(&tmp, Z_OBJ_P(struc));
1173 if (!(ht = php_var_serialize_call_sleep(Z_OBJ(tmp), Z_FUNC_P(zv)))) {
1174 if (!EG(exception)) {
1175 /* we should still add element even if it's not OK,
1176 * since we already wrote the length of the array before */
1177 smart_str_appendl(buf, "N;", 2);
1178 }
1179 OBJ_RELEASE(Z_OBJ(tmp));
1180 return;
1181 }
1182
1183 php_var_serialize_class(buf, &tmp, ht, var_hash);
1184 zend_array_release(ht);
1185 OBJ_RELEASE(Z_OBJ(tmp));
1186 return;
1187 }
1188 }
1189
1190 incomplete_class = php_var_serialize_class_name(buf, struc);
1191
1192 if (Z_OBJ_P(struc)->properties == NULL
1193 && Z_OBJ_HT_P(struc)->get_properties_for == NULL
1194 && Z_OBJ_HT_P(struc)->get_properties == zend_std_get_properties) {
1195 /* Optimized version without rebulding properties HashTable */
1196 zend_object *obj = Z_OBJ_P(struc);
1197 zend_class_entry *ce = obj->ce;
1198 zend_property_info *prop_info;
1199 zval *prop;
1200 int i;
1201
1202 count = ce->default_properties_count;
1203 for (i = 0; i < ce->default_properties_count; i++) {
1204 prop_info = ce->properties_info_table[i];
1205 if (!prop_info) {
1206 count--;
1207 continue;
1208 }
1209 prop = OBJ_PROP(obj, prop_info->offset);
1210 if (Z_TYPE_P(prop) == IS_UNDEF) {
1211 count--;
1212 continue;
1213 }
1214 }
1215 if (count) {
1216 smart_str_append_unsigned(buf, count);
1217 smart_str_appendl(buf, ":{", 2);
1218 for (i = 0; i < ce->default_properties_count; i++) {
1219 prop_info = ce->properties_info_table[i];
1220 if (!prop_info) {
1221 continue;
1222 }
1223 prop = OBJ_PROP(obj, prop_info->offset);
1224 if (Z_TYPE_P(prop) == IS_UNDEF) {
1225 continue;
1226 }
1227
1228 php_var_serialize_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name));
1229
1230 if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
1231 prop = Z_REFVAL_P(prop);
1232 }
1233
1234 php_var_serialize_intern(buf, prop, var_hash, false, false);
1235 }
1236 smart_str_appendc(buf, '}');
1237 } else {
1238 smart_str_appendl(buf, "0:{}", 4);
1239 }
1240 return;
1241 }
1242 myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
1243 /* count after serializing name, since php_var_serialize_class_name
1244 * changes the count if the variable is incomplete class */
1245 count = zend_array_count(myht);
1246 if (count > 0 && incomplete_class) {
1247 --count;
1248 }
1249 php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash, GC_REFCOUNT(myht) > 1);
1250 zend_release_properties(myht);
1251 return;
1252 }
1253 case IS_ARRAY:
1254 smart_str_appendl(buf, "a:", 2);
1255 myht = Z_ARRVAL_P(struc);
1256 php_var_serialize_nested_data(
1257 buf, struc, myht, zend_array_count(myht), /* incomplete_class */ 0, var_hash,
1258 !is_root && (in_rcn_array || GC_REFCOUNT(myht) > 1));
1259 return;
1260 case IS_REFERENCE:
1261 struc = Z_REFVAL_P(struc);
1262 goto again;
1263 default:
1264 smart_str_appendl(buf, "i:0;", 4);
1265 return;
1266 }
1267 }
1268 /* }}} */
1269
php_var_serialize(smart_str * buf,zval * struc,php_serialize_data_t * data)1270 PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */
1271 {
1272 php_var_serialize_intern(buf, struc, *data, false, true);
1273 smart_str_0(buf);
1274 }
1275 /* }}} */
1276
php_var_serialize_init(void)1277 PHPAPI php_serialize_data_t php_var_serialize_init(void) {
1278 struct php_serialize_data *d;
1279 /* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
1280 if (BG(serialize_lock) || !BG(serialize).level) {
1281 d = emalloc(sizeof(struct php_serialize_data));
1282 zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0);
1283 d->n = 0;
1284 if (!BG(serialize_lock)) {
1285 BG(serialize).data = d;
1286 BG(serialize).level = 1;
1287 }
1288 } else {
1289 d = BG(serialize).data;
1290 ++BG(serialize).level;
1291 }
1292 return d;
1293 }
1294
php_var_serialize_destroy(php_serialize_data_t d)1295 PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) {
1296 /* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
1297 if (BG(serialize_lock) || BG(serialize).level == 1) {
1298 zend_hash_destroy(&d->ht);
1299 efree(d);
1300 }
1301 if (!BG(serialize_lock) && !--BG(serialize).level) {
1302 BG(serialize).data = NULL;
1303 }
1304 }
1305
1306 /* {{{ Returns a string representation of variable (which can later be unserialized) */
PHP_FUNCTION(serialize)1307 PHP_FUNCTION(serialize)
1308 {
1309 zval *struc;
1310 php_serialize_data_t var_hash;
1311 smart_str buf = {0};
1312
1313 ZEND_PARSE_PARAMETERS_START(1, 1)
1314 Z_PARAM_ZVAL(struc)
1315 ZEND_PARSE_PARAMETERS_END();
1316
1317 PHP_VAR_SERIALIZE_INIT(var_hash);
1318 php_var_serialize(&buf, struc, &var_hash);
1319 PHP_VAR_SERIALIZE_DESTROY(var_hash);
1320
1321 if (EG(exception)) {
1322 smart_str_free(&buf);
1323 RETURN_THROWS();
1324 }
1325
1326 if (buf.s) {
1327 RETURN_NEW_STR(buf.s);
1328 } else {
1329 RETURN_EMPTY_STRING();
1330 }
1331 }
1332 /* }}} */
1333
1334 /* {{{ 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)1335 PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, const size_t buf_len, HashTable *options, const char* function_name)
1336 {
1337 const unsigned char *p;
1338 php_unserialize_data_t var_hash;
1339 zval *retval;
1340 HashTable *class_hash = NULL, *prev_class_hash;
1341 zend_long prev_max_depth, prev_cur_depth;
1342
1343 if (buf_len == 0) {
1344 RETURN_FALSE;
1345 }
1346
1347 p = (const unsigned char*) buf;
1348 PHP_VAR_UNSERIALIZE_INIT(var_hash);
1349
1350 prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash);
1351 prev_max_depth = php_var_unserialize_get_max_depth(var_hash);
1352 prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash);
1353 if (options != NULL) {
1354 zval *classes, *max_depth;
1355
1356 classes = zend_hash_str_find_deref(options, "allowed_classes", sizeof("allowed_classes")-1);
1357 if (classes && Z_TYPE_P(classes) != IS_ARRAY && Z_TYPE_P(classes) != IS_TRUE && Z_TYPE_P(classes) != IS_FALSE) {
1358 zend_type_error("%s(): Option \"allowed_classes\" must be of type array|bool, %s given", function_name, zend_zval_type_name(classes));
1359 goto cleanup;
1360 }
1361
1362 if(classes && (Z_TYPE_P(classes) == IS_ARRAY || !zend_is_true(classes))) {
1363 ALLOC_HASHTABLE(class_hash);
1364 zend_hash_init(class_hash, (Z_TYPE_P(classes) == IS_ARRAY)?zend_hash_num_elements(Z_ARRVAL_P(classes)):0, NULL, NULL, 0);
1365 }
1366 if(class_hash && Z_TYPE_P(classes) == IS_ARRAY) {
1367 zval *entry;
1368 zend_string *lcname;
1369
1370 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classes), entry) {
1371 convert_to_string(entry);
1372 lcname = zend_string_tolower(Z_STR_P(entry));
1373 zend_hash_add_empty_element(class_hash, lcname);
1374 zend_string_release_ex(lcname, 0);
1375 } ZEND_HASH_FOREACH_END();
1376
1377 /* Exception during string conversion. */
1378 if (EG(exception)) {
1379 goto cleanup;
1380 }
1381 }
1382 php_var_unserialize_set_allowed_classes(var_hash, class_hash);
1383
1384 max_depth = zend_hash_str_find_deref(options, "max_depth", sizeof("max_depth") - 1);
1385 if (max_depth) {
1386 if (Z_TYPE_P(max_depth) != IS_LONG) {
1387 zend_type_error("%s(): Option \"max_depth\" must be of type int, %s given", function_name, zend_zval_type_name(max_depth));
1388 goto cleanup;
1389 }
1390 if (Z_LVAL_P(max_depth) < 0) {
1391 zend_value_error("%s(): Option \"max_depth\" must be greater than or equal to 0", function_name);
1392 goto cleanup;
1393 }
1394
1395 php_var_unserialize_set_max_depth(var_hash, Z_LVAL_P(max_depth));
1396 /* If the max_depth for a nested unserialize() call has been overridden,
1397 * start counting from zero again (for the nested call only). */
1398 php_var_unserialize_set_cur_depth(var_hash, 0);
1399 }
1400 }
1401
1402 if (BG(unserialize).level > 1) {
1403 retval = var_tmp_var(&var_hash);
1404 } else {
1405 retval = return_value;
1406 }
1407 if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) {
1408 if (!EG(exception)) {
1409 php_error_docref(NULL, E_NOTICE, "Error at offset " ZEND_LONG_FMT " of %zd bytes",
1410 (zend_long)((char*)p - buf), buf_len);
1411 }
1412 if (BG(unserialize).level <= 1) {
1413 zval_ptr_dtor(return_value);
1414 }
1415 RETVAL_FALSE;
1416 } else if (BG(unserialize).level > 1) {
1417 ZVAL_COPY(return_value, retval);
1418 } else if (Z_REFCOUNTED_P(return_value)) {
1419 zend_refcounted *ref = Z_COUNTED_P(return_value);
1420 gc_check_possible_root(ref);
1421 }
1422
1423 cleanup:
1424 if (class_hash) {
1425 zend_hash_destroy(class_hash);
1426 FREE_HASHTABLE(class_hash);
1427 }
1428
1429 /* Reset to previous options in case this is a nested call */
1430 php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash);
1431 php_var_unserialize_set_max_depth(var_hash, prev_max_depth);
1432 php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth);
1433 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1434
1435 /* Per calling convention we must not return a reference here, so unwrap. We're doing this at
1436 * the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect
1437 * the value we unwrap here. This is compatible with behavior in PHP <=7.0. */
1438 if (Z_ISREF_P(return_value)) {
1439 zend_unwrap_reference(return_value);
1440 }
1441 }
1442 /* }}} */
1443
1444 /* {{{ Takes a string representation of variable and recreates it */
PHP_FUNCTION(unserialize)1445 PHP_FUNCTION(unserialize)
1446 {
1447 char *buf = NULL;
1448 size_t buf_len;
1449 HashTable *options = NULL;
1450
1451 ZEND_PARSE_PARAMETERS_START(1, 2)
1452 Z_PARAM_STRING(buf, buf_len)
1453 Z_PARAM_OPTIONAL
1454 Z_PARAM_ARRAY_HT(options)
1455 ZEND_PARSE_PARAMETERS_END();
1456
1457 php_unserialize_with_options(return_value, buf, buf_len, options, "unserialize");
1458 }
1459 /* }}} */
1460
1461 /* {{{ Returns the allocated by PHP memory */
PHP_FUNCTION(memory_get_usage)1462 PHP_FUNCTION(memory_get_usage) {
1463 bool real_usage = 0;
1464
1465 ZEND_PARSE_PARAMETERS_START(0, 1)
1466 Z_PARAM_OPTIONAL
1467 Z_PARAM_BOOL(real_usage)
1468 ZEND_PARSE_PARAMETERS_END();
1469
1470 RETURN_LONG(zend_memory_usage(real_usage));
1471 }
1472 /* }}} */
1473
1474 /* {{{ Returns the peak allocated by PHP memory */
PHP_FUNCTION(memory_get_peak_usage)1475 PHP_FUNCTION(memory_get_peak_usage) {
1476 bool real_usage = 0;
1477
1478 ZEND_PARSE_PARAMETERS_START(0, 1)
1479 Z_PARAM_OPTIONAL
1480 Z_PARAM_BOOL(real_usage)
1481 ZEND_PARSE_PARAMETERS_END();
1482
1483 RETURN_LONG(zend_memory_peak_usage(real_usage));
1484 }
1485 /* }}} */
1486
1487 PHP_INI_BEGIN()
1488 STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals)
PHP_INI_END()1489 PHP_INI_END()
1490
1491 PHP_MINIT_FUNCTION(var)
1492 {
1493 REGISTER_INI_ENTRIES();
1494 return SUCCESS;
1495 }
1496