Skip to content

Fixed Bug #61025 __invoke() visibility not honored #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Zend/tests/bug61025-1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Bug #61025 (__invoke() visibility not honored) invoke private
--FILE--
<?php

class FooPrivate {
private function __invoke() {
echo __CLASS__ . "::" . __FUNCTION__ . "()\n";
}
}
$foo_private = new FooPrivate();

$foo_private();
?>
===DONE===
--EXPECTF--
Fatal error: Call to private FooPrivate::__invoke() from context '' in %s/bug61025-1.php on line %d
67 changes: 67 additions & 0 deletions Zend/tests/bug61025-2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--TEST--
Bug #61025 (__invoke() visibility not honored) is_callable consist
--FILE--
<?php

class FooPublic {
public function __construct() {
echo "Check in class: " . __CLASS__ . " \$this is callable: ";
echo is_callable($this) ? 'true' : 'false';
echo "\n";
}
public function __invoke() {
echo __CLASS__ . "::" . __FUNCTION__ . "()\n";
}
}

class FooProtected {
public function __construct() {
echo "Check in class: " . __CLASS__ . " \$this is callable: ";
echo is_callable($this) ? 'true' : 'false';
echo "\n";
}

protected function __invoke() {
echo __CLASS__ . "::" . __FUNCTION__ . "()\n";
}
}

class FooPrivate {
public function __construct() {
echo "Check in class: " . __CLASS__ . " \$this is callable: ";
echo is_callable($this) ? 'true' : 'false';
echo "\n";
}

private function __invoke() {
echo __CLASS__ . "::" . __FUNCTION__ . "()\n";
}
}

$foo_public = new FooPublic();
echo "Check outter is callable: ";
echo is_callable($foo_public) ? 'true' : 'false';
echo "\n\n";

$foo_protected = new FooProtected();
echo "Check outter is callable: ";
echo is_callable($foo_protected) ? 'true' : 'false';
echo "\n\n";

$foo_private = new FooPrivate();
echo "Check outter is callable: ";
echo is_callable($foo_private) ? 'true' : 'false';
echo "\n\n";
?>
===DONE===
--EXPECT--
Check in class: FooPublic $this is callable: true
Check outter is callable: true

Check in class: FooProtected $this is callable: true
Check outter is callable: false

Check in class: FooPrivate $this is callable: true
Check outter is callable: false

===DONE===
21 changes: 21 additions & 0 deletions Zend/tests/bug61025-3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Bug #61025 (__invoke() visibility not honored) raise warning when using static
--FILE--
<?php

interface IInvoke {
public static function __invoke();
}

class Foo {
public static function __invoke() {
echo __CLASS__;
}
}
?>
===DONE===
--EXPECTF--
Warning: The magic method __invoke() cannot be static in %s/bug61025-3.php on line %d

Warning: The magic method __invoke() cannot be static in %s/bug61025-3.php on line %d
===DONE===
34 changes: 34 additions & 0 deletions Zend/tests/bug61025.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Bug #61025 (__invoke() visibility not honored)
--FILE--
<?php

class FooPublic {
public function __invoke() {
echo __CLASS__ . "::" . __FUNCTION__ . "()\n";
}
}

class FooProtected {
protected function __invoke() {
echo __CLASS__ . "::" . __FUNCTION__ . "()\n";
}
}

$closure = function () {
echo "Closure\n";
};

$foo_public = new FooPublic();
$foo_protected = new FooProtected();

$closure();
$foo_public();
$foo_protected();
?>
===DONE===
--EXPECTF--
Closure
FooPublic::__invoke()

