Skip to content

Add proactive stack overflow detection via MaxCallDepth + tests#317

Open
ModMaker101 wants to merge 1 commit into
nuskey8:mainfrom
ModMaker101:feat/168-stackoverflow-protection
Open

Add proactive stack overflow detection via MaxCallDepth + tests#317
ModMaker101 wants to merge 1 commit into
nuskey8:mainfrom
ModMaker101:feat/168-stackoverflow-protection

Conversation

@ModMaker101

Copy link
Copy Markdown

Description

Adds proactive stack overflow detection to prevent process terminating native StackOverflowException and enable sandboxing use.

Changes

  • Add MaxCallDepth property to LuaState.
  • Add native stack check in Call and TailCall before pushing frames
  • Move logical depth check into PushCallStackFrames to cover all the call sites.
  • Make LuaStackOverflowException public so users can catch it by type.

Tests:

Added the following:

  • ExceedingMaxCallDepth_ThrowsLuaRuntimeException
  • ExceedingMaxCallDepth_WithPCall_ReturnsErrorMessage
  • UnderMaxCallLimit_Succeeds
  • DefaultMaxCallDepth_AllowsDeepRecursion

All tests passed. Ready for review.

@ModMaker101

Copy link
Copy Markdown
Author

And solves #168.

@ModMaker101

Copy link
Copy Markdown
Author

@nuskey8 Mind reviewing this?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds configurable, proactive stack overflow protection to the Lua runtime to avoid process-terminating StackOverflowException and support sandboxing by enforcing a maximum Lua call depth and surfacing a catchable LuaStackOverflowException.

Changes:

  • Introduces LuaState.MaxCallDepth and enforces it when pushing call stack frames.
  • Adds RuntimeHelpers.TryEnsureSufficientExecutionStack() checks in VM CALL / TAILCALL.
  • Makes LuaStackOverflowException public and adds test coverage for max call depth behavior (including pcall).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/Lua.Tests/StackOverflowTests.cs Adds tests validating max call depth enforcement and pcall behavior.
src/Lua/Standard/BasicLibrary.cs Updates pcall exception handling to return a stack overflow message.
src/Lua/Runtime/LuaVirtualMachine.cs Adds stack-availability checks on CALL and TAILCALL.
src/Lua/LuaState.cs Adds MaxCallDepth and enforces it in PushCallStackFrame.
src/Lua/Exceptions.cs Makes LuaStackOverflowException public for user catchability.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +370 to +371
case LuaStackOverflowException:
return context.Return(false, ex.Message);
Comment on lines +1422 to +1425
if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
{
throw new LuaStackOverflowException();
}
Comment on lines +1628 to +1631
if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
{
throw new LuaStackOverflowException();
}
Comment thread src/Lua/LuaState.cs
Comment on lines +200 to +201
public int MaxCallDepth { get; set; } = 100_000;

@akeit0

akeit0 commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

I would appreciate it if you could measure the performance changes using recursive call benchmarks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants