Safe Lua invoke C++ registered function - c++

Hey,everyone! I've a C++ app embedded Lua as script. A non-programmer edits the Lua script, then the C++ app invoke the Lua script and the Lua script also invokes C++ registered function.
I use Luaplus to do the above job. My question is: when the script-editor makes mistakes such as misspelling the the parameter, the C++ app crashes! What can I do to prevent this happening? thanks

Look at lua_cpcall and lua_pcall. They both allow protected function calls of lua in c. If they return a non-negative number then the call failed and the lua stack contains only the error string. In cpcalls case the stack is otherwise unmodified. For pcall you'll need to look at lua_pushcclosure to invoke a cfunction safely.
What you do is this: you create a c function with all of the lua_* calls you want, such as loadfile and dofile. You call this function using lua_cpcall or lua_pushcclosure amd lua_pcall. This allows you to detect if an error occured in t
he function you passed to cpcall.
Examples:
function hello() {
string hello_ = "Hello Lua!";
struct C {
static int call(lua_State* L) {
C *p = static_cast<C*>(lua_touserdata(L,-1));
lua_pushstring(L, p->str.c_str() );
lua_getglobal(L, "print");
lua_call(L, 1, 0); //ok
lua_pushstring(L, p->str.c_str() );
lua_getglobal(L, "notprint");
lua_call(L, 1, 0); //error -> longjmps
return 0; //Number of values on stack to 'return' to lua
}
const string& str;
} p = { hello_ };
//protected call of C::call() above
//with &p as 1st/only element on Lua stack
//any errors encountered will trigger a longjmp out of lua and
//return a non-0 error code and a string on the stack
//A return of 0 indicates success and the stack is unmodified
//to invoke LUA functions safely use the lua_pcall function
int res = lua_cpcall(L, &C::call, &p);
if( res ) {
string err = lua_tostring(L, -1);
lua_pop(L, 1);
//Error hanlder here
}
//load a .lua file
if( (res=luaL_loadfile(L, "myLuaFile.lua")) ) {
string err = lua_tostring(L, -1);
lua_pop(L, 1);
//res is one of
//LUA_ERRSYNTAX - Lua syntax error
//LUA_ERRMEM - Out of memory error
//LUE_ERRFILE - File not found/accessible error
}
//execute it
if( (res=lua_pcall(L,0,0,0)) ) {
string err = lua_tostring(L, -1);
lua_pop(L, 1);
// res is one of
// LUA_ERRRUN: a runtime error.
// LUA_ERRMEM: memory allocation error.
// LUA_ERRERR: error while running the error handler function (NULL in this case).
}
// try to call [a_int,b_str] = Foo(1,2,"3")
lua_getglobal(L,"Foo");
if( lua_isfunction(L,lua_gettop(L)) ) { //Foo exists
lua_pushnumber(L,1);
lua_pushnumber(L,2);
lua_pushstring(L,"3");
lua_pushvalue(L, -4); //copy of foo()
if( (res = lua_pcall(L, 3, 2, 0/*default error func*/)) ) {
string err = lua_tostring(L, -1);
lua_pop(L, 1);
//error: see above
}
int a_int = (int)lua_tointeger(L,-2);
string b_str = lua_tostring(L,-1);
lua_pop(L,2+1); //2 returns, + extra copy of Foo()
}
}

Related

Lua 5.2 Sandboxing in different objects with C API

