Coverage Report

Created: 2026-06-02 06:39

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