2012-01-28 16:22:18 +00:00
|
|
|
<?php
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
class Scribunto_LuaSandboxEngine extends Scribunto_LuaEngine {
|
|
|
|
public $options, $loaded = false;
|
2012-02-06 22:14:47 +00:00
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
public function getLimitReport() {
|
2012-01-28 16:22:18 +00:00
|
|
|
$this->load();
|
2012-04-13 10:38:12 +00:00
|
|
|
$usage = $this->interpreter->getMemoryUsage();
|
|
|
|
$lang = Language::factory( 'en' );
|
|
|
|
$usageStr = $lang->formatSize( $usage );
|
|
|
|
return "Lua memory usage: {$usageStr}\n";
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-04 05:10:47 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
function newInterpreter() {
|
|
|
|
return new Scribunto_LuaSandboxInterpreter( $this, $this->options );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
class Scribunto_LuaSandboxInterpreter extends Scribunto_LuaInterpreter {
|
|
|
|
var $engine, $sandbox, $libraries;
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
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' ) ) {
|
2012-04-19 07:40:56 +00:00
|
|
|
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
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
$this->engine = $engine;
|
|
|
|
$this->sandbox = new LuaSandbox;
|
|
|
|
$this->sandbox->setMemoryLimit( $options['memoryLimit'] );
|
|
|
|
$this->sandbox->setCPULimit( $options['cpuLimit'] );
|
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
public function loadString( $text, $chunkName ) {
|
2012-01-28 16:22:18 +00:00
|
|
|
try {
|
2012-04-13 10:38:12 +00:00
|
|
|
return $this->sandbox->loadString( $text, $chunkName );
|
|
|
|
} catch ( LuaSandboxError $e ) {
|
2012-04-23 11:36:13 +00:00
|
|
|
throw $this->engine->newLuaError( $e->getMessage() );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function registerLibrary( $name, $functions ) {
|
|
|
|
$realLibrary = array();
|
|
|
|
foreach ( $functions as $funcName => $callback ) {
|
|
|
|
$realLibrary[$funcName] = array(
|
|
|
|
new Scribunto_LuaSandboxCallback( $callback ),
|
|
|
|
'call' );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
$this->sandbox->registerLibrary( $name, $realLibrary );
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
# TODO: replace this with
|
|
|
|
#$this->sandbox->registerVirtualLibrary(
|
|
|
|
# $name, array( $this, 'callback' ), $functions );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
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 ) {
|
2012-04-23 11:36:13 +00:00
|
|
|
throw $this->engine->newException( 'scribunto-common-timeout' );
|
2012-04-13 10:38:12 +00:00
|
|
|
} catch ( LuaSandboxError $e ) {
|
2012-04-23 11:36:13 +00:00
|
|
|
throw $this->engine->newLuaError( $e->getMessage() );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
public function getMemoryUsage() {
|
|
|
|
return $this->sandbox->getMemoryUsage();
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-13 10:38:12 +00:00
|
|
|
}
|
2012-04-05 07:58:02 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
class Scribunto_LuaSandboxCallback {
|
|
|
|
function __construct( $callback ) {
|
|
|
|
$this->callback = $callback;
|
2012-04-05 07:58:02 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
function call( /*...*/ ) {
|
|
|
|
$args = func_get_args();
|
2012-01-28 16:22:18 +00:00
|
|
|
try {
|
2012-04-13 10:38:12 +00:00
|
|
|
return call_user_func_array( $this->callback, $args );
|
|
|
|
} catch ( Scribunto_LuaError $e ) {
|
|
|
|
throw new LuaSandboxRuntimeError( $e->getLuaMessage() );
|
2012-04-04 06:10:32 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
|
|
|
}
|