Coverage Report

Created: 2026-06-02 06:40

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
201k
#define COMMON (is_ref ? "&" : "")
39
40
static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
41
112k
{
42
112k
  if (key == NULL) { /* numeric key */
43
5.60k
    php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
44
106k
  } else { /* string key */
45
106k
    php_printf("%*c[\"", level + 1, ' ');
46
106k
    PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
47
106k
    php_printf("\"]=>\n");
48
106k
  }
49
112k
  php_var_dump(zv, level + 2);
50
112k
}
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
3.39k
{
55
3.39k
  const char *prop_name, *class_name;
56
57
3.39k
#ifdef ZEND_CHECK_STACK_LIMIT
58
3.39k
  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
3.39k
#endif
63
3.39k
  if (key == NULL) { /* numeric key */
64
106
    php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
65
3.28k
  } else { /* string key */
66
3.28k
    int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
67
3.28k
    php_printf("%*c[", level + 1, ' ');
68
69
3.28k
    if (class_name && unmangle == SUCCESS) {
70
311
      if (class_name[0] == '*') {
71
32
        php_printf("\"%s\":protected", prop_name);
72
279
      } else {
73
279
        php_printf("\"%s\":\"%s\":private", prop_name, class_name);
74
279
      }
75
2.97k
    } else {
76
2.97k
      php_printf("\"");
77
2.97k
      PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
78
2.97k
      php_printf("\"");
79
2.97k
    }
80
3.28k
    ZEND_PUTS("]=>\n");
81
3.28k
  }
82
83
3.39k
  if (Z_TYPE_P(zv) == IS_UNDEF) {
84
326
    ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type));
85
326
    zend_string *type_str = zend_type_to_string(prop_info->type);
86
326
    php_printf("%*cuninitialized(%s)\n",
87
326
      level + 1, ' ', ZSTR_VAL(type_str));
88
326
    zend_string_release(type_str);
89
3.06k
  } else {
90
3.06k
    php_var_dump(zv, level + 2);
91
3.06k
  }
92
3.39k
}
93
/* }}} */
94
95
3.47k
static const char *php_var_dump_object_prefix(zend_object *obj) {
96
3.47k
  if (EXPECTED(!zend_object_is_lazy(obj))) {
97
3.06k
    return "";
98
3.06k
  }
99
100
410
  if (zend_object_is_lazy_proxy(obj)) {
101
250
    return "lazy proxy ";
102
250
  }
103
104
160
  return "lazy ghost ";
105
410
}
106
107
PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
108
201k
{
109
201k
  HashTable *myht;
110
201k
  zend_string *class_name;
111
201k
  int is_ref = 0;
112
201k
  zend_ulong num;
113
201k
  zend_string *key;
114
201k
  zval *val;
115
201k
  uint32_t count;
116
117
201k
  if (level > 1) {
118
115k
    php_printf("%*c", level - 1, ' ');
119
115k
  }
120
121
201k
again:
122
201k
  switch (Z_TYPE_P(struc)) {
123
2.99k
    case IS_FALSE:
124
2.99k
      php_printf("%sbool(false)\n", COMMON);
125
2.99k
      break;
126
2.77k
    case IS_TRUE:
127
2.77k
      php_printf("%sbool(true)\n", COMMON);
128
2.77k
      break;
129
3.86k
    case IS_NULL:
130
3.86k
      php_printf("%sNULL\n", COMMON);
131
3.86k
      break;
132
64.9k
    case IS_LONG:
133
64.9k
      php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
134
64.9k
      break;
135
934
    case IS_DOUBLE:
136
934
      php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
137
934
      break;
138
92.4k
    case IS_STRING:
139
92.4k
      php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
140
92.4k
      PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
141
92.4k
      PUTS("\"\n");
142
92.4k
      break;
143
29.4k
    case IS_ARRAY:
144
29.4k
      myht = Z_ARRVAL_P(struc);
145
29.4k
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
146
28.6k
        if (GC_IS_RECURSIVE(myht)) {
147
16
          PUTS("*RECURSION*\n");
148
16
          return;
149
16
        }
150
28.5k
        GC_ADDREF(myht);
151
28.5k
        GC_PROTECT_RECURSION(myht);
152
28.5k
      }
153
29.4k
      count = zend_hash_num_elements(myht);
154
29.4k
      php_printf("%sarray(%d) {\n", COMMON, count);
155
254k
      ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) {
156
254k
        php_array_element_dump(val, num, key, level);
157
254k
      } ZEND_HASH_FOREACH_END();
158
29.4k
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
159
28.5k
        GC_UNPROTECT_RECURSION(myht);
160
28.5k
        GC_DTOR_NO_REF(myht);
161
28.5k
      }
162
29.4k
      if (level > 1) {
163
798
        php_printf("%*c", level-1, ' ');
164
798
      }
165
29.4k
      PUTS("}\n");
166
29.4k
      break;
167
3.74k
    case IS_OBJECT: {
168
3.74k
      zend_class_entry *ce = Z_OBJCE_P(struc);
169
3.74k
      if ((ce->ce_flags & ZEND_ACC_ENUM) && ce->__debugInfo == NULL) {
170
228
        zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
171
228
        php_printf("%senum(%s::%s)\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval));
172
228
        return;
173
228
      }
174
3.51k
      zend_object *zobj = Z_OBJ_P(struc);
175
3.51k
      uint32_t *guard = zend_get_recursion_guard(zobj);
176
3.51k
      if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) {
177
88
        PUTS("*RECURSION*\n");
178
88
        return;
179
88
      }
180
3.42k
      ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj);
181
182
3.42k
      myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
183
3.42k
      if (ce->ce_flags & ZEND_ACC_ENUM) {
184
4
        zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
185
4
        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
3.42k
      } else {
187
3.42k
        class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
188
3.42k
        const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc));
189
190
3.42k
        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
3.42k
        zend_string_release_ex(class_name, 0);
192
3.42k
      }
