|
| 1 | +// Copyright 2025 the libevm authors. |
| 2 | +// |
| 3 | +// The libevm additions to go-ethereum are free software: you can redistribute |
| 4 | +// them and/or modify them under the terms of the GNU Lesser General Public License |
| 5 | +// as published by the Free Software Foundation, either version 3 of the License, |
| 6 | +// or (at your option) any later version. |
| 7 | +// |
| 8 | +// The libevm additions are distributed in the hope that they will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 11 | +// General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Lesser General Public License |
| 14 | +// along with the go-ethereum library. If not, see |
| 15 | +// <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package reentrancy |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/ava-labs/libevm/common" |
| 23 | + "github.com/ava-labs/libevm/core/vm" |
| 24 | + "github.com/ava-labs/libevm/libevm" |
| 25 | + "github.com/ava-labs/libevm/libevm/ethtest" |
| 26 | + "github.com/ava-labs/libevm/libevm/hookstest" |
| 27 | + "github.com/holiman/uint256" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | + "gotest.tools/assert" |
| 30 | +) |
| 31 | + |
| 32 | +func TestGuard(t *testing.T) { |
| 33 | + sut := common.HexToAddress("7E57ED") |
| 34 | + eve := common.HexToAddress("BAD") |
| 35 | + eveCalled := false |
| 36 | + |
| 37 | + zero := func() *uint256.Int { |
| 38 | + return uint256.NewInt(0) |
| 39 | + } |
| 40 | + |
| 41 | + returnIfGuarded := []byte("guarded") |
| 42 | + |
| 43 | + hooks := &hookstest.Stub{ |
| 44 | + PrecompileOverrides: map[common.Address]libevm.PrecompiledContract{ |
| 45 | + eve: vm.NewStatefulPrecompile(func(env vm.PrecompileEnvironment, input []byte) (ret []byte, err error) { |
| 46 | + eveCalled = true |
| 47 | + return env.Call(sut, []byte{}, env.Gas(), zero()) // i.e. reenter |
| 48 | + }), |
| 49 | + sut: vm.NewStatefulPrecompile(func(env vm.PrecompileEnvironment, input []byte) (ret []byte, err error) { |
| 50 | + // The argument is optional and used only to allow more than one |
| 51 | + // guard in a contract. |
| 52 | + if err := Guard(env, nil); err != nil { |
| 53 | + return returnIfGuarded, err |
| 54 | + } |
| 55 | + if env.Addresses().EVMSemantic.Caller == eve { |
| 56 | + // A real precompile MUST NOT panic under any circumstances. |
| 57 | + // It is done here to avoid a loop should the guard not |
| 58 | + // work. |
| 59 | + panic("reentrancy") |
| 60 | + } |
| 61 | + return env.Call(eve, []byte{}, env.Gas(), zero()) |
| 62 | + }), |
| 63 | + }, |
| 64 | + } |
| 65 | + hooks.Register(t) |
| 66 | + |
| 67 | + _, evm := ethtest.NewZeroEVM(t) |
| 68 | + got, _, err := evm.Call(vm.AccountRef{}, sut, []byte{}, 1e6, zero()) |
| 69 | + require.True(t, eveCalled, "Malicious contract called") |
| 70 | + // The error is propagated Guard() -> reentered SUT -> Eve -> top-level SUT -> evm.Call() |
| 71 | + // This MUST NOT be [assert.ErrorIs] as such errors are never wrapped in geth. |
| 72 | + assert.Equal(t, err, vm.ErrExecutionReverted, "Precompile reverted") |
| 73 | + assert.Equal(t, returnIfGuarded, got, "Precompile reverted with expected data") |
| 74 | +} |
0 commit comments