XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3425 lines
89 KiB

<?php
define('INDENT', "\t");
ini_set('error_reporting', E_ALL);
assert_options(ASSERT_ACTIVE, 0);
if (function_exists("gc_disable")) {
gc_collect_cycles();
gc_disable();
}
function color($str, $color = 33)
{
return "\x1B[{$color}m$str\x1B[0m";
}
function printBacktrace() // {{{
{
if (!$GLOBALS['__xcache_decompiler']->inComment++) {
echo '/*', PHP_EOL;
}
$backtrace = debug_backtrace();
foreach ($backtrace as $stack) {
$args = array();
foreach ($stack['args'] as $arg) {
if (is_scalar($arg)) {
$args[] = var_export($arg, true);
}
else if (is_array($arg)) {
$array = array();
foreach ($arg as $key => $value) {
$array[] = var_export($key, true) . " => " . (is_scalar($value) ? var_export($value, true) : gettype($value));
if (count($array) >= 5) {
$array[] = '...';
break;
}
}
$args[] = "array(" . implode(', ', $array) . ')';
}
else {
$args[] = gettype($arg);
}
}
printf("%d: %s::%s(%s)" . PHP_EOL
, $stack['line']
, isset($stack['class']) ? $stack['class'] : ''
, $stack['function']
, implode(', ', $args)
);
}
if (!--$GLOBALS['__xcache_decompiler']->inComment) {
echo '*/', PHP_EOL;
}
}
// }}}
function str($code, $indent = '') // {{{
{
if (is_array($code)) {
$array = array();
foreach ($code as $key => $value) {
$array[$key] = str($value, $indent);
}
return $array;
}
if (is_object($code)) {
$code = foldToCode($code, $indent);
return $code->toCode($indent);
}
return (string) $code;
}
// }}}
function unsetArray(&$array, $name) // {{{
{
unset($array[$name]);
}
// }}}
function foldToCode($src, $indent = '') // {{{ wrap or rewrap anything to Decompiler_Code
{
if (is_array($indent)) {
$indent = $indent['indent'];
}
$decompiler = &$GLOBALS['__xcache_decompiler'];
if (!is_object($src)) {
return new Decompiler_Code($decompiler, $src);
}
if (!method_exists($src, 'toCode')) {
var_dump($src);
exit('no toCode');
}
if (get_class($src) != 'Decompiler_Code') {
// rewrap it
$src = new Decompiler_Code($decompiler, $src->toCode($indent));
}
return $src;
}
// }}}
function decompileAst($ast) // {{{
{
$kind = $ast['kind'];
$children = $ast['children'];
unset($ast['kind']);
unset($ast['children']);
$decompiler = &$GLOBALS['__xcache_decompiler'];
switch ($kind) {
case ZEND_CONST:
return value($ast[0]);
case XC_INIT_ARRAY:
$array = new Decompiler_Array($decompiler);
for ($i = 0; $i < $children; $i += 2) {
if (isset($ast[$i + 1])) {
$key = decompileAst($ast[$i]);
$value = decompileAst($ast[$i + 1]);
$array->value[] = array($key, $value, '');
}
else {
$array->value[] = array(null, decompileAst($ast[$i]), '');
}
}
return $array;
// ZEND_BOOL_AND: handled in binop
// ZEND_BOOL_OR: handled in binop
case ZEND_SELECT:
return new Decompiler_TernaryOp($decompiler
, decompileAst($ast[0])
, decompileAst($ast[1])
, decompileAst($ast[2])
);
case ZEND_UNARY_PLUS:
return new Decompiler_Code($decompiler, '+' . str(decompileAst($ast[0])));
case ZEND_UNARY_MINUS:
return new Decompiler_Code($decompiler, '-' . str(decompileAst($ast[0])));
default:
if (isset($decompiler->binaryOp[$kind])) {
return new Decompiler_BinaryOp($decompiler
, decompileAst($ast[0])
, $kind
, decompileAst($ast[1])
);
}
return "un-handled kind $kind in zend_ast";
}
}
// }}}
function value($value) // {{{
{
if (ZEND_ENGINE_2_6 && (xcache_get_type($value) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT_AST) {
return decompileAst(xcache_dasm_ast($value));
}
$decompiler = &$GLOBALS['__xcache_decompiler'];
$originalValue = xcache_get_special_value($value);
if (isset($originalValue)) {
if ((xcache_get_type($value) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
// constant
return $decompiler->stripNamespace($originalValue);
}
$value = $originalValue;
}
if (is_a($value, 'Decompiler_Object')) {
// use as is
}
else if (is_array($value)) {
$value = new Decompiler_ConstArray($decompiler, $value);
}
else {
if (is_scalar($value) && ($constant = $decompiler->value2constant($value)) && isset($constant)) {
$value = new Decompiler_Code($decompiler, $constant);
}
else {
$value = new Decompiler_Value($decompiler, $value);
}
}
return $value;
}
// }}}
function unquoteName_($str, $asVariableName, $indent = '') // {{{
{
$str = str($str, $indent);
if (preg_match("!^'[\\w_][\\w\\d_\\\\]*'\$!", $str)) {
return str_replace('\\\\', '\\', substr($str, 1, -1));
}
else if ($asVariableName) {
return "{" . $str . "}";
}
else {
return $str;
}
}
// }}}
function unquoteVariableName($str, $indent = '') // {{{
{
return unquoteName_($str, true, $indent);
}
// }}}
function unquoteName($str, $indent = '') // {{{
{
return unquoteName_($str, false, $indent);
}
// }}}
class Decompiler_Object // {{{
{
}
// }}}
class Decompiler_Value extends Decompiler_Object // {{{
{
var $value;
function Decompiler_Value(&$decompiler, $value = null)
{
$this->decompiler = &$decompiler;
$this->value = $value;
}
function toCode($indent)
{
$code = var_export($this->value, true);
if (gettype($this->value) == 'string') {
$code = preg_replace_callback("![\t\r\n]+!", array(&$this, 'convertNewline'), $code);
$code = preg_replace_callback("![\\x01-\\x1f]+!", array(&$this, 'escapeString'), $code);
$code = preg_replace_callback("![\\x7f-\\xff]+!", array(&$this, 'escape8BitString'), $code);
$code = preg_replace("!^'' \\. \"|\" \\. ''\$!", '"', $code);
}
return $code;
}
function convertNewline($m)
{
return "' . \"" . strtr($m[0], array("\t" => "\\t", "\r" => "\\r", "\n" => "\\n")) . "\" . '";
}
function escape8BitString($m)
{
if (function_exists("iconv") && @iconv("UTF-8", "UTF-8//IGNORE", $m[0]) == $m[0]) {
return $m[0];
}
return $this->escapeString($m);
}
function escapeString($m)
{
$s = $m[0];
$escaped = '';
for ($i = 0, $c = strlen($s); $i < $c; ++$i) {
$escaped .= "\\x" . dechex(ord($s[$i]));
}
return "' . \"" . $escaped . "\" . '";
}
}
// }}}
class Decompiler_Code extends Decompiler_Object // {{{
{
var $src;
function Decompiler_Code(&$decompiler, $src)
{
$this->decompiler = &$decompiler;
if (!assert('isset($src)')) {
printBacktrace();
}
$this->src = $src;
}
function toCode($indent)
{
return $this->src;
}
}
// }}}
class Decompiler_UnaryOp extends Decompiler_Code // {{{
{
var $opc;
var $op;
function Decompiler_UnaryOp(&$decompiler, $opc, $op)
{
$this->decompiler = &$decompiler;
$this->opc = $opc;
$this->op = $op;
}
function toCode($indent)
{
$opstr = $this->decompiler->unaryOp[$this->opc];
if (is_a($this->op, 'Decompiler_TernaryOp') || is_a($this->op, 'Decompiler_BinaryOp') && $this->op->opc != $this->opc) {
$op = "(" . str($this->op, $indent) . ")";
}
else {
$op = $this->op;
}
return $opstr . str($op, $indent);
}
}
// }}}
class Decompiler_BinaryOp extends Decompiler_Code // {{{
{
var $opc;
var $op1;
var $op2;
function Decompiler_BinaryOp(&$decompiler, $op1, $opc, $op2)
{
$this->decompiler = &$decompiler;
$this->opc = $opc;
$this->op1 = $op1;
$this->op2 = $op2;
}
function toCode($indent)
{
$opstr = $this->decompiler->binaryOp[$this->opc];
if (is_a($this->op1, 'Decompiler_TernaryOp') || is_a($this->op1, 'Decompiler_BinaryOp') && $this->op1->opc != $this->opc) {
$op1 = "(" . str($this->op1, $indent) . ")";
}
else {
$op1 = $this->op1;
}
if (is_a($this->op2, 'Decompiler_TernaryOp') || is_a($this->op2, 'Decompiler_BinaryOp') && $this->op2->opc != $this->opc && substr($opstr, -1) != '=') {
$op2 = "(" . str($this->op2, $indent) . ")";
}
else {
$op2 = $this->op2;
}
if (str($op1) == '0' && ($this->opc == XC_ADD || $this->opc == XC_SUB)) {
return $opstr . str($op2, $indent);
}
return str($op1, $indent) . ' ' . $opstr . ($this->opc == XC_ASSIGN_REF ? '' : ' ') . str($op2, $indent);
}
}
// }}}
class Decompiler_TernaryOp extends Decompiler_Code // {{{
{
var $condition;
var $trueValue;
var $falseValue;
function Decompiler_TernaryOp(&$decompiler, $condition, $trueValue, $falseValue)
{
$this->decompiler = &$decompiler;
$this->condition = $condition;
$this->trueValue = $trueValue;
$this->falseValue = $falseValue;
}
function toCode($indent)
{
$trueValue = $this->trueValue;
if (is_a($this->trueValue, 'Decompiler_TernaryOp')) {
$trueValue = "(" . str($trueValue, $indent) . ")";
}
$falseValue = $this->falseValue;
if (is_a($this->falseValue, 'Decompiler_TernaryOp')) {
$falseValue = "(" . str($falseValue, $indent) . ")";
}
return str($this->condition) . ' ? ' . str($trueValue) . ' : ' . str($falseValue);
}
}
// }}}
class Decompiler_Fetch extends Decompiler_Code // {{{
{
var $src;
var $fetchType;
function Decompiler_Fetch(&$decompiler, $src, $type, $globalSrc)
{
$this->decompiler = &$decompiler;
$this->src = $src;
$this->fetchType = $type;
$this->globalSrc = $globalSrc;
}
function toCode($indent)
{
switch ($this->fetchType) {
case ZEND_FETCH_LOCAL:
return '$' . $this->src;
case ZEND_FETCH_STATIC:
if (ZEND_ENGINE_2_3) {
// closure local variable?
return 'STR' . str($this->src);
}
else {
return str(value($this->src));
}
die('static fetch cant to string');
case ZEND_FETCH_GLOBAL:
case ZEND_FETCH_GLOBAL_LOCK:
return $this->globalSrc;
default:
var_dump($this->fetchType);
assert(0);
}
}
}
// }}}
class Decompiler_Box // {{{
{
var $obj;
function Decompiler_Box(&$decompiler, &$obj)
{
$this->decompiler = &$decompiler;
$this->obj = &$obj;
}
function toCode($indent)
{
return $this->obj->toCode($indent);
}
}
// }}}
class Decompiler_Dim extends Decompiler_Value // {{{
{
var $offsets = array();
var $isLast = false;
var $isObject = false;
var $assign = null;
function toCode($indent)
{
if (is_a($this->value, 'Decompiler_ListBox')) {
$exp = str($this->value->obj->src, $indent);
}
else {
$exp = str($this->value, $indent);
}
$last = count($this->offsets) - 1;
foreach ($this->offsets as $i => $dim) {
if ($this->isObject && $i == $last) {
$exp .= '->' . unquoteVariableName($dim, $indent);
}
else {
$exp .= '[' . str($dim, $indent) . ']';
}
}
return $exp;
}
}
// }}}
class Decompiler_DimBox extends Decompiler_Box // {{{
{
}
// }}}
class Decompiler_List extends Decompiler_Code // {{{
{
var $src;
var $dims = array();
var $everLocked = false;
function toCode($indent)
{
if (count($this->dims) == 1 && !$this->everLocked) {
$dim = $this->dims[0];
unset($dim->value);
$dim->value = $this->src;
if (!isset($dim->assign)) {
return str($dim, $indent);
}
return str($this->dims[0]->assign, $indent) . ' = ' . str($dim, $indent);
}
/* flatten dims */
$assigns = array();
foreach ($this->dims as $dim) {
$assign = &$assigns;
foreach ($dim->offsets as $offset) {
$assign = &$assign[$offset];
}
$assign = foldToCode($dim->assign, $indent);
}
return str($this->toList($assigns)) . ' = ' . str($this->src, $indent);
}
function toList($assigns)
{
$keys = array_keys($assigns);
if (count($keys) < 2) {
$keys[] = 0;
}
$max = call_user_func_array('max', $keys);
$list = "list(";
for ($i = 0; $i <= $max; $i++) {
if ($i) {
$list .= ', ';
}
if (!isset($assigns[$i])) {
continue;
}
if (is_array($assigns[$i])) {
$list .= $this->toList($assigns[$i]);
}
else {
$list .= $assigns[$i];
}
}
return $list . ')';
}
}
// }}}
class Decompiler_ListBox extends Decompiler_Box // {{{
{
}
// }}}
class Decompiler_Array extends Decompiler_Value // {{{
{
// elements
function Decompiler_Array(&$decompiler)
{
$this->decompiler = &$decompiler;
$this->value = array();
}
function toCode($indent)
{
$subindent = $indent . INDENT;
$elementsCode = array();
$index = 0;
foreach ($this->value as $element) {
list($key, $value, $ref) = $element;
if (!isset($key)) {
$key = $index++;
}
$elementsCode[] = array(str($key, $subindent), str($value, $subindent), $key, $value, $ref);
}
$exp = "array(";
$indent = $indent . INDENT;
$assocWidth = 0;
$multiline = 0;
$i = 0;
foreach ($elementsCode as $element) {
list($keyCode, $valueCode) = $element;
if ((string) $i !== $keyCode) {
$assocWidth = 1;
break;
}
++$i;
}
foreach ($elementsCode as $element) {
list($keyCode, $valueCode, $key, $value) = $element;
if ($assocWidth) {
$len = strlen($keyCode);
if ($assocWidth < $len) {
$assocWidth = $len;
}
}
if (is_array($value) || is_a($value, 'Decompiler_Array')) {
$multiline++;
}
}
$i = 0;
foreach ($elementsCode as $element) {
list($keyCode, $value, , , $ref) = $element;
if ($multiline) {
if ($i) {
$exp .= ",";
}
$exp .= PHP_EOL;
$exp .= $indent;
}
else {
if ($i) {
$exp .= ", ";
}
}
if ($assocWidth) {
if ($multiline) {
$exp .= sprintf("%-{$assocWidth}s => ", $keyCode);
}
else {
$exp .= $keyCode . ' => ';
}
}
$exp .= $ref;
$exp .= $value;
$i++;
}
if ($multiline) {
$exp .= PHP_EOL . "$indent)";
}
else {
$exp .= ")";
}
return $exp;
}
}
// }}}
class Decompiler_ConstArray extends Decompiler_Array // {{{
{
function Decompiler_ConstArray(&$decompiler, $array)
{
$this->decompiler = &$decompiler;
$elements = array();
foreach ($array as $key => $value) {
if ((xcache_get_type($value) & IS_CONSTANT_INDEX)) {
$keyCode = $GLOBALS['__xcache_decompiler']->stripNamespace(
ZEND_ENGINE_2_3
? substr($key, 0, -2)
: $key
);
}
else {
$keyCode = value($key);
}
$elements[] = array($keyCode, value($value), '');
}
$this->value = $elements;
}
}
// }}}
class Decompiler_ForeachBox extends Decompiler_Box // {{{
{
var $iskey;
function toCode($indent)
{
return '#foreachBox#';
}
}
// }}}
class Decompiler
{
var $namespace;
var $namespaceDecided;
var $activeFile;
var $activeDir;
var $activeClass;
var $activeMethod;
var $activeFunction;
var $outputPhp;
var $outputOpcode;
var $inComment = 0;
var $value2constant = array();
function Decompiler($outputTypes)
{
$this->outputPhp = in_array('php', $outputTypes);
$this->outputOpcode = in_array('opcode', $outputTypes);
$GLOBALS['__xcache_decompiler'] = $this;
// {{{ testing
// XC_UNDEF XC_OP_DATA
$this->test = !empty($_ENV['XCACHE_DECOMPILER_TEST']);
$this->usedOps = array();
if ($this->test) {
$content = file_get_contents(__FILE__);
for ($i = 0; $opname = xcache_get_opcode($i); $i++) {
if (!preg_match("/\\bXC_" . $opname . "\\b(?!')/", $content)) {
echo "not recognized opcode ", $opname, PHP_EOL;
}
}
}
// }}}
// {{{ opinfo
$this->unaryOp = array(
XC_BW_NOT => '~',
XC_BOOL_NOT => '!',
);
$this->binaryOp = array(
XC_ADD => "+",
XC_ASSIGN_ADD => "+=",
XC_SUB => "-",
XC_ASSIGN_SUB => "-=",
XC_MUL => "*",
XC_ASSIGN_MUL => "*=",
XC_DIV => "/",
XC_ASSIGN_DIV => "/=",
XC_MOD => "%",
XC_ASSIGN_MOD => "%=",
XC_SL => "<<",
XC_ASSIGN_SL => "<<=",
XC_SR => ">>",
XC_ASSIGN_SR => ">>=",
XC_CONCAT => ".",
XC_ASSIGN_CONCAT => ".=",
XC_POW => "**",
XC_ASSIGN_POW => "*=",
XC_IS_IDENTICAL => "===",
XC_IS_NOT_IDENTICAL => "!==",
XC_IS_EQUAL => "==",
XC_IS_NOT_EQUAL => "!=",
XC_IS_SMALLER => "<",
XC_IS_SMALLER_OR_EQUAL => "<=",
XC_BW_OR => "|",
XC_ASSIGN_BW_OR => "|=",
XC_BW_AND => "&",
XC_ASSIGN_BW_AND => "&=",
XC_BW_XOR => "^",
XC_ASSIGN_BW_XOR => "^=",
XC_BOOL_XOR => "xor",
XC_ASSIGN => "=",
XC_ASSIGN_REF => "= &",
XC_JMP_SET => "?:",
XC_JMP_SET_VAR => "?:",
XC_JMPZ_EX => "&&",
XC_JMPNZ_EX => "||",
);
if (defined('IS_CONSTANT_AST')) {
$this->binaryOp[ZEND_BOOL_AND] = '&&';
$this->binaryOp[ZEND_BOOL_OR] = '||';
}
// }}}
$this->includeTypes = array( // {{{
ZEND_EVAL => 'eval',
ZEND_INCLUDE => 'include',
ZEND_INCLUDE_ONCE => 'include_once',
ZEND_REQUIRE => 'require',
ZEND_REQUIRE_ONCE => 'require_once',
);
// }}}
}
function detectNamespace($name) // {{{
{
if ($this->namespaceDecided) {
return;
}
if (strpos($name, '\\') !== false) {
$namespace = strtok($name, '\\');
if ($namespace == $this->namespace) {
return;
}
$this->namespace = $namespace;
echo 'namespace ', $this->namespace, ";", PHP_EOL, PHP_EOL;
}
$this->namespaceDecided = true;
}
// }}}
function stripNamespace($name) // {{{
{
if (!isset($name)) {
return $name;
}
$name = str($name);
$len = strlen($this->namespace) + 1;
if (strncasecmp($name, $this->namespace . '\\', $len) == 0) {
return substr($name, $len);
}
else {
return $name;
}
}
// }}}
function value2constant($value) // {{{
{
if (isset($this->EX) && isset($this->EX['value2constant'][$value])) {
return $this->EX['value2constant'][$value];
}
else if (isset($this->value2constant[$value])) {
return $this->value2constant[$value];
}
}
// }}}
function outputPhp($range) // {{{
{
$needBlankline = isset($this->EX['lastBlock']);
$indent = $this->EX['indent'];
$curticks = 0;
for ($i = $range[0]; $i <= $range[1]; $i++) {
$op = $this->EX['opcodes'][$i];
if (isset($op['gofrom'])) {
if ($needBlankline) {
$needBlankline = false;
echo PHP_EOL;
}
echo 'label' . $i, ":", PHP_EOL;
}
if (isset($op['php'])) {
$toticks = isset($op['ticks']) ? (int) str($op['ticks']) : 0;
if ($curticks != $toticks) {
$oldticks = $curticks;
$curticks = $toticks;
if (!$curticks) {
echo $this->EX['indent'], "}", PHP_EOL, PHP_EOL;
$indent = $this->EX['indent'];
}
else {
if ($oldticks) {
echo $this->EX['indent'], "}", PHP_EOL, PHP_EOL;
}
else if (!$oldticks) {
$indent .= INDENT;
}
if ($needBlankline) {
$needBlankline = false;
echo PHP_EOL;
}
echo $this->EX['indent'], "declare (ticks=$curticks) {", PHP_EOL;
}
}
if ($needBlankline) {
$needBlankline = false;
echo PHP_EOL;
}
echo $indent, str($op['php'], $indent), ";", PHP_EOL;
unset($op['php']);
$this->EX['lastBlock'] = 'basic';
}
}
if ($curticks) {
echo $this->EX['indent'], "}", PHP_EOL;
}
}
// }}}
function getOpVal($op, $free = false) // {{{
{
switch ($op['op_type']) {
case XC_IS_CONST:
return value($op['constant']);
case XC_IS_VAR:
case XC_IS_TMP_VAR:
$T = &$this->EX['Ts'];
if (!isset($T[$op['var']])) {
if ($this->outputPhp && isset($free)) {
printBacktrace();
}
return null;
}
$ret = $T[$op['var']];
if ($free && empty($this->keepTs)) {
unset($T[$op['var']]);
}
return $ret;
case XC_IS_CV:
$var = $op['var'];
$var = $this->EX['op_array']['vars'][$var];
return '$' . $var['name'];
case XC_IS_UNUSED:
return null;
}
}
// }}}
function removeKeyPrefix($array, $prefix) // {{{
{
$prefixLen = strlen($prefix);
$ret = array();
foreach ($array as $key => $value) {
if (substr($key, 0, $prefixLen) == $prefix) {
$key = substr($key, $prefixLen);
}
$ret[$key] = $value;
}
return $ret;
}
// }}}
function fixOpCode(&$opcodes, $removeTailing = false, $defaultReturnValue = null) // {{{
{
$last = count($opcodes) - 1;
for ($i = 0; $i <= $last; $i++) {
if (function_exists('xcache_get_fixed_opcode')) {
$opcodes[$i]['opcode'] = xcache_get_fixed_opcode($opcodes[$i]['opcode'], $i);
}
if (isset($opcodes[$i]['op1'])) {
$opcodes[$i]['op1'] = $this->removeKeyPrefix($opcodes[$i]['op1'], 'u.');
$opcodes[$i]['op2'] = $this->removeKeyPrefix($opcodes[$i]['op2'], 'u.');
$opcodes[$i]['result'] = $this->removeKeyPrefix($opcodes[$i]['result'], 'u.');
}
else {
$op = array(
'op1' => array(),
'op2' => array(),
'result' => array(),
);
foreach ($opcodes[$i] as $name => $value) {
if (preg_match('!^(op1|op2|result)\\.(.*)!', $name, $m)) {
list(, $which, $field) = $m;
$op[$which][$field] = $value;
}
else if (preg_match('!^(op1|op2|result)_type$!', $name, $m)) {
list(, $which) = $m;
$op[$which]['op_type'] = $value;
}
else {
$op[$name] = $value;
}
}
$opcodes[$i] = $op;
}
}