forked from holiman/uint256
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz.go
353 lines (320 loc) · 7.96 KB
/
fuzz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// uint256: Fixed size 256-bit math library
// Copyright 2020 uint256 Authors
// SPDX-License-Identifier: BSD-3-Clause
//go:build gofuzz
// +build gofuzz
package uint256
import (
"fmt"
"math/big"
"reflect"
"runtime"
"strings"
)
const (
opUdivrem = iota
opMul
opLsh
opAdd
opSub
opMulmod
)
type opUnaryArgFunc func(*Int, *Int) *Int
type bigUnaryArgFunc func(*big.Int, *big.Int) *big.Int
type opDualArgFunc func(*Int, *Int, *Int) *Int
type bigDualArgFunc func(*big.Int, *big.Int, *big.Int) *big.Int
type opThreeArgFunc func(*Int, *Int, *Int, *Int) *Int
type bigThreeArgFunc func(*big.Int, *big.Int, *big.Int, *big.Int) *big.Int
func crash(op interface{}, msg string, args ...Int) {
fn := runtime.FuncForPC(reflect.ValueOf(op).Pointer())
fnName := fn.Name()
fnFile, fnLine := fn.FileLine(fn.Entry())
var strArgs []string
for i, arg := range args {
strArgs = append(strArgs, fmt.Sprintf("%d: %x", i, &arg))
}
panic(fmt.Sprintf("%s\nfor %s (%s:%d)\n%v",
msg, fnName, fnFile, fnLine, strings.Join(strArgs, "\n")))
}
func checkUnaryOp(op opUnaryArgFunc, bigOp bigUnaryArgFunc, x Int) {
origX := x
var result Int
ret := op(&result, &x)
if ret != &result {
crash(op, "returned not the pointer receiver", x)
}
if x != origX {
crash(op, "argument modified", x)
}
expected, _ := FromBig(bigOp(new(big.Int), x.ToBig()))
if result != *expected {
crash(op, "unexpected result", x)
}
// Test again when the receiver is not zero.
var garbage Int
garbage.Sub(&garbage, NewInt(1))
ret = op(&garbage, &x)
if ret != &garbage {
crash(op, "returned not the pointer receiver", x)
}
if garbage != *expected {
crash(op, "unexpected result", x)
}
// Test again with the receiver aliasing arguments.
ret = op(&x, &x)
if ret != &x {
crash(op, "returned not the pointer receiver", x)
}
if x != *expected {
crash(op, "unexpected result", x)
}
}
func checkDualArgOp(op opDualArgFunc, bigOp bigDualArgFunc, x, y Int) {
origX := x
origY := y
var result Int
ret := op(&result, &x, &y)
if ret != &result {
crash(op, "returned not the pointer receiver", x, y)
}
if x != origX {
crash(op, "first argument modified", x, y)
}
if y != origY {
crash(op, "second argument modified", x, y)
}
expected, _ := FromBig(bigOp(new(big.Int), x.ToBig(), y.ToBig()))
if result != *expected {
crash(op, "unexpected result", x, y)
}
// Test again when the receiver is not zero.
var garbage Int
garbage.Xor(&x, &y)
ret = op(&garbage, &x, &y)
if ret != &garbage {
crash(op, "returned not the pointer receiver", x, y)
}
if garbage != *expected {
crash(op, "unexpected result", x, y)
}
if x != origX {
crash(op, "first argument modified", x, y)
}
if y != origY {
crash(op, "second argument modified", x, y)
}
// Test again with the receiver aliasing arguments.
ret = op(&x, &x, &y)
if ret != &x {
crash(op, "returned not the pointer receiver", x, y)
}
if x != *expected {
crash(op, "unexpected result", x, y)
}
ret = op(&y, &origX, &y)
if ret != &y {
crash(op, "returned not the pointer receiver", x, y)
}
if y != *expected {
crash(op, "unexpected result", x, y)
}
}
func checkThreeArgOp(op opThreeArgFunc, bigOp bigThreeArgFunc, x, y, z Int) {
origX := x
origY := y
origZ := z
var result Int
ret := op(&result, &x, &y, &z)
if ret != &result {
crash(op, "returned not the pointer receiver", x, y, z)
}
switch {
case x != origX:
crash(op, "first argument modified", x, y, z)
case y != origY:
crash(op, "second argument modified", x, y, z)
case z != origZ:
crash(op, "third argument modified", x, y, z)
}
expected, _ := FromBig(bigOp(new(big.Int), x.ToBig(), y.ToBig(), z.ToBig()))
if have, want := result, *expected; have != want {
crash(op, fmt.Sprintf("unexpected result: have %v want %v", have, want), x, y, z)
}
// Test again when the receiver is not zero.
var garbage Int
garbage.Xor(&x, &y)
ret = op(&garbage, &x, &y, &z)
if ret != &garbage {
crash(op, "returned not the pointer receiver", x, y, z)
}
if have, want := garbage, *expected; have != want {
crash(op, fmt.Sprintf("unexpected result: have %v want %v", have, want), x, y, z)
}
switch {
case x != origX:
crash(op, "first argument modified", x, y, z)
case y != origY:
crash(op, "second argument modified", x, y, z)
case z != origZ:
crash(op, "third argument modified", x, y, z)
}
// Test again with the receiver aliasing arguments.
ret = op(&x, &x, &y, &z)
if ret != &x {
crash(op, "returned not the pointer receiver", x, y, z)
}
if have, want := x, *expected; have != want {
crash(op, fmt.Sprintf("unexpected result: have %v want %v", have, want), x, y, z)
}
ret = op(&y, &origX, &y, &z)
if ret != &y {
crash(op, "returned not the pointer receiver", x, y, z)
}
if y != *expected {
crash(op, "unexpected result", x, y, z)
}
ret = op(&z, &origX, &origY, &z)
if ret != &z {
crash(op, "returned not the pointer receiver", x, y, z)
}
if z != *expected {
crash(op, fmt.Sprintf("unexpected result: have %v want %v", z.ToBig(), expected), x, y, z)
}
}
func Fuzz(data []byte) int {
if len(data) < 32 {
return 0
}
switch {
case len(data) < 64:
return fuzzUnaryOp(data) // needs 32 byte
case len(data) < 96:
return fuzzBinaryOp(data) // needs 64 byte
case len(data) < 128:
return fuzzTernaryOp(data) // needs 96 byte
}
// Too large input
return -1
}
func fuzzUnaryOp(data []byte) int {
var x Int
x.SetBytes(data[0:32])
checkUnaryOp((*Int).Sqrt, (*big.Int).Sqrt, x)
return 1
}
func fuzzBinaryOp(data []byte) int {
var x, y Int
x.SetBytes(data[0:32])
y.SetBytes(data[32:])
if !y.IsZero() { // uDivrem
checkDualArgOp((*Int).Div, (*big.Int).Div, x, y)
checkDualArgOp((*Int).Mod, (*big.Int).Mod, x, y)
}
{ // opMul
checkDualArgOp((*Int).Mul, (*big.Int).Mul, x, y)
}
{ // opLsh
lsh := func(z, x, y *Int) *Int {
return z.Lsh(x, uint(y[0]))
}
bigLsh := func(z, x, y *big.Int) *big.Int {
n := uint(y.Uint64())
if n > 256 {
n = 256
}
return z.Lsh(x, n)
}
checkDualArgOp(lsh, bigLsh, x, y)
}
{ // opAdd
checkDualArgOp((*Int).Add, (*big.Int).Add, x, y)
}
{ // opSub
checkDualArgOp((*Int).Sub, (*big.Int).Sub, x, y)
}
return 1
}
func bigintMulMod(b1, b2, b3, b4 *big.Int) *big.Int {
return b1.Mod(big.NewInt(0).Mul(b2, b3), b4)
}
func intMulMod(f1, f2, f3, f4 *Int) *Int {
return f1.MulMod(f2, f3, f4)
}
func bigintAddMod(b1, b2, b3, b4 *big.Int) *big.Int {
return b1.Mod(big.NewInt(0).Add(b2, b3), b4)
}
func intAddMod(f1, f2, f3, f4 *Int) *Int {
return f1.AddMod(f2, f3, f4)
}
func bigintMulDiv(b1, b2, b3, b4 *big.Int) *big.Int {
b1.Mul(b2, b3)
return b1.Div(b1, b4)
}
func intMulDiv(f1, f2, f3, f4 *Int) *Int {
f1.MulDivOverflow(f2, f3, f4)
return f1
}
func fuzzTernaryOp(data []byte) int {
var x, y, z Int
x.SetBytes(data[:32])
y.SetBytes(data[32:64])
z.SetBytes(data[64:])
if z.IsZero() {
return 0
}
{ // mulMod
checkThreeArgOp(intMulMod, bigintMulMod, x, y, z)
}
{ // addMod
checkThreeArgOp(intAddMod, bigintAddMod, x, y, z)
}
{ // mulDiv
checkThreeArgOp(intMulDiv, bigintMulDiv, x, y, z)
}
return 1
}
// Test SetFromDecimal
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(data []byte) int {
if len(data) > 512 {
// Too large, makes no sense
return -1
}
if err := testSetFromDecForFuzzing(string(data)); err != nil {
panic(err)
}
return 1
}