Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Lua/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static string GetMessageWithNearToken(string message, string? nearToken)

public class LuaUndumpException(string message) : Exception(message);

class LuaStackOverflowException() : Exception("stack overflow")
public class LuaStackOverflowException() : Exception("stack overflow")
{
public override string ToString()
{
Expand Down
7 changes: 7 additions & 0 deletions src/Lua/LuaState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public void Release()
public LuaTable PreloadModules => GlobalState.PreloadModules;
public LuaState MainThread => GlobalState.MainThread;

public int MaxCallDepth { get; set; } = 100_000;

Comment on lines +200 to +201
public ILuaModuleLoader? ModuleLoader
{
get => GlobalState.ModuleLoader;
Expand Down Expand Up @@ -285,6 +287,11 @@ int callerInstructionIndex
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void PushCallStackFrame(in CallStackFrame frame)
{
if (CallStackFrameCount >= MaxCallDepth)
{
throw new LuaStackOverflowException();
}

CurrentException?.BuildOrGet();
CurrentException = null;
ref var callStack = ref CoreData!.CallStack;
Expand Down
11 changes: 11 additions & 0 deletions src/Lua/Runtime/LuaVirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,11 @@ static bool Call(VirtualMachineExecutionContext context, out bool doRestart)

var newFrame = func.CreateNewFrame(context, newBase, RA, variableArgumentCount);

if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
{
throw new LuaStackOverflowException();
}
Comment on lines +1422 to +1425

state.PushCallStackFrame(newFrame);
if (state.CallOrReturnHookMask.Value != 0 && !context.State.IsInHook)
{
Expand Down Expand Up @@ -1619,6 +1624,12 @@ static bool TailCall(VirtualMachineExecutionContext context, out bool doRestart)
);
newBase = context.FrameBase + variableArgumentCount;
stack.PopUntil(newBase + argumentCount);

if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
{
throw new LuaStackOverflowException();
}
Comment on lines +1628 to +1631

var lastFrame = state.GetCurrentFrame();
state.LastPc = context.Pc;
state.LastCallerFunction = lastFrame.Function;
Expand Down
2 changes: 2 additions & 0 deletions src/Lua/Standard/BasicLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ CancellationToken cancellationToken
throw;
case OperationCanceledException:
throw new LuaCanceledException(context.State, cancellationToken, ex);
case LuaStackOverflowException:
return context.Return(false, ex.Message);
Comment on lines +370 to +371
case LuaRuntimeException luaEx:
{
if (
Expand Down
87 changes: 87 additions & 0 deletions tests/Lua.Tests/StackOverflowTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Lua.Standard;

namespace Lua.Tests;

public class StackOverflowTests
{
[Test]
public void ExceedingMaxCallDepth_ThrowsLuaRuntimeException()
{
var state = LuaState.Create();
state.MaxCallDepth = 5;

var ex = Assert.ThrowsAsync<LuaRuntimeException>(async () =>
await state.DoStringAsync(
"""
local function f()
f()
end
f()
"""
).AsTask()
);

Assert.That(ex!.InnerException, Is.TypeOf<LuaStackOverflowException>());
}

[Test]
public async Task ExceedingMaxCallDepth_WithPCall_ReturnsErrorMessage()
{
var state = LuaState.Create();
state.OpenStandardLibraries();
state.MaxCallDepth = 5;

var result = await state.DoStringAsync(
"""
local function f()
f()
end
local ok, msg = pcall(f)
return ok, msg
"""
);

Assert.That(result, Has.Length.EqualTo(2));
Assert.That(result[0], Is.EqualTo(new LuaValue(false)));
Assert.That(result[1].Read<string>(), Does.Contain("stack overflow"));
}

[Test]
public async Task UnderMaxCallLimit_Succeeds()
{
var state = LuaState.Create();
state.MaxCallDepth = 100;

var result = await state.DoStringAsync(
"""
local function f(n)
if n <= 0 then return 'done' end
return f(n - 1)
end
return f(10)
"""
);

Assert.That(result, Has.Length.EqualTo(1));
Assert.That(result[0], Is.EqualTo(new LuaValue("done")));
}

[Test]
public async Task DefaultMaxCallDepth_AllowsDeepRecursion()
{
var state = LuaState.Create();

var result = await state.DoStringAsync(
"""
local function f(n)
if n <= 0 then return n end
return f(n - 1)
end
return f(1000)
"""
);

Assert.That(result, Has.Length.EqualTo(1));
Assert.That(result[0], Is.EqualTo(new LuaValue(0)));
}
}