Using Lua with and without SO

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Lua with and without SO

Bob Schiavo

Hello everybody

I’m a beginner of Lua, and I was wondering, looking at the use of Lua in environment with and without operating system, the following questions:

 

Usage of Lua with SO

1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e freeRTOS)?

2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs a porting, and how the porting can be done (is there some document/user guide that describes how to do that for the two SO above)?

 

Usage of Lua without SO

a)      Looking at eLua, that is Lua for embedded application on different platforms and CPUs, if I well understood, I have a complete description of the microcontroller and its resources. Supposing that I need only the interpreter to execute some script Lua, without any description of the platform resources (I mean no platform/…/FwLib sources files):

Do I have to configure the building toolchain for eLua to skip the FwLib modules, or could Lua (not eLua) be ported directly on the platform?

 

b)      I try to explain in poor words what I’m evaluating, so I might not be so clear as I would be.

Assuming to have a scheduler that executes sequentially a script Lua (appl_script) and some other C functions, totally independent by the execution of the lua script. Something like the following (just a logical representation of the scheduler):

While(true)

{

               Execution of lua app_script();

               Execution of C_function1();

               …

               Execution of C_functionN();

}

If the lua script itself has a while(true), the others functions will never be executed.

Is there the possibility, in a such situation, to interrupt the execution of the lua script, passing the control to the other functions of the scheduler, and resume the script next time it will be executed from the point it left off?

 

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua with and without SO

Justin Cormack
On Tue, Oct 29, 2013 at 3:10 PM, Bob Schiavo
<[hidden email]> wrote:

> Hello everybody
>
> I’m a beginner of Lua, and I was wondering, looking at the use of Lua in
> environment with and without operating system, the following questions:
>
>
>
> Usage of Lua with SO
>
> 1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e
> freeRTOS)?
>
> 2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs
> a porting, and how the porting can be done (is there some document/user
> guide that describes how to do that for the two SO above)?
>
>
>
> Usage of Lua without SO
>
> a)      Looking at eLua, that is Lua for embedded application on different
> platforms and CPUs, if I well understood, I have a complete description of
> the microcontroller and its resources. Supposing that I need only the
> interpreter to execute some script Lua, without any description of the
> platform resources (I mean no platform/…/FwLib sources files):
>
> Do I have to configure the building toolchain for eLua to skip the FwLib
> modules, or could Lua (not eLua) be ported directly on the platform?
>
>
>
> b)      I try to explain in poor words what I’m evaluating, so I might not
> be so clear as I would be.
>
> Assuming to have a scheduler that executes sequentially a script Lua
> (appl_script) and some other C functions, totally independent by the
> execution of the lua script. Something like the following (just a logical
> representation of the scheduler):
>
> While(true)
>
> {
>
>                Execution of lua app_script();
>
>                Execution of C_function1();
>
>                …
>
>                Execution of C_functionN();
>
> }
>
> If the lua script itself has a while(true), the others functions will never
> be executed.
>
> Is there the possibility, in a such situation, to interrupt the execution of
> the lua script, passing the control to the other functions of the scheduler,
> and resume the script next time it will be executed from the point it left
> off?

You can port Lua directly. It needs a certain amount of libc to work,
but you can stub out some of it if you eg do not need all the standard
libraries (say you have no useful io and will use something else). You
do need memory allocation and so on. It does depend on what your
environment has.

For the second part, it sounds like you want a scheduler, but again
depending on your platform you might be able to use interrupts and
multiple stacks.

Which environment are you looking at?

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua with and without SO

Volodymyr Bezobiuk
In reply to this post by Bob Schiavo
Hi Bob,

for the b) case there are decent Lua coroutines around. You can resume/yield back from Lua function wrapped in coroutine. So you'll probably get not while(true) in Lua part, but rather while(coroutine.yield(your_value)) - at this point you pass to the C caller your values (like returning them) and wait for resume from C part. That's looks like ping-pong. C part resume coroutine, coroutine do something, and return values to C. The C part may resume coroutine passing false to it, indicating that Lua part should finish (and probably clean up resources properly). Try to read some coroutine tutorials and experiment with them to get it handy. Note that coroutines not intended specifically for interaction with C, they are standard Lua library and can be used when both the caller and coroutine are Lua code, or with Lua caller and C method coroutine-wrapped.


On Tue, Oct 29, 2013 at 5:10 PM, Bob Schiavo <[hidden email]> wrote:

Hello everybody

I’m a beginner of Lua, and I was wondering, looking at the use of Lua in environment with and without operating system, the following questions:

 

