解密PHP运行机制
Welcome!
php|tek - Chicago, US
Derick Rethans - dr@ez.no
http://derickrethans.nl/talks.php
About Me
● Dutchman living in Norway
● eZ Systems A.S.
● eZ Components project lead
● PHP development
● mcrypt, input_filter, date/time support, unicode
● QA
...
Welcome!
php|tek - Chicago, US
Derick Rethans - dr@ez.no
http://derickrethans.nl/talks.php
About Me
● Dutchman living in Norway
● eZ Systems A.S.
● eZ Components project lead
● PHP development
● mcrypt, input_filter, date/time support, unicode
● QA
Introduction
Input: Apache module
PHP registers handlers for:
Mime types:
● application/x-httpd-php
● application/x-httpd-php-source
Others:
● text/html (Apache xbithack)
● php-script (Apache 2)
On a request, Apache:
● checks for the MIME type handler
● opens the file
● fills a meta data record
● hands PHP a filepointer
Input: Apache/CGI
CGI binary is linked to mime-type:
ScriptAlias /php/ /usr/local/bin/php-cgi
AddType application/x-httpd-php .php
Action application/x-httpd-php "/php/php.exe"
On a request, Apache:
● checks if there is a handler for the mime type
● fills in needed environment variables
● executes the CGI processor
Parsing
● Lexical analyze script source
● Divide into logical blocks of characters
● Give special blocks a meaning
● flex (but only 2.5.4!)
The Parse Error:
Parse error: parse error,
unexpected T_CLASS, expecting ',' or ';' in - on line 2
Parsing: Tokenize
Syntax highlighting = Tokenizing
Parsing: Tokens
● The building blocks of a script
● Strings, variables, keywords
● Action rules:
"::" {
return T_PAAMAYIM_NEKUDOTAYIM;
}
{DNUM}|{EXPONENT_DNUM} {
zendlval->value.dval = strtod(yytext, NULL);
zendlval->type = IS_DOUBLE;
return T_DNUMBER;
}
Parsing: Tokens in PHP
From Zend/zend_language_parser.y:
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
%left ','
%left T_LOGICAL_OR
%left T_LOGICAL_XOR
%left T_LOGICAL_AND
%right T_PRINT
%left '=' T_PLUS_EQUAL T_MINUS_EQUAL T_MUL_EQUAL T_DIV_EQUAL T_CONCAT_EQUAL
T_MOD_EQUAL T_AND_EQUAL T_OR_EQUAL T_XOR_EQUAL T_SL_EQUAL T_SR_EQUAL
%left '?' ':'
%left T_BOOLEAN_OR
%left T_BOOLEAN_AND
%left '|'
%left '^'
%left '&'
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
%left T_SL T_SR
%left '+' '-' '.'
%left '*' '/' '%'
%right '!' '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST
T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@'
%right '['
%nonassoc T_NEW T_INSTANCEOF
%token T_EXIT
%token T_IF
%left T_ELSEIF
%left T_ELSE
%left T_ENDIF
%token T_LNUMBER
%token T_DNUMBER
%token T_STRING
%token T_STRING_VARNAME
%token T_VARIABLE
%token T_NUM_STRING
%token T_INLINE_HTML
%token T_CHARACTER
%token T_BAD_CHARACTER
%token T_ENCAPSED_AND_WHITESPACE
%token T_CONSTANT_ENCAPSED_STRING
%token T_ECHO
%token T_DO
%token T_WHILE
%token T_ENDWHILE
%token T_FOR
%token T_ENDFOR
%token T_FOREACH
%token T_ENDFOREACH
%token T_DECLARE
%token T_ENDDECLARE
%token T_INSTANCEOF
%token T_AS
%token T_SWITCH
%token T_ENDSWITCH
%token T_CASE
%token T_DEFAULT
%token T_BREAK
%token T_CONTINUE
%token T_FUNCTION
%token T_CONST
%token T_RETURN
%token T_TRY
%token T_CATCH
%token T_THROW
%token T_USE
%token T_GLOBAL
%right T_STATIC T_ABSTRACT T_FINAL T_PRIVATE T_PROTECTED T_PUBLIC
%token T_VAR
%token T_UNSET
%token T_ISSET
%token T_EMPTY
%token T_CLASS
%token T_INTERFACE
%token T_EXTENDS
%token T_IMPLEMENTS
%token T_OBJECT_OPERATOR
%token T_DOUBLE_ARROW
%token T_LIST
%token T_ARRAY
%token T_CLASS_C
%token T_FUNC_C
%token T_LINE
%token T_FILE
%token T_COMMENT
%token T_DOC_COMMENT
%token T_OPEN_TAG
%token T_OPEN_TAG_WITH_ECHO
%token T_CLOSE_TAG
%token T_WHITESPACE
%token T_START_HEREDOC
%token T_END_HEREDOC
%token T_DOLLAR_OPEN_CURLY_BRACES
%token T_CURLY_OPEN
%token T_PAAMAYIM_NEKUDOTAYIM
}
Parsing: Example (1)
Tokenizer extension:
magic.php:
tokenize.php:
Parsing: Example (2)
T_OPEN_TAG []
Compiling
● Interpreting tokens
● Generating execution units
● Generating class and function tables
Compiling
Diagram
Compiling: Example
fibonacci.php:
Compiling
Break-down
Compiling
Disassembler
Disassembler: vld
Dumps oparray per element:
● main script
● function
● class method
Usage:
wget wget http://pecl.php.net/get/vld-0.8.0.tgz
tar -xvzf vld-0.8.0.tgz
cd vld-0.8.0
phpize && make && make install
php -dextension=vld.so -dvld.active=1 script.php
Executing
● Executor executes opcodes
● 116 or 140 different opcodes
● Per file execution
Executing: Diagram
Executing Example
Scripts
test.inc:
test.php:
Executing Example
Workflow
The engine:
● parses and compiles test.php
● starts executing test.php
● parses and compiles test.inc when
● the include() statement is encountered
● starts executing test.inc
● resumes executing test.php
Executing
In a diagram
Compiler Caches
How it works
● In general, each source file is compiled once
● Compilation overhead becomes inconsequential
● Cache introduces its own overhead due to dynamic nature of includes
Compiler Caches
Serialization
● Serialization into SHM
● Direct execution from SHM (mostly)
PHP Accelerators
● APC: active development, PHP license
● eAcclerator: GPL
● PHP Accelerator: updated, but not improved
● Turck MM Cache: abandonned
● XCache: new
● Zend Platform: commercial
Questions
?
Variables
The Symbol Table
Variables
Introducing Refcounting
Variables
Refcounting and Passing Variables to
Functions
Variables
Introducing References
Variables
Mixing Assign-By Value and Assign-by
Reference
Variables
Mixing Assign-by Reference and Assign-by
Value
Variables
Passing by Reference
Variables
Return by Reference
Variables
The "global" Keyword
Variables
Bad Return by Reference
Variables
Circular References
Questions
?
Resources
These Slides: http://pres.derickrethans.nl
VLD site: http://www.derickrethans.nl/vld.php
PHP: http://www.php.net
本文档为【解密PHP运行机制】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。