Coverage Report

Created: 2025-09-27 06:26

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 (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
3.61M
#define COMMON (is_ref ? "&" : "")
41
42
static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
43
1.31M
{
44
1.31M
  if (key == NULL) { /* numeric key */
45
1.05M
    php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
46
1.05M
  } else { /* string key */
47
260k
    php_printf("%*c[\"", level + 1, ' ');
48
260k
    PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
49
260k
    php_printf("\"]=>\n");
50
260k
  }
51
1.31M
  php_var_dump(zv, level + 2);
52
1.31M
}
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
2.10M
{
57
2.10M
  const char *prop_name, *class_name;
58
59
2.10M
  if (key == NULL) { /* numeric key */
60
2.09M
    php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
61
2.09M
  } else { /* string key */
62
11.1k
    int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name);
63
11.1k
    php_printf("%*c[", level + 1, ' ');
64
65
11.1k
    if (class_name && unmangle == SUCCESS) {
66
804
      if (class_name[0] == '*') {
67
111
        php_printf("\"%s\":protected", prop_name);
68
693
      } else {
69
693
        php_printf("\"%s\":\"%s\":private", prop_name, class_name);
70
693
      }
71
10.3k
    } else {
72
10.3k
      php_printf("\"");
73
10.3k
      PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key));
74
10.3k
      php_printf("\"");
75
10.3k
    }
76
11.1k
    ZEND_PUTS("]=>\n");
77
11.1k
  }
78
79
2.10M
  if (Z_TYPE_P(zv) == IS_UNDEF) {
80
805
    ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type));
81
805
    zend_string *type_str = zend_type_to_string(prop_info->type);
82
805
    php_printf("%*cuninitialized(%s)\n",
83
805
      level + 1, ' ', ZSTR_VAL(type_str));
84
805
    zend_string_release(type_str);
85
2.10M
  } else {
86
2.10M
    php_var_dump(zv, level + 2);
87
2.10M
  }
88
2.10M
}
89
/* }}} */
90
91
9.71k
static const char *php_var_dump_object_prefix(zend_object *obj) {
92
9.71k
  if (EXPECTED(!zend_object_is_lazy(obj))) {
93
8.72k
    return "";
94
8.72k
  }
95
96
985
  if (zend_object_is_lazy_proxy(obj)) {
97
578
    return "lazy proxy ";
98
578
  }
99
100
407
  return "lazy ghost ";
101
985
}
102
103
PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
104
3.61M
{
105
3.61M
  HashTable *myht;
106
3.61M
  zend_string *class_name;
107
3.61M
  int is_ref = 0;
108
3.61M
  zend_ulong num;
109
3.61M
  zend_string *key;
110
3.61M
  zval *val;
111
3.61M
  uint32_t count;
112
113
3.61M
  if (level > 1) {
114
3.42M
    php_printf("%*c", level - 1, ' ');
115
3.42M
  }
116
117
3.61M
again:
118
3.61M
  switch (Z_TYPE_P(struc)) {
119
8.71k
    case IS_FALSE:
120
8.71k
      php_printf("%sbool(false)\n", COMMON);
121
8.71k
      break;
122
8.16k
    case IS_TRUE:
123
8.16k
      php_printf("%sbool(true)\n", COMMON);
124
8.16k
      break;
125
2.10M
    case IS_NULL:
126
2.10M
      php_printf("%sNULL\n", COMMON);
127
2.10M
      break;
128
858k
    case IS_LONG:
129
858k
      php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
130
858k
      break;
131
26.9k
    case IS_DOUBLE:
132
26.9k
      php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
133
26.9k
      break;
134
519k
    case IS_STRING:
135
519k
      php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
136
519k
      PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
137
519k
      PUTS("\"\n");
138
519k
      break;
139
71.6k
    case IS_ARRAY:
140
71.6k
      myht = Z_ARRVAL_P(struc);
141
71.6k
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
142
69.7k
        if (GC_IS_RECURSIVE(myht)) {
143
72
          PUTS("*RECURSION*\n");
144
72
          return;
145
72
        }
146
69.6k
        GC_ADDREF(myht);
147
69.6k
        GC_PROTECT_RECURSION(myht);
148
69.6k
      }
149
71.6k
      count = zend_hash_num_elements(myht);
150
71.6k
      php_printf("%sarray(%d) {\n", COMMON, count);
151
2.69M
      ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) {
152
2.69M
        php_array_element_dump(val, num, key, level);
153
2.69M
      } ZEND_HASH_FOREACH_END();
154
71.6k
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
155
69.6k
        GC_UNPROTECT_RECURSION(myht);
156
69.6k
        GC_DTOR_NO_REF(myht);
157
69.6k
      }
158
71.6k
      if (level > 1) {
159
9.50k
        php_printf("%*c", level-1, ' ');
160
9.50k
      }
161
71.6k
      PUTS("}\n");
162
71.6k
      break;
163
10.4k
    case IS_OBJECT: {
164
10.4k
      zend_class_entry *ce = Z_OBJCE_P(struc);
165
10.4k
      if (ce->ce_flags & ZEND_ACC_ENUM) {
166
657
        zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
167
657
        php_printf("%senum(%s::%s)\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval));
168
657
        return;
169
657
      }
170
9.80k
      zend_object *zobj = Z_OBJ_P(struc);
171
9.80k
      uint32_t *guard = zend_get_recursion_guard(zobj);
172
9.80k
      if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) {
173
221
        PUTS("*RECURSION*\n");
174
221
        return;
175
221
      }
176
9.57k
      ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj);
177
178
9.57k
      myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
179
9.57k
      class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
180
9.57k
      const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc));
181
182
9.57k
      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
9.57k
      zend_string_release_ex(class_name, 0);
184
185
9.57k
      if (myht) {
186
9.37k
        zend_ulong num;
187
9.37k
        zend_string *key;
188
9.37k
        zval *val;
189
190
4.22M
        ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) {
191
4.22M
          zend_property_info *prop_info = NULL;
192
193
4.22M
          if (Z_TYPE_P(val) == IS_INDIRECT) {
194
6.20k
            val = Z_INDIRECT_P(val);
195
6.20k
            if (key) {
196
6.20k
              prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
197
6.20k
            }
198
6.20k
          }
199
200
4.22M
          if (!Z_ISUNDEF_P(val) || prop_info) {
201
2.10M
            php_object_property_dump(prop_info, val, num, key, level);
202
2.10M
          }
203
4.22M
        } ZEND_HASH_FOREACH_END();
204
9.37k
        zend_release_properties(myht);
205
9.37k
      }
206
9.57k
      if (level > 1) {
207
2.48k
        php_printf("%*c", level-1, ' ');
208
2.48k
      }
209
9.57k
      PUTS("}\n");
210
9.57k
      ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj);
211
9.57k
      break;
212
9.80k
    }
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.80k
    }
218
1.87k
    case IS_REFERENCE:
