From 792205a0c8c679629a64916aa6cd5d7fefc68ff7 Mon Sep 17 00:00:00 2001 From: Hecate2 <2474101468@qq.com> Date: Wed, 9 Oct 2024 15:11:39 +0800 Subject: [PATCH 1/3] handle https://github.com/neo-project/neo/pull/3520 --- src/Neo.VM/ExecutionEngine.cs | 2 +- src/Neo.VM/JumpTable/JumpTable.Compound.cs | 1 + src/Neo.VM/ReferenceCounter.cs | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Neo.VM/ExecutionEngine.cs b/src/Neo.VM/ExecutionEngine.cs index bec60c4348..d5ae88ee2e 100644 --- a/src/Neo.VM/ExecutionEngine.cs +++ b/src/Neo.VM/ExecutionEngine.cs @@ -288,7 +288,7 @@ public T Pop() where T : StackItem /// protected virtual void PostExecuteInstruction(Instruction instruction) { - if (ReferenceCounter.Count < Limits.MaxStackSize) return; + if (ReferenceCounter.Count <= Limits.MaxStackSize) return; if (ReferenceCounter.CheckZeroReferred() > Limits.MaxStackSize) throw new InvalidOperationException($"MaxStackSize exceed: {ReferenceCounter.Count}"); } diff --git a/src/Neo.VM/JumpTable/JumpTable.Compound.cs b/src/Neo.VM/JumpTable/JumpTable.Compound.cs index 817b460cf9..038bbf609c 100644 --- a/src/Neo.VM/JumpTable/JumpTable.Compound.cs +++ b/src/Neo.VM/JumpTable/JumpTable.Compound.cs @@ -119,6 +119,7 @@ public virtual void Unpack(ExecutionEngine engine, Instruction instruction) default: throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {compound.Type}"); } + engine.ReferenceCounter.TryCleanTrackedItem(compound); engine.Push(compound.Count); } diff --git a/src/Neo.VM/ReferenceCounter.cs b/src/Neo.VM/ReferenceCounter.cs index f9ea08a2e2..82bf565870 100644 --- a/src/Neo.VM/ReferenceCounter.cs +++ b/src/Neo.VM/ReferenceCounter.cs @@ -292,5 +292,16 @@ internal void RemoveStackReference(StackItem item) if (--item.StackReferences == 0) _zeroReferred.Add(item); } + + internal bool TryCleanTrackedItem(StackItem item) + { + if (item.StackReferences > 0 || item.ObjectReferences?.Values.Any(p => p.References > 0 && p.Item.OnStack) == true) + // Still referred by stack or some compound item + return false; + if (item is CompoundType compound) + _referencesCount -= compound.SubItemsCount; + item.Cleanup(); + return _trackedItems.Remove(item); + } } } From 97bcd019bc398b38514db204454c7e587ed99697 Mon Sep 17 00:00:00 2001 From: Hecate2 <2474101468@qq.com> Date: Thu, 17 Oct 2024 18:16:09 +0800 Subject: [PATCH 2/3] fix interface --- src/Neo.VM/IReferenceCounter.cs | 1 + src/Neo.VM/ReferenceCounter.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Neo.VM/IReferenceCounter.cs b/src/Neo.VM/IReferenceCounter.cs index f451c0e316..faf850226f 100644 --- a/src/Neo.VM/IReferenceCounter.cs +++ b/src/Neo.VM/IReferenceCounter.cs @@ -87,5 +87,6 @@ public interface IReferenceCounter /// /// The current reference count. int CheckZeroReferred(); + bool TryCleanTrackedItem(CompoundType compound); } } diff --git a/src/Neo.VM/ReferenceCounter.cs b/src/Neo.VM/ReferenceCounter.cs index 976c93e2e1..c5c2b30987 100644 --- a/src/Neo.VM/ReferenceCounter.cs +++ b/src/Neo.VM/ReferenceCounter.cs @@ -247,7 +247,7 @@ public void RemoveStackReference(StackItem item) _zeroReferred.Add(item); } - internal bool TryCleanTrackedItem(StackItem item) + public bool TryCleanTrackedItem(CompoundType item) { if (item.StackReferences > 0 || item.ObjectReferences?.Values.Any(p => p.References > 0 && p.Item.OnStack) == true) // Still referred by stack or some compound item From 3e699017b32735a3e16c77797eb702c0b8638474 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sat, 26 Oct 2024 01:26:06 +0800 Subject: [PATCH 3/3] Update src/Neo.VM/ExecutionEngine.cs --- src/Neo.VM/ExecutionEngine.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo.VM/ExecutionEngine.cs b/src/Neo.VM/ExecutionEngine.cs index 972ccf4d60..8a3167b4c8 100644 --- a/src/Neo.VM/ExecutionEngine.cs +++ b/src/Neo.VM/ExecutionEngine.cs @@ -289,7 +289,7 @@ public T Pop() where T : StackItem /// protected virtual void PostExecuteInstruction(Instruction instruction) { - if (ReferenceCounter.Count <= Limits.MaxStackSize) return; + if (ReferenceCounter.Count < Limits.MaxStackSize) return; if (ReferenceCounter.CheckZeroReferred() > Limits.MaxStackSize) throw new InvalidOperationException($"MaxStackSize exceed: {ReferenceCounter.Count}"); }