Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Add buffer limit #512

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions src/Neo.VM/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,20 @@ protected virtual void PreExecuteInstruction(Instruction instruction) { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Push(StackItem item)
{
if (item is Buffer buff)
{
MemorySize += buff.Size;
if (MemorySize > Limits.MaxMemorySize)
{
throw new InvalidOperationException($"MaxMemorySize exceed: {MemorySize}");
}
}
CurrentContext!.EvaluationStack.Push(item);
}

/// <summary>
/// Used memory size
/// </summary>
public long MemorySize { get; private set; }
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/ExecutionEngineLimits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public sealed record ExecutionEngineLimits
/// </summary>
public uint MaxItemSize { get; init; } = 1024 * 1024;

/// <summary>
/// The maximum size of used memory in the VM.
/// </summary>
public uint MaxMemorySize { get; init; } = 100 * 1024 * 1024;

/// <summary>
/// The largest comparable size. If a <see cref="Types.ByteString"/> or <see cref="Types.Struct"/> exceeds this size, comparison operations on it cannot be performed in the VM.
/// </summary>
Expand Down