From a9ab2a2be4a75d4d6422f1c611eb76b79ab1a2a1 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 3 Nov 2009 04:01:57 +0000 Subject: [PATCH] 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 --- .todo | 1 - Engine.cc | 19 +++++++++++++++++++ Engine.h | 2 ++ tests/managed_objects.lua | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.todo b/.todo index a546004..e69de29 100644 --- a/.todo +++ b/.todo @@ -1 +0,0 @@ -- debug hooks for loading and execution to halt infinite lua loops diff --git a/Engine.cc b/Engine.cc index 80c594d..1b3563f 100644 --- a/Engine.cc +++ b/Engine.cc @@ -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: diff --git a/Engine.h b/Engine.h index 4f1b2be..97b1301 100644 --- a/Engine.h +++ b/Engine.h @@ -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 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; diff --git a/tests/managed_objects.lua b/tests/managed_objects.lua index 90342d7..fb7e6e5 100644 --- a/tests/managed_objects.lua +++ b/tests/managed_objects.lua @@ -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)