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