219
      //??? hide references with refcount==1 (for compatibility)
220
1.87k
      if (Z_REFCOUNT_P(struc) > 1) {
221
1.04k
        is_ref = 1;
222
1.04k
      }
223
1.87k
      struc = Z_REFVAL_P(struc);
224
1.87k
      goto again;
225
0
      break;
226
864
    default:
227
864
      php_printf("%sUNKNOWN:0\n", COMMON);
228
864
      break;
229
3.61M
  }
230
3.61M
}
231
/* }}} */
232
233
/* {{{ Dumps a string representation of variable to output */
234
PHP_FUNCTION(var_dump)
235
171k
{
236
171k
  zval *args;
237
171k
  int argc;
238
171k
  int i;
239
240
514k
  ZEND_PARSE_PARAMETERS_START(1, -1)
241
514k
    Z_PARAM_VARIADIC('+', args, argc)
242
514k
  ZEND_PARSE_PARAMETERS_END();
243
244
349k
  for (i = 0; i < argc; i++) {
245
177k
    php_var_dump(&args[i], 1);
246
177k
  }
247
171k
}
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
120
{
265
120
  const char *prop_name, *class_name;
266
267
120
  if (key == NULL) { /* numeric key */
268
0
    php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index);
269
120
  } else { /* string key */
270
120
    zend_unmangle_property_name(key, &class_name, &prop_name);
271
120
    php_printf("%*c[", level + 1, ' ');
272
273
120
    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
110
    } else {
280
110
      php_printf("\"%s\"", prop_name);
281
110
    }
282
120
    ZEND_PUTS("]=>\n");
283
120
  }
284
120
  if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) {
285
42
    zend_string *type_str = zend_type_to_string(prop_info->type);
286
42
    php_printf("%*cuninitialized(%s)\n",
287
42
      level + 1, ' ', ZSTR_VAL(type_str));
288
42
    zend_string_release(type_str);
289
78
  } else {
290
78
    php_debug_zval_dump(zv, level + 2);
291
78
  }
292
120
}
293
/* }}} */
294
295
PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */
296
238
{
297
238
  HashTable *myht = NULL;
298
238
  zend_string *class_name;
299
238
  zend_ulong index;
300
238
  zend_string *key;
301
238
  zval *val;
302
238
  uint32_t count;
303
238
  char *packed;
304
305
238
  if (level > 1) {
306
78
    php_printf("%*c", level - 1, ' ');
307
78
  }
308
309
238
  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
17
  case IS_NULL:
317
17
    PUTS("NULL\n");
318
17
    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
51
  case IS_STRING:
326
51
    php_printf("string(%zd) \"", Z_STRLEN_P(struc));
327
51
    PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc));
328
51
    if (Z_REFCOUNTED_P(struc)) {
329
11
      php_printf("\" refcount(%u)\n", Z_REFCOUNT_P(struc));
330
40
    } else {
331
40
      PUTS("\" interned\n");
332
40
    }
333
51
    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_DTOR_NO_REF(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
146
  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
146
    zend_object *zobj = Z_OBJ_P(struc);
370
146
    uint32_t *guard = zend_get_recursion_guard(zobj);
371
146
    if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) {
372
0
      PUTS("*RECURSION*\n");
373
0
      return;
374
0
    }
375
146
    ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj);
376
377
146
    myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG);
378
146
    class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc));
379
146
    const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc));
380
381
146
    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
146
    zend_string_release_ex(class_name, 0);
383
146
    if (myht) {
384
381
      ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
385
381
        zend_property_info *prop_info = NULL;
386
387
381
        if (Z_TYPE_P(val) == IS_INDIRECT) {
388
115
          val = Z_INDIRECT_P(val);
389
115
          if (key) {
390
115
            prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
391
115
          }
392
115
        }
393
394
381
        if (!Z_ISUNDEF_P(val) || prop_info) {
395
120
          zval_object_property_dump(prop_info, val, index, key, level);
396
120
        }
397
381
      } ZEND_HASH_FOREACH_END();
398
141
      zend_release_properties(myht);
399
141
    }
400
146
    if (level > 1) {
401
5
      php_printf("%*c", level - 1, ' ');
402
5
    }
403
146
    PUTS("}\n");
404
146
    ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj);
405
146
    break;
406
146
  }
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
146
  }
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
238
  }
424
238
}
425
/* }}} */
426
427
/* {{{ Dumps a string representation of an internal zend value to output. */
428
PHP_FUNCTION(debug_zval_dump)
429
157
{
430
157
  zval *args;
431
157
  int argc;
432
157
  int i;
433
434
471
  ZEND_PARSE_PARAMETERS_START(1, -1)
435
471
    Z_PARAM_VARIADIC('+', args, argc)
436
471
  ZEND_PARSE_PARAMETERS_END();
437
438
317
  for (i = 0; i < argc; i++) {
439
160
    php_debug_zval_dump(&args[i], 1);
440
160
  }
441
157
}
442
/* }}} */
443
444
#define buffer_append_spaces(buf, num_spaces) \
445
627
  do { \
446
627
    char *tmp_spaces; \
447
627
    size_t tmp_spaces_len; \
448
627
    tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \
449
627
    smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \
450
627
    efree(tmp_spaces); \
451
627
  } 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
153
{
455
153
  if (key == NULL) { /* numeric key */
456
78
    buffer_append_spaces(buf, level+1);
457
78
    smart_str_append_long(buf, (zend_long) index);
458
78
    smart_str_appendl(buf, " => ", 4);
459
460
78
  } 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
153
  zend_result result = php_var_export_ex(zv, level + 2, buf);
475
476
153
  smart_str_appendc(buf, ',');
477
153
  smart_str_appendc(buf, '\n');
478
479
153
  return result;
480
153
}
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
859
{
512
859
  HashTable *myht;
513
859
  zend_string *ztmp, *ztmp2;
514
859
  zend_ulong index;
515
859
  zend_string *key;
516
859
  zval *val;
517
518
859
again:
519
859
  switch (Z_TYPE_P(struc)) {
520
7
    case IS_FALSE:
521
7
      smart_str_appendl(buf, "false", 5);
522
7
      break;
523
5
    case IS_TRUE:
524
5
      smart_str_appendl(buf, "true", 4);
525
5
      break;
526
35
    case IS_NULL:
527
35
      smart_str_appendl(buf, "NULL", 4);
528
35
      break;
529
164
    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
164
      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
164
      smart_str_append_long(buf, Z_LVAL_P(struc));
538
164
      break;
539
5
    case IS_DOUBLE:
540
5
      smart_str_append_double(
541
5
        buf, Z_DVAL_P(struc), (int) PG(serialize_precision), /* zero_fraction */ true);
542
5
      break;
543
439
    case IS_STRING:
544
439
      ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2);
545
439
      ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12);
546
547
439
      smart_str_appendc(buf, '\'');