Usage of Lua with SO

1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e freeRTOS)?

2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs a porting, and how the porting can be done (is there some document/user guide that describes how to do that for the two SO above)?

 

Usage of Lua without SO

a)      Looking at eLua, that is Lua for embedded application on different platforms and CPUs, if I well understood, I have a complete description of the microcontroller and its resources. Supposing that I need only the interpreter to execute some script Lua, without any description of the platform resources (I mean no platform/…/FwLib sources files):

Do I have to configure the building toolchain for eLua to skip the FwLib modules, or could Lua (not eLua) be ported directly on the platform?

 

b)      I try to explain in poor words what I’m evaluating, so I might not be so clear as I would be.

Assuming to have a scheduler that executes sequentially a script Lua (appl_script) and some other C functions, totally independent by the execution of the lua script. Something like the following (just a logical representation of the scheduler):

While(true)

{

               Execution of lua app_script();

               Execution of C_function1();

               …

               Execution of C_functionN();

}

If the lua script itself has a while(true), the others functions will never be executed.

Is there the possibility, in a such situation, to interrupt the execution of the lua script, passing the control to the other functions of the scheduler, and resume the script next time it will be executed from the point it left off?

 


Reply | Threaded
Open this post in threaded view
|

R: Using Lua with and without SO

Bob Schiavo
In reply to this post by Justin Cormack
Many thanks for your fast reply
I'm evaluating to use STM32 platform.



-----Messaggio originale-----
Da: [hidden email] [mailto:[hidden email]] Per conto di Justin Cormack
Inviato: martedì 29 ottobre 2013 16:35
A: Lua mailing list
Oggetto: Re: Using Lua with and without SO

On Tue, Oct 29, 2013 at 3:10 PM, Bob Schiavo <[hidden email]> wrote:

> Hello everybody
>
> I’m a beginner of Lua, and I was wondering, looking at the use of Lua
> in environment with and without operating system, the following questions:
>
>
>
> Usage of Lua with SO
>
> 1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e
> freeRTOS)?
>
> 2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs
> a porting, and how the porting can be done (is there some
> document/user guide that describes how to do that for the two SO above)?
>
>
>
> Usage of Lua without SO
>
> a)      Looking at eLua, that is Lua for embedded application on different
> platforms and CPUs, if I well understood, I have a complete
> description of the microcontroller and its resources. Supposing that I
> need only the interpreter to execute some script Lua, without any
> description of the platform resources (I mean no platform/…/FwLib sources files):
>
> Do I have to configure the building toolchain for eLua to skip the
> FwLib modules, or could Lua (not eLua) be ported directly on the platform?
>
>
>
> b)      I try to explain in poor words what I’m evaluating, so I might not
> be so clear as I would be.
>
> Assuming to have a scheduler that executes sequentially a script Lua
> (appl_script) and some other C functions, totally independent by the
> execution of the lua script. Something like the following (just a
> logical representation of the scheduler):
>
> While(true)
>
> {
>
>                Execution of lua app_script();
>
>                Execution of C_function1();
>
>                …
>
>                Execution of C_functionN();
>
> }
>
> If the lua script itself has a while(true), the others functions will
> never be executed.
>
> Is there the possibility, in a such situation, to interrupt the
> execution of the lua script, passing the control to the other
> functions of the scheduler, and resume the script next time it will be
> executed from the point it left off?

You can port Lua directly. It needs a certain amount of libc to work, but you can stub out some of it if you eg do not need all the standard libraries (say you have no useful io and will use something else). You do need memory allocation and so on. It does depend on what your environment has.

For the second part, it sounds like you want a scheduler, but again depending on your platform you might be able to use interrupts and multiple stacks.

Which environment are you looking at?



Reply | Threaded
Open this post in threaded view
|

Re: Using Lua with and without SO

Thomas Jericke
In reply to this post by Bob Schiavo
On 10/29/2013 03:10 PM, Bob Schiavo wrote:

Hello everybody

I’m a beginner of Lua, and I was wondering, looking at the use of Lua in environment with and without operating system, the following questions:

 

Usage of Lua with SO

1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e freeRTOS)?

2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs a porting, and how the porting can be done (is there some document/user guide that describes how to do that for the two SO above)?

 

(...)

 

b)      I try to explain in poor words what I’m evaluating, so I might not be so clear as I would be.

Assuming to have a scheduler that executes sequentially a script Lua (appl_script) and some other C functions, totally independent by the execution of the lua script. Something like the following (just a logical representation of the scheduler):

