Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/fuzzers: move fuzzers into native packages #28467

Merged
merged 16 commits into from
Nov 14, 2023
Merged
Prev Previous commit
Next Next commit
core/vm/runtime, core/vm: fuzzers
  • Loading branch information
holiman committed Nov 2, 2023
commit 0187574244ca73f346129b15cafca3f6a0d88604
70 changes: 70 additions & 0 deletions core/vm/contracts_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package vm

import (
"testing"

"github.com/ethereum/go-ethereum/common"
)

// allPrecompiles does not map to the actual set of precompiles, as it also contains
// repriced versions of precompiles at certain slots
var allPrecompiles = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
common.BytesToAddress([]byte{0xf5}): &bigModExp{eip2565: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},

common.BytesToAddress([]byte{0x0f, 0x0a}): &bls12381G1Add{},
common.BytesToAddress([]byte{0x0f, 0x0b}): &bls12381G1Mul{},
common.BytesToAddress([]byte{0x0f, 0x0c}): &bls12381G1MultiExp{},
common.BytesToAddress([]byte{0x0f, 0x0d}): &bls12381G2Add{},
common.BytesToAddress([]byte{0x0f, 0x0e}): &bls12381G2Mul{},
common.BytesToAddress([]byte{0x0f, 0x0f}): &bls12381G2MultiExp{},
common.BytesToAddress([]byte{0x0f, 0x10}): &bls12381Pairing{},
common.BytesToAddress([]byte{0x0f, 0x11}): &bls12381MapG1{},
common.BytesToAddress([]byte{0x0f, 0x12}): &bls12381MapG2{},
}

func FuzzPrecompiledContracts(f *testing.F) {
// Create list of addresses
var addrs []common.Address
for k := range allPrecompiles {
addrs = append(addrs, k)
}
f.Fuzz(func(t *testing.T, addr uint8, input []byte) {
a := addrs[int(int(addr)%len(addrs))]
p := allPrecompiles[a]
gas := p.RequiredGas(input)
if gas > 10_000_000 {
return
}
inWant := string(input)
RunPrecompiledContract(p, input, gas)
if inHave := string(input); inWant != inHave {
t.Errorf("Precompiled %v modified input data", a)
}
})
}
26 changes: 0 additions & 26 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,6 @@ type precompiledFailureTest struct {
Name string
}

// allPrecompiles does not map to the actual set of precompiles, as it also contains
holiman marked this conversation as resolved.
Show resolved Hide resolved
// repriced versions of precompiles at certain slots
var allPrecompiles = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &ecrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
common.BytesToAddress([]byte{4}): &dataCopy{},
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
common.BytesToAddress([]byte{0xf5}): &bigModExp{eip2565: true},
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
common.BytesToAddress([]byte{9}): &blake2F{},
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},

common.BytesToAddress([]byte{0x0f, 0x0a}): &bls12381G1Add{},
common.BytesToAddress([]byte{0x0f, 0x0b}): &bls12381G1Mul{},
common.BytesToAddress([]byte{0x0f, 0x0c}): &bls12381G1MultiExp{},
common.BytesToAddress([]byte{0x0f, 0x0d}): &bls12381G2Add{},
common.BytesToAddress([]byte{0x0f, 0x0e}): &bls12381G2Mul{},
common.BytesToAddress([]byte{0x0f, 0x0f}): &bls12381G2MultiExp{},
common.BytesToAddress([]byte{0x0f, 0x10}): &bls12381Pairing{},
common.BytesToAddress([]byte{0x0f, 0x11}): &bls12381MapG1{},
common.BytesToAddress([]byte{0x0f, 0x12}): &bls12381MapG2{},
}

// EIP-152 test vectors
var blake2FMalformedInputTests = []precompiledFailureTest{
{
Expand Down
6 changes: 2 additions & 4 deletions core/vm/runtime/runtime_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ package runtime

import (
"testing"

"github.com/ethereum/go-ethereum/core/vm/runtime"
)

func Fuzz(f *testing.F) {
func FuzzVmRuntime(f *testing.F) {
f.Fuzz(func(t *testing.T, code, input []byte) {
runtime.Execute(code, input, &runtime.Config{
Execute(code, input, &Config{
GasLimit: 12000000,
})
})
Expand Down
4 changes: 3 additions & 1 deletion oss-fuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ mv $PROJPATH/common/bitutil/compress_test.go $PROJPATH/common/bitutil/compress_t
compile_fuzzer common/bitutil FuzzEncoder fuzzBitutilEncoder
compile_fuzzer common/bitutil FuzzDecoder fuzzBitutilDecoder

compile_fuzzer core/vm/runtime FuzzVmRuntime fuzzVmRuntime
compile_fuzzer core/vm FuzzPrecompiledContracts fuzzPrecompiledContracts

compile_fuzzer tests/fuzzers/bn256 FuzzAdd fuzzBn256Add
compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul
compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair
compile_fuzzer tests/fuzzers/runtime Fuzz fuzzVmRuntime
compile_fuzzer tests/fuzzers/keystore Fuzz fuzzKeystore
compile_fuzzer tests/fuzzers/txfetcher Fuzz fuzzTxfetcher
compile_fuzzer tests/fuzzers/rlp Fuzz fuzzRlp
Expand Down