548
439
      smart_str_append(buf, ztmp2);
549
439
      smart_str_appendc(buf, '\'');
550
551
439
      zend_string_free(ztmp);
552
439
      zend_string_free(ztmp2);
553
439
      break;
554
68
    case IS_ARRAY:
555
68
      myht = Z_ARRVAL_P(struc);
556
68
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
557
47
        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
47
        GC_ADDREF(myht);
563
47
        GC_PROTECT_RECURSION(myht);
564
47
      }
565
68
      if (level > 1) {
566
13
        smart_str_appendc(buf, '\n');
567
13
        buffer_append_spaces(buf, level - 1);
568
13
      }
569
68
      smart_str_appendl(buf, "array (\n", 8);
570
388
      ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) {
571
388
        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
388
      } ZEND_HASH_FOREACH_END();
579
68
      if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) {
580
47
        GC_UNPROTECT_RECURSION(myht);
581
47
        GC_DELREF(myht);
582
47
      }
583
68
      if (level > 1) {
584
13
        buffer_append_spaces(buf, level - 1);
585
13
      }
586
68
      smart_str_appendc(buf, ')');
587
588
68
      break;
589
590
136
    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
136
      zend_object *zobj = Z_OBJ_P(struc);
596
136
      uint32_t *guard = zend_get_recursion_guard(zobj);
597
136
      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
136
      ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, EXPORT, zobj);
603
136
      myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT);
604
136
      if (level > 1) {
605
7
        smart_str_appendc(buf, '\n');
606
7
        buffer_append_spaces(buf, level - 1);
607
7
      }
608
609
136
      zend_class_entry *ce = Z_OBJCE_P(struc);
610
136
      bool is_enum = ce->ce_flags & ZEND_ACC_ENUM;
611
612
      /* stdClass has no __set_state method, but can be casted to */
613
136
      if (ce == zend_standard_class_def) {
614
0
        smart_str_appendl(buf, "(object) array(\n", 16);
615
136
      } else {
616
136
        smart_str_appendc(buf, '\\');
617
136
        smart_str_append(buf, ce->name);
618
136
        if (is_enum) {
619
36
          zend_object *zobj = Z_OBJ_P(struc);
620
36
          zval *case_name_zval = zend_enum_fetch_case_name(zobj);
621
36
          smart_str_appendl(buf, "::", 2);
622
36
          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
136
      }
627
628
136
      if (myht) {
629
131
        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
129
        zend_release_properties(myht);
655
129
      }
656
134
      ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj);
657
134
      if (level > 1 && !is_enum) {
658
0
        buffer_append_spaces(buf, level - 1);
659
0
      }
660
134
      if (ce == zend_standard_class_def) {
661
0
        smart_str_appendc(buf, ')');
662
134
      } else if (!is_enum) {
663
98
        smart_str_appendl(buf, "))", 2);
664
98
      }
665
666
134
      break;
667
136
    }
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
859
  }
676
677
857
  return SUCCESS;
678
859
}
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
265
{
697
265
  zval *var;
698
265
  bool return_output = 0;
699
265
  smart_str buf = {0};
700
701
795
  ZEND_PARSE_PARAMETERS_START(1, 2)
702
1.06k
    Z_PARAM_ZVAL(var)
703
1.06k
    Z_PARAM_OPTIONAL
704
1.06k
    Z_PARAM_BOOL(return_output)
705
265
  ZEND_PARSE_PARAMETERS_END();
706
707
265
  zend_result result = php_var_export_ex(var, 1, &buf);
708
265
  smart_str_0 (&buf);
709
710
265
  if (result == FAILURE) {
711
2
    smart_str_free(&buf);
712
263
  } else if (return_output) {
713
80
    RETURN_STR(smart_str_extract(&buf));
714
183
  } else {
715
183
    PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
716
183
    smart_str_free(&buf);
717
183
  }
718
265
}
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
24.2k
{
728
24.2k
  zval *zv;
729
24.2k
  zend_ulong key;
730
24.2k
  bool is_ref = Z_ISREF_P(var);
731
732
24.2k
  data->n += 1;
733
734
24.2k
  if (is_ref) {
735
    /* pass */
736
21.1k
  } else if (Z_TYPE_P(var) != IS_OBJECT) {
737
20.7k
    return 0;
738
20.7k
  } else if (!in_rcn_array
739
374
   && Z_REFCOUNT_P(var) == 1
740
53
   && (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1)
741
   /* __serialize and __sleep may arbitrarily increase the refcount */
742
53
   && Z_OBJCE_P(var)->__serialize == NULL
743
53
   && zend_hash_find_known_hash(&Z_OBJCE_P(var)->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP)) == NULL) {
744
53
    return 0;
745
53
  }
746
747
  /* References to objects are treated as if the reference didn't exist */
748
3.49k
  if (is_ref && Z_TYPE_P(Z_REFVAL_P(var)) == IS_OBJECT) {
749
0
    var = Z_REFVAL_P(var);
750
0
  }
751
752
  /* Index for the variable is stored using the numeric value of the pointer to
753
   * the zend_refcounted struct */
754
3.49k
  key = (zend_ulong) (uintptr_t) Z_COUNTED_P(var);
755
3.49k
  zv = zend_hash_index_find(&data->ht, key);
756
757
3.49k
  if (zv) {
758
    /* References are only counted once, undo the data->n increment above */
759
1.74k
    if (is_ref && Z_LVAL_P(zv) != -1) {
760
1.72k
      data->n -= 1;
761
1.72k
    }
762
763
1.74k
    return Z_LVAL_P(zv);
764
1.75k
  } else {
765
1.75k
    zval zv_n;
766
1.75k
    ZVAL_LONG(&zv_n, data->n);
767
1.75k
    zend_hash_index_add_new(&data->ht, key, &zv_n);
768
769
    /* Additionally to the index, we also store the variable, to ensure that it is
770
     * not destroyed during serialization and its pointer reused. The variable is
771
     * stored at the numeric value of the pointer + 1, which cannot be the location
772
     * of another zend_refcounted structure. */
773
1.75k
    zend_hash_index_add_new(&data->ht, key + 1, var);
774
1.75k
    Z_ADDREF_P(var);
775
776
1.75k
    return 0;
777
1.75k
  }