193
194
3.42k
      if (myht) {
195
3.37k
        zend_ulong num;
196
3.37k
        zend_string *key;
197
3.37k
        zval *val;
198
199
10.5k
        ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) {
200
10.5k
          zend_property_info *prop_info = NULL;
201
202
10.5k
          if (Z_TYPE_P(val) == IS_INDIRECT) {
203
2.49k
            val = Z_INDIRECT_P(val);
204
2.49k
            if (key) {
205
2.49k
              prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
206
2.49k
            }
207
2.49k
          }
208
209
10.5k
          if (!Z_ISUNDEF_P(val) || prop_info) {
210
3.39k
            php_object_property_dump(prop_info, val, num, key, level);
211
3.39k
          }
212
10.5k
        } ZEND_HASH_FOREACH_END();
213
3.37k
        zend_release_properties(myht);
214
3.37k
      }
215
3.42k
      if (level > 1) {
216
790
        php_printf("%*c", level-1, ' ');
217
790
      }
218
3.42k
      PUTS("}\n");
219
3.42k
      ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj);
220
3.42k
      break;
221
3.51k
    }
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
3.51k
    }
227
744
    case IS_REFERENCE:
228
      //??? hide references with refcount==1 (for compatibility)
229
744
      if (Z_REFCOUNT_P(struc) > 1) {
230
380
        is_ref = 1;
231
380
      }
232
744
      struc = Z_REFVAL_P(struc);
233
744
      goto again;
234
0
    default:
235
0
      php_printf("%sUNKNOWN:0\n", COMMON);
236
0
      break;
237
201k
  }
238
201k
}
239
/* }}} */
240
241
/* {{{ Dumps a string representation of variable to output */
242
PHP_FUNCTION(var_dump)
243
83.2k
{
244
83.2k
  zval *args;
245
83.2k
  int argc;
246
83.2k
  int i;
247
248
249k
  ZEND_PARSE_PARAMETERS_START(1, -1)
249
249k
    Z_PARAM_VARIADIC('+', args, argc)
250
249k
  ZEND_PARSE_PARAMETERS_END();
251
252
168k
  for (i = 0; i < argc; i++) {
253
85.6k
    php_var_dump(&args[i], 1);
254
85.6k
  }
255
83.2k
}
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
46
{
273
46
  const char *prop_name, *class_name;
274
275
46
  if (key == NULL) { /* numeric key */
276
0
    php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
277
46
  } else { /* string key */
278
46
    zend_unmangle_property_name(key, &class_name, &prop_name);
279
46
    php_printf("%*c[", level + 1, ' ');
280
281
46
    if (class_name) {
282
4
      if (class_name[0] == '*') {
283
2
        php_printf("\"%s\":protected", prop_name);
284
2
      } else {
285
2
        php_printf("\"%s\":\"%s\":private", prop_name, class_name);
286
2
      }
287
42
    } else {
288
42
      php_printf("\"%s\"", prop_name);
289
42
    }
290
46
    ZEND_PUTS("]=>\n");
291
46
  }
292
46
  if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) {
293
12
    zend_string *type_str = zend_type_to_string(prop_info->type);
294
12
    php_printf("%*cuninitialized(%s)\n",
295
12
      level + 1, ' ', ZSTR_VAL(type_str));
296
12
    zend_string_release(type_str);
297
34
  } else {
298
34
    php_debug_zval_dump(zv, level + 2);
299
34
  }
300
46
}
301
/* }}} */
302
303
PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */
304
90
{
305
90
  HashTable *myht = NULL;
306
90
  zend_string *class_name;
307
90
  zend_ulong index;
308
90
  zend_string *key;
309
90
  zval *val;
310
90
  uint32_t count;
311
90
  char *packed;
312
313
90
  if (level > 1) {
314
34
    php_printf("%*c", level - 1, ' ');
315
34
  }
316
317
90
  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
6
  case IS_NULL:
325
6
    PUTS("NULL\n");
326
6
    break;
327
4
  case IS_LONG:
328
4
    php_printf("int(" ZEND_LONG_FMT ")\n", Z_LVAL_P(struc));
329
4
    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
28
  case IS_STRING:
334
28
    php_printf("string(%zd) \"", Z_STRLEN_P(struc));
335
28
    PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
336
28
    if (Z_REFCOUNTED_P(struc)) {
337
0
      php_printf("\" refcount(%u)\n", Z_REFCOUNT_P(struc));
338
28
    } else {
339
28
      PUTS("\" interned\n");
340
28
    }
341
28
    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
52
  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
52
    zend_object *zobj = Z_OBJ_P(struc);
378
52
    uint32_t *guard = zend_get_recursion_guard(zobj);
379
52
    if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) {
380
0
      PUTS("*RECURSION*\n");
381
0
      return;
382
0
    }
383
52
    ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj);
384
385
52
    myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
386
52
    class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
387
52
    const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc));
388
389
52
    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
52
    zend_string_release_ex(class_name, 0);
391
52
    if (myht) {
392
142
      ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
393
142
        zend_property_info *prop_info = NULL;
394
395
142
        if (Z_TYPE_P(val) == IS_INDIRECT) {
396
44
          val = Z_INDIRECT_P(val);
397
44
          if (key) {
398
44
            prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
399
44
          }
400
44
        }
401
402
142
        if (!Z_ISUNDEF_P(val) || prop_info) {
403
46
          zval_object_property_dump(prop_info, val, index, key, level);
404
46
        }
405
142
      } ZEND_HASH_FOREACH_END();
406
50
      zend_release_properties(myht);
407
50
    }
408
52
    if (level > 1) {
409
2
      php_printf("%*c", level - 1, ' ');
410
2
    }
411
52
    PUTS("}\n");
412
52
    ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj);
413
52
    break;
414
52
  }
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
52
  }
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
90
  }
432
90
}
433
/* }}} */
434
435
/* {{{ Dumps a string representation of an internal zend value to output. */
436
PHP_FUNCTION(debug_zval_dump)
437
56
{
438
56
  zval *args;
439
56
  int argc;
440
56
  int i;
441
442
168
  ZEND_PARSE_PARAMETERS_START(1, -1)
443
168
    Z_PARAM_VARIADIC('+', args, argc)
444
168
  ZEND_PARSE_PARAMETERS_END();
445
446
112
  for (i = 0; i < argc; i++) {
447
56
    php_debug_zval_dump(&args[i], 1);
448
56
  }
449
56
}
450
/* }}} */
451
452
#define buffer_append_spaces(buf, num_spaces) \
453
278
  do { \
454
278
    char *tmp_spaces; \
455
278
    size_t tmp_spaces_len; \
456
278
    tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \
457
278
    smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \
458
278
    efree(tmp_spaces); \
459
278
  } 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
