Skip to content

Commit

Permalink
Fix GetTrace crash when memory is not yet resized
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej committed Dec 2, 2022
1 parent d2aefed commit 640ab75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/Nethermind/Nethermind.Evm.Test/EvmPooledMemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using FluentAssertions;
using Nethermind.Core.Test.Builders;
using Nethermind.Int256;
using NUnit.Framework;
Expand Down Expand Up @@ -86,5 +87,13 @@ public void Load_should_update_size_of_memory()
Assert.AreNotEqual(initialSize, memory.Size);
Assert.AreEqual(expectedResult, result.ToArray());
}

[Test]
public void GetTrace_should_not_thor_on_not_initialized_memory()
{
EvmPooledMemory memory = new();
memory.CalculateMemoryCost(0, 32);
memory.GetTrace().Should().BeEquivalentTo(new string[] { "0000000000000000000000000000000000000000000000000000000000000000" });
}
}
}
25 changes: 16 additions & 9 deletions src/Nethermind/Nethermind.Evm/EvmPooledMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class EvmPooledMemory : IEvmMemory

private int _lastZeroedSize;

private byte[] _memory;
private byte[]? _memory;
public ulong Length { get; private set; }
public ulong Size { get; private set; }

Expand All @@ -29,7 +29,7 @@ public void SaveWord(in UInt256 location, Span<byte> word)

if (word.Length < WordSize)
{
Array.Clear(_memory, (int)location, WordSize - word.Length);
Array.Clear(_memory!, (int)location, WordSize - word.Length);
}

word.CopyTo(_memory.AsSpan((int)location + WordSize - word.Length, word.Length));
Expand All @@ -40,7 +40,7 @@ public void SaveByte(in UInt256 location, byte value)
CheckMemoryAccessViolation(in location, WordSize);
UpdateSize(in location, 1);

_memory[(long)location] = value;
_memory![(long)location] = value;
}

public void Save(in UInt256 location, Span<byte> value)
Expand Down Expand Up @@ -76,7 +76,7 @@ public void Save(in UInt256 location, byte[] value)
CheckMemoryAccessViolation(in location, (UInt256)value.Length);
UpdateSize(in location, (UInt256)value.Length);

Array.Copy(value, 0, _memory, (long)location, value.Length);
Array.Copy(value, 0, _memory!, (long)location, value.Length);
}

public void Save(in UInt256 location, ZeroPaddedSpan value)
Expand Down Expand Up @@ -205,14 +205,21 @@ public List<string> GetTrace()
{
int traceLocation = 0;
List<string> memoryTrace = new();
if (_memory is not null)

while ((ulong)traceLocation < Size)
{
while ((ulong)traceLocation < Size)
int sizeAvailable = Math.Min(WordSize, (_memory?.Length ?? 0) - traceLocation);
if (sizeAvailable > 0)
{
int sizeAvailable = Math.Min(WordSize, (int)Size - traceLocation);
memoryTrace.Add(_memory.Slice(traceLocation, sizeAvailable).ToHexString());
traceLocation = traceLocation + WordSize;
Span<byte> bytes = _memory.AsSpan().Slice(traceLocation, sizeAvailable);
memoryTrace.Add(bytes.ToHexString());
}
else // Memory might not be initialized
{
memoryTrace.Add(Bytes.Zero32.ToHexString());
}

traceLocation += WordSize;
}

return memoryTrace;
Expand Down

0 comments on commit 640ab75

Please sign in to comment.