Skip to content

Commit

Permalink
common, crypto: move fuzzers out of core (ethereum#22029)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Sep 21, 2024
1 parent b5566a7 commit 3b7431e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
// 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/>.

// +build gofuzz

package bitutil

import "bytes"
import (
"bytes"

"github.com/XinFinOrg/XDPoSChain/common/bitutil"
)

// Fuzz implements a go-fuzz fuzzer method to test various encoding method
// invocations.
Expand All @@ -35,7 +37,7 @@ func Fuzz(data []byte) int {
// fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and
// decoding algorithm.
func fuzzEncode(data []byte) int {
proc, _ := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
proc, _ := bitutil.DecompressBytes(bitutil.CompressBytes(data), len(data))
if !bytes.Equal(data, proc) {
panic("content mismatch")
}
Expand All @@ -45,11 +47,11 @@ func fuzzEncode(data []byte) int {
// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
// reencoding algorithm.
func fuzzDecode(data []byte) int {
blob, err := bitsetDecodeBytes(data, 1024)
blob, err := bitutil.DecompressBytes(data, 1024)
if err != nil {
return 0
}
if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) {
if comp := bitutil.CompressBytes(blob); !bytes.Equal(comp, data) {
panic("content mismatch")
}
return 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// 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/>.

// +build gofuzz

package bn256

import (
Expand All @@ -36,7 +34,7 @@ func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1) {
}
xg := new(google.G1)
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
}
return xc, xg
}
Expand All @@ -49,7 +47,7 @@ func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2) {
}
xg := new(google.G2)
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
}
return xc, xg
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
// 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/>.

// +build gofuzz

package runtime

import (
"github.com/XinFinOrg/XDPoSChain/core/vm/runtime"
)

// Fuzz is the basic entry point for the go-fuzz tool
//
// This returns 1 for valid parsable/runable code, 0
// for invalid opcode.
func Fuzz(input []byte) int {
_, _, err := Execute(input, input, &Config{
GasLimit: 3000000,
_, _, err := runtime.Execute(input, input, &runtime.Config{
GasLimit: 12000000,
})

// invalid opcode
if err != nil && len(err.Error()) > 6 && string(err.Error()[:7]) == "invalid" {
if err != nil && len(err.Error()) > 6 && err.Error()[:7] == "invalid" {
return 0
}

return 1
}

0 comments on commit 3b7431e

Please sign in to comment.