Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory guard and slice #6396

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ public Engine(IReleaseSpec spec)
private ITypedArray<byte> Slice(object input, long start, long end)
{
ArgumentNullException.ThrowIfNull(input);
var bytes = input.ToBytes();

if (start < 0 || end < start || end > Array.MaxLength)
{
throw new ArgumentOutOfRangeException(nameof(start), $"tracer accessed out of bound memory: offset {start}, end {end}");
}

return input.ToBytes().Slice((int)start, (int)(end - start)).ToTypedScriptArray();
return start < 0 || end < start || end > bytes.Length
? throw new ArgumentOutOfRangeException(nameof(start), $"tracer accessed out of bound memory: available {bytes.Length}, offset {start}, size {end - start}")
: bytes.Slice((int)start, (int)(end - start)).ToTypedScriptArray();
}

/// <summary>
Expand Down
29 changes: 13 additions & 16 deletions src/Nethermind/Nethermind.Evm/Tracing/TraceMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,23 @@ public string[] ToHexWordList()
return memory;
}

private const int MemoryPadLimit = 1024 * 1024;
public ReadOnlySpan<byte> Slice(int start, int length)
{
if ((ulong)start + (ulong)length > Size)
{
throw new IndexOutOfRangeException("Requested memory range is out of bounds.");
}
ArgumentOutOfRangeException.ThrowIfNegative(start, nameof(start));
ArgumentOutOfRangeException.ThrowIfNegative(length, nameof(length));

ReadOnlySpan<byte> span = _memory.Span;

if (start + length > _memory.Length)
if (start + length > span.Length)
{
int paddingNeeded = start + length - span.Length;
if (paddingNeeded > MemoryPadLimit) throw new InvalidOperationException($"reached limit for padding memory slice: {paddingNeeded}");
byte[] result = new byte[length];
for (int i = 0, index = start; index < _memory.Length; i++, index++)
int overlap = span.Length - start;
if (overlap > 0)
{
result[i] = span[index];
span.Slice(start, overlap).CopyTo(result.AsSpan(0, overlap));
}

return result;
Expand All @@ -68,13 +70,8 @@ public ReadOnlySpan<byte> Slice(int start, int length)
return span.Slice(start, length);
}

public BigInteger GetUint(int offset)
{
if (offset < 0 || (ulong)(offset + EvmPooledMemory.WordSize) > Size)
{
throw new ArgumentOutOfRangeException(nameof(offset), $"tracer accessed out of bound memory: available {Size}, offset {offset}, size {EvmPooledMemory.WordSize}");
}

return new BigInteger(Slice(offset, EvmPooledMemory.WordSize), true, true);
}
public BigInteger GetUint(int offset) =>
offset < 0 || (ulong)(offset + EvmPooledMemory.WordSize) > Size
? throw new ArgumentOutOfRangeException(nameof(offset), $"tracer accessed out of bound memory: available {Size}, offset {offset}, size {EvmPooledMemory.WordSize}")
: new BigInteger(Slice(offset, EvmPooledMemory.WordSize), true, true);
}