Sandbox Lua
Setting the hooks is not sufficient to prevent inadvertent waste of resources, not to mention abuse - here is a simple example (time spent while matching string patterns: the debug call was not called):
s=('a'):rep(20000):match('.-b')
The only reliable way to force time and memory limits on a piece of Lua code is to run the Lua interpreter in its own process and make your OS monitor such a process.
The good thing with Lua is that you donβt need a complicated, OS-dependent permission setting for the sandbox: you just limit the time and memory (reasonable, there are Job Objects on windows, Unix has corresponding restrictions: Linux resource limit ), and then save things like os.execute, half of the io libraries and modules like luasocket from (pretty easy).
Recovering from errors in isolated code
You can handle almost everything (except for violation of time / memory limitations) without confusing your Lua interpreter: just wrap the user-provided code in pcall ; if you call any Lua-API functions that may fail yourself, you need to wrap them inside a function that you can pcall also (or install the Lua panic function and process it from there).
[I did not want people to look at this thread to suggest that debug.sethook is suitable for the sandbox, and stackoverflow will not let me comment (for now)]
radioflash
source share