Add proactive stack overflow detection via MaxCallDepth + tests#317
Open
ModMaker101 wants to merge 1 commit into
Open
Add proactive stack overflow detection via MaxCallDepth + tests#317ModMaker101 wants to merge 1 commit into
ModMaker101 wants to merge 1 commit into
Conversation
Author
|
And solves #168. |
Author
|
@nuskey8 Mind reviewing this? |
Contributor
There was a problem hiding this comment.
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.MaxCallDepthand enforces it when pushing call stack frames. - Adds
RuntimeHelpers.TryEnsureSufficientExecutionStack()checks in VMCALL/TAILCALL. - Makes
LuaStackOverflowExceptionpublic and adds test coverage for max call depth behavior (includingpcall).
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 on lines
+200
to
+201
| public int MaxCallDepth { get; set; } = 100_000; | ||
|
|
Collaborator
|
I would appreciate it if you could measure the performance changes using recursive call benchmarks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds proactive stack overflow detection to prevent process terminating native
StackOverflowExceptionand enable sandboxing use.Changes
MaxCallDepthproperty to LuaState.CallandTailCallbefore pushing framesPushCallStackFramesto cover all the call sites.Tests:
Added the following:
ExceedingMaxCallDepth_ThrowsLuaRuntimeExceptionExceedingMaxCallDepth_WithPCall_ReturnsErrorMessageUnderMaxCallLimit_SucceedsDefaultMaxCallDepth_AllowsDeepRecursionAll tests passed. Ready for review.