Fixied calling object closures from internal functions

This commit is contained in:
Dmitry Stogov 2014-03-05 01:54:21 +04:00
parent db7b5325f0
commit 19670c2bbc
9 changed files with 58 additions and 59 deletions

View File

@ -2762,8 +2762,8 @@ static int zend_is_callable_check_class(zend_string *name, zend_fcall_info_cache
} else {
fcc->called_scope = EG(called_scope);
fcc->calling_scope = EG(scope);
if (!fcc->object_ptr && Z_TYPE(EG(This)) == IS_OBJECT) {
fcc->object_ptr = &EG(This);
if (Z_TYPE(fcc->object) == IS_UNDEF && Z_TYPE(EG(This)) == IS_OBJECT) {
ZVAL_COPY_VALUE(&fcc->object, &EG(This));
}
ret = 1;
}
@ -2776,8 +2776,8 @@ static int zend_is_callable_check_class(zend_string *name, zend_fcall_info_cache
} else {
fcc->called_scope = EG(called_scope);
fcc->calling_scope = EG(scope)->parent;
if (!fcc->object_ptr && Z_TYPE(EG(This)) == IS_OBJECT) {
fcc->object_ptr = &EG(This);
if (Z_TYPE(fcc->object) == IS_UNDEF && Z_TYPE(EG(This)) == IS_OBJECT) {
ZVAL_COPY_VALUE(&fcc->object, &EG(This));
}
*strict_class = 1;
ret = 1;
@ -2789,8 +2789,8 @@ static int zend_is_callable_check_class(zend_string *name, zend_fcall_info_cache
} else {
fcc->called_scope = EG(called_scope);
fcc->calling_scope = EG(called_scope);
if (!fcc->object_ptr && Z_TYPE(EG(This)) == IS_OBJECT) {
fcc->object_ptr = &EG(This);
if (Z_TYPE(fcc->object) == IS_UNDEF && Z_TYPE(EG(This)) == IS_OBJECT) {
ZVAL_COPY_VALUE(&fcc->object, &EG(This));
}
*strict_class = 1;
ret = 1;
@ -2799,13 +2799,13 @@ static int zend_is_callable_check_class(zend_string *name, zend_fcall_info_cache
zend_class_entry *scope = EG(active_op_array) ? EG(active_op_array)->scope : NULL;
fcc->calling_scope = ce;
if (scope && !fcc->object_ptr && Z_TYPE(EG(This)) != IS_UNDEF &&
if (scope && Z_TYPE(fcc->object) == IS_UNDEF && Z_TYPE(EG(This)) != IS_UNDEF &&
instanceof_function(Z_OBJCE(EG(This)), scope TSRMLS_CC) &&
instanceof_function(scope, fcc->calling_scope TSRMLS_CC)) {
fcc->object_ptr = &EG(This);
fcc->called_scope = Z_OBJCE_P(fcc->object_ptr);
ZVAL_COPY_VALUE(&fcc->object, &EG(This));
fcc->called_scope = Z_OBJCE(fcc->object);
} else {
fcc->called_scope = fcc->object_ptr ? Z_OBJCE_P(fcc->object_ptr) : fcc->calling_scope;
fcc->called_scope = Z_TYPE(fcc->object) == IS_OBJECT ? Z_OBJCE(fcc->object) : fcc->calling_scope;
}
*strict_class = 1;
ret = 1;
@ -2931,10 +2931,10 @@ static int zend_is_callable_check_func(int check_flags, zval *callable, zend_fca
}
if ((check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0 &&
(fcc->calling_scope &&
((fcc->object_ptr && fcc->calling_scope->__call) ||
(!fcc->object_ptr && fcc->calling_scope->__callstatic)))) {
((Z_TYPE(fcc->object) == IS_OBJECT && fcc->calling_scope->__call) ||
(Z_TYPE(fcc->object) == IS_UNDEF && fcc->calling_scope->__callstatic)))) {
if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
if (!zend_check_private(fcc->function_handler, fcc->object_ptr ? Z_OBJCE_P(fcc->object_ptr) : EG(scope), lmname TSRMLS_CC)) {
if (!zend_check_private(fcc->function_handler, Z_TYPE(fcc->object) == IS_OBJECT ? Z_OBJCE(fcc->object) : EG(scope), lmname TSRMLS_CC)) {
retval = 0;
fcc->function_handler = NULL;
goto get_function_via_handler;
@ -2949,7 +2949,7 @@ static int zend_is_callable_check_func(int check_flags, zval *callable, zend_fca
}
} else {
get_function_via_handler:
if (fcc->object_ptr && fcc->calling_scope == ce_org) {
if (Z_TYPE(fcc->object) == IS_OBJECT && fcc->calling_scope == ce_org) {
if (strict_class && ce_org->__call) {
fcc->function_handler = emalloc(sizeof(zend_internal_function));
fcc->function_handler->internal_function.type = ZEND_INTERNAL_FUNCTION;
@ -2963,8 +2963,8 @@ get_function_via_handler:
STR_ADDREF(mname);
call_via_handler = 1;
retval = 1;
} else if (Z_OBJ_HT_P(fcc->object_ptr)->get_method) {
fcc->function_handler = Z_OBJ_HT_P(fcc->object_ptr)->get_method(fcc->object_ptr, mname, NULL TSRMLS_CC);
} else if (Z_OBJ_HT(fcc->object)->get_method) {
fcc->function_handler = Z_OBJ_HT(fcc->object)->get_method(&fcc->object, mname, NULL TSRMLS_CC);
if (fcc->function_handler) {
if (strict_class &&
(!fcc->function_handler->common.scope ||
@ -2990,10 +2990,10 @@ get_function_via_handler:
if (fcc->function_handler) {
retval = 1;
call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0;
if (call_via_handler && !fcc->object_ptr && Z_TYPE(EG(This)) != IS_UNDEF &&
if (call_via_handler && Z_TYPE(fcc->object) == IS_UNDEF && Z_TYPE(EG(This)) != IS_UNDEF &&
Z_OBJ_HT(EG(This))->get_class_entry &&
instanceof_function(Z_OBJCE(EG(This)), fcc->calling_scope TSRMLS_CC)) {
fcc->object_ptr = &EG(This);
ZVAL_COPY_VALUE(&fcc->object, &EG(This));
}
}
}
@ -3001,14 +3001,14 @@ get_function_via_handler:
if (retval) {
if (fcc->calling_scope && !call_via_handler) {
if (!fcc->object_ptr && (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT)) {
if (Z_TYPE(fcc->object) == IS_UNDEF && (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT)) {
if (error) {
zend_spprintf(error, 0, "cannot call abstract method %s::%s()", fcc->calling_scope->name->val, fcc->function_handler->common.function_name->val);
retval = 0;
} else {
zend_error(E_ERROR, "Cannot call abstract method %s::%s()", fcc->calling_scope->name->val, fcc->function_handler->common.function_name->val);
}
} else if (!fcc->object_ptr && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
} else if (Z_TYPE(fcc->object) == IS_UNDEF && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
int severity;
char *verb;
if (fcc->function_handler->common.fn_flags & ZEND_ACC_ALLOW_STATIC) {
@ -3023,7 +3023,7 @@ get_function_via_handler:
retval = 0;
}
if (Z_TYPE(EG(This)) != IS_UNDEF && instanceof_function(Z_OBJCE(EG(This)), fcc->calling_scope TSRMLS_CC)) {
fcc->object_ptr = &EG(This);
ZVAL_COPY_VALUE(&fcc->object, &EG(This));
if (error) {
zend_spprintf(error, 0, "non-static method %s::%s() %s be called statically, assuming $this from compatible context %s", fcc->calling_scope->name->val, fcc->function_handler->common.function_name->val, verb, Z_OBJCE(EG(This))->name->val);
if (severity == E_ERROR) {
@ -3045,7 +3045,7 @@ get_function_via_handler:
}
if (retval && (check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0) {
if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
if (!zend_check_private(fcc->function_handler, fcc->object_ptr ? Z_OBJCE_P(fcc->object_ptr) : EG(scope), lmname TSRMLS_CC)) {
if (!zend_check_private(fcc->function_handler, Z_TYPE(fcc->object) == IS_OBJECT ? Z_OBJCE(fcc->object) : EG(scope), lmname TSRMLS_CC)) {
if (error) {
if (*error) {
efree(*error);
@ -3077,8 +3077,8 @@ get_function_via_handler:
STR_FREE(lmname);
STR_RELEASE(mname);
if (fcc->object_ptr) {
fcc->called_scope = Z_OBJCE_P(fcc->object_ptr);
if (Z_TYPE(fcc->object) == IS_OBJECT) {
fcc->called_scope = Z_OBJCE(fcc->object);
}
if (retval) {
fcc->initialized = 1;
@ -3107,7 +3107,7 @@ ZEND_API zend_bool zend_is_callable_ex(zval *callable, zval *object_ptr, uint ch
fcc->called_scope = NULL;
fcc->function_handler = NULL;
fcc->calling_scope = NULL;
fcc->object_ptr = NULL;
ZVAL_UNDEF(&fcc->object);
if (object_ptr && Z_TYPE_P(object_ptr) != IS_OBJECT) {
object_ptr = NULL;
@ -3122,7 +3122,7 @@ ZEND_API zend_bool zend_is_callable_ex(zval *callable, zval *object_ptr, uint ch
switch (Z_TYPE_P(callable)) {
case IS_STRING:
if (object_ptr) {
fcc->object_ptr = object_ptr;
ZVAL_COPY_VALUE(&fcc->object, object_ptr);
fcc->calling_scope = Z_OBJCE_P(object_ptr);
if (callable_name) {
char *ptr;
@ -3208,7 +3208,7 @@ ZEND_API zend_bool zend_is_callable_ex(zval *callable, zval *object_ptr, uint ch
fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
fcc->object_ptr = obj;
ZVAL_COPY_VALUE(&fcc->object, obj);
if (callable_name) {
char *ptr;
@ -3260,7 +3260,7 @@ ZEND_API zend_bool zend_is_callable_ex(zval *callable, zval *object_ptr, uint ch
return 0;
case IS_OBJECT:
if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(callable, &fcc->calling_scope, &fcc->function_handler, fcc->object_ptr TSRMLS_CC) == SUCCESS) {
if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(callable, &fcc->calling_scope, &fcc->function_handler, &fcc->object TSRMLS_CC) == SUCCESS) {
fcc->called_scope = fcc->calling_scope;
if (callable_name) {
zend_class_entry *ce = Z_OBJCE_P(callable); /* TBFixed: what if it's overloaded? */
@ -3329,7 +3329,7 @@ ZEND_API int zend_fcall_info_init(zval *callable, uint check_flags, zend_fcall_i
fci->size = sizeof(*fci);
fci->function_table = fcc->calling_scope ? &fcc->calling_scope->function_table : EG(function_table);
fci->object_ptr = fcc->object_ptr;
fci->object_ptr = &fcc->object;
ZVAL_COPY_VALUE(&fci->function_name, callable);
fci->retval = NULL;
fci->param_count = 0;

View File

@ -57,7 +57,7 @@ typedef struct _zend_fcall_info_cache {
zend_function *function_handler;
zend_class_entry *calling_scope;
zend_class_entry *called_scope;
zval *object_ptr;
zval object;
} zend_fcall_info_cache;
#define ZEND_NS_NAME(ns, name) ns "\\" name

View File

@ -43,7 +43,7 @@ ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data_ptr, zend
/* true globals */
ZEND_API const zend_fcall_info empty_fcall_info = { 0, NULL, {{0},0}, NULL, NULL, 0, NULL, NULL, 0 };
ZEND_API const zend_fcall_info_cache empty_fcall_info_cache = { 0, NULL, NULL, NULL, NULL };
ZEND_API const zend_fcall_info_cache empty_fcall_info_cache = { 0, NULL, NULL, NULL, {{0},0} };
#ifdef ZEND_WIN32
#include <process.h>
@ -811,12 +811,8 @@ int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TS
EX(function_state).function = fci_cache->function_handler;
calling_scope = fci_cache->calling_scope;
called_scope = fci_cache->called_scope;
fci->object_ptr = fci_cache->object_ptr;
if (fci->object_ptr) {
ZVAL_COPY_VALUE(&EX(object), fci->object_ptr);
} else {
ZVAL_UNDEF(&EX(object));
}
fci->object_ptr = &fci_cache->object;
ZVAL_COPY_VALUE(&EX(object), fci->object_ptr);
if (fci->object_ptr && Z_TYPE_P(fci->object_ptr) == IS_OBJECT &&
(!EG(objects_store).object_buckets ||
!IS_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(fci->object_ptr)]))) {
@ -1084,7 +1080,7 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, const zend_li
fcall_cache.function_handler = EG(autoload_func);
fcall_cache.calling_scope = NULL;
fcall_cache.called_scope = NULL;
fcall_cache.object_ptr = NULL;
ZVAL_UNDEF(&fcall_cache.object);
zend_exception_save(TSRMLS_C);
retval = zend_call_function(&fcall_info, &fcall_cache TSRMLS_CC);

View File

@ -96,7 +96,11 @@ ZEND_API zval* zend_call_method(zval *object, zend_class_entry *obj_ce, zend_fun
} else {
fcic.called_scope = EG(called_scope);
}
fcic.object_ptr = object;
if (object) {
ZVAL_COPY_VALUE(&fcic.object, object);
} else {
ZVAL_UNDEF(&fcic.object);
}
result = zend_call_function(&fci, &fcic TSRMLS_CC);
zval_ptr_dtor(&fci.function_name);
}

View File

@ -1388,7 +1388,7 @@ static void _reflection_export(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *c
fcc.function_handler = ce_ptr->constructor;
fcc.calling_scope = ce_ptr;
fcc.called_scope = Z_OBJCE(reflector);
fcc.object_ptr = &reflector;
ZVAL_COPY_VALUE(&fcc.object, &reflector);
result = zend_call_function(&fci, &fcc TSRMLS_CC);
@ -1904,7 +1904,7 @@ ZEND_METHOD(reflection_function, invoke)
fcc.function_handler = fptr;
fcc.calling_scope = EG(scope);
fcc.called_scope = NULL;
fcc.object_ptr = NULL;
ZVAL_UNDEF(&fcc.object);
result = zend_call_function(&fci, &fcc TSRMLS_CC);
@ -1972,7 +1972,7 @@ ZEND_METHOD(reflection_function, invokeArgs)
fcc.function_handler = fptr;
fcc.calling_scope = EG(scope);
fcc.called_scope = NULL;
fcc.object_ptr = NULL;
ZVAL_UNDEF(&fcc.object);
result = zend_call_function(&fci, &fcc TSRMLS_CC);
@ -2886,7 +2886,7 @@ ZEND_METHOD(reflection_method, invoke)
fcc.function_handler = mptr;
fcc.calling_scope = obj_ce;
fcc.called_scope = intern->ce;
fcc.object_ptr = &object;
ZVAL_COPY_VALUE(&fcc.object, &object);
result = zend_call_function(&fci, &fcc TSRMLS_CC);
@ -2995,7 +2995,7 @@ ZEND_METHOD(reflection_method, invokeArgs)
fcc.function_handler = mptr;
fcc.calling_scope = obj_ce;
fcc.called_scope = intern->ce;
fcc.object_ptr = object;
ZVAL_COPY_VALUE(&fcc.object, object);
/*
* Copy the zend_function when calling via handler (e.g. Closure::__invoke())
@ -4228,7 +4228,7 @@ ZEND_METHOD(reflection_class, newInstance)
fcc.function_handler = constructor;
fcc.calling_scope = EG(scope);
fcc.called_scope = Z_OBJCE_P(return_value);
fcc.object_ptr = return_value;
ZVAL_COPY_VALUE(&fcc.object, return_value);
if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
if (params) {
@ -4333,7 +4333,7 @@ ZEND_METHOD(reflection_class, newInstanceArgs)
fcc.function_handler = constructor;
fcc.calling_scope = EG(scope);
fcc.called_scope = Z_OBJCE_P(return_value);
fcc.object_ptr = return_value;
ZVAL_COPY_VALUE(&fcc.object, return_value);
if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
if (params) {

View File

@ -496,9 +496,9 @@ PHP_FUNCTION(spl_autoload_register)
if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_STRICT, &func_name, &fcc, &error TSRMLS_CC)) {
alfi.ce = fcc.calling_scope;
alfi.func_ptr = fcc.function_handler;
obj_ptr = fcc.object_ptr;
obj_ptr = &fcc.object;
if (Z_TYPE_P(zcallable) == IS_ARRAY) {
if (!obj_ptr && alfi.func_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
if (Z_TYPE_P(obj_ptr) == IS_UNDEF && alfi.func_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
if (do_throw) {
zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Passed array specifies a non static method but no object (%s)", error);
}
@ -508,7 +508,7 @@ PHP_FUNCTION(spl_autoload_register)
STR_RELEASE(func_name);
RETURN_FALSE;
} else if (do_throw) {
zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Passed array does not specify %s %smethod (%s)", alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Passed array does not specify %s %smethod (%s)", alfi.func_ptr ? "a callable" : "an existing", Z_TYPE_P(obj_ptr) == IS_UNDEF ? "static " : "", error);
}
if (error) {
efree(error);
@ -547,7 +547,7 @@ PHP_FUNCTION(spl_autoload_register)
}
alfi.ce = fcc.calling_scope;
alfi.func_ptr = fcc.function_handler;
obj_ptr = fcc.object_ptr;
obj_ptr = &fcc.object;
if (error) {
efree(error);
}
@ -573,7 +573,7 @@ PHP_FUNCTION(spl_autoload_register)
goto skip;
}
if (obj_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
if (Z_TYPE_P(obj_ptr) == IS_OBJECT && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
/* add object id to the hash to ensure uniqueness, for more reference look at bug #40091 */
lc_name = STR_REALLOC(lc_name, lc_name->len + sizeof(zend_uint), 0);
memcpy(lc_name->val + lc_name->len - sizeof(zend_uint), &Z_OBJ_HANDLE_P(obj_ptr), sizeof(zend_uint));
@ -606,7 +606,7 @@ PHP_FUNCTION(spl_autoload_register)
}
if (zend_hash_add_mem(SPL_G(autoload_functions), lc_name, &alfi, sizeof(autoload_func_info)) == NULL) {
if (obj_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
if (Z_TYPE_P(obj_ptr) == IS_OBJECT && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
Z_DELREF(alfi.obj);
}
if (!ZVAL_IS_UNDEF(&alfi.closure)) {
@ -657,7 +657,7 @@ PHP_FUNCTION(spl_autoload_unregister)
}
RETURN_FALSE;
}
obj_ptr = fcc.object_ptr;
obj_ptr = &fcc.object;
if (error) {
efree(error);
}
@ -685,7 +685,7 @@ PHP_FUNCTION(spl_autoload_unregister)
} else {
/* remove specific */
success = zend_hash_del(SPL_G(autoload_functions), lc_name);
if (success != SUCCESS && obj_ptr) {
if (success != SUCCESS && Z_TYPE_P(obj_ptr) == IS_OBJECT) {
STR_REALLOC(lc_name, lc_name->len + sizeof(zend_uint), 0);
memcpy(lc_name->val + lc_name->len - sizeof(zend_uint), &Z_OBJ_HANDLE_P(obj_ptr), sizeof(zend_uint));
lc_name->val[lc_name->len] = '\0';

View File

@ -2102,7 +2102,7 @@ static int spl_filesystem_file_call(spl_filesystem_object *intern, zend_function
fcic.function_handler = func_ptr;
fcic.calling_scope = NULL;
fcic.called_scope = NULL;
fcic.object_ptr = NULL;
ZVAL_UNDEF(&fcic.object);
result = zend_call_function(&fci, &fcic TSRMLS_CC);

View File

@ -1504,9 +1504,8 @@ static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, z
return NULL;
}
Z_ADDREF(cfi->fci.function_name);
if (cfi->fcc.object_ptr) {
ZVAL_COPY(&cfi->object, cfi->fcc.object_ptr);
cfi->fcc.object_ptr = &cfi->object;
if (Z_TYPE(cfi->fcc.object) == IS_OBJECT) {
ZVAL_COPY(&cfi->object, &cfi->fcc.object);
}
intern->u.cbfilter = cfi;
break;

View File

@ -313,7 +313,7 @@ static void user_stream_create_object(struct php_user_stream_wrapper *uwrap, php
fcc.function_handler = uwrap->ce->constructor;
fcc.calling_scope = EG(scope);
fcc.called_scope = Z_OBJCE_P(object);
fcc.object_ptr = object;
ZVAL_COPY_VALUE(&fcc.object, object);
if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute %s::%s()", uwrap->ce->name->val, uwrap->ce->constructor->common.function_name->val);