Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack pointer validation for call function/method opcodes #237

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cleo_plugins/MemoryOperations/MemoryOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ class MemoryOperations
SCRIPT_VAR* arguments_end = arguments + numArg;
numPop *= 4; // bytes peer argument
DWORD result;
int oriSp, postSp; // stack validation
_asm
{
mov oriSp, esp

// transfer args to stack
lea ecx, arguments
call_func_loop :
Expand All @@ -179,6 +182,22 @@ class MemoryOperations
call func
mov result, eax // get result
add esp, numPop // cleanup stack

mov postSp, esp
}

// validate stack pointer
if (postSp != oriSp)
{
_asm
{
mov esp, oriSp // fix stack pointer
}

int diff = oriSp - postSp;
int requiredPop = (numPop + diff) / 4;
SHOW_ERROR("Function call left stack position changed (%s%d). This usually happens when incorrect calling convention is used. \nArgument 'pop' value should have been %d in script %s \nScript suspended.", diff > 0 ? "+" : "", diff, requiredPop, CLEO::ScriptInfoStr(thread).c_str());
return thread->Suspend();
}

if (returnArg)
Expand Down