Coverage Report

Created: 2025-12-14 06:09

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