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