778
3.49k
}
779
/* }}} */
780
781
static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */
782
12.1k
{
783
12.1k
  char b[32];
784
12.1k
  char *s = zend_print_long_to_buf(b + sizeof(b) - 1, val);
785
12.1k
  size_t l = b + sizeof(b) - 1 - s;
786
12.1k
  char *res = smart_str_extend(buf, 2 + l + 1);
787
12.1k
  res = zend_mempcpy(res, "i:", 2);
788
12.1k
  memcpy(res, s, l);
789
12.1k
  res[l] = ';';
790
12.1k
}
791
/* }}} */
792
793
static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */
794
17.1k
{
795
17.1k
  char b[32];
796
17.1k
  char *s = zend_print_long_to_buf(b + sizeof(b) - 1, len);
797
17.1k
  size_t l = b + sizeof(b) - 1 - s;
798
17.1k
  char *res = smart_str_extend(buf, 2 + l + 2 + len + 2);
799
17.1k
  res = zend_mempcpy(res, "s:", 2);
800
17.1k
  res = zend_mempcpy(res, s, l);
801
17.1k
  res = zend_mempcpy(res, ":\"", 2);
802
17.1k
  res = zend_mempcpy(res, str, len);
803
17.1k
  memcpy(res, "\";", 2);
804
17.1k
}
805
/* }}} */
806
807
static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */
808
304
{
809
304
  char b[32];
810
304
  PHP_CLASS_ATTRIBUTES;
811
812
304
  PHP_SET_CLASS_ATTRIBUTES(struc);
813
304
  size_t class_name_len = ZSTR_LEN(class_name);
814
304
  char *s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len);
815
304
  size_t l = b + sizeof(b) - 1 - s;
816
304
  char *res = smart_str_extend(buf, 2 + l + 2 + class_name_len + 2);
817
304
  res = zend_mempcpy(res, "O:", 2);
818
304
  res = zend_mempcpy(res, s, l);
819
304
  res = zend_mempcpy(res, ":\"", 2);
820
304
  res = zend_mempcpy(res, ZSTR_VAL(class_name), class_name_len);
821
304
  memcpy(res, "\":", 2);
822
304
  PHP_CLEANUP_CLASS_ATTRIBUTES();
823
304
  return incomplete_class;
824
304
}
825
/* }}} */
826
827
static HashTable* php_var_serialize_call_sleep(zend_object *obj, zend_function *fn) /* {{{ */
828
57
{
829
57
  zval retval;
830
831
57
  BG(serialize_lock)++;
832
57
  zend_call_known_instance_method(fn, obj, &retval, /* param_count */ 0, /* params */ NULL);
833
57
  BG(serialize_lock)--;
834
835
57
  if (Z_ISUNDEF(retval) || EG(exception)) {
836
3
    zval_ptr_dtor(&retval);
837
3
    return NULL;
838
3
  }
839
840
54
  if (Z_TYPE(retval) != IS_ARRAY) {
841
2
    zval_ptr_dtor(&retval);
842
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));
843
2
    return NULL;
844
2
  }
845
846
52
  return Z_ARRVAL(retval);
847
54
}
848
/* }}} */
849
850
static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */
851
36
{
852
36
  BG(serialize_lock)++;
853
36
  zend_call_known_instance_method_with_0_params(
854
36
    Z_OBJCE_P(obj)->__serialize, Z_OBJ_P(obj), retval);
855
36
  BG(serialize_lock)--;
856
857
36
  if (EG(exception)) {
858
0
    zval_ptr_dtor(retval);
859
0
    return FAILURE;
860
0
  }
861
862
36
  if (Z_TYPE_P(retval) != IS_ARRAY) {
863
0
    zval_ptr_dtor(retval);
864
0
    zend_type_error("%s::__serialize() must return an array", ZSTR_VAL(Z_OBJCE_P(obj)->name));
865
0
    return FAILURE;
866
0
  }
867
868
36
  return SUCCESS;
869
36
}
870
/* }}} */
871
872
static int php_var_serialize_try_add_sleep_prop(
873
    HashTable *ht, HashTable *props, zend_string *name, zend_string *error_name, zval *struc) /* {{{ */
874
72
{
875
72
  zval *val = zend_hash_find(props, name);
876
72
  if (val == NULL) {
877
21
    return FAILURE;
878
21
  }
879
880
51
  if (Z_TYPE_P(val) == IS_INDIRECT) {
881
51
    val = Z_INDIRECT_P(val);
882
51
    if (Z_TYPE_P(val) == IS_UNDEF) {
883
23
      zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
884
23
      if (info) {
885
23
        return SUCCESS;
886
23
      }
887
0
      return FAILURE;
888
23
    }
889
51
  }
890
891
28
  if (!zend_hash_add(ht, name, val)) {
892
0
    php_error_docref(NULL, E_WARNING,
893
0
      "\"%s\" is returned from __sleep() multiple times", ZSTR_VAL(error_name));
894
0
    return SUCCESS;
895
0
  }
896
897
28
  Z_TRY_ADDREF_P(val);
898
28
  return SUCCESS;
899
28
}
900
/* }}} */
901
902
static int php_var_serialize_get_sleep_props(
903
    HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */
904
52
{
905
52
  zend_class_entry *ce = Z_OBJCE_P(struc);
906
52
  HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
907
52
  zval *name_val;
908
52
  int retval = SUCCESS;
909
910
52
  zend_hash_init(ht, zend_hash_num_elements(sleep_retval), NULL, ZVAL_PTR_DTOR, 0);
911
  /* TODO: Rewrite this by fetching the property info instead of trying out different
912
   * name manglings? */
913
172
  ZEND_HASH_FOREACH_VAL_IND(sleep_retval, name_val) {
914
172
    zend_string *name, *tmp_name, *priv_name, *prot_name;
915
916
172
    ZVAL_DEREF(name_val);
917
172
    if (Z_TYPE_P(name_val) != IS_STRING) {
918
0
      php_error_docref(NULL, E_WARNING,
919
0
          "%s::__sleep() should return an array only containing the names of instance-variables to serialize",
920
0
          ZSTR_VAL(ce->name));
921
0
    }
922
923
172
    name = zval_get_tmp_string(name_val, &tmp_name);
924
172
    if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) {
925
43
      zend_tmp_string_release(tmp_name);
926
43
      continue;
927
43
    }
928
929
17
    if (EG(exception)) {
930
7
      zend_tmp_string_release(tmp_name);
931
7
      retval = FAILURE;
932
7
      break;
933
7
    }
934
935
10
    priv_name = zend_mangle_property_name(
936
10
      ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
937
10
      ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
938
10
    if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) {
939
8
      zend_tmp_string_release(tmp_name);
940
8
      zend_string_release(priv_name);
941
8
      continue;
942
8
    }
943
2
    zend_string_release(priv_name);
944
945
2
    if (EG(exception)) {
946
0
      zend_tmp_string_release(tmp_name);
947
0
      retval = FAILURE;
948
0
      break;
949
0
    }
950
951
2
    prot_name = zend_mangle_property_name(
952
2
      "*", 1, ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS);
953
2
    if (php_var_serialize_try_add_sleep_prop(ht, props, prot_name, name, struc) == SUCCESS) {
954
0
      zend_tmp_string_release(tmp_name);
955
0
      zend_string_release(prot_name);
956
0
      continue;
957
0
    }
958
2
    zend_string_release(prot_name);
959
960
2
    if (EG(exception)) {
961
0
      zend_tmp_string_release(tmp_name);
962
0
      retval = FAILURE;
963
0
      break;
964
0
    }
965
966
2
    php_error_docref(NULL, E_WARNING,
967
2
      "\"%s\" returned as member variable from __sleep() but does not exist", ZSTR_VAL(name));
968
2
    zend_tmp_string_release(tmp_name);
969
2
  } ZEND_HASH_FOREACH_END();
970
971
52
  zend_release_properties(props);
972
52
  return retval;
973
52
}
974
/* }}} */
975
976
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) /* {{{ */
977
13.7k
{
978
13.7k
  smart_str_append_unsigned(buf, count);
979
13.7k
  smart_str_appendl(buf, ":{", 2);
980
13.7k
  if (count > 0) {
981
3.44k
    zend_string *key;
982
3.44k
    zval *data;
983
3.44k
    zend_ulong index;
984
985
49.4k
    ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) {
986
49.4k
      if (incomplete_class && zend_string_equals_literal(key, MAGIC_MEMBER)) {
987
0
        incomplete_class = false;
988
0
        continue;
989
0
      }
990
991
22.9k
      if (!key) {
992
7.95k
        php_var_serialize_long(buf, index);
993
14.9k
      } else {
994
14.9k
        php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
995
14.9k
      }
996
997
22.9k
      if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
998
54
        data = Z_REFVAL_P(data);
999
54
      }
1000
1001
      /* we should still add element even if it's not OK,
1002
       * since we already wrote the length of the array before */
1003
22.9k
      if (Z_TYPE_P(data) == IS_ARRAY) {
1004
12.5k
        if (UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
1005
0
          php_add_var_hash(var_hash, struc, in_rcn_array);
1006
0
          smart_str_appendl(buf, "N;", 2);
1007
12.5k
        } else {
1008
12.5k
          php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
1009
12.5k
        }
1010
12.5k
      } else {
1011
10.3k
        php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
1012
10.3k
      }
1013
22.9k
    } ZEND_HASH_FOREACH_END();
