Skip to content
Open
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
60 changes: 47 additions & 13 deletions src/Nethermind/Nethermind.Evm.Test/Tracing/DebugTracerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

#if DEBUG
using System;
using Nethermind.Blockchain.Tracing.GethStyle;
using NUnit.Framework;
using System.Threading;
Expand All @@ -14,6 +15,7 @@ namespace Nethermind.Evm.Test;

public class DebugTracerTests : VirtualMachineTestsBase
{
private static readonly TimeSpan ThreadJoinTimeout = TimeSpan.FromSeconds(5);
private GethLikeTxMemoryTracer GethLikeTxTracer => new(Build.A.Transaction.TestObject, GethTraceOptions.Default);

[SetUp]
Expand All @@ -22,6 +24,18 @@ public override void Setup()
base.Setup();
}

private void ExecuteSafe(DebugTracer tracer, byte[] bytecode)
{
try
{
Execute(tracer, bytecode);
}
catch (ThreadInterruptedException)
{
// Expected when test aborts the thread
}
}

[TestCase("0x5b601760005600")]
public void Debugger_Halts_Execution_On_Breakpoint(string bytecodeHex)
{
Expand All @@ -39,7 +53,7 @@ public void Debugger_Halts_Execution_On_Breakpoint(string bytecodeHex)
tracer.SetBreakPoint(JUMP_OPCODE_PTR_BREAK_POINT);

// we set the break point to BREAK_POINT
Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

// we run the bytecode for <confidenceLevelDesired> iteration and check how many times we stopped at BREAK_POINT
Expand Down Expand Up @@ -69,6 +83,7 @@ public void Debugger_Halts_Execution_On_Breakpoint(string bytecodeHex)

}

vmThread.Join(ThreadJoinTimeout);
Assert.That(TestFailed, Is.False);
}

Expand All @@ -91,7 +106,7 @@ public void Debugger_Halts_Execution_On_Breakpoint_If_Condition_Is_True(string b
return state.DataStackHead == 23;
});

Thread vmThread = new(() => Execute(tracer, bytecode));
Thread vmThread = new(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

// we run the bytecode for <confidenceLevelDesired> iteration and check how many times we stopped at BREAK_POINT
Expand Down Expand Up @@ -121,6 +136,7 @@ public void Debugger_Halts_Execution_On_Breakpoint_If_Condition_Is_True(string b
}
}

vmThread.Join(ThreadJoinTimeout);
Assert.That(TestFailed, Is.False);
}

Expand All @@ -136,7 +152,7 @@ public void Debugger_Halts_Execution_On_Each_Iteration_using_StepByStepMode(stri
IsStepByStepModeOn = true,
};

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

int countBreaks = 0;
Expand All @@ -152,6 +168,7 @@ public void Debugger_Halts_Execution_On_Each_Iteration_using_StepByStepMode(stri
}
}

vmThread.Join(ThreadJoinTimeout);
countBreaks--; //Pre-run break

// we check that it matches the number of opcodes in the bytecode
Expand All @@ -170,7 +187,7 @@ public void Debugger_Skips_Single_Step_Breakpoints_When_MoveNext_Uses_Override(s
IsStepByStepModeOn = true,
};

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

int countBreaks = 0;
Expand All @@ -186,6 +203,8 @@ public void Debugger_Skips_Single_Step_Breakpoints_When_MoveNext_Uses_Override(s
}
}

vmThread.Join(ThreadJoinTimeout);

// we check that it matches the number of opcodes in the bytecode
Assert.That(countBreaks, Is.EqualTo(1));
}
Expand All @@ -205,7 +224,7 @@ public void Debugger_Switches_To_Single_Steps_After_First_Breakpoint(string byte

tracer.SetBreakPoint(BREAKPOINT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

int countBreaks = -1; // not counting the post-run stop
Expand All @@ -221,6 +240,8 @@ public void Debugger_Switches_To_Single_Steps_After_First_Breakpoint(string byte
}
}

vmThread.Join(ThreadJoinTimeout);

// we check that it matches the number of opcodes in the bytecode
Assert.That(countBreaks, Is.EqualTo(bytecode.Length - BREAKPOINT.pc));
}
Expand All @@ -239,7 +260,7 @@ public void Debugger_Can_Alter_Program_Counter(string bytecodeHex)

tracer.SetBreakPoint(JUMP_OPCODE_PTR_BREAK_POINT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

while (vmThread.IsAlive)
Expand All @@ -251,6 +272,8 @@ public void Debugger_Can_Alter_Program_Counter(string bytecodeHex)
}
}

vmThread.Join(ThreadJoinTimeout);

