added count hook to prevent infinite lua loops from running the engine forever

git-svn-id: svn://anubis/anaglym/trunk@151 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-11-03 04:01:57 +00:00
parent 7e17b0f220
commit a9ab2a2be4
4 changed files with 25 additions and 1 deletions

1
.todo
View File

@ -1 +0,0 @@
- debug hooks for loading and execution to halt infinite lua loops

View File

@ -30,6 +30,8 @@ using namespace std;
do { EVENT_PRESENT_FLAG(event) = false; } while(0)
#define WHITESPACE " \t\r\n\f"
#define HOOK_TIMEOUT 10000u
#define HOOK_STEPS 250000000
#define FP_EQ(a,b) (fabsf(a - b) < 0.0001)
@ -63,6 +65,11 @@ static void cross_product(dVector3 result, dVector3 first, dVector3 second)
result[2] = first[0] * second[1] - first[1] * second[0];
}
static void debug_hook(lua_State * L, lua_Debug * debug)
{
g_engine->debug_hook(debug);
}
/******** Engine functions ********/
Engine::Engine(const string & path, Video & video)
@ -84,6 +91,7 @@ Engine::Engine(const string & path, Video & video)
m_autoEndFrame = true;
m_autoDrawObjects = true;
m_fileLoader = new EngineFileLoader(this);
m_event_time = 0;
size_t pos = path.find_last_of("\\/");
m_engine_path = (pos != string::npos) ? string(path, 0, pos) : ".";
@ -131,6 +139,7 @@ bool Engine::load(const char * program)
if (s == 0)
{
// execute Lua program
lua_sethook(m_luaState, ::debug_hook, LUA_MASKCOUNT, HOOK_STEPS);
s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
}
else
@ -426,6 +435,15 @@ GLuint Engine::loadTexture(const char * name)
return m_textureCache.load(path, *m_fileLoader);
}
void Engine::debug_hook(lua_Debug * debug)
{
Uint32 ticks = SDL_GetTicks();
if (ticks - m_event_time > HOOK_TIMEOUT)
{
::exit(3);
}
}
/* called by SDL when the update timer expires */
Uint32 Engine::updateCallback(Uint32 interval, void * param)
{
@ -452,6 +470,7 @@ void Engine::run()
SDL_Event event;
while (SDL_WaitEvent(&event))
{
m_event_time = SDL_GetTicks();
switch (event.type)
{
case SDL_KEYDOWN:

View File

@ -133,6 +133,7 @@ class Engine
void exit();
bool import(const char * name);
GLuint loadTexture(const char * name);
void debug_hook(lua_Debug * debug);
/* lua services */
int setCamera(lua_State * L);
@ -177,6 +178,7 @@ class Engine
std::map<std::string, bool> m_keysDown;
SDL_Event m_updateEvent;
SDL_Event m_exitEvent;
Uint32 m_event_time;
bool m_event_update_present;
bool m_event_key_down_present;

View File

@ -1,4 +1,8 @@
while (9)
do
end
ground = ag.createStaticPlane(0, 0, 0, 0, 0, 0)
ground:setColor(0.2, 1.0, 0.2)
ag.setCamera(10, -10, 10, 0, 0, 0)