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: Nikita Popov <nikic@php.net> |
16 | Bob Weinand <bobwei9@hotmail.com> |
17 +----------------------------------------------------------------------+
18 */
19
20 #include "zend.h"
21 #include "zend_API.h"
22 #include "zend_interfaces.h"
23 #include "zend_exceptions.h"
24 #include "zend_generators.h"
25 #include "zend_closures.h"
26 #include "zend_generators_arginfo.h"
27 #include "zend_observer.h"
28
29 ZEND_API zend_class_entry *zend_ce_generator;
30 ZEND_API zend_class_entry *zend_ce_ClosedGeneratorException;
31 static zend_object_handlers zend_generator_handlers;
32
33 static zend_object *zend_generator_create(zend_class_entry *class_type);
34
zend_generator_restore_call_stack(zend_generator * generator)35 ZEND_API void zend_generator_restore_call_stack(zend_generator *generator) /* {{{ */
36 {
37 zend_execute_data *call, *new_call, *prev_call = NULL;
38
39 call = generator->frozen_call_stack;
40 do {
41 new_call = zend_vm_stack_push_call_frame(
42 (ZEND_CALL_INFO(call) & ~ZEND_CALL_ALLOCATED),
43 call->func,
44 ZEND_CALL_NUM_ARGS(call),
45 Z_PTR(call->This));
46 memcpy(((zval*)new_call) + ZEND_CALL_FRAME_SLOT, ((zval*)call) + ZEND_CALL_FRAME_SLOT, ZEND_CALL_NUM_ARGS(call) * sizeof(zval));
47 new_call->extra_named_params = call->extra_named_params;
48 new_call->prev_execute_data = prev_call;
49 prev_call = new_call;
50
51 call = call->prev_execute_data;
52 } while (call);
53 generator->execute_data->call = prev_call;
54 efree(generator->frozen_call_stack);
55 generator->frozen_call_stack = NULL;
56 }
57 /* }}} */
58
zend_generator_freeze_call_stack(zend_execute_data * execute_data)59 ZEND_API zend_execute_data* zend_generator_freeze_call_stack(zend_execute_data *execute_data) /* {{{ */
60 {
61 size_t used_stack;
62 zend_execute_data *call, *new_call, *prev_call = NULL;
63 zval *stack;
64
65 /* calculate required stack size */
66 used_stack = 0;
67 call = EX(call);
68 do {
69 used_stack += ZEND_CALL_FRAME_SLOT + ZEND_CALL_NUM_ARGS(call);
70 call = call->prev_execute_data;
71 } while (call);
72
73 stack = emalloc(used_stack * sizeof(zval));
74
75 /* save stack, linking frames in reverse order */
76 call = EX(call);
77 do {
78 size_t frame_size = ZEND_CALL_FRAME_SLOT + ZEND_CALL_NUM_ARGS(call);
79
80 new_call = (zend_execute_data*)(stack + used_stack - frame_size);
81 memcpy(new_call, call, frame_size * sizeof(zval));
82 used_stack -= frame_size;
83 new_call->prev_execute_data = prev_call;
84 prev_call = new_call;
85
86 new_call = call->prev_execute_data;
87 zend_vm_stack_free_call_frame(call);
88 call = new_call;
89 } while (call);
90
91 execute_data->call = NULL;
92 ZEND_ASSERT(prev_call == (zend_execute_data*)stack);
93
94 return prev_call;
95 }
96 /* }}} */
97
zend_generator_revert_call_stack(zend_execute_data * call)98 static zend_execute_data* zend_generator_revert_call_stack(zend_execute_data *call)
99 {
100 zend_execute_data *prev = NULL;
101
102 do {
103 zend_execute_data *next = call->prev_execute_data;
104 call->prev_execute_data = prev;
105 prev = call;
106 call = next;
107 } while (call);
108
109 return prev;
110 }
111
zend_generator_cleanup_unfinished_execution(zend_generator * generator,zend_execute_data * execute_data,uint32_t catch_op_num)112 static void zend_generator_cleanup_unfinished_execution(
113 zend_generator *generator, zend_execute_data *execute_data, uint32_t catch_op_num) /* {{{ */
114 {
115 zend_op_array *op_array = &execute_data->func->op_array;
116 if (execute_data->opline != op_array->opcodes) {
117 /* -1 required because we want the last run opcode, not the next to-be-run one. */
118 uint32_t op_num = execute_data->opline - op_array->opcodes - 1;
119
120 if (UNEXPECTED(generator->frozen_call_stack)) {
121 /* Temporarily restore generator->execute_data if it has been NULLed out already. */
122 zend_execute_data *save_ex = generator->execute_data;
123 generator->execute_data = execute_data;
124 zend_generator_restore_call_stack(generator);
125 generator->execute_data = save_ex;
126 }
127
128 zend_cleanup_unfinished_execution(execute_data, op_num, catch_op_num);
129 }
130 }
131 /* }}} */
132
zend_generator_close(zend_generator * generator,zend_bool finished_execution)133 ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution) /* {{{ */
134 {
135 if (EXPECTED(generator->execute_data)) {
136 zend_execute_data *execute_data = generator->execute_data;
137 /* Null out execute_data early, to prevent double frees if GC runs while we're
138 * already cleaning up execute_data. */
139 generator->execute_data = NULL;
140
141 if (EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE) {
142 zend_clean_and_cache_symbol_table(execute_data->symbol_table);
143 }
144 /* always free the CV's, in the symtable are only not-free'd IS_INDIRECT's */
145 zend_free_compiled_variables(execute_data);
146 if (EX_CALL_INFO() & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
147 zend_free_extra_named_params(execute_data->extra_named_params);
148 }
149
150 if (EX_CALL_INFO() & ZEND_CALL_RELEASE_THIS) {
151 OBJ_RELEASE(Z_OBJ(execute_data->This));
152 }
153
154 /* A fatal error / die occurred during the generator execution.
155 * Trying to clean up the stack may not be safe in this case. */
156 if (UNEXPECTED(CG(unclean_shutdown))) {
157 generator->execute_data = NULL;
158 return;
159 }
160
161 zend_vm_stack_free_extra_args(execute_data);
162
163 /* Some cleanups are only necessary if the generator was closed
164 * before it could finish execution (reach a return statement). */
165 if (UNEXPECTED(!finished_execution)) {
166 zend_generator_cleanup_unfinished_execution(generator, execute_data, 0);
167 }
168
169 /* Free closure object */
170 if (EX_CALL_INFO() & ZEND_CALL_CLOSURE) {
171 OBJ_RELEASE(ZEND_CLOSURE_OBJECT(EX(func)));
172 }
173
174 efree(execute_data);
175 }
176 }
177 /* }}} */
178
zend_generator_remove_child(zend_generator_node * node,zend_generator * child)179 static void zend_generator_remove_child(zend_generator_node *node, zend_generator *child)
180 {
181 ZEND_ASSERT(node->children >= 1);
182 if (node->children == 1) {
183 node->child.single.child = NULL;
184 } else {
185 HashTable *ht = node->child.ht;
186 zend_hash_index_del(ht, (zend_ulong) child);
187 if (node->children == 2) {
188 zend_generator *other_child;
189 ZEND_HASH_FOREACH_PTR(ht, other_child) {
190 node->child.single.child = other_child;
191 break;
192 } ZEND_HASH_FOREACH_END();
193 zend_hash_destroy(ht);
194 efree(ht);
195 }
196 }
197 node->children--;
198 }
199
clear_link_to_leaf(zend_generator * generator)200 static zend_always_inline zend_generator *clear_link_to_leaf(zend_generator *generator) {
201 ZEND_ASSERT(!generator->node.parent);
202 zend_generator *leaf = generator->node.ptr.leaf;
203 if (leaf) {
204 leaf->node.ptr.root = NULL;
205 generator->node.ptr.leaf = NULL;
206 return leaf;
207 }
208 return NULL;
209 }
210
clear_link_to_root(zend_generator * generator)211 static zend_always_inline void clear_link_to_root(zend_generator *generator) {
212 ZEND_ASSERT(generator->node.parent);
213 if (generator->node.ptr.root) {
214 generator->node.ptr.root->node.ptr.leaf = NULL;
215 generator->node.ptr.root = NULL;
216 }
217 }
218
zend_generator_dtor_storage(zend_object * object)219 static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
220 {
221 zend_generator *generator = (zend_generator*) object;
222 zend_execute_data *ex = generator->execute_data;
223 uint32_t op_num, try_catch_offset;
224 int i;
225
226 /* leave yield from mode to properly allow finally execution */
227 if (UNEXPECTED(Z_TYPE(generator->values) != IS_UNDEF)) {
228 zval_ptr_dtor(&generator->values);
229 ZVAL_UNDEF(&generator->values);
230 }
231
232 zend_generator *parent = generator->node.parent;
233 if (parent) {
234 zend_generator_remove_child(&parent->node, generator);
235 clear_link_to_root(generator);
236 generator->node.parent = NULL;
237 OBJ_RELEASE(&parent->std);
238 } else {
239 clear_link_to_leaf(generator);
240 }
241
242 if (EXPECTED(!ex) || EXPECTED(!(ex->func->op_array.fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK))
243 || CG(unclean_shutdown)) {
244 return;
245 }
246
247 /* -1 required because we want the last run opcode, not the
248 * next to-be-run one. */
249 op_num = ex->opline - ex->func->op_array.opcodes - 1;
250 try_catch_offset = -1;
251
252 /* Find the innermost try/catch that we are inside of. */
253 for (i = 0; i < ex->func->op_array.last_try_catch; i++) {
254 zend_try_catch_element *try_catch = &ex->func->op_array.try_catch_array[i];
255 if (op_num < try_catch->try_op) {
256 break;
257 }
258 if (op_num < try_catch->catch_op || op_num < try_catch->finally_end) {
259 try_catch_offset = i;
260 }
261 }
262
263 /* Walk try/catch/finally structures upwards, performing the necessary actions. */
264 while (try_catch_offset != (uint32_t) -1) {
265 zend_try_catch_element *try_catch = &ex->func->op_array.try_catch_array[try_catch_offset];
266
267 if (op_num < try_catch->finally_op) {
268 /* Go to finally block */
269 zval *fast_call =
270 ZEND_CALL_VAR(ex, ex->func->op_array.opcodes[try_catch->finally_end].op1.var);
271
272 zend_generator_cleanup_unfinished_execution(generator, ex, try_catch->finally_op);
273 Z_OBJ_P(fast_call) = EG(exception);
274 EG(exception) = NULL;
275 Z_OPLINE_NUM_P(fast_call) = (uint32_t)-1;
276
277 ex->opline = &ex->func->op_array.opcodes[try_catch->finally_op];
278 generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
279 zend_generator_resume(generator);
280
281 /* TODO: If we hit another yield inside try/finally,
282 * should we also jump to the next finally block? */
283 return;
284 } else if (op_num < try_catch->finally_end) {
285 zval *fast_call =
286 ZEND_CALL_VAR(ex, ex->func->op_array.opcodes[try_catch->finally_end].op1.var);
287 /* Clean up incomplete return statement */
288 if (Z_OPLINE_NUM_P(fast_call) != (uint32_t) -1) {
289 zend_op *retval_op = &ex->func->op_array.opcodes[Z_OPLINE_NUM_P(fast_call)];
290 if (retval_op->op2_type & (IS_TMP_VAR | IS_VAR)) {
291 zval_ptr_dtor(ZEND_CALL_VAR(ex, retval_op->op2.var));
292 }
293 }
294 /* Clean up backed-up exception */
295 if (Z_OBJ_P(fast_call)) {
296 OBJ_RELEASE(Z_OBJ_P(fast_call));
297 }
298 }
299
300 try_catch_offset--;
301 }
302 }
303 /* }}} */
304
zend_generator_free_storage(zend_object * object)305 static void zend_generator_free_storage(zend_object *object) /* {{{ */
306 {
307 zend_generator *generator = (zend_generator*) object;
308
309 zend_generator_close(generator, 0);
310
311 /* we can't immediately free them in zend_generator_close() else yield from won't be able to fetch it */
312 zval_ptr_dtor(&generator->value);
313 zval_ptr_dtor(&generator->key);
314
315 if (EXPECTED(!Z_ISUNDEF(generator->retval))) {
316 zval_ptr_dtor(&generator->retval);
317 }
318
319 if (UNEXPECTED(generator->node.children > 1)) {
320 zend_hash_destroy(generator->node.child.ht);
321 efree(generator->node.child.ht);
322 }
323
324 zend_object_std_dtor(&generator->std);
325 }
326 /* }}} */
327
zend_generator_get_gc(zend_object * object,zval ** table,int * n)328 static HashTable *zend_generator_get_gc(zend_object *object, zval **table, int *n) /* {{{ */
329 {
330 zend_generator *generator = (zend_generator*)object;
331 zend_execute_data *execute_data = generator->execute_data;
332 zend_op_array *op_array;
333
334 if (!execute_data) {
335 /* If the generator has been closed, it can only hold on to three values: The value, key
336 * and retval. These three zvals are stored sequentially starting at &generator->value. */
337 *table = &generator->value;
338 *n = 3;
339 return NULL;
340 }
341
342 if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
343 /* If the generator is currently running, we certainly won't be able to GC any values it
344 * holds on to. The execute_data state might be inconsistent during execution (e.g. because
345 * GC has been triggered in the middle of a variable reassignment), so we should not try
346 * to inspect it here. */
347 *table = NULL;
348 *n = 0;
349 return NULL;
350 }
351
352 op_array = &EX(func)->op_array;
353
354 zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
355 zend_get_gc_buffer_add_zval(gc_buffer, &generator->value);
356 zend_get_gc_buffer_add_zval(gc_buffer, &generator->key);
357 zend_get_gc_buffer_add_zval(gc_buffer, &generator->retval);
358 zend_get_gc_buffer_add_zval(gc_buffer, &generator->values);
359
360 if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
361 uint32_t i, num_cvs = EX(func)->op_array.last_var;
362 for (i = 0; i < num_cvs; i++) {
363 zend_get_gc_buffer_add_zval(gc_buffer, EX_VAR_NUM(i));
364 }
365 }
366
367 if (EX_CALL_INFO() & ZEND_CALL_FREE_EXTRA_ARGS) {
368 zval *zv = EX_VAR_NUM(op_array->last_var + op_array->T);
369 zval *end = zv + (EX_NUM_ARGS() - op_array->num_args);
370 while (zv != end) {
371 zend_get_gc_buffer_add_zval(gc_buffer, zv++);
372 }
373 }
374
375 if (EX_CALL_INFO() & ZEND_CALL_RELEASE_THIS) {
376 zend_get_gc_buffer_add_obj(gc_buffer, Z_OBJ(execute_data->This));
377 }
378 if (EX_CALL_INFO() & ZEND_CALL_CLOSURE) {
379 zend_get_gc_buffer_add_obj(gc_buffer, ZEND_CLOSURE_OBJECT(EX(func)));
380 }
381 if (EX_CALL_INFO() & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
382 zval extra_named_params;
383 ZVAL_ARR(&extra_named_params, EX(extra_named_params));
384 zend_get_gc_buffer_add_zval(gc_buffer, &extra_named_params);
385 }
386
387 if (UNEXPECTED(generator->frozen_call_stack)) {
388 /* The frozen stack is linked in reverse order */
389 zend_execute_data *call = zend_generator_revert_call_stack(generator->frozen_call_stack);
390 /* -1 required because we want the last run opcode, not the next to-be-run one. */
391 uint32_t op_num = execute_data->opline - op_array->opcodes - 1;
392 zend_unfinished_calls_gc(execute_data, call, op_num, gc_buffer);
393 zend_generator_revert_call_stack(call);
394 }
395
396 if (execute_data->opline != op_array->opcodes) {
397 uint32_t i, op_num = execute_data->opline - op_array->opcodes - 1;
398 for (i = 0; i < op_array->last_live_range; i++) {
399 const zend_live_range *range = &op_array->live_range[i];
400 if (range->start > op_num) {
401 break;
402 } else if (op_num < range->end) {
403 uint32_t kind = range->var & ZEND_LIVE_MASK;
404 uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
405 zval *var = EX_VAR(var_num);
406 if (kind == ZEND_LIVE_TMPVAR || kind == ZEND_LIVE_LOOP) {
407 zend_get_gc_buffer_add_zval(gc_buffer, var);
408 }
409 }
410 }
411 }
412
413 if (generator->node.parent) {
414 zend_get_gc_buffer_add_obj(gc_buffer, &generator->node.parent->std);
415 }
416
417 zend_get_gc_buffer_use(gc_buffer, table, n);
418 if (EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE) {
419 return execute_data->symbol_table;
420 } else {
421 return NULL;
422 }
423 }
424 /* }}} */
425
zend_generator_create(zend_class_entry * class_type)426 static zend_object *zend_generator_create(zend_class_entry *class_type) /* {{{ */
427 {
428 zend_generator *generator;
429
430 generator = emalloc(sizeof(zend_generator));
431 memset(generator, 0, sizeof(zend_generator));
432
433 /* The key will be incremented on first use, so it'll start at 0 */
434 generator->largest_used_integer_key = -1;
435
436 ZVAL_UNDEF(&generator->retval);
437 ZVAL_UNDEF(&generator->values);
438
439 /* By default we have a tree of only one node */
440 generator->node.parent = NULL;
441 generator->node.children = 0;
442 generator->node.ptr.root = NULL;
443
444 zend_object_std_init(&generator->std, class_type);
445 generator->std.handlers = &zend_generator_handlers;
446
447 return (zend_object*)generator;
448 }
449 /* }}} */
450
zend_generator_get_constructor(zend_object * object)451 static ZEND_COLD zend_function *zend_generator_get_constructor(zend_object *object) /* {{{ */
452 {
453 zend_throw_error(NULL, "The \"Generator\" class is reserved for internal use and cannot be manually instantiated");
454
455 return NULL;
456 }
457 /* }}} */
458
zend_generator_check_placeholder_frame(zend_execute_data * ptr)459 ZEND_API zend_execute_data *zend_generator_check_placeholder_frame(zend_execute_data *ptr)
460 {
461 if (!ptr->func && Z_TYPE(ptr->This) == IS_OBJECT) {
462 if (Z_OBJCE(ptr->This) == zend_ce_generator) {
463 zend_generator *generator = (zend_generator *) Z_OBJ(ptr->This);
464 zend_execute_data *prev = ptr->prev_execute_data;
465 ZEND_ASSERT(generator->node.parent && "Placeholder only used with delegation");
466 while (generator->node.parent->node.parent) {
467 generator->execute_data->prev_execute_data = prev;
468 prev = generator->execute_data;
469 generator = generator->node.parent;
470 }
471 generator->execute_data->prev_execute_data = prev;
472 ptr = generator->execute_data;
473 }
474 }
475 return ptr;
476 }
477
zend_generator_throw_exception(zend_generator * generator,zval * exception)478 static void zend_generator_throw_exception(zend_generator *generator, zval *exception)
479 {
480 zend_execute_data *original_execute_data = EG(current_execute_data);
481
482 /* Throw the exception in the context of the generator. Decrementing the opline
483 * to pretend the exception happened during the YIELD opcode. */
484 EG(current_execute_data) = generator->execute_data;
485 generator->execute_data->opline--;
486
487 if (exception) {
488 zend_throw_exception_object(exception);
489 } else {
490 zend_rethrow_exception(EG(current_execute_data));
491 }
492
493 /* if we don't stop an array/iterator yield from, the exception will only reach the generator after the values were all iterated over */
494 if (UNEXPECTED(Z_TYPE(generator->values) != IS_UNDEF)) {
495 zval_ptr_dtor(&generator->values);
496 ZVAL_UNDEF(&generator->values);
497 }
498
499 generator->execute_data->opline++;
500 EG(current_execute_data) = original_execute_data;
501 }
502
zend_generator_add_child(zend_generator * generator,zend_generator * child)503 static void zend_generator_add_child(zend_generator *generator, zend_generator *child)
504 {
505 zend_generator_node *node = &generator->node;
506
507 if (node->children == 0) {
508 node->child.single.child = child;
509 } else {
510 if (node->children == 1) {
511 HashTable *ht = emalloc(sizeof(HashTable));
512 zend_hash_init(ht, 0, NULL, NULL, 0);
513 zend_hash_index_add_new_ptr(ht,
514 (zend_ulong) node->child.single.child, node->child.single.child);
515 node->child.ht = ht;
516 }
517
518 zend_hash_index_add_new_ptr(node->child.ht, (zend_ulong) child, child);
519 }
520
521 ++node->children;
522 }
523
zend_generator_yield_from(zend_generator * generator,zend_generator * from)524 void zend_generator_yield_from(zend_generator *generator, zend_generator *from)
525 {
526 ZEND_ASSERT(!generator->node.parent && "Already has parent?");
527 zend_generator *leaf = clear_link_to_leaf(generator);
528 if (leaf && !from->node.parent && !from->node.ptr.leaf) {
529 from->node.ptr.leaf = leaf;
530 leaf->node.ptr.root = from;
531 }
532 generator->node.parent = from;
533 zend_generator_add_child(from, generator);
534 generator->flags |= ZEND_GENERATOR_DO_INIT;
535 }
536
zend_generator_update_root(zend_generator * generator)537 ZEND_API zend_generator *zend_generator_update_root(zend_generator *generator)
538 {
539 zend_generator *root = generator->node.parent;
540 while (root->node.parent) {
541 root = root->node.parent;
542 }
543
544 clear_link_to_leaf(root);
545 root->node.ptr.leaf = generator;
546 generator->node.ptr.root = root;
547 return root;
548 }
549
get_new_root(zend_generator * generator,zend_generator * root)550 static zend_generator *get_new_root(zend_generator *generator, zend_generator *root)
551 {
552 while (!root->execute_data && root->node.children == 1) {
553 root = root->node.child.single.child;
554 }
555
556 if (root->execute_data) {
557 return root;
558 }
559
560 /* We have reached a multi-child node haven't found the root yet. We don't know which
561 * child to follow, so perform the search from the other direction instead. */
562 while (generator->node.parent->execute_data) {
563 generator = generator->node.parent;
564 }
565
566 return generator;
567 }
568
zend_generator_update_current(zend_generator * generator)569 ZEND_API zend_generator *zend_generator_update_current(zend_generator *generator)
570 {
571 zend_generator *old_root = generator->node.ptr.root;
572 ZEND_ASSERT(!old_root->execute_data && "Nothing to update?");
573
574 zend_generator *new_root = get_new_root(generator, old_root);
575
576 ZEND_ASSERT(old_root->node.ptr.leaf == generator);
577 generator->node.ptr.root = new_root;
578 new_root->node.ptr.leaf = generator;
579 old_root->node.ptr.leaf = NULL;
580
581 zend_generator *new_root_parent = new_root->node.parent;
582 ZEND_ASSERT(new_root_parent);
583 zend_generator_remove_child(&new_root_parent->node, new_root);
584
585 if (EXPECTED(EG(exception) == NULL) && EXPECTED((OBJ_FLAGS(&generator->std) & IS_OBJ_DESTRUCTOR_CALLED) == 0)) {
586 zend_op *yield_from = (zend_op *) new_root->execute_data->opline - 1;
587
588 if (yield_from->opcode == ZEND_YIELD_FROM) {
589 if (Z_ISUNDEF(new_root_parent->retval)) {
590 /* Throw the exception in the context of the generator */
591 zend_execute_data *original_execute_data = EG(current_execute_data);
592 EG(current_execute_data) = new_root->execute_data;
593
594 if (new_root == generator) {
595 new_root->execute_data->prev_execute_data = original_execute_data;
596 } else {
597 new_root->execute_data->prev_execute_data = &generator->execute_fake;
598 generator->execute_fake.prev_execute_data = original_execute_data;
599 }
600
601 /* ZEND_YIELD(_FROM) already advance, so decrement opline to throw from correct place */
602 new_root->execute_data->opline--;
603 zend_throw_exception(zend_ce_ClosedGeneratorException, "Generator yielded from aborted, no return value available", 0);
604
605 EG(current_execute_data) = original_execute_data;
606
607 if (!((old_root ? old_root : generator)->flags & ZEND_GENERATOR_CURRENTLY_RUNNING)) {
608 new_root->node.parent = NULL;
609 OBJ_RELEASE(&new_root_parent->std);
610 zend_generator_resume(generator);
611 return zend_generator_get_current(generator);
612 }
613 } else {
614 zval_ptr_dtor(&new_root->value);
615 ZVAL_COPY(&new_root->value, &new_root_parent->value);
616 ZVAL_COPY(ZEND_CALL_VAR(new_root->execute_data, yield_from->result.var), &new_root_parent->retval);
617 }
618 }
619 }
620
621 new_root->node.parent = NULL;
622 OBJ_RELEASE(&new_root_parent->std);
623
624 return new_root;
625 }
626
zend_generator_get_next_delegated_value(zend_generator * generator)627 static zend_result zend_generator_get_next_delegated_value(zend_generator *generator) /* {{{ */
628 {
629 --generator->execute_data->opline;
630
631 zval *value;
632 if (Z_TYPE(generator->values) == IS_ARRAY) {
633 HashTable *ht = Z_ARR(generator->values);
634 HashPosition pos = Z_FE_POS(generator->values);
635
636 Bucket *p;
637 do {
638 if (UNEXPECTED(pos >= ht->nNumUsed)) {
639 /* Reached end of array */
640 goto failure;
641 }
642
643 p = &ht->arData[pos];
644 value = &p->val;
645 if (Z_TYPE_P(value) == IS_INDIRECT) {
646 value = Z_INDIRECT_P(value);
647 }
648 pos++;
649 } while (Z_ISUNDEF_P(value));
650
651 zval_ptr_dtor(&generator->value);
652 ZVAL_COPY(&generator->value, value);
653
654 zval_ptr_dtor(&generator->key);
655 if (p->key) {
656 ZVAL_STR_COPY(&generator->key, p->key);
657 } else {
658 ZVAL_LONG(&generator->key, p->h);
659 }
660
661 Z_FE_POS(generator->values) = pos;
662 } else {
663 zend_object_iterator *iter = (zend_object_iterator *) Z_OBJ(generator->values);
664
665 if (iter->index++ > 0) {
666 iter->funcs->move_forward(iter);
667 if (UNEXPECTED(EG(exception) != NULL)) {
668 goto failure;
669 }
670 }
671
672 if (iter->funcs->valid(iter) == FAILURE) {
673 /* reached end of iteration */
674 goto failure;
675 }
676
677 value = iter->funcs->get_current_data(iter);
678 if (UNEXPECTED(EG(exception) != NULL) || UNEXPECTED(!value)) {
679 goto failure;
680 }
681
682 zval_ptr_dtor(&generator->value);
683 ZVAL_COPY(&generator->value, value);
684
685 zval_ptr_dtor(&generator->key);
686 if (iter->funcs->get_current_key) {
687 iter->funcs->get_current_key(iter, &generator->key);
688 if (UNEXPECTED(EG(exception) != NULL)) {
689 ZVAL_UNDEF(&generator->key);
690 goto failure;
691 }
692 } else {
693 ZVAL_LONG(&generator->key, iter->index);
694 }
695 }
696
697 ++generator->execute_data->opline;
698 return SUCCESS;
699
700 failure:
701 zval_ptr_dtor(&generator->values);
702 ZVAL_UNDEF(&generator->values);
703
704 ++generator->execute_data->opline;
705 return FAILURE;
706 }
707 /* }}} */
708
zend_generator_resume(zend_generator * orig_generator)709 ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */
710 {
711 zend_generator *generator = zend_generator_get_current(orig_generator);
712
713 /* The generator is already closed, thus can't resume */
714 if (UNEXPECTED(!generator->execute_data)) {
715 return;
716 }
717
718 try_again:
719 if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
720 zend_throw_error(NULL, "Cannot resume an already running generator");
721 return;
722 }
723
724 if (UNEXPECTED((orig_generator->flags & ZEND_GENERATOR_DO_INIT) != 0 && !Z_ISUNDEF(generator->value))) {
725 /* We must not advance Generator if we yield from a Generator being currently run */
726 orig_generator->flags &= ~ZEND_GENERATOR_DO_INIT;
727 return;
728 }
729
730 /* Drop the AT_FIRST_YIELD flag */
731 orig_generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
732
733 /* Backup executor globals */
734 zend_execute_data *original_execute_data = EG(current_execute_data);
735 uint32_t original_jit_trace_num = EG(jit_trace_num);
736
737 /* Set executor globals */
738 EG(current_execute_data) = generator->execute_data;
739 EG(jit_trace_num) = 0;
740
741 /* We want the backtrace to look as if the generator function was
742 * called from whatever method we are current running (e.g. next()).
743 * So we have to link generator call frame with caller call frame. */
744 if (generator == orig_generator) {
745 generator->execute_data->prev_execute_data = original_execute_data;
746 } else {
747 /* We need some execute_data placeholder in stacktrace to be replaced
748 * by the real stack trace when needed */
749 generator->execute_data->prev_execute_data = &orig_generator->execute_fake;
750 orig_generator->execute_fake.prev_execute_data = original_execute_data;
751 }
752
753 /* Ensure this is run after executor_data swap to have a proper stack trace */
754 if (UNEXPECTED(!Z_ISUNDEF(generator->values))) {
755 if (EXPECTED(zend_generator_get_next_delegated_value(generator) == SUCCESS)) {
756 /* Restore executor globals */
757 EG(current_execute_data) = original_execute_data;
758 EG(jit_trace_num) = original_jit_trace_num;
759
760 orig_generator->flags &= ~ZEND_GENERATOR_DO_INIT;
761 return;
762 }
763 /* If there are no more deletegated values, resume the generator
764 * after the "yield from" expression. */
765 }
766
767 if (UNEXPECTED(generator->frozen_call_stack)) {
768 /* Restore frozen call-stack */
769 zend_generator_restore_call_stack(generator);
770 }
771
772 /* Resume execution */
773 generator->flags |= ZEND_GENERATOR_CURRENTLY_RUNNING;
774 if (!ZEND_OBSERVER_ENABLED) {
775 zend_execute_ex(generator->execute_data);
776 } else {
777 zend_observer_generator_resume(generator->execute_data);
778 zend_execute_ex(generator->execute_data);
779 if (generator->execute_data) {
780 /* On the final return, this will be called from ZEND_GENERATOR_RETURN */
781 zend_observer_fcall_end(generator->execute_data, &generator->value);
782 }
783 }
784 generator->flags &= ~ZEND_GENERATOR_CURRENTLY_RUNNING;
785
786 generator->frozen_call_stack = NULL;
787 if (EXPECTED(generator->execute_data) &&
788 UNEXPECTED(generator->execute_data->call)) {
789 /* Frize call-stack */
790 generator->frozen_call_stack = zend_generator_freeze_call_stack(generator->execute_data);
791 }
792
793 /* Restore executor globals */
794 EG(current_execute_data) = original_execute_data;
795 EG(jit_trace_num) = original_jit_trace_num;
796
797 /* If an exception was thrown in the generator we have to internally
798 * rethrow it in the parent scope.
799 * In case we did yield from, the Exception must be rethrown into
800 * its calling frame (see above in if (check_yield_from). */
801 if (UNEXPECTED(EG(exception) != NULL)) {
802 if (generator == orig_generator) {
803 zend_generator_close(generator, 0);
804 if (!EG(current_execute_data)) {
805 zend_throw_exception_internal(NULL);
806 } else if (EG(current_execute_data)->func &&
807 ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
808 zend_rethrow_exception(EG(current_execute_data));
809 }
810 } else {
811 generator = zend_generator_get_current(orig_generator);
812 zend_generator_throw_exception(generator, NULL);
813 orig_generator->flags &= ~ZEND_GENERATOR_DO_INIT;
814 goto try_again;
815 }
816 }
817
818 /* yield from was used, try another resume. */
819 if (UNEXPECTED((generator != orig_generator && !Z_ISUNDEF(generator->retval)) || (generator->execute_data && (generator->execute_data->opline - 1)->opcode == ZEND_YIELD_FROM))) {
820 generator = zend_generator_get_current(orig_generator);
821 goto try_again;
822 }
823
824 orig_generator->flags &= ~ZEND_GENERATOR_DO_INIT;
825 }
826 /* }}} */
827
zend_generator_ensure_initialized(zend_generator * generator)828 static inline void zend_generator_ensure_initialized(zend_generator *generator) /* {{{ */
829 {
830 if (UNEXPECTED(Z_TYPE(generator->value) == IS_UNDEF) && EXPECTED(generator->execute_data) && EXPECTED(generator->node.parent == NULL)) {
831 zend_generator_resume(generator);
832 generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
833 }
834 }
835 /* }}} */
836
zend_generator_rewind(zend_generator * generator)837 static inline void zend_generator_rewind(zend_generator *generator) /* {{{ */
838 {
839 zend_generator_ensure_initialized(generator);
840
841 if (!(generator->flags & ZEND_GENERATOR_AT_FIRST_YIELD)) {
842 zend_throw_exception(NULL, "Cannot rewind a generator that was already run", 0);
843 }
844 }
845 /* }}} */
846
847 /* {{{ Rewind the generator */
ZEND_METHOD(Generator,rewind)848 ZEND_METHOD(Generator, rewind)
849 {
850 zend_generator *generator;
851
852 ZEND_PARSE_PARAMETERS_NONE();
853
854 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
855
856 zend_generator_rewind(generator);
857 }
858 /* }}} */
859
860 /* {{{ Check whether the generator is valid */
ZEND_METHOD(Generator,valid)861 ZEND_METHOD(Generator, valid)
862 {
863 zend_generator *generator;
864
865 ZEND_PARSE_PARAMETERS_NONE();
866
867 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
868
869 zend_generator_ensure_initialized(generator);
870
871 zend_generator_get_current(generator);
872
873 RETURN_BOOL(EXPECTED(generator->execute_data != NULL));
874 }
875 /* }}} */
876
877 /* {{{ Get the current value */
ZEND_METHOD(Generator,current)878 ZEND_METHOD(Generator, current)
879 {
880 zend_generator *generator, *root;
881
882 ZEND_PARSE_PARAMETERS_NONE();
883
884 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
885
886 zend_generator_ensure_initialized(generator);
887
888 root = zend_generator_get_current(generator);
889 if (EXPECTED(generator->execute_data != NULL && Z_TYPE(root->value) != IS_UNDEF)) {
890 zval *value = &root->value;
891
892 ZVAL_COPY_DEREF(return_value, value);
893 }
894 }
895 /* }}} */
896
897 /* {{{ Get the current key */
ZEND_METHOD(Generator,key)898 ZEND_METHOD(Generator, key)
899 {
900 zend_generator *generator, *root;
901
902 ZEND_PARSE_PARAMETERS_NONE();
903
904 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
905
906 zend_generator_ensure_initialized(generator);
907
908 root = zend_generator_get_current(generator);
909 if (EXPECTED(generator->execute_data != NULL && Z_TYPE(root->key) != IS_UNDEF)) {
910 zval *key = &root->key;
911
912 ZVAL_COPY_DEREF(return_value, key);
913 }
914 }
915 /* }}} */
916
917 /* {{{ Advances the generator */
ZEND_METHOD(Generator,next)918 ZEND_METHOD(Generator, next)
919 {
920 zend_generator *generator;
921
922 ZEND_PARSE_PARAMETERS_NONE();
923
924 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
925
926 zend_generator_ensure_initialized(generator);
927
928 zend_generator_resume(generator);
929 }
930 /* }}} */
931
932 /* {{{ Sends a value to the generator */
ZEND_METHOD(Generator,send)933 ZEND_METHOD(Generator, send)
934 {
935 zval *value;
936 zend_generator *generator, *root;
937
938 ZEND_PARSE_PARAMETERS_START(1, 1)
939 Z_PARAM_ZVAL(value)
940 ZEND_PARSE_PARAMETERS_END();
941
942 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
943
944 zend_generator_ensure_initialized(generator);
945
946 /* The generator is already closed, thus can't send anything */
947 if (UNEXPECTED(!generator->execute_data)) {
948 return;
949 }
950
951 root = zend_generator_get_current(generator);
952 /* Put sent value in the target VAR slot, if it is used */
953 if (root->send_target && !(root->flags & ZEND_GENERATOR_CURRENTLY_RUNNING)) {
954 ZVAL_COPY(root->send_target, value);
955 }
956
957 zend_generator_resume(generator);
958
959 root = zend_generator_get_current(generator);
960 if (EXPECTED(generator->execute_data)) {
961 zval *value = &root->value;
962
963 ZVAL_COPY_DEREF(return_value, value);
964 }
965 }
966 /* }}} */
967
968 /* {{{ Throws an exception into the generator */
ZEND_METHOD(Generator,throw)969 ZEND_METHOD(Generator, throw)
970 {
971 zval *exception;
972 zend_generator *generator;
973
974 ZEND_PARSE_PARAMETERS_START(1, 1)
975 Z_PARAM_OBJECT_OF_CLASS(exception, zend_ce_throwable);
976 ZEND_PARSE_PARAMETERS_END();
977
978 Z_TRY_ADDREF_P(exception);
979
980 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
981
982 zend_generator_ensure_initialized(generator);
983
984 if (generator->execute_data) {
985 zend_generator *root = zend_generator_get_current(generator);
986
987 zend_generator_throw_exception(root, exception);
988
989 zend_generator_resume(generator);
990
991 root = zend_generator_get_current(generator);
992 if (generator->execute_data) {
993 zval *value = &root->value;
994
995 ZVAL_COPY_DEREF(return_value, value);
996 }
997 } else {
998 /* If the generator is already closed throw the exception in the
999 * current context */
1000 zend_throw_exception_object(exception);
1001 }
1002 }
1003 /* }}} */
1004
1005 /* {{{ Retrieves the return value of the generator */
ZEND_METHOD(Generator,getReturn)1006 ZEND_METHOD(Generator, getReturn)
1007 {
1008 zend_generator *generator;
1009
1010 ZEND_PARSE_PARAMETERS_NONE();
1011
1012 generator = (zend_generator *) Z_OBJ_P(ZEND_THIS);
1013
1014 zend_generator_ensure_initialized(generator);
1015 if (UNEXPECTED(EG(exception))) {
1016 return;
1017 }
1018
1019 if (Z_ISUNDEF(generator->retval)) {
1020 /* Generator hasn't returned yet -> error! */
1021 zend_throw_exception(NULL,
1022 "Cannot get return value of a generator that hasn't returned", 0);
1023 return;
1024 }
1025
1026 ZVAL_COPY(return_value, &generator->retval);
1027 }
1028 /* }}} */
1029
1030 /* get_iterator implementation */
1031
zend_generator_iterator_dtor(zend_object_iterator * iterator)1032 static void zend_generator_iterator_dtor(zend_object_iterator *iterator) /* {{{ */
1033 {
1034 zval_ptr_dtor(&iterator->data);
1035 }
1036 /* }}} */
1037
zend_generator_iterator_valid(zend_object_iterator * iterator)1038 static int zend_generator_iterator_valid(zend_object_iterator *iterator) /* {{{ */
1039 {
1040 zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
1041
1042 zend_generator_ensure_initialized(generator);
1043
1044 zend_generator_get_current(generator);
1045
1046 return generator->execute_data ? SUCCESS : FAILURE;
1047 }
1048 /* }}} */
1049
zend_generator_iterator_get_data(zend_object_iterator * iterator)1050 static zval *zend_generator_iterator_get_data(zend_object_iterator *iterator) /* {{{ */
1051 {
1052 zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data), *root;
1053
1054 zend_generator_ensure_initialized(generator);
1055
1056 root = zend_generator_get_current(generator);
1057
1058 return &root->value;
1059 }
1060 /* }}} */
1061
zend_generator_iterator_get_key(zend_object_iterator * iterator,zval * key)1062 static void zend_generator_iterator_get_key(zend_object_iterator *iterator, zval *key) /* {{{ */
1063 {
1064 zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data), *root;
1065
1066 zend_generator_ensure_initialized(generator);
1067
1068 root = zend_generator_get_current(generator);
1069
1070 if (EXPECTED(Z_TYPE(root->key) != IS_UNDEF)) {
1071 zval *zv = &root->key;
1072
1073 ZVAL_COPY_DEREF(key, zv);
1074 } else {
1075 ZVAL_NULL(key);
1076 }
1077 }
1078 /* }}} */
1079
zend_generator_iterator_move_forward(zend_object_iterator * iterator)1080 static void zend_generator_iterator_move_forward(zend_object_iterator *iterator) /* {{{ */
1081 {
1082 zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
1083
1084 zend_generator_ensure_initialized(generator);
1085
1086 zend_generator_resume(generator);
1087 }
1088 /* }}} */
1089
zend_generator_iterator_rewind(zend_object_iterator * iterator)1090 static void zend_generator_iterator_rewind(zend_object_iterator *iterator) /* {{{ */
1091 {
1092 zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
1093
1094 zend_generator_rewind(generator);
1095 }
1096 /* }}} */
1097
zend_generator_iterator_get_gc(zend_object_iterator * iterator,zval ** table,int * n)1098 static HashTable *zend_generator_iterator_get_gc(
1099 zend_object_iterator *iterator, zval **table, int *n)
1100 {
1101 *table = &iterator->data;
1102 *n = 1;
1103 return NULL;
1104 }
1105
1106 static const zend_object_iterator_funcs zend_generator_iterator_functions = {
1107 zend_generator_iterator_dtor,
1108 zend_generator_iterator_valid,
1109 zend_generator_iterator_get_data,
1110 zend_generator_iterator_get_key,
1111 zend_generator_iterator_move_forward,
1112 zend_generator_iterator_rewind,
1113 NULL,
1114 zend_generator_iterator_get_gc,
1115 };
1116
1117 /* by_ref is int due to Iterator API */
zend_generator_get_iterator(zend_class_entry * ce,zval * object,int by_ref)1118 zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1119 {
1120 zend_object_iterator *iterator;
1121 zend_generator *generator = (zend_generator*)Z_OBJ_P(object);
1122
1123 if (!generator->execute_data) {
1124 zend_throw_exception(NULL, "Cannot traverse an already closed generator", 0);
1125 return NULL;
1126 }
1127
1128 if (UNEXPECTED(by_ref) && !(generator->execute_data->func->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
1129 zend_throw_exception(NULL, "You can only iterate a generator by-reference if it declared that it yields by-reference", 0);
1130 return NULL;
1131 }
1132
1133 iterator = emalloc(sizeof(zend_object_iterator));
1134 zend_iterator_init(iterator);
1135
1136 iterator->funcs = &zend_generator_iterator_functions;
1137 ZVAL_OBJ_COPY(&iterator->data, Z_OBJ_P(object));
1138
1139 return iterator;
1140 }
1141 /* }}} */
1142
zend_register_generator_ce(void)1143 void zend_register_generator_ce(void) /* {{{ */
1144 {
1145 zend_class_entry ce;
1146
1147 INIT_CLASS_ENTRY(ce, "Generator", class_Generator_methods);
1148 zend_ce_generator = zend_register_internal_class(&ce);
1149 zend_ce_generator->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES;
1150 zend_ce_generator->create_object = zend_generator_create;
1151 zend_ce_generator->serialize = zend_class_serialize_deny;
1152 zend_ce_generator->unserialize = zend_class_unserialize_deny;
1153
1154 /* get_iterator has to be assigned *after* implementing the interface */
1155 zend_class_implements(zend_ce_generator, 1, zend_ce_iterator);
1156 zend_ce_generator->get_iterator = zend_generator_get_iterator;
1157
1158 memcpy(&zend_generator_handlers, &std_object_handlers, sizeof(zend_object_handlers));
1159 zend_generator_handlers.free_obj = zend_generator_free_storage;
1160 zend_generator_handlers.dtor_obj = zend_generator_dtor_storage;
1161 zend_generator_handlers.get_gc = zend_generator_get_gc;
1162 zend_generator_handlers.clone_obj = NULL;
1163 zend_generator_handlers.get_constructor = zend_generator_get_constructor;
1164
1165 INIT_CLASS_ENTRY(ce, "ClosedGeneratorException", NULL);
1166 zend_ce_ClosedGeneratorException = zend_register_internal_class_ex(&ce, zend_ce_exception);
1167 }
1168 /* }}} */
1169