Consider the following C++ code using the Lua C API:
#include <string>
#include <cassert>
#include <lua/lua.hpp>
class AwesomeThing
{
lua_State* _lua;
std::string _name;
public:
AwesomeThing(lua_State* L, const std::string& name, const std::string& luafile)
: _lua{ L },
_name{ name }
{
assert(luaL_loadfile(_lua, luafile.c_str()) == 0); // 1:chunk
lua_newtable(_lua); // 1:chunk, 2:tbl
lua_newtable(_lua); // 1:chunk, 2:tbl, 3:tbl(mt)
lua_getglobal(_lua, "_G"); // 1:chunk, 2: tbl, 3:tbl(mt), 4:_G
lua_setfield(_lua, 3, "__index"); // 1:chunk, 2: tbl, 3:tbl(mt)
lua_setmetatable(_lua, 2); // 1:chunk, 2: tbl
lua_setupvalue(_lua, -2, 1); // 1:chunk
if (lua_pcall(_lua, 0, 0, 0) != 0) // compiled chunk
{
auto error = lua_tostring(_lua, -1);
throw std::runtime_error(error);
}
lua_setglobal(_lua, _name.c_str()); // empty stack
}
void init()
{
lua_getglobal(_lua, _name.c_str()); // 1:env
assert(lua_isnil(_lua, 1) == 0);
lua_getfield(_lua, 1, "onInit"); // 1:env, 2:func
assert(lua_isnil(_lua, 2) == 0);
assert(lua_isfunction(_lua, 2) == 1);
assert(lua_pcall(_lua, 0, LUA_MULTRET, 0) == 0); // 1:env, 2:retval
lua_pop(_lua, 1); // -1:env
lua_pop(_lua, 1); // empty stack
assert(lua_gettop(_lua) == 0);
}
};
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
AwesomeThing at1(L, "thing1", "file1.lua");
AwesomeThing at2(L, "thing2", "file2.lua");
at1.init();
at2.init();
return 0;
}
With two very basic Lua files:
file1.lua
function onInit()
print("init file1")
end
file2.lua
function onInit()
print("init file2")
end
As is, I get an error in at2's constructor call at lua_pcall: attempt to call table value
When I comment out all references/calls to at2, I instead get an error in at1's init() at lua_getfield(_lua, 1, "onInit"): PANIC: unprotected error in call to Lua API (attempt to index a nil value)
I feel like there's something fundamental I'm missing in the way I'm handling the sandboxing. I've tried my best to follow a few other Lua 5.2 sandboxing examples I've found online, but so far nothing has helped.
After messing around with the code myself, I was able to fix it and the errors seem to come from just a few errors.
lua_pcall pops the called function from the stack, but in both cases in your code you assume the function is still on the stack after lua_pcall. This results in bad stack manipulation.
In the constructor, you apparently try to store a reference to the chunk (function) instead of the environment table. This doesn't even work though, because the function was already popped. If it did work, the lua_getfield call in init() wouldn't work as intended since the chunk doesn't have a field named onInit -- the environment table does.
Fixing the constructor involves creating the environment table and loading the chunk in the opposite order, so that the environment table is left on the stack after the function call:
lua_newtable(_lua); // 1:tbl
assert(luaL_loadfile(_lua, luafile.c_str()) == 0); // 1:tbl, 2:chunk
lua_newtable(_lua); // 1:tbl, 2:chunk, 3:tbl(mt)
lua_getglobal(_lua, "_G"); // 1:tbl, 2:chunk, 3:tbl(mt), 4:_G
lua_setfield(_lua, 3, "__index"); // 1:tbl, 2:chunk, 3:tbl(mt)
lua_setmetatable(_lua, 1); // 1:tbl, 2:chunk
lua_pushvalue(_lua, 1); // 1:tbl, 2:chunk, 3:tbl
lua_setupvalue(_lua, -2, 1); // 1:tbl, 2:chunk
if (lua_pcall(_lua, 0, 0, 0) != 0) // compiled chunk
{
auto error = lua_tostring(_lua, -1);
throw std::runtime_error(error);
}
// 1:tbl
lua_setglobal(_lua, _name.c_str()); // empty stack
Then in init(), since you use LUA_MULTRET, just clear the stack by replacing both pop calls with lua_settop(_lua, 0).

Multiple Lua environments issue

I'm having trouble on creating multiple Lua environments to run multiple similar scripts in the same lua_State.
The program crashes with the following error
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
Here's the Script.Compile and Script.runFunc methods:
void Script::Compile() {
if (m_filename.IsEmpty()) return;
if (m_filename.Contains("res:")) {
astd::String file = m_filename.Replace("res:", "");
astd::String filecontent = AEIO::OpenTextFromAssetPack(file);
m_lua->RunString(filecontent);
} else {
m_lua->RunScript(m_filename);
}
astd::String id = astd::String("file") + astd::String(UID);
lua_setfield(m_lua->State(), LUA_REGISTRYINDEX, id.CStr());
UID++;
}
void Script::runFunc(astd::String func) {
astd::String id = astd::String("file") + astd::String(UID);
lua_State* state = m_lua->State();
// Weird behaviour during debug.
// The debugger goes back and forth on these 2 lines.
lua_getfield(state, LUA_REGISTRYINDEX, id.CStr());
lua_getfield(state, -1, func.CStr()); // CRASH!
if (lua_isfunction(state, -1)) {
lua_pcall(state, 0, 0, 0);
}
luabind::globals(state)["self"] = m_owner;
}
And this is the basic structure of the scripts:
print("Script Created") --Gets printed before the error occurs.
function onInit()
print("Script Initialized")
end
function onUpdate()
print("Script Update")
end
And it only fails when I call runFunc("onInit") or runFunc("onUpdate").
Thanks in advance.

Using lua_call a lot of times in c++ function

first of all I'm sorry for my english.
My question is about how to use lua_call more than one time in C++ function. I have a program that uses lua as primary language, but it accept c++ plugins to add functionalities. I want to call a LUA function from c++, and call that c++ function in LUA Runtime.
I want to write a c++ function with a progress while is working, then pass this progress to a LUA function which is responsible to show that progress to user.
For now I've a test function in LUA:
function ShowText(text)
Dialog.Message("Hi", text);
return true;
end
and a c++ function:
static int Test(lua_State *L){
lua_pushstring(L, "Hi There");
lua_call(L, 1, 1);
lua_pushstring(L, "Again");
lua_call(L, 1, 1);
return 0;
}
Then i call this function from LUA using:
Test.Test(ShowText);
All works fine with the first lua_call but then the LUA pile is cleared, function dissapear and the second lua_call try to use the return boolean of first call instead function.
i want something like this:
static int Test(lua_State *L){
int total = 10;
for (int j; j<total; j++){
lua_pushnumber(L, j);
lua_pushnumber(L, j);
lua_call(L, 2, 1);
bool continue = IRLUA_PLUGIN_CheckBoolean(L, -1);
lua_pop(L, 1); //Delete the last boolean
if (continue == false){
break;
}
}
return 0;
}
and in LUA:
function ShowProgress(actual, final)
local percent = (actual/final)*100;
Dialog.Message("Working", "I'm in "..actual.." from "..final.." ("..percent.."%)");
return true;
end
NOTES:
Dialog.Message is a function of the program tha i'm using to show a message. Is like MessageBox(NULL, Text, Title, MB_OK); in c++.
IRLUA_PLUGIN_CheckBoolean is a function of plugin SDK that check if argument is booleand and return its value, or return an error if not.
I can do it with lua_getfield(L, LUA_GLOBALSINDEX , "FunctionName");, but is not what i want.
Someone knows how to do it?
You have understood the problem well. Here is how you fix it.
In your first example, lua_call pops the function from the stack so you need to duplicate it first. Also, the boolean returned by the function is useless, so you need to pop it or just not ask it to lua_call by setting the last argument to 0:
static int Test(lua_State *L) {
lua_pushvalue(L, 1); /* duplicate function */
lua_pushstring(L, "Hi There");
lua_call(L, 1, 0);
lua_pushstring(L, "Again");
lua_call(L, 1, 0);
return 0;
}
Now applying that to your second example:
static int Test(lua_State *L) {
int total = 10;
for (int j = 0; j<total; j++) {
lua_pushvalue(L, 1); /* duplicate function */
lua_pushnumber(L, j);
lua_pushnumber(L, total);
lua_call(L, 2, 1);
bool keep_going = IRLUA_PLUGIN_CheckBoolean(L, -1);
lua_pop(L, 1); /* pop the boolean */
if (keep_going == false) {
break;
}
}
return 0;
}
(I have fixed a few other issues with your code: the second number passed should probably be total, not j, you don't want to use continue as a variable name...)

Implementing lua callbacks for a menu system

In our menu system, we define menus in xml with lua chunks used for callbacks for menu component events. Currently, everytime a script callback is called, we call lua_loadstring which is quite slow. I'm trying to make it so this only happens once, when the menu is loaded.
My initial thought was to maintain a lua table per menu component and to do the following to add a new callback function to the table:
//create lua code that will assign a function to our table
std::string callback = "temp." + callbackName + " = function (" + params + ")" + luaCode + "end";
//push table onto stack
lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);
//pop table from stack and set it as value of global "temp"
lua_setglobal(L, "temp");
//push new function onto stack
int error = luaL_loadstring(L, callback.c_str());
if ( error )
{
const char* errorMsg = lua_tostring(L, -1);
Dbg::Printf("error loading the script '%s' : %s\n", callbackName, errorMsg);
lua_pop(L,1);
return;
}
//call the lua code to insert the loaded function into the global temp table
if (lua_pcall(L, 0, 0, 0))
{
Dbg::Printf("luascript: error running the script '%s'\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
//table now has function in it
This seems kind of dirty. Is there a better way that allows me to assign the function to the table directly from a lua chunk without having to use a temp global variable and running lua_pcall?
If you want to put the function in a table, then put the function in the table. It seems that your Lua-stack-fu is not strong; consider studying the manual a bit more closely.
Anyway, I'd say the biggest problem you have is your insistence on params. Callback functions should expect to be varadic; they take ... as their parameters. If they want to get those values, they should use locals like this:
local param1, param2 = ...;
But if you insist on allowing them to specify a list of parameters, you may do the following:
std::string luaChunk =
//The ; is here instead of a \n so that the line numbering
//won't be broken by the addition of this code.
"local " + params + " = ...; " +
luaCode;
lua_checkstack(L, 3);
lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);
if(lua_isnil(L, -1))
{
//Create the table if it doesn't already exist.
lua_newtable(L);
//Put it in the registry.
lua_rawseti(L, LUA_REGISTRYINDEX, luaTableRef_);
//Get it back, since setting it popped it.
lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);
}
//The table is on the stack. Now put the key on the stack.
lua_pushlstring(L, callbackName.c_str(), callbackName.size());
//Load up our function.
int error = luaL_loadbuffer(L, luaChunk.c_str(), luaChunk.size(),
callbackName.c_str());
if( error )
{
const char* errorMsg = lua_tostring(L, -1);
Dbg::Printf("error loading the script '%s' : %s\n", callbackName, errorMsg);
//Pop the function name and the table.
lua_pop(L, 2);
return;
}
//Put the function in the table.
lua_settable(L, -3);
//Remove the table from the stack.
lua_pop(L, 1);

