lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Kurt Nordstrom wrote:
Hello all,

However, I am looking for a way to put limits on the code that users can
run.  Namely, I don't want to see somebody kick something off with an
infinite loop and lock up the entire game.

The solution seems to be to use Lua's debugging API to let the system
check the running process occasionally, and limit either the amount of
execution time a particular function gets, or the number of times that it
can loop through a particular point.  If I'm not mistaken, the line hook
should be sufficient for this sort of thing.

I've used this approach before.  The performance hit is actually not bad.

Things to watch out for:
-  If possible, hook by instruction and not by line.
-  Memory can be eaten *quickly*.  You'll want to put memory limits on
   scripts and check for memory usage often.
-  Making sure the scripts do not have access to the debug library,
   useful global variables, or io/os libraries.
-  Depending on the situation and game, scripts may or may not need
   access to other scripts' variables.  My suggestion for this is to
   push each project into its own environment using setfenv.  Set a
   variable inside that project's globals table to a shared variables
   table.  That way all the projects can choose which globals are
   private and which should be shared with other modules.  When the
   project needs to be unloaded, you can just delete the environment
   that the project was in and the garbage collector should pick up
   all its pieces, but save the variables in the shared table.
-  Multithreading (LuaTask, LuaThreads, and Lua Lanes) are probably
   also good ideas for this.