pause/resume Lua VM execution?

524 views
Skip to first unread message

gtim...@gmail.com

unread,
Feb 20, 2009, 4:45:59 PM2/20/09
to lua-alchemy-dev
Hi all,

Is there any possibilities to allow AS3 to pause/resume the execution
of the Lua VM?I am trying to write a programming game (something like
GNU robots) in which I define a group of Lua API (such as robot.move
(), robot.shoot(), etc.) then let game player to program the robot
using Lua. Since the Lua code will be written by user, I cannot
implement any game rule to force game player calling yield()
periodically inside their code. Ideally a game player assumes they
have all the resources of the VM (CPU time, memory and hardware IO -
which is the robot at this time). The game will even allow more than
one players to submit their code and let their robots compete with
each other. In this case multiple instances of Lua VM will be
initialized and AS3 will switch the executions of different VM
instances.

I understand currently lua-alchemy doesn't support this. Is it
possible that this feature will be included in future releases?

Thanks,

Tim

Alexander Gladysh

unread,
Feb 21, 2009, 4:20:41 AM2/21/09
to lua-alc...@googlegroups.com
Hi,

On Sat, Feb 21, 2009 at 12:45 AM, gtim...@gmail.com
<gtim...@gmail.com> wrote:
> Is there any possibilities to allow AS3 to pause/resume the execution