54
{
463
54
  if (key == NULL) { /* numeric key */
464
16
    buffer_append_spaces(buf, level+1);
465
16
    smart_str_append_long(buf, (zend_long) index);
466
16
    smart_str_appendl(buf, " => ", 4);
467
468
38
  } else { /* string key */
469
38
    zend_string *tmp_str;
470
38
    zend_string *ckey = php_addcslashes(key, "'\\", 2);
471
38
    tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12);
472
473
38
    buffer_append_spaces(buf, level + 1);
474
475
38
    smart_str_appendc(buf, '\'');
476
38
    smart_str_append(buf, tmp_str);
477
38
    smart_str_appendl(buf, "' => ", 5);
478
479
38
    zend_string_free(ckey);
480
38
    zend_string_free(tmp_str);
481
38
  }
482
54
  zend_result result = php_var_export_ex(zv, level + 2, buf);
483
484
54
  smart_str_appendc(buf, ',');
485
54
  smart_str_appendc(buf, '\n');
486
487
54
  return result;
488
54
}
489
/* }}} */
490
491
static zend_result php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */
492
214
{
493
214
  buffer_append_spaces(buf, level + 2);
494
214
  if (key != NULL) {
495
214
    const char *class_name, *prop_name;
496
214
    size_t prop_name_len;
497
214
    zend_string *pname_esc;
498
499
214
    zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len);
500
214
    pname_esc = php_addcslashes_str(prop_name, prop_name_len, "'\\", 2);
501
502
214
    smart_str_appendc(buf, '\'');
503
214
    smart_str_append(buf, pname_esc);
504
214
    smart_str_appendc(buf, '\'');
505
214
    zend_string_release_ex(pname_esc, 0);
506
214
  } else {
507
0
    smart_str_append_long(buf, (zend_long) index);
508
0
  }
509
214
  smart_str_appendl(buf, " => ", 4);
510
214
  zend_result result = php_var_export_ex(zv, level + 2, buf);
511
214
  smart_str_appendc(buf, ',');
512
214
  smart_str_appendc(buf, '\n');
513
514
214
  return result;
515
214
}
516
/* }}} */
517
518
PHPAPI zend_result php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
519
427
{
520
427
  HashTable *myht;
521
427
  zend_string *ztmp, *ztmp2;
522
427
  zend_ulong index;
523
427
  zend_string *key;
524
427
  zval *val;
525
526
427
again:
527
427
  switch (Z_TYPE_P(struc)) {
528
13
    case IS_FALSE:
529
13
      smart_str_appendl(buf, "false", 5);
530
13
      break;
531
11
    case IS_TRUE:
532
11
      smart_str_appendl(buf, "true", 4);
533
11
      break;
534
25
    case IS_NULL:
535
25
      smart_str_appendl(buf, "NULL", 4);
536
25
      break;
537
67
    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
67
      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
67
      smart_str_append_long(buf, Z_LVAL_P(struc));
546
67
      break;
547
10
    case IS_DOUBLE:
548
10
      smart_str_append_double(
549
10
        buf, Z_DVAL_P(struc), (int) PG(serialize_precision), /* zero_fraction */ true);
550
10
      break;
551
211
    case IS_STRING:
552
211
      ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2);
553
211
      ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12);
554
555
211
      smart_str_appendc(buf, '\'');
556
211
      smart_str_append(buf, ztmp2);
557
211
      smart_str_appendc(buf, '\'');
558
559
211
      zend_string_free(ztmp);
560
211
      zend_string_free(ztmp2);
561
211
      break;
562
32
    case IS_ARRAY:
563
32
      myht = Z_ARRVAL_P(struc);
564
32
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
565
20
        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
20
        GC_ADDREF(myht);
571
20
        GC_PROTECT_RECURSION(myht);
572
20
      }
573
32
      if (level > 1) {
574
4
        smart_str_appendc(buf, '\n');
575
4
        buffer_append_spaces(buf, level - 1);
576
4
      }
577
32
      smart_str_appendl(buf, "array (\n", 8);
578
142
      ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
579
142
        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
142
      } ZEND_HASH_FOREACH_END();
587
32
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
588
20
        GC_UNPROTECT_RECURSION(myht);
589
20
        GC_DELREF(myht);
590
20
      }
591
32
      if (level > 1) {
592
4
        buffer_append_spaces(buf, level - 1);
593
4
      }
594
32
      smart_str_appendc(buf, ')');
595
596
32
      break;
597
598
58
    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
58
      zend_object *zobj = Z_OBJ_P(struc);
604
58
      uint32_t *guard = zend_get_recursion_guard(zobj);
605
58
      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
58
      ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, EXPORT, zobj);
611
58
      myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT);
612
58
      if (level > 1) {
613
2
        smart_str_appendc(buf, '\n');
614
2
        buffer_append_spaces(buf, level - 1);
615
2
      }
616
617
58
      zend_class_entry *ce = Z_OBJCE_P(struc);
618
58
      bool is_enum = ce->ce_flags & ZEND_ACC_ENUM;
619
620
      /* stdClass has no __set_state method, but can be casted to */
621
58
      if (ce == zend_standard_class_def) {
622
0
        smart_str_appendl(buf, "(object) array(\n", 16);
623
58
      } else {
624
58
        smart_str_appendc(buf, '\\');
625
58
        smart_str_append(buf, ce->name);
626
58
        if (is_enum) {
627
14
          zend_object *zobj = Z_OBJ_P(struc);
628
14
          zval *case_name_zval = zend_enum_fetch_case_name(zobj);
629
14
          smart_str_appendl(buf, "::", 2);
630
14
          smart_str_append(buf, Z_STR_P(case_name_zval));
631
44
        } else {
632
44
          smart_str_appendl(buf, "::__set_state(array(\n", 21);
633
44
        }
634
58
      }
