Multiple related lua states

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

Multiple related lua states

Greg McCreath

Hi All,

 

I am investigating using lua in an embedded environment where application separation and security is very important.  The environment runs multiple logical applications within a single proprietary VM.

 

Each logical application has a virtual file system and cache etc etc.  Applications will be able to create and run script.  Lua seems very good for providing scripting in this environment.   Except …

 

I want a lot of common scripted functionality to be available to all applications.  But I do not want to have multiple lua states each with a lot of code loaded.

 

I want a single lua state with all common functions and data loaded in a central location and then each application to be able to operate on its **own** ‘virtual’ lua state without knowledge of other applications.  This really does have to be secure.  I do not want applications altering the global in any way unless it is using a function that exists in the global state.

 

As the application shuts down I also want to be able to get of an applications data and application state.

 

Can I achieve this? 

 

Greg McCreath
Chief Technical Officer
TAFMO Limited
ABN:  94 109 766 592

Level 8, 342 Flinders Street
Melbourne
Victoria, 3000
Australia

http://www.tafmo.com
Ph
: +61 (0) 3 9018 6824
Fax : +61 (0) 3 9018 6899
Mobile : +61 (0) 401 988 957

 


This email and any files transmitted with it may be confidential and are intended solely for the use of the individual or entity to whom they are addressed. This email may contain personal information of individuals, and be subject to Commonwealth and/or State privacy laws in Australia. This email is also subject to copyright. If you are not the intended recipient, you must not read, print, store, copy, forward or use this email for any reason, in accordance with privacy and copyright laws. If you have received this email in error, please notify the sender by return email, and delete this email from your inbox.

Reply | Threaded
Open this post in threaded view
|

RE: Multiple related lua states

Couwenberg, Wim
Hi,
 
I wondered about this same question for a multi-state system we've been developing.  Since functions can have upvalues, they are less likely candidates to share over separate states than you'd expect.  But maybe function prototypes could be shared?  There is a fundamental issue: how to recognise that function prototypes are actually the same?  E.g. if I require "foo" in two separate states, how is it decided that a lot of function prototypes can be shared?  Maybe by interning prototypes like it is done for strings, only shared over states?  Another issue is thread safety and garbage collection on such shared structures.
 
--Wim

I want a single lua state with all common functions and data loaded in a central location and then each application to be able to operate on its **own** ‘virtual’ lua state without knowledge of other applications.  This really does have to be secure.  I do not want applications altering the global in any way unless it is using a function that exists in the global state.


This message and attachment(s) are intended solely for the use of the addressee and may contain information that is privileged, confidential or otherwise exempt from disclosure under applicable law. If you are not the intended recipient or agent thereof responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender immediately by telephone and with a "reply" message. Thank you for your cooperation.
Reply | Threaded
Open this post in threaded view
|

RE: Multiple related lua states

Greg McCreath
In reply to this post by Greg McCreath

Thanks Andrew,

 

I’m quite new to lua (just investigating) so excuse me if my questions are a bit tame.

 

Does using lua threads actually imply that I must use a thread?  In the model I am suggesting, only one application is ‘live’ at a time – you can imagine a main menu with apps hanging off it.  You go into each app one at a time.  No application concurrency.

 

Can application access the threads global table (and by implication) access another application’s data?  Or can this be prottected in the manner you have described with the global table?

 

Many thanks,

 

Greg.

 

PS.  I have cc’d this back to the mainling list.

 


From: Andrew Teirney [mailto:[hidden email]]
Sent: Wednesday, 24 May 2006 4:08 PM
To: Greg McCreath
Subject: Re: Multiple related lua states

 

Hi Greg,

    I myself have been aspiring to do something like this for stuff i work on in the embedded hardware/pos sector. I think there are several approaches to doing this, ultimately i think the implemenation comes down to what the embedded environment supports and what level of performance you are wanting.

    The approach i would probably take is using system threads and implement the lua_lock/lua_unlock macros to protect the atomic regions of the lua core. For each logical application i would create a new lua state off the master state, this being done with lua_newthread. At this point here you have a new thread of lua execution that has nothing within its global state. from there i would use metamethods to enable access to the "system" global table when the "logical application" global table doesn't have the required entry. Depending on your requirements you may/or may not need to provide means via metamethods to mutate the behaviour of the "local application" table to prevent overriding the global table entries, also i would have to suggest that the "system" global table and its children are protected from accidental overwritting/addition using metamethods and perhaps a proxy table.

    Naturally since i have not implemented this, just have done the research in the past it may or may not fit the requirements you have.

    As for when the application shuts down, getting access to the applications data/state that would be simply stored in the "threads" global table. So you can interogate that prior to the thread being destroyed, ultimately the threads global table would be garbage collected, so unless persisted you'd need to maintain a reference to it should you want to be able to reuse it.

    Alternatively if you don't support a preemptive multi threaded system and are happy with cooperative multi threading you could use coroutines and a simple scheduler.

    Hopefully this has provided some for of help to get to where you want to.

--
Andrew
Auckland, New Zealand

