rfc:restrict_globals_usage

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:restrict_globals_usage [2020/12/04 10:40] nikicrfc:restrict_globals_usage [2021/01/06 11:47] (current) nikic
Line 2: Line 2:
   * Date: 2020-12-02   * Date: 2020-12-02
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Under Discussion+  * Status: Implemented
   * Target Version: PHP 8.1   * Target Version: PHP 8.1
   * Implementation: https://github.com/php/php-src/pull/6487   * Implementation: https://github.com/php/php-src/pull/6487
Line 18: Line 18:
 </PHP> </PHP>
  
-The variable ''$a'' is stored inside a compiled-variable (CV) call frame slot on the virtual machine stack, which allows it to be accessed efficient. In order to allow modification of the variable through ''$GLOBALS'', the ''$GLOBALS'' array stores array elements of type ''INDIRECT'', which contain a pointer to the CV slot.+The variable ''$a'' is stored inside a compiled-variable (CV) call frame slot on the virtual machine stack, which allows it to be accessed efficiently. In order to allow modification of the variable through ''$GLOBALS'', the ''$GLOBALS'' array stores array elements of type ''INDIRECT'', which contain a pointer to the CV slot.
  
 As such, array operations on ''$GLOBALS'' need to check whether the accessed element is ''INDIRECT'' and perform a de-indirection operation. However, as //any// array could potentially be the ''$GLOBALS'' array, this check has to be performed for essentially all array operations on all arrays. This imposes an implementation and performance cost to account for a rarely used edge-case. As such, array operations on ''$GLOBALS'' need to check whether the accessed element is ''INDIRECT'' and perform a de-indirection operation. However, as //any// array could potentially be the ''$GLOBALS'' array, this check has to be performed for essentially all array operations on all arrays. This imposes an implementation and performance cost to account for a rarely used edge-case.
Line 112: Line 112:
  
 From an implementation perspective, these changes mean that ''INDIRECT'' elements no longer have to be considered when working on ordinary PHP arrays (though many places didn't do so in the first place). However, ''INDIRECT'' elements still need to be considered when working with certain special hashtables. In particular, internal symbol tables, as well as object property tables may still contain ''INDIRECT'' elements. However, these special hashtables will never escape into ordinary PHP arrays. From an implementation perspective, these changes mean that ''INDIRECT'' elements no longer have to be considered when working on ordinary PHP arrays (though many places didn't do so in the first place). However, ''INDIRECT'' elements still need to be considered when working with certain special hashtables. In particular, internal symbol tables, as well as object property tables may still contain ''INDIRECT'' elements. However, these special hashtables will never escape into ordinary PHP arrays.
 +
 +Apart from manual checks for ''IS_INDIRECT'', the use of the following APIs is no longer necessary:
 +
 +  * ''*_IND()'' HT iteration macros. Suffix-free macros can be used instead.
 +  * ''*_ind()'' HT functions. Suffix-free functions can be used instead.
 +  * ''zend_array_count()''. Use of ''zend_hash_num_elements()'' is now safe.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 140: Line 146:
 ===== Vote ===== ===== Vote =====
  
-Yes/No.+Yes/No. Voting started 2020-12-23 and closes 2021-01-06. 
 + 
 +<doodle title="Restrict $GLOBALS usage as specified?" auth="nikic" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
rfc/restrict_globals_usage.1607078450.txt.gz · Last modified: 2020/12/04 10:40 by nikic