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