Greg McCreath wrote:

Hi All,

 

I am investigating using lua in an embedded environment where application separation and security is very important.  The environment runs multiple logical applications within a single proprietary VM.

 

Each logical application has a virtual file system and cache etc etc.  Applications will be able to create and run script.  Lua seems very good for providing scripting in this environment.   Except …

 

I want a lot of common scripted functionality to be available to all applications.  But I do not want to have multiple lua states each with a lot of code loaded.

 

I want a single lua state with all common functions and data loaded in a central location and then each application to be able to operate on its **own** ‘virtual’ lua state without knowledge of other applications.  This really does have to be secure.  I do not want applications altering the global in any way unless it is using a function that exists in the global state.

 

As the application shuts down I also want to be able to get of an applications data and application state.

 

Can I achieve this? 

 

Greg McCreath
Chief Technical Officer
TAFMO Limited
ABN:  94 109 766 592

Level 8, 342 Flinders Street
Melbourne

Victoria, 3000
Australia

http://www.tafmo.com
Ph
: +61 (0) 3 9018 6824
Fax : +61 (0) 3 9018 6899
Mobile : +61 (0) 401 988 957

 


This email and any files transmitted with it may be confidential and are intended solely for the use of the individual or entity to whom they are addressed. This email may contain personal information of individuals, and be subject to Commonwealth and/or State privacy laws in Australia. This email is also subject to copyright. If you are not the intended recipient, you must not read, print, store, copy, forward or use this email for any reason, in accordance with privacy and copyright laws. If you have received this email in error, please notify the sender by return email, and delete this email from your inbox.


This email and any files transmitted with it may be confidential and are intended solely for the use of the individual or entity to whom they are addressed. This email may contain personal information of individuals, and be subject to Commonwealth and/or State privacy laws in Australia. This email is also subject to copyright. If you are not the intended recipient, you must not read, print, store, copy, forward or use this email for any reason, in accordance with privacy and copyright laws. If you have received this email in error, please notify the sender by return email, and delete this email from your inbox.

Reply | Threaded
Open this post in threaded view
|

Re: Multiple related lua states

Luiz Henrique de Figueiredo
> Does using lua threads actually imply that I must use a thread?  In the
> model I am suggesting, only one application is 'live' at a time - you
> can imagine a main menu with apps hanging off it.  You go into each app
> one at a time.  No application concurrency.
>
> Can application access the threads global table (and by implication)
> access another application's data?  Or can this be prottected in the
> manner you have described with the global table?

If there is no concurrency, then you may try to create a single parent
state containing your static data and functions and then load each
"application" into a separate child state for which the global table
has been changed to an empty table with an __index metamethod pointing
to the parent global table. All static data will be seen in the child
state but they'll be protected against wrtiting.
--lhf
Reply | Threaded
Open this post in threaded view
|

Re: Multiple related lua states

Jose Luis Hidalgo Valiño
Well, I use that code in a ScriptManager class which creates a  
initial lua state, so I can
put in there global variables or functions. Once the initial lua  
state is created, you can add
children with something like this:

        Script *ScriptManager::createScript()
        {
                int top = lua_gettop(L);
                Script *script = 0;
                lua_getfield(L, LUA_REGISTRYINDEX, "Scripts"); // top + 1

                lua_State *thread = lua_newthread(L); // top + 2
                script = new Script(thread); // this is the new lua-state for that  
script

                lua_newtable(L);  // a global table for this script
                lua_newtable(L);  // metatable
                lua_getfenv(L,top+2); // that returns the global table (we are  
going to protect)
                lua_setfield(L, -2, "__index"); // set global table as __index of  
the thread
                lua_setmetatable(L, -2);

                lua_setfenv(L,top+2);  // set env of the new thread

                lua_pushlightuserdata(L, script); // key, the pointer to my own  
Script class
                lua_pushvalue(L, top+2);          // value, the new lua thread.
                lua_rawset(L,top+1); // Scripts table
                lua_settop(L,top);

                _scriptCount++;

                return script;
        }

Script is a class that handles a single lua-state ( in fact it  
doesn't know that is a thread)
By the way, the ScriptManager stores it's children scripts in the  
registry in a table called "Scripts" indexed by the
pointer of  the new Script class instance.

Jose L.



El 24/05/2006, a las 13:49, Luiz Henrique de Figueiredo escribió:

>> Does using lua threads actually imply that I must use a thread?  
>> In the
>> model I am suggesting, only one application is 'live' at a time - you
>> can imagine a main menu with apps hanging off it.  You go into  
>> each app
>> one at a time.  No application concurrency.
>>
>> Can application access the threads global table (and by implication)
>> access another application's data?  Or can this be prottected in the
>> manner you have described with the global table?
>
> If there is no concurrency, then you may try to create a single parent
> state containing your static data and functions and then load each
> "application" into a separate child state for which the global table
> has been changed to an empty table with an __index metamethod pointing
> to the parent global table. All static data will be seen in the child
> state but they'll be protected against wrtiting.
> --lhf