Lua toString exception for boolean value

I wrote a simple lua function which uses 'C++' function to execute. As my intention of creating a 'C++' function is to use the same across all lua functions to update the 'C++' variables. It works fine for numbers, but when I tried it for boolean values, it give me exception when convert to string.
Here is my code snippet.
C++ code.
#include <lua.hpp>
/* the Lua interpreter */
lua_State *luaState;
std::map<lua_State *, CLuaTest *> luaFbtlookup;
void CLuaTest::CLuaTest() {
// initialize Lua
luaState = luaL_newstate();
lua_register(luaState, "get_value", get_value); // func to get values
lua_register(luaState, "set_value", set_value); // func to set values
// load Lua base libraries
luaL_openlibs(luaState);
luaL_dofile(luaState, "C:\LuaTest.lua");
luaFbtlookup.insert(make_pair(luaState, this));
}
int get_value(lua_State *L);
int set_value(lua_State *L);
extern "C++" int get_value(lua_State *L)
{
string lightName = lua_tostring(L, 1);
FbTLuaLookup::iterator iter = luaFbtlookup.find(L);
if (iter != luaFbtlookup.end()) {
lua_pushstring(L, iter->second->getValueFrom(lightName).c_str());
return 1; // do not return zero
}
return 1;
}
extern "C++" int set_value(lua_State *L)
{
string lightName = lua_tostring(L, 1);
if (NULL == lua_tostring(L, 2))
{
printf("WARNING : Invalid String Argument / Cannot convert arg#2 to string");
}
else {
string value = lua_tostring(L, 2);
FbTLuaLookup::iterator iter = luaFbtlookup.find(L);
if (iter != luaFbtlookup.end()) {
iter->second->setValueTo(lightName, value);
lua_pushnumber(L, true);
return 1; // do not return zero
}
}
return 1;
}
CLuaTest::ExecuteScript(enum Seq) {
switch(Seq) {
case 0:
lua_getglobal(luaState, "AllLightsOff");
break;
case 1:
lua_getglobal(luaState, "RedLightOn");
break;
case 2:
lua_getglobal(luaState, "AmberLightOn");
break;
case 3:
lua_getglobal(luaState, "GreenLightOn");
break;
}
}
My lua script:
function AllLightsOff
set_value("RedLight", 0)
set_value("AmberLight",0)
set_value("GrenLight",0)
end
function RedLightOn
set_value("RedLight", 1)
set_value("AmberLight",0)
set_value("GrenLight",0)
end
function AmberLightOn
set_value("RedLight", 0)
set_value("AmberLight",1)
set_value("GrenLight",0)
end
function GreenLightOn
set_value("RedLight", 0)
set_value("AmberLight",0)
set_value("GrenLight",1)
end
Things work fine with the above code, but when I tried to change the set value to use boolean values like set_value("RedLight", False) I get warning message printing. Should I need to pass False as string?
Lua doesn't have False, so it simply tries to read global _G["False"] which is of course nil.
It has false keyword, however, but it wouldn't work either: lua_tostring is working only for numbers and strings.
We don't see setValueTo code, so it is hard to guess how it works.
If you simply want to pass bool value to it, use lua_toboolean, but be aware that it treats 0 as true (as Lua in general).
If you want to pass "True" or "False" as strings, then yes, you must write set_value("RedLight", "False")
As a side note, consider reading how to implement lua_CFunction protocol. Now, if get_value can't find lightName, it will return the last passed parameter as its result.