Skip to content

Commit 09331ab

Browse files
authored
make oss-fuzz integration work again (#164)
This should make the oss-fuzz integration work again
1 parent 7aa449e commit 09331ab

7 files changed

Lines changed: 288 additions & 232 deletions

File tree

benchmarks_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func BenchmarkMul(bench *testing.B) {
198198
result := new(big.Int)
199199
bench.ResetTimer()
200200
for i := 0; i < bench.N; i++ {
201-
u256(result.Mul(a, b))
201+
bigU256(result.Mul(a, b))
202202
}
203203
}
204204

@@ -226,7 +226,7 @@ func BenchmarkMulOverflow(bench *testing.B) {
226226
result := new(big.Int)
227227
bench.ResetTimer()
228228
for i := 0; i < bench.N; i++ {
229-
u256(result.Mul(a, b))
229+
bigU256(result.Mul(a, b))
230230
}
231231
}
232232

@@ -251,7 +251,7 @@ func BenchmarkSquare(bench *testing.B) {
251251
result := new(big.Int)
252252
bench.ResetTimer()
253253
for i := 0; i < bench.N; i++ {
254-
u256(result.Mul(a, a))
254+
bigU256(result.Mul(a, a))
255255
}
256256
}
257257

@@ -563,9 +563,9 @@ func bigExp(result, base, exponent *big.Int) *big.Int {
563563
for _, word := range exponent.Bits() {
564564
for i := 0; i < wordBits; i++ {
565565
if word&1 == 1 {
566-
u256(result.Mul(result, base))
566+
bigU256(result.Mul(result, base))
567567
}
568-
u256(base.Mul(base, base))
568+
bigU256(base.Mul(base, base))
569569
word >>= 1
570570
}
571571
}
@@ -808,7 +808,7 @@ func benchmark_SdivLarge_Big(bench *testing.B) {
808808
bench.ResetTimer()
809809

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

binary_test.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ type binaryOpEntry struct {
2222
bigFn bigDualArgFunc
2323
}
2424

25+
func lookupBinary(name string) binaryOpEntry {
26+
for _, tc := range binaryOpFuncs {
27+
if tc.name == name {
28+
return tc
29+
}
30+
}
31+
panic(fmt.Sprintf("%v not found", name))
32+
}
33+
2534
var binaryOpFuncs = []binaryOpEntry{
2635
{"Add", (*Int).Add, (*big.Int).Add},
2736
{"Sub", (*Int).Sub, (*big.Int).Sub},
@@ -52,24 +61,15 @@ var cmpOpFuncs = []struct {
5261
{"Eq", (*Int).Eq, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }},
5362
{"Lt", (*Int).Lt, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }},
5463
{"Gt", (*Int).Gt, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }},
55-
{"Slt", (*Int).Slt, func(a, b *big.Int) bool { return S256(a).Cmp(S256(b)) < 0 }},
56-
{"Sgt", (*Int).Sgt, func(a, b *big.Int) bool { return S256(a).Cmp(S256(b)) > 0 }},
64+
{"Slt", (*Int).Slt, func(a, b *big.Int) bool { return bigS256(a).Cmp(bigS256(b)) < 0 }},
65+
{"Sgt", (*Int).Sgt, func(a, b *big.Int) bool { return bigS256(a).Cmp(bigS256(b)) > 0 }},
5766
{"CmpEq", func(a, b *Int) bool { return a.Cmp(b) == 0 }, func(a, b *big.Int) bool { return a.Cmp(b) == 0 }},
5867
{"CmpLt", func(a, b *Int) bool { return a.Cmp(b) < 0 }, func(a, b *big.Int) bool { return a.Cmp(b) < 0 }},
5968
{"CmpGt", func(a, b *Int) bool { return a.Cmp(b) > 0 }, func(a, b *big.Int) bool { return a.Cmp(b) > 0 }},
6069
{"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 }},
6170
{"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 }},
6271
}
6372

64-
func lookupBinary(name string) binaryOpEntry {
65-
for _, tc := range binaryOpFuncs {
66-
if tc.name == name {
67-
return tc
68-
}
69-
}
70-
panic(fmt.Sprintf("%v not found", name))
71-
}
72-
7373
func checkBinaryOperation(t *testing.T, opName string, op opDualArgFunc, bigOp bigDualArgFunc, x, y Int) {
7474
var (
7575
b1 = x.ToBig()
@@ -159,7 +159,7 @@ func bigLsh(z, x, y *big.Int) *big.Int {
159159
}
160160

161161
func bigSRsh(z, x, y *big.Int) *big.Int {
162-
return z.Rsh(S256(x), uint(y.Uint64()&0x1FF))
162+
return z.Rsh(bigS256(x), uint(y.Uint64()&0x1FF))
163163
}
164164

165165
func bigExtendSign(result, num, byteNum *big.Int) *big.Int {
@@ -198,8 +198,8 @@ func bigSDiv(result, x, y *big.Int) *big.Int {
198198
if y.Sign() == 0 {
199199
return result.SetUint64(0)
200200
}
201-
sx := S256(x)
202-
sy := S256(y)
201+
sx := bigS256(x)
202+
sy := bigS256(y)
203203

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

221-
sx := S256(x)
222-
sy := S256(y)
221+
sx := bigS256(x)
222+
sy := bigS256(y)
223223
neg := sx.Sign() < 0
224224

225225
result.Mod(sx.Abs(sx), sy.Abs(sy))
226226
if neg {
227227
result.Neg(result)
228228
}
229-
return u256(result)
229+
return bigU256(result)
230+
}
231+
232+
// divModDiv wraps DivMod and returns quotient only
233+
func divModDiv(z, x, y *Int) *Int {
234+
var m Int
235+
z.DivMod(x, y, &m)
236+
return z
237+
}
238+
239+
// divModMod wraps DivMod and returns modulus only
240+
func divModMod(z, x, y *Int) *Int {
241+
new(Int).DivMod(x, y, z)
242+
return z
243+
}
244+
245+
// udivremDiv wraps udivrem and returns quotient
246+
func udivremDiv(z, x, y *Int) *Int {
247+
var quot Int
248+
if !y.IsZero() {
249+
udivrem(quot[:], x[:], y)
250+
}
251+
return z.Set(&quot)
252+
}
253+
254+
// udivremMod wraps udivrem and returns remainder
255+
func udivremMod(z, x, y *Int) *Int {
256+
if y.IsZero() {
257+
return z.Clear()
258+
}
259+
var quot Int
260+
rem := udivrem(quot[:], x[:], y)
261+
return z.Set(&rem)
230262
}
231263

232264
func checkCompareOperation(t *testing.T, opName string, op opCmpArgFunc, bigOp bigCmpArgFunc, x, y Int) {

conversion_fuzz_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package uint256
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"testing"
7+
)
8+
9+
func testSetFromDecForFuzzing(tc string) error {
10+
a := new(Int).SetAllOne()
11+
err := a.SetFromDecimal(tc)
12+
// If input is negative, we should eror
13+
if len(tc) > 0 && tc[0] == '-' {
14+
if err == nil {
15+
return fmt.Errorf("want error on negative input")
16+
}
17+
return nil
18+
}
19+
// Need to compare with big.Int
20+
bigA, ok := big.NewInt(0).SetString(tc, 10)
21+
if !ok {
22+
if err == nil {
23+
return fmt.Errorf("want error")
24+
}
25+
return nil // both agree that input is bad
26+
}
27+
if bigA.BitLen() > 256 {
28+
if err == nil {
29+
return fmt.Errorf("want error (bitlen > 256)")
30+
}
31+
return nil
32+
}
33+
want := bigA.String()
34+
have := a.Dec()
35+
if want != have {
36+
return fmt.Errorf("want %v, have %v", want, have)
37+
}
38+
if _, err := a.Value(); err != nil {
39+
return fmt.Errorf("fail to Value() %s, got err %s", tc, err)
40+
}
41+
return nil
42+
}
43+
44+
func FuzzSetString(f *testing.F) {
45+
f.Fuzz(func(t *testing.T, data []byte) {
46+
if len(data) > 512 {
47+
return
48+
}
49+
if err := testSetFromDecForFuzzing(string(data)); err != nil {
50+
t.Fatal(err)
51+
}
52+
})
53+
}

oss-fuzz.sh

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
11
#!/bin/bash -eu
22

3+
# This sets the -coverpgk for the coverage report when the corpus is executed through go test
4+
coverpkg="github.com/holiman/uint256/..."
5+
6+
(cd /src/ && git clone https://github.com/holiman/gofuzz-shim.git )
7+
8+
function coverbuild {
9+
path=$1
10+
function=$2
11+
fuzzer=$3
12+
tags=""
13+
14+
if [[ $# -eq 4 ]]; then
15+
tags="-tags $4"
16+
fi
17+
cd $path
18+
fuzzed_package=`pwd | rev | cut -d'/' -f 1 | rev`
19+
cp /src/gofuzz-shim/coverage_runner_template.txt ./"${function,,}"_test.go
20+
sed -i -e 's/FuzzFunction/'$function'/' ./"${function,,}"_test.go
21+
sed -i -e 's/mypackagebeingfuzzed/'$fuzzed_package'/' ./"${function,,}"_test.go
22+
sed -i -e 's/TestFuzzCorpus/Test'$function'Corpus/' ./"${function,,}"_test.go
23+
24+
cat << DOG > $OUT/$fuzzer
25+
#/bin/sh
26+
27+
cd $OUT/$path
28+
go test -run Test${function}Corpus -v $tags -coverprofile \$1 -coverpkg $coverpkg
29+
30+
DOG
31+
32+
chmod +x $OUT/$fuzzer
33+
#echo "Built script $OUT/$fuzzer"
34+
#cat $OUT/$fuzzer
35+
cd -
36+
}
37+
38+
repo=/src/uint256
39+
340
function compile_fuzzer() {
441
package=$1
542
function=$2
643
fuzzer=$3
744
file=$4
845

9-
path=$GOPATH/src/$package
46+
path=$repo
1047

1148
echo "Building $fuzzer"
1249
cd $path
@@ -34,10 +71,9 @@ function compile_fuzzer() {
3471

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

37-
repo=$GOPATH/src/github.com/holiman/uint256
3874

39-
compile_fuzzer github.com/holiman/uint256 FuzzUnaryOperations fuzzUnary $repo/unary_test.go
40-
compile_fuzzer github.com/holiman/uint256 FuzzBinaryOperations fuzzBinary $repo/binary_test.go
41-
compile_fuzzer github.com/holiman/uint256 FuzzCompareOperations fuzzCompare $repo/binary_test.go
42-
compile_fuzzer github.com/holiman/uint256 FuzzTernaryOperations fuzzTernary $repo/ternary_test.go
43-
compile_fuzzer github.com/holiman/uint256 FuzzSetString fuzzSetString $repo/uint256_test.go
75+
compile_fuzzer github.com/holiman/uint256 FuzzUnaryOperations fuzzUnary $repo/unary_test.go,$repo/shared_test.go
76+
compile_fuzzer github.com/holiman/uint256 FuzzBinaryOperations fuzzBinary $repo/binary_test.go,$repo/shared_test.go
77+
compile_fuzzer github.com/holiman/uint256 FuzzCompareOperations fuzzCompare $repo/binary_test.go,$repo/shared_test.go
78+
compile_fuzzer github.com/holiman/uint256 FuzzTernaryOperations fuzzTernary $repo/ternary_test.go,$repo/shared_test.go
79+
compile_fuzzer github.com/holiman/uint256 FuzzSetString fuzzSetString $repo/conversion_fuzz_test.go,$repo/shared_test.go

0 commit comments

Comments
 (0)