Fatal error: Call to protected FooProtected::__invoke() from context '' in %s/bug61025.php on line %d
11 changes: 11 additions & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -3100,6 +3100,17 @@ ZEND_API zend_bool zend_is_callable_ex(zval *callable, zval *object_ptr, uint ch
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) {
fcc->called_scope = fcc->calling_scope;
if (!(fcc->function_handler->common.fn_flags & (ZEND_ACC_PUBLIC | ZEND_ACC_CLOSURE))) {
if (fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE) {
if (UNEXPECTED(fcc->called_scope != EG(scope))) {
return 0;
}
} else if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED)) {
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fcc->function_handler), EG(scope)))) {
return 0;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t we miss a return 0 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I only see four conditions, they are closure function and other three possible ZEND_ACC_PPP* flags.
or else it's common function, but that wouldn't happen when get the function with get_closure

}
if (callable_name) {
zend_class_entry *ce = Z_OBJCE_P(callable); /* TBFixed: what if it's overloaded? */

Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "zend_objects.h"
#include "zend_objects_API.h"
#include "zend_globals.h"
#include "zend_compile.h"

#define ZEND_CLOSURE_PRINT_NAME "Closure object"

Expand Down Expand Up @@ -450,6 +451,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent

closure->func = *func;
closure->func.common.prototype = NULL;
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;

if ((scope == NULL) && (this_ptr != NULL)) {
/* use dummy scope if we're binding an object without specifying a scope */
Expand Down
2 changes: 0 additions & 2 deletions Zend/zend_closures.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

BEGIN_EXTERN_C()

#define ZEND_INVOKE_FUNC_NAME "__invoke"

void zend_register_closure_ce(TSRMLS_D);

extern ZEND_API zend_class_entry *zend_ce_closure;
Expand Down
9 changes: 9 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "tsrm_virtual_cwd.h"
#include "zend_multibyte.h"
#include "zend_language_scanner.h"
#include "zend_closures.h"

#define CONSTANT_EX(op_array, op) \
(op_array)->literals[op].constant
Expand Down Expand Up @@ -1621,6 +1622,10 @@ void zend_do_begin_function_declaration(znode *function_token, znode *function_n
if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
zend_error(E_WARNING, "The magic method __toString() must have public visibility and cannot be static");
}
} else if ((name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1))) {
if (fn_flags & ZEND_ACC_STATIC) {
zend_error(E_WARNING, "The magic method __invoke() cannot be static");
}
}
} else {
char *class_lcname;
Expand Down Expand Up @@ -1677,6 +1682,10 @@ void zend_do_begin_function_declaration(znode *function_token, znode *function_n
zend_error(E_WARNING, "The magic method __toString() must have public visibility and cannot be static");
}
CG(active_class_entry)->__tostring = (zend_function *) CG(active_op_array);
} else if ((name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1))) {
if (fn_flags & ZEND_ACC_STATIC) {
zend_error(E_WARNING, "The magic method __invoke() cannot be static");
}
} else if (!(fn_flags & ZEND_ACC_STATIC)) {
CG(active_op_array)->fn_flags |= ZEND_ACC_ALLOW_STATIC;
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ END_EXTERN_C()
#define ZEND_CALLSTATIC_FUNC_NAME "__callstatic"
#define ZEND_TOSTRING_FUNC_NAME "__tostring"
#define ZEND_AUTOLOAD_FUNC_NAME "__autoload"
#define ZEND_INVOKE_FUNC_NAME "__invoke"

/* The following constants may be combined in CG(compiler_options)
* to change the default compiler behavior */
Expand Down
11 changes: 11 additions & 0 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,17 @@ ZEND_VM_HANDLER(59, ZEND_INIT_FCALL_BY_NAME, ANY, CONST|TMP|VAR|CV)
EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT) &&
Z_OBJ_HANDLER_P(function_name, get_closure) &&
Z_OBJ_HANDLER_P(function_name, get_closure)(function_name, &call->called_scope, &call->fbc, &call->object TSRMLS_CC) == SUCCESS) {
if (!(call->fbc->common.fn_flags & (ZEND_ACC_PUBLIC | ZEND_ACC_CLOSURE))) {
if (call->fbc->common.fn_flags & ZEND_ACC_PRIVATE) {
if (UNEXPECTED(call->called_scope != EG(scope))) {
zend_error_noreturn(E_ERROR, "Call to private %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
} else if ((call->fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(call->fbc), EG(scope)))) {
zend_error_noreturn(E_ERROR, "Call to protected %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
}
}
if (call->object) {
Z_ADDREF_P(call->object);
}
Expand Down
44 changes: 44 additions & 0 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,17 @@ static int ZEND_FASTCALL ZEND_INIT_FCALL_BY_NAME_SPEC_CONST_HANDLER(ZEND_OPCODE
EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT) &&
Z_OBJ_HANDLER_P(function_name, get_closure) &&
Z_OBJ_HANDLER_P(function_name, get_closure)(function_name, &call->called_scope, &call->fbc, &call->object TSRMLS_CC) == SUCCESS) {
if (!(call->fbc->common.fn_flags & (ZEND_ACC_PUBLIC | ZEND_ACC_CLOSURE))) {
if (call->fbc->common.fn_flags & ZEND_ACC_PRIVATE) {
if (UNEXPECTED(call->called_scope != EG(scope))) {
zend_error_noreturn(E_ERROR, "Call to private %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
} else if ((call->fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(call->fbc), EG(scope)))) {
zend_error_noreturn(E_ERROR, "Call to protected %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
}
}
if (call->object) {
Z_ADDREF_P(call->object);
}
Expand Down Expand Up @@ -1583,6 +1594,17 @@ static int ZEND_FASTCALL ZEND_INIT_FCALL_BY_NAME_SPEC_TMP_HANDLER(ZEND_OPCODE_H
EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT) &&
Z_OBJ_HANDLER_P(function_name, get_closure) &&
Z_OBJ_HANDLER_P(function_name, get_closure)(function_name, &call->called_scope, &call->fbc, &call->object TSRMLS_CC) == SUCCESS) {
if (!(call->fbc->common.fn_flags & (ZEND_ACC_PUBLIC | ZEND_ACC_CLOSURE))) {
if (call->fbc->common.fn_flags & ZEND_ACC_PRIVATE) {
if (UNEXPECTED(call->called_scope != EG(scope))) {
zend_error_noreturn(E_ERROR, "Call to private %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
} else if ((call->fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(call->fbc), EG(scope)))) {
zend_error_noreturn(E_ERROR, "Call to protected %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
}
}
if (call->object) {
Z_ADDREF_P(call->object);
}
Expand Down Expand Up @@ -1770,6 +1792,17 @@ static int ZEND_FASTCALL ZEND_INIT_FCALL_BY_NAME_SPEC_VAR_HANDLER(ZEND_OPCODE_H
EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT) &&
Z_OBJ_HANDLER_P(function_name, get_closure) &&
Z_OBJ_HANDLER_P(function_name, get_closure)(function_name, &call->called_scope, &call->fbc, &call->object TSRMLS_CC) == SUCCESS) {
if (!(call->fbc->common.fn_flags & (ZEND_ACC_PUBLIC | ZEND_ACC_CLOSURE))) {
if (call->fbc->common.fn_flags & ZEND_ACC_PRIVATE) {
if (UNEXPECTED(call->called_scope != EG(scope))) {
zend_error_noreturn(E_ERROR, "Call to private %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
} else if ((call->fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(call->fbc), EG(scope)))) {
zend_error_noreturn(E_ERROR, "Call to protected %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
}
}
if (call->object) {
Z_ADDREF_P(call->object);
}
Expand Down Expand Up @@ -1995,6 +2028,17 @@ static int ZEND_FASTCALL ZEND_INIT_FCALL_BY_NAME_SPEC_CV_HANDLER(ZEND_OPCODE_HA
EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT) &&
Z_OBJ_HANDLER_P(function_name, get_closure) &&
Z_OBJ_HANDLER_P(function_name, get_closure)(function_name, &call->called_scope, &call->fbc, &call->object TSRMLS_CC) == SUCCESS) {
if (!(call->fbc->common.fn_flags & (ZEND_ACC_PUBLIC | ZEND_ACC_CLOSURE))) {
if (call->fbc->common.fn_flags & ZEND_ACC_PRIVATE) {
if (UNEXPECTED(call->called_scope != EG(scope))) {
zend_error_noreturn(E_ERROR, "Call to private %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
} else if ((call->fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(call->fbc), EG(scope)))) {
zend_error_noreturn(E_ERROR, "Call to protected %s::__invoke() from context '%s'", Z_OBJCE_P(function_name)->name, EG(scope) ? EG(scope)->name : "");
}
}
}
if (call->object) {
Z_ADDREF_P(call->object);
}
Expand Down