Exemplo n.º 1
0
ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution) /* {{{ */
{
	if (Z_TYPE(generator->value) != IS_UNDEF) {
		zval_ptr_dtor(&generator->value);
		ZVAL_UNDEF(&generator->value);
	}

	if (Z_TYPE(generator->key) != IS_UNDEF) {
		zval_ptr_dtor(&generator->key);
		ZVAL_UNDEF(&generator->key);
	}

	if (generator->execute_data) {
		zend_execute_data *execute_data = generator->execute_data;
		zend_op_array *op_array = &execute_data->func->op_array;

		if (!execute_data->symbol_table) {
			zend_free_compiled_variables(execute_data);
		} else {
			zend_clean_and_cache_symbol_table(execute_data->symbol_table);
		}

		if (Z_OBJ(execute_data->This)) {
			OBJ_RELEASE(Z_OBJ(execute_data->This));
		}

		/* A fatal error / die occurred during the generator execution. Trying to clean
		 * up the stack may not be safe in this case. */
		if (CG(unclean_shutdown)) {
			generator->execute_data = NULL;
			return;
		}

		zend_vm_stack_free_extra_args(generator->execute_data);

		/* Some cleanups are only necessary if the generator was closued
		 * before it could finish execution (reach a return statement). */
		if (!finished_execution) {
			zend_generator_cleanup_unfinished_execution(generator);
		}

		/* Free a clone of closure */
		if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
			destroy_op_array(op_array);
			efree_size(op_array, sizeof(zend_op_array));
		}

		efree(generator->stack);
		generator->execute_data = NULL;
	}
}
Exemplo n.º 2
0
ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution TSRMLS_DC) /* {{{ */
{
	if (generator->value) {
		zval_ptr_dtor(&generator->value);
		generator->value = NULL;
	}

	if (generator->key) {
		zval_ptr_dtor(&generator->key);
		generator->key = NULL;
	}

	if (generator->execute_data) {
		zend_execute_data *execute_data = generator->execute_data;
		zend_op_array *op_array = execute_data->op_array;

		if (!execute_data->symbol_table) {
			zend_free_compiled_variables(execute_data);
		} else {
			zend_clean_and_cache_symbol_table(execute_data->symbol_table TSRMLS_CC);
		}

		if (execute_data->current_this) {
			zval_ptr_dtor(&execute_data->current_this);
		}

		/* A fatal error / die occured during the generator execution. Trying to clean
		 * up the stack may not be safe in this case. */
		if (CG(unclean_shutdown)) {
			return;
		}

		/* If the generator is closed before it can finish execution (reach
		 * a return statement) we have to free loop variables manually, as
		 * we don't know whether the SWITCH_FREE / FREE opcodes have run */
		if (!finished_execution) {
			/* -1 required because we want the last run opcode, not the
			 * next to-be-run one. */
			zend_uint op_num = execute_data->opline - op_array->opcodes - 1;

			int i;
			for (i = 0; i < op_array->last_brk_cont; ++i) {
				zend_brk_cont_element *brk_cont = op_array->brk_cont_array + i;

				if (brk_cont->start < 0) {
					continue;
				} else if (brk_cont->start > op_num) {
					break;
				} else if (brk_cont->brk > op_num) {
					zend_op *brk_opline = op_array->opcodes + brk_cont->brk;

					switch (brk_opline->opcode) {
						case ZEND_SWITCH_FREE:
							{
								temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
								zval_ptr_dtor(&var->var.ptr);
							}
							break;
						case ZEND_FREE:
							{
								temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
								zval_dtor(&var->tmp_var);
							}
							break;
					}
				}
			}
		}

		/* Clear any backed up stack arguments */
		if (generator->stack != EG(argument_stack)) {
			void **ptr = generator->stack->top - 1;
			void **end = zend_vm_stack_frame_base(execute_data);

			/* If the top stack element is the argument count, skip it */
			if (execute_data->function_state.arguments) {
				ptr--;
			}

			for (; ptr >= end; --ptr) {
				zval_ptr_dtor((zval**) ptr);
			}
		}

		while (execute_data->call >= execute_data->call_slots) {
			if (execute_data->call->object) {
				zval_ptr_dtor(&execute_data->call->object);
			}
			execute_data->call--;
		}

		/* We have added an additional stack frame in prev_execute_data, so we
		 * have to free it. It also contains the arguments passed to the
		 * generator (for func_get_args) so those have to be freed too. */
		{
			zend_execute_data *prev_execute_data = execute_data->prev_execute_data;
			void **arguments = prev_execute_data->function_state.arguments;

			if (arguments) {
				int arguments_count = (int) (zend_uintptr_t) *arguments;
				zval **arguments_start = (zval **) (arguments - arguments_count);
				int i;

				for (i = 0; i < arguments_count; ++i) {
					zval_ptr_dtor(arguments_start + i);
				}
			}
		}

		/* Free a clone of closure */
		if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
			destroy_op_array(op_array TSRMLS_CC);
			efree(op_array);
		}

		efree(generator->stack);
		if (generator->stack == EG(argument_stack)) {
			/* abnormal exit for running generator */
			EG(argument_stack) = NULL;
		}
		generator->execute_data = NULL;
	}
}
Exemplo n.º 3
0
void zend_generator_close(zend_generator *generator, zend_bool finished_execution TSRMLS_DC) /* {{{ */
{
	if (generator->execute_data) {
		zend_execute_data *execute_data = generator->execute_data;
		zend_op_array *op_array = execute_data->op_array;

		if (!finished_execution) {
			if (op_array->has_finally_block) {
				/* -1 required because we want the last run opcode, not the
				 * next to-be-run one. */
				zend_uint op_num = execute_data->opline - op_array->opcodes - 1;
				zend_uint finally_op_num = 0;

				/* Find next finally block */
				int i;
				for (i = 0; i < op_array->last_try_catch; i++) {
					zend_try_catch_element *try_catch = &op_array->try_catch_array[i];

					if (op_num < try_catch->try_op) {
						break;
					}

					if (op_num < try_catch->finally_op) {
						finally_op_num = try_catch->finally_op;
					}
				}

				/* If a finally block was found we jump directly to it and
				 * resume the generator. Furthermore we abort this close call
				 * because the generator will already be closed somewhere in
				 * the resume. */
				if (finally_op_num) {
					execute_data->opline = &op_array->opcodes[finally_op_num];
					execute_data->leaving = ZEND_RETURN;
					generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
					zend_generator_resume(generator TSRMLS_CC);
					return;
				}
			}
		}

		if (!execute_data->symbol_table) {
			zend_free_compiled_variables(execute_data->CVs, op_array->last_var);
		} else {
			zend_clean_and_cache_symbol_table(execute_data->symbol_table TSRMLS_CC);
		}

		if (execute_data->current_this) {
			zval_ptr_dtor(&execute_data->current_this);
		}

		if (execute_data->object) {
			zval_ptr_dtor(&execute_data->object);
		}

		/* If the generator is closed before it can finish execution (reach
		 * a return statement) we have to free loop variables manually, as
		 * we don't know whether the SWITCH_FREE / FREE opcodes have run */
		if (!finished_execution) {
			/* -1 required because we want the last run opcode, not the
			 * next to-be-run one. */
			zend_uint op_num = execute_data->opline - op_array->opcodes - 1;

			int i;
			for (i = 0; i < op_array->last_brk_cont; ++i) {
				zend_brk_cont_element *brk_cont = op_array->brk_cont_array + i;

				if (brk_cont->start < 0) {
					continue;
				} else if (brk_cont->start > op_num) {
					break;
				} else if (brk_cont->brk > op_num) {
					zend_op *brk_opline = op_array->opcodes + brk_cont->brk;

					switch (brk_opline->opcode) {
						case ZEND_SWITCH_FREE:
							{
								temp_variable *var = (temp_variable *) ((char *) execute_data->Ts + brk_opline->op1.var);
								zval_ptr_dtor(&var->var.ptr);
							}
							break;
						case ZEND_FREE:
							{
								temp_variable *var = (temp_variable *) ((char *) execute_data->Ts + brk_opline->op1.var);
								zval_dtor(&var->tmp_var);
							}
							break;
					}
				}
			}
		}

		/* Clear any backed up stack arguments */
		if (generator->backed_up_stack) {
			zval **zvals = (zval **) generator->backed_up_stack;
			size_t zval_num = generator->backed_up_stack_size / sizeof(zval *);
			int i;

			for (i = 0; i < zval_num; i++) {
				zval_ptr_dtor(&zvals[i]);
			}

			efree(generator->backed_up_stack);
		}

		if (generator->backed_up_arg_types_stack) {
			/* The arg types stack contains three elements per call: fbc, object
			 * and called_scope. Here we traverse the stack from top to bottom
			 * and dtor the object. */
			int i = generator->backed_up_arg_types_stack_count / 3;
			while (i--) {
				zval *object = (zval *) generator->backed_up_arg_types_stack[3*i + 1];
				if (object) {
					zval_ptr_dtor(&object);
				}
			}

			efree(generator->backed_up_arg_types_stack);
		} 

		/* We have added an additional stack frame in prev_execute_data, so we
		 * have to free it. It also contains the arguments passed to the
		 * generator (for func_get_args) so those have to be freed too. */
		{
			zend_execute_data *prev_execute_data = execute_data->prev_execute_data;
			void **arguments = prev_execute_data->function_state.arguments;

			if (arguments) {
				int arguments_count = (int) (zend_uintptr_t) *arguments;
				zval **arguments_start = (zval **) (arguments - arguments_count);
				int i;

				for (i = 0; i < arguments_count; ++i) {
					zval_ptr_dtor(arguments_start + i);
				}

				efree(arguments_start);
			}

			efree(prev_execute_data);
		}

		/* Free a clone of closure */
		if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
			destroy_op_array(op_array TSRMLS_CC);
			efree(op_array);
		}

		efree(execute_data);
		generator->execute_data = NULL;
	}

	if (generator->value) {
		zval_ptr_dtor(&generator->value);
		generator->value = NULL;
	}

	if (generator->key) {
		zval_ptr_dtor(&generator->key);
		generator->key = NULL;
	}
}
Exemplo n.º 4
0
ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution TSRMLS_DC) /* {{{ */
{
	if (generator->execute_data) {
		zend_execute_data *execute_data = generator->execute_data;
		zend_op_array *op_array = execute_data->op_array;

		if (!finished_execution) {
			if (op_array->has_finally_block) {
				/* -1 required because we want the last run opcode, not the
  				 * next to-be-run one. */
				zend_uint op_num = execute_data->opline - op_array->opcodes - 1;
				zend_uint finally_op_num = 0;

				/* Find next finally block */
				int i;
				for (i = 0; i < op_array->last_try_catch; i++) {
					zend_try_catch_element *try_catch = &op_array->try_catch_array[i];

					if (op_num < try_catch->try_op) {
						break;
					}

					if (op_num < try_catch->finally_op) {
						finally_op_num = try_catch->finally_op;
					}
				}

				/* If a finally block was found we jump directly to it and
				 * resume the generator. Furthermore we abort this close call
				 * because the generator will already be closed somewhere in
				 * the resume. */
				if (finally_op_num) {
					execute_data->opline = &op_array->opcodes[finally_op_num];
					execute_data->fast_ret = NULL;
					generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
					zend_generator_resume(generator TSRMLS_CC);
					return;
				}
			}
		}

		if (!execute_data->symbol_table) {
			zend_free_compiled_variables(execute_data);
		} else {
			zend_clean_and_cache_symbol_table(execute_data->symbol_table TSRMLS_CC);
		}

		if (execute_data->current_this) {
			zval_ptr_dtor(&execute_data->current_this);
		}

		/* If the generator is closed before it can finish execution (reach
		 * a return statement) we have to free loop variables manually, as
		 * we don't know whether the SWITCH_FREE / FREE opcodes have run */
		if (!finished_execution) {
			/* -1 required because we want the last run opcode, not the
			 * next to-be-run one. */
			zend_uint op_num = execute_data->opline - op_array->opcodes - 1;

			int i;
			for (i = 0; i < op_array->last_brk_cont; ++i) {
				zend_brk_cont_element *brk_cont = op_array->brk_cont_array + i;

				if (brk_cont->start < 0) {
					continue;
				} else if (brk_cont->start > op_num) {
					break;
				} else if (brk_cont->brk > op_num) {
					zend_op *brk_opline = op_array->opcodes + brk_cont->brk;

					switch (brk_opline->opcode) {
						case ZEND_SWITCH_FREE:
							{
								temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
								zval_ptr_dtor(&var->var.ptr);
							}
							break;
						case ZEND_FREE:
							{
								temp_variable *var = EX_TMP_VAR(execute_data, brk_opline->op1.var);
								zval_dtor(&var->tmp_var);
							}
							break;
					}
				}
			}
		}

		/* Clear any backed up stack arguments */
		if (generator->stack != EG(argument_stack)) {
			void **stack_frame = zend_vm_stack_frame_base(execute_data);
			while (generator->stack->top != stack_frame) {
				zval_ptr_dtor((zval**)stack_frame);
				stack_frame++;
			}
		}

		while (execute_data->call >= execute_data->call_slots) {
			if (execute_data->call->object) {
				zval_ptr_dtor(&execute_data->call->object);
			}
			execute_data->call--;
		}

		/* We have added an additional stack frame in prev_execute_data, so we
		 * have to free it. It also contains the arguments passed to the
		 * generator (for func_get_args) so those have to be freed too. */
		{
			zend_execute_data *prev_execute_data = execute_data->prev_execute_data;
			void **arguments = prev_execute_data->function_state.arguments;

			if (arguments) {
				int arguments_count = (int) (zend_uintptr_t) *arguments;
				zval **arguments_start = (zval **) (arguments - arguments_count);
				int i;

				for (i = 0; i < arguments_count; ++i) {
					zval_ptr_dtor(arguments_start + i);
				}
			}
		}

		/* Free a clone of closure */
		if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
			destroy_op_array(op_array TSRMLS_CC);
			efree(op_array);
		}

		efree(generator->stack);
		if (generator->stack == EG(argument_stack)) {
			/* abnormal exit for running generator */
			EG(argument_stack) = NULL;
		}
		generator->execute_data = NULL;
	}

	if (generator->value) {
		zval_ptr_dtor(&generator->value);
		generator->value = NULL;
	}

	if (generator->key) {
		zval_ptr_dtor(&generator->key);
		generator->key = NULL;
	}
}