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