1014
3.44k
  }
1015
13.7k
  smart_str_appendc(buf, '}');
1016
13.7k
}
1017
/* }}} */
1018
1019
static void php_var_serialize_class(smart_str *buf, zval *struc, HashTable *ht, php_serialize_data_t var_hash) /* {{{ */
1020
52
{
1021
52
  HashTable props;
1022
1023
52
  if (php_var_serialize_get_sleep_props(&props, struc, ht) == SUCCESS) {
1024
45
    php_var_serialize_class_name(buf, struc);
1025
45
    php_var_serialize_nested_data(
1026
45
      buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ false, var_hash,
1027
45
      GC_REFCOUNT(&props) > 1);
1028
45
  }
1029
52
  zend_hash_destroy(&props);
1030
52
}
1031
/* }}} */
1032
1033
static zend_always_inline bool php_serialize_check_stack_limit(void)
1034
24.2k
{
1035
24.2k
#ifdef ZEND_CHECK_STACK_LIMIT
1036
24.2k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
1037
0
    zend_call_stack_size_error();
1038
0
    return true;
1039
0
  }
1040
24.2k
#endif
1041
24.2k
  return false;
1042
24.2k
}
1043
1044
static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash, bool in_rcn_array, bool is_root) /* {{{ */
1045
24.2k
{
1046
24.2k
  zend_long var_already;
1047
24.2k
  HashTable *myht;
1048
1049
24.2k
  if (EG(exception)) {
1050
0
    return;
1051
0
  }
1052
1053
24.2k
  if (UNEXPECTED(php_serialize_check_stack_limit())) {
1054
0
    return;
1055
0
  }
1056
1057
24.2k
  if (var_hash && (var_already = php_add_var_hash(var_hash, struc, in_rcn_array))) {
1058
1.74k
    if (var_already == -1) {
1059
      /* Reference to an object that failed to serialize, replace with null. */
1060
0
      smart_str_appendl(buf, "N;", 2);
1061
0
      return;
1062
1.74k
    } else if (Z_ISREF_P(struc)) {
1063
1.72k
      smart_str_appendl(buf, "R:", 2);
1064
1.72k
      smart_str_append_long(buf, var_already);
1065
1.72k
      smart_str_appendc(buf, ';');
1066
1.72k
      return;
1067
1.72k
    } else if (Z_TYPE_P(struc) == IS_OBJECT) {
1068
14
      smart_str_appendl(buf, "r:", 2);
1069
14
      smart_str_append_long(buf, var_already);
1070
14
      smart_str_appendc(buf, ';');
1071
14
      return;
1072
14
    }
1073
1.74k
  }
1074
1075
23.9k
again:
1076
23.9k
  switch (Z_TYPE_P(struc)) {
1077
2
    case IS_FALSE:
1078
2
      smart_str_appendl(buf, "b:0;", 4);
1079
2
      return;
1080
1081
2
    case IS_TRUE:
1082
2
      smart_str_appendl(buf, "b:1;", 4);
1083
2
      return;
1084
1085
2.48k
    case IS_NULL:
1086
2.48k
      smart_str_appendl(buf, "N;", 2);
1087
2.48k
      return;
1088
1089
4.17k
    case IS_LONG:
1090
4.17k
      php_var_serialize_long(buf, Z_LVAL_P(struc));
1091
4.17k
      return;
1092
1093
212
    case IS_DOUBLE: {
1094
212
      char tmp_str[ZEND_DOUBLE_MAX_LENGTH];
1095
212
      zend_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
1096
1097
212
      size_t len = strlen(tmp_str);
1098
212
      char *res = smart_str_extend(buf, 2 + len + 1);
1099
212
      res = zend_mempcpy(res, "d:", 2);
1100
212
      memcpy(res, tmp_str, len);
1101
212
      res[len] = ';';
1102
212
      return;
1103
0
    }
1104
1105
1.68k
    case IS_STRING:
1106
1.68k
      php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc));
1107
1.68k
      return;
1108
1109
388
    case IS_OBJECT: {
1110
388
        zend_class_entry *ce = Z_OBJCE_P(struc);
1111
388
        bool incomplete_class;
1112
388
        uint32_t count;
1113
1114
388
        if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
1115
28
          zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed",
1116
28
            ZSTR_VAL(ce->name));
1117
28
          return;
1118
28
        }
1119
1120
360
        if (ce->ce_flags & ZEND_ACC_ENUM) {
1121
34
          PHP_CLASS_ATTRIBUTES;
1122
1123
34
          zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
1124
1125
34
          PHP_SET_CLASS_ATTRIBUTES(struc);
1126
34
          smart_str_appendl(buf, "E:", 2);
1127
34
          smart_str_append_unsigned(buf, ZSTR_LEN(class_name) + strlen(":") + Z_STRLEN_P(case_name_zval));
1128
34
          smart_str_appendl(buf, ":\"", 2);
1129
34
          smart_str_append(buf, class_name);
1130
34
          smart_str_appendc(buf, ':');
1131
34
          smart_str_append(buf, Z_STR_P(case_name_zval));
1132
34
          smart_str_appendl(buf, "\";", 2);
1133
34
          PHP_CLEANUP_CLASS_ATTRIBUTES();
1134
34
          return;
1135
34
        }
