From 1953c18769e75948b411aa4cada224a9ab3b1a98 Mon Sep 17 00:00:00 2001 From: joohhnnn <386201195@qq.com> Date: Tue, 24 Sep 2024 22:51:52 +0800 Subject: [PATCH 1/2] add EmptyThreadStack test --- .../mipsevm/tests/evm_multithreaded_test.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cannon/mipsevm/tests/evm_multithreaded_test.go b/cannon/mipsevm/tests/evm_multithreaded_test.go index a26ebe96eb37..54fae0afadfe 100644 --- a/cannon/mipsevm/tests/evm_multithreaded_test.go +++ b/cannon/mipsevm/tests/evm_multithreaded_test.go @@ -1187,6 +1187,47 @@ func TestEVM_UnsupportedSyscall(t *testing.T) { } } +func TestEVM_EmptyThreadStacks(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + activeStackSize int + otherStackSize int + shouldPanic bool + }{ + {name: "Empty stacks", activeStackSize: 0, otherStackSize: 0, shouldPanic: true}, + {name: "Active stack only", activeStackSize: 1, otherStackSize: 0, shouldPanic: false}, + {name: "Other stack only", activeStackSize: 0, otherStackSize: 1, shouldPanic: true}, + {name: "Active and other stacks", activeStackSize: 1, otherStackSize: 1, shouldPanic: false}, + {name: "Multiple active stacks", activeStackSize: 2, otherStackSize: 0, shouldPanic: false}, + {name: "Multiple other stacks", activeStackSize: 0, otherStackSize: 2, shouldPanic: true}, + {name: "Multiple stacks both", activeStackSize: 2, otherStackSize: 2, shouldPanic: false}, + } + + for i, c := range cases { + t.Run(c.name, func(t *testing.T) { + goVm, state, _ := setup(t, i*123, nil) + mttestutil.SetupThreads(int64(i*123), state, false, c.activeStackSize, c.otherStackSize) + + defer func() { + if r := recover(); r != nil { + if !c.shouldPanic { + t.Errorf("Unexpected panic: %v", r) + } else if r != "Invalid empty thread stack" { + t.Errorf("Expected panic with message 'Invalid empty thread stack', but got: %v", r) + } + } else if c.shouldPanic { + t.Errorf("Expected panic, but no panic occurred") + } + }() + + // This should cause a panic if shouldPanic is true + goVm.Step(true) + }) + } +} + func TestEVM_NormalTraversalStep_HandleWaitingThread(t *testing.T) { var tracer *tracing.Hooks cases := []struct { From 9ce742e8625325b43d5af115fc87d6d04e0a80df Mon Sep 17 00:00:00 2001 From: joohhnnn <386201195@qq.com> Date: Wed, 25 Sep 2024 01:14:57 +0800 Subject: [PATCH 2/2] use AssertEVMReverts --- .../mipsevm/tests/evm_multithreaded_test.go | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/cannon/mipsevm/tests/evm_multithreaded_test.go b/cannon/mipsevm/tests/evm_multithreaded_test.go index 54fae0afadfe..0502eec07323 100644 --- a/cannon/mipsevm/tests/evm_multithreaded_test.go +++ b/cannon/mipsevm/tests/evm_multithreaded_test.go @@ -1189,6 +1189,7 @@ func TestEVM_UnsupportedSyscall(t *testing.T) { func TestEVM_EmptyThreadStacks(t *testing.T) { t.Parallel() + var tracer *tracing.Hooks cases := []struct { name string @@ -1196,7 +1197,7 @@ func TestEVM_EmptyThreadStacks(t *testing.T) { otherStackSize int shouldPanic bool }{ - {name: "Empty stacks", activeStackSize: 0, otherStackSize: 0, shouldPanic: true}, + {name: "Empty stacks", activeStackSize: 0, otherStackSize: 0, shouldPanic: true}, {name: "Active stack only", activeStackSize: 1, otherStackSize: 0, shouldPanic: false}, {name: "Other stack only", activeStackSize: 0, otherStackSize: 1, shouldPanic: true}, {name: "Active and other stacks", activeStackSize: 1, otherStackSize: 1, shouldPanic: false}, @@ -1206,24 +1207,24 @@ func TestEVM_EmptyThreadStacks(t *testing.T) { } for i, c := range cases { + c := c + i := i t.Run(c.name, func(t *testing.T) { - goVm, state, _ := setup(t, i*123, nil) + t.Parallel() + goVm, state, contracts := setup(t, i*123, nil) mttestutil.SetupThreads(int64(i*123), state, false, c.activeStackSize, c.otherStackSize) - defer func() { - if r := recover(); r != nil { - if !c.shouldPanic { - t.Errorf("Unexpected panic: %v", r) - } else if r != "Invalid empty thread stack" { - t.Errorf("Expected panic with message 'Invalid empty thread stack', but got: %v", r) - } - } else if c.shouldPanic { - t.Errorf("Expected panic, but no panic occurred") - } - }() - - // This should cause a panic if shouldPanic is true - goVm.Step(true) + if c.shouldPanic { + require.Panics(t, func() { + goVm.Step(true) + testutil.AssertEVMReverts(t, state, contracts, tracer) + }) + } else { + require.NotPanics(t, func() { + goVm.Step(true) + testutil.AssertEVMReverts(t, state, contracts, tracer) + }) + } }) } }