mediawiki-extensions-Scribunto/engines/LuaSandbox/Engine.php

164 lines
4.7 KiB
PHP
Raw Normal View History

<?php
class Scribunto_LuaSandboxEngine extends Scribunto_LuaEngine {
public $options, $loaded = false;
protected $lineCache = array();
* Removed scriptlinks table. It just seemed the same as templatelinks to me, and tl_namespace can be used if you want to separate out modules. * Used Parser::fetchTemplateAndTitle() to get modules and register them in templatelinks. Most of the logic was previously duplicated. * Changed the configuration and factory functions to allow for the possibility of multiple engines coexisting on the one wiki. * Made the $parser parameter optional, to improve debugging in the case where a parser is needed but parsing has not started. Removed all $wgParser references. * Renamed Scripting::getEngine() to getParserEngine() and resetEngine() to resetParserEngine() * Removed setOptions() and updateOptions(). If you want to change the options, you can always make a new instance. * Renamed getModule() to fetchModuleFromParser() * Simplified module constructor parameters and member variable list * Fixed spelling error langauge -> language * Renamed a few variables for clarity: $module -> $moduleName, $function -> $functionName * Renamed getLimitsReport() to getLimitReport() as it is in Parser * Use an accessor for getting LuaSandboxEngineModule::$contents * Renamed configuration variable maxCPU to cpuLimit * Include the full message name as a parameter to ScriptingException. This makes it easier to find messages in the i18n file, and it makes it easier to find invocation points when a translator wants to know how a message is used. Adding the message name as a comment on the same line seems like a waste of space when you can just make it an actual parameter. * Reduce the number of formal parameters to ScriptingException::__construct(), since there is already too many and we may want to add more things later, such as backtraces with hyperlinks and other such stuff. * Include the code location as $2 unconditionally so that there is less chance of getting the parameters wrong * Shortened some message names. Wrote English text for messages without it.
2012-04-05 07:58:02 +00:00
public function getLimitReport() {
$this->load();
$lang = Language::factory( 'en' );
$t = $this->interpreter->getCPUUsage();
$s = 'Lua time usage: ' . sprintf( "%.3f", $t ) . "s\n" .
'Lua memory usage: ' . $lang->formatSize( $this->interpreter->getPeakMemoryUsage() ) . "\n";
if ( $t < 1.0 ) {
return $s;
}
$percentProfile = $this->interpreter->getProfilerFunctionReport( LuaSandbox::PERCENT );
if ( !count( $percentProfile ) ) {
return $s;
}
$timeProfile = $this->interpreter->getProfilerFunctionReport( LuaSandbox::SECONDS );
$s .= "Lua Profile:\n";
$cumulativePercent = $cumulativeTime = 0;
$num = $otherTime = $otherPercent = 0;
$format = " %-59s %8.0f ms %8.1f%%\n";
foreach ( $percentProfile as $name => $percent ) {
$time = $timeProfile[$name] * 1000;
$cumulativePercent += $percent;
$cumulativeTime += $time;
$num++;
if ( $cumulativePercent <= 99 && $num <= 10 ) {
// Map some regularly appearing internal names
if ( preg_match( '/^<mw.lua:(\d+)>$/', $name, $m ) ) {
$line = $this->getMwLuaLine( $m[1] );
if ( preg_match( '/^\s*(local\s+)?function ([a-zA-Z0-9_.]*)/', $line, $m ) ) {
$name = $m[2] . ' ' . $name;
}
}
$s .= sprintf( $format, $name, $time, $percent );
} else {
$otherTime += $time;
$otherPercent += $percent;
}
}
if ( $otherTime ) {
$s .= sprintf( $format, "[others]", $otherTime, $otherPercent );
}
return $s;
}
protected function getMwLuaLine( $lineNum ) {
if ( !isset( $this->lineCache['mw.lua'] ) ) {
$this->lineCache['mw.lua'] = file( $this->getLuaLibDir() . '/mw.lua' );
}
return $this->lineCache['mw.lua'][$lineNum - 1];
}
function newInterpreter() {
return new Scribunto_LuaSandboxInterpreter( $this, $this->options );
}
}
class Scribunto_LuaSandboxInterpreter extends Scribunto_LuaInterpreter {
var $engine, $sandbox, $libraries, $profilerEnabled;
function __construct( $engine, $options ) {
Added tests and fixed bugs * Added unit tests for the two Lua interpreter classes * Fixed a bug in checkType() * Have Scribunto_LuaSandboxInterpreter throw an exception on construct when the extension doesn't exist, to match the standalone behaviour. * In Scribunto_LuaSandboxInterpreter, removed debugging statements accidentally left in. * Convert LuaSandboxTimeoutError to the appropriate common error message. * Moved the option munging from the sandbox engine to the interpreter, so that the interpreter can be unit tested separately. * Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller and still supports ulimit. * Use exec to run the lua binary, so that the vsize of the shell doesn't add to the memory limit. * Added a quit function to the standalone interpreter. Unused at present. * Don't add a comma after the last element of a table in a Lua expression. * Make the SIGXCPU detection work: proc_open() runs the command via a shell, which reports signals in the child via the exit status, so proc_get_status() will never return a valid termsig element. * In MWServer:call(), fixed a bug causing the return values to be wrapped in an array. * Fixed a misunderstanding of what select() does. * In MWServer:getStatus(), fixed indexes so that vsize will be correct. Removed RSS, since it wasn't used anyway and turns out to be measured in multiples of the page size, and I couldn't be bothered trying to fetch that from getconf. Return the PID and vsize as numbers rather than strings. * Added a simple table dump feature to MWServer:debug(). * Fixed brackets in MWServer:tostring(). * Added missing Linux 32-bit binary. Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
if ( !extension_loaded( 'luasandbox' ) ) {
throw new Scribunto_LuaInterpreterNotFoundError(
'The luasandbox extension is not present, this engine cannot be used.' );
Added tests and fixed bugs * Added unit tests for the two Lua interpreter classes * Fixed a bug in checkType() * Have Scribunto_LuaSandboxInterpreter throw an exception on construct when the extension doesn't exist, to match the standalone behaviour. * In Scribunto_LuaSandboxInterpreter, removed debugging statements accidentally left in. * Convert LuaSandboxTimeoutError to the appropriate common error message. * Moved the option munging from the sandbox engine to the interpreter, so that the interpreter can be unit tested separately. * Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller and still supports ulimit. * Use exec to run the lua binary, so that the vsize of the shell doesn't add to the memory limit. * Added a quit function to the standalone interpreter. Unused at present. * Don't add a comma after the last element of a table in a Lua expression. * Make the SIGXCPU detection work: proc_open() runs the command via a shell, which reports signals in the child via the exit status, so proc_get_status() will never return a valid termsig element. * In MWServer:call(), fixed a bug causing the return values to be wrapped in an array. * Fixed a misunderstanding of what select() does. * In MWServer:getStatus(), fixed indexes so that vsize will be correct. Removed RSS, since it wasn't used anyway and turns out to be measured in multiples of the page size, and I couldn't be bothered trying to fetch that from getconf. Return the PID and vsize as numbers rather than strings. * Added a simple table dump feature to MWServer:debug(). * Fixed brackets in MWServer:tostring(). * Added missing Linux 32-bit binary. Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
}
$this->engine = $engine;
$this->sandbox = new LuaSandbox;
$this->sandbox->setMemoryLimit( $options['memoryLimit'] );
$this->sandbox->setCPULimit( $options['cpuLimit'] );
if ( is_callable( array( $this->sandbox, 'enableProfiler' ) ) ) {
$this->profilerEnabled = true;
$this->sandbox->enableProfiler( 0.002 );
}
}
protected function convertSandboxError( $e ) {
$opts = array();
if ( isset( $e->luaTrace ) ) {
$opts['trace'] = $e->luaTrace;
}
$message = $e->getMessage();
if ( preg_match( '/^(.*?):(\d+): (.*)$/', $message, $m ) ) {
$opts['module'] = $m[1];
$opts['line'] = $m[2];
$message = $m[3];
}
return $this->engine->newLuaError( $message, $opts );
}
public function loadString( $text, $chunkName ) {
try {
return $this->sandbox->loadString( $text, $chunkName );
} catch ( LuaSandboxError $e ) {
throw $this->convertSandboxError( $e );
}
}
public function registerLibrary( $name, $functions ) {
$realLibrary = array();
foreach ( $functions as $funcName => $callback ) {
$realLibrary[$funcName] = array(
new Scribunto_LuaSandboxCallback( $callback ),
$funcName );
}
$this->sandbox->registerLibrary( $name, $realLibrary );
# TODO: replace this with
#$this->sandbox->registerVirtualLibrary(
# $name, array( $this, 'callback' ), $functions );
}
public function callFunction( $func /*, ... */ ) {
$args = func_get_args();
$func = array_shift( $args );
try {
return call_user_func_array( array( $func, 'call' ), $args );
Added tests and fixed bugs * Added unit tests for the two Lua interpreter classes * Fixed a bug in checkType() * Have Scribunto_LuaSandboxInterpreter throw an exception on construct when the extension doesn't exist, to match the standalone behaviour. * In Scribunto_LuaSandboxInterpreter, removed debugging statements accidentally left in. * Convert LuaSandboxTimeoutError to the appropriate common error message. * Moved the option munging from the sandbox engine to the interpreter, so that the interpreter can be unit tested separately. * Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller and still supports ulimit. * Use exec to run the lua binary, so that the vsize of the shell doesn't add to the memory limit. * Added a quit function to the standalone interpreter. Unused at present. * Don't add a comma after the last element of a table in a Lua expression. * Make the SIGXCPU detection work: proc_open() runs the command via a shell, which reports signals in the child via the exit status, so proc_get_status() will never return a valid termsig element. * In MWServer:call(), fixed a bug causing the return values to be wrapped in an array. * Fixed a misunderstanding of what select() does. * In MWServer:getStatus(), fixed indexes so that vsize will be correct. Removed RSS, since it wasn't used anyway and turns out to be measured in multiples of the page size, and I couldn't be bothered trying to fetch that from getconf. Return the PID and vsize as numbers rather than strings. * Added a simple table dump feature to MWServer:debug(). * Fixed brackets in MWServer:tostring(). * Added missing Linux 32-bit binary. Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
} catch ( LuaSandboxTimeoutError $e ) {
throw $this->engine->newException( 'scribunto-common-timeout' );
} catch ( LuaSandboxError $e ) {
throw $this->convertSandboxError( $e );
}
}
public function getPeakMemoryUsage() {
return $this->sandbox->getPeakMemoryUsage();
}
public function getCPUUsage() {
return $this->sandbox->getCPUUsage();
}
public function getProfilerFunctionReport( $units ) {
if ( $this->profilerEnabled ) {
return $this->sandbox->getProfilerFunctionReport( $units );
} else {
return array();
}
}
}
* Removed scriptlinks table. It just seemed the same as templatelinks to me, and tl_namespace can be used if you want to separate out modules. * Used Parser::fetchTemplateAndTitle() to get modules and register them in templatelinks. Most of the logic was previously duplicated. * Changed the configuration and factory functions to allow for the possibility of multiple engines coexisting on the one wiki. * Made the $parser parameter optional, to improve debugging in the case where a parser is needed but parsing has not started. Removed all $wgParser references. * Renamed Scripting::getEngine() to getParserEngine() and resetEngine() to resetParserEngine() * Removed setOptions() and updateOptions(). If you want to change the options, you can always make a new instance. * Renamed getModule() to fetchModuleFromParser() * Simplified module constructor parameters and member variable list * Fixed spelling error langauge -> language * Renamed a few variables for clarity: $module -> $moduleName, $function -> $functionName * Renamed getLimitsReport() to getLimitReport() as it is in Parser * Use an accessor for getting LuaSandboxEngineModule::$contents * Renamed configuration variable maxCPU to cpuLimit * Include the full message name as a parameter to ScriptingException. This makes it easier to find messages in the i18n file, and it makes it easier to find invocation points when a translator wants to know how a message is used. Adding the message name as a comment on the same line seems like a waste of space when you can just make it an actual parameter. * Reduce the number of formal parameters to ScriptingException::__construct(), since there is already too many and we may want to add more things later, such as backtraces with hyperlinks and other such stuff. * Include the code location as $2 unconditionally so that there is less chance of getting the parameters wrong * Shortened some message names. Wrote English text for messages without it.
2012-04-05 07:58:02 +00:00
class Scribunto_LuaSandboxCallback {
function __construct( $callback ) {
$this->callback = $callback;
* Removed scriptlinks table. It just seemed the same as templatelinks to me, and tl_namespace can be used if you want to separate out modules. * Used Parser::fetchTemplateAndTitle() to get modules and register them in templatelinks. Most of the logic was previously duplicated. * Changed the configuration and factory functions to allow for the possibility of multiple engines coexisting on the one wiki. * Made the $parser parameter optional, to improve debugging in the case where a parser is needed but parsing has not started. Removed all $wgParser references. * Renamed Scripting::getEngine() to getParserEngine() and resetEngine() to resetParserEngine() * Removed setOptions() and updateOptions(). If you want to change the options, you can always make a new instance. * Renamed getModule() to fetchModuleFromParser() * Simplified module constructor parameters and member variable list * Fixed spelling error langauge -> language * Renamed a few variables for clarity: $module -> $moduleName, $function -> $functionName * Renamed getLimitsReport() to getLimitReport() as it is in Parser * Use an accessor for getting LuaSandboxEngineModule::$contents * Renamed configuration variable maxCPU to cpuLimit * Include the full message name as a parameter to ScriptingException. This makes it easier to find messages in the i18n file, and it makes it easier to find invocation points when a translator wants to know how a message is used. Adding the message name as a comment on the same line seems like a waste of space when you can just make it an actual parameter. * Reduce the number of formal parameters to ScriptingException::__construct(), since there is already too many and we may want to add more things later, such as backtraces with hyperlinks and other such stuff. * Include the code location as $2 unconditionally so that there is less chance of getting the parameters wrong * Shortened some message names. Wrote English text for messages without it.
2012-04-05 07:58:02 +00:00
}
/**
* We use __call with a variable function name so that LuaSandbox will be
* able to return a meaningful function name in profiling data.
*/
function __call( $funcName, $args ) {
try {
return call_user_func_array( $this->callback, $args );
} catch ( Scribunto_LuaError $e ) {
throw new LuaSandboxRuntimeError( $e->getLuaMessage() );
}
}
}