1136
1137
326
        if (ce->__serialize) {
1138
36
          zval retval, obj;
1139
36
          zend_string *key;
1140
36
          zval *data;
1141
36
          zend_ulong index;
1142
1143
36
          ZVAL_OBJ_COPY(&obj, Z_OBJ_P(struc));
1144
36
          if (php_var_serialize_call_magic_serialize(&retval, &obj) == FAILURE) {
1145
0
            if (!EG(exception)) {
1146
0
              smart_str_appendl(buf, "N;", 2);
1147
0
            }
1148
0
            zval_ptr_dtor(&obj);
1149
0
            return;
1150
0
          }
1151
1152
36
          php_var_serialize_class_name(buf, &obj);
1153
36
          smart_str_append_unsigned(buf, zend_hash_num_elements(Z_ARRVAL(retval)));
1154
36
          smart_str_appendl(buf, ":{", 2);
1155
96
          ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(retval), index, key, data) {
1156
96
            if (!key) {
1157
20
              php_var_serialize_long(buf, index);
1158
20
            } else {
1159
10
              php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
1160
10
            }
1161
1162
96
            if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
1163
0
              data = Z_REFVAL_P(data);
1164
0
            }
1165
96
            php_var_serialize_intern(buf, data, var_hash, Z_REFCOUNT(retval) > 1, false);
1166
96
          } ZEND_HASH_FOREACH_END();
1167
36
          smart_str_appendc(buf, '}');
1168
1169
36
          zval_ptr_dtor(&obj);
1170
36
          zval_ptr_dtor(&retval);
1171
36
          return;
1172
36
        }
1173
1174
290
        if (ce->serialize != NULL) {
1175
          /* has custom handler */
1176
10
          unsigned char *serialized_data = NULL;
1177
10
          size_t serialized_length;
1178
1179
10
          if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) {
1180
5
            char b1[32], b2[32];
1181
5
            char *s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name));
1182
5
            size_t l1 = b1 + sizeof(b1) - 1 - s1;
1183
5
            char *s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length);
1184
5
            size_t l2 = b2 + sizeof(b2) - 1 - s2;
1185
5
            char *res = smart_str_extend(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1);
1186
5
            res = zend_mempcpy(res, "C:", 2);
1187
5
            res = zend_mempcpy(res, s1, l1);
1188
5
            res = zend_mempcpy(res, ":\"", 2);
1189
5
            res = zend_mempcpy(res, ZSTR_VAL(Z_OBJCE_P(struc)->name), ZSTR_LEN(Z_OBJCE_P(struc)->name));
1190
5
            res = zend_mempcpy(res, "\":", 2);
1191
5
            res = zend_mempcpy(res, s2, l2);
1192
5
            res = zend_mempcpy(res, ":{", 2);
1193
5
            memcpy(res, (char *) serialized_data, serialized_length);
1194
5
            res[serialized_length] = '}';
1195
5
          } else {
1196
            /* Mark this value in the var_hash, to avoid creating references to it. */
1197
5
            zval *var_idx = zend_hash_index_find(&var_hash->ht,
1198
5
              (zend_ulong) (uintptr_t) Z_COUNTED_P(struc));
1199
5
            if (var_idx) {
1200
0
              ZVAL_LONG(var_idx, -1);
1201
0
            }
1202
5
            smart_str_appendl(buf, "N;", 2);
1203
5
          }
1204
10
          if (serialized_data) {
1205
5
            efree(serialized_data);
1206
5
          }
1207
10
          return;
1208
10
        }
1209
1210
280
        if (ce != PHP_IC_ENTRY) {
1211
280
          zval *zv = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP));
1212
1213
280
          if (zv) {
1214
57
            HashTable *ht;
1215
57
            zval tmp;
1216
1217
57
            ZVAL_OBJ_COPY(&tmp, Z_OBJ_P(struc));
1218
57
            if (!(ht = php_var_serialize_call_sleep(Z_OBJ(tmp), Z_FUNC_P(zv)))) {
1219
5
              if (!EG(exception)) {
1220
                /* we should still add element even if it's not OK,
1221
                 * since we already wrote the length of the array before */
1222
2
                smart_str_appendl(buf, "N;", 2);
1223
2
              }
1224
5
              OBJ_RELEASE(Z_OBJ(tmp));
1225
5
              return;
1226
5
            }
1227
1228
52
            php_var_serialize_class(buf, &tmp, ht, var_hash);
1229
52
            zend_array_release(ht);
1230
52
            OBJ_RELEASE(Z_OBJ(tmp));
1231
52
            return;
1232
57
          }
1233
280
        }
1234
1235
223
        incomplete_class = php_var_serialize_class_name(buf, struc);
1236
1237
223
        if (Z_OBJ_P(struc)->properties == NULL
1238
171
         && Z_OBJ_HT_P(struc)->get_properties_for == NULL
1239
171
         && Z_OBJ_HT_P(struc)->get_properties == zend_std_get_properties
1240
171
         && !zend_object_is_lazy(Z_OBJ_P(struc))) {
1241
          /* Optimized version without rebulding properties HashTable */
1242
114
          zend_object *obj = Z_OBJ_P(struc);
1243
114
          zend_class_entry *ce = obj->ce;
1244
114
          zend_property_info *prop_info;
1245
114
          zval *prop;
1246
114
          int i;
1247
1248
114
          count = ce->default_properties_count;
1249
639
          for (i = 0; i < ce->default_properties_count; i++) {
1250
525
            prop_info = ce->properties_info_table[i];
1251
525
            if (!prop_info) {
1252
8
              count--;
1253
8
              continue;
1254
8
            }
1255
517
            prop = OBJ_PROP(obj, prop_info->offset);
1256
517
            if (Z_TYPE_P(prop) == IS_UNDEF) {
1257
0
              count--;
1258
0
              continue;
1259
0
            }
1260
517
          }
1261
114
          if (count) {
1262
109
            smart_str_append_unsigned(buf, count);
1263
109
            smart_str_appendl(buf, ":{", 2);
1264
634
            for (i = 0; i < ce->default_properties_count; i++) {
1265
525
              prop_info = ce->properties_info_table[i];
1266
525
              if (!prop_info) {
1267
8
                continue;
1268
8
              }
1269
517
              prop = OBJ_PROP(obj, prop_info->offset);
1270
517
              if (Z_TYPE_P(prop) == IS_UNDEF) {
1271
0
                continue;
1272
0
              }
1273
1274
517
              php_var_serialize_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name));
1275
1276
517
              if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
1277
0
                prop = Z_REFVAL_P(prop);
1278
0
              }
