lua-users home
lua-l archive

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


Hi!

> > What is the current "state of Lua nation" on non-blocking script
> > execution?
> >
> > http://lua-users.org/wiki/NonBlockingLuaExecution
> >
> > What is considered to be the best way to prevent untrusted Lua code
> > from hanging up the system?

I do it this way in an irc-bot:

void script_Watchdog(lua_State *L, lua_Debug *Dummy)
{
	char buffer[MSGLEN];
	struct aLuaScriptInfo *LSI;
	int i;
	const char *msg;
	struct aBot *backup;

	LSI = script_LuaFindByState(L);
	i = LSI->Watchdog_MaxInstructions;
	snprintf(buffer, sizeof(buffer), "LUA script watchdog alert (%i instructions 
reached)!", i);
	luaL_where(L, 0);
	lua_pushstring(L, buffer);
	lua_concat(L, 2);
	if (LSI->Watchdog_PartylineMsg)
	{
		msg = lua_tostring(L, -1);
		backup = current;
		current = LSI->context;
		send_statmsg("%s", nullstr((char*)msg));
		current = backup;
	}
	lua_error(L);	/* never returns */
}

the send_statmsg() gives a informative report about script name, line number 
and file name where the error (this time: the Watchdog alert) occured.

When calling a script function i do:

if (LSI->Watchdog_MaxInstructions)
lua_sethook(LSI->L,script_Watchdog,LUA_MASKCOUNT,LSI->Watchdog_MaxInstructions);
...
i = lua_pcall(LSI->L, pnum, 1, 0);
...

The watchdog event gets captured like any other error (well, i dont check the 
time but only the execution counter).

ciao,

  Torsten