// we check if bytecode execution failed
var resultTraces = (tracer.InnerTracer as GethLikeTxMemoryTracer).BuildResult();
Assert.That(resultTraces.Failed, Is.False);
Expand All @@ -270,7 +293,7 @@ public void Debugger_Can_Alter_Data_Stack(string bytecodeHex)

tracer.SetBreakPoint(JUMP_OPCODE_PTR_BREAK_POINT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

while (vmThread.IsAlive)
Expand All @@ -286,6 +309,8 @@ public void Debugger_Can_Alter_Data_Stack(string bytecodeHex)
}
}

vmThread.Join(ThreadJoinTimeout);

// we check if bytecode execution failed
var resultTraces = (tracer.InnerTracer as GethLikeTxMemoryTracer).BuildResult();
Assert.That(resultTraces.Failed, Is.False);
Expand All @@ -305,7 +330,7 @@ public void Debugger_Can_Alter_Memory(string bytecodeHex)

tracer.SetBreakPoint(MSTORE_OPCODE_PTR_BREAK_POINT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

while (vmThread.IsAlive)
Expand All @@ -319,6 +344,8 @@ public void Debugger_Can_Alter_Memory(string bytecodeHex)
}
}

vmThread.Join(ThreadJoinTimeout);

// we check if bytecode execution failed
var resultTraces = (tracer.InnerTracer as GethLikeTxMemoryTracer).BuildResult();
Assert.That(resultTraces.ReturnValue[31] == 0, Is.True);
Expand All @@ -344,7 +371,7 @@ public void Use_Debug_Tracer_To_Check_Assertion_Live(string bytecodeHex)
tracer.SetBreakPoint(MSTORE_OPCODE_PTR_BREAK_POINT);
tracer.SetBreakPoint(POST_MSTORE_OPCODE_PTR_BREAK_POINT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

long? gasAvailable_pre_MSTORE = null;
Expand All @@ -362,6 +389,8 @@ public void Use_Debug_Tracer_To_Check_Assertion_Live(string bytecodeHex)
tracer.MoveNext();
}
}

vmThread.Join(ThreadJoinTimeout);
}

[TestCase("ef601700")]
Expand All @@ -375,7 +404,7 @@ public void Use_Debug_Tracer_To_Check_failure_status(string bytecodeHex)
IsStepByStepModeOn = true,
};

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

while (vmThread.IsAlive)
Expand All @@ -386,6 +415,8 @@ public void Use_Debug_Tracer_To_Check_failure_status(string bytecodeHex)
}
}

vmThread.Join(ThreadJoinTimeout);

// we check if bytecode execution failed
var resultTraces = (tracer.InnerTracer as GethLikeTxMemoryTracer).BuildResult();
Assert.That(resultTraces.Failed, Is.True);
Expand All @@ -403,7 +434,7 @@ public void Debugger_stops_at_correct_breakpoint_depth(string bytecodeHex)

tracer.SetBreakPoint(TARGET_OPCODE_PTR_BREAK_POINT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

bool stoppedAtCorrectBreakpoint = false;
Expand All @@ -423,6 +454,7 @@ public void Debugger_stops_at_correct_breakpoint_depth(string bytecodeHex)
}
}

vmThread.Join(ThreadJoinTimeout);
Assert.That(stoppedAtCorrectBreakpoint, Is.True);
}

Expand All @@ -437,7 +469,7 @@ public void Debugger_Halts_Execution_When_Global_Condition_Is_Met(string bytecod

tracer.SetCondition(state => state.DataStackHead == DATA_STACK_HEIGHT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

bool stoppedAtLeastOneTime = false;
Expand All @@ -451,6 +483,7 @@ public void Debugger_Halts_Execution_When_Global_Condition_Is_Met(string bytecod
}
}

vmThread.Join(ThreadJoinTimeout);
Assert.That(stoppedAtLeastOneTime, Is.True);
}

Expand All @@ -464,7 +497,7 @@ public void Debugger_Wont_Halt_If_Breakpoint_Is_Unreachable(string bytecodeHex)

tracer.SetBreakPoint(TARGET_OPCODE_PTR_BREAK_POINT);

Thread vmThread = new Thread(() => Execute(tracer, bytecode));
Thread vmThread = new Thread(() => ExecuteSafe(tracer, bytecode));
vmThread.Start();

bool stoppedAtCorrectBreakpoint = false;
Expand All @@ -477,6 +510,7 @@ public void Debugger_Wont_Halt_If_Breakpoint_Is_Unreachable(string bytecodeHex)
}
}

vmThread.Join(ThreadJoinTimeout);
Assert.That(stoppedAtCorrectBreakpoint, Is.False);
}
}
Expand Down