1279
1280
517
              php_var_serialize_intern(buf, prop, var_hash, false, false);
1281
517
            }
1282
109
            smart_str_appendc(buf, '}');
1283
109
          } else {
1284
5
            smart_str_appendl(buf, "0:{}", 4);
1285
5
          }
1286
114
          return;
1287
114
        }
1288
109
        myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE);
1289
        /* count after serializing name, since php_var_serialize_class_name
1290
         * changes the count if the variable is incomplete class */
1291
109
        count = zend_array_count(myht);
1292
109
        if (count > 0 && incomplete_class) {
1293
0
          --count;
1294
0
        }
1295
109
        php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash, GC_REFCOUNT(myht) > 1);
1296
109
        zend_release_properties(myht);
1297
109
        return;
1298
223
      }
1299
13.6k
    case IS_ARRAY:
1300
13.6k
      smart_str_appendl(buf, "a:", 2);
1301
13.6k
      myht = Z_ARRVAL_P(struc);
1302
13.6k
      php_var_serialize_nested_data(
1303
13.6k
        buf, struc, myht, zend_array_count(myht), /* incomplete_class */ false, var_hash,
1304
13.6k
        !is_root && (in_rcn_array || GC_REFCOUNT(myht) > 1));
1305
13.6k
      return;
1306
1.41k
    case IS_REFERENCE:
1307
1.41k
      struc = Z_REFVAL_P(struc);
1308
1.41k
      goto again;
1309
0
    default:
1310
0
      smart_str_appendl(buf, "i:0;", 4);
1311
0
      return;
1312
23.9k
  }
1313
23.9k
}
1314
/* }}} */
1315
1316
PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */
1317
838
{
1318
838
  php_var_serialize_intern(buf, struc, *data, false, true);
1319
838
  smart_str_0(buf);
1320
838
}
1321
/* }}} */
1322
1323
838
PHPAPI php_serialize_data_t php_var_serialize_init(void) {
1324
838
  struct php_serialize_data *d;
1325
  /* fprintf(stderr, "SERIALIZE_INIT      == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
1326
838
  if (BG(serialize_lock) || !BG(serialize).level) {
1327
838
    d = emalloc(sizeof(struct php_serialize_data));
1328
838
    zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0);
1329
838
    d->n = 0;
1330
838
    if (!BG(serialize_lock)) {
1331
838
      BG(serialize).data = d;
1332
838
      BG(serialize).level = 1;
1333
838
    }
1334
838
  } else {
1335
0
    d = BG(serialize).data;
1336
0
    ++BG(serialize).level;
1337
0
  }
1338
838
  return d;
1339
838
}
1340
1341
838
PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) {
1342
  /* fprintf(stderr, "SERIALIZE_DESTROY   == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
1343
838
  if (BG(serialize_lock) || BG(serialize).level == 1) {
1344
838
    zend_hash_destroy(&d->ht);
1345
838
    efree(d);
1346
838
  }
1347
838
  if (!BG(serialize_lock) && !--BG(serialize).level) {
1348
838
    BG(serialize).data = NULL;
1349
838
  }
1350
838
}
1351
1352
/* {{{ Returns a string representation of variable (which can later be unserialized) */
1353
PHP_FUNCTION(serialize)
1354
838
{
1355
838
  zval *struc;
1356
838
  php_serialize_data_t var_hash;
1357
838
  smart_str buf = {0};
1358
1359
2.51k
  ZEND_PARSE_PARAMETERS_START(1, 1)
1360
3.35k
    Z_PARAM_ZVAL(struc)
1361
3.35k
  ZEND_PARSE_PARAMETERS_END();
1362
1363
838
  PHP_VAR_SERIALIZE_INIT(var_hash);
1364
838
  php_var_serialize(&buf, struc, &var_hash);
1365
838
  PHP_VAR_SERIALIZE_DESTROY(var_hash);
1366
1367
838
  if (EG(exception)) {
1368
56
    smart_str_free(&buf);
1369
56
    RETURN_THROWS();
1370
56
  }
1371
1372
782
  RETURN_STR(smart_str_extract(&buf));
1373
782
}
1374
/* }}} */
1375
1376
/* {{{ Takes a string representation of variable and recreates it, subject to the optional unserialize options HashTable */
1377
PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, const size_t buf_len, HashTable *options, const char* function_name)
1378
1.20k
{
1379
1.20k
  const unsigned char *p;
1380
1.20k
  php_unserialize_data_t var_hash;
1381
1.20k
  zval *retval;
1382
1.20k
  HashTable *class_hash = NULL, *prev_class_hash;
1383
1.20k
  zend_long prev_max_depth, prev_cur_depth;
1384
1385
1.20k
  if (buf_len == 0) {
1386
181
    RETURN_FALSE;
1387
181
  }
1388
1389
1.02k
  p = (const unsigned char*) buf;
1390
1.02k
  PHP_VAR_UNSERIALIZE_INIT(var_hash);
1391
1392
1.02k
  prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash);
1393
1.02k
  prev_max_depth = php_var_unserialize_get_max_depth(var_hash);
1394
1.02k
  prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash);
1395
1.02k
  if (options != NULL) {
1396
0
    zval *classes, *max_depth;
1397
1398
0
    classes = zend_hash_str_find_deref(options, "allowed_classes", sizeof("allowed_classes")-1);
1399
0
    if (classes && Z_TYPE_P(classes) != IS_ARRAY && Z_TYPE_P(classes) != IS_TRUE && Z_TYPE_P(classes) != IS_FALSE) {
1400
0
      zend_type_error("%s(): Option \"allowed_classes\" must be of type array|bool, %s given", function_name, zend_zval_value_name(classes));
1401
0
      goto cleanup;
1402
0
    }
1403
1404
0
    if (classes && (Z_TYPE_P(classes) == IS_ARRAY || !zend_is_true(classes))) {
1405
0
      ALLOC_HASHTABLE(class_hash);
1406
0
      zend_hash_init(class_hash, (Z_TYPE_P(classes) == IS_ARRAY)?zend_hash_num_elements(Z_ARRVAL_P(classes)):0, NULL, NULL, 0);
1407
0
    }
1408
0
    if (class_hash && Z_TYPE_P(classes) == IS_ARRAY) {
1409
0
      zval *entry;
1410
1411
0
      ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(classes), entry) {
1412
0
        ZVAL_DEREF(entry);
1413
0
        if (UNEXPECTED(Z_TYPE_P(entry) != IS_STRING && Z_TYPE_P(entry) != IS_OBJECT)) {
1414
0
          zend_type_error("%s(): Option \"allowed_classes\" must be an array of class names, %s given",
1415
0
            function_name, zend_zval_value_name(entry));
1416
0
          goto cleanup;
1417
0
        }
1418
0
        zend_string *name = zval_try_get_string(entry);
1419
0
        if (UNEXPECTED(name == NULL)) {
1420
0
          goto cleanup;
1421
0
        }
1422
0
        if (UNEXPECTED(!zend_is_valid_class_name(name))) {
1423
0
          zend_value_error("%s(): Option \"allowed_classes\" must be an array of class names, \"%s\" given", function_name, ZSTR_VAL(name));
1424
0
          zend_string_release_ex(name, false);
1425
0
          goto cleanup;
1426
0
        }
1427
0
        zend_string *lcname = zend_string_tolower(name);
1428
0
        zend_hash_add_empty_element(class_hash, lcname);
1429
0
            zend_string_release_ex(name, false);
1430
0
            zend_string_release_ex(lcname, false);
1431
0
      } ZEND_HASH_FOREACH_END();
1432
0
    }