> oz the Lua VM?I am trying to write a programming game (something like

You need to sandbox user's code. There was a recent thread here on this topic:

http://groups.google.com/group/lua-alchemy-dev/browse_thread/thread/ad662b451320b83b

In short: most of what you need can be done in pure Lua (google for
"Lua sandboxing"). If you need advanced functionality, we would be
glad to add it.

If you have further questions, please do not hesitate to ask.

Alexander.

gtim...@gmail.com

unread,
Feb 23, 2009, 4:56:06 PM2/23/09
to lua-alchemy-dev
Thanks Alexander for the quick response.

I looks the thread named "Using GEP to jail untrusted code?" and also
googled Lua sandboxing. However this is one thing I cannot find a
solution. it is about limiting the CPU usage (Actually a little bit
more than that). What I want is that either AS3 or trusted Lua code is
able to suspend the execution (at any time) of the untrusted Lua code
which is sand-boxed. After resuming, the untrusted Lua code is not
even aware that it has been suspended. There are two solutions I found
from the web might be related:

1. asking the untrusted code calling some version of yield() to yield
the control out. This is not an ideal solution for me because I am
providing an environment to allow multiple player writing code to
compete with each other. Some player may write a dead loop
intentionally to hang the whole environment, if they know the are able
to do it. I am not assuming that the player is cooperative.

2. Use scriptTimeLimit (http://tinyurl.com/d638mx). This could help to
detect the timeout. however, this approach still taking the whole
environment as a whole. when the timeout happens, it cannot tell which
piece of untrusted code is hanging the system. By that point, the only
optionis to kill the whole system.

If I use dedicated Lua VM instances for each pieces of untrusted code
and write an AS3 scheduler to distribute time slices fairly, I should
be able to implement a "virtual" OS kernel which support multi-
tasking. From untrusted Lua code point of view, the switching is
preemptive (which cannot be achieved within one Lua VM instance). What
I expected is that if one piece of untrusted code has a dead loop, the
AS3 code could still be able to switch out the control to allow AS3
code, trusted Lua code and other pieces of untrusted Lua code use
their own time slice.

In theory this should be achievable since it is just about suspending/
resuming the execution of the bytecode. We don't even need to save the
context because we will not use the VM instance for any other purpose.
In reality it becomes the question about at which level (in which
module) this "hard" suspend/resume will be supported.

I hope I have described my question clearly (If not, forgive me.
English is not my mother language ;-)).

Thanks,

Tim

On Feb 21, 4:20 am, Alexander Gladysh <aglad...@gmail.com> wrote:
> Hi,
>
> On Sat, Feb 21, 2009 at 12:45 AM, gtimwo...@gmail.com
>
>
>
> <gtimwo...@gmail.com> wrote:
> > Is there any possibilities to allow AS3 to pause/resume the execution
> > oz the Lua VM?I am trying to write a programming game (something like
> > GNU robots) in which I define a group of Lua API (such as robot.move
> > (), robot.shoot(), etc.) then let game player to program the robot
> > using Lua. Since the Lua code will be written by user, I cannot
> > implement any game rule to force game player calling yield()
> > periodically inside their code. Ideally a game player assumes they
> > have all the resources of the VM (CPU time, memory and hardware IO -
> > which is the robot at this time). The game will even allow more than
> > one players to submit their code and let their robots compete with
> > each other. In this case multiple instances of Lua VM will be
> > initialized and AS3 will switch the executions of different VM
> > instances.
> > I understand currently lua-alchemy doesn't support this. Is it
> > possible that this feature will be included in future releases?
>
> You need to sandbox user's code. There was a recent thread here on this topic:
>
> http://groups.google.com/group/lua-alchemy-dev/browse_thread/thread/a...

Alexander Gladysh

unread,
Feb 24, 2009, 8:01:17 AM2/24/09
to lua-alc...@googlegroups.com
You should put a Lua hook on maximum allowed instructions count. If
hook is triggered before user's code called, say, coroutine.yield() to
your trusted Lua code, you should consider code to be hanged, and kill
it. Unfortunately, I do not know a way to kill Lua state gracefully in
this case. Looks like you would have to cleanup allocated resources by
hand...

Instruction count hook is possible only with Lua C API which is
currently unavailable to Lua Alchemy user's AS3 code.

I think we may expose something like
lua_alchemy.DoStringLimitInstructionsCount(code, num_instructions)...
This would require some extra though though... Please say if you need
this.

See also:

http://lua-users.org/wiki/NonBlockingLuaExecution

Alexander.

gtim...@gmail.com

unread,
Feb 24, 2009, 11:29:53 AM2/24/09
to lua-alchemy-dev
Thanks Alexander for the pointer. It looks like a pure Lua solution
might work for me, but I need to do more tests...

A new API like lua_alchemy.DoStringLimitInstructionsCount(code,
num_instructions) will be useful for sure, especially when AS3 itself
is not multi-threaded. I would recommend to add this regardless
whether the pure Lua solution works or not.

Thanks,

Tim


On Feb 24, 8:01 am, Alexander Gladysh <aglad...@gmail.com> wrote:
> You should put a Lua hook on maximum allowed instructions count. If
> hook is triggered before user's code called, say, coroutine.yield() to
> your trusted Lua code, you should consider code to be hanged, and kill
> it. Unfortunately, I do not know a way to kill Lua state gracefully in
> this case. Looks like you would have to cleanup allocated resources by
> hand...
>
> Instruction count hook is possible only with Lua C API which is
> currently unavailable to Lua Alchemy user's AS3 code.
>
> I think we may expose something like
> lua_alchemy.DoStringLimitInstructionsCount(code, num_instructions)...
> This would require some extra though though... Please say if you need
> this.
>
> See also:
>
> http://lua-users.org/wiki/NonBlockingLuaExecution
>
> Alexander.
>
> On Tue, Feb 24, 2009 at 12:56 AM, gtimwo...@gmail.com

Alexander Gladysh

unread,
Feb 24, 2009, 3:31:43 PM2/24/09
to lua-alc...@googlegroups.com
On Tue, Feb 24, 2009 at 7:29 PM, gtim...@gmail.com
<gtim...@gmail.com> wrote:
> Thanks Alexander for the pointer. It looks like a pure Lua solution
> might work for me, but I need to do more tests...

If possible, please keep us informed with your progress. This subject
is quite interesting.

> A new API like lua_alchemy.DoStringLimitInstructionsCount(code,
> num_instructions) will be useful for sure, especially when AS3 itself
> is not multi-threaded. I would recommend to add this regardless
> whether the pure Lua solution works or not.

I added an issue to the tracker.

Alexander.

gtim...@gmail.com

unread,
Feb 24, 2009, 4:13:14 PM2/24/09
to lua-alchemy-dev
yes I will get you updated once I reach a conclusion. Thank you for
all the supports.

Tim

On Feb 24, 3:31 pm, Alexander Gladysh <aglad...@gmail.com> wrote:
> On Tue, Feb 24, 2009 at 7:29 PM, gtimwo...@gmail.com

gtim...@gmail.com

unread,
Mar 24, 2009, 10:30:43 AM3/24/09
to lua-alchemy-dev
All right, it took a long time for me to come up with a pure Lua
solution. Here it is:

First of all, with standard Lua engine, there is no way to pause/
resume a lua co-routine without letting it know, even by using
debugger hooks. I guess after all, the idea of yielding control
transparently is kind of contradict to the "co-operative" concept. So
what I could do is to use debugger hook function to count executing
time (number of instructions executed, to be precise) of the executing
co-routine. the co-routine, which is running inside a sandbox, will be
provided a function to check whether the time slice is used up. If
there is no more time available, the co-routine should call yield() to
yield the control out. if the co-routine refused to do so, the next
time when the debugger hook function is called, an exception will be
thrown out, and the co-routine will be simply killed. Putting this to
the game context, this means player who doesn't want to be co-
operative (yielding out periodically) will lose the game. Lua built-in
debugger functions and APis such as pcall will not be available to the
sandboxed co-routine.

I also wrapped yield() and resume() to allow co-routine creating there
own children co-routines. Any time when yield() or resume() is called,
the debugger hook will be updated to time the new co-routine.

I have the working library and sample code ready. I could upload them
if needed.

Tim

Alexander Gladysh

unread,
Mar 26, 2009, 5:10:31 PM3/26/09
to lua-alc...@googlegroups.com
Tim,

Thank you for your report!

> I have the working library and sample code ready. I could upload them
> if needed.

I'm interested in looking at your library. Would you please upload it?
Perhaps as a github project?

Alexander.

gtim...@gmail.com

unread,
Mar 27, 2009, 11:41:06 AM3/27/09
to lua-alchemy-dev
Done. the project name is "lua-process".

Tim

Alexander Gladysh

unread,
Mar 30, 2009, 3:57:24 PM3/30/09
to lua-alc...@googlegroups.com
On Fri, Mar 27, 2009 at 7:41 PM, gtim...@gmail.com
<gtim...@gmail.com> wrote:

> Done. the project name is "lua-process".

Thanks!

For the record, the url is http://github.com/gtimworks/lua-process/

I didn't knew that it is actually possible to set instruction count
hook from the plain Lua. That is a nice trick!

Alexander.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages