Skip to content
New issue

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

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

Already on GitHub? # to your account

make oss-fuzz integration work again #164

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func BenchmarkMul(bench *testing.B) {
result := new(big.Int)
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
u256(result.Mul(a, b))
bigU256(result.Mul(a, b))
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ func BenchmarkMulOverflow(bench *testing.B) {
result := new(big.Int)
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
u256(result.Mul(a, b))
bigU256(result.Mul(a, b))
}
}

Expand All @@ -251,7 +251,7 @@ func BenchmarkSquare(bench *testing.B) {
result := new(big.Int)
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
u256(result.Mul(a, a))
bigU256(result.Mul(a, a))
}
}

Expand Down Expand Up @@ -563,9 +563,9 @@ func bigExp(result, base, exponent *big.Int) *big.Int {
for _, word := range exponent.Bits() {
for i := 0; i < wordBits; i++ {
if word&1 == 1 {
u256(result.Mul(result, base))
bigU256(result.Mul(result, base))
}
u256(base.Mul(base, base))
bigU256(base.Mul(base, base))
word >>= 1
}
}
Expand Down Expand Up @@ -808,7 +808,7 @@ func benchmark_SdivLarge_Big(bench *testing.B) {
bench.ResetTimer()

for i := 0; i < bench.N; i++ {
u256(bigSDiv(new(big.Int), a, b))
bigU256(bigSDiv(new(big.Int), a, b))
}
}

Expand Down
66 changes: 49 additions & 17 deletions binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ type binaryOpEntry struct {
bigFn bigDualArgFunc
}

func lookupBinary(name string) binaryOpEntry {
for _, tc := range binaryOpFuncs {
if tc.name == name {
return tc
}
}
panic(fmt.Sprintf("%v not found", name))
}

var binaryOpFuncs = []binaryOpEntry{
{"Add", (*Int).Add, (*big.Int).Add},
{"Sub", (*Int).Sub, (*big.Int).Sub},
Expand Down Expand Up @@ -52,24 +61,15 @@ var cmpOpFuncs = []struct {
{"Eq", (*Int).Eq, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }},
{"Lt", (*Int).Lt, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }},
{"Gt", (*Int).Gt, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }},
{"Slt", (*Int).Slt, func(a, b *big.Int) bool { return S256(a).Cmp(S256(b)) < 0 }},
{"Sgt", (*Int).Sgt, func(a, b *big.Int) bool { return S256(a).Cmp(S256(b)) > 0 }},
{"Slt", (*Int).Slt, func(a, b *big.Int) bool { return bigS256(a).Cmp(bigS256(b)) < 0 }},
{"Sgt", (*Int).Sgt, func(a, b *big.Int) bool { return bigS256(a).Cmp(bigS256(b)) > 0 }},
{"CmpEq", func(a, b *Int) bool { return a.Cmp(b) == 0 }, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }},
{"CmpLt", func(a, b *Int) bool { return a.Cmp(b) < 0 }, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }},
{"CmpGt", func(a, b *Int) bool { return a.Cmp(b) > 0 }, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }},
{"LtUint64", func(a, b *Int) bool { return a.LtUint64(b.Uint64()) }, func(a, b *big.Int) bool { return a.Cmp(new(big.Int).SetUint64(b.Uint64())) < 0 }},
{"GtUint64", func(a, b *Int) bool { return a.GtUint64(b.Uint64()) }, func(a, b *big.Int) bool { return a.Cmp(new(big.Int).SetUint64(b.Uint64())) > 0 }},
}

func lookupBinary(name string) binaryOpEntry {
for _, tc := range binaryOpFuncs {
if tc.name == name {
return tc
}
}
panic(fmt.Sprintf("%v not found", name))
}

func checkBinaryOperation(t *testing.T, opName string, op opDualArgFunc, bigOp bigDualArgFunc, x, y Int) {
var (
b1 = x.ToBig()
Expand Down Expand Up @@ -159,7 +159,7 @@ func bigLsh(z, x, y *big.Int) *big.Int {
}

func bigSRsh(z, x, y *big.Int) *big.Int {
return z.Rsh(S256(x), uint(y.Uint64()&0x1FF))
return z.Rsh(bigS256(x), uint(y.Uint64()&0x1FF))
}

func bigExtendSign(result, num, byteNum *big.Int) *big.Int {
Expand Down Expand Up @@ -198,8 +198,8 @@ func bigSDiv(result, x, y *big.Int) *big.Int {
if y.Sign() == 0 {
return result.SetUint64(0)
}
sx := S256(x)
sy := S256(y)
sx := bigS256(x)
sy := bigS256(y)

n := new(big.Int)
if sx.Sign() == sy.Sign() {
Expand All @@ -218,15 +218,47 @@ func bigSMod(result, x, y *big.Int) *big.Int {
return result.SetUint64(0)
}

sx := S256(x)
sy := S256(y)
sx := bigS256(x)
sy := bigS256(y)
neg := sx.Sign() < 0

result.Mod(sx.Abs(sx), sy.Abs(sy))
if neg {
result.Neg(result)
}
return u256(result)
return bigU256(result)
}

// divModDiv wraps DivMod and returns quotient only
func divModDiv(z, x, y *Int) *Int {
var m Int
z.DivMod(x, y, &m)
return z
}

// divModMod wraps DivMod and returns modulus only
func divModMod(z, x, y *Int) *Int {
new(Int).DivMod(x, y, z)
return z
}

// udivremDiv wraps udivrem and returns quotient
func udivremDiv(z, x, y *Int) *Int {
var quot Int
if !y.IsZero() {
udivrem(quot[:], x[:], y)
}
return z.Set(&quot)
}

// udivremMod wraps udivrem and returns remainder
func udivremMod(z, x, y *Int) *Int {
if y.IsZero() {
return z.Clear()
}
var quot Int
rem := udivrem(quot[:], x[:], y)
return z.Set(&rem)
}

func checkCompareOperation(t *testing.T, opName string, op opCmpArgFunc, bigOp bigCmpArgFunc, x, y Int) {
Expand Down
53 changes: 53 additions & 0 deletions conversion_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package uint256

import (
"fmt"
"math/big"
"testing"
)

func testSetFromDecForFuzzing(tc string) error {
a := new(Int).SetAllOne()
err := a.SetFromDecimal(tc)
// If input is negative, we should eror
if len(tc) > 0 && tc[0] == '-' {
if err == nil {
return fmt.Errorf("want error on negative input")
}
return nil
}
// Need to compare with big.Int
bigA, ok := big.NewInt(0).SetString(tc, 10)
if !ok {
if err == nil {
return fmt.Errorf("want error")
}
return nil // both agree that input is bad
}
if bigA.BitLen() > 256 {
if err == nil {
return fmt.Errorf("want error (bitlen > 256)")
}
return nil
}
want := bigA.String()
have := a.Dec()
if want != have {
return fmt.Errorf("want %v, have %v", want, have)
}
if _, err := a.Value(); err != nil {
return fmt.Errorf("fail to Value() %s, got err %s", tc, err)
}
return nil
}

func FuzzSetString(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
if len(data) > 512 {
return
}
if err := testSetFromDecForFuzzing(string(data)); err != nil {
t.Fatal(err)
}
})
}
50 changes: 43 additions & 7 deletions oss-fuzz.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
#!/bin/bash -eu

# This sets the -coverpgk for the coverage report when the corpus is executed through go test
coverpkg="github.com/holiman/uint256/..."

(cd /src/ && git clone https://github.com/holiman/gofuzz-shim.git )

function coverbuild {
path=$1
function=$2
fuzzer=$3
tags=""

if [[ $# -eq 4 ]]; then
tags="-tags $4"
fi
cd $path
fuzzed_package=`pwd | rev | cut -d'/' -f 1 | rev`
cp /src/gofuzz-shim/coverage_runner_template.txt ./"${function,,}"_test.go
sed -i -e 's/FuzzFunction/'$function'/' ./"${function,,}"_test.go
sed -i -e 's/mypackagebeingfuzzed/'$fuzzed_package'/' ./"${function,,}"_test.go
sed -i -e 's/TestFuzzCorpus/Test'$function'Corpus/' ./"${function,,}"_test.go

cat << DOG > $OUT/$fuzzer
#/bin/sh
cd $OUT/$path
go test -run Test${function}Corpus -v $tags -coverprofile \$1 -coverpkg $coverpkg
DOG

chmod +x $OUT/$fuzzer
#echo "Built script $OUT/$fuzzer"
#cat $OUT/$fuzzer
cd -
}

repo=/src/uint256

function compile_fuzzer() {
package=$1
function=$2
fuzzer=$3
file=$4

path=$GOPATH/src/$package
path=$repo

echo "Building $fuzzer"
cd $path
Expand Down Expand Up @@ -34,10 +71,9 @@ function compile_fuzzer() {

go install github.com/holiman/gofuzz-shim@latest

repo=$GOPATH/src/github.com/holiman/uint256

compile_fuzzer github.com/holiman/uint256 FuzzUnaryOperations fuzzUnary $repo/unary_test.go
compile_fuzzer github.com/holiman/uint256 FuzzBinaryOperations fuzzBinary $repo/binary_test.go
compile_fuzzer github.com/holiman/uint256 FuzzCompareOperations fuzzCompare $repo/binary_test.go
compile_fuzzer github.com/holiman/uint256 FuzzTernaryOperations fuzzTernary $repo/ternary_test.go
compile_fuzzer github.com/holiman/uint256 FuzzSetString fuzzSetString $repo/uint256_test.go
compile_fuzzer github.com/holiman/uint256 FuzzUnaryOperations fuzzUnary $repo/unary_test.go,$repo/shared_test.go
compile_fuzzer github.com/holiman/uint256 FuzzBinaryOperations fuzzBinary $repo/binary_test.go,$repo/shared_test.go
compile_fuzzer github.com/holiman/uint256 FuzzCompareOperations fuzzCompare $repo/binary_test.go,$repo/shared_test.go
compile_fuzzer github.com/holiman/uint256 FuzzTernaryOperations fuzzTernary $repo/ternary_test.go,$repo/shared_test.go
compile_fuzzer github.com/holiman/uint256 FuzzSetString fuzzSetString $repo/conversion_fuzz_test.go,$repo/shared_test.go
Loading