2015-10-12 08:16:57 +00:00
|
|
|
// Copyright 2015 syzkaller project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package prog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2016-09-13 10:43:03 +00:00
|
|
|
"unsafe"
|
2015-10-12 08:16:57 +00:00
|
|
|
)
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// Maximum length of generated binary blobs inserted into the program.
|
2017-12-08 09:22:56 +00:00
|
|
|
const maxBlobLen = uint64(100 << 10)
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// Mutate program p.
|
|
|
|
//
|
|
|
|
// p: The program to mutate.
|
|
|
|
// rs: Random source.
|
|
|
|
// ncalls: The allowed maximum calls in mutated program.
|
|
|
|
// ct: ChoiceTable for syscalls.
|
|
|
|
// corpus: The entire corpus, including original program p.
|
2016-10-21 16:19:56 +00:00
|
|
|
func (p *Prog) Mutate(rs rand.Source, ncalls int, ct *ChoiceTable, corpus []*Prog) {
|
2017-09-14 17:25:01 +00:00
|
|
|
r := newRand(p.Target, rs)
|
2018-08-01 18:19:44 +00:00
|
|
|
ctx := &mutator{
|
|
|
|
p: p,
|
|
|
|
r: r,
|
|
|
|
ncalls: ncalls,
|
|
|
|
ct: ct,
|
|
|
|
corpus: corpus,
|
|
|
|
}
|
|
|
|
for stop, ok := false, false; !stop; stop = ok && r.oneOf(3) {
|
2017-02-01 15:27:13 +00:00
|
|
|
switch {
|
2018-02-24 13:33:36 +00:00
|
|
|
case r.oneOf(5):
|
|
|
|
// Not all calls have anything squashable,
|
|
|
|
// so this has lower priority in reality.
|
2018-08-01 18:19:44 +00:00
|
|
|
ok = ctx.squashAny()
|
2017-02-01 15:27:13 +00:00
|
|
|
case r.nOutOf(1, 100):
|
2018-08-01 18:19:44 +00:00
|
|
|
ok = ctx.splice()
|
2017-02-01 15:27:13 +00:00
|
|
|
case r.nOutOf(20, 31):
|
2018-08-01 18:19:44 +00:00
|
|
|
ok = ctx.insertCall()
|
2017-02-01 15:27:13 +00:00
|
|
|
case r.nOutOf(10, 11):
|
2018-08-01 18:19:44 +00:00
|
|
|
ok = ctx.mutateArg()
|
2017-02-01 15:27:13 +00:00
|
|
|
default:
|
2018-08-01 18:19:44 +00:00
|
|
|
ok = ctx.removeCall()
|
2016-10-21 16:19:56 +00:00
|
|
|
}
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
for _, c := range p.Calls {
|
2017-09-14 17:25:01 +00:00
|
|
|
p.Target.SanitizeCall(c)
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2018-08-01 17:45:15 +00:00
|
|
|
p.debugValidate()
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// Internal state required for performing mutations -- currently this matches
|
|
|
|
// the arguments passed to Mutate().
|
2018-08-01 18:19:44 +00:00
|
|
|
type mutator struct {
|
2019-06-06 16:44:34 +00:00
|
|
|
p *Prog // The program to mutate.
|
|
|
|
r *randGen // The randGen instance.
|
|
|
|
ncalls int // The allowed maximum calls in mutated program.
|
|
|
|
ct *ChoiceTable // ChoiceTable for syscalls.
|
|
|
|
corpus []*Prog // The entire corpus, including original program p.
|
2018-08-01 18:19:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// This function selects a random other program p0 out of the corpus, and
|
|
|
|
// mutates ctx.p as follows: preserve ctx.p's Calls up to a random index i
|
|
|
|
// (exclusive) concatenated with p0's calls from index i (inclusive).
|
2018-08-01 18:19:44 +00:00
|
|
|
func (ctx *mutator) splice() bool {
|
|
|
|
p, r := ctx.p, ctx.r
|
|
|
|
if len(ctx.corpus) == 0 || len(p.Calls) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
p0 := ctx.corpus[r.Intn(len(ctx.corpus))]
|
|
|
|
p0c := p0.Clone()
|
|
|
|
idx := r.Intn(len(p.Calls))
|
|
|
|
p.Calls = append(p.Calls[:idx], append(p0c.Calls, p.Calls[idx:]...)...)
|
|
|
|
for i := len(p.Calls) - 1; i >= ctx.ncalls; i-- {
|
|
|
|
p.removeCall(i)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// Picks a random complex pointer and squashes its arguments into an ANY.
|
|
|
|
// Subsequently, if the ANY contains blobs, mutates a random blob.
|
2018-08-01 18:19:44 +00:00
|
|
|
func (ctx *mutator) squashAny() bool {
|
|
|
|
p, r := ctx.p, ctx.r
|
|
|
|
complexPtrs := p.complexPtrs()
|
|
|
|
if len(complexPtrs) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ptr := complexPtrs[r.Intn(len(complexPtrs))]
|
|
|
|
if !p.Target.isAnyPtr(ptr.Type()) {
|
|
|
|
p.Target.squashPtr(ptr, true)
|
|
|
|
}
|
|
|
|
var blobs []*DataArg
|
|
|
|
var bases []*PointerArg
|
|
|
|
ForeachSubArg(ptr, func(arg Arg, ctx *ArgCtx) {
|
|
|
|
if data, ok := arg.(*DataArg); ok && arg.Type().Dir() != DirOut {
|
|
|
|
blobs = append(blobs, data)
|
|
|
|
bases = append(bases, ctx.Base)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if len(blobs) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// TODO(dvyukov): we probably want special mutation for ANY.
|
|
|
|
// E.g. merging adjacent ANYBLOBs (we don't create them,
|
|
|
|
// but they can appear in future); or replacing ANYRES
|
|
|
|
// with a blob (and merging it with adjacent blobs).
|
|
|
|
idx := r.Intn(len(blobs))
|
|
|
|
arg := blobs[idx]
|
|
|
|
base := bases[idx]
|
|
|
|
baseSize := base.Res.Size()
|
|
|
|
arg.data = mutateData(r, arg.Data(), 0, maxBlobLen)
|
|
|
|
// Update base pointer if size has increased.
|
|
|
|
if baseSize < base.Res.Size() {
|
|
|
|
s := analyze(ctx.ct, p, p.Calls[0])
|
|
|
|
newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)
|
|
|
|
*base = *newArg
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// Inserts a new call at a randomly chosen point (with bias towards the end of
|
|
|
|
// existing program). Does not insert a call if program already has ncalls.
|
2018-08-01 18:19:44 +00:00
|
|
|
func (ctx *mutator) insertCall() bool {
|
|
|
|
p, r := ctx.p, ctx.r
|
|
|
|
if len(p.Calls) >= ctx.ncalls {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
idx := r.biasedRand(len(p.Calls)+1, 5)
|
|
|
|
var c *Call
|
|
|
|
if idx < len(p.Calls) {
|
|
|
|
c = p.Calls[idx]
|
|
|
|
}
|
|
|
|
s := analyze(ctx.ct, p, c)
|
|
|
|
calls := r.generateCall(s, p)
|
|
|
|
p.insertBefore(c, calls)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// Removes a random call from program.
|
2018-08-01 18:19:44 +00:00
|
|
|
func (ctx *mutator) removeCall() bool {
|
|
|
|
p, r := ctx.p, ctx.r
|
|
|
|
if len(p.Calls) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
idx := r.Intn(len(p.Calls))
|
|
|
|
p.removeCall(idx)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// Mutate an argument of a random call.
|
2018-08-01 18:19:44 +00:00
|
|
|
func (ctx *mutator) mutateArg() bool {
|
|
|
|
p, r := ctx.p, ctx.r
|
|
|
|
if len(p.Calls) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c := p.Calls[r.Intn(len(p.Calls))]
|
|
|
|
if len(c.Args) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s := analyze(ctx.ct, p, c)
|
|
|
|
updateSizes := true
|
|
|
|
for stop, ok := false, false; !stop; stop = ok && r.oneOf(3) {
|
|
|
|
ok = true
|
|
|
|
ma := &mutationArgs{target: p.Target}
|
|
|
|
ForeachArg(c, ma.collectArg)
|
|
|
|
if len(ma.args) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
idx := r.Intn(len(ma.args))
|
|
|
|
arg, ctx := ma.args[idx], ma.ctxes[idx]
|
|
|
|
calls, ok1 := p.Target.mutateArg(r, s, arg, ctx, &updateSizes)
|
|
|
|
if !ok1 {
|
|
|
|
ok = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.insertBefore(c, calls)
|
|
|
|
if updateSizes {
|
|
|
|
p.Target.assignSizesCall(c)
|
|
|
|
}
|
|
|
|
p.Target.SanitizeCall(c)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-05-07 14:24:03 +00:00
|
|
|
func (target *Target) mutateArg(r *randGen, s *state, arg Arg, ctx ArgCtx, updateSizes *bool) ([]*Call, bool) {
|
2018-01-24 18:28:36 +00:00
|
|
|
var baseSize uint64
|
2018-02-18 12:49:48 +00:00
|
|
|
if ctx.Base != nil {
|
|
|
|
baseSize = ctx.Base.Res.Size()
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
calls, retry, preserve := arg.Type().mutate(r, s, arg, ctx)
|
|
|
|
if retry {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if preserve {
|
2018-01-24 18:28:36 +00:00
|
|
|
*updateSizes = false
|
2018-05-07 14:24:03 +00:00
|
|
|
}
|
|
|
|
// Update base pointer if size has increased.
|
|
|
|
if base := ctx.Base; base != nil && baseSize < base.Res.Size() {
|
|
|
|
newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)
|
|
|
|
replaceArg(base, newArg)
|
|
|
|
}
|
|
|
|
for _, c := range calls {
|
|
|
|
target.SanitizeCall(c)
|
|
|
|
}
|
|
|
|
return calls, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func regenerate(r *randGen, s *state, arg Arg) (calls []*Call, retry, preserve bool) {
|
|
|
|
var newArg Arg
|
|
|
|
newArg, calls = r.generateArg(s, arg.Type())
|
|
|
|
replaceArg(arg, newArg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func mutateInt(r *randGen, s *state, arg Arg) (calls []*Call, retry, preserve bool) {
|
|
|
|
if r.bin() {
|
|
|
|
return regenerate(r, s, arg)
|
|
|
|
}
|
|
|
|
a := arg.(*ConstArg)
|
|
|
|
switch {
|
|
|
|
case r.nOutOf(1, 3):
|
|
|
|
a.Val += uint64(r.Intn(4)) + 1
|
|
|
|
case r.nOutOf(1, 2):
|
|
|
|
a.Val -= uint64(r.Intn(4)) + 1
|
|
|
|
default:
|
|
|
|
a.Val ^= 1 << uint64(r.Intn(64))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *IntType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
return mutateInt(r, s, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FlagsType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
return mutateInt(r, s, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LenType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
if !r.mutateSize(arg.(*ConstArg), *ctx.Parent) {
|
|
|
|
retry = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
preserve = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ResourceType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
return regenerate(r, s, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *VmaType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
return regenerate(r, s, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ProcType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
return regenerate(r, s, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BufferType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
a := arg.(*DataArg)
|
|
|
|
switch t.Kind {
|
|
|
|
case BufferBlobRand, BufferBlobRange:
|
|
|
|
data := append([]byte{}, a.Data()...)
|
|
|
|
minLen, maxLen := uint64(0), maxBlobLen
|
|
|
|
if t.Kind == BufferBlobRange {
|
|
|
|
minLen, maxLen = t.RangeBegin, t.RangeEnd
|
|
|
|
}
|
|
|
|
a.data = mutateData(r, data, minLen, maxLen)
|
|
|
|
case BufferString:
|
|
|
|
data := append([]byte{}, a.Data()...)
|
|
|
|
if r.bin() {
|
2018-01-24 18:28:36 +00:00
|
|
|
minLen, maxLen := uint64(0), maxBlobLen
|
2018-05-07 14:24:03 +00:00
|
|
|
if t.TypeSize != 0 {
|
|
|
|
minLen, maxLen = t.TypeSize, t.TypeSize
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
|
|
|
a.data = mutateData(r, data, minLen, maxLen)
|
2018-05-07 14:24:03 +00:00
|
|
|
} else {
|
|
|
|
a.data = r.randString(s, t)
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
case BufferFilename:
|
|
|
|
a.data = []byte(r.filename(s, t))
|
|
|
|
case BufferText:
|
|
|
|
data := append([]byte{}, a.Data()...)
|
|
|
|
a.data = r.mutateText(t.Text, data)
|
|
|
|
default:
|
|
|
|
panic("unknown buffer kind")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ArrayType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
// TODO: swap elements of the array
|
|
|
|
a := arg.(*GroupArg)
|
|
|
|
count := uint64(0)
|
|
|
|
switch t.Kind {
|
|
|
|
case ArrayRandLen:
|
|
|
|
for count == uint64(len(a.Inner)) {
|
|
|
|
count = r.randArrayLen()
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
case ArrayRangeLen:
|
|
|
|
if t.RangeBegin == t.RangeEnd {
|
|
|
|
panic("trying to mutate fixed length array")
|
|
|
|
}
|
|
|
|
for count == uint64(len(a.Inner)) {
|
|
|
|
count = r.randRange(t.RangeBegin, t.RangeEnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count > uint64(len(a.Inner)) {
|
|
|
|
for count > uint64(len(a.Inner)) {
|
|
|
|
newArg, newCalls := r.generateArg(s, t.Type)
|
|
|
|
a.Inner = append(a.Inner, newArg)
|
|
|
|
calls = append(calls, newCalls...)
|
|
|
|
for _, c := range newCalls {
|
|
|
|
s.analyze(c)
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2015-12-23 12:47:45 +00:00
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
} else if count < uint64(len(a.Inner)) {
|
|
|
|
for _, arg := range a.Inner[count:] {
|
|
|
|
removeArg(arg)
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
a.Inner = a.Inner[:count]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *PtrType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
a := arg.(*PointerArg)
|
2018-08-30 21:17:47 +00:00
|
|
|
if r.oneOf(1000) {
|
|
|
|
removeArg(a.Res)
|
|
|
|
index := r.rand(len(r.target.SpecialPointers))
|
|
|
|
newArg := MakeSpecialPointerArg(t, index)
|
|
|
|
replaceArg(arg, newArg)
|
|
|
|
return
|
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
newArg := r.allocAddr(s, t, a.Res.Size(), a.Res)
|
|
|
|
replaceArg(arg, newArg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *StructType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
gen := r.target.SpecialTypes[t.Name()]
|
|
|
|
if gen == nil {
|
|
|
|
panic("bad arg returned by mutationArgs: StructType")
|
|
|
|
}
|
|
|
|
var newArg Arg
|
|
|
|
newArg, calls = gen(&Gen{r, s}, t, arg)
|
|
|
|
a := arg.(*GroupArg)
|
|
|
|
for i, f := range newArg.(*GroupArg).Inner {
|
|
|
|
replaceArg(a.Inner[i], f)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UnionType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
if gen := r.target.SpecialTypes[t.Name()]; gen != nil {
|
2018-01-24 18:28:36 +00:00
|
|
|
var newArg Arg
|
|
|
|
newArg, calls = gen(&Gen{r, s}, t, arg)
|
2018-05-07 14:24:03 +00:00
|
|
|
replaceArg(arg, newArg)
|
|
|
|
} else {
|
|
|
|
a := arg.(*UnionArg)
|
|
|
|
current := -1
|
|
|
|
for i, option := range t.Fields {
|
|
|
|
if a.Option.Type().FieldName() == option.FieldName() {
|
|
|
|
current = i
|
|
|
|
break
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
if current == -1 {
|
|
|
|
panic("can't find current option in union")
|
|
|
|
}
|
|
|
|
newIdx := r.Intn(len(t.Fields) - 1)
|
|
|
|
if newIdx >= current {
|
|
|
|
newIdx++
|
|
|
|
}
|
|
|
|
optType := t.Fields[newIdx]
|
|
|
|
removeArg(a.Option)
|
|
|
|
var newOpt Arg
|
|
|
|
newOpt, calls = r.generateArg(s, optType)
|
|
|
|
replaceArg(arg, MakeUnionArg(t, newOpt))
|
2016-10-21 16:24:33 +00:00
|
|
|
}
|
2018-05-07 14:24:03 +00:00
|
|
|
return
|
|
|
|
}
|
2016-10-21 16:24:33 +00:00
|
|
|
|
2018-05-07 14:24:03 +00:00
|
|
|
func (t *CsumType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
panic("CsumType can't be mutated")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ConstType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {
|
|
|
|
panic("ConstType can't be mutated")
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 13:45:32 +00:00
|
|
|
type mutationArgs struct {
|
|
|
|
target *Target
|
|
|
|
args []Arg
|
|
|
|
ctxes []ArgCtx
|
|
|
|
ignoreSpecial bool
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 13:45:32 +00:00
|
|
|
func (ma *mutationArgs) collectArg(arg Arg, ctx *ArgCtx) {
|
|
|
|
ignoreSpecial := ma.ignoreSpecial
|
|
|
|
ma.ignoreSpecial = false
|
2018-01-24 18:28:36 +00:00
|
|
|
switch typ := arg.Type().(type) {
|
|
|
|
case *StructType:
|
2018-02-18 13:45:32 +00:00
|
|
|
if ma.target.SpecialTypes[typ.Name()] == nil || ignoreSpecial {
|
|
|
|
return // For structs only individual fields are updated.
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
|
|
|
// These special structs are mutated as a whole.
|
2018-02-18 13:45:32 +00:00
|
|
|
ctx.Stop = true
|
2018-01-24 18:28:36 +00:00
|
|
|
case *UnionType:
|
2018-02-18 13:45:32 +00:00
|
|
|
if ma.target.SpecialTypes[typ.Name()] == nil && len(typ.Fields) == 1 || ignoreSpecial {
|
|
|
|
return
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2018-02-18 13:45:32 +00:00
|
|
|
ctx.Stop = true
|
2018-01-24 18:28:36 +00:00
|
|
|
case *ArrayType:
|
|
|
|
// Don't mutate fixed-size arrays.
|
|
|
|
if typ.Kind == ArrayRangeLen && typ.RangeBegin == typ.RangeEnd {
|
2018-02-18 13:45:32 +00:00
|
|
|
return
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
|
|
|
case *CsumType:
|
2018-02-18 13:45:32 +00:00
|
|
|
return // Checksum is updated when the checksummed data changes.
|
2018-01-24 18:28:36 +00:00
|
|
|
case *ConstType:
|
2018-02-18 13:45:32 +00:00
|
|
|
return // Well, this is const.
|
2018-01-24 18:28:36 +00:00
|
|
|
case *BufferType:
|
|
|
|
if typ.Kind == BufferString && len(typ.Values) == 1 {
|
2018-02-18 13:45:32 +00:00
|
|
|
return // string const
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2018-02-19 18:35:04 +00:00
|
|
|
case *PtrType:
|
2018-08-30 21:17:47 +00:00
|
|
|
if arg.(*PointerArg).IsSpecial() {
|
2018-02-19 18:35:04 +00:00
|
|
|
// TODO: we ought to mutate this, but we don't have code for this yet.
|
|
|
|
return
|
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
|
|
|
typ := arg.Type()
|
2018-02-18 12:49:48 +00:00
|
|
|
if typ == nil || typ.Dir() == DirOut || !typ.Varlen() && typ.Size() == 0 {
|
2018-02-18 13:45:32 +00:00
|
|
|
return
|
2018-01-24 18:28:36 +00:00
|
|
|
}
|
2018-02-18 13:45:32 +00:00
|
|
|
ma.args = append(ma.args, arg)
|
|
|
|
ma.ctxes = append(ma.ctxes, *ctx)
|
2016-09-13 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 07:46:43 +00:00
|
|
|
func mutateData(r *randGen, data []byte, minLen, maxLen uint64) []byte {
|
2018-05-04 16:03:46 +00:00
|
|
|
for stop := false; !stop; stop = stop && r.oneOf(3) {
|
|
|
|
f := mutateDataFuncs[r.Intn(len(mutateDataFuncs))]
|
|
|
|
data, stop = f(r, data, minLen, maxLen)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2019-06-06 16:44:34 +00:00
|
|
|
// The maximum delta for integer mutations.
|
|
|
|
const maxDelta = 35
|
2018-05-04 16:03:46 +00:00
|
|
|
|
|
|
|
var mutateDataFuncs = [...]func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool){
|
|
|
|
// TODO(dvyukov): duplicate part of data.
|
|
|
|
// Flip bit in byte.
|
|
|
|
func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return data, false
|
|
|
|
}
|
|
|
|
byt := r.Intn(len(data))
|
|
|
|
bit := r.Intn(8)
|
|
|
|
data[byt] ^= 1 << uint(bit)
|
|
|
|
return data, true
|
|
|
|
},
|
|
|
|
// Insert random bytes.
|
|
|
|
func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {
|
2019-07-26 08:43:08 +00:00
|
|
|
if len(data) == 0 || uint64(len(data)) >= maxLen {
|
2018-05-04 16:03:46 +00:00
|
|
|
return data, false
|
|
|
|
}
|
|
|
|
n := r.Intn(16) + 1
|
|
|
|
if r := int(maxLen) - len(data); n > r {
|
|
|
|
n = r
|
|
|
|
}
|
|
|
|
pos := r.Intn(len(data))
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data = append(data, 0)
|
|
|
|
}
|
|
|
|
copy(data[pos+n:], data[pos:])
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data[pos+i] = byte(r.Int31())
|
|
|
|
}
|
2018-06-12 17:09:26 +00:00
|
|
|
if uint64(len(data)) > maxLen || r.bin() {
|
2018-05-04 16:03:46 +00:00
|
|
|
data = data[:len(data)-n] // preserve original length
|
|
|
|
}
|
|
|
|
return data, true
|
|
|
|
},
|
|
|
|
// Remove bytes.
|
|
|
|
func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {
|
2018-06-12 17:09:26 +00:00
|
|
|
if len(data) == 0 {
|
2018-05-04 16:03:46 +00:00
|
|
|
return data, false
|
|
|
|
}
|
|
|
|
n := r.Intn(16) + 1
|
|
|
|
if n > len(data) {
|
|
|
|
n = len(data)
|
|
|
|
}
|
|
|
|
pos := 0
|
|
|
|
if n < len(data) {
|
|
|
|
pos = r.Intn(len(data) - n)
|
|
|
|
}
|
|
|
|
copy(data[pos:], data[pos+n:])
|
|
|
|
data = data[:len(data)-n]
|
2018-06-12 17:09:26 +00:00
|
|
|
if uint64(len(data)) < minLen || r.bin() {
|
2018-02-24 13:33:36 +00:00
|
|
|
for i := 0; i < n; i++ {
|
2018-05-04 16:03:46 +00:00
|
|
|
data = append(data, 0) // preserve original length
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-05-04 16:03:46 +00:00
|
|
|
}
|
|
|
|
return data, true
|
|
|
|
},
|
|
|
|
// Append a bunch of bytes.
|
|
|
|
func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {
|
|
|
|
if uint64(len(data)) >= maxLen {
|
|
|
|
return data, false
|
|
|
|
}
|
|
|
|
const max = 256
|
|
|
|
n := max - r.biasedRand(max, 10)
|
|
|
|
if r := int(maxLen) - len(data); n > r {
|
|
|
|
n = r
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
data = append(data, byte(r.rand(256)))
|
|
|
|
}
|
|
|
|
return data, true
|
|
|
|
},
|
|
|
|
// Replace int8/int16/int32/int64 with a random value.
|
|
|
|
func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {
|
2018-05-07 12:24:00 +00:00
|
|
|
width := 1 << uint(r.Intn(4))
|
|
|
|
if len(data) < width {
|
|
|
|
return data, false
|
2018-05-04 16:03:46 +00:00
|
|
|
}
|
2018-05-07 12:24:00 +00:00
|
|
|
i := r.Intn(len(data) - width + 1)
|
|
|
|
storeInt(data[i:], r.Uint64(), width)
|
2018-05-04 16:03:46 +00:00
|
|
|
return data, true
|
|
|
|
},
|
|
|
|
// Add/subtract from an int8/int16/int32/int64.
|
|
|
|
func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {
|
2018-05-07 12:24:00 +00:00
|
|
|
width := 1 << uint(r.Intn(4))
|
|
|
|
if len(data) < width {
|
|
|
|
return data, false
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - width + 1)
|
|
|
|
v := loadInt(data[i:], width)
|
2019-06-06 16:44:34 +00:00
|
|
|
delta := r.rand(2*maxDelta+1) - maxDelta
|
2018-05-07 12:24:00 +00:00
|
|
|
if delta == 0 {
|
|
|
|
delta = 1
|
|
|
|
}
|
|
|
|
if r.oneOf(10) {
|
|
|
|
v = swapInt(v, width)
|
|
|
|
v += delta
|
|
|
|
v = swapInt(v, width)
|
|
|
|
} else {
|
|
|
|
v += delta
|
2017-01-20 22:55:25 +00:00
|
|
|
}
|
2018-05-07 12:24:00 +00:00
|
|
|
storeInt(data[i:], v, width)
|
2018-05-04 16:03:46 +00:00
|
|
|
return data, true
|
|
|
|
},
|
|
|
|
// Set int8/int16/int32/int64 to an interesting value.
|
|
|
|
func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {
|
2018-05-07 12:24:00 +00:00
|
|
|
width := 1 << uint(r.Intn(4))
|
|
|
|
if len(data) < width {
|
|
|
|
return data, false
|
|
|
|
}
|
|
|
|
i := r.Intn(len(data) - width + 1)
|
|
|
|
value := r.randInt()
|
|
|
|
if r.oneOf(10) {
|
|
|
|
value = swap64(value)
|
2018-05-04 16:03:46 +00:00
|
|
|
}
|
2018-05-07 12:24:00 +00:00
|
|
|
storeInt(data[i:], value, width)
|
2018-05-04 16:03:46 +00:00
|
|
|
return data, true
|
|
|
|
},
|
2015-10-12 08:16:57 +00:00
|
|
|
}
|
2018-01-24 18:28:36 +00:00
|
|
|
|
|
|
|
func swap16(v uint16) uint16 {
|
|
|
|
v0 := byte(v >> 0)
|
|
|
|
v1 := byte(v >> 8)
|
|
|
|
v = 0
|
|
|
|
v |= uint16(v1) << 0
|
|
|
|
v |= uint16(v0) << 8
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func swap32(v uint32) uint32 {
|
|
|
|
v0 := byte(v >> 0)
|
|
|
|
v1 := byte(v >> 8)
|
|
|
|
v2 := byte(v >> 16)
|
|
|
|
v3 := byte(v >> 24)
|
|
|
|
v = 0
|
|
|
|
v |= uint32(v3) << 0
|
|
|
|
v |= uint32(v2) << 8
|
|
|
|
v |= uint32(v1) << 16
|
|
|
|
v |= uint32(v0) << 24
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func swap64(v uint64) uint64 {
|
|
|
|
v0 := byte(v >> 0)
|
|
|
|
v1 := byte(v >> 8)
|
|
|
|
v2 := byte(v >> 16)
|
|
|
|
v3 := byte(v >> 24)
|
|
|
|
v4 := byte(v >> 32)
|
|
|
|
v5 := byte(v >> 40)
|
|
|
|
v6 := byte(v >> 48)
|
|
|
|
v7 := byte(v >> 56)
|
|
|
|
v = 0
|
|
|
|
v |= uint64(v7) << 0
|
|
|
|
v |= uint64(v6) << 8
|
|
|
|
v |= uint64(v5) << 16
|
|
|
|
v |= uint64(v4) << 24
|
|
|
|
v |= uint64(v3) << 32
|
|
|
|
v |= uint64(v2) << 40
|
|
|
|
v |= uint64(v1) << 48
|
|
|
|
v |= uint64(v0) << 56
|
|
|
|
return v
|
|
|
|
}
|
2018-04-01 13:28:01 +00:00
|
|
|
|
2018-05-07 12:24:00 +00:00
|
|
|
func swapInt(v uint64, size int) uint64 {
|
2018-04-01 13:28:01 +00:00
|
|
|
switch size {
|
2018-05-07 12:24:00 +00:00
|
|
|
case 1:
|
|
|
|
return v
|
|
|
|
case 2:
|
2018-04-01 13:28:01 +00:00
|
|
|
return uint64(swap16(uint16(v)))
|
2018-05-07 12:24:00 +00:00
|
|
|
case 4:
|
|
|
|
return uint64(swap32(uint32(v)))
|
2018-04-01 13:28:01 +00:00
|
|
|
case 8:
|
2018-05-07 12:24:00 +00:00
|
|
|
return swap64(v)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("swapInt: bad size %v", size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadInt(data []byte, size int) uint64 {
|
|
|
|
p := unsafe.Pointer(&data[0])
|
|
|
|
switch size {
|
|
|
|
case 1:
|
|
|
|
return uint64(*(*uint8)(p))
|
|
|
|
case 2:
|
|
|
|
return uint64(*(*uint16)(p))
|
|
|
|
case 4:
|
|
|
|
return uint64(*(*uint32)(p))
|
|
|
|
case 8:
|
|
|
|
return *(*uint64)(p)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("loadInt: bad size %v", size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeInt(data []byte, v uint64, size int) {
|
|
|
|
p := unsafe.Pointer(&data[0])
|
|
|
|
switch size {
|
|
|
|
case 1:
|
|
|
|
*(*uint8)(p) = uint8(v)
|
|
|
|
case 2:
|
|
|
|
*(*uint16)(p) = uint16(v)
|
|
|
|
case 4:
|
|
|
|
*(*uint32)(p) = uint32(v)
|
|
|
|
case 8:
|
|
|
|
*(*uint64)(p) = v
|
2018-04-01 13:28:01 +00:00
|
|
|
default:
|
2018-05-07 12:24:00 +00:00
|
|
|
panic(fmt.Sprintf("storeInt: bad size %v", size))
|
2018-04-01 13:28:01 +00:00
|
|
|
}
|
|
|
|
}
|