635
636
58
      if (myht) {
637
56
        if (!is_enum) {
638
526
          ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) {
639
            /* data is IS_PTR for properties with hooks. */
640
526
            zval tmp;
641
526
            if (UNEXPECTED(Z_TYPE_P(val) == IS_PTR)) {
642
182
              zend_property_info *prop_info = Z_PTR_P(val);
643
182
              if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) {
644
28
                continue;
645
28
              }
646
154
              const char *unmangled_name_cstr = zend_get_unmangled_property_name(prop_info->name);
647
154
              zend_string *unmangled_name = zend_string_init(unmangled_name_cstr, strlen(unmangled_name_cstr), false);
648
154
              val = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp);
649
154
              zend_string_release_ex(unmangled_name, false);
650
154
              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
154
            }
656
214
            php_object_element_export(val, index, key, level, buf);
657
214
            if (val == &tmp) {
658
150
              zval_ptr_dtor(val);
659
150
            }
660
214
          } ZEND_HASH_FOREACH_END();
661
42
        }
662
56
        zend_release_properties(myht);
663
56
      }
664
58
      ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj);
665
58
      if (level > 1 && !is_enum) {
666
0
        buffer_append_spaces(buf, level - 1);
667
0
      }
668
58
      if (ce == zend_standard_class_def) {
669
0
        smart_str_appendc(buf, ')');
670
58
      } else if (!is_enum) {
671
44
        smart_str_appendl(buf, "))", 2);
672
44
      }
673
674
58
      break;
675
58
    }
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
427
  }
683
684
427
  return SUCCESS;
685
427
}
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
159
{
704
159
  zval *var;
705
159
  bool return_output = 0;
706
159
  smart_str buf = {0};
707
708
477
  ZEND_PARSE_PARAMETERS_START(1, 2)
709
636
    Z_PARAM_ZVAL(var)
710
636
    Z_PARAM_OPTIONAL
711
636
    Z_PARAM_BOOL(return_output)
712
159
  ZEND_PARSE_PARAMETERS_END();
713
714
159
  zend_result result = php_var_export_ex(var, 1, &buf);
715
159
  smart_str_0 (&buf);
716
717
159
  if (result == FAILURE) {
718
0
    smart_str_free(&buf);
719
159
  } else if (return_output) {
720
24
    RETURN_STR(smart_str_extract(&buf));
721
135
  } else {
722
135
    PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
723
135
    smart_str_free(&buf);
724
135
  }
725
159
}
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
36.2k
{
735
36.2k
  zval *zv;
736
36.2k
  zend_ulong key;
737
36.2k
  bool is_ref = Z_ISREF_P(var);
738
739
36.2k
  data->n += 1;
740
741
36.2k
  if (is_ref) {
742
    /* pass */
743
32.8k
  } else if (Z_TYPE_P(var) != IS_OBJECT) {
744
32.6k
    return 0;
745
32.6k
  } else if (!in_rcn_array
746
187
   && Z_REFCOUNT_P(var) == 1
747
40
   && (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1)
748
   /* __serialize and __sleep may arbitrarily increase the refcount */
749
40
   && Z_OBJCE_P(var)->__serialize == NULL
750
38
   && zend_hash_find_known_hash(&Z_OBJCE_P(var)->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP)) == NULL) {
751
38
    return 0;
752
38
  }
753
754
  /* References to objects are treated as if the reference didn't exist */
755
3.59k
  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
3.59k
  key = (zend_ulong) (uintptr_t) Z_COUNTED_P(var);
762
3.59k
  zv = zend_hash_index_find(&data->ht, key);
763
764
3.59k
  if (zv) {
765
    /* References are only counted once, undo the data->n increment above */
766
1.83k
    if (is_ref && Z_LVAL_P(zv) != -1) {
767
1.82k
      data->n -= 1;
768
1.82k
    }
769
770
1.83k
    return Z_LVAL_P(zv);
771
1.83k
  } else {
772
1.76k
    zval zv_n;
773
1.76k
    ZVAL_LONG(&zv_n, data->n);
774
1.76k
    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
1.76k
    zend_hash_index_add_new(&data->ht, key + 1, var);
781
1.76k
    Z_ADDREF_P(var);
782
783
1.76k
    return 0;
784
1.76k
  }
785
3.59k
}
786
/* }}} */
787
788
static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */
789
13.6k
{
790
13.6k
  char b[32];
791
13.6k
  char *s = zend_print_long_to_buf(b + sizeof(b) - 1, val);
792
13.6k
  size_t l = b + sizeof(b) - 1 - s;
793
13.6k
  char *res = smart_str_extend(buf, 2 + l + 1);
794
13.6k
  res = zend_mempcpy(res, "i:", 2);
795
13.6k
  memcpy(res, s, l);
796
13.6k
  res[l] = ';';
797
13.6k
}
798
/* }}} */
799
800
static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */
801
29.7k
{
802
29.7k
  char b[32];
803
29.7k
  char *s = zend_print_long_to_buf(b + sizeof(b) - 1, len);
804
29.7k
  size_t l = b + sizeof(b) - 1 - s;
805
29.7k
  char *res = smart_str_extend(buf, 2 + l + 2 + len + 2);
806
29.7k
  res = zend_mempcpy(res, "s:", 2);
807
29.7k
  res = zend_mempcpy(res, s, l);
808
29.7k
  res = zend_mempcpy(res, ":\"", 2);
809
29.7k
  res = zend_mempcpy(res, str, len);
810
29.7k
  memcpy(res, "\";", 2);
811
29.7k
}
812
/* }}} */
813
814
static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */
815
167
{
816
167
  char b[32];
817
167
  PHP_CLASS_ATTRIBUTES;
818
819
167
  PHP_SET_CLASS_ATTRIBUTES(struc);
820
167
  size_t class_name_len = ZSTR_LEN(class_name);
821
167
  char *s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len);
822
167
  size_t l = b + sizeof(b) - 1 - s;
823
167
  char *res = smart_str_extend(buf, 2 + l + 2 + class_name_len + 2);
824
167
  res = zend_mempcpy(res, "O:", 2);
825
167
  res = zend_mempcpy(res, s, l);
