This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit_test.go
167 lines (140 loc) · 3.56 KB
/
init_test.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
package num
import (
"flag"
"fmt"
"log"
"math/big"
"math/rand"
"os"
"regexp"
"strings"
"testing"
"time"
)
var (
fuzzIterations = fuzzDefaultIterations
fuzzOpsActive = allFuzzOps
fuzzTypesActive = allFuzzTypes
fuzzSeed int64
globalRNG *rand.Rand
)
func TestMain(m *testing.M) {
testing.Init()
var ops StringList
var types StringList
flag.IntVar(&fuzzIterations, "num.fuzziter", fuzzIterations, "Number of iterations to fuzz each op")
flag.Int64Var(&fuzzSeed, "num.fuzzseed", fuzzSeed, "Seed the RNG (0 == current nanotime)")
flag.Var(&ops, "num.fuzzop", "Fuzz op to run (can pass multiple times, or a comma separated list)")
flag.Var(&types, "num.fuzztype", "Fuzz type (u128, i128) (can pass multiple)")
flag.Parse()
if fuzzSeed == 0 {
fuzzSeed = time.Now().UnixNano()
}
globalRNG = rand.New(rand.NewSource(fuzzSeed))
if len(ops) > 0 {
fuzzOpsActive = nil
for _, op := range ops {
fuzzOpsActive = append(fuzzOpsActive, fuzzOp(op))
}
}
if len(types) > 0 {
fuzzTypesActive = nil
for _, t := range types {
fuzzTypesActive = append(fuzzTypesActive, fuzzType(t))
}
}
log.Println("integer sz", intSize)
log.Println("rando seed (-num.fuzzseed):", fuzzSeed) // classic rando!
log.Println("fuzz types (-num.fuzztype):", fuzzTypesActive)
log.Println("iterations (-num.fuzziter):", fuzzIterations)
log.Println("active ops (-num.fuzzop) :", fuzzOpsActive)
code := m.Run()
os.Exit(code)
}
var trimFloatPattern = regexp.MustCompile(`(\.0+$|(\.\d+[1-9])\0+$)`)
func cleanFloatStr(str string) string {
return trimFloatPattern.ReplaceAllString(str, "$2")
}
func accU128FromBigInt(b *big.Int) U128 {
u, acc := U128FromBigInt(b)
if !acc {
panic(fmt.Errorf("num: inaccurate conversion to U128 in fuzz tester for %s", b))
}
return u
}
func accI128FromBigInt(b *big.Int) I128 {
i, acc := I128FromBigInt(b)
if !acc {
panic(fmt.Errorf("num: inaccurate conversion to I128 in fuzz tester for %s", b))
}
return i
}
func accU64FromBigInt(b *big.Int) uint64 {
if !b.IsUint64() {
panic(fmt.Errorf("num: inaccurate conversion to U64 in fuzz tester for %s", b))
}
return b.Uint64()
}
func accI64FromBigInt(b *big.Int) int64 {
if !b.IsInt64() {
panic(fmt.Errorf("num: inaccurate conversion to I64 in fuzz tester for %s", b))
}
return b.Int64()
}
type StringList []string
func (s StringList) Strings() []string { return s }
func (s *StringList) String() string {
if s == nil {
return ""
}
return strings.Join(*s, ",")
}
func (s *StringList) Set(v string) error {
vs := strings.Split(v, ",")
for _, vi := range vs {
vi = strings.TrimSpace(vi)
if vi != "" {
*s = append(*s, vi)
}
}
return nil
}
func randomBigU128(rng *rand.Rand) *big.Int {
if rng == nil {
rng = globalRNG
}
var v = new(big.Int)
bits := rng.Intn(129) - 1 // 128 bits, +1 for "0 bits"
if bits < 0 {
return v // "-1 bits" == "0"
} else if bits <= 64 {
v = v.Rand(rng, maxBigUint64)
} else {
v = v.Rand(rng, maxBigU128)
}
v.And(v, masks[bits])
v.SetBit(v, bits, 1)
return v
}
func simulateBigU128Overflow(rb *big.Int) *big.Int {
for rb.Cmp(wrapBigU128) >= 0 {
rb = new(big.Int).And(rb, maxBigU128)
}
return rb
}
func simulateBigI128Overflow(rb *big.Int) *big.Int {
if rb.Cmp(maxBigI128) > 0 {
// simulate overflow
gap := new(big.Int)
gap.Sub(rb, minBigI128)
r := new(big.Int).Rem(gap, wrapBigU128)
rb = new(big.Int).Add(r, minBigI128)
} else if rb.Cmp(minBigI128) < 0 {
// simulate underflow
gap := new(big.Int).Set(rb)
gap.Sub(maxBigI128, gap)
r := new(big.Int).Rem(gap, wrapBigU128)
rb = new(big.Int).Sub(maxBigI128, r)
}
return rb
}