/src/php-src/Zend/zend_API.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to version 2.00 of the Zend license, | |
8 | | | that is bundled with this package in the file LICENSE, and is | |
9 | | | available through the world-wide-web at the following url: | |
10 | | | http://www.zend.com/license/2_00.txt. | |
11 | | | If you did not receive a copy of the Zend license and are unable to | |
12 | | | obtain it through the world-wide-web, please send a note to | |
13 | | | license@zend.com so we can mail you a copy immediately. | |
14 | | +----------------------------------------------------------------------+ |
15 | | | Authors: Andi Gutmans <andi@php.net> | |
16 | | | Zeev Suraski <zeev@php.net> | |
17 | | | Andrei Zmievski <andrei@php.net> | |
18 | | | Dmitry Stogov <dmitry@php.net> | |
19 | | +----------------------------------------------------------------------+ |
20 | | */ |
21 | | |
22 | | #include "zend.h" |
23 | | #include "zend_execute.h" |
24 | | #include "zend_API.h" |
25 | | #include "zend_hash.h" |
26 | | #include "zend_modules.h" |
27 | | #include "zend_extensions.h" |
28 | | #include "zend_constants.h" |
29 | | #include "zend_interfaces.h" |
30 | | #include "zend_exceptions.h" |
31 | | #include "zend_closures.h" |
32 | | #include "zend_inheritance.h" |
33 | | #include "zend_ini.h" |
34 | | #include "zend_enum.h" |
35 | | #include "zend_object_handlers.h" |
36 | | #include "zend_observer.h" |
37 | | |
38 | | #include <stdarg.h> |
39 | | |
40 | | /* these variables are true statics/globals, and have to be mutex'ed on every access */ |
41 | | ZEND_API HashTable module_registry; |
42 | | |
43 | | ZEND_API bool zend_dl_use_deepbind = false; |
44 | | |
45 | | static zend_module_entry **module_request_startup_handlers; |
46 | | static zend_module_entry **module_request_shutdown_handlers; |
47 | | static zend_module_entry **module_post_deactivate_handlers; |
48 | | static zend_module_entry **modules_dl_loaded; |
49 | | |
50 | | static zend_class_entry **class_cleanup_handlers; |
51 | | |
52 | | ZEND_API void zend_set_dl_use_deepbind(bool use_deepbind) |
53 | 0 | { |
54 | 0 | zend_dl_use_deepbind = use_deepbind; |
55 | 0 | } |
56 | | |
57 | | ZEND_API zend_result zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */ |
58 | 0 | { |
59 | 0 | zval *param_ptr; |
60 | 0 | uint32_t arg_count; |
61 | |
|
62 | 0 | param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1); |
63 | 0 | arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data)); |
64 | |
|
65 | 0 | if (param_count>arg_count) { |
66 | 0 | return FAILURE; |
67 | 0 | } |
68 | | |
69 | 0 | while (param_count-->0) { |
70 | 0 | ZVAL_COPY_VALUE(argument_array, param_ptr); |
71 | 0 | argument_array++; |
72 | 0 | param_ptr++; |
73 | 0 | } |
74 | |
|
75 | 0 | return SUCCESS; |
76 | 0 | } |
77 | | /* }}} */ |
78 | | |
79 | | ZEND_API zend_result zend_copy_parameters_array(uint32_t param_count, zval *argument_array) /* {{{ */ |
80 | 201 | { |
81 | 201 | zval *param_ptr; |
82 | 201 | uint32_t arg_count; |
83 | | |
84 | 201 | param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1); |
85 | 201 | arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data)); |
86 | | |
87 | 201 | if (param_count>arg_count) { |
88 | 0 | return FAILURE; |
89 | 0 | } |
90 | | |
91 | 699 | while (param_count-->0) { |
92 | 498 | Z_TRY_ADDREF_P(param_ptr); |
93 | 498 | zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr); |
94 | 498 | param_ptr++; |
95 | 498 | } |
96 | | |
97 | 201 | return SUCCESS; |
98 | 201 | } |
99 | | /* }}} */ |
100 | | |
101 | | ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */ |
102 | 0 | { |
103 | 0 | const char *space; |
104 | 0 | const char *class_name = get_active_class_name(&space); |
105 | |
|
106 | 0 | zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name()); |
107 | 0 | } |
108 | | /* }}} */ |
109 | | |
110 | | ZEND_API ZEND_COLD void zend_wrong_property_read(zval *object, zval *property) |
111 | 29.3k | { |
112 | 29.3k | zend_string *tmp_property_name; |
113 | 29.3k | zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name); |
114 | 29.3k | zend_error(E_WARNING, "Attempt to read property \"%s\" on %s", ZSTR_VAL(property_name), zend_zval_value_name(object)); |
115 | 29.3k | zend_tmp_string_release(tmp_property_name); |
116 | 29.3k | } |
117 | | |
118 | | /* Argument parsing API -- andrei */ |
119 | | ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */ |
120 | 29.3k | { |
121 | 29.3k | switch(type) { |
122 | 432 | case IS_FALSE: |
123 | 669 | case IS_TRUE: |
124 | 669 | case _IS_BOOL: |
125 | 669 | return "bool"; |
126 | 2.01k | case IS_LONG: |
127 | 2.01k | return "int"; |
128 | 781 | case IS_DOUBLE: |
129 | 781 | return "float"; |
130 | 9.94k | case IS_STRING: |
131 | 9.94k | return "string"; |
132 | 0 | case IS_OBJECT: |
133 | 0 | return "object"; |
134 | 0 | case IS_RESOURCE: |
135 | 0 | return "resource"; |
136 | 14.6k | case IS_NULL: |
137 | 14.6k | return "null"; |
138 | 0 | case IS_CALLABLE: |
139 | 0 | return "callable"; |
140 | 0 | case IS_ITERABLE: |
141 | 0 | return "iterable"; |
142 | 1.33k | case IS_ARRAY: |
143 | 1.33k | return "array"; |
144 | 0 | case IS_VOID: |
145 | 0 | return "void"; |
146 | 0 | case IS_MIXED: |
147 | 0 | return "mixed"; |
148 | 0 | case _IS_NUMBER: |
149 | 0 | return "int|float"; |
150 | 29.3k | EMPTY_SWITCH_DEFAULT_CASE() |
151 | 29.3k | } |
152 | 29.3k | } |
153 | | /* }}} */ |
154 | | |
155 | | ZEND_API const char *zend_zval_value_name(const zval *arg) |
156 | 42.1k | { |
157 | 42.1k | ZVAL_DEREF(arg); |
158 | | |
159 | 42.1k | if (Z_ISUNDEF_P(arg)) { |
160 | 24.6k | return "null"; |
161 | 24.6k | } |
162 | | |
163 | 17.4k | if (Z_TYPE_P(arg) == IS_OBJECT) { |
164 | 1.17k | return ZSTR_VAL(Z_OBJCE_P(arg)->name); |
165 | 16.3k | } else if (Z_TYPE_P(arg) == IS_FALSE) { |
166 | 104 | return "false"; |
167 | 16.2k | } else if (Z_TYPE_P(arg) == IS_TRUE) { |
168 | 233 | return "true"; |
169 | 233 | } |
170 | | |
171 | 15.9k | return zend_get_type_by_const(Z_TYPE_P(arg)); |
172 | 17.4k | } |
173 | | |
174 | | ZEND_API const char *zend_zval_type_name(const zval *arg) |
175 | 12.8k | { |
176 | 12.8k | ZVAL_DEREF(arg); |
177 | | |
178 | 12.8k | if (Z_ISUNDEF_P(arg)) { |
179 | 0 | return "null"; |
180 | 0 | } |
181 | | |
182 | 12.8k | if (Z_TYPE_P(arg) == IS_OBJECT) { |
183 | 534 | return ZSTR_VAL(Z_OBJCE_P(arg)->name); |
184 | 534 | } |
185 | | |
186 | 12.2k | return zend_get_type_by_const(Z_TYPE_P(arg)); |
187 | 12.8k | } |
188 | | |
189 | | /* This API exists *only* for use in gettype(). |
190 | | * For anything else, you likely want zend_zval_type_name(). */ |
191 | | ZEND_API zend_string *zend_zval_get_legacy_type(const zval *arg) /* {{{ */ |
192 | 185 | { |
193 | 185 | switch (Z_TYPE_P(arg)) { |
194 | 16 | case IS_NULL: |
195 | 16 | return ZSTR_KNOWN(ZEND_STR_NULL); |
196 | 0 | case IS_FALSE: |
197 | 0 | case IS_TRUE: |
198 | 0 | return ZSTR_KNOWN(ZEND_STR_BOOLEAN); |
199 | 133 | case IS_LONG: |
200 | 133 | return ZSTR_KNOWN(ZEND_STR_INTEGER); |
201 | 0 | case IS_DOUBLE: |
202 | 0 | return ZSTR_KNOWN(ZEND_STR_DOUBLE); |
203 | 2 | case IS_STRING: |
204 | 2 | return ZSTR_KNOWN(ZEND_STR_STRING); |
205 | 34 | case IS_ARRAY: |
206 | 34 | return ZSTR_KNOWN(ZEND_STR_ARRAY); |
207 | 0 | case IS_OBJECT: |
208 | 0 | return ZSTR_KNOWN(ZEND_STR_OBJECT); |
209 | 0 | case IS_RESOURCE: |
210 | 0 | if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) { |
211 | 0 | return ZSTR_KNOWN(ZEND_STR_RESOURCE); |
212 | 0 | } else { |
213 | 0 | return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE); |
214 | 0 | } |
215 | 0 | default: |
216 | 0 | return NULL; |
217 | 185 | } |
218 | 185 | } |
219 | | /* }}} */ |
220 | | |
221 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */ |
222 | 157 | { |
223 | 157 | int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data)); |
224 | 157 | zend_string *func_name = get_active_function_or_method_name(); |
225 | | |
226 | 157 | zend_argument_count_error("%s() expects exactly 0 arguments, %d given", ZSTR_VAL(func_name), num_args); |
227 | | |
228 | 157 | zend_string_release(func_name); |
229 | 157 | } |
230 | | /* }}} */ |
231 | | |
232 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args) /* {{{ */ |
233 | 359 | { |
234 | 359 | uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data)); |
235 | 359 | zend_string *func_name = get_active_function_or_method_name(); |
236 | | |
237 | 359 | zend_argument_count_error( |
238 | 359 | "%s() expects %s %d argument%s, %d given", |
239 | 359 | ZSTR_VAL(func_name), |
240 | 359 | min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most", |
241 | 359 | num_args < min_num_args ? min_num_args : max_num_args, |
242 | 359 | (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s", |
243 | 359 | num_args |
244 | 359 | ); |
245 | | |
246 | 359 | zend_string_release(func_name); |
247 | 359 | } |
248 | | /* }}} */ |
249 | | |
250 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_error(int error_code, uint32_t num, char *name, zend_expected_type expected_type, zval *arg) /* {{{ */ |
251 | 872 | { |
252 | 872 | switch (error_code) { |
253 | 44 | case ZPP_ERROR_WRONG_CALLBACK: |
254 | 44 | zend_wrong_callback_error(num, name); |
255 | 44 | break; |
256 | 64 | case ZPP_ERROR_WRONG_CALLBACK_OR_NULL: |
257 | 64 | zend_wrong_callback_or_null_error(num, name); |
258 | 64 | break; |
259 | 29 | case ZPP_ERROR_WRONG_CLASS: |
260 | 29 | zend_wrong_parameter_class_error(num, name, arg); |
261 | 29 | break; |
262 | 107 | case ZPP_ERROR_WRONG_CLASS_OR_NULL: |
263 | 107 | zend_wrong_parameter_class_or_null_error(num, name, arg); |
264 | 107 | break; |
265 | 0 | case ZPP_ERROR_WRONG_CLASS_OR_STRING: |
266 | 0 | zend_wrong_parameter_class_or_string_error(num, name, arg); |
267 | 0 | break; |
268 | 2 | case ZPP_ERROR_WRONG_CLASS_OR_STRING_OR_NULL: |
269 | 2 | zend_wrong_parameter_class_or_string_or_null_error(num, name, arg); |
270 | 2 | break; |
271 | 0 | case ZPP_ERROR_WRONG_CLASS_OR_LONG: |
272 | 0 | zend_wrong_parameter_class_or_long_error(num, name, arg); |
273 | 0 | break; |
274 | 0 | case ZPP_ERROR_WRONG_CLASS_OR_LONG_OR_NULL: |
275 | 0 | zend_wrong_parameter_class_or_long_or_null_error(num, name, arg); |
276 | 0 | break; |
277 | 262 | case ZPP_ERROR_WRONG_ARG: |
278 | 262 | zend_wrong_parameter_type_error(num, expected_type, arg); |
279 | 262 | break; |
280 | 5 | case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED: |
281 | 5 | zend_unexpected_extra_named_error(); |
282 | 5 | break; |
283 | 359 | case ZPP_ERROR_FAILURE: |
284 | 359 | ZEND_ASSERT(EG(exception) && "Should have produced an error already"); |
285 | 359 | break; |
286 | 872 | EMPTY_SWITCH_DEFAULT_CASE() |
287 | 872 | } |
288 | 872 | } |
289 | | /* }}} */ |
290 | | |
291 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t num, zend_expected_type expected_type, zval *arg) /* {{{ */ |
292 | 262 | { |
293 | 262 | static const char * const expected_error[] = { |
294 | 8.90k | Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR) |
295 | 262 | NULL |
296 | 262 | }; |
297 | | |
298 | 262 | if (EG(exception)) { |
299 | 13 | return; |
300 | 13 | } |
301 | | |
302 | 249 | if ((expected_type == Z_EXPECTED_PATH || expected_type == Z_EXPECTED_PATH_OR_NULL) |
303 | 249 | && Z_TYPE_P(arg) == IS_STRING) { |
304 | 10 | zend_argument_value_error(num, "must not contain any null bytes"); |
305 | 10 | return; |
306 | 10 | } |
307 | | |
308 | 239 | zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_value_name(arg)); |
309 | 239 | } |
310 | | /* }}} */ |
311 | | |
312 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, zval *arg) /* {{{ */ |
313 | 29 | { |
314 | 29 | if (EG(exception)) { |
315 | 0 | return; |
316 | 0 | } |
317 | | |
318 | 29 | zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_value_name(arg)); |
319 | 29 | } |
320 | | /* }}} */ |
321 | | |
322 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */ |
323 | 107 | { |
324 | 107 | if (EG(exception)) { |
325 | 0 | return; |
326 | 0 | } |
327 | | |
328 | 107 | zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_value_name(arg)); |
329 | 107 | } |
330 | | /* }}} */ |
331 | | |
332 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_error(uint32_t num, const char *name, zval *arg) /* {{{ */ |
333 | 0 | { |
334 | 0 | if (EG(exception)) { |
335 | 0 | return; |
336 | 0 | } |
337 | | |
338 | 0 | zend_argument_type_error(num, "must be of type %s|int, %s given", name, zend_zval_value_name(arg)); |
339 | 0 | } |
340 | | /* }}} */ |
341 | | |
342 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */ |
343 | 0 | { |
344 | 0 | if (EG(exception)) { |
345 | 0 | return; |
346 | 0 | } |
347 | | |
348 | 0 | zend_argument_type_error(num, "must be of type %s|int|null, %s given", name, zend_zval_value_name(arg)); |
349 | 0 | } |
350 | | /* }}} */ |
351 | | |
352 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_error(uint32_t num, const char *name, zval *arg) /* {{{ */ |
353 | 0 | { |
354 | 0 | if (EG(exception)) { |
355 | 0 | return; |
356 | 0 | } |
357 | | |
358 | 0 | zend_argument_type_error(num, "must be of type %s|string, %s given", name, zend_zval_value_name(arg)); |
359 | 0 | } |
360 | | /* }}} */ |
361 | | |
362 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */ |
363 | 2 | { |
364 | 2 | if (EG(exception)) { |
365 | 0 | return; |
366 | 0 | } |
367 | | |
368 | 2 | zend_argument_type_error(num, "must be of type %s|string|null, %s given", name, zend_zval_value_name(arg)); |
369 | 2 | } |
370 | | /* }}} */ |
371 | | |
372 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error) /* {{{ */ |
373 | 44 | { |
374 | 44 | if (!EG(exception)) { |
375 | 44 | zend_argument_type_error(num, "must be a valid callback, %s", error); |
376 | 44 | } |
377 | 44 | efree(error); |
378 | 44 | } |
379 | | /* }}} */ |
380 | | |
381 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_or_null_error(uint32_t num, char *error) /* {{{ */ |
382 | 64 | { |
383 | 64 | if (!EG(exception)) { |
384 | 51 | zend_argument_type_error(num, "must be a valid callback or null, %s", error); |
385 | 51 | } |
386 | 64 | efree(error); |
387 | 64 | } |
388 | | /* }}} */ |
389 | | |
390 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void) |
391 | 10 | { |
392 | 10 | const char *space; |
393 | 10 | const char *class_name = get_active_class_name(&space); |
394 | 10 | zend_argument_count_error("%s%s%s() does not accept unknown named parameters", |
395 | 10 | class_name, space, get_active_function_name()); |
396 | 10 | } |
397 | | |
398 | | ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_entry *error_ce, uint32_t arg_num, const char *format, va_list va) /* {{{ */ |
399 | 1.75k | { |
400 | 1.75k | zend_string *func_name; |
401 | 1.75k | const char *arg_name; |
402 | 1.75k | char *message = NULL; |
403 | 1.75k | if (EG(exception)) { |
404 | 0 | return; |
405 | 0 | } |
406 | | |
407 | 1.75k | func_name = get_active_function_or_method_name(); |
408 | 1.75k | arg_name = get_active_function_arg_name(arg_num); |
409 | | |
410 | 1.75k | zend_vspprintf(&message, 0, format, va); |
411 | 1.75k | zend_throw_error(error_ce, "%s(): Argument #%d%s%s%s %s", |
412 | 1.75k | ZSTR_VAL(func_name), arg_num, |
413 | 1.75k | arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message |
414 | 1.75k | ); |
415 | 1.75k | efree(message); |
416 | 1.75k | zend_string_release(func_name); |
417 | 1.75k | } |
418 | | /* }}} */ |
419 | | |
420 | | ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */ |
421 | 97 | { |
422 | 97 | va_list va; |
423 | | |
424 | 97 | va_start(va, format); |
425 | 97 | zend_argument_error_variadic(error_ce, arg_num, format, va); |
426 | 97 | va_end(va); |
427 | 97 | } |
428 | | /* }}} */ |
429 | | |
430 | | ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */ |
431 | 1.48k | { |
432 | 1.48k | va_list va; |
433 | | |
434 | 1.48k | va_start(va, format); |
435 | 1.48k | zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va); |
436 | 1.48k | va_end(va); |
437 | 1.48k | } |
438 | | /* }}} */ |
439 | | |
440 | | ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */ |
441 | 174 | { |
442 | 174 | va_list va; |
443 | | |
444 | 174 | va_start(va, format); |
445 | 174 | zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va); |
446 | 174 | va_end(va); |
447 | 174 | } |
448 | | /* }}} */ |
449 | | |
450 | | ZEND_API ZEND_COLD void zend_argument_must_not_be_empty_error(uint32_t arg_num) |
451 | 0 | { |
452 | 0 | zend_argument_value_error(arg_num, "must not be empty"); |
453 | 0 | } |
454 | | |
455 | | ZEND_API ZEND_COLD void zend_class_redeclaration_error_ex(int type, zend_string *new_name, zend_class_entry *old_ce) |
456 | 155 | { |
457 | 155 | if (old_ce->type == ZEND_INTERNAL_CLASS) { |
458 | 10 | zend_error(type, "Cannot redeclare %s %s", |
459 | 10 | zend_get_object_type(old_ce), |
460 | 10 | ZSTR_VAL(new_name)); |
461 | 145 | } else { |
462 | 145 | zend_error(type, "Cannot redeclare %s %s (previously declared in %s:%d)", |
463 | 145 | zend_get_object_type(old_ce), |
464 | 145 | ZSTR_VAL(new_name), |
465 | 145 | ZSTR_VAL(old_ce->info.user.filename), |
466 | 145 | old_ce->info.user.line_start); |
467 | 145 | } |
468 | 155 | } |
469 | | |
470 | | ZEND_API ZEND_COLD void zend_class_redeclaration_error(int type, zend_class_entry *old_ce) |
471 | 140 | { |
472 | 140 | zend_class_redeclaration_error_ex(type, old_ce->name, old_ce); |
473 | 140 | } |
474 | | |
475 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, uint32_t num, bool check_null) /* {{{ */ |
476 | 0 | { |
477 | 0 | zend_class_entry *ce_base = *pce; |
478 | |
|
479 | 0 | if (check_null && Z_TYPE_P(arg) == IS_NULL) { |
480 | 0 | *pce = NULL; |
481 | 0 | return 1; |
482 | 0 | } |
483 | 0 | if (!try_convert_to_string(arg)) { |
484 | 0 | *pce = NULL; |
485 | 0 | return 0; |
486 | 0 | } |
487 | | |
488 | 0 | *pce = zend_lookup_class(Z_STR_P(arg)); |
489 | 0 | if (ce_base) { |
490 | 0 | if ((!*pce || !instanceof_function(*pce, ce_base))) { |
491 | 0 | zend_argument_type_error(num, "must be a class name derived from %s, %s given", ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg)); |
492 | 0 | *pce = NULL; |
493 | 0 | return 0; |
494 | 0 | } |
495 | 0 | } |
496 | 0 | if (!*pce) { |
497 | 0 | zend_argument_type_error(num, "must be a valid class name, %s given", Z_STRVAL_P(arg)); |
498 | 0 | return 0; |
499 | 0 | } |
500 | 0 | return 1; |
501 | 0 | } |
502 | | /* }}} */ |
503 | | |
504 | 4.92k | static ZEND_COLD bool zend_null_arg_deprecated(const char *fallback_type, uint32_t arg_num) { |
505 | 4.92k | zend_function *func = zend_active_function(); |
506 | 4.92k | ZEND_ASSERT(arg_num > 0); |
507 | 4.92k | uint32_t arg_offset = arg_num - 1; |
508 | 4.92k | if (arg_offset >= func->common.num_args) { |
509 | 0 | ZEND_ASSERT(func->common.fn_flags & ZEND_ACC_VARIADIC); |
510 | 0 | arg_offset = func->common.num_args; |
511 | 0 | } |
512 | | |
513 | 4.92k | zend_arg_info *arg_info = &func->common.arg_info[arg_offset]; |
514 | 4.92k | zend_string *func_name = get_active_function_or_method_name(); |
515 | 4.92k | const char *arg_name = get_active_function_arg_name(arg_num); |
516 | | |
517 | | /* If no type is specified in arginfo, use the specified fallback_type determined through |
518 | | * zend_parse_parameters instead. */ |
519 | 4.92k | zend_string *type_str = zend_type_to_string(arg_info->type); |
520 | 4.92k | const char *type = type_str ? ZSTR_VAL(type_str) : fallback_type; |
521 | 4.92k | zend_error(E_DEPRECATED, |
522 | 4.92k | "%s(): Passing null to parameter #%" PRIu32 "%s%s%s of type %s is deprecated", |
523 | 4.92k | ZSTR_VAL(func_name), arg_num, |
524 | 4.92k | arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", |
525 | 4.92k | type); |
526 | 4.92k | zend_string_release(func_name); |
527 | 4.92k | if (type_str) { |
528 | 4.91k | zend_string_release(type_str); |
529 | 4.91k | } |
530 | 4.92k | return !EG(exception); |
531 | 4.92k | } |
532 | | |
533 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_weak(const zval *arg, bool *dest, uint32_t arg_num) /* {{{ */ |
534 | 3.39k | { |
535 | 3.39k | if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) { |
536 | 3.39k | if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("bool", arg_num)) { |
537 | 0 | return 0; |
538 | 0 | } |
539 | 3.39k | *dest = zend_is_true(arg); |
540 | 3.39k | } else { |
541 | 0 | return 0; |
542 | 0 | } |
543 | 3.39k | return 1; |
544 | 3.39k | } |
545 | | /* }}} */ |
546 | | |
547 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num) /* {{{ */ |
548 | 1.81k | { |
549 | 1.81k | if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) { |
550 | 0 | return 0; |
551 | 0 | } |
552 | 1.81k | return zend_parse_arg_bool_weak(arg, dest, arg_num); |
553 | 1.81k | } |
554 | | /* }}} */ |
555 | | |
556 | | ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_bool_slow(const zval *arg, bool *dest, uint32_t arg_num) |
557 | 0 | { |
558 | 0 | if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) { |
559 | 0 | return 0; |
560 | 0 | } |
561 | 0 | return zend_parse_arg_bool_weak(arg, dest, arg_num); |
562 | 0 | } |
563 | | |
564 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */ |
565 | 2.19k | { |
566 | 2.19k | if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) { |
567 | 351 | if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) { |
568 | 10 | return 0; |
569 | 10 | } |
570 | 341 | if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) { |
571 | 105 | return 0; |
572 | 236 | } else { |
573 | 236 | zend_long lval = zend_dval_to_lval(Z_DVAL_P(arg)); |
574 | 236 | if (UNEXPECTED(!zend_is_long_compatible(Z_DVAL_P(arg), lval))) { |
575 | | /* Check arg_num is not (uint32_t)-1, as otherwise its called by |
576 | | * zend_verify_weak_scalar_type_hint_no_sideeffect() */ |
577 | 67 | if (arg_num != (uint32_t)-1) { |
578 | 53 | zend_incompatible_double_to_long_error(Z_DVAL_P(arg)); |
579 | 53 | } |
580 | 67 | if (UNEXPECTED(EG(exception))) { |
581 | 0 | return 0; |
582 | 0 | } |
583 | 67 | } |
584 | 236 | *dest = lval; |
585 | 236 | } |
586 | 1.84k | } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) { |
587 | 962 | double d; |
588 | 962 | uint8_t type; |
589 | | |
590 | 962 | if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) { |
591 | 445 | if (EXPECTED(type != 0)) { |
592 | 208 | zend_long lval; |
593 | 208 | if (UNEXPECTED(zend_isnan(d))) { |
594 | 0 | return 0; |
595 | 0 | } |
596 | 208 | if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) { |
597 | 34 | return 0; |
598 | 34 | } |
599 | | |
600 | 174 | lval = zend_dval_to_lval(d); |
601 | | /* This only checks for a fractional part as if doesn't fit it already throws a TypeError */ |
602 | 174 | if (UNEXPECTED(!zend_is_long_compatible(d, lval))) { |
603 | | /* Check arg_num is not (uint32_t)-1, as otherwise its called by |
604 | | * zend_verify_weak_scalar_type_hint_no_sideeffect() */ |
605 | 65 | if (arg_num != (uint32_t)-1) { |
606 | 51 | zend_incompatible_string_to_long_error(Z_STR_P(arg)); |
607 | 51 | } |
608 | 65 | if (UNEXPECTED(EG(exception))) { |
609 | 0 | return 0; |
610 | 0 | } |
611 | 65 | } |
612 | 174 | *dest = lval; |
613 | 237 | } else { |
614 | 237 | return 0; |
615 | 237 | } |
616 | 445 | } |
617 | 691 | if (UNEXPECTED(EG(exception))) { |
618 | 0 | return 0; |
619 | 0 | } |
620 | 883 | } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) { |
621 | 624 | if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int", arg_num)) { |
622 | 0 | return 0; |
623 | 0 | } |
624 | 624 | *dest = 0; |
625 | 624 | } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) { |
626 | 89 | *dest = 1; |
627 | 170 | } else { |
628 | 170 | return 0; |
629 | 170 | } |
630 | 1.64k | return 1; |
631 | 2.19k | } |
632 | | /* }}} */ |
633 | | |
634 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */ |
635 | 653 | { |
636 | 653 | if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) { |
637 | 5 | return 0; |
638 | 5 | } |
639 | 648 | return zend_parse_arg_long_weak(arg, dest, arg_num); |
640 | 653 | } |
641 | | /* }}} */ |
642 | | |
643 | | ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_long_slow(const zval *arg, zend_long *dest, uint32_t arg_num) |
644 | 0 | { |
645 | 0 | if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) { |
646 | 0 | return 0; |
647 | 0 | } |
648 | 0 | return zend_parse_arg_long_weak(arg, dest, arg_num); |
649 | 0 | } |
650 | | |
651 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_weak(const zval *arg, double *dest, uint32_t arg_num) /* {{{ */ |
652 | 893 | { |
653 | 893 | if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) { |
654 | 740 | *dest = (double)Z_LVAL_P(arg); |
655 | 740 | } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) { |
656 | 119 | zend_long l; |
657 | 119 | uint8_t type; |
658 | | |
659 | 119 | if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) { |
660 | 104 | if (EXPECTED(type != 0)) { |
661 | 82 | *dest = (double)(l); |
662 | 82 | } else { |
663 | 22 | return 0; |
664 | 22 | } |
665 | 104 | } |
666 | 97 | if (UNEXPECTED(EG(exception))) { |
667 | 0 | return 0; |
668 | 0 | } |
669 | 97 | } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) { |
670 | 0 | if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("float", arg_num)) { |
671 | 0 | return 0; |
672 | 0 | } |
673 | 0 | *dest = 0.0; |
674 | 34 | } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) { |
675 | 0 | *dest = 1.0; |
676 | 34 | } else { |
677 | 34 | return 0; |
678 | 34 | } |
679 | 837 | return 1; |
680 | 893 | } |
681 | | /* }}} */ |
682 | | |
683 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(const zval *arg, double *dest, uint32_t arg_num) /* {{{ */ |
684 | 315 | { |
685 | 315 | if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) { |
686 | | /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */ |
687 | 315 | *dest = (double)Z_LVAL_P(arg); |
688 | 315 | } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) { |
689 | 0 | return 0; |
690 | 0 | } |
691 | 315 | return zend_parse_arg_double_weak(arg, dest, arg_num); |
692 | 315 | } |
693 | | /* }}} */ |
694 | | |
695 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest, uint32_t arg_num) /* {{{ */ |
696 | 0 | { |
697 | 0 | if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) { |
698 | 0 | return 0; |
699 | 0 | } |
700 | 0 | if (Z_TYPE_P(arg) == IS_STRING) { |
701 | 0 | zend_string *str = Z_STR_P(arg); |
702 | 0 | zend_long lval; |
703 | 0 | double dval; |
704 | 0 | uint8_t type = is_numeric_str_function(str, &lval, &dval); |
705 | 0 | if (type == IS_LONG) { |
706 | 0 | ZVAL_LONG(arg, lval); |
707 | 0 | } else if (type == IS_DOUBLE) { |
708 | 0 | ZVAL_DOUBLE(arg, dval); |
709 | 0 | } else { |
710 | 0 | return 0; |
711 | 0 | } |
712 | 0 | zend_string_release(str); |
713 | 0 | } else if (Z_TYPE_P(arg) < IS_TRUE) { |
714 | 0 | if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int|float", arg_num)) { |
715 | 0 | return 0; |
716 | 0 | } |
717 | 0 | ZVAL_LONG(arg, 0); |
718 | 0 | } else if (Z_TYPE_P(arg) == IS_TRUE) { |
719 | 0 | ZVAL_LONG(arg, 1); |
720 | 0 | } else { |
721 | 0 | return 0; |
722 | 0 | } |
723 | 0 | *dest = arg; |
724 | 0 | return 1; |
725 | 0 | } |
726 | | /* }}} */ |
727 | | |
728 | | |
729 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_or_str_slow(zval *arg, zval **dest, uint32_t arg_num) /* {{{ */ |
730 | 0 | { |
731 | 0 | if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) { |
732 | 0 | return false; |
733 | 0 | } |
734 | 0 | if (Z_TYPE_P(arg) < IS_TRUE) { |
735 | 0 | if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string|int|float", arg_num)) { |
736 | 0 | return false; |
737 | 0 | } |
738 | 0 | ZVAL_LONG(arg, 0); |
739 | 0 | } else if (Z_TYPE_P(arg) == IS_TRUE) { |
740 | 0 | ZVAL_LONG(arg, 1); |
741 | 0 | } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) { |
742 | 0 | zend_object *zobj = Z_OBJ_P(arg); |
743 | 0 | zval obj; |
744 | 0 | if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) { |
745 | 0 | OBJ_RELEASE(zobj); |
746 | 0 | ZVAL_COPY_VALUE(arg, &obj); |
747 | 0 | *dest = arg; |
748 | 0 | return true; |
749 | 0 | } |
750 | 0 | return false; |
751 | 0 | } else { |
752 | 0 | return false; |
753 | 0 | } |
754 | 0 | *dest = arg; |
755 | 0 | return true; |
756 | 0 | } |
757 | | |
758 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */ |
759 | 8.55k | { |
760 | 8.55k | if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) { |
761 | 7.42k | if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string", arg_num)) { |
762 | 0 | return 0; |
763 | 0 | } |
764 | 7.42k | convert_to_string(arg); |
765 | 7.42k | *dest = Z_STR_P(arg); |
766 | 7.42k | } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) { |
767 | 1.08k | zend_object *zobj = Z_OBJ_P(arg); |
768 | 1.08k | zval obj; |
769 | 1.08k | if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) { |
770 | 952 | OBJ_RELEASE(zobj); |
771 | 952 | ZVAL_COPY_VALUE(arg, &obj); |
772 | 952 | *dest = Z_STR_P(arg); |
773 | 952 | return 1; |
774 | 952 | } |
775 | 137 | return 0; |
776 | 1.08k | } else { |
777 | 42 | return 0; |
778 | 42 | } |
779 | 7.42k | return 1; |
780 | 8.55k | } |
781 | | /* }}} */ |
782 | | |
783 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */ |
784 | 8.00k | { |
785 | 8.00k | if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) { |
786 | 17 | return 0; |
787 | 17 | } |
788 | 7.98k | return zend_parse_arg_str_weak(arg, dest, arg_num); |
789 | 8.00k | } |
790 | | /* }}} */ |
791 | | |
792 | | ZEND_API bool ZEND_FASTCALL zend_flf_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num) |
793 | 0 | { |
794 | 0 | if (UNEXPECTED(ZEND_FLF_ARG_USES_STRICT_TYPES())) { |
795 | 0 | return 0; |
796 | 0 | } |
797 | 0 | return zend_parse_arg_str_weak(arg, dest, arg_num); |
798 | 0 | } |
799 | | |
800 | | ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long, uint32_t arg_num) /* {{{ */ |
801 | 5 | { |
802 | 5 | if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) { |
803 | 0 | return 0; |
804 | 0 | } |
805 | 5 | if (zend_parse_arg_long_weak(arg, dest_long, arg_num)) { |
806 | 0 | *dest_str = NULL; |
807 | 0 | return 1; |
808 | 5 | } else if (zend_parse_arg_str_weak(arg, dest_str, arg_num)) { |
809 | 0 | *dest_long = 0; |
810 | 0 | return 1; |
811 | 5 | } else { |
812 | 5 | return 0; |
813 | 5 | } |
814 | 5 | } |
815 | | /* }}} */ |
816 | | |
817 | | static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec, char **error, uint32_t arg_num) /* {{{ */ |
818 | 411k | { |
819 | 411k | const char *spec_walk = *spec; |
820 | 411k | char c = *spec_walk++; |
821 | 411k | bool check_null = 0; |
822 | 411k | bool separate = 0; |
823 | 411k | zval *real_arg = arg; |
824 | | |
825 | | /* scan through modifiers */ |
826 | 411k | ZVAL_DEREF(arg); |
827 | 412k | while (1) { |
828 | 412k | if (*spec_walk == '/') { |
829 | 0 | SEPARATE_ZVAL_NOREF(arg); |
830 | 0 | real_arg = arg; |
831 | 0 | separate = 1; |
832 | 412k | } else if (*spec_walk == '!') { |
833 | 479 | check_null = 1; |
834 | 411k | } else { |
835 | 411k | break; |
836 | 411k | } |
837 | 479 | spec_walk++; |
838 | 479 | } |
839 | | |
840 | 411k | switch (c) { |
841 | 1.43k | case 'l': |
842 | 1.43k | { |
843 | 1.43k | zend_long *p = va_arg(*va, zend_long *); |
844 | 1.43k | bool *is_null = NULL; |
845 | | |
846 | 1.43k | if (check_null) { |
847 | 56 | is_null = va_arg(*va, bool *); |
848 | 56 | } |
849 | | |
850 | 1.43k | if (!zend_parse_arg_long(arg, p, is_null, check_null, arg_num)) { |
851 | 7 | return check_null ? "?int" : "int"; |
852 | 7 | } |
853 | 1.43k | } |
854 | 1.42k | break; |
855 | | |
856 | 1.42k | case 'd': |
857 | 0 | { |
858 | 0 | double *p = va_arg(*va, double *); |
859 | 0 | bool *is_null = NULL; |
860 | |
|
861 | 0 | if (check_null) { |
862 | 0 | is_null = va_arg(*va, bool *); |
863 | 0 | } |
864 | |
|
865 | 0 | if (!zend_parse_arg_double(arg, p, is_null, check_null, arg_num)) { |
866 | 0 | return check_null ? "?float" : "float"; |
867 | 0 | } |
868 | 0 | } |
869 | 0 | break; |
870 | | |
871 | 0 | case 'n': |
872 | 0 | { |
873 | 0 | zval **p = va_arg(*va, zval **); |
874 | |
|
875 | 0 | if (!zend_parse_arg_number(arg, p, check_null, arg_num)) { |
876 | 0 | return check_null ? "int|float|null" : "int|float"; |
877 | 0 | } |
878 | 0 | } |
879 | 0 | break; |
880 | | |
881 | 185k | case 's': |
882 | 185k | { |
883 | 185k | char **p = va_arg(*va, char **); |
884 | 185k | size_t *pl = va_arg(*va, size_t *); |
885 | 185k | if (!zend_parse_arg_string(arg, p, pl, check_null, arg_num)) { |
886 | 2 | return check_null ? "?string" : "string"; |
887 | 2 | } |
888 | 185k | } |
889 | 185k | break; |
890 | | |
891 | 185k | case 'p': |
892 | 0 | { |
893 | 0 | char **p = va_arg(*va, char **); |
894 | 0 | size_t *pl = va_arg(*va, size_t *); |
895 | 0 | if (!zend_parse_arg_path(arg, p, pl, check_null, arg_num)) { |
896 | 0 | if (Z_TYPE_P(arg) == IS_STRING) { |
897 | 0 | zend_spprintf(error, 0, "must not contain any null bytes"); |
898 | 0 | return ""; |
899 | 0 | } else { |
900 | 0 | return check_null ? "?string" : "string"; |
901 | 0 | } |
902 | 0 | } |
903 | 0 | } |
904 | 0 | break; |
905 | | |
906 | 0 | case 'P': |
907 | 0 | { |
908 | 0 | zend_string **str = va_arg(*va, zend_string **); |
909 | 0 | if (!zend_parse_arg_path_str(arg, str, check_null, arg_num)) { |
910 | 0 | if (Z_TYPE_P(arg) == IS_STRING) { |
911 | 0 | zend_spprintf(error, 0, "must not contain any null bytes"); |
912 | 0 | return ""; |
913 | 0 | } else { |
914 | 0 | return check_null ? "?string" : "string"; |
915 | 0 | } |
916 | 0 | } |
917 | 0 | } |
918 | 0 | break; |
919 | | |
920 | 92.8k | case 'S': |
921 | 92.8k | { |
922 | 92.8k | zend_string **str = va_arg(*va, zend_string **); |
923 | 92.8k | if (!zend_parse_arg_str(arg, str, check_null, arg_num)) { |
924 | 22 | return check_null ? "?string" : "string"; |
925 | 22 | } |
926 | 92.8k | } |
927 | 92.8k | break; |
928 | | |
929 | 92.8k | case 'b': |
930 | 86.0k | { |
931 | 86.0k | bool *p = va_arg(*va, bool *); |
932 | 86.0k | bool *is_null = NULL; |
933 | | |
934 | 86.0k | if (check_null) { |
935 | 0 | is_null = va_arg(*va, bool *); |
936 | 0 | } |
937 | | |
938 | 86.0k | if (!zend_parse_arg_bool(arg, p, is_null, check_null, arg_num)) { |
939 | 0 | return check_null ? "?bool" : "bool"; |
940 | 0 | } |
941 | 86.0k | } |
942 | 86.0k | break; |
943 | | |
944 | 86.0k | case 'r': |
945 | 10 | { |
946 | 10 | zval **p = va_arg(*va, zval **); |
947 | | |
948 | 10 | if (!zend_parse_arg_resource(arg, p, check_null)) { |
949 | 0 | return check_null ? "resource or null" : "resource"; |
950 | 0 | } |
951 | 10 | } |
952 | 10 | break; |
953 | | |
954 | 274 | case 'A': |
955 | 492 | case 'a': |
956 | 492 | { |
957 | 492 | zval **p = va_arg(*va, zval **); |
958 | | |
959 | 492 | if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) { |
960 | 2 | return check_null ? "?array" : "array"; |
961 | 2 | } |
962 | 492 | } |
963 | 490 | break; |
964 | | |
965 | 490 | case 'H': |
966 | 39.6k | case 'h': |
967 | 39.6k | { |
968 | 39.6k | HashTable **p = va_arg(*va, HashTable **); |
969 | | |
970 | 39.6k | if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) { |
971 | 2 | return check_null ? "?array" : "array"; |
972 | 2 | } |
973 | 39.6k | } |
974 | 39.6k | break; |
975 | | |
976 | 39.6k | case 'o': |
977 | 129 | { |
978 | 129 | zval **p = va_arg(*va, zval **); |
979 | | |
980 | 129 | if (!zend_parse_arg_object(arg, p, NULL, check_null)) { |
981 | 0 | return check_null ? "?object" : "object"; |
982 | 0 | } |
983 | 129 | } |
984 | 129 | break; |
985 | | |
986 | 4.18k | case 'O': |
987 | 4.18k | { |
988 | 4.18k | zval **p = va_arg(*va, zval **); |
989 | 4.18k | zend_class_entry *ce = va_arg(*va, zend_class_entry *); |
990 | | |
991 | 4.18k | if (!zend_parse_arg_object(arg, p, ce, check_null)) { |
992 | 16 | if (ce) { |
993 | 16 | if (check_null) { |
994 | 2 | zend_spprintf(error, 0, "must be of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_value_name(arg)); |
995 | 2 | return ""; |
996 | 14 | } else { |
997 | 14 | return ZSTR_VAL(ce->name); |
998 | 14 | } |
999 | 16 | } else { |
1000 | 0 | return check_null ? "?object" : "object"; |
1001 | 0 | } |
1002 | 16 | } |
1003 | 4.18k | } |
1004 | 4.16k | break; |
1005 | | |
1006 | 4.16k | case 'C': |
1007 | 386 | { |
1008 | 386 | zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **); |
1009 | 386 | zend_class_entry *ce_base = *pce; |
1010 | | |
1011 | 386 | if (check_null && Z_TYPE_P(arg) == IS_NULL) { |
1012 | 0 | *pce = NULL; |
1013 | 0 | break; |
1014 | 0 | } |
1015 | 386 | if (!try_convert_to_string(arg)) { |
1016 | 10 | *pce = NULL; |
1017 | 10 | return ""; /* try_convert_to_string() throws an exception */ |
1018 | 10 | } |
1019 | | |
1020 | 376 | if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) { |
1021 | 10 | *pce = NULL; |
1022 | 366 | } else { |
1023 | 366 | *pce = lookup; |
1024 | 366 | } |
1025 | 376 | if (ce_base) { |
1026 | 0 | if ((!*pce || !instanceof_function(*pce, ce_base))) { |
1027 | 0 | zend_spprintf(error, 0, "must be a class name derived from %s%s, %s given", |
1028 | 0 | ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg)); |
1029 | 0 | *pce = NULL; |
1030 | 0 | return ""; |
1031 | 0 | } |
1032 | 0 | } |
1033 | 376 | if (!*pce) { |
1034 | 10 | zend_spprintf(error, 0, "must be a valid class name%s, %s given", |
1035 | 10 | check_null ? " or null" : "", Z_STRVAL_P(arg)); |
1036 | 10 | return ""; |
1037 | 10 | } |
1038 | 366 | break; |
1039 | | |
1040 | 376 | } |
1041 | 366 | break; |
1042 | | |
1043 | 753 | case 'F': |
1044 | 753 | case 'f': |
1045 | 753 | { |
1046 | 753 | zend_fcall_info *fci = va_arg(*va, zend_fcall_info *); |
1047 | 753 | zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *); |
1048 | 753 | char *is_callable_error = NULL; |
1049 | | |
1050 | 753 | if (check_null && Z_TYPE_P(arg) == IS_NULL) { |
1051 | 0 | fci->size = 0; |
1052 | 0 | fcc->function_handler = 0; |
1053 | 0 | break; |
1054 | 0 | } |
1055 | | |
1056 | 753 | if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) { |
1057 | 748 | ZEND_ASSERT(!is_callable_error); |
1058 | 748 | if (c == 'f') { |
1059 | | /* Release call trampolines: The function may not get called, in which case |
1060 | | * the trampoline will leak. Force it to be refetched during |
1061 | | * zend_call_function instead. */ |
1062 | 0 | zend_release_fcall_info_cache(fcc); |
1063 | 0 | } |
1064 | 748 | break; |
1065 | 748 | } |
1066 | | |
1067 | 5 | if (is_callable_error) { |
1068 | 5 | zend_spprintf(error, 0, "must be a valid callback%s, %s", check_null ? " or null" : "", is_callable_error); |
1069 | 5 | efree(is_callable_error); |
1070 | 5 | return ""; |
1071 | 5 | } else { |
1072 | 0 | return check_null ? "a valid callback or null" : "a valid callback"; |
1073 | 0 | } |
1074 | 5 | } |
1075 | | |
1076 | 583 | case 'z': |
1077 | 583 | { |
1078 | 583 | zval **p = va_arg(*va, zval **); |
1079 | | |
1080 | 583 | zend_parse_arg_zval_deref(real_arg, p, check_null); |
1081 | 583 | } |
1082 | 583 | break; |
1083 | | |
1084 | 0 | case 'Z': /* replace with 'z' */ |
1085 | 0 | case 'L': /* replace with 'l' */ |
1086 | 0 | ZEND_ASSERT(0 && "ZPP modifier no longer supported"); |
1087 | 0 | ZEND_FALLTHROUGH; |
1088 | 0 | default: |
1089 | 0 | return "unknown"; |
1090 | 411k | } |
1091 | | |
1092 | 411k | *spec = spec_walk; |
1093 | | |
1094 | 411k | return NULL; |
1095 | 411k | } |
1096 | | /* }}} */ |
1097 | | |
1098 | | static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */ |
1099 | 411k | { |
1100 | 411k | const char *expected_type = NULL; |
1101 | 411k | char *error = NULL; |
1102 | | |
1103 | 411k | expected_type = zend_parse_arg_impl(arg, va, spec, &error, arg_num); |
1104 | 411k | if (expected_type) { |
1105 | 76 | if (EG(exception)) { |
1106 | 10 | return FAILURE; |
1107 | 10 | } |
1108 | 66 | if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) { |
1109 | 64 | if (error) { |
1110 | 17 | if (strcmp(error, "must not contain any null bytes") == 0) { |
1111 | 0 | zend_argument_value_error(arg_num, "%s", error); |
1112 | 17 | } else { |
1113 | 17 | zend_argument_type_error(arg_num, "%s", error); |
1114 | 17 | } |
1115 | 17 | efree(error); |
1116 | 47 | } else { |
1117 | 47 | zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_value_name(arg)); |
1118 | 47 | } |
1119 | 64 | } else if (error) { |
1120 | 0 | efree(error); |
1121 | 0 | } |
1122 | | |
1123 | 66 | return FAILURE; |
1124 | 76 | } |
1125 | | |
1126 | 411k | return SUCCESS; |
1127 | 411k | } |
1128 | | /* }}} */ |
1129 | | |
1130 | | ZEND_API zend_result zend_parse_parameter(int flags, uint32_t arg_num, zval *arg, const char *spec, ...) |
1131 | 0 | { |
1132 | 0 | va_list va; |
1133 | 0 | zend_result ret; |
1134 | |
|
1135 | 0 | va_start(va, spec); |
1136 | 0 | ret = zend_parse_arg(arg_num, arg, &va, &spec, flags); |
1137 | 0 | va_end(va); |
1138 | |
|
1139 | 0 | return ret; |
1140 | 0 | } |
1141 | | |
1142 | 0 | static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) { |
1143 | 0 | zend_function *active_function = EG(current_execute_data)->func; |
1144 | 0 | const char *class_name = active_function->common.scope |
1145 | 0 | ? ZSTR_VAL(active_function->common.scope->name) : ""; |
1146 | 0 | zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s", |
1147 | 0 | class_name, class_name[0] ? "::" : "", |
1148 | 0 | ZSTR_VAL(active_function->common.function_name), msg); |
1149 | 0 | } |
1150 | | |
1151 | | static zend_result zend_parse_va_args(uint32_t num_args, const char *type_spec, va_list *va, int flags) /* {{{ */ |
1152 | 325k | { |
1153 | 325k | const char *spec_walk; |
1154 | 325k | char c; |
1155 | 325k | uint32_t i; |
1156 | 325k | uint32_t min_num_args = 0; |
1157 | 325k | uint32_t max_num_args = 0; |
1158 | 325k | uint32_t post_varargs = 0; |
1159 | 325k | zval *arg; |
1160 | 325k | bool have_varargs = 0; |
1161 | 325k | bool have_optional_args = 0; |
1162 | 325k | zval **varargs = NULL; |
1163 | 325k | uint32_t *n_varargs = NULL; |
1164 | | |
1165 | 856k | for (spec_walk = type_spec; *spec_walk; spec_walk++) { |
1166 | 530k | c = *spec_walk; |
1167 | 530k | switch (c) { |
1168 | 9.59k | case 'l': case 'd': |
1169 | 283k | case 's': case 'b': |
1170 | 283k | case 'r': case 'a': |
1171 | 291k | case 'o': case 'O': |
1172 | 292k | case 'z': case 'Z': |
1173 | 332k | case 'C': case 'h': |
1174 | 334k | case 'f': case 'F': case 'A': |
1175 | 334k | case 'H': case 'p': |
1176 | 429k | case 'S': case 'P': |
1177 | 429k | case 'L': case 'n': |
1178 | 429k | max_num_args++; |
1179 | 429k | break; |
1180 | | |
1181 | 95.5k | case '|': |
1182 | 95.5k | min_num_args = max_num_args; |
1183 | 95.5k | have_optional_args = 1; |
1184 | 95.5k | break; |
1185 | | |
1186 | 0 | case '/': |
1187 | 4.84k | case '!': |
1188 | | /* Pass */ |
1189 | 4.84k | break; |
1190 | | |
1191 | 713 | case '*': |
1192 | 718 | case '+': |
1193 | 718 | if (have_varargs) { |
1194 | 0 | zend_parse_parameters_debug_error( |
1195 | 0 | "only one varargs specifier (* or +) is permitted"); |
1196 | 0 | return FAILURE; |
1197 | 0 | } |
1198 | 718 | have_varargs = 1; |
1199 | | /* we expect at least one parameter in varargs */ |
1200 | 718 | if (c == '+') { |
1201 | 5 | max_num_args++; |
1202 | 5 | } |
1203 | | /* mark the beginning of varargs */ |
1204 | 718 | post_varargs = max_num_args; |
1205 | | |
1206 | 718 | if (ZEND_CALL_INFO(EG(current_execute_data)) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { |
1207 | 5 | zend_unexpected_extra_named_error(); |
1208 | 5 | return FAILURE; |
1209 | 5 | } |
1210 | 713 | break; |
1211 | | |
1212 | 713 | default: |
1213 | 0 | zend_parse_parameters_debug_error("bad type specifier while parsing parameters"); |
1214 | 0 | return FAILURE; |
1215 | 530k | } |
1216 | 530k | } |
1217 | | |
1218 | | /* with no optional arguments the minimum number of arguments must be the same as the maximum */ |
1219 | 325k | if (!have_optional_args) { |
1220 | 230k | min_num_args = max_num_args; |
1221 | 230k | } |
1222 | | |
1223 | 325k | if (have_varargs) { |
1224 | | /* calculate how many required args are at the end of the specifier list */ |
1225 | 713 | post_varargs = max_num_args - post_varargs; |
1226 | 713 | max_num_args = UINT32_MAX; |
1227 | 713 | } |
1228 | | |
1229 | 325k | if (num_args < min_num_args || num_args > max_num_args) { |
1230 | 142 | if (!(flags & ZEND_PARSE_PARAMS_QUIET)) { |
1231 | 112 | zend_string *func_name = get_active_function_or_method_name(); |
1232 | | |
1233 | 112 | zend_argument_count_error("%s() expects %s %d argument%s, %d given", |
1234 | 112 | ZSTR_VAL(func_name), |
1235 | 112 | min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most", |
1236 | 112 | num_args < min_num_args ? min_num_args : max_num_args, |
1237 | 112 | (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s", |
1238 | 112 | num_args |
1239 | 112 | ); |
1240 | | |
1241 | 112 | zend_string_release(func_name); |
1242 | 112 | } |
1243 | 142 | return FAILURE; |
1244 | 142 | } |
1245 | | |
1246 | 325k | if (num_args > ZEND_CALL_NUM_ARGS(EG(current_execute_data))) { |
1247 | 0 | zend_parse_parameters_debug_error("could not obtain parameters for parsing"); |
1248 | 0 | return FAILURE; |
1249 | 0 | } |
1250 | | |
1251 | 325k | i = 0; |
1252 | 737k | while (num_args-- > 0) { |
1253 | 411k | if (*type_spec == '|') { |
1254 | 89.5k | type_spec++; |
1255 | 89.5k | } |
1256 | | |
1257 | 411k | if (*type_spec == '*' || *type_spec == '+') { |
1258 | 5 | uint32_t num_varargs = num_args + 1 - post_varargs; |
1259 | | |
1260 | | /* eat up the passed in storage even if it won't be filled in with varargs */ |
1261 | 5 | varargs = va_arg(*va, zval **); |
1262 | 5 | n_varargs = va_arg(*va, uint32_t *); |
1263 | 5 | type_spec++; |
1264 | | |
1265 | 5 | if (num_varargs > 0) { |
1266 | 5 | *n_varargs = num_varargs; |
1267 | 5 | *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1); |
1268 | | /* adjust how many args we have left and restart loop */ |
1269 | 5 | num_args += 1 - num_varargs; |
1270 | 5 | i += num_varargs; |
1271 | 5 | continue; |
1272 | 5 | } else { |
1273 | 0 | *varargs = NULL; |
1274 | 0 | *n_varargs = 0; |
1275 | 0 | } |
1276 | 5 | } |
1277 | | |
1278 | 411k | arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1); |
1279 | | |
1280 | 411k | if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) { |
1281 | | /* clean up varargs array if it was used */ |
1282 | 76 | if (varargs && *varargs) { |
1283 | 0 | *varargs = NULL; |
1284 | 0 | } |
1285 | 76 | return FAILURE; |
1286 | 76 | } |
1287 | 411k | i++; |
1288 | 411k | } |
1289 | | |
1290 | 325k | return SUCCESS; |
1291 | 325k | } |
1292 | | /* }}} */ |
1293 | | |
1294 | | ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...) /* {{{ */ |
1295 | 41 | { |
1296 | 41 | va_list va; |
1297 | 41 | zend_result retval; |
1298 | | |
1299 | 41 | va_start(va, type_spec); |
1300 | 41 | retval = zend_parse_va_args(num_args, type_spec, &va, flags); |
1301 | 41 | va_end(va); |
1302 | | |
1303 | 41 | return retval; |
1304 | 41 | } |
1305 | | /* }}} */ |
1306 | | |
1307 | | ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...) /* {{{ */ |
1308 | 325k | { |
1309 | 325k | va_list va; |
1310 | 325k | zend_result retval; |
1311 | 325k | int flags = 0; |
1312 | | |
1313 | 325k | va_start(va, type_spec); |
1314 | 325k | retval = zend_parse_va_args(num_args, type_spec, &va, flags); |
1315 | 325k | va_end(va); |
1316 | | |
1317 | 325k | return retval; |
1318 | 325k | } |
1319 | | /* }}} */ |
1320 | | |
1321 | | ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */ |
1322 | 15 | { |
1323 | 15 | va_list va; |
1324 | 15 | zend_result retval; |
1325 | 15 | int flags = 0; |
1326 | 15 | const char *p = type_spec; |
1327 | 15 | zval **object; |
1328 | 15 | zend_class_entry *ce; |
1329 | | |
1330 | | /* Just checking this_ptr is not enough, because fcall_common_helper does not set |
1331 | | * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL. |
1332 | | * In that case EG(This) would still be the $this from the calling code and we'd take the |
1333 | | * wrong branch here. */ |
1334 | 15 | bool is_method = EG(current_execute_data)->func->common.scope != NULL; |
1335 | | |
1336 | 15 | if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) { |
1337 | 0 | va_start(va, type_spec); |
1338 | 0 | retval = zend_parse_va_args(num_args, type_spec, &va, flags); |
1339 | 0 | va_end(va); |
1340 | 15 | } else { |
1341 | 15 | p++; |
1342 | | |
1343 | 15 | va_start(va, type_spec); |
1344 | | |
1345 | 15 | object = va_arg(va, zval **); |
1346 | 15 | ce = va_arg(va, zend_class_entry *); |
1347 | 15 | *object = this_ptr; |
1348 | | |
1349 | 15 | if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) { |
1350 | 0 | zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()", |
1351 | 0 | ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name()); |
1352 | 0 | } |
1353 | | |
1354 | 15 | retval = zend_parse_va_args(num_args, p, &va, flags); |
1355 | 15 | va_end(va); |
1356 | 15 | } |
1357 | 15 | return retval; |
1358 | 15 | } |
1359 | | /* }}} */ |
1360 | | |
1361 | | ZEND_API zend_result zend_parse_method_parameters_ex(int flags, uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */ |
1362 | 0 | { |
1363 | 0 | va_list va; |
1364 | 0 | zend_result retval; |
1365 | 0 | const char *p = type_spec; |
1366 | 0 | zval **object; |
1367 | 0 | zend_class_entry *ce; |
1368 | |
|
1369 | 0 | if (!this_ptr) { |
1370 | 0 | va_start(va, type_spec); |
1371 | 0 | retval = zend_parse_va_args(num_args, type_spec, &va, flags); |
1372 | 0 | va_end(va); |
1373 | 0 | } else { |
1374 | 0 | p++; |
1375 | 0 | va_start(va, type_spec); |
1376 | |
|
1377 | 0 | object = va_arg(va, zval **); |
1378 | 0 | ce = va_arg(va, zend_class_entry *); |
1379 | 0 | *object = this_ptr; |
1380 | |
|
1381 | 0 | if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) { |
1382 | 0 | if (!(flags & ZEND_PARSE_PARAMS_QUIET)) { |
1383 | 0 | zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()", |
1384 | 0 | ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name()); |
1385 | 0 | } |
1386 | 0 | va_end(va); |
1387 | 0 | return FAILURE; |
1388 | 0 | } |
1389 | | |
1390 | 0 | retval = zend_parse_va_args(num_args, p, &va, flags); |
1391 | 0 | va_end(va); |
1392 | 0 | } |
1393 | 0 | return retval; |
1394 | 0 | } |
1395 | | /* }}} */ |
1396 | | |
1397 | | /* This function should be called after the constructor has been called |
1398 | | * because it may call __set from the uninitialized object otherwise. */ |
1399 | | ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */ |
1400 | 0 | { |
1401 | 0 | zend_object *zobj = Z_OBJ_P(obj); |
1402 | 0 | zend_object_write_property_t write_property = zobj->handlers->write_property; |
1403 | 0 | zend_string *key; |
1404 | 0 | zval *value; |
1405 | |
|
1406 | 0 | if (HT_IS_PACKED(properties)) { |
1407 | 0 | return; |
1408 | 0 | } |
1409 | | |
1410 | 0 | const zend_class_entry *old_scope = EG(fake_scope); |
1411 | 0 | EG(fake_scope) = Z_OBJCE_P(obj); |
1412 | 0 | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(properties, key, value) { |
1413 | 0 | if (key) { |
1414 | 0 | write_property(zobj, key, value, NULL); |
1415 | 0 | } |
1416 | 0 | } ZEND_HASH_FOREACH_END(); |
1417 | 0 | EG(fake_scope) = old_scope; |
1418 | 0 | } |
1419 | | /* }}} */ |
1420 | | |
1421 | | static zend_class_mutable_data *zend_allocate_mutable_data(zend_class_entry *class_type) /* {{{ */ |
1422 | 741 | { |
1423 | 741 | zend_class_mutable_data *mutable_data; |
1424 | | |
1425 | 741 | ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL); |
1426 | 741 | ZEND_ASSERT(ZEND_MAP_PTR_GET_IMM(class_type->mutable_data) == NULL); |
1427 | | |
1428 | 741 | mutable_data = zend_arena_alloc(&CG(arena), sizeof(zend_class_mutable_data)); |
1429 | 741 | memset(mutable_data, 0, sizeof(zend_class_mutable_data)); |
1430 | 741 | mutable_data->ce_flags = class_type->ce_flags; |
1431 | 741 | ZEND_MAP_PTR_SET_IMM(class_type->mutable_data, mutable_data); |
1432 | | |
1433 | 741 | return mutable_data; |
1434 | 741 | } |
1435 | | /* }}} */ |
1436 | | |
1437 | | ZEND_API HashTable *zend_separate_class_constants_table(zend_class_entry *class_type) /* {{{ */ |
1438 | 461 | { |
1439 | 461 | zend_class_mutable_data *mutable_data; |
1440 | 461 | HashTable *constants_table; |
1441 | 461 | zend_string *key; |
1442 | 461 | zend_class_constant *new_c, *c; |
1443 | | |
1444 | 461 | constants_table = zend_arena_alloc(&CG(arena), sizeof(HashTable)); |
1445 | 461 | zend_hash_init(constants_table, zend_hash_num_elements(&class_type->constants_table), NULL, NULL, 0); |
1446 | 461 | zend_hash_extend(constants_table, zend_hash_num_elements(&class_type->constants_table), 0); |
1447 | | |
1448 | 3.14k | ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&class_type->constants_table, key, c) { |
1449 | 3.14k | if (c->ce == class_type) { |
1450 | 1.00k | if (Z_TYPE(c->value) == IS_CONSTANT_AST || (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) { |
1451 | 774 | new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant)); |
1452 | 774 | memcpy(new_c, c, sizeof(zend_class_constant)); |
1453 | 774 | c = new_c; |
1454 | 774 | } |
1455 | 1.00k | Z_TRY_ADDREF(c->value); |
1456 | 1.00k | } else { |
1457 | 106 | if (Z_TYPE(c->value) == IS_CONSTANT_AST) { |
1458 | 94 | c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), key); |
1459 | 94 | ZEND_ASSERT(c); |
1460 | 94 | } |
1461 | 106 | } |
1462 | 1.10k | _zend_hash_append_ptr(constants_table, key, c); |
1463 | 1.10k | } ZEND_HASH_FOREACH_END(); |
1464 | | |
1465 | 461 | ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL); |
1466 | | |
1467 | 461 | mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data); |
1468 | 461 | if (!mutable_data) { |
1469 | 385 | mutable_data = zend_allocate_mutable_data(class_type); |
1470 | 385 | } |
1471 | | |
1472 | 461 | mutable_data->constants_table = constants_table; |
1473 | | |
1474 | 461 | return constants_table; |
1475 | 461 | } |
1476 | | |
1477 | 694 | static zend_result update_property(zval *val, zend_property_info *prop_info) { |
1478 | 694 | if (ZEND_TYPE_IS_SET(prop_info->type)) { |
1479 | 132 | zval tmp; |
1480 | | |
1481 | 132 | ZVAL_COPY(&tmp, val); |
1482 | 132 | if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) { |
1483 | 19 | zval_ptr_dtor(&tmp); |
1484 | 19 | return FAILURE; |
1485 | 19 | } |
1486 | 113 | /* property initializers must always be evaluated with strict types */; |
1487 | 113 | if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) { |
1488 | 24 | zval_ptr_dtor(&tmp); |
1489 | 24 | return FAILURE; |
1490 | 24 | } |
1491 | 89 | zval_ptr_dtor(val); |
1492 | 89 | ZVAL_COPY_VALUE(val, &tmp); |
1493 | 89 | return SUCCESS; |
1494 | 113 | } |
1495 | 562 | return zval_update_constant_ex(val, prop_info->ce); |
1496 | 694 | } |
1497 | | |
1498 | | ZEND_API zend_result zend_update_class_constant(zend_class_constant *c, const zend_string *name, zend_class_entry *scope) |
1499 | 2.21k | { |
1500 | 2.21k | ZEND_ASSERT(Z_TYPE(c->value) == IS_CONSTANT_AST); |
1501 | | |
1502 | 2.21k | if (EXPECTED(!ZEND_TYPE_IS_SET(c->type) || ZEND_TYPE_PURE_MASK(c->type) == MAY_BE_ANY)) { |
1503 | 1.94k | return zval_update_constant_ex(&c->value, scope); |
1504 | 1.94k | } |
1505 | | |
1506 | 276 | zval tmp; |
1507 | | |
1508 | 276 | ZVAL_COPY(&tmp, &c->value); |
1509 | 276 | zend_result result = zval_update_constant_ex(&tmp, scope); |
1510 | 276 | if (result == FAILURE) { |
1511 | 15 | zval_ptr_dtor(&tmp); |
1512 | 15 | return FAILURE; |
1513 | 15 | } |
1514 | | |
1515 | 261 | if (UNEXPECTED(!zend_verify_class_constant_type(c, name, &tmp))) { |
1516 | 62 | zval_ptr_dtor(&tmp); |
1517 | 62 | return FAILURE; |
1518 | 62 | } |
1519 | | |
1520 | 199 | zval_ptr_dtor(&c->value); |
1521 | 199 | ZVAL_COPY_VALUE(&c->value, &tmp); |
1522 | | |
1523 | 199 | return SUCCESS; |
1524 | 261 | } |
1525 | | |
1526 | | ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /* {{{ */ |
1527 | 1.04k | { |
1528 | 1.04k | zend_class_mutable_data *mutable_data = NULL; |
1529 | 1.04k | zval *default_properties_table = NULL; |
1530 | 1.04k | zval *static_members_table = NULL; |
1531 | 1.04k | zend_class_constant *c; |
1532 | 1.04k | zval *val; |
1533 | 1.04k | uint32_t ce_flags; |
1534 | | |
1535 | 1.04k | ce_flags = class_type->ce_flags; |
1536 | | |
1537 | 1.04k | if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) { |
1538 | 26 | return SUCCESS; |
1539 | 26 | } |
1540 | | |
1541 | 1.01k | bool uses_mutable_data = ZEND_MAP_PTR(class_type->mutable_data) != NULL; |
1542 | 1.01k | if (uses_mutable_data) { |
1543 | 639 | mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data); |
1544 | 639 | if (mutable_data) { |
1545 | 283 | ce_flags = mutable_data->ce_flags; |
1546 | 283 | if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) { |
1547 | 236 | return SUCCESS; |
1548 | 236 | } |
1549 | 356 | } else { |
1550 | 356 | mutable_data = zend_allocate_mutable_data(class_type); |
1551 | 356 | } |
1552 | 639 | } |
1553 | | |
1554 | 783 | if (class_type->parent) { |
1555 | 49 | if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) { |
1556 | 7 | return FAILURE; |
1557 | 7 | } |
1558 | 49 | } |
1559 | | |
1560 | 776 | if (ce_flags & ZEND_ACC_HAS_AST_CONSTANTS) { |
1561 | 468 | HashTable *constants_table; |
1562 | | |
1563 | 468 | if (uses_mutable_data) { |
1564 | 104 | constants_table = mutable_data->constants_table; |
1565 | 104 | if (!constants_table) { |
1566 | 76 | constants_table = zend_separate_class_constants_table(class_type); |
1567 | 76 | } |
1568 | 364 | } else { |
1569 | 364 | constants_table = &class_type->constants_table; |
1570 | 364 | } |
1571 | | |
1572 | 468 | zend_string *name; |
1573 | 2.77k | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(constants_table, name, val) { |
1574 | 2.77k | c = Z_PTR_P(val); |
1575 | 2.77k | if (Z_TYPE(c->value) == IS_CONSTANT_AST) { |
1576 | 707 | if (c->ce != class_type) { |
1577 | 10 | Z_PTR_P(val) = c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), name); |
1578 | 10 | if (Z_TYPE(c->value) != IS_CONSTANT_AST) { |
1579 | 5 | continue; |
1580 | 5 | } |
1581 | 10 | } |
1582 | | |
1583 | 702 | val = &c->value; |
1584 | 702 | if (UNEXPECTED(zend_update_class_constant(c, name, c->ce) != SUCCESS)) { |
1585 | 44 | return FAILURE; |
1586 | 44 | } |
1587 | 702 | } |
1588 | 2.77k | } ZEND_HASH_FOREACH_END(); |
1589 | 468 | } |
1590 | | |
1591 | 732 | if (class_type->default_static_members_count) { |
1592 | 135 | static_members_table = CE_STATIC_MEMBERS(class_type); |
1593 | 135 | if (!static_members_table) { |
1594 | 133 | zend_class_init_statics(class_type); |
1595 | 133 | static_members_table = CE_STATIC_MEMBERS(class_type); |
1596 | 133 | } |
1597 | 135 | } |
1598 | | |
1599 | 732 | default_properties_table = class_type->default_properties_table; |
1600 | 732 | if (uses_mutable_data && (ce_flags & ZEND_ACC_HAS_AST_PROPERTIES)) { |
1601 | 216 | zval *src, *dst, *end; |
1602 | | |
1603 | 216 | default_properties_table = mutable_data->default_properties_table; |
1604 | 216 | if (!default_properties_table) { |
1605 | 199 | default_properties_table = zend_arena_alloc(&CG(arena), sizeof(zval) * class_type->default_properties_count); |
1606 | 199 | src = class_type->default_properties_table; |
1607 | 199 | dst = default_properties_table; |
1608 | 199 | end = dst + class_type->default_properties_count; |
1609 | 319 | do { |
1610 | 319 | ZVAL_COPY_PROP(dst, src); |
1611 | 319 | src++; |
1612 | 319 | dst++; |
1613 | 319 | } while (dst != end); |
1614 | 199 | mutable_data->default_properties_table = default_properties_table; |
1615 | 199 | } |
1616 | 216 | } |
1617 | | |
1618 | 732 | if (ce_flags & (ZEND_ACC_HAS_AST_PROPERTIES|ZEND_ACC_HAS_AST_STATICS)) { |
1619 | 318 | zend_property_info *prop_info; |
1620 | | |
1621 | | /* Use the default properties table to also update initializers of private properties |
1622 | | * that have been shadowed in a child class. */ |
1623 | 573 | for (uint32_t i = 0; i < class_type->default_properties_count; i++) { |
1624 | 317 | prop_info = class_type->properties_info_table[i]; |
1625 | 317 | if (!prop_info) { |
1626 | 5 | continue; |
1627 | 5 | } |
1628 | | |
1629 | 312 | val = &default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]; |
1630 | 312 | if (Z_TYPE_P(val) == IS_CONSTANT_AST |
1631 | 312 | && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) { |
1632 | 62 | return FAILURE; |
1633 | 62 | } |
1634 | 312 | } |
1635 | | |
1636 | 256 | if (class_type->default_static_members_count) { |
1637 | 1.12k | ZEND_HASH_MAP_FOREACH_PTR(&class_type->properties_info, prop_info) { |
1638 | 1.12k | if (prop_info->flags & ZEND_ACC_STATIC) { |
1639 | 437 | val = static_members_table + prop_info->offset; |
1640 | 437 | if (Z_TYPE_P(val) == IS_CONSTANT_AST |
1641 | 437 | && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) { |
1642 | 32 | return FAILURE; |
1643 | 32 | } |
1644 | 437 | } |
1645 | 1.12k | } ZEND_HASH_FOREACH_END(); |
1646 | 112 | } |
1647 | 256 | } |
1648 | | |
1649 | 638 | if (class_type->type == ZEND_USER_CLASS && class_type->ce_flags & ZEND_ACC_ENUM && class_type->enum_backing_type != IS_UNDEF) { |
1650 | 352 | if (zend_enum_build_backed_enum_table(class_type) == FAILURE) { |
1651 | 87 | return FAILURE; |
1652 | 87 | } |
1653 | 352 | } |
1654 | | |
1655 | 551 | ce_flags |= ZEND_ACC_CONSTANTS_UPDATED; |
1656 | 551 | ce_flags &= ~ZEND_ACC_HAS_AST_CONSTANTS; |
1657 | 551 | ce_flags &= ~ZEND_ACC_HAS_AST_PROPERTIES; |
1658 | 551 | ce_flags &= ~ZEND_ACC_HAS_AST_STATICS; |
1659 | 551 | if (uses_mutable_data) { |
1660 | 268 | mutable_data->ce_flags = ce_flags; |
1661 | 283 | } else { |
1662 | 283 | class_type->ce_flags = ce_flags; |
1663 | 283 | } |
1664 | | |
1665 | 551 | return SUCCESS; |
1666 | 638 | } |
1667 | | /* }}} */ |
1668 | | |
1669 | | static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */ |
1670 | 2.98M | { |
1671 | 2.98M | if (class_type->default_properties_count) { |
1672 | 1.26M | zval *src = CE_DEFAULT_PROPERTIES_TABLE(class_type); |
1673 | 1.26M | zval *dst = object->properties_table; |
1674 | 1.26M | zval *end = src + class_type->default_properties_count; |
1675 | | |
1676 | 1.26M | if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) { |
1677 | | /* We don't have to account for refcounting because |
1678 | | * zend_declare_typed_property() disallows refcounted defaults for internal classes. */ |
1679 | 8.69M | do { |
1680 | 8.69M | ZEND_ASSERT(!Z_REFCOUNTED_P(src)); |
1681 | 8.69M | ZVAL_COPY_VALUE_PROP(dst, src); |
1682 | 8.69M | src++; |
1683 | 8.69M | dst++; |
1684 | 8.69M | } while (src != end); |
1685 | 1.24M | } else { |
1686 | 27.2k | do { |
1687 | 27.2k | ZVAL_COPY_PROP(dst, src); |
1688 | 27.2k | src++; |
1689 | 27.2k | dst++; |
1690 | 27.2k | } while (src != end); |
1691 | 17.7k | } |
1692 | 1.26M | } |
1693 | 2.98M | } |
1694 | | /* }}} */ |
1695 | | |
1696 | | ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */ |
1697 | 2.66M | { |
1698 | 2.66M | object->properties = NULL; |
1699 | 2.66M | _object_properties_init(object, class_type); |
1700 | 2.66M | } |
1701 | | /* }}} */ |
1702 | | |
1703 | | ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */ |
1704 | 0 | { |
1705 | 0 | object->properties = properties; |
1706 | 0 | if (object->ce->default_properties_count) { |
1707 | 0 | zval *prop; |
1708 | 0 | zend_string *key; |
1709 | 0 | zend_property_info *property_info; |
1710 | |
|
1711 | 0 | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(properties, key, prop) { |
1712 | 0 | property_info = zend_get_property_info(object->ce, key, 1); |
1713 | 0 | if (property_info != ZEND_WRONG_PROPERTY_INFO && |
1714 | 0 | property_info && |
1715 | 0 | (property_info->flags & ZEND_ACC_STATIC) == 0) { |
1716 | 0 | zval *slot = OBJ_PROP(object, property_info->offset); |
1717 | |
|
1718 | 0 | if (ZEND_TYPE_IS_SET(property_info->type)) { |
1719 | 0 | zval tmp; |
1720 | |
|
1721 | 0 | ZVAL_COPY_VALUE(&tmp, prop); |
1722 | 0 | if (UNEXPECTED(!zend_verify_property_type(property_info, &tmp, 0))) { |
1723 | 0 | continue; |
1724 | 0 | } |
1725 | 0 | ZVAL_COPY_VALUE(slot, &tmp); |
1726 | 0 | } else { |
1727 | 0 | ZVAL_COPY_VALUE(slot, prop); |
1728 | 0 | } |
1729 | 0 | ZVAL_INDIRECT(prop, slot); |
1730 | 0 | } |
1731 | 0 | } ZEND_HASH_FOREACH_END(); |
1732 | 0 | } |
1733 | 0 | } |
1734 | | /* }}} */ |
1735 | | |
1736 | | ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */ |
1737 | 42.9k | { |
1738 | 42.9k | zval *prop, tmp; |
1739 | 42.9k | zend_string *key; |
1740 | 42.9k | zend_long h; |
1741 | 42.9k | zend_property_info *property_info; |
1742 | | |
1743 | 208k | ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) { |
1744 | 208k | if (key) { |
1745 | 5.95k | if (ZSTR_VAL(key)[0] == '\0') { |
1746 | 3.67k | const char *class_name, *prop_name; |
1747 | 3.67k | size_t prop_name_len; |
1748 | 3.67k | if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) { |
1749 | 3.33k | zend_string *pname = zend_string_init(prop_name, prop_name_len, 0); |
1750 | 3.33k | const zend_class_entry *prev_scope = EG(fake_scope); |
1751 | 3.33k | if (class_name && class_name[0] != '*') { |
1752 | 3.15k | zend_string *cname = zend_string_init(class_name, strlen(class_name), 0); |
1753 | 3.15k | EG(fake_scope) = zend_lookup_class(cname); |
1754 | 3.15k | zend_string_release_ex(cname, 0); |
1755 | 3.15k | } |
1756 | 3.33k | property_info = zend_get_property_info(object->ce, pname, 1); |
1757 | 3.33k | zend_string_release_ex(pname, 0); |
1758 | 3.33k | EG(fake_scope) = prev_scope; |
1759 | 3.33k | } else { |
1760 | 345 | property_info = ZEND_WRONG_PROPERTY_INFO; |
1761 | 345 | } |
1762 | 3.67k | } else { |
1763 | 2.27k | property_info = zend_get_property_info(object->ce, key, 1); |
1764 | 2.27k | } |
1765 | 5.95k | if (property_info != ZEND_WRONG_PROPERTY_INFO && |
1766 | 5.95k | property_info && |
1767 | 5.95k | (property_info->flags & ZEND_ACC_STATIC) == 0) { |
1768 | 0 | zval *slot = OBJ_PROP(object, property_info->offset); |
1769 | 0 | zval_ptr_dtor(slot); |
1770 | 0 | ZVAL_COPY_VALUE(slot, prop); |
1771 | 0 | zval_add_ref(slot); |
1772 | 0 | if (object->properties) { |
1773 | 0 | ZVAL_INDIRECT(&tmp, slot); |
1774 | 0 | zend_hash_update(object->properties, key, &tmp); |
1775 | 0 | } |
1776 | 5.95k | } else { |
1777 | 5.95k | if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) { |
1778 | 0 | zend_throw_error(NULL, "Cannot create dynamic property %s::$%s", |
1779 | 0 | ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): ""); |
1780 | 0 | return; |
1781 | 5.95k | } else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) { |
1782 | 5.95k | zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated", |
1783 | 5.95k | ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): ""); |
1784 | 5.95k | } |
1785 | | |
1786 | 5.95k | prop = zend_hash_update(zend_std_get_properties_ex(object), key, prop); |
1787 | 5.95k | zval_add_ref(prop); |
1788 | 5.95k | } |
1789 | 76.9k | } else { |
1790 | 76.9k | if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) { |
1791 | 0 | zend_throw_error(NULL, "Cannot create dynamic property %s::$" ZEND_LONG_FMT, ZSTR_VAL(object->ce->name), h); |
1792 | 0 | return; |
1793 | 76.9k | } else if (!(object->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) { |
1794 | 76.9k | zend_error(E_DEPRECATED, "Creation of dynamic property %s::$" ZEND_LONG_FMT " is deprecated", |
1795 | 76.9k | ZSTR_VAL(object->ce->name), h); |
1796 | 76.9k | } |
1797 | | |
1798 | 76.9k | prop = zend_hash_index_update(zend_std_get_properties_ex(object), h, prop); |
1799 | 76.9k | zval_add_ref(prop); |
1800 | 76.9k | } |
1801 | 208k | } ZEND_HASH_FOREACH_END(); |
1802 | 42.9k | } |
1803 | | /* }}} */ |
1804 | | |
1805 | | /* This function requires 'properties' to contain all props declared in the |
1806 | | * class and all props being public. If only a subset is given or the class |
1807 | | * has protected members then you need to merge the properties separately by |
1808 | | * calling zend_merge_properties(). */ |
1809 | | static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */ |
1810 | 3.00M | { |
1811 | 3.00M | if (UNEXPECTED(class_type->ce_flags & ZEND_ACC_UNINSTANTIABLE)) { |
1812 | 1.64k | if (class_type->ce_flags & ZEND_ACC_INTERFACE) { |
1813 | 6 | zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name)); |
1814 | 1.63k | } else if (class_type->ce_flags & ZEND_ACC_TRAIT) { |
1815 | 10 | zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name)); |
1816 | 1.62k | } else if (class_type->ce_flags & ZEND_ACC_ENUM) { |
1817 | 44 | zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(class_type->name)); |
1818 | 1.58k | } else { |
1819 | 1.58k | ZEND_ASSERT(class_type->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)); |
1820 | 1.58k | zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name)); |
1821 | 1.58k | } |
1822 | 1.64k | ZVAL_NULL(arg); |
1823 | 1.64k | Z_OBJ_P(arg) = NULL; |
1824 | 1.64k | return FAILURE; |
1825 | 1.64k | } |
1826 | | |
1827 | 3.00M | if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) { |
1828 | 275 | if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) { |
1829 | 92 | ZVAL_NULL(arg); |
1830 | 92 | Z_OBJ_P(arg) = NULL; |
1831 | 92 | return FAILURE; |
1832 | 92 | } |
1833 | 275 | } |
1834 | | |
1835 | 3.00M | if (class_type->create_object == NULL) { |
1836 | 323k | zend_object *obj = zend_objects_new(class_type); |
1837 | | |
1838 | 323k | ZVAL_OBJ(arg, obj); |
1839 | 323k | if (properties) { |
1840 | 0 | object_properties_init_ex(obj, properties); |
1841 | 323k | } else { |
1842 | 323k | _object_properties_init(obj, class_type); |
1843 | 323k | } |
1844 | 2.67M | } else { |
1845 | 2.67M | ZVAL_OBJ(arg, class_type->create_object(class_type)); |
1846 | 2.67M | } |
1847 | 3.00M | return SUCCESS; |
1848 | 3.00M | } |
1849 | | /* }}} */ |
1850 | | |
1851 | | ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */ |
1852 | 0 | { |
1853 | 0 | return _object_and_properties_init(arg, class_type, properties); |
1854 | 0 | } |
1855 | | /* }}} */ |
1856 | | |
1857 | | ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */ |
1858 | 3.00M | { |
1859 | 3.00M | return _object_and_properties_init(arg, class_type, NULL); |
1860 | 3.00M | } |
1861 | | /* }}} */ |
1862 | | |
1863 | | ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *class_type, uint32_t param_count, zval *params, HashTable *named_params) /* {{{ */ |
1864 | 573 | { |
1865 | 573 | zend_result status = _object_and_properties_init(arg, class_type, NULL); |
1866 | 573 | if (UNEXPECTED(status == FAILURE)) { |
1867 | 0 | ZVAL_UNDEF(arg); |
1868 | 0 | return FAILURE; |
1869 | 0 | } |
1870 | 573 | zend_object *obj = Z_OBJ_P(arg); |
1871 | 573 | zend_function *constructor = obj->handlers->get_constructor(obj); |
1872 | 573 | if (constructor == NULL) { |
1873 | | /* The constructor can be NULL for 2 different reasons: |
1874 | | * - It is not defined |
1875 | | * - We are not allowed to call the constructor (e.g. private, or internal opaque class) |
1876 | | * and an exception has been thrown |
1877 | | * in the former case, we are (mostly) done and the object is initialized, |
1878 | | * in the latter we need to destroy the object as initialization failed |
1879 | | */ |
1880 | 88 | if (UNEXPECTED(EG(exception))) { |
1881 | 5 | zval_ptr_dtor(arg); |
1882 | 5 | ZVAL_UNDEF(arg); |
1883 | 5 | return FAILURE; |
1884 | 5 | } |
1885 | | |
1886 | | /* Surprisingly, this is the only case where internal classes will allow to pass extra arguments |
1887 | | * However, if there are named arguments (and it is not empty), |
1888 | | * an Error must be thrown to be consistent with new ClassName() */ |
1889 | 83 | if (UNEXPECTED(named_params != NULL && zend_hash_num_elements(named_params) != 0)) { |
1890 | | /* Throw standard Error */ |
1891 | 5 | zend_string *arg_name = NULL; |
1892 | 5 | zend_hash_get_current_key(named_params, &arg_name, /* num_index */ NULL); |
1893 | 5 | ZEND_ASSERT(arg_name != NULL); |
1894 | 5 | zend_throw_error(NULL, "Unknown named parameter $%s", ZSTR_VAL(arg_name)); |
1895 | | /* Do not call destructor, free object, and set arg to IS_UNDEF */ |
1896 | 5 | zend_object_store_ctor_failed(obj); |
1897 | 5 | zval_ptr_dtor(arg); |
1898 | 5 | ZVAL_UNDEF(arg); |
1899 | 5 | return FAILURE; |
1900 | 78 | } else { |
1901 | 78 | return SUCCESS; |
1902 | 78 | } |
1903 | 83 | } |
1904 | | /* A constructor should not return a value, however if an exception is thrown |
1905 | | * zend_call_known_function() will set the retval to IS_UNDEF */ |
1906 | 485 | zval retval; |
1907 | 485 | zend_call_known_function( |
1908 | 485 | constructor, |
1909 | 485 | obj, |
1910 | 485 | class_type, |
1911 | 485 | &retval, |
1912 | 485 | param_count, |
1913 | 485 | params, |
1914 | 485 | named_params |
1915 | 485 | ); |
1916 | 485 | if (Z_TYPE(retval) == IS_UNDEF) { |
1917 | | /* Do not call destructor, free object, and set arg to IS_UNDEF */ |
1918 | 46 | zend_object_store_ctor_failed(obj); |
1919 | 46 | zval_ptr_dtor(arg); |
1920 | 46 | ZVAL_UNDEF(arg); |
1921 | 46 | return FAILURE; |
1922 | 439 | } else { |
1923 | | /* Unlikely, but user constructors may return any value they want */ |
1924 | 439 | zval_ptr_dtor(&retval); |
1925 | 439 | return SUCCESS; |
1926 | 439 | } |
1927 | 485 | } |
1928 | | /* }}} */ |
1929 | | |
1930 | | ZEND_API void object_init(zval *arg) /* {{{ */ |
1931 | 1.06M | { |
1932 | 1.06M | ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def)); |
1933 | 1.06M | } |
1934 | | /* }}} */ |
1935 | | |
1936 | | ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */ |
1937 | 35.1k | { |
1938 | 35.1k | zval tmp; |
1939 | | |
1940 | 35.1k | ZVAL_LONG(&tmp, n); |
1941 | 35.1k | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
1942 | 35.1k | } |
1943 | | /* }}} */ |
1944 | | |
1945 | | ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */ |
1946 | 29.4k | { |
1947 | 29.4k | zval tmp; |
1948 | | |
1949 | 29.4k | ZVAL_NULL(&tmp); |
1950 | 29.4k | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
1951 | 29.4k | } |
1952 | | /* }}} */ |
1953 | | |
1954 | | ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b) /* {{{ */ |
1955 | 1.13k | { |
1956 | 1.13k | zval tmp; |
1957 | | |
1958 | 1.13k | ZVAL_BOOL(&tmp, b); |
1959 | 1.13k | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
1960 | 1.13k | } |
1961 | | /* }}} */ |
1962 | | |
1963 | | ZEND_API void add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */ |
1964 | 0 | { |
1965 | 0 | zval tmp; |
1966 | |
|
1967 | 0 | ZVAL_RES(&tmp, r); |
1968 | 0 | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
1969 | 0 | } |
1970 | | /* }}} */ |
1971 | | |
1972 | | ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */ |
1973 | 8.69k | { |
1974 | 8.69k | zval tmp; |
1975 | | |
1976 | 8.69k | ZVAL_DOUBLE(&tmp, d); |
1977 | 8.69k | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
1978 | 8.69k | } |
1979 | | /* }}} */ |
1980 | | |
1981 | | ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */ |
1982 | 12 | { |
1983 | 12 | zval tmp; |
1984 | | |
1985 | 12 | ZVAL_STR(&tmp, str); |
1986 | 12 | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
1987 | 12 | } |
1988 | | /* }}} */ |
1989 | | |
1990 | | ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */ |
1991 | 20.0k | { |
1992 | 20.0k | zval tmp; |
1993 | | |
1994 | 20.0k | ZVAL_STRING(&tmp, str); |
1995 | 20.0k | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
1996 | 20.0k | } |
1997 | | /* }}} */ |
1998 | | |
1999 | | ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */ |
2000 | 37.0k | { |
2001 | 37.0k | zval tmp; |
2002 | | |
2003 | 37.0k | ZVAL_STRINGL(&tmp, str, length); |
2004 | 37.0k | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
2005 | 37.0k | } |
2006 | | /* }}} */ |
2007 | | |
2008 | | ZEND_API void add_assoc_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */ |
2009 | 0 | { |
2010 | 0 | zval tmp; |
2011 | |
|
2012 | 0 | ZVAL_ARR(&tmp, arr); |
2013 | 0 | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
2014 | 0 | } |
2015 | | /* }}} */ |
2016 | | |
2017 | | ZEND_API void add_assoc_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */ |
2018 | 291 | { |
2019 | 291 | zval tmp; |
2020 | | |
2021 | 291 | ZVAL_OBJ(&tmp, obj); |
2022 | 291 | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
2023 | 291 | } |
2024 | | /* }}} */ |
2025 | | |
2026 | | ZEND_API void add_assoc_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */ |
2027 | 0 | { |
2028 | 0 | zval tmp; |
2029 | |
|
2030 | 0 | ZVAL_REF(&tmp, ref); |
2031 | 0 | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp); |
2032 | 0 | } |
2033 | | /* }}} */ |
2034 | | |
2035 | | ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */ |
2036 | 38.7k | { |
2037 | 38.7k | zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value); |
2038 | 38.7k | } |
2039 | | /* }}} */ |
2040 | | |
2041 | | ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */ |
2042 | 3.66M | { |
2043 | 3.66M | zval tmp; |
2044 | | |
2045 | 3.66M | ZVAL_LONG(&tmp, n); |
2046 | 3.66M | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2047 | 3.66M | } |
2048 | | /* }}} */ |
2049 | | |
2050 | | ZEND_API void add_index_null(zval *arg, zend_ulong index) /* {{{ */ |
2051 | 0 | { |
2052 | 0 | zval tmp; |
2053 | |
|
2054 | 0 | ZVAL_NULL(&tmp); |
2055 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2056 | 0 | } |
2057 | | /* }}} */ |
2058 | | |
2059 | | ZEND_API void add_index_bool(zval *arg, zend_ulong index, bool b) /* {{{ */ |
2060 | 0 | { |
2061 | 0 | zval tmp; |
2062 | |
|
2063 | 0 | ZVAL_BOOL(&tmp, b); |
2064 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2065 | 0 | } |
2066 | | /* }}} */ |
2067 | | |
2068 | | ZEND_API void add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */ |
2069 | 0 | { |
2070 | 0 | zval tmp; |
2071 | |
|
2072 | 0 | ZVAL_RES(&tmp, r); |
2073 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2074 | 0 | } |
2075 | | /* }}} */ |
2076 | | |
2077 | | ZEND_API void add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */ |
2078 | 348k | { |
2079 | 348k | zval tmp; |
2080 | | |
2081 | 348k | ZVAL_DOUBLE(&tmp, d); |
2082 | 348k | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2083 | 348k | } |
2084 | | /* }}} */ |
2085 | | |
2086 | | ZEND_API void add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */ |
2087 | 0 | { |
2088 | 0 | zval tmp; |
2089 | |
|
2090 | 0 | ZVAL_STR(&tmp, str); |
2091 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2092 | 0 | } |
2093 | | /* }}} */ |
2094 | | |
2095 | | ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */ |
2096 | 294k | { |
2097 | 294k | zval tmp; |
2098 | | |
2099 | 294k | ZVAL_STRING(&tmp, str); |
2100 | 294k | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2101 | 294k | } |
2102 | | /* }}} */ |
2103 | | |
2104 | | ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */ |
2105 | 0 | { |
2106 | 0 | zval tmp; |
2107 | |
|
2108 | 0 | ZVAL_STRINGL(&tmp, str, length); |
2109 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2110 | 0 | } |
2111 | | /* }}} */ |
2112 | | |
2113 | | ZEND_API void add_index_array(zval *arg, zend_ulong index, zend_array *arr) /* {{{ */ |
2114 | 0 | { |
2115 | 0 | zval tmp; |
2116 | |
|
2117 | 0 | ZVAL_ARR(&tmp, arr); |
2118 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2119 | 0 | } |
2120 | | /* }}} */ |
2121 | | |
2122 | | ZEND_API void add_index_object(zval *arg, zend_ulong index, zend_object *obj) /* {{{ */ |
2123 | 0 | { |
2124 | 0 | zval tmp; |
2125 | |
|
2126 | 0 | ZVAL_OBJ(&tmp, obj); |
2127 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2128 | 0 | } |
2129 | | /* }}} */ |
2130 | | |
2131 | | ZEND_API void add_index_reference(zval *arg, zend_ulong index, zend_reference *ref) /* {{{ */ |
2132 | 0 | { |
2133 | 0 | zval tmp; |
2134 | |
|
2135 | 0 | ZVAL_REF(&tmp, ref); |
2136 | 0 | zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp); |
2137 | 0 | } |
2138 | | /* }}} */ |
2139 | | |
2140 | | ZEND_API zend_result add_next_index_long(zval *arg, zend_long n) /* {{{ */ |
2141 | 0 | { |
2142 | 0 | zval tmp; |
2143 | |
|
2144 | 0 | ZVAL_LONG(&tmp, n); |
2145 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2146 | 0 | } |
2147 | | /* }}} */ |
2148 | | |
2149 | | ZEND_API zend_result add_next_index_null(zval *arg) /* {{{ */ |
2150 | 0 | { |
2151 | 0 | zval tmp; |
2152 | |
|
2153 | 0 | ZVAL_NULL(&tmp); |
2154 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2155 | 0 | } |
2156 | | /* }}} */ |
2157 | | |
2158 | | ZEND_API zend_result add_next_index_bool(zval *arg, bool b) /* {{{ */ |
2159 | 0 | { |
2160 | 0 | zval tmp; |
2161 | |
|
2162 | 0 | ZVAL_BOOL(&tmp, b); |
2163 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2164 | 0 | } |
2165 | | /* }}} */ |
2166 | | |
2167 | | ZEND_API zend_result add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */ |
2168 | 0 | { |
2169 | 0 | zval tmp; |
2170 | |
|
2171 | 0 | ZVAL_RES(&tmp, r); |
2172 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2173 | 0 | } |
2174 | | /* }}} */ |
2175 | | |
2176 | | ZEND_API zend_result add_next_index_double(zval *arg, double d) /* {{{ */ |
2177 | 0 | { |
2178 | 0 | zval tmp; |
2179 | |
|
2180 | 0 | ZVAL_DOUBLE(&tmp, d); |
2181 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2182 | 0 | } |
2183 | | /* }}} */ |
2184 | | |
2185 | | ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str) /* {{{ */ |
2186 | 31.0k | { |
2187 | 31.0k | zval tmp; |
2188 | | |
2189 | 31.0k | ZVAL_STR(&tmp, str); |
2190 | 31.0k | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2191 | 31.0k | } |
2192 | | /* }}} */ |
2193 | | |
2194 | | ZEND_API zend_result add_next_index_string(zval *arg, const char *str) /* {{{ */ |
2195 | 838 | { |
2196 | 838 | zval tmp; |
2197 | | |
2198 | 838 | ZVAL_STRING(&tmp, str); |
2199 | 838 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2200 | 838 | } |
2201 | | /* }}} */ |
2202 | | |
2203 | | ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */ |
2204 | 0 | { |
2205 | 0 | zval tmp; |
2206 | |
|
2207 | 0 | ZVAL_STRINGL(&tmp, str, length); |
2208 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2209 | 0 | } |
2210 | | /* }}} */ |
2211 | | |
2212 | | ZEND_API zend_result add_next_index_array(zval *arg, zend_array *arr) /* {{{ */ |
2213 | 0 | { |
2214 | 0 | zval tmp; |
2215 | |
|
2216 | 0 | ZVAL_ARR(&tmp, arr); |
2217 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2218 | 0 | } |
2219 | | /* }}} */ |
2220 | | |
2221 | | ZEND_API zend_result add_next_index_object(zval *arg, zend_object *obj) /* {{{ */ |
2222 | 0 | { |
2223 | 0 | zval tmp; |
2224 | |
|
2225 | 0 | ZVAL_OBJ(&tmp, obj); |
2226 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2227 | 0 | } |
2228 | | /* }}} */ |
2229 | | |
2230 | | ZEND_API zend_result add_next_index_reference(zval *arg, zend_reference *ref) /* {{{ */ |
2231 | 0 | { |
2232 | 0 | zval tmp; |
2233 | |
|
2234 | 0 | ZVAL_REF(&tmp, ref); |
2235 | 0 | return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE; |
2236 | 0 | } |
2237 | | /* }}} */ |
2238 | | |
2239 | | ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */ |
2240 | 569 | { |
2241 | 569 | zval *result; |
2242 | | |
2243 | 569 | switch (Z_TYPE_P(key)) { |
2244 | 47 | case IS_STRING: |
2245 | 47 | result = zend_symtable_update(ht, Z_STR_P(key), value); |
2246 | 47 | break; |
2247 | 18 | case IS_NULL: |
2248 | 18 | result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value); |
2249 | 18 | break; |
2250 | 0 | case IS_RESOURCE: |
2251 | 0 | zend_use_resource_as_offset(key); |
2252 | 0 | result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value); |
2253 | 0 | break; |
2254 | 15 | case IS_FALSE: |
2255 | 15 | result = zend_hash_index_update(ht, 0, value); |
2256 | 15 | break; |
2257 | 12 | case IS_TRUE: |
2258 | 12 | result = zend_hash_index_update(ht, 1, value); |
2259 | 12 | break; |
2260 | 446 | case IS_LONG: |
2261 | 446 | result = zend_hash_index_update(ht, Z_LVAL_P(key), value); |
2262 | 446 | break; |
2263 | 21 | case IS_DOUBLE: |
2264 | 21 | result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value); |
2265 | 21 | break; |
2266 | 10 | default: |
2267 | 10 | zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), key, BP_VAR_W); |
2268 | 10 | result = NULL; |
2269 | 569 | } |
2270 | | |
2271 | 569 | if (result) { |
2272 | 559 | Z_TRY_ADDREF_P(result); |
2273 | 559 | return SUCCESS; |
2274 | 559 | } else { |
2275 | 10 | return FAILURE; |
2276 | 10 | } |
2277 | 569 | } |
2278 | | /* }}} */ |
2279 | | |
2280 | | ZEND_API void add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */ |
2281 | 0 | { |
2282 | 0 | zval tmp; |
2283 | |
|
2284 | 0 | ZVAL_LONG(&tmp, n); |
2285 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2286 | 0 | } |
2287 | | /* }}} */ |
2288 | | |
2289 | | ZEND_API void add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */ |
2290 | 0 | { |
2291 | 0 | zval tmp; |
2292 | |
|
2293 | 0 | ZVAL_BOOL(&tmp, b); |
2294 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2295 | 0 | } |
2296 | | /* }}} */ |
2297 | | |
2298 | | ZEND_API void add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */ |
2299 | 101 | { |
2300 | 101 | zval tmp; |
2301 | | |
2302 | 101 | ZVAL_NULL(&tmp); |
2303 | 101 | add_property_zval_ex(arg, key, key_len, &tmp); |
2304 | 101 | } |
2305 | | /* }}} */ |
2306 | | |
2307 | | ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */ |
2308 | 147 | { |
2309 | 147 | zval tmp; |
2310 | | |
2311 | 147 | ZVAL_RES(&tmp, r); |
2312 | 147 | add_property_zval_ex(arg, key, key_len, &tmp); |
2313 | 147 | zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */ |
2314 | 147 | } |
2315 | | /* }}} */ |
2316 | | |
2317 | | ZEND_API void add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */ |
2318 | 0 | { |
2319 | 0 | zval tmp; |
2320 | |
|
2321 | 0 | ZVAL_DOUBLE(&tmp, d); |
2322 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2323 | 0 | } |
2324 | | /* }}} */ |
2325 | | |
2326 | | ZEND_API void add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */ |
2327 | 0 | { |
2328 | 0 | zval tmp; |
2329 | |
|
2330 | 0 | ZVAL_STR(&tmp, str); |
2331 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2332 | 0 | zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */ |
2333 | 0 | } |
2334 | | /* }}} */ |
2335 | | |
2336 | | ZEND_API void add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */ |
2337 | 20 | { |
2338 | 20 | zval tmp; |
2339 | | |
2340 | 20 | ZVAL_STRING(&tmp, str); |
2341 | 20 | add_property_zval_ex(arg, key, key_len, &tmp); |
2342 | 20 | zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */ |
2343 | 20 | } |
2344 | | /* }}} */ |
2345 | | |
2346 | | ZEND_API void add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */ |
2347 | 0 | { |
2348 | 0 | zval tmp; |
2349 | |
|
2350 | 0 | ZVAL_STRINGL(&tmp, str, length); |
2351 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2352 | 0 | zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */ |
2353 | 0 | } |
2354 | | /* }}} */ |
2355 | | |
2356 | | ZEND_API void add_property_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */ |
2357 | 0 | { |
2358 | 0 | zval tmp; |
2359 | |
|
2360 | 0 | ZVAL_ARR(&tmp, arr); |
2361 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2362 | 0 | zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */ |
2363 | 0 | } |
2364 | | /* }}} */ |
2365 | | |
2366 | | ZEND_API void add_property_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */ |
2367 | 0 | { |
2368 | 0 | zval tmp; |
2369 | |
|
2370 | 0 | ZVAL_OBJ(&tmp, obj); |
2371 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2372 | 0 | zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */ |
2373 | 0 | } |
2374 | | /* }}} */ |
2375 | | |
2376 | | ZEND_API void add_property_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */ |
2377 | 0 | { |
2378 | 0 | zval tmp; |
2379 | |
|
2380 | 0 | ZVAL_REF(&tmp, ref); |
2381 | 0 | add_property_zval_ex(arg, key, key_len, &tmp); |
2382 | 0 | zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */ |
2383 | 0 | } |
2384 | | /* }}} */ |
2385 | | |
2386 | | ZEND_API void add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */ |
2387 | 268 | { |
2388 | 268 | zend_string *str; |
2389 | | |
2390 | 268 | str = zend_string_init(key, key_len, 0); |
2391 | 268 | Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL); |
2392 | 268 | zend_string_release_ex(str, 0); |
2393 | 268 | } |
2394 | | /* }}} */ |
2395 | | |
2396 | | ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module) /* {{{ */ |
2397 | 196 | { |
2398 | 196 | size_t name_len; |
2399 | 196 | zend_string *lcname; |
2400 | | |
2401 | 196 | if (module->module_started) { |
2402 | 0 | return SUCCESS; |
2403 | 0 | } |
2404 | 196 | module->module_started = 1; |
2405 | | |
2406 | | /* Check module dependencies */ |
2407 | 196 | if (module->deps) { |
2408 | 64 | const zend_module_dep *dep = module->deps; |
2409 | | |
2410 | 144 | while (dep->name) { |
2411 | 80 | if (dep->type == MODULE_DEP_REQUIRED) { |
2412 | 48 | zend_module_entry *req_mod; |
2413 | | |
2414 | 48 | name_len = strlen(dep->name); |
2415 | 48 | lcname = zend_string_alloc(name_len, 0); |
2416 | 48 | zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len); |
2417 | | |
2418 | 48 | if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) { |
2419 | 0 | zend_string_efree(lcname); |
2420 | | /* TODO: Check version relationship */ |
2421 | 0 | zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because required module \"%s\" is not loaded", module->name, dep->name); |
2422 | 0 | module->module_started = 0; |
2423 | 0 | return FAILURE; |
2424 | 0 | } |
2425 | 48 | zend_string_efree(lcname); |
2426 | 48 | } |
2427 | 80 | ++dep; |
2428 | 80 | } |
2429 | 64 | } |
2430 | | |
2431 | | /* Initialize module globals */ |
2432 | 196 | if (module->globals_size) { |
2433 | | #ifdef ZTS |
2434 | | ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor); |
2435 | | #else |
2436 | 96 | if (module->globals_ctor) { |
2437 | 80 | module->globals_ctor(module->globals_ptr); |
2438 | 80 | } |
2439 | 96 | #endif |
2440 | 96 | } |
2441 | 196 | if (module->module_startup_func) { |
2442 | 196 | EG(current_module) = module; |
2443 | 196 | if (module->module_startup_func(module->type, module->module_number)==FAILURE) { |
2444 | 0 | zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name); |
2445 | 0 | EG(current_module) = NULL; |
2446 | 0 | return FAILURE; |
2447 | 0 | } |
2448 | 196 | EG(current_module) = NULL; |
2449 | 196 | } |
2450 | 196 | return SUCCESS; |
2451 | 196 | } |
2452 | | /* }}} */ |
2453 | | |
2454 | | static int zend_startup_module_zval(zval *zv) /* {{{ */ |
2455 | 192 | { |
2456 | 192 | zend_module_entry *module = Z_PTR_P(zv); |
2457 | | |
2458 | 192 | return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE; |
2459 | 192 | } |
2460 | | /* }}} */ |
2461 | | |
2462 | | static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */ |
2463 | 16 | { |
2464 | 16 | Bucket *b1 = base; |
2465 | 16 | Bucket *b2; |
2466 | 16 | Bucket *end = b1 + count; |
2467 | 16 | Bucket tmp; |
2468 | 16 | zend_module_entry *m, *r; |
2469 | | |
2470 | 208 | while (b1 < end) { |
2471 | 208 | try_again: |
2472 | 208 | m = (zend_module_entry*)Z_PTR(b1->val); |
2473 | 208 | if (!m->module_started && m->deps) { |
2474 | 80 | const zend_module_dep *dep = m->deps; |
2475 | 160 | while (dep->name) { |
2476 | 96 | if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) { |
2477 | 96 | b2 = b1 + 1; |
2478 | 384 | while (b2 < end) { |
2479 | 304 | r = (zend_module_entry*)Z_PTR(b2->val); |
2480 | 304 | if (strcasecmp(dep->name, r->name) == 0) { |
2481 | 16 | tmp = *b1; |
2482 | 16 | *b1 = *b2; |
2483 | 16 | *b2 = tmp; |
2484 | 16 | goto try_again; |
2485 | 16 | } |
2486 | 288 | b2++; |
2487 | 288 | } |
2488 | 96 | } |
2489 | 80 | dep++; |
2490 | 80 | } |
2491 | 80 | } |
2492 | 192 | b1++; |
2493 | 192 | } |
2494 | 16 | } |
2495 | | /* }}} */ |
2496 | | |
2497 | | ZEND_API void zend_collect_module_handlers(void) /* {{{ */ |
2498 | 16 | { |
2499 | 16 | zend_module_entry *module; |
2500 | 16 | int startup_count = 0; |
2501 | 16 | int shutdown_count = 0; |
2502 | 16 | int post_deactivate_count = 0; |
2503 | 16 | int dl_loaded_count = 0; |
2504 | 16 | zend_class_entry *ce; |
2505 | 16 | int class_count = 0; |
2506 | | |
2507 | | /* Collect extensions with request startup/shutdown handlers */ |
2508 | 424 | ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { |
2509 | 424 | if (module->request_startup_func) { |
2510 | 116 | startup_count++; |
2511 | 116 | } |
2512 | 424 | if (module->request_shutdown_func) { |
2513 | 80 | shutdown_count++; |
2514 | 80 | } |
2515 | 424 | if (module->post_deactivate_func) { |
2516 | 20 | post_deactivate_count++; |
2517 | 20 | } |
2518 | 424 | if (module->handle) { |
2519 | 0 | dl_loaded_count++; |
2520 | 0 | } |
2521 | 424 | } ZEND_HASH_FOREACH_END(); |
2522 | 16 | module_request_startup_handlers = (zend_module_entry**)realloc( |
2523 | 16 | module_request_startup_handlers, |
2524 | 16 | sizeof(zend_module_entry*) * |
2525 | 16 | (startup_count + 1 + |
2526 | 16 | shutdown_count + 1 + |
2527 | 16 | post_deactivate_count + 1)); |
2528 | 16 | module_request_startup_handlers[startup_count] = NULL; |
2529 | 16 | module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1; |
2530 | 16 | module_request_shutdown_handlers[shutdown_count] = NULL; |
2531 | 16 | module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1; |
2532 | 16 | module_post_deactivate_handlers[post_deactivate_count] = NULL; |
2533 | | /* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */ |
2534 | 16 | modules_dl_loaded = realloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1)); |
2535 | 16 | modules_dl_loaded[dl_loaded_count] = NULL; |
2536 | 16 | startup_count = 0; |
2537 | | |
2538 | 424 | ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { |
2539 | 424 | if (module->request_startup_func) { |
2540 | 116 | module_request_startup_handlers[startup_count++] = module; |
2541 | 116 | } |
2542 | 424 | if (module->request_shutdown_func) { |
2543 | 80 | module_request_shutdown_handlers[--shutdown_count] = module; |
2544 | 80 | } |
2545 | 424 | if (module->post_deactivate_func) { |
2546 | 20 | module_post_deactivate_handlers[--post_deactivate_count] = module; |
2547 | 20 | } |
2548 | 424 | if (module->handle) { |
2549 | 0 | modules_dl_loaded[--dl_loaded_count] = module; |
2550 | 0 | } |
2551 | 424 | } ZEND_HASH_FOREACH_END(); |
2552 | | |
2553 | | /* Collect internal classes with static members */ |
2554 | 5.24k | ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) { |
2555 | 5.24k | if (ce->type == ZEND_INTERNAL_CLASS && |
2556 | 2.60k | ce->default_static_members_count > 0) { |
2557 | 0 | class_count++; |
2558 | 0 | } |
2559 | 5.24k | } ZEND_HASH_FOREACH_END(); |
2560 | | |
2561 | 16 | class_cleanup_handlers = (zend_class_entry**)realloc( |
2562 | 16 | class_cleanup_handlers, |
2563 | 16 | sizeof(zend_class_entry*) * |
2564 | 16 | (class_count + 1)); |
2565 | 16 | class_cleanup_handlers[class_count] = NULL; |
2566 | | |
2567 | 16 | if (class_count) { |
2568 | 0 | ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) { |
2569 | 0 | if (ce->type == ZEND_INTERNAL_CLASS && |
2570 | 0 | ce->default_static_members_count > 0) { |
2571 | 0 | class_cleanup_handlers[--class_count] = ce; |
2572 | 0 | } |
2573 | 0 | } ZEND_HASH_FOREACH_END(); |
2574 | 0 | } |
2575 | 16 | } |
2576 | | /* }}} */ |
2577 | | |
2578 | | ZEND_API void zend_startup_modules(void) /* {{{ */ |
2579 | 16 | { |
2580 | 16 | zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0); |
2581 | 16 | zend_hash_apply(&module_registry, zend_startup_module_zval); |
2582 | 16 | } |
2583 | | /* }}} */ |
2584 | | |
2585 | | ZEND_API void zend_destroy_modules(void) /* {{{ */ |
2586 | 0 | { |
2587 | 0 | free(class_cleanup_handlers); |
2588 | 0 | class_cleanup_handlers = NULL; |
2589 | 0 | free(module_request_startup_handlers); |
2590 | 0 | module_request_startup_handlers = NULL; |
2591 | 0 | zend_hash_graceful_reverse_destroy(&module_registry); |
2592 | 0 | } |
2593 | | /* }}} */ |
2594 | | |
2595 | | ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, int module_type) /* {{{ */ |
2596 | 196 | { |
2597 | 196 | size_t name_len; |
2598 | 196 | zend_string *lcname; |
2599 | 196 | zend_module_entry *module_ptr; |
2600 | | |
2601 | 196 | if (!module) { |
2602 | 0 | return NULL; |
2603 | 0 | } |
2604 | | |
2605 | | #if 0 |
2606 | | zend_printf("%s: Registering module %d\n", module->name, module->module_number); |
2607 | | #endif |
2608 | | |
2609 | | /* Check module dependencies */ |
2610 | 196 | if (module->deps) { |
2611 | 64 | const zend_module_dep *dep = module->deps; |
2612 | | |
2613 | 144 | while (dep->name) { |
2614 | 80 | if (dep->type == MODULE_DEP_CONFLICTS) { |
2615 | 0 | name_len = strlen(dep->name); |
2616 | 0 | lcname = zend_string_alloc(name_len, 0); |
2617 | 0 | zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len); |
2618 | |
|
2619 | 0 | if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) { |
2620 | 0 | zend_string_efree(lcname); |
2621 | | /* TODO: Check version relationship */ |
2622 | 0 | zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because conflicting module \"%s\" is already loaded", module->name, dep->name); |
2623 | 0 | return NULL; |
2624 | 0 | } |
2625 | 0 | zend_string_efree(lcname); |
2626 | 0 | } |
2627 | 80 | ++dep; |
2628 | 80 | } |
2629 | 64 | } |
2630 | | |
2631 | 196 | name_len = strlen(module->name); |
2632 | 196 | lcname = zend_string_alloc(name_len, module_type == MODULE_PERSISTENT); |
2633 | 196 | zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len); |
2634 | | |
2635 | 196 | int module_number = zend_next_free_module(); |
2636 | | |
2637 | 196 | lcname = zend_new_interned_string(lcname); |
2638 | 196 | if ((module_ptr = zend_hash_add_ptr(&module_registry, lcname, module)) == NULL) { |
2639 | 0 | zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module->name); |
2640 | 0 | zend_string_release(lcname); |
2641 | 0 | return NULL; |
2642 | 0 | } |
2643 | 196 | module = module_ptr; |
2644 | 196 | EG(current_module) = module; |
2645 | | |
2646 | 196 | module->module_number = module_number; |
2647 | 196 | module->type = module_type; |
2648 | | |
2649 | 196 | if (module->functions && zend_register_functions(NULL, module->functions, NULL, module_type)==FAILURE) { |
2650 | 0 | zend_hash_del(&module_registry, lcname); |
2651 | 0 | zend_string_release(lcname); |
2652 | 0 | EG(current_module) = NULL; |
2653 | 0 | zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name); |
2654 | 0 | return NULL; |
2655 | 0 | } |
2656 | | |
2657 | 196 | EG(current_module) = NULL; |
2658 | 196 | zend_string_release(lcname); |
2659 | 196 | return module; |
2660 | 196 | } |
2661 | | /* }}} */ |
2662 | | |
2663 | | ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */ |
2664 | 180 | { |
2665 | 180 | return zend_register_module_ex(module, MODULE_PERSISTENT); |
2666 | 180 | } |
2667 | | /* }}} */ |
2668 | | |
2669 | | static void zend_check_magic_method_args( |
2670 | | uint32_t num_args, const zend_class_entry *ce, const zend_function *fptr, int error_type) |
2671 | 9.58k | { |
2672 | 9.58k | if (fptr->common.num_args != num_args) { |
2673 | 53 | if (num_args == 0) { |
2674 | 39 | zend_error(error_type, "Method %s::%s() cannot take arguments", |
2675 | 39 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2676 | 39 | } else if (num_args == 1) { |
2677 | 6 | zend_error(error_type, "Method %s::%s() must take exactly 1 argument", |
2678 | 6 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2679 | 8 | } else { |
2680 | 8 | zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments", |
2681 | 8 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args); |
2682 | 8 | } |
2683 | 53 | return; |
2684 | 53 | } |
2685 | 14.6k | for (uint32_t i = 0; i < num_args; i++) { |
2686 | 5.08k | if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) { |
2687 | 8 | zend_error(error_type, "Method %s::%s() cannot take arguments by reference", |
2688 | 8 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2689 | 8 | return; |
2690 | 8 | } |
2691 | 5.08k | } |
2692 | 9.53k | } |
2693 | | |
2694 | | static void zend_check_magic_method_arg_type(uint32_t arg_num, const zend_class_entry *ce, const zend_function *fptr, int error_type, int arg_type) |
2695 | 4.55k | { |
2696 | 4.55k | if ( |
2697 | 4.55k | ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type) |
2698 | 4.55k | && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type) |
2699 | 4.55k | ) { |
2700 | 52 | zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared", |
2701 | 52 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), |
2702 | 52 | arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name), |
2703 | 52 | ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type)))); |
2704 | 52 | } |
2705 | 4.55k | } |
2706 | | |
2707 | | static void zend_check_magic_method_return_type(const zend_class_entry *ce, const zend_function *fptr, int error_type, int return_type) |
2708 | 4.03k | { |
2709 | 4.03k | if (return_type == MAY_BE_VOID) { |
2710 | 1.77k | if (fptr->common.fn_flags & ZEND_ACC_NODISCARD) { |
2711 | 6 | zend_error_noreturn(error_type, "Method %s::%s cannot be #[\\NoDiscard]", ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2712 | 6 | } |
2713 | 1.77k | } |
2714 | | |
2715 | 4.02k | if (!(fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
2716 | | /* For backwards compatibility reasons, do not enforce the return type if it is not set. */ |
2717 | 1.04k | return; |
2718 | 1.04k | } |
2719 | | |
2720 | 2.98k | if (ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NEVER) { |
2721 | | /* It is always legal to specify the never type. */ |
2722 | 94 | return; |
2723 | 94 | } |
2724 | | |
2725 | 2.88k | bool is_complex_type = ZEND_TYPE_IS_COMPLEX(fptr->common.arg_info[-1].type); |
2726 | 2.88k | uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type; |
2727 | 2.88k | if (extra_types & MAY_BE_STATIC) { |
2728 | 25 | extra_types &= ~MAY_BE_STATIC; |
2729 | 25 | is_complex_type = true; |
2730 | 25 | } |
2731 | | |
2732 | 2.88k | if (extra_types || (is_complex_type && return_type != MAY_BE_OBJECT)) { |
2733 | 81 | zend_error(error_type, "%s::%s(): Return type must be %s when declared", |
2734 | 81 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), |
2735 | 81 | ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type)))); |
2736 | 81 | } |
2737 | 2.88k | } |
2738 | | |
2739 | | static void zend_check_magic_method_non_static( |
2740 | | const zend_class_entry *ce, const zend_function *fptr, int error_type) |
2741 | 13.5k | { |
2742 | 13.5k | if (fptr->common.fn_flags & ZEND_ACC_STATIC) { |
2743 | 41 | zend_error(error_type, "Method %s::%s() cannot be static", |
2744 | 41 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2745 | 41 | } |
2746 | 13.5k | } |
2747 | | |
2748 | | static void zend_check_magic_method_static( |
2749 | | const zend_class_entry *ce, const zend_function *fptr, int error_type) |
2750 | 717 | { |
2751 | 717 | if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) { |
2752 | 13 | zend_error(error_type, "Method %s::%s() must be static", |
2753 | 13 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2754 | 13 | } |
2755 | 717 | } |
2756 | | |
2757 | | static void zend_check_magic_method_public( |
2758 | | const zend_class_entry *ce, const zend_function *fptr, int error_type) |
2759 | 5.77k | { |
2760 | | // TODO: Remove this warning after adding proper visibility handling. |
2761 | 5.77k | if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) { |
2762 | 224 | zend_error(E_WARNING, "The magic method %s::%s() must have public visibility", |
2763 | 224 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2764 | 224 | } |
2765 | 5.77k | } |
2766 | | |
2767 | | static void zend_check_magic_method_no_return_type( |
2768 | | const zend_class_entry *ce, const zend_function *fptr, int error_type) |
2769 | 7.98k | { |
2770 | 7.98k | if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
2771 | 12 | zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type", |
2772 | 12 | ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2773 | 12 | } |
2774 | | |
2775 | 7.97k | if (fptr->common.fn_flags & ZEND_ACC_NODISCARD) { |
2776 | 6 | zend_error_noreturn(error_type, "Method %s::%s cannot be #[\\NoDiscard]", ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name)); |
2777 | 6 | } |
2778 | 7.97k | } |
2779 | | |
2780 | | ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */ |
2781 | 47.0k | { |
2782 | 47.0k | if (ZSTR_VAL(lcname)[0] != '_' |
2783 | 47.0k | || ZSTR_VAL(lcname)[1] != '_') { |
2784 | 31.3k | return; |
2785 | 31.3k | } |
2786 | | |
2787 | 15.6k | if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) { |
2788 | 4.50k | zend_check_magic_method_non_static(ce, fptr, error_type); |
2789 | 4.50k | zend_check_magic_method_no_return_type(ce, fptr, error_type); |
2790 | 11.1k | } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) { |
2791 | 3.49k | zend_check_magic_method_args(0, ce, fptr, error_type); |
2792 | 3.49k | zend_check_magic_method_non_static(ce, fptr, error_type); |
2793 | 3.49k | zend_check_magic_method_no_return_type(ce, fptr, error_type); |
2794 | 7.69k | } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) { |
2795 | 464 | zend_check_magic_method_args(0, ce, fptr, error_type); |
2796 | 464 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2797 | 464 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID); |
2798 | 7.22k | } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) { |
2799 | 839 | zend_check_magic_method_args(1, ce, fptr, error_type); |
2800 | 839 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2801 | 839 | zend_check_magic_method_public(ce, fptr, error_type); |
2802 | 839 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING); |
2803 | 6.38k | } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) { |
2804 | 466 | zend_check_magic_method_args(2, ce, fptr, error_type); |
2805 | 466 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2806 | 466 | zend_check_magic_method_public(ce, fptr, error_type); |
2807 | 466 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING); |
2808 | 466 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID); |
2809 | 5.92k | } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) { |
2810 | 268 | zend_check_magic_method_args(1, ce, fptr, error_type); |
2811 | 268 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2812 | 268 | zend_check_magic_method_public(ce, fptr, error_type); |
2813 | 268 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING); |
2814 | 268 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID); |
2815 | 5.65k | } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) { |
2816 | 256 | zend_check_magic_method_args(1, ce, fptr, error_type); |
2817 | 256 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2818 | 256 | zend_check_magic_method_public(ce, fptr, error_type); |
2819 | 256 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING); |
2820 | 256 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL); |
2821 | 5.39k | } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) { |
2822 | 579 | zend_check_magic_method_args(2, ce, fptr, error_type); |
2823 | 579 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2824 | 579 | zend_check_magic_method_public(ce, fptr, error_type); |
2825 | 579 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING); |
2826 | 579 | zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY); |
2827 | 4.82k | } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) { |
2828 | 534 | zend_check_magic_method_args(2, ce, fptr, error_type); |
2829 | 534 | zend_check_magic_method_static(ce, fptr, error_type); |
2830 | 534 | zend_check_magic_method_public(ce, fptr, error_type); |
2831 | 534 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING); |
2832 | 534 | zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY); |
2833 | 4.28k | } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) { |
2834 | 911 | zend_check_magic_method_args(0, ce, fptr, error_type); |
2835 | 911 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2836 | 911 | zend_check_magic_method_public(ce, fptr, error_type); |
2837 | 911 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_STRING); |
2838 | 3.37k | } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) { |
2839 | 429 | zend_check_magic_method_args(0, ce, fptr, error_type); |
2840 | 429 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2841 | 429 | zend_check_magic_method_public(ce, fptr, error_type); |
2842 | 429 | zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL)); |
2843 | 2.94k | } else if (zend_string_equals_literal(lcname, "__serialize")) { |
2844 | 347 | zend_check_magic_method_args(0, ce, fptr, error_type); |
2845 | 347 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2846 | 347 | zend_check_magic_method_public(ce, fptr, error_type); |
2847 | 347 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY); |
2848 | 2.59k | } else if (zend_string_equals_literal(lcname, "__unserialize")) { |
2849 | 401 | zend_check_magic_method_args(1, ce, fptr, error_type); |
2850 | 401 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2851 | 401 | zend_check_magic_method_public(ce, fptr, error_type); |
2852 | 401 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY); |
2853 | 401 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID); |
2854 | 2.19k | } else if (zend_string_equals_literal(lcname, "__set_state")) { |
2855 | 183 | zend_check_magic_method_args(1, ce, fptr, error_type); |
2856 | 183 | zend_check_magic_method_static(ce, fptr, error_type); |
2857 | 183 | zend_check_magic_method_public(ce, fptr, error_type); |
2858 | 183 | zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY); |
2859 | 183 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT); |
2860 | 2.01k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) { |
2861 | 238 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2862 | 238 | zend_check_magic_method_public(ce, fptr, error_type); |
2863 | 1.77k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SLEEP))) { |
2864 | 175 | zend_check_magic_method_args(0, ce, fptr, error_type); |
2865 | 175 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2866 | 175 | zend_check_magic_method_public(ce, fptr, error_type); |
2867 | 175 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY); |
2868 | 1.60k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_WAKEUP))) { |
2869 | 232 | zend_check_magic_method_args(0, ce, fptr, error_type); |
2870 | 232 | zend_check_magic_method_non_static(ce, fptr, error_type); |
2871 | 232 | zend_check_magic_method_public(ce, fptr, error_type); |
2872 | 232 | zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID); |
2873 | 232 | } |
2874 | 15.6k | } |
2875 | | /* }}} */ |
2876 | | |
2877 | | ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, zend_string *lcname) |
2878 | 48.8k | { |
2879 | 48.8k | if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') { |
2880 | | /* pass */ |
2881 | 32.9k | } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) { |
2882 | 483 | ce->clone = fptr; |
2883 | 15.4k | } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) { |
2884 | 4.61k | ce->constructor = fptr; |
2885 | 4.61k | ce->constructor->common.fn_flags |= ZEND_ACC_CTOR; |
2886 | 10.8k | } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) { |
2887 | 3.51k | ce->destructor = fptr; |
2888 | 7.33k | } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) { |
2889 | 855 | ce->__get = fptr; |
2890 | 855 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2891 | 6.48k | } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) { |
2892 | 470 | ce->__set = fptr; |
2893 | 470 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2894 | 6.01k | } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) { |
2895 | 579 | ce->__call = fptr; |
2896 | 5.43k | } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) { |
2897 | 268 | ce->__unset = fptr; |
2898 | 268 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2899 | 5.16k | } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) { |
2900 | 268 | ce->__isset = fptr; |
2901 | 268 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2902 | 4.89k | } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) { |
2903 | 548 | ce->__callstatic = fptr; |
2904 | 4.35k | } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) { |
2905 | 939 | ce->__tostring = fptr; |
2906 | 3.41k | } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) { |
2907 | 429 | ce->__debugInfo = fptr; |
2908 | 429 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2909 | 2.98k | } else if (zend_string_equals_literal(lcname, "__serialize")) { |
2910 | 347 | ce->__serialize = fptr; |
2911 | 2.63k | } else if (zend_string_equals_literal(lcname, "__unserialize")) { |
2912 | 401 | ce->__unserialize = fptr; |
2913 | 401 | } |
2914 | 48.8k | } |
2915 | | |
2916 | | ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arg_info_toString, 0, 0, IS_STRING, 0) |
2917 | | ZEND_END_ARG_INFO() |
2918 | | |
2919 | 55.1k | static zend_always_inline void zend_normalize_internal_type(zend_type *type) { |
2920 | 55.1k | ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*type)); |
2921 | 55.1k | if (ZEND_TYPE_PURE_MASK(*type) != MAY_BE_ANY) { |
2922 | 51.7k | ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(*type, IS_RESOURCE) && "resource is not allowed in a zend_type"); |
2923 | 51.7k | } |
2924 | 55.1k | zend_type *current; |
2925 | 110k | ZEND_TYPE_FOREACH_MUTABLE(*type, current) { |
2926 | 110k | if (ZEND_TYPE_HAS_NAME(*current)) { |
2927 | 3.86k | zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*current)); |
2928 | 3.86k | zend_alloc_ce_cache(name); |
2929 | 3.86k | ZEND_TYPE_SET_PTR(*current, name); |
2930 | 51.3k | } else if (ZEND_TYPE_HAS_LIST(*current)) { |
2931 | 0 | zend_type *inner; |
2932 | 0 | ZEND_TYPE_FOREACH_MUTABLE(*current, inner) { |
2933 | 0 | ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*inner) && !ZEND_TYPE_HAS_LIST(*inner)); |
2934 | 0 | if (ZEND_TYPE_HAS_NAME(*inner)) { |
2935 | 0 | zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*inner)); |
2936 | 0 | zend_alloc_ce_cache(name); |
2937 | 0 | ZEND_TYPE_SET_PTR(*inner, name); |
2938 | 0 | } |
2939 | 0 | } ZEND_TYPE_FOREACH_END(); |
2940 | 0 | } |
2941 | 110k | } ZEND_TYPE_FOREACH_END(); |
2942 | 55.1k | } |
2943 | | |
2944 | | /* registers all functions in *library_functions in the function hash */ |
2945 | | ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */ |
2946 | 2.00k | { |
2947 | 2.00k | const zend_function_entry *ptr = functions; |
2948 | 2.00k | zend_function function; |
2949 | 2.00k | zend_internal_function *reg_function, *internal_function = (zend_internal_function *)&function; |
2950 | 2.00k | int count=0, unload=0; |
2951 | 2.00k | HashTable *target_function_table = function_table; |
2952 | 2.00k | int error_type; |
2953 | 2.00k | zend_string *lowercase_name; |
2954 | 2.00k | size_t fname_len; |
2955 | | |
2956 | 2.00k | if (type==MODULE_PERSISTENT) { |
2957 | 2.00k | error_type = E_CORE_WARNING; |
2958 | 2.00k | } else { |
2959 | 0 | error_type = E_WARNING; |
2960 | 0 | } |
2961 | | |
2962 | 2.00k | if (!target_function_table) { |
2963 | 148 | target_function_table = CG(function_table); |
2964 | 148 | } |
2965 | 2.00k | internal_function->type = ZEND_INTERNAL_FUNCTION; |
2966 | 2.00k | internal_function->module = EG(current_module); |
2967 | 2.00k | if (EG(active) && ZEND_OBSERVER_ENABLED) { |
2968 | | /* Add an observer temporary to store previous observed frames. This is |
2969 | | * normally handled by zend_observer_post_startup(), except for |
2970 | | * functions registered at runtime (EG(active)). */ |
2971 | 0 | internal_function->T = 1; |
2972 | 2.00k | } else { |
2973 | 2.00k | internal_function->T = 0; |
2974 | 2.00k | } |
2975 | 2.00k | memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*)); |
2976 | | |
2977 | 28.4k | while (ptr->fname) { |
2978 | 26.4k | fname_len = strlen(ptr->fname); |
2979 | 26.4k | internal_function->handler = ptr->handler; |
2980 | 26.4k | internal_function->doc_comment = ptr->doc_comment ? zend_string_init_interned(ptr->doc_comment, strlen(ptr->doc_comment), 1) : NULL; |
2981 | 26.4k | internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1); |
2982 | 26.4k | internal_function->scope = scope; |
2983 | 26.4k | internal_function->prototype = NULL; |
2984 | 26.4k | internal_function->prop_info = NULL; |
2985 | 26.4k | internal_function->attributes = NULL; |
2986 | 26.4k | internal_function->frameless_function_infos = ptr->frameless_function_infos; |
2987 | 26.4k | if (EG(active)) { // at run-time: this ought to only happen if registered with dl() or somehow temporarily at runtime |
2988 | 0 | ZEND_MAP_PTR_INIT(internal_function->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size())); |
2989 | 26.4k | } else { |
2990 | | #ifdef ZTS |
2991 | | ZEND_MAP_PTR_NEW_STATIC(internal_function->run_time_cache); |
2992 | | #else |
2993 | 26.4k | ZEND_MAP_PTR_INIT(internal_function->run_time_cache, NULL); |
2994 | 26.4k | #endif |
2995 | 26.4k | } |
2996 | 26.4k | if (ptr->flags) { |
2997 | 17.9k | if (!(ptr->flags & ZEND_ACC_PPP_MASK)) { |
2998 | 2.92k | if (ptr->flags != ZEND_ACC_DEPRECATED && scope) { |
2999 | 0 | zend_error(error_type, "Invalid access level for %s::%s() - access must be exactly one of public, protected or private", ZSTR_VAL(scope->name), ptr->fname); |
3000 | 0 | } |
3001 | 2.92k | internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags; |
3002 | 15.0k | } else { |
3003 | 15.0k | internal_function->fn_flags = ptr->flags; |
3004 | 15.0k | } |
3005 | 17.9k | } else { |
3006 | 8.48k | internal_function->fn_flags = ZEND_ACC_PUBLIC; |
3007 | 8.48k | } |
3008 | | |
3009 | 26.4k | if (ptr->arg_info) { |
3010 | 26.4k | zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info; |
3011 | 26.4k | internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1; |
3012 | 26.4k | internal_function->num_args = ptr->num_args; |
3013 | | /* Currently you cannot denote that the function can accept less arguments than num_args */ |
3014 | 26.4k | if (info->required_num_args == (uintptr_t)-1) { |
3015 | 0 | internal_function->required_num_args = ptr->num_args; |
3016 | 26.4k | } else { |
3017 | 26.4k | internal_function->required_num_args = info->required_num_args; |
3018 | 26.4k | } |
3019 | 26.4k | if (ZEND_ARG_SEND_MODE(info)) { |
3020 | 0 | internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE; |
3021 | 0 | } |
3022 | 26.4k | if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) { |
3023 | 736 | internal_function->fn_flags |= ZEND_ACC_VARIADIC; |
3024 | | /* Don't count the variadic argument */ |
3025 | 736 | internal_function->num_args--; |
3026 | 736 | } |
3027 | 26.4k | if (ZEND_TYPE_IS_SET(info->type)) { |
3028 | 24.9k | if (ZEND_TYPE_HAS_NAME(info->type)) { |
3029 | 0 | const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type); |
3030 | 0 | if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) { |
3031 | 0 | zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name); |
3032 | 0 | } |
3033 | 0 | } |
3034 | | |
3035 | 24.9k | internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE; |
3036 | 24.9k | } |
3037 | 26.4k | } else { |
3038 | 0 | zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()", |
3039 | 0 | scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname); |
3040 | |
|
3041 | 0 | internal_function->arg_info = NULL; |
3042 | 0 | internal_function->num_args = 0; |
3043 | 0 | internal_function->required_num_args = 0; |
3044 | 0 | } |
3045 | | |
3046 | | /* If not specified, add __toString() return type for compatibility with Stringable |
3047 | | * interface. */ |
3048 | 26.4k | if (scope && zend_string_equals_literal_ci(internal_function->function_name, "__tostring") && |
3049 | 26.4k | !(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
3050 | 0 | zend_error(E_CORE_WARNING, "%s::__toString() implemented without string return type", |
3051 | 0 | ZSTR_VAL(scope->name)); |
3052 | 0 | internal_function->arg_info = (zend_internal_arg_info *) arg_info_toString + 1; |
3053 | 0 | internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE; |
3054 | 0 | internal_function->num_args = internal_function->required_num_args = 0; |
3055 | 0 | } |
3056 | | |
3057 | | |
3058 | 26.4k | zend_set_function_arg_flags((zend_function*)internal_function); |
3059 | 26.4k | if (ptr->flags & ZEND_ACC_ABSTRACT) { |
3060 | 720 | if (scope) { |
3061 | | /* This is a class that must be abstract itself. Here we set the check info. */ |
3062 | 720 | scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; |
3063 | 720 | if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) { |
3064 | | /* Since the class is not an interface it needs to be declared as a abstract class. */ |
3065 | | /* Since here we are handling internal functions only we can add the keyword flag. */ |
3066 | | /* This time we set the flag for the keyword 'abstract'. */ |
3067 | 32 | scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; |
3068 | 32 | } |
3069 | 720 | } |
3070 | 720 | if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) { |
3071 | 0 | zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname); |
3072 | 0 | } |
3073 | 25.7k | } else { |
3074 | 25.7k | if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) { |
3075 | 0 | zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname); |
3076 | 0 | return FAILURE; |
3077 | 0 | } |
3078 | 25.7k | if (!internal_function->handler) { |
3079 | 0 | zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname); |
3080 | 0 | zend_unregister_functions(functions, count, target_function_table); |
3081 | 0 | return FAILURE; |
3082 | 0 | } |
3083 | 25.7k | } |
3084 | 26.4k | lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT); |
3085 | 26.4k | lowercase_name = zend_new_interned_string(lowercase_name); |
3086 | 26.4k | reg_function = malloc(sizeof(zend_internal_function)); |
3087 | 26.4k | memcpy(reg_function, &function, sizeof(zend_internal_function)); |
3088 | 26.4k | if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) { |
3089 | 0 | unload=1; |
3090 | 0 | free(reg_function); |
3091 | 0 | zend_string_release(lowercase_name); |
3092 | 0 | break; |
3093 | 0 | } |
3094 | 26.4k | if (reg_function->frameless_function_infos) { |
3095 | 304 | const zend_frameless_function_info *flf_info = reg_function->frameless_function_infos; |
3096 | 752 | while (flf_info->handler) { |
3097 | 448 | if (zend_flf_count == zend_flf_capacity) { |
3098 | 48 | if (!zend_flf_capacity) { |
3099 | 16 | zend_flf_capacity = 8; |
3100 | 32 | } else { |
3101 | 32 | zend_flf_capacity *= 2; |
3102 | 32 | } |
3103 | | /* +1 for NULL terminator */ |
3104 | 48 | zend_flf_handlers = realloc(zend_flf_handlers, (zend_flf_capacity + 1) * sizeof(void *)); |
3105 | 48 | zend_flf_functions = realloc(zend_flf_functions, (zend_flf_capacity + 1) * sizeof(zend_function *)); |
3106 | 48 | } |
3107 | 448 | zend_flf_handlers[zend_flf_count] = flf_info->handler; |
3108 | 448 | zend_flf_functions[zend_flf_count] = (zend_function *)reg_function; |
3109 | 448 | zend_flf_count++; |
3110 | 448 | flf_info++; |
3111 | 448 | } |
3112 | 304 | zend_flf_handlers[zend_flf_count] = NULL; |
3113 | 304 | zend_flf_functions[zend_flf_count] = NULL; |
3114 | 304 | } |
3115 | | |
3116 | | /* Get parameter count including variadic parameter. */ |
3117 | 26.4k | uint32_t num_args = reg_function->num_args; |
3118 | 26.4k | if (reg_function->fn_flags & ZEND_ACC_VARIADIC) { |
3119 | 736 | num_args++; |
3120 | 736 | } |
3121 | | |
3122 | | /* If types of arguments have to be checked */ |
3123 | 26.4k | if (reg_function->arg_info && num_args) { |
3124 | 15.4k | uint32_t i; |
3125 | 43.5k | for (i = 0; i < num_args; i++) { |
3126 | 28.0k | zend_internal_arg_info *arg_info = ®_function->arg_info[i]; |
3127 | 28.0k | ZEND_ASSERT(arg_info->name && "Parameter must have a name"); |
3128 | 28.0k | if (ZEND_TYPE_IS_SET(arg_info->type)) { |
3129 | 25.1k | reg_function->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS; |
3130 | 25.1k | } |
3131 | 28.0k | #if ZEND_DEBUG |
3132 | 48.7k | for (uint32_t j = 0; j < i; j++) { |
3133 | 20.6k | if (!strcmp(arg_info->name, reg_function->arg_info[j].name)) { |
3134 | 0 | zend_error_noreturn(E_CORE_ERROR, |
3135 | 0 | "Duplicate parameter name $%s for function %s%s%s()", arg_info->name, |
3136 | 0 | scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname); |
3137 | 0 | } |
3138 | 20.6k | } |
3139 | 28.0k | #endif |
3140 | 28.0k | } |
3141 | 15.4k | } |
3142 | | |
3143 | | /* Rebuild arginfos if parameter/property types and/or a return type are used */ |
3144 | 26.4k | if (reg_function->arg_info && |
3145 | 26.4k | (reg_function->fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))) { |
3146 | | /* convert "const char*" class type names into "zend_string*" */ |
3147 | 26.1k | uint32_t i; |
3148 | 26.1k | zend_internal_arg_info *arg_info = reg_function->arg_info - 1; |
3149 | 26.1k | zend_internal_arg_info *new_arg_info; |
3150 | | |
3151 | | /* Treat return type as an extra argument */ |
3152 | 26.1k | num_args++; |
3153 | 26.1k | new_arg_info = malloc(sizeof(zend_internal_arg_info) * num_args); |
3154 | 26.1k | memcpy(new_arg_info, arg_info, sizeof(zend_internal_arg_info) * num_args); |
3155 | 26.1k | reg_function->arg_info = new_arg_info + 1; |
3156 | 80.3k | for (i = 0; i < num_args; i++) { |
3157 | 54.1k | if (ZEND_TYPE_HAS_LITERAL_NAME(new_arg_info[i].type)) { |
3158 | | // gen_stubs.php does not support codegen for DNF types in arg infos. |
3159 | | // As a temporary workaround, we split the type name on `|` characters, |
3160 | | // converting it to an union type if necessary. |
3161 | 3.70k | const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type); |
3162 | 3.70k | new_arg_info[i].type.type_mask &= ~_ZEND_TYPE_LITERAL_NAME_BIT; |
3163 | | |
3164 | 3.70k | size_t num_types = 1; |
3165 | 3.70k | const char *p = class_name; |
3166 | 3.73k | while ((p = strchr(p, '|'))) { |
3167 | 32 | num_types++; |
3168 | 32 | p++; |
3169 | 32 | } |
3170 | | |
3171 | 3.70k | if (num_types == 1) { |
3172 | | /* Simple class type */ |
3173 | 3.66k | zend_string *str = zend_string_init_interned(class_name, strlen(class_name), 1); |
3174 | 3.66k | zend_alloc_ce_cache(str); |
3175 | 3.66k | ZEND_TYPE_SET_PTR(new_arg_info[i].type, str); |
3176 | 3.66k | new_arg_info[i].type.type_mask |= _ZEND_TYPE_NAME_BIT; |
3177 | 3.66k | } else { |
3178 | | /* Union type */ |
3179 | 32 | zend_type_list *list = malloc(ZEND_TYPE_LIST_SIZE(num_types)); |
3180 | 32 | list->num_types = num_types; |
3181 | 32 | ZEND_TYPE_SET_LIST(new_arg_info[i].type, list); |
3182 | 32 | ZEND_TYPE_FULL_MASK(new_arg_info[i].type) |= _ZEND_TYPE_UNION_BIT; |
3183 | | |
3184 | 32 | const char *start = class_name; |
3185 | 32 | uint32_t j = 0; |
3186 | 64 | while (true) { |
3187 | 64 | const char *end = strchr(start, '|'); |
3188 | 64 | zend_string *str = zend_string_init_interned(start, end ? end - start : strlen(start), 1); |
3189 | 64 | zend_alloc_ce_cache(str); |
3190 | 64 | list->types[j] = (zend_type) ZEND_TYPE_INIT_CLASS(str, 0, 0); |
3191 | 64 | if (!end) { |
3192 | 32 | break; |
3193 | 32 | } |
3194 | 32 | start = end + 1; |
3195 | 32 | j++; |
3196 | 32 | } |
3197 | 32 | } |
3198 | 3.70k | } |
3199 | 54.1k | if (ZEND_TYPE_IS_ITERABLE_FALLBACK(new_arg_info[i].type)) { |
3200 | | /* Warning generated an extension load warning which is emitted for every test |
3201 | | zend_error(E_CORE_WARNING, "iterable type is now a compile time alias for array|Traversable," |
3202 | | " regenerate the argument info via the php-src gen_stub build script"); |
3203 | | */ |
3204 | 0 | zend_type legacy_iterable = ZEND_TYPE_INIT_CLASS_MASK( |
3205 | 0 | ZSTR_KNOWN(ZEND_STR_TRAVERSABLE), |
3206 | 0 | (new_arg_info[i].type.type_mask | MAY_BE_ARRAY) |
3207 | 0 | ); |
3208 | 0 | new_arg_info[i].type = legacy_iterable; |
3209 | 0 | } |
3210 | | |
3211 | 54.1k | zend_normalize_internal_type(&new_arg_info[i].type); |
3212 | 54.1k | } |
3213 | 26.1k | } |
3214 | | |
3215 | 26.4k | if (scope) { |
3216 | 15.0k | zend_check_magic_method_implementation( |
3217 | 15.0k | scope, (zend_function *)reg_function, lowercase_name, E_CORE_ERROR); |
3218 | 15.0k | zend_add_magic_method(scope, (zend_function *)reg_function, lowercase_name); |
3219 | 15.0k | } |
3220 | 26.4k | ptr++; |
3221 | 26.4k | count++; |
3222 | 26.4k | zend_string_release(lowercase_name); |
3223 | 26.4k | } |
3224 | 2.00k | if (unload) { /* before unloading, display all remaining bad function in the module */ |
3225 | 0 | while (ptr->fname) { |
3226 | 0 | fname_len = strlen(ptr->fname); |
3227 | 0 | lowercase_name = zend_string_alloc(fname_len, 0); |
3228 | 0 | zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len); |
3229 | 0 | if (zend_hash_exists(target_function_table, lowercase_name)) { |
3230 | 0 | zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname); |
3231 | 0 | } |
3232 | 0 | zend_string_efree(lowercase_name); |
3233 | 0 | ptr++; |
3234 | 0 | } |
3235 | 0 | zend_unregister_functions(functions, count, target_function_table); |
3236 | 0 | return FAILURE; |
3237 | 0 | } |
3238 | 2.00k | return SUCCESS; |
3239 | 2.00k | } |
3240 | | /* }}} */ |
3241 | | |
3242 | | /* count=-1 means erase all functions, otherwise, |
3243 | | * erase the first count functions |
3244 | | */ |
3245 | | ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */ |
3246 | 0 | { |
3247 | 0 | const zend_function_entry *ptr = functions; |
3248 | 0 | int i=0; |
3249 | 0 | HashTable *target_function_table = function_table; |
3250 | 0 | zend_string *lowercase_name; |
3251 | 0 | size_t fname_len; |
3252 | |
|
3253 | 0 | if (!target_function_table) { |
3254 | 0 | target_function_table = CG(function_table); |
3255 | 0 | } |
3256 | 0 | while (ptr->fname) { |
3257 | 0 | if (count!=-1 && i>=count) { |
3258 | 0 | break; |
3259 | 0 | } |
3260 | 0 | fname_len = strlen(ptr->fname); |
3261 | 0 | lowercase_name = zend_string_alloc(fname_len, 0); |
3262 | 0 | zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len); |
3263 | 0 | zend_hash_del(target_function_table, lowercase_name); |
3264 | 0 | zend_string_efree(lowercase_name); |
3265 | 0 | ptr++; |
3266 | 0 | i++; |
3267 | 0 | } |
3268 | 0 | } |
3269 | | /* }}} */ |
3270 | | |
3271 | | ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */ |
3272 | 4 | { |
3273 | 4 | if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) { |
3274 | 4 | return SUCCESS; |
3275 | 4 | } |
3276 | 0 | return FAILURE; |
3277 | 4 | } |
3278 | | /* }}} */ |
3279 | | |
3280 | | ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */ |
3281 | 0 | { |
3282 | 0 | zend_module_entry *module; |
3283 | |
|
3284 | 0 | module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name)); |
3285 | 0 | return (module && module->module_started) ? SUCCESS : FAILURE; |
3286 | 0 | } |
3287 | | /* }}} */ |
3288 | | |
3289 | | static void clean_module_classes(int module_number) /* {{{ */ |
3290 | 0 | { |
3291 | | /* Child classes may reuse structures from parent classes, so destroy in reverse order. */ |
3292 | 0 | Bucket *bucket; |
3293 | 0 | ZEND_HASH_REVERSE_FOREACH_BUCKET(EG(class_table), bucket) { |
3294 | 0 | zend_class_entry *ce = Z_CE(bucket->val); |
3295 | 0 | if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) { |
3296 | 0 | zend_hash_del_bucket(EG(class_table), bucket); |
3297 | 0 | } |
3298 | 0 | } ZEND_HASH_FOREACH_END(); |
3299 | |
|
3300 | 0 | } |
3301 | | /* }}} */ |
3302 | | |
3303 | | static int clean_module_function(zval *el, void *arg) /* {{{ */ |
3304 | 0 | { |
3305 | 0 | zend_function *fe = (zend_function *) Z_PTR_P(el); |
3306 | 0 | zend_module_entry *module = (zend_module_entry *) arg; |
3307 | 0 | if (fe->common.type == ZEND_INTERNAL_FUNCTION && fe->internal_function.module == module) { |
3308 | 0 | return ZEND_HASH_APPLY_REMOVE; |
3309 | 0 | } else { |
3310 | 0 | return ZEND_HASH_APPLY_KEEP; |
3311 | 0 | } |
3312 | 0 | } |
3313 | | /* }}} */ |
3314 | | |
3315 | | static void clean_module_functions(zend_module_entry *module) /* {{{ */ |
3316 | 0 | { |
3317 | 0 | zend_hash_apply_with_argument(CG(function_table), clean_module_function, module); |
3318 | 0 | } |
3319 | | /* }}} */ |
3320 | | |
3321 | | void module_destructor(zend_module_entry *module) /* {{{ */ |
3322 | 0 | { |
3323 | | #if ZEND_RC_DEBUG |
3324 | | bool orig_rc_debug = zend_rc_debug; |
3325 | | #endif |
3326 | |
|
3327 | 0 | if (module->type == MODULE_TEMPORARY) { |
3328 | | #if ZEND_RC_DEBUG |
3329 | | /* FIXME: Loading extensions during the request breaks some invariants. |
3330 | | * In particular, it will create persistent interned strings, which is |
3331 | | * not allowed at this stage. */ |
3332 | | zend_rc_debug = false; |
3333 | | #endif |
3334 | 0 | zend_clean_module_rsrc_dtors(module->module_number); |
3335 | 0 | clean_module_constants(module->module_number); |
3336 | 0 | clean_module_classes(module->module_number); |
3337 | 0 | } |
3338 | |
|
3339 | 0 | if (module->module_started && module->module_shutdown_func) { |
3340 | | #if 0 |
3341 | | zend_printf("%s: Module shutdown\n", module->name); |
3342 | | #endif |
3343 | 0 | module->module_shutdown_func(module->type, module->module_number); |
3344 | 0 | } |
3345 | |
|
3346 | 0 | if (module->module_started |
3347 | 0 | && !module->module_shutdown_func |
3348 | 0 | && module->type == MODULE_TEMPORARY) { |
3349 | 0 | zend_unregister_ini_entries_ex(module->module_number, module->type); |
3350 | 0 | } |
3351 | | |
3352 | | /* Deinitialize module globals */ |
3353 | 0 | if (module->globals_size) { |
3354 | | #ifdef ZTS |
3355 | | if (*module->globals_id_ptr) { |
3356 | | ts_free_id(*module->globals_id_ptr); |
3357 | | } |
3358 | | #else |
3359 | 0 | if (module->globals_dtor) { |
3360 | 0 | module->globals_dtor(module->globals_ptr); |
3361 | 0 | } |
3362 | 0 | #endif |
3363 | 0 | } |
3364 | |
|
3365 | 0 | module->module_started=0; |
3366 | 0 | if (module->type == MODULE_TEMPORARY && module->functions) { |
3367 | 0 | zend_unregister_functions(module->functions, -1, NULL); |
3368 | | /* Clean functions registered separately from module->functions */ |
3369 | 0 | clean_module_functions(module); |
3370 | 0 | } |
3371 | |
|
3372 | | #if ZEND_RC_DEBUG |
3373 | | zend_rc_debug = orig_rc_debug; |
3374 | | #endif |
3375 | 0 | } |
3376 | | /* }}} */ |
3377 | | |
3378 | | void module_registry_unload(const zend_module_entry *module) |
3379 | 0 | { |
3380 | 0 | #ifdef HAVE_LIBDL |
3381 | 0 | if (!getenv("ZEND_DONT_UNLOAD_MODULES")) { |
3382 | 0 | DL_UNLOAD(module->handle); |
3383 | 0 | } |
3384 | | #else |
3385 | | ZEND_IGNORE_VALUE(module); |
3386 | | #endif |
3387 | 0 | } |
3388 | | |
3389 | | ZEND_API void zend_activate_modules(void) /* {{{ */ |
3390 | 268k | { |
3391 | 268k | zend_module_entry **p = module_request_startup_handlers; |
3392 | | |
3393 | 2.25M | while (*p) { |
3394 | 1.98M | zend_module_entry *module = *p; |
3395 | | |
3396 | 1.98M | if (module->request_startup_func(module->type, module->module_number)==FAILURE) { |
3397 | 0 | zend_error(E_WARNING, "request_startup() for %s module failed", module->name); |
3398 | 0 | exit(1); |
3399 | 0 | } |
3400 | 1.98M | p++; |
3401 | 1.98M | } |
3402 | 268k | } |
3403 | | /* }}} */ |
3404 | | |
3405 | | ZEND_API void zend_deactivate_modules(void) /* {{{ */ |
3406 | 268k | { |
3407 | 268k | EG(current_execute_data) = NULL; /* we're no longer executing anything */ |
3408 | | |
3409 | 268k | if (EG(full_tables_cleanup)) { |
3410 | 0 | zend_module_entry *module; |
3411 | |
|
3412 | 0 | ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { |
3413 | 0 | if (module->request_shutdown_func) { |
3414 | 0 | zend_try { |
3415 | 0 | module->request_shutdown_func(module->type, module->module_number); |
3416 | 0 | } zend_end_try(); |
3417 | 0 | } |
3418 | 0 | } ZEND_HASH_FOREACH_END(); |
3419 | 268k | } else { |
3420 | 268k | zend_module_entry **p = module_request_shutdown_handlers; |
3421 | | |
3422 | 1.60M | while (*p) { |
3423 | 1.34M | zend_module_entry *module = *p; |
3424 | 1.34M | zend_try { |
3425 | 1.34M | module->request_shutdown_func(module->type, module->module_number); |
3426 | 1.34M | } zend_end_try(); |
3427 | 1.34M | p++; |
3428 | 1.34M | } |
3429 | 268k | } |
3430 | 268k | } |
3431 | | /* }}} */ |
3432 | | |
3433 | | void zend_unload_modules(void) /* {{{ */ |
3434 | 0 | { |
3435 | 0 | zend_module_entry **modules = modules_dl_loaded; |
3436 | 0 | while (*modules) { |
3437 | 0 | module_registry_unload(*modules); |
3438 | 0 | modules++; |
3439 | 0 | } |
3440 | 0 | free(modules_dl_loaded); |
3441 | 0 | modules_dl_loaded = NULL; |
3442 | 0 | } |
3443 | | /* }}} */ |
3444 | | |
3445 | | ZEND_API void zend_post_deactivate_modules(void) /* {{{ */ |
3446 | 268k | { |
3447 | 268k | if (EG(full_tables_cleanup)) { |
3448 | 0 | zend_module_entry *module; |
3449 | 0 | zval *zv; |
3450 | 0 | zend_string *key; |
3451 | |
|
3452 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { |
3453 | 0 | if (module->post_deactivate_func) { |
3454 | 0 | module->post_deactivate_func(); |
3455 | 0 | } |
3456 | 0 | } ZEND_HASH_FOREACH_END(); |
3457 | 0 | ZEND_HASH_MAP_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) { |
3458 | 0 | module = Z_PTR_P(zv); |
3459 | 0 | if (module->type != MODULE_TEMPORARY) { |
3460 | 0 | break; |
3461 | 0 | } |
3462 | 0 | module_destructor(module); |
3463 | 0 | if (module->handle) { |
3464 | 0 | module_registry_unload(module); |
3465 | 0 | } |
3466 | 0 | zend_string_release_ex(key, 0); |
3467 | 0 | } ZEND_HASH_MAP_FOREACH_END_DEL(); |
3468 | 268k | } else { |
3469 | 268k | zend_module_entry **p = module_post_deactivate_handlers; |
3470 | | |
3471 | 640k | while (*p) { |
3472 | 372k | zend_module_entry *module = *p; |
3473 | | |
3474 | 372k | module->post_deactivate_func(); |
3475 | 372k | p++; |
3476 | 372k | } |
3477 | 268k | } |
3478 | 268k | } |
3479 | | /* }}} */ |
3480 | | |
3481 | | /* return the next free module number */ |
3482 | | ZEND_API int zend_next_free_module(void) /* {{{ */ |
3483 | 196 | { |
3484 | 196 | return zend_hash_num_elements(&module_registry); |
3485 | 196 | } |
3486 | | /* }}} */ |
3487 | | |
3488 | | static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */ |
3489 | 2.60k | { |
3490 | 2.60k | zend_class_entry *class_entry = malloc(sizeof(zend_class_entry)); |
3491 | 2.60k | zend_string *lowercase_name; |
3492 | 2.60k | *class_entry = *orig_class_entry; |
3493 | | |
3494 | 2.60k | class_entry->type = ZEND_INTERNAL_CLASS; |
3495 | 2.60k | zend_initialize_class_data(class_entry, 0); |
3496 | 2.60k | zend_alloc_ce_cache(class_entry->name); |
3497 | 2.60k | class_entry->ce_flags = orig_class_entry->ce_flags | ce_flags | ZEND_ACC_CONSTANTS_UPDATED | ZEND_ACC_LINKED | ZEND_ACC_RESOLVED_PARENT | ZEND_ACC_RESOLVED_INTERFACES; |
3498 | 2.60k | class_entry->info.internal.module = EG(current_module); |
3499 | | |
3500 | 2.60k | if (class_entry->info.internal.builtin_functions) { |
3501 | 1.77k | zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type); |
3502 | 1.77k | } |
3503 | | |
3504 | 2.60k | lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT); |
3505 | 2.60k | lowercase_name = zend_new_interned_string(lowercase_name); |
3506 | 2.60k | zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry); |
3507 | 2.60k | zend_string_release_ex(lowercase_name, 1); |
3508 | | |
3509 | 2.60k | if (class_entry->__tostring && !zend_string_equals_literal(class_entry->name, "Stringable") |
3510 | 2.60k | && !(class_entry->ce_flags & ZEND_ACC_TRAIT)) { |
3511 | 272 | ZEND_ASSERT(zend_ce_stringable |
3512 | 272 | && "Should be registered before first class using __toString()"); |
3513 | 272 | zend_do_implement_interface(class_entry, zend_ce_stringable); |
3514 | 272 | } |
3515 | 2.60k | return class_entry; |
3516 | 2.60k | } |
3517 | | /* }}} */ |
3518 | | |
3519 | | /* If parent_ce is not NULL then it inherits from parent_ce |
3520 | | * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it |
3521 | | * If both parent_ce and parent_name are NULL it does a regular class registration |
3522 | | * If parent_name is specified but not found NULL is returned |
3523 | | */ |
3524 | | ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */ |
3525 | 0 | { |
3526 | 0 | return zend_register_internal_class_with_flags(class_entry, parent_ce, 0); |
3527 | 0 | } |
3528 | | /* }}} */ |
3529 | | |
3530 | | ZEND_API zend_class_entry *zend_register_internal_class_with_flags( |
3531 | | zend_class_entry *class_entry, |
3532 | | zend_class_entry *parent_ce, |
3533 | | uint32_t ce_flags |
3534 | 2.20k | ) { |
3535 | 2.20k | zend_class_entry *register_class = do_register_internal_class(class_entry, ce_flags); |
3536 | | |
3537 | 2.20k | if (parent_ce) { |
3538 | 1.23k | zend_do_inheritance(register_class, parent_ce); |
3539 | 1.23k | zend_build_properties_info_table(register_class); |
3540 | 1.23k | } |
3541 | | |
3542 | 2.20k | return register_class; |
3543 | 2.20k | } |
3544 | | |
3545 | | ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */ |
3546 | 928 | { |
3547 | 928 | zend_class_entry *interface_entry; |
3548 | 928 | va_list interface_list; |
3549 | 928 | va_start(interface_list, num_interfaces); |
3550 | | |
3551 | 2.20k | while (num_interfaces--) { |
3552 | 1.28k | interface_entry = va_arg(interface_list, zend_class_entry *); |
3553 | 1.28k | if (interface_entry == zend_ce_stringable |
3554 | 1.28k | && zend_class_implements_interface(class_entry, zend_ce_stringable)) { |
3555 | | /* Stringable is implemented automatically, |
3556 | | * silently ignore an explicit implementation. */ |
3557 | 48 | continue; |
3558 | 48 | } |
3559 | | |
3560 | 1.23k | zend_do_implement_interface(class_entry, interface_entry); |
3561 | 1.23k | } |
3562 | | |
3563 | 928 | va_end(interface_list); |
3564 | 928 | } |
3565 | | /* }}} */ |
3566 | | |
3567 | | /* A class that contains at least one abstract method automatically becomes an abstract class. |
3568 | | */ |
3569 | | ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */ |
3570 | 80 | { |
3571 | 80 | return do_register_internal_class(orig_class_entry, 0); |
3572 | 80 | } |
3573 | | /* }}} */ |
3574 | | |
3575 | | ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */ |
3576 | 320 | { |
3577 | 320 | return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE); |
3578 | 320 | } |
3579 | | /* }}} */ |
3580 | | |
3581 | | ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */ |
3582 | 218 | { |
3583 | 218 | zend_string *lcname; |
3584 | 218 | zval zv, *ret; |
3585 | | |
3586 | | /* TODO: Move this out of here in 7.4. */ |
3587 | 218 | if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) { |
3588 | 0 | persistent = 0; |
3589 | 0 | } |
3590 | | |
3591 | 218 | if (name[0] == '\\') { |
3592 | 0 | lcname = zend_string_alloc(name_len-1, persistent); |
3593 | 0 | zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1); |
3594 | 218 | } else { |
3595 | 218 | lcname = zend_string_alloc(name_len, persistent); |
3596 | 218 | zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len); |
3597 | 218 | } |
3598 | | |
3599 | 218 | zend_assert_valid_class_name(lcname, "a class alias"); |
3600 | | |
3601 | 218 | lcname = zend_new_interned_string(lcname); |
3602 | | |
3603 | | /* We cannot increase the refcount of an internal class during request time. |
3604 | | * Instead of having to deal with differentiating between class types and lifetimes, |
3605 | | * we simply don't increase the refcount of a class entry for aliases. |
3606 | | */ |
3607 | 218 | ZVAL_ALIAS_PTR(&zv, ce); |
3608 | | |
3609 | 218 | ret = zend_hash_add(CG(class_table), lcname, &zv); |
3610 | 218 | zend_string_release_ex(lcname, 0); |
3611 | 218 | if (ret) { |
3612 | | // avoid notifying at MINIT time |
3613 | 180 | if (ce->type == ZEND_USER_CLASS) { |
3614 | 170 | zend_observer_class_linked_notify(ce, lcname); |
3615 | 170 | } |
3616 | 180 | return SUCCESS; |
3617 | 180 | } |
3618 | 38 | return FAILURE; |
3619 | 218 | } |
3620 | | /* }}} */ |
3621 | | |
3622 | | // TODO num_symbol_tables as unsigned int? |
3623 | | ZEND_API zend_result zend_set_hash_symbol(zval *symbol, const char *name, size_t name_length, bool is_ref, int num_symbol_tables, ...) /* {{{ */ |
3624 | 0 | { |
3625 | 0 | HashTable *symbol_table; |
3626 | 0 | va_list symbol_table_list; |
3627 | |
|
3628 | 0 | if (num_symbol_tables <= 0) return FAILURE; |
3629 | | |
3630 | 0 | if (is_ref) { |
3631 | 0 | ZVAL_MAKE_REF(symbol); |
3632 | 0 | } |
3633 | |
|
3634 | 0 | va_start(symbol_table_list, num_symbol_tables); |
3635 | 0 | while (num_symbol_tables-- > 0) { |
3636 | 0 | symbol_table = va_arg(symbol_table_list, HashTable *); |
3637 | 0 | zend_hash_str_update(symbol_table, name, name_length, symbol); |
3638 | 0 | Z_TRY_ADDREF_P(symbol); |
3639 | 0 | } |
3640 | 0 | va_end(symbol_table_list); |
3641 | 0 | return SUCCESS; |
3642 | 0 | } |
3643 | | /* }}} */ |
3644 | | |
3645 | | /* Disabled functions support */ |
3646 | | |
3647 | | static void zend_disable_function(const char *function_name, size_t function_name_length) |
3648 | 576 | { |
3649 | 576 | if (UNEXPECTED( |
3650 | 576 | (function_name_length == strlen("exit") && !memcmp(function_name, "exit", strlen("exit"))) |
3651 | 576 | || (function_name_length == strlen("die") && !memcmp(function_name, "die", strlen("die"))) |
3652 | 576 | || (function_name_length == strlen("clone") && !memcmp(function_name, "clone", strlen("clone"))) |
3653 | 576 | )) { |
3654 | 0 | zend_error(E_WARNING, "Cannot disable function %s()", function_name); |
3655 | 0 | return; |
3656 | 0 | } |
3657 | 576 | zend_hash_str_del(CG(function_table), function_name, function_name_length); |
3658 | 576 | } |
3659 | | |
3660 | | ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */ |
3661 | 16 | { |
3662 | 16 | if (!function_list || !*function_list) { |
3663 | 0 | return; |
3664 | 0 | } |
3665 | | |
3666 | 16 | const char *s = NULL, *e = function_list; |
3667 | 5.34k | while (*e) { |
3668 | 5.32k | switch (*e) { |
3669 | 0 | case ' ': |
3670 | 560 | case ',': |
3671 | 560 | if (s) { |
3672 | 560 | zend_disable_function(s, e - s); |
3673 | 560 | s = NULL; |
3674 | 560 | } |
3675 | 560 | break; |
3676 | 4.76k | default: |
3677 | 4.76k | if (!s) { |
3678 | 576 | s = e; |
3679 | 576 | } |
3680 | 4.76k | break; |
3681 | 5.32k | } |
3682 | 5.32k | e++; |
3683 | 5.32k | } |
3684 | 16 | if (s) { |
3685 | 16 | zend_disable_function(s, e - s); |
3686 | 16 | } |
3687 | | |
3688 | | /* Rehash the function table after deleting functions. This ensures that all internal |
3689 | | * functions are contiguous, which means we don't need to perform full table cleanup |
3690 | | * on shutdown. */ |
3691 | 16 | zend_hash_rehash(CG(function_table)); |
3692 | 16 | } |
3693 | | /* }}} */ |
3694 | | |
3695 | | #ifdef ZEND_WIN32 |
3696 | | #pragma optimize("", off) |
3697 | | #endif |
3698 | | static ZEND_COLD zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */ |
3699 | 23 | { |
3700 | 23 | zend_object *intern; |
3701 | | |
3702 | 23 | intern = zend_objects_new(class_type); |
3703 | | |
3704 | | /* Initialize default properties */ |
3705 | 23 | if (EXPECTED(class_type->default_properties_count != 0)) { |
3706 | 0 | zval *p = intern->properties_table; |
3707 | 0 | zval *end = p + class_type->default_properties_count; |
3708 | 0 | do { |
3709 | 0 | ZVAL_UNDEF(p); |
3710 | 0 | p++; |
3711 | 0 | } while (p != end); |
3712 | 0 | } |
3713 | | |
3714 | 23 | zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name)); |
3715 | 23 | return intern; |
3716 | 23 | } |
3717 | | #ifdef ZEND_WIN32 |
3718 | | #pragma optimize("", on) |
3719 | | #endif |
3720 | | /* }}} */ |
3721 | | |
3722 | | static const zend_function_entry disabled_class_new[] = { |
3723 | | ZEND_FE_END |
3724 | | }; |
3725 | | |
3726 | | ZEND_API zend_result zend_disable_class(const char *class_name, size_t class_name_length) /* {{{ */ |
3727 | 16 | { |
3728 | 16 | zend_class_entry *disabled_class; |
3729 | 16 | zend_string *key; |
3730 | 16 | zend_function *fn; |
3731 | 16 | zend_property_info *prop; |
3732 | | |
3733 | 16 | key = zend_string_alloc(class_name_length, 0); |
3734 | 16 | zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length); |
3735 | 16 | disabled_class = zend_hash_find_ptr(CG(class_table), key); |
3736 | 16 | zend_string_release_ex(key, 0); |
3737 | 16 | if (!disabled_class) { |
3738 | 0 | return FAILURE; |
3739 | 0 | } |
3740 | | |
3741 | | /* Will be reset by INIT_CLASS_ENTRY. */ |
3742 | 16 | free(disabled_class->interfaces); |
3743 | | |
3744 | 16 | INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new); |
3745 | 16 | disabled_class->create_object = display_disabled_class; |
3746 | | |
3747 | 256 | ZEND_HASH_MAP_FOREACH_PTR(&disabled_class->function_table, fn) { |
3748 | 256 | if ((fn->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) && |
3749 | 112 | fn->common.scope == disabled_class) { |
3750 | 32 | zend_free_internal_arg_info(&fn->internal_function); |
3751 | 32 | } |
3752 | 256 | } ZEND_HASH_FOREACH_END(); |
3753 | 16 | zend_hash_clean(&disabled_class->function_table); |
3754 | 32 | ZEND_HASH_MAP_FOREACH_PTR(&disabled_class->properties_info, prop) { |
3755 | 32 | if (prop->ce == disabled_class) { |
3756 | 0 | zend_string_release(prop->name); |
3757 | 0 | zend_type_release(prop->type, /* persistent */ 1); |
3758 | 0 | free(prop); |
3759 | 0 | } |
3760 | 32 | } ZEND_HASH_FOREACH_END(); |
3761 | 16 | zend_hash_clean(&disabled_class->properties_info); |
3762 | 16 | return SUCCESS; |
3763 | 16 | } |
3764 | | /* }}} */ |
3765 | | |
3766 | | static zend_always_inline zend_class_entry *get_scope(zend_execute_data *frame) |
3767 | 3.75k | { |
3768 | 3.75k | return frame && frame->func ? frame->func->common.scope : NULL; |
3769 | 3.75k | } |
3770 | | |
3771 | | static bool zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool *strict_class, char **error, bool suppress_deprecation) /* {{{ */ |
3772 | 2.12k | { |
3773 | 2.12k | bool ret = 0; |
3774 | 2.12k | zend_class_entry *ce; |
3775 | 2.12k | size_t name_len = ZSTR_LEN(name); |
3776 | 2.12k | zend_string *lcname; |
3777 | 2.12k | ALLOCA_FLAG(use_heap); |
3778 | | |
3779 | 2.12k | ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap); |
3780 | 2.12k | zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len); |
3781 | | |
3782 | 2.12k | *strict_class = 0; |
3783 | 2.12k | if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_SELF))) { |
3784 | 526 | if (!scope) { |
3785 | 5 | if (error) *error = estrdup("cannot access \"self\" when no class scope is active"); |
3786 | 521 | } else { |
3787 | 521 | if (!suppress_deprecation) { |
3788 | 369 | zend_error(E_DEPRECATED, "Use of \"self\" in callables is deprecated"); |
3789 | 369 | } |
3790 | 521 | fcc->called_scope = zend_get_called_scope(frame); |
3791 | 521 | if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope)) { |
3792 | 0 | fcc->called_scope = scope; |
3793 | 0 | } |
3794 | 521 | fcc->calling_scope = scope; |
3795 | 521 | if (!fcc->object) { |
3796 | 392 | fcc->object = zend_get_this_object(frame); |
3797 | 392 | } |
3798 | 521 | ret = 1; |
3799 | 521 | } |
3800 | 1.59k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_PARENT))) { |
3801 | 348 | if (!scope) { |
3802 | 0 | if (error) *error = estrdup("cannot access \"parent\" when no class scope is active"); |
3803 | 348 | } else if (!scope->parent) { |
3804 | 8 | if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent"); |
3805 | 340 | } else { |
3806 | 340 | if (!suppress_deprecation) { |
3807 | 160 | zend_error(E_DEPRECATED, "Use of \"parent\" in callables is deprecated"); |
3808 | 160 | } |
3809 | 340 | fcc->called_scope = zend_get_called_scope(frame); |
3810 | 340 | if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope->parent)) { |
3811 | 13 | fcc->called_scope = scope->parent; |
3812 | 13 | } |
3813 | 340 | fcc->calling_scope = scope->parent; |
3814 | 340 | if (!fcc->object) { |
3815 | 191 | fcc->object = zend_get_this_object(frame); |
3816 | 191 | } |
3817 | 340 | *strict_class = 1; |
3818 | 340 | ret = 1; |
3819 | 340 | } |
3820 | 1.24k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_STATIC))) { |
3821 | 304 | zend_class_entry *called_scope = zend_get_called_scope(frame); |
3822 | | |
3823 | 304 | if (!called_scope) { |
3824 | 0 | if (error) *error = estrdup("cannot access \"static\" when no class scope is active"); |
3825 | 304 | } else { |
3826 | 304 | if (!suppress_deprecation) { |
3827 | 173 | zend_error(E_DEPRECATED, "Use of \"static\" in callables is deprecated"); |
3828 | 173 | } |
3829 | 304 | fcc->called_scope = called_scope; |
3830 | 304 | fcc->calling_scope = called_scope; |
3831 | 304 | if (!fcc->object) { |
3832 | 191 | fcc->object = zend_get_this_object(frame); |
3833 | 191 | } |
3834 | 304 | *strict_class = 1; |
3835 | 304 | ret = 1; |
3836 | 304 | } |
3837 | 942 | } else if ((ce = zend_lookup_class(name)) != NULL) { |
3838 | 699 | zend_class_entry *scope = get_scope(frame); |
3839 | 699 | fcc->calling_scope = ce; |
3840 | 699 | if (scope && !fcc->object) { |
3841 | 341 | zend_object *object = zend_get_this_object(frame); |
3842 | | |
3843 | 341 | if (object && |
3844 | 341 | instanceof_function(object->ce, scope) && |
3845 | 341 | instanceof_function(scope, ce)) { |
3846 | 278 | fcc->object = object; |
3847 | 278 | fcc->called_scope = object->ce; |
3848 | 278 | } else { |
3849 | 63 | fcc->called_scope = ce; |
3850 | 63 | } |
3851 | 358 | } else { |
3852 | 358 | fcc->called_scope = fcc->object ? fcc->object->ce : ce; |
3853 | 358 | } |
3854 | 699 | *strict_class = 1; |
3855 | 699 | ret = 1; |
3856 | 699 | } else { |
3857 | 243 | if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name)); |
3858 | 243 | } |
3859 | 2.12k | ZSTR_ALLOCA_FREE(lcname, use_heap); |
3860 | 2.12k | return ret; |
3861 | 2.12k | } |
3862 | | /* }}} */ |
3863 | | |
3864 | 17.5k | ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) { |
3865 | 17.5k | if (fcc->function_handler && |
3866 | 17.5k | (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) { |
3867 | 231 | if (fcc->function_handler->common.function_name) { |
3868 | 231 | zend_string_release_ex(fcc->function_handler->common.function_name, 0); |
3869 | 231 | } |
3870 | 231 | zend_free_trampoline(fcc->function_handler); |
3871 | 231 | fcc->function_handler = NULL; |
3872 | 231 | } |
3873 | 17.5k | } |
3874 | | |
3875 | | static zend_always_inline bool zend_is_callable_check_func(zval *callable, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool strict_class, char **error, bool suppress_deprecation) /* {{{ */ |
3876 | 13.8k | { |
3877 | 13.8k | zend_class_entry *ce_org = fcc->calling_scope; |
3878 | 13.8k | bool retval = 0; |
3879 | 13.8k | zend_string *mname, *cname; |
3880 | 13.8k | zend_string *lmname; |
3881 | 13.8k | const char *colon; |
3882 | 13.8k | size_t clen; |
3883 | 13.8k | HashTable *ftable; |
3884 | 13.8k | int call_via_handler = 0; |
3885 | 13.8k | zend_class_entry *scope; |
3886 | 13.8k | zval *zv; |
3887 | 13.8k | ALLOCA_FLAG(use_heap) |
3888 | | |
3889 | 13.8k | fcc->calling_scope = NULL; |
3890 | | |
3891 | 13.8k | if (!ce_org) { |
3892 | 4.45k | zend_function *func; |
3893 | 4.45k | zend_string *lmname; |
3894 | | |
3895 | | /* Check if function with given name exists. |
3896 | | * This may be a compound name that includes namespace name */ |
3897 | 4.45k | if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) { |
3898 | | /* Skip leading \ */ |
3899 | 82 | ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap); |
3900 | 82 | zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1); |
3901 | 82 | func = zend_fetch_function(lmname); |
3902 | 82 | ZSTR_ALLOCA_FREE(lmname, use_heap); |
3903 | 4.37k | } else { |
3904 | 4.37k | lmname = Z_STR_P(callable); |
3905 | 4.37k | func = zend_fetch_function(lmname); |
3906 | 4.37k | if (!func) { |
3907 | 1.91k | ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap); |
3908 | 1.91k | zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable)); |
3909 | 1.91k | func = zend_fetch_function(lmname); |
3910 | 1.91k | ZSTR_ALLOCA_FREE(lmname, use_heap); |
3911 | 1.91k | } |
3912 | 4.37k | } |
3913 | 4.45k | if (EXPECTED(func != NULL)) { |
3914 | 2.74k | fcc->function_handler = func; |
3915 | 2.74k | return 1; |
3916 | 2.74k | } |
3917 | 4.45k | } |
3918 | | |
3919 | | /* Split name into class/namespace and method/function names */ |
3920 | 11.1k | if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL && |
3921 | 11.1k | colon > Z_STRVAL_P(callable) && |
3922 | 11.1k | *(colon-1) == ':' |
3923 | 11.1k | ) { |
3924 | 1.99k | size_t mlen; |
3925 | | |
3926 | 1.99k | colon--; |
3927 | 1.99k | clen = colon - Z_STRVAL_P(callable); |
3928 | 1.99k | mlen = Z_STRLEN_P(callable) - clen - 2; |
3929 | | |
3930 | 1.99k | if (colon == Z_STRVAL_P(callable)) { |
3931 | 0 | if (error) *error = estrdup("invalid function name"); |
3932 | 0 | return 0; |
3933 | 0 | } |
3934 | | |
3935 | | /* This is a compound name. |
3936 | | * Try to fetch class and then find static method. */ |
3937 | 1.99k | if (ce_org) { |
3938 | 527 | scope = ce_org; |
3939 | 1.46k | } else { |
3940 | 1.46k | scope = get_scope(frame); |
3941 | 1.46k | } |
3942 | | |
3943 | 1.99k | cname = zend_string_init_interned(Z_STRVAL_P(callable), clen, 0); |
3944 | 1.99k | if (ZSTR_HAS_CE_CACHE(cname) && ZSTR_GET_CE_CACHE(cname)) { |
3945 | 1.15k | fcc->calling_scope = ZSTR_GET_CE_CACHE(cname); |
3946 | 1.15k | if (scope && !fcc->object) { |
3947 | 887 | zend_object *object = zend_get_this_object(frame); |
3948 | | |
3949 | 887 | if (object && |
3950 | 887 | instanceof_function(object->ce, scope) && |
3951 | 887 | instanceof_function(scope, fcc->calling_scope)) { |
3952 | 829 | fcc->object = object; |
3953 | 829 | fcc->called_scope = object->ce; |
3954 | 829 | } else { |
3955 | 58 | fcc->called_scope = fcc->calling_scope; |
3956 | 58 | } |
3957 | 887 | } else { |
3958 | 268 | fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope; |
3959 | 268 | } |
3960 | 1.15k | strict_class = 1; |
3961 | 1.15k | } else if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error, suppress_deprecation || ce_org != NULL)) { |
3962 | 137 | zend_string_release_ex(cname, 0); |
3963 | 137 | return 0; |
3964 | 137 | } |
3965 | 1.85k | zend_string_release_ex(cname, 0); |
3966 | | |
3967 | 1.85k | ftable = &fcc->calling_scope->function_table; |
3968 | 1.85k | if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) { |
3969 | 0 | if (error) zend_spprintf(error, 0, "class %s is not a subclass of %s", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name)); |
3970 | 0 | return 0; |
3971 | 0 | } |
3972 | 1.85k | if (ce_org && !suppress_deprecation) { |
3973 | 450 | zend_error(E_DEPRECATED, |
3974 | 450 | "Callables of the form [\"%s\", \"%s\"] are deprecated", |
3975 | 450 | ZSTR_VAL(ce_org->name), Z_STRVAL_P(callable)); |
3976 | 450 | } |
3977 | 1.85k | mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0); |
3978 | 9.12k | } else if (ce_org) { |
3979 | | /* Try to fetch find static method of given class. */ |
3980 | 8.87k | mname = Z_STR_P(callable); |
3981 | 8.87k | zend_string_addref(mname); |
3982 | 8.87k | ftable = &ce_org->function_table; |
3983 | 8.87k | fcc->calling_scope = ce_org; |
3984 | 8.87k | } else { |
3985 | | /* We already checked for plain function before. */ |
3986 | 247 | if (error) { |
3987 | 85 | zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable)); |
3988 | 85 | } |
3989 | 247 | return 0; |
3990 | 247 | } |
3991 | | |
3992 | 10.7k | lmname = zend_string_tolower(mname); |
3993 | 10.7k | if (strict_class && |
3994 | 10.7k | fcc->calling_scope && |
3995 | 10.7k | zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) { |
3996 | 802 | fcc->function_handler = fcc->calling_scope->constructor; |
3997 | 802 | if (fcc->function_handler) { |
3998 | 802 | retval = 1; |
3999 | 802 | } |
4000 | 9.92k | } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) { |
4001 | 8.44k | fcc->function_handler = Z_PTR_P(zv); |
4002 | 8.44k | retval = 1; |
4003 | 8.44k | if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) && |
4004 | 8.44k | !strict_class) { |
4005 | 13 | scope = get_scope(frame); |
4006 | 13 | if (scope && |
4007 | 13 | instanceof_function(fcc->function_handler->common.scope, scope)) { |
4008 | | |
4009 | 13 | zv = zend_hash_find(&scope->function_table, lmname); |
4010 | 13 | if (zv != NULL) { |
4011 | 13 | zend_function *priv_fbc = Z_PTR_P(zv); |
4012 | | |
4013 | 13 | if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE) |
4014 | 13 | && priv_fbc->common.scope == scope) { |
4015 | 13 | fcc->function_handler = priv_fbc; |
4016 | 13 | } |
4017 | 13 | } |
4018 | 13 | } |
4019 | 13 | } |
4020 | 8.44k | if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) && |
4021 | 8.44k | (fcc->calling_scope && |
4022 | 279 | ((fcc->object && fcc->calling_scope->__call) || |
4023 | 279 | (!fcc->object && fcc->calling_scope->__callstatic)))) { |
4024 | 32 | scope = get_scope(frame); |
4025 | 32 | ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)); |
4026 | 32 | if (!zend_check_method_accessible(fcc->function_handler, scope)) { |
4027 | 10 | retval = 0; |
4028 | 10 | fcc->function_handler = NULL; |
4029 | 10 | goto get_function_via_handler; |
4030 | 10 | } |
4031 | 32 | } |
4032 | 8.44k | } else { |
4033 | 1.49k | get_function_via_handler: |
4034 | 1.49k | if (fcc->object && fcc->calling_scope == ce_org) { |
4035 | 1.32k | if (strict_class && ce_org->__call) { |
4036 | 13 | fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0); |
4037 | 13 | call_via_handler = 1; |
4038 | 13 | retval = 1; |
4039 | 1.31k | } else { |
4040 | 1.31k | fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL); |
4041 | 1.31k | if (fcc->function_handler) { |
4042 | 1.11k | if (strict_class && |
4043 | 1.11k | (!fcc->function_handler->common.scope || |
4044 | 10 | !instanceof_function(ce_org, fcc->function_handler->common.scope))) { |
4045 | 10 | zend_release_fcall_info_cache(fcc); |
4046 | 1.10k | } else { |
4047 | 1.10k | retval = 1; |
4048 | 1.10k | call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0; |
4049 | 1.10k | } |
4050 | 1.11k | } |
4051 | 1.31k | } |
4052 | 1.32k | } else if (fcc->calling_scope) { |
4053 | 164 | if (fcc->calling_scope->get_static_method) { |
4054 | 0 | fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname); |
4055 | 164 | } else { |
4056 | 164 | fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL); |
4057 | 164 | } |
4058 | 164 | if (fcc->function_handler) { |
4059 | 86 | retval = 1; |
4060 | 86 | call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0; |
4061 | 86 | if (call_via_handler && !fcc->object) { |
4062 | 71 | zend_object *object = zend_get_this_object(frame); |
4063 | 71 | if (object && |
4064 | 71 | instanceof_function(object->ce, fcc->calling_scope)) { |
4065 | 12 | fcc->object = object; |
4066 | 12 | } |
4067 | 71 | } |
4068 | 86 | } |
4069 | 164 | } |
4070 | 1.49k | } |
4071 | | |
4072 | 10.7k | if (retval) { |
4073 | 10.4k | if (fcc->calling_scope && !call_via_handler) { |
4074 | 9.23k | if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) { |
4075 | 47 | retval = 0; |
4076 | 47 | if (error) { |
4077 | 0 | zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name)); |
4078 | 0 | } |
4079 | 9.19k | } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) { |
4080 | 17 | retval = 0; |
4081 | 17 | if (error) { |
4082 | 5 | zend_spprintf(error, 0, "non-static method %s::%s() cannot be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name)); |
4083 | 5 | } |
4084 | 17 | } |
4085 | 9.23k | if (retval |
4086 | 9.23k | && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) { |
4087 | 264 | scope = get_scope(frame); |
4088 | 264 | ZEND_ASSERT(!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)); |
4089 | 264 | if (!zend_check_method_accessible(fcc->function_handler, scope)) { |
4090 | 118 | if (error) { |
4091 | 18 | if (*error) { |
4092 | 0 | efree(*error); |
4093 | 0 | } |
4094 | 18 | zend_spprintf(error, 0, "cannot access %s method %s::%s()", zend_visibility_string(fcc->function_handler->common.fn_flags), ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name)); |
4095 | 18 | } |
4096 | 118 | retval = 0; |
4097 | 118 | } |
4098 | 264 | } |
4099 | 9.23k | } |
4100 | 10.4k | } else if (error) { |
4101 | 43 | if (fcc->calling_scope) { |
4102 | 43 | zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname)); |
4103 | 43 | } else { |
4104 | 0 | zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname)); |
4105 | 0 | } |
4106 | 43 | } |
4107 | 10.7k | zend_string_release_ex(lmname, 0); |
4108 | 10.7k | zend_string_release_ex(mname, 0); |
4109 | | |
4110 | 10.7k | if (fcc->object) { |
4111 | 9.96k | fcc->called_scope = fcc->object->ce; |
4112 | 9.96k | if (fcc->function_handler |
4113 | 9.96k | && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) { |
4114 | 57 | fcc->object = NULL; |
4115 | 57 | } |
4116 | 9.96k | } |
4117 | 10.7k | return retval; |
4118 | 10.7k | } |
4119 | | /* }}} */ |
4120 | | |
4121 | | ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */ |
4122 | 139 | { |
4123 | 139 | try_again: |
4124 | 139 | switch (Z_TYPE_P(callable)) { |
4125 | 12 | case IS_STRING: |
4126 | 12 | if (object) { |
4127 | 0 | return zend_create_member_string(object->ce->name, Z_STR_P(callable)); |
4128 | 0 | } |
4129 | 12 | return zend_string_copy(Z_STR_P(callable)); |
4130 | | |
4131 | 28 | case IS_ARRAY: |
4132 | 28 | { |
4133 | 28 | zval *method = NULL; |
4134 | 28 | zval *obj = NULL; |
4135 | | |
4136 | 28 | if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) { |
4137 | 28 | obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0); |
4138 | 28 | method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1); |
4139 | 28 | } |
4140 | | |
4141 | 28 | if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) { |
4142 | 0 | return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED); |
4143 | 0 | } |
4144 | | |
4145 | 28 | if (Z_TYPE_P(obj) == IS_STRING) { |
4146 | 0 | return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method)); |
4147 | 28 | } else if (Z_TYPE_P(obj) == IS_OBJECT) { |
4148 | 28 | return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method)); |
4149 | 28 | } else { |
4150 | 0 | return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED); |
4151 | 0 | } |
4152 | 28 | } |
4153 | 99 | case IS_OBJECT: |
4154 | 99 | { |
4155 | 99 | zend_class_entry *ce = Z_OBJCE_P(callable); |
4156 | | |
4157 | 99 | if (ce == zend_ce_closure) { |
4158 | 81 | const zend_function *fn = zend_get_closure_method_def(Z_OBJ_P(callable)); |
4159 | | |
4160 | 81 | if ((fn->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && fn->common.scope) { |
4161 | 10 | return zend_create_member_string(fn->common.scope->name, fn->common.function_name); |
4162 | 10 | } |
4163 | 71 | return zend_string_copy(fn->common.function_name); |
4164 | 81 | } |
4165 | | |
4166 | 18 | return zend_string_concat2( |
4167 | 18 | ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), |
4168 | 18 | "::__invoke", sizeof("::__invoke") - 1); |
4169 | 99 | } |
4170 | 0 | case IS_REFERENCE: |
4171 | 0 | callable = Z_REFVAL_P(callable); |
4172 | 0 | goto try_again; |
4173 | 0 | default: |
4174 | 0 | return zval_get_string_func(callable); |
4175 | 139 | } |
4176 | 139 | } |
4177 | | /* }}} */ |
4178 | | |
4179 | | ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */ |
4180 | 0 | { |
4181 | 0 | return zend_get_callable_name_ex(callable, NULL); |
4182 | 0 | } |
4183 | | /* }}} */ |
4184 | | |
4185 | | ZEND_API bool zend_is_callable_at_frame( |
4186 | | zval *callable, zend_object *object, zend_execute_data *frame, |
4187 | | uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */ |
4188 | 23.3k | { |
4189 | 23.3k | bool ret; |
4190 | 23.3k | zend_fcall_info_cache fcc_local; |
4191 | 23.3k | bool strict_class = 0; |
4192 | | |
4193 | 23.3k | if (fcc == NULL) { |
4194 | 8.21k | fcc = &fcc_local; |
4195 | 8.21k | } |
4196 | 23.3k | if (error) { |
4197 | 14.5k | *error = NULL; |
4198 | 14.5k | } |
4199 | | |
4200 | 23.3k | fcc->calling_scope = NULL; |
4201 | 23.3k | fcc->called_scope = NULL; |
4202 | 23.3k | fcc->function_handler = NULL; |
4203 | 23.3k | fcc->object = NULL; |
4204 | 23.3k | fcc->closure = NULL; |
4205 | | |
4206 | 23.3k | again: |
4207 | 23.3k | switch (Z_TYPE_P(callable)) { |
4208 | 10.6k | case IS_STRING: |
4209 | 10.6k | if (object) { |
4210 | 6.18k | fcc->object = object; |
4211 | 6.18k | fcc->calling_scope = object->ce; |
4212 | 6.18k | } |
4213 | | |
4214 | 10.6k | if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) { |
4215 | 0 | fcc->called_scope = fcc->calling_scope; |
4216 | 0 | return 1; |
4217 | 0 | } |
4218 | | |
4219 | 13.8k | check_func: |
4220 | 13.8k | ret = zend_is_callable_check_func(callable, frame, fcc, strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS); |
4221 | 13.8k | if (fcc == &fcc_local) { |
4222 | 3.38k | zend_release_fcall_info_cache(fcc); |
4223 | 3.38k | } |
4224 | 13.8k | return ret; |
4225 | | |
4226 | 3.39k | case IS_ARRAY: |
4227 | 3.39k | { |
4228 | 3.39k | if (zend_hash_num_elements(Z_ARRVAL_P(callable)) != 2) { |
4229 | 4 | if (error) *error = estrdup("array callback must have exactly two members"); |
4230 | 4 | return 0; |
4231 | 4 | } |
4232 | | |
4233 | 3.39k | zval *obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0); |
4234 | 3.39k | zval *method = zend_hash_index_find(Z_ARRVAL_P(callable), 1); |
4235 | 3.39k | if (!obj || !method) { |
4236 | 0 | if (error) *error = estrdup("array callback has to contain indices 0 and 1"); |
4237 | 0 | return 0; |
4238 | 0 | } |
4239 | | |
4240 | 3.39k | ZVAL_DEREF(obj); |
4241 | 3.39k | if (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) { |
4242 | 33 | if (error) *error = estrdup("first array member is not a valid class name or object"); |
4243 | 33 | return 0; |
4244 | 33 | } |
4245 | | |
4246 | 3.35k | ZVAL_DEREF(method); |
4247 | 3.35k | if (Z_TYPE_P(method) != IS_STRING) { |
4248 | 4 | if (error) *error = estrdup("second array member is not a valid method"); |
4249 | 4 | return 0; |
4250 | 4 | } |
4251 | | |
4252 | 3.35k | if (Z_TYPE_P(obj) == IS_STRING) { |
4253 | 1.28k | if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) { |
4254 | 0 | return 1; |
4255 | 0 | } |
4256 | | |
4257 | 1.28k | if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error, check_flags & IS_CALLABLE_SUPPRESS_DEPRECATIONS)) { |
4258 | 119 | return 0; |
4259 | 119 | } |
4260 | 2.06k | } else { |
4261 | 2.06k | ZEND_ASSERT(Z_TYPE_P(obj) == IS_OBJECT); |
4262 | 2.06k | fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */ |
4263 | 2.06k | fcc->object = Z_OBJ_P(obj); |
4264 | | |
4265 | 2.06k | if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) { |
4266 | 14 | fcc->called_scope = fcc->calling_scope; |
4267 | 14 | return 1; |
4268 | 14 | } |
4269 | 2.06k | } |
4270 | | |
4271 | 3.22k | callable = method; |
4272 | 3.22k | goto check_func; |
4273 | 3.35k | } |
4274 | 0 | return 0; |
4275 | 9.29k | case IS_OBJECT: |
4276 | 9.29k | if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, 1) == SUCCESS) { |
4277 | 9.27k | fcc->called_scope = fcc->calling_scope; |
4278 | 9.27k | fcc->closure = Z_OBJ_P(callable); |
4279 | 9.27k | if (fcc == &fcc_local) { |
4280 | 4.70k | zend_release_fcall_info_cache(fcc); |
4281 | 4.70k | } |
4282 | 9.27k | return 1; |
4283 | 9.27k | } |
4284 | 17 | if (error) *error = estrdup("no array or string given"); |
4285 | 17 | return 0; |
4286 | 5 | case IS_REFERENCE: |
4287 | 5 | callable = Z_REFVAL_P(callable); |
4288 | 5 | goto again; |
4289 | 52 | default: |
4290 | 52 | if (error) *error = estrdup("no array or string given"); |
4291 | 52 | return 0; |
4292 | 23.3k | } |
4293 | 23.3k | } |
4294 | | /* }}} */ |
4295 | | |
4296 | | ZEND_API bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */ |
4297 | 23.3k | { |
4298 | | /* Determine callability at the first parent user frame. */ |
4299 | 23.3k | zend_execute_data *frame = EG(current_execute_data); |
4300 | 44.3k | while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) { |
4301 | 20.9k | frame = frame->prev_execute_data; |
4302 | 20.9k | } |
4303 | | |
4304 | 23.3k | bool ret = zend_is_callable_at_frame(callable, object, frame, check_flags, fcc, error); |
4305 | 23.3k | if (callable_name) { |
4306 | 127 | *callable_name = zend_get_callable_name_ex(callable, object); |
4307 | 127 | } |
4308 | 23.3k | return ret; |
4309 | 23.3k | } |
4310 | | |
4311 | | ZEND_API bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */ |
4312 | 7.52k | { |
4313 | 7.52k | return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL); |
4314 | 7.52k | } |
4315 | | /* }}} */ |
4316 | | |
4317 | | ZEND_API bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */ |
4318 | 0 | { |
4319 | 0 | zend_fcall_info_cache fcc; |
4320 | |
|
4321 | 0 | if (zend_is_callable_ex(callable, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, callable_name, &fcc, NULL)) { |
4322 | 0 | if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) { |
4323 | 0 | zval_ptr_dtor_str(callable); |
4324 | 0 | array_init(callable); |
4325 | 0 | add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name)); |
4326 | 0 | add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name)); |
4327 | 0 | } |
4328 | 0 | zend_release_fcall_info_cache(&fcc); |
4329 | 0 | return 1; |
4330 | 0 | } |
4331 | 0 | return 0; |
4332 | 0 | } |
4333 | | /* }}} */ |
4334 | | |
4335 | | ZEND_API zend_result zend_fcall_info_init(zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */ |
4336 | 6.36k | { |
4337 | 6.36k | if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) { |
4338 | 113 | return FAILURE; |
4339 | 113 | } |
4340 | | |
4341 | 6.25k | fci->size = sizeof(*fci); |
4342 | 6.25k | fci->object = fcc->object; |
4343 | 6.25k | ZVAL_COPY_VALUE(&fci->function_name, callable); |
4344 | 6.25k | fci->retval = NULL; |
4345 | 6.25k | fci->param_count = 0; |
4346 | 6.25k | fci->params = NULL; |
4347 | 6.25k | fci->named_params = NULL; |
4348 | | |
4349 | 6.25k | return SUCCESS; |
4350 | 6.36k | } |
4351 | | /* }}} */ |
4352 | | |
4353 | | ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */ |
4354 | 0 | { |
4355 | 0 | if (fci->params) { |
4356 | 0 | zval *p = fci->params; |
4357 | 0 | zval *end = p + fci->param_count; |
4358 | |
|
4359 | 0 | while (p != end) { |
4360 | 0 | i_zval_ptr_dtor(p); |
4361 | 0 | p++; |
4362 | 0 | } |
4363 | 0 | if (free_mem) { |
4364 | 0 | efree(fci->params); |
4365 | 0 | fci->params = NULL; |
4366 | 0 | } |
4367 | 0 | } |
4368 | 0 | fci->param_count = 0; |
4369 | 0 | } |
4370 | | /* }}} */ |
4371 | | |
4372 | | ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */ |
4373 | 0 | { |
4374 | 0 | *param_count = fci->param_count; |
4375 | 0 | *params = fci->params; |
4376 | 0 | fci->param_count = 0; |
4377 | 0 | fci->params = NULL; |
4378 | 0 | } |
4379 | | /* }}} */ |
4380 | | |
4381 | | ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */ |
4382 | 0 | { |
4383 | 0 | zend_fcall_info_args_clear(fci, 1); |
4384 | 0 | fci->param_count = param_count; |
4385 | 0 | fci->params = params; |
4386 | 0 | } |
4387 | | /* }}} */ |
4388 | | |
4389 | | ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */ |
4390 | 0 | { |
4391 | 0 | zval *arg, *params; |
4392 | 0 | uint32_t n = 1; |
4393 | |
|
4394 | 0 | zend_fcall_info_args_clear(fci, !args); |
4395 | |
|
4396 | 0 | if (!args) { |
4397 | 0 | return SUCCESS; |
4398 | 0 | } |
4399 | | |
4400 | 0 | if (Z_TYPE_P(args) != IS_ARRAY) { |
4401 | 0 | return FAILURE; |
4402 | 0 | } |
4403 | | |
4404 | 0 | fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args)); |
4405 | 0 | fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval)); |
4406 | |
|
4407 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) { |
4408 | 0 | if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) { |
4409 | 0 | ZVAL_NEW_REF(params, arg); |
4410 | 0 | Z_TRY_ADDREF_P(arg); |
4411 | 0 | } else { |
4412 | 0 | ZVAL_COPY(params, arg); |
4413 | 0 | } |
4414 | 0 | params++; |
4415 | 0 | n++; |
4416 | 0 | } ZEND_HASH_FOREACH_END(); |
4417 | |
|
4418 | 0 | return SUCCESS; |
4419 | 0 | } |
4420 | | /* }}} */ |
4421 | | |
4422 | | ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */ |
4423 | 0 | { |
4424 | 0 | return zend_fcall_info_args_ex(fci, NULL, args); |
4425 | 0 | } |
4426 | | /* }}} */ |
4427 | | |
4428 | | ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */ |
4429 | 0 | { |
4430 | 0 | zend_fcall_info_args_clear(fci, !argc); |
4431 | |
|
4432 | 0 | if (argc) { |
4433 | 0 | fci->param_count = argc; |
4434 | 0 | fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval)); |
4435 | |
|
4436 | 0 | for (uint32_t i = 0; i < argc; ++i) { |
4437 | 0 | ZVAL_COPY(&fci->params[i], &argv[i]); |
4438 | 0 | } |
4439 | 0 | } |
4440 | 0 | } |
4441 | | /* }}} */ |
4442 | | |
4443 | | ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */ |
4444 | 0 | { |
4445 | 0 | zend_fcall_info_args_clear(fci, !argc); |
4446 | |
|
4447 | 0 | if (argc) { |
4448 | 0 | zval *arg; |
4449 | 0 | fci->param_count = argc; |
4450 | 0 | fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval)); |
4451 | |
|
4452 | 0 | for (uint32_t i = 0; i < argc; ++i) { |
4453 | 0 | arg = va_arg(*argv, zval *); |
4454 | 0 | ZVAL_COPY(&fci->params[i], arg); |
4455 | 0 | } |
4456 | 0 | } |
4457 | 0 | } |
4458 | | /* }}} */ |
4459 | | |
4460 | | ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */ |
4461 | 0 | { |
4462 | 0 | va_list argv; |
4463 | |
|
4464 | 0 | va_start(argv, argc); |
4465 | 0 | zend_fcall_info_argv(fci, argc, &argv); |
4466 | 0 | va_end(argv); |
4467 | 0 | } |
4468 | | /* }}} */ |
4469 | | |
4470 | | ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */ |
4471 | 0 | { |
4472 | 0 | zval retval, *org_params = NULL; |
4473 | 0 | uint32_t org_count = 0; |
4474 | 0 | zend_result result; |
4475 | |
|
4476 | 0 | fci->retval = retval_ptr ? retval_ptr : &retval; |
4477 | 0 | if (args) { |
4478 | 0 | zend_fcall_info_args_save(fci, &org_count, &org_params); |
4479 | 0 | zend_fcall_info_args(fci, args); |
4480 | 0 | } |
4481 | 0 | result = zend_call_function(fci, fcc); |
4482 | |
|
4483 | 0 | if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) { |
4484 | 0 | zval_ptr_dtor(&retval); |
4485 | 0 | } |
4486 | 0 | if (args) { |
4487 | 0 | zend_fcall_info_args_restore(fci, org_count, org_params); |
4488 | 0 | } |
4489 | 0 | return result; |
4490 | 0 | } |
4491 | | /* }}} */ |
4492 | | |
4493 | | ZEND_API void zend_get_callable_zval_from_fcc(const zend_fcall_info_cache *fcc, zval *callable) |
4494 | 0 | { |
4495 | 0 | if (fcc->closure) { |
4496 | 0 | ZVAL_OBJ_COPY(callable, fcc->closure); |
4497 | 0 | } else if (fcc->function_handler->common.scope) { |
4498 | 0 | array_init(callable); |
4499 | 0 | if (fcc->object) { |
4500 | 0 | GC_ADDREF(fcc->object); |
4501 | 0 | add_next_index_object(callable, fcc->object); |
4502 | 0 | } else { |
4503 | 0 | add_next_index_str(callable, zend_string_copy(fcc->calling_scope->name)); |
4504 | 0 | } |
4505 | 0 | add_next_index_str(callable, zend_string_copy(fcc->function_handler->common.function_name)); |
4506 | 0 | } else { |
4507 | 0 | ZVAL_STR_COPY(callable, fcc->function_handler->common.function_name); |
4508 | 0 | } |
4509 | 0 | } |
4510 | | |
4511 | | ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */ |
4512 | 0 | { |
4513 | 0 | zend_string *lname; |
4514 | 0 | size_t name_len = strlen(module_name); |
4515 | 0 | zend_module_entry *module; |
4516 | |
|
4517 | 0 | lname = zend_string_alloc(name_len, 0); |
4518 | 0 | zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len); |
4519 | 0 | module = zend_hash_find_ptr(&module_registry, lname); |
4520 | 0 | zend_string_efree(lname); |
4521 | 0 | return module ? module->version : NULL; |
4522 | 0 | } |
4523 | | /* }}} */ |
4524 | | |
4525 | 43.3k | static zend_always_inline bool is_persistent_class(zend_class_entry *ce) { |
4526 | 43.3k | return (ce->type & ZEND_INTERNAL_CLASS) |
4527 | 43.3k | && ce->info.internal.module->type == MODULE_PERSISTENT; |
4528 | 43.3k | } |
4529 | | |
4530 | | ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_type type) /* {{{ */ |
4531 | 38.1k | { |
4532 | 38.1k | zend_property_info *property_info, *property_info_ptr; |
4533 | | |
4534 | 38.1k | if (ZEND_TYPE_IS_SET(type)) { |
4535 | 25.1k | ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS; |
4536 | | |
4537 | 25.1k | if (access_type & ZEND_ACC_READONLY) { |
4538 | 6.56k | ce->ce_flags |= ZEND_ACC_HAS_READONLY_PROPS; |
4539 | 6.56k | } |
4540 | 25.1k | } |
4541 | | |
4542 | 38.1k | if (ce->type == ZEND_INTERNAL_CLASS) { |
4543 | 960 | property_info = pemalloc(sizeof(zend_property_info), 1); |
4544 | 37.2k | } else { |
4545 | 37.2k | property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info)); |
4546 | 37.2k | if (Z_TYPE_P(property) == IS_CONSTANT_AST) { |
4547 | 2.92k | ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
4548 | 2.92k | if (access_type & ZEND_ACC_STATIC) { |
4549 | 1.90k | ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS; |
4550 | 1.90k | } else { |
4551 | 1.02k | ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES; |
4552 | 1.02k | } |
4553 | 2.92k | } |
4554 | 37.2k | } |
4555 | | |
4556 | 38.1k | if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) { |
4557 | 2.19k | zval_make_interned_string(property); |
4558 | 2.19k | } |
4559 | | |
4560 | 38.1k | if (!(access_type & ZEND_ACC_PPP_MASK)) { |
4561 | 3.27k | access_type |= ZEND_ACC_PUBLIC; |
4562 | 3.27k | } |
4563 | | /* Add the protected(set) bit for public readonly properties with no set visibility. */ |
4564 | 38.1k | if ((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK)) == (ZEND_ACC_PUBLIC|ZEND_ACC_READONLY)) { |
4565 | 6.46k | access_type |= ZEND_ACC_PROTECTED_SET; |
4566 | 31.7k | } else if (UNEXPECTED(access_type & ZEND_ACC_PPP_SET_MASK)) { |
4567 | 552 | if (!ZEND_TYPE_IS_SET(type)) { |
4568 | 12 | zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR, |
4569 | 12 | "Property with asymmetric visibility %s::$%s must have type", |
4570 | 12 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
4571 | 12 | } |
4572 | 540 | uint32_t get_visibility = zend_visibility_to_set_visibility(access_type & ZEND_ACC_PPP_MASK); |
4573 | 540 | uint32_t set_visibility = access_type & ZEND_ACC_PPP_SET_MASK; |
4574 | 540 | if (get_visibility > set_visibility) { |
4575 | 11 | zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR, |
4576 | 11 | "Visibility of property %s::$%s must not be weaker than set visibility", |
4577 | 11 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
4578 | 11 | } |
4579 | | /* Remove equivalent set visibility. */ |
4580 | 529 | if (((access_type & (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET)) == (ZEND_ACC_PUBLIC|ZEND_ACC_PUBLIC_SET)) |
4581 | 529 | || ((access_type & (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET)) == (ZEND_ACC_PROTECTED|ZEND_ACC_PROTECTED_SET)) |
4582 | 529 | || ((access_type & (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET)) == (ZEND_ACC_PRIVATE|ZEND_ACC_PRIVATE_SET))) { |
4583 | 160 | access_type &= ~ZEND_ACC_PPP_SET_MASK; |
4584 | 160 | } |
4585 | | /* private(set) properties are implicitly final. */ |
4586 | 529 | if (access_type & ZEND_ACC_PRIVATE_SET) { |
4587 | 303 | access_type |= ZEND_ACC_FINAL; |
4588 | 303 | } |
4589 | 529 | } |
4590 | | |
4591 | | /* Virtual properties have no backing storage, the offset should never be used. However, the |
4592 | | * virtual flag cannot be definitively determined at compile time. Allow using default values |
4593 | | * anyway, and assert after inheritance that the property is not actually virtual. */ |
4594 | 38.1k | if (access_type & ZEND_ACC_VIRTUAL) { |
4595 | 3.44k | if (Z_TYPE_P(property) == IS_UNDEF) { |
4596 | 3.31k | property_info->offset = (uint32_t)-1; |
4597 | 3.31k | goto skip_property_storage; |
4598 | 3.31k | } |
4599 | 3.44k | } |
4600 | 34.8k | if (access_type & ZEND_ACC_STATIC) { |
4601 | 5.40k | if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) { |
4602 | 4 | ZEND_ASSERT(property_info_ptr->flags & ZEND_ACC_STATIC); |
4603 | 4 | property_info->offset = property_info_ptr->offset; |
4604 | 4 | zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]); |
4605 | 4 | if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) { |
4606 | 4 | zend_string_release(property_info_ptr->doc_comment); |
4607 | 4 | } |
4608 | 4 | zend_hash_del(&ce->properties_info, name); |
4609 | 5.39k | } else { |
4610 | 5.39k | property_info->offset = ce->default_static_members_count++; |
4611 | 5.39k | ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS); |
4612 | 5.39k | } |
4613 | 5.40k | ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property); |
4614 | 5.40k | if (!ZEND_MAP_PTR(ce->static_members_table)) { |
4615 | 5.40k | if (ce->type == ZEND_INTERNAL_CLASS && |
4616 | 5.40k | ce->info.internal.module->type == MODULE_PERSISTENT) { |
4617 | 0 | ZEND_MAP_PTR_NEW(ce->static_members_table); |
4618 | 0 | } |
4619 | 5.40k | } |
4620 | 29.4k | } else { |
4621 | 29.4k | zval *property_default_ptr; |
4622 | 29.4k | if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL) { |
4623 | 0 | ZEND_ASSERT(!(property_info_ptr->flags & ZEND_ACC_STATIC)); |
4624 | 0 | property_info->offset = property_info_ptr->offset; |
4625 | 0 | zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]); |
4626 | 0 | if (property_info_ptr->doc_comment && property_info_ptr->ce == ce) { |
4627 | 0 | zend_string_release_ex(property_info_ptr->doc_comment, 1); |
4628 | 0 | } |
4629 | 0 | zend_hash_del(&ce->properties_info, name); |
4630 | |
|
4631 | 0 | ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS); |
4632 | 0 | ZEND_ASSERT(ce->properties_info_table != NULL); |
4633 | 0 | ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info; |
4634 | 29.4k | } else { |
4635 | 29.4k | property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count); |
4636 | 29.4k | ce->default_properties_count++; |
4637 | 29.4k | ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS); |
4638 | | |
4639 | | /* For user classes this is handled during linking */ |
4640 | 29.4k | if (ce->type == ZEND_INTERNAL_CLASS) { |
4641 | 848 | ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1); |
4642 | 848 | ce->properties_info_table[ce->default_properties_count - 1] = property_info; |
4643 | 848 | } |
4644 | 29.4k | } |
4645 | 29.4k | property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]; |
4646 | 29.4k | ZVAL_COPY_VALUE(property_default_ptr, property); |
4647 | 29.4k | Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0; |
4648 | 29.4k | } |
4649 | 38.1k | skip_property_storage: |
4650 | 38.1k | if (ce->type & ZEND_INTERNAL_CLASS) { |
4651 | | /* Must be interned to avoid ZTS data races */ |
4652 | 960 | if (is_persistent_class(ce)) { |
4653 | 960 | name = zend_new_interned_string(zend_string_copy(name)); |
4654 | 960 | } |
4655 | | |
4656 | 960 | if (Z_REFCOUNTED_P(property)) { |
4657 | 0 | zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted"); |
4658 | 0 | } |
4659 | 960 | } |
4660 | | |
4661 | 38.1k | if (access_type & ZEND_ACC_PUBLIC) { |
4662 | 33.9k | property_info->name = zend_string_copy(name); |
4663 | 33.9k | } else if (access_type & ZEND_ACC_PRIVATE) { |
4664 | 3.02k | property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce)); |
4665 | 3.02k | } else { |
4666 | 1.16k | ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED); |
4667 | 1.16k | property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce)); |
4668 | 1.16k | } |
4669 | | |
4670 | 38.1k | property_info->name = zend_new_interned_string(property_info->name); |
4671 | 38.1k | property_info->flags = access_type; |
4672 | 38.1k | property_info->doc_comment = doc_comment; |
4673 | 38.1k | property_info->attributes = NULL; |
4674 | 38.1k | property_info->prototype = property_info; |
4675 | 38.1k | property_info->hooks = NULL; |
4676 | 38.1k | property_info->ce = ce; |
4677 | 38.1k | property_info->type = type; |
4678 | | |
4679 | 38.1k | if (is_persistent_class(ce)) { |
4680 | 960 | zend_normalize_internal_type(&property_info->type); |
4681 | 960 | } |
4682 | | |
4683 | 38.1k | zend_hash_update_ptr(&ce->properties_info, name, property_info); |
4684 | | |
4685 | 38.1k | return property_info; |
4686 | 38.1k | } |
4687 | | /* }}} */ |
4688 | | |
4689 | | ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, bool strict) /* {{{ */ |
4690 | 12 | { |
4691 | 12 | if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) { |
4692 | 2 | zval_ptr_dtor(val); |
4693 | 2 | return FAILURE; |
4694 | 10 | } else { |
4695 | 10 | zend_safe_assign_to_variable_noref(&ref->val, val); |
4696 | 10 | return SUCCESS; |
4697 | 10 | } |
4698 | 12 | } |
4699 | | /* }}} */ |
4700 | | |
4701 | | ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */ |
4702 | 12 | { |
4703 | 12 | return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES()); |
4704 | 12 | } |
4705 | | /* }}} */ |
4706 | | |
4707 | | ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */ |
4708 | 0 | { |
4709 | 0 | zval tmp; |
4710 | |
|
4711 | 0 | ZVAL_NULL(&tmp); |
4712 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4713 | 0 | } |
4714 | | /* }}} */ |
4715 | | |
4716 | | ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, bool val) /* {{{ */ |
4717 | 0 | { |
4718 | 0 | zval tmp; |
4719 | |
|
4720 | 0 | ZVAL_BOOL(&tmp, val); |
4721 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4722 | 0 | } |
4723 | | /* }}} */ |
4724 | | |
4725 | | ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */ |
4726 | 0 | { |
4727 | 0 | zval tmp; |
4728 | |
|
4729 | 0 | ZVAL_LONG(&tmp, lval); |
4730 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4731 | 0 | } |
4732 | | /* }}} */ |
4733 | | |
4734 | | ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */ |
4735 | 0 | { |
4736 | 0 | zval tmp; |
4737 | |
|
4738 | 0 | ZVAL_DOUBLE(&tmp, dval); |
4739 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4740 | 0 | } |
4741 | | /* }}} */ |
4742 | | |
4743 | | ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */ |
4744 | 0 | { |
4745 | 0 | zval tmp; |
4746 | |
|
4747 | 0 | ZVAL_EMPTY_STRING(&tmp); |
4748 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4749 | 0 | } |
4750 | | /* }}} */ |
4751 | | |
4752 | | ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */ |
4753 | 0 | { |
4754 | 0 | zval tmp; |
4755 | |
|
4756 | 0 | ZVAL_STR(&tmp, str); |
4757 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4758 | 0 | } |
4759 | | /* }}} */ |
4760 | | |
4761 | | ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */ |
4762 | 7 | { |
4763 | 7 | zval tmp; |
4764 | | |
4765 | 7 | ZVAL_STRING(&tmp, string); |
4766 | 7 | return zend_try_assign_typed_ref(ref, &tmp); |
4767 | 7 | } |
4768 | | /* }}} */ |
4769 | | |
4770 | | ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */ |
4771 | 0 | { |
4772 | 0 | zval tmp; |
4773 | |
|
4774 | 0 | ZVAL_STRINGL(&tmp, string, len); |
4775 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4776 | 0 | } |
4777 | | /* }}} */ |
4778 | | |
4779 | | ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */ |
4780 | 5 | { |
4781 | 5 | zval tmp; |
4782 | | |
4783 | 5 | ZVAL_ARR(&tmp, arr); |
4784 | 5 | return zend_try_assign_typed_ref(ref, &tmp); |
4785 | 5 | } |
4786 | | /* }}} */ |
4787 | | |
4788 | | ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */ |
4789 | 0 | { |
4790 | 0 | zval tmp; |
4791 | |
|
4792 | 0 | ZVAL_RES(&tmp, res); |
4793 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4794 | 0 | } |
4795 | | /* }}} */ |
4796 | | |
4797 | | ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */ |
4798 | 0 | { |
4799 | 0 | zval tmp; |
4800 | |
|
4801 | 0 | ZVAL_COPY_VALUE(&tmp, zv); |
4802 | 0 | return zend_try_assign_typed_ref(ref, &tmp); |
4803 | 0 | } |
4804 | | /* }}} */ |
4805 | | |
4806 | | ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, bool strict) /* {{{ */ |
4807 | 0 | { |
4808 | 0 | zval tmp; |
4809 | |
|
4810 | 0 | ZVAL_COPY_VALUE(&tmp, zv); |
4811 | 0 | return zend_try_assign_typed_ref_ex(ref, &tmp, strict); |
4812 | 0 | } |
4813 | | /* }}} */ |
4814 | | |
4815 | | ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */ |
4816 | 0 | { |
4817 | 0 | zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0)); |
4818 | 0 | } |
4819 | | /* }}} */ |
4820 | | |
4821 | | ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */ |
4822 | 0 | { |
4823 | 0 | zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce)); |
4824 | 0 | zend_declare_property_ex(ce, key, property, access_type, NULL); |
4825 | 0 | zend_string_release(key); |
4826 | 0 | } |
4827 | | /* }}} */ |
4828 | | |
4829 | | ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */ |
4830 | 0 | { |
4831 | 0 | zval property; |
4832 | |
|
4833 | 0 | ZVAL_NULL(&property); |
4834 | 0 | zend_declare_property(ce, name, name_length, &property, access_type); |
4835 | 0 | } |
4836 | | /* }}} */ |
4837 | | |
4838 | | ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */ |
4839 | 0 | { |
4840 | 0 | zval property; |
4841 | |
|
4842 | 0 | ZVAL_BOOL(&property, value); |
4843 | 0 | zend_declare_property(ce, name, name_length, &property, access_type); |
4844 | 0 | } |
4845 | | /* }}} */ |
4846 | | |
4847 | | ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */ |
4848 | 0 | { |
4849 | 0 | zval property; |
4850 | |
|
4851 | 0 | ZVAL_LONG(&property, value); |
4852 | 0 | zend_declare_property(ce, name, name_length, &property, access_type); |
4853 | 0 | } |
4854 | | /* }}} */ |
4855 | | |
4856 | | ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */ |
4857 | 0 | { |
4858 | 0 | zval property; |
4859 | |
|
4860 | 0 | ZVAL_DOUBLE(&property, value); |
4861 | 0 | zend_declare_property(ce, name, name_length, &property, access_type); |
4862 | 0 | } |
4863 | | /* }}} */ |
4864 | | |
4865 | | ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */ |
4866 | 0 | { |
4867 | 0 | zval property; |
4868 | |
|
4869 | 0 | ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS)); |
4870 | 0 | zend_declare_property(ce, name, name_length, &property, access_type); |
4871 | 0 | } |
4872 | | /* }}} */ |
4873 | | |
4874 | | ZEND_API void zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type) /* {{{ */ |
4875 | 0 | { |
4876 | 0 | zval property; |
4877 | |
|
4878 | 0 | ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS)); |
4879 | 0 | zend_declare_property(ce, name, name_length, &property, access_type); |
4880 | 0 | } |
4881 | | /* }}} */ |
4882 | | |
4883 | | ZEND_API zend_class_constant *zend_declare_typed_class_constant(zend_class_entry *ce, zend_string *name, zval *value, int flags, zend_string *doc_comment, zend_type type) /* {{{ */ |
4884 | 10.6k | { |
4885 | 10.6k | zend_class_constant *c; |
4886 | | |
4887 | 10.6k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
4888 | 455 | if (!(flags & ZEND_ACC_PUBLIC)) { |
4889 | 1 | zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
4890 | 1 | } |
4891 | 455 | } |
4892 | | |
4893 | 10.6k | if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_CLASS))) { |
4894 | 6 | zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR, |
4895 | 6 | "A class constant must not be called 'class'; it is reserved for class name fetching"); |
4896 | 6 | } |
4897 | | |
4898 | 10.6k | if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) { |
4899 | 2.32k | zval_make_interned_string(value); |
4900 | 2.32k | } |
4901 | | |
4902 | 10.6k | if (ce->type == ZEND_INTERNAL_CLASS) { |
4903 | 2.70k | c = pemalloc(sizeof(zend_class_constant), 1); |
4904 | 2.70k | if (ZEND_TYPE_PURE_MASK(type) != MAY_BE_ANY) { |
4905 | 2.70k | ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(type, IS_RESOURCE) && "resource is not allowed in a zend_type"); |
4906 | 2.70k | } |
4907 | 7.90k | } else { |
4908 | 7.90k | c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant)); |
4909 | 7.90k | } |
4910 | 10.6k | ZVAL_COPY_VALUE(&c->value, value); |
4911 | 10.6k | ZEND_CLASS_CONST_FLAGS(c) = flags; |
4912 | 10.6k | c->doc_comment = doc_comment; |
4913 | 10.6k | c->attributes = NULL; |
4914 | 10.6k | c->ce = ce; |
4915 | 10.6k | c->type = type; |
4916 | | |
4917 | 10.6k | if (Z_TYPE_P(value) == IS_CONSTANT_AST) { |
4918 | 4.74k | ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
4919 | 4.74k | ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
4920 | 4.74k | if (ce->type == ZEND_INTERNAL_CLASS && !ZEND_MAP_PTR(ce->mutable_data)) { |
4921 | 80 | ZEND_MAP_PTR_NEW(ce->mutable_data); |
4922 | 80 | } |
4923 | 4.74k | } |
4924 | | |
4925 | 10.6k | if (!zend_hash_add_ptr(&ce->constants_table, name, c)) { |
4926 | 37 | zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR, |
4927 | 37 | "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
4928 | 37 | } |
4929 | | |
4930 | 10.5k | return c; |
4931 | 10.6k | } |
4932 | | |
4933 | | ZEND_API zend_class_constant *zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int flags, zend_string *doc_comment) |
4934 | 3.13k | { |
4935 | 3.13k | return zend_declare_typed_class_constant(ce, name, value, flags, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0)); |
4936 | 3.13k | } |
4937 | | |
4938 | | ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */ |
4939 | 0 | { |
4940 | 0 | zend_string *key; |
4941 | |
|
4942 | 0 | if (ce->type == ZEND_INTERNAL_CLASS) { |
4943 | 0 | key = zend_string_init_interned(name, name_length, 1); |
4944 | 0 | } else { |
4945 | 0 | key = zend_string_init(name, name_length, 0); |
4946 | 0 | } |
4947 | 0 | zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL); |
4948 | 0 | zend_string_release(key); |
4949 | 0 | } |
4950 | | /* }}} */ |
4951 | | |
4952 | | ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */ |
4953 | 0 | { |
4954 | 0 | zval constant; |
4955 | |
|
4956 | 0 | ZVAL_NULL(&constant); |
4957 | 0 | zend_declare_class_constant(ce, name, name_length, &constant); |
4958 | 0 | } |
4959 | | /* }}} */ |
4960 | | |
4961 | | ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */ |
4962 | 0 | { |
4963 | 0 | zval constant; |
4964 | |
|
4965 | 0 | ZVAL_LONG(&constant, value); |
4966 | 0 | zend_declare_class_constant(ce, name, name_length, &constant); |
4967 | 0 | } |
4968 | | /* }}} */ |
4969 | | |
4970 | | ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, bool value) /* {{{ */ |
4971 | 0 | { |
4972 | 0 | zval constant; |
4973 | |
|
4974 | 0 | ZVAL_BOOL(&constant, value); |
4975 | 0 | zend_declare_class_constant(ce, name, name_length, &constant); |
4976 | 0 | } |
4977 | | /* }}} */ |
4978 | | |
4979 | | ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */ |
4980 | 0 | { |
4981 | 0 | zval constant; |
4982 | |
|
4983 | 0 | ZVAL_DOUBLE(&constant, value); |
4984 | 0 | zend_declare_class_constant(ce, name, name_length, &constant); |
4985 | 0 | } |
4986 | | /* }}} */ |
4987 | | |
4988 | | ZEND_API void zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */ |
4989 | 0 | { |
4990 | 0 | zval constant; |
4991 | |
|
4992 | 0 | ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS)); |
4993 | 0 | zend_declare_class_constant(ce, name, name_length, &constant); |
4994 | 0 | } |
4995 | | /* }}} */ |
4996 | | |
4997 | | ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */ |
4998 | 0 | { |
4999 | 0 | zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value)); |
5000 | 0 | } |
5001 | | /* }}} */ |
5002 | | |
5003 | | ZEND_API void zend_update_property_ex(const zend_class_entry *scope, zend_object *object, zend_string *name, zval *value) /* {{{ */ |
5004 | 1.28M | { |
5005 | 1.28M | const zend_class_entry *old_scope = EG(fake_scope); |
5006 | | |
5007 | 1.28M | EG(fake_scope) = scope; |
5008 | | |
5009 | 1.28M | object->handlers->write_property(object, name, value, NULL); |
5010 | | |
5011 | 1.28M | EG(fake_scope) = old_scope; |
5012 | 1.28M | } |
5013 | | /* }}} */ |
5014 | | |
5015 | | ZEND_API void zend_update_property(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value) /* {{{ */ |
5016 | 27.7k | { |
5017 | 27.7k | zend_string *property; |
5018 | 27.7k | const zend_class_entry *old_scope = EG(fake_scope); |
5019 | | |
5020 | 27.7k | EG(fake_scope) = scope; |
5021 | | |
5022 | 27.7k | property = zend_string_init(name, name_length, 0); |
5023 | 27.7k | object->handlers->write_property(object, property, value, NULL); |
5024 | 27.7k | zend_string_release_ex(property, 0); |
5025 | | |
5026 | 27.7k | EG(fake_scope) = old_scope; |
5027 | 27.7k | } |
5028 | | /* }}} */ |
5029 | | |
5030 | | ZEND_API void zend_update_property_null(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */ |
5031 | 0 | { |
5032 | 0 | zval tmp; |
5033 | |
|
5034 | 0 | ZVAL_NULL(&tmp); |
5035 | 0 | zend_update_property(scope, object, name, name_length, &tmp); |
5036 | 0 | } |
5037 | | /* }}} */ |
5038 | | |
5039 | | ZEND_API void zend_unset_property(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */ |
5040 | 2 | { |
5041 | 2 | zend_string *property; |
5042 | 2 | const zend_class_entry *old_scope = EG(fake_scope); |
5043 | | |
5044 | 2 | EG(fake_scope) = scope; |
5045 | | |
5046 | 2 | property = zend_string_init(name, name_length, 0); |
5047 | 2 | object->handlers->unset_property(object, property, 0); |
5048 | 2 | zend_string_release_ex(property, 0); |
5049 | | |
5050 | 2 | EG(fake_scope) = old_scope; |
5051 | 2 | } |
5052 | | /* }}} */ |
5053 | | |
5054 | | ZEND_API void zend_update_property_bool(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */ |
5055 | 0 | { |
5056 | 0 | zval tmp; |
5057 | |
|
5058 | 0 | ZVAL_BOOL(&tmp, value); |
5059 | 0 | zend_update_property(scope, object, name, name_length, &tmp); |
5060 | 0 | } |
5061 | | /* }}} */ |
5062 | | |
5063 | | ZEND_API void zend_update_property_long(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */ |
5064 | 18 | { |
5065 | 18 | zval tmp; |
5066 | | |
5067 | 18 | ZVAL_LONG(&tmp, value); |
5068 | 18 | zend_update_property(scope, object, name, name_length, &tmp); |
5069 | 18 | } |
5070 | | /* }}} */ |
5071 | | |
5072 | | ZEND_API void zend_update_property_double(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value) /* {{{ */ |
5073 | 0 | { |
5074 | 0 | zval tmp; |
5075 | |
|
5076 | 0 | ZVAL_DOUBLE(&tmp, value); |
5077 | 0 | zend_update_property(scope, object, name, name_length, &tmp); |
5078 | 0 | } |
5079 | | /* }}} */ |
5080 | | |
5081 | | ZEND_API void zend_update_property_str(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_string *value) /* {{{ */ |
5082 | 0 | { |
5083 | 0 | zval tmp; |
5084 | |
|
5085 | 0 | ZVAL_STR(&tmp, value); |
5086 | 0 | zend_update_property(scope, object, name, name_length, &tmp); |
5087 | 0 | } |
5088 | | /* }}} */ |
5089 | | |
5090 | | ZEND_API void zend_update_property_string(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value) /* {{{ */ |
5091 | 0 | { |
5092 | 0 | zval tmp; |
5093 | |
|
5094 | 0 | ZVAL_STRING(&tmp, value); |
5095 | 0 | Z_SET_REFCOUNT(tmp, 0); |
5096 | 0 | zend_update_property(scope, object, name, name_length, &tmp); |
5097 | 0 | } |
5098 | | /* }}} */ |
5099 | | |
5100 | | ZEND_API void zend_update_property_stringl(const zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */ |
5101 | 9 | { |
5102 | 9 | zval tmp; |
5103 | | |
5104 | 9 | ZVAL_STRINGL(&tmp, value, value_len); |
5105 | 9 | Z_SET_REFCOUNT(tmp, 0); |
5106 | 9 | zend_update_property(scope, object, name, name_length, &tmp); |
5107 | 9 | } |
5108 | | /* }}} */ |
5109 | | |
5110 | | ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */ |
5111 | 12 | { |
5112 | 12 | zval *property, tmp; |
5113 | 12 | zend_property_info *prop_info; |
5114 | | |
5115 | 12 | if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) { |
5116 | 8 | if (UNEXPECTED(zend_update_class_constants(scope) != SUCCESS)) { |
5117 | 2 | return FAILURE; |
5118 | 2 | } |
5119 | 8 | } |
5120 | | |
5121 | 10 | const zend_class_entry *old_scope = EG(fake_scope); |
5122 | 10 | EG(fake_scope) = scope; |
5123 | 10 | property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info); |
5124 | 10 | EG(fake_scope) = old_scope; |
5125 | | |
5126 | 10 | if (!property) { |
5127 | 0 | return FAILURE; |
5128 | 0 | } |
5129 | | |
5130 | 10 | ZEND_ASSERT(!Z_ISREF_P(value)); |
5131 | 10 | Z_TRY_ADDREF_P(value); |
5132 | 10 | if (ZEND_TYPE_IS_SET(prop_info->type)) { |
5133 | 0 | ZVAL_COPY_VALUE(&tmp, value); |
5134 | 0 | if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) { |
5135 | 0 | Z_TRY_DELREF_P(value); |
5136 | 0 | return FAILURE; |
5137 | 0 | } |
5138 | 0 | value = &tmp; |
5139 | 0 | } |
5140 | | |
5141 | 10 | zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0); |
5142 | 10 | return SUCCESS; |
5143 | 10 | } |
5144 | | /* }}} */ |
5145 | | |
5146 | | ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */ |
5147 | 0 | { |
5148 | 0 | zend_string *key = zend_string_init(name, name_length, 0); |
5149 | 0 | zend_result retval = zend_update_static_property_ex(scope, key, value); |
5150 | 0 | zend_string_efree(key); |
5151 | 0 | return retval; |
5152 | 0 | } |
5153 | | /* }}} */ |
5154 | | |
5155 | | ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */ |
5156 | 0 | { |
5157 | 0 | zval tmp; |
5158 | |
|
5159 | 0 | ZVAL_NULL(&tmp); |
5160 | 0 | return zend_update_static_property(scope, name, name_length, &tmp); |
5161 | 0 | } |
5162 | | /* }}} */ |
5163 | | |
5164 | | ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */ |
5165 | 0 | { |
5166 | 0 | zval tmp; |
5167 | |
|
5168 | 0 | ZVAL_BOOL(&tmp, value); |
5169 | 0 | return zend_update_static_property(scope, name, name_length, &tmp); |
5170 | 0 | } |
5171 | | /* }}} */ |
5172 | | |
5173 | | ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */ |
5174 | 0 | { |
5175 | 0 | zval tmp; |
5176 | |
|
5177 | 0 | ZVAL_LONG(&tmp, value); |
5178 | 0 | return zend_update_static_property(scope, name, name_length, &tmp); |
5179 | 0 | } |
5180 | | /* }}} */ |
5181 | | |
5182 | | ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */ |
5183 | 0 | { |
5184 | 0 | zval tmp; |
5185 | |
|
5186 | 0 | ZVAL_DOUBLE(&tmp, value); |
5187 | 0 | return zend_update_static_property(scope, name, name_length, &tmp); |
5188 | 0 | } |
5189 | | /* }}} */ |
5190 | | |
5191 | | ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */ |
5192 | 0 | { |
5193 | 0 | zval tmp; |
5194 | |
|
5195 | 0 | ZVAL_STRING(&tmp, value); |
5196 | 0 | Z_SET_REFCOUNT(tmp, 0); |
5197 | 0 | return zend_update_static_property(scope, name, name_length, &tmp); |
5198 | 0 | } |
5199 | | /* }}} */ |
5200 | | |
5201 | | ZEND_API zend_result zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */ |
5202 | 0 | { |
5203 | 0 | zval tmp; |
5204 | |
|
5205 | 0 | ZVAL_STRINGL(&tmp, value, value_len); |
5206 | 0 | Z_SET_REFCOUNT(tmp, 0); |
5207 | 0 | return zend_update_static_property(scope, name, name_length, &tmp); |
5208 | 0 | } |
5209 | | /* }}} */ |
5210 | | |
5211 | | ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv) /* {{{ */ |
5212 | 27.5M | { |
5213 | 27.5M | zval *value; |
5214 | 27.5M | const zend_class_entry *old_scope = EG(fake_scope); |
5215 | | |
5216 | 27.5M | EG(fake_scope) = scope; |
5217 | | |
5218 | 27.5M | value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv); |
5219 | | |
5220 | 27.5M | EG(fake_scope) = old_scope; |
5221 | 27.5M | return value; |
5222 | 27.5M | } |
5223 | | /* }}} */ |
5224 | | |
5225 | | ZEND_API zval *zend_read_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, bool silent, zval *rv) /* {{{ */ |
5226 | 14 | { |
5227 | 14 | zval *value; |
5228 | 14 | zend_string *str; |
5229 | | |
5230 | 14 | str = zend_string_init(name, name_length, 0); |
5231 | 14 | value = zend_read_property_ex(scope, object, str, silent, rv); |
5232 | 14 | zend_string_release_ex(str, 0); |
5233 | 14 | return value; |
5234 | 14 | } |
5235 | | /* }}} */ |
5236 | | |
5237 | | ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, bool silent) /* {{{ */ |
5238 | 18 | { |
5239 | 18 | zval *property; |
5240 | 18 | const zend_class_entry *old_scope = EG(fake_scope); |
5241 | | |
5242 | 18 | EG(fake_scope) = scope; |
5243 | 18 | property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R); |
5244 | 18 | EG(fake_scope) = old_scope; |
5245 | | |
5246 | 18 | return property; |
5247 | 18 | } |
5248 | | /* }}} */ |
5249 | | |
5250 | | ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, bool silent) /* {{{ */ |
5251 | 0 | { |
5252 | 0 | zend_string *key = zend_string_init(name, name_length, 0); |
5253 | 0 | zval *property = zend_read_static_property_ex(scope, key, silent); |
5254 | 0 | zend_string_efree(key); |
5255 | 0 | return property; |
5256 | 0 | } |
5257 | | /* }}} */ |
5258 | | |
5259 | | ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */ |
5260 | 10 | { |
5261 | 10 | current->handling = EG(error_handling); |
5262 | 10 | current->exception = EG(exception_class); |
5263 | 10 | } |
5264 | | /* }}} */ |
5265 | | |
5266 | | ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */ |
5267 | 10 | { |
5268 | 10 | if (current) { |
5269 | 10 | zend_save_error_handling(current); |
5270 | 10 | } |
5271 | 10 | ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL); |
5272 | 10 | EG(error_handling) = error_handling; |
5273 | 10 | EG(exception_class) = exception_class; |
5274 | 10 | } |
5275 | | /* }}} */ |
5276 | | |
5277 | | ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */ |
5278 | 10 | { |
5279 | 10 | EG(error_handling) = saved->handling; |
5280 | 10 | EG(exception_class) = saved->exception; |
5281 | 10 | } |
5282 | | /* }}} */ |
5283 | | |
5284 | | ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case) /* {{{ */ |
5285 | 410 | { |
5286 | 410 | if (ce->ce_flags & ZEND_ACC_TRAIT) { |
5287 | 5 | return upper_case ? "Trait" : "trait"; |
5288 | 405 | } else if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
5289 | 52 | return upper_case ? "Interface" : "interface"; |
5290 | 353 | } else if (ce->ce_flags & ZEND_ACC_ENUM) { |
5291 | 49 | return upper_case ? "Enum" : "enum"; |
5292 | 304 | } else { |
5293 | 304 | return upper_case ? "Class" : "class"; |
5294 | 304 | } |
5295 | 410 | } |
5296 | | /* }}} */ |
5297 | | |
5298 | | ZEND_API bool zend_is_iterable(const zval *iterable) /* {{{ */ |
5299 | 51 | { |
5300 | 51 | switch (Z_TYPE_P(iterable)) { |
5301 | 0 | case IS_ARRAY: |
5302 | 0 | return 1; |
5303 | 48 | case IS_OBJECT: |
5304 | 48 | return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable); |
5305 | 3 | default: |
5306 | 3 | return 0; |
5307 | 51 | } |
5308 | 51 | } |
5309 | | /* }}} */ |
5310 | | |
5311 | | ZEND_API bool zend_is_countable(const zval *countable) /* {{{ */ |
5312 | 0 | { |
5313 | 0 | switch (Z_TYPE_P(countable)) { |
5314 | 0 | case IS_ARRAY: |
5315 | 0 | return 1; |
5316 | 0 | case IS_OBJECT: |
5317 | 0 | if (Z_OBJ_HT_P(countable)->count_elements) { |
5318 | 0 | return 1; |
5319 | 0 | } |
5320 | | |
5321 | 0 | return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable); |
5322 | 0 | default: |
5323 | 0 | return 0; |
5324 | 0 | } |
5325 | 0 | } |
5326 | | /* }}} */ |
5327 | | |
5328 | 25 | static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) { |
5329 | 25 | zend_ast *ast; |
5330 | 25 | zend_arena *ast_arena; |
5331 | | |
5332 | 25 | zend_string *code = zend_string_concat3( |
5333 | 25 | "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1); |
5334 | | |
5335 | 25 | ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC()); |
5336 | 25 | zend_string_release(code); |
5337 | | |
5338 | 25 | if (!ast) { |
5339 | 0 | return FAILURE; |
5340 | 0 | } |
5341 | | |
5342 | 25 | zend_ast_list *statement_list = zend_ast_get_list(ast); |
5343 | 25 | zend_ast **const_expr_ast_ptr = &statement_list->child[0]; |
5344 | | |
5345 | 25 | zend_arena *original_ast_arena = CG(ast_arena); |
5346 | 25 | uint32_t original_compiler_options = CG(compiler_options); |
5347 | 25 | zend_file_context original_file_context; |
5348 | 25 | CG(ast_arena) = ast_arena; |
5349 | | /* Disable constant substitution, to make getDefaultValueConstant() work. */ |
5350 | 25 | CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION; |
5351 | 25 | zend_file_context_begin(&original_file_context); |
5352 | 25 | zend_const_expr_to_zval(default_value_zval, const_expr_ast_ptr, /* allow_dynamic */ true); |
5353 | 25 | CG(ast_arena) = original_ast_arena; |
5354 | 25 | CG(compiler_options) = original_compiler_options; |
5355 | 25 | zend_file_context_end(&original_file_context); |
5356 | | |
5357 | 25 | zend_ast_destroy(ast); |
5358 | 25 | zend_arena_destroy(ast_arena); |
5359 | | |
5360 | 25 | return SUCCESS; |
5361 | 25 | } |
5362 | | |
5363 | 7 | static zend_string *try_parse_string(const char *str, size_t len, char quote) { |
5364 | 7 | if (len == 0) { |
5365 | 0 | return ZSTR_EMPTY_ALLOC(); |
5366 | 0 | } |
5367 | | |
5368 | 14 | for (size_t i = 0; i < len; i++) { |
5369 | 7 | if (str[i] == '\\' || str[i] == quote) { |
5370 | 0 | return NULL; |
5371 | 0 | } |
5372 | 7 | } |
5373 | 7 | return zend_string_init(str, len, 0); |
5374 | 7 | } |
5375 | | |
5376 | | ZEND_API zend_result zend_get_default_from_internal_arg_info( |
5377 | | zval *default_value_zval, zend_internal_arg_info *arg_info) |
5378 | 173 | { |
5379 | 173 | const char *default_value = arg_info->default_value; |
5380 | 173 | if (!default_value) { |
5381 | 5 | return FAILURE; |
5382 | 5 | } |
5383 | | |
5384 | | /* Avoid going through the full AST machinery for some simple and common cases. */ |
5385 | 168 | size_t default_value_len = strlen(default_value); |
5386 | 168 | zend_ulong lval; |
5387 | 168 | if (default_value_len == sizeof("null")-1 |
5388 | 168 | && !memcmp(default_value, "null", sizeof("null")-1)) { |
5389 | 119 | ZVAL_NULL(default_value_zval); |
5390 | 119 | return SUCCESS; |
5391 | 119 | } else if (default_value_len == sizeof("true")-1 |
5392 | 49 | && !memcmp(default_value, "true", sizeof("true")-1)) { |
5393 | 0 | ZVAL_TRUE(default_value_zval); |
5394 | 0 | return SUCCESS; |
5395 | 49 | } else if (default_value_len == sizeof("false")-1 |
5396 | 49 | && !memcmp(default_value, "false", sizeof("false")-1)) { |
5397 | 17 | ZVAL_FALSE(default_value_zval); |
5398 | 17 | return SUCCESS; |
5399 | 32 | } else if (default_value_len >= 2 |
5400 | 32 | && (default_value[0] == '\'' || default_value[0] == '"') |
5401 | 32 | && default_value[default_value_len - 1] == default_value[0]) { |
5402 | 7 | zend_string *str = try_parse_string( |
5403 | 7 | default_value + 1, default_value_len - 2, default_value[0]); |
5404 | 7 | if (str) { |
5405 | 7 | ZVAL_STR(default_value_zval, str); |
5406 | 7 | return SUCCESS; |
5407 | 7 | } |
5408 | 25 | } else if (default_value_len == sizeof("[]")-1 |
5409 | 25 | && !memcmp(default_value, "[]", sizeof("[]")-1)) { |
5410 | 0 | ZVAL_EMPTY_ARRAY(default_value_zval); |
5411 | 0 | return SUCCESS; |
5412 | 25 | } else if (ZEND_HANDLE_NUMERIC_STR(default_value, default_value_len, lval)) { |
5413 | 0 | ZVAL_LONG(default_value_zval, lval); |
5414 | 0 | return SUCCESS; |
5415 | 0 | } |
5416 | | |
5417 | | #if 0 |
5418 | | fprintf(stderr, "Evaluating %s via AST\n", default_value); |
5419 | | #endif |
5420 | 25 | return get_default_via_ast(default_value_zval, default_value); |
5421 | 168 | } |