826
167
  res = zend_mempcpy(res, ":\"", 2);
827
167
  res = zend_mempcpy(res, ZSTR_VAL(class_name), class_name_len);
828
167
  memcpy(res, "\":", 2);
829
167
  PHP_CLEANUP_CLASS_ATTRIBUTES();
830
167
  return incomplete_class;
831
167
}
832
/* }}} */
833
834
static HashTable* php_var_serialize_call_sleep(zend_object *obj, zend_function *fn) /* {{{ */
835
28
{
836
28
  zval retval;
837
838
28
  BG(serialize_lock)++;
839
28
  zend_call_known_instance_method(fn, obj, &retval, /* param_count */ 0, /* params */ NULL);
840
28
  BG(serialize_lock)--;
841
842
28
  if (Z_ISUNDEF(retval) || EG(exception)) {
843
0
    zval_ptr_dtor(&retval);
844
0
    return NULL;
845
0
  }
846
847
28
  if (Z_TYPE(retval) != IS_ARRAY) {
848
2
    zval_ptr_dtor(&retval);
849
2
    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
2
    return NULL;
851
2
  }
852
853
26
  return Z_ARRVAL(retval);
854
28
}
855
/* }}} */
856
857
static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */
858
41
{
859
41
  BG(serialize_lock)++;
860
41
  zend_call_known_instance_method_with_0_params(
861
41
    Z_OBJCE_P(obj)->__serialize, Z_OBJ_P(obj), retval);
862
41
  BG(serialize_lock)--;
863
864
41
  if (EG(exception)) {
865
2
    zval_ptr_dtor(retval);
866
2
    return FAILURE;
867
2
  }
868
869
39
  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
39
  return SUCCESS;
876
39
}
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
46
{
882
46
  zval *val = zend_hash_find(props, name);
883
46
  if (val == NULL) {
884
22
    return FAILURE;
885
22
  }
886
887
24
  if (Z_TYPE_P(val) == IS_INDIRECT) {
888
24
    val = Z_INDIRECT_P(val);
889
24
    if (Z_TYPE_P(val) == IS_UNDEF) {
890
10
      zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
891
10
      if (info) {
892
10
        return SUCCESS;
893
10
      }
894
0
      return FAILURE;
895
10
    }
896
24
  }
897
898
14
  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
14
  Z_TRY_ADDREF_P(val);
905
14
  return SUCCESS;
906
14
}
907
/* }}} */
908
909
static int php_var_serialize_get_sleep_props(
910
    HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */
911
26
{
912
26
  zend_class_entry *ce = Z_OBJCE_P(struc);
913
26
  HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
914
26
  zval *name_val;
915
26
  int retval = SUCCESS;
916
917
26
  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
90
  ZEND_HASH_FOREACH_VAL_IND(sleep_retval, name_val) {
921
90
    zend_string *name, *tmp_name, *priv_name, *prot_name;
922
923
90
    ZVAL_DEREF(name_val);
924
90
    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
90
    name = zval_get_tmp_string(name_val, &tmp_name);
931
90
    if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) {
932
18
      zend_tmp_string_release(tmp_name);
933
18
      continue;
934
18
    }
935
936
14
    if (EG(exception)) {
937
4
      zend_tmp_string_release(tmp_name);
938
4
      retval = FAILURE;
939
4
      break;
940
4
    }
941
942
10
    priv_name = zend_mangle_property_name(
943
10
      ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
944
10
      ZSTR_VAL(name), ZSTR_LEN(name), ce->type == ZEND_INTERNAL_CLASS);
945
10
    if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) {
946
6
      zend_tmp_string_release(tmp_name);
947
6
      zend_string_release(priv_name);
948
6
      continue;
949
6
    }
950
4
    zend_string_release(priv_name);
951
952
4
    if (EG(exception)) {
953
0
      zend_tmp_string_release(tmp_name);
954
0
      retval = FAILURE;
955
0
      break;
956
0
    }
957
958
4
    prot_name = zend_mangle_property_name(
959
4
      "*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type == ZEND_INTERNAL_CLASS);
960
4
    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
4
    zend_string_release(prot_name);
966
967
4
    if (EG(exception)) {
968
0
      zend_tmp_string_release(tmp_name);
969
0
      retval = FAILURE;
970
0
      break;
971
0
    }
972
973
4
    php_error_docref(NULL, E_WARNING,
974
4
      "\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name));
975
4
    zend_tmp_string_release(tmp_name);
976
4
  } ZEND_HASH_FOREACH_END();
977
978
26
  zend_release_properties(props);
979
26
  return retval;
980
26
}
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
22.7k
{
985
22.7k
  smart_str_append_unsigned(buf, count);
986
22.7k
  smart_str_appendl(buf, ":{", 2);
987
22.7k
  if (count > 0) {
988
3.33k
    zend_string *key;
989
3.33k
    zval *data;
990
3.33k
    zend_ulong index;
991
992
74.0k
    ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) {
993
74.0k
      if (incomplete_class && zend_string_equals_literal(key, MAGIC_MEMBER)) {
994
0
        incomplete_class = false;
995
0
        continue;
996
0
      }
997
998
35.3k
      if (!key) {
999
8.10k
        php_var_serialize_long(buf, index);
1000
27.2k
      } else {
1001
27.2k
        php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
1002
27.2k
      }
1003
1004
35.3k
      if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
1005
0
        data = Z_REFVAL_P(data);
1006
0
      }
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
35.3k
      if (Z_TYPE_P(data) == IS_ARRAY) {
1011
20.7k
        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
20.7k
        } else {
1015
20.7k
          php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
1016
20.7k
        }
1017
20.7k
      } else {
1018
14.5k
        php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
1019
14.5k
      }
1020
35.3k
    } ZEND_HASH_FOREACH_END();
1021
3.33k
  }
1022
22.7k
  smart_str_appendc(buf, '}');
1023
22.7k
}
1024
/* }}} */
1025
1026
static void php_var_serialize_class(smart_str *buf, zval *struc, HashTable *ht, php_serialize_data_t var_hash) /* {{{ */
1027
26
{
1028
26
  HashTable props;
1029
1030
26
  if (php_var_serialize_get_sleep_props(&props, struc, ht) == SUCCESS) {
1031
22
    php_var_serialize_class_name(buf, struc);
1032
22
    php_var_serialize_nested_data(
1033
22
      buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ false, var_hash,
1034
22
      GC_REFCOUNT(&props) > 1);
1035
22
  }
1036
26
  zend_hash_destroy(&props);
1037
26
}
1038
/* }}} */
1039
1040
static zend_always_inline bool php_serialize_check_stack_limit(void)
1041
36.2k
{
1042
36.2k
#ifdef ZEND_CHECK_STACK_LIMIT
1043
36.2k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
1044
0
    zend_call_stack_size_error();
1045
0
    return true;
1046
0
  }
1047
36.2k
#endif
1048
36.2k
  return false;
1049
36.2k
}
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
36.2k
{
1053
36.2k
  zend_long var_already;
1054
36.2k
  HashTable *myht;
1055
1056
36.2k
  if (EG(exception)) {
1057
0
    return;
1058
0
  }
1059
1060
36.2k
  if (UNEXPECTED(php_serialize_check_stack_limit())) {
1061
0
    return;
1062
0
  }
1063
1064
36.2k
  if (var_hash && (var_already = php_add_var_hash(var_hash, struc, in_rcn_array))) {
1065
1.83k
    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.83k
    } else if (Z_ISREF_P(struc)) {
1070
1.82k
      smart_str_appendl(buf, "R:", 2);
1071
1.82k
      smart_str_append_long(buf, var_already);
1072
1.82k
      smart_str_appendc(buf, ';');
1073
1.82k
      return;
1074
1.82k
    } else if (Z_TYPE_P(struc) == IS_OBJECT) {
1075
10
      smart_str_appendl(buf, "r:", 2);
1076
10
      smart_str_append_long(buf, var_already);
1077
10
      smart_str_appendc(buf, ';');
1078
10
      return;
1079
10
    }
1080
1.83k
  }
1081
1082
36.0k
again:
1083
36.0k
  switch (Z_TYPE_P(struc)) {
1084
4
    case IS_FALSE:
1085
4
      smart_str_appendl(buf, "b:0;", 4);
1086
4
      return;
1087
1088
12
    case IS_TRUE:
1089
12
      smart_str_appendl(buf, "b:1;", 4);
1090
12
      return;
1091
1092
3.62k
    case IS_NULL:
1093
3.62k
      smart_str_appendl(buf, "N;", 2);
1094
3.62k
      return;
1095
1096
5.55k
    case IS_LONG:
1097
5.55k
      php_var_serialize_long(buf, Z_LVAL_P(struc));
1098
5.55k
      return;
1099
1100
230
    case IS_DOUBLE: {
1101
230
      char tmp_str[ZEND_DOUBLE_MAX_LENGTH];
1102
230
      zend_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
1103
1104
230
      size_t len = strlen(tmp_str);
1105
230
      char *res = smart_str_extend(buf, 2 + len + 1);
1106
230
      res = zend_mempcpy(res, "d:", 2);
1107
230
      memcpy(res, tmp_str, len);
1108
230
      res[len] = ';';
1109
230
      return;
1110
0
    }
1111
1112
2.14k
    case IS_STRING:
1113
2.14k
      php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));
1114
2.14k
      return;
1115
1116
197
    case IS_OBJECT: {
1117
197
        zend_class_entry *ce = Z_OBJCE_P(struc);
1118
197
        bool incomplete_class;
1119
197
        uint32_t count;
1120
1121
197
        if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1122
6
          zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed",
1123
6
            ZSTR_VAL(ce->name));
1124
6
          return;
1125
6
        }
1126
1127
191
        if (ce->ce_flags & ZEND_ACC_ENUM) {
1128
8
          PHP_CLASS_ATTRIBUTES;
1129
1130
8
          zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
1131
1132
8
          PHP_SET_CLASS_ATTRIBUTES(struc);
1133
8
          smart_str_appendl(buf, "E:", 2);
1134
8
          smart_str_append_unsigned(buf, ZSTR_LEN(class_name) + strlen(":") + Z_STRLEN_P(case_name_zval));
1135
8
          smart_str_appendl(buf, ":\"", 2);
1136
8
          smart_str_append(buf, class_name);
1137
8
          smart_str_appendc(buf, ':');
1138
8
          smart_str_append(buf, Z_STR_P(case_name_zval));
1139
8
          smart_str_appendl(buf, "\";", 2);
1140
8
          PHP_CLEANUP_CLASS_ATTRIBUTES();
1141
8
          return;
1142
8
        }
1143
1144
183
        if (ce->__serialize) {
1145
41
          zval retval, obj;
1146
41
          zend_string *key;
1147
41
          zval *data;
1148
41
          zend_ulong index;
1149
1150
41
          ZVAL_OBJ_COPY(&obj, Z_OBJ_P(struc));
1151
41
          if (php_var_serialize_call_magic_serialize(&retval, &obj) == FAILURE) {
1152
2
            if (!EG(exception)) {
1153
0
              smart_str_appendl(buf, "N;", 2);
1154
0
            }
1155
2
            zval_ptr_dtor(&obj);
1156
2
            return;
1157
2
          }
1158
1159
39
          php_var_serialize_class_name(buf, &obj);
1160
39
          smart_str_append_unsigned(buf, zend_hash_num_elements(Z_ARRVAL(retval)));
1161
39
          smart_str_appendl(buf, ":{", 2);
1162
145
          ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(retval), index, key, data) {
1163
145
            if (!key) {
1164
28
              php_var_serialize_long(buf, index);
1165
28
            } else {
1166
25
              php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
1167
25
            }
1168
1169
145
            if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
1170
0
              data = Z_REFVAL_P(data);
1171
0
            }
1172
145
            php_var_serialize_intern(buf, data, var_hash, Z_REFCOUNT(retval) > 1, false);
1173
145
          } ZEND_HASH_FOREACH_END();
1174
39
          smart_str_appendc(buf, '}');
1175
1176
39
          zval_ptr_dtor(&obj);
1177
39
          zval_ptr_dtor(&retval);
1178
39
          return;
1179
41
        }
1180
1181
142
        if (ce->serialize != NULL) {
1182
          /* has custom handler */
1183
8
          unsigned char *serialized_data = NULL;
1184
8
          size_t serialized_length;
1185
1186
8
          if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) {
1187
6
            char b1[32], b2[32];
1188
6
            char *s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name));
1189
6
            size_t l1 = b1 + sizeof(b1) - 1 - s1;
1190
6
            char *s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length);
1191
6
            size_t l2 = b2 + sizeof(b2) - 1 - s2;
1192
6
            char *res = smart_str_extend(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1);
1193
6
            res = zend_mempcpy(res, "C:", 2);
1194
6
            res = zend_mempcpy(res, s1, l1);
1195
6
            res = zend_mempcpy(res, ":\"", 2);
1196
6
            res = zend_mempcpy(res, ZSTR_VAL(Z_OBJCE_P(struc)->name), ZSTR_LEN(Z_OBJCE_P(struc)->name));
1197
6
            res = zend_mempcpy(res, "\":", 2);
1198
6
            res = zend_mempcpy(res, s2, l2);
1199
6
            res = zend_mempcpy(res, ":{", 2);
1200
6
            memcpy(res, (char *) serialized_data, serialized_length);
1201
6
            res[serialized_length] = '}';
1202
6
          } else {
1203
            /* Mark this value in the var_hash, to avoid creating references to it. */
1204
2
            zval *var_idx = zend_hash_index_find(&var_hash->ht,
1205
2
              (zend_ulong) (uintptr_t) Z_COUNTED_P(struc));
1206
2
            if (var_idx) {
1207
0
              ZVAL_LONG(var_idx, -1);
1208
0
            }
1209
2
            smart_str_appendl(buf, "N;", 2);
1210
2
          }
1211
8
          if (serialized_data) {
1212
6
            efree(serialized_data);
1213
6
          }
1214
8
          return;
1215
8
        }
1216
1217
134
        if (ce != PHP_IC_ENTRY) {
1218
134
          zval *zv = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP));
1219
1220
134
          if (zv) {
1221
28
            HashTable *ht;
1222
28
            zval tmp;
1223
1224
28
            ZVAL_OBJ_COPY(&tmp, Z_OBJ_P(struc));
1225
28
            if (!(ht = php_var_serialize_call_sleep(Z_OBJ(tmp), Z_FUNC_P(zv)))) {
1226
2
              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
2
                smart_str_appendl(buf, "N;", 2);
1230
2
              }
1231
2
              OBJ_RELEASE(Z_OBJ(tmp));
1232
2
              return;
1233
2
            }
1234
1235
26
            php_var_serialize_class(buf, &tmp, ht, var_hash);
1236
26
            zend_array_release(ht);
1237
26
            OBJ_RELEASE(Z_OBJ(tmp));
1238
26
            return;
1239
28
          }
1240
134
        }
1241
1242
106
        incomplete_class = php_var_serialize_class_name(buf, struc);
1243
1244
106
        if (Z_OBJ_P(struc)->properties == NULL
1245
86
         && Z_OBJ_HT_P(struc)->get_properties_for == NULL
1246
86
         && Z_OBJ_HT_P(struc)->get_properties == zend_std_get_properties
1247
86
         && !zend_object_is_lazy(Z_OBJ_P(struc))) {
1248
          /* Optimized version without rebuilding properties HashTable */
1249
68
          zend_object *obj = Z_OBJ_P(struc);
1250
68
          zend_class_entry *ce = obj->ce;
1251
68
          zend_property_info *prop_info;
1252
68
          zval *prop;
1253
68
          int i;
1254
1255
68
          count = ce->default_properties_count;
1256
424
          for (i = 0; i < ce->default_properties_count; i++) {
1257
356
            prop_info = ce->properties_info_table[i];
1258
356
            if (!prop_info) {
1259
2
              count--;
1260
2
              continue;
1261
2
            }
1262
354
            prop = OBJ_PROP(obj, prop_info->offset);
1263
354
            if (Z_TYPE_P(prop) == IS_UNDEF) {
1264
0
              count--;
1265
0
              continue;
1266
0
            }
1267
354
          }
1268
68
          if (count) {
1269
66
            smart_str_append_unsigned(buf, count);
1270
66
            smart_str_appendl(buf, ":{", 2);
1271
422
            for (i = 0; i < ce->default_properties_count; i++) {
1272
356
              prop_info = ce->properties_info_table[i];
1273
356
              if (!prop_info) {
1274
2
                continue;
1275
2
              }
1276
354
              prop = OBJ_PROP(obj, prop_info->offset);
1277
354
              if (Z_TYPE_P(prop) == IS_UNDEF) {
1278
0
                continue;
1279
0
              }
1280
1281
354
              php_var_serialize_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name));
1282
1283
354
              if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
1284
0
                prop = Z_REFVAL_P(prop);
1285
0
              }
1286
1287
354
              php_var_serialize_intern(buf, prop, var_hash, false, false);
1288
354
            }
1289
66
            smart_str_appendc(buf, '}');
1290
66
          } else {
1291
2
            smart_str_appendl(buf, "0:{}", 4);
1292
2
          }
1293
68
          return;
1294
68
        }
1295
38
        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
38
        count = zend_array_count(myht);
1299
38
        if (count > 0 && incomplete_class) {
1300
0
          --count;
1301
0
        }
1302
38
        php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash, GC_REFCOUNT(myht) > 1);
1303
38
        zend_release_properties(myht);
1304
38
        return;
1305
106
      }
1306
22.6k
    case IS_ARRAY:
1307
22.6k
      smart_str_appendl(buf, "a:", 2);
1308
22.6k
      myht = Z_ARRVAL_P(struc);
1309
22.6k
      php_var_serialize_nested_data(
1310
22.6k
        buf, struc, myht, zend_array_count(myht), /* incomplete_class */ false, var_hash,
1311
22.6k
        !is_root && (in_rcn_array || GC_REFCOUNT(myht) > 1));
1312
22.6k
      return;
1313
1.60k
    case IS_REFERENCE:
