lua-users home
lua-l archive

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



Am 26.08.2013 08:36 schrieb "Philipp Kraus" <philipp.kraus@tu-clausthal.de>:
>
> Hello,
>
> I'm using LUA for a simulation on a cluster system, so my user can define their own scripts. In some cases there exists a infinite loop or infinity recursion in the LUA code so the simulation does not stop anymore.
> My idea is to create a maximum execution time (like PHP) with a watchdog (I don't use a formal verification of the code e.g. wp-calculus). Each script has got a defined function which is called by my C++
> class, so I would like to send the LUA script a "warning" that the max. execution time is reached, but if the script does not stop, the C++ side should stop the script.
>
> Can I / How can I stop a running script from C side?
> Do you think it is a good idea to send a warning first, because the script does not use threads, so IMHO the warning can not be handled by the script?
> I hope for some ideas
>
> Thanks
>
> Phil
>
>

The easiest approach would be to use a a debug hook with an instruction counter limit (see debug.sethook). When the instruction limit is reached, you can throw an error  Inside the hook function - which could be caught on script side. After that limit, you set another limit that terminates the script execution when reached.
You can allow the user to access the instruction count so the programmer can see the "countdown" - for that, you could set a limit of 1000000 instructions and have a hook that gets called each 10000 instructions and each time, you store how many instructions are left for use.

However, there's a penalty when using the debug API. In case of Lua jit, the optimizations of the VM might disable certain debug hooks.

For more sophisticated approach, I would look into the VM execution code and do something similar as the debug API does, just more efficiently... Hopefully.

Oh, and coroutine require special care because they have each their own debug hooks. So executing a coroutine will prevent a debug hook from firing. The solution to that is to modify coroutine.create that it hooks automatically to your hook functions.

Cheers
Eike