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 casting when tracing #7464

Merged
merged 1 commit into from
Sep 23, 2024
Merged
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
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Evm/EvmPooledMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
Expand Down Expand Up @@ -335,8 +336,7 @@ private static void ThrowOutOfGasException()

internal static class UInt256Extensions
{
public static bool IsLargerThanULong(in this UInt256 value)
{
return (value.u1 | value.u2 | value.u3) != 0;
}
public static bool IsLargerThanULong(in this UInt256 value) => (value.u1 | value.u2 | value.u3) != 0;
public static bool IsLargerThanLong(in this UInt256 value) => value.IsLargerThanULong() || value.u0 > long.MaxValue;
public static long ToLong(in this UInt256 value) => value.IsLargerThanLong() ? long.MaxValue : (long)value.u0;
}
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm/Tracing/CancellationTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void ReportMemoryChange(long offset, in ReadOnlySpan<byte> data)
}
}

public void ReportMemoryChange(long offset, in ZeroPaddedSpan data)
public void ReportMemoryChange(UInt256 offset, in ZeroPaddedSpan data)
{
token.ThrowIfCancellationRequested();
if (innerTracer.IsTracingInstructions)
Expand All @@ -298,7 +298,7 @@ public void ReportMemoryChange(long offset, in ZeroPaddedSpan data)
}
}

public void ReportMemoryChange(long offset, byte data)
public void ReportMemoryChange(UInt256 offset, byte data)
{
token.ThrowIfCancellationRequested();
if (innerTracer.IsTracingInstructions)
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm/Tracing/CompositeTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void ReportMemoryChange(long offset, in ReadOnlySpan<byte> data)
}
}

public void ReportMemoryChange(long offset, in ZeroPaddedSpan data)
public void ReportMemoryChange(UInt256 offset, in ZeroPaddedSpan data)
{
for (int index = 0; index < _txTracers.Count; index++)
{
Expand All @@ -307,7 +307,7 @@ public void ReportMemoryChange(long offset, in ZeroPaddedSpan data)
}
}

public void ReportMemoryChange(long offset, byte data)
public void ReportMemoryChange(UInt256 offset, byte data)
{
for (int index = 0; index < _txTracers.Count; index++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm/Tracing/ITxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void ReportMemoryChange(UInt256 offset, in ReadOnlySpan<byte> data)
/// <param name="offset"></param>
/// <param name="data"></param>
/// <remarks>Depends on <see cref="IsTracingInstructions"/></remarks>
void ReportMemoryChange(long offset, byte data)
void ReportMemoryChange(UInt256 offset, byte data)
{
ReportMemoryChange(offset, new[] { data });
}
Expand All @@ -289,7 +289,7 @@ void ReportMemoryChange(long offset, byte data)
/// <param name="offset"></param>
/// <param name="data"></param>
/// <remarks>Depends on <see cref="IsTracingInstructions"/></remarks>
void ReportMemoryChange(long offset, in ZeroPaddedSpan data)
void ReportMemoryChange(UInt256 offset, in ZeroPaddedSpan data)
{
ReportMemoryChange(offset, data.ToArray());
}
Expand Down
34 changes: 13 additions & 21 deletions src/Nethermind/Nethermind.Evm/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public TransactionSubstate Run<TTracingActions>(EvmState state, IWorldState worl
// parity induced if else for vmtrace
if (_txTracer.IsTracingInstructions)
{
_txTracer.ReportMemoryChange((long)previousCallOutputDestination, previousCallOutput);
_txTracer.ReportMemoryChange(previousCallOutputDestination, previousCallOutput);
}
}

Expand Down Expand Up @@ -1208,7 +1208,7 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
vmState.Memory.Save(in a, in slice);
if (typeof(TTracingInstructions) == typeof(IsTracing))
{
_txTracer.ReportMemoryChange((long)a, slice);
_txTracer.ReportMemoryChange(a, slice);
}
}

Expand All @@ -1235,7 +1235,7 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt

slice = code.SliceWithZeroPadding(in b, (int)result);
vmState.Memory.Save(in a, in slice);
if (typeof(TTracingInstructions) == typeof(IsTracing)) _txTracer.ReportMemoryChange((long)a, in slice);
if (typeof(TTracingInstructions) == typeof(IsTracing)) _txTracer.ReportMemoryChange(a, in slice);
}

