Skip to content

Commit

Permalink
Do a check if a local variable exceeds the available stack space.
Browse files Browse the repository at this point in the history
Windows stack is 1 MB so play it safe and allow 512 kb as max. stack space for a single function.
  • Loading branch information
coelckers committed Oct 25, 2024
1 parent 59dab44 commit ee6991e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/common/scripting/backend/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12640,6 +12640,15 @@ FxExpression *FxLocalVariableDeclaration::Resolve(FCompileContext &ctx)
if (ValueType->RegType == REGT_NIL && ValueType != TypeAuto)
{
auto sfunc = static_cast<VMScriptFunction *>(ctx.Function->Variants[0].Implementation);

const unsigned MAX_STACK_ALLOC = 512 * 1024; // Windows stack is 1 MB, but we cannot go up there without problems
if (uint64_t(ValueType->Size) + uint64_t(sfunc->ExtraSpace) > MAX_STACK_ALLOC)
{
ScriptPosition.Message(MSG_ERROR, "%s exceeds max. allowed size of 512kb for local variables at variable %s", sfunc->Name.GetChars(), Name.GetChars());
delete this;
return nullptr;
}

StackOffset = sfunc->AllocExtraStack(ValueType);

if (Init != nullptr)
Expand Down

1 comment on commit ee6991e

@RicardoLuis0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of erroring out, wouldn't it be better to just print a warning and set blockJit to true in the containing function to force it to execute in the VM, like functions that overflow the max register count?

Please sign in to comment.