16
http://www.yAnbiN.org PHP 运行机制初探 Ben [email protected]

PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

PHP 运行机制初探

Ben [email protected]

Page 2: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Introduction

.php SAPI Compiler

Executor Opcodes

Output Buffer

• IE/Firefox

• Console

• Host

• Apache

• CLI

• Embed

• Lexer

• Parser

• Main Script

• Function Table

• Class Table

• Include file

• Switch/Call/Goto

• 129 Operators

Page 3: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Apache

Mime type handler

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

Server context

Override php.ini (php_value, php_flag, etc)

Environment variables(PHP_SELF, etc)

Create Child Process/Thread

Page 4: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Apache

Mime type handler

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

Server context

Override php.ini (php_value, php_flag, etc)

Environment variables(PHP_SELF, etc)

Create Child Process/Thread

Page 5: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

CLI (command line interface)

CLI ≈ CGI SAPI

differences

start up in quiet mode by default

plain text error message(no http header)

implicit_flush always on

max_execution_time is set to unlimited

others

Page 6: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Embed

Embed = Mini CLI

php5embed .lib

example.c#include <php_embed.h>

int main (int argc, char *argv[]){

PHP_EMBED_START_BLOCK(argc, argv)

zend_eval_string(“echo „Hello World‟;”, NULL, “Embedded Code” TSRMLS_CC);

PHP_EMBED_END_BLOCK()

return 0;

}

Page 7: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Lexer(flex)

<?php

$sum = 1 + 2;

echo „1+2=‟.$sum;

?>

T_OPEN_TAG: '<?php '

=

T_LNUMBER: '1'

+

T_LNUMBER: '2'

T_ECHO: 'echo''

T_CONSTANT_ENCAPSED_STRING:

''1+2=''.

T_CLOSE_TAG: '?>'

source: Zend/zend_language_scanner.l

Page 8: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Parser(bison)

Opcode Op1 Op2 Result

ADD 1 2 $tmp0

ASSIGN $cv0(sum) $tmp0 $var1

CONCAT '1+2=' $cv0(sum) $tmp2

ECHO $tmp2

RETURN 1

source: Zend/zend_language_parser.y

T_OPEN_TAG: '<?php '

=

T_LNUMBER: '1'

+

T_LNUMBER: '2'

T_ECHO: 'echo''

T_CONSTANT_ENCAPSED_STRING:

''1+2=''.

T_CLOSE_TAG: '?>'

Page 9: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Compiler

<?php

$sum = 1 + 2;

echo „1+2=‟.$sum;

?>

Opcode Op1 Op2 Result

ADD 1 2 $tmp0

ASSIGN $cv0(sum) $tmp0 $var1

CONCAT '1+2=' $cv0(sum) tmp2

ECHO $tmp2

RETURN 1

zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);

Page 10: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

struct zend_op {

opcode_handler_t handler;

znode result;

znode op1;

znode op2;

ulong extended_value;

uint lineno;

zend_uchar opcode;

};

Opcode

Page 11: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Executor

PHP 4.x

switch (opcode){

case ZEND_ADD:

break;

case ZEND_CALL:

break;

….

}

PHP 5.2

call ZEND_ADD_HANDLER()

PHP 5.2

switch (

case ZEND_ADD:

goto: zend_add

)

zend_add:

//

CALL GOTOSWITCH

void (*zend_execute)(zend_op_array *op_array TSRMLS_DC);

Page 12: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Cacher

Opcodes

Cacher

Compiler Executor

•APC

•eAcclerator

•XCache

•Zend Platform

PHP_MINIT_FUNCTION(xxx){

old_compile_file = zend_compile_file;

zend_compile_file = xxx_compile_file;;

}

Page 13: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Encoder / Decoder

Opcodes

Encoder

Decoder

Compiler Executor

•ZendGuard

• ionCube

•eAcclerator Encoder

NOT Encoder, BUT Obfuscator!

Page 14: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Debugger

PHP_MINIT_FUNCTION(xxx){

old_ execute = zend_ execute;

zend_ execute = xxx_ execute;

}

struct zend_extension {

message_handler_func_t message_handler;

op_array_handler_func_t op_array_handler;

statement_handler_func_t statement_handler;

fcall_begin_handler_func_t fcall_begin_handler;

fcall_end_handler_func_t fcall_end_handler;

op_array_ctor_func_t op_array_ctor;

op_array_dtor_func_t op_array_dtor;

}

Page 15: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org

Question

?

Page 16: PHP 运行机制初探 · 2013-03-14 · PHP Internals Author: Ben Keywords: PHP Extension ZendEngine Created Date: 9/19/2009 10:16:43 AM

http://www.yAnbiN.org