Coverage Report

Created: 2025-07-23 06:33

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