While(true)

{

               Execution of lua app_script();

               Execution of C_function1();

               …

               Execution of C_functionN();

}

If the lua script itself has a while(true), the others functions will never be executed.

Is there the possibility, in a such situation, to interrupt the execution of the lua script, passing the control to the other functions of the scheduler, and resume the script next time it will be executed from the point it left off?

 

Hi
porting Lua to embedded devices and even microcontrollers is possible. What you need to care of is a memory allocation.

A scheduler that does what you want is actually possible, you can use resume and yield for this kind of scheduler. I will actually talk exactly about this in my speech at the Lua workshop.
--
Thomas
Reply | Threaded
Open this post in threaded view
|

R: Using Lua with and without SO

Bob Schiavo
In reply to this post by Volodymyr Bezobiuk

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):

 

Main()

{

Init_system();

  …

  …

  while(true)

{

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

C_function1();

C_functionN();

}

}

 

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?

 

Regards

Bob

 

 

Da: [hidden email] [[hidden email]] Per conto di Bezobiuk Volodymyr
Inviato: martedì 29 ottobre 2013 16:35
A: Lua mailing list
Oggetto: Re: Using Lua with and without SO

 

Hi Bob,

for the b) case there are decent Lua coroutines around. You can resume/yield back from Lua function wrapped in coroutine. So you'll probably get not while(true) in Lua part, but rather while(coroutine.yield(your_value)) - at this point you pass to the C caller your values (like returning them) and wait for resume from C part. That's looks like ping-pong. C part resume coroutine, coroutine do something, and return values to C. The C part may resume coroutine passing false to it, indicating that Lua part should finish (and probably clean up resources properly). Try to read some coroutine tutorials and experiment with them to get it handy. Note that coroutines not intended specifically for interaction with C, they are standard Lua library and can be used when both the caller and coroutine are Lua code, or with Lua caller and C method coroutine-wrapped.

 

On Tue, Oct 29, 2013 at 5:10 PM, Bob Schiavo <[hidden email]> wrote:

Hello everybody

I’m a beginner of Lua, and I was wondering, looking at the use of Lua in environment with and without operating system, the following questions:

 

Usage of Lua with SO

1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e freeRTOS)?

2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs a porting, and how the porting can be done (is there some document/user guide that describes how to do that for the two SO above)?

 

Usage of Lua without SO

a)      Looking at eLua, that is Lua for embedded application on different platforms and CPUs, if I well understood, I have a complete description of the microcontroller and its resources. Supposing that I need only the interpreter to execute some script Lua, without any description of the platform resources (I mean no platform/…/FwLib sources files):

Do I have to configure the building toolchain for eLua to skip the FwLib modules, or could Lua (not eLua) be ported directly on the platform?

 

b)      I try to explain in poor words what I’m evaluating, so I might not be so clear as I would be.

Assuming to have a scheduler that executes sequentially a script Lua (appl_script) and some other C functions, totally independent by the execution of the lua script. Something like the following (just a logical representation of the scheduler):

While(true)

{

               Execution of lua app_script();

               Execution of C_function1();

               …

               Execution of C_functionN();

}

If the lua script itself has a while(true), the others functions will never be executed.

Is there the possibility, in a such situation, to interrupt the execution of the lua script, passing the control to the other functions of the scheduler, and resume the script next time it will be executed from the point it left off?

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Using Lua with and without SO

Volodymyr Bezobiuk
You place lua_open(), lua_openlibs(), and create new coroutine by lua_newthread() before the main loop in C code. Then for sure you can resume/yield coroutine, created by lua_newthread() with lua_resume() and lua_yield(). It is really the right way. You can read about it in Lua manual and in the 'Programming in Lua' book.

Sincerely,
  Volodymyr.



On Thu, Oct 31, 2013 at 6:04 PM, Bob Schiavo <[hidden email]> wrote:

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):

 

Main()

{

Init_system();

  …

  …

  while(true)

{

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

C_function1();

C_functionN();

}

}

 

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?

 

Regards

Bob

 

 

Da: [hidden email] [[hidden email]] Per conto di Bezobiuk Volodymyr


Inviato: martedì 29 ottobre 2013 16:35
A: Lua mailing list
Oggetto: Re: Using Lua with and without SO

 

Hi Bob,

