aboutsummaryrefslogtreecommitdiff
path: root/src/internal/fuzz
diff options
context:
space:
mode:
authorKatie Hockman <katie@golang.org>2021-04-07 16:28:13 -0400
committerKatie Hockman <katie@golang.org>2021-04-09 16:22:38 +0000
commit2f3bc725fe80e1ba3ef726e412c212d533a43684 (patch)
treeff1fc1fa76c1fd3eeb7211e4b8c89bb6a0b180f2 /src/internal/fuzz
parent06a7c848d9e3c2804e6d84aeaf777e76df4e5bea (diff)
downloadgo-2f3bc725fe80e1ba3ef726e412c212d533a43684.tar.xz
[dev.fuzz] internal/fuzz: add mutator for int types
Assuming this works, will follow up with another CL that mutates other types. Change-Id: Id61acaacd56ca41e3be52e400f8f768672313bbb Reviewed-on: https://go-review.googlesource.com/c/go/+/308169 Trust: Katie Hockman <katie@golang.org> Trust: Jay Conrod <jayconrod@google.com> Run-TryBot: Katie Hockman <katie@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/internal/fuzz')
-rw-r--r--src/internal/fuzz/mutator.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/internal/fuzz/mutator.go b/src/internal/fuzz/mutator.go
index aa72972147..e4ee2f44ea 100644
--- a/src/internal/fuzz/mutator.go
+++ b/src/internal/fuzz/mutator.go
@@ -7,6 +7,7 @@ package fuzz
import (
"encoding/binary"
"fmt"
+ "math"
"reflect"
"unsafe"
)
@@ -75,11 +76,53 @@ func (m *mutator) mutate(vals []interface{}, maxBytes int) {
}
m.mutateBytes(&v)
vals[i] = v
+ case int8:
+ vals[i] = int8(m.mutateInt(int64(v), math.MaxInt8))
+ case int16:
+ vals[i] = int16(m.mutateInt(int64(v), math.MaxInt16))
+ case int32:
+ vals[i] = int32(m.mutateInt(int64(v), math.MaxInt32))
+ case int64:
+ vals[i] = m.mutateInt(v, int64(maxInt))
+ case int:
+ vals[i] = int(m.mutateInt(int64(v), int64(maxInt)))
default:
panic(fmt.Sprintf("type not supported for mutating: %T", vals[i]))
}
}
+func (m *mutator) mutateInt(v, maxValue int64) int64 {
+ numIters := 1 + m.r.exp2()
+ var max int64
+ for iter := 0; iter < numIters; iter++ {
+ switch m.rand(2) {
+ case 0:
+ // Add a random number
+ if v >= maxValue {
+ continue
+ }
+ max = 100
+ if v > 0 && maxValue-v < max {
+ // Don't let v exceed maxValue
+ max = maxValue - v
+ }
+ v += int64(m.rand(int(max)))
+ case 1:
+ // Subtract a random number
+ if v <= -maxValue {
+ continue
+ }
+ max = 100
+ if v < 0 && maxValue+v < max {
+ // Don't let v drop below -maxValue
+ max = maxValue + v
+ }
+ v -= int64(m.rand(int(max)))
+ }
+ }
+ return v
+}
+
func (m *mutator) mutateBytes(ptrB *[]byte) {
b := *ptrB
defer func() {
@@ -267,6 +310,11 @@ var (
interesting32 = []int32{-2147483648, -100663046, -32769, 32768, 65535, 65536, 100663045, 2147483647}
)
+const (
+ maxUint = ^uint(0)
+ maxInt = maxUint >> 1
+)
+
func init() {
for _, v := range interesting8 {
interesting16 = append(interesting16, int16(v))