From 9e3cefcf745ea32bd9013564ec0780a9a5f64850 Mon Sep 17 00:00:00 2001 From: Mita Date: Fri, 2 Feb 2024 21:34:21 +0100 Subject: [PATCH 1/4] Fixing stRandom/randomStatetest649.json failing test. opCodeCopy instruction tried to allocate memory even when length was zero. Since this specific test used invalid memory offset, allocation failed causing rollback and eventually test failure. --- state/runtime/evm/instructions.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/state/runtime/evm/instructions.go b/state/runtime/evm/instructions.go index 7cd86a58e6..73b64924d6 100644 --- a/state/runtime/evm/instructions.go +++ b/state/runtime/evm/instructions.go @@ -855,6 +855,10 @@ func opCodeCopy(c *state) { dataOffset := c.pop() length := c.pop() + if length.Uint64() <= 0 { + return + } + if !c.allocateMemory(memOffset, length) { return } From 70b924da64f5334e394a6ac328598db52e1ab353 Mon Sep 17 00:00:00 2001 From: Mita Date: Sat, 3 Feb 2024 23:24:51 +0100 Subject: [PATCH 2/4] Unit test covering this specific issue. --- state/runtime/evm/instructions_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/state/runtime/evm/instructions_test.go b/state/runtime/evm/instructions_test.go index 04053e2510..9990fe2d28 100644 --- a/state/runtime/evm/instructions_test.go +++ b/state/runtime/evm/instructions_test.go @@ -1127,6 +1127,22 @@ func TestCallDataCopy(t *testing.T) { assert.Equal(t, big.NewInt(1).FillBytes(make([]byte, 32)), s.memory) } +func TestCodeCopyLenZero(t *testing.T) { + s, cancelFn := getState(&chain.ForksInTime{}) + var expectedGas = s.gas + defer cancelFn() + + s.push(big.NewInt(0)) //length + s.push(big.NewInt(0)) //dataOffset + s.push(big.NewInt(0)) //memOffset + + opCodeCopy(s) + + // We check that no gas was spent and there was no error + assert.Equal(t, expectedGas, s.gas) + assert.Equal(t, s.err, nil) +} + func TestCodeCopy(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{}) defer cancelFn() From a411b0ae58ce09d2941b60bacbb0a929b71fa008 Mon Sep 17 00:00:00 2001 From: Mita Date: Sat, 3 Feb 2024 23:33:00 +0100 Subject: [PATCH 3/4] Fixing linter issue. --- state/runtime/evm/instructions_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/state/runtime/evm/instructions_test.go b/state/runtime/evm/instructions_test.go index 9990fe2d28..b0285e523a 100644 --- a/state/runtime/evm/instructions_test.go +++ b/state/runtime/evm/instructions_test.go @@ -1129,9 +1129,10 @@ func TestCallDataCopy(t *testing.T) { func TestCodeCopyLenZero(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{}) - var expectedGas = s.gas defer cancelFn() + var expectedGas = s.gas + s.push(big.NewInt(0)) //length s.push(big.NewInt(0)) //dataOffset s.push(big.NewInt(0)) //memOffset From 73abe36962242c28c27d49ca832786abe34a986d Mon Sep 17 00:00:00 2001 From: cokicm Date: Sun, 4 Feb 2024 21:27:44 +0100 Subject: [PATCH 4/4] Update state/runtime/evm/instructions_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Negovanović <93934272+Stefan-Ethernal@users.noreply.github.com> --- state/runtime/evm/instructions_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/state/runtime/evm/instructions_test.go b/state/runtime/evm/instructions_test.go index b0285e523a..95bab07923 100644 --- a/state/runtime/evm/instructions_test.go +++ b/state/runtime/evm/instructions_test.go @@ -1141,7 +1141,7 @@ func TestCodeCopyLenZero(t *testing.T) { // We check that no gas was spent and there was no error assert.Equal(t, expectedGas, s.gas) - assert.Equal(t, s.err, nil) + assert.NoError(t, s.err) } func TestCodeCopy(t *testing.T) {