/src/php-src/Zend/zend_closures.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © Zend Technologies Ltd., a subsidiary company of | |
6 | | | Perforce Software, Inc., and Contributors. | |
7 | | +----------------------------------------------------------------------+ |
8 | | | This source file is subject to the Modified BSD License that is | |
9 | | | bundled with this package in the file LICENSE, and is available | |
10 | | | through the World Wide Web at <https://www.php.net/license/>. | |
11 | | | | |
12 | | | SPDX-License-Identifier: BSD-3-Clause | |
13 | | +----------------------------------------------------------------------+ |
14 | | | Authors: Christian Seiler <chris_se@gmx.net> | |
15 | | | Dmitry Stogov <dmitry@php.net> | |
16 | | | Marcus Boerger <helly@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include "zend.h" |
21 | | #include "zend_API.h" |
22 | | #include "zend_closures.h" |
23 | | #include "zend_exceptions.h" |
24 | | #include "zend_interfaces.h" |
25 | | #include "zend_objects.h" |
26 | | #include "zend_objects_API.h" |
27 | | #include "zend_globals.h" |
28 | | #include "zend_closures_arginfo.h" |
29 | | |
30 | | /* Closure is a PFA */ |
31 | 4.45k | #define ZEND_PARTIAL OBJ_EXTRA_FLAG_PRIV_1 |
32 | | /* Closure is a PFA of a Closure. Rebinding the PFA requires rebinding the inner Closure. */ |
33 | 2.62k | #define ZEND_PARTIAL_OF_CLOSURE OBJ_EXTRA_FLAG_PRIV_2 |
34 | | |
35 | | typedef struct _zend_closure { |
36 | | zend_object std; |
37 | | zend_function func; |
38 | | zval this_ptr; |
39 | | zend_class_entry *called_scope; |
40 | | zif_handler orig_internal_handler; |
41 | | } zend_closure; |
42 | | |
43 | | /* non-static since it needs to be referenced */ |
44 | | ZEND_API zend_class_entry *zend_ce_closure; |
45 | | static zend_object_handlers closure_handlers; |
46 | | |
47 | | static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only); |
48 | | static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr, bool is_fake, uint32_t flags); |
49 | | |
50 | | static inline uint32_t zend_closure_flags(const zend_closure *closure) |
51 | 1.89k | { |
52 | 1.89k | return closure->std.extra_flags & (ZEND_PARTIAL|ZEND_PARTIAL_OF_CLOSURE); |
53 | 1.89k | } |
54 | | |
55 | | static inline bool zend_closure_is_fake(const zend_closure *closure) |
56 | 611 | { |
57 | 611 | return closure->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE; |
58 | 611 | } |
59 | | |
60 | | ZEND_METHOD(Closure, __invoke) /* {{{ */ |
61 | 303 | { |
62 | 303 | zend_function *func = EX(func); |
63 | 303 | zval *args; |
64 | 303 | uint32_t num_args; |
65 | 303 | HashTable *named_args; |
66 | | |
67 | 909 | ZEND_PARSE_PARAMETERS_START(0, -1) |
68 | 909 | Z_PARAM_VARIADIC_WITH_NAMED(args, num_args, named_args) |
69 | 909 | ZEND_PARSE_PARAMETERS_END(); |
70 | | |
71 | 303 | zend_fcall_info_cache fcc = { |
72 | 303 | .closure = Z_OBJ_P(ZEND_THIS), |
73 | 303 | }; |
74 | 303 | zend_closure_get_closure(Z_OBJ_P(ZEND_THIS), &fcc.calling_scope, &fcc.function_handler, &fcc.object, false); |
75 | 303 | fcc.called_scope = fcc.calling_scope; |
76 | 303 | zend_call_known_fcc(&fcc, return_value, num_args, args, named_args); |
77 | | |
78 | | /* destruct the function also, then - we have allocated it in get_method */ |
79 | 303 | zend_string_release_ex(func->internal_function.function_name, 0); |
80 | 303 | efree(func); |
81 | | |
82 | | /* Set the func pointer to NULL. Prior to PHP 8.3, this was only done for debug builds, |
83 | | * because debug builds check certain properties after the call and needed to know this |
84 | | * had been freed. |
85 | | * However, extensions can proxy zend_execute_internal, and it's a bit surprising to have |
86 | | * an invalid func pointer sitting on there, so this was changed in PHP 8.3. |
87 | | */ |
88 | 303 | execute_data->func = NULL; |
89 | 303 | } |
90 | | /* }}} */ |
91 | | |
92 | | static bool zend_valid_closure_binding( |
93 | | zend_closure *closure, zval *newthis, zend_class_entry *scope) /* {{{ */ |
94 | 1.84k | { |
95 | 1.84k | zend_function *func = &closure->func; |
96 | | // TODO: rename variable |
97 | 1.84k | bool is_fake_closure = (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0 |
98 | 1.69k | || (closure->std.extra_flags & ZEND_PARTIAL); |
99 | 1.84k | if (newthis) { |
100 | 1.53k | if (func->common.fn_flags & ZEND_ACC_STATIC) { |
101 | 57 | zend_error(E_WARNING, "Cannot bind an instance to a static closure, this will be an error in PHP 9"); |
102 | 57 | return false; |
103 | 57 | } |
104 | | |
105 | 1.48k | if (is_fake_closure && func->common.scope && |
106 | 179 | !instanceof_function(Z_OBJCE_P(newthis), func->common.scope)) { |
107 | | /* Binding incompatible $this to an internal method is not supported. */ |
108 | 21 | zend_error(E_WARNING, "Cannot bind method %s::%s() to object of class %s, this will be an error in PHP 9", |
109 | 21 | ZSTR_VAL(func->common.scope->name), |
110 | 21 | ZSTR_VAL(func->common.function_name), |
111 | 21 | ZSTR_VAL(Z_OBJCE_P(newthis)->name)); |
112 | 21 | return false; |
113 | 21 | } |
114 | 1.48k | } else if (is_fake_closure && func->common.scope |
115 | 25 | && !(func->common.fn_flags & ZEND_ACC_STATIC)) { |
116 | 15 | zend_error(E_WARNING, "Cannot unbind $this of method, this will be an error in PHP 9"); |
117 | 15 | return false; |
118 | 291 | } else if (!is_fake_closure && !Z_ISUNDEF(closure->this_ptr) |
119 | 38 | && (func->common.fn_flags & ZEND_ACC_USES_THIS)) { |
120 | 33 | zend_error(E_WARNING, "Cannot unbind $this of closure using $this, this will be an error in PHP 9"); |
121 | 33 | return false; |
122 | 33 | } |
123 | | |
124 | 1.71k | if (scope && scope != func->common.scope && scope->type == ZEND_INTERNAL_CLASS) { |
125 | | /* rebinding to internal class is not allowed */ |
126 | 414 | zend_error(E_WARNING, "Cannot bind closure to scope of internal class %s, this will be an error in PHP 9", |
127 | 414 | ZSTR_VAL(scope->name)); |
128 | 414 | return false; |
129 | 414 | } |
130 | | |
131 | 1.30k | if (is_fake_closure && scope != func->common.scope) { |
132 | 14 | if (func->common.scope == NULL) { |
133 | 4 | zend_error(E_WARNING, "Cannot rebind scope of closure created from function, this will be an error in PHP 9"); |
134 | 10 | } else { |
135 | 10 | zend_error(E_WARNING, "Cannot rebind scope of closure created from method, this will be an error in PHP 9"); |
136 | 10 | } |
137 | 14 | return false; |
138 | 14 | } |
139 | | |
140 | 1.29k | return true; |
141 | 1.30k | } |
142 | | /* }}} */ |
143 | | |
144 | | /* {{{ Call closure, binding to a given object with its class as the scope */ |
145 | | ZEND_METHOD(Closure, call) |
146 | 1.12k | { |
147 | 1.12k | zval *newthis, closure_result; |
148 | 1.12k | zend_closure *closure; |
149 | 1.12k | zend_fcall_info fci; |
150 | 1.12k | zend_fcall_info_cache fci_cache; |
151 | 1.12k | zend_object *newobj; |
152 | 1.12k | zend_class_entry *newclass; |
153 | | |
154 | 1.12k | fci.param_count = 0; |
155 | 1.12k | fci.params = NULL; |
156 | | |
157 | 3.37k | ZEND_PARSE_PARAMETERS_START(1, -1) |
158 | 4.49k | Z_PARAM_OBJECT(newthis) |
159 | 1.11k | Z_PARAM_VARIADIC_WITH_NAMED(fci.params, fci.param_count, fci.named_params) |
160 | 1.12k | ZEND_PARSE_PARAMETERS_END(); |
161 | | |
162 | 1.11k | closure = (zend_closure *) Z_OBJ_P(ZEND_THIS); |
163 | | |
164 | 1.11k | newobj = Z_OBJ_P(newthis); |
165 | 1.11k | newclass = newobj->ce; |
166 | | |
167 | 1.11k | if (!zend_valid_closure_binding(closure, newthis, newclass)) { |
168 | 424 | return; |
169 | 424 | } |
170 | | |
171 | 695 | fci_cache.called_scope = newclass; |
172 | 695 | fci_cache.object = fci.object = newobj; |
173 | | |
174 | 695 | fci.size = sizeof(fci); |
175 | 695 | fci.consumed_args = 0; |
176 | 695 | ZVAL_OBJ(&fci.function_name, &closure->std); |
177 | 695 | ZVAL_UNDEF(&closure_result); |
178 | 695 | fci.retval = &closure_result; |
179 | | |
180 | 695 | if (closure->func.common.fn_flags & ZEND_ACC_GENERATOR) { |
181 | 6 | zval new_closure; |
182 | 6 | zend_create_closure_ex(&new_closure, &closure->func, newclass, |
183 | 6 | closure->called_scope, newthis, |
184 | 6 | zend_closure_is_fake(closure), zend_closure_flags(closure)); |
185 | 6 | closure = (zend_closure *) Z_OBJ(new_closure); |
186 | 6 | fci_cache.function_handler = &closure->func; |
187 | | |
188 | 6 | zend_call_function(&fci, &fci_cache); |
189 | | |
190 | | /* copied upon generator creation */ |
191 | 6 | GC_DELREF(&closure->std); |
192 | 689 | } else { |
193 | 689 | zend_closure *fake_closure; |
194 | 689 | zend_function *my_function; |
195 | | |
196 | 689 | fake_closure = emalloc(sizeof(zend_closure)); |
197 | 689 | memset(&fake_closure->std, 0, sizeof(fake_closure->std)); |
198 | 689 | fake_closure->std.gc.refcount = 1; |
199 | 689 | fake_closure->std.gc.u.type_info = GC_NULL; |
200 | 689 | fake_closure->std.extra_flags = zend_closure_flags(closure); |
201 | 689 | ZVAL_UNDEF(&fake_closure->this_ptr); |
202 | 689 | fake_closure->called_scope = NULL; |
203 | 689 | my_function = &fake_closure->func; |
204 | 689 | if (ZEND_USER_CODE(closure->func.type)) { |
205 | 682 | memcpy(my_function, &closure->func, sizeof(zend_op_array)); |
206 | 682 | } else { |
207 | 7 | memcpy(my_function, &closure->func, sizeof(zend_internal_function)); |
208 | 7 | } |
209 | | /* use scope of passed object */ |
210 | 689 | my_function->common.scope = newclass; |
211 | 689 | if (closure->func.type == ZEND_INTERNAL_FUNCTION) { |
212 | 7 | my_function->internal_function.handler = closure->orig_internal_handler; |
213 | 7 | } |
214 | 689 | fci_cache.function_handler = my_function; |
215 | | |
216 | | /* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */ |
217 | 689 | if (ZEND_USER_CODE(my_function->type) |
218 | 682 | && (closure->func.common.scope != newclass |
219 | 682 | || (closure->func.common.fn_flags & ZEND_ACC_HEAP_RT_CACHE))) { |
220 | 682 | void *ptr; |
221 | | |
222 | 682 | my_function->op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE; |
223 | 682 | ptr = emalloc(my_function->op_array.cache_size); |
224 | 682 | ZEND_MAP_PTR_INIT(my_function->op_array.run_time_cache, ptr); |
225 | 682 | memset(ptr, 0, my_function->op_array.cache_size); |
226 | 682 | } |
227 | | |
228 | 689 | zend_call_function(&fci, &fci_cache); |
229 | | |
230 | 689 | if (ZEND_USER_CODE(my_function->type)) { |
231 | 682 | if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) { |
232 | 682 | efree(ZEND_MAP_PTR(my_function->op_array.run_time_cache)); |
233 | 682 | } |
234 | 682 | } |
235 | 689 | efree_size(fake_closure, sizeof(zend_closure)); |
236 | 689 | } |
237 | | |
238 | 695 | if (Z_TYPE(closure_result) != IS_UNDEF) { |
239 | 688 | if (Z_ISREF(closure_result)) { |
240 | 10 | zend_unwrap_reference(&closure_result); |
241 | 10 | } |
242 | 688 | ZVAL_COPY_VALUE(return_value, &closure_result); |
243 | 688 | } |
244 | 695 | } |
245 | | /* }}} */ |
246 | | |
247 | | static zend_result do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, zend_object *scope_obj, zend_string *scope_str) |
248 | 730 | { |
249 | 730 | zend_class_entry *ce, *called_scope; |
250 | 730 | zend_closure *closure = (zend_closure *) Z_OBJ_P(zclosure); |
251 | | |
252 | 730 | if (scope_obj) { |
253 | 24 | ce = scope_obj->ce; |
254 | 706 | } else if (scope_str) { |
255 | 634 | if (zend_string_equals(scope_str, ZSTR_KNOWN(ZEND_STR_STATIC))) { |
256 | 358 | ce = closure->func.common.scope; |
257 | 358 | } else if ((ce = zend_lookup_class(scope_str)) == NULL) { |
258 | 5 | zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(scope_str)); |
259 | 5 | RETVAL_NULL(); |
260 | 5 | return FAILURE; |
261 | 5 | } |
262 | 634 | } else { |
263 | 72 | ce = NULL; |
264 | 72 | } |
265 | | |
266 | 725 | if (!zend_valid_closure_binding(closure, newthis, ce)) { |
267 | 130 | return FAILURE; |
268 | 130 | } |
269 | | |
270 | 595 | if (newthis) { |
271 | 337 | called_scope = Z_OBJCE_P(newthis); |
272 | 337 | } else { |
273 | 258 | called_scope = ce; |
274 | 258 | } |
275 | | |
276 | 595 | zend_create_closure_ex(return_value, &closure->func, ce, called_scope, newthis, |
277 | 595 | zend_closure_is_fake(closure), zend_closure_flags(closure)); |
278 | | |
279 | 595 | if (zend_closure_flags(closure) & ZEND_PARTIAL_OF_CLOSURE) { |
280 | | /* Re-bind the inner closure */ |
281 | | |
282 | 20 | zend_closure *new_closure = (zend_closure*)Z_OBJ_P(return_value); |
283 | 20 | HashTable *static_variables = ZEND_MAP_PTR_GET(new_closure->func.op_array.static_variables_ptr); |
284 | 20 | ZEND_ASSERT(static_variables->nNumOfElements > 0); |
285 | 20 | zval *inner = &static_variables->arData[0].val; |
286 | 20 | ZEND_ASSERT(Z_TYPE_P(inner) == IS_OBJECT && Z_OBJCE_P(inner) == zend_ce_closure); |
287 | | |
288 | 20 | zval new_inner; |
289 | 20 | if (do_closure_bind(&new_inner, inner, newthis, scope_obj, scope_str) != SUCCESS) { |
290 | | /* Should not happen, as we have already validated arguments and the |
291 | | * inner closure should have the same constraints. */ |
292 | 0 | ZEND_UNREACHABLE(); |
293 | 0 | zval_ptr_dtor(return_value); |
294 | 0 | ZVAL_NULL(return_value); |
295 | 0 | return FAILURE; |
296 | 0 | } |
297 | | |
298 | 20 | zend_object *garbage = Z_OBJ_P(inner); |
299 | 20 | ZVAL_COPY_VALUE(inner, &new_inner); |
300 | 20 | zend_object_release(garbage); |
301 | 20 | } |
302 | | |
303 | 595 | return SUCCESS; |
304 | 595 | } |
305 | | |
306 | | /* {{{ Create a closure from another one and bind to another object and scope */ |
307 | | ZEND_METHOD(Closure, bind) |
308 | 99 | { |
309 | 99 | zval *zclosure, *newthis; |
310 | 99 | zend_object *scope_obj = NULL; |
311 | 99 | zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC); |
312 | | |
313 | 297 | ZEND_PARSE_PARAMETERS_START(2, 3) |
314 | 396 | Z_PARAM_OBJECT_OF_CLASS(zclosure, zend_ce_closure) |
315 | 495 | Z_PARAM_OBJECT_OR_NULL(newthis) |
316 | 99 | Z_PARAM_OPTIONAL |
317 | 480 | Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str) |
318 | 480 | ZEND_PARSE_PARAMETERS_END(); |
319 | | |
320 | 99 | do_closure_bind(return_value, zclosure, newthis, scope_obj, scope_str); |
321 | 99 | } |
322 | | |
323 | | /* {{{ Create a closure from another one and bind to another object and scope */ |
324 | | ZEND_METHOD(Closure, bindTo) |
325 | 617 | { |
326 | 617 | zval *newthis; |
327 | 617 | zend_object *scope_obj = NULL; |
328 | 617 | zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC); |
329 | | |
330 | 1.85k | ZEND_PARSE_PARAMETERS_START(1, 2) |
331 | 2.46k | Z_PARAM_OBJECT_OR_NULL(newthis) |
332 | 616 | Z_PARAM_OPTIONAL |
333 | 2.15k | Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str) |
334 | 2.15k | ZEND_PARSE_PARAMETERS_END(); |
335 | | |
336 | 611 | do_closure_bind(return_value, ZEND_THIS, newthis, scope_obj, scope_str); |
337 | 611 | } |
338 | | |
339 | | static void zend_copy_parameters_array(const uint32_t param_count, HashTable *argument_array) /* {{{ */ |
340 | 249 | { |
341 | 249 | zval *param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1); |
342 | | |
343 | 249 | ZEND_ASSERT(param_count <= ZEND_CALL_NUM_ARGS(EG(current_execute_data))); |
344 | | |
345 | 859 | for (uint32_t i = 0; i < param_count; i++) { |
346 | 610 | Z_TRY_ADDREF_P(param_ptr); |
347 | 610 | zend_hash_next_index_insert_new(argument_array, param_ptr); |
348 | 610 | param_ptr++; |
349 | 610 | } |
350 | 249 | } |
351 | | |
352 | 405 | static ZEND_NAMED_FUNCTION(zend_closure_call_magic) /* {{{ */ { |
353 | 405 | zend_fcall_info fci; |
354 | 405 | zend_fcall_info_cache fcc; |
355 | 405 | zval params[2]; |
356 | | |
357 | 405 | memset(&fci, 0, sizeof(zend_fcall_info)); |
358 | 405 | memset(&fcc, 0, sizeof(zend_fcall_info_cache)); |
359 | | |
360 | 405 | fci.size = sizeof(zend_fcall_info); |
361 | 405 | fci.retval = return_value; |
362 | | |
363 | 405 | fcc.function_handler = (EX(func)->internal_function.fn_flags & ZEND_ACC_STATIC) ? |
364 | 340 | EX(func)->internal_function.scope->__callstatic : EX(func)->internal_function.scope->__call; |
365 | 405 | fci.named_params = NULL; |
366 | 405 | fci.params = params; |
367 | 405 | fci.param_count = 2; |
368 | 405 | ZVAL_STR(&fci.params[0], EX(func)->common.function_name); |
369 | 405 | if (EX_CALL_INFO() & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { |
370 | 40 | zend_string *name; |
371 | 40 | zval *named_param_zval; |
372 | 40 | array_init_size(&fci.params[1], ZEND_NUM_ARGS() + zend_hash_num_elements(EX(extra_named_params))); |
373 | | /* Avoid conversion from packed to mixed later. */ |
374 | 40 | zend_hash_real_init_mixed(Z_ARRVAL(fci.params[1])); |
375 | 40 | zend_copy_parameters_array(ZEND_NUM_ARGS(), Z_ARRVAL(fci.params[1])); |
376 | 240 | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EX(extra_named_params), name, named_param_zval) { |
377 | 240 | Z_TRY_ADDREF_P(named_param_zval); |
378 | 240 | zend_hash_add_new(Z_ARRVAL(fci.params[1]), name, named_param_zval); |
379 | 240 | } ZEND_HASH_FOREACH_END(); |
380 | 365 | } else if (ZEND_NUM_ARGS()) { |
381 | 209 | array_init_size(&fci.params[1], ZEND_NUM_ARGS()); |
382 | 209 | zend_copy_parameters_array(ZEND_NUM_ARGS(), Z_ARRVAL(fci.params[1])); |
383 | 209 | } else { |
384 | 156 | ZVAL_EMPTY_ARRAY(&fci.params[1]); |
385 | 156 | } |
386 | | |
387 | 405 | fcc.object = fci.object = Z_OBJ_P(ZEND_THIS); |
388 | 405 | fcc.called_scope = zend_get_called_scope(EG(current_execute_data)); |
389 | | |
390 | 405 | zend_call_function(&fci, &fcc); |
391 | 405 | zend_return_unwrap_ref(EG(current_execute_data), return_value); |
392 | 405 | zval_ptr_dtor(&fci.params[1]); |
393 | 405 | } |
394 | | /* }}} */ |
395 | | |
396 | 672 | static zend_result zend_create_closure_from_callable(zval *return_value, zval *callable, char **error) /* {{{ */ { |
397 | 672 | zend_fcall_info_cache fcc; |
398 | 672 | zend_function *mptr; |
399 | 672 | zval instance; |
400 | 672 | zend_internal_function call; |
401 | | |
402 | 672 | if (!zend_is_callable_ex(callable, NULL, 0, NULL, &fcc, error)) { |
403 | 80 | return FAILURE; |
404 | 80 | } |
405 | | |
406 | 592 | mptr = fcc.function_handler; |
407 | 592 | if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) { |
408 | | /* For Closure::fromCallable([$closure, "__invoke"]) return $closure. */ |
409 | 157 | if (fcc.object && fcc.object->ce == zend_ce_closure |
410 | 42 | && zend_string_equals(mptr->common.function_name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) { |
411 | 42 | RETVAL_OBJ_COPY(fcc.object); |
412 | 42 | zend_free_trampoline(mptr); |
413 | 42 | return SUCCESS; |
414 | 42 | } |
415 | | |
416 | 115 | if (!mptr->common.scope) { |
417 | 0 | return FAILURE; |
418 | 0 | } |
419 | 115 | if (mptr->common.fn_flags & ZEND_ACC_STATIC) { |
420 | 7 | if (!mptr->common.scope->__callstatic) { |
421 | 0 | return FAILURE; |
422 | 0 | } |
423 | 108 | } else { |
424 | 108 | if (!mptr->common.scope->__call) { |
425 | 0 | return FAILURE; |
426 | 0 | } |
427 | 108 | } |
428 | | |
429 | 115 | memset(&call, 0, sizeof(zend_internal_function)); |
430 | 115 | call.type = ZEND_INTERNAL_FUNCTION; |
431 | 115 | call.fn_flags = mptr->common.fn_flags & (ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED); |
432 | 115 | call.handler = zend_closure_call_magic; |
433 | 115 | call.function_name = mptr->common.function_name; |
434 | 115 | call.scope = mptr->common.scope; |
435 | 115 | call.doc_comment = NULL; |
436 | 115 | call.attributes = mptr->common.attributes; |
437 | | |
438 | 115 | zend_free_trampoline(mptr); |
439 | 115 | mptr = (zend_function *) &call; |
440 | 115 | } |
441 | | |
442 | 550 | if (fcc.object) { |
443 | 230 | ZVAL_OBJ(&instance, fcc.object); |
444 | 230 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, &instance); |
445 | 320 | } else { |
446 | 320 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, NULL); |
447 | 320 | } |
448 | | |
449 | 550 | if (&mptr->internal_function == &call) { |
450 | 115 | zend_string_release(mptr->common.function_name); |
451 | 115 | } |
452 | | |
453 | 550 | return SUCCESS; |
454 | 592 | } |
455 | | /* }}} */ |
456 | | |
457 | | /* {{{ Create a closure from a callable using the current scope. */ |
458 | | ZEND_METHOD(Closure, fromCallable) |
459 | 680 | { |
460 | 680 | zval *callable; |
461 | 680 | char *error = NULL; |
462 | | |
463 | 2.04k | ZEND_PARSE_PARAMETERS_START(1, 1) |
464 | 2.72k | Z_PARAM_ZVAL(callable) |
465 | 2.72k | ZEND_PARSE_PARAMETERS_END(); |
466 | | |
467 | 680 | if (Z_TYPE_P(callable) == IS_OBJECT && instanceof_function(Z_OBJCE_P(callable), zend_ce_closure)) { |
468 | | /* It's already a closure */ |
469 | 8 | RETURN_COPY(callable); |
470 | 8 | } |
471 | | |
472 | 672 | if (zend_create_closure_from_callable(return_value, callable, &error) == FAILURE) { |
473 | 80 | if (error) { |
474 | 80 | zend_type_error("Failed to create closure from callable: %s", error); |
475 | 80 | efree(error); |
476 | 80 | } else { |
477 | 0 | zend_type_error("Failed to create closure from callable"); |
478 | 0 | } |
479 | 80 | } |
480 | 672 | } |
481 | | /* }}} */ |
482 | | |
483 | | ZEND_METHOD(Closure, getCurrent) |
484 | 146 | { |
485 | 146 | ZEND_PARSE_PARAMETERS_NONE(); |
486 | | |
487 | 146 | zend_execute_data *prev_ex = EX(prev_execute_data); |
488 | | |
489 | 146 | if (!prev_ex |
490 | 146 | || !prev_ex->func |
491 | 146 | || (prev_ex->func->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) != ZEND_ACC_CLOSURE) { |
492 | 12 | zend_throw_error(NULL, "Current function is not a closure"); |
493 | 12 | RETURN_THROWS(); |
494 | 12 | } |
495 | | |
496 | 134 | zend_object *obj = ZEND_CLOSURE_OBJECT(prev_ex->func); |
497 | 134 | RETURN_OBJ_COPY(obj); |
498 | 134 | } |
499 | | |
500 | | static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */ |
501 | 12 | { |
502 | 12 | zend_throw_error(NULL, "Instantiation of class Closure is not allowed"); |
503 | 12 | return NULL; |
504 | 12 | } |
505 | | /* }}} */ |
506 | | |
507 | | /* int return due to Object Handler API */ |
508 | | static int zend_closure_compare(zval *o1, zval *o2) /* {{{ */ |
509 | 179 | { |
510 | 179 | ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2); |
511 | | |
512 | 171 | zend_closure *lhs = (zend_closure*) Z_OBJ_P(o1); |
513 | 171 | zend_closure *rhs = (zend_closure*) Z_OBJ_P(o2); |
514 | | |
515 | 171 | if (!((lhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && (rhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE))) { |
516 | 4 | return ZEND_UNCOMPARABLE; |
517 | 4 | } |
518 | | |
519 | 167 | if (Z_TYPE(lhs->this_ptr) != Z_TYPE(rhs->this_ptr)) { |
520 | 0 | return ZEND_UNCOMPARABLE; |
521 | 0 | } |
522 | | |
523 | 167 | if (Z_TYPE(lhs->this_ptr) == IS_OBJECT && Z_OBJ(lhs->this_ptr) != Z_OBJ(rhs->this_ptr)) { |
524 | 34 | return ZEND_UNCOMPARABLE; |
525 | 34 | } |
526 | | |
527 | 133 | if (lhs->called_scope != rhs->called_scope) { |
528 | 9 | return ZEND_UNCOMPARABLE; |
529 | 9 | } |
530 | | |
531 | 124 | if (lhs->func.type != rhs->func.type) { |
532 | 0 | return ZEND_UNCOMPARABLE; |
533 | 0 | } |
534 | | |
535 | 124 | if (lhs->func.common.scope != rhs->func.common.scope) { |
536 | 0 | return ZEND_UNCOMPARABLE; |
537 | 0 | } |
538 | | |
539 | 124 | if (!zend_string_equals(lhs->func.common.function_name, rhs->func.common.function_name)) { |
540 | 52 | return ZEND_UNCOMPARABLE; |
541 | 52 | } |
542 | | |
543 | 72 | return 0; |
544 | 124 | } |
545 | | /* }}} */ |
546 | | |
547 | | ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */ |
548 | 559 | { |
549 | 559 | zend_closure *closure = (zend_closure *)object; |
550 | 559 | zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function)); |
551 | 559 | const uint32_t keep_flags = |
552 | 559 | ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE | ZEND_ACC_DEPRECATED; |
553 | | |
554 | 559 | invoke->common = closure->func.common; |
555 | | /* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the |
556 | | * same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*). |
557 | | * This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set, |
558 | | * and we won't check arguments on internal function. We also set |
559 | | * ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */ |
560 | 559 | invoke->type = ZEND_INTERNAL_FUNCTION; |
561 | 559 | invoke->internal_function.fn_flags = |
562 | 559 | ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | ZEND_ACC_NEVER_CACHE | (closure->func.common.fn_flags & keep_flags); |
563 | 559 | if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) { |
564 | 464 | invoke->internal_function.fn_flags |= |
565 | 464 | ZEND_ACC_USER_ARG_INFO; |
566 | 464 | } |
567 | 559 | invoke->internal_function.handler = ZEND_MN(Closure___invoke); |
568 | 559 | invoke->internal_function.doc_comment = NULL; |
569 | 559 | invoke->internal_function.module = 0; |
570 | 559 | invoke->internal_function.scope = zend_ce_closure; |
571 | 559 | invoke->internal_function.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE); |
572 | 559 | return invoke; |
573 | 559 | } |
574 | | /* }}} */ |
575 | | |
576 | | ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj) /* {{{ */ |
577 | 949 | { |
578 | 949 | zend_closure *closure = (zend_closure *) obj; |
579 | 949 | return &closure->func; |
580 | 949 | } |
581 | | /* }}} */ |
582 | | |
583 | | ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */ |
584 | 13 | { |
585 | 13 | zend_closure *closure = (zend_closure *)Z_OBJ_P(obj); |
586 | 13 | return &closure->this_ptr; |
587 | 13 | } |
588 | | /* }}} */ |
589 | | |
590 | | static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */ |
591 | 1.95k | { |
592 | 1.95k | if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) { |
593 | 529 | return zend_get_closure_invoke_method(*object); |
594 | 529 | } |
595 | | |
596 | 1.42k | return zend_std_get_method(object, method, key); |
597 | 1.95k | } |
598 | | /* }}} */ |
599 | | |
600 | | static void zend_closure_free_storage(zend_object *object) /* {{{ */ |
601 | 15.7k | { |
602 | 15.7k | zend_closure *closure = (zend_closure *)object; |
603 | | |
604 | 15.7k | zend_object_std_dtor(&closure->std); |
605 | | |
606 | 15.7k | if (closure->func.type == ZEND_USER_FUNCTION) { |
607 | | /* We don't own the static variables of fake closures. */ |
608 | 14.8k | if (!(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) { |
609 | 14.1k | zend_destroy_static_vars(&closure->func.op_array); |
610 | 14.1k | } |
611 | 14.8k | destroy_op_array(&closure->func.op_array); |
612 | 14.8k | } else if (closure->func.type == ZEND_INTERNAL_FUNCTION) { |
613 | 822 | zend_string_release(closure->func.common.function_name); |
614 | 822 | } |
615 | | |
616 | 15.7k | if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { |
617 | 2.91k | zval_ptr_dtor(&closure->this_ptr); |
618 | 2.91k | } |
619 | 15.7k | } |
620 | | /* }}} */ |
621 | | |
622 | | static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */ |
623 | 15.7k | { |
624 | 15.7k | zend_closure *closure; |
625 | | |
626 | 15.7k | closure = emalloc(sizeof(zend_closure)); |
627 | 15.7k | memset(closure, 0, sizeof(zend_closure)); |
628 | | |
629 | 15.7k | zend_object_std_init(&closure->std, class_type); |
630 | | |
631 | 15.7k | return (zend_object*)closure; |
632 | 15.7k | } |
633 | | /* }}} */ |
634 | | |
635 | | static zend_object *zend_closure_clone(zend_object *zobject) /* {{{ */ |
636 | 10 | { |
637 | 10 | zend_closure *closure = (zend_closure *)zobject; |
638 | 10 | zval result; |
639 | | |
640 | 10 | zend_create_closure_ex(&result, &closure->func, |
641 | 10 | closure->func.common.scope, closure->called_scope, &closure->this_ptr, |
642 | 10 | zend_closure_is_fake(closure), zend_closure_flags(closure)); |
643 | 10 | return Z_OBJ(result); |
644 | 10 | } |
645 | | /* }}} */ |
646 | | |
647 | | static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only) /* {{{ */ |
648 | 19.5k | { |
649 | 19.5k | zend_closure *closure = (zend_closure*)obj; |
650 | | |
651 | 19.5k | *fptr_ptr = &closure->func; |
652 | 19.5k | *ce_ptr = closure->called_scope; |
653 | | |
654 | 19.5k | if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { |
655 | 2.12k | *obj_ptr = Z_OBJ(closure->this_ptr); |
656 | 17.4k | } else { |
657 | 17.4k | *obj_ptr = NULL; |
658 | 17.4k | } |
659 | | |
660 | 19.5k | return SUCCESS; |
661 | 19.5k | } |
662 | | /* }}} */ |
663 | | |
664 | | /* *is_temp is int due to Object Handler API */ |
665 | | static HashTable *zend_closure_get_debug_info(zend_object *object, int *is_temp) /* {{{ */ |
666 | 1.09k | { |
667 | 1.09k | zend_closure *closure = (zend_closure *)object; |
668 | 1.09k | zval val; |
669 | 1.09k | struct _zend_arg_info *arg_info = closure->func.common.arg_info; |
670 | 1.09k | HashTable *debug_info; |
671 | | |
672 | 1.09k | *is_temp = 1; |
673 | | |
674 | 1.09k | debug_info = zend_new_array(8); |
675 | | |
676 | 1.09k | if (closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE) { |
677 | 185 | if (closure->func.common.scope) { |
678 | 48 | zend_string *class_name = closure->func.common.scope->name; |
679 | 48 | zend_string *func_name = closure->func.common.function_name; |
680 | 48 | zend_string *combined = zend_string_concat3( |
681 | 48 | ZSTR_VAL(class_name), ZSTR_LEN(class_name), |
682 | 48 | "::", strlen("::"), |
683 | 48 | ZSTR_VAL(func_name), ZSTR_LEN(func_name) |
684 | 48 | ); |
685 | 48 | ZVAL_STR(&val, combined); |
686 | 137 | } else { |
687 | 137 | ZVAL_STR_COPY(&val, closure->func.common.function_name); |
688 | 137 | } |
689 | 185 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_FUNCTION), &val); |
690 | 909 | } else { |
691 | 909 | ZVAL_STR_COPY(&val, closure->func.common.function_name); |
692 | 909 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_NAME), &val); |
693 | | |
694 | 909 | ZVAL_STR_COPY(&val, closure->func.op_array.filename); |
695 | 909 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_FILE), &val); |
696 | | |
697 | 909 | ZVAL_LONG(&val, closure->func.op_array.line_start); |
698 | 909 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_LINE), &val); |
699 | 909 | } |
700 | | |
701 | 1.09k | if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) { |
702 | 150 | zval *var; |
703 | 150 | zend_string *key; |
704 | 150 | HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr); |
705 | | |
706 | 150 | array_init(&val); |
707 | | |
708 | 687 | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(static_variables, key, var) { |
709 | 687 | zval copy; |
710 | | |
711 | 687 | if (Z_ISREF_P(var) && Z_REFCOUNT_P(var) == 1) { |
712 | 31 | var = Z_REFVAL_P(var); |
713 | 31 | } |
714 | 687 | ZVAL_COPY(©, var); |
715 | | |
716 | 687 | zend_hash_add_new(Z_ARRVAL(val), key, ©); |
717 | 687 | } ZEND_HASH_FOREACH_END(); |
718 | | |
719 | 150 | if (zend_hash_num_elements(Z_ARRVAL(val))) { |
720 | 137 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_STATIC), &val); |
721 | 137 | } else { |
722 | 13 | zval_ptr_dtor(&val); |
723 | 13 | } |
724 | 150 | } |
725 | | |
726 | 1.09k | if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { |
727 | 637 | Z_ADDREF(closure->this_ptr); |
728 | 637 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_THIS), &closure->this_ptr); |
729 | 637 | } |
730 | | |
731 | 1.09k | if (arg_info && |
732 | 201 | (closure->func.common.num_args || |
733 | 193 | (closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) { |
734 | 193 | uint32_t i, num_args, required = closure->func.common.required_num_args; |
735 | | |
736 | 193 | array_init(&val); |
737 | | |
738 | 193 | num_args = closure->func.common.num_args; |
739 | 193 | if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) { |
740 | 34 | num_args++; |
741 | 34 | } |
742 | 415 | for (i = 0; i < num_args; i++) { |
743 | 222 | zend_string *name; |
744 | 222 | zval info; |
745 | 222 | ZEND_ASSERT(arg_info->name && "Argument should have name"); |
746 | 222 | name = zend_strpprintf(0, "%s$%s", |
747 | 222 | ZEND_ARG_SEND_MODE(arg_info) ? "&" : "", |
748 | 222 | ZSTR_VAL(arg_info->name)); |
749 | 222 | ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>")); |
750 | 222 | zend_hash_update(Z_ARRVAL(val), name, &info); |
751 | 222 | zend_string_release_ex(name, 0); |
752 | 222 | arg_info++; |
753 | 222 | } |
754 | 193 | zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val); |
755 | 193 | } |
756 | | |
757 | 1.09k | return debug_info; |
758 | 1.09k | } |
759 | | /* }}} */ |
760 | | |
761 | | static HashTable *zend_closure_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */ |
762 | 14.8k | { |
763 | 14.8k | zend_closure *closure = (zend_closure *)obj; |
764 | | |
765 | 14.8k | *table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL; |
766 | 14.8k | *n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0; |
767 | | /* Fake closures don't own the static variables they reference. */ |
768 | 14.8k | return (closure->func.type == ZEND_USER_FUNCTION |
769 | 14.5k | && !(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) ? |
770 | 14.8k | ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr) : NULL; |
771 | 14.8k | } |
772 | | /* }}} */ |
773 | | |
774 | | /* {{{ Private constructor preventing instantiation */ |
775 | | ZEND_COLD ZEND_METHOD(Closure, __construct) |
776 | 0 | { |
777 | 0 | zend_throw_error(NULL, "Instantiation of class Closure is not allowed"); |
778 | 0 | } |
779 | | /* }}} */ |
780 | | |
781 | | void zend_register_closure_ce(void) /* {{{ */ |
782 | 16 | { |
783 | 16 | zend_ce_closure = register_class_Closure(); |
784 | 16 | zend_ce_closure->create_object = zend_closure_new; |
785 | 16 | zend_ce_closure->default_object_handlers = &closure_handlers; |
786 | | |
787 | 16 | memcpy(&closure_handlers, &std_object_handlers, sizeof(zend_object_handlers)); |
788 | 16 | closure_handlers.free_obj = zend_closure_free_storage; |
789 | 16 | closure_handlers.get_constructor = zend_closure_get_constructor; |
790 | 16 | closure_handlers.get_method = zend_closure_get_method; |
791 | 16 | closure_handlers.compare = zend_closure_compare; |
792 | 16 | closure_handlers.clone_obj = zend_closure_clone; |
793 | 16 | closure_handlers.get_debug_info = zend_closure_get_debug_info; |
794 | 16 | closure_handlers.get_closure = zend_closure_get_closure; |
795 | 16 | closure_handlers.get_gc = zend_closure_get_gc; |
796 | 16 | } |
797 | | /* }}} */ |
798 | | |
799 | | static ZEND_NAMED_FUNCTION(zend_closure_internal_handler) /* {{{ */ |
800 | 593 | { |
801 | 593 | zend_closure *closure = (zend_closure*)ZEND_CLOSURE_OBJECT(EX(func)); |
802 | 593 | closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU); |
803 | 593 | ZEND_ASSERT(!(closure->func.common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS) || EG(exception)); |
804 | | // Assign to EX(this) so that it is released after observer checks etc. |
805 | 593 | ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_RELEASE_THIS); |
806 | 593 | Z_OBJ(EX(This)) = &closure->std; |
807 | 593 | } |
808 | | /* }}} */ |
809 | | |
810 | | static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr, bool is_fake, uint32_t flags) /* {{{ */ |
811 | 15.7k | { |
812 | 15.7k | zend_closure *closure; |
813 | 15.7k | void *ptr; |
814 | | |
815 | 15.7k | object_init_ex(res, zend_ce_closure); |
816 | | |
817 | 15.7k | closure = (zend_closure *)Z_OBJ_P(res); |
818 | 15.7k | closure->std.extra_flags = flags; |
819 | | |
820 | 15.7k | if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) { |
821 | | /* use dummy scope if we're binding an object without specifying a scope */ |
822 | | /* maybe it would be better to create one for this purpose */ |
823 | 76 | scope = zend_ce_closure; |
824 | 76 | } |
825 | | |
826 | 15.7k | if (func->type == ZEND_USER_FUNCTION) { |
827 | 14.8k | memcpy(&closure->func, func, sizeof(zend_op_array)); |
828 | 14.8k | closure->func.common.fn_flags |= ZEND_ACC_CLOSURE; |
829 | 14.8k | closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE; |
830 | | |
831 | 14.8k | zend_string_addref(closure->func.op_array.function_name); |
832 | 14.8k | if (closure->func.op_array.refcount) { |
833 | 707 | (*closure->func.op_array.refcount)++; |
834 | 707 | } |
835 | | |
836 | | /* For fake closures, we want to reuse the static variables of the original function. */ |
837 | 14.8k | HashTable *ht = ZEND_MAP_PTR_GET(func->op_array.static_variables_ptr); |
838 | 14.8k | if (!is_fake) { |
839 | 14.1k | if (!ht) { |
840 | 14.1k | ht = closure->func.op_array.static_variables; |
841 | 14.1k | } |
842 | 14.1k | ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr, |
843 | 14.1k | ht ? zend_array_dup(ht) : NULL); |
844 | 14.1k | } else if (func->op_array.static_variables) { |
845 | 157 | if (!ht) { |
846 | 79 | ht = zend_array_dup(func->op_array.static_variables); |
847 | 79 | ZEND_MAP_PTR_SET(func->op_array.static_variables_ptr, ht); |
848 | 79 | } |
849 | 157 | ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr, ht); |
850 | 157 | } |
851 | | |
852 | | /* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */ |
853 | 14.8k | ptr = ZEND_MAP_PTR_GET(func->op_array.run_time_cache); |
854 | 14.8k | if (!ptr |
855 | 2.95k | || func->common.scope != scope |
856 | 2.63k | || (func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) |
857 | 14.8k | ) { |
858 | 12.4k | if (!ptr |
859 | 11.9k | && (func->common.fn_flags & ZEND_ACC_CLOSURE) |
860 | 11.7k | && (func->common.scope == scope || |
861 | 9.09k | !(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) { |
862 | | /* If a real closure is used for the first time, we create a shared runtime cache |
863 | | * and remember which scope it is for. */ |
864 | 9.09k | if (func->common.scope != scope) { |
865 | 17 | func->common.scope = scope; |
866 | 17 | } |
867 | 9.09k | ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size); |
868 | 9.09k | ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr); |
869 | 9.09k | closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE; |
870 | 9.09k | } else { |
871 | | /* Otherwise, we use a non-shared runtime cache */ |
872 | 3.30k | ptr = emalloc(func->op_array.cache_size); |
873 | 3.30k | closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE; |
874 | 3.30k | } |
875 | 12.4k | memset(ptr, 0, func->op_array.cache_size); |
876 | 12.4k | } |
877 | 14.8k | ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr); |
878 | 14.8k | } else { |
879 | 822 | memcpy(&closure->func, func, sizeof(zend_internal_function)); |
880 | 822 | closure->func.common.fn_flags |= ZEND_ACC_CLOSURE; |
881 | | /* wrap internal function handler to avoid memory leak */ |
882 | 822 | if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) { |
883 | | /* avoid infinity recursion, by taking handler from nested closure */ |
884 | 99 | zend_closure *nested = ZEND_CONTAINER_OF(func, zend_closure, func); |
885 | 99 | ZEND_ASSERT(nested->std.ce == zend_ce_closure); |
886 | 99 | closure->orig_internal_handler = nested->orig_internal_handler; |
887 | 723 | } else { |
888 | 723 | closure->orig_internal_handler = closure->func.internal_function.handler; |
889 | 723 | } |
890 | 822 | closure->func.internal_function.handler = zend_closure_internal_handler; |
891 | 822 | zend_string_addref(closure->func.op_array.function_name); |
892 | 822 | if (!func->common.scope) { |
893 | | /* if it's a free function, we won't set scope & this since they're meaningless */ |
894 | 382 | this_ptr = NULL; |
895 | 382 | scope = NULL; |
896 | 382 | } |
897 | 822 | } |
898 | | |
899 | 15.7k | ZVAL_UNDEF(&closure->this_ptr); |
900 | | /* Invariant: |
901 | | * If the closure is unscoped or static, it has no bound object. */ |
902 | 15.7k | closure->func.common.scope = scope; |
903 | 15.7k | closure->called_scope = called_scope; |
904 | 15.7k | if (scope) { |
905 | 3.91k | closure->func.common.fn_flags |= ZEND_ACC_PUBLIC; |
906 | 3.91k | if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) { |
907 | 2.91k | ZVAL_OBJ_COPY(&closure->this_ptr, Z_OBJ_P(this_ptr)); |
908 | 2.91k | } |
909 | 3.91k | } |
910 | 15.7k | } |
911 | | /* }}} */ |
912 | | |
913 | | ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) |
914 | 12.7k | { |
915 | 12.7k | zend_create_closure_ex(res, func, scope, called_scope, this_ptr, |
916 | 12.7k | /* is_fake */ (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0, |
917 | 12.7k | /* flags */ 0); |
918 | 12.7k | } |
919 | | |
920 | | ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */ |
921 | 1.44k | { |
922 | 1.44k | zend_closure *closure; |
923 | | |
924 | 1.44k | zend_create_closure_ex(res, func, scope, called_scope, this_ptr, |
925 | 1.44k | /* is_fake */ true, /* flags */ 0); |
926 | | |
927 | 1.44k | closure = (zend_closure *)Z_OBJ_P(res); |
928 | 1.44k | closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE; |
929 | 1.44k | if (Z_TYPE(closure->this_ptr) != IS_OBJECT) { |
930 | 985 | GC_ADD_FLAGS(&closure->std, GC_NOT_COLLECTABLE); |
931 | 985 | } |
932 | 1.44k | } |
933 | | /* }}} */ |
934 | | |
935 | | ZEND_API void zend_create_partial_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr, bool partial_of_closure) |
936 | 864 | { |
937 | 864 | uint32_t flags = ZEND_PARTIAL; |
938 | 864 | if (partial_of_closure) { |
939 | 132 | flags |= ZEND_PARTIAL_OF_CLOSURE; |
940 | 132 | } |
941 | 864 | zend_create_closure_ex(res, func, scope, called_scope, this_ptr, |
942 | 864 | /* is_fake */ false, flags); |
943 | 864 | } |
944 | | |
945 | 674 | void zend_closure_from_frame(zval *return_value, const zend_execute_data *call) { /* {{{ */ |
946 | 674 | zval instance; |
947 | 674 | zend_internal_function trampoline; |
948 | 674 | zend_function *mptr = call->func; |
949 | | |
950 | 674 | if (ZEND_CALL_INFO(call) & ZEND_CALL_CLOSURE) { |
951 | 6 | RETURN_OBJ(ZEND_CLOSURE_OBJECT(mptr)); |
952 | 6 | } |
953 | | |
954 | 668 | if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) { |
955 | 207 | if ((ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) && |
956 | 139 | (Z_OBJCE(call->This) == zend_ce_closure) |
957 | 9 | && zend_string_equals(mptr->common.function_name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) { |
958 | 9 | zend_free_trampoline(mptr); |
959 | 9 | RETURN_OBJ_COPY(Z_OBJ(call->This)); |
960 | 9 | } |
961 | | |
962 | 198 | memset(&trampoline, 0, sizeof(zend_internal_function)); |
963 | 198 | trampoline.type = ZEND_INTERNAL_FUNCTION; |
964 | 198 | trampoline.fn_flags = mptr->common.fn_flags & (ZEND_ACC_STATIC | ZEND_ACC_VARIADIC | ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_DEPRECATED); |
965 | 198 | trampoline.handler = zend_closure_call_magic; |
966 | 198 | trampoline.function_name = mptr->common.function_name; |
967 | 198 | trampoline.scope = mptr->common.scope; |
968 | 198 | trampoline.doc_comment = NULL; |
969 | 198 | trampoline.arg_info = mptr->common.arg_info; |
970 | 198 | trampoline.attributes = mptr->common.attributes; |
971 | | |
972 | 198 | zend_free_trampoline(mptr); |
973 | 198 | mptr = (zend_function *) &trampoline; |
974 | 198 | } |
975 | | |
976 | 659 | if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) { |
977 | 213 | ZVAL_OBJ(&instance, Z_OBJ(call->This)); |
978 | | |
979 | 213 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE(instance), &instance); |
980 | 446 | } else { |
981 | 446 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_CE(call->This), NULL); |
982 | 446 | } |
983 | | |
984 | 659 | if (&mptr->internal_function == &trampoline) { |
985 | 198 | zend_string_release(mptr->common.function_name); |
986 | 198 | } |
987 | 659 | } /* }}} */ |
988 | | |
989 | | void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */ |
990 | 0 | { |
991 | 0 | zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv); |
992 | 0 | HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr); |
993 | 0 | zend_hash_update(static_variables, var_name, var); |
994 | 0 | } |
995 | | /* }}} */ |
996 | | |
997 | | void zend_closure_bind_var_ex(zval *closure_zv, uint32_t offset, zval *val) /* {{{ */ |
998 | 7.19k | { |
999 | 7.19k | zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv); |
1000 | 7.19k | HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr); |
1001 | 7.19k | zval *var = (zval*)((char*)static_variables->arData + offset); |
1002 | 7.19k | zval_ptr_dtor(var); |
1003 | 7.19k | ZVAL_COPY_VALUE(var, val); |
1004 | 7.19k | } |
1005 | | /* }}} */ |