for the b) case there are decent Lua coroutines around. You can resume/yield back from Lua function wrapped in coroutine. So you'll probably get not while(true) in Lua part, but rather while(coroutine.yield(your_value)) - at this point you pass to the C caller your values (like returning them) and wait for resume from C part. That's looks like ping-pong. C part resume coroutine, coroutine do something, and return values to C. The C part may resume coroutine passing false to it, indicating that Lua part should finish (and probably clean up resources properly). Try to read some coroutine tutorials and experiment with them to get it handy. Note that coroutines not intended specifically for interaction with C, they are standard Lua library and can be used when both the caller and coroutine are Lua code, or with Lua caller and C method coroutine-wrapped.

 

On Tue, Oct 29, 2013 at 5:10 PM, Bob Schiavo <[hidden email]> wrote:

Hello everybody

I’m a beginner of Lua, and I was wondering, looking at the use of Lua in environment with and without operating system, the following questions:

 

Usage of Lua with SO

1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e freeRTOS)?

2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs a porting, and how the porting can be done (is there some document/user guide that describes how to do that for the two SO above)?

 

Usage of Lua without SO

a)      Looking at eLua, that is Lua for embedded application on different platforms and CPUs, if I well understood, I have a complete description of the microcontroller and its resources. Supposing that I need only the interpreter to execute some script Lua, without any description of the platform resources (I mean no platform/…/FwLib sources files):

Do I have to configure the building toolchain for eLua to skip the FwLib modules, or could Lua (not eLua) be ported directly on the platform?

 

b)      I try to explain in poor words what I’m evaluating, so I might not be so clear as I would be.

Assuming to have a scheduler that executes sequentially a script Lua (appl_script) and some other C functions, totally independent by the execution of the lua script. Something like the following (just a logical representation of the scheduler):

While(true)

{

               Execution of lua app_script();

               Execution of C_function1();

               …

               Execution of C_functionN();

}

If the lua script itself has a while(true), the others functions will never be executed.

Is there the possibility, in a such situation, to interrupt the execution of the lua script, passing the control to the other functions of the scheduler, and resume the script next time it will be executed from the point it left off?

 

 


Reply | Threaded
Open this post in threaded view
|

Re: Using Lua with and without SO

Bob Schiavo
Thanks Volodymyr. I've seen those C API just aftet sent my mail. 
Now I' m going to do some experiment in my code. 
Thanks again for your hints and courtesy. 

Bob

Inviato da iPhone

Il giorno 31/ott/2013, alle ore 22:19, Bezobiuk Volodymyr <[hidden email]> ha scritto:

You place lua_open(), lua_openlibs(), and create new coroutine by lua_newthread() before the main loop in C code. Then for sure you can resume/yield coroutine, created by lua_newthread() with lua_resume() and lua_yield(). It is really the right way. You can read about it in Lua manual and in the 'Programming in Lua' book.

Sincerely,
  Volodymyr.



On Thu, Oct 31, 2013 at 6:04 PM, Bob Schiavo <[hidden email]> wrote:

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):

 

Main()

{

Init_system();

  …

  …

  while(true)

{

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

C_function1();

C_functionN();

}

}

 

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?

 

Regards

Bob

 

 

Da: [hidden email] [[hidden email]] Per conto di Bezobiuk Volodymyr


Inviato: martedì 29 ottobre 2013 16:35
A: Lua mailing list
Oggetto: Re: Using Lua with and without SO

 

Hi Bob,

for the b) case there are decent Lua coroutines around. You can resume/yield back from Lua function wrapped in coroutine. So you'll probably get not while(true) in Lua part, but rather while(coroutine.yield(your_value)) - at this point you pass to the C caller your values (like returning them) and wait for resume from C part. That's looks like ping-pong. C part resume coroutine, coroutine do something, and return values to C. The C part may resume coroutine passing false to it, indicating that Lua part should finish (and probably clean up resources properly). Try to read some coroutine tutorials and experiment with them to get it handy. Note that coroutines not intended specifically for interaction with C, they are standard Lua library and can be used when both the caller and coroutine are Lua code, or with Lua caller and C method coroutine-wrapped.

 

On Tue, Oct 29, 2013 at 5:10 PM, Bob Schiavo <[hidden email]> wrote:

Hello everybody

I’m a beginner of Lua, and I was wondering, looking at the use of Lua in environment with and without operating system, the following questions:

 

Usage of Lua with SO

1.       Is it possible to embed Lua into uCLinux, or some other RTOSs (i.e freeRTOS)?

2.       If yes, is Lua embeddable “as it is” (I don’t think so) or it needs a porting, and how the porting can be done (is there some document/user guide that describes how to do that for the two SO above)?

 

