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