Skip to content

Commit

Permalink
fix casting when tracing (#7464)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored Sep 23, 2024
1 parent f9ae527 commit a3bd523
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
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 @@ -1210,7 +1210,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 @@ -1237,7 +1237,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 @@ -1340,7 +1340,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 @@ -1378,7 +1378,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 @@ -1391,7 +1391,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 @@ -1538,7 +1538,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 @@ -1550,7 +1550,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 @@ -2133,7 +2133,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 @@ -2204,8 +2204,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 @@ -2472,7 +2472,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 @@ -2697,15 +2697,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

0 comments on commit a3bd523

Please sign in to comment.