break;
Expand Down Expand Up @@ -1338,7 +1338,7 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
vmState.Memory.Save(in a, in slice);
if (typeof(TTracingInstructions) == typeof(IsTracing))
{
_txTracer.ReportMemoryChange((long)a, in slice);
_txTracer.ReportMemoryChange(a, in slice);
}
}

Expand Down Expand Up @@ -1376,7 +1376,7 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
vmState.Memory.Save(in a, in slice);
if (typeof(TTracingInstructions) == typeof(IsTracing))
{
_txTracer.ReportMemoryChange((long)a, in slice);
_txTracer.ReportMemoryChange(a, in slice);
}
}

Expand All @@ -1389,7 +1389,7 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
gasAvailable -= GasCostOf.BlockHash;

if (!stack.PopUInt256(out a)) goto StackUnderflow;
long number = a > long.MaxValue ? long.MaxValue : (long)a;
long number = a.ToLong();

Hash256? blockHash = _blockhashProvider.GetBlockhash(blkCtx.Header, number);

Expand Down Expand Up @@ -1536,7 +1536,7 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
bytes = stack.PopWord256();
if (!UpdateMemoryCost(vmState, ref gasAvailable, in result, in BigInt32)) goto OutOfGas;
vmState.Memory.SaveWord(in result, bytes);
if (typeof(TTracingInstructions) == typeof(IsTracing)) _txTracer.ReportMemoryChange((long)result, bytes);
if (typeof(TTracingInstructions) == typeof(IsTracing)) _txTracer.ReportMemoryChange(result, bytes);

break;
}
Expand All @@ -1548,7 +1548,7 @@ private CallResult ExecuteCode<TTracingInstructions, TTracingRefunds, TTracingSt
byte data = stack.PopByte();
if (!UpdateMemoryCost(vmState, ref gasAvailable, in result, UInt256.One)) goto OutOfGas;
vmState.Memory.SaveByte(in result, data);
if (typeof(TTracingInstructions) == typeof(IsTracing)) _txTracer.ReportMemoryChange((long)result, data);
if (typeof(TTracingInstructions) == typeof(IsTracing)) _txTracer.ReportMemoryChange(result, data);

break;
}
Expand Down Expand Up @@ -2131,7 +2131,7 @@ private EvmExceptionType InstructionCall<TTracingInstructions, TTracingRefunds>(

if (gasLimit >= long.MaxValue) return EvmExceptionType.OutOfGas;

long gasLimitUl = (long)gasLimit;
long gasLimitUl = gasLimit.ToLong();
if (!UpdateGas(gasLimitUl, ref gasAvailable)) return EvmExceptionType.OutOfGas;

if (!transferValue.IsZero)
Expand Down Expand Up @@ -2202,8 +2202,8 @@ private EvmExceptionType InstructionCall<TTracingInstructions, TTracingRefunds>(
executionType,
isTopLevel: false,
snapshot,
(long)outputOffset,
(long)outputLength,
outputOffset.ToLong(),
outputLength.ToLong(),
instruction == Instruction.STATICCALL || vmState.IsStatic,
vmState,
isContinuation: false,
Expand Down Expand Up @@ -2470,7 +2470,7 @@ private EvmExceptionType InstructionLog<TTracing>(EvmState vmState, ref EvmStack
if (!UpdateMemoryCost(vmState, ref gasAvailable, in position, length)) return EvmExceptionType.OutOfGas;
if (!UpdateGas(
GasCostOf.Log + topicsCount * GasCostOf.LogTopic +
(long)length * GasCostOf.LogData, ref gasAvailable)) return EvmExceptionType.OutOfGas;
length.ToLong() * GasCostOf.LogData, ref gasAvailable)) return EvmExceptionType.OutOfGas;

ReadOnlyMemory<byte> data = vmState.Memory.Load(in position, length);
Hash256[] topics = new Hash256[topicsCount];
Expand Down Expand Up @@ -2695,15 +2695,7 @@ private static void UpdateCurrentState(EvmState state, int pc, long gas, int sta
private static bool UpdateMemoryCost(EvmState vmState, ref long gasAvailable, in UInt256 position, in UInt256 length)
{
long memoryCost = vmState.Memory.CalculateMemoryCost(in position, length);
if (memoryCost != 0L)
{
if (!UpdateGas(memoryCost, ref gasAvailable))
{
return false;
}
}

return true;
return memoryCost == 0L || UpdateGas(memoryCost, ref gasAvailable);
}

private static bool Jump(in UInt256 jumpDest, ref int programCounter, in ExecutionEnvironment env)
Expand Down