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