2014-09-19 06:01:05 +08:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Zend Engine |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2019-01-30 17:23:29 +08:00
| Copyright ( c ) Zend Technologies Ltd . ( http : //www.zend.com) |
2014-09-19 06:01:05 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| This source file is subject to version 2.00 of the Zend license , |
| that is bundled with this package in the file LICENSE , and is |
| available through the world - wide - web at the following url : |
| http : //www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world - wide - web , please send a note to |
| license @ zend . com so we can mail you a copy immediately . |
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2018-11-01 23:20:07 +08:00
| Authors : Andi Gutmans < andi @ php . net > |
| Zeev Suraski < zeev @ php . net > |
2014-09-19 06:01:05 +08:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
# include "zend.h"
# include "zend_API.h"
# include "zend_compile.h"
# include "zend_execute.h"
2015-01-09 04:40:36 +08:00
# include "zend_inheritance.h"
2019-05-08 18:30:16 +08:00
# include "zend_interfaces.h"
2014-09-21 05:27:10 +08:00
# include "zend_smart_str.h"
2017-04-11 04:04:18 +08:00
# include "zend_operators.h"
2019-09-11 21:31:04 +08:00
# include "zend_exceptions.h"
2020-06-11 05:10:18 +08:00
# include "zend_enum.h"
2021-05-06 21:05:21 +08:00
# include "zend_attributes.h"
2022-05-11 16:43:01 +08:00
# include "zend_constants.h"
2022-07-11 22:19:26 +08:00
# include "zend_observer.h"
2014-09-19 06:01:05 +08:00
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ZEND_API zend_class_entry * ( * zend_inheritance_cache_get ) ( zend_class_entry * ce , zend_class_entry * parent , zend_class_entry * * traits_and_interfaces ) = NULL ;
ZEND_API zend_class_entry * ( * zend_inheritance_cache_add ) ( zend_class_entry * ce , zend_class_entry * proto , zend_class_entry * parent , zend_class_entry * * traits_and_interfaces , HashTable * dependencies ) = NULL ;
2021-05-06 21:05:21 +08:00
/* Unresolved means that class declarations that are currently not available are needed to
* determine whether the inheritance is valid or not . At runtime UNRESOLVED should be treated
* as an ERROR . */
typedef enum {
INHERITANCE_UNRESOLVED = - 1 ,
INHERITANCE_ERROR = 0 ,
INHERITANCE_WARNING = 1 ,
INHERITANCE_SUCCESS = 2 ,
} inheritance_status ;
2019-05-27 17:39:56 +08:00
static void add_dependency_obligation ( zend_class_entry * ce , zend_class_entry * dependency_ce ) ;
static void add_compatibility_obligation (
2020-10-15 18:46:07 +08:00
zend_class_entry * ce , const zend_function * child_fn , zend_class_entry * child_scope ,
const zend_function * parent_fn , zend_class_entry * parent_scope ) ;
2019-10-17 19:01:02 +08:00
static void add_property_compatibility_obligation (
zend_class_entry * ce , const zend_property_info * child_prop ,
const zend_property_info * parent_prop ) ;
2019-05-27 17:39:56 +08:00
2021-05-06 21:05:21 +08:00
static void ZEND_COLD emit_incompatible_method_error (
const zend_function * child , zend_class_entry * child_scope ,
const zend_function * parent , zend_class_entry * parent_scope ,
inheritance_status status ) ;
2021-06-18 19:25:39 +08:00
static void zend_type_copy_ctor ( zend_type * type , bool persistent ) {
2019-09-25 19:21:13 +08:00
if ( ZEND_TYPE_HAS_LIST ( * type ) ) {
zend_type_list * old_list = ZEND_TYPE_LIST ( * type ) ;
size_t size = ZEND_TYPE_LIST_SIZE ( old_list - > num_types ) ;
zend_type_list * new_list = ZEND_TYPE_USES_ARENA ( * type )
? zend_arena_alloc ( & CG ( arena ) , size ) : pemalloc ( size , persistent ) ;
memcpy ( new_list , old_list , ZEND_TYPE_LIST_SIZE ( old_list - > num_types ) ) ;
ZEND_TYPE_SET_PTR ( * type , new_list ) ;
2020-01-17 00:04:11 +08:00
zend_type * list_type ;
ZEND_TYPE_LIST_FOREACH ( new_list , list_type ) {
ZEND_ASSERT ( ZEND_TYPE_HAS_NAME ( * list_type ) ) ;
zend_string_addref ( ZEND_TYPE_NAME ( * list_type ) ) ;
2019-09-25 19:21:13 +08:00
} ZEND_TYPE_LIST_FOREACH_END ( ) ;
} else if ( ZEND_TYPE_HAS_NAME ( * type ) ) {
zend_string_addref ( ZEND_TYPE_NAME ( * type ) ) ;
}
}
2019-06-27 16:27:53 +08:00
static zend_function * zend_duplicate_internal_function ( zend_function * func , zend_class_entry * ce ) /* { { { */
2015-03-05 07:10:38 +08:00
{
zend_function * new_function ;
2019-06-27 16:27:53 +08:00
if ( UNEXPECTED ( ce - > type & ZEND_INTERNAL_CLASS ) ) {
new_function = pemalloc ( sizeof ( zend_internal_function ) , 1 ) ;
memcpy ( new_function , func , sizeof ( zend_internal_function ) ) ;
} else {
new_function = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_internal_function ) ) ;
memcpy ( new_function , func , sizeof ( zend_internal_function ) ) ;
new_function - > common . fn_flags | = ZEND_ACC_ARENA_ALLOCATED ;
}
if ( EXPECTED ( new_function - > common . function_name ) ) {
zend_string_addref ( new_function - > common . function_name ) ;
}
return new_function ;
}
/* }}} */
2021-02-22 19:28:02 +08:00
static zend_always_inline zend_function * zend_duplicate_function ( zend_function * func , zend_class_entry * ce ) /* { { { */
2019-06-27 16:27:53 +08:00
{
2015-03-06 22:07:36 +08:00
if ( UNEXPECTED ( func - > type = = ZEND_INTERNAL_FUNCTION ) ) {
2019-06-27 16:27:53 +08:00
return zend_duplicate_internal_function ( func , ce ) ;
2015-03-05 07:10:38 +08:00
} else {
if ( func - > op_array . refcount ) {
( * func - > op_array . refcount ) + + ;
}
2020-03-02 18:07:57 +08:00
if ( EXPECTED ( func - > op_array . function_name ) ) {
zend_string_addref ( func - > op_array . function_name ) ;
}
2021-02-22 19:28:02 +08:00
return func ;
2015-03-05 07:10:38 +08:00
}
}
/* }}} */
2014-12-14 06:06:14 +08:00
static void do_inherit_parent_constructor ( zend_class_entry * ce ) /* { { { */
2014-09-19 06:01:05 +08:00
{
2018-09-05 18:16:10 +08:00
zend_class_entry * parent = ce - > parent ;
ZEND_ASSERT ( parent ! = NULL ) ;
2014-09-19 06:01:05 +08:00
/* You cannot change create_object */
2018-09-05 18:16:10 +08:00
ce - > create_object = parent - > create_object ;
2014-09-19 06:01:05 +08:00
/* Inherit special functions if needed */
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > get_iterator ) ) {
2018-09-05 18:16:10 +08:00
ce - > get_iterator = parent - > get_iterator ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __get ) ) {
2018-09-05 18:16:10 +08:00
ce - > __get = parent - > __get ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __set ) ) {
2018-09-05 18:16:10 +08:00
ce - > __set = parent - > __set ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __unset ) ) {
2018-09-05 18:16:10 +08:00
ce - > __unset = parent - > __unset ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __isset ) ) {
2018-09-05 18:16:10 +08:00
ce - > __isset = parent - > __isset ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __call ) ) {
2018-09-05 18:16:10 +08:00
ce - > __call = parent - > __call ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __callstatic ) ) {
2018-09-05 18:16:10 +08:00
ce - > __callstatic = parent - > __callstatic ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __tostring ) ) {
2018-09-05 18:16:10 +08:00
ce - > __tostring = parent - > __tostring ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > clone ) ) {
2018-09-05 18:16:10 +08:00
ce - > clone = parent - > clone ;
2014-09-19 06:01:05 +08:00
}
2020-06-26 16:54:40 +08:00
if ( EXPECTED ( ! ce - > __serialize ) ) {
ce - > __serialize = parent - > __serialize ;
}
if ( EXPECTED ( ! ce - > __unserialize ) ) {
ce - > __unserialize = parent - > __unserialize ;
2019-02-15 18:20:28 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > serialize ) ) {
2018-09-05 18:16:10 +08:00
ce - > serialize = parent - > serialize ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > unserialize ) ) {
2018-09-05 18:16:10 +08:00
ce - > unserialize = parent - > unserialize ;
2014-09-19 06:01:05 +08:00
}
if ( ! ce - > destructor ) {
2018-09-05 18:16:10 +08:00
ce - > destructor = parent - > destructor ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( ! ce - > __debugInfo ) ) {
2018-09-05 18:16:10 +08:00
ce - > __debugInfo = parent - > __debugInfo ;
2014-09-19 06:01:05 +08:00
}
2015-03-05 07:10:38 +08:00
2014-09-19 06:01:05 +08:00
if ( ce - > constructor ) {
2018-09-05 18:16:10 +08:00
if ( parent - > constructor & & UNEXPECTED ( parent - > constructor - > common . fn_flags & ZEND_ACC_FINAL ) ) {
2015-03-05 07:10:38 +08:00
zend_error_noreturn ( E_ERROR , " Cannot override final %s::%s() with %s::%s() " ,
2018-09-05 18:16:10 +08:00
ZSTR_VAL ( parent - > name ) , ZSTR_VAL ( parent - > constructor - > common . function_name ) ,
2015-06-30 18:59:27 +08:00
ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( ce - > constructor - > common . function_name ) ) ;
2014-09-19 06:01:05 +08:00
}
return ;
}
2018-09-05 18:16:10 +08:00
ce - > constructor = parent - > constructor ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
char * zend_visibility_string ( uint32_t fn_flags ) /* { { { */
{
if ( fn_flags & ZEND_ACC_PUBLIC ) {
return " public " ;
2018-09-11 16:56:45 +08:00
} else if ( fn_flags & ZEND_ACC_PRIVATE ) {
return " private " ;
} else {
ZEND_ASSERT ( fn_flags & ZEND_ACC_PROTECTED ) ;
return " protected " ;
2014-09-19 06:01:05 +08:00
}
}
/* }}} */
2019-10-17 19:01:02 +08:00
static zend_string * resolve_class_name ( zend_class_entry * scope , zend_string * name ) {
ZEND_ASSERT ( scope ) ;
if ( zend_string_equals_literal_ci ( name , " parent " ) & & scope - > parent ) {
if ( scope - > ce_flags & ZEND_ACC_RESOLVED_PARENT ) {
return scope - > parent - > name ;
2019-05-09 16:41:06 +08:00
} else {
2019-10-17 19:01:02 +08:00
return scope - > parent_name ;
2019-05-09 16:41:06 +08:00
}
2019-05-08 18:11:12 +08:00
} else if ( zend_string_equals_literal_ci ( name , " self " ) ) {
2019-10-17 19:01:02 +08:00
return scope - > name ;
2019-05-08 18:11:12 +08:00
} else {
return name ;
2016-06-04 22:44:49 +08:00
}
}
2021-01-15 19:30:54 +08:00
static bool class_visible ( zend_class_entry * ce ) {
2019-05-09 16:41:06 +08:00
if ( ce - > type = = ZEND_INTERNAL_CLASS ) {
return ! ( CG ( compiler_options ) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES ) ;
} else {
ZEND_ASSERT ( ce - > type = = ZEND_USER_CLASS ) ;
return ! ( CG ( compiler_options ) & ZEND_COMPILE_IGNORE_OTHER_FILES )
| | ce - > info . user . filename = = CG ( compiled_filename ) ;
}
}
2021-05-10 18:52:59 +08:00
static zend_always_inline void register_unresolved_class ( zend_string * name ) {
/* We'll autoload this class and process delayed variance obligations later. */
if ( ! CG ( delayed_autoloads ) ) {
ALLOC_HASHTABLE ( CG ( delayed_autoloads ) ) ;
zend_hash_init ( CG ( delayed_autoloads ) , 0 , NULL , NULL , 0 ) ;
}
zend_hash_add_empty_element ( CG ( delayed_autoloads ) , name ) ;
}
2021-07-01 16:12:49 +08:00
static zend_class_entry * lookup_class_ex (
2021-01-15 19:30:54 +08:00
zend_class_entry * scope , zend_string * name , bool register_unresolved ) {
2021-05-10 18:52:59 +08:00
zend_class_entry * ce ;
2021-08-16 21:53:12 +08:00
bool in_preload = CG ( compiler_options ) & ZEND_COMPILE_PRELOAD ;
2021-05-10 18:52:59 +08:00
2021-08-16 21:53:12 +08:00
if ( UNEXPECTED ( ! EG ( active ) & & ! in_preload ) ) {
2021-05-10 18:52:59 +08:00
zend_string * lc_name = zend_string_tolower ( name ) ;
ce = zend_hash_find_ptr ( CG ( class_table ) , lc_name ) ;
zend_string_release ( lc_name ) ;
if ( register_unresolved & & ! ce ) {
zend_error_noreturn (
E_COMPILE_ERROR , " %s must be registered before %s " ,
ZSTR_VAL ( name ) , ZSTR_VAL ( scope - > name ) ) ;
}
return ce ;
}
ce = zend_lookup_class_ex (
name , NULL , ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD ) ;
2021-08-16 21:53:12 +08:00
if ( ! CG ( in_compilation ) | | in_preload ) {
2019-05-08 18:30:16 +08:00
if ( ce ) {
return ce ;
}
2019-05-27 17:39:56 +08:00
2019-09-25 19:21:13 +08:00
if ( register_unresolved ) {
2021-05-10 18:52:59 +08:00
register_unresolved_class ( name ) ;
2019-05-27 17:39:56 +08:00
}
2019-05-08 18:30:16 +08:00
} else {
if ( ce & & class_visible ( ce ) ) {
return ce ;
}
2019-05-09 16:41:06 +08:00
2019-05-27 17:39:56 +08:00
/* The current class may not be registered yet, so check for it explicitly. */
2019-10-17 19:01:02 +08:00
if ( zend_string_equals_ci ( scope - > name , name ) ) {
return scope ;
2019-05-27 17:39:56 +08:00
}
2019-05-09 16:41:06 +08:00
}
return NULL ;
}
2021-07-01 16:12:49 +08:00
static zend_class_entry * lookup_class ( zend_class_entry * scope , zend_string * name ) {
return lookup_class_ex ( scope , name , /* register_unresolved */ false ) ;
}
2019-05-27 17:39:56 +08:00
/* Instanceof that's safe to use on unlinked classes. */
2021-01-15 19:30:54 +08:00
static bool unlinked_instanceof ( zend_class_entry * ce1 , zend_class_entry * ce2 ) {
2019-05-27 17:39:56 +08:00
if ( ce1 = = ce2 ) {
return 1 ;
}
2019-10-25 00:11:41 +08:00
if ( ce1 - > ce_flags & ZEND_ACC_LINKED ) {
2019-05-08 18:30:16 +08:00
return instanceof_function ( ce1 , ce2 ) ;
}
2019-05-27 17:39:56 +08:00
2020-10-06 22:30:58 +08:00
if ( ce1 - > parent ) {
zend_class_entry * parent_ce ;
if ( ce1 - > ce_flags & ZEND_ACC_RESOLVED_PARENT ) {
parent_ce = ce1 - > parent ;
2019-05-27 17:39:56 +08:00
} else {
2020-10-06 22:30:58 +08:00
parent_ce = zend_lookup_class_ex ( ce1 - > parent_name , NULL ,
2019-05-27 17:39:56 +08:00
ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD ) ;
}
2020-10-06 22:30:58 +08:00
/* It's not sufficient to only check the parent chain itself, as need to do a full
* recursive instanceof in case the parent interfaces haven ' t been copied yet . */
if ( parent_ce & & unlinked_instanceof ( parent_ce , ce2 ) ) {
2019-05-27 17:39:56 +08:00
return 1 ;
}
}
if ( ce1 - > num_interfaces ) {
uint32_t i ;
2019-10-25 00:11:41 +08:00
if ( ce1 - > ce_flags & ZEND_ACC_RESOLVED_INTERFACES ) {
/* Unlike the normal instanceof_function(), we have to perform a recursive
* check here , as the parent interfaces might not have been fully copied yet . */
for ( i = 0 ; i < ce1 - > num_interfaces ; i + + ) {
if ( unlinked_instanceof ( ce1 - > interfaces [ i ] , ce2 ) ) {
return 1 ;
}
}
} else {
for ( i = 0 ; i < ce1 - > num_interfaces ; i + + ) {
2020-10-06 22:30:58 +08:00
zend_class_entry * ce = zend_lookup_class_ex (
2019-10-25 00:11:41 +08:00
ce1 - > interface_names [ i ] . name , ce1 - > interface_names [ i ] . lc_name ,
ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD ) ;
2021-03-31 11:49:41 +08:00
/* Avoid recursing if class implements itself. */
2021-01-05 20:02:30 +08:00
if ( ce & & ce ! = ce1 & & unlinked_instanceof ( ce , ce2 ) ) {
2019-10-25 00:11:41 +08:00
return 1 ;
}
2019-05-27 17:39:56 +08:00
}
}
}
return 0 ;
2019-05-08 18:30:16 +08:00
}
2021-01-15 19:30:54 +08:00
static bool zend_type_permits_self (
2020-01-07 22:06:36 +08:00
zend_type type , zend_class_entry * scope , zend_class_entry * self ) {
if ( ZEND_TYPE_FULL_MASK ( type ) & MAY_BE_OBJECT ) {
return 1 ;
}
/* Any types that may satisfy self must have already been loaded at this point
* ( as a parent or interface ) , so we never need to register delayed variance obligations
* for this case . */
zend_type * single_type ;
ZEND_TYPE_FOREACH ( type , single_type ) {
if ( ZEND_TYPE_HAS_NAME ( * single_type ) ) {
zend_string * name = resolve_class_name ( scope , ZEND_TYPE_NAME ( * single_type ) ) ;
2021-07-01 16:12:49 +08:00
zend_class_entry * ce = lookup_class ( self , name ) ;
2020-01-07 22:06:36 +08:00
if ( ce & & unlinked_instanceof ( self , ce ) ) {
return 1 ;
}
}
} ZEND_TYPE_FOREACH_END ( ) ;
return 0 ;
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
static void track_class_dependency ( zend_class_entry * ce , zend_string * class_name )
{
HashTable * ht ;
2022-07-08 18:30:23 +08:00
ZEND_ASSERT ( class_name ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
if ( ! CG ( current_linking_class ) | | ce = = CG ( current_linking_class ) ) {
return ;
} else if ( zend_string_equals_literal_ci ( class_name , " self " )
| | zend_string_equals_literal_ci ( class_name , " parent " ) ) {
return ;
}
2021-07-19 16:53:22 +08:00
# ifndef ZEND_WIN32
/* On non-Windows systems, internal classes are always the same,
* so there is no need to explicitly track them . */
if ( ce - > type = = ZEND_INTERNAL_CLASS ) {
return ;
}
# endif
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ht = ( HashTable * ) CG ( current_linking_class ) - > inheritance_cache ;
if ( ! ( ce - > ce_flags & ZEND_ACC_IMMUTABLE ) ) {
// TODO: dependency on not-immutable class ???
if ( ht ) {
zend_hash_destroy ( ht ) ;
FREE_HASHTABLE ( ht ) ;
CG ( current_linking_class ) - > inheritance_cache = NULL ;
}
CG ( current_linking_class ) - > ce_flags & = ~ ZEND_ACC_CACHEABLE ;
CG ( current_linking_class ) = NULL ;
return ;
}
/* Record dependency */
if ( ! ht ) {
ALLOC_HASHTABLE ( ht ) ;
zend_hash_init ( ht , 0 , NULL , NULL , 0 ) ;
CG ( current_linking_class ) - > inheritance_cache = ( zend_inheritance_cache_entry * ) ht ;
}
zend_hash_add_ptr ( ht , class_name , ce ) ;
}
2021-07-05 20:11:03 +08:00
/* Check whether any type in the fe_type intersection type is a subtype of the proto class. */
static inheritance_status zend_is_intersection_subtype_of_class (
zend_class_entry * fe_scope , zend_type fe_type ,
zend_class_entry * proto_scope , zend_string * proto_class_name , zend_class_entry * proto_ce )
{
ZEND_ASSERT ( ZEND_TYPE_IS_INTERSECTION ( fe_type ) ) ;
bool have_unresolved = false ;
zend_type * single_type ;
/* Traverse the list of child types and check that at least one is
* a subtype of the parent type being checked */
ZEND_TYPE_FOREACH ( fe_type , single_type ) {
zend_class_entry * fe_ce ;
zend_string * fe_class_name = NULL ;
if ( ZEND_TYPE_HAS_NAME ( * single_type ) ) {
fe_class_name =
resolve_class_name ( fe_scope , ZEND_TYPE_NAME ( * single_type ) ) ;
if ( zend_string_equals_ci ( fe_class_name , proto_class_name ) ) {
return INHERITANCE_SUCCESS ;
}
if ( ! proto_ce ) proto_ce = lookup_class ( proto_scope , proto_class_name ) ;
fe_ce = lookup_class ( fe_scope , fe_class_name ) ;
} else {
/* standard type in an intersection type is impossible,
* because it would be a fatal compile error */
ZEND_UNREACHABLE ( ) ;
continue ;
}
if ( ! fe_ce | | ! proto_ce ) {
have_unresolved = true ;
continue ;
}
if ( unlinked_instanceof ( fe_ce , proto_ce ) ) {
track_class_dependency ( fe_ce , fe_class_name ) ;
track_class_dependency ( proto_ce , proto_class_name ) ;
return INHERITANCE_SUCCESS ;
}
} ZEND_TYPE_FOREACH_END ( ) ;
return have_unresolved ? INHERITANCE_UNRESOLVED : INHERITANCE_ERROR ;
}
/* Check whether a single class proto type is a subtype of a potentially complex fe_type. */
static inheritance_status zend_is_class_subtype_of_type (
2021-08-11 16:28:52 +08:00
zend_class_entry * fe_scope , zend_string * fe_class_name ,
2021-07-01 16:12:49 +08:00
zend_class_entry * proto_scope , zend_type proto_type ) {
2021-08-11 16:28:52 +08:00
zend_class_entry * fe_ce = NULL ;
2021-01-15 19:30:54 +08:00
bool have_unresolved = 0 ;
2021-07-05 20:11:03 +08:00
/* If the parent has 'object' as a return type, any class satisfies the co-variant check */
2019-09-25 19:21:13 +08:00
if ( ZEND_TYPE_FULL_MASK ( proto_type ) & MAY_BE_OBJECT ) {
/* Currently, any class name would be allowed here. We still perform a class lookup
* for forward - compatibility reasons , as we may have named types in the future that
2021-07-05 20:11:03 +08:00
* are not classes ( such as typedefs ) . */
2021-07-01 16:12:49 +08:00
if ( ! fe_ce ) fe_ce = lookup_class ( fe_scope , fe_class_name ) ;
2019-09-25 19:21:13 +08:00
if ( ! fe_ce ) {
have_unresolved = 1 ;
} else {
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
track_class_dependency ( fe_ce , fe_class_name ) ;
2019-09-25 19:21:13 +08:00
return INHERITANCE_SUCCESS ;
}
2019-05-08 17:24:08 +08:00
}
2015-01-09 04:40:36 +08:00
2020-01-17 00:04:11 +08:00
zend_type * single_type ;
2021-07-05 20:11:03 +08:00
/* Traverse the list of parent types and check if the current child (FE)
* class is the subtype of at least one of them ( union ) or all of them ( intersection ) . */
bool is_intersection = ZEND_TYPE_IS_INTERSECTION ( proto_type ) ;
2020-01-17 00:04:11 +08:00
ZEND_TYPE_FOREACH ( proto_type , single_type ) {
2022-07-08 18:30:23 +08:00
if ( ZEND_TYPE_IS_INTERSECTION ( * single_type ) ) {
inheritance_status subtype_status = zend_is_class_subtype_of_type (
fe_scope , fe_class_name , proto_scope , * single_type ) ;
switch ( subtype_status ) {
case INHERITANCE_ERROR :
if ( is_intersection ) {
return INHERITANCE_ERROR ;
}
continue ;
case INHERITANCE_UNRESOLVED :
have_unresolved = 1 ;
continue ;
case INHERITANCE_SUCCESS :
if ( ! is_intersection ) {
return INHERITANCE_SUCCESS ;
}
continue ;
EMPTY_SWITCH_DEFAULT_CASE ( ) ;
}
}
2020-11-03 21:15:05 +08:00
zend_class_entry * proto_ce ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
zend_string * proto_class_name = NULL ;
2020-01-17 00:04:11 +08:00
if ( ZEND_TYPE_HAS_NAME ( * single_type ) ) {
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
proto_class_name =
2020-01-17 00:04:11 +08:00
resolve_class_name ( proto_scope , ZEND_TYPE_NAME ( * single_type ) ) ;
2019-09-25 19:21:13 +08:00
if ( zend_string_equals_ci ( fe_class_name , proto_class_name ) ) {
2021-07-05 20:11:03 +08:00
if ( ! is_intersection ) {
return INHERITANCE_SUCCESS ;
}
continue ;
2019-09-25 19:21:13 +08:00
}
2019-05-08 18:11:12 +08:00
2021-07-01 16:12:49 +08:00
if ( ! fe_ce ) fe_ce = lookup_class ( fe_scope , fe_class_name ) ;
proto_ce = lookup_class ( proto_scope , proto_class_name ) ;
2020-11-03 21:15:05 +08:00
} else {
2021-07-05 20:11:03 +08:00
/* standard type */
ZEND_ASSERT ( ! is_intersection ) ;
2020-11-03 21:15:05 +08:00
continue ;
}
if ( ! fe_ce | | ! proto_ce ) {
have_unresolved = 1 ;
2021-07-05 20:11:03 +08:00
continue ;
}
if ( unlinked_instanceof ( fe_ce , proto_ce ) ) {
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
track_class_dependency ( fe_ce , fe_class_name ) ;
track_class_dependency ( proto_ce , proto_class_name ) ;
2021-07-05 20:11:03 +08:00
if ( ! is_intersection ) {
return INHERITANCE_SUCCESS ;
}
} else {
if ( is_intersection ) {
return INHERITANCE_ERROR ;
}
2020-01-17 00:04:11 +08:00
}
} ZEND_TYPE_FOREACH_END ( ) ;
2021-07-05 20:11:03 +08:00
if ( have_unresolved ) {
return INHERITANCE_UNRESOLVED ;
}
return is_intersection ? INHERITANCE_SUCCESS : INHERITANCE_ERROR ;
}
2021-08-11 16:28:52 +08:00
static zend_string * get_class_from_type ( zend_class_entry * scope , zend_type single_type ) {
2021-07-05 20:11:03 +08:00
if ( ZEND_TYPE_HAS_NAME ( single_type ) ) {
return resolve_class_name ( scope , ZEND_TYPE_NAME ( single_type ) ) ;
}
return NULL ;
2019-09-25 19:21:13 +08:00
}
2021-07-01 16:12:49 +08:00
static void register_unresolved_classes ( zend_class_entry * scope , zend_type type ) {
zend_type * single_type ;
ZEND_TYPE_FOREACH ( type , single_type ) {
2022-07-08 18:30:23 +08:00
if ( ZEND_TYPE_HAS_LIST ( * single_type ) ) {
register_unresolved_classes ( scope , * single_type ) ;
continue ;
}
2021-07-01 16:12:49 +08:00
if ( ZEND_TYPE_HAS_NAME ( * single_type ) ) {
zend_string * class_name = resolve_class_name ( scope , ZEND_TYPE_NAME ( * single_type ) ) ;
lookup_class_ex ( scope , class_name , /* register_unresolved */ true ) ;
}
} ZEND_TYPE_FOREACH_END ( ) ;
}
2022-07-08 18:30:23 +08:00
static inheritance_status zend_is_intersection_subtype_of_type (
zend_class_entry * fe_scope , zend_type fe_type ,
zend_class_entry * proto_scope , zend_type proto_type )
{
bool have_unresolved = false ;
zend_type * single_type ;
uint32_t proto_type_mask = ZEND_TYPE_PURE_MASK ( proto_type ) ;
/* Currently, for object type any class name would be allowed here.
* We still perform a class lookup for forward - compatibility reasons ,
* as we may have named types in the future that are not classes
* ( such as typedefs ) . */
if ( proto_type_mask & MAY_BE_OBJECT ) {
ZEND_TYPE_FOREACH ( fe_type , single_type ) {
zend_string * fe_class_name = get_class_from_type ( fe_scope , * single_type ) ;
if ( ! fe_class_name ) {
continue ;
}
zend_class_entry * fe_ce = lookup_class ( fe_scope , fe_class_name ) ;
if ( fe_ce ) {
track_class_dependency ( fe_ce , fe_class_name ) ;
return INHERITANCE_SUCCESS ;
} else {
have_unresolved = true ;
}
} ZEND_TYPE_FOREACH_END ( ) ;
}
/* U_1&...&U_n < V_1&...&V_m if forall V_j. exists U_i. U_i < V_j.
* U_1 & . . . & U_n < V_1 | . . . | V_m if exists V_j . exists U_i . U_i < V_j .
* As such , we need to iterate over proto_type ( V_j ) first and use a different
* quantifier depending on whether fe_type is a union or an intersection . */
inheritance_status early_exit_status =
ZEND_TYPE_IS_INTERSECTION ( proto_type ) ? INHERITANCE_ERROR : INHERITANCE_SUCCESS ;
ZEND_TYPE_FOREACH ( proto_type , single_type ) {
inheritance_status status ;
if ( ZEND_TYPE_IS_INTERSECTION ( * single_type ) ) {
status = zend_is_intersection_subtype_of_type (
fe_scope , fe_type , proto_scope , * single_type ) ;
} else {
zend_string * proto_class_name = get_class_from_type ( proto_scope , * single_type ) ;
if ( ! proto_class_name ) {
continue ;
}
zend_class_entry * proto_ce = NULL ;
status = zend_is_intersection_subtype_of_class (
fe_scope , fe_type , proto_scope , proto_class_name , proto_ce ) ;
}
if ( status = = early_exit_status ) {
return status ;
}
if ( status = = INHERITANCE_UNRESOLVED ) {
have_unresolved = true ;
}
} ZEND_TYPE_FOREACH_END ( ) ;
if ( have_unresolved ) {
return INHERITANCE_UNRESOLVED ;
}
return early_exit_status = = INHERITANCE_ERROR ? INHERITANCE_SUCCESS : INHERITANCE_ERROR ;
}
2019-09-25 19:21:13 +08:00
static inheritance_status zend_perform_covariant_type_check (
zend_class_entry * fe_scope , zend_type fe_type ,
2021-06-30 21:45:42 +08:00
zend_class_entry * proto_scope , zend_type proto_type )
2019-09-25 19:21:13 +08:00
{
ZEND_ASSERT ( ZEND_TYPE_IS_SET ( fe_type ) & & ZEND_TYPE_IS_SET ( proto_type ) ) ;
2021-05-10 15:45:48 +08:00
/* Apart from void, everything is trivially covariant to the mixed type.
* Handle this case separately to ensure it never requires class loading . */
if ( ZEND_TYPE_PURE_MASK ( proto_type ) = = MAY_BE_ANY & &
! ZEND_TYPE_CONTAINS_CODE ( fe_type , IS_VOID ) ) {
return INHERITANCE_SUCCESS ;
}
2019-09-25 19:21:13 +08:00
/* Builtin types may be removed, but not added */
uint32_t fe_type_mask = ZEND_TYPE_PURE_MASK ( fe_type ) ;
uint32_t proto_type_mask = ZEND_TYPE_PURE_MASK ( proto_type ) ;
uint32_t added_types = fe_type_mask & ~ proto_type_mask ;
if ( added_types ) {
2020-01-07 22:06:36 +08:00
if ( ( added_types & MAY_BE_STATIC )
& & zend_type_permits_self ( proto_type , proto_scope , fe_scope ) ) {
/* Replacing type that accepts self with static is okay */
added_types & = ~ MAY_BE_STATIC ;
}
2021-04-19 17:23:30 +08:00
if ( added_types = = MAY_BE_NEVER ) {
/* never is the bottom type */
return INHERITANCE_SUCCESS ;
}
2020-01-07 22:06:36 +08:00
if ( added_types ) {
2019-09-25 19:21:13 +08:00
/* Otherwise adding new types is illegal */
2021-06-30 21:45:42 +08:00
return INHERITANCE_ERROR ;
2019-09-25 19:21:13 +08:00
}
}
2020-11-03 21:15:05 +08:00
zend_type * single_type ;
2021-07-05 20:11:03 +08:00
inheritance_status early_exit_status ;
bool have_unresolved = false ;
if ( ZEND_TYPE_IS_INTERSECTION ( fe_type ) ) {
early_exit_status =
ZEND_TYPE_IS_INTERSECTION ( proto_type ) ? INHERITANCE_ERROR : INHERITANCE_SUCCESS ;
2022-07-08 18:30:23 +08:00
inheritance_status status = zend_is_intersection_subtype_of_type (
fe_scope , fe_type , proto_scope , proto_type ) ;
2019-05-08 17:24:08 +08:00
2022-07-08 18:30:23 +08:00
if ( status = = early_exit_status ) {
return status ;
}
if ( status = = INHERITANCE_UNRESOLVED ) {
have_unresolved = true ;
}
2021-07-05 20:11:03 +08:00
} else {
/* U_1|...|U_n < V_1|...|V_m if forall U_i. exists V_j. U_i < V_j.
* U_1 | . . . | U_n < V_1 & . . . & V_m if forall U_i . forall V_j . U_i < V_j .
* We need to iterate over fe_type ( U_i ) first and the logic is independent of
* whether proto_type is a union or intersection ( only the inner check differs ) . */
early_exit_status = INHERITANCE_ERROR ;
ZEND_TYPE_FOREACH ( fe_type , single_type ) {
2022-07-08 18:30:23 +08:00
inheritance_status status ;
/* Union has an intersection type as it's member */
if ( ZEND_TYPE_IS_INTERSECTION ( * single_type ) ) {
status = zend_is_intersection_subtype_of_type (
fe_scope , * single_type , proto_scope , proto_type ) ;
} else {
zend_string * fe_class_name = get_class_from_type ( fe_scope , * single_type ) ;
if ( ! fe_class_name ) {
continue ;
}
status = zend_is_class_subtype_of_type (
fe_scope , fe_class_name , proto_scope , proto_type ) ;
2021-07-05 20:11:03 +08:00
}
if ( status = = early_exit_status ) {
return status ;
}
if ( status = = INHERITANCE_UNRESOLVED ) {
have_unresolved = true ;
}
} ZEND_TYPE_FOREACH_END ( ) ;
}
if ( ! have_unresolved ) {
return early_exit_status = = INHERITANCE_ERROR ? INHERITANCE_SUCCESS : INHERITANCE_ERROR ;
2016-03-28 18:54:25 +08:00
}
2019-09-25 19:21:13 +08:00
2021-07-01 16:12:49 +08:00
register_unresolved_classes ( fe_scope , fe_type ) ;
register_unresolved_classes ( proto_scope , proto_type ) ;
2020-11-03 21:15:05 +08:00
return INHERITANCE_UNRESOLVED ;
2015-01-09 04:40:36 +08:00
}
2019-05-08 18:30:16 +08:00
static inheritance_status zend_do_perform_arg_type_hint_check (
2020-04-23 17:34:07 +08:00
zend_class_entry * fe_scope , zend_arg_info * fe_arg_info ,
zend_class_entry * proto_scope , zend_arg_info * proto_arg_info ) /* {{{ */
2017-01-01 22:53:24 +08:00
{
2020-03-28 06:39:49 +08:00
if ( ! ZEND_TYPE_IS_SET ( fe_arg_info - > type ) | | ZEND_TYPE_PURE_MASK ( fe_arg_info - > type ) = = MAY_BE_ANY ) {
/* Child with no type or mixed type is always compatible */
2019-05-09 16:41:06 +08:00
return INHERITANCE_SUCCESS ;
2017-01-01 22:53:24 +08:00
}
if ( ! ZEND_TYPE_IS_SET ( proto_arg_info - > type ) ) {
/* Child defines a type, but parent doesn't, violates LSP */
2019-05-09 16:41:06 +08:00
return INHERITANCE_ERROR ;
2017-01-01 22:53:24 +08:00
}
2019-05-08 17:24:08 +08:00
/* Contravariant type check is performed as a covariant type check with swapped
* argument order . */
2019-05-08 18:30:16 +08:00
return zend_perform_covariant_type_check (
2021-06-30 21:45:42 +08:00
proto_scope , proto_arg_info - > type , fe_scope , fe_arg_info - > type ) ;
2017-01-01 22:53:24 +08:00
}
/* }}} */
2020-10-15 18:46:07 +08:00
/* For trait methods, fe_scope/proto_scope may differ from fe/proto->common.scope,
2020-04-23 17:34:07 +08:00
* as self will refer to the self of the class the trait is used in , not the trait
* the method was declared in . */
2019-05-08 18:30:16 +08:00
static inheritance_status zend_do_perform_implementation_check (
2020-10-15 18:46:07 +08:00
const zend_function * fe , zend_class_entry * fe_scope ,
const zend_function * proto , zend_class_entry * proto_scope ) /* {{{ */
2014-09-19 06:01:05 +08:00
{
2020-01-06 19:06:51 +08:00
uint32_t i , num_args , proto_num_args , fe_num_args ;
2019-05-09 16:41:06 +08:00
inheritance_status status , local_status ;
2021-01-15 19:30:54 +08:00
bool proto_is_variadic , fe_is_variadic ;
2014-09-19 06:01:05 +08:00
/* Checks for constructors only if they are declared in an interface,
* or explicitly marked as abstract
*/
2019-06-25 23:26:56 +08:00
ZEND_ASSERT ( ! ( ( fe - > common . fn_flags & ZEND_ACC_CTOR )
2014-09-19 06:01:05 +08:00
& & ( ( proto - > common . scope - > ce_flags & ZEND_ACC_INTERFACE ) = = 0
2019-06-25 23:26:56 +08:00
& & ( proto - > common . fn_flags & ZEND_ACC_ABSTRACT ) = = 0 ) ) ) ;
2014-09-19 06:01:05 +08:00
2020-01-09 22:04:33 +08:00
/* If the prototype method is private and not abstract, we do not enforce a signature.
* private abstract methods can only occur in traits . */
ZEND_ASSERT ( ! ( proto - > common . fn_flags & ZEND_ACC_PRIVATE )
| | ( proto - > common . fn_flags & ZEND_ACC_ABSTRACT ) ) ;
2014-09-19 06:01:05 +08:00
2020-01-06 19:06:51 +08:00
/* The number of required arguments cannot increase. */
if ( proto - > common . required_num_args < fe - > common . required_num_args ) {
2019-05-09 16:41:06 +08:00
return INHERITANCE_ERROR ;
2014-09-19 06:01:05 +08:00
}
/* by-ref constraints on return values are covariant */
if ( ( proto - > common . fn_flags & ZEND_ACC_RETURN_REFERENCE )
& & ! ( fe - > common . fn_flags & ZEND_ACC_RETURN_REFERENCE ) ) {
2019-05-09 16:41:06 +08:00
return INHERITANCE_ERROR ;
2014-09-19 06:01:05 +08:00
}
2020-01-06 19:06:51 +08:00
proto_is_variadic = ( proto - > common . fn_flags & ZEND_ACC_VARIADIC ) ! = 0 ;
fe_is_variadic = ( fe - > common . fn_flags & ZEND_ACC_VARIADIC ) ! = 0 ;
/* A variadic function cannot become non-variadic */
if ( proto_is_variadic & & ! fe_is_variadic ) {
2019-05-09 16:41:06 +08:00
return INHERITANCE_ERROR ;
2014-09-19 06:01:05 +08:00
}
2020-01-06 19:06:51 +08:00
/* The variadic argument is not included in the stored argument count. */
proto_num_args = proto - > common . num_args + proto_is_variadic ;
fe_num_args = fe - > common . num_args + fe_is_variadic ;
num_args = MAX ( proto_num_args , fe_num_args ) ;
2014-09-19 06:01:05 +08:00
2019-05-09 16:41:06 +08:00
status = INHERITANCE_SUCCESS ;
2014-09-19 06:01:05 +08:00
for ( i = 0 ; i < num_args ; i + + ) {
2020-01-06 19:06:51 +08:00
zend_arg_info * proto_arg_info =
i < proto_num_args ? & proto - > common . arg_info [ i ] :
proto_is_variadic ? & proto - > common . arg_info [ proto_num_args - 1 ] : NULL ;
zend_arg_info * fe_arg_info =
i < fe_num_args ? & fe - > common . arg_info [ i ] :
fe_is_variadic ? & fe - > common . arg_info [ fe_num_args - 1 ] : NULL ;
if ( ! proto_arg_info ) {
/* A new (optional) argument has been added, which is fine. */
continue ;
}
if ( ! fe_arg_info ) {
/* An argument has been removed. This is considered illegal, because arity checks
* work based on a model where passing more than the declared number of parameters
* to a function is an error . */
return INHERITANCE_ERROR ;
2014-09-19 06:01:05 +08:00
}
2017-01-01 22:53:24 +08:00
2020-04-23 17:34:07 +08:00
local_status = zend_do_perform_arg_type_hint_check (
2020-10-15 18:46:07 +08:00
fe_scope , fe_arg_info , proto_scope , proto_arg_info ) ;
2019-06-26 06:00:31 +08:00
if ( UNEXPECTED ( local_status ! = INHERITANCE_SUCCESS ) ) {
if ( UNEXPECTED ( local_status = = INHERITANCE_ERROR ) ) {
return INHERITANCE_ERROR ;
}
ZEND_ASSERT ( local_status = = INHERITANCE_UNRESOLVED ) ;
2019-05-09 16:41:06 +08:00
status = INHERITANCE_UNRESOLVED ;
2016-04-06 22:17:10 +08:00
}
2015-01-09 04:40:36 +08:00
/* by-ref constraints on arguments are invariant */
2019-09-20 23:01:19 +08:00
if ( ZEND_ARG_SEND_MODE ( fe_arg_info ) ! = ZEND_ARG_SEND_MODE ( proto_arg_info ) ) {
2019-05-09 16:41:06 +08:00
return INHERITANCE_ERROR ;
2015-01-09 04:40:36 +08:00
}
}
2014-09-19 06:01:05 +08:00
2015-03-23 01:50:36 +08:00
/* Check return type compatibility, but only if the prototype already specifies
* a return type . Adding a new return type is always valid . */
if ( proto - > common . fn_flags & ZEND_ACC_HAS_RETURN_TYPE ) {
2021-05-06 21:05:21 +08:00
/* Removing a return type is not valid, unless the parent return type is tentative. */
2015-03-23 01:50:36 +08:00
if ( ! ( fe - > common . fn_flags & ZEND_ACC_HAS_RETURN_TYPE ) ) {
2021-05-17 16:16:47 +08:00
if ( ! ZEND_ARG_TYPE_IS_TENTATIVE ( & proto - > common . arg_info [ - 1 ] ) ) {
2021-05-06 21:05:21 +08:00
return INHERITANCE_ERROR ;
}
2021-05-17 16:16:47 +08:00
if ( status = = INHERITANCE_SUCCESS ) {
return INHERITANCE_WARNING ;
}
return status ;
2015-01-09 04:40:36 +08:00
}
2017-01-01 22:53:24 +08:00
2019-05-08 18:30:16 +08:00
local_status = zend_perform_covariant_type_check (
2021-06-30 21:45:42 +08:00
fe_scope , fe - > common . arg_info [ - 1 ] . type , proto_scope , proto - > common . arg_info [ - 1 ] . type ) ;
2019-06-26 06:00:31 +08:00
if ( UNEXPECTED ( local_status ! = INHERITANCE_SUCCESS ) ) {
2021-06-30 21:45:42 +08:00
if ( local_status = = INHERITANCE_ERROR
& & ZEND_ARG_TYPE_IS_TENTATIVE ( & proto - > common . arg_info [ - 1 ] ) ) {
local_status = INHERITANCE_WARNING ;
2019-06-26 06:00:31 +08:00
}
2021-06-30 21:45:42 +08:00
return local_status ;
2016-04-06 23:24:34 +08:00
}
2015-01-09 04:40:36 +08:00
}
2019-05-09 16:41:06 +08:00
return status ;
2015-01-09 04:40:36 +08:00
}
/* }}} */
2014-09-19 06:01:05 +08:00
2020-04-23 17:34:07 +08:00
static ZEND_COLD void zend_append_type_hint (
2020-08-28 21:41:27 +08:00
smart_str * str , zend_class_entry * scope , zend_arg_info * arg_info , bool return_hint ) /* {{{ */
2015-01-09 04:40:36 +08:00
{
2019-09-19 18:11:29 +08:00
if ( ZEND_TYPE_IS_SET ( arg_info - > type ) ) {
2020-04-23 17:34:07 +08:00
zend_string * type_str = zend_type_to_string_resolved ( arg_info - > type , scope ) ;
2019-09-19 18:11:29 +08:00
smart_str_append ( str , type_str ) ;
zend_string_release ( type_str ) ;
2015-01-09 04:40:36 +08:00
if ( ! return_hint ) {
smart_str_appendc ( str , ' ' ) ;
2014-09-19 06:01:05 +08:00
}
}
}
/* }}} */
2020-04-23 17:34:07 +08:00
static ZEND_COLD zend_string * zend_get_function_declaration (
const zend_function * fptr , zend_class_entry * scope ) /* {{{ */
2014-09-19 06:01:05 +08:00
{
2014-09-21 05:27:10 +08:00
smart_str str = { 0 } ;
2014-09-19 06:01:05 +08:00
if ( fptr - > op_array . fn_flags & ZEND_ACC_RETURN_REFERENCE ) {
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " & " ) ;
2014-09-19 06:01:05 +08:00
}
if ( fptr - > common . scope ) {
2021-02-10 23:00:00 +08:00
if ( fptr - > common . scope - > ce_flags & ZEND_ACC_ANON_CLASS ) {
/* cut off on NULL byte ... class@anonymous */
smart_str_appends ( & str , ZSTR_VAL ( fptr - > common . scope - > name ) ) ;
} else {
smart_str_appendl ( & str , ZSTR_VAL ( fptr - > common . scope - > name ) , ZSTR_LEN ( fptr - > common . scope - > name ) ) ;
}
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " :: " ) ;
2014-09-19 06:01:05 +08:00
}
2014-09-22 02:47:07 +08:00
smart_str_append ( & str , fptr - > common . function_name ) ;
2014-09-21 05:27:10 +08:00
smart_str_appendc ( & str , ' ( ' ) ;
2014-09-19 06:01:05 +08:00
if ( fptr - > common . arg_info ) {
2014-12-22 21:44:39 +08:00
uint32_t i , num_args , required ;
2014-09-19 06:01:05 +08:00
zend_arg_info * arg_info = fptr - > common . arg_info ;
required = fptr - > common . required_num_args ;
2014-12-22 21:44:39 +08:00
num_args = fptr - > common . num_args ;
if ( fptr - > common . fn_flags & ZEND_ACC_VARIADIC ) {
num_args + + ;
}
for ( i = 0 ; i < num_args ; ) {
2020-04-23 17:34:07 +08:00
zend_append_type_hint ( & str , scope , arg_info , 0 ) ;
2014-09-19 06:01:05 +08:00
2019-09-20 23:01:19 +08:00
if ( ZEND_ARG_SEND_MODE ( arg_info ) ) {
2014-09-21 05:27:10 +08:00
smart_str_appendc ( & str , ' & ' ) ;
2014-09-19 06:01:05 +08:00
}
2019-09-20 23:01:19 +08:00
if ( ZEND_ARG_IS_VARIADIC ( arg_info ) ) {
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " ... " ) ;
2014-09-19 06:01:05 +08:00
}
2014-09-21 05:27:10 +08:00
smart_str_appendc ( & str , ' $ ' ) ;
2020-04-07 22:05:33 +08:00
if ( fptr - > type = = ZEND_INTERNAL_FUNCTION ) {
smart_str_appends ( & str , ( ( zend_internal_arg_info * ) arg_info ) - > name ) ;
2014-09-19 06:01:05 +08:00
} else {
2020-04-07 22:05:33 +08:00
smart_str_appendl ( & str , ZSTR_VAL ( arg_info - > name ) , ZSTR_LEN ( arg_info - > name ) ) ;
2014-09-19 06:01:05 +08:00
}
2014-09-21 05:27:10 +08:00
2019-09-20 23:01:19 +08:00
if ( i > = required & & ! ZEND_ARG_IS_VARIADIC ( arg_info ) ) {
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " = " ) ;
2020-04-06 17:02:13 +08:00
if ( fptr - > type = = ZEND_INTERNAL_FUNCTION ) {
if ( ( ( zend_internal_arg_info * ) arg_info ) - > default_value ) {
smart_str_appends ( & str , ( ( zend_internal_arg_info * ) arg_info ) - > default_value ) ;
} else {
smart_str_appends ( & str , " <default> " ) ;
}
} else {
2014-09-19 06:01:05 +08:00
zend_op * precv = NULL ;
{
uint32_t idx = i ;
2014-09-21 05:27:10 +08:00
zend_op * op = fptr - > op_array . opcodes ;
zend_op * end = op + fptr - > op_array . last ;
2014-09-19 06:01:05 +08:00
+ + idx ;
while ( op < end ) {
if ( ( op - > opcode = = ZEND_RECV | | op - > opcode = = ZEND_RECV_INIT )
& & op - > op1 . num = = ( zend_ulong ) idx )
{
precv = op ;
}
+ + op ;
}
}
if ( precv & & precv - > opcode = = ZEND_RECV_INIT & & precv - > op2_type ! = IS_UNUSED ) {
2017-10-04 21:53:01 +08:00
zval * zv = RT_CONSTANT ( precv , precv - > op2 ) ;
2014-09-19 06:01:05 +08:00
2017-10-10 15:11:05 +08:00
if ( Z_TYPE_P ( zv ) = = IS_FALSE ) {
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " false " ) ;
2014-09-19 06:01:05 +08:00
} else if ( Z_TYPE_P ( zv ) = = IS_TRUE ) {
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " true " ) ;
2014-09-19 06:01:05 +08:00
} else if ( Z_TYPE_P ( zv ) = = IS_NULL ) {
2020-04-09 15:46:44 +08:00
smart_str_appends ( & str , " null " ) ;
2014-09-19 06:01:05 +08:00
} else if ( Z_TYPE_P ( zv ) = = IS_STRING ) {
2014-09-21 05:27:10 +08:00
smart_str_appendc ( & str , ' \' ' ) ;
smart_str_appendl ( & str , Z_STRVAL_P ( zv ) , MIN ( Z_STRLEN_P ( zv ) , 10 ) ) ;
2014-09-19 06:01:05 +08:00
if ( Z_STRLEN_P ( zv ) > 10 ) {
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " ... " ) ;
2014-09-19 06:01:05 +08:00
}
2014-09-21 05:27:10 +08:00
smart_str_appendc ( & str , ' \' ' ) ;
2014-09-19 06:01:05 +08:00
} else if ( Z_TYPE_P ( zv ) = = IS_ARRAY ) {
2020-04-09 15:46:44 +08:00
if ( zend_hash_num_elements ( Z_ARRVAL_P ( zv ) ) = = 0 ) {
smart_str_appends ( & str , " [] " ) ;
} else {
smart_str_appends ( & str , " [...] " ) ;
}
2014-09-19 06:01:05 +08:00
} else if ( Z_TYPE_P ( zv ) = = IS_CONSTANT_AST ) {
2017-10-10 15:11:05 +08:00
zend_ast * ast = Z_ASTVAL_P ( zv ) ;
if ( ast - > kind = = ZEND_AST_CONSTANT ) {
smart_str_append ( & str , zend_ast_get_constant_name ( ast ) ) ;
2021-04-09 22:48:28 +08:00
} else if ( ast - > kind = = ZEND_AST_CLASS_CONST ) {
smart_str_append ( & str , zend_ast_get_str ( ast - > child [ 0 ] ) ) ;
smart_str_appends ( & str , " :: " ) ;
smart_str_append ( & str , zend_ast_get_str ( ast - > child [ 1 ] ) ) ;
2017-10-10 15:11:05 +08:00
} else {
smart_str_appends ( & str , " <expression> " ) ;
}
2014-09-19 06:01:05 +08:00
} else {
2017-11-16 22:09:32 +08:00
zend_string * tmp_zv_str ;
zend_string * zv_str = zval_get_tmp_string ( zv , & tmp_zv_str ) ;
2014-09-22 02:47:07 +08:00
smart_str_append ( & str , zv_str ) ;
2017-11-16 22:09:32 +08:00
zend_tmp_string_release ( tmp_zv_str ) ;
2014-09-19 06:01:05 +08:00
}
}
}
}
2014-12-22 21:44:39 +08:00
if ( + + i < num_args ) {
2014-09-21 05:27:10 +08:00
smart_str_appends ( & str , " , " ) ;
2014-09-19 06:01:05 +08:00
}
arg_info + + ;
}
}
2014-09-21 05:27:10 +08:00
smart_str_appendc ( & str , ' ) ' ) ;
2015-01-09 04:40:36 +08:00
if ( fptr - > common . fn_flags & ZEND_ACC_HAS_RETURN_TYPE ) {
smart_str_appends ( & str , " : " ) ;
2020-04-23 17:34:07 +08:00
zend_append_type_hint ( & str , scope , fptr - > common . arg_info - 1 , 1 ) ;
2015-01-09 04:40:36 +08:00
}
2014-09-21 05:27:10 +08:00
smart_str_0 ( & str ) ;
return str . s ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
2021-08-12 20:34:48 +08:00
static zend_always_inline zend_string * func_filename ( const zend_function * fn ) {
return fn - > common . type = = ZEND_USER_FUNCTION ? fn - > op_array . filename : NULL ;
}
2019-05-27 17:39:56 +08:00
static zend_always_inline uint32_t func_lineno ( const zend_function * fn ) {
2019-03-27 20:02:28 +08:00
return fn - > common . type = = ZEND_USER_FUNCTION ? fn - > op_array . line_start : 0 ;
}
2019-05-08 18:30:16 +08:00
static void ZEND_COLD emit_incompatible_method_error (
2020-10-15 18:46:07 +08:00
const zend_function * child , zend_class_entry * child_scope ,
const zend_function * parent , zend_class_entry * parent_scope ,
2019-09-25 19:21:13 +08:00
inheritance_status status ) {
2020-04-23 17:34:07 +08:00
zend_string * parent_prototype = zend_get_function_declaration ( parent , parent_scope ) ;
2020-10-15 18:46:07 +08:00
zend_string * child_prototype = zend_get_function_declaration ( child , child_scope ) ;
2019-05-08 18:30:16 +08:00
if ( status = = INHERITANCE_UNRESOLVED ) {
2022-07-08 18:30:23 +08:00
// TODO Improve error message if first unresolved class is present in child and parent?
2019-09-25 19:21:13 +08:00
/* Fetch the first unresolved class from registered autoloads */
zend_string * unresolved_class = NULL ;
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY ( CG ( delayed_autoloads ) , unresolved_class ) {
2019-09-25 19:21:13 +08:00
break ;
} ZEND_HASH_FOREACH_END ( ) ;
ZEND_ASSERT ( unresolved_class ) ;
2021-08-12 20:34:48 +08:00
zend_error_at ( E_COMPILE_ERROR , func_filename ( child ) , func_lineno ( child ) ,
2019-05-08 18:30:16 +08:00
" Could not check compatibility between %s and %s, because class %s is not available " ,
ZSTR_VAL ( child_prototype ) , ZSTR_VAL ( parent_prototype ) , ZSTR_VAL ( unresolved_class ) ) ;
2021-05-06 21:05:21 +08:00
} else if ( status = = INHERITANCE_WARNING ) {
zend_attribute * return_type_will_change_attribute = zend_get_attribute_str (
child - > common . attributes ,
" returntypewillchange " ,
sizeof ( " returntypewillchange " ) - 1
) ;
if ( ! return_type_will_change_attribute ) {
2021-08-12 20:34:48 +08:00
zend_error_at ( E_DEPRECATED , func_filename ( child ) , func_lineno ( child ) ,
2021-06-28 20:56:21 +08:00
" Return type of %s should either be compatible with %s, "
2021-09-03 00:38:52 +08:00
" or the #[ \\ ReturnTypeWillChange] attribute should be used to temporarily suppress the notice " ,
2021-05-06 21:05:21 +08:00
ZSTR_VAL ( child_prototype ) , ZSTR_VAL ( parent_prototype ) ) ;
2021-05-28 16:19:23 +08:00
if ( EG ( exception ) ) {
zend_exception_uncaught_error (
" During inheritance of %s " , ZSTR_VAL ( parent_scope - > name ) ) ;
}
2021-05-06 21:05:21 +08:00
}
2019-05-08 18:30:16 +08:00
} else {
2021-08-12 20:34:48 +08:00
zend_error_at ( E_COMPILE_ERROR , func_filename ( child ) , func_lineno ( child ) ,
2019-05-24 15:33:58 +08:00
" Declaration of %s must be compatible with %s " ,
ZSTR_VAL ( child_prototype ) , ZSTR_VAL ( parent_prototype ) ) ;
2019-05-08 18:30:16 +08:00
}
zend_string_efree ( child_prototype ) ;
zend_string_efree ( parent_prototype ) ;
}
2019-06-25 21:37:42 +08:00
static void perform_delayable_implementation_check (
2020-10-15 18:46:07 +08:00
zend_class_entry * ce ,
const zend_function * fe , zend_class_entry * fe_scope ,
2020-04-23 17:34:07 +08:00
const zend_function * proto , zend_class_entry * proto_scope )
2019-06-25 21:37:42 +08:00
{
2020-10-15 18:46:07 +08:00
inheritance_status status =
zend_do_perform_implementation_check ( fe , fe_scope , proto , proto_scope ) ;
2019-06-25 21:37:42 +08:00
if ( UNEXPECTED ( status ! = INHERITANCE_SUCCESS ) ) {
if ( EXPECTED ( status = = INHERITANCE_UNRESOLVED ) ) {
2020-10-15 18:46:07 +08:00
add_compatibility_obligation ( ce , fe , fe_scope , proto , proto_scope ) ;
2019-06-26 06:00:31 +08:00
} else {
2021-05-06 21:05:21 +08:00
ZEND_ASSERT ( status = = INHERITANCE_ERROR | | status = = INHERITANCE_WARNING ) ;
2020-10-15 18:46:07 +08:00
emit_incompatible_method_error ( fe , fe_scope , proto , proto_scope , status ) ;
2019-06-25 21:37:42 +08:00
}
}
}
2020-04-23 17:34:07 +08:00
static zend_always_inline inheritance_status do_inheritance_check_on_method_ex (
2020-10-15 18:46:07 +08:00
zend_function * child , zend_class_entry * child_scope ,
zend_function * parent , zend_class_entry * parent_scope ,
zend_class_entry * ce , zval * child_zv ,
2021-01-15 19:30:54 +08:00
bool check_visibility , bool check_only , bool checked ) /* {{{ */
2014-09-19 06:01:05 +08:00
{
uint32_t child_flags ;
uint32_t parent_flags = parent - > common . fn_flags ;
2019-06-26 05:32:22 +08:00
zend_function * proto ;
2014-09-19 06:01:05 +08:00
2020-04-17 01:53:13 +08:00
if ( UNEXPECTED ( ( parent_flags & ZEND_ACC_PRIVATE ) & & ! ( parent_flags & ZEND_ACC_ABSTRACT ) & & ! ( parent_flags & ZEND_ACC_CTOR ) ) ) {
if ( ! check_only ) {
child - > common . fn_flags | = ZEND_ACC_CHANGED ;
}
/* The parent method is private and not an abstract so we don't need to check any inheritance rules */
return INHERITANCE_SUCCESS ;
}
2019-06-27 07:13:06 +08:00
if ( ! checked & & UNEXPECTED ( parent_flags & ZEND_ACC_FINAL ) ) {
if ( check_only ) {
return INHERITANCE_ERROR ;
}
2021-08-12 20:34:48 +08:00
zend_error_at_noreturn ( E_COMPILE_ERROR , func_filename ( child ) , func_lineno ( child ) ,
2019-03-27 20:02:28 +08:00
" Cannot override final method %s::%s() " ,
ZEND_FN_SCOPE_NAME ( parent ) , ZSTR_VAL ( child - > common . function_name ) ) ;
2014-09-19 06:01:05 +08:00
}
child_flags = child - > common . fn_flags ;
/* You cannot change from static to non static and vice versa.
*/
2019-06-27 07:13:06 +08:00
if ( ! checked & & UNEXPECTED ( ( child_flags & ZEND_ACC_STATIC ) ! = ( parent_flags & ZEND_ACC_STATIC ) ) ) {
if ( check_only ) {
return INHERITANCE_ERROR ;
}
2018-07-09 21:28:13 +08:00
if ( child_flags & ZEND_ACC_STATIC ) {
2021-08-12 20:34:48 +08:00
zend_error_at_noreturn ( E_COMPILE_ERROR , func_filename ( child ) , func_lineno ( child ) ,
2019-03-27 20:02:28 +08:00
" Cannot make non static method %s::%s() static in class %s " ,
ZEND_FN_SCOPE_NAME ( parent ) , ZSTR_VAL ( child - > common . function_name ) , ZEND_FN_SCOPE_NAME ( child ) ) ;
2014-09-19 06:01:05 +08:00
} else {
2021-08-12 20:34:48 +08:00
zend_error_at_noreturn ( E_COMPILE_ERROR , func_filename ( child ) , func_lineno ( child ) ,
2019-03-27 20:02:28 +08:00
" Cannot make static method %s::%s() non static in class %s " ,
ZEND_FN_SCOPE_NAME ( parent ) , ZSTR_VAL ( child - > common . function_name ) , ZEND_FN_SCOPE_NAME ( child ) ) ;
2014-09-19 06:01:05 +08:00
}
}
/* Disallow making an inherited method abstract. */
2019-06-27 07:13:06 +08:00
if ( ! checked & & UNEXPECTED ( ( child_flags & ZEND_ACC_ABSTRACT ) > ( parent_flags & ZEND_ACC_ABSTRACT ) ) ) {
if ( check_only ) {
return INHERITANCE_ERROR ;
}
2021-08-12 20:34:48 +08:00
zend_error_at_noreturn ( E_COMPILE_ERROR , func_filename ( child ) , func_lineno ( child ) ,
2019-03-27 20:02:28 +08:00
" Cannot make non abstract method %s::%s() abstract in class %s " ,
ZEND_FN_SCOPE_NAME ( parent ) , ZSTR_VAL ( child - > common . function_name ) , ZEND_FN_SCOPE_NAME ( child ) ) ;
2014-09-19 06:01:05 +08:00
}
2019-06-27 07:13:06 +08:00
if ( ! check_only & & ( parent_flags & ( ZEND_ACC_PRIVATE | ZEND_ACC_CHANGED ) ) ) {
2014-09-19 06:01:05 +08:00
child - > common . fn_flags | = ZEND_ACC_CHANGED ;
}
2019-06-26 05:32:22 +08:00
proto = parent - > common . prototype ?
parent - > common . prototype : parent ;
if ( parent_flags & ZEND_ACC_CTOR ) {
/* ctors only have a prototype if is abstract (or comes from an interface) */
/* and if that is the case, we want to check inheritance against it */
if ( ! ( proto - > common . fn_flags & ZEND_ACC_ABSTRACT ) ) {
2019-06-27 07:13:06 +08:00
return INHERITANCE_SUCCESS ;
2019-06-26 05:32:22 +08:00
}
parent = proto ;
}
2020-01-09 22:04:33 +08:00
if ( ! check_only & & child - > common . prototype ! = proto & & child_zv ) {
2019-06-26 05:32:22 +08:00
do {
2021-02-22 19:28:02 +08:00
if ( child - > common . scope ! = ce & & child - > type = = ZEND_USER_FUNCTION ) {
2019-06-26 05:32:22 +08:00
if ( ce - > ce_flags & ZEND_ACC_INTERFACE ) {
/* Few parent interfaces contain the same method */
2018-09-05 18:16:10 +08:00
break ;
2020-01-09 22:04:33 +08:00
} else {
2019-06-26 05:32:22 +08:00
/* op_array wasn't duplicated yet */
zend_function * new_function = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_op_array ) ) ;
memcpy ( new_function , child , sizeof ( zend_op_array ) ) ;
Z_PTR_P ( child_zv ) = child = new_function ;
2018-09-05 18:16:10 +08:00
}
}
2019-06-26 05:32:22 +08:00
child - > common . prototype = proto ;
} while ( 0 ) ;
}
2018-09-05 18:16:10 +08:00
2019-06-26 05:32:22 +08:00
/* Prevent derived classes from restricting access that was available in parent classes (except deriving from non-abstract ctors) */
2020-01-09 22:04:33 +08:00
if ( ! checked & & check_visibility
& & ( child_flags & ZEND_ACC_PPP_MASK ) > ( parent_flags & ZEND_ACC_PPP_MASK ) ) {
2019-06-27 07:13:06 +08:00
if ( check_only ) {
return INHERITANCE_ERROR ;
}
2021-08-12 20:34:48 +08:00
zend_error_at_noreturn ( E_COMPILE_ERROR , func_filename ( child ) , func_lineno ( child ) ,
2019-06-26 05:32:22 +08:00
" Access level to %s::%s() must be %s (as in class %s)%s " ,
ZEND_FN_SCOPE_NAME ( child ) , ZSTR_VAL ( child - > common . function_name ) , zend_visibility_string ( parent_flags ) , ZEND_FN_SCOPE_NAME ( parent ) , ( parent_flags & ZEND_ACC_PUBLIC ) ? " " : " or weaker " ) ;
}
2019-06-27 07:13:06 +08:00
if ( ! checked ) {
if ( check_only ) {
2020-10-15 18:46:07 +08:00
return zend_do_perform_implementation_check ( child , child_scope , parent , parent_scope ) ;
2019-06-27 07:13:06 +08:00
}
2020-10-15 18:46:07 +08:00
perform_delayable_implementation_check ( ce , child , child_scope , parent , parent_scope ) ;
2019-06-27 07:13:06 +08:00
}
return INHERITANCE_SUCCESS ;
}
/* }}} */
2020-04-23 17:34:07 +08:00
static zend_never_inline void do_inheritance_check_on_method (
2020-10-15 18:46:07 +08:00
zend_function * child , zend_class_entry * child_scope ,
zend_function * parent , zend_class_entry * parent_scope ,
2021-01-15 19:30:54 +08:00
zend_class_entry * ce , zval * child_zv , bool check_visibility )
2019-06-27 07:13:06 +08:00
{
2020-10-15 18:46:07 +08:00
do_inheritance_check_on_method_ex ( child , child_scope , parent , parent_scope , ce , child_zv , check_visibility , 0 , 0 ) ;
2014-09-19 06:01:05 +08:00
}
2021-01-15 19:30:54 +08:00
static zend_always_inline void do_inherit_method ( zend_string * key , zend_function * parent , zend_class_entry * ce , bool is_interface , bool checked ) /* { { { */
2014-09-19 06:01:05 +08:00
{
2021-07-21 07:07:17 +08:00
zval * child = zend_hash_find_known_hash ( & ce - > function_table , key ) ;
2014-09-19 06:01:05 +08:00
2015-03-05 07:10:38 +08:00
if ( child ) {
2016-01-13 16:41:57 +08:00
zend_function * func = ( zend_function * ) Z_PTR_P ( child ) ;
2019-06-27 18:11:08 +08:00
if ( is_interface & & UNEXPECTED ( func = = parent ) ) {
2018-10-03 19:35:36 +08:00
/* The same method in interface may be inherited few times */
2019-06-27 18:11:08 +08:00
return ;
2018-10-03 17:32:33 +08:00
}
2019-06-27 18:11:08 +08:00
if ( checked ) {
2020-01-09 22:04:33 +08:00
do_inheritance_check_on_method_ex (
2020-10-15 18:46:07 +08:00
func , func - > common . scope , parent , parent - > common . scope , ce , child ,
2020-04-23 17:34:07 +08:00
/* check_visibility */ 1 , 0 , checked ) ;
2019-06-27 07:13:06 +08:00
} else {
2020-04-23 17:34:07 +08:00
do_inheritance_check_on_method (
2020-10-15 18:46:07 +08:00
func , func - > common . scope , parent , parent - > common . scope , ce , child ,
/* check_visibility */ 1 ) ;
2019-06-27 18:11:08 +08:00
}
} else {
2019-06-27 07:13:06 +08:00
2019-06-27 18:11:08 +08:00
if ( is_interface | | ( parent - > common . fn_flags & ( ZEND_ACC_ABSTRACT ) ) ) {
ce - > ce_flags | = ZEND_ACC_IMPLICIT_ABSTRACT_CLASS ;
}
2021-02-22 19:28:02 +08:00
parent = zend_duplicate_function ( parent , ce ) ;
2019-06-27 07:13:06 +08:00
2019-06-27 18:11:08 +08:00
if ( ! is_interface ) {
2019-06-27 07:13:06 +08:00
_zend_hash_append_ptr ( & ce - > function_table , key , parent ) ;
2019-06-27 18:11:08 +08:00
} else {
zend_hash_add_new_ptr ( & ce - > function_table , key , parent ) ;
2019-06-27 07:13:06 +08:00
}
2019-06-27 18:11:08 +08:00
}
2019-06-27 07:13:06 +08:00
}
/* }}} */
2019-10-17 19:01:02 +08:00
inheritance_status property_types_compatible (
const zend_property_info * parent_info , const zend_property_info * child_info ) {
2019-09-20 23:01:19 +08:00
if ( ZEND_TYPE_PURE_MASK ( parent_info - > type ) = = ZEND_TYPE_PURE_MASK ( child_info - > type )
& & ZEND_TYPE_NAME ( parent_info - > type ) = = ZEND_TYPE_NAME ( child_info - > type ) ) {
2019-10-17 19:01:02 +08:00
return INHERITANCE_SUCCESS ;
2019-01-07 19:28:51 +08:00
}
2019-09-25 19:21:13 +08:00
if ( ZEND_TYPE_IS_SET ( parent_info - > type ) ! = ZEND_TYPE_IS_SET ( child_info - > type ) ) {
2019-10-17 19:01:02 +08:00
return INHERITANCE_ERROR ;
2019-01-07 19:28:51 +08:00
}
2019-09-25 19:21:13 +08:00
/* Perform a covariant type check in both directions to determined invariance. */
inheritance_status status1 = zend_perform_covariant_type_check (
2021-06-30 21:45:42 +08:00
child_info - > ce , child_info - > type , parent_info - > ce , parent_info - > type ) ;
2019-09-25 19:21:13 +08:00
inheritance_status status2 = zend_perform_covariant_type_check (
2021-06-30 21:45:42 +08:00
parent_info - > ce , parent_info - > type , child_info - > ce , child_info - > type ) ;
2019-09-25 19:21:13 +08:00
if ( status1 = = INHERITANCE_SUCCESS & & status2 = = INHERITANCE_SUCCESS ) {
2019-10-17 19:01:02 +08:00
return INHERITANCE_SUCCESS ;
2019-01-07 19:28:51 +08:00
}
2019-09-25 19:21:13 +08:00
if ( status1 = = INHERITANCE_ERROR | | status2 = = INHERITANCE_ERROR ) {
return INHERITANCE_ERROR ;
2019-10-17 19:01:02 +08:00
}
2019-12-04 18:07:22 +08:00
ZEND_ASSERT ( status1 = = INHERITANCE_UNRESOLVED | | status2 = = INHERITANCE_UNRESOLVED ) ;
2019-09-25 19:21:13 +08:00
return INHERITANCE_UNRESOLVED ;
2019-10-17 19:01:02 +08:00
}
static void emit_incompatible_property_error (
const zend_property_info * child , const zend_property_info * parent ) {
2019-10-17 19:42:47 +08:00
zend_string * type_str = zend_type_to_string_resolved ( parent - > type , parent - > ce ) ;
2019-10-17 19:01:02 +08:00
zend_error_noreturn ( E_COMPILE_ERROR ,
2019-10-17 19:42:47 +08:00
" Type of %s::$%s must be %s (as in class %s) " ,
2019-10-17 19:01:02 +08:00
ZSTR_VAL ( child - > ce - > name ) ,
2019-12-09 15:48:33 +08:00
zend_get_unmangled_property_name ( child - > name ) ,
2019-10-17 19:42:47 +08:00
ZSTR_VAL ( type_str ) ,
2019-10-17 19:01:02 +08:00
ZSTR_VAL ( parent - > ce - > name ) ) ;
2019-01-07 19:28:51 +08:00
}
2015-03-04 22:41:01 +08:00
static void do_inherit_property ( zend_property_info * parent_info , zend_string * key , zend_class_entry * ce ) /* { { { */
2014-09-19 06:01:05 +08:00
{
2021-07-21 07:07:17 +08:00
zval * child = zend_hash_find_known_hash ( & ce - > properties_info , key ) ;
2015-03-05 04:06:07 +08:00
zend_property_info * child_info ;
2014-09-19 06:01:05 +08:00
2015-03-05 04:06:07 +08:00
if ( UNEXPECTED ( child ) ) {
child_info = Z_PTR_P ( child ) ;
2018-09-12 16:43:15 +08:00
if ( parent_info - > flags & ( ZEND_ACC_PRIVATE | ZEND_ACC_CHANGED ) ) {
2014-09-19 06:01:05 +08:00
child_info - > flags | = ZEND_ACC_CHANGED ;
2018-09-12 16:43:15 +08:00
}
if ( ! ( parent_info - > flags & ZEND_ACC_PRIVATE ) ) {
2015-03-05 07:10:38 +08:00
if ( UNEXPECTED ( ( parent_info - > flags & ZEND_ACC_STATIC ) ! = ( child_info - > flags & ZEND_ACC_STATIC ) ) ) {
2015-03-04 22:41:01 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Cannot redeclare %s%s::$%s as %s%s::$%s " ,
2020-01-22 00:19:07 +08:00
( parent_info - > flags & ZEND_ACC_STATIC ) ? " static " : " non static " , ZSTR_VAL ( parent_info - > ce - > name ) , ZSTR_VAL ( key ) ,
2015-06-30 18:59:27 +08:00
( child_info - > flags & ZEND_ACC_STATIC ) ? " static " : " non static " , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( key ) ) ;
2015-03-04 22:41:01 +08:00
}
2021-06-02 16:02:56 +08:00
if ( UNEXPECTED ( ( child_info - > flags & ZEND_ACC_READONLY ) ! = ( parent_info - > flags & ZEND_ACC_READONLY ) ) ) {
zend_error_noreturn ( E_COMPILE_ERROR ,
" Cannot redeclare %s property %s::$%s as %s %s::$%s " ,
parent_info - > flags & ZEND_ACC_READONLY ? " readonly " : " non-readonly " ,
ZSTR_VAL ( parent_info - > ce - > name ) , ZSTR_VAL ( key ) ,
child_info - > flags & ZEND_ACC_READONLY ? " readonly " : " non-readonly " ,
ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( key ) ) ;
}
2015-03-04 22:41:01 +08:00
2015-03-05 07:10:38 +08:00
if ( UNEXPECTED ( ( child_info - > flags & ZEND_ACC_PPP_MASK ) > ( parent_info - > flags & ZEND_ACC_PPP_MASK ) ) ) {
2020-01-22 00:19:07 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Access level to %s::$%s must be %s (as in class %s)%s " , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( key ) , zend_visibility_string ( parent_info - > flags ) , ZSTR_VAL ( parent_info - > ce - > name ) , ( parent_info - > flags & ZEND_ACC_PUBLIC ) ? " " : " or weaker " ) ;
2015-03-04 22:41:01 +08:00
} else if ( ( child_info - > flags & ZEND_ACC_STATIC ) = = 0 ) {
int parent_num = OBJ_PROP_TO_NUM ( parent_info - > offset ) ;
int child_num = OBJ_PROP_TO_NUM ( child_info - > offset ) ;
2016-05-06 14:48:58 +08:00
/* Don't keep default properties in GC (they may be freed by opcache) */
2016-04-19 06:37:24 +08:00
zval_ptr_dtor_nogc ( & ( ce - > default_properties_table [ parent_num ] ) ) ;
2015-03-04 22:41:01 +08:00
ce - > default_properties_table [ parent_num ] = ce - > default_properties_table [ child_num ] ;
ZVAL_UNDEF ( & ce - > default_properties_table [ child_num ] ) ;
child_info - > offset = parent_info - > offset ;
}
2019-01-07 19:28:51 +08:00
if ( UNEXPECTED ( ZEND_TYPE_IS_SET ( parent_info - > type ) ) ) {
2019-10-17 19:01:02 +08:00
inheritance_status status = property_types_compatible ( parent_info , child_info ) ;
if ( status = = INHERITANCE_ERROR ) {
emit_incompatible_property_error ( child_info , parent_info ) ;
}
if ( status = = INHERITANCE_UNRESOLVED ) {
add_property_compatibility_obligation ( ce , child_info , parent_info ) ;
2019-01-07 19:28:51 +08:00
}
} else if ( UNEXPECTED ( ZEND_TYPE_IS_SET ( child_info - > type ) & & ! ZEND_TYPE_IS_SET ( parent_info - > type ) ) ) {
zend_error_noreturn ( E_COMPILE_ERROR ,
" Type of %s::$%s must not be defined (as in class %s) " ,
ZSTR_VAL ( ce - > name ) ,
ZSTR_VAL ( key ) ,
2020-01-22 00:19:07 +08:00
ZSTR_VAL ( parent_info - > ce - > name ) ) ;
2019-01-07 19:28:51 +08:00
}
2015-03-04 22:41:01 +08:00
}
} else {
2021-04-30 02:31:24 +08:00
_zend_hash_append_ptr ( & ce - > properties_info , key , parent_info ) ;
2014-09-19 06:01:05 +08:00
}
}
/* }}} */
2014-12-14 06:06:14 +08:00
static inline void do_implement_interface ( zend_class_entry * ce , zend_class_entry * iface ) /* { { { */
2014-09-19 06:01:05 +08:00
{
2014-12-14 06:06:14 +08:00
if ( ! ( ce - > ce_flags & ZEND_ACC_INTERFACE ) & & iface - > interface_gets_implemented & & iface - > interface_gets_implemented ( iface , ce ) = = FAILURE ) {
2022-03-10 21:41:07 +08:00
zend_error_noreturn ( E_CORE_ERROR , " %s %s could not implement interface %s " , zend_get_object_type_uc ( ce ) , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( iface - > name ) ) ;
2014-09-19 06:01:05 +08:00
}
2019-09-11 22:27:28 +08:00
/* This should be prevented by the class lookup logic. */
ZEND_ASSERT ( ce ! = iface ) ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
2018-08-23 22:16:28 +08:00
static void zend_do_inherit_interfaces ( zend_class_entry * ce , const zend_class_entry * iface ) /* { { { */
2014-09-19 06:01:05 +08:00
{
/* expects interface to be contained in ce's interface list already */
uint32_t i , ce_num , if_num = iface - > num_interfaces ;
zend_class_entry * entry ;
ce_num = ce - > num_interfaces ;
if ( ce - > type = = ZEND_INTERNAL_CLASS ) {
ce - > interfaces = ( zend_class_entry * * ) realloc ( ce - > interfaces , sizeof ( zend_class_entry * ) * ( ce_num + if_num ) ) ;
} else {
ce - > interfaces = ( zend_class_entry * * ) erealloc ( ce - > interfaces , sizeof ( zend_class_entry * ) * ( ce_num + if_num ) ) ;
}
/* Inherit the interfaces, only if they're not already inherited by the class */
while ( if_num - - ) {
entry = iface - > interfaces [ if_num ] ;
for ( i = 0 ; i < ce_num ; i + + ) {
if ( ce - > interfaces [ i ] = = entry ) {
break ;
}
}
if ( i = = ce_num ) {
ce - > interfaces [ ce - > num_interfaces + + ] = entry ;
}
}
2019-05-27 17:39:56 +08:00
ce - > ce_flags | = ZEND_ACC_RESOLVED_INTERFACES ;
2014-09-19 06:01:05 +08:00
/* and now call the implementing handlers */
while ( ce_num < ce - > num_interfaces ) {
2014-12-14 06:06:14 +08:00
do_implement_interface ( ce , ce - > interfaces [ ce_num + + ] ) ;
2014-09-19 06:01:05 +08:00
}
}
/* }}} */
2015-12-08 17:40:42 +08:00
static void do_inherit_class_constant ( zend_string * name , zend_class_constant * parent_const , zend_class_entry * ce ) /* { { { */
2014-09-19 06:01:05 +08:00
{
2021-07-21 07:07:17 +08:00
zval * zv = zend_hash_find_known_hash ( & ce - > constants_table , name ) ;
2017-11-24 19:01:19 +08:00
zend_class_constant * c ;
2015-12-08 17:40:42 +08:00
2017-11-24 19:01:19 +08:00
if ( zv ! = NULL ) {
c = ( zend_class_constant * ) Z_PTR_P ( zv ) ;
2021-04-28 20:51:54 +08:00
if ( UNEXPECTED ( ( ZEND_CLASS_CONST_FLAGS ( c ) & ZEND_ACC_PPP_MASK ) > ( ZEND_CLASS_CONST_FLAGS ( parent_const ) & ZEND_ACC_PPP_MASK ) ) ) {
2015-12-08 17:40:42 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Access level to %s::%s must be %s (as in class %s)%s " ,
2021-04-28 20:51:54 +08:00
ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( name ) , zend_visibility_string ( ZEND_CLASS_CONST_FLAGS ( parent_const ) ) , ZSTR_VAL ( parent_const - > ce - > name ) , ( ZEND_CLASS_CONST_FLAGS ( parent_const ) & ZEND_ACC_PUBLIC ) ? " " : " or weaker " ) ;
2015-03-05 04:06:07 +08:00
}
2021-07-07 03:38:44 +08:00
if ( UNEXPECTED ( ( ZEND_CLASS_CONST_FLAGS ( parent_const ) & ZEND_ACC_FINAL ) ) ) {
zend_error_noreturn (
E_COMPILE_ERROR , " %s::%s cannot override final constant %s::%s " ,
ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( name ) , ZSTR_VAL ( parent_const - > ce - > name ) , ZSTR_VAL ( name )
) ;
}
2021-04-28 20:51:54 +08:00
} else if ( ! ( ZEND_CLASS_CONST_FLAGS ( parent_const ) & ZEND_ACC_PRIVATE ) ) {
2017-10-10 15:11:05 +08:00
if ( Z_TYPE ( parent_const - > value ) = = IS_CONSTANT_AST ) {
2015-03-05 04:06:07 +08:00
ce - > ce_flags & = ~ ZEND_ACC_CONSTANTS_UPDATED ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ce - > ce_flags | = ZEND_ACC_HAS_AST_CONSTANTS ;
if ( ce - > parent - > ce_flags & ZEND_ACC_IMMUTABLE ) {
c = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_class_constant ) ) ;
memcpy ( c , parent_const , sizeof ( zend_class_constant ) ) ;
parent_const = c ;
}
2014-09-19 06:01:05 +08:00
}
2017-10-31 06:20:38 +08:00
if ( ce - > type & ZEND_INTERNAL_CLASS ) {
c = pemalloc ( sizeof ( zend_class_constant ) , 1 ) ;
memcpy ( c , parent_const , sizeof ( zend_class_constant ) ) ;
parent_const = c ;
}
2017-10-31 04:13:10 +08:00
_zend_hash_append_ptr ( & ce - > constants_table , name , parent_const ) ;
2014-09-19 06:01:05 +08:00
}
}
/* }}} */
2019-01-07 19:28:51 +08:00
void zend_build_properties_info_table ( zend_class_entry * ce )
{
zend_property_info * * table , * prop ;
2019-02-15 20:42:37 +08:00
size_t size ;
2019-01-07 19:28:51 +08:00
if ( ce - > default_properties_count = = 0 ) {
return ;
}
ZEND_ASSERT ( ce - > properties_info_table = = NULL ) ;
2019-02-15 20:42:37 +08:00
size = sizeof ( zend_property_info * ) * ce - > default_properties_count ;
2019-01-07 19:28:51 +08:00
if ( ce - > type = = ZEND_USER_CLASS ) {
2019-02-15 20:42:37 +08:00
ce - > properties_info_table = table = zend_arena_alloc ( & CG ( arena ) , size ) ;
2019-01-07 19:28:51 +08:00
} else {
2019-02-15 20:42:37 +08:00
ce - > properties_info_table = table = pemalloc ( size , 1 ) ;
2019-01-07 19:28:51 +08:00
}
2019-02-15 20:42:37 +08:00
/* Dead slots may be left behind during inheritance. Make sure these are NULLed out. */
memset ( table , 0 , size ) ;
2019-01-07 19:28:51 +08:00
if ( ce - > parent & & ce - > parent - > default_properties_count ! = 0 ) {
zend_property_info * * parent_table = ce - > parent - > properties_info_table ;
memcpy (
table , parent_table ,
sizeof ( zend_property_info * ) * ce - > parent - > default_properties_count
) ;
/* Child did not add any new properties, we are done */
if ( ce - > default_properties_count = = ce - > parent - > default_properties_count ) {
return ;
}
}
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_PTR ( & ce - > properties_info , prop ) {
2019-01-07 19:28:51 +08:00
if ( prop - > ce = = ce & & ( prop - > flags & ZEND_ACC_STATIC ) = = 0 ) {
table [ OBJ_PROP_TO_NUM ( prop - > offset ) ] = prop ;
}
} ZEND_HASH_FOREACH_END ( ) ;
}
2021-01-15 19:30:54 +08:00
ZEND_API void zend_do_inheritance_ex ( zend_class_entry * ce , zend_class_entry * parent_ce , bool checked ) /* { { { */
2014-09-19 06:01:05 +08:00
{
zend_property_info * property_info ;
zend_function * func ;
zend_string * key ;
2015-03-05 07:10:38 +08:00
if ( UNEXPECTED ( ce - > ce_flags & ZEND_ACC_INTERFACE ) ) {
2014-12-06 12:42:52 +08:00
/* Interface can only inherit other interfaces */
2015-03-05 07:10:38 +08:00
if ( UNEXPECTED ( ! ( parent_ce - > ce_flags & ZEND_ACC_INTERFACE ) ) ) {
2021-07-27 15:42:37 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Interface %s cannot extend class %s " , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( parent_ce - > name ) ) ;
2014-12-06 12:42:52 +08:00
}
2015-03-05 04:06:07 +08:00
} else if ( UNEXPECTED ( parent_ce - > ce_flags & ( ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_FINAL ) ) ) {
2014-12-06 12:42:52 +08:00
/* Class must not extend a final class */
if ( parent_ce - > ce_flags & ZEND_ACC_FINAL ) {
2021-07-27 15:42:37 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Class %s cannot extend final class %s " , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( parent_ce - > name ) ) ;
}
/* Class declaration must not extend traits or interfaces */
if ( ( parent_ce - > ce_flags & ZEND_ACC_INTERFACE ) | | ( parent_ce - > ce_flags & ZEND_ACC_TRAIT ) ) {
zend_error_noreturn ( E_COMPILE_ERROR , " Class %s cannot extend %s %s " ,
ZSTR_VAL ( ce - > name ) , parent_ce - > ce_flags & ZEND_ACC_INTERFACE ? " interface " : " trait " , ZSTR_VAL ( parent_ce - > name )
) ;
2014-12-06 12:42:52 +08:00
}
2014-09-19 06:01:05 +08:00
}
2022-05-17 02:40:23 +08:00
if ( UNEXPECTED ( ( ce - > ce_flags & ZEND_ACC_READONLY_CLASS ) ! = ( parent_ce - > ce_flags & ZEND_ACC_READONLY_CLASS ) ) ) {
zend_error_noreturn ( E_COMPILE_ERROR , " %s class %s cannot extend %s class %s " ,
ce - > ce_flags & ZEND_ACC_READONLY_CLASS ? " Readonly " : " Non-readonly " , ZSTR_VAL ( ce - > name ) ,
parent_ce - > ce_flags & ZEND_ACC_READONLY_CLASS ? " readonly " : " non-readonly " , ZSTR_VAL ( parent_ce - > name )
) ;
}
2018-09-18 16:41:40 +08:00
if ( ce - > parent_name ) {
2018-08-24 05:20:57 +08:00
zend_string_release_ex ( ce - > parent_name , 0 ) ;
}
2014-09-19 06:01:05 +08:00
ce - > parent = parent_ce ;
2019-05-27 17:39:56 +08:00
ce - > ce_flags | = ZEND_ACC_RESOLVED_PARENT ;
2014-12-06 12:42:52 +08:00
2014-09-19 06:01:05 +08:00
/* Inherit properties */
if ( parent_ce - > default_properties_count ) {
2015-04-22 17:53:54 +08:00
zval * src , * dst , * end ;
2014-09-19 06:01:05 +08:00
if ( ce - > default_properties_count ) {
2015-04-22 17:53:54 +08:00
zval * table = pemalloc ( sizeof ( zval ) * ( ce - > default_properties_count + parent_ce - > default_properties_count ) , ce - > type = = ZEND_INTERNAL_CLASS ) ;
src = ce - > default_properties_table + ce - > default_properties_count ;
end = table + parent_ce - > default_properties_count ;
dst = end + ce - > default_properties_count ;
ce - > default_properties_table = table ;
do {
dst - - ;
src - - ;
2019-10-24 22:36:25 +08:00
ZVAL_COPY_VALUE_PROP ( dst , src ) ;
2015-04-22 17:53:54 +08:00
} while ( dst ! = end ) ;
pefree ( src , ce - > type = = ZEND_INTERNAL_CLASS ) ;
end = ce - > default_properties_table ;
} else {
end = pemalloc ( sizeof ( zval ) * parent_ce - > default_properties_count , ce - > type = = ZEND_INTERNAL_CLASS ) ;
dst = end + parent_ce - > default_properties_count ;
ce - > default_properties_table = end ;
}
src = parent_ce - > default_properties_table + parent_ce - > default_properties_count ;
2017-10-31 04:13:10 +08:00
if ( UNEXPECTED ( parent_ce - > type ! = ce - > type ) ) {
/* User class extends internal */
do {
dst - - ;
src - - ;
2019-10-24 22:36:25 +08:00
ZVAL_COPY_OR_DUP_PROP ( dst , src ) ;
2017-10-10 15:11:05 +08:00
if ( Z_OPT_TYPE_P ( dst ) = = IS_CONSTANT_AST ) {
2014-09-19 06:01:05 +08:00
ce - > ce_flags & = ~ ZEND_ACC_CONSTANTS_UPDATED ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ce - > ce_flags | = ZEND_ACC_HAS_AST_PROPERTIES ;
2014-09-19 06:01:05 +08:00
}
continue ;
2017-10-31 04:13:10 +08:00
} while ( dst ! = end ) ;
} else {
do {
dst - - ;
src - - ;
2019-10-24 22:36:25 +08:00
ZVAL_COPY_PROP ( dst , src ) ;
2017-10-31 04:13:10 +08:00
if ( Z_OPT_TYPE_P ( dst ) = = IS_CONSTANT_AST ) {
ce - > ce_flags & = ~ ZEND_ACC_CONSTANTS_UPDATED ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ce - > ce_flags | = ZEND_ACC_HAS_AST_PROPERTIES ;
2017-10-31 04:13:10 +08:00
}
continue ;
} while ( dst ! = end ) ;
}
2014-09-19 06:01:05 +08:00
ce - > default_properties_count + = parent_ce - > default_properties_count ;
}
2015-04-22 17:53:54 +08:00
if ( parent_ce - > default_static_members_count ) {
zval * src , * dst , * end ;
if ( ce - > default_static_members_count ) {
zval * table = pemalloc ( sizeof ( zval ) * ( ce - > default_static_members_count + parent_ce - > default_static_members_count ) , ce - > type = = ZEND_INTERNAL_CLASS ) ;
src = ce - > default_static_members_table + ce - > default_static_members_count ;
end = table + parent_ce - > default_static_members_count ;
dst = end + ce - > default_static_members_count ;
ce - > default_static_members_table = table ;
do {
dst - - ;
src - - ;
ZVAL_COPY_VALUE ( dst , src ) ;
} while ( dst ! = end ) ;
pefree ( src , ce - > type = = ZEND_INTERNAL_CLASS ) ;
end = ce - > default_static_members_table ;
} else {
end = pemalloc ( sizeof ( zval ) * parent_ce - > default_static_members_count , ce - > type = = ZEND_INTERNAL_CLASS ) ;
dst = end + parent_ce - > default_static_members_count ;
ce - > default_static_members_table = end ;
}
2021-07-22 17:41:24 +08:00
src = parent_ce - > default_static_members_table + parent_ce - > default_static_members_count ;
do {
dst - - ;
src - - ;
if ( Z_TYPE_P ( src ) = = IS_INDIRECT ) {
ZVAL_INDIRECT ( dst , Z_INDIRECT_P ( src ) ) ;
} else {
ZVAL_INDIRECT ( dst , src ) ;
2017-10-31 04:13:10 +08:00
}
2021-07-22 17:41:24 +08:00
if ( Z_TYPE_P ( Z_INDIRECT_P ( dst ) ) = = IS_CONSTANT_AST ) {
ce - > ce_flags & = ~ ZEND_ACC_CONSTANTS_UPDATED ;
ce - > ce_flags | = ZEND_ACC_HAS_AST_STATICS ;
2018-10-19 18:21:31 +08:00
}
2021-07-22 17:41:24 +08:00
} while ( dst ! = end ) ;
2015-04-22 17:53:54 +08:00
ce - > default_static_members_count + = parent_ce - > default_static_members_count ;
2018-10-17 20:52:50 +08:00
if ( ! ZEND_MAP_PTR ( ce - > static_members_table ) ) {
2021-07-22 17:41:24 +08:00
if ( ce - > type = = ZEND_INTERNAL_CLASS & &
ce - > info . internal . module - > type = = MODULE_PERSISTENT ) {
2018-10-17 20:52:50 +08:00
ZEND_MAP_PTR_NEW ( ce - > static_members_table ) ;
2018-10-02 00:05:31 +08:00
}
2014-09-19 06:01:05 +08:00
}
}
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_PTR ( & ce - > properties_info , property_info ) {
2014-09-19 06:01:05 +08:00
if ( property_info - > ce = = ce ) {
if ( property_info - > flags & ZEND_ACC_STATIC ) {
property_info - > offset + = parent_ce - > default_static_members_count ;
} else {
2014-11-06 19:50:03 +08:00
property_info - > offset + = parent_ce - > default_properties_count * sizeof ( zval ) ;
2014-09-19 06:01:05 +08:00
}
}
} ZEND_HASH_FOREACH_END ( ) ;
2015-03-05 04:06:07 +08:00
if ( zend_hash_num_elements ( & parent_ce - > properties_info ) ) {
zend_hash_extend ( & ce - > properties_info ,
zend_hash_num_elements ( & ce - > properties_info ) +
zend_hash_num_elements ( & parent_ce - > properties_info ) , 0 ) ;
2015-03-04 22:41:01 +08:00
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & parent_ce - > properties_info , key , property_info ) {
2015-03-05 04:06:07 +08:00
do_inherit_property ( property_info , key , ce ) ;
} ZEND_HASH_FOREACH_END ( ) ;
}
2014-09-19 06:01:05 +08:00
2015-03-05 04:06:07 +08:00
if ( zend_hash_num_elements ( & parent_ce - > constants_table ) ) {
2015-12-08 17:40:42 +08:00
zend_class_constant * c ;
2015-03-05 04:06:07 +08:00
zend_hash_extend ( & ce - > constants_table ,
zend_hash_num_elements ( & ce - > constants_table ) +
zend_hash_num_elements ( & parent_ce - > constants_table ) , 0 ) ;
2014-09-19 06:01:05 +08:00
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & parent_ce - > constants_table , key , c ) {
2015-12-08 17:40:42 +08:00
do_inherit_class_constant ( key , c , ce ) ;
2015-03-05 04:06:07 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
}
if ( zend_hash_num_elements ( & parent_ce - > function_table ) ) {
zend_hash_extend ( & ce - > function_table ,
zend_hash_num_elements ( & ce - > function_table ) +
zend_hash_num_elements ( & parent_ce - > function_table ) , 0 ) ;
2019-06-27 07:13:06 +08:00
if ( checked ) {
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & parent_ce - > function_table , key , func ) {
2019-06-27 18:11:08 +08:00
do_inherit_method ( key , func , ce , 0 , 1 ) ;
} ZEND_HASH_FOREACH_END ( ) ;
2019-06-27 07:13:06 +08:00
} else {
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & parent_ce - > function_table , key , func ) {
2019-06-27 18:11:08 +08:00
do_inherit_method ( key , func , ce , 0 , 0 ) ;
2019-06-27 07:13:06 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
}
2015-03-05 04:06:07 +08:00
}
2014-09-19 06:01:05 +08:00
2014-12-14 06:06:14 +08:00
do_inherit_parent_constructor ( ce ) ;
2014-09-19 06:01:05 +08:00
2018-08-27 21:40:25 +08:00
if ( ce - > type = = ZEND_INTERNAL_CLASS ) {
2020-03-04 17:10:36 +08:00
if ( parent_ce - > num_interfaces ) {
zend_do_inherit_interfaces ( ce , parent_ce ) ;
}
2018-08-27 21:40:25 +08:00
if ( ce - > ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS ) {
ce - > ce_flags | = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS ;
}
2014-09-19 06:01:05 +08:00
}
2021-10-11 21:49:27 +08:00
ce - > ce_flags | = parent_ce - > ce_flags & ( ZEND_HAS_STATIC_IN_METHODS | ZEND_ACC_HAS_TYPE_HINTS | ZEND_ACC_USE_GUARDS | ZEND_ACC_NOT_SERIALIZABLE | ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES ) ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
2022-08-05 03:08:40 +08:00
static zend_always_inline bool check_trait_property_or_constant_value_compatibility ( zend_class_entry * ce , zval * op1 , zval * op2 ) /* { { { */
{
bool is_compatible ;
zval op1_tmp , op2_tmp ;
/* if any of the values is a constant, we try to resolve it */
if ( UNEXPECTED ( Z_TYPE_P ( op1 ) = = IS_CONSTANT_AST ) ) {
ZVAL_COPY_OR_DUP ( & op1_tmp , op1 ) ;
zval_update_constant_ex ( & op1_tmp , ce ) ;
op1 = & op1_tmp ;
}
if ( UNEXPECTED ( Z_TYPE_P ( op2 ) = = IS_CONSTANT_AST ) ) {
ZVAL_COPY_OR_DUP ( & op2_tmp , op2 ) ;
zval_update_constant_ex ( & op2_tmp , ce ) ;
op2 = & op2_tmp ;
}
is_compatible = fast_is_identical_function ( op1 , op2 ) ;
if ( op1 = = & op1_tmp ) {
zval_ptr_dtor_nogc ( & op1_tmp ) ;
}
if ( op2 = = & op2_tmp ) {
zval_ptr_dtor_nogc ( & op2_tmp ) ;
}
return is_compatible ;
}
/* }}} */
2021-07-07 03:38:44 +08:00
static bool do_inherit_constant_check (
zend_class_entry * ce , zend_class_constant * parent_constant , zend_string * name
) {
2021-07-21 07:07:17 +08:00
zval * zv = zend_hash_find_known_hash ( & ce - > constants_table , name ) ;
2021-07-07 03:38:44 +08:00
if ( zv = = NULL ) {
return true ;
}
2014-09-19 06:01:05 +08:00
2021-07-07 03:38:44 +08:00
zend_class_constant * old_constant = Z_PTR_P ( zv ) ;
2021-12-13 02:53:02 +08:00
if ( parent_constant - > ce ! = old_constant - > ce & & ( ZEND_CLASS_CONST_FLAGS ( parent_constant ) & ZEND_ACC_FINAL ) ) {
2021-07-07 03:38:44 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " %s::%s cannot override final constant %s::%s " ,
ZSTR_VAL ( old_constant - > ce - > name ) , ZSTR_VAL ( name ) ,
ZSTR_VAL ( parent_constant - > ce - > name ) , ZSTR_VAL ( name )
) ;
2014-09-19 06:01:05 +08:00
}
2021-07-07 03:38:44 +08:00
if ( old_constant - > ce ! = parent_constant - > ce & & old_constant - > ce ! = ce ) {
zend_error_noreturn ( E_COMPILE_ERROR ,
2022-03-10 21:41:07 +08:00
" %s %s inherits both %s::%s and %s::%s, which is ambiguous " ,
zend_get_object_type_uc ( ce ) ,
2021-07-07 03:38:44 +08:00
ZSTR_VAL ( ce - > name ) ,
ZSTR_VAL ( old_constant - > ce - > name ) , ZSTR_VAL ( name ) ,
ZSTR_VAL ( parent_constant - > ce - > name ) , ZSTR_VAL ( name ) ) ;
}
return false ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
2015-12-08 17:40:42 +08:00
static void do_inherit_iface_constant ( zend_string * name , zend_class_constant * c , zend_class_entry * ce , zend_class_entry * iface ) /* { { { */
2014-09-19 06:01:05 +08:00
{
2021-07-07 03:38:44 +08:00
if ( do_inherit_constant_check ( ce , c , name ) ) {
2016-01-21 13:30:41 +08:00
zend_class_constant * ct ;
2017-10-10 15:11:05 +08:00
if ( Z_TYPE ( c - > value ) = = IS_CONSTANT_AST ) {
2014-09-19 06:01:05 +08:00
ce - > ce_flags & = ~ ZEND_ACC_CONSTANTS_UPDATED ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ce - > ce_flags | = ZEND_ACC_HAS_AST_CONSTANTS ;
if ( iface - > ce_flags & ZEND_ACC_IMMUTABLE ) {
ct = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_class_constant ) ) ;
memcpy ( ct , c , sizeof ( zend_class_constant ) ) ;
c = ct ;
2022-05-11 16:43:01 +08:00
Z_CONSTANT_FLAGS ( c - > value ) | = CONST_OWNED ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
}
2014-09-19 06:01:05 +08:00
}
2017-10-31 06:20:38 +08:00
if ( ce - > type & ZEND_INTERNAL_CLASS ) {
ct = pemalloc ( sizeof ( zend_class_constant ) , 1 ) ;
memcpy ( ct , c , sizeof ( zend_class_constant ) ) ;
c = ct ;
}
2017-10-31 04:13:10 +08:00
zend_hash_update_ptr ( & ce - > constants_table , name , c ) ;
2014-09-19 06:01:05 +08:00
}
}
/* }}} */
2019-06-27 18:11:08 +08:00
static void do_interface_implementation ( zend_class_entry * ce , zend_class_entry * iface ) /* { { { */
{
zend_function * func ;
zend_string * key ;
zend_class_constant * c ;
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & iface - > constants_table , key , c ) {
2019-06-27 18:11:08 +08:00
do_inherit_iface_constant ( key , c , ce , iface ) ;
} ZEND_HASH_FOREACH_END ( ) ;
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & iface - > function_table , key , func ) {
2019-06-27 18:11:08 +08:00
do_inherit_method ( key , func , ce , 1 , 0 ) ;
} ZEND_HASH_FOREACH_END ( ) ;
do_implement_interface ( ce , iface ) ;
if ( iface - > num_interfaces ) {
zend_do_inherit_interfaces ( ce , iface ) ;
}
}
/* }}} */
2014-12-14 06:06:14 +08:00
ZEND_API void zend_do_implement_interface ( zend_class_entry * ce , zend_class_entry * iface ) /* { { { */
2014-09-19 06:01:05 +08:00
{
uint32_t i , ignore = 0 ;
uint32_t current_iface_num = ce - > num_interfaces ;
uint32_t parent_iface_num = ce - > parent ? ce - > parent - > num_interfaces : 0 ;
zend_string * key ;
2015-12-08 17:40:42 +08:00
zend_class_constant * c ;
2014-09-19 06:01:05 +08:00
2018-09-18 16:41:40 +08:00
ZEND_ASSERT ( ce - > ce_flags & ZEND_ACC_LINKED ) ;
2018-08-23 22:16:28 +08:00
2014-09-19 06:01:05 +08:00
for ( i = 0 ; i < ce - > num_interfaces ; i + + ) {
if ( ce - > interfaces [ i ] = = NULL ) {
memmove ( ce - > interfaces + i , ce - > interfaces + i + 1 , sizeof ( zend_class_entry * ) * ( - - ce - > num_interfaces - i ) ) ;
i - - ;
} else if ( ce - > interfaces [ i ] = = iface ) {
2015-03-05 07:10:38 +08:00
if ( EXPECTED ( i < parent_iface_num ) ) {
2014-09-19 06:01:05 +08:00
ignore = 1 ;
} else {
2015-06-30 18:59:27 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Class %s cannot implement previously implemented interface %s " , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( iface - > name ) ) ;
2014-09-19 06:01:05 +08:00
}
}
}
if ( ignore ) {
/* Check for attempt to redeclare interface constants */
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & iface - > constants_table , key , c ) {
2021-07-07 03:38:44 +08:00
do_inherit_constant_check ( ce , c , key ) ;
2014-09-19 06:01:05 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
} else {
if ( ce - > num_interfaces > = current_iface_num ) {
if ( ce - > type = = ZEND_INTERNAL_CLASS ) {
ce - > interfaces = ( zend_class_entry * * ) realloc ( ce - > interfaces , sizeof ( zend_class_entry * ) * ( + + current_iface_num ) ) ;
} else {
ce - > interfaces = ( zend_class_entry * * ) erealloc ( ce - > interfaces , sizeof ( zend_class_entry * ) * ( + + current_iface_num ) ) ;
}
}
ce - > interfaces [ ce - > num_interfaces + + ] = iface ;
2019-06-27 18:11:08 +08:00
do_interface_implementation ( ce , iface ) ;
2018-08-23 22:16:28 +08:00
}
}
/* }}} */
2019-09-11 21:31:04 +08:00
static void zend_do_implement_interfaces ( zend_class_entry * ce , zend_class_entry * * interfaces ) /* { { { */
2018-08-23 22:16:28 +08:00
{
2019-09-11 21:31:04 +08:00
zend_class_entry * iface ;
uint32_t num_parent_interfaces = ce - > parent ? ce - > parent - > num_interfaces : 0 ;
uint32_t num_interfaces = num_parent_interfaces ;
2018-08-23 22:16:28 +08:00
zend_string * key ;
zend_class_constant * c ;
uint32_t i , j ;
for ( i = 0 ; i < ce - > num_interfaces ; i + + ) {
2019-09-11 21:31:04 +08:00
iface = interfaces [ num_parent_interfaces + i ] ;
2019-05-27 17:39:56 +08:00
if ( ! ( iface - > ce_flags & ZEND_ACC_LINKED ) ) {
add_dependency_obligation ( ce , iface ) ;
2018-08-23 22:16:28 +08:00
}
if ( UNEXPECTED ( ! ( iface - > ce_flags & ZEND_ACC_INTERFACE ) ) ) {
efree ( interfaces ) ;
zend_error_noreturn ( E_ERROR , " %s cannot implement %s - it is not an interface " , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( iface - > name ) ) ;
return ;
}
for ( j = 0 ; j < num_interfaces ; j + + ) {
if ( interfaces [ j ] = = iface ) {
2019-09-11 21:31:04 +08:00
if ( j > = num_parent_interfaces ) {
2018-08-23 22:16:28 +08:00
efree ( interfaces ) ;
2022-03-10 21:41:07 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " %s %s cannot implement previously implemented interface %s " ,
zend_get_object_type_uc ( ce ) ,
ZSTR_VAL ( ce - > name ) ,
ZSTR_VAL ( iface - > name ) ) ;
2018-08-23 22:16:28 +08:00
return ;
}
/* skip duplications */
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & iface - > constants_table , key , c ) {
2021-07-07 03:38:44 +08:00
do_inherit_constant_check ( ce , c , key ) ;
2018-08-23 22:16:28 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
iface = NULL ;
break ;
}
}
if ( iface ) {
interfaces [ num_interfaces ] = iface ;
num_interfaces + + ;
}
}
2021-01-19 16:55:09 +08:00
if ( ! ( ce - > ce_flags & ZEND_ACC_CACHED ) ) {
for ( i = 0 ; i < ce - > num_interfaces ; i + + ) {
zend_string_release_ex ( ce - > interface_names [ i ] . name , 0 ) ;
zend_string_release_ex ( ce - > interface_names [ i ] . lc_name , 0 ) ;
}
efree ( ce - > interface_names ) ;
2018-08-23 22:16:28 +08:00
}
ce - > num_interfaces = num_interfaces ;
ce - > interfaces = interfaces ;
2019-05-27 17:39:56 +08:00
ce - > ce_flags | = ZEND_ACC_RESOLVED_INTERFACES ;
2018-08-23 22:16:28 +08:00
2020-03-04 17:10:36 +08:00
for ( i = 0 ; i < num_parent_interfaces ; i + + ) {
do_implement_interface ( ce , ce - > interfaces [ i ] ) ;
}
2020-02-28 19:16:41 +08:00
/* Note that new interfaces can be added during this loop due to interface inheritance.
* Use num_interfaces rather than ce - > num_interfaces to not re - process the new ones . */
for ( ; i < num_interfaces ; i + + ) {
2019-06-27 18:11:08 +08:00
do_interface_implementation ( ce , ce - > interfaces [ i ] ) ;
2018-08-23 22:16:28 +08:00
}
2014-09-19 06:01:05 +08:00
}
/* }}} */
2020-10-15 18:46:07 +08:00
static zend_class_entry * fixup_trait_scope ( const zend_function * fn , zend_class_entry * ce )
{
/* self in trait methods should be resolved to the using class, not the trait. */
return fn - > common . scope - > ce_flags & ZEND_ACC_TRAIT ? ce : fn - > common . scope ;
}
2020-01-09 22:04:33 +08:00
static void zend_add_trait_method ( zend_class_entry * ce , zend_string * name , zend_string * key , zend_function * fn ) /* { { { */
2014-09-19 06:01:05 +08:00
{
zend_function * existing_fn = NULL ;
zend_function * new_fn ;
if ( ( existing_fn = zend_hash_find_ptr ( & ce - > function_table , key ) ) ! = NULL ) {
2018-08-22 00:40:06 +08:00
/* if it is the same function with the same visibility and has not been assigned a class scope yet, regardless
* of where it is coming from there is no conflict and we do not need to add it again */
2018-08-04 00:30:03 +08:00
if ( existing_fn - > op_array . opcodes = = fn - > op_array . opcodes & &
2018-08-22 00:40:06 +08:00
( existing_fn - > common . fn_flags & ZEND_ACC_PPP_MASK ) = = ( fn - > common . fn_flags & ZEND_ACC_PPP_MASK ) & &
( existing_fn - > common . scope - > ce_flags & ZEND_ACC_TRAIT ) = = ZEND_ACC_TRAIT ) {
2017-10-01 04:24:38 +08:00
return ;
}
2020-01-09 22:04:33 +08:00
/* Abstract method signatures from the trait must be satisfied. */
if ( fn - > common . fn_flags & ZEND_ACC_ABSTRACT ) {
/* "abstract private" methods in traits were not available prior to PHP 8.
* As such , " abstract protected " was sometimes used to indicate trait requirements ,
* even though the " implementing " method was private . Do not check visibility
2020-04-23 17:34:07 +08:00
* requirements to maintain backwards - compatibility with such usage .
*/
do_inheritance_check_on_method (
2020-10-15 18:46:07 +08:00
existing_fn , fixup_trait_scope ( existing_fn , ce ) , fn , fixup_trait_scope ( fn , ce ) ,
ce , NULL , /* check_visibility */ 0 ) ;
2020-01-09 22:04:33 +08:00
return ;
}
2014-09-19 06:01:05 +08:00
if ( existing_fn - > common . scope = = ce ) {
/* members from the current class override trait methods */
return ;
2019-12-18 22:53:09 +08:00
} else if ( UNEXPECTED ( ( existing_fn - > common . scope - > ce_flags & ZEND_ACC_TRAIT )
& & ! ( existing_fn - > common . fn_flags & ZEND_ACC_ABSTRACT ) ) ) {
2014-09-19 06:01:05 +08:00
/* two traits can't define the same non-abstract method */
zend_error_noreturn ( E_COMPILE_ERROR , " Trait method %s::%s has not been applied as %s::%s, because of collision with %s::%s " ,
2015-06-30 18:59:27 +08:00
ZSTR_VAL ( fn - > common . scope - > name ) , ZSTR_VAL ( fn - > common . function_name ) ,
2020-03-02 18:07:57 +08:00
ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( name ) ,
2015-06-30 18:59:27 +08:00
ZSTR_VAL ( existing_fn - > common . scope - > name ) , ZSTR_VAL ( existing_fn - > common . function_name ) ) ;
2014-09-19 06:01:05 +08:00
} else {
2020-10-15 18:46:07 +08:00
/* Inherited members are overridden by members inserted by traits.
* Check whether the trait method fulfills the inheritance requirements . */
2020-04-23 17:34:07 +08:00
do_inheritance_check_on_method (
2020-10-15 18:46:07 +08:00
fn , fixup_trait_scope ( fn , ce ) , existing_fn , fixup_trait_scope ( existing_fn , ce ) ,
ce , NULL , /* check_visibility */ 1 ) ;
2014-09-19 06:01:05 +08:00
}
}
2018-08-30 04:40:17 +08:00
if ( UNEXPECTED ( fn - > type = = ZEND_INTERNAL_FUNCTION ) ) {
new_fn = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_internal_function ) ) ;
memcpy ( new_fn , fn , sizeof ( zend_internal_function ) ) ;
new_fn - > common . fn_flags | = ZEND_ACC_ARENA_ALLOCATED ;
} else {
new_fn = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_op_array ) ) ;
memcpy ( new_fn , fn , sizeof ( zend_op_array ) ) ;
2018-11-14 21:32:07 +08:00
new_fn - > op_array . fn_flags | = ZEND_ACC_TRAIT_CLONE ;
2018-10-17 20:52:50 +08:00
new_fn - > op_array . fn_flags & = ~ ZEND_ACC_IMMUTABLE ;
2018-08-30 04:40:17 +08:00
}
2020-03-02 18:07:57 +08:00
/* Reassign method name, in case it is an alias. */
new_fn - > common . function_name = name ;
2018-10-30 20:08:45 +08:00
function_add_ref ( new_fn ) ;
2014-09-19 06:01:05 +08:00
fn = zend_hash_update_ptr ( & ce - > function_table , key , new_fn ) ;
2020-07-20 20:56:48 +08:00
zend_add_magic_method ( ce , fn , key ) ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
static void zend_fixup_trait_method ( zend_function * fn , zend_class_entry * ce ) /* { { { */
{
if ( ( fn - > common . scope - > ce_flags & ZEND_ACC_TRAIT ) = = ZEND_ACC_TRAIT ) {
fn - > common . scope = ce ;
if ( fn - > common . fn_flags & ZEND_ACC_ABSTRACT ) {
ce - > ce_flags | = ZEND_ACC_IMPLICIT_ABSTRACT_CLASS ;
}
2015-05-04 10:35:55 +08:00
if ( fn - > type = = ZEND_USER_FUNCTION & & fn - > op_array . static_variables ) {
2014-09-19 06:01:05 +08:00
ce - > ce_flags | = ZEND_HAS_STATIC_IN_METHODS ;
}
}
}
/* }}} */
2020-01-09 22:04:33 +08:00
static void zend_traits_copy_functions ( zend_string * fnname , zend_function * fn , zend_class_entry * ce , HashTable * exclude_table , zend_class_entry * * aliases ) /* { { { */
2014-09-19 06:01:05 +08:00
{
zend_trait_alias * alias , * * alias_ptr ;
zend_string * lcname ;
zend_function fn_copy ;
2018-07-11 23:56:10 +08:00
int i ;
2014-09-19 06:01:05 +08:00
/* apply aliases which are qualified with a class name, there should not be any ambiguity */
if ( ce - > trait_aliases ) {
alias_ptr = ce - > trait_aliases ;
alias = * alias_ptr ;
2018-07-11 23:56:10 +08:00
i = 0 ;
2014-09-19 06:01:05 +08:00
while ( alias ) {
/* Scope unset or equal to the function we compare to, and the alias applies to fn */
if ( alias - > alias ! = NULL
2020-03-03 21:21:33 +08:00
& & fn - > common . scope = = aliases [ i ]
& & zend_string_equals_ci ( alias - > trait_method . method_name , fnname )
) {
2014-09-19 06:01:05 +08:00
fn_copy = * fn ;
2021-03-31 11:49:41 +08:00
/* if it is 0, no modifiers have been changed */
2014-09-19 06:01:05 +08:00
if ( alias - > modifiers ) {
2020-03-03 22:00:15 +08:00
fn_copy . common . fn_flags = alias - > modifiers | ( fn - > common . fn_flags & ~ ZEND_ACC_PPP_MASK ) ;
2014-09-19 06:01:05 +08:00
}
2014-12-24 20:04:51 +08:00
lcname = zend_string_tolower ( alias - > alias ) ;
2020-01-09 22:04:33 +08:00
zend_add_trait_method ( ce , alias - > alias , lcname , & fn_copy ) ;
2018-05-28 21:27:12 +08:00
zend_string_release_ex ( lcname , 0 ) ;
2014-09-19 06:01:05 +08:00
}
alias_ptr + + ;
alias = * alias_ptr ;
2018-07-11 23:56:10 +08:00
i + + ;
2014-09-19 06:01:05 +08:00
}
}
if ( exclude_table = = NULL | | zend_hash_find ( exclude_table , fnname ) = = NULL ) {
/* is not in hashtable, thus, function is not to be excluded */
2020-03-03 22:00:15 +08:00
memcpy ( & fn_copy , fn , fn - > type = = ZEND_USER_FUNCTION ? sizeof ( zend_op_array ) : sizeof ( zend_internal_function ) ) ;
2014-09-19 06:01:05 +08:00
/* apply aliases which have not alias name, just setting visibility */
if ( ce - > trait_aliases ) {
alias_ptr = ce - > trait_aliases ;
alias = * alias_ptr ;
2018-07-11 23:56:10 +08:00
i = 0 ;
2014-09-19 06:01:05 +08:00
while ( alias ) {
/* Scope unset or equal to the function we compare to, and the alias applies to fn */
if ( alias - > alias = = NULL & & alias - > modifiers ! = 0
2020-03-03 21:21:33 +08:00
& & fn - > common . scope = = aliases [ i ]
& & zend_string_equals_ci ( alias - > trait_method . method_name , fnname )
) {
2020-03-03 22:00:15 +08:00
fn_copy . common . fn_flags = alias - > modifiers | ( fn - > common . fn_flags & ~ ZEND_ACC_PPP_MASK ) ;
2014-09-19 06:01:05 +08:00
}
alias_ptr + + ;
alias = * alias_ptr ;
2018-07-11 23:56:10 +08:00
i + + ;
2014-09-19 06:01:05 +08:00
}
}
2020-01-09 22:04:33 +08:00
zend_add_trait_method ( ce , fn - > common . function_name , fnname , & fn_copy ) ;
2014-09-19 06:01:05 +08:00
}
}
/* }}} */
2018-08-23 07:02:26 +08:00
static uint32_t zend_check_trait_usage ( zend_class_entry * ce , zend_class_entry * trait , zend_class_entry * * traits ) /* { { { */
2014-09-19 06:01:05 +08:00
{
uint32_t i ;
2015-03-05 07:10:38 +08:00
if ( UNEXPECTED ( ( trait - > ce_flags & ZEND_ACC_TRAIT ) ! = ZEND_ACC_TRAIT ) ) {
2015-06-30 18:59:27 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Class %s is not a trait, Only traits may be used in 'as' and 'insteadof' statements " , ZSTR_VAL ( trait - > name ) ) ;
2018-07-11 23:56:10 +08:00
return 0 ;
2014-09-19 06:01:05 +08:00
}
for ( i = 0 ; i < ce - > num_traits ; i + + ) {
2018-08-23 07:02:26 +08:00
if ( traits [ i ] = = trait ) {
2018-07-11 23:56:10 +08:00
return i ;
2014-09-19 06:01:05 +08:00
}
}
2015-06-30 18:59:27 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Required Trait %s wasn't added to %s " , ZSTR_VAL ( trait - > name ) , ZSTR_VAL ( ce - > name ) ) ;
2018-07-11 23:56:10 +08:00
return 0 ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
2018-08-23 07:02:26 +08:00
static void zend_traits_init_trait_structures ( zend_class_entry * ce , zend_class_entry * * traits , HashTable * * * exclude_tables_ptr , zend_class_entry * * * aliases_ptr ) /* { { { */
2014-09-19 06:01:05 +08:00
{
size_t i , j = 0 ;
2015-03-05 21:06:59 +08:00
zend_trait_precedence * * precedences ;
2014-09-19 06:01:05 +08:00
zend_trait_precedence * cur_precedence ;
zend_trait_method_reference * cur_method_ref ;
2021-03-11 18:42:35 +08:00
zend_string * lc_trait_name ;
2014-09-19 06:01:05 +08:00
zend_string * lcname ;
2018-07-11 23:56:10 +08:00
HashTable * * exclude_tables = NULL ;
zend_class_entry * * aliases = NULL ;
zend_class_entry * trait ;
2014-09-19 06:01:05 +08:00
/* resolve class references */
if ( ce - > trait_precedences ) {
2018-07-11 23:56:10 +08:00
exclude_tables = ecalloc ( ce - > num_traits , sizeof ( HashTable * ) ) ;
2014-09-19 06:01:05 +08:00
i = 0 ;
2015-03-05 21:06:59 +08:00
precedences = ce - > trait_precedences ;
ce - > trait_precedences = NULL ;
while ( ( cur_precedence = precedences [ i ] ) ) {
2014-09-19 06:01:05 +08:00
/** Resolve classes for all precedence operations. */
2018-07-11 23:56:10 +08:00
cur_method_ref = & cur_precedence - > trait_method ;
2021-03-11 18:42:35 +08:00
lc_trait_name = zend_string_tolower ( cur_method_ref - > class_name ) ;
trait = zend_hash_find_ptr ( EG ( class_table ) , lc_trait_name ) ;
zend_string_release_ex ( lc_trait_name , 0 ) ;
2021-03-19 23:04:16 +08:00
if ( ! trait | | ! ( trait - > ce_flags & ZEND_ACC_LINKED ) ) {
2018-07-11 23:56:10 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Could not find trait %s " , ZSTR_VAL ( cur_method_ref - > class_name ) ) ;
}
2018-08-23 07:02:26 +08:00
zend_check_trait_usage ( ce , trait , traits ) ;
2018-07-11 23:56:10 +08:00
/** Ensure that the preferred method is actually available. */
lcname = zend_string_tolower ( cur_method_ref - > method_name ) ;
2018-09-19 18:54:37 +08:00
if ( ! zend_hash_exists ( & trait - > function_table , lcname ) ) {
2018-07-11 23:56:10 +08:00
zend_error_noreturn ( E_COMPILE_ERROR ,
" A precedence rule was defined for %s::%s but this method does not exist " ,
ZSTR_VAL ( trait - > name ) ,
ZSTR_VAL ( cur_method_ref - > method_name ) ) ;
}
2014-09-19 06:01:05 +08:00
2018-07-11 23:56:10 +08:00
/** With the other traits, we are more permissive.
We do not give errors for those . This allows to be more
defensive in such definitions .
However , we want to make sure that the insteadof declaration
is consistent in itself .
*/
2014-09-19 06:01:05 +08:00
2018-07-11 23:56:10 +08:00
for ( j = 0 ; j < cur_precedence - > num_excludes ; j + + ) {
zend_string * class_name = cur_precedence - > exclude_class_names [ j ] ;
2021-03-11 18:42:35 +08:00
zend_class_entry * exclude_ce ;
2018-07-11 23:56:10 +08:00
uint32_t trait_num ;
2014-09-19 06:01:05 +08:00
2021-03-11 18:42:35 +08:00
lc_trait_name = zend_string_tolower ( class_name ) ;
exclude_ce = zend_hash_find_ptr ( EG ( class_table ) , lc_trait_name ) ;
zend_string_release_ex ( lc_trait_name , 0 ) ;
if ( ! exclude_ce | | ! ( exclude_ce - > ce_flags & ZEND_ACC_LINKED ) ) {
2018-07-11 23:56:10 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Could not find trait %s " , ZSTR_VAL ( class_name ) ) ;
}
2018-08-23 07:02:26 +08:00
trait_num = zend_check_trait_usage ( ce , exclude_ce , traits ) ;
2018-07-11 23:56:10 +08:00
if ( ! exclude_tables [ trait_num ] ) {
ALLOC_HASHTABLE ( exclude_tables [ trait_num ] ) ;
zend_hash_init ( exclude_tables [ trait_num ] , 0 , NULL , NULL , 0 ) ;
}
if ( zend_hash_add_empty_element ( exclude_tables [ trait_num ] , lcname ) = = NULL ) {
zend_error_noreturn ( E_COMPILE_ERROR , " Failed to evaluate a trait precedence (%s). Method of trait %s was defined to be excluded multiple times " , ZSTR_VAL ( precedences [ i ] - > trait_method . method_name ) , ZSTR_VAL ( exclude_ce - > name ) ) ;
}
2014-09-19 06:01:05 +08:00
2018-07-11 23:56:10 +08:00
/* make sure that the trait method is not from a class mentioned in
exclude_from_classes , for consistency */
if ( trait = = exclude_ce ) {
zend_error_noreturn ( E_COMPILE_ERROR ,
" Inconsistent insteadof definition. "
" The method %s is to be used from %s, but %s is also on the exclude list " ,
ZSTR_VAL ( cur_method_ref - > method_name ) ,
ZSTR_VAL ( trait - > name ) ,
ZSTR_VAL ( trait - > name ) ) ;
2014-09-19 06:01:05 +08:00
}
}
2018-07-11 23:56:10 +08:00
zend_string_release_ex ( lcname , 0 ) ;
2014-09-19 06:01:05 +08:00
i + + ;
}
2015-03-05 21:06:59 +08:00
ce - > trait_precedences = precedences ;
2014-09-19 06:01:05 +08:00
}
if ( ce - > trait_aliases ) {
2018-07-11 23:56:10 +08:00
i = 0 ;
while ( ce - > trait_aliases [ i ] ) {
i + + ;
}
aliases = ecalloc ( i , sizeof ( zend_class_entry * ) ) ;
2014-09-19 06:01:05 +08:00
i = 0 ;
while ( ce - > trait_aliases [ i ] ) {
2020-03-03 21:21:33 +08:00
zend_trait_alias * cur_alias = ce - > trait_aliases [ i ] ;
cur_method_ref = & ce - > trait_aliases [ i ] - > trait_method ;
lcname = zend_string_tolower ( cur_method_ref - > method_name ) ;
if ( cur_method_ref - > class_name ) {
/* For all aliases with an explicit class name, resolve the class now. */
2021-03-11 18:42:35 +08:00
lc_trait_name = zend_string_tolower ( cur_method_ref - > class_name ) ;
trait = zend_hash_find_ptr ( EG ( class_table ) , lc_trait_name ) ;
zend_string_release_ex ( lc_trait_name , 0 ) ;
if ( ! trait | | ! ( trait - > ce_flags & ZEND_ACC_LINKED ) ) {
2015-06-30 18:59:27 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Could not find trait %s " , ZSTR_VAL ( cur_method_ref - > class_name ) ) ;
2014-09-19 06:01:05 +08:00
}
2018-08-23 07:02:26 +08:00
zend_check_trait_usage ( ce , trait , traits ) ;
2018-07-11 23:56:10 +08:00
aliases [ i ] = trait ;
2014-09-19 06:01:05 +08:00
2020-03-03 21:21:33 +08:00
/* And, ensure that the referenced method is resolvable, too. */
2018-09-19 18:54:37 +08:00
if ( ! zend_hash_exists ( & trait - > function_table , lcname ) ) {
2018-07-11 23:56:10 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " An alias was defined for %s::%s but this method does not exist " , ZSTR_VAL ( trait - > name ) , ZSTR_VAL ( cur_method_ref - > method_name ) ) ;
2014-09-19 06:01:05 +08:00
}
2020-03-03 21:21:33 +08:00
} else {
/* Find out which trait this method refers to. */
trait = NULL ;
for ( j = 0 ; j < ce - > num_traits ; j + + ) {
if ( traits [ j ] ) {
if ( zend_hash_exists ( & traits [ j ] - > function_table , lcname ) ) {
if ( ! trait ) {
trait = traits [ j ] ;
continue ;
}
2020-03-03 21:31:09 +08:00
zend_error_noreturn ( E_COMPILE_ERROR ,
" An alias was defined for method %s(), which exists in both %s and %s. Use %s::%s or %s::%s to resolve the ambiguity " ,
ZSTR_VAL ( cur_method_ref - > method_name ) ,
ZSTR_VAL ( trait - > name ) , ZSTR_VAL ( traits [ j ] - > name ) ,
ZSTR_VAL ( trait - > name ) , ZSTR_VAL ( cur_method_ref - > method_name ) ,
ZSTR_VAL ( traits [ j ] - > name ) , ZSTR_VAL ( cur_method_ref - > method_name ) ) ;
2020-03-03 21:21:33 +08:00
}
}
}
/* Non-absolute method reference refers to method that does not exist. */
if ( ! trait ) {
if ( cur_alias - > alias ) {
zend_error_noreturn ( E_COMPILE_ERROR ,
" An alias (%s) was defined for method %s(), but this method does not exist " ,
ZSTR_VAL ( cur_alias - > alias ) ,
ZSTR_VAL ( cur_alias - > trait_method . method_name ) ) ;
} else {
zend_error_noreturn ( E_COMPILE_ERROR ,
" The modifiers of the trait method %s() are changed, but this method does not exist. Error " ,
ZSTR_VAL ( cur_alias - > trait_method . method_name ) ) ;
}
}
aliases [ i ] = trait ;
2014-09-19 06:01:05 +08:00
}
2020-03-03 21:21:33 +08:00
zend_string_release_ex ( lcname , 0 ) ;
2014-09-19 06:01:05 +08:00
i + + ;
}
}
2018-07-11 23:56:10 +08:00
* exclude_tables_ptr = exclude_tables ;
* aliases_ptr = aliases ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
2018-08-23 07:02:26 +08:00
static void zend_do_traits_method_binding ( zend_class_entry * ce , zend_class_entry * * traits , HashTable * * exclude_tables , zend_class_entry * * aliases ) /* { { { */
2014-09-19 06:01:05 +08:00
{
uint32_t i ;
zend_string * key ;
zend_function * fn ;
2018-07-11 23:56:10 +08:00
if ( exclude_tables ) {
for ( i = 0 ; i < ce - > num_traits ; i + + ) {
2018-08-23 07:02:26 +08:00
if ( traits [ i ] ) {
/* copies functions, applies defined aliasing, and excludes unused trait methods */
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & traits [ i ] - > function_table , key , fn ) {
2020-01-09 22:04:33 +08:00
zend_traits_copy_functions ( key , fn , ce , exclude_tables [ i ] , aliases ) ;
2018-08-23 07:02:26 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
if ( exclude_tables [ i ] ) {
zend_hash_destroy ( exclude_tables [ i ] ) ;
FREE_HASHTABLE ( exclude_tables [ i ] ) ;
exclude_tables [ i ] = NULL ;
}
2018-07-11 23:56:10 +08:00
}
}
} else {
for ( i = 0 ; i < ce - > num_traits ; i + + ) {
2018-08-23 07:02:26 +08:00
if ( traits [ i ] ) {
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & traits [ i ] - > function_table , key , fn ) {
2020-01-09 22:04:33 +08:00
zend_traits_copy_functions ( key , fn , ce , NULL , aliases ) ;
2018-08-23 07:02:26 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
}
2014-09-19 06:01:05 +08:00
}
}
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_PTR ( & ce - > function_table , fn ) {
2014-09-19 06:01:05 +08:00
zend_fixup_trait_method ( fn , ce ) ;
} ZEND_HASH_FOREACH_END ( ) ;
}
/* }}} */
2022-08-05 03:08:40 +08:00
static zend_class_entry * find_first_constant_definition ( zend_class_entry * ce , zend_class_entry * * traits , size_t current_trait , zend_string * constant_name , zend_class_entry * colliding_ce ) /* { { { */
{
/* This function is used to show the place of the existing conflicting
* definition in error messages when conflicts occur . Since trait constants
* are flattened into the constants table of the composing class , and thus
* we lose information about which constant was defined in which trait , a
* process like this is needed to find the location of the first definition
* of the constant from traits .
*/
size_t i ;
if ( colliding_ce = = ce ) {
for ( i = 0 ; i < current_trait ; i + + ) {
if ( traits [ i ]
& & zend_hash_exists ( & traits [ i ] - > constants_table , constant_name ) ) {
return traits [ i ] ;
}
}
}
/* Traits don't have it, then the composing class (or trait) itself has it. */
return colliding_ce ;
}
/* }}} */
static bool do_trait_constant_check ( zend_class_entry * ce , zend_class_constant * trait_constant , zend_string * name , zend_class_entry * * traits , size_t current_trait ) /* { { { */
{
uint32_t flags_mask = ZEND_ACC_PPP_MASK | ZEND_ACC_FINAL ;
zval * zv = zend_hash_find_known_hash ( & ce - > constants_table , name ) ;
if ( zv = = NULL ) {
/* No existing constant of the same name, so this one can be added */
return true ;
}
zend_class_constant * existing_constant = Z_PTR_P ( zv ) ;
if ( ( ZEND_CLASS_CONST_FLAGS ( trait_constant ) & flags_mask ) ! = ( ZEND_CLASS_CONST_FLAGS ( existing_constant ) & flags_mask ) | |
! check_trait_property_or_constant_value_compatibility ( ce , & trait_constant - > value , & existing_constant - > value ) ) {
/* There is an existing constant of the same name, and it conflicts with the new one, so let's throw a fatal error */
zend_error_noreturn ( E_COMPILE_ERROR ,
" %s and %s define the same constant (%s) in the composition of %s. However, the definition differs and is considered incompatible. Class was composed " ,
ZSTR_VAL ( find_first_constant_definition ( ce , traits , current_trait , name , existing_constant - > ce ) - > name ) ,
ZSTR_VAL ( trait_constant - > ce - > name ) ,
ZSTR_VAL ( name ) ,
ZSTR_VAL ( ce - > name ) ) ;
}
/* There is an existing constant which is compatible with the new one, so no need to add it */
return false ;
}
/* }}} */
static void zend_do_traits_constant_binding ( zend_class_entry * ce , zend_class_entry * * traits ) /* { { { */
{
size_t i ;
for ( i = 0 ; i < ce - > num_traits ; i + + ) {
zend_string * constant_name ;
zend_class_constant * constant ;
if ( ! traits [ i ] ) {
continue ;
}
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & traits [ i ] - > constants_table , constant_name , constant ) {
if ( do_trait_constant_check ( ce , constant , constant_name , traits , i ) ) {
zend_class_constant * ct = NULL ;
ct = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_class_constant ) ) ;
memcpy ( ct , constant , sizeof ( zend_class_constant ) ) ;
constant = ct ;
if ( Z_TYPE ( constant - > value ) = = IS_CONSTANT_AST ) {
ce - > ce_flags & = ~ ZEND_ACC_CONSTANTS_UPDATED ;
ce - > ce_flags | = ZEND_ACC_HAS_AST_CONSTANTS ;
}
/* Unlike interface implementations and class inheritances,
* access control of the trait constants is done by the scope
* of the composing class . So let ' s replace the ce here .
*/
constant - > ce = ce ;
Z_TRY_ADDREF ( constant - > value ) ;
constant - > doc_comment = constant - > doc_comment ? zend_string_copy ( constant - > doc_comment ) : NULL ;
if ( constant - > attributes & & ( ! ( GC_FLAGS ( constant - > attributes ) & IS_ARRAY_IMMUTABLE ) ) ) {
GC_ADDREF ( constant - > attributes ) ;
}
zend_hash_update_ptr ( & ce - > constants_table , constant_name , constant ) ;
}
} ZEND_HASH_FOREACH_END ( ) ;
}
}
/* }}} */
static zend_class_entry * find_first_property_definition ( zend_class_entry * ce , zend_class_entry * * traits , size_t current_trait , zend_string * prop_name , zend_class_entry * colliding_ce ) /* { { { */
2014-09-19 06:01:05 +08:00
{
size_t i ;
2021-03-31 11:49:41 +08:00
if ( colliding_ce = = ce ) {
2014-09-19 06:01:05 +08:00
for ( i = 0 ; i < current_trait ; i + + ) {
2018-08-23 07:02:26 +08:00
if ( traits [ i ]
& & zend_hash_exists ( & traits [ i ] - > properties_info , prop_name ) ) {
return traits [ i ] ;
2014-09-19 06:01:05 +08:00
}
}
}
2021-03-31 11:49:41 +08:00
return colliding_ce ;
2014-09-19 06:01:05 +08:00
}
/* }}} */
2018-08-23 07:02:26 +08:00
static void zend_do_traits_property_binding ( zend_class_entry * ce , zend_class_entry * * traits ) /* { { { */
2014-09-19 06:01:05 +08:00
{
size_t i ;
zend_property_info * property_info ;
2021-03-31 11:49:41 +08:00
zend_property_info * colliding_prop ;
2020-05-25 02:57:00 +08:00
zend_property_info * new_prop ;
2014-09-19 06:01:05 +08:00
zend_string * prop_name ;
zval * prop_value ;
zend_string * doc_comment ;
/* In the following steps the properties are inserted into the property table
* for that , a very strict approach is applied :
* - check for compatibility , if not compatible with any property in class - > fatal
* - if compatible , then strict notice
*/
for ( i = 0 ; i < ce - > num_traits ; i + + ) {
2018-08-23 07:02:26 +08:00
if ( ! traits [ i ] ) {
continue ;
}
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & traits [ i ] - > properties_info , prop_name , property_info ) {
2021-03-26 19:14:32 +08:00
uint32_t flags = property_info - > flags ;
2014-09-19 06:01:05 +08:00
/* next: check for conflicts with current class */
2021-03-31 11:49:41 +08:00
if ( ( colliding_prop = zend_hash_find_ptr ( & ce - > properties_info , prop_name ) ) ! = NULL ) {
if ( ( colliding_prop - > flags & ZEND_ACC_PRIVATE ) & & colliding_prop - > ce ! = ce ) {
2014-09-19 06:01:05 +08:00
zend_hash_del ( & ce - > properties_info , prop_name ) ;
flags | = ZEND_ACC_CHANGED ;
} else {
2022-08-05 03:08:40 +08:00
bool is_compatible = false ;
2021-06-02 16:02:56 +08:00
uint32_t flags_mask = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC | ZEND_ACC_READONLY ;
2017-12-31 12:35:25 +08:00
2021-06-02 16:02:56 +08:00
if ( ( colliding_prop - > flags & flags_mask ) = = ( flags & flags_mask ) & &
2021-03-31 11:49:41 +08:00
property_types_compatible ( property_info , colliding_prop ) = = INHERITANCE_SUCCESS
2019-01-07 19:28:51 +08:00
) {
2017-09-27 08:31:03 +08:00
/* the flags are identical, thus, the properties may be compatible */
2017-12-02 00:08:11 +08:00
zval * op1 , * op2 ;
2017-09-27 08:31:03 +08:00
2014-09-19 06:01:05 +08:00
if ( flags & ZEND_ACC_STATIC ) {
2021-03-31 11:49:41 +08:00
op1 = & ce - > default_static_members_table [ colliding_prop - > offset ] ;
2018-08-23 07:02:26 +08:00
op2 = & traits [ i ] - > default_static_members_table [ property_info - > offset ] ;
2018-06-22 18:58:48 +08:00
ZVAL_DEINDIRECT ( op1 ) ;
ZVAL_DEINDIRECT ( op2 ) ;
2014-09-19 06:01:05 +08:00
} else {
2021-03-31 11:49:41 +08:00
op1 = & ce - > default_properties_table [ OBJ_PROP_TO_NUM ( colliding_prop - > offset ) ] ;
2018-08-23 07:02:26 +08:00
op2 = & traits [ i ] - > default_properties_table [ OBJ_PROP_TO_NUM ( property_info - > offset ) ] ;
2017-09-27 08:31:03 +08:00
}
2022-08-05 03:08:40 +08:00
is_compatible = check_trait_property_or_constant_value_compatibility ( ce , op1 , op2 ) ;
2014-09-19 06:01:05 +08:00
}
2022-08-05 03:08:40 +08:00
if ( ! is_compatible ) {
2014-09-19 06:01:05 +08:00
zend_error_noreturn ( E_COMPILE_ERROR ,
2022-08-21 16:29:51 +08:00
" %s and %s define the same property ($%s) in the composition of %s. However, the definition differs and is considered incompatible. Class was composed " ,
2022-08-05 03:08:40 +08:00
ZSTR_VAL ( find_first_property_definition ( ce , traits , i , prop_name , colliding_prop - > ce ) - > name ) ,
2015-06-30 18:59:27 +08:00
ZSTR_VAL ( property_info - > ce - > name ) ,
ZSTR_VAL ( prop_name ) ,
ZSTR_VAL ( ce - > name ) ) ;
2014-09-19 06:01:05 +08:00
}
2015-03-15 16:47:25 +08:00
continue ;
2014-09-19 06:01:05 +08:00
}
}
2022-08-21 16:29:51 +08:00
if ( ( ce - > ce_flags & ZEND_ACC_READONLY_CLASS ) & & ! ( property_info - > flags & ZEND_ACC_READONLY ) ) {
zend_error_noreturn ( E_COMPILE_ERROR ,
" Readonly class %s cannot use trait with a non-readonly property %s::$%s " ,
ZSTR_VAL ( ce - > name ) ,
ZSTR_VAL ( property_info - > ce - > name ) ,
ZSTR_VAL ( prop_name )
) ;
}
2014-09-19 06:01:05 +08:00
/* property not found, so lets add it */
if ( flags & ZEND_ACC_STATIC ) {
2018-08-23 07:02:26 +08:00
prop_value = & traits [ i ] - > default_static_members_table [ property_info - > offset ] ;
2018-06-22 18:58:48 +08:00
ZEND_ASSERT ( Z_TYPE_P ( prop_value ) ! = IS_INDIRECT ) ;
2014-09-19 06:01:05 +08:00
} else {
2018-08-23 07:02:26 +08:00
prop_value = & traits [ i ] - > default_properties_table [ OBJ_PROP_TO_NUM ( property_info - > offset ) ] ;
2014-09-19 06:01:05 +08:00
}
2017-11-01 10:25:10 +08:00
Z_TRY_ADDREF_P ( prop_value ) ;
2014-09-19 06:01:05 +08:00
doc_comment = property_info - > doc_comment ? zend_string_copy ( property_info - > doc_comment ) : NULL ;
2020-05-25 02:57:00 +08:00
2020-11-03 22:29:18 +08:00
zend_type type = property_info - > type ;
zend_type_copy_ctor ( & type , /* persistent */ 0 ) ;
new_prop = zend_declare_typed_property ( ce , prop_name , prop_value , flags , doc_comment , type ) ;
2020-05-25 02:57:00 +08:00
if ( property_info - > attributes ) {
new_prop - > attributes = property_info - > attributes ;
if ( ! ( GC_FLAGS ( new_prop - > attributes ) & IS_ARRAY_IMMUTABLE ) ) {
GC_ADDREF ( new_prop - > attributes ) ;
}
}
2014-09-19 06:01:05 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
}
}
/* }}} */
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
static void zend_do_bind_traits ( zend_class_entry * ce , zend_class_entry * * traits ) /* { { { */
2014-09-19 06:01:05 +08:00
{
2018-07-11 23:56:10 +08:00
HashTable * * exclude_tables ;
zend_class_entry * * aliases ;
2014-09-19 06:01:05 +08:00
2018-08-23 22:16:28 +08:00
ZEND_ASSERT ( ce - > num_traits > 0 ) ;
2014-09-19 06:01:05 +08:00
2021-03-31 11:49:41 +08:00
/* complete initialization of trait structures in ce */
2018-08-23 07:02:26 +08:00
zend_traits_init_trait_structures ( ce , traits , & exclude_tables , & aliases ) ;
2014-09-19 06:01:05 +08:00
/* first care about all methods to be flattened into the class */
2018-08-23 07:02:26 +08:00
zend_do_traits_method_binding ( ce , traits , exclude_tables , aliases ) ;
2014-09-19 06:01:05 +08:00
2018-07-11 23:56:10 +08:00
if ( aliases ) {
efree ( aliases ) ;
}
if ( exclude_tables ) {
efree ( exclude_tables ) ;
}
2014-09-19 06:01:05 +08:00
2022-08-05 03:08:40 +08:00
/* then flatten the constants and properties into it, to, mostly to notify developer about problems */
zend_do_traits_constant_binding ( ce , traits ) ;
2018-08-23 07:02:26 +08:00
zend_do_traits_property_binding ( ce , traits ) ;
2015-03-31 22:10:22 +08:00
}
/* }}} */
2018-08-27 17:56:17 +08:00
# define MAX_ABSTRACT_INFO_CNT 3
# define MAX_ABSTRACT_INFO_FMT "%s%s%s%s"
# define DISPLAY_ABSTRACT_FN(idx) \
ai . afn [ idx ] ? ZEND_FN_SCOPE_NAME ( ai . afn [ idx ] ) : " " , \
ai . afn [ idx ] ? " :: " : " " , \
ai . afn [ idx ] ? ZSTR_VAL ( ai . afn [ idx ] - > common . function_name ) : " " , \
ai . afn [ idx ] & & ai . afn [ idx + 1 ] ? " , " : ( ai . afn [ idx ] & & ai . cnt > MAX_ABSTRACT_INFO_CNT ? " , ... " : " " )
typedef struct _zend_abstract_info {
zend_function * afn [ MAX_ABSTRACT_INFO_CNT + 1 ] ;
int cnt ;
} zend_abstract_info ;
static void zend_verify_abstract_class_function ( zend_function * fn , zend_abstract_info * ai ) /* { { { */
{
2020-01-09 22:04:33 +08:00
if ( ai - > cnt < MAX_ABSTRACT_INFO_CNT ) {
ai - > afn [ ai - > cnt ] = fn ;
}
2021-07-27 16:16:09 +08:00
ai - > cnt + + ;
2018-08-27 17:56:17 +08:00
}
/* }}} */
void zend_verify_abstract_class ( zend_class_entry * ce ) /* { { { */
{
zend_function * func ;
zend_abstract_info ai ;
2021-01-15 19:30:54 +08:00
bool is_explicit_abstract = ( ce - > ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS ) ! = 0 ;
2022-03-10 21:41:07 +08:00
bool can_be_abstract = ( ce - > ce_flags & ZEND_ACC_ENUM ) = = 0 ;
2018-08-27 21:40:25 +08:00
memset ( & ai , 0 , sizeof ( ai ) ) ;
2018-08-27 17:56:17 +08:00
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_PTR ( & ce - > function_table , func ) {
2020-01-09 22:04:33 +08:00
if ( func - > common . fn_flags & ZEND_ACC_ABSTRACT ) {
/* If the class is explicitly abstract, we only check private abstract methods,
* because only they must be declared in the same class . */
if ( ! is_explicit_abstract | | ( func - > common . fn_flags & ZEND_ACC_PRIVATE ) ) {
zend_verify_abstract_class_function ( func , & ai ) ;
}
}
2018-08-27 21:40:25 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
2018-08-27 17:56:17 +08:00
2018-08-27 21:40:25 +08:00
if ( ai . cnt ) {
2022-03-10 21:41:07 +08:00
zend_error_noreturn ( E_ERROR , ! is_explicit_abstract & & can_be_abstract
? " %s %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods ( " MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT " ) "
: " %s %s must implement %d abstract private method%s ( " MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT " ) " ,
zend_get_object_type_uc ( ce ) ,
2018-08-27 21:40:25 +08:00
ZSTR_VAL ( ce - > name ) , ai . cnt ,
ai . cnt > 1 ? " s " : " " ,
DISPLAY_ABSTRACT_FN ( 0 ) ,
DISPLAY_ABSTRACT_FN ( 1 ) ,
DISPLAY_ABSTRACT_FN ( 2 )
) ;
} else {
/* now everything should be fine and an added ZEND_ACC_IMPLICIT_ABSTRACT_CLASS should be removed */
ce - > ce_flags & = ~ ZEND_ACC_IMPLICIT_ABSTRACT_CLASS ;
}
}
/* }}} */
2019-05-27 17:39:56 +08:00
typedef struct {
2019-10-17 19:01:02 +08:00
enum {
OBLIGATION_DEPENDENCY ,
OBLIGATION_COMPATIBILITY ,
OBLIGATION_PROPERTY_COMPATIBILITY
} type ;
2019-05-27 17:39:56 +08:00
union {
zend_class_entry * dependency_ce ;
struct {
2020-01-28 17:41:11 +08:00
/* Traits may use temporary on-stack functions during inheritance checks,
* so use copies of functions here as well . */
zend_function parent_fn ;
zend_function child_fn ;
2020-10-15 18:46:07 +08:00
zend_class_entry * child_scope ;
2020-04-23 17:34:07 +08:00
zend_class_entry * parent_scope ;
2019-05-27 17:39:56 +08:00
} ;
2019-10-17 19:01:02 +08:00
struct {
const zend_property_info * parent_prop ;
const zend_property_info * child_prop ;
} ;
2019-05-27 17:39:56 +08:00
} ;
} variance_obligation ;
static void variance_obligation_dtor ( zval * zv ) {
efree ( Z_PTR_P ( zv ) ) ;
}
static void variance_obligation_ht_dtor ( zval * zv ) {
zend_hash_destroy ( Z_PTR_P ( zv ) ) ;
FREE_HASHTABLE ( Z_PTR_P ( zv ) ) ;
}
static HashTable * get_or_init_obligations_for_class ( zend_class_entry * ce ) {
HashTable * ht ;
zend_ulong key ;
if ( ! CG ( delayed_variance_obligations ) ) {
ALLOC_HASHTABLE ( CG ( delayed_variance_obligations ) ) ;
zend_hash_init ( CG ( delayed_variance_obligations ) , 0 , NULL , variance_obligation_ht_dtor , 0 ) ;
}
key = ( zend_ulong ) ( uintptr_t ) ce ;
ht = zend_hash_index_find_ptr ( CG ( delayed_variance_obligations ) , key ) ;
if ( ht ) {
return ht ;
}
ALLOC_HASHTABLE ( ht ) ;
zend_hash_init ( ht , 0 , NULL , variance_obligation_dtor , 0 ) ;
zend_hash_index_add_new_ptr ( CG ( delayed_variance_obligations ) , key , ht ) ;
ce - > ce_flags | = ZEND_ACC_UNRESOLVED_VARIANCE ;
return ht ;
}
static void add_dependency_obligation ( zend_class_entry * ce , zend_class_entry * dependency_ce ) {
HashTable * obligations = get_or_init_obligations_for_class ( ce ) ;
variance_obligation * obligation = emalloc ( sizeof ( variance_obligation ) ) ;
obligation - > type = OBLIGATION_DEPENDENCY ;
obligation - > dependency_ce = dependency_ce ;
zend_hash_next_index_insert_ptr ( obligations , obligation ) ;
}
static void add_compatibility_obligation (
2020-10-15 18:46:07 +08:00
zend_class_entry * ce ,
const zend_function * child_fn , zend_class_entry * child_scope ,
const zend_function * parent_fn , zend_class_entry * parent_scope ) {
2019-05-27 17:39:56 +08:00
HashTable * obligations = get_or_init_obligations_for_class ( ce ) ;
variance_obligation * obligation = emalloc ( sizeof ( variance_obligation ) ) ;
obligation - > type = OBLIGATION_COMPATIBILITY ;
2020-01-30 18:55:38 +08:00
/* Copy functions, because they may be stack-allocated in the case of traits. */
if ( child_fn - > common . type = = ZEND_INTERNAL_FUNCTION ) {
memcpy ( & obligation - > child_fn , child_fn , sizeof ( zend_internal_function ) ) ;
} else {
memcpy ( & obligation - > child_fn , child_fn , sizeof ( zend_op_array ) ) ;
}
if ( parent_fn - > common . type = = ZEND_INTERNAL_FUNCTION ) {
memcpy ( & obligation - > parent_fn , parent_fn , sizeof ( zend_internal_function ) ) ;
} else {
memcpy ( & obligation - > parent_fn , parent_fn , sizeof ( zend_op_array ) ) ;
}
2020-10-15 18:46:07 +08:00
obligation - > child_scope = child_scope ;
2020-04-23 17:34:07 +08:00
obligation - > parent_scope = parent_scope ;
2019-05-27 17:39:56 +08:00
zend_hash_next_index_insert_ptr ( obligations , obligation ) ;
}
2019-10-17 19:01:02 +08:00
static void add_property_compatibility_obligation (
zend_class_entry * ce , const zend_property_info * child_prop ,
const zend_property_info * parent_prop ) {
HashTable * obligations = get_or_init_obligations_for_class ( ce ) ;
variance_obligation * obligation = emalloc ( sizeof ( variance_obligation ) ) ;
obligation - > type = OBLIGATION_PROPERTY_COMPATIBILITY ;
obligation - > child_prop = child_prop ;
obligation - > parent_prop = parent_prop ;
zend_hash_next_index_insert_ptr ( obligations , obligation ) ;
}
2019-05-27 17:39:56 +08:00
static void resolve_delayed_variance_obligations ( zend_class_entry * ce ) ;
2021-11-08 17:32:47 +08:00
static void check_variance_obligation ( variance_obligation * obligation ) {
2019-05-27 17:39:56 +08:00
if ( obligation - > type = = OBLIGATION_DEPENDENCY ) {
zend_class_entry * dependency_ce = obligation - > dependency_ce ;
if ( dependency_ce - > ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE ) {
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
zend_class_entry * orig_linking_class = CG ( current_linking_class ) ;
CG ( current_linking_class ) =
( dependency_ce - > ce_flags & ZEND_ACC_CACHEABLE ) ? dependency_ce : NULL ;
2019-05-27 17:39:56 +08:00
resolve_delayed_variance_obligations ( dependency_ce ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
CG ( current_linking_class ) = orig_linking_class ;
2019-05-27 17:39:56 +08:00
}
2019-10-17 19:01:02 +08:00
} else if ( obligation - > type = = OBLIGATION_COMPATIBILITY ) {
2019-05-27 17:39:56 +08:00
inheritance_status status = zend_do_perform_implementation_check (
2020-10-15 18:46:07 +08:00
& obligation - > child_fn , obligation - > child_scope ,
& obligation - > parent_fn , obligation - > parent_scope ) ;
2019-06-26 06:00:31 +08:00
if ( UNEXPECTED ( status ! = INHERITANCE_SUCCESS ) ) {
2020-04-23 17:34:07 +08:00
emit_incompatible_method_error (
2020-10-15 18:46:07 +08:00
& obligation - > child_fn , obligation - > child_scope ,
& obligation - > parent_fn , obligation - > parent_scope , status ) ;
2019-05-27 17:39:56 +08:00
}
/* Either the compatibility check was successful or only threw a warning. */
2019-10-17 19:01:02 +08:00
} else {
ZEND_ASSERT ( obligation - > type = = OBLIGATION_PROPERTY_COMPATIBILITY ) ;
inheritance_status status =
property_types_compatible ( obligation - > parent_prop , obligation - > child_prop ) ;
if ( status ! = INHERITANCE_SUCCESS ) {
emit_incompatible_property_error ( obligation - > child_prop , obligation - > parent_prop ) ;
}
2019-05-27 17:39:56 +08:00
}
}
2021-09-29 16:01:05 +08:00
static void load_delayed_classes ( zend_class_entry * ce ) {
2019-05-27 17:39:56 +08:00
HashTable * delayed_autoloads = CG ( delayed_autoloads ) ;
if ( ! delayed_autoloads ) {
return ;
}
2021-11-06 00:33:08 +08:00
/* Autoloading can trigger linking of another class, which may register new delayed autoloads.
* For that reason , this code uses a loop that pops and loads the first element of the HT . If
* this triggers linking , then the remaining classes may get loaded when linking the newly
* loaded class . This is important , as otherwise necessary dependencies may not be available
* if the new class is lower in the hierarchy than the current one . */
HashPosition pos = 0 ;
zend_string * name ;
zend_ulong idx ;
while ( zend_hash_get_current_key_ex ( delayed_autoloads , & name , & idx , & pos )
! = HASH_KEY_NON_EXISTENT ) {
zend_string_addref ( name ) ;
zend_hash_del ( delayed_autoloads , name ) ;
2019-05-27 17:39:56 +08:00
zend_lookup_class ( name ) ;
2021-11-06 00:33:08 +08:00
zend_string_release ( name ) ;
2021-09-23 18:44:51 +08:00
if ( EG ( exception ) ) {
2021-09-29 16:01:05 +08:00
zend_exception_uncaught_error (
" During inheritance of %s, while autoloading %s " ,
ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( name ) ) ;
2021-09-23 18:44:51 +08:00
}
2021-11-06 00:33:08 +08:00
}
2019-05-27 17:39:56 +08:00
}
static void resolve_delayed_variance_obligations ( zend_class_entry * ce ) {
HashTable * all_obligations = CG ( delayed_variance_obligations ) , * obligations ;
zend_ulong num_key = ( zend_ulong ) ( uintptr_t ) ce ;
ZEND_ASSERT ( all_obligations ! = NULL ) ;
obligations = zend_hash_index_find_ptr ( all_obligations , num_key ) ;
ZEND_ASSERT ( obligations ! = NULL ) ;
variance_obligation * obligation ;
ZEND_HASH_FOREACH_PTR ( obligations , obligation ) {
2021-11-08 17:32:47 +08:00
check_variance_obligation ( obligation ) ;
2019-05-27 17:39:56 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
ce - > ce_flags & = ~ ZEND_ACC_UNRESOLVED_VARIANCE ;
ce - > ce_flags | = ZEND_ACC_LINKED ;
zend_hash_index_del ( all_obligations , num_key ) ;
}
2019-09-11 21:31:04 +08:00
static void check_unrecoverable_load_failure ( zend_class_entry * ce ) {
/* If this class has been used while unlinked through a variance obligation, it is not legal
* to remove the class from the class table and throw an exception , because there is already
* a dependence on the inheritance hierarchy of this specific class . Instead we fall back to
* a fatal error , as would happen if we did not allow exceptions in the first place . */
2021-07-29 22:09:23 +08:00
if ( CG ( unlinked_uses )
& & zend_hash_index_del ( CG ( unlinked_uses ) , ( zend_long ) ( zend_uintptr_t ) ce ) = = SUCCESS ) {
2021-05-28 16:19:23 +08:00
zend_exception_uncaught_error (
" During inheritance of %s with variance dependencies " , ZSTR_VAL ( ce - > name ) ) ;
2019-09-11 21:31:04 +08:00
}
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
# define zend_update_inherited_handler(handler) do { \
if ( ce - > handler = = ( zend_function * ) op_array ) { \
ce - > handler = ( zend_function * ) new_op_array ; \
} \
} while ( 0 )
static zend_class_entry * zend_lazy_class_load ( zend_class_entry * pce )
{
zend_class_entry * ce ;
Bucket * p , * end ;
ce = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_class_entry ) ) ;
memcpy ( ce , pce , sizeof ( zend_class_entry ) ) ;
ce - > ce_flags & = ~ ZEND_ACC_IMMUTABLE ;
ce - > refcount = 1 ;
ce - > inheritance_cache = NULL ;
2021-08-02 17:05:43 +08:00
if ( CG ( compiler_options ) & ZEND_COMPILE_PRELOAD ) {
ZEND_MAP_PTR_NEW ( ce - > mutable_data ) ;
} else {
ZEND_MAP_PTR_INIT ( ce - > mutable_data , NULL ) ;
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
/* properties */
if ( ce - > default_properties_table ) {
zval * dst = emalloc ( sizeof ( zval ) * ce - > default_properties_count ) ;
zval * src = ce - > default_properties_table ;
zval * end = src + ce - > default_properties_count ;
ce - > default_properties_table = dst ;
for ( ; src ! = end ; src + + , dst + + ) {
ZVAL_COPY_VALUE_PROP ( dst , src ) ;
}
}
/* methods */
ce - > function_table . pDestructor = ZEND_FUNCTION_DTOR ;
if ( ! ( HT_FLAGS ( & ce - > function_table ) & HASH_FLAG_UNINITIALIZED ) ) {
p = emalloc ( HT_SIZE ( & ce - > function_table ) ) ;
memcpy ( p , HT_GET_DATA_ADDR ( & ce - > function_table ) , HT_USED_SIZE ( & ce - > function_table ) ) ;
HT_SET_DATA_ADDR ( & ce - > function_table , p ) ;
p = ce - > function_table . arData ;
end = p + ce - > function_table . nNumUsed ;
for ( ; p ! = end ; p + + ) {
zend_op_array * op_array , * new_op_array ;
op_array = Z_PTR ( p - > val ) ;
ZEND_ASSERT ( op_array - > type = = ZEND_USER_FUNCTION ) ;
ZEND_ASSERT ( op_array - > scope = = pce ) ;
ZEND_ASSERT ( op_array - > prototype = = NULL ) ;
2021-10-14 17:16:18 +08:00
new_op_array = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_op_array ) ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
Z_PTR ( p - > val ) = new_op_array ;
memcpy ( new_op_array , op_array , sizeof ( zend_op_array ) ) ;
new_op_array - > fn_flags & = ~ ZEND_ACC_IMMUTABLE ;
new_op_array - > scope = ce ;
2021-10-14 17:16:18 +08:00
ZEND_MAP_PTR_INIT ( new_op_array - > run_time_cache , NULL ) ;
ZEND_MAP_PTR_INIT ( new_op_array - > static_variables_ptr , NULL ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
zend_update_inherited_handler ( constructor ) ;
zend_update_inherited_handler ( destructor ) ;
zend_update_inherited_handler ( clone ) ;
zend_update_inherited_handler ( __get ) ;
zend_update_inherited_handler ( __set ) ;
zend_update_inherited_handler ( __call ) ;
zend_update_inherited_handler ( __isset ) ;
zend_update_inherited_handler ( __unset ) ;
zend_update_inherited_handler ( __tostring ) ;
zend_update_inherited_handler ( __callstatic ) ;
zend_update_inherited_handler ( __debugInfo ) ;
zend_update_inherited_handler ( __serialize ) ;
zend_update_inherited_handler ( __unserialize ) ;
}
}
/* static members */
if ( ce - > default_static_members_table ) {
zval * dst = emalloc ( sizeof ( zval ) * ce - > default_static_members_count ) ;
zval * src = ce - > default_static_members_table ;
zval * end = src + ce - > default_static_members_count ;
ce - > default_static_members_table = dst ;
for ( ; src ! = end ; src + + , dst + + ) {
ZVAL_COPY_VALUE ( dst , src ) ;
}
}
2021-10-14 17:16:18 +08:00
ZEND_MAP_PTR_INIT ( ce - > static_members_table , NULL ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
/* properties_info */
if ( ! ( HT_FLAGS ( & ce - > properties_info ) & HASH_FLAG_UNINITIALIZED ) ) {
p = emalloc ( HT_SIZE ( & ce - > properties_info ) ) ;
memcpy ( p , HT_GET_DATA_ADDR ( & ce - > properties_info ) , HT_USED_SIZE ( & ce - > properties_info ) ) ;
HT_SET_DATA_ADDR ( & ce - > properties_info , p ) ;
p = ce - > properties_info . arData ;
end = p + ce - > properties_info . nNumUsed ;
for ( ; p ! = end ; p + + ) {
zend_property_info * prop_info , * new_prop_info ;
prop_info = Z_PTR ( p - > val ) ;
ZEND_ASSERT ( prop_info - > ce = = pce ) ;
new_prop_info = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_property_info ) ) ;
Z_PTR ( p - > val ) = new_prop_info ;
memcpy ( new_prop_info , prop_info , sizeof ( zend_property_info ) ) ;
new_prop_info - > ce = ce ;
if ( ZEND_TYPE_HAS_LIST ( new_prop_info - > type ) ) {
zend_type_list * new_list ;
zend_type_list * list = ZEND_TYPE_LIST ( new_prop_info - > type ) ;
new_list = zend_arena_alloc ( & CG ( arena ) , ZEND_TYPE_LIST_SIZE ( list - > num_types ) ) ;
memcpy ( new_list , list , ZEND_TYPE_LIST_SIZE ( list - > num_types ) ) ;
ZEND_TYPE_SET_PTR ( new_prop_info - > type , list ) ;
ZEND_TYPE_FULL_MASK ( new_prop_info - > type ) | = _ZEND_TYPE_ARENA_BIT ;
}
}
}
/* constants table */
if ( ! ( HT_FLAGS ( & ce - > constants_table ) & HASH_FLAG_UNINITIALIZED ) ) {
p = emalloc ( HT_SIZE ( & ce - > constants_table ) ) ;
memcpy ( p , HT_GET_DATA_ADDR ( & ce - > constants_table ) , HT_USED_SIZE ( & ce - > constants_table ) ) ;
HT_SET_DATA_ADDR ( & ce - > constants_table , p ) ;
p = ce - > constants_table . arData ;
end = p + ce - > constants_table . nNumUsed ;
for ( ; p ! = end ; p + + ) {
zend_class_constant * c , * new_c ;
c = Z_PTR ( p - > val ) ;
ZEND_ASSERT ( c - > ce = = pce ) ;
new_c = zend_arena_alloc ( & CG ( arena ) , sizeof ( zend_class_constant ) ) ;
Z_PTR ( p - > val ) = new_c ;
memcpy ( new_c , c , sizeof ( zend_class_constant ) ) ;
new_c - > ce = ce ;
}
}
return ce ;
}
# ifndef ZEND_WIN32
# define UPDATE_IS_CACHEABLE(ce) do { \
if ( ( ce ) - > type = = ZEND_USER_CLASS ) { \
is_cacheable & = ( ce ) - > ce_flags ; \
} \
} while ( 0 )
# else
// TODO: ASLR may cause different addresses in different workers ???
# define UPDATE_IS_CACHEABLE(ce) do { \
is_cacheable & = ( ce ) - > ce_flags ; \
} while ( 0 )
# endif
ZEND_API zend_class_entry * zend_do_link_class ( zend_class_entry * ce , zend_string * lc_parent_name , zend_string * key ) /* { { { */
2018-08-27 21:40:25 +08:00
{
2019-09-11 21:31:04 +08:00
/* Load parent/interface dependencies first, so we can still gracefully abort linking
* with an exception and remove the class from the class table . This is only possible
* if no variance obligations on the current class have been added during autoloading . */
zend_class_entry * parent = NULL ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
zend_class_entry * * traits_and_interfaces = NULL ;
zend_class_entry * proto = NULL ;
zend_class_entry * orig_linking_class ;
uint32_t is_cacheable = ce - > ce_flags & ZEND_ACC_IMMUTABLE ;
uint32_t i , j ;
zval * zv ;
2021-03-11 18:42:35 +08:00
ALLOCA_FLAG ( use_heap )
2019-09-11 21:31:04 +08:00
2021-03-13 02:50:26 +08:00
SET_ALLOCA_FLAG ( use_heap ) ;
2021-02-11 04:57:50 +08:00
ZEND_ASSERT ( ! ( ce - > ce_flags & ZEND_ACC_LINKED ) ) ;
2019-05-24 20:28:44 +08:00
if ( ce - > parent_name ) {
2019-09-11 21:31:04 +08:00
parent = zend_fetch_class_by_name (
ce - > parent_name , lc_parent_name ,
ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED | ZEND_FETCH_CLASS_EXCEPTION ) ;
if ( ! parent ) {
check_unrecoverable_load_failure ( ce ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return NULL ;
}
UPDATE_IS_CACHEABLE ( parent ) ;
}
if ( ce - > num_traits | | ce - > num_interfaces ) {
2021-03-11 18:42:35 +08:00
traits_and_interfaces = do_alloca ( sizeof ( zend_class_entry * ) * ( ce - > num_traits + ce - > num_interfaces ) , use_heap ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
for ( i = 0 ; i < ce - > num_traits ; i + + ) {
zend_class_entry * trait = zend_fetch_class_by_name ( ce - > trait_names [ i ] . name ,
ce - > trait_names [ i ] . lc_name , ZEND_FETCH_CLASS_TRAIT ) ;
if ( UNEXPECTED ( trait = = NULL ) ) {
2021-03-11 18:42:35 +08:00
free_alloca ( traits_and_interfaces , use_heap ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return NULL ;
}
if ( UNEXPECTED ( ! ( trait - > ce_flags & ZEND_ACC_TRAIT ) ) ) {
zend_error_noreturn ( E_ERROR , " %s cannot use %s - it is not a trait " , ZSTR_VAL ( ce - > name ) , ZSTR_VAL ( trait - > name ) ) ;
2021-03-11 18:42:35 +08:00
free_alloca ( traits_and_interfaces , use_heap ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return NULL ;
}
for ( j = 0 ; j < i ; j + + ) {
if ( traits_and_interfaces [ j ] = = trait ) {
/* skip duplications */
trait = NULL ;
break ;
}
}
traits_and_interfaces [ i ] = trait ;
if ( trait ) {
UPDATE_IS_CACHEABLE ( trait ) ;
}
2019-09-11 21:31:04 +08:00
}
}
if ( ce - > num_interfaces ) {
for ( i = 0 ; i < ce - > num_interfaces ; i + + ) {
zend_class_entry * iface = zend_fetch_class_by_name (
ce - > interface_names [ i ] . name , ce - > interface_names [ i ] . lc_name ,
ZEND_FETCH_CLASS_INTERFACE |
ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED | ZEND_FETCH_CLASS_EXCEPTION ) ;
if ( ! iface ) {
check_unrecoverable_load_failure ( ce ) ;
2021-03-11 18:42:35 +08:00
free_alloca ( traits_and_interfaces , use_heap ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return NULL ;
2019-09-11 21:31:04 +08:00
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
traits_and_interfaces [ ce - > num_traits + i ] = iface ;
if ( iface ) {
UPDATE_IS_CACHEABLE ( iface ) ;
}
2019-09-11 21:31:04 +08:00
}
}
2020-06-11 05:10:18 +08:00
# ifndef ZEND_WIN32
if ( ce - > ce_flags & ZEND_ACC_ENUM ) {
/* We will add internal methods. */
is_cacheable = false ;
}
# endif
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
2021-07-29 22:50:00 +08:00
bool orig_record_errors = EG ( record_errors ) ;
2021-04-29 22:37:53 +08:00
2022-04-03 19:16:33 +08:00
if ( ce - > ce_flags & ZEND_ACC_IMMUTABLE & & is_cacheable ) {
if ( zend_inheritance_cache_get & & zend_inheritance_cache_add ) {
zend_class_entry * ret = zend_inheritance_cache_get ( ce , parent , traits_and_interfaces ) ;
if ( ret ) {
if ( traits_and_interfaces ) {
free_alloca ( traits_and_interfaces , use_heap ) ;
}
zv = zend_hash_find_known_hash ( CG ( class_table ) , key ) ;
Z_CE_P ( zv ) = ret ;
return ret ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
}
2022-04-03 19:16:33 +08:00
/* Make sure warnings (such as deprecations) thrown during inheritance
* will be recorded in the inheritance cache . */
zend_begin_record_errors ( ) ;
} else {
is_cacheable = 0 ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
}
2022-04-03 19:16:33 +08:00
proto = ce ;
2021-07-29 22:09:23 +08:00
}
2022-04-03 19:16:33 +08:00
zend_try {
if ( ce - > ce_flags & ZEND_ACC_IMMUTABLE ) {
/* Lazy class loading */
ce = zend_lazy_class_load ( ce ) ;
zv = zend_hash_find_known_hash ( CG ( class_table ) , key ) ;
Z_CE_P ( zv ) = ce ;
} else if ( ce - > ce_flags & ZEND_ACC_FILE_CACHED ) {
/* Lazy class loading */
ce = zend_lazy_class_load ( ce ) ;
ce - > ce_flags & = ~ ZEND_ACC_FILE_CACHED ;
zv = zend_hash_find_known_hash ( CG ( class_table ) , key ) ;
Z_CE_P ( zv ) = ce ;
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
2022-04-03 19:16:33 +08:00
if ( CG ( unlinked_uses ) ) {
zend_hash_index_del ( CG ( unlinked_uses ) , ( zend_long ) ( zend_uintptr_t ) ce ) ;
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
2022-04-03 19:16:33 +08:00
orig_linking_class = CG ( current_linking_class ) ;
CG ( current_linking_class ) = is_cacheable ? ce : NULL ;
2020-06-11 05:10:18 +08:00
2022-04-03 19:16:33 +08:00
if ( ce - > ce_flags & ZEND_ACC_ENUM ) {
/* Only register builtin enum methods during inheritance to avoid persisting them in
* opcache . */
zend_enum_register_funcs ( ce ) ;
2019-05-27 17:39:56 +08:00
}
2021-03-11 18:42:35 +08:00
2022-04-03 19:16:33 +08:00
if ( parent ) {
if ( ! ( parent - > ce_flags & ZEND_ACC_LINKED ) ) {
add_dependency_obligation ( ce , parent ) ;
}
zend_do_inheritance ( ce , parent ) ;
}
if ( ce - > num_traits ) {
zend_do_bind_traits ( ce , traits_and_interfaces ) ;
2021-03-11 18:42:35 +08:00
}
2022-04-03 19:16:33 +08:00
if ( ce - > num_interfaces ) {
/* Also copy the parent interfaces here, so we don't need to reallocate later. */
uint32_t num_parent_interfaces = parent ? parent - > num_interfaces : 0 ;
zend_class_entry * * interfaces = emalloc (
sizeof ( zend_class_entry * ) * ( ce - > num_interfaces + num_parent_interfaces ) ) ;
2021-03-11 18:42:35 +08:00
2022-04-03 19:16:33 +08:00
if ( num_parent_interfaces ) {
memcpy ( interfaces , parent - > interfaces ,
sizeof ( zend_class_entry * ) * num_parent_interfaces ) ;
}
memcpy ( interfaces + num_parent_interfaces , traits_and_interfaces + ce - > num_traits ,
sizeof ( zend_class_entry * ) * ce - > num_interfaces ) ;
2019-01-07 19:28:51 +08:00
2022-04-03 19:16:33 +08:00
zend_do_implement_interfaces ( ce , interfaces ) ;
} else if ( parent & & parent - > num_interfaces ) {
zend_do_inherit_interfaces ( ce , parent ) ;
}
if ( ! ( ce - > ce_flags & ( ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT ) )
& & ( ce - > ce_flags & ( ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS ) )
) {
zend_verify_abstract_class ( ce ) ;
}
if ( ce - > ce_flags & ZEND_ACC_ENUM ) {
zend_verify_enum ( ce ) ;
}
/* Normally Stringable is added during compilation. However, if it is imported from a trait,
* we need to explicilty add the interface here . */
if ( ce - > __tostring & & ! ( ce - > ce_flags & ZEND_ACC_TRAIT )
2021-11-05 16:42:58 +08:00
& & ! zend_class_implements_interface ( ce , zend_ce_stringable ) ) {
2022-04-03 19:16:33 +08:00
ZEND_ASSERT ( ce - > __tostring - > common . fn_flags & ZEND_ACC_TRAIT_CLONE ) ;
ce - > ce_flags | = ZEND_ACC_RESOLVED_INTERFACES ;
ce - > num_interfaces + + ;
ce - > interfaces = perealloc ( ce - > interfaces ,
sizeof ( zend_class_entry * ) * ce - > num_interfaces , ce - > type = = ZEND_INTERNAL_CLASS ) ;
ce - > interfaces [ ce - > num_interfaces - 1 ] = zend_ce_stringable ;
do_interface_implementation ( ce , zend_ce_stringable ) ;
}
zend_build_properties_info_table ( ce ) ;
} zend_catch {
/* Do not leak recorded errors to the next linked class. */
if ( ! orig_record_errors ) {
EG ( record_errors ) = false ;
zend_free_recorded_errors ( ) ;
}
zend_bailout ( ) ;
} zend_end_try ( ) ;
2021-11-04 18:28:00 +08:00
2021-07-29 22:50:00 +08:00
EG ( record_errors ) = orig_record_errors ;
2019-05-27 17:39:56 +08:00
if ( ! ( ce - > ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE ) ) {
ce - > ce_flags | = ZEND_ACC_LINKED ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
} else {
ce - > ce_flags | = ZEND_ACC_NEARLY_LINKED ;
if ( CG ( current_linking_class ) ) {
ce - > ce_flags | = ZEND_ACC_CACHEABLE ;
}
2021-09-29 16:01:05 +08:00
load_delayed_classes ( ce ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
if ( ce - > ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE ) {
resolve_delayed_variance_obligations ( ce ) ;
}
if ( ce - > ce_flags & ZEND_ACC_CACHEABLE ) {
ce - > ce_flags & = ~ ZEND_ACC_CACHEABLE ;
} else {
CG ( current_linking_class ) = NULL ;
}
}
if ( ! CG ( current_linking_class ) ) {
is_cacheable = 0 ;
2019-05-27 17:39:56 +08:00
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
CG ( current_linking_class ) = orig_linking_class ;
2019-05-27 17:39:56 +08:00
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
if ( is_cacheable ) {
HashTable * ht = ( HashTable * ) ce - > inheritance_cache ;
zend_class_entry * new_ce ;
ce - > inheritance_cache = NULL ;
new_ce = zend_inheritance_cache_add ( ce , proto , parent , traits_and_interfaces , ht ) ;
if ( new_ce ) {
2021-07-21 07:07:17 +08:00
zv = zend_hash_find_known_hash ( CG ( class_table ) , key ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ce = new_ce ;
Z_CE_P ( zv ) = ce ;
}
if ( ht ) {
zend_hash_destroy ( ht ) ;
FREE_HASHTABLE ( ht ) ;
2019-05-27 17:39:56 +08:00
}
}
2019-09-11 21:31:04 +08:00
2021-07-29 22:50:00 +08:00
if ( ! orig_record_errors ) {
zend_free_recorded_errors ( ) ;
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
if ( traits_and_interfaces ) {
2021-03-11 18:42:35 +08:00
free_alloca ( traits_and_interfaces , use_heap ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
}
2021-04-09 04:37:40 +08:00
if ( ZSTR_HAS_CE_CACHE ( ce - > name ) ) {
ZSTR_SET_CE_CACHE ( ce - > name , ce ) ;
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return ce ;
2018-08-27 17:56:17 +08:00
}
2019-06-27 07:13:06 +08:00
/* }}} */
2019-05-09 16:41:06 +08:00
/* Check whether early binding is prevented due to unresolved types in inheritance checks. */
2019-06-27 07:13:06 +08:00
static inheritance_status zend_can_early_bind ( zend_class_entry * ce , zend_class_entry * parent_ce ) /* { { { */
{
2019-05-09 16:41:06 +08:00
zend_string * key ;
zend_function * parent_func ;
2019-10-17 19:01:02 +08:00
zend_property_info * parent_info ;
2021-07-15 21:23:08 +08:00
inheritance_status overall_status = INHERITANCE_SUCCESS ;
2019-06-25 17:41:06 +08:00
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & parent_ce - > function_table , key , parent_func ) {
2021-07-21 07:07:17 +08:00
zval * zv = zend_hash_find_known_hash ( & ce - > function_table , key ) ;
2019-06-27 07:13:06 +08:00
if ( zv ) {
zend_function * child_func = Z_FUNC_P ( zv ) ;
inheritance_status status =
2020-01-09 22:04:33 +08:00
do_inheritance_check_on_method_ex (
2020-10-15 18:46:07 +08:00
child_func , child_func - > common . scope ,
parent_func , parent_func - > common . scope ,
ce , NULL , /* check_visibility */ 1 , 1 , 0 ) ;
2021-07-15 21:23:08 +08:00
if ( UNEXPECTED ( status = = INHERITANCE_WARNING ) ) {
overall_status = INHERITANCE_WARNING ;
} else if ( UNEXPECTED ( status ! = INHERITANCE_SUCCESS ) ) {
2019-06-27 07:25:57 +08:00
return status ;
2019-06-25 22:43:46 +08:00
}
2019-05-09 16:41:06 +08:00
}
2019-06-27 07:13:06 +08:00
} ZEND_HASH_FOREACH_END ( ) ;
2019-06-25 17:41:06 +08:00
2021-11-03 20:18:26 +08:00
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR ( & parent_ce - > properties_info , key , parent_info ) {
2019-10-17 19:01:02 +08:00
zval * zv ;
if ( ( parent_info - > flags & ZEND_ACC_PRIVATE ) | | ! ZEND_TYPE_IS_SET ( parent_info - > type ) ) {
continue ;
}
2021-07-21 07:07:17 +08:00
zv = zend_hash_find_known_hash ( & ce - > properties_info , key ) ;
2019-10-17 19:01:02 +08:00
if ( zv ) {
zend_property_info * child_info = Z_PTR_P ( zv ) ;
if ( ZEND_TYPE_IS_SET ( child_info - > type ) ) {
inheritance_status status = property_types_compatible ( parent_info , child_info ) ;
2021-07-15 21:23:08 +08:00
ZEND_ASSERT ( status ! = INHERITANCE_WARNING ) ;
2019-10-17 19:01:02 +08:00
if ( UNEXPECTED ( status ! = INHERITANCE_SUCCESS ) ) {
2019-10-17 19:42:47 +08:00
return status ;
2019-10-17 19:01:02 +08:00
}
}
}
} ZEND_HASH_FOREACH_END ( ) ;
2021-07-15 21:23:08 +08:00
return overall_status ;
2019-06-27 07:13:06 +08:00
}
/* }}} */
2019-06-26 06:00:31 +08:00
2021-07-28 17:18:26 +08:00
static zend_always_inline bool register_early_bound_ce ( zval * delayed_early_binding , zend_string * lcname , zend_class_entry * ce ) {
if ( delayed_early_binding ) {
2021-07-28 20:27:58 +08:00
if ( EXPECTED ( ! ( ce - > ce_flags & ZEND_ACC_PRELOADED ) ) ) {
if ( zend_hash_set_bucket_key ( EG ( class_table ) , ( Bucket * ) delayed_early_binding , lcname ) ! = NULL ) {
Z_CE_P ( delayed_early_binding ) = ce ;
return true ;
}
} else {
/* If preloading is used, don't replace the existing bucket, add a new one. */
if ( zend_hash_add_ptr ( EG ( class_table ) , lcname , ce ) ! = NULL ) {
return true ;
}
2021-07-28 17:18:26 +08:00
}
2021-07-28 20:27:58 +08:00
zend_error_noreturn ( E_COMPILE_ERROR , " Cannot declare %s %s, because the name is already in use " , zend_get_object_type ( ce ) , ZSTR_VAL ( ce - > name ) ) ;
return false ;
2021-07-28 17:18:26 +08:00
}
2022-07-11 22:19:26 +08:00
if ( zend_hash_add_ptr ( CG ( class_table ) , lcname , ce ) ! = NULL ) {
return true ;
}
return false ;
2021-07-28 17:18:26 +08:00
}
2021-09-27 18:21:40 +08:00
ZEND_API zend_class_entry * zend_try_early_bind ( zend_class_entry * ce , zend_class_entry * parent_ce , zend_string * lcname , zval * delayed_early_binding ) /* { { { */
2019-06-27 07:13:06 +08:00
{
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
inheritance_status status ;
zend_class_entry * proto = NULL ;
zend_class_entry * orig_linking_class ;
uint32_t is_cacheable = ce - > ce_flags & ZEND_ACC_IMMUTABLE ;
UPDATE_IS_CACHEABLE ( parent_ce ) ;
if ( is_cacheable ) {
if ( zend_inheritance_cache_get & & zend_inheritance_cache_add ) {
zend_class_entry * ret = zend_inheritance_cache_get ( ce , parent_ce , NULL ) ;
if ( ret ) {
2021-07-28 17:18:26 +08:00
if ( UNEXPECTED ( ! register_early_bound_ce ( delayed_early_binding , lcname , ret ) ) ) {
return NULL ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
}
2022-11-02 22:28:53 +08:00
zend_observer_class_linked_notify ( ret , lcname ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return ret ;
}
} else {
is_cacheable = 0 ;
}
proto = ce ;
}
2019-06-26 06:00:31 +08:00
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
orig_linking_class = CG ( current_linking_class ) ;
CG ( current_linking_class ) = NULL ;
status = zend_can_early_bind ( ce , parent_ce ) ;
CG ( current_linking_class ) = orig_linking_class ;
2019-06-27 07:13:06 +08:00
if ( EXPECTED ( status ! = INHERITANCE_UNRESOLVED ) ) {
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
if ( ce - > ce_flags & ZEND_ACC_IMMUTABLE ) {
/* Lazy class loading */
ce = zend_lazy_class_load ( ce ) ;
} else if ( ce - > ce_flags & ZEND_ACC_FILE_CACHED ) {
/* Lazy class loading */
ce = zend_lazy_class_load ( ce ) ;
ce - > ce_flags & = ~ ZEND_ACC_FILE_CACHED ;
}
2021-07-28 17:18:26 +08:00
if ( UNEXPECTED ( ! register_early_bound_ce ( delayed_early_binding , lcname , ce ) ) ) {
return NULL ;
2019-05-09 16:41:06 +08:00
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
orig_linking_class = CG ( current_linking_class ) ;
CG ( current_linking_class ) = is_cacheable ? ce : NULL ;
2022-04-03 19:16:33 +08:00
zend_try {
if ( is_cacheable ) {
zend_begin_record_errors ( ) ;
}
2021-05-17 16:18:26 +08:00
2022-04-03 19:16:33 +08:00
zend_do_inheritance_ex ( ce , parent_ce , status = = INHERITANCE_SUCCESS ) ;
if ( parent_ce & & parent_ce - > num_interfaces ) {
zend_do_inherit_interfaces ( ce , parent_ce ) ;
}
zend_build_properties_info_table ( ce ) ;
if ( ( ce - > ce_flags & ( ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS ) ) = = ZEND_ACC_IMPLICIT_ABSTRACT_CLASS ) {
zend_verify_abstract_class ( ce ) ;
}
ZEND_ASSERT ( ! ( ce - > ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE ) ) ;
ce - > ce_flags | = ZEND_ACC_LINKED ;
CG ( current_linking_class ) = orig_linking_class ;
} zend_catch {
EG ( record_errors ) = false ;
zend_free_recorded_errors ( ) ;
zend_bailout ( ) ;
} zend_end_try ( ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
2021-05-17 16:18:26 +08:00
EG ( record_errors ) = false ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
if ( is_cacheable ) {
HashTable * ht = ( HashTable * ) ce - > inheritance_cache ;
zend_class_entry * new_ce ;
ce - > inheritance_cache = NULL ;
new_ce = zend_inheritance_cache_add ( ce , proto , parent_ce , NULL , ht ) ;
if ( new_ce ) {
2021-07-21 07:07:17 +08:00
zval * zv = zend_hash_find_known_hash ( CG ( class_table ) , lcname ) ;
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
ce = new_ce ;
Z_CE_P ( zv ) = ce ;
}
if ( ht ) {
zend_hash_destroy ( ht ) ;
FREE_HASHTABLE ( ht ) ;
}
}
2021-04-09 04:37:40 +08:00
if ( ZSTR_HAS_CE_CACHE ( ce - > name ) ) {
ZSTR_SET_CE_CACHE ( ce - > name , ce ) ;
}
2022-11-02 22:28:53 +08:00
zend_observer_class_linked_notify ( ce , lcname ) ;
2021-04-09 04:37:40 +08:00
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return ce ;
2019-06-27 07:13:06 +08:00
}
Added Inheritance Cache.
This is a new transparent technology that eliminates overhead of PHP class inheritance.
PHP classes are compiled and cached (by opcahce) separately, however their "linking" was done at run-time - on each request. The process of "linking" may involve a number of compatibility checks and borrowing methods/properties/constants form parent and traits. This takes significant time, but the result is the same on each request.
Inheritance Cache performs "linking" for unique set of all the depending classes (parent, interfaces, traits, property types, method types involved into compatibility checks) once and stores result in opcache shared memory. As a part of the this patch, I removed limitations for immutable classes (unresolved constants, typed properties and covariant type checks). So now all classes stored in opcache are "immutable". They may be lazily loaded into process memory, if necessary, but this usually occurs just once (on first linking).
The patch shows 8% improvement on Symphony "Hello World" app.
2021-02-10 03:53:57 +08:00
return NULL ;
2019-05-09 16:41:06 +08:00
}
2018-08-27 17:56:17 +08:00
/* }}} */