Usage of Lua without SO

a)      Looking at eLua, that is Lua for embedded application on different platforms and CPUs, if I well understood, I have a complete description of the microcontroller and its resources. Supposing that I need only the interpreter to execute some script Lua, without any description of the platform resources (I mean no platform/…/FwLib sources files):

Do I have to configure the building toolchain for eLua to skip the FwLib modules, or could Lua (not eLua) be ported directly on the platform?

 

b)      I try to explain in poor words what I’m evaluating, so I might not be so clear as I would be.

Assuming to have a scheduler that executes sequentially a script Lua (appl_script) and some other C functions, totally independent by the execution of the lua script. Something like the following (just a logical representation of the scheduler):

While(true)

{

               Execution of lua app_script();

               Execution of C_function1();

               …

               Execution of C_functionN();

}

If the lua script itself has a while(true), the others functions will never be executed.

Is there the possibility, in a such situation, to interrupt the execution of the lua script, passing the control to the other functions of the scheduler, and resume the script next time it will be executed from the point it left off?

 

 


Reply | Threaded
Open this post in threaded view
|

Re: R: Using Lua with and without SO

Peng Zhicheng
In reply to this post by Bob Schiavo
于 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):

 

Main()

{

Init_system();

  …

  …

  while(true)

{

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

C_function1();

C_functionN();

}

}

 

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?

 

Regards

Bob

 


 


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!
Reply | Threaded
Open this post in threaded view
|

Re: R: Using Lua with and without SO

Thomas Jericke
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):

 

Main()

{

Init_system();

  …

  …

  while(true)

{

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

C_function1();

C_functionN();

}

}

 

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?

 

Regards

Bob

 


 


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;
}

--
Thomas
Reply | Threaded
Open this post in threaded view
|

RE: R: Using Lua with and without SO

Thijs Schreijer
In reply to this post by Peng Zhicheng
> 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.

There are some caveats here. See corowatch [1] for a Lua example, and check its readme for the caveats

Thijs

[1] https://github.com/Tieske/corowatch
Reply | Threaded
Open this post in threaded view
|

R: R: Using Lua with and without SO

Bob Schiavo
Dear Thijs
After some test, thanks to your hints, I've got what I was trying to do. Great!!

Thank you very much
Bob




-----Messaggio originale-----
Da: [hidden email] [mailto:[hidden email]] Per conto di Thijs Schreijer
Inviato: venerdì 1 novembre 2013 20:21
A: Lua mailing list
Oggetto: RE: R: Using Lua with and without SO

> 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.

There are some caveats here. See corowatch [1] for a Lua example, and check its readme for the caveats

Thijs

[1] https://github.com/Tieske/corowatch


---
Questa e-mail è priva di virus e malware perché è attiva la protezione avast! Antivirus.
http://www.avast.com


Reply | Threaded
Open this post in threaded view
|

RE: R: Using Lua with and without SO

Thijs Schreijer


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of Bob Schiavo
> Sent: maandag 25 november 2013 10:47
> To: 'Lua mailing list'
> Subject: R: R: Using Lua with and without SO
>
> Dear Thijs
> After some test, thanks to your hints, I've got what I was trying to do.
> Great!!
>
> Thank you very much
> Bob
>
>
> -----Messaggio originale-----
> Da: [hidden email] [mailto:[hidden email]] Per
> conto di Thijs Schreijer
> Inviato: venerdì 1 novembre 2013 20:21
> A: Lua mailing list
> Oggetto: RE: R: Using Lua with and without SO
>
> > 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.
>
> There are some caveats here. See corowatch [1] for a Lua example, and
> check its readme for the caveats
>
> Thijs
>
> [1] https://github.com/Tieske/corowatch
>

In some of the presentations at the Lua workshop the HOOKCOUNT was also used to check for infinite loops etc. I think the McRobot [1] and the BACnet [2] had that mentioned in their presentations. In my own code [3], so far, I check the coroutine and kill (and log) it when it takes too long and continue.

But on my way home I gave it some more thought and actually came to the conclusion that it is a fairly dangerous thing to do. Whenever a coroutine has access to some shared data/element in the system, just terminating it might leave the system in some _unknown inconsistent state_. Therefore the only right thing to do is log as much info as needed to inform the code owner of the problem, and then terminate the application.

Thijs

[1] http://www.lua.org/wshop13.html#Jericke
[2] http://www.lua.org/wshop13.html#Schlephorst 
[3] https://github.com/Tieske/corowatch