lua-users home
lua-l archive

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

On 11/01/2013 03:47 PM, Peng Zhicheng wrote:
于 2013-11-1 0:04, Bob Schiavo 写道:

Thank you very much Bezobiuk

I was able to implement a small lua code working as you suggest by coroutines. But it works inside lua code. What I meant is a little bit different. I try to explain better.

Suppose I’m working on platform STM32 using Lua embedded in STM32F1xx CPU. Suppose also my main.c file, after the initialization routines, starts by the while(true) cycle, something like the following (take it as a logical description of the code):









lua app_script()               à ( lua_State* L = lua_open(), luaL_openlibs(L), … … and so on )






This is the situation I mean. I don’t know if coroutines method could be applicable to a such situation, to interrupt a while(true) inside the lua app_script. On other words, I don’t think I can insert the resume/yield of a coroutine inside a pure C_functionX() running in my main.c file. Isn’t it?


By the way, do you have any answer about point 1) and 2), regarding the usage of lua over uCLinux, FreeRTOS, or some other RTOSs?






By "interrupt a DEADLOOP inside tha Lua app_script", do you mean that
you want to load UNTRUSTED (potentially malicious) Lua code from outside,
and that you want to prevent the code from hanging your own code?
(i.e. you want to set an upper limit execution time for the that code)?

If so, I think it is possible. You may try this:
Install a LUA_HOOKCOUNT hook into the Lua VM, which would be called after
the VM had executed LIMIT instructions.
If the LIMIT is reached, you may throw an error and the control would return
to your main program.

Don't forget to remove the hook afterwards.

BTW, untrusted code should run in a sandbox.
If the sandbox allows protection points(i.e. pcall),
things might get complicated, but solutions still exist.

Good luck!
I don't just 'think' it is possible, I know it. My scheduler does just this. But usually I don't call lua_error(...) from the hook but lua_yield(...). To make sure my hook doesn't yield the interpreter in a "stupid" moment I introduced a test function into the Lua api:

LUA_API int lua_canyield (lua_State *L) {
    return L->nny == 0;