1314
1.60k
      struc = Z_REFVAL_P(struc);
1315
1.60k
      goto again;
1316
0
    default:
1317
0
      smart_str_appendl(buf, "i:0;", 4);
1318
0
      return;
1319
36.0k
  }
1320
36.0k
}
1321
/* }}} */
1322
1323
PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */
1324
546
{
1325
546
  php_var_serialize_intern(buf, struc, *data, false, true);
1326
546
  smart_str_0(buf);
1327
546
}
1328
/* }}} */
1329
1330
546
PHPAPI php_serialize_data_t php_var_serialize_init(void) {
1331
546
  struct php_serialize_data *d;
1332
  /* fprintf(stderr, "SERIALIZE_INIT      == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
1333
546
  if (BG(serialize_lock) || !BG(serialize).level) {
1334
546
    d = emalloc(sizeof(struct php_serialize_data));
1335
546
    zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0);
1336
546
    d->n = 0;
1337
546
    if (!BG(serialize_lock)) {
1338
546
      BG(serialize).data = d;
1339
546
      BG(serialize).level = 1;
1340
546
    }
1341
546
  } else {
1342
0
    d = BG(serialize).data;
1343
0
    ++BG(serialize).level;
1344
0
  }
1345
546
  return d;
1346
546
}
1347
1348
546
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
546
  if (BG(serialize_lock) || BG(serialize).level == 1) {
1351
546
    zend_hash_destroy(&d->ht);
1352
546
    efree(d);
1353
546
  }
1354
546
  if (!BG(serialize_lock) && !--BG(serialize).level) {
1355
546
    BG(serialize).data = NULL;
1356
546
  }
1357
546
}
1358
1359
/* {{{ Returns a string representation of variable (which can later be unserialized) */
1360
PHP_FUNCTION(serialize)
1361
546
{
1362
546
  zval *struc;
1363
546
  php_serialize_data_t var_hash;
1364
546
  smart_str buf = {0};
1365
1366
1.63k
  ZEND_PARSE_PARAMETERS_START(1, 1)
1367
2.18k
    Z_PARAM_ZVAL(struc)
1368
2.18k
  ZEND_PARSE_PARAMETERS_END();
1369
1370
546
  PHP_VAR_SERIALIZE_INIT(var_hash);
1371
546
  php_var_serialize(&buf, struc, &var_hash);
1372
546
  PHP_VAR_SERIALIZE_DESTROY(var_hash);
1373
1374
546
  if (EG(exception)) {
1375
18
    smart_str_free(&buf);
1376
18
    RETURN_THROWS();
1377
18
  }
1378
1379
528
  RETURN_STR(smart_str_extract(&buf));
1380
528
}
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
816
{
1386
816
  const unsigned char *p;
1387
816
  php_unserialize_data_t var_hash;
1388
816
  zval *retval;
1389
816
  HashTable *class_hash = NULL, *prev_class_hash;
1390
816
  zend_long prev_max_depth, prev_cur_depth;
1391
1392
816
  if (buf_len == 0) {
1393
156
    RETURN_FALSE;
1394
156
  }
1395
1396
660
  p = (const unsigned char*) buf;
1397
660
  PHP_VAR_UNSERIALIZE_INIT(var_hash);
1398
1399
660
  prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash);
1400
660
  prev_max_depth = php_var_unserialize_get_max_depth(var_hash);
1401
660
  prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash);
1402
660
  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
660
  if (BG(unserialize).level > 1) {
1462
0
    retval = var_tmp_var(&var_hash);
1463
660
  } else {
1464
660
    retval = return_value;
1465
660
  }
1466
660
  if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) {
1467
128
    if (!EG(exception)) {
1468
100
      php_error_docref(NULL, E_WARNING, "Error at offset " ZEND_LONG_FMT " of %zd bytes",
1469
100
        (zend_long)((char*)p - buf), buf_len);
1470
100
    }
1471
128
    if (BG(unserialize).level <= 1) {
1472
128
      zval_ptr_dtor(return_value);
1473
128
    }
1474
128
    RETVAL_FALSE;
1475
532
  } else {
1476
532
    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
532
    if (BG(unserialize).level > 1) {
1483
0
      ZVAL_COPY(return_value, retval);
1484
532
    } else if (Z_REFCOUNTED_P(return_value)) {
1485
500
      zend_refcounted *ref = Z_COUNTED_P(return_value);
1486
500
      gc_check_possible_root(ref);
1487
500
    }
1488
532
  }
1489
1490
660
cleanup:
1491
660
  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
660
  php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash);
1498
660
  php_var_unserialize_set_max_depth(var_hash, prev_max_depth);
1499
660
  php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth);
1500
660
  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
660
  if (Z_ISREF_P(return_value)) {
1506
0
    zend_unwrap_reference(return_value);
1507
0
  }
1508
660
}
1509
/* }}} */
1510
1511
/* {{{ Takes a string representation of variable and recreates it */
1512
PHP_FUNCTION(unserialize)
1513
818
{
1514
818
  char *buf = NULL;
1515
818
  size_t buf_len;
1516
818
  HashTable *options = NULL;
1517
1518
2.45k
  ZEND_PARSE_PARAMETERS_START(1, 2)
1519
3.27k
    Z_PARAM_STRING(buf, buf_len)
1520
818
    Z_PARAM_OPTIONAL
1521
1.64k
    Z_PARAM_ARRAY_HT(options)
1522
818
  ZEND_PARSE_PARAMETERS_END();
1523
1524
816
  php_unserialize_with_options(return_value, buf, buf_len, options, "unserialize");
1525
816
}
1526
/* }}} */
1527
1528
/* {{{ Returns the allocated by PHP memory */
1529
22
PHP_FUNCTION(memory_get_usage) {
1530
22
  bool real_usage = 0;
1531
1532
66
  ZEND_PARSE_PARAMETERS_START(0, 1)
1533
66
    Z_PARAM_OPTIONAL
1534
66
    Z_PARAM_BOOL(real_usage)
1535
22
  ZEND_PARSE_PARAMETERS_END();
1536
1537
22
  RETURN_LONG(zend_memory_usage(real_usage));
1538
22
}
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
}