void zend_optimize_func_calls(zend_op_array *op_array, zend_optimizer_ctx *ctx) { zend_op *opline = op_array->opcodes; zend_op *end = opline + op_array->last; int call = 0; void *checkpoint; optimizer_call_info *call_stack; if (op_array->last < 2) { return; } checkpoint = zend_arena_checkpoint(ctx->arena); call_stack = zend_arena_calloc(&ctx->arena, op_array->last / 2, sizeof(optimizer_call_info)); while (opline < end) { switch (opline->opcode) { case ZEND_INIT_FCALL_BY_NAME: case ZEND_INIT_NS_FCALL_BY_NAME: case ZEND_INIT_STATIC_METHOD_CALL: case ZEND_INIT_METHOD_CALL: case ZEND_INIT_FCALL: case ZEND_NEW: call_stack[call].func = zend_optimizer_get_called_func( ctx->script, op_array, opline, 0); call_stack[call].try_inline = opline->opcode != ZEND_NEW; /* break missing intentionally */ case ZEND_INIT_DYNAMIC_CALL: case ZEND_INIT_USER_CALL: call_stack[call].opline = opline; call++; break; case ZEND_DO_FCALL: case ZEND_DO_ICALL: case ZEND_DO_UCALL: case ZEND_DO_FCALL_BY_NAME: call--; if (call_stack[call].func && call_stack[call].opline) { zend_op *fcall = call_stack[call].opline; if (fcall->opcode == ZEND_INIT_FCALL) { /* nothing to do */ } else if (fcall->opcode == ZEND_INIT_FCALL_BY_NAME) { fcall->opcode = ZEND_INIT_FCALL; fcall->op1.num = zend_vm_calc_used_stack(fcall->extended_value, call_stack[call].func); Z_CACHE_SLOT(op_array->literals[fcall->op2.constant + 1]) = Z_CACHE_SLOT(op_array->literals[fcall->op2.constant]); literal_dtor(&ZEND_OP2_LITERAL(fcall)); fcall->op2.constant = fcall->op2.constant + 1; opline->opcode = zend_get_call_op(fcall, call_stack[call].func); } else if (fcall->opcode == ZEND_INIT_NS_FCALL_BY_NAME) { fcall->opcode = ZEND_INIT_FCALL; fcall->op1.num = zend_vm_calc_used_stack(fcall->extended_value, call_stack[call].func); Z_CACHE_SLOT(op_array->literals[fcall->op2.constant + 1]) = Z_CACHE_SLOT(op_array->literals[fcall->op2.constant]); literal_dtor(&op_array->literals[fcall->op2.constant]); literal_dtor(&op_array->literals[fcall->op2.constant + 2]); fcall->op2.constant = fcall->op2.constant + 1; opline->opcode = zend_get_call_op(fcall, call_stack[call].func); } else if (fcall->opcode == ZEND_INIT_STATIC_METHOD_CALL || fcall->opcode == ZEND_INIT_METHOD_CALL || fcall->opcode == ZEND_NEW) { /* We don't have specialized opcodes for this, do nothing */ } else { ZEND_ASSERT(0); } if ((ZEND_OPTIMIZER_PASS_16 & ctx->optimization_level) && call_stack[call].try_inline) { zend_try_inline_call(op_array, fcall, opline, call_stack[call].func); } } call_stack[call].func = NULL; call_stack[call].opline = NULL; call_stack[call].try_inline = 0; break; case ZEND_FETCH_FUNC_ARG: case ZEND_FETCH_STATIC_PROP_FUNC_ARG: case ZEND_FETCH_OBJ_FUNC_ARG: case ZEND_FETCH_DIM_FUNC_ARG: if (call_stack[call - 1].func) { if (ARG_SHOULD_BE_SENT_BY_REF(call_stack[call - 1].func, (opline->extended_value & ZEND_FETCH_ARG_MASK))) { opline->extended_value &= ZEND_FETCH_TYPE_MASK; if (opline->opcode != ZEND_FETCH_STATIC_PROP_FUNC_ARG) { opline->opcode -= 9; } else { opline->opcode = ZEND_FETCH_STATIC_PROP_W; } } else { if (opline->opcode == ZEND_FETCH_DIM_FUNC_ARG && opline->op2_type == IS_UNUSED) { /* FETCH_DIM_FUNC_ARG supports UNUSED op2, while FETCH_DIM_R does not. * Performing the replacement would create an invalid opcode. */ call_stack[call - 1].try_inline = 0; break; } opline->extended_value &= ZEND_FETCH_TYPE_MASK; if (opline->opcode != ZEND_FETCH_STATIC_PROP_FUNC_ARG) { opline->opcode -= 12; } else { opline->opcode = ZEND_FETCH_STATIC_PROP_R; } } } break; case ZEND_SEND_VAL_EX: if (call_stack[call - 1].func) { if (ARG_MUST_BE_SENT_BY_REF(call_stack[call - 1].func, opline->op2.num)) { /* We won't convert it into_DO_FCALL to emit error at run-time */ call_stack[call - 1].opline = NULL; } else { opline->opcode = ZEND_SEND_VAL; } } break; case ZEND_SEND_VAR_EX: if (call_stack[call - 1].func) { if (ARG_SHOULD_BE_SENT_BY_REF(call_stack[call - 1].func, opline->op2.num)) { opline->opcode = ZEND_SEND_REF; } else { opline->opcode = ZEND_SEND_VAR; } } break; case ZEND_SEND_VAR_NO_REF_EX: if (call_stack[call - 1].func) { if (ARG_MUST_BE_SENT_BY_REF(call_stack[call - 1].func, opline->op2.num)) { opline->opcode = ZEND_SEND_VAR_NO_REF; } else if (ARG_MAY_BE_SENT_BY_REF(call_stack[call - 1].func, opline->op2.num)) { opline->opcode = ZEND_SEND_VAL; } else { opline->opcode = ZEND_SEND_VAR; } } break; case ZEND_SEND_UNPACK: case ZEND_SEND_USER: case ZEND_SEND_ARRAY: call_stack[call - 1].try_inline = 0; break; default: break; } opline++; } zend_arena_release(&ctx->arena, checkpoint); }
/** * Copies of internal methods from Zend/zend_execute_API.c * These are used to call internal methods (not in the function table) from the external method. * TODO: See if xdebug works */ int runkit_forward_call_user_function(zend_function *fbc, zend_function *fbc_inner, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ { uint32_t i; zend_execute_data *call, dummy_execute_data; zend_fcall_info_cache fci_cache_local = {0}; zend_function *func; /* {{{ patch for runkit */ zend_fcall_info fci = {0}; zend_fcall_info_cache *fci_cache = NULL; fci.size = sizeof(fci); fci.object = NULL; // FIXME for methods? // object ? Z_OBJ_P(object) : NULL; ZVAL_STR(&fci.function_name, fbc_inner->common.function_name); zend_string_addref(fbc_inner->common.function_name); fci.retval = return_value; fci.param_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data)); fci.params = ZEND_CALL_ARG(EG(current_execute_data), 1); // params and param_count From zend_API.c fci.no_separation = (zend_bool)1; // ??? /* end patch for runkit }}} */ ZVAL_UNDEF(fci.retval); if (!EG(active)) { return FAILURE; /* executor is already inactive */ } if (EG(exception)) { return FAILURE; /* we would result in an unstable executor otherwise */ } /* Initialize execute_data */ if (!EG(current_execute_data)) { /* This only happens when we're called outside any execute()'s * It shouldn't be strictly necessary to NULL execute_data out, * but it may make bugs easier to spot */ memset(&dummy_execute_data, 0, sizeof(zend_execute_data)); EG(current_execute_data) = &dummy_execute_data; } else if (EG(current_execute_data)->func && ZEND_USER_CODE(EG(current_execute_data)->func->common.type) && EG(current_execute_data)->opline->opcode != ZEND_DO_FCALL && EG(current_execute_data)->opline->opcode != ZEND_DO_ICALL && EG(current_execute_data)->opline->opcode != ZEND_DO_UCALL && EG(current_execute_data)->opline->opcode != ZEND_DO_FCALL_BY_NAME) { /* Insert fake frame in case of include or magic calls */ dummy_execute_data = *EG(current_execute_data); dummy_execute_data.prev_execute_data = EG(current_execute_data); dummy_execute_data.call = NULL; dummy_execute_data.opline = NULL; dummy_execute_data.func = NULL; EG(current_execute_data) = &dummy_execute_data; } if (!fci_cache || !RUNKIT_IS_FCI_CACHE_INITIALIZED(fci_cache)) { zend_string *callable_name; char *error = NULL; if (!fci_cache) { fci_cache = &fci_cache_local; } if (!zend_is_callable_ex(&fci.function_name, fci.object, IS_CALLABLE_CHECK_SILENT, &callable_name, fci_cache, &error)) { if (error) { zend_error(E_WARNING, "Invalid callback %s, %s", ZSTR_VAL(callable_name), error); efree(error); } if (callable_name) { zend_string_release(callable_name); } if (EG(current_execute_data) == &dummy_execute_data) { EG(current_execute_data) = dummy_execute_data.prev_execute_data; } return FAILURE; } else if (error) { /* Capitalize the first latter of the error message */ if (error[0] >= 'a' && error[0] <= 'z') { error[0] += ('A' - 'a'); } zend_error(E_DEPRECATED, "%s", error); efree(error); } zend_string_release(callable_name); } func = fbc_inner; fci.object = (func->common.fn_flags & ZEND_ACC_STATIC) ? NULL : fci_cache->object; call = zend_vm_stack_push_call_frame(ZEND_CALL_TOP_FUNCTION | ZEND_CALL_DYNAMIC, func, fci.param_count, fci_cache->called_scope, fci.object); if (fci.object && (!EG(objects_store).object_buckets || !IS_OBJ_VALID(EG(objects_store).object_buckets[fci.object->handle]))) { if (EG(current_execute_data) == &dummy_execute_data) { EG(current_execute_data) = dummy_execute_data.prev_execute_data; } return FAILURE; } if (func->common.fn_flags & (ZEND_ACC_ABSTRACT | ZEND_ACC_DEPRECATED)) { if (func->common.fn_flags & ZEND_ACC_ABSTRACT) { zend_throw_error(NULL, "Cannot call abstract method %s::%s()", ZSTR_VAL(func->common.scope->name), ZSTR_VAL(func->common.function_name)); if (EG(current_execute_data) == &dummy_execute_data) { EG(current_execute_data) = dummy_execute_data.prev_execute_data; } return FAILURE; } if (func->common.fn_flags & ZEND_ACC_DEPRECATED) { zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated", func->common.scope ? ZSTR_VAL(func->common.scope->name) : "", func->common.scope ? "::" : "", ZSTR_VAL(func->common.function_name)); } } for (i = 0; i < fci.param_count; i++) { zval *param; zval *arg = &fci.params[i]; if (ARG_SHOULD_BE_SENT_BY_REF(func, i + 1)) { if (UNEXPECTED(!Z_ISREF_P(arg))) { if (!fci.no_separation) { /* Separation is enabled -- create a ref */ ZVAL_NEW_REF(arg, arg); } else if (!ARG_MAY_BE_SENT_BY_REF(func, i + 1)) { /* By-value send is not allowed -- emit a warning, * but still perform the call with a by-value send. */ zend_error(E_WARNING, "Parameter %d to %s%s%s() expected to be a reference, value given", i + 1, func->common.scope ? ZSTR_VAL(func->common.scope->name) : "", func->common.scope ? "::" : "", ZSTR_VAL(func->common.function_name)); } } } else { if (Z_ISREF_P(arg) && !(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) { /* don't separate references for __call */ arg = Z_REFVAL_P(arg); } } param = ZEND_CALL_ARG(call, i + 1); ZVAL_COPY(param, arg); } if (UNEXPECTED(func->op_array.fn_flags & ZEND_ACC_CLOSURE)) { ZEND_ASSERT(GC_TYPE((zend_object *)func->op_array.prototype) == IS_OBJECT); GC_ADDREF((zend_object *)func->op_array.prototype); ZEND_ADD_CALL_FLAG(call, ZEND_CALL_CLOSURE); } if (func->type == ZEND_USER_FUNCTION) { int call_via_handler = (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0; zend_init_execute_data(call, &func->op_array, fci.retval); zend_execute_ex(call); if (call_via_handler) { /* We must re-initialize function again */ RUNKIT_CLEAR_FCI_CACHE(fci_cache); } } else if (func->type == ZEND_INTERNAL_FUNCTION) { int call_via_handler = (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0; ZVAL_NULL(fci.retval); call->prev_execute_data = EG(current_execute_data); call->return_value = NULL; /* this is not a constructor call */ EG(current_execute_data) = call; if (EXPECTED(zend_execute_internal == NULL)) { /* saves one function call if zend_execute_internal is not used */ func->internal_function.handler(call, fci.retval); } else { zend_execute_internal(call, fci.retval); } EG(current_execute_data) = call->prev_execute_data; zend_vm_stack_free_args(call); /* We shouldn't fix bad extensions here, because it can break proper ones (Bug #34045) if (!EX(function_state).function->common.return_reference) { INIT_PZVAL(f->retval); }*/ if (EG(exception)) { zval_ptr_dtor(fci.retval); ZVAL_UNDEF(fci.retval); } if (call_via_handler) { /* We must re-initialize function again */ RUNKIT_CLEAR_FCI_CACHE(fci_cache); } } else { /* ZEND_OVERLOADED_FUNCTION */ ZVAL_NULL(fci.retval); /* Not sure what should be done here if it's a static method */ if (fci.object) { call->prev_execute_data = EG(current_execute_data); EG(current_execute_data) = call; fci.object->handlers->call_method(func->common.function_name, fci.object, call, fci.retval); EG(current_execute_data) = call->prev_execute_data; } else { zend_throw_error(NULL, "Cannot call overloaded function for non-object"); } zend_vm_stack_free_args(call); if (func->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) { zend_string_release(func->common.function_name); } efree(func); if (EG(exception)) { zval_ptr_dtor(fci.retval); ZVAL_UNDEF(fci.retval); } } zend_vm_stack_free_call_frame(call); if (EG(current_execute_data) == &dummy_execute_data) { EG(current_execute_data) = dummy_execute_data.prev_execute_data; } if (EG(exception)) { zend_throw_exception_internal(NULL); } return SUCCESS; }