From e9936f1c80afb013fbd3dd14549974785b41e589 Mon Sep 17 00:00:00 2001 From: Emagi Date: Sat, 18 Jan 2025 18:54:10 -0500 Subject: [PATCH] script protection on spells against reuse of the lua state between proc / other spell processes --- source/WorldServer/Combat.cpp | 9 +++++++++ source/WorldServer/LuaInterface.h | 1 + source/WorldServer/SpellProcess.cpp | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/source/WorldServer/Combat.cpp b/source/WorldServer/Combat.cpp index a9c6dd0..37f0c04 100644 --- a/source/WorldServer/Combat.cpp +++ b/source/WorldServer/Combat.cpp @@ -1740,6 +1740,9 @@ bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) { if(item_proc) { mutex = lua_interface->GetItemScriptMutex(proc->item->GetItemScript()); } + else if(proc->spell) { + proc->spell->MScriptMutex.writelock(__FUNCTION__, __LINE__); + } if(mutex) mutex->readlock(__FUNCTION__, __LINE__); @@ -1804,6 +1807,9 @@ bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) { mutex->releasereadlock(__FUNCTION__, __LINE__); if(item_proc) lua_interface->UseItemScript(proc->item->GetItemScript(), state, false); + else if(proc->spell) { + proc->spell->MScriptMutex.releasewritelock(__FUNCTION__, __LINE__); + } return false; } } @@ -1815,6 +1821,9 @@ bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) { mutex->releasereadlock(__FUNCTION__); if(item_proc) lua_interface->UseItemScript(proc->item->GetItemScript(), state, false); + else if(proc->spell) { + proc->spell->MScriptMutex.releasewritelock(__FUNCTION__, __LINE__); + } return true; } diff --git a/source/WorldServer/LuaInterface.h b/source/WorldServer/LuaInterface.h index 791e12e..36f42a5 100644 --- a/source/WorldServer/LuaInterface.h +++ b/source/WorldServer/LuaInterface.h @@ -100,6 +100,7 @@ struct LuaSpell{ bool had_triggers; bool had_dmg_remaining; Mutex MSpellTargets; + Mutex MScriptMutex; int32 effect_bitmask; bool restored; // restored spell cross zone std::atomic has_proc; diff --git a/source/WorldServer/SpellProcess.cpp b/source/WorldServer/SpellProcess.cpp index faf14f4..2df13fa 100644 --- a/source/WorldServer/SpellProcess.cpp +++ b/source/WorldServer/SpellProcess.cpp @@ -551,6 +551,8 @@ bool SpellProcess::DeleteCasterSpell(LuaSpell* spell, string reason, bool removi bool SpellProcess::ProcessSpell(LuaSpell* spell, bool first_cast, const char* function, SpellScriptTimer* timer, bool all_targets) { bool ret = false; + + spell->MScriptMutex.writelock(__FUNCTION__, __LINE__); if(!spell->state) { LogWrite(SPELL__ERROR, 0, "Spell", "Error: State is NULL! SpellProcess::ProcessSpell for Spell '%s'", (spell->spell != nullptr) ? spell->spell->GetName() : "Unknown"); @@ -574,6 +576,7 @@ bool SpellProcess::ProcessSpell(LuaSpell* spell, bool first_cast, const char* fu } } } + spell->MScriptMutex.releasewritelock(__FUNCTION__, __LINE__); return true; } std::string functionCall = ApplyLuaFunction(spell, first_cast, function, timer); @@ -584,6 +587,7 @@ bool SpellProcess::ProcessSpell(LuaSpell* spell, bool first_cast, const char* fu else ret = lua_interface->CallSpellProcess(spell, 2 + spell->spell->GetLUAData()->size(), functionCall); } + spell->MScriptMutex.releasewritelock(__FUNCTION__, __LINE__); return ret; }