common, crypto: move fuzzers out of core (#22029)

This commit is contained in:
Daniel Liu 2024-09-18 12:47:22 +08:00 committed by Daniel Liu
parent 96f5876896
commit 4f9501f12c
3 changed files with 17 additions and 17 deletions

View file

@ -14,11 +14,13 @@
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build gofuzz
package bitutil package bitutil
import "bytes" import (
"bytes"
"github.com/XinFinOrg/XDPoSChain/common/bitutil"
)
// Fuzz implements a go-fuzz fuzzer method to test various encoding method // Fuzz implements a go-fuzz fuzzer method to test various encoding method
// invocations. // invocations.
@ -35,7 +37,7 @@ func Fuzz(data []byte) int {
// fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and // fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and
// decoding algorithm. // decoding algorithm.
func fuzzEncode(data []byte) int { func fuzzEncode(data []byte) int {
proc, _ := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data)) proc, _ := bitutil.DecompressBytes(bitutil.CompressBytes(data), len(data))
if !bytes.Equal(data, proc) { if !bytes.Equal(data, proc) {
panic("content mismatch") panic("content mismatch")
} }
@ -45,11 +47,11 @@ func fuzzEncode(data []byte) int {
// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and // fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
// reencoding algorithm. // reencoding algorithm.
func fuzzDecode(data []byte) int { func fuzzDecode(data []byte) int {
blob, err := bitsetDecodeBytes(data, 1024) blob, err := bitutil.DecompressBytes(data, 1024)
if err != nil { if err != nil {
return 0 return 0
} }
if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) { if comp := bitutil.CompressBytes(blob); !bytes.Equal(comp, data) {
panic("content mismatch") panic("content mismatch")
} }
return 1 return 1

View file

@ -14,8 +14,6 @@
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build gofuzz
package bn256 package bn256
import ( import (
@ -36,7 +34,7 @@ func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1) {
} }
xg := new(google.G1) xg := new(google.G1)
if _, err := xg.Unmarshal(xc.Marshal()); err != nil { 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 return xc, xg
} }
@ -49,7 +47,7 @@ func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2) {
} }
xg := new(google.G2) xg := new(google.G2)
if _, err := xg.Unmarshal(xc.Marshal()); err != nil { 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 return xc, xg
} }

View file

@ -14,23 +14,23 @@
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build gofuzz
package runtime package runtime
import (
"github.com/XinFinOrg/XDPoSChain/core/vm/runtime"
)
// Fuzz is the basic entry point for the go-fuzz tool // Fuzz is the basic entry point for the go-fuzz tool
// //
// This returns 1 for valid parsable/runable code, 0 // This returns 1 for valid parsable/runable code, 0
// for invalid opcode. // for invalid opcode.
func Fuzz(input []byte) int { func Fuzz(input []byte) int {
_, _, err := Execute(input, input, &Config{ _, _, err := runtime.Execute(input, input, &runtime.Config{
GasLimit: 3000000, GasLimit: 12000000,
}) })
// invalid opcode // 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 0
} }
return 1 return 1
} }