Coverage Report

Created: 2026-06-13 07:01

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