1433
0
    php_var_unserialize_set_allowed_classes(var_hash, class_hash);
1434
1435
0
    max_depth = zend_hash_str_find_deref(options, "max_depth", sizeof("max_depth") - 1);
1436
0
    if (max_depth) {
1437
0
      if (Z_TYPE_P(max_depth) != IS_LONG) {
1438
0
        zend_type_error("%s(): Option \"max_depth\" must be of type int, %s given", function_name, zend_zval_value_name(max_depth));
1439
0
        goto cleanup;
1440
0
      }
1441
0
      if (Z_LVAL_P(max_depth) < 0) {
1442
0
        zend_value_error("%s(): Option \"max_depth\" must be greater than or equal to 0", function_name);
1443
0
        goto cleanup;
1444
0
      }
1445
1446
0
      php_var_unserialize_set_max_depth(var_hash, Z_LVAL_P(max_depth));
1447
      /* If the max_depth for a nested unserialize() call has been overridden,
1448
       * start counting from zero again (for the nested call only). */
1449
0
      php_var_unserialize_set_cur_depth(var_hash, 0);
1450
0
    }
1451
0
  }
1452
1453
1.02k
  if (BG(unserialize).level > 1) {
1454
0
    retval = var_tmp_var(&var_hash);
1455
1.02k
  } else {
1456
1.02k
    retval = return_value;
1457
1.02k
  }
1458
1.02k
  if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) {
1459
274
    if (!EG(exception)) {
1460
224
      php_error_docref(NULL, E_WARNING, "Error at offset " ZEND_LONG_FMT " of %zd bytes",
1461
224
        (zend_long)((char*)p - buf), buf_len);
1462
224
    }
1463
274
    if (BG(unserialize).level <= 1) {
1464
274
      zval_ptr_dtor(return_value);
1465
274
    }
1466
274
    RETVAL_FALSE;
1467
748
  } else {
1468
748
    if ((char*)p < buf + buf_len) {
1469
0
      if (!EG(exception)) {
1470
0
        php_error_docref(NULL, E_WARNING, "Extra data starting at offset " ZEND_LONG_FMT " of %zd bytes",
1471
0
          (zend_long)((char*)p - buf), buf_len);
1472
0
      }
1473
0
    }
1474
748
    if (BG(unserialize).level > 1) {
1475
0
      ZVAL_COPY(return_value, retval);
1476
748
    } else if (Z_REFCOUNTED_P(return_value)) {
1477
645
      zend_refcounted *ref = Z_COUNTED_P(return_value);
1478
645
      gc_check_possible_root(ref);
1479
645
    }
1480
748
  }
1481
1482
1.02k
cleanup:
1483
1.02k
  if (class_hash) {
1484
0
    zend_hash_destroy(class_hash);
1485
0
    FREE_HASHTABLE(class_hash);
1486
0
  }
1487
1488
  /* Reset to previous options in case this is a nested call */
1489
1.02k
  php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash);
1490
1.02k
  php_var_unserialize_set_max_depth(var_hash, prev_max_depth);
1491
1.02k
  php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth);
1492
1.02k
  PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1493
1494
  /* Per calling convention we must not return a reference here, so unwrap. We're doing this at
1495
   * the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect
1496
   * the value we unwrap here. This is compatible with behavior in PHP <=7.0. */
1497
1.02k
  if (Z_ISREF_P(return_value)) {
1498
0
    zend_unwrap_reference(return_value);
1499
0
  }
1500
1.02k
}
1501
/* }}} */
1502
1503
/* {{{ Takes a string representation of variable and recreates it */
1504
PHP_FUNCTION(unserialize)
1505
1.20k
{
1506
1.20k
  char *buf = NULL;
1507
1.20k
  size_t buf_len;
1508
1.20k
  HashTable *options = NULL;
1509
1510
3.62k
  ZEND_PARSE_PARAMETERS_START(1, 2)
1511
4.82k
    Z_PARAM_STRING(buf, buf_len)
1512
1.20k
    Z_PARAM_OPTIONAL
1513
2.41k
    Z_PARAM_ARRAY_HT(options)
1514
1.20k
  ZEND_PARSE_PARAMETERS_END();
1515
1516
1.20k
  php_unserialize_with_options(return_value, buf, buf_len, options, "unserialize");
1517
1.20k
}
1518
/* }}} */
1519
1520
/* {{{ Returns the allocated by PHP memory */
1521
34
PHP_FUNCTION(memory_get_usage) {
1522
34
  bool real_usage = 0;
1523
1524
102
  ZEND_PARSE_PARAMETERS_START(0, 1)
1525
102
    Z_PARAM_OPTIONAL
1526
102
    Z_PARAM_BOOL(real_usage)
1527
34
  ZEND_PARSE_PARAMETERS_END();
1528
1529
34
  RETURN_LONG(zend_memory_usage(real_usage));
1530
34
}
1531
/* }}} */
1532
1533
/* {{{ Returns the peak allocated by PHP memory */
1534
0
PHP_FUNCTION(memory_get_peak_usage) {
1535
0
  bool real_usage = 0;
1536
1537
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
1538
0
    Z_PARAM_OPTIONAL
1539
0
    Z_PARAM_BOOL(real_usage)
1540
0
  ZEND_PARSE_PARAMETERS_END();
1541
1542
0
  RETURN_LONG(zend_memory_peak_usage(real_usage));
1543
0
}
1544
/* }}} */
1545
1546
/* {{{ Resets the peak PHP memory usage */
1547
0
PHP_FUNCTION(memory_reset_peak_usage) {
1548
0
  ZEND_PARSE_PARAMETERS_NONE();
1549
1550
0
  zend_memory_reset_peak_usage();
1551
0
}
1552
/* }}} */
1553
1554
PHP_INI_BEGIN()
1555
  STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals)
1556
PHP_INI_END()
1557
1558
PHP_MINIT_FUNCTION(var)
1559
16
{
1560
16
  REGISTER_INI_ENTRIES();
1561
16
  return SUCCESS;
1562
16
}