From e60836742b2f95419eb3a1c1e7821563763b115a Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Sat, 2 May 2026 18:58:57 -0700 Subject: [PATCH] [RFC] Friends --- Zend/tests/friends/anonymous_class.phpt | 106 ++++++++++++++++ Zend/tests/friends/ast_printing.phpt | 23 ++++ Zend/tests/friends/clone_access.phpt | 80 ++++++++++++ .../compiler_errors/interface_error.phpt | 12 ++ .../compiler_errors/repetition_basic.phpt | 13 ++ .../repetition_case_insensitive.phpt | 13 ++ .../repetition_name_resolution.phpt | 16 +++ .../compiler_errors/reserved_parent.phpt | 12 ++ .../compiler_errors/reserved_self.phpt | 12 ++ .../compiler_errors/reserved_static.phpt | 12 ++ .../friends/compiler_errors/trait_error.phpt | 12 ++ Zend/tests/friends/constant_access.phpt | 62 +++++++++ .../friends/constant_access_inherited.phpt | 40 ++++++ Zend/tests/friends/constructor_access.phpt | 75 +++++++++++ Zend/tests/friends/destructor_access.phpt | 75 +++++++++++ Zend/tests/friends/enum_constant_access.phpt | 65 ++++++++++ Zend/tests/friends/enum_methods_access.phpt | 119 ++++++++++++++++++ Zend/tests/friends/example_001.phpt | 59 +++++++++ Zend/tests/friends/example_002.phpt | 62 +++++++++ Zend/tests/friends/example_003.phpt | 94 ++++++++++++++ Zend/tests/friends/method_access.phpt | 117 +++++++++++++++++ Zend/tests/friends/not_inherited.phpt | 51 ++++++++ Zend/tests/friends/not_reciprocal.phpt | 48 +++++++ Zend/tests/friends/not_reserved.phpt | 54 ++++++++ Zend/tests/friends/not_transitive.phpt | 59 +++++++++ Zend/tests/friends/property_access.phpt | 97 ++++++++++++++ Zend/tests/friends/property_access_aviz.phpt | 109 ++++++++++++++++ .../friends/property_access_inherited.phpt | 74 +++++++++++ Zend/zend.h | 3 + Zend/zend_ast.c | 6 + Zend/zend_ast.h | 1 + Zend/zend_compile.c | 57 ++++++++- Zend/zend_compile.h | 5 +- Zend/zend_language_parser.y | 2 + Zend/zend_language_scanner.l | 13 ++ Zend/zend_object_handlers.c | 52 +++++++- Zend/zend_object_handlers.h | 2 + Zend/zend_opcode.c | 9 ++ ext/opcache/zend_file_cache.c | 20 +++ ext/opcache/zend_persist.c | 8 ++ ext/opcache/zend_persist_calc.c | 8 ++ ext/reflection/php_reflection.c | 31 ++++- ext/reflection/php_reflection.stub.php | 2 + ext/reflection/php_reflection_arginfo.h | 6 +- ext/reflection/php_reflection_decl.h | 8 +- .../tests/ReflectionClass_getFriendNames.phpt | 34 +++++ .../tests/ReflectionClass_toString_001.phpt | 9 +- .../tests/ReflectionClass_toString_010.phpt | 82 ++++++++++++ .../ReflectionProperty_isReadable_friend.phpt | 24 ++++ ...tionProperty_isReadable_invalid_scope.phpt | 9 ++ .../ReflectionProperty_isWritable_friend.phpt | 24 ++++ ...tionProperty_isWritable_invalid_scope.phpt | 9 ++ ext/tokenizer/tokenizer_data.c | 1 + ext/tokenizer/tokenizer_data.stub.php | 5 + ext/tokenizer/tokenizer_data_arginfo.h | 3 +- 55 files changed, 1988 insertions(+), 16 deletions(-) create mode 100644 Zend/tests/friends/anonymous_class.phpt create mode 100644 Zend/tests/friends/ast_printing.phpt create mode 100644 Zend/tests/friends/clone_access.phpt create mode 100644 Zend/tests/friends/compiler_errors/interface_error.phpt create mode 100644 Zend/tests/friends/compiler_errors/repetition_basic.phpt create mode 100644 Zend/tests/friends/compiler_errors/repetition_case_insensitive.phpt create mode 100644 Zend/tests/friends/compiler_errors/repetition_name_resolution.phpt create mode 100644 Zend/tests/friends/compiler_errors/reserved_parent.phpt create mode 100644 Zend/tests/friends/compiler_errors/reserved_self.phpt create mode 100644 Zend/tests/friends/compiler_errors/reserved_static.phpt create mode 100644 Zend/tests/friends/compiler_errors/trait_error.phpt create mode 100644 Zend/tests/friends/constant_access.phpt create mode 100644 Zend/tests/friends/constant_access_inherited.phpt create mode 100644 Zend/tests/friends/constructor_access.phpt create mode 100644 Zend/tests/friends/destructor_access.phpt create mode 100644 Zend/tests/friends/enum_constant_access.phpt create mode 100644 Zend/tests/friends/enum_methods_access.phpt create mode 100644 Zend/tests/friends/example_001.phpt create mode 100644 Zend/tests/friends/example_002.phpt create mode 100644 Zend/tests/friends/example_003.phpt create mode 100644 Zend/tests/friends/method_access.phpt create mode 100644 Zend/tests/friends/not_inherited.phpt create mode 100644 Zend/tests/friends/not_reciprocal.phpt create mode 100644 Zend/tests/friends/not_reserved.phpt create mode 100644 Zend/tests/friends/not_transitive.phpt create mode 100644 Zend/tests/friends/property_access.phpt create mode 100644 Zend/tests/friends/property_access_aviz.phpt create mode 100644 Zend/tests/friends/property_access_inherited.phpt create mode 100644 ext/reflection/tests/ReflectionClass_getFriendNames.phpt create mode 100644 ext/reflection/tests/ReflectionClass_toString_010.phpt create mode 100644 ext/reflection/tests/ReflectionProperty_isReadable_friend.phpt create mode 100644 ext/reflection/tests/ReflectionProperty_isWritable_friend.phpt diff --git a/Zend/tests/friends/anonymous_class.phpt b/Zend/tests/friends/anonymous_class.phpt new file mode 100644 index 000000000000..92bde59f8b4c --- /dev/null +++ b/Zend/tests/friends/anonymous_class.phpt @@ -0,0 +1,106 @@ +--TEST-- +Friends: anonymous classes can have friends +--FILE-- +protectedInstance(); + echo "\n"; + try { + $obj->privateInstance(); + } catch (Error $e) { + echo $e . "\n\n"; + } + $obj->protectedProp = 1; + try { + $obj->privateProp = 2; + } catch (Error $e) { + echo $e . "\n\n"; + } + var_dump($obj); + var_dump($obj::FIRST); + try { + var_dump($obj::SECOND); + $obj::privateStatic(); + } catch (Error $e) { + echo $e . "\n"; + } + } +} + +$obj = new class () { + friend Bar; + + protected $protectedProp; + private $privateProp; + + protected const FIRST = 1; + private const SECOND = 2; + + protected static function protectedStatic() { + echo __METHOD__ . "() called, backtrace:\n"; + debug_print_backtrace(); + } + + private static function privateStatic() { + echo __METHOD__ . "() called, backtrace:\n"; + debug_print_backtrace(); + } + + protected function protectedInstance() { + echo __METHOD__ . "() called, backtrace:\n"; + debug_print_backtrace(); + } + + private function privateInstance() { + echo __METHOD__ . "() called, backtrace:\n"; + debug_print_backtrace(); + } +}; + +Bar::testAccess($obj); + +?> +--EXPECTF-- +class@anonymous%s:%d$0::protectedStatic() called, backtrace: +#0 %s(%d): class@anonymous::protectedStatic() +#1 %s(%d): Bar::testAccess(Object(class@anonymous)) + +Error: Call to private method class@anonymous%0%s::privateStatic() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testAccess(Object(class@anonymous)) +#1 {main} + +class@anonymous%s:%d$0::protectedInstance() called, backtrace: +#0 %s(%d): class@anonymous->protectedInstance() +#1 %s(%d): Bar::testAccess(Object(class@anonymous)) + +Error: Call to private method class@anonymous%0%s::privateInstance() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testAccess(Object(class@anonymous)) +#1 {main} + +Error: Cannot access private property class@anonymous%0%s::$privateProp in %s:%d +Stack trace: +#0 %s(%d): Bar::testAccess(Object(class@anonymous)) +#1 {main} + +object(class@anonymous%0%s)#1 (2) { + ["protectedProp":protected]=> + int(1) + ["privateProp":"class@anonymous":private]=> + NULL +} +int(1) +Error: Cannot access private constant class@anonymous%0%s::SECOND in %s:%d +Stack trace: +#0 %s(%d): Bar::testAccess(Object(class@anonymous)) +#1 {main} diff --git a/Zend/tests/friends/ast_printing.phpt b/Zend/tests/friends/ast_printing.phpt new file mode 100644 index 000000000000..9aa8e0409baa --- /dev/null +++ b/Zend/tests/friends/ast_printing.phpt @@ -0,0 +1,23 @@ +--TEST-- +Friends: AST printing +--FILE-- +getMessage(); +} + +?> +--EXPECT-- +assert(function () { + class Foo { + friend Bar; + } + +} && false) diff --git a/Zend/tests/friends/clone_access.phpt b/Zend/tests/friends/clone_access.phpt new file mode 100644 index 000000000000..79099930ce9e --- /dev/null +++ b/Zend/tests/friends/clone_access.phpt @@ -0,0 +1,80 @@ +--TEST-- +Friends: allows access to private __clone +--FILE-- + +--EXPECTF-- +Error: Call to protected method ProtectedClone::__clone() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to private method PrivateClone::__clone() from global scope in %s:%d +Stack trace: +#0 {main} + +----- + +ProtectedClone::__clone() called, backtrace: +#0 %s(%d): ProtectedClone->__clone() +#1 %s(%d): Bar::testCloneAccess() + +Error: Call to private method PrivateClone::__clone() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testCloneAccess() +#1 {main} diff --git a/Zend/tests/friends/compiler_errors/interface_error.phpt b/Zend/tests/friends/compiler_errors/interface_error.phpt new file mode 100644 index 000000000000..47a020efe76e --- /dev/null +++ b/Zend/tests/friends/compiler_errors/interface_error.phpt @@ -0,0 +1,12 @@ +--TEST-- +Friends: cannot be used on interfaces +--FILE-- + +--EXPECTF-- +Fatal error: Cannot add friends to interfaces. Bar is used in Foo in %s on line %d diff --git a/Zend/tests/friends/compiler_errors/repetition_basic.phpt b/Zend/tests/friends/compiler_errors/repetition_basic.phpt new file mode 100644 index 000000000000..8e817c3d6bd7 --- /dev/null +++ b/Zend/tests/friends/compiler_errors/repetition_basic.phpt @@ -0,0 +1,13 @@ +--TEST-- +Friends: same friend cannot be used multiple times +--FILE-- + +--EXPECTF-- +Fatal error: Cannot add Bar as a friend of Foo multiple times. in %s on line %d diff --git a/Zend/tests/friends/compiler_errors/repetition_case_insensitive.phpt b/Zend/tests/friends/compiler_errors/repetition_case_insensitive.phpt new file mode 100644 index 000000000000..8a58a903a6d9 --- /dev/null +++ b/Zend/tests/friends/compiler_errors/repetition_case_insensitive.phpt @@ -0,0 +1,13 @@ +--TEST-- +Friends: same friend cannot be used multiple times (case insensitive) +--FILE-- + +--EXPECTF-- +Fatal error: Cannot add BAR as a friend of Foo multiple times. in %s on line %d diff --git a/Zend/tests/friends/compiler_errors/repetition_name_resolution.phpt b/Zend/tests/friends/compiler_errors/repetition_name_resolution.phpt new file mode 100644 index 000000000000..3421367d6419 --- /dev/null +++ b/Zend/tests/friends/compiler_errors/repetition_name_resolution.phpt @@ -0,0 +1,16 @@ +--TEST-- +Friends: same friend cannot be used multiple times (after name resolution) +--FILE-- + +--EXPECTF-- +Fatal error: Cannot add SomeNamespace\Bar as a friend of Foo multiple times. in %s on line %d diff --git a/Zend/tests/friends/compiler_errors/reserved_parent.phpt b/Zend/tests/friends/compiler_errors/reserved_parent.phpt new file mode 100644 index 000000000000..a4e25f1fc8ab --- /dev/null +++ b/Zend/tests/friends/compiler_errors/reserved_parent.phpt @@ -0,0 +1,12 @@ +--TEST-- +Friends: 'parent' cannot be used as a friend name +--FILE-- + +--EXPECTF-- +Fatal error: Cannot use "parent" as friend name, as it is reserved in %s on line %d diff --git a/Zend/tests/friends/compiler_errors/reserved_self.phpt b/Zend/tests/friends/compiler_errors/reserved_self.phpt new file mode 100644 index 000000000000..524fdee358ee --- /dev/null +++ b/Zend/tests/friends/compiler_errors/reserved_self.phpt @@ -0,0 +1,12 @@ +--TEST-- +Friends: 'self' cannot be used as a friend name +--FILE-- + +--EXPECTF-- +Fatal error: Cannot use "self" as friend name, as it is reserved in %s on line %d diff --git a/Zend/tests/friends/compiler_errors/reserved_static.phpt b/Zend/tests/friends/compiler_errors/reserved_static.phpt new file mode 100644 index 000000000000..692845d036c3 --- /dev/null +++ b/Zend/tests/friends/compiler_errors/reserved_static.phpt @@ -0,0 +1,12 @@ +--TEST-- +Friends: 'static' cannot be used as a friend name +--FILE-- + +--EXPECTF-- +Fatal error: Cannot use "static" as friend name, as it is reserved in %s on line %d diff --git a/Zend/tests/friends/compiler_errors/trait_error.phpt b/Zend/tests/friends/compiler_errors/trait_error.phpt new file mode 100644 index 000000000000..85606ef7ea7d --- /dev/null +++ b/Zend/tests/friends/compiler_errors/trait_error.phpt @@ -0,0 +1,12 @@ +--TEST-- +Friends: cannot be used on traits +--FILE-- + +--EXPECTF-- +Fatal error: Cannot add friends to traits. Bar is used in Foo in %s on line %d diff --git a/Zend/tests/friends/constant_access.phpt b/Zend/tests/friends/constant_access.phpt new file mode 100644 index 000000000000..44cb11c5e3d9 --- /dev/null +++ b/Zend/tests/friends/constant_access.phpt @@ -0,0 +1,62 @@ +--TEST-- +Friends: allows access to protected constants +--FILE-- + +--EXPECTF-- +Error: Cannot access protected constant Foo::FIRST in %s:%d +Stack trace: +#0 {main} + +Error: Cannot access private constant Foo::SECOND in %s:%d +Stack trace: +#0 {main} + + + +----- + +int(1) + +Error: Cannot access private constant Foo::SECOND in %s:%d +Stack trace: +#0 %s(%d): Bar::testConstantAccess() +#1 {main} diff --git a/Zend/tests/friends/constant_access_inherited.phpt b/Zend/tests/friends/constant_access_inherited.phpt new file mode 100644 index 000000000000..a4ee5b8a9afc --- /dev/null +++ b/Zend/tests/friends/constant_access_inherited.phpt @@ -0,0 +1,40 @@ +--TEST-- +Friends: allows access to inherited non-overridden constants +--FILE-- + +--EXPECTF-- +int(1) + +Error: Cannot access protected constant FooChild::SECOND in %s:%d +Stack trace: +#0 %s(%d): Bar::testConstantAccess() +#1 {main} diff --git a/Zend/tests/friends/constructor_access.phpt b/Zend/tests/friends/constructor_access.phpt new file mode 100644 index 000000000000..905b1fff7061 --- /dev/null +++ b/Zend/tests/friends/constructor_access.phpt @@ -0,0 +1,75 @@ +--TEST-- +Friends: allows access to constructor +--FILE-- + +--EXPECTF-- +Error: Call to protected ConstructorProtected::__construct() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to private ConstructorPrivate::__construct() from global scope in %s:%d +Stack trace: +#0 {main} + + + +----- + +ConstructorProtected::__construct() called, backtrace: +#0 %s(%d): ConstructorProtected->__construct() +#1 %s(%d): Bar::testConstructorAccess() + +Error: Call to private ConstructorPrivate::__construct() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testConstructorAccess() +#1 {main} diff --git a/Zend/tests/friends/destructor_access.phpt b/Zend/tests/friends/destructor_access.phpt new file mode 100644 index 000000000000..d920c25fc9a8 --- /dev/null +++ b/Zend/tests/friends/destructor_access.phpt @@ -0,0 +1,75 @@ +--TEST-- +Friends: allows access to destructors +--FILE-- + +--EXPECTF-- +Error: Call to protected ProtectedDestructor::__destruct() from global scope in %s:%d +Stack trace: +#0 {main} +Error: Call to private PrivateDestructor::__destruct() from global scope in %s:%d +Stack trace: +#0 {main} + + +----- + +ProtectedDestructor::__destruct() called, backtrace: +#0 %s(%d): ProtectedDestructor->__destruct() +#1 %s(%d): Bar::testDestructorAccess() + +Error: Call to private PrivateDestructor::__destruct() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testDestructorAccess() +#1 {main} diff --git a/Zend/tests/friends/enum_constant_access.phpt b/Zend/tests/friends/enum_constant_access.phpt new file mode 100644 index 000000000000..f000a4301a36 --- /dev/null +++ b/Zend/tests/friends/enum_constant_access.phpt @@ -0,0 +1,65 @@ +--TEST-- +Friends: allows access to protected constants on enums +--FILE-- + +--EXPECTF-- +Error: Cannot access protected constant Foo::FIRST in %s:%d +Stack trace: +#0 {main} + +Error: Cannot access private constant Foo::SECOND in %s:%d +Stack trace: +#0 {main} + + + +----- + +int(1) + +Error: Cannot access private constant Foo::SECOND in %s:%d +Stack trace: +#0 %s(%d): Bar::testConstantAccess() +#1 {main} diff --git a/Zend/tests/friends/enum_methods_access.phpt b/Zend/tests/friends/enum_methods_access.phpt new file mode 100644 index 000000000000..a88e143d4287 --- /dev/null +++ b/Zend/tests/friends/enum_methods_access.phpt @@ -0,0 +1,119 @@ +--TEST-- +Friends: allows access to protected methods on enums +--FILE-- +protectedInstance(); + echo "\n"; + try { + $f->privateInstance(); + } catch (Error $e) { + echo $e . "\n"; + } + } +} + +// Confirm that the presence of a friend does not negate normal visibility +// enforcement for non friends +try { + Foo::protectedStatic(); +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + Foo::privateStatic(); +} catch (Error $e) { + echo $e . "\n\n"; +} +$f = Foo::First; +try { + $f->protectedInstance(); +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + $f->privateInstance(); +} catch (Error $e) { + echo $e . "\n\n"; +} + +echo "\n\n-----\n\n"; + +// But friend works +Bar::testMethodAccess(); + +?> +--EXPECTF-- +Error: Call to protected method Foo::protectedStatic() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to private method Foo::privateStatic() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to protected method Foo::protectedInstance() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to private method Foo::privateInstance() from global scope in %s:%d +Stack trace: +#0 {main} + + + +----- + +Foo::protectedStatic() called, backtrace: +#0 %s(%d): Foo::protectedStatic() +#1 %s(%d): Bar::testMethodAccess() + +Error: Call to private method Foo::privateStatic() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testMethodAccess() +#1 {main} +Foo::protectedInstance() called, backtrace: +#0 %s(%d): Foo->protectedInstance() +#1 %s(%d): Bar::testMethodAccess() + +Error: Call to private method Foo::privateInstance() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testMethodAccess() +#1 {main} diff --git a/Zend/tests/friends/example_001.phpt b/Zend/tests/friends/example_001.phpt new file mode 100644 index 000000000000..abbc92790083 --- /dev/null +++ b/Zend/tests/friends/example_001.phpt @@ -0,0 +1,59 @@ +--TEST-- +Friends: RFC example 1 (User and UserFactory) +--FILE-- + new User(1, "Alice"), + 2 => new User(2, "Bob"), + default => null, + }; + } +} + +$factory = new UserFactory; + +$alice = $factory->newFromId(1); +var_dump($alice); + +$bob = $factory->newFromId(2); +var_dump($bob); + +// Creation outside of the factory fails +try { + $unknown = new User(3, "Camille"); +} catch (Error $e) { + echo $e; +} + +?> +--EXPECTF-- +object(User)#%d (2) { + ["userId"]=> + int(1) + ["username"]=> + string(5) "Alice" +} +object(User)#%d (2) { + ["userId"]=> + int(2) + ["username"]=> + string(3) "Bob" +} +Error: Call to protected User::__construct() from global scope in %s:%d +Stack trace: +#0 {main} diff --git a/Zend/tests/friends/example_002.phpt b/Zend/tests/friends/example_002.phpt new file mode 100644 index 000000000000..975e3775f960 --- /dev/null +++ b/Zend/tests/friends/example_002.phpt @@ -0,0 +1,62 @@ +--TEST-- +Friends: RFC example 2 (User and UserBuilder) +--FILE-- +userId = $userId; + return $u; + } + + public function newWithName(string $username): User { + $u = new User(); + $u->username = $username; + return $u; + } +} + +$builder = new UserBuilder(); + +$alice = $builder->newWithId(1); +var_dump($alice); + +$bob = $builder->newWithName("Bob"); +var_dump($bob); + +// Manipulation outside of the builder fails +try { + $bob->userId = 2; +} catch (Error $e) { + echo $e; +} + +?> +--EXPECTF-- +object(User)#%d (2) { + ["userId"]=> + int(1) + ["username"]=> + NULL +} +object(User)#%d (2) { + ["userId"]=> + NULL + ["username"]=> + string(3) "Bob" +} +Error: Cannot modify protected(set) property User::$userId from global scope in %s:%d +Stack trace: +#0 {main} diff --git a/Zend/tests/friends/example_003.phpt b/Zend/tests/friends/example_003.phpt new file mode 100644 index 000000000000..8993bd004224 --- /dev/null +++ b/Zend/tests/friends/example_003.phpt @@ -0,0 +1,94 @@ +--TEST-- +Friends: RFC example 3 (UserBuilder and LoggedUserBuilder) +--FILE-- +userId = $userId; + return $u; + } + + public function newWithName(string $username): User { + $u = new User(); + $u->username = $username; + return $u; + } +} + +class LoggedUserBuilder extends UserBuilder { + + public function newWithId(int $userId): User { + echo "Creating user with id: $userId\n"; + return parent::newWithId($userId); + } + + public function newWithName(string $username): User { + echo "Creating user with name: $username\n"; + return parent::newWithName($username); + } +} + +$builder = new UserBuilder(); + +$alice = $builder->newWithId(1); +var_dump($alice); + +$bob = $builder->newWithName("Bob"); +var_dump($bob); + +echo "\n\n----------\n\n"; + +// LoggedUserBuilder doesn't actually access anything protected, that is all +// done in the parent:: calls, this works fine +$loggedBuilder = new LoggedUserBuilder(); +$charlie = $loggedBuilder->newWithId(3); +var_dump($charlie); + +$daniel = $loggedBuilder->newWithName("Daniel"); +var_dump($daniel); + +?> +--EXPECTF-- +object(User)#%d (2) { + ["userId"]=> + int(1) + ["username"]=> + NULL +} +object(User)#%d (2) { + ["userId"]=> + NULL + ["username"]=> + string(3) "Bob" +} + + +---------- + +Creating user with id: 3 +object(User)#%d (2) { + ["userId"]=> + int(3) + ["username"]=> + NULL +} +Creating user with name: Daniel +object(User)#%d (2) { + ["userId"]=> + NULL + ["username"]=> + string(6) "Daniel" +} diff --git a/Zend/tests/friends/method_access.phpt b/Zend/tests/friends/method_access.phpt new file mode 100644 index 000000000000..c890e18e18ee --- /dev/null +++ b/Zend/tests/friends/method_access.phpt @@ -0,0 +1,117 @@ +--TEST-- +Friends: allows access to protectd methods +--FILE-- +protectedInstance(); + echo "\n"; + try { + $f->privateInstance(); + } catch (Error $e) { + echo $e . "\n"; + } + } +} + +// Confirm that the presence of a friend does not negate normal visibility +// enforcement for non friends +try { + Foo::protectedStatic(); +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + Foo::privateStatic(); +} catch (Error $e) { + echo $e . "\n\n"; +} +$f = new Foo(); +try { + $f->protectedInstance(); +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + $f->privateInstance(); +} catch (Error $e) { + echo $e . "\n\n"; +} + +echo "\n\n-----\n\n"; + +// But friend works +Bar::testMethodAccess(); + +?> +--EXPECTF-- +Error: Call to protected method Foo::protectedStatic() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to private method Foo::privateStatic() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to protected method Foo::protectedInstance() from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Call to private method Foo::privateInstance() from global scope in %s:%d +Stack trace: +#0 {main} + + + +----- + +Foo::protectedStatic() called, backtrace: +#0 %s(%d): Foo::protectedStatic() +#1 %s(%d): Bar::testMethodAccess() + +Error: Call to private method Foo::privateStatic() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testMethodAccess() +#1 {main} + +Foo::protectedInstance() called, backtrace: +#0 %s(%d): Foo->protectedInstance() +#1 %s(%d): Bar::testMethodAccess() + +Error: Call to private method Foo::privateInstance() from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testMethodAccess() +#1 {main} diff --git a/Zend/tests/friends/not_inherited.phpt b/Zend/tests/friends/not_inherited.phpt new file mode 100644 index 000000000000..82b89c336164 --- /dev/null +++ b/Zend/tests/friends/not_inherited.phpt @@ -0,0 +1,51 @@ +--TEST-- +Friends: friendship is not inherited +--FILE-- + +--EXPECTF-- +Foo::protectedStatic() +Foo::protectedStatic() + + +----- + + +Fatal error: Uncaught Error: Call to protected method Foo::protectedStatic() from scope Baz in %s:%d +Stack trace: +#0 %s(%d): Baz::testFooAccessAgain() +#1 {main} + thrown in %s on line %d diff --git a/Zend/tests/friends/not_reciprocal.phpt b/Zend/tests/friends/not_reciprocal.phpt new file mode 100644 index 000000000000..8ed86f9c4ffb --- /dev/null +++ b/Zend/tests/friends/not_reciprocal.phpt @@ -0,0 +1,48 @@ +--TEST-- +Friends: friendship is not reciprocal +--FILE-- + +--EXPECTF-- +Foo::protectedStatic() + + +----- + + +Fatal error: Uncaught Error: Call to protected method Bar::protectedStatic() from scope Foo in %s:%d +Stack trace: +#0 %s(%d): Foo::testBarAccess() +#1 {main} + thrown in %s on line %d diff --git a/Zend/tests/friends/not_reserved.phpt b/Zend/tests/friends/not_reserved.phpt new file mode 100644 index 000000000000..5235eeddc13d --- /dev/null +++ b/Zend/tests/friends/not_reserved.phpt @@ -0,0 +1,54 @@ +--TEST-- +Friends: `friend` is not reserved +--FILE-- +Friend); + return $this; + } + } + + class Subclass extends Friend {} +} + +namespace InterfaceDemo { + interface Friend {} +} + +namespace TraitDemo { + trait Friend {} +} + +namespace EnumDemo { + enum Friend { + case Friend; + } +} + +namespace { + use ClazzDemo\Friend; + use ClazzDemo\Subclass; + + $f = new Friend; + $f->Friend = "demo"; + $f->Friend(); + + $f = new Subclass; + $f->Friend = "demo"; + $f->Friend(); + + var_dump(\EnumDemo\Friend::Friend); +} + +?> +--EXPECT-- +string(4) "demo" +string(4) "demo" +enum(EnumDemo\Friend::Friend) diff --git a/Zend/tests/friends/not_transitive.phpt b/Zend/tests/friends/not_transitive.phpt new file mode 100644 index 000000000000..23a4ce32d093 --- /dev/null +++ b/Zend/tests/friends/not_transitive.phpt @@ -0,0 +1,59 @@ +--TEST-- +Friends: friendship is not transitive +--FILE-- + +--EXPECTF-- +Foo::protectedStatic() +Bar::protectedStatic() + + +----- + + +Fatal error: Uncaught Error: Call to protected method Foo::protectedStatic() from scope Baz in %s:%d +Stack trace: +#0 %s(%d): Baz::testFooAccess() +#1 {main} + thrown in %s on line %d diff --git a/Zend/tests/friends/property_access.phpt b/Zend/tests/friends/property_access.phpt new file mode 100644 index 000000000000..5d1b10ae0108 --- /dev/null +++ b/Zend/tests/friends/property_access.phpt @@ -0,0 +1,97 @@ +--TEST-- +Friends: allows access to protected properties +--FILE-- +protectedInstance = 3; + try { + $f->privateInstance = 4; + } catch (Error $e) { + echo $e . "\n"; + } + var_dump($f); + } +} + +// Confirm that the presence of a friend does not negate normal visibility +// enforcement for non friends +try { + Foo::$protectedStatic = 1; +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + Foo::$privateStatic = 2; +} catch (Error $e) { + echo $e . "\n\n"; +} +$f = new Foo(); +try { + $f->protectedInstance = 3; +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + $f->privateInstance = 4; +} catch (Error $e) { + echo $e . "\n\n"; +} + +echo "\n\n-----\n\n"; + +// But friend works +Bar::testPropertyAccess(); + +?> +--EXPECTF-- +Error: Cannot access protected property Foo::$protectedStatic in %s:%d +Stack trace: +#0 {main} + +Error: Cannot access private property Foo::$privateStatic in %s:%d +Stack trace: +#0 {main} + +Error: Cannot access protected property Foo::$protectedInstance in %s:%d +Stack trace: +#0 {main} + +Error: Cannot access private property Foo::$privateInstance in %s:%d +Stack trace: +#0 {main} + + + +----- + + +Fatal error: Uncaught Error: Cannot access protected property Foo::$protectedStatic in %s:%d +Stack trace: +#0 %s(%d): Bar::testPropertyAccess() +#1 {main} + thrown in %s on line %d diff --git a/Zend/tests/friends/property_access_aviz.phpt b/Zend/tests/friends/property_access_aviz.phpt new file mode 100644 index 000000000000..a87118a2e232 --- /dev/null +++ b/Zend/tests/friends/property_access_aviz.phpt @@ -0,0 +1,109 @@ +--TEST-- +Friends: allows access to protected properties with asymmetric visibility +--FILE-- +protectedInstance = 3; + try { + $f->privateInstance = 4; + } catch (Error $e) { + echo $e . "\n\n"; + } + var_dump($f); + } +} + +// Confirm that the presence of a friend does not negate normal visibility +// enforcement for non friends +try { + Foo::$protectedStatic = 1; +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + Foo::$privateStatic = 2; +} catch (Error $e) { + echo $e . "\n\n"; +} +$f = new Foo(); +try { + $f->protectedInstance = 3; +} catch (Error $e) { + echo $e . "\n\n"; +} +try { + $f->privateInstance = 4; +} catch (Error $e) { + echo $e . "\n\n"; +} + +echo "\n\n-----\n\n"; + +// But friend works +Bar::testPropertyAccess(); + +?> +--EXPECTF-- +Error: Cannot modify protected(set) property Foo::$protectedStatic from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Cannot modify private(set) property Foo::$privateStatic from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Cannot modify protected(set) property Foo::$protectedInstance from global scope in %s:%d +Stack trace: +#0 {main} + +Error: Cannot modify private(set) property Foo::$privateInstance from global scope in %s:%d +Stack trace: +#0 {main} + + + +----- + +int(1) + +Error: Cannot modify private(set) property Foo::$privateStatic from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testPropertyAccess() +#1 {main} + +bool(true) + +Error: Cannot modify private(set) property Foo::$privateInstance from scope Bar in %s:%d +Stack trace: +#0 %s(%d): Bar::testPropertyAccess() +#1 {main} + +object(Foo)#%d (1) { + ["protectedInstance"]=> + int(3) + ["privateInstance"]=> + uninitialized(mixed) +} diff --git a/Zend/tests/friends/property_access_inherited.phpt b/Zend/tests/friends/property_access_inherited.phpt new file mode 100644 index 000000000000..2e783fdb1211 --- /dev/null +++ b/Zend/tests/friends/property_access_inherited.phpt @@ -0,0 +1,74 @@ +--TEST-- +Friends: allows access to inherited non-overridden protected instance properties +--FILE-- +protectedInstance = 1; + try { + $fc->privateInstance = 2; + } catch (Error $e) { + echo $e . "\n"; + } + + try { + $fc->protectedInstance2 = 3; + } catch (Error $e) { + echo $e . "\n\n"; + } + try { + $fc->privateInstance2 = 4; + } catch (Error $e) { + echo $e . "\n\n"; + } + + var_dump($fc); + } +} + +Bar::testPropertyAccess(); + +?> +--EXPECTF-- +Deprecated: Creation of dynamic property FooChild::$privateInstance is deprecated in %s on line %d +Error: Cannot access protected property FooChild::$protectedInstance2 in %s:%d +Stack trace: +#0 %s(%d): Bar::testPropertyAccess() +#1 {main} + +Error: Cannot access private property FooChild::$privateInstance2 in %s:%d +Stack trace: +#0 %s(%d): Bar::testPropertyAccess() +#1 {main} + +object(FooChild)#%d (6) { + ["protectedInstance":protected]=> + int(1) + ["privateInstance":"Foo":private]=> + NULL + ["protectedInstance2":protected]=> + NULL + ["privateInstance2":"Foo":private]=> + NULL + ["privateInstance2":"FooChild":private]=> + NULL + ["privateInstance"]=> + int(2) +} diff --git a/Zend/zend.h b/Zend/zend.h index 2bbf0283991d..4075d043bf8d 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -226,6 +226,9 @@ struct _zend_class_entry { uint32_t enum_backing_type; HashTable *backed_enum_table; + uint32_t num_friends; + zend_class_name *friend_names; + zend_string *doc_comment; union { diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index 9ef1cf3b6788..d702074a5725 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -2040,6 +2040,7 @@ static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int in case ZEND_AST_METHOD: case ZEND_AST_CLASS: case ZEND_AST_USE_TRAIT: + case ZEND_AST_FRIEND: case ZEND_AST_NAMESPACE: case ZEND_AST_DECLARE: break; @@ -2723,6 +2724,11 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio APPEND_NODE_1("break"); case ZEND_AST_CONTINUE: APPEND_NODE_1("continue"); + case ZEND_AST_FRIEND: + smart_str_appends(str, "friend "); + zend_ast_export_name(str, ast->child[0], 0, indent); + smart_str_appendc(str, ';'); + break; /* 2 child nodes */ case ZEND_AST_DIM: diff --git a/Zend/zend_ast.h b/Zend/zend_ast.h index 5d646f428c1b..e08dde91d714 100644 --- a/Zend/zend_ast.h +++ b/Zend/zend_ast.h @@ -111,6 +111,7 @@ enum _zend_ast_kind { ZEND_AST_BREAK, ZEND_AST_CONTINUE, ZEND_AST_PROPERTY_HOOK_SHORT_BODY, + ZEND_AST_FRIEND, /* 2 child nodes */ ZEND_AST_DIM = 2 << ZEND_AST_NUM_CHILDREN_SHIFT, diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 42395006c3fb..cd671650a94b 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -2081,6 +2081,8 @@ ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_hand ce->attributes = NULL; ce->enum_backing_type = IS_UNDEF; ce->backed_enum_table = NULL; + ce->num_friends = 0; + ce->friend_names = NULL; if (nullify_handlers) { ce->constructor = NULL; @@ -2791,7 +2793,8 @@ static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */ { return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP - || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD; + || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD + || ast->kind == ZEND_AST_FRIEND; } /* }}} */ @@ -9951,6 +9954,55 @@ static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */ } /* }}} */ +static void zend_compile_friend(const zend_ast *ast) +{ + zend_class_entry *ce = CG(active_class_entry); + // Not yet supported for internal classes + ZEND_ASSERT(ce->type == ZEND_USER_CLASS); + + if (ce->ce_flags & ZEND_ACC_INTERFACE) { + zend_string *friend_name = zend_ast_get_str(ast->child[0]); + zend_error_noreturn(E_COMPILE_ERROR, "Cannot add friends to interfaces. " + "%pS is used in %pS", friend_name, ce->name); + } + + if (ce->ce_flags & ZEND_ACC_TRAIT) { + zend_string *friend_name = zend_ast_get_str(ast->child[0]); + zend_error_noreturn(E_COMPILE_ERROR, "Cannot add friends to traits. " + "%pS is used in %pS", friend_name, ce->name); + } + + zend_string *resolved = zend_resolve_const_class_name_reference(ast->child[0], "friend name"); + zend_string *resolved_lc = zend_string_tolower(resolved); + + // Check if the friend is already declared + // Optimization: skip the check for the first friend being added + if (ce->num_friends) { + ZEND_ASSERT(ce->ce_flags & ZEND_ACC_HAS_FRIENDS); + for (uint32_t i = 0; i < ce->num_friends; ++i) { + // No need for case insensitive comparison here since names are + // stored in lower case already + if (zend_string_equals(ce->friend_names[i].lc_name, resolved_lc)) { + zend_error_noreturn( + E_COMPILE_ERROR, + "Cannot add %pS as a friend of %pS multiple times.", + resolved, + ce->name + ); + } + } + } else { + ZEND_ASSERT((ce->ce_flags & ZEND_ACC_HAS_FRIENDS) == 0); + ce->ce_flags |= ZEND_ACC_HAS_FRIENDS; + } + + ce->friend_names = erealloc(ce->friend_names, sizeof(zend_class_name) * (ce->num_friends + 1)); + + ce->friend_names[ce->num_friends].name = resolved; + ce->friend_names[ce->num_friends].lc_name = resolved_lc; + ce->num_friends++; +} + static void zend_compile_implements(zend_ast *ast) /* {{{ */ { const zend_ast_list *list = zend_ast_get_list(ast); @@ -12619,6 +12671,9 @@ static void zend_compile_stmt(zend_ast *ast) /* {{{ */ case ZEND_AST_USE_TRAIT: zend_compile_use_trait(ast); break; + case ZEND_AST_FRIEND: + zend_compile_friend(ast); + break; case ZEND_AST_CLASS: zend_compile_class_decl(NULL, ast, false); break; diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index ec6d8b09bbe7..33bd3debf65f 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -275,7 +275,7 @@ typedef struct _zend_oparray_context { #define ZEND_ACC_PROTECTED_SET (1 << 11) /* | | X | */ #define ZEND_ACC_PRIVATE_SET (1 << 12) /* | | X | */ /* | | | */ -/* Class Flags (unused: 31) | | | */ +/* Class Flags (unused: none, use ZEND_ACC2_*) | | | */ /* =========== | | | */ /* | | | */ /* Special class types | | | */ @@ -341,6 +341,9 @@ typedef struct _zend_oparray_context { /* Class cannot be serialized or unserialized | | | */ #define ZEND_ACC_NOT_SERIALIZABLE (1 << 29) /* X | | | */ /* | | | */ +/* Class has friends | | | */ +#define ZEND_ACC_HAS_FRIENDS (1U << 31) /* X | | | */ +/* | | | */ /* Class Flags 2 (ce_flags2) (unused: 0-31) | | | */ /* ========================= | | | */ /* | | | */ diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index 2d9d7b4a23d6..622b1060e1dc 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -158,6 +158,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*); %token T_PROTECTED_SET "'protected(set)'" %token T_PUBLIC_SET "'public(set)'" %token T_READONLY "'readonly'" +%token T_FRIEND "'friend'" %token T_VAR "'var'" %token T_UNSET "'unset'" %token T_ISSET "'isset'" @@ -1006,6 +1007,7 @@ class_statement: | attributes attributed_class_statement { $$ = zend_ast_with_attributes($2, $1); } | T_USE class_name_list trait_adaptations { $$ = zend_ast_create(ZEND_AST_USE_TRAIT, $2, $3); } + | T_FRIEND class_name ';' { $$ = zend_ast_create(ZEND_AST_FRIEND, $2); } ; class_name_list: diff --git a/Zend/zend_language_scanner.l b/Zend/zend_language_scanner.l index 60143bc800fe..0649eccd3f32 100644 --- a/Zend/zend_language_scanner.l +++ b/Zend/zend_language_scanner.l @@ -1832,6 +1832,19 @@ OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_ RETURN_TOKEN_WITH_IDENT(T_READONLY); } +/* + * The friend keyword must be followed by whitespace and another identifier. + * This avoids the BC break of using friend in classes, namespaces, functions and constants. + */ +"friend"{WHITESPACE_OR_COMMENTS}("extends"|"implements") { + yyless(6); + RETURN_TOKEN_WITH_STR(T_STRING, 0); +} +"friend"{WHITESPACE_OR_COMMENTS}[a-zA-Z_\x80-\xff] { + yyless(6); + RETURN_TOKEN_WITH_IDENT(T_FRIEND); +} + "unset" { RETURN_TOKEN_WITH_IDENT(T_UNSET); } diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index c88e2e061997..530a5d2bbb88 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -417,7 +417,10 @@ static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *c } } else { ZEND_ASSERT(flags & ZEND_ACC_PROTECTED); - if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) { + if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope)) + && !zend_check_friend(property_info->ce, scope) + ) { + // Friendship with the class that *defines* the property is required goto wrong; } } @@ -510,7 +513,10 @@ ZEND_API zend_property_info *zend_get_property_info(const zend_class_entry *ce, } } else { ZEND_ASSERT(flags & ZEND_ACC_PROTECTED); - if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) { + if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope)) + && !zend_check_friend(property_info->ce, scope) + ) { + // Friendship with the class that *defines* the property is required goto wrong; } } @@ -586,7 +592,11 @@ ZEND_API bool ZEND_FASTCALL zend_asymmetric_property_has_set_access(const zend_p return true; } return EXPECTED((prop_info->flags & ZEND_ACC_PROTECTED_SET) - && is_protected_compatible_scope(prop_info->prototype->ce, scope)); + && ( + is_protected_compatible_scope(prop_info->prototype->ce, scope) + || zend_check_friend(prop_info->ce, scope) + ) + ); } static void zend_property_guard_dtor(zval *el) /* {{{ */ { @@ -1781,19 +1791,49 @@ ZEND_API bool zend_check_protected(const zend_class_entry *ce, const zend_class_ fbc_scope = fbc_scope->parent; } + const zend_class_entry *call_scope = scope; + /* Is the function's scope the same as our current object context, * or any of the parents of our context? */ - while (scope) { - if (scope==ce) { + while (call_scope) { + if (call_scope==ce) { return 1; } - scope = scope->parent; + call_scope = call_scope->parent; + } + + if (zend_check_friend(ce, scope)) { + return 1; } return 0; } /* }}} */ +/** + * Check whether the `scope` class is a friend of the given `ce` class + */ +ZEND_API bool zend_check_friend(const zend_class_entry *ce, const zend_class_entry *scope) { + if ((ce->ce_flags & ZEND_ACC_HAS_FRIENDS) == 0) { + return false; + } + if (scope == NULL) { + // Null scope = global + return false; + } + zend_string *potential_friend = zend_string_tolower(scope->name); + for (uint32_t i = 0; i < ce->num_friends; ++i) { + // No need for case insensitive comparison here since names are + // stored in lower case already + if (zend_string_equals(ce->friend_names[i].lc_name, potential_friend)) { + zend_string_release(potential_friend); + return true; + } + } + zend_string_release(potential_friend); + return false; +} + ZEND_API ZEND_ATTRIBUTE_NONNULL zend_function *zend_get_call_trampoline_func( const zend_function *fbc, zend_string *method_name) /* {{{ */ { diff --git a/Zend/zend_object_handlers.h b/Zend/zend_object_handlers.h index d41ae01bf700..86310476d98a 100644 --- a/Zend/zend_object_handlers.h +++ b/Zend/zend_object_handlers.h @@ -310,6 +310,8 @@ ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2); ZEND_API bool zend_check_protected(const zend_class_entry *ce, const zend_class_entry *scope); +ZEND_API bool zend_check_friend(const zend_class_entry *ce, const zend_class_entry *scope); + ZEND_API zend_result zend_check_property_access(const zend_object *zobj, zend_string *prop_info_name, bool is_dynamic); ZEND_API ZEND_ATTRIBUTE_NONNULL zend_function *zend_get_call_trampoline_func(const zend_function *fbc, zend_string *method_name); diff --git a/Zend/zend_opcode.c b/Zend/zend_opcode.c index a25f236c3c20..646f8ba1997c 100644 --- a/Zend/zend_opcode.c +++ b/Zend/zend_opcode.c @@ -375,6 +375,14 @@ ZEND_API void destroy_zend_class(zval *zv) if (ce->num_traits > 0) { _destroy_zend_class_traits_info(ce); } + + if (ce->num_friends > 0) { + for (uint32_t i = 0; i < ce->num_friends; i++) { + zend_string_release_ex(ce->friend_names[i].name, 0); + zend_string_release_ex(ce->friend_names[i].lc_name, 0); + } + efree(ce->friend_names); + } } if (ce->default_properties_table) { @@ -542,6 +550,7 @@ ZEND_API void destroy_zend_class(zval *zv) } free(ce->trait_names); } + ZEND_ASSERT(ce->num_friends == 0); free(ce); break; } diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c index 1235b1fc89fa..593db47cae2e 100644 --- a/ext/opcache/zend_file_cache.c +++ b/ext/opcache/zend_file_cache.c @@ -925,6 +925,17 @@ static void zend_file_cache_serialize_class(zval *zv, } } + if (ce->num_friends) { + SERIALIZE_PTR(ce->friend_names); + zend_class_name *friend_names = ce->friend_names; + UNSERIALIZE_PTR(friend_names); + + for (uint32_t i = 0; i < ce->num_friends; i++) { + SERIALIZE_STR(friend_names[i].name); + SERIALIZE_STR(friend_names[i].lc_name); + } + } + SERIALIZE_PTR(ce->constructor); SERIALIZE_PTR(ce->destructor); SERIALIZE_PTR(ce->clone); @@ -1813,6 +1824,15 @@ static void zend_file_cache_unserialize_class(zval *zv, } } + if (ce->num_friends) { + UNSERIALIZE_PTR(ce->friend_names); + + for (uint32_t i = 0; i < ce->num_friends; i++) { + UNSERIALIZE_STR(ce->friend_names[i].name); + UNSERIALIZE_STR(ce->friend_names[i].lc_name); + } + } + UNSERIALIZE_PTR(ce->constructor); UNSERIALIZE_PTR(ce->destructor); UNSERIALIZE_PTR(ce->clone); diff --git a/ext/opcache/zend_persist.c b/ext/opcache/zend_persist.c index 84043ed0450a..17038754a20a 100644 --- a/ext/opcache/zend_persist.c +++ b/ext/opcache/zend_persist.c @@ -1192,6 +1192,14 @@ zend_class_entry *zend_persist_class_entry(zend_class_entry *orig_ce) } ZEND_ASSERT(ce->backed_enum_table == NULL); + + if (ce->num_friends) { + for (uint32_t i = 0; i < ce->num_friends; i++) { + zend_accel_store_interned_string(ce->friend_names[i].name); + zend_accel_store_interned_string(ce->friend_names[i].lc_name); + } + ce->friend_names = zend_shared_memdup_free(ce->friend_names, sizeof(zend_class_name) * ce->num_friends); + } } return ce; diff --git a/ext/opcache/zend_persist_calc.c b/ext/opcache/zend_persist_calc.c index 71398024e7c6..7517b5701b21 100644 --- a/ext/opcache/zend_persist_calc.c +++ b/ext/opcache/zend_persist_calc.c @@ -600,6 +600,14 @@ void zend_persist_class_entry_calc(zend_class_entry *ce) ADD_SIZE(sizeof(zend_trait_precedence*) * (i + 1)); } } + + if (ce->num_friends) { + for (uint32_t i = 0; i < ce->num_friends; i++) { + ADD_INTERNED_STRING(ce->friend_names[i].name); + ADD_INTERNED_STRING(ce->friend_names[i].lc_name); + } + ADD_SIZE(sizeof(zend_class_name) * ce->num_friends); + } } } diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 85229e868edc..2a4c13503893 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -421,6 +421,15 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zend_object *obj smart_str_free(&enum_case_str); smart_str_free(&constant_str); + if (ce->num_friends > 0) { + smart_str_appendc(str, '\n'); + smart_str_append_printf(str, "%s - Friends [%" PRIu32 "] {\n", indent, ce->num_friends); + for (uint32_t i = 0; i < ce->num_friends; i++) { + smart_str_append_printf(str, "%pS friend %pS\n", sub_indent, ce->friend_names[i].name); + } + smart_str_append_printf(str, "%s }\n", indent); + } + /* Static properties */ /* counting static properties */ uint32_t count = zend_hash_num_elements(&ce->properties_info); @@ -5421,6 +5430,26 @@ ZEND_METHOD(ReflectionClass, getTraitAliases) } /* }}} */ +// Returns an array of names of friends of this class +ZEND_METHOD(ReflectionClass, getFriendNames) +{ + reflection_object *intern; + zend_class_entry *ce; + + ZEND_PARSE_PARAMETERS_NONE(); + GET_REFLECTION_OBJECT_PTR(ce); + + if (!ce->num_friends) { + RETURN_EMPTY_ARRAY(); + } + + array_init(return_value); + + for (uint32_t i = 0; i < ce->num_friends; i++) { + add_next_index_str(return_value, zend_string_copy(ce->friend_names[i].name)); + } +} + /* {{{ Returns the class' parent class, or, if none exists, FALSE */ ZEND_METHOD(ReflectionClass, getParentClass) { @@ -6659,7 +6688,7 @@ static bool check_visibility(uint32_t visibility, const zend_class_entry *ce, co } ZEND_ASSERT(visibility & ZEND_ACC_PROTECTED); if (!instanceof_function(scope, ce) && !instanceof_function(ce, scope)) { - return false; + return zend_check_friend(ce, scope); } } return true; diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index b55978cd1df3..21c3d1514280 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -346,6 +346,8 @@ public function getTraitNames(): array {} /** @tentative-return-type */ public function getTraitAliases(): array {} + public function getFriendNames(): array {} + /** @tentative-return-type */ public function isTrait(): bool {} diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index eafbc9c0bd21..1ecb8d35bed3 100644 --- a/ext/reflection/php_reflection_arginfo.h +++ b/ext/reflection/php_reflection_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit php_reflection.stub.php instead. - * Stub hash: 6191049a28b4c5fe134fc37cd8969a33d00c51de + * Stub hash: 3a6f918d7103438ce451c4c92f9e8770b5aa782a * Has decl header: yes */ #include "zend_attributes.h" @@ -266,6 +266,8 @@ ZEND_END_ARG_INFO() #define arginfo_class_ReflectionClass_getTraitAliases arginfo_class_ReflectionFunctionAbstract_getParameters +#define arginfo_class_ReflectionClass_getFriendNames arginfo_class_ReflectionFunctionAbstract_getClosureUsedVariables + #define arginfo_class_ReflectionClass_isTrait arginfo_class_ReflectionFunctionAbstract_inNamespace #define arginfo_class_ReflectionClass_isEnum arginfo_class_ReflectionFunctionAbstract_hasTentativeReturnType @@ -840,6 +842,7 @@ ZEND_METHOD(ReflectionClass, isInterface); ZEND_METHOD(ReflectionClass, getTraits); ZEND_METHOD(ReflectionClass, getTraitNames); ZEND_METHOD(ReflectionClass, getTraitAliases); +ZEND_METHOD(ReflectionClass, getFriendNames); ZEND_METHOD(ReflectionClass, isTrait); ZEND_METHOD(ReflectionClass, isEnum); ZEND_METHOD(ReflectionClass, isAbstract); @@ -1140,6 +1143,7 @@ static const zend_function_entry class_ReflectionClass_methods[] = { ZEND_ME(ReflectionClass, getTraits, arginfo_class_ReflectionClass_getTraits, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionClass, getTraitNames, arginfo_class_ReflectionClass_getTraitNames, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionClass, getTraitAliases, arginfo_class_ReflectionClass_getTraitAliases, ZEND_ACC_PUBLIC) + ZEND_ME(ReflectionClass, getFriendNames, arginfo_class_ReflectionClass_getFriendNames, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionClass, isTrait, arginfo_class_ReflectionClass_isTrait, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionClass, isEnum, arginfo_class_ReflectionClass_isEnum, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionClass, isAbstract, arginfo_class_ReflectionClass_isAbstract, ZEND_ACC_PUBLIC) diff --git a/ext/reflection/php_reflection_decl.h b/ext/reflection/php_reflection_decl.h index e58dce51b39c..55f88dae49d8 100644 --- a/ext/reflection/php_reflection_decl.h +++ b/ext/reflection/php_reflection_decl.h @@ -1,12 +1,12 @@ /* This is a generated file, edit php_reflection.stub.php instead. - * Stub hash: 6191049a28b4c5fe134fc37cd8969a33d00c51de */ + * Stub hash: 3a6f918d7103438ce451c4c92f9e8770b5aa782a */ -#ifndef ZEND_PHP_REFLECTION_DECL_6191049a28b4c5fe134fc37cd8969a33d00c51de_H -#define ZEND_PHP_REFLECTION_DECL_6191049a28b4c5fe134fc37cd8969a33d00c51de_H +#ifndef ZEND_PHP_REFLECTION_DECL_3a6f918d7103438ce451c4c92f9e8770b5aa782a_H +#define ZEND_PHP_REFLECTION_DECL_3a6f918d7103438ce451c4c92f9e8770b5aa782a_H typedef enum zend_enum_PropertyHookType { ZEND_ENUM_PropertyHookType_Get = 1, ZEND_ENUM_PropertyHookType_Set = 2, } zend_enum_PropertyHookType; -#endif /* ZEND_PHP_REFLECTION_DECL_6191049a28b4c5fe134fc37cd8969a33d00c51de_H */ +#endif /* ZEND_PHP_REFLECTION_DECL_3a6f918d7103438ce451c4c92f9e8770b5aa782a_H */ diff --git a/ext/reflection/tests/ReflectionClass_getFriendNames.phpt b/ext/reflection/tests/ReflectionClass_getFriendNames.phpt new file mode 100644 index 000000000000..cdc4bf3b5ef3 --- /dev/null +++ b/ext/reflection/tests/ReflectionClass_getFriendNames.phpt @@ -0,0 +1,34 @@ +--TEST-- +ReflectionClass::getFriendNames() method +--FILE-- +getFriendNames()); + +$rc = new ReflectionClass(ReflectionClass::class); +var_dump($rc->getFriendNames()); + +?> +--EXPECT-- +array(4) { + [0]=> + string(3) "Bar" + [1]=> + string(8) "Demo\Bar" + [2]=> + string(3) "Baz" + [3]=> + string(8) "Demo\Qux" +} +array(0) { +} diff --git a/ext/reflection/tests/ReflectionClass_toString_001.phpt b/ext/reflection/tests/ReflectionClass_toString_001.phpt index 03495847857d..0fc418e360c5 100644 --- a/ext/reflection/tests/ReflectionClass_toString_001.phpt +++ b/ext/reflection/tests/ReflectionClass_toString_001.phpt @@ -30,7 +30,7 @@ Class [ class ReflectionClass implements Stringable, Refle Property [ public protected(set) readonly string $name ] } - - Methods [64] { + - Methods [65] { Method [ private method __clone ] { - Parameters [0] { @@ -259,6 +259,13 @@ Class [ class ReflectionClass implements Stringable, Refle - Tentative return [ array ] } + Method [ public method getFriendNames ] { + + - Parameters [0] { + } + - Return [ array ] + } + Method [ public method isTrait ] { - Parameters [0] { diff --git a/ext/reflection/tests/ReflectionClass_toString_010.phpt b/ext/reflection/tests/ReflectionClass_toString_010.phpt new file mode 100644 index 000000000000..eccd4a5e4fff --- /dev/null +++ b/ext/reflection/tests/ReflectionClass_toString_010.phpt @@ -0,0 +1,82 @@ +--TEST-- +Using ReflectionClass::__toString() with friends +--FILE-- + +--EXPECTF-- +Class [ class Foo1 ] { + @@ %s %d-%d + + - Constants [0] { + } + + - Friends [4] { + friend Bar + friend Demo\Bar + friend Baz + friend Demo\Qux + } + + - Static properties [0] { + } + + - Static methods [0] { + } + + - Properties [0] { + } + + - Methods [0] { + } +} +Enum [ enum Foo2 implements UnitEnum ] { + @@ %s %d-%d + + - Constants [0] { + } + + - Friends [4] { + friend Bar + friend Demo\Bar + friend Baz + friend Demo\Qux + } + + - Static properties [0] { + } + + - Static methods [1] { + Method [ static public method cases ] { + + - Parameters [0] { + } + - Return [ array ] + } + } + + - Properties [1] { + Property [ public protected(set) readonly string $name ] + } + + - Methods [0] { + } +} diff --git a/ext/reflection/tests/ReflectionProperty_isReadable_friend.phpt b/ext/reflection/tests/ReflectionProperty_isReadable_friend.phpt new file mode 100644 index 000000000000..a39fe12530f2 --- /dev/null +++ b/ext/reflection/tests/ReflectionProperty_isReadable_friend.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test ReflectionProperty::isReadable() with a friend +--FILE-- +isReadable('C', null)); +var_dump(new ReflectionProperty('A', 'protectedProp')->isReadable('C', null)); +var_dump(new ReflectionProperty('A', 'privateProp')->isReadable('C', null)); + +?> +--EXPECT-- +bool(true) +bool(true) +bool(false) diff --git a/ext/reflection/tests/ReflectionProperty_isReadable_invalid_scope.phpt b/ext/reflection/tests/ReflectionProperty_isReadable_invalid_scope.phpt index ae9f446c6771..1caa39cb56bf 100644 --- a/ext/reflection/tests/ReflectionProperty_isReadable_invalid_scope.phpt +++ b/ext/reflection/tests/ReflectionProperty_isReadable_invalid_scope.phpt @@ -4,6 +4,8 @@ Test ReflectionProperty::isReadable() invalid scope getMessage(), "\n"; } +try { + $r->isReadable('C', null); +} catch (Error $e) { + echo $e::class, ': ', $e->getMessage(), "\n"; +} + ?> --EXPECT-- Error: Class "B" not found +Error: Class "C" not found diff --git a/ext/reflection/tests/ReflectionProperty_isWritable_friend.phpt b/ext/reflection/tests/ReflectionProperty_isWritable_friend.phpt new file mode 100644 index 000000000000..9c8e6e470f7a --- /dev/null +++ b/ext/reflection/tests/ReflectionProperty_isWritable_friend.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test ReflectionProperty::isWritable() with a friend +--FILE-- +isWritable('C', null)); +var_dump(new ReflectionProperty('A', 'protectedProp')->isWritable('C', null)); +var_dump(new ReflectionProperty('A', 'privateProp')->isWritable('C', null)); + +?> +--EXPECT-- +bool(true) +bool(true) +bool(false) diff --git a/ext/reflection/tests/ReflectionProperty_isWritable_invalid_scope.phpt b/ext/reflection/tests/ReflectionProperty_isWritable_invalid_scope.phpt index 7474e521b35e..e5dc691ff5b3 100644 --- a/ext/reflection/tests/ReflectionProperty_isWritable_invalid_scope.phpt +++ b/ext/reflection/tests/ReflectionProperty_isWritable_invalid_scope.phpt @@ -4,6 +4,8 @@ Test ReflectionProperty::isWritable() invalid scope getMessage(), "\n"; } +try { + $r->isWritable('C', null); +} catch (Error $e) { + echo $e::class, ': ', $e->getMessage(), "\n"; +} + ?> --EXPECT-- Error: Class "B" not found +Error: Class "C" not found diff --git a/ext/tokenizer/tokenizer_data.c b/ext/tokenizer/tokenizer_data.c index 87b15b8bb345..ee0426f705bc 100644 --- a/ext/tokenizer/tokenizer_data.c +++ b/ext/tokenizer/tokenizer_data.c @@ -94,6 +94,7 @@ char *get_token_type_name(int token_type) case T_PROTECTED_SET: return "T_PROTECTED_SET"; case T_PUBLIC_SET: return "T_PUBLIC_SET"; case T_READONLY: return "T_READONLY"; + case T_FRIEND: return "T_FRIEND"; case T_VAR: return "T_VAR"; case T_UNSET: return "T_UNSET"; case T_ISSET: return "T_ISSET"; diff --git a/ext/tokenizer/tokenizer_data.stub.php b/ext/tokenizer/tokenizer_data.stub.php index 57c8edad8acb..c5d492e0d02f 100644 --- a/ext/tokenizer/tokenizer_data.stub.php +++ b/ext/tokenizer/tokenizer_data.stub.php @@ -357,6 +357,11 @@ * @cvalue T_READONLY */ const T_READONLY = UNKNOWN; +/** + * @var int + * @cvalue T_FRIEND + */ +const T_FRIEND = UNKNOWN; /** * @var int * @cvalue T_VAR diff --git a/ext/tokenizer/tokenizer_data_arginfo.h b/ext/tokenizer/tokenizer_data_arginfo.h index 20e5a5f776f5..9823d693397c 100644 --- a/ext/tokenizer/tokenizer_data_arginfo.h +++ b/ext/tokenizer/tokenizer_data_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit tokenizer_data.stub.php instead. - * Stub hash: c5235344b7c651d27c2c33c90696a418a9c96837 */ + * Stub hash: bd0be4aeae74201c37c8f49a49f27f077e109b34 */ #include "zend_constants.h" @@ -76,6 +76,7 @@ static void register_tokenizer_data_symbols(int module_number) REGISTER_LONG_CONSTANT("T_PROTECTED_SET", T_PROTECTED_SET, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_PUBLIC_SET", T_PUBLIC_SET, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_READONLY", T_READONLY, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("T_FRIEND", T_FRIEND, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_VAR", T_VAR, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_UNSET", T_UNSET, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("T_ISSET", T_ISSET, CONST_PERSISTENT);