From 2a01e0e6eba9222fd3d58d35d6d57484a2a73051 Mon Sep 17 00:00:00 2001 From: Shuai Date: Wed, 18 Oct 2023 14:30:49 +0800 Subject: [PATCH] init --- src/Neo.VM/ExecutionEngine.cs | 13 +++++++++++++ src/Neo.VM/ExecutionEngineLimits.cs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/Neo.VM/ExecutionEngine.cs b/src/Neo.VM/ExecutionEngine.cs index ab344b32..4c4f562a 100644 --- a/src/Neo.VM/ExecutionEngine.cs +++ b/src/Neo.VM/ExecutionEngine.cs @@ -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); } + + /// + /// Used memory size + /// + public long MemorySize { get; private set; } } } diff --git a/src/Neo.VM/ExecutionEngineLimits.cs b/src/Neo.VM/ExecutionEngineLimits.cs index 553d7ee8..cf2580ff 100644 --- a/src/Neo.VM/ExecutionEngineLimits.cs +++ b/src/Neo.VM/ExecutionEngineLimits.cs @@ -38,6 +38,11 @@ public sealed record ExecutionEngineLimits /// public uint MaxItemSize { get; init; } = 1024 * 1024; + /// + /// The maximum size of used memory in the VM. + /// + public uint MaxMemorySize { get; init; } = 100 * 1024 * 1024; + /// /// The largest comparable size. If a or exceeds this size, comparison operations on it cannot be performed in the VM. ///