/src/php-src/ext/standard/var.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Jani Lehtimäki <jkl@njet.net> | |
14 | | | Thies C. Arntzen <thies@thieso.net> | |
15 | | | Sascha Schumann <sascha@schumann.cx> | |
16 | | +----------------------------------------------------------------------+ |
17 | | */ |
18 | | |
19 | | /* {{{ includes */ |
20 | | #include <stdio.h> |
21 | | #include <stdlib.h> |
22 | | #include <errno.h> |
23 | | #include "php.h" |
24 | | #include "php_string.h" |
25 | | #include "php_var.h" |
26 | | #include "zend_lazy_objects.h" |
27 | | #include "zend_smart_str.h" |
28 | | #include "basic_functions.h" |
29 | | #include "php_incomplete_class.h" |
30 | | #include "zend_enum.h" |
31 | | #include "zend_exceptions.h" |
32 | | #include "zend_types.h" |
33 | | /* }}} */ |
34 | | |
35 | | struct php_serialize_data { |
36 | | HashTable ht; |
37 | | uint32_t n; |
38 | | }; |
39 | | |
40 | 3.30M | #define COMMON (is_ref ? "&" : "") |
41 | | |
42 | | static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
43 | 1.12M | { |
44 | 1.12M | if (key == NULL) { /* numeric key */ |
45 | 978k | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
46 | 978k | } else { /* string key */ |
47 | 142k | php_printf("%*c[\"", level + 1, ' '); |
48 | 142k | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
49 | 142k | php_printf("\"]=>\n"); |
50 | 142k | } |
51 | 1.12M | php_var_dump(zv, level + 2); |
52 | 1.12M | } |
53 | | /* }}} */ |
54 | | |
55 | | static void php_object_property_dump(zend_property_info *prop_info, zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */ |
56 | 2.10M | { |
57 | 2.10M | const char *prop_name, *class_name; |
58 | | |
59 | 2.10M | if (key == NULL) { /* numeric key */ |
60 | 2.09M | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
61 | 2.09M | } else { /* string key */ |
62 | 4.17k | int unmangle = zend_unmangle_property_name(key, &class_name, &prop_name); |
63 | 4.17k | php_printf("%*c[", level + 1, ' '); |
64 | | |
65 | 4.17k | if (class_name && unmangle == SUCCESS) { |
66 | 268 | if (class_name[0] == '*') { |
67 | 28 | php_printf("\"%s\":protected", prop_name); |
68 | 240 | } else { |
69 | 240 | php_printf("\"%s\":\"%s\":private", prop_name, class_name); |
70 | 240 | } |
71 | 3.90k | } else { |
72 | 3.90k | php_printf("\""); |
73 | 3.90k | PHPWRITE(ZSTR_VAL(key), ZSTR_LEN(key)); |
74 | 3.90k | php_printf("\""); |
75 | 3.90k | } |
76 | 4.17k | ZEND_PUTS("]=>\n"); |
77 | 4.17k | } |
78 | | |
79 | 2.10M | if (Z_TYPE_P(zv) == IS_UNDEF) { |
80 | 55 | ZEND_ASSERT(ZEND_TYPE_IS_SET(prop_info->type)); |
81 | 55 | zend_string *type_str = zend_type_to_string(prop_info->type); |
82 | 55 | php_printf("%*cuninitialized(%s)\n", |
83 | 55 | level + 1, ' ', ZSTR_VAL(type_str)); |
84 | 55 | zend_string_release(type_str); |
85 | 2.10M | } else { |
86 | 2.10M | php_var_dump(zv, level + 2); |
87 | 2.10M | } |
88 | 2.10M | } |
89 | | /* }}} */ |
90 | | |
91 | 2.08k | static const char *php_var_dump_object_prefix(zend_object *obj) { |
92 | 2.08k | if (EXPECTED(!zend_object_is_lazy(obj))) { |
93 | 2.01k | return ""; |
94 | 2.01k | } |
95 | | |
96 | 69 | if (zend_object_is_lazy_proxy(obj)) { |
97 | 39 | return "lazy proxy "; |
98 | 39 | } |
99 | | |
100 | 30 | return "lazy ghost "; |
101 | 69 | } |
102 | | |
103 | | PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */ |
104 | 3.30M | { |
105 | 3.30M | HashTable *myht; |
106 | 3.30M | zend_string *class_name; |
107 | 3.30M | int is_ref = 0; |
108 | 3.30M | zend_ulong num; |
109 | 3.30M | zend_string *key; |
110 | 3.30M | zval *val; |
111 | 3.30M | uint32_t count; |
112 | | |
113 | 3.30M | if (level > 1) { |
114 | 3.22M | php_printf("%*c", level - 1, ' '); |
115 | 3.22M | } |
116 | | |
117 | 3.30M | again: |
118 | 3.30M | switch (Z_TYPE_P(struc)) { |
119 | 2.26k | case IS_FALSE: |
120 | 2.26k | php_printf("%sbool(false)\n", COMMON); |
121 | 2.26k | break; |
122 | 3.05k | case IS_TRUE: |
123 | 3.05k | php_printf("%sbool(true)\n", COMMON); |
124 | 3.05k | break; |
125 | 2.10M | case IS_NULL: |
126 | 2.10M | php_printf("%sNULL\n", COMMON); |
127 | 2.10M | break; |
128 | 758k | case IS_LONG: |
129 | 758k | php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc)); |
130 | 758k | break; |
131 | 54.9k | case IS_DOUBLE: |
132 | 54.9k | php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc)); |
133 | 54.9k | break; |
134 | 347k | case IS_STRING: |
135 | 347k | php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc)); |
136 | 347k | PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
137 | 347k | PUTS("\"\n"); |
138 | 347k | break; |
139 | 36.3k | case IS_ARRAY: |
140 | 36.3k | myht = Z_ARRVAL_P(struc); |
141 | 36.3k | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
142 | 36.0k | if (GC_IS_RECURSIVE(myht)) { |
143 | 10 | PUTS("*RECURSION*\n"); |
144 | 10 | return; |
145 | 10 | } |
146 | 36.0k | GC_ADDREF(myht); |
147 | 36.0k | GC_PROTECT_RECURSION(myht); |
148 | 36.0k | } |
149 | 36.3k | count = zend_hash_num_elements(myht); |
150 | 36.3k | php_printf("%sarray(%d) {\n", COMMON, count); |
151 | 2.27M | ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) { |
152 | 2.27M | php_array_element_dump(val, num, key, level); |
153 | 2.27M | } ZEND_HASH_FOREACH_END(); |
154 | 36.3k | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
155 | 36.0k | GC_UNPROTECT_RECURSION(myht); |
156 | 36.0k | GC_DTOR_NO_REF(myht); |
157 | 36.0k | } |
158 | 36.3k | if (level > 1) { |
159 | 2.43k | php_printf("%*c", level-1, ' '); |
160 | 2.43k | } |
161 | 36.3k | PUTS("}\n"); |
162 | 36.3k | break; |
163 | 2.12k | case IS_OBJECT: { |
164 | 2.12k | zend_class_entry *ce = Z_OBJCE_P(struc); |
165 | 2.12k | if (ce->ce_flags & ZEND_ACC_ENUM) { |
166 | 29 | zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc)); |
167 | 29 | php_printf("%senum(%s::%s)\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval)); |
168 | 29 | return; |
169 | 29 | } |
170 | 2.09k | zend_object *zobj = Z_OBJ_P(struc); |
171 | 2.09k | uint32_t *guard = zend_get_recursion_guard(zobj); |
172 | 2.09k | if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) { |
173 | 15 | PUTS("*RECURSION*\n"); |
174 | 15 | return; |
175 | 15 | } |
176 | 2.07k | ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj); |
177 | | |
178 | 2.07k | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); |
179 | 2.07k | class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); |
180 | 2.07k | const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc)); |
181 | | |
182 | 2.07k | 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 | 2.07k | zend_string_release_ex(class_name, 0); |
184 | | |
185 | 2.07k | if (myht) { |
186 | 2.06k | zend_ulong num; |
187 | 2.06k | zend_string *key; |
188 | 2.06k | zval *val; |
189 | | |
190 | 4.20M | ZEND_HASH_FOREACH_KEY_VAL(myht, num, key, val) { |
191 | 4.20M | zend_property_info *prop_info = NULL; |
192 | | |
193 | 4.20M | if (Z_TYPE_P(val) == IS_INDIRECT) { |
194 | 1.41k | val = Z_INDIRECT_P(val); |
195 | 1.41k | if (key) { |
196 | 1.41k | prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
197 | 1.41k | } |
198 | 1.41k | } |
199 | | |
200 | 4.20M | if (!Z_ISUNDEF_P(val) || prop_info) { |
201 | 2.10M | php_object_property_dump(prop_info, val, num, key, level); |
202 | 2.10M | } |
203 | 4.20M | } ZEND_HASH_FOREACH_END(); |
204 | 2.06k | zend_release_properties(myht); |
205 | 2.06k | } |
206 | 2.07k | if (level > 1) { |
207 | 812 | php_printf("%*c", level-1, ' '); |
208 | 812 | } |
209 | 2.07k | PUTS("}\n"); |
210 | 2.07k | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj); |
211 | 2.07k | break; |
212 | 2.09k | } |
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 | 2.09k | } |
218 | 294 | case IS_REFERENCE: |
219 | | //??? hide references with refcount==1 (for compatibility) |
220 | 294 | if (Z_REFCOUNT_P(struc) > 1) { |
221 | 220 | is_ref = 1; |
222 | 220 | } |
223 | 294 | struc = Z_REFVAL_P(struc); |
224 | 294 | goto again; |
225 | 0 | break; |
226 | 1.02k | default: |
227 | 1.02k | php_printf("%sUNKNOWN:0\n", COMMON); |
228 | 1.02k | break; |
229 | 3.30M | } |
230 | 3.30M | } |
231 | | /* }}} */ |
232 | | |
233 | | /* {{{ Dumps a string representation of variable to output */ |
234 | | PHP_FUNCTION(var_dump) |
235 | 75.2k | { |
236 | 75.2k | zval *args; |
237 | 75.2k | int argc; |
238 | 75.2k | int i; |
239 | | |
240 | 225k | ZEND_PARSE_PARAMETERS_START(1, -1) |
241 | 225k | Z_PARAM_VARIADIC('+', args, argc) |
242 | 225k | ZEND_PARSE_PARAMETERS_END(); |
243 | | |
244 | 151k | for (i = 0; i < argc; i++) { |
245 | 76.0k | php_var_dump(&args[i], 1); |
246 | 76.0k | } |
247 | 75.2k | } |
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 | 4 | { |
265 | 4 | const char *prop_name, *class_name; |
266 | | |
267 | 4 | if (key == NULL) { /* numeric key */ |
268 | 0 | php_printf("%*c[" ZEND_LONG_FMT "]=>\n", level + 1, ' ', index); |
269 | 4 | } else { /* string key */ |
270 | 4 | zend_unmangle_property_name(key, &class_name, &prop_name); |
271 | 4 | php_printf("%*c[", level + 1, ' '); |
272 | | |
273 | 4 | if (class_name) { |
274 | 0 | if (class_name[0] == '*') { |
275 | 0 | php_printf("\"%s\":protected", prop_name); |
276 | 0 | } else { |
277 | 0 | php_printf("\"%s\":\"%s\":private", prop_name, class_name); |
278 | 0 | } |
279 | 4 | } else { |
280 | 4 | php_printf("\"%s\"", prop_name); |
281 | 4 | } |
282 | 4 | ZEND_PUTS("]=>\n"); |
283 | 4 | } |
284 | 4 | if (prop_info && Z_TYPE_P(zv) == IS_UNDEF) { |
285 | 0 | zend_string *type_str = zend_type_to_string(prop_info->type); |
286 | 0 | php_printf("%*cuninitialized(%s)\n", |
287 | 0 | level + 1, ' ', ZSTR_VAL(type_str)); |
288 | 0 | zend_string_release(type_str); |
289 | 4 | } else { |
290 | 4 | php_debug_zval_dump(zv, level + 2); |
291 | 4 | } |
292 | 4 | } |
293 | | /* }}} */ |
294 | | |
295 | | PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */ |
296 | 11 | { |
297 | 11 | HashTable *myht = NULL; |
298 | 11 | zend_string *class_name; |
299 | 11 | zend_ulong index; |
300 | 11 | zend_string *key; |
301 | 11 | zval *val; |
302 | 11 | uint32_t count; |
303 | 11 | char *packed; |
304 | | |
305 | 11 | if (level > 1) { |
306 | 4 | php_printf("%*c", level - 1, ' '); |
307 | 4 | } |
308 | | |
309 | 11 | 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 | 0 | case IS_NULL: |
317 | 0 | PUTS("NULL\n"); |
318 | 0 | break; |
319 | 0 | case IS_LONG: |
320 | 0 | php_printf("int(" ZEND_LONG_FMT ")\n", Z_LVAL_P(struc)); |
321 | 0 | 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 | 4 | case IS_STRING: |
326 | 4 | php_printf("string(%zd) \"", Z_STRLEN_P(struc)); |
327 | 4 | PHPWRITE(Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
328 | 4 | if (Z_REFCOUNTED_P(struc)) { |
329 | 4 | php_printf("\" refcount(%u)\n", Z_REFCOUNT_P(struc)); |
330 | 4 | } else { |
331 | 0 | PUTS("\" interned\n"); |
332 | 0 | } |
333 | 4 | 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 | 7 | 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 | 7 | zend_object *zobj = Z_OBJ_P(struc); |
370 | 7 | uint32_t *guard = zend_get_recursion_guard(zobj); |
371 | 7 | if (ZEND_GUARD_OR_GC_IS_RECURSIVE(guard, DEBUG, zobj)) { |
372 | 0 | PUTS("*RECURSION*\n"); |
373 | 0 | return; |
374 | 0 | } |
375 | 7 | ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, DEBUG, zobj); |
376 | | |
377 | 7 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_DEBUG); |
378 | 7 | class_name = Z_OBJ_HANDLER_P(struc, get_class_name)(Z_OBJ_P(struc)); |
379 | 7 | const char *prefix = php_var_dump_object_prefix(Z_OBJ_P(struc)); |
380 | | |
381 | 7 | 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 | 7 | zend_string_release_ex(class_name, 0); |
383 | 7 | if (myht) { |
384 | 15 | ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) { |
385 | 15 | zend_property_info *prop_info = NULL; |
386 | | |
387 | 15 | if (Z_TYPE_P(val) == IS_INDIRECT) { |
388 | 4 | val = Z_INDIRECT_P(val); |
389 | 4 | if (key) { |
390 | 4 | prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
391 | 4 | } |
392 | 4 | } |
393 | | |
394 | 15 | if (!Z_ISUNDEF_P(val) || prop_info) { |
395 | 4 | zval_object_property_dump(prop_info, val, index, key, level); |
396 | 4 | } |
397 | 15 | } ZEND_HASH_FOREACH_END(); |
398 | 7 | zend_release_properties(myht); |
399 | 7 | } |
400 | 7 | if (level > 1) { |
401 | 0 | php_printf("%*c", level - 1, ' '); |
402 | 0 | } |
403 | 7 | PUTS("}\n"); |
404 | 7 | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, DEBUG, zobj); |
405 | 7 | break; |
406 | 7 | } |
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 | 7 | } |
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 | 11 | } |
424 | 11 | } |
425 | | /* }}} */ |
426 | | |
427 | | /* {{{ Dumps a string representation of an internal zend value to output. */ |
428 | | PHP_FUNCTION(debug_zval_dump) |
429 | 6 | { |
430 | 6 | zval *args; |
431 | 6 | int argc; |
432 | 6 | int i; |
433 | | |
434 | 18 | ZEND_PARSE_PARAMETERS_START(1, -1) |
435 | 18 | Z_PARAM_VARIADIC('+', args, argc) |
436 | 18 | ZEND_PARSE_PARAMETERS_END(); |
437 | | |
438 | 13 | for (i = 0; i < argc; i++) { |
439 | 7 | php_debug_zval_dump(&args[i], 1); |
440 | 7 | } |
441 | 6 | } |
442 | | /* }}} */ |
443 | | |
444 | | #define buffer_append_spaces(buf, num_spaces) \ |
445 | 199 | do { \ |
446 | 199 | char *tmp_spaces; \ |
447 | 199 | size_t tmp_spaces_len; \ |
448 | 199 | tmp_spaces_len = spprintf(&tmp_spaces, 0,"%*c", num_spaces, ' '); \ |
449 | 199 | smart_str_appendl(buf, tmp_spaces, tmp_spaces_len); \ |
450 | 199 | efree(tmp_spaces); \ |
451 | 199 | } 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 | 2 | { |
455 | 2 | if (key == NULL) { /* numeric key */ |
456 | 2 | buffer_append_spaces(buf, level+1); |
457 | 2 | smart_str_append_long(buf, (zend_long) index); |
458 | 2 | smart_str_appendl(buf, " => ", 4); |
459 | | |
460 | 2 | } else { /* string key */ |
461 | 0 | zend_string *tmp_str; |
462 | 0 | zend_string *ckey = php_addcslashes(key, "'\\", 2); |
463 | 0 | tmp_str = php_str_to_str(ZSTR_VAL(ckey), ZSTR_LEN(ckey), "\0", 1, "' . \"\\0\" . '", 12); |
464 | |
|
465 | 0 | buffer_append_spaces(buf, level + 1); |
466 | |
|
467 | 0 | smart_str_appendc(buf, '\''); |
468 | 0 | smart_str_append(buf, tmp_str); |
469 | 0 | smart_str_appendl(buf, "' => ", 5); |
470 | |
|
471 | 0 | zend_string_free(ckey); |
472 | 0 | zend_string_free(tmp_str); |
473 | 0 | } |
474 | 2 | zend_result result = php_var_export_ex(zv, level + 2, buf); |
475 | | |
476 | 2 | smart_str_appendc(buf, ','); |
477 | 2 | smart_str_appendc(buf, '\n'); |
478 | | |
479 | 2 | return result; |
480 | 2 | } |
481 | | /* }}} */ |
482 | | |
483 | | static zend_result php_object_element_export(zval *zv, zend_ulong index, zend_string *key, int level, smart_str *buf) /* {{{ */ |
484 | 193 | { |
485 | 193 | buffer_append_spaces(buf, level + 2); |
486 | 193 | if (key != NULL) { |
487 | 193 | const char *class_name, *prop_name; |
488 | 193 | size_t prop_name_len; |
489 | 193 | zend_string *pname_esc; |
490 | | |
491 | 193 | zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len); |
492 | 193 | pname_esc = php_addcslashes_str(prop_name, prop_name_len, "'\\", 2); |
493 | | |
494 | 193 | smart_str_appendc(buf, '\''); |
495 | 193 | smart_str_append(buf, pname_esc); |
496 | 193 | smart_str_appendc(buf, '\''); |
497 | 193 | zend_string_release_ex(pname_esc, 0); |
498 | 193 | } else { |
499 | 0 | smart_str_append_long(buf, (zend_long) index); |
500 | 0 | } |
501 | 193 | smart_str_appendl(buf, " => ", 4); |
502 | 193 | zend_result result = php_var_export_ex(zv, level + 2, buf); |
503 | 193 | smart_str_appendc(buf, ','); |
504 | 193 | smart_str_appendc(buf, '\n'); |
505 | | |
506 | 193 | return result; |
507 | 193 | } |
508 | | /* }}} */ |
509 | | |
510 | | PHPAPI zend_result php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */ |
511 | 233 | { |
512 | 233 | HashTable *myht; |
513 | 233 | zend_string *ztmp, *ztmp2; |
514 | 233 | zend_ulong index; |
515 | 233 | zend_string *key; |
516 | 233 | zval *val; |
517 | | |
518 | 233 | again: |
519 | 233 | switch (Z_TYPE_P(struc)) { |
520 | 0 | case IS_FALSE: |
521 | 0 | smart_str_appendl(buf, "false", 5); |
522 | 0 | break; |
523 | 0 | case IS_TRUE: |
524 | 0 | smart_str_appendl(buf, "true", 4); |
525 | 0 | break; |
526 | 1 | case IS_NULL: |
527 | 1 | smart_str_appendl(buf, "NULL", 4); |
528 | 1 | break; |
529 | 2 | 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 | 2 | 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 | 2 | smart_str_append_long(buf, Z_LVAL_P(struc)); |
538 | 2 | 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 | 189 | case IS_STRING: |
544 | 189 | ztmp = php_addcslashes(Z_STR_P(struc), "'\\", 2); |
545 | 189 | ztmp2 = php_str_to_str(ZSTR_VAL(ztmp), ZSTR_LEN(ztmp), "\0", 1, "' . \"\\0\" . '", 12); |
546 | | |
547 | 189 | smart_str_appendc(buf, '\''); |
548 | 189 | smart_str_append(buf, ztmp2); |
549 | 189 | smart_str_appendc(buf, '\''); |
550 | | |
551 | 189 | zend_string_free(ztmp); |
552 | 189 | zend_string_free(ztmp2); |
553 | 189 | break; |
554 | 3 | case IS_ARRAY: |
555 | 3 | myht = Z_ARRVAL_P(struc); |
556 | 3 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
557 | 3 | 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 | 3 | GC_ADDREF(myht); |
563 | 3 | GC_PROTECT_RECURSION(myht); |
564 | 3 | } |
565 | 3 | if (level > 1) { |
566 | 1 | smart_str_appendc(buf, '\n'); |
567 | 1 | buffer_append_spaces(buf, level - 1); |
568 | 1 | } |
569 | 3 | smart_str_appendl(buf, "array (\n", 8); |
570 | 7 | ZEND_HASH_FOREACH_KEY_VAL(myht, index, key, val) { |
571 | 7 | 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 | 7 | } ZEND_HASH_FOREACH_END(); |
579 | 3 | if (!(GC_FLAGS(myht) & GC_IMMUTABLE)) { |
580 | 3 | GC_UNPROTECT_RECURSION(myht); |
581 | 3 | GC_DELREF(myht); |
582 | 3 | } |
583 | 3 | if (level > 1) { |
584 | 1 | buffer_append_spaces(buf, level - 1); |
585 | 1 | } |
586 | 3 | smart_str_appendc(buf, ')'); |
587 | | |
588 | 3 | break; |
589 | | |
590 | 38 | 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 | 38 | zend_object *zobj = Z_OBJ_P(struc); |
596 | 38 | uint32_t *guard = zend_get_recursion_guard(zobj); |
597 | 38 | 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 | 38 | ZEND_GUARD_OR_GC_PROTECT_RECURSION(guard, EXPORT, zobj); |
603 | 38 | myht = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_VAR_EXPORT); |
604 | 38 | if (level > 1) { |
605 | 2 | smart_str_appendc(buf, '\n'); |
606 | 2 | buffer_append_spaces(buf, level - 1); |
607 | 2 | } |
608 | | |
609 | 38 | zend_class_entry *ce = Z_OBJCE_P(struc); |
610 | 38 | bool is_enum = ce->ce_flags & ZEND_ACC_ENUM; |
611 | | |
612 | | /* stdClass has no __set_state method, but can be casted to */ |
613 | 38 | if (ce == zend_standard_class_def) { |
614 | 0 | smart_str_appendl(buf, "(object) array(\n", 16); |
615 | 38 | } else { |
616 | 38 | smart_str_appendc(buf, '\\'); |
617 | 38 | smart_str_append(buf, ce->name); |
618 | 38 | if (is_enum) { |
619 | 4 | zend_object *zobj = Z_OBJ_P(struc); |
620 | 4 | zval *case_name_zval = zend_enum_fetch_case_name(zobj); |
621 | 4 | smart_str_appendl(buf, "::", 2); |
622 | 4 | smart_str_append(buf, Z_STR_P(case_name_zval)); |
623 | 34 | } else { |
624 | 34 | smart_str_appendl(buf, "::__set_state(array(\n", 21); |
625 | 34 | } |
626 | 38 | } |
627 | | |
628 | 38 | if (myht) { |
629 | 38 | if (!is_enum) { |
630 | 486 | ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, val) { |
631 | | /* data is IS_PTR for properties with hooks. */ |
632 | 486 | zval tmp; |
633 | 486 | if (UNEXPECTED(Z_TYPE_P(val) == IS_PTR)) { |
634 | 207 | zend_property_info *prop_info = Z_PTR_P(val); |
635 | 207 | if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) { |
636 | 33 | continue; |
637 | 33 | } |
638 | 174 | const char *unmangled_name_cstr = zend_get_unmangled_property_name(prop_info->name); |
639 | 174 | zend_string *unmangled_name = zend_string_init(unmangled_name_cstr, strlen(unmangled_name_cstr), false); |
640 | 174 | val = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp); |
641 | 174 | zend_string_release_ex(unmangled_name, false); |
642 | 174 | 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 | 174 | } |
648 | 193 | php_object_element_export(val, index, key, level, buf); |
649 | 193 | if (val == &tmp) { |
650 | 174 | zval_ptr_dtor(val); |
651 | 174 | } |
652 | 193 | } ZEND_HASH_FOREACH_END(); |
653 | 34 | } |
654 | 38 | zend_release_properties(myht); |
655 | 38 | } |
656 | 38 | ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj); |
657 | 38 | if (level > 1 && !is_enum) { |
658 | 0 | buffer_append_spaces(buf, level - 1); |
659 | 0 | } |
660 | 38 | if (ce == zend_standard_class_def) { |
661 | 0 | smart_str_appendc(buf, ')'); |
662 | 38 | } else if (!is_enum) { |
663 | 34 | smart_str_appendl(buf, "))", 2); |
664 | 34 | } |
665 | | |
666 | 38 | break; |
667 | 38 | } |
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 | 233 | } |
676 | | |
677 | 233 | return SUCCESS; |
678 | 233 | } |
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 | 38 | { |
697 | 38 | zval *var; |
698 | 38 | bool return_output = 0; |
699 | 38 | smart_str buf = {0}; |
700 | | |
701 | 114 | ZEND_PARSE_PARAMETERS_START(1, 2) |
702 | 152 | Z_PARAM_ZVAL(var) |
703 | 152 | Z_PARAM_OPTIONAL |
704 | 152 | Z_PARAM_BOOL(return_output) |
705 | 38 | ZEND_PARSE_PARAMETERS_END(); |
706 | | |
707 | 38 | zend_result result = php_var_export_ex(var, 1, &buf); |
708 | 38 | smart_str_0 (&buf); |
709 | | |
710 | 38 | if (result == FAILURE) { |
711 | 0 | smart_str_free(&buf); |
712 | 38 | } else if (return_output) { |
713 | 2 | RETURN_STR(smart_str_extract(&buf)); |
714 | 36 | } else { |
715 | 36 | PHPWRITE(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); |
716 | 36 | smart_str_free(&buf); |
717 | 36 | } |
718 | 38 | } |
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 | 2.35k | { |
728 | 2.35k | zval *zv; |
729 | 2.35k | zend_ulong key; |
730 | 2.35k | bool is_ref = Z_ISREF_P(var); |
731 | | |
732 | 2.35k | data->n += 1; |
733 | | |
734 | 2.35k | if (is_ref) { |
735 | | /* pass */ |
736 | 2.04k | } else if (Z_TYPE_P(var) != IS_OBJECT) { |
737 | 2.03k | return 0; |
738 | 2.03k | } else if (!in_rcn_array |
739 | 8 | && Z_REFCOUNT_P(var) == 1 |
740 | 0 | && (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1) |
741 | | /* __serialize and __sleep may arbitrarily increase the refcount */ |
742 | 0 | && Z_OBJCE_P(var)->__serialize == NULL |
743 | 0 | && zend_hash_find_known_hash(&Z_OBJCE_P(var)->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP)) == NULL) { |
744 | 0 | return 0; |
745 | 0 | } |
746 | | |
747 | | /* References to objects are treated as if the reference didn't exist */ |
748 | 319 | 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 | 319 | key = (zend_ulong) (uintptr_t) Z_COUNTED_P(var); |
755 | 319 | zv = zend_hash_index_find(&data->ht, key); |
756 | | |
757 | 319 | if (zv) { |
758 | | /* References are only counted once, undo the data->n increment above */ |
759 | 223 | if (is_ref && Z_LVAL_P(zv) != -1) { |
760 | 221 | data->n -= 1; |
761 | 221 | } |
762 | | |
763 | 223 | return Z_LVAL_P(zv); |
764 | 223 | } else { |
765 | 96 | zval zv_n; |
766 | 96 | ZVAL_LONG(&zv_n, data->n); |
767 | 96 | 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 | 96 | zend_hash_index_add_new(&data->ht, key + 1, var); |
774 | 96 | Z_ADDREF_P(var); |
775 | | |
776 | 96 | return 0; |
777 | 96 | } |
778 | 319 | } |
779 | | /* }}} */ |
780 | | |
781 | | static inline void php_var_serialize_long(smart_str *buf, zend_long val) /* {{{ */ |
782 | 877 | { |
783 | 877 | char b[32]; |
784 | 877 | char *s = zend_print_long_to_buf(b + sizeof(b) - 1, val); |
785 | 877 | size_t l = b + sizeof(b) - 1 - s; |
786 | 877 | char *res = smart_str_extend(buf, 2 + l + 1); |
787 | 877 | res = zend_mempcpy(res, "i:", 2); |
788 | 877 | memcpy(res, s, l); |
789 | 877 | res[l] = ';'; |
790 | 877 | } |
791 | | /* }}} */ |
792 | | |
793 | | static inline void php_var_serialize_string(smart_str *buf, char *str, size_t len) /* {{{ */ |
794 | 1.45k | { |
795 | 1.45k | char b[32]; |
796 | 1.45k | char *s = zend_print_long_to_buf(b + sizeof(b) - 1, len); |
797 | 1.45k | size_t l = b + sizeof(b) - 1 - s; |
798 | 1.45k | char *res = smart_str_extend(buf, 2 + l + 2 + len + 2); |
799 | 1.45k | res = zend_mempcpy(res, "s:", 2); |
800 | 1.45k | res = zend_mempcpy(res, s, l); |
801 | 1.45k | res = zend_mempcpy(res, ":\"", 2); |
802 | 1.45k | res = zend_mempcpy(res, str, len); |
803 | 1.45k | memcpy(res, "\";", 2); |
804 | 1.45k | } |
805 | | /* }}} */ |
806 | | |
807 | | static inline bool php_var_serialize_class_name(smart_str *buf, zval *struc) /* {{{ */ |
808 | 5 | { |
809 | 5 | char b[32]; |
810 | 5 | PHP_CLASS_ATTRIBUTES; |
811 | | |
812 | 5 | PHP_SET_CLASS_ATTRIBUTES(struc); |
813 | 5 | size_t class_name_len = ZSTR_LEN(class_name); |
814 | 5 | char *s = zend_print_long_to_buf(b + sizeof(b) - 1, class_name_len); |
815 | 5 | size_t l = b + sizeof(b) - 1 - s; |
816 | 5 | char *res = smart_str_extend(buf, 2 + l + 2 + class_name_len + 2); |
817 | 5 | res = zend_mempcpy(res, "O:", 2); |
818 | 5 | res = zend_mempcpy(res, s, l); |
819 | 5 | res = zend_mempcpy(res, ":\"", 2); |
820 | 5 | res = zend_mempcpy(res, ZSTR_VAL(class_name), class_name_len); |
821 | 5 | memcpy(res, "\":", 2); |
822 | 5 | PHP_CLEANUP_CLASS_ATTRIBUTES(); |
823 | 5 | return incomplete_class; |
824 | 5 | } |
825 | | /* }}} */ |
826 | | |
827 | | static HashTable* php_var_serialize_call_sleep(zend_object *obj, zend_function *fn) /* {{{ */ |
828 | 0 | { |
829 | 0 | zval retval; |
830 | |
|
831 | 0 | BG(serialize_lock)++; |
832 | 0 | zend_call_known_instance_method(fn, obj, &retval, /* param_count */ 0, /* params */ NULL); |
833 | 0 | BG(serialize_lock)--; |
834 | |
|
835 | 0 | if (Z_ISUNDEF(retval) || EG(exception)) { |
836 | 0 | zval_ptr_dtor(&retval); |
837 | 0 | return NULL; |
838 | 0 | } |
839 | | |
840 | 0 | 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 | 0 | return Z_ARRVAL(retval); |
847 | 0 | } |
848 | | /* }}} */ |
849 | | |
850 | | static int php_var_serialize_call_magic_serialize(zval *retval, zval *obj) /* {{{ */ |
851 | 0 | { |
852 | 0 | BG(serialize_lock)++; |
853 | 0 | zend_call_known_instance_method_with_0_params( |
854 | 0 | Z_OBJCE_P(obj)->__serialize, Z_OBJ_P(obj), retval); |
855 | 0 | BG(serialize_lock)--; |
856 | |
|
857 | 0 | if (EG(exception)) { |
858 | 0 | zval_ptr_dtor(retval); |
859 | 0 | return FAILURE; |
860 | 0 | } |
861 | | |
862 | 0 | 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 | 0 | return SUCCESS; |
869 | 0 | } |
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 | 0 | { |
875 | 0 | zval *val = zend_hash_find(props, name); |
876 | 0 | if (val == NULL) { |
877 | 0 | return FAILURE; |
878 | 0 | } |
879 | | |
880 | 0 | if (Z_TYPE_P(val) == IS_INDIRECT) { |
881 | 0 | val = Z_INDIRECT_P(val); |
882 | 0 | if (Z_TYPE_P(val) == IS_UNDEF) { |
883 | 0 | zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val); |
884 | 0 | if (info) { |
885 | 0 | return SUCCESS; |
886 | 0 | } |
887 | 0 | return FAILURE; |
888 | 0 | } |
889 | 0 | } |
890 | | |
891 | 0 | 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 | 0 | Z_TRY_ADDREF_P(val); |
898 | 0 | return SUCCESS; |
899 | 0 | } |
900 | | /* }}} */ |
901 | | |
902 | | static int php_var_serialize_get_sleep_props( |
903 | | HashTable *ht, zval *struc, HashTable *sleep_retval) /* {{{ */ |
904 | 0 | { |
905 | 0 | zend_class_entry *ce = Z_OBJCE_P(struc); |
906 | 0 | HashTable *props = zend_get_properties_for(struc, ZEND_PROP_PURPOSE_SERIALIZE); |
907 | 0 | zval *name_val; |
908 | 0 | int retval = SUCCESS; |
909 | |
|
910 | 0 | 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 | 0 | ZEND_HASH_FOREACH_VAL_IND(sleep_retval, name_val) { |
914 | 0 | zend_string *name, *tmp_name, *priv_name, *prot_name; |
915 | |
|
916 | 0 | ZVAL_DEREF(name_val); |
917 | 0 | 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 | 0 | name = zval_get_tmp_string(name_val, &tmp_name); |
924 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, name, name, struc) == SUCCESS) { |
925 | 0 | zend_tmp_string_release(tmp_name); |
926 | 0 | continue; |
927 | 0 | } |
928 | | |
929 | 0 | if (EG(exception)) { |
930 | 0 | zend_tmp_string_release(tmp_name); |
931 | 0 | retval = FAILURE; |
932 | 0 | break; |
933 | 0 | } |
934 | | |
935 | 0 | priv_name = zend_mangle_property_name( |
936 | 0 | ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), |
937 | 0 | ZSTR_VAL(name), ZSTR_LEN(name), ce->type & ZEND_INTERNAL_CLASS); |
938 | 0 | if (php_var_serialize_try_add_sleep_prop(ht, props, priv_name, name, struc) == SUCCESS) { |
939 | 0 | zend_tmp_string_release(tmp_name); |
940 | 0 | zend_string_release(priv_name); |
941 | 0 | continue; |
942 | 0 | } |
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 | 0 | zend_release_properties(props); |
972 | 0 | return retval; |
973 | 0 | } |
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 | 1.79k | { |
978 | 1.79k | smart_str_append_unsigned(buf, count); |
979 | 1.79k | smart_str_appendl(buf, ":{", 2); |
980 | 1.79k | if (count > 0) { |
981 | 488 | zend_string *key; |
982 | 488 | zval *data; |
983 | 488 | zend_ulong index; |
984 | | |
985 | 4.79k | ZEND_HASH_FOREACH_KEY_VAL_IND(ht, index, key, data) { |
986 | 4.79k | if (incomplete_class && zend_string_equals_literal(key, MAGIC_MEMBER)) { |
987 | 0 | incomplete_class = false; |
988 | 0 | continue; |
989 | 0 | } |
990 | | |
991 | 2.14k | if (!key) { |
992 | 819 | php_var_serialize_long(buf, index); |
993 | 1.32k | } else { |
994 | 1.32k | php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key)); |
995 | 1.32k | } |
996 | | |
997 | 2.14k | if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) { |
998 | 9 | data = Z_REFVAL_P(data); |
999 | 9 | } |
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 | 2.14k | if (Z_TYPE_P(data) == IS_ARRAY) { |
1004 | 1.56k | 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 | 1.56k | } else { |
1008 | 1.56k | php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false); |
1009 | 1.56k | } |
1010 | 1.56k | } else { |
1011 | 582 | php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false); |
1012 | 582 | } |
1013 | 2.14k | } ZEND_HASH_FOREACH_END(); |
1014 | 488 | } |
1015 | 1.79k | smart_str_appendc(buf, '}'); |
1016 | 1.79k | } |
1017 | | /* }}} */ |
1018 | | |
1019 | | static void php_var_serialize_class(smart_str *buf, zval *struc, HashTable *ht, php_serialize_data_t var_hash) /* {{{ */ |
1020 | 0 | { |
1021 | 0 | HashTable props; |
1022 | |
|
1023 | 0 | if (php_var_serialize_get_sleep_props(&props, struc, ht) == SUCCESS) { |
1024 | 0 | php_var_serialize_class_name(buf, struc); |
1025 | 0 | php_var_serialize_nested_data( |
1026 | 0 | buf, struc, &props, zend_hash_num_elements(&props), /* incomplete_class */ false, var_hash, |
1027 | 0 | GC_REFCOUNT(&props) > 1); |
1028 | 0 | } |
1029 | 0 | zend_hash_destroy(&props); |
1030 | 0 | } |
1031 | | /* }}} */ |
1032 | | |
1033 | | static zend_always_inline bool php_serialize_check_stack_limit(void) |
1034 | 2.35k | { |
1035 | 2.35k | #ifdef ZEND_CHECK_STACK_LIMIT |
1036 | 2.35k | if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { |
1037 | 0 | zend_call_stack_size_error(); |
1038 | 0 | return true; |
1039 | 0 | } |
1040 | 2.35k | #endif |
1041 | 2.35k | return false; |
1042 | 2.35k | } |
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 | 2.35k | { |
1046 | 2.35k | zend_long var_already; |
1047 | 2.35k | HashTable *myht; |
1048 | | |
1049 | 2.35k | if (EG(exception)) { |
1050 | 0 | return; |
1051 | 0 | } |
1052 | | |
1053 | 2.35k | if (UNEXPECTED(php_serialize_check_stack_limit())) { |
1054 | 0 | return; |
1055 | 0 | } |
1056 | | |
1057 | 2.35k | if (var_hash && (var_already = php_add_var_hash(var_hash, struc, in_rcn_array))) { |
1058 | 223 | 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 | 223 | } else if (Z_ISREF_P(struc)) { |
1063 | 221 | smart_str_appendl(buf, "R:", 2); |
1064 | 221 | smart_str_append_long(buf, var_already); |
1065 | 221 | smart_str_appendc(buf, ';'); |
1066 | 221 | return; |
1067 | 221 | } else if (Z_TYPE_P(struc) == IS_OBJECT) { |
1068 | 2 | smart_str_appendl(buf, "r:", 2); |
1069 | 2 | smart_str_append_long(buf, var_already); |
1070 | 2 | smart_str_appendc(buf, ';'); |
1071 | 2 | return; |
1072 | 2 | } |
1073 | 223 | } |
1074 | | |
1075 | 2.21k | again: |
1076 | 2.21k | 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 | 157 | case IS_NULL: |
1086 | 157 | smart_str_appendl(buf, "N;", 2); |
1087 | 157 | return; |
1088 | | |
1089 | 58 | case IS_LONG: |
1090 | 58 | php_var_serialize_long(buf, Z_LVAL_P(struc)); |
1091 | 58 | 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 | 118 | case IS_STRING: |
1106 | 118 | php_var_serialize_string(buf, Z_STRVAL_P(struc), Z_STRLEN_P(struc)); |
1107 | 118 | return; |
1108 | | |
1109 | 10 | case IS_OBJECT: { |
1110 | 10 | zend_class_entry *ce = Z_OBJCE_P(struc); |
1111 | 10 | bool incomplete_class; |
1112 | 10 | uint32_t count; |
1113 | | |
1114 | 10 | if (ce->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) { |
1115 | 3 | zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed", |
1116 | 3 | ZSTR_VAL(ce->name)); |
1117 | 3 | return; |
1118 | 3 | } |
1119 | | |
1120 | 7 | if (ce->ce_flags & ZEND_ACC_ENUM) { |
1121 | 2 | PHP_CLASS_ATTRIBUTES; |
1122 | | |
1123 | 2 | zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc)); |
1124 | | |
1125 | 2 | PHP_SET_CLASS_ATTRIBUTES(struc); |
1126 | 2 | smart_str_appendl(buf, "E:", 2); |
1127 | 2 | smart_str_append_unsigned(buf, ZSTR_LEN(class_name) + strlen(":") + Z_STRLEN_P(case_name_zval)); |
1128 | 2 | smart_str_appendl(buf, ":\"", 2); |
1129 | 2 | smart_str_append(buf, class_name); |
1130 | 2 | smart_str_appendc(buf, ':'); |
1131 | 2 | smart_str_append(buf, Z_STR_P(case_name_zval)); |
1132 | 2 | smart_str_appendl(buf, "\";", 2); |
1133 | 2 | PHP_CLEANUP_CLASS_ATTRIBUTES(); |
1134 | 2 | return; |
1135 | 2 | } |
1136 | | |
1137 | 5 | if (ce->__serialize) { |
1138 | 0 | zval retval, obj; |
1139 | 0 | zend_string *key; |
1140 | 0 | zval *data; |
1141 | 0 | zend_ulong index; |
1142 | |
|
1143 | 0 | ZVAL_OBJ_COPY(&obj, Z_OBJ_P(struc)); |
1144 | 0 | 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 | 0 | php_var_serialize_class_name(buf, &obj); |
1153 | 0 | smart_str_append_unsigned(buf, zend_hash_num_elements(Z_ARRVAL(retval))); |
1154 | 0 | smart_str_appendl(buf, ":{", 2); |
1155 | 0 | ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(retval), index, key, data) { |
1156 | 0 | if (!key) { |
1157 | 0 | php_var_serialize_long(buf, index); |
1158 | 0 | } else { |
1159 | 0 | php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key)); |
1160 | 0 | } |
1161 | |
|
1162 | 0 | if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) { |
1163 | 0 | data = Z_REFVAL_P(data); |
1164 | 0 | } |
1165 | 0 | php_var_serialize_intern(buf, data, var_hash, Z_REFCOUNT(retval) > 1, false); |
1166 | 0 | } ZEND_HASH_FOREACH_END(); |
1167 | 0 | smart_str_appendc(buf, '}'); |
1168 | |
|
1169 | 0 | zval_ptr_dtor(&obj); |
1170 | 0 | zval_ptr_dtor(&retval); |
1171 | 0 | return; |
1172 | 0 | } |
1173 | | |
1174 | 5 | if (ce->serialize != NULL) { |
1175 | | /* has custom handler */ |
1176 | 0 | unsigned char *serialized_data = NULL; |
1177 | 0 | size_t serialized_length; |
1178 | |
|
1179 | 0 | if (ce->serialize(struc, &serialized_data, &serialized_length, (zend_serialize_data *)var_hash) == SUCCESS) { |
1180 | 0 | char b1[32], b2[32]; |
1181 | 0 | char *s1 = zend_print_long_to_buf(b1 + sizeof(b1) - 1, ZSTR_LEN(Z_OBJCE_P(struc)->name)); |
1182 | 0 | size_t l1 = b1 + sizeof(b1) - 1 - s1; |
1183 | 0 | char *s2 = zend_print_long_to_buf(b2 + sizeof(b2) - 1, serialized_length); |
1184 | 0 | size_t l2 = b2 + sizeof(b2) - 1 - s2; |
1185 | 0 | char *res = smart_str_extend(buf, 2 + l1 + 2 + ZSTR_LEN(Z_OBJCE_P(struc)->name) + 2 + l2 + 2 + serialized_length + 1); |
1186 | 0 | res = zend_mempcpy(res, "C:", 2); |
1187 | 0 | res = zend_mempcpy(res, s1, l1); |
1188 | 0 | res = zend_mempcpy(res, ":\"", 2); |
1189 | 0 | res = zend_mempcpy(res, ZSTR_VAL(Z_OBJCE_P(struc)->name), ZSTR_LEN(Z_OBJCE_P(struc)->name)); |
1190 | 0 | res = zend_mempcpy(res, "\":", 2); |
1191 | 0 | res = zend_mempcpy(res, s2, l2); |
1192 | 0 | res = zend_mempcpy(res, ":{", 2); |
1193 | 0 | memcpy(res, (char *) serialized_data, serialized_length); |
1194 | 0 | res[serialized_length] = '}'; |
1195 | 0 | } else { |
1196 | | /* Mark this value in the var_hash, to avoid creating references to it. */ |
1197 | 0 | zval *var_idx = zend_hash_index_find(&var_hash->ht, |
1198 | 0 | (zend_ulong) (uintptr_t) Z_COUNTED_P(struc)); |
1199 | 0 | if (var_idx) { |
1200 | 0 | ZVAL_LONG(var_idx, -1); |
1201 | 0 | } |
1202 | 0 | smart_str_appendl(buf, "N;", 2); |
1203 | 0 | } |
1204 | 0 | if (serialized_data) { |
1205 | 0 | efree(serialized_data); |
1206 | 0 | } |
1207 | 0 | return; |
1208 | 0 | } |
1209 | | |
1210 | 5 | if (ce != PHP_IC_ENTRY) { |
1211 | 5 | zval *zv = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP)); |
1212 | | |
1213 | 5 | if (zv) { |
1214 | 0 | HashTable *ht; |
1215 | 0 | zval tmp; |
1216 | |
|
1217 | 0 | ZVAL_OBJ_COPY(&tmp, Z_OBJ_P(struc)); |
1218 | 0 | if (!(ht = php_var_serialize_call_sleep(Z_OBJ(tmp), Z_FUNC_P(zv)))) { |
1219 | 0 | 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 | 0 | OBJ_RELEASE(Z_OBJ(tmp)); |
1225 | 0 | return; |
1226 | 0 | } |
1227 | | |
1228 | 0 | php_var_serialize_class(buf, &tmp, ht, var_hash); |
1229 | 0 | zend_array_release(ht); |
1230 | 0 | OBJ_RELEASE(Z_OBJ(tmp)); |
1231 | 0 | return; |
1232 | 0 | } |
1233 | 5 | } |
1234 | | |
1235 | 5 | incomplete_class = php_var_serialize_class_name(buf, struc); |
1236 | | |
1237 | 5 | if (Z_OBJ_P(struc)->properties == NULL |
1238 | 1 | && Z_OBJ_HT_P(struc)->get_properties_for == NULL |
1239 | 1 | && Z_OBJ_HT_P(struc)->get_properties == zend_std_get_properties |
1240 | 1 | && !zend_object_is_lazy(Z_OBJ_P(struc))) { |
1241 | | /* Optimized version without rebulding properties HashTable */ |
1242 | 1 | zend_object *obj = Z_OBJ_P(struc); |
1243 | 1 | zend_class_entry *ce = obj->ce; |
1244 | 1 | zend_property_info *prop_info; |
1245 | 1 | zval *prop; |
1246 | 1 | int i; |
1247 | | |
1248 | 1 | count = ce->default_properties_count; |
1249 | 8 | for (i = 0; i < ce->default_properties_count; i++) { |
1250 | 7 | prop_info = ce->properties_info_table[i]; |
1251 | 7 | if (!prop_info) { |
1252 | 0 | count--; |
1253 | 0 | continue; |
1254 | 0 | } |
1255 | 7 | prop = OBJ_PROP(obj, prop_info->offset); |
1256 | 7 | if (Z_TYPE_P(prop) == IS_UNDEF) { |
1257 | 0 | count--; |
1258 | 0 | continue; |
1259 | 0 | } |
1260 | 7 | } |
1261 | 1 | if (count) { |
1262 | 1 | smart_str_append_unsigned(buf, count); |
1263 | 1 | smart_str_appendl(buf, ":{", 2); |
1264 | 8 | for (i = 0; i < ce->default_properties_count; i++) { |
1265 | 7 | prop_info = ce->properties_info_table[i]; |
1266 | 7 | if (!prop_info) { |
1267 | 0 | continue; |
1268 | 0 | } |
1269 | 7 | prop = OBJ_PROP(obj, prop_info->offset); |
1270 | 7 | if (Z_TYPE_P(prop) == IS_UNDEF) { |
1271 | 0 | continue; |
1272 | 0 | } |
1273 | | |
1274 | 7 | php_var_serialize_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name)); |
1275 | | |
1276 | 7 | if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) { |
1277 | 0 | prop = Z_REFVAL_P(prop); |
1278 | 0 | } |
1279 | | |
1280 | 7 | php_var_serialize_intern(buf, prop, var_hash, false, false); |
1281 | 7 | } |
1282 | 1 | smart_str_appendc(buf, '}'); |
1283 | 1 | } else { |
1284 | 0 | smart_str_appendl(buf, "0:{}", 4); |
1285 | 0 | } |
1286 | 1 | return; |
1287 | 1 | } |
1288 | 4 | 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 | 4 | count = zend_array_count(myht); |
1292 | 4 | if (count > 0 && incomplete_class) { |
1293 | 0 | --count; |
1294 | 0 | } |
1295 | 4 | php_var_serialize_nested_data(buf, struc, myht, count, incomplete_class, var_hash, GC_REFCOUNT(myht) > 1); |
1296 | 4 | zend_release_properties(myht); |
1297 | 4 | return; |
1298 | 5 | } |
1299 | 1.78k | case IS_ARRAY: |
1300 | 1.78k | smart_str_appendl(buf, "a:", 2); |
1301 | 1.78k | myht = Z_ARRVAL_P(struc); |
1302 | 1.78k | php_var_serialize_nested_data( |
1303 | 1.78k | buf, struc, myht, zend_array_count(myht), /* incomplete_class */ false, var_hash, |
1304 | 1.78k | !is_root && (in_rcn_array || GC_REFCOUNT(myht) > 1)); |
1305 | 1.78k | return; |
1306 | 86 | case IS_REFERENCE: |
1307 | 86 | struc = Z_REFVAL_P(struc); |
1308 | 86 | goto again; |
1309 | 0 | default: |
1310 | 0 | smart_str_appendl(buf, "i:0;", 4); |
1311 | 0 | return; |
1312 | 2.21k | } |
1313 | 2.21k | } |
1314 | | /* }}} */ |
1315 | | |
1316 | | PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data) /* {{{ */ |
1317 | 201 | { |
1318 | 201 | php_var_serialize_intern(buf, struc, *data, false, true); |
1319 | 201 | smart_str_0(buf); |
1320 | 201 | } |
1321 | | /* }}} */ |
1322 | | |
1323 | 201 | PHPAPI php_serialize_data_t php_var_serialize_init(void) { |
1324 | 201 | struct php_serialize_data *d; |
1325 | | /* fprintf(stderr, "SERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */ |
1326 | 201 | if (BG(serialize_lock) || !BG(serialize).level) { |
1327 | 201 | d = emalloc(sizeof(struct php_serialize_data)); |
1328 | 201 | zend_hash_init(&d->ht, 16, NULL, ZVAL_PTR_DTOR, 0); |
1329 | 201 | d->n = 0; |
1330 | 201 | if (!BG(serialize_lock)) { |
1331 | 201 | BG(serialize).data = d; |
1332 | 201 | BG(serialize).level = 1; |
1333 | 201 | } |
1334 | 201 | } else { |
1335 | 0 | d = BG(serialize).data; |
1336 | 0 | ++BG(serialize).level; |
1337 | 0 | } |
1338 | 201 | return d; |
1339 | 201 | } |
1340 | | |
1341 | 201 | 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 | 201 | if (BG(serialize_lock) || BG(serialize).level == 1) { |
1344 | 201 | zend_hash_destroy(&d->ht); |
1345 | 201 | efree(d); |
1346 | 201 | } |
1347 | 201 | if (!BG(serialize_lock) && !--BG(serialize).level) { |
1348 | 201 | BG(serialize).data = NULL; |
1349 | 201 | } |
1350 | 201 | } |
1351 | | |
1352 | | /* {{{ Returns a string representation of variable (which can later be unserialized) */ |
1353 | | PHP_FUNCTION(serialize) |
1354 | 201 | { |
1355 | 201 | zval *struc; |
1356 | 201 | php_serialize_data_t var_hash; |
1357 | 201 | smart_str buf = {0}; |
1358 | | |
1359 | 603 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1360 | 804 | Z_PARAM_ZVAL(struc) |
1361 | 804 | ZEND_PARSE_PARAMETERS_END(); |
1362 | | |
1363 | 201 | PHP_VAR_SERIALIZE_INIT(var_hash); |
1364 | 201 | php_var_serialize(&buf, struc, &var_hash); |
1365 | 201 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
1366 | | |
1367 | 201 | if (EG(exception)) { |
1368 | 3 | smart_str_free(&buf); |
1369 | 3 | RETURN_THROWS(); |
1370 | 3 | } |
1371 | | |
1372 | 198 | RETURN_STR(smart_str_extract(&buf)); |
1373 | 198 | } |
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 | 234 | { |
1379 | 234 | const unsigned char *p; |
1380 | 234 | php_unserialize_data_t var_hash; |
1381 | 234 | zval *retval; |
1382 | 234 | HashTable *class_hash = NULL, *prev_class_hash; |
1383 | 234 | zend_long prev_max_depth, prev_cur_depth; |
1384 | | |
1385 | 234 | if (buf_len == 0) { |
1386 | 1 | RETURN_FALSE; |
1387 | 1 | } |
1388 | | |
1389 | 233 | p = (const unsigned char*) buf; |
1390 | 233 | PHP_VAR_UNSERIALIZE_INIT(var_hash); |
1391 | | |
1392 | 233 | prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash); |
1393 | 233 | prev_max_depth = php_var_unserialize_get_max_depth(var_hash); |
1394 | 233 | prev_cur_depth = php_var_unserialize_get_cur_depth(var_hash); |
1395 | 233 | 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 | 233 | if (BG(unserialize).level > 1) { |
1455 | 0 | retval = var_tmp_var(&var_hash); |
1456 | 233 | } else { |
1457 | 233 | retval = return_value; |
1458 | 233 | } |
1459 | 233 | if (!php_var_unserialize(retval, &p, p + buf_len, &var_hash)) { |
1460 | 41 | if (!EG(exception)) { |
1461 | 37 | php_error_docref(NULL, E_WARNING, "Error at offset " ZEND_LONG_FMT " of %zd bytes", |
1462 | 37 | (zend_long)((char*)p - buf), buf_len); |
1463 | 37 | } |
1464 | 41 | if (BG(unserialize).level <= 1) { |
1465 | 41 | zval_ptr_dtor(return_value); |
1466 | 41 | } |
1467 | 41 | RETVAL_FALSE; |
1468 | 192 | } else { |
1469 | 192 | 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 | 192 | if (BG(unserialize).level > 1) { |
1476 | 0 | ZVAL_COPY(return_value, retval); |
1477 | 192 | } else if (Z_REFCOUNTED_P(return_value)) { |
1478 | 192 | zend_refcounted *ref = Z_COUNTED_P(return_value); |
1479 | 192 | gc_check_possible_root(ref); |
1480 | 192 | } |
1481 | 192 | } |
1482 | | |
1483 | 233 | cleanup: |
1484 | 233 | 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 | 233 | php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash); |
1491 | 233 | php_var_unserialize_set_max_depth(var_hash, prev_max_depth); |
1492 | 233 | php_var_unserialize_set_cur_depth(var_hash, prev_cur_depth); |
1493 | 233 | 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 | 233 | if (Z_ISREF_P(return_value)) { |
1499 | 0 | zend_unwrap_reference(return_value); |
1500 | 0 | } |
1501 | 233 | } |
1502 | | /* }}} */ |
1503 | | |
1504 | | /* {{{ Takes a string representation of variable and recreates it */ |
1505 | | PHP_FUNCTION(unserialize) |
1506 | 235 | { |
1507 | 235 | char *buf = NULL; |
1508 | 235 | size_t buf_len; |
1509 | 235 | HashTable *options = NULL; |
1510 | | |
1511 | 705 | ZEND_PARSE_PARAMETERS_START(1, 2) |
1512 | 940 | Z_PARAM_STRING(buf, buf_len) |
1513 | 235 | Z_PARAM_OPTIONAL |
1514 | 472 | Z_PARAM_ARRAY_HT(options) |
1515 | 235 | ZEND_PARSE_PARAMETERS_END(); |
1516 | | |
1517 | 234 | php_unserialize_with_options(return_value, buf, buf_len, options, "unserialize"); |
1518 | 234 | } |
1519 | | /* }}} */ |
1520 | | |
1521 | | /* {{{ Returns the allocated by PHP memory */ |
1522 | 0 | PHP_FUNCTION(memory_get_usage) { |
1523 | 0 | bool real_usage = 0; |
1524 | |
|
1525 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
1526 | 0 | Z_PARAM_OPTIONAL |
1527 | 0 | Z_PARAM_BOOL(real_usage) |
1528 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1529 | | |
1530 | 0 | RETURN_LONG(zend_memory_usage(real_usage)); |
1531 | 0 | } |
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 | 14 | { |
1561 | 14 | REGISTER_INI_ENTRIES(); |
1562 | 14 | return SUCCESS; |
1563 | 14 | } |