From f7a4fd6dbd4dcab2d1808056bf97dd5c9f8dcba8 Mon Sep 17 00:00:00 2001 From: Sour Date: Tue, 21 Apr 2020 20:24:44 -0400 Subject: [PATCH] Debugger: Lua - Fixed crash when calling emu.reset() outside callbacks --- Core/LuaApi.cpp | 6 ++++++ Core/LuaScriptingContext.cpp | 1 + Core/ScriptingContext.cpp | 5 +++++ Core/ScriptingContext.h | 2 ++ 4 files changed, 14 insertions(+) diff --git a/Core/LuaApi.cpp b/Core/LuaApi.cpp index 70906f8..044d6e2 100644 --- a/Core/LuaApi.cpp +++ b/Core/LuaApi.cpp @@ -37,6 +37,7 @@ #define errorCond(cond, text) if(cond) { luaL_error(lua, text); return 0; } #define checkparams() if(!l.CheckParamCount()) { return 0; } #define checkminparams(x) if(!l.CheckParamCount(x)) { return 0; } +#define checkinitdone() if(!_context->CheckInitDone()) { error("This function cannot be called outside a callback"); return 0; } #define checksavestateconditions() if(!_context->CheckInStartFrameEvent() && !_context->CheckInExecOpEvent()) { error("This function must be called inside a StartFrame event callback or a CpuExec memory operation callback"); return 0; } Debugger* LuaApi::_debugger = nullptr; @@ -499,6 +500,7 @@ int LuaApi::Reset(lua_State *lua) { LuaCallHelper l(lua); checkparams(); + checkinitdone(); _console->Reset(); return l.ReturnCount(); } @@ -508,6 +510,7 @@ int LuaApi::Stop(lua_State *lua) LuaCallHelper l(lua); int32_t stopCode = l.ReadInteger(0); checkminparams(0); + checkinitdone(); _console->Stop(stopCode); return l.ReturnCount(); } @@ -516,6 +519,7 @@ int LuaApi::Break(lua_State *lua) { LuaCallHelper l(lua); checkparams(); + checkinitdone(); _debugger->Step(CpuType::Cpu, 1, StepType::Step); return l.ReturnCount(); } @@ -524,6 +528,7 @@ int LuaApi::Resume(lua_State *lua) { LuaCallHelper l(lua); checkparams(); + checkinitdone(); _debugger->Run(); return l.ReturnCount(); } @@ -534,6 +539,7 @@ int LuaApi::Execute(lua_State *lua) StepType type = (StepType)l.ReadInteger(); int count = l.ReadInteger(); checkparams(); + checkinitdone(); errorCond(count <= 0, "count must be >= 1"); errorCond(type != StepType::Step && type != StepType::PpuStep, "type is invalid"); diff --git a/Core/LuaScriptingContext.cpp b/Core/LuaScriptingContext.cpp index bf2fd50..04a43c3 100644 --- a/Core/LuaScriptingContext.cpp +++ b/Core/LuaScriptingContext.cpp @@ -84,6 +84,7 @@ bool LuaScriptingContext::LoadScript(string scriptName, string scriptContent, De if((iErr = lua_pcall(_lua, 0, LUA_MULTRET, 0)) == 0) { //Script loaded properly Log("Script loaded successfully."); + _initDone = true; return true; } } diff --git a/Core/ScriptingContext.cpp b/Core/ScriptingContext.cpp index 55bd982..abc86ae 100644 --- a/Core/ScriptingContext.cpp +++ b/Core/ScriptingContext.cpp @@ -59,6 +59,11 @@ int ScriptingContext::CallEventCallback(EventType type) return returnValue; } +bool ScriptingContext::CheckInitDone() +{ + return _initDone; +} + bool ScriptingContext::CheckInStartFrameEvent() { return _inStartFrameEvent; diff --git a/Core/ScriptingContext.h b/Core/ScriptingContext.h index 4519785..6bf5e33 100644 --- a/Core/ScriptingContext.h +++ b/Core/ScriptingContext.h @@ -42,6 +42,7 @@ private: protected: string _scriptName; + bool _initDone = false; vector _callbacks[3]; vector _eventCallbacks[(int)EventType::EventTypeSize]; @@ -71,6 +72,7 @@ public: void CallMemoryCallback(uint32_t addr, uint8_t &value, CallbackType type); int CallEventCallback(EventType type); + bool CheckInitDone(); bool CheckInStartFrameEvent(); bool CheckInExecOpEvent(); bool CheckStateLoadedFlag();